]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Take account of periodic fringe bitmap's dependency on y-position in redrawing.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 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 {
4646 if (STRINGP (it->string))
4647 pos_byte = string_char_to_byte (it->string, start);
4648 else
4649 pos_byte = CHAR_TO_BYTE (start);
4650 }
4651 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4652 prop, string);
4653
4654 if (it->cmp_it.id >= 0)
4655 {
4656 it->cmp_it.ch = -1;
4657 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4658 it->cmp_it.nglyphs = -1;
4659 }
4660 }
4661
4662 return HANDLED_NORMALLY;
4663 }
4664
4665
4666 \f
4667 /***********************************************************************
4668 Overlay strings
4669 ***********************************************************************/
4670
4671 /* The following structure is used to record overlay strings for
4672 later sorting in load_overlay_strings. */
4673
4674 struct overlay_entry
4675 {
4676 Lisp_Object overlay;
4677 Lisp_Object string;
4678 int priority;
4679 int after_string_p;
4680 };
4681
4682
4683 /* Set up iterator IT from overlay strings at its current position.
4684 Called from handle_stop. */
4685
4686 static enum prop_handled
4687 handle_overlay_change (it)
4688 struct it *it;
4689 {
4690 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4691 return HANDLED_RECOMPUTE_PROPS;
4692 else
4693 return HANDLED_NORMALLY;
4694 }
4695
4696
4697 /* Set up the next overlay string for delivery by IT, if there is an
4698 overlay string to deliver. Called by set_iterator_to_next when the
4699 end of the current overlay string is reached. If there are more
4700 overlay strings to display, IT->string and
4701 IT->current.overlay_string_index are set appropriately here.
4702 Otherwise IT->string is set to nil. */
4703
4704 static void
4705 next_overlay_string (it)
4706 struct it *it;
4707 {
4708 ++it->current.overlay_string_index;
4709 if (it->current.overlay_string_index == it->n_overlay_strings)
4710 {
4711 /* No more overlay strings. Restore IT's settings to what
4712 they were before overlay strings were processed, and
4713 continue to deliver from current_buffer. */
4714
4715 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4716 pop_it (it);
4717 xassert (it->sp > 0
4718 || (NILP (it->string)
4719 && it->method == GET_FROM_BUFFER
4720 && it->stop_charpos >= BEGV
4721 && it->stop_charpos <= it->end_charpos));
4722 it->current.overlay_string_index = -1;
4723 it->n_overlay_strings = 0;
4724 it->overlay_strings_charpos = -1;
4725
4726 /* If we're at the end of the buffer, record that we have
4727 processed the overlay strings there already, so that
4728 next_element_from_buffer doesn't try it again. */
4729 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4730 it->overlay_strings_at_end_processed_p = 1;
4731 }
4732 else
4733 {
4734 /* There are more overlay strings to process. If
4735 IT->current.overlay_string_index has advanced to a position
4736 where we must load IT->overlay_strings with more strings, do
4737 it. We must load at the IT->overlay_strings_charpos where
4738 IT->n_overlay_strings was originally computed; when invisible
4739 text is present, this might not be IT_CHARPOS (Bug#7016). */
4740 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4741
4742 if (it->current.overlay_string_index && i == 0)
4743 load_overlay_strings (it, it->overlay_strings_charpos);
4744
4745 /* Initialize IT to deliver display elements from the overlay
4746 string. */
4747 it->string = it->overlay_strings[i];
4748 it->multibyte_p = STRING_MULTIBYTE (it->string);
4749 SET_TEXT_POS (it->current.string_pos, 0, 0);
4750 it->method = GET_FROM_STRING;
4751 it->stop_charpos = 0;
4752 if (it->cmp_it.stop_pos >= 0)
4753 it->cmp_it.stop_pos = 0;
4754 }
4755
4756 CHECK_IT (it);
4757 }
4758
4759
4760 /* Compare two overlay_entry structures E1 and E2. Used as a
4761 comparison function for qsort in load_overlay_strings. Overlay
4762 strings for the same position are sorted so that
4763
4764 1. All after-strings come in front of before-strings, except
4765 when they come from the same overlay.
4766
4767 2. Within after-strings, strings are sorted so that overlay strings
4768 from overlays with higher priorities come first.
4769
4770 2. Within before-strings, strings are sorted so that overlay
4771 strings from overlays with higher priorities come last.
4772
4773 Value is analogous to strcmp. */
4774
4775
4776 static int
4777 compare_overlay_entries (e1, e2)
4778 void *e1, *e2;
4779 {
4780 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4781 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4782 int result;
4783
4784 if (entry1->after_string_p != entry2->after_string_p)
4785 {
4786 /* Let after-strings appear in front of before-strings if
4787 they come from different overlays. */
4788 if (EQ (entry1->overlay, entry2->overlay))
4789 result = entry1->after_string_p ? 1 : -1;
4790 else
4791 result = entry1->after_string_p ? -1 : 1;
4792 }
4793 else if (entry1->after_string_p)
4794 /* After-strings sorted in order of decreasing priority. */
4795 result = entry2->priority - entry1->priority;
4796 else
4797 /* Before-strings sorted in order of increasing priority. */
4798 result = entry1->priority - entry2->priority;
4799
4800 return result;
4801 }
4802
4803
4804 /* Load the vector IT->overlay_strings with overlay strings from IT's
4805 current buffer position, or from CHARPOS if that is > 0. Set
4806 IT->n_overlays to the total number of overlay strings found.
4807
4808 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4809 a time. On entry into load_overlay_strings,
4810 IT->current.overlay_string_index gives the number of overlay
4811 strings that have already been loaded by previous calls to this
4812 function.
4813
4814 IT->add_overlay_start contains an additional overlay start
4815 position to consider for taking overlay strings from, if non-zero.
4816 This position comes into play when the overlay has an `invisible'
4817 property, and both before and after-strings. When we've skipped to
4818 the end of the overlay, because of its `invisible' property, we
4819 nevertheless want its before-string to appear.
4820 IT->add_overlay_start will contain the overlay start position
4821 in this case.
4822
4823 Overlay strings are sorted so that after-string strings come in
4824 front of before-string strings. Within before and after-strings,
4825 strings are sorted by overlay priority. See also function
4826 compare_overlay_entries. */
4827
4828 static void
4829 load_overlay_strings (it, charpos)
4830 struct it *it;
4831 int charpos;
4832 {
4833 extern Lisp_Object Qwindow, Qpriority;
4834 Lisp_Object overlay, window, str, invisible;
4835 struct Lisp_Overlay *ov;
4836 int start, end;
4837 int size = 20;
4838 int n = 0, i, j, invis_p;
4839 struct overlay_entry *entries
4840 = (struct overlay_entry *) alloca (size * sizeof *entries);
4841
4842 if (charpos <= 0)
4843 charpos = IT_CHARPOS (*it);
4844
4845 /* Append the overlay string STRING of overlay OVERLAY to vector
4846 `entries' which has size `size' and currently contains `n'
4847 elements. AFTER_P non-zero means STRING is an after-string of
4848 OVERLAY. */
4849 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4850 do \
4851 { \
4852 Lisp_Object priority; \
4853 \
4854 if (n == size) \
4855 { \
4856 int new_size = 2 * size; \
4857 struct overlay_entry *old = entries; \
4858 entries = \
4859 (struct overlay_entry *) alloca (new_size \
4860 * sizeof *entries); \
4861 bcopy (old, entries, size * sizeof *entries); \
4862 size = new_size; \
4863 } \
4864 \
4865 entries[n].string = (STRING); \
4866 entries[n].overlay = (OVERLAY); \
4867 priority = Foverlay_get ((OVERLAY), Qpriority); \
4868 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4869 entries[n].after_string_p = (AFTER_P); \
4870 ++n; \
4871 } \
4872 while (0)
4873
4874 /* Process overlay before the overlay center. */
4875 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4876 {
4877 XSETMISC (overlay, ov);
4878 xassert (OVERLAYP (overlay));
4879 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4880 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4881
4882 if (end < charpos)
4883 break;
4884
4885 /* Skip this overlay if it doesn't start or end at IT's current
4886 position. */
4887 if (end != charpos && start != charpos)
4888 continue;
4889
4890 /* Skip this overlay if it doesn't apply to IT->w. */
4891 window = Foverlay_get (overlay, Qwindow);
4892 if (WINDOWP (window) && XWINDOW (window) != it->w)
4893 continue;
4894
4895 /* If the text ``under'' the overlay is invisible, both before-
4896 and after-strings from this overlay are visible; start and
4897 end position are indistinguishable. */
4898 invisible = Foverlay_get (overlay, Qinvisible);
4899 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4900
4901 /* If overlay has a non-empty before-string, record it. */
4902 if ((start == charpos || (end == charpos && invis_p))
4903 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4904 && SCHARS (str))
4905 RECORD_OVERLAY_STRING (overlay, str, 0);
4906
4907 /* If overlay has a non-empty after-string, record it. */
4908 if ((end == charpos || (start == charpos && invis_p))
4909 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4910 && SCHARS (str))
4911 RECORD_OVERLAY_STRING (overlay, str, 1);
4912 }
4913
4914 /* Process overlays after the overlay center. */
4915 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4916 {
4917 XSETMISC (overlay, ov);
4918 xassert (OVERLAYP (overlay));
4919 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4920 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4921
4922 if (start > charpos)
4923 break;
4924
4925 /* Skip this overlay if it doesn't start or end at IT's current
4926 position. */
4927 if (end != charpos && start != charpos)
4928 continue;
4929
4930 /* Skip this overlay if it doesn't apply to IT->w. */
4931 window = Foverlay_get (overlay, Qwindow);
4932 if (WINDOWP (window) && XWINDOW (window) != it->w)
4933 continue;
4934
4935 /* If the text ``under'' the overlay is invisible, it has a zero
4936 dimension, and both before- and after-strings apply. */
4937 invisible = Foverlay_get (overlay, Qinvisible);
4938 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4939
4940 /* If overlay has a non-empty before-string, record it. */
4941 if ((start == charpos || (end == charpos && invis_p))
4942 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4943 && SCHARS (str))
4944 RECORD_OVERLAY_STRING (overlay, str, 0);
4945
4946 /* If overlay has a non-empty after-string, record it. */
4947 if ((end == charpos || (start == charpos && invis_p))
4948 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4949 && SCHARS (str))
4950 RECORD_OVERLAY_STRING (overlay, str, 1);
4951 }
4952
4953 #undef RECORD_OVERLAY_STRING
4954
4955 /* Sort entries. */
4956 if (n > 1)
4957 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4958
4959 /* Record number of overlay strings, and where we computed it. */
4960 it->n_overlay_strings = n;
4961 it->overlay_strings_charpos = charpos;
4962
4963 /* IT->current.overlay_string_index is the number of overlay strings
4964 that have already been consumed by IT. Copy some of the
4965 remaining overlay strings to IT->overlay_strings. */
4966 i = 0;
4967 j = it->current.overlay_string_index;
4968 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4969 {
4970 it->overlay_strings[i] = entries[j].string;
4971 it->string_overlays[i++] = entries[j++].overlay;
4972 }
4973
4974 CHECK_IT (it);
4975 }
4976
4977
4978 /* Get the first chunk of overlay strings at IT's current buffer
4979 position, or at CHARPOS if that is > 0. Value is non-zero if at
4980 least one overlay string was found. */
4981
4982 static int
4983 get_overlay_strings_1 (it, charpos, compute_stop_p)
4984 struct it *it;
4985 int charpos;
4986 int compute_stop_p;
4987 {
4988 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4989 process. This fills IT->overlay_strings with strings, and sets
4990 IT->n_overlay_strings to the total number of strings to process.
4991 IT->pos.overlay_string_index has to be set temporarily to zero
4992 because load_overlay_strings needs this; it must be set to -1
4993 when no overlay strings are found because a zero value would
4994 indicate a position in the first overlay string. */
4995 it->current.overlay_string_index = 0;
4996 load_overlay_strings (it, charpos);
4997
4998 /* If we found overlay strings, set up IT to deliver display
4999 elements from the first one. Otherwise set up IT to deliver
5000 from current_buffer. */
5001 if (it->n_overlay_strings)
5002 {
5003 /* Make sure we know settings in current_buffer, so that we can
5004 restore meaningful values when we're done with the overlay
5005 strings. */
5006 if (compute_stop_p)
5007 compute_stop_pos (it);
5008 xassert (it->face_id >= 0);
5009
5010 /* Save IT's settings. They are restored after all overlay
5011 strings have been processed. */
5012 xassert (!compute_stop_p || it->sp == 0);
5013
5014 /* When called from handle_stop, there might be an empty display
5015 string loaded. In that case, don't bother saving it. */
5016 if (!STRINGP (it->string) || SCHARS (it->string))
5017 push_it (it);
5018
5019 /* Set up IT to deliver display elements from the first overlay
5020 string. */
5021 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5022 it->string = it->overlay_strings[0];
5023 it->from_overlay = Qnil;
5024 it->stop_charpos = 0;
5025 xassert (STRINGP (it->string));
5026 it->end_charpos = SCHARS (it->string);
5027 it->multibyte_p = STRING_MULTIBYTE (it->string);
5028 it->method = GET_FROM_STRING;
5029 return 1;
5030 }
5031
5032 it->current.overlay_string_index = -1;
5033 return 0;
5034 }
5035
5036 static int
5037 get_overlay_strings (it, charpos)
5038 struct it *it;
5039 int charpos;
5040 {
5041 it->string = Qnil;
5042 it->method = GET_FROM_BUFFER;
5043
5044 (void) get_overlay_strings_1 (it, charpos, 1);
5045
5046 CHECK_IT (it);
5047
5048 /* Value is non-zero if we found at least one overlay string. */
5049 return STRINGP (it->string);
5050 }
5051
5052
5053 \f
5054 /***********************************************************************
5055 Saving and restoring state
5056 ***********************************************************************/
5057
5058 /* Save current settings of IT on IT->stack. Called, for example,
5059 before setting up IT for an overlay string, to be able to restore
5060 IT's settings to what they were after the overlay string has been
5061 processed. */
5062
5063 static void
5064 push_it (it)
5065 struct it *it;
5066 {
5067 struct iterator_stack_entry *p;
5068
5069 xassert (it->sp < IT_STACK_SIZE);
5070 p = it->stack + it->sp;
5071
5072 p->stop_charpos = it->stop_charpos;
5073 p->cmp_it = it->cmp_it;
5074 xassert (it->face_id >= 0);
5075 p->face_id = it->face_id;
5076 p->string = it->string;
5077 p->method = it->method;
5078 p->from_overlay = it->from_overlay;
5079 switch (p->method)
5080 {
5081 case GET_FROM_IMAGE:
5082 p->u.image.object = it->object;
5083 p->u.image.image_id = it->image_id;
5084 p->u.image.slice = it->slice;
5085 break;
5086 case GET_FROM_STRETCH:
5087 p->u.stretch.object = it->object;
5088 break;
5089 }
5090 p->position = it->position;
5091 p->current = it->current;
5092 p->end_charpos = it->end_charpos;
5093 p->string_nchars = it->string_nchars;
5094 p->area = it->area;
5095 p->multibyte_p = it->multibyte_p;
5096 p->avoid_cursor_p = it->avoid_cursor_p;
5097 p->space_width = it->space_width;
5098 p->font_height = it->font_height;
5099 p->voffset = it->voffset;
5100 p->string_from_display_prop_p = it->string_from_display_prop_p;
5101 p->display_ellipsis_p = 0;
5102 p->line_wrap = it->line_wrap;
5103 ++it->sp;
5104 }
5105
5106
5107 /* Restore IT's settings from IT->stack. Called, for example, when no
5108 more overlay strings must be processed, and we return to delivering
5109 display elements from a buffer, or when the end of a string from a
5110 `display' property is reached and we return to delivering display
5111 elements from an overlay string, or from a buffer. */
5112
5113 static void
5114 pop_it (it)
5115 struct it *it;
5116 {
5117 struct iterator_stack_entry *p;
5118
5119 xassert (it->sp > 0);
5120 --it->sp;
5121 p = it->stack + it->sp;
5122 it->stop_charpos = p->stop_charpos;
5123 it->cmp_it = p->cmp_it;
5124 it->face_id = p->face_id;
5125 it->current = p->current;
5126 it->position = p->position;
5127 it->string = p->string;
5128 it->from_overlay = p->from_overlay;
5129 if (NILP (it->string))
5130 SET_TEXT_POS (it->current.string_pos, -1, -1);
5131 it->method = p->method;
5132 switch (it->method)
5133 {
5134 case GET_FROM_IMAGE:
5135 it->image_id = p->u.image.image_id;
5136 it->object = p->u.image.object;
5137 it->slice = p->u.image.slice;
5138 break;
5139 case GET_FROM_STRETCH:
5140 it->object = p->u.comp.object;
5141 break;
5142 case GET_FROM_BUFFER:
5143 it->object = it->w->buffer;
5144 break;
5145 case GET_FROM_STRING:
5146 it->object = it->string;
5147 break;
5148 case GET_FROM_DISPLAY_VECTOR:
5149 if (it->s)
5150 it->method = GET_FROM_C_STRING;
5151 else if (STRINGP (it->string))
5152 it->method = GET_FROM_STRING;
5153 else
5154 {
5155 it->method = GET_FROM_BUFFER;
5156 it->object = it->w->buffer;
5157 }
5158 }
5159 it->end_charpos = p->end_charpos;
5160 it->string_nchars = p->string_nchars;
5161 it->area = p->area;
5162 it->multibyte_p = p->multibyte_p;
5163 it->avoid_cursor_p = p->avoid_cursor_p;
5164 it->space_width = p->space_width;
5165 it->font_height = p->font_height;
5166 it->voffset = p->voffset;
5167 it->string_from_display_prop_p = p->string_from_display_prop_p;
5168 it->line_wrap = p->line_wrap;
5169 }
5170
5171
5172 \f
5173 /***********************************************************************
5174 Moving over lines
5175 ***********************************************************************/
5176
5177 /* Set IT's current position to the previous line start. */
5178
5179 static void
5180 back_to_previous_line_start (it)
5181 struct it *it;
5182 {
5183 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5184 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5185 }
5186
5187
5188 /* Move IT to the next line start.
5189
5190 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5191 we skipped over part of the text (as opposed to moving the iterator
5192 continuously over the text). Otherwise, don't change the value
5193 of *SKIPPED_P.
5194
5195 Newlines may come from buffer text, overlay strings, or strings
5196 displayed via the `display' property. That's the reason we can't
5197 simply use find_next_newline_no_quit.
5198
5199 Note that this function may not skip over invisible text that is so
5200 because of text properties and immediately follows a newline. If
5201 it would, function reseat_at_next_visible_line_start, when called
5202 from set_iterator_to_next, would effectively make invisible
5203 characters following a newline part of the wrong glyph row, which
5204 leads to wrong cursor motion. */
5205
5206 static int
5207 forward_to_next_line_start (it, skipped_p)
5208 struct it *it;
5209 int *skipped_p;
5210 {
5211 int old_selective, newline_found_p, n;
5212 const int MAX_NEWLINE_DISTANCE = 500;
5213
5214 /* If already on a newline, just consume it to avoid unintended
5215 skipping over invisible text below. */
5216 if (it->what == IT_CHARACTER
5217 && it->c == '\n'
5218 && CHARPOS (it->position) == IT_CHARPOS (*it))
5219 {
5220 set_iterator_to_next (it, 0);
5221 it->c = 0;
5222 return 1;
5223 }
5224
5225 /* Don't handle selective display in the following. It's (a)
5226 unnecessary because it's done by the caller, and (b) leads to an
5227 infinite recursion because next_element_from_ellipsis indirectly
5228 calls this function. */
5229 old_selective = it->selective;
5230 it->selective = 0;
5231
5232 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5233 from buffer text. */
5234 for (n = newline_found_p = 0;
5235 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5236 n += STRINGP (it->string) ? 0 : 1)
5237 {
5238 if (!get_next_display_element (it))
5239 return 0;
5240 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5241 set_iterator_to_next (it, 0);
5242 }
5243
5244 /* If we didn't find a newline near enough, see if we can use a
5245 short-cut. */
5246 if (!newline_found_p)
5247 {
5248 int start = IT_CHARPOS (*it);
5249 int limit = find_next_newline_no_quit (start, 1);
5250 Lisp_Object pos;
5251
5252 xassert (!STRINGP (it->string));
5253
5254 /* If there isn't any `display' property in sight, and no
5255 overlays, we can just use the position of the newline in
5256 buffer text. */
5257 if (it->stop_charpos >= limit
5258 || ((pos = Fnext_single_property_change (make_number (start),
5259 Qdisplay,
5260 Qnil, make_number (limit)),
5261 NILP (pos))
5262 && next_overlay_change (start) == ZV))
5263 {
5264 IT_CHARPOS (*it) = limit;
5265 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5266 *skipped_p = newline_found_p = 1;
5267 }
5268 else
5269 {
5270 while (get_next_display_element (it)
5271 && !newline_found_p)
5272 {
5273 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5274 set_iterator_to_next (it, 0);
5275 }
5276 }
5277 }
5278
5279 it->selective = old_selective;
5280 return newline_found_p;
5281 }
5282
5283
5284 /* Set IT's current position to the previous visible line start. Skip
5285 invisible text that is so either due to text properties or due to
5286 selective display. Caution: this does not change IT->current_x and
5287 IT->hpos. */
5288
5289 static void
5290 back_to_previous_visible_line_start (it)
5291 struct it *it;
5292 {
5293 while (IT_CHARPOS (*it) > BEGV)
5294 {
5295 back_to_previous_line_start (it);
5296
5297 if (IT_CHARPOS (*it) <= BEGV)
5298 break;
5299
5300 /* If selective > 0, then lines indented more than that values
5301 are invisible. */
5302 if (it->selective > 0
5303 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5304 (double) it->selective)) /* iftc */
5305 continue;
5306
5307 /* Check the newline before point for invisibility. */
5308 {
5309 Lisp_Object prop;
5310 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5311 Qinvisible, it->window);
5312 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5313 continue;
5314 }
5315
5316 if (IT_CHARPOS (*it) <= BEGV)
5317 break;
5318
5319 {
5320 struct it it2;
5321 int pos;
5322 EMACS_INT beg, end;
5323 Lisp_Object val, overlay;
5324
5325 /* If newline is part of a composition, continue from start of composition */
5326 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5327 && beg < IT_CHARPOS (*it))
5328 goto replaced;
5329
5330 /* If newline is replaced by a display property, find start of overlay
5331 or interval and continue search from that point. */
5332 it2 = *it;
5333 pos = --IT_CHARPOS (it2);
5334 --IT_BYTEPOS (it2);
5335 it2.sp = 0;
5336 it2.string_from_display_prop_p = 0;
5337 if (handle_display_prop (&it2) == HANDLED_RETURN
5338 && !NILP (val = get_char_property_and_overlay
5339 (make_number (pos), Qdisplay, Qnil, &overlay))
5340 && (OVERLAYP (overlay)
5341 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5342 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5343 goto replaced;
5344
5345 /* Newline is not replaced by anything -- so we are done. */
5346 break;
5347
5348 replaced:
5349 if (beg < BEGV)
5350 beg = BEGV;
5351 IT_CHARPOS (*it) = beg;
5352 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5353 }
5354 }
5355
5356 it->continuation_lines_width = 0;
5357
5358 xassert (IT_CHARPOS (*it) >= BEGV);
5359 xassert (IT_CHARPOS (*it) == BEGV
5360 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5361 CHECK_IT (it);
5362 }
5363
5364
5365 /* Reseat iterator IT at the previous visible line start. Skip
5366 invisible text that is so either due to text properties or due to
5367 selective display. At the end, update IT's overlay information,
5368 face information etc. */
5369
5370 void
5371 reseat_at_previous_visible_line_start (it)
5372 struct it *it;
5373 {
5374 back_to_previous_visible_line_start (it);
5375 reseat (it, it->current.pos, 1);
5376 CHECK_IT (it);
5377 }
5378
5379
5380 /* Reseat iterator IT on the next visible line start in the current
5381 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5382 preceding the line start. Skip over invisible text that is so
5383 because of selective display. Compute faces, overlays etc at the
5384 new position. Note that this function does not skip over text that
5385 is invisible because of text properties. */
5386
5387 static void
5388 reseat_at_next_visible_line_start (it, on_newline_p)
5389 struct it *it;
5390 int on_newline_p;
5391 {
5392 int newline_found_p, skipped_p = 0;
5393
5394 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5395
5396 /* Skip over lines that are invisible because they are indented
5397 more than the value of IT->selective. */
5398 if (it->selective > 0)
5399 while (IT_CHARPOS (*it) < ZV
5400 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5401 (double) it->selective)) /* iftc */
5402 {
5403 xassert (IT_BYTEPOS (*it) == BEGV
5404 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5405 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5406 }
5407
5408 /* Position on the newline if that's what's requested. */
5409 if (on_newline_p && newline_found_p)
5410 {
5411 if (STRINGP (it->string))
5412 {
5413 if (IT_STRING_CHARPOS (*it) > 0)
5414 {
5415 --IT_STRING_CHARPOS (*it);
5416 --IT_STRING_BYTEPOS (*it);
5417 }
5418 }
5419 else if (IT_CHARPOS (*it) > BEGV)
5420 {
5421 --IT_CHARPOS (*it);
5422 --IT_BYTEPOS (*it);
5423 reseat (it, it->current.pos, 0);
5424 }
5425 }
5426 else if (skipped_p)
5427 reseat (it, it->current.pos, 0);
5428
5429 CHECK_IT (it);
5430 }
5431
5432
5433 \f
5434 /***********************************************************************
5435 Changing an iterator's position
5436 ***********************************************************************/
5437
5438 /* Change IT's current position to POS in current_buffer. If FORCE_P
5439 is non-zero, always check for text properties at the new position.
5440 Otherwise, text properties are only looked up if POS >=
5441 IT->check_charpos of a property. */
5442
5443 static void
5444 reseat (it, pos, force_p)
5445 struct it *it;
5446 struct text_pos pos;
5447 int force_p;
5448 {
5449 int original_pos = IT_CHARPOS (*it);
5450
5451 reseat_1 (it, pos, 0);
5452
5453 /* Determine where to check text properties. Avoid doing it
5454 where possible because text property lookup is very expensive. */
5455 if (force_p
5456 || CHARPOS (pos) > it->stop_charpos
5457 || CHARPOS (pos) < original_pos)
5458 handle_stop (it);
5459
5460 CHECK_IT (it);
5461 }
5462
5463
5464 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5465 IT->stop_pos to POS, also. */
5466
5467 static void
5468 reseat_1 (it, pos, set_stop_p)
5469 struct it *it;
5470 struct text_pos pos;
5471 int set_stop_p;
5472 {
5473 /* Don't call this function when scanning a C string. */
5474 xassert (it->s == NULL);
5475
5476 /* POS must be a reasonable value. */
5477 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5478
5479 it->current.pos = it->position = pos;
5480 it->end_charpos = ZV;
5481 it->dpvec = NULL;
5482 it->current.dpvec_index = -1;
5483 it->current.overlay_string_index = -1;
5484 IT_STRING_CHARPOS (*it) = -1;
5485 IT_STRING_BYTEPOS (*it) = -1;
5486 it->string = Qnil;
5487 it->string_from_display_prop_p = 0;
5488 it->method = GET_FROM_BUFFER;
5489 it->object = it->w->buffer;
5490 it->area = TEXT_AREA;
5491 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5492 it->sp = 0;
5493 it->string_from_display_prop_p = 0;
5494 it->face_before_selective_p = 0;
5495
5496 if (set_stop_p)
5497 it->stop_charpos = CHARPOS (pos);
5498 }
5499
5500
5501 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5502 If S is non-null, it is a C string to iterate over. Otherwise,
5503 STRING gives a Lisp string to iterate over.
5504
5505 If PRECISION > 0, don't return more then PRECISION number of
5506 characters from the string.
5507
5508 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5509 characters have been returned. FIELD_WIDTH < 0 means an infinite
5510 field width.
5511
5512 MULTIBYTE = 0 means disable processing of multibyte characters,
5513 MULTIBYTE > 0 means enable it,
5514 MULTIBYTE < 0 means use IT->multibyte_p.
5515
5516 IT must be initialized via a prior call to init_iterator before
5517 calling this function. */
5518
5519 static void
5520 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5521 struct it *it;
5522 unsigned char *s;
5523 Lisp_Object string;
5524 int charpos;
5525 int precision, field_width, multibyte;
5526 {
5527 /* No region in strings. */
5528 it->region_beg_charpos = it->region_end_charpos = -1;
5529
5530 /* No text property checks performed by default, but see below. */
5531 it->stop_charpos = -1;
5532
5533 /* Set iterator position and end position. */
5534 bzero (&it->current, sizeof it->current);
5535 it->current.overlay_string_index = -1;
5536 it->current.dpvec_index = -1;
5537 xassert (charpos >= 0);
5538
5539 /* If STRING is specified, use its multibyteness, otherwise use the
5540 setting of MULTIBYTE, if specified. */
5541 if (multibyte >= 0)
5542 it->multibyte_p = multibyte > 0;
5543
5544 if (s == NULL)
5545 {
5546 xassert (STRINGP (string));
5547 it->string = string;
5548 it->s = NULL;
5549 it->end_charpos = it->string_nchars = SCHARS (string);
5550 it->method = GET_FROM_STRING;
5551 it->current.string_pos = string_pos (charpos, string);
5552 }
5553 else
5554 {
5555 it->s = s;
5556 it->string = Qnil;
5557
5558 /* Note that we use IT->current.pos, not it->current.string_pos,
5559 for displaying C strings. */
5560 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5561 if (it->multibyte_p)
5562 {
5563 it->current.pos = c_string_pos (charpos, s, 1);
5564 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5565 }
5566 else
5567 {
5568 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5569 it->end_charpos = it->string_nchars = strlen (s);
5570 }
5571
5572 it->method = GET_FROM_C_STRING;
5573 }
5574
5575 /* PRECISION > 0 means don't return more than PRECISION characters
5576 from the string. */
5577 if (precision > 0 && it->end_charpos - charpos > precision)
5578 it->end_charpos = it->string_nchars = charpos + precision;
5579
5580 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5581 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5582 FIELD_WIDTH < 0 means infinite field width. This is useful for
5583 padding with `-' at the end of a mode line. */
5584 if (field_width < 0)
5585 field_width = INFINITY;
5586 if (field_width > it->end_charpos - charpos)
5587 it->end_charpos = charpos + field_width;
5588
5589 /* Use the standard display table for displaying strings. */
5590 if (DISP_TABLE_P (Vstandard_display_table))
5591 it->dp = XCHAR_TABLE (Vstandard_display_table);
5592
5593 it->stop_charpos = charpos;
5594 if (s == NULL && it->multibyte_p)
5595 {
5596 EMACS_INT endpos = SCHARS (it->string);
5597 if (endpos > it->end_charpos)
5598 endpos = it->end_charpos;
5599 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5600 it->string);
5601 }
5602 CHECK_IT (it);
5603 }
5604
5605
5606 \f
5607 /***********************************************************************
5608 Iteration
5609 ***********************************************************************/
5610
5611 /* Map enum it_method value to corresponding next_element_from_* function. */
5612
5613 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5614 {
5615 next_element_from_buffer,
5616 next_element_from_display_vector,
5617 next_element_from_string,
5618 next_element_from_c_string,
5619 next_element_from_image,
5620 next_element_from_stretch
5621 };
5622
5623 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5624
5625
5626 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5627 (possibly with the following characters). */
5628
5629 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5630 ((IT)->cmp_it.id >= 0 \
5631 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5632 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5633 END_CHARPOS, (IT)->w, \
5634 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5635 (IT)->string)))
5636
5637
5638 /* Load IT's display element fields with information about the next
5639 display element from the current position of IT. Value is zero if
5640 end of buffer (or C string) is reached. */
5641
5642 static struct frame *last_escape_glyph_frame = NULL;
5643 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5644 static int last_escape_glyph_merged_face_id = 0;
5645
5646 int
5647 get_next_display_element (it)
5648 struct it *it;
5649 {
5650 /* Non-zero means that we found a display element. Zero means that
5651 we hit the end of what we iterate over. Performance note: the
5652 function pointer `method' used here turns out to be faster than
5653 using a sequence of if-statements. */
5654 int success_p;
5655
5656 get_next:
5657 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5658
5659 if (it->what == IT_CHARACTER)
5660 {
5661 /* Map via display table or translate control characters.
5662 IT->c, IT->len etc. have been set to the next character by
5663 the function call above. If we have a display table, and it
5664 contains an entry for IT->c, translate it. Don't do this if
5665 IT->c itself comes from a display table, otherwise we could
5666 end up in an infinite recursion. (An alternative could be to
5667 count the recursion depth of this function and signal an
5668 error when a certain maximum depth is reached.) Is it worth
5669 it? */
5670 if (success_p && it->dpvec == NULL)
5671 {
5672 Lisp_Object dv;
5673 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5674 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5675 nbsp_or_shy = char_is_other;
5676 int c = it->c; /* This is the character to display. */
5677
5678 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5679 {
5680 xassert (SINGLE_BYTE_CHAR_P (c));
5681 if (unibyte_display_via_language_environment)
5682 {
5683 c = DECODE_CHAR (unibyte, c);
5684 if (c < 0)
5685 c = BYTE8_TO_CHAR (it->c);
5686 }
5687 else
5688 c = BYTE8_TO_CHAR (it->c);
5689 }
5690
5691 if (it->dp
5692 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5693 VECTORP (dv)))
5694 {
5695 struct Lisp_Vector *v = XVECTOR (dv);
5696
5697 /* Return the first character from the display table
5698 entry, if not empty. If empty, don't display the
5699 current character. */
5700 if (v->header.size)
5701 {
5702 it->dpvec_char_len = it->len;
5703 it->dpvec = v->contents;
5704 it->dpend = v->contents + v->header.size;
5705 it->current.dpvec_index = 0;
5706 it->dpvec_face_id = -1;
5707 it->saved_face_id = it->face_id;
5708 it->method = GET_FROM_DISPLAY_VECTOR;
5709 it->ellipsis_p = 0;
5710 }
5711 else
5712 {
5713 set_iterator_to_next (it, 0);
5714 }
5715 goto get_next;
5716 }
5717
5718 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5719 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5720 : c == 0xAD ? char_is_soft_hyphen
5721 : char_is_other);
5722
5723 /* Translate control characters into `\003' or `^C' form.
5724 Control characters coming from a display table entry are
5725 currently not translated because we use IT->dpvec to hold
5726 the translation. This could easily be changed but I
5727 don't believe that it is worth doing.
5728
5729 NBSP and SOFT-HYPEN are property translated too.
5730
5731 Non-printable characters and raw-byte characters are also
5732 translated to octal form. */
5733 if (((c < ' ' || c == 127) /* ASCII control chars */
5734 ? (it->area != TEXT_AREA
5735 /* In mode line, treat \n, \t like other crl chars. */
5736 || (c != '\t'
5737 && it->glyph_row
5738 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5739 || (c != '\n' && c != '\t'))
5740 : (nbsp_or_shy
5741 || CHAR_BYTE8_P (c)
5742 || ! CHAR_PRINTABLE_P (c))))
5743 {
5744 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5745 or a non-printable character which must be displayed
5746 either as '\003' or as `^C' where the '\\' and '^'
5747 can be defined in the display table. Fill
5748 IT->ctl_chars with glyphs for what we have to
5749 display. Then, set IT->dpvec to these glyphs. */
5750 Lisp_Object gc;
5751 int ctl_len;
5752 int face_id, lface_id = 0 ;
5753 int escape_glyph;
5754
5755 /* Handle control characters with ^. */
5756
5757 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5758 {
5759 int g;
5760
5761 g = '^'; /* default glyph for Control */
5762 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5763 if (it->dp
5764 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5765 && GLYPH_CODE_CHAR_VALID_P (gc))
5766 {
5767 g = GLYPH_CODE_CHAR (gc);
5768 lface_id = GLYPH_CODE_FACE (gc);
5769 }
5770 if (lface_id)
5771 {
5772 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5773 }
5774 else if (it->f == last_escape_glyph_frame
5775 && it->face_id == last_escape_glyph_face_id)
5776 {
5777 face_id = last_escape_glyph_merged_face_id;
5778 }
5779 else
5780 {
5781 /* Merge the escape-glyph face into the current face. */
5782 face_id = merge_faces (it->f, Qescape_glyph, 0,
5783 it->face_id);
5784 last_escape_glyph_frame = it->f;
5785 last_escape_glyph_face_id = it->face_id;
5786 last_escape_glyph_merged_face_id = face_id;
5787 }
5788
5789 XSETINT (it->ctl_chars[0], g);
5790 XSETINT (it->ctl_chars[1], c ^ 0100);
5791 ctl_len = 2;
5792 goto display_control;
5793 }
5794
5795 /* Handle non-break space in the mode where it only gets
5796 highlighting. */
5797
5798 if (EQ (Vnobreak_char_display, Qt)
5799 && nbsp_or_shy == char_is_nbsp)
5800 {
5801 /* Merge the no-break-space face into the current face. */
5802 face_id = merge_faces (it->f, Qnobreak_space, 0,
5803 it->face_id);
5804
5805 c = ' ';
5806 XSETINT (it->ctl_chars[0], ' ');
5807 ctl_len = 1;
5808 goto display_control;
5809 }
5810
5811 /* Handle sequences that start with the "escape glyph". */
5812
5813 /* the default escape glyph is \. */
5814 escape_glyph = '\\';
5815
5816 if (it->dp
5817 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5818 && GLYPH_CODE_CHAR_VALID_P (gc))
5819 {
5820 escape_glyph = GLYPH_CODE_CHAR (gc);
5821 lface_id = GLYPH_CODE_FACE (gc);
5822 }
5823 if (lface_id)
5824 {
5825 /* The display table specified a face.
5826 Merge it into face_id and also into escape_glyph. */
5827 face_id = merge_faces (it->f, Qt, lface_id,
5828 it->face_id);
5829 }
5830 else if (it->f == last_escape_glyph_frame
5831 && it->face_id == last_escape_glyph_face_id)
5832 {
5833 face_id = last_escape_glyph_merged_face_id;
5834 }
5835 else
5836 {
5837 /* Merge the escape-glyph face into the current face. */
5838 face_id = merge_faces (it->f, Qescape_glyph, 0,
5839 it->face_id);
5840 last_escape_glyph_frame = it->f;
5841 last_escape_glyph_face_id = it->face_id;
5842 last_escape_glyph_merged_face_id = face_id;
5843 }
5844
5845 /* Handle soft hyphens in the mode where they only get
5846 highlighting. */
5847
5848 if (EQ (Vnobreak_char_display, Qt)
5849 && nbsp_or_shy == char_is_soft_hyphen)
5850 {
5851 XSETINT (it->ctl_chars[0], '-');
5852 ctl_len = 1;
5853 goto display_control;
5854 }
5855
5856 /* Handle non-break space and soft hyphen
5857 with the escape glyph. */
5858
5859 if (nbsp_or_shy)
5860 {
5861 XSETINT (it->ctl_chars[0], escape_glyph);
5862 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5863 XSETINT (it->ctl_chars[1], c);
5864 ctl_len = 2;
5865 goto display_control;
5866 }
5867
5868 {
5869 char str[10];
5870 int len, i;
5871
5872 if (CHAR_BYTE8_P (c))
5873 /* Display \200 instead of \17777600. */
5874 c = CHAR_TO_BYTE8 (c);
5875 len = sprintf (str, "%03o", c);
5876
5877 XSETINT (it->ctl_chars[0], escape_glyph);
5878 for (i = 0; i < len; i++)
5879 XSETINT (it->ctl_chars[i + 1], str[i]);
5880 ctl_len = len + 1;
5881 }
5882
5883 display_control:
5884 /* Set up IT->dpvec and return first character from it. */
5885 it->dpvec_char_len = it->len;
5886 it->dpvec = it->ctl_chars;
5887 it->dpend = it->dpvec + ctl_len;
5888 it->current.dpvec_index = 0;
5889 it->dpvec_face_id = face_id;
5890 it->saved_face_id = it->face_id;
5891 it->method = GET_FROM_DISPLAY_VECTOR;
5892 it->ellipsis_p = 0;
5893 goto get_next;
5894 }
5895 it->char_to_display = c;
5896 }
5897 else if (success_p)
5898 {
5899 it->char_to_display = it->c;
5900 }
5901 }
5902
5903 #ifdef HAVE_WINDOW_SYSTEM
5904 /* Adjust face id for a multibyte character. There are no multibyte
5905 character in unibyte text. */
5906 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5907 && it->multibyte_p
5908 && success_p
5909 && FRAME_WINDOW_P (it->f))
5910 {
5911 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5912
5913 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5914 {
5915 /* Automatic composition with glyph-string. */
5916 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5917
5918 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5919 }
5920 else
5921 {
5922 int pos = (it->s ? -1
5923 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5924 : IT_CHARPOS (*it));
5925 int c;
5926
5927 if (it->what == IT_CHARACTER)
5928 c = it->char_to_display;
5929 else
5930 {
5931 struct composition *cmp = composition_table[it->cmp_it.id];
5932 int i;
5933
5934 c = ' ';
5935 for (i = 0; i < cmp->glyph_len; i++)
5936 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
5937 break;
5938 }
5939 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
5940 }
5941 }
5942 #endif
5943
5944 /* Is this character the last one of a run of characters with
5945 box? If yes, set IT->end_of_box_run_p to 1. */
5946 if (it->face_box_p
5947 && it->s == NULL)
5948 {
5949 if (it->method == GET_FROM_STRING && it->sp)
5950 {
5951 int face_id = underlying_face_id (it);
5952 struct face *face = FACE_FROM_ID (it->f, face_id);
5953
5954 if (face)
5955 {
5956 if (face->box == FACE_NO_BOX)
5957 {
5958 /* If the box comes from face properties in a
5959 display string, check faces in that string. */
5960 int string_face_id = face_after_it_pos (it);
5961 it->end_of_box_run_p
5962 = (FACE_FROM_ID (it->f, string_face_id)->box
5963 == FACE_NO_BOX);
5964 }
5965 /* Otherwise, the box comes from the underlying face.
5966 If this is the last string character displayed, check
5967 the next buffer location. */
5968 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5969 && (it->current.overlay_string_index
5970 == it->n_overlay_strings - 1))
5971 {
5972 EMACS_INT ignore;
5973 int next_face_id;
5974 struct text_pos pos = it->current.pos;
5975 INC_TEXT_POS (pos, it->multibyte_p);
5976
5977 next_face_id = face_at_buffer_position
5978 (it->w, CHARPOS (pos), it->region_beg_charpos,
5979 it->region_end_charpos, &ignore,
5980 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5981 -1);
5982 it->end_of_box_run_p
5983 = (FACE_FROM_ID (it->f, next_face_id)->box
5984 == FACE_NO_BOX);
5985 }
5986 }
5987 }
5988 else
5989 {
5990 int face_id = face_after_it_pos (it);
5991 it->end_of_box_run_p
5992 = (face_id != it->face_id
5993 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5994 }
5995 }
5996
5997 /* Value is 0 if end of buffer or string reached. */
5998 return success_p;
5999 }
6000
6001
6002 /* Move IT to the next display element.
6003
6004 RESEAT_P non-zero means if called on a newline in buffer text,
6005 skip to the next visible line start.
6006
6007 Functions get_next_display_element and set_iterator_to_next are
6008 separate because I find this arrangement easier to handle than a
6009 get_next_display_element function that also increments IT's
6010 position. The way it is we can first look at an iterator's current
6011 display element, decide whether it fits on a line, and if it does,
6012 increment the iterator position. The other way around we probably
6013 would either need a flag indicating whether the iterator has to be
6014 incremented the next time, or we would have to implement a
6015 decrement position function which would not be easy to write. */
6016
6017 void
6018 set_iterator_to_next (it, reseat_p)
6019 struct it *it;
6020 int reseat_p;
6021 {
6022 /* Reset flags indicating start and end of a sequence of characters
6023 with box. Reset them at the start of this function because
6024 moving the iterator to a new position might set them. */
6025 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6026
6027 switch (it->method)
6028 {
6029 case GET_FROM_BUFFER:
6030 /* The current display element of IT is a character from
6031 current_buffer. Advance in the buffer, and maybe skip over
6032 invisible lines that are so because of selective display. */
6033 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6034 reseat_at_next_visible_line_start (it, 0);
6035 else if (it->cmp_it.id >= 0)
6036 {
6037 IT_CHARPOS (*it) += it->cmp_it.nchars;
6038 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6039 if (it->cmp_it.to < it->cmp_it.nglyphs)
6040 it->cmp_it.from = it->cmp_it.to;
6041 else
6042 {
6043 it->cmp_it.id = -1;
6044 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6045 IT_BYTEPOS (*it), it->stop_charpos,
6046 Qnil);
6047 }
6048 }
6049 else
6050 {
6051 xassert (it->len != 0);
6052 IT_BYTEPOS (*it) += it->len;
6053 IT_CHARPOS (*it) += 1;
6054 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6055 }
6056 break;
6057
6058 case GET_FROM_C_STRING:
6059 /* Current display element of IT is from a C string. */
6060 IT_BYTEPOS (*it) += it->len;
6061 IT_CHARPOS (*it) += 1;
6062 break;
6063
6064 case GET_FROM_DISPLAY_VECTOR:
6065 /* Current display element of IT is from a display table entry.
6066 Advance in the display table definition. Reset it to null if
6067 end reached, and continue with characters from buffers/
6068 strings. */
6069 ++it->current.dpvec_index;
6070
6071 /* Restore face of the iterator to what they were before the
6072 display vector entry (these entries may contain faces). */
6073 it->face_id = it->saved_face_id;
6074
6075 if (it->dpvec + it->current.dpvec_index == it->dpend)
6076 {
6077 int recheck_faces = it->ellipsis_p;
6078
6079 if (it->s)
6080 it->method = GET_FROM_C_STRING;
6081 else if (STRINGP (it->string))
6082 it->method = GET_FROM_STRING;
6083 else
6084 {
6085 it->method = GET_FROM_BUFFER;
6086 it->object = it->w->buffer;
6087 }
6088
6089 it->dpvec = NULL;
6090 it->current.dpvec_index = -1;
6091
6092 /* Skip over characters which were displayed via IT->dpvec. */
6093 if (it->dpvec_char_len < 0)
6094 reseat_at_next_visible_line_start (it, 1);
6095 else if (it->dpvec_char_len > 0)
6096 {
6097 if (it->method == GET_FROM_STRING
6098 && it->n_overlay_strings > 0)
6099 it->ignore_overlay_strings_at_pos_p = 1;
6100 it->len = it->dpvec_char_len;
6101 set_iterator_to_next (it, reseat_p);
6102 }
6103
6104 /* Maybe recheck faces after display vector */
6105 if (recheck_faces)
6106 it->stop_charpos = IT_CHARPOS (*it);
6107 }
6108 break;
6109
6110 case GET_FROM_STRING:
6111 /* Current display element is a character from a Lisp string. */
6112 xassert (it->s == NULL && STRINGP (it->string));
6113 if (it->cmp_it.id >= 0)
6114 {
6115 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6116 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6117 if (it->cmp_it.to < it->cmp_it.nglyphs)
6118 it->cmp_it.from = it->cmp_it.to;
6119 else
6120 {
6121 it->cmp_it.id = -1;
6122 composition_compute_stop_pos (&it->cmp_it,
6123 IT_STRING_CHARPOS (*it),
6124 IT_STRING_BYTEPOS (*it),
6125 it->stop_charpos, it->string);
6126 }
6127 }
6128 else
6129 {
6130 IT_STRING_BYTEPOS (*it) += it->len;
6131 IT_STRING_CHARPOS (*it) += 1;
6132 }
6133
6134 consider_string_end:
6135
6136 if (it->current.overlay_string_index >= 0)
6137 {
6138 /* IT->string is an overlay string. Advance to the
6139 next, if there is one. */
6140 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6141 {
6142 it->ellipsis_p = 0;
6143 next_overlay_string (it);
6144 if (it->ellipsis_p)
6145 setup_for_ellipsis (it, 0);
6146 }
6147 }
6148 else
6149 {
6150 /* IT->string is not an overlay string. If we reached
6151 its end, and there is something on IT->stack, proceed
6152 with what is on the stack. This can be either another
6153 string, this time an overlay string, or a buffer. */
6154 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6155 && it->sp > 0)
6156 {
6157 pop_it (it);
6158 if (it->method == GET_FROM_STRING)
6159 goto consider_string_end;
6160 }
6161 }
6162 break;
6163
6164 case GET_FROM_IMAGE:
6165 case GET_FROM_STRETCH:
6166 /* The position etc with which we have to proceed are on
6167 the stack. The position may be at the end of a string,
6168 if the `display' property takes up the whole string. */
6169 xassert (it->sp > 0);
6170 pop_it (it);
6171 if (it->method == GET_FROM_STRING)
6172 goto consider_string_end;
6173 break;
6174
6175 default:
6176 /* There are no other methods defined, so this should be a bug. */
6177 abort ();
6178 }
6179
6180 xassert (it->method != GET_FROM_STRING
6181 || (STRINGP (it->string)
6182 && IT_STRING_CHARPOS (*it) >= 0));
6183 }
6184
6185 /* Load IT's display element fields with information about the next
6186 display element which comes from a display table entry or from the
6187 result of translating a control character to one of the forms `^C'
6188 or `\003'.
6189
6190 IT->dpvec holds the glyphs to return as characters.
6191 IT->saved_face_id holds the face id before the display vector--it
6192 is restored into IT->face_id in set_iterator_to_next. */
6193
6194 static int
6195 next_element_from_display_vector (it)
6196 struct it *it;
6197 {
6198 Lisp_Object gc;
6199
6200 /* Precondition. */
6201 xassert (it->dpvec && it->current.dpvec_index >= 0);
6202
6203 it->face_id = it->saved_face_id;
6204
6205 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6206 That seemed totally bogus - so I changed it... */
6207 gc = it->dpvec[it->current.dpvec_index];
6208
6209 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6210 {
6211 it->c = GLYPH_CODE_CHAR (gc);
6212 it->len = CHAR_BYTES (it->c);
6213
6214 /* The entry may contain a face id to use. Such a face id is
6215 the id of a Lisp face, not a realized face. A face id of
6216 zero means no face is specified. */
6217 if (it->dpvec_face_id >= 0)
6218 it->face_id = it->dpvec_face_id;
6219 else
6220 {
6221 int lface_id = GLYPH_CODE_FACE (gc);
6222 if (lface_id > 0)
6223 it->face_id = merge_faces (it->f, Qt, lface_id,
6224 it->saved_face_id);
6225 }
6226 }
6227 else
6228 /* Display table entry is invalid. Return a space. */
6229 it->c = ' ', it->len = 1;
6230
6231 /* Don't change position and object of the iterator here. They are
6232 still the values of the character that had this display table
6233 entry or was translated, and that's what we want. */
6234 it->what = IT_CHARACTER;
6235 return 1;
6236 }
6237
6238
6239 /* Load IT with the next display element from Lisp string IT->string.
6240 IT->current.string_pos is the current position within the string.
6241 If IT->current.overlay_string_index >= 0, the Lisp string is an
6242 overlay string. */
6243
6244 static int
6245 next_element_from_string (it)
6246 struct it *it;
6247 {
6248 struct text_pos position;
6249
6250 xassert (STRINGP (it->string));
6251 xassert (IT_STRING_CHARPOS (*it) >= 0);
6252 position = it->current.string_pos;
6253
6254 /* Time to check for invisible text? */
6255 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6256 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6257 {
6258 handle_stop (it);
6259
6260 /* Since a handler may have changed IT->method, we must
6261 recurse here. */
6262 return GET_NEXT_DISPLAY_ELEMENT (it);
6263 }
6264
6265 if (it->current.overlay_string_index >= 0)
6266 {
6267 /* Get the next character from an overlay string. In overlay
6268 strings, There is no field width or padding with spaces to
6269 do. */
6270 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6271 {
6272 it->what = IT_EOB;
6273 return 0;
6274 }
6275 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6276 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6277 && next_element_from_composition (it))
6278 {
6279 return 1;
6280 }
6281 else if (STRING_MULTIBYTE (it->string))
6282 {
6283 const unsigned char *s = (SDATA (it->string)
6284 + IT_STRING_BYTEPOS (*it));
6285 it->c = string_char_and_length (s, &it->len);
6286 }
6287 else
6288 {
6289 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6290 it->len = 1;
6291 }
6292 }
6293 else
6294 {
6295 /* Get the next character from a Lisp string that is not an
6296 overlay string. Such strings come from the mode line, for
6297 example. We may have to pad with spaces, or truncate the
6298 string. See also next_element_from_c_string. */
6299 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6300 {
6301 it->what = IT_EOB;
6302 return 0;
6303 }
6304 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6305 {
6306 /* Pad with spaces. */
6307 it->c = ' ', it->len = 1;
6308 CHARPOS (position) = BYTEPOS (position) = -1;
6309 }
6310 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6311 IT_STRING_BYTEPOS (*it), it->string_nchars)
6312 && next_element_from_composition (it))
6313 {
6314 return 1;
6315 }
6316 else if (STRING_MULTIBYTE (it->string))
6317 {
6318 const unsigned char *s = (SDATA (it->string)
6319 + IT_STRING_BYTEPOS (*it));
6320 it->c = string_char_and_length (s, &it->len);
6321 }
6322 else
6323 {
6324 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6325 it->len = 1;
6326 }
6327 }
6328
6329 /* Record what we have and where it came from. */
6330 it->what = IT_CHARACTER;
6331 it->object = it->string;
6332 it->position = position;
6333 return 1;
6334 }
6335
6336
6337 /* Load IT with next display element from C string IT->s.
6338 IT->string_nchars is the maximum number of characters to return
6339 from the string. IT->end_charpos may be greater than
6340 IT->string_nchars when this function is called, in which case we
6341 may have to return padding spaces. Value is zero if end of string
6342 reached, including padding spaces. */
6343
6344 static int
6345 next_element_from_c_string (it)
6346 struct it *it;
6347 {
6348 int success_p = 1;
6349
6350 xassert (it->s);
6351 it->what = IT_CHARACTER;
6352 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6353 it->object = Qnil;
6354
6355 /* IT's position can be greater IT->string_nchars in case a field
6356 width or precision has been specified when the iterator was
6357 initialized. */
6358 if (IT_CHARPOS (*it) >= it->end_charpos)
6359 {
6360 /* End of the game. */
6361 it->what = IT_EOB;
6362 success_p = 0;
6363 }
6364 else if (IT_CHARPOS (*it) >= it->string_nchars)
6365 {
6366 /* Pad with spaces. */
6367 it->c = ' ', it->len = 1;
6368 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6369 }
6370 else if (it->multibyte_p)
6371 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6372 else
6373 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6374
6375 return success_p;
6376 }
6377
6378
6379 /* Set up IT to return characters from an ellipsis, if appropriate.
6380 The definition of the ellipsis glyphs may come from a display table
6381 entry. This function fills IT with the first glyph from the
6382 ellipsis if an ellipsis is to be displayed. */
6383
6384 static int
6385 next_element_from_ellipsis (it)
6386 struct it *it;
6387 {
6388 if (it->selective_display_ellipsis_p)
6389 setup_for_ellipsis (it, it->len);
6390 else
6391 {
6392 /* The face at the current position may be different from the
6393 face we find after the invisible text. Remember what it
6394 was in IT->saved_face_id, and signal that it's there by
6395 setting face_before_selective_p. */
6396 it->saved_face_id = it->face_id;
6397 it->method = GET_FROM_BUFFER;
6398 it->object = it->w->buffer;
6399 reseat_at_next_visible_line_start (it, 1);
6400 it->face_before_selective_p = 1;
6401 }
6402
6403 return GET_NEXT_DISPLAY_ELEMENT (it);
6404 }
6405
6406
6407 /* Deliver an image display element. The iterator IT is already
6408 filled with image information (done in handle_display_prop). Value
6409 is always 1. */
6410
6411
6412 static int
6413 next_element_from_image (it)
6414 struct it *it;
6415 {
6416 it->what = IT_IMAGE;
6417 it->ignore_overlay_strings_at_pos_p = 0;
6418 return 1;
6419 }
6420
6421
6422 /* Fill iterator IT with next display element from a stretch glyph
6423 property. IT->object is the value of the text property. Value is
6424 always 1. */
6425
6426 static int
6427 next_element_from_stretch (it)
6428 struct it *it;
6429 {
6430 it->what = IT_STRETCH;
6431 return 1;
6432 }
6433
6434
6435 /* Load IT with the next display element from current_buffer. Value
6436 is zero if end of buffer reached. IT->stop_charpos is the next
6437 position at which to stop and check for text properties or buffer
6438 end. */
6439
6440 static int
6441 next_element_from_buffer (it)
6442 struct it *it;
6443 {
6444 int success_p = 1;
6445
6446 xassert (IT_CHARPOS (*it) >= BEGV);
6447
6448 if (IT_CHARPOS (*it) >= it->stop_charpos)
6449 {
6450 if (IT_CHARPOS (*it) >= it->end_charpos)
6451 {
6452 int overlay_strings_follow_p;
6453
6454 /* End of the game, except when overlay strings follow that
6455 haven't been returned yet. */
6456 if (it->overlay_strings_at_end_processed_p)
6457 overlay_strings_follow_p = 0;
6458 else
6459 {
6460 it->overlay_strings_at_end_processed_p = 1;
6461 overlay_strings_follow_p = get_overlay_strings (it, 0);
6462 }
6463
6464 if (overlay_strings_follow_p)
6465 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6466 else
6467 {
6468 it->what = IT_EOB;
6469 it->position = it->current.pos;
6470 success_p = 0;
6471 }
6472 }
6473 else
6474 {
6475 handle_stop (it);
6476 return GET_NEXT_DISPLAY_ELEMENT (it);
6477 }
6478 }
6479 else
6480 {
6481 /* No face changes, overlays etc. in sight, so just return a
6482 character from current_buffer. */
6483 unsigned char *p;
6484
6485 /* Maybe run the redisplay end trigger hook. Performance note:
6486 This doesn't seem to cost measurable time. */
6487 if (it->redisplay_end_trigger_charpos
6488 && it->glyph_row
6489 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6490 run_redisplay_end_trigger_hook (it);
6491
6492 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6493 it->end_charpos)
6494 && next_element_from_composition (it))
6495 {
6496 return 1;
6497 }
6498
6499 /* Get the next character, maybe multibyte. */
6500 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6501 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6502 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6503 else
6504 it->c = *p, it->len = 1;
6505
6506 /* Record what we have and where it came from. */
6507 it->what = IT_CHARACTER;
6508 it->object = it->w->buffer;
6509 it->position = it->current.pos;
6510
6511 /* Normally we return the character found above, except when we
6512 really want to return an ellipsis for selective display. */
6513 if (it->selective)
6514 {
6515 if (it->c == '\n')
6516 {
6517 /* A value of selective > 0 means hide lines indented more
6518 than that number of columns. */
6519 if (it->selective > 0
6520 && IT_CHARPOS (*it) + 1 < ZV
6521 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6522 IT_BYTEPOS (*it) + 1,
6523 (double) it->selective)) /* iftc */
6524 {
6525 success_p = next_element_from_ellipsis (it);
6526 it->dpvec_char_len = -1;
6527 }
6528 }
6529 else if (it->c == '\r' && it->selective == -1)
6530 {
6531 /* A value of selective == -1 means that everything from the
6532 CR to the end of the line is invisible, with maybe an
6533 ellipsis displayed for it. */
6534 success_p = next_element_from_ellipsis (it);
6535 it->dpvec_char_len = -1;
6536 }
6537 }
6538 }
6539
6540 /* Value is zero if end of buffer reached. */
6541 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6542 return success_p;
6543 }
6544
6545
6546 /* Run the redisplay end trigger hook for IT. */
6547
6548 static void
6549 run_redisplay_end_trigger_hook (it)
6550 struct it *it;
6551 {
6552 Lisp_Object args[3];
6553
6554 /* IT->glyph_row should be non-null, i.e. we should be actually
6555 displaying something, or otherwise we should not run the hook. */
6556 xassert (it->glyph_row);
6557
6558 /* Set up hook arguments. */
6559 args[0] = Qredisplay_end_trigger_functions;
6560 args[1] = it->window;
6561 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6562 it->redisplay_end_trigger_charpos = 0;
6563
6564 /* Since we are *trying* to run these functions, don't try to run
6565 them again, even if they get an error. */
6566 it->w->redisplay_end_trigger = Qnil;
6567 Frun_hook_with_args (3, args);
6568
6569 /* Notice if it changed the face of the character we are on. */
6570 handle_face_prop (it);
6571 }
6572
6573
6574 /* Deliver a composition display element. Unlike the other
6575 next_element_from_XXX, this function is not registered in the array
6576 get_next_element[]. It is called from next_element_from_buffer and
6577 next_element_from_string when necessary. */
6578
6579 static int
6580 next_element_from_composition (it)
6581 struct it *it;
6582 {
6583 it->what = IT_COMPOSITION;
6584 it->len = it->cmp_it.nbytes;
6585 if (STRINGP (it->string))
6586 {
6587 if (it->c < 0)
6588 {
6589 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6590 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6591 return 0;
6592 }
6593 it->position = it->current.string_pos;
6594 it->object = it->string;
6595 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6596 IT_STRING_BYTEPOS (*it), it->string);
6597 }
6598 else
6599 {
6600 if (it->c < 0)
6601 {
6602 IT_CHARPOS (*it) += it->cmp_it.nchars;
6603 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6604 return 0;
6605 }
6606 it->position = it->current.pos;
6607 it->object = it->w->buffer;
6608 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6609 IT_BYTEPOS (*it), Qnil);
6610 }
6611 return 1;
6612 }
6613
6614
6615 \f
6616 /***********************************************************************
6617 Moving an iterator without producing glyphs
6618 ***********************************************************************/
6619
6620 /* Check if iterator is at a position corresponding to a valid buffer
6621 position after some move_it_ call. */
6622
6623 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6624 ((it)->method == GET_FROM_STRING \
6625 ? IT_STRING_CHARPOS (*it) == 0 \
6626 : 1)
6627
6628
6629 /* Move iterator IT to a specified buffer or X position within one
6630 line on the display without producing glyphs.
6631
6632 OP should be a bit mask including some or all of these bits:
6633 MOVE_TO_X: Stop on reaching x-position TO_X.
6634 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6635 Regardless of OP's value, stop in reaching the end of the display line.
6636
6637 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6638 This means, in particular, that TO_X includes window's horizontal
6639 scroll amount.
6640
6641 The return value has several possible values that
6642 say what condition caused the scan to stop:
6643
6644 MOVE_POS_MATCH_OR_ZV
6645 - when TO_POS or ZV was reached.
6646
6647 MOVE_X_REACHED
6648 -when TO_X was reached before TO_POS or ZV were reached.
6649
6650 MOVE_LINE_CONTINUED
6651 - when we reached the end of the display area and the line must
6652 be continued.
6653
6654 MOVE_LINE_TRUNCATED
6655 - when we reached the end of the display area and the line is
6656 truncated.
6657
6658 MOVE_NEWLINE_OR_CR
6659 - when we stopped at a line end, i.e. a newline or a CR and selective
6660 display is on. */
6661
6662 static enum move_it_result
6663 move_it_in_display_line_to (struct it *it,
6664 EMACS_INT to_charpos, int to_x,
6665 enum move_operation_enum op)
6666 {
6667 enum move_it_result result = MOVE_UNDEFINED;
6668 struct glyph_row *saved_glyph_row;
6669 struct it wrap_it, atpos_it, atx_it;
6670 int may_wrap = 0;
6671
6672 /* Don't produce glyphs in produce_glyphs. */
6673 saved_glyph_row = it->glyph_row;
6674 it->glyph_row = NULL;
6675
6676 /* Use wrap_it to save a copy of IT wherever a word wrap could
6677 occur. Use atpos_it to save a copy of IT at the desired buffer
6678 position, if found, so that we can scan ahead and check if the
6679 word later overshoots the window edge. Use atx_it similarly, for
6680 pixel positions. */
6681 wrap_it.sp = -1;
6682 atpos_it.sp = -1;
6683 atx_it.sp = -1;
6684
6685 #define BUFFER_POS_REACHED_P() \
6686 ((op & MOVE_TO_POS) != 0 \
6687 && BUFFERP (it->object) \
6688 && IT_CHARPOS (*it) >= to_charpos \
6689 && (it->method == GET_FROM_BUFFER \
6690 || (it->method == GET_FROM_DISPLAY_VECTOR \
6691 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6692
6693 /* If there's a line-/wrap-prefix, handle it. */
6694 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6695 && it->current_y < it->last_visible_y)
6696 handle_line_prefix (it);
6697
6698 while (1)
6699 {
6700 int x, i, ascent = 0, descent = 0;
6701
6702 /* Utility macro to reset an iterator with x, ascent, and descent. */
6703 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6704 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6705 (IT)->max_descent = descent)
6706
6707 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6708 glyph). */
6709 if ((op & MOVE_TO_POS) != 0
6710 && BUFFERP (it->object)
6711 && it->method == GET_FROM_BUFFER
6712 && IT_CHARPOS (*it) > to_charpos)
6713 {
6714 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6715 {
6716 result = MOVE_POS_MATCH_OR_ZV;
6717 break;
6718 }
6719 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6720 /* If wrap_it is valid, the current position might be in a
6721 word that is wrapped. So, save the iterator in
6722 atpos_it and continue to see if wrapping happens. */
6723 atpos_it = *it;
6724 }
6725
6726 /* Stop when ZV reached.
6727 We used to stop here when TO_CHARPOS reached as well, but that is
6728 too soon if this glyph does not fit on this line. So we handle it
6729 explicitly below. */
6730 if (!get_next_display_element (it))
6731 {
6732 result = MOVE_POS_MATCH_OR_ZV;
6733 break;
6734 }
6735
6736 if (it->line_wrap == TRUNCATE)
6737 {
6738 if (BUFFER_POS_REACHED_P ())
6739 {
6740 result = MOVE_POS_MATCH_OR_ZV;
6741 break;
6742 }
6743 }
6744 else
6745 {
6746 if (it->line_wrap == WORD_WRAP)
6747 {
6748 if (IT_DISPLAYING_WHITESPACE (it))
6749 may_wrap = 1;
6750 else if (may_wrap)
6751 {
6752 /* We have reached a glyph that follows one or more
6753 whitespace characters. If the position is
6754 already found, we are done. */
6755 if (atpos_it.sp >= 0)
6756 {
6757 *it = atpos_it;
6758 result = MOVE_POS_MATCH_OR_ZV;
6759 goto done;
6760 }
6761 if (atx_it.sp >= 0)
6762 {
6763 *it = atx_it;
6764 result = MOVE_X_REACHED;
6765 goto done;
6766 }
6767 /* Otherwise, we can wrap here. */
6768 wrap_it = *it;
6769 may_wrap = 0;
6770 }
6771 }
6772 }
6773
6774 /* Remember the line height for the current line, in case
6775 the next element doesn't fit on the line. */
6776 ascent = it->max_ascent;
6777 descent = it->max_descent;
6778
6779 /* The call to produce_glyphs will get the metrics of the
6780 display element IT is loaded with. Record the x-position
6781 before this display element, in case it doesn't fit on the
6782 line. */
6783 x = it->current_x;
6784
6785 PRODUCE_GLYPHS (it);
6786
6787 if (it->area != TEXT_AREA)
6788 {
6789 set_iterator_to_next (it, 1);
6790 continue;
6791 }
6792
6793 /* The number of glyphs we get back in IT->nglyphs will normally
6794 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6795 character on a terminal frame, or (iii) a line end. For the
6796 second case, IT->nglyphs - 1 padding glyphs will be present.
6797 (On X frames, there is only one glyph produced for a
6798 composite character.)
6799
6800 The behavior implemented below means, for continuation lines,
6801 that as many spaces of a TAB as fit on the current line are
6802 displayed there. For terminal frames, as many glyphs of a
6803 multi-glyph character are displayed in the current line, too.
6804 This is what the old redisplay code did, and we keep it that
6805 way. Under X, the whole shape of a complex character must
6806 fit on the line or it will be completely displayed in the
6807 next line.
6808
6809 Note that both for tabs and padding glyphs, all glyphs have
6810 the same width. */
6811 if (it->nglyphs)
6812 {
6813 /* More than one glyph or glyph doesn't fit on line. All
6814 glyphs have the same width. */
6815 int single_glyph_width = it->pixel_width / it->nglyphs;
6816 int new_x;
6817 int x_before_this_char = x;
6818 int hpos_before_this_char = it->hpos;
6819
6820 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6821 {
6822 new_x = x + single_glyph_width;
6823
6824 /* We want to leave anything reaching TO_X to the caller. */
6825 if ((op & MOVE_TO_X) && new_x > to_x)
6826 {
6827 if (BUFFER_POS_REACHED_P ())
6828 {
6829 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6830 goto buffer_pos_reached;
6831 if (atpos_it.sp < 0)
6832 {
6833 atpos_it = *it;
6834 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6835 }
6836 }
6837 else
6838 {
6839 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6840 {
6841 it->current_x = x;
6842 result = MOVE_X_REACHED;
6843 break;
6844 }
6845 if (atx_it.sp < 0)
6846 {
6847 atx_it = *it;
6848 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6849 }
6850 }
6851 }
6852
6853 if (/* Lines are continued. */
6854 it->line_wrap != TRUNCATE
6855 && (/* And glyph doesn't fit on the line. */
6856 new_x > it->last_visible_x
6857 /* Or it fits exactly and we're on a window
6858 system frame. */
6859 || (new_x == it->last_visible_x
6860 && FRAME_WINDOW_P (it->f))))
6861 {
6862 if (/* IT->hpos == 0 means the very first glyph
6863 doesn't fit on the line, e.g. a wide image. */
6864 it->hpos == 0
6865 || (new_x == it->last_visible_x
6866 && FRAME_WINDOW_P (it->f)))
6867 {
6868 ++it->hpos;
6869 it->current_x = new_x;
6870
6871 /* The character's last glyph just barely fits
6872 in this row. */
6873 if (i == it->nglyphs - 1)
6874 {
6875 /* If this is the destination position,
6876 return a position *before* it in this row,
6877 now that we know it fits in this row. */
6878 if (BUFFER_POS_REACHED_P ())
6879 {
6880 if (it->line_wrap != WORD_WRAP
6881 || wrap_it.sp < 0)
6882 {
6883 it->hpos = hpos_before_this_char;
6884 it->current_x = x_before_this_char;
6885 result = MOVE_POS_MATCH_OR_ZV;
6886 break;
6887 }
6888 if (it->line_wrap == WORD_WRAP
6889 && atpos_it.sp < 0)
6890 {
6891 atpos_it = *it;
6892 atpos_it.current_x = x_before_this_char;
6893 atpos_it.hpos = hpos_before_this_char;
6894 }
6895 }
6896
6897 set_iterator_to_next (it, 1);
6898 /* On graphical terminals, newlines may
6899 "overflow" into the fringe if
6900 overflow-newline-into-fringe is non-nil.
6901 On text-only terminals, newlines may
6902 overflow into the last glyph on the
6903 display line.*/
6904 if (!FRAME_WINDOW_P (it->f)
6905 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6906 {
6907 if (!get_next_display_element (it))
6908 {
6909 result = MOVE_POS_MATCH_OR_ZV;
6910 break;
6911 }
6912 if (BUFFER_POS_REACHED_P ())
6913 {
6914 if (ITERATOR_AT_END_OF_LINE_P (it))
6915 result = MOVE_POS_MATCH_OR_ZV;
6916 else
6917 result = MOVE_LINE_CONTINUED;
6918 break;
6919 }
6920 if (ITERATOR_AT_END_OF_LINE_P (it))
6921 {
6922 result = MOVE_NEWLINE_OR_CR;
6923 break;
6924 }
6925 }
6926 }
6927 }
6928 else
6929 IT_RESET_X_ASCENT_DESCENT (it);
6930
6931 if (wrap_it.sp >= 0)
6932 {
6933 *it = wrap_it;
6934 atpos_it.sp = -1;
6935 atx_it.sp = -1;
6936 }
6937
6938 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6939 IT_CHARPOS (*it)));
6940 result = MOVE_LINE_CONTINUED;
6941 break;
6942 }
6943
6944 if (BUFFER_POS_REACHED_P ())
6945 {
6946 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6947 goto buffer_pos_reached;
6948 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6949 {
6950 atpos_it = *it;
6951 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6952 }
6953 }
6954
6955 if (new_x > it->first_visible_x)
6956 {
6957 /* Glyph is visible. Increment number of glyphs that
6958 would be displayed. */
6959 ++it->hpos;
6960 }
6961 }
6962
6963 if (result != MOVE_UNDEFINED)
6964 break;
6965 }
6966 else if (BUFFER_POS_REACHED_P ())
6967 {
6968 buffer_pos_reached:
6969 IT_RESET_X_ASCENT_DESCENT (it);
6970 result = MOVE_POS_MATCH_OR_ZV;
6971 break;
6972 }
6973 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6974 {
6975 /* Stop when TO_X specified and reached. This check is
6976 necessary here because of lines consisting of a line end,
6977 only. The line end will not produce any glyphs and we
6978 would never get MOVE_X_REACHED. */
6979 xassert (it->nglyphs == 0);
6980 result = MOVE_X_REACHED;
6981 break;
6982 }
6983
6984 /* Is this a line end? If yes, we're done. */
6985 if (ITERATOR_AT_END_OF_LINE_P (it))
6986 {
6987 result = MOVE_NEWLINE_OR_CR;
6988 break;
6989 }
6990
6991 /* The current display element has been consumed. Advance
6992 to the next. */
6993 set_iterator_to_next (it, 1);
6994
6995 /* Stop if lines are truncated and IT's current x-position is
6996 past the right edge of the window now. */
6997 if (it->line_wrap == TRUNCATE
6998 && it->current_x >= it->last_visible_x)
6999 {
7000 if (!FRAME_WINDOW_P (it->f)
7001 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7002 {
7003 if (!get_next_display_element (it)
7004 || BUFFER_POS_REACHED_P ())
7005 {
7006 result = MOVE_POS_MATCH_OR_ZV;
7007 break;
7008 }
7009 if (ITERATOR_AT_END_OF_LINE_P (it))
7010 {
7011 result = MOVE_NEWLINE_OR_CR;
7012 break;
7013 }
7014 }
7015 result = MOVE_LINE_TRUNCATED;
7016 break;
7017 }
7018 #undef IT_RESET_X_ASCENT_DESCENT
7019 }
7020
7021 #undef BUFFER_POS_REACHED_P
7022
7023 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7024 restore the saved iterator. */
7025 if (atpos_it.sp >= 0)
7026 *it = atpos_it;
7027 else if (atx_it.sp >= 0)
7028 *it = atx_it;
7029
7030 done:
7031
7032 /* Restore the iterator settings altered at the beginning of this
7033 function. */
7034 it->glyph_row = saved_glyph_row;
7035 return result;
7036 }
7037
7038 /* For external use. */
7039 void
7040 move_it_in_display_line (struct it *it,
7041 EMACS_INT to_charpos, int to_x,
7042 enum move_operation_enum op)
7043 {
7044 if (it->line_wrap == WORD_WRAP
7045 && (op & MOVE_TO_X))
7046 {
7047 struct it save_it = *it;
7048 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7049 /* When word-wrap is on, TO_X may lie past the end
7050 of a wrapped line. Then it->current is the
7051 character on the next line, so backtrack to the
7052 space before the wrap point. */
7053 if (skip == MOVE_LINE_CONTINUED)
7054 {
7055 int prev_x = max (it->current_x - 1, 0);
7056 *it = save_it;
7057 move_it_in_display_line_to
7058 (it, -1, prev_x, MOVE_TO_X);
7059 }
7060 }
7061 else
7062 move_it_in_display_line_to (it, to_charpos, to_x, op);
7063 }
7064
7065
7066 /* Move IT forward until it satisfies one or more of the criteria in
7067 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7068
7069 OP is a bit-mask that specifies where to stop, and in particular,
7070 which of those four position arguments makes a difference. See the
7071 description of enum move_operation_enum.
7072
7073 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7074 screen line, this function will set IT to the next position >
7075 TO_CHARPOS. */
7076
7077 void
7078 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7079 struct it *it;
7080 int to_charpos, to_x, to_y, to_vpos;
7081 int op;
7082 {
7083 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7084 int line_height, line_start_x = 0, reached = 0;
7085
7086 for (;;)
7087 {
7088 if (op & MOVE_TO_VPOS)
7089 {
7090 /* If no TO_CHARPOS and no TO_X specified, stop at the
7091 start of the line TO_VPOS. */
7092 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7093 {
7094 if (it->vpos == to_vpos)
7095 {
7096 reached = 1;
7097 break;
7098 }
7099 else
7100 skip = move_it_in_display_line_to (it, -1, -1, 0);
7101 }
7102 else
7103 {
7104 /* TO_VPOS >= 0 means stop at TO_X in the line at
7105 TO_VPOS, or at TO_POS, whichever comes first. */
7106 if (it->vpos == to_vpos)
7107 {
7108 reached = 2;
7109 break;
7110 }
7111
7112 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7113
7114 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7115 {
7116 reached = 3;
7117 break;
7118 }
7119 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7120 {
7121 /* We have reached TO_X but not in the line we want. */
7122 skip = move_it_in_display_line_to (it, to_charpos,
7123 -1, MOVE_TO_POS);
7124 if (skip == MOVE_POS_MATCH_OR_ZV)
7125 {
7126 reached = 4;
7127 break;
7128 }
7129 }
7130 }
7131 }
7132 else if (op & MOVE_TO_Y)
7133 {
7134 struct it it_backup;
7135
7136 if (it->line_wrap == WORD_WRAP)
7137 it_backup = *it;
7138
7139 /* TO_Y specified means stop at TO_X in the line containing
7140 TO_Y---or at TO_CHARPOS if this is reached first. The
7141 problem is that we can't really tell whether the line
7142 contains TO_Y before we have completely scanned it, and
7143 this may skip past TO_X. What we do is to first scan to
7144 TO_X.
7145
7146 If TO_X is not specified, use a TO_X of zero. The reason
7147 is to make the outcome of this function more predictable.
7148 If we didn't use TO_X == 0, we would stop at the end of
7149 the line which is probably not what a caller would expect
7150 to happen. */
7151 skip = move_it_in_display_line_to
7152 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7153 (MOVE_TO_X | (op & MOVE_TO_POS)));
7154
7155 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7156 if (skip == MOVE_POS_MATCH_OR_ZV)
7157 reached = 5;
7158 else if (skip == MOVE_X_REACHED)
7159 {
7160 /* If TO_X was reached, we want to know whether TO_Y is
7161 in the line. We know this is the case if the already
7162 scanned glyphs make the line tall enough. Otherwise,
7163 we must check by scanning the rest of the line. */
7164 line_height = it->max_ascent + it->max_descent;
7165 if (to_y >= it->current_y
7166 && to_y < it->current_y + line_height)
7167 {
7168 reached = 6;
7169 break;
7170 }
7171 it_backup = *it;
7172 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7173 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7174 op & MOVE_TO_POS);
7175 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7176 line_height = it->max_ascent + it->max_descent;
7177 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7178
7179 if (to_y >= it->current_y
7180 && to_y < it->current_y + line_height)
7181 {
7182 /* If TO_Y is in this line and TO_X was reached
7183 above, we scanned too far. We have to restore
7184 IT's settings to the ones before skipping. */
7185 *it = it_backup;
7186 reached = 6;
7187 }
7188 else
7189 {
7190 skip = skip2;
7191 if (skip == MOVE_POS_MATCH_OR_ZV)
7192 reached = 7;
7193 }
7194 }
7195 else
7196 {
7197 /* Check whether TO_Y is in this line. */
7198 line_height = it->max_ascent + it->max_descent;
7199 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7200
7201 if (to_y >= it->current_y
7202 && to_y < it->current_y + line_height)
7203 {
7204 /* When word-wrap is on, TO_X may lie past the end
7205 of a wrapped line. Then it->current is the
7206 character on the next line, so backtrack to the
7207 space before the wrap point. */
7208 if (skip == MOVE_LINE_CONTINUED
7209 && it->line_wrap == WORD_WRAP)
7210 {
7211 int prev_x = max (it->current_x - 1, 0);
7212 *it = it_backup;
7213 skip = move_it_in_display_line_to
7214 (it, -1, prev_x, MOVE_TO_X);
7215 }
7216 reached = 6;
7217 }
7218 }
7219
7220 if (reached)
7221 break;
7222 }
7223 else if (BUFFERP (it->object)
7224 && (it->method == GET_FROM_BUFFER
7225 || it->method == GET_FROM_STRETCH)
7226 && IT_CHARPOS (*it) >= to_charpos)
7227 skip = MOVE_POS_MATCH_OR_ZV;
7228 else
7229 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7230
7231 switch (skip)
7232 {
7233 case MOVE_POS_MATCH_OR_ZV:
7234 reached = 8;
7235 goto out;
7236
7237 case MOVE_NEWLINE_OR_CR:
7238 set_iterator_to_next (it, 1);
7239 it->continuation_lines_width = 0;
7240 break;
7241
7242 case MOVE_LINE_TRUNCATED:
7243 it->continuation_lines_width = 0;
7244 reseat_at_next_visible_line_start (it, 0);
7245 if ((op & MOVE_TO_POS) != 0
7246 && IT_CHARPOS (*it) > to_charpos)
7247 {
7248 reached = 9;
7249 goto out;
7250 }
7251 break;
7252
7253 case MOVE_LINE_CONTINUED:
7254 /* For continued lines ending in a tab, some of the glyphs
7255 associated with the tab are displayed on the current
7256 line. Since it->current_x does not include these glyphs,
7257 we use it->last_visible_x instead. */
7258 if (it->c == '\t')
7259 {
7260 it->continuation_lines_width += it->last_visible_x;
7261 /* When moving by vpos, ensure that the iterator really
7262 advances to the next line (bug#847, bug#969). Fixme:
7263 do we need to do this in other circumstances? */
7264 if (it->current_x != it->last_visible_x
7265 && (op & MOVE_TO_VPOS)
7266 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7267 {
7268 line_start_x = it->current_x + it->pixel_width
7269 - it->last_visible_x;
7270 set_iterator_to_next (it, 0);
7271 }
7272 }
7273 else
7274 it->continuation_lines_width += it->current_x;
7275 break;
7276
7277 default:
7278 abort ();
7279 }
7280
7281 /* Reset/increment for the next run. */
7282 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7283 it->current_x = line_start_x;
7284 line_start_x = 0;
7285 it->hpos = 0;
7286 it->current_y += it->max_ascent + it->max_descent;
7287 ++it->vpos;
7288 last_height = it->max_ascent + it->max_descent;
7289 last_max_ascent = it->max_ascent;
7290 it->max_ascent = it->max_descent = 0;
7291 }
7292
7293 out:
7294
7295 /* On text terminals, we may stop at the end of a line in the middle
7296 of a multi-character glyph. If the glyph itself is continued,
7297 i.e. it is actually displayed on the next line, don't treat this
7298 stopping point as valid; move to the next line instead (unless
7299 that brings us offscreen). */
7300 if (!FRAME_WINDOW_P (it->f)
7301 && op & MOVE_TO_POS
7302 && IT_CHARPOS (*it) == to_charpos
7303 && it->what == IT_CHARACTER
7304 && it->nglyphs > 1
7305 && it->line_wrap == WINDOW_WRAP
7306 && it->current_x == it->last_visible_x - 1
7307 && it->c != '\n'
7308 && it->c != '\t'
7309 && it->vpos < XFASTINT (it->w->window_end_vpos))
7310 {
7311 it->continuation_lines_width += it->current_x;
7312 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7313 it->current_y += it->max_ascent + it->max_descent;
7314 ++it->vpos;
7315 last_height = it->max_ascent + it->max_descent;
7316 last_max_ascent = it->max_ascent;
7317 }
7318
7319 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7320 }
7321
7322
7323 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7324
7325 If DY > 0, move IT backward at least that many pixels. DY = 0
7326 means move IT backward to the preceding line start or BEGV. This
7327 function may move over more than DY pixels if IT->current_y - DY
7328 ends up in the middle of a line; in this case IT->current_y will be
7329 set to the top of the line moved to. */
7330
7331 void
7332 move_it_vertically_backward (it, dy)
7333 struct it *it;
7334 int dy;
7335 {
7336 int nlines, h;
7337 struct it it2, it3;
7338 int start_pos;
7339
7340 move_further_back:
7341 xassert (dy >= 0);
7342
7343 start_pos = IT_CHARPOS (*it);
7344
7345 /* Estimate how many newlines we must move back. */
7346 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7347
7348 /* Set the iterator's position that many lines back. */
7349 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7350 back_to_previous_visible_line_start (it);
7351
7352 /* Reseat the iterator here. When moving backward, we don't want
7353 reseat to skip forward over invisible text, set up the iterator
7354 to deliver from overlay strings at the new position etc. So,
7355 use reseat_1 here. */
7356 reseat_1 (it, it->current.pos, 1);
7357
7358 /* We are now surely at a line start. */
7359 it->current_x = it->hpos = 0;
7360 it->continuation_lines_width = 0;
7361
7362 /* Move forward and see what y-distance we moved. First move to the
7363 start of the next line so that we get its height. We need this
7364 height to be able to tell whether we reached the specified
7365 y-distance. */
7366 it2 = *it;
7367 it2.max_ascent = it2.max_descent = 0;
7368 do
7369 {
7370 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7371 MOVE_TO_POS | MOVE_TO_VPOS);
7372 }
7373 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7374 xassert (IT_CHARPOS (*it) >= BEGV);
7375 it3 = it2;
7376
7377 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7378 xassert (IT_CHARPOS (*it) >= BEGV);
7379 /* H is the actual vertical distance from the position in *IT
7380 and the starting position. */
7381 h = it2.current_y - it->current_y;
7382 /* NLINES is the distance in number of lines. */
7383 nlines = it2.vpos - it->vpos;
7384
7385 /* Correct IT's y and vpos position
7386 so that they are relative to the starting point. */
7387 it->vpos -= nlines;
7388 it->current_y -= h;
7389
7390 if (dy == 0)
7391 {
7392 /* DY == 0 means move to the start of the screen line. The
7393 value of nlines is > 0 if continuation lines were involved. */
7394 if (nlines > 0)
7395 move_it_by_lines (it, nlines, 1);
7396 }
7397 else
7398 {
7399 /* The y-position we try to reach, relative to *IT.
7400 Note that H has been subtracted in front of the if-statement. */
7401 int target_y = it->current_y + h - dy;
7402 int y0 = it3.current_y;
7403 int y1 = line_bottom_y (&it3);
7404 int line_height = y1 - y0;
7405
7406 /* If we did not reach target_y, try to move further backward if
7407 we can. If we moved too far backward, try to move forward. */
7408 if (target_y < it->current_y
7409 /* This is heuristic. In a window that's 3 lines high, with
7410 a line height of 13 pixels each, recentering with point
7411 on the bottom line will try to move -39/2 = 19 pixels
7412 backward. Try to avoid moving into the first line. */
7413 && (it->current_y - target_y
7414 > min (window_box_height (it->w), line_height * 2 / 3))
7415 && IT_CHARPOS (*it) > BEGV)
7416 {
7417 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7418 target_y - it->current_y));
7419 dy = it->current_y - target_y;
7420 goto move_further_back;
7421 }
7422 else if (target_y >= it->current_y + line_height
7423 && IT_CHARPOS (*it) < ZV)
7424 {
7425 /* Should move forward by at least one line, maybe more.
7426
7427 Note: Calling move_it_by_lines can be expensive on
7428 terminal frames, where compute_motion is used (via
7429 vmotion) to do the job, when there are very long lines
7430 and truncate-lines is nil. That's the reason for
7431 treating terminal frames specially here. */
7432
7433 if (!FRAME_WINDOW_P (it->f))
7434 move_it_vertically (it, target_y - (it->current_y + line_height));
7435 else
7436 {
7437 do
7438 {
7439 move_it_by_lines (it, 1, 1);
7440 }
7441 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7442 }
7443 }
7444 }
7445 }
7446
7447
7448 /* Move IT by a specified amount of pixel lines DY. DY negative means
7449 move backwards. DY = 0 means move to start of screen line. At the
7450 end, IT will be on the start of a screen line. */
7451
7452 void
7453 move_it_vertically (it, dy)
7454 struct it *it;
7455 int dy;
7456 {
7457 if (dy <= 0)
7458 move_it_vertically_backward (it, -dy);
7459 else
7460 {
7461 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7462 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7463 MOVE_TO_POS | MOVE_TO_Y);
7464 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7465
7466 /* If buffer ends in ZV without a newline, move to the start of
7467 the line to satisfy the post-condition. */
7468 if (IT_CHARPOS (*it) == ZV
7469 && ZV > BEGV
7470 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7471 move_it_by_lines (it, 0, 0);
7472 }
7473 }
7474
7475
7476 /* Move iterator IT past the end of the text line it is in. */
7477
7478 void
7479 move_it_past_eol (it)
7480 struct it *it;
7481 {
7482 enum move_it_result rc;
7483
7484 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7485 if (rc == MOVE_NEWLINE_OR_CR)
7486 set_iterator_to_next (it, 0);
7487 }
7488
7489
7490 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7491 negative means move up. DVPOS == 0 means move to the start of the
7492 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7493 NEED_Y_P is zero, IT->current_y will be left unchanged.
7494
7495 Further optimization ideas: If we would know that IT->f doesn't use
7496 a face with proportional font, we could be faster for
7497 truncate-lines nil. */
7498
7499 void
7500 move_it_by_lines (it, dvpos, need_y_p)
7501 struct it *it;
7502 int dvpos, need_y_p;
7503 {
7504 struct position pos;
7505
7506 /* The commented-out optimization uses vmotion on terminals. This
7507 gives bad results, because elements like it->what, on which
7508 callers such as pos_visible_p rely, aren't updated. */
7509 /* if (!FRAME_WINDOW_P (it->f))
7510 {
7511 struct text_pos textpos;
7512
7513 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7514 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7515 reseat (it, textpos, 1);
7516 it->vpos += pos.vpos;
7517 it->current_y += pos.vpos;
7518 }
7519 else */
7520
7521 if (dvpos == 0)
7522 {
7523 /* DVPOS == 0 means move to the start of the screen line. */
7524 move_it_vertically_backward (it, 0);
7525 xassert (it->current_x == 0 && it->hpos == 0);
7526 /* Let next call to line_bottom_y calculate real line height */
7527 last_height = 0;
7528 }
7529 else if (dvpos > 0)
7530 {
7531 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7532 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7533 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7534 }
7535 else
7536 {
7537 struct it it2;
7538 int start_charpos, i;
7539
7540 /* Start at the beginning of the screen line containing IT's
7541 position. This may actually move vertically backwards,
7542 in case of overlays, so adjust dvpos accordingly. */
7543 dvpos += it->vpos;
7544 move_it_vertically_backward (it, 0);
7545 dvpos -= it->vpos;
7546
7547 /* Go back -DVPOS visible lines and reseat the iterator there. */
7548 start_charpos = IT_CHARPOS (*it);
7549 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7550 back_to_previous_visible_line_start (it);
7551 reseat (it, it->current.pos, 1);
7552
7553 /* Move further back if we end up in a string or an image. */
7554 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7555 {
7556 /* First try to move to start of display line. */
7557 dvpos += it->vpos;
7558 move_it_vertically_backward (it, 0);
7559 dvpos -= it->vpos;
7560 if (IT_POS_VALID_AFTER_MOVE_P (it))
7561 break;
7562 /* If start of line is still in string or image,
7563 move further back. */
7564 back_to_previous_visible_line_start (it);
7565 reseat (it, it->current.pos, 1);
7566 dvpos--;
7567 }
7568
7569 it->current_x = it->hpos = 0;
7570
7571 /* Above call may have moved too far if continuation lines
7572 are involved. Scan forward and see if it did. */
7573 it2 = *it;
7574 it2.vpos = it2.current_y = 0;
7575 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7576 it->vpos -= it2.vpos;
7577 it->current_y -= it2.current_y;
7578 it->current_x = it->hpos = 0;
7579
7580 /* If we moved too far back, move IT some lines forward. */
7581 if (it2.vpos > -dvpos)
7582 {
7583 int delta = it2.vpos + dvpos;
7584 it2 = *it;
7585 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7586 /* Move back again if we got too far ahead. */
7587 if (IT_CHARPOS (*it) >= start_charpos)
7588 *it = it2;
7589 }
7590 }
7591 }
7592
7593 /* Return 1 if IT points into the middle of a display vector. */
7594
7595 int
7596 in_display_vector_p (it)
7597 struct it *it;
7598 {
7599 return (it->method == GET_FROM_DISPLAY_VECTOR
7600 && it->current.dpvec_index > 0
7601 && it->dpvec + it->current.dpvec_index != it->dpend);
7602 }
7603
7604 \f
7605 /***********************************************************************
7606 Messages
7607 ***********************************************************************/
7608
7609
7610 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7611 to *Messages*. */
7612
7613 void
7614 add_to_log (format, arg1, arg2)
7615 char *format;
7616 Lisp_Object arg1, arg2;
7617 {
7618 Lisp_Object args[3];
7619 Lisp_Object msg, fmt;
7620 char *buffer;
7621 int len;
7622 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7623 USE_SAFE_ALLOCA;
7624
7625 /* Do nothing if called asynchronously. Inserting text into
7626 a buffer may call after-change-functions and alike and
7627 that would means running Lisp asynchronously. */
7628 if (handling_signal)
7629 return;
7630
7631 fmt = msg = Qnil;
7632 GCPRO4 (fmt, msg, arg1, arg2);
7633
7634 args[0] = fmt = build_string (format);
7635 args[1] = arg1;
7636 args[2] = arg2;
7637 msg = Fformat (3, args);
7638
7639 len = SBYTES (msg) + 1;
7640 SAFE_ALLOCA (buffer, char *, len);
7641 bcopy (SDATA (msg), buffer, len);
7642
7643 message_dolog (buffer, len - 1, 1, 0);
7644 SAFE_FREE ();
7645
7646 UNGCPRO;
7647 }
7648
7649
7650 /* Output a newline in the *Messages* buffer if "needs" one. */
7651
7652 void
7653 message_log_maybe_newline ()
7654 {
7655 if (message_log_need_newline)
7656 message_dolog ("", 0, 1, 0);
7657 }
7658
7659
7660 /* Add a string M of length NBYTES to the message log, optionally
7661 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7662 nonzero, means interpret the contents of M as multibyte. This
7663 function calls low-level routines in order to bypass text property
7664 hooks, etc. which might not be safe to run.
7665
7666 This may GC (insert may run before/after change hooks),
7667 so the buffer M must NOT point to a Lisp string. */
7668
7669 void
7670 message_dolog (m, nbytes, nlflag, multibyte)
7671 const char *m;
7672 int nbytes, nlflag, multibyte;
7673 {
7674 if (!NILP (Vmemory_full))
7675 return;
7676
7677 if (!NILP (Vmessage_log_max))
7678 {
7679 struct buffer *oldbuf;
7680 Lisp_Object oldpoint, oldbegv, oldzv;
7681 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7682 int point_at_end = 0;
7683 int zv_at_end = 0;
7684 Lisp_Object old_deactivate_mark, tem;
7685 struct gcpro gcpro1;
7686
7687 old_deactivate_mark = Vdeactivate_mark;
7688 oldbuf = current_buffer;
7689 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7690 current_buffer->undo_list = Qt;
7691
7692 oldpoint = message_dolog_marker1;
7693 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7694 oldbegv = message_dolog_marker2;
7695 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7696 oldzv = message_dolog_marker3;
7697 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7698 GCPRO1 (old_deactivate_mark);
7699
7700 if (PT == Z)
7701 point_at_end = 1;
7702 if (ZV == Z)
7703 zv_at_end = 1;
7704
7705 BEGV = BEG;
7706 BEGV_BYTE = BEG_BYTE;
7707 ZV = Z;
7708 ZV_BYTE = Z_BYTE;
7709 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7710
7711 /* Insert the string--maybe converting multibyte to single byte
7712 or vice versa, so that all the text fits the buffer. */
7713 if (multibyte
7714 && NILP (current_buffer->enable_multibyte_characters))
7715 {
7716 int i, c, char_bytes;
7717 unsigned char work[1];
7718
7719 /* Convert a multibyte string to single-byte
7720 for the *Message* buffer. */
7721 for (i = 0; i < nbytes; i += char_bytes)
7722 {
7723 c = string_char_and_length (m + i, &char_bytes);
7724 work[0] = (ASCII_CHAR_P (c)
7725 ? c
7726 : multibyte_char_to_unibyte (c, Qnil));
7727 insert_1_both (work, 1, 1, 1, 0, 0);
7728 }
7729 }
7730 else if (! multibyte
7731 && ! NILP (current_buffer->enable_multibyte_characters))
7732 {
7733 int i, c, char_bytes;
7734 unsigned char *msg = (unsigned char *) m;
7735 unsigned char str[MAX_MULTIBYTE_LENGTH];
7736 /* Convert a single-byte string to multibyte
7737 for the *Message* buffer. */
7738 for (i = 0; i < nbytes; i++)
7739 {
7740 c = msg[i];
7741 MAKE_CHAR_MULTIBYTE (c);
7742 char_bytes = CHAR_STRING (c, str);
7743 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7744 }
7745 }
7746 else if (nbytes)
7747 insert_1 (m, nbytes, 1, 0, 0);
7748
7749 if (nlflag)
7750 {
7751 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7752 insert_1 ("\n", 1, 1, 0, 0);
7753
7754 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7755 this_bol = PT;
7756 this_bol_byte = PT_BYTE;
7757
7758 /* See if this line duplicates the previous one.
7759 If so, combine duplicates. */
7760 if (this_bol > BEG)
7761 {
7762 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7763 prev_bol = PT;
7764 prev_bol_byte = PT_BYTE;
7765
7766 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7767 this_bol, this_bol_byte);
7768 if (dup)
7769 {
7770 del_range_both (prev_bol, prev_bol_byte,
7771 this_bol, this_bol_byte, 0);
7772 if (dup > 1)
7773 {
7774 char dupstr[40];
7775 int duplen;
7776
7777 /* If you change this format, don't forget to also
7778 change message_log_check_duplicate. */
7779 sprintf (dupstr, " [%d times]", dup);
7780 duplen = strlen (dupstr);
7781 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7782 insert_1 (dupstr, duplen, 1, 0, 1);
7783 }
7784 }
7785 }
7786
7787 /* If we have more than the desired maximum number of lines
7788 in the *Messages* buffer now, delete the oldest ones.
7789 This is safe because we don't have undo in this buffer. */
7790
7791 if (NATNUMP (Vmessage_log_max))
7792 {
7793 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7794 -XFASTINT (Vmessage_log_max) - 1, 0);
7795 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7796 }
7797 }
7798 BEGV = XMARKER (oldbegv)->charpos;
7799 BEGV_BYTE = marker_byte_position (oldbegv);
7800
7801 if (zv_at_end)
7802 {
7803 ZV = Z;
7804 ZV_BYTE = Z_BYTE;
7805 }
7806 else
7807 {
7808 ZV = XMARKER (oldzv)->charpos;
7809 ZV_BYTE = marker_byte_position (oldzv);
7810 }
7811
7812 if (point_at_end)
7813 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7814 else
7815 /* We can't do Fgoto_char (oldpoint) because it will run some
7816 Lisp code. */
7817 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7818 XMARKER (oldpoint)->bytepos);
7819
7820 UNGCPRO;
7821 unchain_marker (XMARKER (oldpoint));
7822 unchain_marker (XMARKER (oldbegv));
7823 unchain_marker (XMARKER (oldzv));
7824
7825 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7826 set_buffer_internal (oldbuf);
7827 if (NILP (tem))
7828 windows_or_buffers_changed = old_windows_or_buffers_changed;
7829 message_log_need_newline = !nlflag;
7830 Vdeactivate_mark = old_deactivate_mark;
7831 }
7832 }
7833
7834
7835 /* We are at the end of the buffer after just having inserted a newline.
7836 (Note: We depend on the fact we won't be crossing the gap.)
7837 Check to see if the most recent message looks a lot like the previous one.
7838 Return 0 if different, 1 if the new one should just replace it, or a
7839 value N > 1 if we should also append " [N times]". */
7840
7841 static int
7842 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7843 int prev_bol, this_bol;
7844 int prev_bol_byte, this_bol_byte;
7845 {
7846 int i;
7847 int len = Z_BYTE - 1 - this_bol_byte;
7848 int seen_dots = 0;
7849 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7850 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7851
7852 for (i = 0; i < len; i++)
7853 {
7854 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7855 seen_dots = 1;
7856 if (p1[i] != p2[i])
7857 return seen_dots;
7858 }
7859 p1 += len;
7860 if (*p1 == '\n')
7861 return 2;
7862 if (*p1++ == ' ' && *p1++ == '[')
7863 {
7864 int n = 0;
7865 while (*p1 >= '0' && *p1 <= '9')
7866 n = n * 10 + *p1++ - '0';
7867 if (strncmp (p1, " times]\n", 8) == 0)
7868 return n+1;
7869 }
7870 return 0;
7871 }
7872 \f
7873
7874 /* Display an echo area message M with a specified length of NBYTES
7875 bytes. The string may include null characters. If M is 0, clear
7876 out any existing message, and let the mini-buffer text show
7877 through.
7878
7879 This may GC, so the buffer M must NOT point to a Lisp string. */
7880
7881 void
7882 message2 (m, nbytes, multibyte)
7883 const char *m;
7884 int nbytes;
7885 int multibyte;
7886 {
7887 /* First flush out any partial line written with print. */
7888 message_log_maybe_newline ();
7889 if (m)
7890 message_dolog (m, nbytes, 1, multibyte);
7891 message2_nolog (m, nbytes, multibyte);
7892 }
7893
7894
7895 /* The non-logging counterpart of message2. */
7896
7897 void
7898 message2_nolog (m, nbytes, multibyte)
7899 const char *m;
7900 int nbytes, multibyte;
7901 {
7902 struct frame *sf = SELECTED_FRAME ();
7903 message_enable_multibyte = multibyte;
7904
7905 if (FRAME_INITIAL_P (sf))
7906 {
7907 if (noninteractive_need_newline)
7908 putc ('\n', stderr);
7909 noninteractive_need_newline = 0;
7910 if (m)
7911 fwrite (m, nbytes, 1, stderr);
7912 if (cursor_in_echo_area == 0)
7913 fprintf (stderr, "\n");
7914 fflush (stderr);
7915 }
7916 /* A null message buffer means that the frame hasn't really been
7917 initialized yet. Error messages get reported properly by
7918 cmd_error, so this must be just an informative message; toss it. */
7919 else if (INTERACTIVE
7920 && sf->glyphs_initialized_p
7921 && FRAME_MESSAGE_BUF (sf))
7922 {
7923 Lisp_Object mini_window;
7924 struct frame *f;
7925
7926 /* Get the frame containing the mini-buffer
7927 that the selected frame is using. */
7928 mini_window = FRAME_MINIBUF_WINDOW (sf);
7929 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7930
7931 FRAME_SAMPLE_VISIBILITY (f);
7932 if (FRAME_VISIBLE_P (sf)
7933 && ! FRAME_VISIBLE_P (f))
7934 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7935
7936 if (m)
7937 {
7938 set_message (m, Qnil, nbytes, multibyte);
7939 if (minibuffer_auto_raise)
7940 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7941 }
7942 else
7943 clear_message (1, 1);
7944
7945 do_pending_window_change (0);
7946 echo_area_display (1);
7947 do_pending_window_change (0);
7948 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7949 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7950 }
7951 }
7952
7953
7954 /* Display an echo area message M with a specified length of NBYTES
7955 bytes. The string may include null characters. If M is not a
7956 string, clear out any existing message, and let the mini-buffer
7957 text show through.
7958
7959 This function cancels echoing. */
7960
7961 void
7962 message3 (m, nbytes, multibyte)
7963 Lisp_Object m;
7964 int nbytes;
7965 int multibyte;
7966 {
7967 struct gcpro gcpro1;
7968
7969 GCPRO1 (m);
7970 clear_message (1,1);
7971 cancel_echoing ();
7972
7973 /* First flush out any partial line written with print. */
7974 message_log_maybe_newline ();
7975 if (STRINGP (m))
7976 {
7977 char *buffer;
7978 USE_SAFE_ALLOCA;
7979
7980 SAFE_ALLOCA (buffer, char *, nbytes);
7981 bcopy (SDATA (m), buffer, nbytes);
7982 message_dolog (buffer, nbytes, 1, multibyte);
7983 SAFE_FREE ();
7984 }
7985 message3_nolog (m, nbytes, multibyte);
7986
7987 UNGCPRO;
7988 }
7989
7990
7991 /* The non-logging version of message3.
7992 This does not cancel echoing, because it is used for echoing.
7993 Perhaps we need to make a separate function for echoing
7994 and make this cancel echoing. */
7995
7996 void
7997 message3_nolog (m, nbytes, multibyte)
7998 Lisp_Object m;
7999 int nbytes, multibyte;
8000 {
8001 struct frame *sf = SELECTED_FRAME ();
8002 message_enable_multibyte = multibyte;
8003
8004 if (FRAME_INITIAL_P (sf))
8005 {
8006 if (noninteractive_need_newline)
8007 putc ('\n', stderr);
8008 noninteractive_need_newline = 0;
8009 if (STRINGP (m))
8010 fwrite (SDATA (m), nbytes, 1, stderr);
8011 if (cursor_in_echo_area == 0)
8012 fprintf (stderr, "\n");
8013 fflush (stderr);
8014 }
8015 /* A null message buffer means that the frame hasn't really been
8016 initialized yet. Error messages get reported properly by
8017 cmd_error, so this must be just an informative message; toss it. */
8018 else if (INTERACTIVE
8019 && sf->glyphs_initialized_p
8020 && FRAME_MESSAGE_BUF (sf))
8021 {
8022 Lisp_Object mini_window;
8023 Lisp_Object frame;
8024 struct frame *f;
8025
8026 /* Get the frame containing the mini-buffer
8027 that the selected frame is using. */
8028 mini_window = FRAME_MINIBUF_WINDOW (sf);
8029 frame = XWINDOW (mini_window)->frame;
8030 f = XFRAME (frame);
8031
8032 FRAME_SAMPLE_VISIBILITY (f);
8033 if (FRAME_VISIBLE_P (sf)
8034 && !FRAME_VISIBLE_P (f))
8035 Fmake_frame_visible (frame);
8036
8037 if (STRINGP (m) && SCHARS (m) > 0)
8038 {
8039 set_message (NULL, m, nbytes, multibyte);
8040 if (minibuffer_auto_raise)
8041 Fraise_frame (frame);
8042 /* Assume we are not echoing.
8043 (If we are, echo_now will override this.) */
8044 echo_message_buffer = Qnil;
8045 }
8046 else
8047 clear_message (1, 1);
8048
8049 do_pending_window_change (0);
8050 echo_area_display (1);
8051 do_pending_window_change (0);
8052 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8053 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8054 }
8055 }
8056
8057
8058 /* Display a null-terminated echo area message M. If M is 0, clear
8059 out any existing message, and let the mini-buffer text show through.
8060
8061 The buffer M must continue to exist until after the echo area gets
8062 cleared or some other message gets displayed there. Do not pass
8063 text that is stored in a Lisp string. Do not pass text in a buffer
8064 that was alloca'd. */
8065
8066 void
8067 message1 (m)
8068 char *m;
8069 {
8070 message2 (m, (m ? strlen (m) : 0), 0);
8071 }
8072
8073
8074 /* The non-logging counterpart of message1. */
8075
8076 void
8077 message1_nolog (m)
8078 char *m;
8079 {
8080 message2_nolog (m, (m ? strlen (m) : 0), 0);
8081 }
8082
8083 /* Display a message M which contains a single %s
8084 which gets replaced with STRING. */
8085
8086 void
8087 message_with_string (m, string, log)
8088 char *m;
8089 Lisp_Object string;
8090 int log;
8091 {
8092 CHECK_STRING (string);
8093
8094 if (noninteractive)
8095 {
8096 if (m)
8097 {
8098 if (noninteractive_need_newline)
8099 putc ('\n', stderr);
8100 noninteractive_need_newline = 0;
8101 fprintf (stderr, m, SDATA (string));
8102 if (!cursor_in_echo_area)
8103 fprintf (stderr, "\n");
8104 fflush (stderr);
8105 }
8106 }
8107 else if (INTERACTIVE)
8108 {
8109 /* The frame whose minibuffer we're going to display the message on.
8110 It may be larger than the selected frame, so we need
8111 to use its buffer, not the selected frame's buffer. */
8112 Lisp_Object mini_window;
8113 struct frame *f, *sf = SELECTED_FRAME ();
8114
8115 /* Get the frame containing the minibuffer
8116 that the selected frame is using. */
8117 mini_window = FRAME_MINIBUF_WINDOW (sf);
8118 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8119
8120 /* A null message buffer means that the frame hasn't really been
8121 initialized yet. Error messages get reported properly by
8122 cmd_error, so this must be just an informative message; toss it. */
8123 if (FRAME_MESSAGE_BUF (f))
8124 {
8125 Lisp_Object args[2], message;
8126 struct gcpro gcpro1, gcpro2;
8127
8128 args[0] = build_string (m);
8129 args[1] = message = string;
8130 GCPRO2 (args[0], message);
8131 gcpro1.nvars = 2;
8132
8133 message = Fformat (2, args);
8134
8135 if (log)
8136 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8137 else
8138 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8139
8140 UNGCPRO;
8141
8142 /* Print should start at the beginning of the message
8143 buffer next time. */
8144 message_buf_print = 0;
8145 }
8146 }
8147 }
8148
8149
8150 /* Dump an informative message to the minibuf. If M is 0, clear out
8151 any existing message, and let the mini-buffer text show through. */
8152
8153 /* VARARGS 1 */
8154 void
8155 message (m, a1, a2, a3)
8156 char *m;
8157 EMACS_INT a1, a2, a3;
8158 {
8159 if (noninteractive)
8160 {
8161 if (m)
8162 {
8163 if (noninteractive_need_newline)
8164 putc ('\n', stderr);
8165 noninteractive_need_newline = 0;
8166 fprintf (stderr, m, a1, a2, a3);
8167 if (cursor_in_echo_area == 0)
8168 fprintf (stderr, "\n");
8169 fflush (stderr);
8170 }
8171 }
8172 else if (INTERACTIVE)
8173 {
8174 /* The frame whose mini-buffer we're going to display the message
8175 on. It may be larger than the selected frame, so we need to
8176 use its buffer, not the selected frame's buffer. */
8177 Lisp_Object mini_window;
8178 struct frame *f, *sf = SELECTED_FRAME ();
8179
8180 /* Get the frame containing the mini-buffer
8181 that the selected frame is using. */
8182 mini_window = FRAME_MINIBUF_WINDOW (sf);
8183 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8184
8185 /* A null message buffer means that the frame hasn't really been
8186 initialized yet. Error messages get reported properly by
8187 cmd_error, so this must be just an informative message; toss
8188 it. */
8189 if (FRAME_MESSAGE_BUF (f))
8190 {
8191 if (m)
8192 {
8193 int len;
8194 #ifdef NO_ARG_ARRAY
8195 char *a[3];
8196 a[0] = (char *) a1;
8197 a[1] = (char *) a2;
8198 a[2] = (char *) a3;
8199
8200 len = doprnt (FRAME_MESSAGE_BUF (f),
8201 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8202 #else
8203 len = doprnt (FRAME_MESSAGE_BUF (f),
8204 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8205 (char **) &a1);
8206 #endif /* NO_ARG_ARRAY */
8207
8208 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8209 }
8210 else
8211 message1 (0);
8212
8213 /* Print should start at the beginning of the message
8214 buffer next time. */
8215 message_buf_print = 0;
8216 }
8217 }
8218 }
8219
8220
8221 /* The non-logging version of message. */
8222
8223 void
8224 message_nolog (m, a1, a2, a3)
8225 char *m;
8226 EMACS_INT a1, a2, a3;
8227 {
8228 Lisp_Object old_log_max;
8229 old_log_max = Vmessage_log_max;
8230 Vmessage_log_max = Qnil;
8231 message (m, a1, a2, a3);
8232 Vmessage_log_max = old_log_max;
8233 }
8234
8235
8236 /* Display the current message in the current mini-buffer. This is
8237 only called from error handlers in process.c, and is not time
8238 critical. */
8239
8240 void
8241 update_echo_area ()
8242 {
8243 if (!NILP (echo_area_buffer[0]))
8244 {
8245 Lisp_Object string;
8246 string = Fcurrent_message ();
8247 message3 (string, SBYTES (string),
8248 !NILP (current_buffer->enable_multibyte_characters));
8249 }
8250 }
8251
8252
8253 /* Make sure echo area buffers in `echo_buffers' are live.
8254 If they aren't, make new ones. */
8255
8256 static void
8257 ensure_echo_area_buffers ()
8258 {
8259 int i;
8260
8261 for (i = 0; i < 2; ++i)
8262 if (!BUFFERP (echo_buffer[i])
8263 || NILP (XBUFFER (echo_buffer[i])->name))
8264 {
8265 char name[30];
8266 Lisp_Object old_buffer;
8267 int j;
8268
8269 old_buffer = echo_buffer[i];
8270 sprintf (name, " *Echo Area %d*", i);
8271 echo_buffer[i] = Fget_buffer_create (build_string (name));
8272 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8273 /* to force word wrap in echo area -
8274 it was decided to postpone this*/
8275 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8276
8277 for (j = 0; j < 2; ++j)
8278 if (EQ (old_buffer, echo_area_buffer[j]))
8279 echo_area_buffer[j] = echo_buffer[i];
8280 }
8281 }
8282
8283
8284 /* Call FN with args A1..A4 with either the current or last displayed
8285 echo_area_buffer as current buffer.
8286
8287 WHICH zero means use the current message buffer
8288 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8289 from echo_buffer[] and clear it.
8290
8291 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8292 suitable buffer from echo_buffer[] and clear it.
8293
8294 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8295 that the current message becomes the last displayed one, make
8296 choose a suitable buffer for echo_area_buffer[0], and clear it.
8297
8298 Value is what FN returns. */
8299
8300 static int
8301 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8302 struct window *w;
8303 int which;
8304 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8305 EMACS_INT a1;
8306 Lisp_Object a2;
8307 EMACS_INT a3, a4;
8308 {
8309 Lisp_Object buffer;
8310 int this_one, the_other, clear_buffer_p, rc;
8311 int count = SPECPDL_INDEX ();
8312
8313 /* If buffers aren't live, make new ones. */
8314 ensure_echo_area_buffers ();
8315
8316 clear_buffer_p = 0;
8317
8318 if (which == 0)
8319 this_one = 0, the_other = 1;
8320 else if (which > 0)
8321 this_one = 1, the_other = 0;
8322 else
8323 {
8324 this_one = 0, the_other = 1;
8325 clear_buffer_p = 1;
8326
8327 /* We need a fresh one in case the current echo buffer equals
8328 the one containing the last displayed echo area message. */
8329 if (!NILP (echo_area_buffer[this_one])
8330 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8331 echo_area_buffer[this_one] = Qnil;
8332 }
8333
8334 /* Choose a suitable buffer from echo_buffer[] is we don't
8335 have one. */
8336 if (NILP (echo_area_buffer[this_one]))
8337 {
8338 echo_area_buffer[this_one]
8339 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8340 ? echo_buffer[the_other]
8341 : echo_buffer[this_one]);
8342 clear_buffer_p = 1;
8343 }
8344
8345 buffer = echo_area_buffer[this_one];
8346
8347 /* Don't get confused by reusing the buffer used for echoing
8348 for a different purpose. */
8349 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8350 cancel_echoing ();
8351
8352 record_unwind_protect (unwind_with_echo_area_buffer,
8353 with_echo_area_buffer_unwind_data (w));
8354
8355 /* Make the echo area buffer current. Note that for display
8356 purposes, it is not necessary that the displayed window's buffer
8357 == current_buffer, except for text property lookup. So, let's
8358 only set that buffer temporarily here without doing a full
8359 Fset_window_buffer. We must also change w->pointm, though,
8360 because otherwise an assertions in unshow_buffer fails, and Emacs
8361 aborts. */
8362 set_buffer_internal_1 (XBUFFER (buffer));
8363 if (w)
8364 {
8365 w->buffer = buffer;
8366 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8367 }
8368
8369 current_buffer->undo_list = Qt;
8370 current_buffer->read_only = Qnil;
8371 specbind (Qinhibit_read_only, Qt);
8372 specbind (Qinhibit_modification_hooks, Qt);
8373
8374 if (clear_buffer_p && Z > BEG)
8375 del_range (BEG, Z);
8376
8377 xassert (BEGV >= BEG);
8378 xassert (ZV <= Z && ZV >= BEGV);
8379
8380 rc = fn (a1, a2, a3, a4);
8381
8382 xassert (BEGV >= BEG);
8383 xassert (ZV <= Z && ZV >= BEGV);
8384
8385 unbind_to (count, Qnil);
8386 return rc;
8387 }
8388
8389
8390 /* Save state that should be preserved around the call to the function
8391 FN called in with_echo_area_buffer. */
8392
8393 static Lisp_Object
8394 with_echo_area_buffer_unwind_data (w)
8395 struct window *w;
8396 {
8397 int i = 0;
8398 Lisp_Object vector, tmp;
8399
8400 /* Reduce consing by keeping one vector in
8401 Vwith_echo_area_save_vector. */
8402 vector = Vwith_echo_area_save_vector;
8403 Vwith_echo_area_save_vector = Qnil;
8404
8405 if (NILP (vector))
8406 vector = Fmake_vector (make_number (7), Qnil);
8407
8408 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8409 ASET (vector, i, Vdeactivate_mark); ++i;
8410 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8411
8412 if (w)
8413 {
8414 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8415 ASET (vector, i, w->buffer); ++i;
8416 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8417 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8418 }
8419 else
8420 {
8421 int end = i + 4;
8422 for (; i < end; ++i)
8423 ASET (vector, i, Qnil);
8424 }
8425
8426 xassert (i == ASIZE (vector));
8427 return vector;
8428 }
8429
8430
8431 /* Restore global state from VECTOR which was created by
8432 with_echo_area_buffer_unwind_data. */
8433
8434 static Lisp_Object
8435 unwind_with_echo_area_buffer (vector)
8436 Lisp_Object vector;
8437 {
8438 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8439 Vdeactivate_mark = AREF (vector, 1);
8440 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8441
8442 if (WINDOWP (AREF (vector, 3)))
8443 {
8444 struct window *w;
8445 Lisp_Object buffer, charpos, bytepos;
8446
8447 w = XWINDOW (AREF (vector, 3));
8448 buffer = AREF (vector, 4);
8449 charpos = AREF (vector, 5);
8450 bytepos = AREF (vector, 6);
8451
8452 w->buffer = buffer;
8453 set_marker_both (w->pointm, buffer,
8454 XFASTINT (charpos), XFASTINT (bytepos));
8455 }
8456
8457 Vwith_echo_area_save_vector = vector;
8458 return Qnil;
8459 }
8460
8461
8462 /* Set up the echo area for use by print functions. MULTIBYTE_P
8463 non-zero means we will print multibyte. */
8464
8465 void
8466 setup_echo_area_for_printing (multibyte_p)
8467 int multibyte_p;
8468 {
8469 /* If we can't find an echo area any more, exit. */
8470 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8471 Fkill_emacs (Qnil);
8472
8473 ensure_echo_area_buffers ();
8474
8475 if (!message_buf_print)
8476 {
8477 /* A message has been output since the last time we printed.
8478 Choose a fresh echo area buffer. */
8479 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8480 echo_area_buffer[0] = echo_buffer[1];
8481 else
8482 echo_area_buffer[0] = echo_buffer[0];
8483
8484 /* Switch to that buffer and clear it. */
8485 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8486 current_buffer->truncate_lines = Qnil;
8487
8488 if (Z > BEG)
8489 {
8490 int count = SPECPDL_INDEX ();
8491 specbind (Qinhibit_read_only, Qt);
8492 /* Note that undo recording is always disabled. */
8493 del_range (BEG, Z);
8494 unbind_to (count, Qnil);
8495 }
8496 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8497
8498 /* Set up the buffer for the multibyteness we need. */
8499 if (multibyte_p
8500 != !NILP (current_buffer->enable_multibyte_characters))
8501 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8502
8503 /* Raise the frame containing the echo area. */
8504 if (minibuffer_auto_raise)
8505 {
8506 struct frame *sf = SELECTED_FRAME ();
8507 Lisp_Object mini_window;
8508 mini_window = FRAME_MINIBUF_WINDOW (sf);
8509 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8510 }
8511
8512 message_log_maybe_newline ();
8513 message_buf_print = 1;
8514 }
8515 else
8516 {
8517 if (NILP (echo_area_buffer[0]))
8518 {
8519 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8520 echo_area_buffer[0] = echo_buffer[1];
8521 else
8522 echo_area_buffer[0] = echo_buffer[0];
8523 }
8524
8525 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8526 {
8527 /* Someone switched buffers between print requests. */
8528 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8529 current_buffer->truncate_lines = Qnil;
8530 }
8531 }
8532 }
8533
8534
8535 /* Display an echo area message in window W. Value is non-zero if W's
8536 height is changed. If display_last_displayed_message_p is
8537 non-zero, display the message that was last displayed, otherwise
8538 display the current message. */
8539
8540 static int
8541 display_echo_area (w)
8542 struct window *w;
8543 {
8544 int i, no_message_p, window_height_changed_p, count;
8545
8546 /* Temporarily disable garbage collections while displaying the echo
8547 area. This is done because a GC can print a message itself.
8548 That message would modify the echo area buffer's contents while a
8549 redisplay of the buffer is going on, and seriously confuse
8550 redisplay. */
8551 count = inhibit_garbage_collection ();
8552
8553 /* If there is no message, we must call display_echo_area_1
8554 nevertheless because it resizes the window. But we will have to
8555 reset the echo_area_buffer in question to nil at the end because
8556 with_echo_area_buffer will sets it to an empty buffer. */
8557 i = display_last_displayed_message_p ? 1 : 0;
8558 no_message_p = NILP (echo_area_buffer[i]);
8559
8560 window_height_changed_p
8561 = with_echo_area_buffer (w, display_last_displayed_message_p,
8562 display_echo_area_1,
8563 (EMACS_INT) w, Qnil, 0, 0);
8564
8565 if (no_message_p)
8566 echo_area_buffer[i] = Qnil;
8567
8568 unbind_to (count, Qnil);
8569 return window_height_changed_p;
8570 }
8571
8572
8573 /* Helper for display_echo_area. Display the current buffer which
8574 contains the current echo area message in window W, a mini-window,
8575 a pointer to which is passed in A1. A2..A4 are currently not used.
8576 Change the height of W so that all of the message is displayed.
8577 Value is non-zero if height of W was changed. */
8578
8579 static int
8580 display_echo_area_1 (a1, a2, a3, a4)
8581 EMACS_INT a1;
8582 Lisp_Object a2;
8583 EMACS_INT a3, a4;
8584 {
8585 struct window *w = (struct window *) a1;
8586 Lisp_Object window;
8587 struct text_pos start;
8588 int window_height_changed_p = 0;
8589
8590 /* Do this before displaying, so that we have a large enough glyph
8591 matrix for the display. If we can't get enough space for the
8592 whole text, display the last N lines. That works by setting w->start. */
8593 window_height_changed_p = resize_mini_window (w, 0);
8594
8595 /* Use the starting position chosen by resize_mini_window. */
8596 SET_TEXT_POS_FROM_MARKER (start, w->start);
8597
8598 /* Display. */
8599 clear_glyph_matrix (w->desired_matrix);
8600 XSETWINDOW (window, w);
8601 try_window (window, start, 0);
8602
8603 return window_height_changed_p;
8604 }
8605
8606
8607 /* Resize the echo area window to exactly the size needed for the
8608 currently displayed message, if there is one. If a mini-buffer
8609 is active, don't shrink it. */
8610
8611 void
8612 resize_echo_area_exactly ()
8613 {
8614 if (BUFFERP (echo_area_buffer[0])
8615 && WINDOWP (echo_area_window))
8616 {
8617 struct window *w = XWINDOW (echo_area_window);
8618 int resized_p;
8619 Lisp_Object resize_exactly;
8620
8621 if (minibuf_level == 0)
8622 resize_exactly = Qt;
8623 else
8624 resize_exactly = Qnil;
8625
8626 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8627 (EMACS_INT) w, resize_exactly, 0, 0);
8628 if (resized_p)
8629 {
8630 ++windows_or_buffers_changed;
8631 ++update_mode_lines;
8632 redisplay_internal (0);
8633 }
8634 }
8635 }
8636
8637
8638 /* Callback function for with_echo_area_buffer, when used from
8639 resize_echo_area_exactly. A1 contains a pointer to the window to
8640 resize, EXACTLY non-nil means resize the mini-window exactly to the
8641 size of the text displayed. A3 and A4 are not used. Value is what
8642 resize_mini_window returns. */
8643
8644 static int
8645 resize_mini_window_1 (a1, exactly, a3, a4)
8646 EMACS_INT a1;
8647 Lisp_Object exactly;
8648 EMACS_INT a3, a4;
8649 {
8650 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8651 }
8652
8653
8654 /* Resize mini-window W to fit the size of its contents. EXACT_P
8655 means size the window exactly to the size needed. Otherwise, it's
8656 only enlarged until W's buffer is empty.
8657
8658 Set W->start to the right place to begin display. If the whole
8659 contents fit, start at the beginning. Otherwise, start so as
8660 to make the end of the contents appear. This is particularly
8661 important for y-or-n-p, but seems desirable generally.
8662
8663 Value is non-zero if the window height has been changed. */
8664
8665 int
8666 resize_mini_window (w, exact_p)
8667 struct window *w;
8668 int exact_p;
8669 {
8670 struct frame *f = XFRAME (w->frame);
8671 int window_height_changed_p = 0;
8672
8673 xassert (MINI_WINDOW_P (w));
8674
8675 /* By default, start display at the beginning. */
8676 set_marker_both (w->start, w->buffer,
8677 BUF_BEGV (XBUFFER (w->buffer)),
8678 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8679
8680 /* Don't resize windows while redisplaying a window; it would
8681 confuse redisplay functions when the size of the window they are
8682 displaying changes from under them. Such a resizing can happen,
8683 for instance, when which-func prints a long message while
8684 we are running fontification-functions. We're running these
8685 functions with safe_call which binds inhibit-redisplay to t. */
8686 if (!NILP (Vinhibit_redisplay))
8687 return 0;
8688
8689 /* Nil means don't try to resize. */
8690 if (NILP (Vresize_mini_windows)
8691 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8692 return 0;
8693
8694 if (!FRAME_MINIBUF_ONLY_P (f))
8695 {
8696 struct it it;
8697 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8698 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8699 int height, max_height;
8700 int unit = FRAME_LINE_HEIGHT (f);
8701 struct text_pos start;
8702 struct buffer *old_current_buffer = NULL;
8703
8704 if (current_buffer != XBUFFER (w->buffer))
8705 {
8706 old_current_buffer = current_buffer;
8707 set_buffer_internal (XBUFFER (w->buffer));
8708 }
8709
8710 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8711
8712 /* Compute the max. number of lines specified by the user. */
8713 if (FLOATP (Vmax_mini_window_height))
8714 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8715 else if (INTEGERP (Vmax_mini_window_height))
8716 max_height = XINT (Vmax_mini_window_height);
8717 else
8718 max_height = total_height / 4;
8719
8720 /* Correct that max. height if it's bogus. */
8721 max_height = max (1, max_height);
8722 max_height = min (total_height, max_height);
8723
8724 /* Find out the height of the text in the window. */
8725 if (it.line_wrap == TRUNCATE)
8726 height = 1;
8727 else
8728 {
8729 last_height = 0;
8730 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8731 if (it.max_ascent == 0 && it.max_descent == 0)
8732 height = it.current_y + last_height;
8733 else
8734 height = it.current_y + it.max_ascent + it.max_descent;
8735 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8736 height = (height + unit - 1) / unit;
8737 }
8738
8739 /* Compute a suitable window start. */
8740 if (height > max_height)
8741 {
8742 height = max_height;
8743 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8744 move_it_vertically_backward (&it, (height - 1) * unit);
8745 start = it.current.pos;
8746 }
8747 else
8748 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8749 SET_MARKER_FROM_TEXT_POS (w->start, start);
8750
8751 if (EQ (Vresize_mini_windows, Qgrow_only))
8752 {
8753 /* Let it grow only, until we display an empty message, in which
8754 case the window shrinks again. */
8755 if (height > WINDOW_TOTAL_LINES (w))
8756 {
8757 int old_height = WINDOW_TOTAL_LINES (w);
8758 freeze_window_starts (f, 1);
8759 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8760 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8761 }
8762 else if (height < WINDOW_TOTAL_LINES (w)
8763 && (exact_p || BEGV == ZV))
8764 {
8765 int old_height = WINDOW_TOTAL_LINES (w);
8766 freeze_window_starts (f, 0);
8767 shrink_mini_window (w);
8768 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8769 }
8770 }
8771 else
8772 {
8773 /* Always resize to exact size needed. */
8774 if (height > WINDOW_TOTAL_LINES (w))
8775 {
8776 int old_height = WINDOW_TOTAL_LINES (w);
8777 freeze_window_starts (f, 1);
8778 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8779 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8780 }
8781 else if (height < WINDOW_TOTAL_LINES (w))
8782 {
8783 int old_height = WINDOW_TOTAL_LINES (w);
8784 freeze_window_starts (f, 0);
8785 shrink_mini_window (w);
8786
8787 if (height)
8788 {
8789 freeze_window_starts (f, 1);
8790 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8791 }
8792
8793 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8794 }
8795 }
8796
8797 if (old_current_buffer)
8798 set_buffer_internal (old_current_buffer);
8799 }
8800
8801 return window_height_changed_p;
8802 }
8803
8804
8805 /* Value is the current message, a string, or nil if there is no
8806 current message. */
8807
8808 Lisp_Object
8809 current_message ()
8810 {
8811 Lisp_Object msg;
8812
8813 if (!BUFFERP (echo_area_buffer[0]))
8814 msg = Qnil;
8815 else
8816 {
8817 with_echo_area_buffer (0, 0, current_message_1,
8818 (EMACS_INT) &msg, Qnil, 0, 0);
8819 if (NILP (msg))
8820 echo_area_buffer[0] = Qnil;
8821 }
8822
8823 return msg;
8824 }
8825
8826
8827 static int
8828 current_message_1 (a1, a2, a3, a4)
8829 EMACS_INT a1;
8830 Lisp_Object a2;
8831 EMACS_INT a3, a4;
8832 {
8833 Lisp_Object *msg = (Lisp_Object *) a1;
8834
8835 if (Z > BEG)
8836 *msg = make_buffer_string (BEG, Z, 1);
8837 else
8838 *msg = Qnil;
8839 return 0;
8840 }
8841
8842
8843 /* Push the current message on Vmessage_stack for later restauration
8844 by restore_message. Value is non-zero if the current message isn't
8845 empty. This is a relatively infrequent operation, so it's not
8846 worth optimizing. */
8847
8848 int
8849 push_message ()
8850 {
8851 Lisp_Object msg;
8852 msg = current_message ();
8853 Vmessage_stack = Fcons (msg, Vmessage_stack);
8854 return STRINGP (msg);
8855 }
8856
8857
8858 /* Restore message display from the top of Vmessage_stack. */
8859
8860 void
8861 restore_message ()
8862 {
8863 Lisp_Object msg;
8864
8865 xassert (CONSP (Vmessage_stack));
8866 msg = XCAR (Vmessage_stack);
8867 if (STRINGP (msg))
8868 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8869 else
8870 message3_nolog (msg, 0, 0);
8871 }
8872
8873
8874 /* Handler for record_unwind_protect calling pop_message. */
8875
8876 Lisp_Object
8877 pop_message_unwind (dummy)
8878 Lisp_Object dummy;
8879 {
8880 pop_message ();
8881 return Qnil;
8882 }
8883
8884 /* Pop the top-most entry off Vmessage_stack. */
8885
8886 void
8887 pop_message ()
8888 {
8889 xassert (CONSP (Vmessage_stack));
8890 Vmessage_stack = XCDR (Vmessage_stack);
8891 }
8892
8893
8894 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8895 exits. If the stack is not empty, we have a missing pop_message
8896 somewhere. */
8897
8898 void
8899 check_message_stack ()
8900 {
8901 if (!NILP (Vmessage_stack))
8902 abort ();
8903 }
8904
8905
8906 /* Truncate to NCHARS what will be displayed in the echo area the next
8907 time we display it---but don't redisplay it now. */
8908
8909 void
8910 truncate_echo_area (nchars)
8911 int nchars;
8912 {
8913 if (nchars == 0)
8914 echo_area_buffer[0] = Qnil;
8915 /* A null message buffer means that the frame hasn't really been
8916 initialized yet. Error messages get reported properly by
8917 cmd_error, so this must be just an informative message; toss it. */
8918 else if (!noninteractive
8919 && INTERACTIVE
8920 && !NILP (echo_area_buffer[0]))
8921 {
8922 struct frame *sf = SELECTED_FRAME ();
8923 if (FRAME_MESSAGE_BUF (sf))
8924 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8925 }
8926 }
8927
8928
8929 /* Helper function for truncate_echo_area. Truncate the current
8930 message to at most NCHARS characters. */
8931
8932 static int
8933 truncate_message_1 (nchars, a2, a3, a4)
8934 EMACS_INT nchars;
8935 Lisp_Object a2;
8936 EMACS_INT a3, a4;
8937 {
8938 if (BEG + nchars < Z)
8939 del_range (BEG + nchars, Z);
8940 if (Z == BEG)
8941 echo_area_buffer[0] = Qnil;
8942 return 0;
8943 }
8944
8945
8946 /* Set the current message to a substring of S or STRING.
8947
8948 If STRING is a Lisp string, set the message to the first NBYTES
8949 bytes from STRING. NBYTES zero means use the whole string. If
8950 STRING is multibyte, the message will be displayed multibyte.
8951
8952 If S is not null, set the message to the first LEN bytes of S. LEN
8953 zero means use the whole string. MULTIBYTE_P non-zero means S is
8954 multibyte. Display the message multibyte in that case.
8955
8956 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8957 to t before calling set_message_1 (which calls insert).
8958 */
8959
8960 void
8961 set_message (s, string, nbytes, multibyte_p)
8962 const char *s;
8963 Lisp_Object string;
8964 int nbytes, multibyte_p;
8965 {
8966 message_enable_multibyte
8967 = ((s && multibyte_p)
8968 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8969
8970 with_echo_area_buffer (0, -1, set_message_1,
8971 (EMACS_INT) s, string, nbytes, multibyte_p);
8972 message_buf_print = 0;
8973 help_echo_showing_p = 0;
8974 }
8975
8976
8977 /* Helper function for set_message. Arguments have the same meaning
8978 as there, with A1 corresponding to S and A2 corresponding to STRING
8979 This function is called with the echo area buffer being
8980 current. */
8981
8982 static int
8983 set_message_1 (a1, a2, nbytes, multibyte_p)
8984 EMACS_INT a1;
8985 Lisp_Object a2;
8986 EMACS_INT nbytes, multibyte_p;
8987 {
8988 const char *s = (const char *) a1;
8989 Lisp_Object string = a2;
8990
8991 /* Change multibyteness of the echo buffer appropriately. */
8992 if (message_enable_multibyte
8993 != !NILP (current_buffer->enable_multibyte_characters))
8994 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8995
8996 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8997
8998 /* Insert new message at BEG. */
8999 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9000
9001 if (STRINGP (string))
9002 {
9003 int nchars;
9004
9005 if (nbytes == 0)
9006 nbytes = SBYTES (string);
9007 nchars = string_byte_to_char (string, nbytes);
9008
9009 /* This function takes care of single/multibyte conversion. We
9010 just have to ensure that the echo area buffer has the right
9011 setting of enable_multibyte_characters. */
9012 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9013 }
9014 else if (s)
9015 {
9016 if (nbytes == 0)
9017 nbytes = strlen (s);
9018
9019 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9020 {
9021 /* Convert from multi-byte to single-byte. */
9022 int i, c, n;
9023 unsigned char work[1];
9024
9025 /* Convert a multibyte string to single-byte. */
9026 for (i = 0; i < nbytes; i += n)
9027 {
9028 c = string_char_and_length (s + i, &n);
9029 work[0] = (ASCII_CHAR_P (c)
9030 ? c
9031 : multibyte_char_to_unibyte (c, Qnil));
9032 insert_1_both (work, 1, 1, 1, 0, 0);
9033 }
9034 }
9035 else if (!multibyte_p
9036 && !NILP (current_buffer->enable_multibyte_characters))
9037 {
9038 /* Convert from single-byte to multi-byte. */
9039 int i, c, n;
9040 const unsigned char *msg = (const unsigned char *) s;
9041 unsigned char str[MAX_MULTIBYTE_LENGTH];
9042
9043 /* Convert a single-byte string to multibyte. */
9044 for (i = 0; i < nbytes; i++)
9045 {
9046 c = msg[i];
9047 MAKE_CHAR_MULTIBYTE (c);
9048 n = CHAR_STRING (c, str);
9049 insert_1_both (str, 1, n, 1, 0, 0);
9050 }
9051 }
9052 else
9053 insert_1 (s, nbytes, 1, 0, 0);
9054 }
9055
9056 return 0;
9057 }
9058
9059
9060 /* Clear messages. CURRENT_P non-zero means clear the current
9061 message. LAST_DISPLAYED_P non-zero means clear the message
9062 last displayed. */
9063
9064 void
9065 clear_message (current_p, last_displayed_p)
9066 int current_p, last_displayed_p;
9067 {
9068 if (current_p)
9069 {
9070 echo_area_buffer[0] = Qnil;
9071 message_cleared_p = 1;
9072 }
9073
9074 if (last_displayed_p)
9075 echo_area_buffer[1] = Qnil;
9076
9077 message_buf_print = 0;
9078 }
9079
9080 /* Clear garbaged frames.
9081
9082 This function is used where the old redisplay called
9083 redraw_garbaged_frames which in turn called redraw_frame which in
9084 turn called clear_frame. The call to clear_frame was a source of
9085 flickering. I believe a clear_frame is not necessary. It should
9086 suffice in the new redisplay to invalidate all current matrices,
9087 and ensure a complete redisplay of all windows. */
9088
9089 static void
9090 clear_garbaged_frames ()
9091 {
9092 if (frame_garbaged)
9093 {
9094 Lisp_Object tail, frame;
9095 int changed_count = 0;
9096
9097 FOR_EACH_FRAME (tail, frame)
9098 {
9099 struct frame *f = XFRAME (frame);
9100
9101 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9102 {
9103 if (f->resized_p)
9104 {
9105 Fredraw_frame (frame);
9106 f->force_flush_display_p = 1;
9107 }
9108 clear_current_matrices (f);
9109 changed_count++;
9110 f->garbaged = 0;
9111 f->resized_p = 0;
9112 }
9113 }
9114
9115 frame_garbaged = 0;
9116 if (changed_count)
9117 ++windows_or_buffers_changed;
9118 }
9119 }
9120
9121
9122 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9123 is non-zero update selected_frame. Value is non-zero if the
9124 mini-windows height has been changed. */
9125
9126 static int
9127 echo_area_display (update_frame_p)
9128 int update_frame_p;
9129 {
9130 Lisp_Object mini_window;
9131 struct window *w;
9132 struct frame *f;
9133 int window_height_changed_p = 0;
9134 struct frame *sf = SELECTED_FRAME ();
9135
9136 mini_window = FRAME_MINIBUF_WINDOW (sf);
9137 w = XWINDOW (mini_window);
9138 f = XFRAME (WINDOW_FRAME (w));
9139
9140 /* Don't display if frame is invisible or not yet initialized. */
9141 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9142 return 0;
9143
9144 #ifdef HAVE_WINDOW_SYSTEM
9145 /* When Emacs starts, selected_frame may be the initial terminal
9146 frame. If we let this through, a message would be displayed on
9147 the terminal. */
9148 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9149 return 0;
9150 #endif /* HAVE_WINDOW_SYSTEM */
9151
9152 /* Redraw garbaged frames. */
9153 if (frame_garbaged)
9154 clear_garbaged_frames ();
9155
9156 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9157 {
9158 echo_area_window = mini_window;
9159 window_height_changed_p = display_echo_area (w);
9160 w->must_be_updated_p = 1;
9161
9162 /* Update the display, unless called from redisplay_internal.
9163 Also don't update the screen during redisplay itself. The
9164 update will happen at the end of redisplay, and an update
9165 here could cause confusion. */
9166 if (update_frame_p && !redisplaying_p)
9167 {
9168 int n = 0;
9169
9170 /* If the display update has been interrupted by pending
9171 input, update mode lines in the frame. Due to the
9172 pending input, it might have been that redisplay hasn't
9173 been called, so that mode lines above the echo area are
9174 garbaged. This looks odd, so we prevent it here. */
9175 if (!display_completed)
9176 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9177
9178 if (window_height_changed_p
9179 /* Don't do this if Emacs is shutting down. Redisplay
9180 needs to run hooks. */
9181 && !NILP (Vrun_hooks))
9182 {
9183 /* Must update other windows. Likewise as in other
9184 cases, don't let this update be interrupted by
9185 pending input. */
9186 int count = SPECPDL_INDEX ();
9187 specbind (Qredisplay_dont_pause, Qt);
9188 windows_or_buffers_changed = 1;
9189 redisplay_internal (0);
9190 unbind_to (count, Qnil);
9191 }
9192 else if (FRAME_WINDOW_P (f) && n == 0)
9193 {
9194 /* Window configuration is the same as before.
9195 Can do with a display update of the echo area,
9196 unless we displayed some mode lines. */
9197 update_single_window (w, 1);
9198 FRAME_RIF (f)->flush_display (f);
9199 }
9200 else
9201 update_frame (f, 1, 1);
9202
9203 /* If cursor is in the echo area, make sure that the next
9204 redisplay displays the minibuffer, so that the cursor will
9205 be replaced with what the minibuffer wants. */
9206 if (cursor_in_echo_area)
9207 ++windows_or_buffers_changed;
9208 }
9209 }
9210 else if (!EQ (mini_window, selected_window))
9211 windows_or_buffers_changed++;
9212
9213 /* Last displayed message is now the current message. */
9214 echo_area_buffer[1] = echo_area_buffer[0];
9215 /* Inform read_char that we're not echoing. */
9216 echo_message_buffer = Qnil;
9217
9218 /* Prevent redisplay optimization in redisplay_internal by resetting
9219 this_line_start_pos. This is done because the mini-buffer now
9220 displays the message instead of its buffer text. */
9221 if (EQ (mini_window, selected_window))
9222 CHARPOS (this_line_start_pos) = 0;
9223
9224 return window_height_changed_p;
9225 }
9226
9227
9228 \f
9229 /***********************************************************************
9230 Mode Lines and Frame Titles
9231 ***********************************************************************/
9232
9233 /* A buffer for constructing non-propertized mode-line strings and
9234 frame titles in it; allocated from the heap in init_xdisp and
9235 resized as needed in store_mode_line_noprop_char. */
9236
9237 static char *mode_line_noprop_buf;
9238
9239 /* The buffer's end, and a current output position in it. */
9240
9241 static char *mode_line_noprop_buf_end;
9242 static char *mode_line_noprop_ptr;
9243
9244 #define MODE_LINE_NOPROP_LEN(start) \
9245 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9246
9247 static enum {
9248 MODE_LINE_DISPLAY = 0,
9249 MODE_LINE_TITLE,
9250 MODE_LINE_NOPROP,
9251 MODE_LINE_STRING
9252 } mode_line_target;
9253
9254 /* Alist that caches the results of :propertize.
9255 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9256 static Lisp_Object mode_line_proptrans_alist;
9257
9258 /* List of strings making up the mode-line. */
9259 static Lisp_Object mode_line_string_list;
9260
9261 /* Base face property when building propertized mode line string. */
9262 static Lisp_Object mode_line_string_face;
9263 static Lisp_Object mode_line_string_face_prop;
9264
9265
9266 /* Unwind data for mode line strings */
9267
9268 static Lisp_Object Vmode_line_unwind_vector;
9269
9270 static Lisp_Object
9271 format_mode_line_unwind_data (struct buffer *obuf,
9272 Lisp_Object owin,
9273 int save_proptrans)
9274 {
9275 Lisp_Object vector, tmp;
9276
9277 /* Reduce consing by keeping one vector in
9278 Vwith_echo_area_save_vector. */
9279 vector = Vmode_line_unwind_vector;
9280 Vmode_line_unwind_vector = Qnil;
9281
9282 if (NILP (vector))
9283 vector = Fmake_vector (make_number (8), Qnil);
9284
9285 ASET (vector, 0, make_number (mode_line_target));
9286 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9287 ASET (vector, 2, mode_line_string_list);
9288 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9289 ASET (vector, 4, mode_line_string_face);
9290 ASET (vector, 5, mode_line_string_face_prop);
9291
9292 if (obuf)
9293 XSETBUFFER (tmp, obuf);
9294 else
9295 tmp = Qnil;
9296 ASET (vector, 6, tmp);
9297 ASET (vector, 7, owin);
9298
9299 return vector;
9300 }
9301
9302 static Lisp_Object
9303 unwind_format_mode_line (vector)
9304 Lisp_Object vector;
9305 {
9306 mode_line_target = XINT (AREF (vector, 0));
9307 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9308 mode_line_string_list = AREF (vector, 2);
9309 if (! EQ (AREF (vector, 3), Qt))
9310 mode_line_proptrans_alist = AREF (vector, 3);
9311 mode_line_string_face = AREF (vector, 4);
9312 mode_line_string_face_prop = AREF (vector, 5);
9313
9314 if (!NILP (AREF (vector, 7)))
9315 /* Select window before buffer, since it may change the buffer. */
9316 Fselect_window (AREF (vector, 7), Qt);
9317
9318 if (!NILP (AREF (vector, 6)))
9319 {
9320 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9321 ASET (vector, 6, Qnil);
9322 }
9323
9324 Vmode_line_unwind_vector = vector;
9325 return Qnil;
9326 }
9327
9328
9329 /* Store a single character C for the frame title in mode_line_noprop_buf.
9330 Re-allocate mode_line_noprop_buf if necessary. */
9331
9332 static void
9333 #ifdef PROTOTYPES
9334 store_mode_line_noprop_char (char c)
9335 #else
9336 store_mode_line_noprop_char (c)
9337 char c;
9338 #endif
9339 {
9340 /* If output position has reached the end of the allocated buffer,
9341 double the buffer's size. */
9342 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9343 {
9344 int len = MODE_LINE_NOPROP_LEN (0);
9345 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9346 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9347 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9348 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9349 }
9350
9351 *mode_line_noprop_ptr++ = c;
9352 }
9353
9354
9355 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9356 mode_line_noprop_ptr. STR is the string to store. Do not copy
9357 characters that yield more columns than PRECISION; PRECISION <= 0
9358 means copy the whole string. Pad with spaces until FIELD_WIDTH
9359 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9360 pad. Called from display_mode_element when it is used to build a
9361 frame title. */
9362
9363 static int
9364 store_mode_line_noprop (str, field_width, precision)
9365 const unsigned char *str;
9366 int field_width, precision;
9367 {
9368 int n = 0;
9369 int dummy, nbytes;
9370
9371 /* Copy at most PRECISION chars from STR. */
9372 nbytes = strlen (str);
9373 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9374 while (nbytes--)
9375 store_mode_line_noprop_char (*str++);
9376
9377 /* Fill up with spaces until FIELD_WIDTH reached. */
9378 while (field_width > 0
9379 && n < field_width)
9380 {
9381 store_mode_line_noprop_char (' ');
9382 ++n;
9383 }
9384
9385 return n;
9386 }
9387
9388 /***********************************************************************
9389 Frame Titles
9390 ***********************************************************************/
9391
9392 #ifdef HAVE_WINDOW_SYSTEM
9393
9394 /* Set the title of FRAME, if it has changed. The title format is
9395 Vicon_title_format if FRAME is iconified, otherwise it is
9396 frame_title_format. */
9397
9398 static void
9399 x_consider_frame_title (frame)
9400 Lisp_Object frame;
9401 {
9402 struct frame *f = XFRAME (frame);
9403
9404 if (FRAME_WINDOW_P (f)
9405 || FRAME_MINIBUF_ONLY_P (f)
9406 || f->explicit_name)
9407 {
9408 /* Do we have more than one visible frame on this X display? */
9409 Lisp_Object tail;
9410 Lisp_Object fmt;
9411 int title_start;
9412 char *title;
9413 int len;
9414 struct it it;
9415 int count = SPECPDL_INDEX ();
9416
9417 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9418 {
9419 Lisp_Object other_frame = XCAR (tail);
9420 struct frame *tf = XFRAME (other_frame);
9421
9422 if (tf != f
9423 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9424 && !FRAME_MINIBUF_ONLY_P (tf)
9425 && !EQ (other_frame, tip_frame)
9426 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9427 break;
9428 }
9429
9430 /* Set global variable indicating that multiple frames exist. */
9431 multiple_frames = CONSP (tail);
9432
9433 /* Switch to the buffer of selected window of the frame. Set up
9434 mode_line_target so that display_mode_element will output into
9435 mode_line_noprop_buf; then display the title. */
9436 record_unwind_protect (unwind_format_mode_line,
9437 format_mode_line_unwind_data
9438 (current_buffer, selected_window, 0));
9439
9440 Fselect_window (f->selected_window, Qt);
9441 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9442 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9443
9444 mode_line_target = MODE_LINE_TITLE;
9445 title_start = MODE_LINE_NOPROP_LEN (0);
9446 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9447 NULL, DEFAULT_FACE_ID);
9448 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9449 len = MODE_LINE_NOPROP_LEN (title_start);
9450 title = mode_line_noprop_buf + title_start;
9451 unbind_to (count, Qnil);
9452
9453 /* Set the title only if it's changed. This avoids consing in
9454 the common case where it hasn't. (If it turns out that we've
9455 already wasted too much time by walking through the list with
9456 display_mode_element, then we might need to optimize at a
9457 higher level than this.) */
9458 if (! STRINGP (f->name)
9459 || SBYTES (f->name) != len
9460 || bcmp (title, SDATA (f->name), len) != 0)
9461 x_implicitly_set_name (f, make_string (title, len), Qnil);
9462 }
9463 }
9464
9465 #endif /* not HAVE_WINDOW_SYSTEM */
9466
9467
9468
9469 \f
9470 /***********************************************************************
9471 Menu Bars
9472 ***********************************************************************/
9473
9474
9475 /* Prepare for redisplay by updating menu-bar item lists when
9476 appropriate. This can call eval. */
9477
9478 void
9479 prepare_menu_bars ()
9480 {
9481 int all_windows;
9482 struct gcpro gcpro1, gcpro2;
9483 struct frame *f;
9484 Lisp_Object tooltip_frame;
9485
9486 #ifdef HAVE_WINDOW_SYSTEM
9487 tooltip_frame = tip_frame;
9488 #else
9489 tooltip_frame = Qnil;
9490 #endif
9491
9492 /* Update all frame titles based on their buffer names, etc. We do
9493 this before the menu bars so that the buffer-menu will show the
9494 up-to-date frame titles. */
9495 #ifdef HAVE_WINDOW_SYSTEM
9496 if (windows_or_buffers_changed || update_mode_lines)
9497 {
9498 Lisp_Object tail, frame;
9499
9500 FOR_EACH_FRAME (tail, frame)
9501 {
9502 f = XFRAME (frame);
9503 if (!EQ (frame, tooltip_frame)
9504 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9505 x_consider_frame_title (frame);
9506 }
9507 }
9508 #endif /* HAVE_WINDOW_SYSTEM */
9509
9510 /* Update the menu bar item lists, if appropriate. This has to be
9511 done before any actual redisplay or generation of display lines. */
9512 all_windows = (update_mode_lines
9513 || buffer_shared > 1
9514 || windows_or_buffers_changed);
9515 if (all_windows)
9516 {
9517 Lisp_Object tail, frame;
9518 int count = SPECPDL_INDEX ();
9519 /* 1 means that update_menu_bar has run its hooks
9520 so any further calls to update_menu_bar shouldn't do so again. */
9521 int menu_bar_hooks_run = 0;
9522
9523 record_unwind_save_match_data ();
9524
9525 FOR_EACH_FRAME (tail, frame)
9526 {
9527 f = XFRAME (frame);
9528
9529 /* Ignore tooltip frame. */
9530 if (EQ (frame, tooltip_frame))
9531 continue;
9532
9533 /* If a window on this frame changed size, report that to
9534 the user and clear the size-change flag. */
9535 if (FRAME_WINDOW_SIZES_CHANGED (f))
9536 {
9537 Lisp_Object functions;
9538
9539 /* Clear flag first in case we get an error below. */
9540 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9541 functions = Vwindow_size_change_functions;
9542 GCPRO2 (tail, functions);
9543
9544 while (CONSP (functions))
9545 {
9546 if (!EQ (XCAR (functions), Qt))
9547 call1 (XCAR (functions), frame);
9548 functions = XCDR (functions);
9549 }
9550 UNGCPRO;
9551 }
9552
9553 GCPRO1 (tail);
9554 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9555 #ifdef HAVE_WINDOW_SYSTEM
9556 update_tool_bar (f, 0);
9557 #endif
9558 #ifdef HAVE_NS
9559 if (windows_or_buffers_changed
9560 && FRAME_NS_P (f))
9561 ns_set_doc_edited (f, Fbuffer_modified_p
9562 (XWINDOW (f->selected_window)->buffer));
9563 #endif
9564 UNGCPRO;
9565 }
9566
9567 unbind_to (count, Qnil);
9568 }
9569 else
9570 {
9571 struct frame *sf = SELECTED_FRAME ();
9572 update_menu_bar (sf, 1, 0);
9573 #ifdef HAVE_WINDOW_SYSTEM
9574 update_tool_bar (sf, 1);
9575 #endif
9576 }
9577
9578 /* Motif needs this. See comment in xmenu.c. Turn it off when
9579 pending_menu_activation is not defined. */
9580 #ifdef USE_X_TOOLKIT
9581 pending_menu_activation = 0;
9582 #endif
9583 }
9584
9585
9586 /* Update the menu bar item list for frame F. This has to be done
9587 before we start to fill in any display lines, because it can call
9588 eval.
9589
9590 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9591
9592 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9593 already ran the menu bar hooks for this redisplay, so there
9594 is no need to run them again. The return value is the
9595 updated value of this flag, to pass to the next call. */
9596
9597 static int
9598 update_menu_bar (f, save_match_data, hooks_run)
9599 struct frame *f;
9600 int save_match_data;
9601 int hooks_run;
9602 {
9603 Lisp_Object window;
9604 register struct window *w;
9605
9606 /* If called recursively during a menu update, do nothing. This can
9607 happen when, for instance, an activate-menubar-hook causes a
9608 redisplay. */
9609 if (inhibit_menubar_update)
9610 return hooks_run;
9611
9612 window = FRAME_SELECTED_WINDOW (f);
9613 w = XWINDOW (window);
9614
9615 if (FRAME_WINDOW_P (f)
9616 ?
9617 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9618 || defined (HAVE_NS) || defined (USE_GTK)
9619 FRAME_EXTERNAL_MENU_BAR (f)
9620 #else
9621 FRAME_MENU_BAR_LINES (f) > 0
9622 #endif
9623 : FRAME_MENU_BAR_LINES (f) > 0)
9624 {
9625 /* If the user has switched buffers or windows, we need to
9626 recompute to reflect the new bindings. But we'll
9627 recompute when update_mode_lines is set too; that means
9628 that people can use force-mode-line-update to request
9629 that the menu bar be recomputed. The adverse effect on
9630 the rest of the redisplay algorithm is about the same as
9631 windows_or_buffers_changed anyway. */
9632 if (windows_or_buffers_changed
9633 /* This used to test w->update_mode_line, but we believe
9634 there is no need to recompute the menu in that case. */
9635 || update_mode_lines
9636 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9637 < BUF_MODIFF (XBUFFER (w->buffer)))
9638 != !NILP (w->last_had_star))
9639 || ((!NILP (Vtransient_mark_mode)
9640 && !NILP (XBUFFER (w->buffer)->mark_active))
9641 != !NILP (w->region_showing)))
9642 {
9643 struct buffer *prev = current_buffer;
9644 int count = SPECPDL_INDEX ();
9645
9646 specbind (Qinhibit_menubar_update, Qt);
9647
9648 set_buffer_internal_1 (XBUFFER (w->buffer));
9649 if (save_match_data)
9650 record_unwind_save_match_data ();
9651 if (NILP (Voverriding_local_map_menu_flag))
9652 {
9653 specbind (Qoverriding_terminal_local_map, Qnil);
9654 specbind (Qoverriding_local_map, Qnil);
9655 }
9656
9657 if (!hooks_run)
9658 {
9659 /* Run the Lucid hook. */
9660 safe_run_hooks (Qactivate_menubar_hook);
9661
9662 /* If it has changed current-menubar from previous value,
9663 really recompute the menu-bar from the value. */
9664 if (! NILP (Vlucid_menu_bar_dirty_flag))
9665 call0 (Qrecompute_lucid_menubar);
9666
9667 safe_run_hooks (Qmenu_bar_update_hook);
9668
9669 hooks_run = 1;
9670 }
9671
9672 XSETFRAME (Vmenu_updating_frame, f);
9673 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9674
9675 /* Redisplay the menu bar in case we changed it. */
9676 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9677 || defined (HAVE_NS) || defined (USE_GTK)
9678 if (FRAME_WINDOW_P (f))
9679 {
9680 #if defined (HAVE_NS)
9681 /* All frames on Mac OS share the same menubar. So only
9682 the selected frame should be allowed to set it. */
9683 if (f == SELECTED_FRAME ())
9684 #endif
9685 set_frame_menubar (f, 0, 0);
9686 }
9687 else
9688 /* On a terminal screen, the menu bar is an ordinary screen
9689 line, and this makes it get updated. */
9690 w->update_mode_line = Qt;
9691 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9692 /* In the non-toolkit version, the menu bar is an ordinary screen
9693 line, and this makes it get updated. */
9694 w->update_mode_line = Qt;
9695 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9696
9697 unbind_to (count, Qnil);
9698 set_buffer_internal_1 (prev);
9699 }
9700 }
9701
9702 return hooks_run;
9703 }
9704
9705
9706 \f
9707 /***********************************************************************
9708 Output Cursor
9709 ***********************************************************************/
9710
9711 #ifdef HAVE_WINDOW_SYSTEM
9712
9713 /* EXPORT:
9714 Nominal cursor position -- where to draw output.
9715 HPOS and VPOS are window relative glyph matrix coordinates.
9716 X and Y are window relative pixel coordinates. */
9717
9718 struct cursor_pos output_cursor;
9719
9720
9721 /* EXPORT:
9722 Set the global variable output_cursor to CURSOR. All cursor
9723 positions are relative to updated_window. */
9724
9725 void
9726 set_output_cursor (cursor)
9727 struct cursor_pos *cursor;
9728 {
9729 output_cursor.hpos = cursor->hpos;
9730 output_cursor.vpos = cursor->vpos;
9731 output_cursor.x = cursor->x;
9732 output_cursor.y = cursor->y;
9733 }
9734
9735
9736 /* EXPORT for RIF:
9737 Set a nominal cursor position.
9738
9739 HPOS and VPOS are column/row positions in a window glyph matrix. X
9740 and Y are window text area relative pixel positions.
9741
9742 If this is done during an update, updated_window will contain the
9743 window that is being updated and the position is the future output
9744 cursor position for that window. If updated_window is null, use
9745 selected_window and display the cursor at the given position. */
9746
9747 void
9748 x_cursor_to (vpos, hpos, y, x)
9749 int vpos, hpos, y, x;
9750 {
9751 struct window *w;
9752
9753 /* If updated_window is not set, work on selected_window. */
9754 if (updated_window)
9755 w = updated_window;
9756 else
9757 w = XWINDOW (selected_window);
9758
9759 /* Set the output cursor. */
9760 output_cursor.hpos = hpos;
9761 output_cursor.vpos = vpos;
9762 output_cursor.x = x;
9763 output_cursor.y = y;
9764
9765 /* If not called as part of an update, really display the cursor.
9766 This will also set the cursor position of W. */
9767 if (updated_window == NULL)
9768 {
9769 BLOCK_INPUT;
9770 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9771 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9772 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9773 UNBLOCK_INPUT;
9774 }
9775 }
9776
9777 #endif /* HAVE_WINDOW_SYSTEM */
9778
9779 \f
9780 /***********************************************************************
9781 Tool-bars
9782 ***********************************************************************/
9783
9784 #ifdef HAVE_WINDOW_SYSTEM
9785
9786 /* Where the mouse was last time we reported a mouse event. */
9787
9788 FRAME_PTR last_mouse_frame;
9789
9790 /* Tool-bar item index of the item on which a mouse button was pressed
9791 or -1. */
9792
9793 int last_tool_bar_item;
9794
9795
9796 static Lisp_Object
9797 update_tool_bar_unwind (frame)
9798 Lisp_Object frame;
9799 {
9800 selected_frame = frame;
9801 return Qnil;
9802 }
9803
9804 /* Update the tool-bar item list for frame F. This has to be done
9805 before we start to fill in any display lines. Called from
9806 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9807 and restore it here. */
9808
9809 static void
9810 update_tool_bar (f, save_match_data)
9811 struct frame *f;
9812 int save_match_data;
9813 {
9814 #if defined (USE_GTK) || defined (HAVE_NS)
9815 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9816 #else
9817 int do_update = WINDOWP (f->tool_bar_window)
9818 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9819 #endif
9820
9821 if (do_update)
9822 {
9823 Lisp_Object window;
9824 struct window *w;
9825
9826 window = FRAME_SELECTED_WINDOW (f);
9827 w = XWINDOW (window);
9828
9829 /* If the user has switched buffers or windows, we need to
9830 recompute to reflect the new bindings. But we'll
9831 recompute when update_mode_lines is set too; that means
9832 that people can use force-mode-line-update to request
9833 that the menu bar be recomputed. The adverse effect on
9834 the rest of the redisplay algorithm is about the same as
9835 windows_or_buffers_changed anyway. */
9836 if (windows_or_buffers_changed
9837 || !NILP (w->update_mode_line)
9838 || update_mode_lines
9839 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9840 < BUF_MODIFF (XBUFFER (w->buffer)))
9841 != !NILP (w->last_had_star))
9842 || ((!NILP (Vtransient_mark_mode)
9843 && !NILP (XBUFFER (w->buffer)->mark_active))
9844 != !NILP (w->region_showing)))
9845 {
9846 struct buffer *prev = current_buffer;
9847 int count = SPECPDL_INDEX ();
9848 Lisp_Object frame, new_tool_bar;
9849 int new_n_tool_bar;
9850 struct gcpro gcpro1;
9851
9852 /* Set current_buffer to the buffer of the selected
9853 window of the frame, so that we get the right local
9854 keymaps. */
9855 set_buffer_internal_1 (XBUFFER (w->buffer));
9856
9857 /* Save match data, if we must. */
9858 if (save_match_data)
9859 record_unwind_save_match_data ();
9860
9861 /* Make sure that we don't accidentally use bogus keymaps. */
9862 if (NILP (Voverriding_local_map_menu_flag))
9863 {
9864 specbind (Qoverriding_terminal_local_map, Qnil);
9865 specbind (Qoverriding_local_map, Qnil);
9866 }
9867
9868 GCPRO1 (new_tool_bar);
9869
9870 /* We must temporarily set the selected frame to this frame
9871 before calling tool_bar_items, because the calculation of
9872 the tool-bar keymap uses the selected frame (see
9873 `tool-bar-make-keymap' in tool-bar.el). */
9874 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9875 XSETFRAME (frame, f);
9876 selected_frame = frame;
9877
9878 /* Build desired tool-bar items from keymaps. */
9879 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9880 &new_n_tool_bar);
9881
9882 /* Redisplay the tool-bar if we changed it. */
9883 if (new_n_tool_bar != f->n_tool_bar_items
9884 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9885 {
9886 /* Redisplay that happens asynchronously due to an expose event
9887 may access f->tool_bar_items. Make sure we update both
9888 variables within BLOCK_INPUT so no such event interrupts. */
9889 BLOCK_INPUT;
9890 f->tool_bar_items = new_tool_bar;
9891 f->n_tool_bar_items = new_n_tool_bar;
9892 w->update_mode_line = Qt;
9893 UNBLOCK_INPUT;
9894 }
9895
9896 UNGCPRO;
9897
9898 unbind_to (count, Qnil);
9899 set_buffer_internal_1 (prev);
9900 }
9901 }
9902 }
9903
9904
9905 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9906 F's desired tool-bar contents. F->tool_bar_items must have
9907 been set up previously by calling prepare_menu_bars. */
9908
9909 static void
9910 build_desired_tool_bar_string (f)
9911 struct frame *f;
9912 {
9913 int i, size, size_needed;
9914 struct gcpro gcpro1, gcpro2, gcpro3;
9915 Lisp_Object image, plist, props;
9916
9917 image = plist = props = Qnil;
9918 GCPRO3 (image, plist, props);
9919
9920 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9921 Otherwise, make a new string. */
9922
9923 /* The size of the string we might be able to reuse. */
9924 size = (STRINGP (f->desired_tool_bar_string)
9925 ? SCHARS (f->desired_tool_bar_string)
9926 : 0);
9927
9928 /* We need one space in the string for each image. */
9929 size_needed = f->n_tool_bar_items;
9930
9931 /* Reuse f->desired_tool_bar_string, if possible. */
9932 if (size < size_needed || NILP (f->desired_tool_bar_string))
9933 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9934 make_number (' '));
9935 else
9936 {
9937 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9938 Fremove_text_properties (make_number (0), make_number (size),
9939 props, f->desired_tool_bar_string);
9940 }
9941
9942 /* Put a `display' property on the string for the images to display,
9943 put a `menu_item' property on tool-bar items with a value that
9944 is the index of the item in F's tool-bar item vector. */
9945 for (i = 0; i < f->n_tool_bar_items; ++i)
9946 {
9947 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9948
9949 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9950 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9951 int hmargin, vmargin, relief, idx, end;
9952 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9953
9954 /* If image is a vector, choose the image according to the
9955 button state. */
9956 image = PROP (TOOL_BAR_ITEM_IMAGES);
9957 if (VECTORP (image))
9958 {
9959 if (enabled_p)
9960 idx = (selected_p
9961 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9962 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9963 else
9964 idx = (selected_p
9965 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9966 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9967
9968 xassert (ASIZE (image) >= idx);
9969 image = AREF (image, idx);
9970 }
9971 else
9972 idx = -1;
9973
9974 /* Ignore invalid image specifications. */
9975 if (!valid_image_p (image))
9976 continue;
9977
9978 /* Display the tool-bar button pressed, or depressed. */
9979 plist = Fcopy_sequence (XCDR (image));
9980
9981 /* Compute margin and relief to draw. */
9982 relief = (tool_bar_button_relief >= 0
9983 ? tool_bar_button_relief
9984 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9985 hmargin = vmargin = relief;
9986
9987 if (INTEGERP (Vtool_bar_button_margin)
9988 && XINT (Vtool_bar_button_margin) > 0)
9989 {
9990 hmargin += XFASTINT (Vtool_bar_button_margin);
9991 vmargin += XFASTINT (Vtool_bar_button_margin);
9992 }
9993 else if (CONSP (Vtool_bar_button_margin))
9994 {
9995 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9996 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9997 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9998
9999 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10000 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10001 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10002 }
10003
10004 if (auto_raise_tool_bar_buttons_p)
10005 {
10006 /* Add a `:relief' property to the image spec if the item is
10007 selected. */
10008 if (selected_p)
10009 {
10010 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10011 hmargin -= relief;
10012 vmargin -= relief;
10013 }
10014 }
10015 else
10016 {
10017 /* If image is selected, display it pressed, i.e. with a
10018 negative relief. If it's not selected, display it with a
10019 raised relief. */
10020 plist = Fplist_put (plist, QCrelief,
10021 (selected_p
10022 ? make_number (-relief)
10023 : make_number (relief)));
10024 hmargin -= relief;
10025 vmargin -= relief;
10026 }
10027
10028 /* Put a margin around the image. */
10029 if (hmargin || vmargin)
10030 {
10031 if (hmargin == vmargin)
10032 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10033 else
10034 plist = Fplist_put (plist, QCmargin,
10035 Fcons (make_number (hmargin),
10036 make_number (vmargin)));
10037 }
10038
10039 /* If button is not enabled, and we don't have special images
10040 for the disabled state, make the image appear disabled by
10041 applying an appropriate algorithm to it. */
10042 if (!enabled_p && idx < 0)
10043 plist = Fplist_put (plist, QCconversion, Qdisabled);
10044
10045 /* Put a `display' text property on the string for the image to
10046 display. Put a `menu-item' property on the string that gives
10047 the start of this item's properties in the tool-bar items
10048 vector. */
10049 image = Fcons (Qimage, plist);
10050 props = list4 (Qdisplay, image,
10051 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10052
10053 /* Let the last image hide all remaining spaces in the tool bar
10054 string. The string can be longer than needed when we reuse a
10055 previous string. */
10056 if (i + 1 == f->n_tool_bar_items)
10057 end = SCHARS (f->desired_tool_bar_string);
10058 else
10059 end = i + 1;
10060 Fadd_text_properties (make_number (i), make_number (end),
10061 props, f->desired_tool_bar_string);
10062 #undef PROP
10063 }
10064
10065 UNGCPRO;
10066 }
10067
10068
10069 /* Display one line of the tool-bar of frame IT->f.
10070
10071 HEIGHT specifies the desired height of the tool-bar line.
10072 If the actual height of the glyph row is less than HEIGHT, the
10073 row's height is increased to HEIGHT, and the icons are centered
10074 vertically in the new height.
10075
10076 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10077 count a final empty row in case the tool-bar width exactly matches
10078 the window width.
10079 */
10080
10081 static void
10082 display_tool_bar_line (it, height)
10083 struct it *it;
10084 int height;
10085 {
10086 struct glyph_row *row = it->glyph_row;
10087 int max_x = it->last_visible_x;
10088 struct glyph *last;
10089
10090 prepare_desired_row (row);
10091 row->y = it->current_y;
10092
10093 /* Note that this isn't made use of if the face hasn't a box,
10094 so there's no need to check the face here. */
10095 it->start_of_box_run_p = 1;
10096
10097 while (it->current_x < max_x)
10098 {
10099 int x, n_glyphs_before, i, nglyphs;
10100 struct it it_before;
10101
10102 /* Get the next display element. */
10103 if (!get_next_display_element (it))
10104 {
10105 /* Don't count empty row if we are counting needed tool-bar lines. */
10106 if (height < 0 && !it->hpos)
10107 return;
10108 break;
10109 }
10110
10111 /* Produce glyphs. */
10112 n_glyphs_before = row->used[TEXT_AREA];
10113 it_before = *it;
10114
10115 PRODUCE_GLYPHS (it);
10116
10117 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10118 i = 0;
10119 x = it_before.current_x;
10120 while (i < nglyphs)
10121 {
10122 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10123
10124 if (x + glyph->pixel_width > max_x)
10125 {
10126 /* Glyph doesn't fit on line. Backtrack. */
10127 row->used[TEXT_AREA] = n_glyphs_before;
10128 *it = it_before;
10129 /* If this is the only glyph on this line, it will never fit on the
10130 toolbar, so skip it. But ensure there is at least one glyph,
10131 so we don't accidentally disable the tool-bar. */
10132 if (n_glyphs_before == 0
10133 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10134 break;
10135 goto out;
10136 }
10137
10138 ++it->hpos;
10139 x += glyph->pixel_width;
10140 ++i;
10141 }
10142
10143 /* Stop at line ends. */
10144 if (ITERATOR_AT_END_OF_LINE_P (it))
10145 break;
10146
10147 set_iterator_to_next (it, 1);
10148 }
10149
10150 out:;
10151
10152 row->displays_text_p = row->used[TEXT_AREA] != 0;
10153
10154 /* Use default face for the border below the tool bar.
10155
10156 FIXME: When auto-resize-tool-bars is grow-only, there is
10157 no additional border below the possibly empty tool-bar lines.
10158 So to make the extra empty lines look "normal", we have to
10159 use the tool-bar face for the border too. */
10160 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10161 it->face_id = DEFAULT_FACE_ID;
10162
10163 extend_face_to_end_of_line (it);
10164 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10165 last->right_box_line_p = 1;
10166 if (last == row->glyphs[TEXT_AREA])
10167 last->left_box_line_p = 1;
10168
10169 /* Make line the desired height and center it vertically. */
10170 if ((height -= it->max_ascent + it->max_descent) > 0)
10171 {
10172 /* Don't add more than one line height. */
10173 height %= FRAME_LINE_HEIGHT (it->f);
10174 it->max_ascent += height / 2;
10175 it->max_descent += (height + 1) / 2;
10176 }
10177
10178 compute_line_metrics (it);
10179
10180 /* If line is empty, make it occupy the rest of the tool-bar. */
10181 if (!row->displays_text_p)
10182 {
10183 row->height = row->phys_height = it->last_visible_y - row->y;
10184 row->visible_height = row->height;
10185 row->ascent = row->phys_ascent = 0;
10186 row->extra_line_spacing = 0;
10187 }
10188
10189 row->full_width_p = 1;
10190 row->continued_p = 0;
10191 row->truncated_on_left_p = 0;
10192 row->truncated_on_right_p = 0;
10193
10194 it->current_x = it->hpos = 0;
10195 it->current_y += row->height;
10196 ++it->vpos;
10197 ++it->glyph_row;
10198 }
10199
10200
10201 /* Max tool-bar height. */
10202
10203 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10204 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10205
10206 /* Value is the number of screen lines needed to make all tool-bar
10207 items of frame F visible. The number of actual rows needed is
10208 returned in *N_ROWS if non-NULL. */
10209
10210 static int
10211 tool_bar_lines_needed (f, n_rows)
10212 struct frame *f;
10213 int *n_rows;
10214 {
10215 struct window *w = XWINDOW (f->tool_bar_window);
10216 struct it it;
10217 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10218 the desired matrix, so use (unused) mode-line row as temporary row to
10219 avoid destroying the first tool-bar row. */
10220 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10221
10222 /* Initialize an iterator for iteration over
10223 F->desired_tool_bar_string in the tool-bar window of frame F. */
10224 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10225 it.first_visible_x = 0;
10226 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10227 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10228
10229 while (!ITERATOR_AT_END_P (&it))
10230 {
10231 clear_glyph_row (temp_row);
10232 it.glyph_row = temp_row;
10233 display_tool_bar_line (&it, -1);
10234 }
10235 clear_glyph_row (temp_row);
10236
10237 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10238 if (n_rows)
10239 *n_rows = it.vpos > 0 ? it.vpos : -1;
10240
10241 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10242 }
10243
10244
10245 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10246 0, 1, 0,
10247 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10248 (frame)
10249 Lisp_Object frame;
10250 {
10251 struct frame *f;
10252 struct window *w;
10253 int nlines = 0;
10254
10255 if (NILP (frame))
10256 frame = selected_frame;
10257 else
10258 CHECK_FRAME (frame);
10259 f = XFRAME (frame);
10260
10261 if (WINDOWP (f->tool_bar_window)
10262 || (w = XWINDOW (f->tool_bar_window),
10263 WINDOW_TOTAL_LINES (w) > 0))
10264 {
10265 update_tool_bar (f, 1);
10266 if (f->n_tool_bar_items)
10267 {
10268 build_desired_tool_bar_string (f);
10269 nlines = tool_bar_lines_needed (f, NULL);
10270 }
10271 }
10272
10273 return make_number (nlines);
10274 }
10275
10276
10277 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10278 height should be changed. */
10279
10280 static int
10281 redisplay_tool_bar (f)
10282 struct frame *f;
10283 {
10284 struct window *w;
10285 struct it it;
10286 struct glyph_row *row;
10287
10288 #if defined (USE_GTK) || defined (HAVE_NS)
10289 if (FRAME_EXTERNAL_TOOL_BAR (f))
10290 update_frame_tool_bar (f);
10291 return 0;
10292 #endif
10293
10294 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10295 do anything. This means you must start with tool-bar-lines
10296 non-zero to get the auto-sizing effect. Or in other words, you
10297 can turn off tool-bars by specifying tool-bar-lines zero. */
10298 if (!WINDOWP (f->tool_bar_window)
10299 || (w = XWINDOW (f->tool_bar_window),
10300 WINDOW_TOTAL_LINES (w) == 0))
10301 return 0;
10302
10303 /* Set up an iterator for the tool-bar window. */
10304 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10305 it.first_visible_x = 0;
10306 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10307 row = it.glyph_row;
10308
10309 /* Build a string that represents the contents of the tool-bar. */
10310 build_desired_tool_bar_string (f);
10311 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10312
10313 if (f->n_tool_bar_rows == 0)
10314 {
10315 int nlines;
10316
10317 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10318 nlines != WINDOW_TOTAL_LINES (w)))
10319 {
10320 extern Lisp_Object Qtool_bar_lines;
10321 Lisp_Object frame;
10322 int old_height = WINDOW_TOTAL_LINES (w);
10323
10324 XSETFRAME (frame, f);
10325 Fmodify_frame_parameters (frame,
10326 Fcons (Fcons (Qtool_bar_lines,
10327 make_number (nlines)),
10328 Qnil));
10329 if (WINDOW_TOTAL_LINES (w) != old_height)
10330 {
10331 clear_glyph_matrix (w->desired_matrix);
10332 fonts_changed_p = 1;
10333 return 1;
10334 }
10335 }
10336 }
10337
10338 /* Display as many lines as needed to display all tool-bar items. */
10339
10340 if (f->n_tool_bar_rows > 0)
10341 {
10342 int border, rows, height, extra;
10343
10344 if (INTEGERP (Vtool_bar_border))
10345 border = XINT (Vtool_bar_border);
10346 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10347 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10348 else if (EQ (Vtool_bar_border, Qborder_width))
10349 border = f->border_width;
10350 else
10351 border = 0;
10352 if (border < 0)
10353 border = 0;
10354
10355 rows = f->n_tool_bar_rows;
10356 height = max (1, (it.last_visible_y - border) / rows);
10357 extra = it.last_visible_y - border - height * rows;
10358
10359 while (it.current_y < it.last_visible_y)
10360 {
10361 int h = 0;
10362 if (extra > 0 && rows-- > 0)
10363 {
10364 h = (extra + rows - 1) / rows;
10365 extra -= h;
10366 }
10367 display_tool_bar_line (&it, height + h);
10368 }
10369 }
10370 else
10371 {
10372 while (it.current_y < it.last_visible_y)
10373 display_tool_bar_line (&it, 0);
10374 }
10375
10376 /* It doesn't make much sense to try scrolling in the tool-bar
10377 window, so don't do it. */
10378 w->desired_matrix->no_scrolling_p = 1;
10379 w->must_be_updated_p = 1;
10380
10381 if (!NILP (Vauto_resize_tool_bars))
10382 {
10383 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10384 int change_height_p = 0;
10385
10386 /* If we couldn't display everything, change the tool-bar's
10387 height if there is room for more. */
10388 if (IT_STRING_CHARPOS (it) < it.end_charpos
10389 && it.current_y < max_tool_bar_height)
10390 change_height_p = 1;
10391
10392 row = it.glyph_row - 1;
10393
10394 /* If there are blank lines at the end, except for a partially
10395 visible blank line at the end that is smaller than
10396 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10397 if (!row->displays_text_p
10398 && row->height >= FRAME_LINE_HEIGHT (f))
10399 change_height_p = 1;
10400
10401 /* If row displays tool-bar items, but is partially visible,
10402 change the tool-bar's height. */
10403 if (row->displays_text_p
10404 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10405 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10406 change_height_p = 1;
10407
10408 /* Resize windows as needed by changing the `tool-bar-lines'
10409 frame parameter. */
10410 if (change_height_p)
10411 {
10412 extern Lisp_Object Qtool_bar_lines;
10413 Lisp_Object frame;
10414 int old_height = WINDOW_TOTAL_LINES (w);
10415 int nrows;
10416 int nlines = tool_bar_lines_needed (f, &nrows);
10417
10418 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10419 && !f->minimize_tool_bar_window_p)
10420 ? (nlines > old_height)
10421 : (nlines != old_height));
10422 f->minimize_tool_bar_window_p = 0;
10423
10424 if (change_height_p)
10425 {
10426 XSETFRAME (frame, f);
10427 Fmodify_frame_parameters (frame,
10428 Fcons (Fcons (Qtool_bar_lines,
10429 make_number (nlines)),
10430 Qnil));
10431 if (WINDOW_TOTAL_LINES (w) != old_height)
10432 {
10433 clear_glyph_matrix (w->desired_matrix);
10434 f->n_tool_bar_rows = nrows;
10435 fonts_changed_p = 1;
10436 return 1;
10437 }
10438 }
10439 }
10440 }
10441
10442 f->minimize_tool_bar_window_p = 0;
10443 return 0;
10444 }
10445
10446
10447 /* Get information about the tool-bar item which is displayed in GLYPH
10448 on frame F. Return in *PROP_IDX the index where tool-bar item
10449 properties start in F->tool_bar_items. Value is zero if
10450 GLYPH doesn't display a tool-bar item. */
10451
10452 static int
10453 tool_bar_item_info (f, glyph, prop_idx)
10454 struct frame *f;
10455 struct glyph *glyph;
10456 int *prop_idx;
10457 {
10458 Lisp_Object prop;
10459 int success_p;
10460 int charpos;
10461
10462 /* This function can be called asynchronously, which means we must
10463 exclude any possibility that Fget_text_property signals an
10464 error. */
10465 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10466 charpos = max (0, charpos);
10467
10468 /* Get the text property `menu-item' at pos. The value of that
10469 property is the start index of this item's properties in
10470 F->tool_bar_items. */
10471 prop = Fget_text_property (make_number (charpos),
10472 Qmenu_item, f->current_tool_bar_string);
10473 if (INTEGERP (prop))
10474 {
10475 *prop_idx = XINT (prop);
10476 success_p = 1;
10477 }
10478 else
10479 success_p = 0;
10480
10481 return success_p;
10482 }
10483
10484 \f
10485 /* Get information about the tool-bar item at position X/Y on frame F.
10486 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10487 the current matrix of the tool-bar window of F, or NULL if not
10488 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10489 item in F->tool_bar_items. Value is
10490
10491 -1 if X/Y is not on a tool-bar item
10492 0 if X/Y is on the same item that was highlighted before.
10493 1 otherwise. */
10494
10495 static int
10496 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10497 struct frame *f;
10498 int x, y;
10499 struct glyph **glyph;
10500 int *hpos, *vpos, *prop_idx;
10501 {
10502 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10503 struct window *w = XWINDOW (f->tool_bar_window);
10504 int area;
10505
10506 /* Find the glyph under X/Y. */
10507 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10508 if (*glyph == NULL)
10509 return -1;
10510
10511 /* Get the start of this tool-bar item's properties in
10512 f->tool_bar_items. */
10513 if (!tool_bar_item_info (f, *glyph, prop_idx))
10514 return -1;
10515
10516 /* Is mouse on the highlighted item? */
10517 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10518 && *vpos >= dpyinfo->mouse_face_beg_row
10519 && *vpos <= dpyinfo->mouse_face_end_row
10520 && (*vpos > dpyinfo->mouse_face_beg_row
10521 || *hpos >= dpyinfo->mouse_face_beg_col)
10522 && (*vpos < dpyinfo->mouse_face_end_row
10523 || *hpos < dpyinfo->mouse_face_end_col
10524 || dpyinfo->mouse_face_past_end))
10525 return 0;
10526
10527 return 1;
10528 }
10529
10530
10531 /* EXPORT:
10532 Handle mouse button event on the tool-bar of frame F, at
10533 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10534 0 for button release. MODIFIERS is event modifiers for button
10535 release. */
10536
10537 void
10538 handle_tool_bar_click (f, x, y, down_p, modifiers)
10539 struct frame *f;
10540 int x, y, down_p;
10541 unsigned int modifiers;
10542 {
10543 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10544 struct window *w = XWINDOW (f->tool_bar_window);
10545 int hpos, vpos, prop_idx;
10546 struct glyph *glyph;
10547 Lisp_Object enabled_p;
10548
10549 /* If not on the highlighted tool-bar item, return. */
10550 frame_to_window_pixel_xy (w, &x, &y);
10551 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10552 return;
10553
10554 /* If item is disabled, do nothing. */
10555 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10556 if (NILP (enabled_p))
10557 return;
10558
10559 if (down_p)
10560 {
10561 /* Show item in pressed state. */
10562 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10563 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10564 last_tool_bar_item = prop_idx;
10565 }
10566 else
10567 {
10568 Lisp_Object key, frame;
10569 struct input_event event;
10570 EVENT_INIT (event);
10571
10572 /* Show item in released state. */
10573 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10574 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10575
10576 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10577
10578 XSETFRAME (frame, f);
10579 event.kind = TOOL_BAR_EVENT;
10580 event.frame_or_window = frame;
10581 event.arg = frame;
10582 kbd_buffer_store_event (&event);
10583
10584 event.kind = TOOL_BAR_EVENT;
10585 event.frame_or_window = frame;
10586 event.arg = key;
10587 event.modifiers = modifiers;
10588 kbd_buffer_store_event (&event);
10589 last_tool_bar_item = -1;
10590 }
10591 }
10592
10593
10594 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10595 tool-bar window-relative coordinates X/Y. Called from
10596 note_mouse_highlight. */
10597
10598 static void
10599 note_tool_bar_highlight (f, x, y)
10600 struct frame *f;
10601 int x, y;
10602 {
10603 Lisp_Object window = f->tool_bar_window;
10604 struct window *w = XWINDOW (window);
10605 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10606 int hpos, vpos;
10607 struct glyph *glyph;
10608 struct glyph_row *row;
10609 int i;
10610 Lisp_Object enabled_p;
10611 int prop_idx;
10612 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10613 int mouse_down_p, rc;
10614
10615 /* Function note_mouse_highlight is called with negative x(y
10616 values when mouse moves outside of the frame. */
10617 if (x <= 0 || y <= 0)
10618 {
10619 clear_mouse_face (dpyinfo);
10620 return;
10621 }
10622
10623 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10624 if (rc < 0)
10625 {
10626 /* Not on tool-bar item. */
10627 clear_mouse_face (dpyinfo);
10628 return;
10629 }
10630 else if (rc == 0)
10631 /* On same tool-bar item as before. */
10632 goto set_help_echo;
10633
10634 clear_mouse_face (dpyinfo);
10635
10636 /* Mouse is down, but on different tool-bar item? */
10637 mouse_down_p = (dpyinfo->grabbed
10638 && f == last_mouse_frame
10639 && FRAME_LIVE_P (f));
10640 if (mouse_down_p
10641 && last_tool_bar_item != prop_idx)
10642 return;
10643
10644 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10645 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10646
10647 /* If tool-bar item is not enabled, don't highlight it. */
10648 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10649 if (!NILP (enabled_p))
10650 {
10651 /* Compute the x-position of the glyph. In front and past the
10652 image is a space. We include this in the highlighted area. */
10653 row = MATRIX_ROW (w->current_matrix, vpos);
10654 for (i = x = 0; i < hpos; ++i)
10655 x += row->glyphs[TEXT_AREA][i].pixel_width;
10656
10657 /* Record this as the current active region. */
10658 dpyinfo->mouse_face_beg_col = hpos;
10659 dpyinfo->mouse_face_beg_row = vpos;
10660 dpyinfo->mouse_face_beg_x = x;
10661 dpyinfo->mouse_face_beg_y = row->y;
10662 dpyinfo->mouse_face_past_end = 0;
10663
10664 dpyinfo->mouse_face_end_col = hpos + 1;
10665 dpyinfo->mouse_face_end_row = vpos;
10666 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10667 dpyinfo->mouse_face_end_y = row->y;
10668 dpyinfo->mouse_face_window = window;
10669 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10670
10671 /* Display it as active. */
10672 show_mouse_face (dpyinfo, draw);
10673 dpyinfo->mouse_face_image_state = draw;
10674 }
10675
10676 set_help_echo:
10677
10678 /* Set help_echo_string to a help string to display for this tool-bar item.
10679 XTread_socket does the rest. */
10680 help_echo_object = help_echo_window = Qnil;
10681 help_echo_pos = -1;
10682 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10683 if (NILP (help_echo_string))
10684 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10685 }
10686
10687 #endif /* HAVE_WINDOW_SYSTEM */
10688
10689
10690 \f
10691 /************************************************************************
10692 Horizontal scrolling
10693 ************************************************************************/
10694
10695 static int hscroll_window_tree P_ ((Lisp_Object));
10696 static int hscroll_windows P_ ((Lisp_Object));
10697
10698 /* For all leaf windows in the window tree rooted at WINDOW, set their
10699 hscroll value so that PT is (i) visible in the window, and (ii) so
10700 that it is not within a certain margin at the window's left and
10701 right border. Value is non-zero if any window's hscroll has been
10702 changed. */
10703
10704 static int
10705 hscroll_window_tree (window)
10706 Lisp_Object window;
10707 {
10708 int hscrolled_p = 0;
10709 int hscroll_relative_p = FLOATP (Vhscroll_step);
10710 int hscroll_step_abs = 0;
10711 double hscroll_step_rel = 0;
10712
10713 if (hscroll_relative_p)
10714 {
10715 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10716 if (hscroll_step_rel < 0)
10717 {
10718 hscroll_relative_p = 0;
10719 hscroll_step_abs = 0;
10720 }
10721 }
10722 else if (INTEGERP (Vhscroll_step))
10723 {
10724 hscroll_step_abs = XINT (Vhscroll_step);
10725 if (hscroll_step_abs < 0)
10726 hscroll_step_abs = 0;
10727 }
10728 else
10729 hscroll_step_abs = 0;
10730
10731 while (WINDOWP (window))
10732 {
10733 struct window *w = XWINDOW (window);
10734
10735 if (WINDOWP (w->hchild))
10736 hscrolled_p |= hscroll_window_tree (w->hchild);
10737 else if (WINDOWP (w->vchild))
10738 hscrolled_p |= hscroll_window_tree (w->vchild);
10739 else if (w->cursor.vpos >= 0)
10740 {
10741 int h_margin;
10742 int text_area_width;
10743 struct glyph_row *current_cursor_row
10744 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10745 struct glyph_row *desired_cursor_row
10746 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10747 struct glyph_row *cursor_row
10748 = (desired_cursor_row->enabled_p
10749 ? desired_cursor_row
10750 : current_cursor_row);
10751
10752 text_area_width = window_box_width (w, TEXT_AREA);
10753
10754 /* Scroll when cursor is inside this scroll margin. */
10755 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10756
10757 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10758 && ((XFASTINT (w->hscroll)
10759 && w->cursor.x <= h_margin)
10760 || (cursor_row->enabled_p
10761 && cursor_row->truncated_on_right_p
10762 && (w->cursor.x >= text_area_width - h_margin))))
10763 {
10764 struct it it;
10765 int hscroll;
10766 struct buffer *saved_current_buffer;
10767 int pt;
10768 int wanted_x;
10769
10770 /* Find point in a display of infinite width. */
10771 saved_current_buffer = current_buffer;
10772 current_buffer = XBUFFER (w->buffer);
10773
10774 if (w == XWINDOW (selected_window))
10775 pt = PT;
10776 else
10777 {
10778 pt = marker_position (w->pointm);
10779 pt = max (BEGV, pt);
10780 pt = min (ZV, pt);
10781 }
10782
10783 /* Move iterator to pt starting at cursor_row->start in
10784 a line with infinite width. */
10785 init_to_row_start (&it, w, cursor_row);
10786 it.last_visible_x = INFINITY;
10787 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10788 current_buffer = saved_current_buffer;
10789
10790 /* Position cursor in window. */
10791 if (!hscroll_relative_p && hscroll_step_abs == 0)
10792 hscroll = max (0, (it.current_x
10793 - (ITERATOR_AT_END_OF_LINE_P (&it)
10794 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10795 : (text_area_width / 2))))
10796 / FRAME_COLUMN_WIDTH (it.f);
10797 else if (w->cursor.x >= text_area_width - h_margin)
10798 {
10799 if (hscroll_relative_p)
10800 wanted_x = text_area_width * (1 - hscroll_step_rel)
10801 - h_margin;
10802 else
10803 wanted_x = text_area_width
10804 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10805 - h_margin;
10806 hscroll
10807 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10808 }
10809 else
10810 {
10811 if (hscroll_relative_p)
10812 wanted_x = text_area_width * hscroll_step_rel
10813 + h_margin;
10814 else
10815 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10816 + h_margin;
10817 hscroll
10818 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10819 }
10820 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10821
10822 /* Don't call Fset_window_hscroll if value hasn't
10823 changed because it will prevent redisplay
10824 optimizations. */
10825 if (XFASTINT (w->hscroll) != hscroll)
10826 {
10827 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10828 w->hscroll = make_number (hscroll);
10829 hscrolled_p = 1;
10830 }
10831 }
10832 }
10833
10834 window = w->next;
10835 }
10836
10837 /* Value is non-zero if hscroll of any leaf window has been changed. */
10838 return hscrolled_p;
10839 }
10840
10841
10842 /* Set hscroll so that cursor is visible and not inside horizontal
10843 scroll margins for all windows in the tree rooted at WINDOW. See
10844 also hscroll_window_tree above. Value is non-zero if any window's
10845 hscroll has been changed. If it has, desired matrices on the frame
10846 of WINDOW are cleared. */
10847
10848 static int
10849 hscroll_windows (window)
10850 Lisp_Object window;
10851 {
10852 int hscrolled_p = hscroll_window_tree (window);
10853 if (hscrolled_p)
10854 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10855 return hscrolled_p;
10856 }
10857
10858
10859 \f
10860 /************************************************************************
10861 Redisplay
10862 ************************************************************************/
10863
10864 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10865 to a non-zero value. This is sometimes handy to have in a debugger
10866 session. */
10867
10868 #if GLYPH_DEBUG
10869
10870 /* First and last unchanged row for try_window_id. */
10871
10872 int debug_first_unchanged_at_end_vpos;
10873 int debug_last_unchanged_at_beg_vpos;
10874
10875 /* Delta vpos and y. */
10876
10877 int debug_dvpos, debug_dy;
10878
10879 /* Delta in characters and bytes for try_window_id. */
10880
10881 int debug_delta, debug_delta_bytes;
10882
10883 /* Values of window_end_pos and window_end_vpos at the end of
10884 try_window_id. */
10885
10886 EMACS_INT debug_end_pos, debug_end_vpos;
10887
10888 /* Append a string to W->desired_matrix->method. FMT is a printf
10889 format string. A1...A9 are a supplement for a variable-length
10890 argument list. If trace_redisplay_p is non-zero also printf the
10891 resulting string to stderr. */
10892
10893 static void
10894 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10895 struct window *w;
10896 char *fmt;
10897 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10898 {
10899 char buffer[512];
10900 char *method = w->desired_matrix->method;
10901 int len = strlen (method);
10902 int size = sizeof w->desired_matrix->method;
10903 int remaining = size - len - 1;
10904
10905 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10906 if (len && remaining)
10907 {
10908 method[len] = '|';
10909 --remaining, ++len;
10910 }
10911
10912 strncpy (method + len, buffer, remaining);
10913
10914 if (trace_redisplay_p)
10915 fprintf (stderr, "%p (%s): %s\n",
10916 w,
10917 ((BUFFERP (w->buffer)
10918 && STRINGP (XBUFFER (w->buffer)->name))
10919 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10920 : "no buffer"),
10921 buffer);
10922 }
10923
10924 #endif /* GLYPH_DEBUG */
10925
10926
10927 /* Value is non-zero if all changes in window W, which displays
10928 current_buffer, are in the text between START and END. START is a
10929 buffer position, END is given as a distance from Z. Used in
10930 redisplay_internal for display optimization. */
10931
10932 static INLINE int
10933 text_outside_line_unchanged_p (w, start, end)
10934 struct window *w;
10935 int start, end;
10936 {
10937 int unchanged_p = 1;
10938
10939 /* If text or overlays have changed, see where. */
10940 if (XFASTINT (w->last_modified) < MODIFF
10941 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10942 {
10943 /* Gap in the line? */
10944 if (GPT < start || Z - GPT < end)
10945 unchanged_p = 0;
10946
10947 /* Changes start in front of the line, or end after it? */
10948 if (unchanged_p
10949 && (BEG_UNCHANGED < start - 1
10950 || END_UNCHANGED < end))
10951 unchanged_p = 0;
10952
10953 /* If selective display, can't optimize if changes start at the
10954 beginning of the line. */
10955 if (unchanged_p
10956 && INTEGERP (current_buffer->selective_display)
10957 && XINT (current_buffer->selective_display) > 0
10958 && (BEG_UNCHANGED < start || GPT <= start))
10959 unchanged_p = 0;
10960
10961 /* If there are overlays at the start or end of the line, these
10962 may have overlay strings with newlines in them. A change at
10963 START, for instance, may actually concern the display of such
10964 overlay strings as well, and they are displayed on different
10965 lines. So, quickly rule out this case. (For the future, it
10966 might be desirable to implement something more telling than
10967 just BEG/END_UNCHANGED.) */
10968 if (unchanged_p)
10969 {
10970 if (BEG + BEG_UNCHANGED == start
10971 && overlay_touches_p (start))
10972 unchanged_p = 0;
10973 if (END_UNCHANGED == end
10974 && overlay_touches_p (Z - end))
10975 unchanged_p = 0;
10976 }
10977 }
10978
10979 return unchanged_p;
10980 }
10981
10982
10983 /* Do a frame update, taking possible shortcuts into account. This is
10984 the main external entry point for redisplay.
10985
10986 If the last redisplay displayed an echo area message and that message
10987 is no longer requested, we clear the echo area or bring back the
10988 mini-buffer if that is in use. */
10989
10990 void
10991 redisplay ()
10992 {
10993 redisplay_internal (0);
10994 }
10995
10996
10997 static Lisp_Object
10998 overlay_arrow_string_or_property (var)
10999 Lisp_Object var;
11000 {
11001 Lisp_Object val;
11002
11003 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11004 return val;
11005
11006 return Voverlay_arrow_string;
11007 }
11008
11009 /* Return 1 if there are any overlay-arrows in current_buffer. */
11010 static int
11011 overlay_arrow_in_current_buffer_p ()
11012 {
11013 Lisp_Object vlist;
11014
11015 for (vlist = Voverlay_arrow_variable_list;
11016 CONSP (vlist);
11017 vlist = XCDR (vlist))
11018 {
11019 Lisp_Object var = XCAR (vlist);
11020 Lisp_Object val;
11021
11022 if (!SYMBOLP (var))
11023 continue;
11024 val = find_symbol_value (var);
11025 if (MARKERP (val)
11026 && current_buffer == XMARKER (val)->buffer)
11027 return 1;
11028 }
11029 return 0;
11030 }
11031
11032
11033 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11034 has changed. */
11035
11036 static int
11037 overlay_arrows_changed_p ()
11038 {
11039 Lisp_Object vlist;
11040
11041 for (vlist = Voverlay_arrow_variable_list;
11042 CONSP (vlist);
11043 vlist = XCDR (vlist))
11044 {
11045 Lisp_Object var = XCAR (vlist);
11046 Lisp_Object val, pstr;
11047
11048 if (!SYMBOLP (var))
11049 continue;
11050 val = find_symbol_value (var);
11051 if (!MARKERP (val))
11052 continue;
11053 if (! EQ (COERCE_MARKER (val),
11054 Fget (var, Qlast_arrow_position))
11055 || ! (pstr = overlay_arrow_string_or_property (var),
11056 EQ (pstr, Fget (var, Qlast_arrow_string))))
11057 return 1;
11058 }
11059 return 0;
11060 }
11061
11062 /* Mark overlay arrows to be updated on next redisplay. */
11063
11064 static void
11065 update_overlay_arrows (up_to_date)
11066 int up_to_date;
11067 {
11068 Lisp_Object vlist;
11069
11070 for (vlist = Voverlay_arrow_variable_list;
11071 CONSP (vlist);
11072 vlist = XCDR (vlist))
11073 {
11074 Lisp_Object var = XCAR (vlist);
11075
11076 if (!SYMBOLP (var))
11077 continue;
11078
11079 if (up_to_date > 0)
11080 {
11081 Lisp_Object val = find_symbol_value (var);
11082 Fput (var, Qlast_arrow_position,
11083 COERCE_MARKER (val));
11084 Fput (var, Qlast_arrow_string,
11085 overlay_arrow_string_or_property (var));
11086 }
11087 else if (up_to_date < 0
11088 || !NILP (Fget (var, Qlast_arrow_position)))
11089 {
11090 Fput (var, Qlast_arrow_position, Qt);
11091 Fput (var, Qlast_arrow_string, Qt);
11092 }
11093 }
11094 }
11095
11096
11097 /* Return overlay arrow string to display at row.
11098 Return integer (bitmap number) for arrow bitmap in left fringe.
11099 Return nil if no overlay arrow. */
11100
11101 static Lisp_Object
11102 overlay_arrow_at_row (it, row)
11103 struct it *it;
11104 struct glyph_row *row;
11105 {
11106 Lisp_Object vlist;
11107
11108 for (vlist = Voverlay_arrow_variable_list;
11109 CONSP (vlist);
11110 vlist = XCDR (vlist))
11111 {
11112 Lisp_Object var = XCAR (vlist);
11113 Lisp_Object val;
11114
11115 if (!SYMBOLP (var))
11116 continue;
11117
11118 val = find_symbol_value (var);
11119
11120 if (MARKERP (val)
11121 && current_buffer == XMARKER (val)->buffer
11122 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11123 {
11124 if (FRAME_WINDOW_P (it->f)
11125 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11126 {
11127 #ifdef HAVE_WINDOW_SYSTEM
11128 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11129 {
11130 int fringe_bitmap;
11131 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11132 return make_number (fringe_bitmap);
11133 }
11134 #endif
11135 return make_number (-1); /* Use default arrow bitmap */
11136 }
11137 return overlay_arrow_string_or_property (var);
11138 }
11139 }
11140
11141 return Qnil;
11142 }
11143
11144 /* Return 1 if point moved out of or into a composition. Otherwise
11145 return 0. PREV_BUF and PREV_PT are the last point buffer and
11146 position. BUF and PT are the current point buffer and position. */
11147
11148 int
11149 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11150 struct buffer *prev_buf, *buf;
11151 int prev_pt, pt;
11152 {
11153 EMACS_INT start, end;
11154 Lisp_Object prop;
11155 Lisp_Object buffer;
11156
11157 XSETBUFFER (buffer, buf);
11158 /* Check a composition at the last point if point moved within the
11159 same buffer. */
11160 if (prev_buf == buf)
11161 {
11162 if (prev_pt == pt)
11163 /* Point didn't move. */
11164 return 0;
11165
11166 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11167 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11168 && COMPOSITION_VALID_P (start, end, prop)
11169 && start < prev_pt && end > prev_pt)
11170 /* The last point was within the composition. Return 1 iff
11171 point moved out of the composition. */
11172 return (pt <= start || pt >= end);
11173 }
11174
11175 /* Check a composition at the current point. */
11176 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11177 && find_composition (pt, -1, &start, &end, &prop, buffer)
11178 && COMPOSITION_VALID_P (start, end, prop)
11179 && start < pt && end > pt);
11180 }
11181
11182
11183 /* Reconsider the setting of B->clip_changed which is displayed
11184 in window W. */
11185
11186 static INLINE void
11187 reconsider_clip_changes (w, b)
11188 struct window *w;
11189 struct buffer *b;
11190 {
11191 if (b->clip_changed
11192 && !NILP (w->window_end_valid)
11193 && w->current_matrix->buffer == b
11194 && w->current_matrix->zv == BUF_ZV (b)
11195 && w->current_matrix->begv == BUF_BEGV (b))
11196 b->clip_changed = 0;
11197
11198 /* If display wasn't paused, and W is not a tool bar window, see if
11199 point has been moved into or out of a composition. In that case,
11200 we set b->clip_changed to 1 to force updating the screen. If
11201 b->clip_changed has already been set to 1, we can skip this
11202 check. */
11203 if (!b->clip_changed
11204 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11205 {
11206 int pt;
11207
11208 if (w == XWINDOW (selected_window))
11209 pt = PT;
11210 else
11211 pt = marker_position (w->pointm);
11212
11213 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11214 || pt != XINT (w->last_point))
11215 && check_point_in_composition (w->current_matrix->buffer,
11216 XINT (w->last_point),
11217 XBUFFER (w->buffer), pt))
11218 b->clip_changed = 1;
11219 }
11220 }
11221 \f
11222
11223 /* Select FRAME to forward the values of frame-local variables into C
11224 variables so that the redisplay routines can access those values
11225 directly. */
11226
11227 static void
11228 select_frame_for_redisplay (frame)
11229 Lisp_Object frame;
11230 {
11231 Lisp_Object tail, symbol, val;
11232 Lisp_Object old = selected_frame;
11233 struct Lisp_Symbol *sym;
11234
11235 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11236
11237 selected_frame = frame;
11238
11239 do
11240 {
11241 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11242 if (CONSP (XCAR (tail))
11243 && (symbol = XCAR (XCAR (tail)),
11244 SYMBOLP (symbol))
11245 && (sym = indirect_variable (XSYMBOL (symbol)),
11246 val = sym->value,
11247 (BUFFER_LOCAL_VALUEP (val)))
11248 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11249 /* Use find_symbol_value rather than Fsymbol_value
11250 to avoid an error if it is void. */
11251 find_symbol_value (symbol);
11252 } while (!EQ (frame, old) && (frame = old, 1));
11253 }
11254
11255
11256 #define STOP_POLLING \
11257 do { if (! polling_stopped_here) stop_polling (); \
11258 polling_stopped_here = 1; } while (0)
11259
11260 #define RESUME_POLLING \
11261 do { if (polling_stopped_here) start_polling (); \
11262 polling_stopped_here = 0; } while (0)
11263
11264
11265 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11266 response to any user action; therefore, we should preserve the echo
11267 area. (Actually, our caller does that job.) Perhaps in the future
11268 avoid recentering windows if it is not necessary; currently that
11269 causes some problems. */
11270
11271 static void
11272 redisplay_internal (preserve_echo_area)
11273 int preserve_echo_area;
11274 {
11275 struct window *w = XWINDOW (selected_window);
11276 struct window *sw;
11277 struct frame *f;
11278 int pause;
11279 int must_finish = 0;
11280 struct text_pos tlbufpos, tlendpos;
11281 int number_of_visible_frames;
11282 int count, count1;
11283 struct frame *sf;
11284 int polling_stopped_here = 0;
11285 Lisp_Object old_frame = selected_frame;
11286
11287 /* Non-zero means redisplay has to consider all windows on all
11288 frames. Zero means, only selected_window is considered. */
11289 int consider_all_windows_p;
11290
11291 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11292
11293 /* No redisplay if running in batch mode or frame is not yet fully
11294 initialized, or redisplay is explicitly turned off by setting
11295 Vinhibit_redisplay. */
11296 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11297 || !NILP (Vinhibit_redisplay))
11298 return;
11299
11300 /* Don't examine these until after testing Vinhibit_redisplay.
11301 When Emacs is shutting down, perhaps because its connection to
11302 X has dropped, we should not look at them at all. */
11303 f = XFRAME (w->frame);
11304 sf = SELECTED_FRAME ();
11305
11306 if (!f->glyphs_initialized_p)
11307 return;
11308
11309 /* The flag redisplay_performed_directly_p is set by
11310 direct_output_for_insert when it already did the whole screen
11311 update necessary. */
11312 if (redisplay_performed_directly_p)
11313 {
11314 redisplay_performed_directly_p = 0;
11315 if (!hscroll_windows (selected_window))
11316 return;
11317 }
11318
11319 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11320 if (popup_activated ())
11321 return;
11322 #endif
11323
11324 /* I don't think this happens but let's be paranoid. */
11325 if (redisplaying_p)
11326 return;
11327
11328 /* Record a function that resets redisplaying_p to its old value
11329 when we leave this function. */
11330 count = SPECPDL_INDEX ();
11331 record_unwind_protect (unwind_redisplay,
11332 Fcons (make_number (redisplaying_p), selected_frame));
11333 ++redisplaying_p;
11334 specbind (Qinhibit_free_realized_faces, Qnil);
11335
11336 {
11337 Lisp_Object tail, frame;
11338
11339 FOR_EACH_FRAME (tail, frame)
11340 {
11341 struct frame *f = XFRAME (frame);
11342 f->already_hscrolled_p = 0;
11343 }
11344 }
11345
11346 retry:
11347 /* Remember the currently selected window. */
11348 sw = w;
11349
11350 if (!EQ (old_frame, selected_frame)
11351 && FRAME_LIVE_P (XFRAME (old_frame)))
11352 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11353 selected_frame and selected_window to be temporarily out-of-sync so
11354 when we come back here via `goto retry', we need to resync because we
11355 may need to run Elisp code (via prepare_menu_bars). */
11356 select_frame_for_redisplay (old_frame);
11357
11358 pause = 0;
11359 reconsider_clip_changes (w, current_buffer);
11360 last_escape_glyph_frame = NULL;
11361 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11362
11363 /* If new fonts have been loaded that make a glyph matrix adjustment
11364 necessary, do it. */
11365 if (fonts_changed_p)
11366 {
11367 adjust_glyphs (NULL);
11368 ++windows_or_buffers_changed;
11369 fonts_changed_p = 0;
11370 }
11371
11372 /* If face_change_count is non-zero, init_iterator will free all
11373 realized faces, which includes the faces referenced from current
11374 matrices. So, we can't reuse current matrices in this case. */
11375 if (face_change_count)
11376 ++windows_or_buffers_changed;
11377
11378 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11379 && FRAME_TTY (sf)->previous_frame != sf)
11380 {
11381 /* Since frames on a single ASCII terminal share the same
11382 display area, displaying a different frame means redisplay
11383 the whole thing. */
11384 windows_or_buffers_changed++;
11385 SET_FRAME_GARBAGED (sf);
11386 #ifndef DOS_NT
11387 set_tty_color_mode (FRAME_TTY (sf), sf);
11388 #endif
11389 FRAME_TTY (sf)->previous_frame = sf;
11390 }
11391
11392 /* Set the visible flags for all frames. Do this before checking
11393 for resized or garbaged frames; they want to know if their frames
11394 are visible. See the comment in frame.h for
11395 FRAME_SAMPLE_VISIBILITY. */
11396 {
11397 Lisp_Object tail, frame;
11398
11399 number_of_visible_frames = 0;
11400
11401 FOR_EACH_FRAME (tail, frame)
11402 {
11403 struct frame *f = XFRAME (frame);
11404
11405 FRAME_SAMPLE_VISIBILITY (f);
11406 if (FRAME_VISIBLE_P (f))
11407 ++number_of_visible_frames;
11408 clear_desired_matrices (f);
11409 }
11410 }
11411
11412 /* Notice any pending interrupt request to change frame size. */
11413 do_pending_window_change (1);
11414
11415 /* do_pending_window_change could change the selected_window due to
11416 frame resizing which makes the selected window too small. */
11417 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
11418 {
11419 sw = w;
11420 reconsider_clip_changes (w, current_buffer);
11421 }
11422
11423 /* Clear frames marked as garbaged. */
11424 if (frame_garbaged)
11425 clear_garbaged_frames ();
11426
11427 /* Build menubar and tool-bar items. */
11428 if (NILP (Vmemory_full))
11429 prepare_menu_bars ();
11430
11431 if (windows_or_buffers_changed)
11432 update_mode_lines++;
11433
11434 /* Detect case that we need to write or remove a star in the mode line. */
11435 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11436 {
11437 w->update_mode_line = Qt;
11438 if (buffer_shared > 1)
11439 update_mode_lines++;
11440 }
11441
11442 /* Avoid invocation of point motion hooks by `current_column' below. */
11443 count1 = SPECPDL_INDEX ();
11444 specbind (Qinhibit_point_motion_hooks, Qt);
11445
11446 /* If %c is in the mode line, update it if needed. */
11447 if (!NILP (w->column_number_displayed)
11448 /* This alternative quickly identifies a common case
11449 where no change is needed. */
11450 && !(PT == XFASTINT (w->last_point)
11451 && XFASTINT (w->last_modified) >= MODIFF
11452 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11453 && (XFASTINT (w->column_number_displayed)
11454 != (int) current_column ())) /* iftc */
11455 w->update_mode_line = Qt;
11456
11457 unbind_to (count1, Qnil);
11458
11459 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11460
11461 /* The variable buffer_shared is set in redisplay_window and
11462 indicates that we redisplay a buffer in different windows. See
11463 there. */
11464 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11465 || cursor_type_changed);
11466
11467 /* If specs for an arrow have changed, do thorough redisplay
11468 to ensure we remove any arrow that should no longer exist. */
11469 if (overlay_arrows_changed_p ())
11470 consider_all_windows_p = windows_or_buffers_changed = 1;
11471
11472 /* Normally the message* functions will have already displayed and
11473 updated the echo area, but the frame may have been trashed, or
11474 the update may have been preempted, so display the echo area
11475 again here. Checking message_cleared_p captures the case that
11476 the echo area should be cleared. */
11477 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11478 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11479 || (message_cleared_p
11480 && minibuf_level == 0
11481 /* If the mini-window is currently selected, this means the
11482 echo-area doesn't show through. */
11483 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11484 {
11485 int window_height_changed_p = echo_area_display (0);
11486 must_finish = 1;
11487
11488 /* If we don't display the current message, don't clear the
11489 message_cleared_p flag, because, if we did, we wouldn't clear
11490 the echo area in the next redisplay which doesn't preserve
11491 the echo area. */
11492 if (!display_last_displayed_message_p)
11493 message_cleared_p = 0;
11494
11495 if (fonts_changed_p)
11496 goto retry;
11497 else if (window_height_changed_p)
11498 {
11499 consider_all_windows_p = 1;
11500 ++update_mode_lines;
11501 ++windows_or_buffers_changed;
11502
11503 /* If window configuration was changed, frames may have been
11504 marked garbaged. Clear them or we will experience
11505 surprises wrt scrolling. */
11506 if (frame_garbaged)
11507 clear_garbaged_frames ();
11508 }
11509 }
11510 else if (EQ (selected_window, minibuf_window)
11511 && (current_buffer->clip_changed
11512 || XFASTINT (w->last_modified) < MODIFF
11513 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11514 && resize_mini_window (w, 0))
11515 {
11516 /* Resized active mini-window to fit the size of what it is
11517 showing if its contents might have changed. */
11518 must_finish = 1;
11519 /* FIXME: this causes all frames to be updated, which seems unnecessary
11520 since only the current frame needs to be considered. This function needs
11521 to be rewritten with two variables, consider_all_windows and
11522 consider_all_frames. */
11523 consider_all_windows_p = 1;
11524 ++windows_or_buffers_changed;
11525 ++update_mode_lines;
11526
11527 /* If window configuration was changed, frames may have been
11528 marked garbaged. Clear them or we will experience
11529 surprises wrt scrolling. */
11530 if (frame_garbaged)
11531 clear_garbaged_frames ();
11532 }
11533
11534
11535 /* If showing the region, and mark has changed, we must redisplay
11536 the whole window. The assignment to this_line_start_pos prevents
11537 the optimization directly below this if-statement. */
11538 if (((!NILP (Vtransient_mark_mode)
11539 && !NILP (XBUFFER (w->buffer)->mark_active))
11540 != !NILP (w->region_showing))
11541 || (!NILP (w->region_showing)
11542 && !EQ (w->region_showing,
11543 Fmarker_position (XBUFFER (w->buffer)->mark))))
11544 CHARPOS (this_line_start_pos) = 0;
11545
11546 /* Optimize the case that only the line containing the cursor in the
11547 selected window has changed. Variables starting with this_ are
11548 set in display_line and record information about the line
11549 containing the cursor. */
11550 tlbufpos = this_line_start_pos;
11551 tlendpos = this_line_end_pos;
11552 if (!consider_all_windows_p
11553 && CHARPOS (tlbufpos) > 0
11554 && NILP (w->update_mode_line)
11555 && !current_buffer->clip_changed
11556 && !current_buffer->prevent_redisplay_optimizations_p
11557 && FRAME_VISIBLE_P (XFRAME (w->frame))
11558 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11559 /* Make sure recorded data applies to current buffer, etc. */
11560 && this_line_buffer == current_buffer
11561 && current_buffer == XBUFFER (w->buffer)
11562 && NILP (w->force_start)
11563 && NILP (w->optional_new_start)
11564 /* Point must be on the line that we have info recorded about. */
11565 && PT >= CHARPOS (tlbufpos)
11566 && PT <= Z - CHARPOS (tlendpos)
11567 /* All text outside that line, including its final newline,
11568 must be unchanged. */
11569 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11570 CHARPOS (tlendpos)))
11571 {
11572 if (CHARPOS (tlbufpos) > BEGV
11573 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11574 && (CHARPOS (tlbufpos) == ZV
11575 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11576 /* Former continuation line has disappeared by becoming empty. */
11577 goto cancel;
11578 else if (XFASTINT (w->last_modified) < MODIFF
11579 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11580 || MINI_WINDOW_P (w))
11581 {
11582 /* We have to handle the case of continuation around a
11583 wide-column character (see the comment in indent.c around
11584 line 1340).
11585
11586 For instance, in the following case:
11587
11588 -------- Insert --------
11589 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11590 J_I_ ==> J_I_ `^^' are cursors.
11591 ^^ ^^
11592 -------- --------
11593
11594 As we have to redraw the line above, we cannot use this
11595 optimization. */
11596
11597 struct it it;
11598 int line_height_before = this_line_pixel_height;
11599
11600 /* Note that start_display will handle the case that the
11601 line starting at tlbufpos is a continuation line. */
11602 start_display (&it, w, tlbufpos);
11603
11604 /* Implementation note: It this still necessary? */
11605 if (it.current_x != this_line_start_x)
11606 goto cancel;
11607
11608 TRACE ((stderr, "trying display optimization 1\n"));
11609 w->cursor.vpos = -1;
11610 overlay_arrow_seen = 0;
11611 it.vpos = this_line_vpos;
11612 it.current_y = this_line_y;
11613 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11614 display_line (&it);
11615
11616 /* If line contains point, is not continued,
11617 and ends at same distance from eob as before, we win. */
11618 if (w->cursor.vpos >= 0
11619 /* Line is not continued, otherwise this_line_start_pos
11620 would have been set to 0 in display_line. */
11621 && CHARPOS (this_line_start_pos)
11622 /* Line ends as before. */
11623 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11624 /* Line has same height as before. Otherwise other lines
11625 would have to be shifted up or down. */
11626 && this_line_pixel_height == line_height_before)
11627 {
11628 /* If this is not the window's last line, we must adjust
11629 the charstarts of the lines below. */
11630 if (it.current_y < it.last_visible_y)
11631 {
11632 struct glyph_row *row
11633 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11634 int delta, delta_bytes;
11635
11636 /* We used to distinguish between two cases here,
11637 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11638 when the line ends in a newline or the end of the
11639 buffer's accessible portion. But both cases did
11640 the same, so they were collapsed. */
11641 delta = (Z
11642 - CHARPOS (tlendpos)
11643 - MATRIX_ROW_START_CHARPOS (row));
11644 delta_bytes = (Z_BYTE
11645 - BYTEPOS (tlendpos)
11646 - MATRIX_ROW_START_BYTEPOS (row));
11647
11648 increment_matrix_positions (w->current_matrix,
11649 this_line_vpos + 1,
11650 w->current_matrix->nrows,
11651 delta, delta_bytes);
11652 }
11653
11654 /* If this row displays text now but previously didn't,
11655 or vice versa, w->window_end_vpos may have to be
11656 adjusted. */
11657 if ((it.glyph_row - 1)->displays_text_p)
11658 {
11659 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11660 XSETINT (w->window_end_vpos, this_line_vpos);
11661 }
11662 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11663 && this_line_vpos > 0)
11664 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11665 w->window_end_valid = Qnil;
11666
11667 /* Update hint: No need to try to scroll in update_window. */
11668 w->desired_matrix->no_scrolling_p = 1;
11669
11670 #if GLYPH_DEBUG
11671 *w->desired_matrix->method = 0;
11672 debug_method_add (w, "optimization 1");
11673 #endif
11674 #ifdef HAVE_WINDOW_SYSTEM
11675 update_window_fringes (w, 0);
11676 #endif
11677 goto update;
11678 }
11679 else
11680 goto cancel;
11681 }
11682 else if (/* Cursor position hasn't changed. */
11683 PT == XFASTINT (w->last_point)
11684 /* Make sure the cursor was last displayed
11685 in this window. Otherwise we have to reposition it. */
11686 && 0 <= w->cursor.vpos
11687 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11688 {
11689 if (!must_finish)
11690 {
11691 do_pending_window_change (1);
11692 /* If selected_window changed, redisplay again. */
11693 if (WINDOWP (selected_window)
11694 && (w = XWINDOW (selected_window)) != sw)
11695 goto retry;
11696
11697 /* We used to always goto end_of_redisplay here, but this
11698 isn't enough if we have a blinking cursor. */
11699 if (w->cursor_off_p == w->last_cursor_off_p)
11700 goto end_of_redisplay;
11701 }
11702 goto update;
11703 }
11704 /* If highlighting the region, or if the cursor is in the echo area,
11705 then we can't just move the cursor. */
11706 else if (! (!NILP (Vtransient_mark_mode)
11707 && !NILP (current_buffer->mark_active))
11708 && (EQ (selected_window, current_buffer->last_selected_window)
11709 || highlight_nonselected_windows)
11710 && NILP (w->region_showing)
11711 && NILP (Vshow_trailing_whitespace)
11712 && !cursor_in_echo_area)
11713 {
11714 struct it it;
11715 struct glyph_row *row;
11716
11717 /* Skip from tlbufpos to PT and see where it is. Note that
11718 PT may be in invisible text. If so, we will end at the
11719 next visible position. */
11720 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11721 NULL, DEFAULT_FACE_ID);
11722 it.current_x = this_line_start_x;
11723 it.current_y = this_line_y;
11724 it.vpos = this_line_vpos;
11725
11726 /* The call to move_it_to stops in front of PT, but
11727 moves over before-strings. */
11728 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11729
11730 if (it.vpos == this_line_vpos
11731 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11732 row->enabled_p))
11733 {
11734 xassert (this_line_vpos == it.vpos);
11735 xassert (this_line_y == it.current_y);
11736 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11737 #if GLYPH_DEBUG
11738 *w->desired_matrix->method = 0;
11739 debug_method_add (w, "optimization 3");
11740 #endif
11741 goto update;
11742 }
11743 else
11744 goto cancel;
11745 }
11746
11747 cancel:
11748 /* Text changed drastically or point moved off of line. */
11749 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11750 }
11751
11752 CHARPOS (this_line_start_pos) = 0;
11753 consider_all_windows_p |= buffer_shared > 1;
11754 ++clear_face_cache_count;
11755 #ifdef HAVE_WINDOW_SYSTEM
11756 ++clear_image_cache_count;
11757 #endif
11758
11759 /* Build desired matrices, and update the display. If
11760 consider_all_windows_p is non-zero, do it for all windows on all
11761 frames. Otherwise do it for selected_window, only. */
11762
11763 if (consider_all_windows_p)
11764 {
11765 Lisp_Object tail, frame;
11766
11767 FOR_EACH_FRAME (tail, frame)
11768 XFRAME (frame)->updated_p = 0;
11769
11770 /* Recompute # windows showing selected buffer. This will be
11771 incremented each time such a window is displayed. */
11772 buffer_shared = 0;
11773
11774 FOR_EACH_FRAME (tail, frame)
11775 {
11776 struct frame *f = XFRAME (frame);
11777
11778 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11779 {
11780 if (! EQ (frame, selected_frame))
11781 /* Select the frame, for the sake of frame-local
11782 variables. */
11783 select_frame_for_redisplay (frame);
11784
11785 /* Mark all the scroll bars to be removed; we'll redeem
11786 the ones we want when we redisplay their windows. */
11787 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11788 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11789
11790 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11791 redisplay_windows (FRAME_ROOT_WINDOW (f));
11792
11793 /* The X error handler may have deleted that frame. */
11794 if (!FRAME_LIVE_P (f))
11795 continue;
11796
11797 /* Any scroll bars which redisplay_windows should have
11798 nuked should now go away. */
11799 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11800 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11801
11802 /* If fonts changed, display again. */
11803 /* ??? rms: I suspect it is a mistake to jump all the way
11804 back to retry here. It should just retry this frame. */
11805 if (fonts_changed_p)
11806 goto retry;
11807
11808 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11809 {
11810 /* See if we have to hscroll. */
11811 if (!f->already_hscrolled_p)
11812 {
11813 f->already_hscrolled_p = 1;
11814 if (hscroll_windows (f->root_window))
11815 goto retry;
11816 }
11817
11818 /* Prevent various kinds of signals during display
11819 update. stdio is not robust about handling
11820 signals, which can cause an apparent I/O
11821 error. */
11822 if (interrupt_input)
11823 unrequest_sigio ();
11824 STOP_POLLING;
11825
11826 /* Update the display. */
11827 set_window_update_flags (XWINDOW (f->root_window), 1);
11828 pause |= update_frame (f, 0, 0);
11829 f->updated_p = 1;
11830 }
11831 }
11832 }
11833
11834 if (!EQ (old_frame, selected_frame)
11835 && FRAME_LIVE_P (XFRAME (old_frame)))
11836 /* We played a bit fast-and-loose above and allowed selected_frame
11837 and selected_window to be temporarily out-of-sync but let's make
11838 sure this stays contained. */
11839 select_frame_for_redisplay (old_frame);
11840 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11841
11842 if (!pause)
11843 {
11844 /* Do the mark_window_display_accurate after all windows have
11845 been redisplayed because this call resets flags in buffers
11846 which are needed for proper redisplay. */
11847 FOR_EACH_FRAME (tail, frame)
11848 {
11849 struct frame *f = XFRAME (frame);
11850 if (f->updated_p)
11851 {
11852 mark_window_display_accurate (f->root_window, 1);
11853 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11854 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11855 }
11856 }
11857 }
11858 }
11859 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11860 {
11861 Lisp_Object mini_window;
11862 struct frame *mini_frame;
11863
11864 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11865 /* Use list_of_error, not Qerror, so that
11866 we catch only errors and don't run the debugger. */
11867 internal_condition_case_1 (redisplay_window_1, selected_window,
11868 list_of_error,
11869 redisplay_window_error);
11870
11871 /* Compare desired and current matrices, perform output. */
11872
11873 update:
11874 /* If fonts changed, display again. */
11875 if (fonts_changed_p)
11876 goto retry;
11877
11878 /* Prevent various kinds of signals during display update.
11879 stdio is not robust about handling signals,
11880 which can cause an apparent I/O error. */
11881 if (interrupt_input)
11882 unrequest_sigio ();
11883 STOP_POLLING;
11884
11885 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11886 {
11887 if (hscroll_windows (selected_window))
11888 goto retry;
11889
11890 XWINDOW (selected_window)->must_be_updated_p = 1;
11891 pause = update_frame (sf, 0, 0);
11892 }
11893
11894 /* We may have called echo_area_display at the top of this
11895 function. If the echo area is on another frame, that may
11896 have put text on a frame other than the selected one, so the
11897 above call to update_frame would not have caught it. Catch
11898 it here. */
11899 mini_window = FRAME_MINIBUF_WINDOW (sf);
11900 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11901
11902 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11903 {
11904 XWINDOW (mini_window)->must_be_updated_p = 1;
11905 pause |= update_frame (mini_frame, 0, 0);
11906 if (!pause && hscroll_windows (mini_window))
11907 goto retry;
11908 }
11909 }
11910
11911 /* If display was paused because of pending input, make sure we do a
11912 thorough update the next time. */
11913 if (pause)
11914 {
11915 /* Prevent the optimization at the beginning of
11916 redisplay_internal that tries a single-line update of the
11917 line containing the cursor in the selected window. */
11918 CHARPOS (this_line_start_pos) = 0;
11919
11920 /* Let the overlay arrow be updated the next time. */
11921 update_overlay_arrows (0);
11922
11923 /* If we pause after scrolling, some rows in the current
11924 matrices of some windows are not valid. */
11925 if (!WINDOW_FULL_WIDTH_P (w)
11926 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11927 update_mode_lines = 1;
11928 }
11929 else
11930 {
11931 if (!consider_all_windows_p)
11932 {
11933 /* This has already been done above if
11934 consider_all_windows_p is set. */
11935 mark_window_display_accurate_1 (w, 1);
11936
11937 /* Say overlay arrows are up to date. */
11938 update_overlay_arrows (1);
11939
11940 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11941 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11942 }
11943
11944 update_mode_lines = 0;
11945 windows_or_buffers_changed = 0;
11946 cursor_type_changed = 0;
11947 }
11948
11949 /* Start SIGIO interrupts coming again. Having them off during the
11950 code above makes it less likely one will discard output, but not
11951 impossible, since there might be stuff in the system buffer here.
11952 But it is much hairier to try to do anything about that. */
11953 if (interrupt_input)
11954 request_sigio ();
11955 RESUME_POLLING;
11956
11957 /* If a frame has become visible which was not before, redisplay
11958 again, so that we display it. Expose events for such a frame
11959 (which it gets when becoming visible) don't call the parts of
11960 redisplay constructing glyphs, so simply exposing a frame won't
11961 display anything in this case. So, we have to display these
11962 frames here explicitly. */
11963 if (!pause)
11964 {
11965 Lisp_Object tail, frame;
11966 int new_count = 0;
11967
11968 FOR_EACH_FRAME (tail, frame)
11969 {
11970 int this_is_visible = 0;
11971
11972 if (XFRAME (frame)->visible)
11973 this_is_visible = 1;
11974 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11975 if (XFRAME (frame)->visible)
11976 this_is_visible = 1;
11977
11978 if (this_is_visible)
11979 new_count++;
11980 }
11981
11982 if (new_count != number_of_visible_frames)
11983 windows_or_buffers_changed++;
11984 }
11985
11986 /* Change frame size now if a change is pending. */
11987 do_pending_window_change (1);
11988
11989 /* If we just did a pending size change, or have additional
11990 visible frames, or selected_window changed, redisplay again. */
11991 if ((windows_or_buffers_changed && !pause)
11992 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
11993 goto retry;
11994
11995 /* Clear the face cache eventually. */
11996 if (consider_all_windows_p)
11997 {
11998 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11999 {
12000 clear_face_cache (0);
12001 clear_face_cache_count = 0;
12002 }
12003 #ifdef HAVE_WINDOW_SYSTEM
12004 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12005 {
12006 clear_image_caches (Qnil);
12007 clear_image_cache_count = 0;
12008 }
12009 #endif /* HAVE_WINDOW_SYSTEM */
12010 }
12011
12012 end_of_redisplay:
12013 unbind_to (count, Qnil);
12014 RESUME_POLLING;
12015 }
12016
12017
12018 /* Redisplay, but leave alone any recent echo area message unless
12019 another message has been requested in its place.
12020
12021 This is useful in situations where you need to redisplay but no
12022 user action has occurred, making it inappropriate for the message
12023 area to be cleared. See tracking_off and
12024 wait_reading_process_output for examples of these situations.
12025
12026 FROM_WHERE is an integer saying from where this function was
12027 called. This is useful for debugging. */
12028
12029 void
12030 redisplay_preserve_echo_area (from_where)
12031 int from_where;
12032 {
12033 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12034
12035 if (!NILP (echo_area_buffer[1]))
12036 {
12037 /* We have a previously displayed message, but no current
12038 message. Redisplay the previous message. */
12039 display_last_displayed_message_p = 1;
12040 redisplay_internal (1);
12041 display_last_displayed_message_p = 0;
12042 }
12043 else
12044 redisplay_internal (1);
12045
12046 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12047 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12048 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12049 }
12050
12051
12052 /* Function registered with record_unwind_protect in
12053 redisplay_internal. Reset redisplaying_p to the value it had
12054 before redisplay_internal was called, and clear
12055 prevent_freeing_realized_faces_p. It also selects the previously
12056 selected frame, unless it has been deleted (by an X connection
12057 failure during redisplay, for example). */
12058
12059 static Lisp_Object
12060 unwind_redisplay (val)
12061 Lisp_Object val;
12062 {
12063 Lisp_Object old_redisplaying_p, old_frame;
12064
12065 old_redisplaying_p = XCAR (val);
12066 redisplaying_p = XFASTINT (old_redisplaying_p);
12067 old_frame = XCDR (val);
12068 if (! EQ (old_frame, selected_frame)
12069 && FRAME_LIVE_P (XFRAME (old_frame)))
12070 select_frame_for_redisplay (old_frame);
12071 return Qnil;
12072 }
12073
12074
12075 /* Mark the display of window W as accurate or inaccurate. If
12076 ACCURATE_P is non-zero mark display of W as accurate. If
12077 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12078 redisplay_internal is called. */
12079
12080 static void
12081 mark_window_display_accurate_1 (w, accurate_p)
12082 struct window *w;
12083 int accurate_p;
12084 {
12085 if (BUFFERP (w->buffer))
12086 {
12087 struct buffer *b = XBUFFER (w->buffer);
12088
12089 w->last_modified
12090 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12091 w->last_overlay_modified
12092 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12093 w->last_had_star
12094 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12095
12096 if (accurate_p)
12097 {
12098 b->clip_changed = 0;
12099 b->prevent_redisplay_optimizations_p = 0;
12100
12101 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12102 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12103 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12104 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12105
12106 w->current_matrix->buffer = b;
12107 w->current_matrix->begv = BUF_BEGV (b);
12108 w->current_matrix->zv = BUF_ZV (b);
12109
12110 w->last_cursor = w->cursor;
12111 w->last_cursor_off_p = w->cursor_off_p;
12112
12113 if (w == XWINDOW (selected_window))
12114 w->last_point = make_number (BUF_PT (b));
12115 else
12116 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12117 }
12118 }
12119
12120 if (accurate_p)
12121 {
12122 w->window_end_valid = w->buffer;
12123 w->update_mode_line = Qnil;
12124 }
12125 }
12126
12127
12128 /* Mark the display of windows in the window tree rooted at WINDOW as
12129 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12130 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12131 be redisplayed the next time redisplay_internal is called. */
12132
12133 void
12134 mark_window_display_accurate (window, accurate_p)
12135 Lisp_Object window;
12136 int accurate_p;
12137 {
12138 struct window *w;
12139
12140 for (; !NILP (window); window = w->next)
12141 {
12142 w = XWINDOW (window);
12143 mark_window_display_accurate_1 (w, accurate_p);
12144
12145 if (!NILP (w->vchild))
12146 mark_window_display_accurate (w->vchild, accurate_p);
12147 if (!NILP (w->hchild))
12148 mark_window_display_accurate (w->hchild, accurate_p);
12149 }
12150
12151 if (accurate_p)
12152 {
12153 update_overlay_arrows (1);
12154 }
12155 else
12156 {
12157 /* Force a thorough redisplay the next time by setting
12158 last_arrow_position and last_arrow_string to t, which is
12159 unequal to any useful value of Voverlay_arrow_... */
12160 update_overlay_arrows (-1);
12161 }
12162 }
12163
12164
12165 /* Return value in display table DP (Lisp_Char_Table *) for character
12166 C. Since a display table doesn't have any parent, we don't have to
12167 follow parent. Do not call this function directly but use the
12168 macro DISP_CHAR_VECTOR. */
12169
12170 Lisp_Object
12171 disp_char_vector (dp, c)
12172 struct Lisp_Char_Table *dp;
12173 int c;
12174 {
12175 Lisp_Object val;
12176
12177 if (ASCII_CHAR_P (c))
12178 {
12179 val = dp->ascii;
12180 if (SUB_CHAR_TABLE_P (val))
12181 val = XSUB_CHAR_TABLE (val)->contents[c];
12182 }
12183 else
12184 {
12185 Lisp_Object table;
12186
12187 XSETCHAR_TABLE (table, dp);
12188 val = char_table_ref (table, c);
12189 }
12190 if (NILP (val))
12191 val = dp->defalt;
12192 return val;
12193 }
12194
12195
12196 \f
12197 /***********************************************************************
12198 Window Redisplay
12199 ***********************************************************************/
12200
12201 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12202
12203 static void
12204 redisplay_windows (window)
12205 Lisp_Object window;
12206 {
12207 while (!NILP (window))
12208 {
12209 struct window *w = XWINDOW (window);
12210
12211 if (!NILP (w->hchild))
12212 redisplay_windows (w->hchild);
12213 else if (!NILP (w->vchild))
12214 redisplay_windows (w->vchild);
12215 else if (!NILP (w->buffer))
12216 {
12217 displayed_buffer = XBUFFER (w->buffer);
12218 /* Use list_of_error, not Qerror, so that
12219 we catch only errors and don't run the debugger. */
12220 internal_condition_case_1 (redisplay_window_0, window,
12221 list_of_error,
12222 redisplay_window_error);
12223 }
12224
12225 window = w->next;
12226 }
12227 }
12228
12229 static Lisp_Object
12230 redisplay_window_error ()
12231 {
12232 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12233 return Qnil;
12234 }
12235
12236 static Lisp_Object
12237 redisplay_window_0 (window)
12238 Lisp_Object window;
12239 {
12240 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12241 redisplay_window (window, 0);
12242 return Qnil;
12243 }
12244
12245 static Lisp_Object
12246 redisplay_window_1 (window)
12247 Lisp_Object window;
12248 {
12249 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12250 redisplay_window (window, 1);
12251 return Qnil;
12252 }
12253 \f
12254
12255 /* Increment GLYPH until it reaches END or CONDITION fails while
12256 adding (GLYPH)->pixel_width to X. */
12257
12258 #define SKIP_GLYPHS(glyph, end, x, condition) \
12259 do \
12260 { \
12261 (x) += (glyph)->pixel_width; \
12262 ++(glyph); \
12263 } \
12264 while ((glyph) < (end) && (condition))
12265
12266
12267 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12268 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12269 which positions recorded in ROW differ from current buffer
12270 positions.
12271
12272 Return 0 if cursor is not on this row, 1 otherwise. */
12273
12274 int
12275 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12276 struct window *w;
12277 struct glyph_row *row;
12278 struct glyph_matrix *matrix;
12279 int delta, delta_bytes, dy, dvpos;
12280 {
12281 struct glyph *glyph = row->glyphs[TEXT_AREA];
12282 struct glyph *end = glyph + row->used[TEXT_AREA];
12283 struct glyph *cursor = NULL;
12284 /* The first glyph that starts a sequence of glyphs from a string
12285 that is a value of a display property. */
12286 struct glyph *string_start;
12287 /* The X coordinate of string_start. */
12288 int string_start_x;
12289 /* The last known character position in row. */
12290 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12291 /* The last known character position before string_start. */
12292 int string_before_pos;
12293 int x = row->x;
12294 int cursor_x = x;
12295 /* Last buffer position covered by an overlay. */
12296 int cursor_from_overlay_pos = 0;
12297 int pt_old = PT - delta;
12298
12299 /* Skip over glyphs not having an object at the start of the row.
12300 These are special glyphs like truncation marks on terminal
12301 frames. */
12302 if (row->displays_text_p)
12303 while (glyph < end
12304 && INTEGERP (glyph->object)
12305 && glyph->charpos < 0)
12306 {
12307 x += glyph->pixel_width;
12308 ++glyph;
12309 }
12310
12311 string_start = NULL;
12312 while (glyph < end
12313 && !INTEGERP (glyph->object)
12314 && (!BUFFERP (glyph->object)
12315 || (last_pos = glyph->charpos) < pt_old
12316 || glyph->avoid_cursor_p))
12317 {
12318 if (! STRINGP (glyph->object))
12319 {
12320 string_start = NULL;
12321 x += glyph->pixel_width;
12322 ++glyph;
12323 /* If we are beyond the cursor position computed from the
12324 last overlay seen, that overlay is not in effect for
12325 current cursor position. Reset the cursor information
12326 computed from that overlay. */
12327 if (cursor_from_overlay_pos
12328 && last_pos >= cursor_from_overlay_pos)
12329 {
12330 cursor_from_overlay_pos = 0;
12331 cursor = NULL;
12332 }
12333 }
12334 else
12335 {
12336 if (string_start == NULL)
12337 {
12338 string_before_pos = last_pos;
12339 string_start = glyph;
12340 string_start_x = x;
12341 }
12342 /* Skip all glyphs from a string. */
12343 do
12344 {
12345 Lisp_Object cprop;
12346 int pos;
12347 if ((cursor == NULL || glyph > cursor)
12348 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12349 Qcursor, (glyph)->object),
12350 !NILP (cprop))
12351 && (pos = string_buffer_position (w, glyph->object,
12352 string_before_pos),
12353 (pos == 0 /* from overlay */
12354 || pos == pt_old)))
12355 {
12356 /* Compute the first buffer position after the overlay.
12357 If the `cursor' property tells us how many positions
12358 are associated with the overlay, use that. Otherwise,
12359 estimate from the buffer positions of the glyphs
12360 before and after the overlay. */
12361 cursor_from_overlay_pos = (pos ? 0 : last_pos
12362 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12363 cursor = glyph;
12364 cursor_x = x;
12365 }
12366 x += glyph->pixel_width;
12367 ++glyph;
12368 }
12369 while (glyph < end && EQ (glyph->object, string_start->object));
12370 }
12371 }
12372
12373 if (cursor != NULL)
12374 {
12375 glyph = cursor;
12376 x = cursor_x;
12377 }
12378 else if (row->ends_in_ellipsis_p && glyph == end)
12379 {
12380 /* Scan back over the ellipsis glyphs, decrementing positions. */
12381 while (glyph > row->glyphs[TEXT_AREA]
12382 && (glyph - 1)->charpos == last_pos)
12383 glyph--, x -= glyph->pixel_width;
12384 /* That loop always goes one position too far, including the
12385 glyph before the ellipsis. So scan forward over that one. */
12386 x += glyph->pixel_width;
12387 glyph++;
12388 }
12389 else if (string_start
12390 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12391 {
12392 /* We may have skipped over point because the previous glyphs
12393 are from string. As there's no easy way to know the
12394 character position of the current glyph, find the correct
12395 glyph on point by scanning from string_start again. */
12396 Lisp_Object limit;
12397 Lisp_Object string;
12398 struct glyph *stop = glyph;
12399 int pos;
12400
12401 limit = make_number (pt_old + 1);
12402 glyph = string_start;
12403 x = string_start_x;
12404 string = glyph->object;
12405 pos = string_buffer_position (w, string, string_before_pos);
12406 /* If POS == 0, STRING is from overlay. We skip such glyphs
12407 because we always put the cursor after overlay strings. */
12408 while (pos == 0 && glyph < stop)
12409 {
12410 string = glyph->object;
12411 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12412 if (glyph < stop)
12413 pos = string_buffer_position (w, glyph->object, string_before_pos);
12414 }
12415
12416 while (glyph < stop)
12417 {
12418 pos = XINT (Fnext_single_char_property_change
12419 (make_number (pos), Qdisplay, Qnil, limit));
12420 if (pos > pt_old)
12421 break;
12422 /* Skip glyphs from the same string. */
12423 string = glyph->object;
12424 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12425 /* Skip glyphs from an overlay. */
12426 while (glyph < stop
12427 && ! string_buffer_position (w, glyph->object, pos))
12428 {
12429 string = glyph->object;
12430 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12431 }
12432 }
12433
12434 /* If we reached the end of the line, and END was from a string,
12435 the cursor is not on this line. */
12436 if (glyph == end && row->continued_p)
12437 return 0;
12438 }
12439
12440 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12441 w->cursor.x = x;
12442 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12443 w->cursor.y = row->y + dy;
12444
12445 if (w == XWINDOW (selected_window))
12446 {
12447 if (!row->continued_p
12448 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12449 && row->x == 0)
12450 {
12451 this_line_buffer = XBUFFER (w->buffer);
12452
12453 CHARPOS (this_line_start_pos)
12454 = MATRIX_ROW_START_CHARPOS (row) + delta;
12455 BYTEPOS (this_line_start_pos)
12456 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12457
12458 CHARPOS (this_line_end_pos)
12459 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12460 BYTEPOS (this_line_end_pos)
12461 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12462
12463 this_line_y = w->cursor.y;
12464 this_line_pixel_height = row->height;
12465 this_line_vpos = w->cursor.vpos;
12466 this_line_start_x = row->x;
12467 }
12468 else
12469 CHARPOS (this_line_start_pos) = 0;
12470 }
12471
12472 return 1;
12473 }
12474
12475
12476 /* Run window scroll functions, if any, for WINDOW with new window
12477 start STARTP. Sets the window start of WINDOW to that position.
12478
12479 We assume that the window's buffer is really current. */
12480
12481 static INLINE struct text_pos
12482 run_window_scroll_functions (window, startp)
12483 Lisp_Object window;
12484 struct text_pos startp;
12485 {
12486 struct window *w = XWINDOW (window);
12487 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12488
12489 if (current_buffer != XBUFFER (w->buffer))
12490 abort ();
12491
12492 if (!NILP (Vwindow_scroll_functions))
12493 {
12494 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12495 make_number (CHARPOS (startp)));
12496 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12497 /* In case the hook functions switch buffers. */
12498 if (current_buffer != XBUFFER (w->buffer))
12499 set_buffer_internal_1 (XBUFFER (w->buffer));
12500 }
12501
12502 return startp;
12503 }
12504
12505
12506 /* Make sure the line containing the cursor is fully visible.
12507 A value of 1 means there is nothing to be done.
12508 (Either the line is fully visible, or it cannot be made so,
12509 or we cannot tell.)
12510
12511 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12512 is higher than window.
12513
12514 A value of 0 means the caller should do scrolling
12515 as if point had gone off the screen. */
12516
12517 static int
12518 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12519 struct window *w;
12520 int force_p;
12521 int current_matrix_p;
12522 {
12523 struct glyph_matrix *matrix;
12524 struct glyph_row *row;
12525 int window_height;
12526
12527 if (!make_cursor_line_fully_visible_p)
12528 return 1;
12529
12530 /* It's not always possible to find the cursor, e.g, when a window
12531 is full of overlay strings. Don't do anything in that case. */
12532 if (w->cursor.vpos < 0)
12533 return 1;
12534
12535 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12536 row = MATRIX_ROW (matrix, w->cursor.vpos);
12537
12538 /* If the cursor row is not partially visible, there's nothing to do. */
12539 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12540 return 1;
12541
12542 /* If the row the cursor is in is taller than the window's height,
12543 it's not clear what to do, so do nothing. */
12544 window_height = window_box_height (w);
12545 if (row->height >= window_height)
12546 {
12547 if (!force_p || MINI_WINDOW_P (w)
12548 || w->vscroll || w->cursor.vpos == 0)
12549 return 1;
12550 }
12551 return 0;
12552 }
12553
12554
12555 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12556 non-zero means only WINDOW is redisplayed in redisplay_internal.
12557 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12558 in redisplay_window to bring a partially visible line into view in
12559 the case that only the cursor has moved.
12560
12561 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12562 last screen line's vertical height extends past the end of the screen.
12563
12564 Value is
12565
12566 1 if scrolling succeeded
12567
12568 0 if scrolling didn't find point.
12569
12570 -1 if new fonts have been loaded so that we must interrupt
12571 redisplay, adjust glyph matrices, and try again. */
12572
12573 enum
12574 {
12575 SCROLLING_SUCCESS,
12576 SCROLLING_FAILED,
12577 SCROLLING_NEED_LARGER_MATRICES
12578 };
12579
12580 static int
12581 try_scrolling (window, just_this_one_p, scroll_conservatively,
12582 scroll_step, temp_scroll_step, last_line_misfit)
12583 Lisp_Object window;
12584 int just_this_one_p;
12585 EMACS_INT scroll_conservatively, scroll_step;
12586 int temp_scroll_step;
12587 int last_line_misfit;
12588 {
12589 struct window *w = XWINDOW (window);
12590 struct frame *f = XFRAME (w->frame);
12591 struct text_pos pos, startp;
12592 struct it it;
12593 int this_scroll_margin, scroll_max, rc, height;
12594 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12595 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12596 Lisp_Object aggressive;
12597 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12598
12599 #if GLYPH_DEBUG
12600 debug_method_add (w, "try_scrolling");
12601 #endif
12602
12603 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12604
12605 /* Compute scroll margin height in pixels. We scroll when point is
12606 within this distance from the top or bottom of the window. */
12607 if (scroll_margin > 0)
12608 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12609 * FRAME_LINE_HEIGHT (f);
12610 else
12611 this_scroll_margin = 0;
12612
12613 /* Force scroll_conservatively to have a reasonable value, to avoid
12614 overflow while computing how much to scroll. Note that the user
12615 can supply scroll-conservatively equal to `most-positive-fixnum',
12616 which can be larger than INT_MAX. */
12617 if (scroll_conservatively > scroll_limit)
12618 {
12619 scroll_conservatively = scroll_limit;
12620 scroll_max = INT_MAX;
12621 }
12622 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12623 /* Compute how much we should try to scroll maximally to bring
12624 point into view. */
12625 scroll_max = (max (scroll_step,
12626 max (scroll_conservatively, temp_scroll_step))
12627 * FRAME_LINE_HEIGHT (f));
12628 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12629 || NUMBERP (current_buffer->scroll_up_aggressively))
12630 /* We're trying to scroll because of aggressive scrolling but no
12631 scroll_step is set. Choose an arbitrary one. */
12632 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12633 else
12634 scroll_max = 0;
12635
12636 too_near_end:
12637
12638 /* Decide whether to scroll down. */
12639 if (PT > CHARPOS (startp))
12640 {
12641 int scroll_margin_y;
12642
12643 /* Compute the pixel ypos of the scroll margin, then move it to
12644 either that ypos or PT, whichever comes first. */
12645 start_display (&it, w, startp);
12646 scroll_margin_y = it.last_visible_y - this_scroll_margin
12647 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12648 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12649 (MOVE_TO_POS | MOVE_TO_Y));
12650
12651 if (PT > CHARPOS (it.current.pos))
12652 {
12653 int y0 = line_bottom_y (&it);
12654
12655 /* Compute the distance from the scroll margin to PT
12656 (including the height of the cursor line). Moving the
12657 iterator unconditionally to PT can be slow if PT is far
12658 away, so stop 10 lines past the window bottom (is there a
12659 way to do the right thing quickly?). */
12660 move_it_to (&it, PT, -1,
12661 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12662 -1, MOVE_TO_POS | MOVE_TO_Y);
12663 dy = line_bottom_y (&it) - y0;
12664
12665 if (dy > scroll_max)
12666 return SCROLLING_FAILED;
12667
12668 scroll_down_p = 1;
12669 }
12670 }
12671
12672 if (scroll_down_p)
12673 {
12674 /* Point is in or below the bottom scroll margin, so move the
12675 window start down. If scrolling conservatively, move it just
12676 enough down to make point visible. If scroll_step is set,
12677 move it down by scroll_step. */
12678 if (scroll_conservatively)
12679 amount_to_scroll
12680 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12681 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12682 else if (scroll_step || temp_scroll_step)
12683 amount_to_scroll = scroll_max;
12684 else
12685 {
12686 aggressive = current_buffer->scroll_up_aggressively;
12687 height = WINDOW_BOX_TEXT_HEIGHT (w);
12688 if (NUMBERP (aggressive))
12689 {
12690 double float_amount = XFLOATINT (aggressive) * height;
12691 amount_to_scroll = float_amount;
12692 if (amount_to_scroll == 0 && float_amount > 0)
12693 amount_to_scroll = 1;
12694 }
12695 }
12696
12697 if (amount_to_scroll <= 0)
12698 return SCROLLING_FAILED;
12699
12700 start_display (&it, w, startp);
12701 move_it_vertically (&it, amount_to_scroll);
12702
12703 /* If STARTP is unchanged, move it down another screen line. */
12704 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12705 move_it_by_lines (&it, 1, 1);
12706 startp = it.current.pos;
12707 }
12708 else
12709 {
12710 struct text_pos scroll_margin_pos = startp;
12711
12712 /* See if point is inside the scroll margin at the top of the
12713 window. */
12714 if (this_scroll_margin)
12715 {
12716 start_display (&it, w, startp);
12717 move_it_vertically (&it, this_scroll_margin);
12718 scroll_margin_pos = it.current.pos;
12719 }
12720
12721 if (PT < CHARPOS (scroll_margin_pos))
12722 {
12723 /* Point is in the scroll margin at the top of the window or
12724 above what is displayed in the window. */
12725 int y0;
12726
12727 /* Compute the vertical distance from PT to the scroll
12728 margin position. Give up if distance is greater than
12729 scroll_max. */
12730 SET_TEXT_POS (pos, PT, PT_BYTE);
12731 start_display (&it, w, pos);
12732 y0 = it.current_y;
12733 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12734 it.last_visible_y, -1,
12735 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12736 dy = it.current_y - y0;
12737 if (dy > scroll_max)
12738 return SCROLLING_FAILED;
12739
12740 /* Compute new window start. */
12741 start_display (&it, w, startp);
12742
12743 if (scroll_conservatively)
12744 amount_to_scroll
12745 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12746 else if (scroll_step || temp_scroll_step)
12747 amount_to_scroll = scroll_max;
12748 else
12749 {
12750 aggressive = current_buffer->scroll_down_aggressively;
12751 height = WINDOW_BOX_TEXT_HEIGHT (w);
12752 if (NUMBERP (aggressive))
12753 {
12754 double float_amount = XFLOATINT (aggressive) * height;
12755 amount_to_scroll = float_amount;
12756 if (amount_to_scroll == 0 && float_amount > 0)
12757 amount_to_scroll = 1;
12758 }
12759 }
12760
12761 if (amount_to_scroll <= 0)
12762 return SCROLLING_FAILED;
12763
12764 move_it_vertically_backward (&it, amount_to_scroll);
12765 startp = it.current.pos;
12766 }
12767 }
12768
12769 /* Run window scroll functions. */
12770 startp = run_window_scroll_functions (window, startp);
12771
12772 /* Display the window. Give up if new fonts are loaded, or if point
12773 doesn't appear. */
12774 if (!try_window (window, startp, 0))
12775 rc = SCROLLING_NEED_LARGER_MATRICES;
12776 else if (w->cursor.vpos < 0)
12777 {
12778 clear_glyph_matrix (w->desired_matrix);
12779 rc = SCROLLING_FAILED;
12780 }
12781 else
12782 {
12783 /* Maybe forget recorded base line for line number display. */
12784 if (!just_this_one_p
12785 || current_buffer->clip_changed
12786 || BEG_UNCHANGED < CHARPOS (startp))
12787 w->base_line_number = Qnil;
12788
12789 /* If cursor ends up on a partially visible line,
12790 treat that as being off the bottom of the screen. */
12791 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
12792 /* It's possible that the cursor is on the first line of the
12793 buffer, which is partially obscured due to a vscroll
12794 (Bug#7537). In that case, avoid looping forever . */
12795 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
12796 {
12797 clear_glyph_matrix (w->desired_matrix);
12798 ++extra_scroll_margin_lines;
12799 goto too_near_end;
12800 }
12801 rc = SCROLLING_SUCCESS;
12802 }
12803
12804 return rc;
12805 }
12806
12807
12808 /* Compute a suitable window start for window W if display of W starts
12809 on a continuation line. Value is non-zero if a new window start
12810 was computed.
12811
12812 The new window start will be computed, based on W's width, starting
12813 from the start of the continued line. It is the start of the
12814 screen line with the minimum distance from the old start W->start. */
12815
12816 static int
12817 compute_window_start_on_continuation_line (w)
12818 struct window *w;
12819 {
12820 struct text_pos pos, start_pos;
12821 int window_start_changed_p = 0;
12822
12823 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12824
12825 /* If window start is on a continuation line... Window start may be
12826 < BEGV in case there's invisible text at the start of the
12827 buffer (M-x rmail, for example). */
12828 if (CHARPOS (start_pos) > BEGV
12829 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12830 {
12831 struct it it;
12832 struct glyph_row *row;
12833
12834 /* Handle the case that the window start is out of range. */
12835 if (CHARPOS (start_pos) < BEGV)
12836 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12837 else if (CHARPOS (start_pos) > ZV)
12838 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12839
12840 /* Find the start of the continued line. This should be fast
12841 because scan_buffer is fast (newline cache). */
12842 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12843 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12844 row, DEFAULT_FACE_ID);
12845 reseat_at_previous_visible_line_start (&it);
12846
12847 /* If the line start is "too far" away from the window start,
12848 say it takes too much time to compute a new window start. */
12849 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12850 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12851 {
12852 int min_distance, distance;
12853
12854 /* Move forward by display lines to find the new window
12855 start. If window width was enlarged, the new start can
12856 be expected to be > the old start. If window width was
12857 decreased, the new window start will be < the old start.
12858 So, we're looking for the display line start with the
12859 minimum distance from the old window start. */
12860 pos = it.current.pos;
12861 min_distance = INFINITY;
12862 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12863 distance < min_distance)
12864 {
12865 min_distance = distance;
12866 pos = it.current.pos;
12867 move_it_by_lines (&it, 1, 0);
12868 }
12869
12870 /* Set the window start there. */
12871 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12872 window_start_changed_p = 1;
12873 }
12874 }
12875
12876 return window_start_changed_p;
12877 }
12878
12879
12880 /* Try cursor movement in case text has not changed in window WINDOW,
12881 with window start STARTP. Value is
12882
12883 CURSOR_MOVEMENT_SUCCESS if successful
12884
12885 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12886
12887 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12888 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12889 we want to scroll as if scroll-step were set to 1. See the code.
12890
12891 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12892 which case we have to abort this redisplay, and adjust matrices
12893 first. */
12894
12895 enum
12896 {
12897 CURSOR_MOVEMENT_SUCCESS,
12898 CURSOR_MOVEMENT_CANNOT_BE_USED,
12899 CURSOR_MOVEMENT_MUST_SCROLL,
12900 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12901 };
12902
12903 static int
12904 try_cursor_movement (window, startp, scroll_step)
12905 Lisp_Object window;
12906 struct text_pos startp;
12907 int *scroll_step;
12908 {
12909 struct window *w = XWINDOW (window);
12910 struct frame *f = XFRAME (w->frame);
12911 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12912
12913 #if GLYPH_DEBUG
12914 if (inhibit_try_cursor_movement)
12915 return rc;
12916 #endif
12917
12918 /* Handle case where text has not changed, only point, and it has
12919 not moved off the frame. */
12920 if (/* Point may be in this window. */
12921 PT >= CHARPOS (startp)
12922 /* Selective display hasn't changed. */
12923 && !current_buffer->clip_changed
12924 /* Function force-mode-line-update is used to force a thorough
12925 redisplay. It sets either windows_or_buffers_changed or
12926 update_mode_lines. So don't take a shortcut here for these
12927 cases. */
12928 && !update_mode_lines
12929 && !windows_or_buffers_changed
12930 && !cursor_type_changed
12931 /* Can't use this case if highlighting a region. When a
12932 region exists, cursor movement has to do more than just
12933 set the cursor. */
12934 && !(!NILP (Vtransient_mark_mode)
12935 && !NILP (current_buffer->mark_active))
12936 && NILP (w->region_showing)
12937 && NILP (Vshow_trailing_whitespace)
12938 /* Right after splitting windows, last_point may be nil. */
12939 && INTEGERP (w->last_point)
12940 /* This code is not used for mini-buffer for the sake of the case
12941 of redisplaying to replace an echo area message; since in
12942 that case the mini-buffer contents per se are usually
12943 unchanged. This code is of no real use in the mini-buffer
12944 since the handling of this_line_start_pos, etc., in redisplay
12945 handles the same cases. */
12946 && !EQ (window, minibuf_window)
12947 /* When splitting windows or for new windows, it happens that
12948 redisplay is called with a nil window_end_vpos or one being
12949 larger than the window. This should really be fixed in
12950 window.c. I don't have this on my list, now, so we do
12951 approximately the same as the old redisplay code. --gerd. */
12952 && INTEGERP (w->window_end_vpos)
12953 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12954 && (FRAME_WINDOW_P (f)
12955 || !overlay_arrow_in_current_buffer_p ()))
12956 {
12957 int this_scroll_margin, top_scroll_margin;
12958 struct glyph_row *row = NULL;
12959
12960 #if GLYPH_DEBUG
12961 debug_method_add (w, "cursor movement");
12962 #endif
12963
12964 /* Scroll if point within this distance from the top or bottom
12965 of the window. This is a pixel value. */
12966 if (scroll_margin > 0)
12967 {
12968 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12969 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12970 }
12971 else
12972 this_scroll_margin = 0;
12973
12974 top_scroll_margin = this_scroll_margin;
12975 if (WINDOW_WANTS_HEADER_LINE_P (w))
12976 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12977
12978 /* Start with the row the cursor was displayed during the last
12979 not paused redisplay. Give up if that row is not valid. */
12980 if (w->last_cursor.vpos < 0
12981 || w->last_cursor.vpos >= w->current_matrix->nrows)
12982 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12983 else
12984 {
12985 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12986 if (row->mode_line_p)
12987 ++row;
12988 if (!row->enabled_p)
12989 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12990 }
12991
12992 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12993 {
12994 int scroll_p = 0;
12995 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12996
12997 if (PT > XFASTINT (w->last_point))
12998 {
12999 /* Point has moved forward. */
13000 while (MATRIX_ROW_END_CHARPOS (row) < PT
13001 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
13002 {
13003 xassert (row->enabled_p);
13004 ++row;
13005 }
13006
13007 /* The end position of a row equals the start position
13008 of the next row. If PT is there, we would rather
13009 display it in the next line. */
13010 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13011 && MATRIX_ROW_END_CHARPOS (row) == PT
13012 && !cursor_row_p (w, row))
13013 ++row;
13014
13015 /* If within the scroll margin, scroll. Note that
13016 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13017 the next line would be drawn, and that
13018 this_scroll_margin can be zero. */
13019 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13020 || PT > MATRIX_ROW_END_CHARPOS (row)
13021 /* Line is completely visible last line in window
13022 and PT is to be set in the next line. */
13023 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13024 && PT == MATRIX_ROW_END_CHARPOS (row)
13025 && !row->ends_at_zv_p
13026 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13027 scroll_p = 1;
13028 }
13029 else if (PT < XFASTINT (w->last_point))
13030 {
13031 /* Cursor has to be moved backward. Note that PT >=
13032 CHARPOS (startp) because of the outer if-statement. */
13033 while (!row->mode_line_p
13034 && (MATRIX_ROW_START_CHARPOS (row) > PT
13035 || (MATRIX_ROW_START_CHARPOS (row) == PT
13036 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13037 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13038 row > w->current_matrix->rows
13039 && (row-1)->ends_in_newline_from_string_p))))
13040 && (row->y > top_scroll_margin
13041 || CHARPOS (startp) == BEGV))
13042 {
13043 xassert (row->enabled_p);
13044 --row;
13045 }
13046
13047 /* Consider the following case: Window starts at BEGV,
13048 there is invisible, intangible text at BEGV, so that
13049 display starts at some point START > BEGV. It can
13050 happen that we are called with PT somewhere between
13051 BEGV and START. Try to handle that case. */
13052 if (row < w->current_matrix->rows
13053 || row->mode_line_p)
13054 {
13055 row = w->current_matrix->rows;
13056 if (row->mode_line_p)
13057 ++row;
13058 }
13059
13060 /* Due to newlines in overlay strings, we may have to
13061 skip forward over overlay strings. */
13062 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13063 && MATRIX_ROW_END_CHARPOS (row) == PT
13064 && !cursor_row_p (w, row))
13065 ++row;
13066
13067 /* If within the scroll margin, scroll. */
13068 if (row->y < top_scroll_margin
13069 && CHARPOS (startp) != BEGV)
13070 scroll_p = 1;
13071 }
13072 else
13073 {
13074 /* Cursor did not move. So don't scroll even if cursor line
13075 is partially visible, as it was so before. */
13076 rc = CURSOR_MOVEMENT_SUCCESS;
13077 }
13078
13079 if (PT < MATRIX_ROW_START_CHARPOS (row)
13080 || PT > MATRIX_ROW_END_CHARPOS (row))
13081 {
13082 /* if PT is not in the glyph row, give up. */
13083 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13084 }
13085 else if (rc != CURSOR_MOVEMENT_SUCCESS
13086 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13087 && make_cursor_line_fully_visible_p)
13088 {
13089 if (PT == MATRIX_ROW_END_CHARPOS (row)
13090 && !row->ends_at_zv_p
13091 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13092 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13093 else if (row->height > window_box_height (w))
13094 {
13095 /* If we end up in a partially visible line, let's
13096 make it fully visible, except when it's taller
13097 than the window, in which case we can't do much
13098 about it. */
13099 *scroll_step = 1;
13100 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13101 }
13102 else
13103 {
13104 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13105 if (!cursor_row_fully_visible_p (w, 0, 1))
13106 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13107 else
13108 rc = CURSOR_MOVEMENT_SUCCESS;
13109 }
13110 }
13111 else if (scroll_p)
13112 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13113 else
13114 {
13115 do
13116 {
13117 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13118 {
13119 rc = CURSOR_MOVEMENT_SUCCESS;
13120 break;
13121 }
13122 ++row;
13123 }
13124 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13125 && MATRIX_ROW_START_CHARPOS (row) == PT
13126 && cursor_row_p (w, row));
13127 }
13128 }
13129 }
13130
13131 return rc;
13132 }
13133
13134 void
13135 set_vertical_scroll_bar (w)
13136 struct window *w;
13137 {
13138 int start, end, whole;
13139
13140 /* Calculate the start and end positions for the current window.
13141 At some point, it would be nice to choose between scrollbars
13142 which reflect the whole buffer size, with special markers
13143 indicating narrowing, and scrollbars which reflect only the
13144 visible region.
13145
13146 Note that mini-buffers sometimes aren't displaying any text. */
13147 if (!MINI_WINDOW_P (w)
13148 || (w == XWINDOW (minibuf_window)
13149 && NILP (echo_area_buffer[0])))
13150 {
13151 struct buffer *buf = XBUFFER (w->buffer);
13152 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13153 start = marker_position (w->start) - BUF_BEGV (buf);
13154 /* I don't think this is guaranteed to be right. For the
13155 moment, we'll pretend it is. */
13156 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13157
13158 if (end < start)
13159 end = start;
13160 if (whole < (end - start))
13161 whole = end - start;
13162 }
13163 else
13164 start = end = whole = 0;
13165
13166 /* Indicate what this scroll bar ought to be displaying now. */
13167 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13168 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13169 (w, end - start, whole, start);
13170 }
13171
13172
13173 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13174 selected_window is redisplayed.
13175
13176 We can return without actually redisplaying the window if
13177 fonts_changed_p is nonzero. In that case, redisplay_internal will
13178 retry. */
13179
13180 static void
13181 redisplay_window (window, just_this_one_p)
13182 Lisp_Object window;
13183 int just_this_one_p;
13184 {
13185 struct window *w = XWINDOW (window);
13186 struct frame *f = XFRAME (w->frame);
13187 struct buffer *buffer = XBUFFER (w->buffer);
13188 struct buffer *old = current_buffer;
13189 struct text_pos lpoint, opoint, startp;
13190 int update_mode_line;
13191 int tem;
13192 struct it it;
13193 /* Record it now because it's overwritten. */
13194 int current_matrix_up_to_date_p = 0;
13195 int used_current_matrix_p = 0;
13196 /* This is less strict than current_matrix_up_to_date_p.
13197 It indictes that the buffer contents and narrowing are unchanged. */
13198 int buffer_unchanged_p = 0;
13199 int temp_scroll_step = 0;
13200 int count = SPECPDL_INDEX ();
13201 int rc;
13202 int centering_position = -1;
13203 int last_line_misfit = 0;
13204 int beg_unchanged, end_unchanged;
13205
13206 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13207 opoint = lpoint;
13208
13209 /* W must be a leaf window here. */
13210 xassert (!NILP (w->buffer));
13211 #if GLYPH_DEBUG
13212 *w->desired_matrix->method = 0;
13213 #endif
13214
13215 restart:
13216 reconsider_clip_changes (w, buffer);
13217
13218 /* Has the mode line to be updated? */
13219 update_mode_line = (!NILP (w->update_mode_line)
13220 || update_mode_lines
13221 || buffer->clip_changed
13222 || buffer->prevent_redisplay_optimizations_p);
13223
13224 if (MINI_WINDOW_P (w))
13225 {
13226 if (w == XWINDOW (echo_area_window)
13227 && !NILP (echo_area_buffer[0]))
13228 {
13229 if (update_mode_line)
13230 /* We may have to update a tty frame's menu bar or a
13231 tool-bar. Example `M-x C-h C-h C-g'. */
13232 goto finish_menu_bars;
13233 else
13234 /* We've already displayed the echo area glyphs in this window. */
13235 goto finish_scroll_bars;
13236 }
13237 else if ((w != XWINDOW (minibuf_window)
13238 || minibuf_level == 0)
13239 /* When buffer is nonempty, redisplay window normally. */
13240 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13241 /* Quail displays non-mini buffers in minibuffer window.
13242 In that case, redisplay the window normally. */
13243 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13244 {
13245 /* W is a mini-buffer window, but it's not active, so clear
13246 it. */
13247 int yb = window_text_bottom_y (w);
13248 struct glyph_row *row;
13249 int y;
13250
13251 for (y = 0, row = w->desired_matrix->rows;
13252 y < yb;
13253 y += row->height, ++row)
13254 blank_row (w, row, y);
13255 goto finish_scroll_bars;
13256 }
13257
13258 clear_glyph_matrix (w->desired_matrix);
13259 }
13260
13261 /* Otherwise set up data on this window; select its buffer and point
13262 value. */
13263 /* Really select the buffer, for the sake of buffer-local
13264 variables. */
13265 set_buffer_internal_1 (XBUFFER (w->buffer));
13266
13267 current_matrix_up_to_date_p
13268 = (!NILP (w->window_end_valid)
13269 && !current_buffer->clip_changed
13270 && !current_buffer->prevent_redisplay_optimizations_p
13271 && XFASTINT (w->last_modified) >= MODIFF
13272 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13273
13274 /* Run the window-bottom-change-functions
13275 if it is possible that the text on the screen has changed
13276 (either due to modification of the text, or any other reason). */
13277 if (!current_matrix_up_to_date_p
13278 && !NILP (Vwindow_text_change_functions))
13279 {
13280 safe_run_hooks (Qwindow_text_change_functions);
13281 goto restart;
13282 }
13283
13284 beg_unchanged = BEG_UNCHANGED;
13285 end_unchanged = END_UNCHANGED;
13286
13287 SET_TEXT_POS (opoint, PT, PT_BYTE);
13288
13289 specbind (Qinhibit_point_motion_hooks, Qt);
13290
13291 buffer_unchanged_p
13292 = (!NILP (w->window_end_valid)
13293 && !current_buffer->clip_changed
13294 && XFASTINT (w->last_modified) >= MODIFF
13295 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13296
13297 /* When windows_or_buffers_changed is non-zero, we can't rely on
13298 the window end being valid, so set it to nil there. */
13299 if (windows_or_buffers_changed)
13300 {
13301 /* If window starts on a continuation line, maybe adjust the
13302 window start in case the window's width changed. */
13303 if (XMARKER (w->start)->buffer == current_buffer)
13304 compute_window_start_on_continuation_line (w);
13305
13306 w->window_end_valid = Qnil;
13307 }
13308
13309 /* Some sanity checks. */
13310 CHECK_WINDOW_END (w);
13311 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13312 abort ();
13313 if (BYTEPOS (opoint) < CHARPOS (opoint))
13314 abort ();
13315
13316 /* If %c is in mode line, update it if needed. */
13317 if (!NILP (w->column_number_displayed)
13318 /* This alternative quickly identifies a common case
13319 where no change is needed. */
13320 && !(PT == XFASTINT (w->last_point)
13321 && XFASTINT (w->last_modified) >= MODIFF
13322 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13323 && (XFASTINT (w->column_number_displayed)
13324 != (int) current_column ())) /* iftc */
13325 update_mode_line = 1;
13326
13327 /* Count number of windows showing the selected buffer. An indirect
13328 buffer counts as its base buffer. */
13329 if (!just_this_one_p)
13330 {
13331 struct buffer *current_base, *window_base;
13332 current_base = current_buffer;
13333 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13334 if (current_base->base_buffer)
13335 current_base = current_base->base_buffer;
13336 if (window_base->base_buffer)
13337 window_base = window_base->base_buffer;
13338 if (current_base == window_base)
13339 buffer_shared++;
13340 }
13341
13342 /* Point refers normally to the selected window. For any other
13343 window, set up appropriate value. */
13344 if (!EQ (window, selected_window))
13345 {
13346 int new_pt = XMARKER (w->pointm)->charpos;
13347 int new_pt_byte = marker_byte_position (w->pointm);
13348 if (new_pt < BEGV)
13349 {
13350 new_pt = BEGV;
13351 new_pt_byte = BEGV_BYTE;
13352 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13353 }
13354 else if (new_pt > (ZV - 1))
13355 {
13356 new_pt = ZV;
13357 new_pt_byte = ZV_BYTE;
13358 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13359 }
13360
13361 /* We don't use SET_PT so that the point-motion hooks don't run. */
13362 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13363 }
13364
13365 /* If any of the character widths specified in the display table
13366 have changed, invalidate the width run cache. It's true that
13367 this may be a bit late to catch such changes, but the rest of
13368 redisplay goes (non-fatally) haywire when the display table is
13369 changed, so why should we worry about doing any better? */
13370 if (current_buffer->width_run_cache)
13371 {
13372 struct Lisp_Char_Table *disptab = buffer_display_table ();
13373
13374 if (! disptab_matches_widthtab (disptab,
13375 XVECTOR (current_buffer->width_table)))
13376 {
13377 invalidate_region_cache (current_buffer,
13378 current_buffer->width_run_cache,
13379 BEG, Z);
13380 recompute_width_table (current_buffer, disptab);
13381 }
13382 }
13383
13384 /* If window-start is screwed up, choose a new one. */
13385 if (XMARKER (w->start)->buffer != current_buffer)
13386 goto recenter;
13387
13388 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13389
13390 /* If someone specified a new starting point but did not insist,
13391 check whether it can be used. */
13392 if (!NILP (w->optional_new_start)
13393 && CHARPOS (startp) >= BEGV
13394 && CHARPOS (startp) <= ZV)
13395 {
13396 w->optional_new_start = Qnil;
13397 start_display (&it, w, startp);
13398 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13399 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13400 if (IT_CHARPOS (it) == PT)
13401 w->force_start = Qt;
13402 /* IT may overshoot PT if text at PT is invisible. */
13403 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13404 w->force_start = Qt;
13405 }
13406
13407 force_start:
13408
13409 /* Handle case where place to start displaying has been specified,
13410 unless the specified location is outside the accessible range. */
13411 if (!NILP (w->force_start)
13412 || w->frozen_window_start_p)
13413 {
13414 /* We set this later on if we have to adjust point. */
13415 int new_vpos = -1;
13416
13417 w->force_start = Qnil;
13418 w->vscroll = 0;
13419 w->window_end_valid = Qnil;
13420
13421 /* Forget any recorded base line for line number display. */
13422 if (!buffer_unchanged_p)
13423 w->base_line_number = Qnil;
13424
13425 /* Redisplay the mode line. Select the buffer properly for that.
13426 Also, run the hook window-scroll-functions
13427 because we have scrolled. */
13428 /* Note, we do this after clearing force_start because
13429 if there's an error, it is better to forget about force_start
13430 than to get into an infinite loop calling the hook functions
13431 and having them get more errors. */
13432 if (!update_mode_line
13433 || ! NILP (Vwindow_scroll_functions))
13434 {
13435 update_mode_line = 1;
13436 w->update_mode_line = Qt;
13437 startp = run_window_scroll_functions (window, startp);
13438 }
13439
13440 w->last_modified = make_number (0);
13441 w->last_overlay_modified = make_number (0);
13442 if (CHARPOS (startp) < BEGV)
13443 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13444 else if (CHARPOS (startp) > ZV)
13445 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13446
13447 /* Redisplay, then check if cursor has been set during the
13448 redisplay. Give up if new fonts were loaded. */
13449 /* We used to issue a CHECK_MARGINS argument to try_window here,
13450 but this causes scrolling to fail when point begins inside
13451 the scroll margin (bug#148) -- cyd */
13452 if (!try_window (window, startp, 0))
13453 {
13454 w->force_start = Qt;
13455 clear_glyph_matrix (w->desired_matrix);
13456 goto need_larger_matrices;
13457 }
13458
13459 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13460 {
13461 /* If point does not appear, try to move point so it does
13462 appear. The desired matrix has been built above, so we
13463 can use it here. */
13464 new_vpos = window_box_height (w) / 2;
13465 }
13466
13467 if (!cursor_row_fully_visible_p (w, 0, 0))
13468 {
13469 /* Point does appear, but on a line partly visible at end of window.
13470 Move it back to a fully-visible line. */
13471 new_vpos = window_box_height (w);
13472 }
13473
13474 /* If we need to move point for either of the above reasons,
13475 now actually do it. */
13476 if (new_vpos >= 0)
13477 {
13478 struct glyph_row *row;
13479
13480 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13481 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13482 ++row;
13483
13484 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13485 MATRIX_ROW_START_BYTEPOS (row));
13486
13487 if (w != XWINDOW (selected_window))
13488 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13489 else if (current_buffer == old)
13490 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13491
13492 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13493
13494 /* If we are highlighting the region, then we just changed
13495 the region, so redisplay to show it. */
13496 if (!NILP (Vtransient_mark_mode)
13497 && !NILP (current_buffer->mark_active))
13498 {
13499 clear_glyph_matrix (w->desired_matrix);
13500 if (!try_window (window, startp, 0))
13501 goto need_larger_matrices;
13502 }
13503 }
13504
13505 #if GLYPH_DEBUG
13506 debug_method_add (w, "forced window start");
13507 #endif
13508 goto done;
13509 }
13510
13511 /* Handle case where text has not changed, only point, and it has
13512 not moved off the frame, and we are not retrying after hscroll.
13513 (current_matrix_up_to_date_p is nonzero when retrying.) */
13514 if (current_matrix_up_to_date_p
13515 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13516 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13517 {
13518 switch (rc)
13519 {
13520 case CURSOR_MOVEMENT_SUCCESS:
13521 used_current_matrix_p = 1;
13522 goto done;
13523
13524 case CURSOR_MOVEMENT_MUST_SCROLL:
13525 goto try_to_scroll;
13526
13527 default:
13528 abort ();
13529 }
13530 }
13531 /* If current starting point was originally the beginning of a line
13532 but no longer is, find a new starting point. */
13533 else if (!NILP (w->start_at_line_beg)
13534 && !(CHARPOS (startp) <= BEGV
13535 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13536 {
13537 #if GLYPH_DEBUG
13538 debug_method_add (w, "recenter 1");
13539 #endif
13540 goto recenter;
13541 }
13542
13543 /* Try scrolling with try_window_id. Value is > 0 if update has
13544 been done, it is -1 if we know that the same window start will
13545 not work. It is 0 if unsuccessful for some other reason. */
13546 else if ((tem = try_window_id (w)) != 0)
13547 {
13548 #if GLYPH_DEBUG
13549 debug_method_add (w, "try_window_id %d", tem);
13550 #endif
13551
13552 if (fonts_changed_p)
13553 goto need_larger_matrices;
13554 if (tem > 0)
13555 goto done;
13556
13557 /* Otherwise try_window_id has returned -1 which means that we
13558 don't want the alternative below this comment to execute. */
13559 }
13560 else if (CHARPOS (startp) >= BEGV
13561 && CHARPOS (startp) <= ZV
13562 && PT >= CHARPOS (startp)
13563 && (CHARPOS (startp) < ZV
13564 /* Avoid starting at end of buffer. */
13565 || CHARPOS (startp) == BEGV
13566 || (XFASTINT (w->last_modified) >= MODIFF
13567 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13568 {
13569
13570 /* If first window line is a continuation line, and window start
13571 is inside the modified region, but the first change is before
13572 current window start, we must select a new window start.
13573
13574 However, if this is the result of a down-mouse event (e.g. by
13575 extending the mouse-drag-overlay), we don't want to select a
13576 new window start, since that would change the position under
13577 the mouse, resulting in an unwanted mouse-movement rather
13578 than a simple mouse-click. */
13579 if (NILP (w->start_at_line_beg)
13580 && NILP (do_mouse_tracking)
13581 && CHARPOS (startp) > BEGV
13582 && CHARPOS (startp) > BEG + beg_unchanged
13583 && CHARPOS (startp) <= Z - end_unchanged
13584 /* Even if w->start_at_line_beg is nil, a new window may
13585 start at a line_beg, since that's how set_buffer_window
13586 sets it. So, we need to check the return value of
13587 compute_window_start_on_continuation_line. (See also
13588 bug#197). */
13589 && XMARKER (w->start)->buffer == current_buffer
13590 && compute_window_start_on_continuation_line (w))
13591 {
13592 w->force_start = Qt;
13593 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13594 goto force_start;
13595 }
13596
13597 #if GLYPH_DEBUG
13598 debug_method_add (w, "same window start");
13599 #endif
13600
13601 /* Try to redisplay starting at same place as before.
13602 If point has not moved off frame, accept the results. */
13603 if (!current_matrix_up_to_date_p
13604 /* Don't use try_window_reusing_current_matrix in this case
13605 because a window scroll function can have changed the
13606 buffer. */
13607 || !NILP (Vwindow_scroll_functions)
13608 || MINI_WINDOW_P (w)
13609 || !(used_current_matrix_p
13610 = try_window_reusing_current_matrix (w)))
13611 {
13612 IF_DEBUG (debug_method_add (w, "1"));
13613 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
13614 /* -1 means we need to scroll.
13615 0 means we need new matrices, but fonts_changed_p
13616 is set in that case, so we will detect it below. */
13617 goto try_to_scroll;
13618 }
13619
13620 if (fonts_changed_p)
13621 goto need_larger_matrices;
13622
13623 if (w->cursor.vpos >= 0)
13624 {
13625 if (!just_this_one_p
13626 || current_buffer->clip_changed
13627 || BEG_UNCHANGED < CHARPOS (startp))
13628 /* Forget any recorded base line for line number display. */
13629 w->base_line_number = Qnil;
13630
13631 if (!cursor_row_fully_visible_p (w, 1, 0))
13632 {
13633 clear_glyph_matrix (w->desired_matrix);
13634 last_line_misfit = 1;
13635 }
13636 /* Drop through and scroll. */
13637 else
13638 goto done;
13639 }
13640 else
13641 clear_glyph_matrix (w->desired_matrix);
13642 }
13643
13644 try_to_scroll:
13645
13646 w->last_modified = make_number (0);
13647 w->last_overlay_modified = make_number (0);
13648
13649 /* Redisplay the mode line. Select the buffer properly for that. */
13650 if (!update_mode_line)
13651 {
13652 update_mode_line = 1;
13653 w->update_mode_line = Qt;
13654 }
13655
13656 /* Try to scroll by specified few lines. */
13657 if ((scroll_conservatively
13658 || scroll_step
13659 || temp_scroll_step
13660 || NUMBERP (current_buffer->scroll_up_aggressively)
13661 || NUMBERP (current_buffer->scroll_down_aggressively))
13662 && !current_buffer->clip_changed
13663 && CHARPOS (startp) >= BEGV
13664 && CHARPOS (startp) <= ZV)
13665 {
13666 /* The function returns -1 if new fonts were loaded, 1 if
13667 successful, 0 if not successful. */
13668 int rc = try_scrolling (window, just_this_one_p,
13669 scroll_conservatively,
13670 scroll_step,
13671 temp_scroll_step, last_line_misfit);
13672 switch (rc)
13673 {
13674 case SCROLLING_SUCCESS:
13675 goto done;
13676
13677 case SCROLLING_NEED_LARGER_MATRICES:
13678 goto need_larger_matrices;
13679
13680 case SCROLLING_FAILED:
13681 break;
13682
13683 default:
13684 abort ();
13685 }
13686 }
13687
13688 /* Finally, just choose place to start which centers point */
13689
13690 recenter:
13691 if (centering_position < 0)
13692 centering_position = window_box_height (w) / 2;
13693
13694 #if GLYPH_DEBUG
13695 debug_method_add (w, "recenter");
13696 #endif
13697
13698 /* w->vscroll = 0; */
13699
13700 /* Forget any previously recorded base line for line number display. */
13701 if (!buffer_unchanged_p)
13702 w->base_line_number = Qnil;
13703
13704 /* Move backward half the height of the window. */
13705 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13706 it.current_y = it.last_visible_y;
13707 move_it_vertically_backward (&it, centering_position);
13708 xassert (IT_CHARPOS (it) >= BEGV);
13709
13710 /* The function move_it_vertically_backward may move over more
13711 than the specified y-distance. If it->w is small, e.g. a
13712 mini-buffer window, we may end up in front of the window's
13713 display area. Start displaying at the start of the line
13714 containing PT in this case. */
13715 if (it.current_y <= 0)
13716 {
13717 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13718 move_it_vertically_backward (&it, 0);
13719 it.current_y = 0;
13720 }
13721
13722 it.current_x = it.hpos = 0;
13723
13724 /* Set startp here explicitly in case that helps avoid an infinite loop
13725 in case the window-scroll-functions functions get errors. */
13726 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13727
13728 /* Run scroll hooks. */
13729 startp = run_window_scroll_functions (window, it.current.pos);
13730
13731 /* Redisplay the window. */
13732 if (!current_matrix_up_to_date_p
13733 || windows_or_buffers_changed
13734 || cursor_type_changed
13735 /* Don't use try_window_reusing_current_matrix in this case
13736 because it can have changed the buffer. */
13737 || !NILP (Vwindow_scroll_functions)
13738 || !just_this_one_p
13739 || MINI_WINDOW_P (w)
13740 || !(used_current_matrix_p
13741 = try_window_reusing_current_matrix (w)))
13742 try_window (window, startp, 0);
13743
13744 /* If new fonts have been loaded (due to fontsets), give up. We
13745 have to start a new redisplay since we need to re-adjust glyph
13746 matrices. */
13747 if (fonts_changed_p)
13748 goto need_larger_matrices;
13749
13750 /* If cursor did not appear assume that the middle of the window is
13751 in the first line of the window. Do it again with the next line.
13752 (Imagine a window of height 100, displaying two lines of height
13753 60. Moving back 50 from it->last_visible_y will end in the first
13754 line.) */
13755 if (w->cursor.vpos < 0)
13756 {
13757 if (!NILP (w->window_end_valid)
13758 && PT >= Z - XFASTINT (w->window_end_pos))
13759 {
13760 clear_glyph_matrix (w->desired_matrix);
13761 move_it_by_lines (&it, 1, 0);
13762 try_window (window, it.current.pos, 0);
13763 }
13764 else if (PT < IT_CHARPOS (it))
13765 {
13766 clear_glyph_matrix (w->desired_matrix);
13767 move_it_by_lines (&it, -1, 0);
13768 try_window (window, it.current.pos, 0);
13769 }
13770 else
13771 {
13772 /* Not much we can do about it. */
13773 }
13774 }
13775
13776 /* Consider the following case: Window starts at BEGV, there is
13777 invisible, intangible text at BEGV, so that display starts at
13778 some point START > BEGV. It can happen that we are called with
13779 PT somewhere between BEGV and START. Try to handle that case. */
13780 if (w->cursor.vpos < 0)
13781 {
13782 struct glyph_row *row = w->current_matrix->rows;
13783 if (row->mode_line_p)
13784 ++row;
13785 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13786 }
13787
13788 if (!cursor_row_fully_visible_p (w, 0, 0))
13789 {
13790 /* If vscroll is enabled, disable it and try again. */
13791 if (w->vscroll)
13792 {
13793 w->vscroll = 0;
13794 clear_glyph_matrix (w->desired_matrix);
13795 goto recenter;
13796 }
13797
13798 /* If centering point failed to make the whole line visible,
13799 put point at the top instead. That has to make the whole line
13800 visible, if it can be done. */
13801 if (centering_position == 0)
13802 goto done;
13803
13804 clear_glyph_matrix (w->desired_matrix);
13805 centering_position = 0;
13806 goto recenter;
13807 }
13808
13809 done:
13810
13811 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13812 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13813 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13814 ? Qt : Qnil);
13815
13816 /* Display the mode line, if we must. */
13817 if ((update_mode_line
13818 /* If window not full width, must redo its mode line
13819 if (a) the window to its side is being redone and
13820 (b) we do a frame-based redisplay. This is a consequence
13821 of how inverted lines are drawn in frame-based redisplay. */
13822 || (!just_this_one_p
13823 && !FRAME_WINDOW_P (f)
13824 && !WINDOW_FULL_WIDTH_P (w))
13825 /* Line number to display. */
13826 || INTEGERP (w->base_line_pos)
13827 /* Column number is displayed and different from the one displayed. */
13828 || (!NILP (w->column_number_displayed)
13829 && (XFASTINT (w->column_number_displayed)
13830 != (int) current_column ()))) /* iftc */
13831 /* This means that the window has a mode line. */
13832 && (WINDOW_WANTS_MODELINE_P (w)
13833 || WINDOW_WANTS_HEADER_LINE_P (w)))
13834 {
13835 display_mode_lines (w);
13836
13837 /* If mode line height has changed, arrange for a thorough
13838 immediate redisplay using the correct mode line height. */
13839 if (WINDOW_WANTS_MODELINE_P (w)
13840 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13841 {
13842 fonts_changed_p = 1;
13843 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13844 = DESIRED_MODE_LINE_HEIGHT (w);
13845 }
13846
13847 /* If header line height has changed, arrange for a thorough
13848 immediate redisplay using the correct header line height. */
13849 if (WINDOW_WANTS_HEADER_LINE_P (w)
13850 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13851 {
13852 fonts_changed_p = 1;
13853 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13854 = DESIRED_HEADER_LINE_HEIGHT (w);
13855 }
13856
13857 if (fonts_changed_p)
13858 goto need_larger_matrices;
13859 }
13860
13861 if (!line_number_displayed
13862 && !BUFFERP (w->base_line_pos))
13863 {
13864 w->base_line_pos = Qnil;
13865 w->base_line_number = Qnil;
13866 }
13867
13868 finish_menu_bars:
13869
13870 /* When we reach a frame's selected window, redo the frame's menu bar. */
13871 if (update_mode_line
13872 && EQ (FRAME_SELECTED_WINDOW (f), window))
13873 {
13874 int redisplay_menu_p = 0;
13875 int redisplay_tool_bar_p = 0;
13876
13877 if (FRAME_WINDOW_P (f))
13878 {
13879 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13880 || defined (HAVE_NS) || defined (USE_GTK)
13881 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13882 #else
13883 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13884 #endif
13885 }
13886 else
13887 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13888
13889 if (redisplay_menu_p)
13890 display_menu_bar (w);
13891
13892 #ifdef HAVE_WINDOW_SYSTEM
13893 if (FRAME_WINDOW_P (f))
13894 {
13895 #if defined (USE_GTK) || defined (HAVE_NS)
13896 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13897 #else
13898 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13899 && (FRAME_TOOL_BAR_LINES (f) > 0
13900 || !NILP (Vauto_resize_tool_bars));
13901 #endif
13902
13903 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13904 {
13905 extern int ignore_mouse_drag_p;
13906 ignore_mouse_drag_p = 1;
13907 }
13908 }
13909 #endif
13910 }
13911
13912 #ifdef HAVE_WINDOW_SYSTEM
13913 if (FRAME_WINDOW_P (f)
13914 && update_window_fringes (w, (just_this_one_p
13915 || (!used_current_matrix_p && !overlay_arrow_seen)
13916 || w->pseudo_window_p)))
13917 {
13918 update_begin (f);
13919 BLOCK_INPUT;
13920 if (draw_window_fringes (w, 1))
13921 x_draw_vertical_border (w);
13922 UNBLOCK_INPUT;
13923 update_end (f);
13924 }
13925 #endif /* HAVE_WINDOW_SYSTEM */
13926
13927 /* We go to this label, with fonts_changed_p nonzero,
13928 if it is necessary to try again using larger glyph matrices.
13929 We have to redeem the scroll bar even in this case,
13930 because the loop in redisplay_internal expects that. */
13931 need_larger_matrices:
13932 ;
13933 finish_scroll_bars:
13934
13935 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13936 {
13937 /* Set the thumb's position and size. */
13938 set_vertical_scroll_bar (w);
13939
13940 /* Note that we actually used the scroll bar attached to this
13941 window, so it shouldn't be deleted at the end of redisplay. */
13942 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13943 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13944 }
13945
13946 /* Restore current_buffer and value of point in it. The window
13947 update may have changed the buffer, so first make sure `opoint'
13948 is still valid (Bug#6177). */
13949 if (CHARPOS (opoint) < BEGV)
13950 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13951 else if (CHARPOS (opoint) > ZV)
13952 TEMP_SET_PT_BOTH (Z, Z_BYTE);
13953 else
13954 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13955
13956 set_buffer_internal_1 (old);
13957 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13958 shorter. This can be caused by log truncation in *Messages*. */
13959 if (CHARPOS (lpoint) <= ZV)
13960 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13961
13962 unbind_to (count, Qnil);
13963 }
13964
13965
13966 /* Build the complete desired matrix of WINDOW with a window start
13967 buffer position POS.
13968
13969 Value is 1 if successful. It is zero if fonts were loaded during
13970 redisplay which makes re-adjusting glyph matrices necessary, and -1
13971 if point would appear in the scroll margins.
13972 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
13973 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
13974 set in FLAGS.) */
13975
13976 int
13977 try_window (window, pos, flags)
13978 Lisp_Object window;
13979 struct text_pos pos;
13980 int flags;
13981 {
13982 struct window *w = XWINDOW (window);
13983 struct it it;
13984 struct glyph_row *last_text_row = NULL;
13985 struct frame *f = XFRAME (w->frame);
13986
13987 /* Make POS the new window start. */
13988 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13989
13990 /* Mark cursor position as unknown. No overlay arrow seen. */
13991 w->cursor.vpos = -1;
13992 overlay_arrow_seen = 0;
13993
13994 /* Initialize iterator and info to start at POS. */
13995 start_display (&it, w, pos);
13996
13997 /* Display all lines of W. */
13998 while (it.current_y < it.last_visible_y)
13999 {
14000 if (display_line (&it))
14001 last_text_row = it.glyph_row - 1;
14002 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
14003 return 0;
14004 }
14005
14006 /* Don't let the cursor end in the scroll margins. */
14007 if ((flags & TRY_WINDOW_CHECK_MARGINS)
14008 && !MINI_WINDOW_P (w))
14009 {
14010 int this_scroll_margin;
14011
14012 if (scroll_margin > 0)
14013 {
14014 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14015 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14016 }
14017 else
14018 this_scroll_margin = 0;
14019
14020 if ((w->cursor.y >= 0 /* not vscrolled */
14021 && w->cursor.y < this_scroll_margin
14022 && CHARPOS (pos) > BEGV
14023 && IT_CHARPOS (it) < ZV)
14024 /* rms: considering make_cursor_line_fully_visible_p here
14025 seems to give wrong results. We don't want to recenter
14026 when the last line is partly visible, we want to allow
14027 that case to be handled in the usual way. */
14028 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
14029 {
14030 w->cursor.vpos = -1;
14031 clear_glyph_matrix (w->desired_matrix);
14032 return -1;
14033 }
14034 }
14035
14036 /* If bottom moved off end of frame, change mode line percentage. */
14037 if (XFASTINT (w->window_end_pos) <= 0
14038 && Z != IT_CHARPOS (it))
14039 w->update_mode_line = Qt;
14040
14041 /* Set window_end_pos to the offset of the last character displayed
14042 on the window from the end of current_buffer. Set
14043 window_end_vpos to its row number. */
14044 if (last_text_row)
14045 {
14046 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14047 w->window_end_bytepos
14048 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14049 w->window_end_pos
14050 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14051 w->window_end_vpos
14052 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14053 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14054 ->displays_text_p);
14055 }
14056 else
14057 {
14058 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14059 w->window_end_pos = make_number (Z - ZV);
14060 w->window_end_vpos = make_number (0);
14061 }
14062
14063 /* But that is not valid info until redisplay finishes. */
14064 w->window_end_valid = Qnil;
14065 return 1;
14066 }
14067
14068
14069 \f
14070 /************************************************************************
14071 Window redisplay reusing current matrix when buffer has not changed
14072 ************************************************************************/
14073
14074 /* Try redisplay of window W showing an unchanged buffer with a
14075 different window start than the last time it was displayed by
14076 reusing its current matrix. Value is non-zero if successful.
14077 W->start is the new window start. */
14078
14079 static int
14080 try_window_reusing_current_matrix (w)
14081 struct window *w;
14082 {
14083 struct frame *f = XFRAME (w->frame);
14084 struct glyph_row *row, *bottom_row;
14085 struct it it;
14086 struct run run;
14087 struct text_pos start, new_start;
14088 int nrows_scrolled, i;
14089 struct glyph_row *last_text_row;
14090 struct glyph_row *last_reused_text_row;
14091 struct glyph_row *start_row;
14092 int start_vpos, min_y, max_y;
14093
14094 #if GLYPH_DEBUG
14095 if (inhibit_try_window_reusing)
14096 return 0;
14097 #endif
14098
14099 if (/* This function doesn't handle terminal frames. */
14100 !FRAME_WINDOW_P (f)
14101 /* Don't try to reuse the display if windows have been split
14102 or such. */
14103 || windows_or_buffers_changed
14104 || cursor_type_changed)
14105 return 0;
14106
14107 /* Can't do this if region may have changed. */
14108 if ((!NILP (Vtransient_mark_mode)
14109 && !NILP (current_buffer->mark_active))
14110 || !NILP (w->region_showing)
14111 || !NILP (Vshow_trailing_whitespace))
14112 return 0;
14113
14114 /* If top-line visibility has changed, give up. */
14115 if (WINDOW_WANTS_HEADER_LINE_P (w)
14116 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14117 return 0;
14118
14119 /* Give up if old or new display is scrolled vertically. We could
14120 make this function handle this, but right now it doesn't. */
14121 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14122 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14123 return 0;
14124
14125 /* The variable new_start now holds the new window start. The old
14126 start `start' can be determined from the current matrix. */
14127 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14128 start = start_row->start.pos;
14129 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14130
14131 /* Clear the desired matrix for the display below. */
14132 clear_glyph_matrix (w->desired_matrix);
14133
14134 if (CHARPOS (new_start) <= CHARPOS (start))
14135 {
14136 int first_row_y;
14137
14138 /* Don't use this method if the display starts with an ellipsis
14139 displayed for invisible text. It's not easy to handle that case
14140 below, and it's certainly not worth the effort since this is
14141 not a frequent case. */
14142 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14143 return 0;
14144
14145 IF_DEBUG (debug_method_add (w, "twu1"));
14146
14147 /* Display up to a row that can be reused. The variable
14148 last_text_row is set to the last row displayed that displays
14149 text. Note that it.vpos == 0 if or if not there is a
14150 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14151 start_display (&it, w, new_start);
14152 first_row_y = it.current_y;
14153 w->cursor.vpos = -1;
14154 last_text_row = last_reused_text_row = NULL;
14155
14156 while (it.current_y < it.last_visible_y
14157 && !fonts_changed_p)
14158 {
14159 /* If we have reached into the characters in the START row,
14160 that means the line boundaries have changed. So we
14161 can't start copying with the row START. Maybe it will
14162 work to start copying with the following row. */
14163 while (IT_CHARPOS (it) > CHARPOS (start))
14164 {
14165 /* Advance to the next row as the "start". */
14166 start_row++;
14167 start = start_row->start.pos;
14168 /* If there are no more rows to try, or just one, give up. */
14169 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14170 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14171 || CHARPOS (start) == ZV)
14172 {
14173 clear_glyph_matrix (w->desired_matrix);
14174 return 0;
14175 }
14176
14177 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14178 }
14179 /* If we have reached alignment,
14180 we can copy the rest of the rows. */
14181 if (IT_CHARPOS (it) == CHARPOS (start))
14182 break;
14183
14184 if (display_line (&it))
14185 last_text_row = it.glyph_row - 1;
14186 }
14187
14188 /* A value of current_y < last_visible_y means that we stopped
14189 at the previous window start, which in turn means that we
14190 have at least one reusable row. */
14191 if (it.current_y < it.last_visible_y)
14192 {
14193 /* IT.vpos always starts from 0; it counts text lines. */
14194 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14195
14196 /* Find PT if not already found in the lines displayed. */
14197 if (w->cursor.vpos < 0)
14198 {
14199 int dy = it.current_y - start_row->y;
14200
14201 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14202 row = row_containing_pos (w, PT, row, NULL, dy);
14203 if (row)
14204 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14205 dy, nrows_scrolled);
14206 else
14207 {
14208 clear_glyph_matrix (w->desired_matrix);
14209 return 0;
14210 }
14211 }
14212
14213 /* Scroll the display. Do it before the current matrix is
14214 changed. The problem here is that update has not yet
14215 run, i.e. part of the current matrix is not up to date.
14216 scroll_run_hook will clear the cursor, and use the
14217 current matrix to get the height of the row the cursor is
14218 in. */
14219 run.current_y = start_row->y;
14220 run.desired_y = it.current_y;
14221 run.height = it.last_visible_y - it.current_y;
14222
14223 if (run.height > 0 && run.current_y != run.desired_y)
14224 {
14225 update_begin (f);
14226 FRAME_RIF (f)->update_window_begin_hook (w);
14227 FRAME_RIF (f)->clear_window_mouse_face (w);
14228 FRAME_RIF (f)->scroll_run_hook (w, &run);
14229 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14230 update_end (f);
14231 }
14232
14233 /* Shift current matrix down by nrows_scrolled lines. */
14234 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14235 rotate_matrix (w->current_matrix,
14236 start_vpos,
14237 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14238 nrows_scrolled);
14239
14240 /* Disable lines that must be updated. */
14241 for (i = 0; i < nrows_scrolled; ++i)
14242 (start_row + i)->enabled_p = 0;
14243
14244 /* Re-compute Y positions. */
14245 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14246 max_y = it.last_visible_y;
14247 for (row = start_row + nrows_scrolled;
14248 row < bottom_row;
14249 ++row)
14250 {
14251 row->y = it.current_y;
14252 row->visible_height = row->height;
14253
14254 if (row->y < min_y)
14255 row->visible_height -= min_y - row->y;
14256 if (row->y + row->height > max_y)
14257 row->visible_height -= row->y + row->height - max_y;
14258 if (row->fringe_bitmap_periodic_p)
14259 row->redraw_fringe_bitmaps_p = 1;
14260
14261 it.current_y += row->height;
14262
14263 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14264 last_reused_text_row = row;
14265 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14266 break;
14267 }
14268
14269 /* Disable lines in the current matrix which are now
14270 below the window. */
14271 for (++row; row < bottom_row; ++row)
14272 row->enabled_p = row->mode_line_p = 0;
14273 }
14274
14275 /* Update window_end_pos etc.; last_reused_text_row is the last
14276 reused row from the current matrix containing text, if any.
14277 The value of last_text_row is the last displayed line
14278 containing text. */
14279 if (last_reused_text_row)
14280 {
14281 w->window_end_bytepos
14282 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14283 w->window_end_pos
14284 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14285 w->window_end_vpos
14286 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14287 w->current_matrix));
14288 }
14289 else if (last_text_row)
14290 {
14291 w->window_end_bytepos
14292 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14293 w->window_end_pos
14294 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14295 w->window_end_vpos
14296 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14297 }
14298 else
14299 {
14300 /* This window must be completely empty. */
14301 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14302 w->window_end_pos = make_number (Z - ZV);
14303 w->window_end_vpos = make_number (0);
14304 }
14305 w->window_end_valid = Qnil;
14306
14307 /* Update hint: don't try scrolling again in update_window. */
14308 w->desired_matrix->no_scrolling_p = 1;
14309
14310 #if GLYPH_DEBUG
14311 debug_method_add (w, "try_window_reusing_current_matrix 1");
14312 #endif
14313 return 1;
14314 }
14315 else if (CHARPOS (new_start) > CHARPOS (start))
14316 {
14317 struct glyph_row *pt_row, *row;
14318 struct glyph_row *first_reusable_row;
14319 struct glyph_row *first_row_to_display;
14320 int dy;
14321 int yb = window_text_bottom_y (w);
14322
14323 /* Find the row starting at new_start, if there is one. Don't
14324 reuse a partially visible line at the end. */
14325 first_reusable_row = start_row;
14326 while (first_reusable_row->enabled_p
14327 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14328 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14329 < CHARPOS (new_start)))
14330 ++first_reusable_row;
14331
14332 /* Give up if there is no row to reuse. */
14333 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14334 || !first_reusable_row->enabled_p
14335 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14336 != CHARPOS (new_start)))
14337 return 0;
14338
14339 /* We can reuse fully visible rows beginning with
14340 first_reusable_row to the end of the window. Set
14341 first_row_to_display to the first row that cannot be reused.
14342 Set pt_row to the row containing point, if there is any. */
14343 pt_row = NULL;
14344 for (first_row_to_display = first_reusable_row;
14345 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14346 ++first_row_to_display)
14347 {
14348 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14349 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14350 pt_row = first_row_to_display;
14351 }
14352
14353 /* Start displaying at the start of first_row_to_display. */
14354 xassert (first_row_to_display->y < yb);
14355 init_to_row_start (&it, w, first_row_to_display);
14356
14357 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14358 - start_vpos);
14359 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14360 - nrows_scrolled);
14361 it.current_y = (first_row_to_display->y - first_reusable_row->y
14362 + WINDOW_HEADER_LINE_HEIGHT (w));
14363
14364 /* Display lines beginning with first_row_to_display in the
14365 desired matrix. Set last_text_row to the last row displayed
14366 that displays text. */
14367 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14368 if (pt_row == NULL)
14369 w->cursor.vpos = -1;
14370 last_text_row = NULL;
14371 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14372 if (display_line (&it))
14373 last_text_row = it.glyph_row - 1;
14374
14375 /* If point is in a reused row, adjust y and vpos of the cursor
14376 position. */
14377 if (pt_row)
14378 {
14379 w->cursor.vpos -= nrows_scrolled;
14380 w->cursor.y -= first_reusable_row->y - start_row->y;
14381 }
14382
14383 /* Give up if point isn't in a row displayed or reused. (This
14384 also handles the case where w->cursor.vpos < nrows_scrolled
14385 after the calls to display_line, which can happen with scroll
14386 margins. See bug#1295.) */
14387 if (w->cursor.vpos < 0)
14388 {
14389 clear_glyph_matrix (w->desired_matrix);
14390 return 0;
14391 }
14392
14393 /* Scroll the display. */
14394 run.current_y = first_reusable_row->y;
14395 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14396 run.height = it.last_visible_y - run.current_y;
14397 dy = run.current_y - run.desired_y;
14398
14399 if (run.height)
14400 {
14401 update_begin (f);
14402 FRAME_RIF (f)->update_window_begin_hook (w);
14403 FRAME_RIF (f)->clear_window_mouse_face (w);
14404 FRAME_RIF (f)->scroll_run_hook (w, &run);
14405 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14406 update_end (f);
14407 }
14408
14409 /* Adjust Y positions of reused rows. */
14410 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14411 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14412 max_y = it.last_visible_y;
14413 for (row = first_reusable_row; row < first_row_to_display; ++row)
14414 {
14415 row->y -= dy;
14416 row->visible_height = row->height;
14417 if (row->y < min_y)
14418 row->visible_height -= min_y - row->y;
14419 if (row->y + row->height > max_y)
14420 row->visible_height -= row->y + row->height - max_y;
14421 if (row->fringe_bitmap_periodic_p)
14422 row->redraw_fringe_bitmaps_p = 1;
14423 }
14424
14425 /* Scroll the current matrix. */
14426 xassert (nrows_scrolled > 0);
14427 rotate_matrix (w->current_matrix,
14428 start_vpos,
14429 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14430 -nrows_scrolled);
14431
14432 /* Disable rows not reused. */
14433 for (row -= nrows_scrolled; row < bottom_row; ++row)
14434 row->enabled_p = 0;
14435
14436 /* Point may have moved to a different line, so we cannot assume that
14437 the previous cursor position is valid; locate the correct row. */
14438 if (pt_row)
14439 {
14440 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14441 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14442 row++)
14443 {
14444 w->cursor.vpos++;
14445 w->cursor.y = row->y;
14446 }
14447 if (row < bottom_row)
14448 {
14449 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14450 struct glyph *end = glyph + row->used[TEXT_AREA];
14451
14452 for (; glyph < end
14453 && (!BUFFERP (glyph->object)
14454 || glyph->charpos < PT);
14455 glyph++)
14456 {
14457 w->cursor.hpos++;
14458 w->cursor.x += glyph->pixel_width;
14459 }
14460 }
14461 }
14462
14463 /* Adjust window end. A null value of last_text_row means that
14464 the window end is in reused rows which in turn means that
14465 only its vpos can have changed. */
14466 if (last_text_row)
14467 {
14468 w->window_end_bytepos
14469 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14470 w->window_end_pos
14471 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14472 w->window_end_vpos
14473 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14474 }
14475 else
14476 {
14477 w->window_end_vpos
14478 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14479 }
14480
14481 w->window_end_valid = Qnil;
14482 w->desired_matrix->no_scrolling_p = 1;
14483
14484 #if GLYPH_DEBUG
14485 debug_method_add (w, "try_window_reusing_current_matrix 2");
14486 #endif
14487 return 1;
14488 }
14489
14490 return 0;
14491 }
14492
14493
14494 \f
14495 /************************************************************************
14496 Window redisplay reusing current matrix when buffer has changed
14497 ************************************************************************/
14498
14499 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14500 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14501 int *, int *));
14502 static struct glyph_row *
14503 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14504 struct glyph_row *));
14505
14506
14507 /* Return the last row in MATRIX displaying text. If row START is
14508 non-null, start searching with that row. IT gives the dimensions
14509 of the display. Value is null if matrix is empty; otherwise it is
14510 a pointer to the row found. */
14511
14512 static struct glyph_row *
14513 find_last_row_displaying_text (matrix, it, start)
14514 struct glyph_matrix *matrix;
14515 struct it *it;
14516 struct glyph_row *start;
14517 {
14518 struct glyph_row *row, *row_found;
14519
14520 /* Set row_found to the last row in IT->w's current matrix
14521 displaying text. The loop looks funny but think of partially
14522 visible lines. */
14523 row_found = NULL;
14524 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14525 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14526 {
14527 xassert (row->enabled_p);
14528 row_found = row;
14529 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14530 break;
14531 ++row;
14532 }
14533
14534 return row_found;
14535 }
14536
14537
14538 /* Return the last row in the current matrix of W that is not affected
14539 by changes at the start of current_buffer that occurred since W's
14540 current matrix was built. Value is null if no such row exists.
14541
14542 BEG_UNCHANGED us the number of characters unchanged at the start of
14543 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14544 first changed character in current_buffer. Characters at positions <
14545 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14546 when the current matrix was built. */
14547
14548 static struct glyph_row *
14549 find_last_unchanged_at_beg_row (w)
14550 struct window *w;
14551 {
14552 int first_changed_pos = BEG + BEG_UNCHANGED;
14553 struct glyph_row *row;
14554 struct glyph_row *row_found = NULL;
14555 int yb = window_text_bottom_y (w);
14556
14557 /* Find the last row displaying unchanged text. */
14558 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14559 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14560 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14561 ++row)
14562 {
14563 if (/* If row ends before first_changed_pos, it is unchanged,
14564 except in some case. */
14565 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14566 /* When row ends in ZV and we write at ZV it is not
14567 unchanged. */
14568 && !row->ends_at_zv_p
14569 /* When first_changed_pos is the end of a continued line,
14570 row is not unchanged because it may be no longer
14571 continued. */
14572 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14573 && (row->continued_p
14574 || row->exact_window_width_line_p)))
14575 row_found = row;
14576
14577 /* Stop if last visible row. */
14578 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14579 break;
14580 }
14581
14582 return row_found;
14583 }
14584
14585
14586 /* Find the first glyph row in the current matrix of W that is not
14587 affected by changes at the end of current_buffer since the
14588 time W's current matrix was built.
14589
14590 Return in *DELTA the number of chars by which buffer positions in
14591 unchanged text at the end of current_buffer must be adjusted.
14592
14593 Return in *DELTA_BYTES the corresponding number of bytes.
14594
14595 Value is null if no such row exists, i.e. all rows are affected by
14596 changes. */
14597
14598 static struct glyph_row *
14599 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14600 struct window *w;
14601 int *delta, *delta_bytes;
14602 {
14603 struct glyph_row *row;
14604 struct glyph_row *row_found = NULL;
14605
14606 *delta = *delta_bytes = 0;
14607
14608 /* Display must not have been paused, otherwise the current matrix
14609 is not up to date. */
14610 eassert (!NILP (w->window_end_valid));
14611
14612 /* A value of window_end_pos >= END_UNCHANGED means that the window
14613 end is in the range of changed text. If so, there is no
14614 unchanged row at the end of W's current matrix. */
14615 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14616 return NULL;
14617
14618 /* Set row to the last row in W's current matrix displaying text. */
14619 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14620
14621 /* If matrix is entirely empty, no unchanged row exists. */
14622 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14623 {
14624 /* The value of row is the last glyph row in the matrix having a
14625 meaningful buffer position in it. The end position of row
14626 corresponds to window_end_pos. This allows us to translate
14627 buffer positions in the current matrix to current buffer
14628 positions for characters not in changed text. */
14629 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14630 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14631 int last_unchanged_pos, last_unchanged_pos_old;
14632 struct glyph_row *first_text_row
14633 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14634
14635 *delta = Z - Z_old;
14636 *delta_bytes = Z_BYTE - Z_BYTE_old;
14637
14638 /* Set last_unchanged_pos to the buffer position of the last
14639 character in the buffer that has not been changed. Z is the
14640 index + 1 of the last character in current_buffer, i.e. by
14641 subtracting END_UNCHANGED we get the index of the last
14642 unchanged character, and we have to add BEG to get its buffer
14643 position. */
14644 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14645 last_unchanged_pos_old = last_unchanged_pos - *delta;
14646
14647 /* Search backward from ROW for a row displaying a line that
14648 starts at a minimum position >= last_unchanged_pos_old. */
14649 for (; row > first_text_row; --row)
14650 {
14651 /* This used to abort, but it can happen.
14652 It is ok to just stop the search instead here. KFS. */
14653 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14654 break;
14655
14656 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14657 row_found = row;
14658 }
14659 }
14660
14661 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14662
14663 return row_found;
14664 }
14665
14666
14667 /* Make sure that glyph rows in the current matrix of window W
14668 reference the same glyph memory as corresponding rows in the
14669 frame's frame matrix. This function is called after scrolling W's
14670 current matrix on a terminal frame in try_window_id and
14671 try_window_reusing_current_matrix. */
14672
14673 static void
14674 sync_frame_with_window_matrix_rows (w)
14675 struct window *w;
14676 {
14677 struct frame *f = XFRAME (w->frame);
14678 struct glyph_row *window_row, *window_row_end, *frame_row;
14679
14680 /* Preconditions: W must be a leaf window and full-width. Its frame
14681 must have a frame matrix. */
14682 xassert (NILP (w->hchild) && NILP (w->vchild));
14683 xassert (WINDOW_FULL_WIDTH_P (w));
14684 xassert (!FRAME_WINDOW_P (f));
14685
14686 /* If W is a full-width window, glyph pointers in W's current matrix
14687 have, by definition, to be the same as glyph pointers in the
14688 corresponding frame matrix. Note that frame matrices have no
14689 marginal areas (see build_frame_matrix). */
14690 window_row = w->current_matrix->rows;
14691 window_row_end = window_row + w->current_matrix->nrows;
14692 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14693 while (window_row < window_row_end)
14694 {
14695 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14696 struct glyph *end = window_row->glyphs[LAST_AREA];
14697
14698 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14699 frame_row->glyphs[TEXT_AREA] = start;
14700 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14701 frame_row->glyphs[LAST_AREA] = end;
14702
14703 /* Disable frame rows whose corresponding window rows have
14704 been disabled in try_window_id. */
14705 if (!window_row->enabled_p)
14706 frame_row->enabled_p = 0;
14707
14708 ++window_row, ++frame_row;
14709 }
14710 }
14711
14712
14713 /* Find the glyph row in window W containing CHARPOS. Consider all
14714 rows between START and END (not inclusive). END null means search
14715 all rows to the end of the display area of W. Value is the row
14716 containing CHARPOS or null. */
14717
14718 struct glyph_row *
14719 row_containing_pos (w, charpos, start, end, dy)
14720 struct window *w;
14721 int charpos;
14722 struct glyph_row *start, *end;
14723 int dy;
14724 {
14725 struct glyph_row *row = start;
14726 int last_y;
14727
14728 /* If we happen to start on a header-line, skip that. */
14729 if (row->mode_line_p)
14730 ++row;
14731
14732 if ((end && row >= end) || !row->enabled_p)
14733 return NULL;
14734
14735 last_y = window_text_bottom_y (w) - dy;
14736
14737 while (1)
14738 {
14739 /* Give up if we have gone too far. */
14740 if (end && row >= end)
14741 return NULL;
14742 /* This formerly returned if they were equal.
14743 I think that both quantities are of a "last plus one" type;
14744 if so, when they are equal, the row is within the screen. -- rms. */
14745 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14746 return NULL;
14747
14748 /* If it is in this row, return this row. */
14749 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14750 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14751 /* The end position of a row equals the start
14752 position of the next row. If CHARPOS is there, we
14753 would rather display it in the next line, except
14754 when this line ends in ZV. */
14755 && !row->ends_at_zv_p
14756 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14757 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14758 return row;
14759 ++row;
14760 }
14761 }
14762
14763
14764 /* Try to redisplay window W by reusing its existing display. W's
14765 current matrix must be up to date when this function is called,
14766 i.e. window_end_valid must not be nil.
14767
14768 Value is
14769
14770 1 if display has been updated
14771 0 if otherwise unsuccessful
14772 -1 if redisplay with same window start is known not to succeed
14773
14774 The following steps are performed:
14775
14776 1. Find the last row in the current matrix of W that is not
14777 affected by changes at the start of current_buffer. If no such row
14778 is found, give up.
14779
14780 2. Find the first row in W's current matrix that is not affected by
14781 changes at the end of current_buffer. Maybe there is no such row.
14782
14783 3. Display lines beginning with the row + 1 found in step 1 to the
14784 row found in step 2 or, if step 2 didn't find a row, to the end of
14785 the window.
14786
14787 4. If cursor is not known to appear on the window, give up.
14788
14789 5. If display stopped at the row found in step 2, scroll the
14790 display and current matrix as needed.
14791
14792 6. Maybe display some lines at the end of W, if we must. This can
14793 happen under various circumstances, like a partially visible line
14794 becoming fully visible, or because newly displayed lines are displayed
14795 in smaller font sizes.
14796
14797 7. Update W's window end information. */
14798
14799 static int
14800 try_window_id (w)
14801 struct window *w;
14802 {
14803 struct frame *f = XFRAME (w->frame);
14804 struct glyph_matrix *current_matrix = w->current_matrix;
14805 struct glyph_matrix *desired_matrix = w->desired_matrix;
14806 struct glyph_row *last_unchanged_at_beg_row;
14807 struct glyph_row *first_unchanged_at_end_row;
14808 struct glyph_row *row;
14809 struct glyph_row *bottom_row;
14810 int bottom_vpos;
14811 struct it it;
14812 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14813 struct text_pos start_pos;
14814 struct run run;
14815 int first_unchanged_at_end_vpos = 0;
14816 struct glyph_row *last_text_row, *last_text_row_at_end;
14817 struct text_pos start;
14818 int first_changed_charpos, last_changed_charpos;
14819
14820 #if GLYPH_DEBUG
14821 if (inhibit_try_window_id)
14822 return 0;
14823 #endif
14824
14825 /* This is handy for debugging. */
14826 #if 0
14827 #define GIVE_UP(X) \
14828 do { \
14829 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14830 return 0; \
14831 } while (0)
14832 #else
14833 #define GIVE_UP(X) return 0
14834 #endif
14835
14836 SET_TEXT_POS_FROM_MARKER (start, w->start);
14837
14838 /* Don't use this for mini-windows because these can show
14839 messages and mini-buffers, and we don't handle that here. */
14840 if (MINI_WINDOW_P (w))
14841 GIVE_UP (1);
14842
14843 /* This flag is used to prevent redisplay optimizations. */
14844 if (windows_or_buffers_changed || cursor_type_changed)
14845 GIVE_UP (2);
14846
14847 /* Verify that narrowing has not changed.
14848 Also verify that we were not told to prevent redisplay optimizations.
14849 It would be nice to further
14850 reduce the number of cases where this prevents try_window_id. */
14851 if (current_buffer->clip_changed
14852 || current_buffer->prevent_redisplay_optimizations_p)
14853 GIVE_UP (3);
14854
14855 /* Window must either use window-based redisplay or be full width. */
14856 if (!FRAME_WINDOW_P (f)
14857 && (!FRAME_LINE_INS_DEL_OK (f)
14858 || !WINDOW_FULL_WIDTH_P (w)))
14859 GIVE_UP (4);
14860
14861 /* Give up if point is known NOT to appear in W. */
14862 if (PT < CHARPOS (start))
14863 GIVE_UP (5);
14864
14865 /* Another way to prevent redisplay optimizations. */
14866 if (XFASTINT (w->last_modified) == 0)
14867 GIVE_UP (6);
14868
14869 /* Verify that window is not hscrolled. */
14870 if (XFASTINT (w->hscroll) != 0)
14871 GIVE_UP (7);
14872
14873 /* Verify that display wasn't paused. */
14874 if (NILP (w->window_end_valid))
14875 GIVE_UP (8);
14876
14877 /* Can't use this if highlighting a region because a cursor movement
14878 will do more than just set the cursor. */
14879 if (!NILP (Vtransient_mark_mode)
14880 && !NILP (current_buffer->mark_active))
14881 GIVE_UP (9);
14882
14883 /* Likewise if highlighting trailing whitespace. */
14884 if (!NILP (Vshow_trailing_whitespace))
14885 GIVE_UP (11);
14886
14887 /* Likewise if showing a region. */
14888 if (!NILP (w->region_showing))
14889 GIVE_UP (10);
14890
14891 /* Can't use this if overlay arrow position and/or string have
14892 changed. */
14893 if (overlay_arrows_changed_p ())
14894 GIVE_UP (12);
14895
14896 /* When word-wrap is on, adding a space to the first word of a
14897 wrapped line can change the wrap position, altering the line
14898 above it. It might be worthwhile to handle this more
14899 intelligently, but for now just redisplay from scratch. */
14900 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14901 GIVE_UP (21);
14902
14903 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14904 only if buffer has really changed. The reason is that the gap is
14905 initially at Z for freshly visited files. The code below would
14906 set end_unchanged to 0 in that case. */
14907 if (MODIFF > SAVE_MODIFF
14908 /* This seems to happen sometimes after saving a buffer. */
14909 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14910 {
14911 if (GPT - BEG < BEG_UNCHANGED)
14912 BEG_UNCHANGED = GPT - BEG;
14913 if (Z - GPT < END_UNCHANGED)
14914 END_UNCHANGED = Z - GPT;
14915 }
14916
14917 /* The position of the first and last character that has been changed. */
14918 first_changed_charpos = BEG + BEG_UNCHANGED;
14919 last_changed_charpos = Z - END_UNCHANGED;
14920
14921 /* If window starts after a line end, and the last change is in
14922 front of that newline, then changes don't affect the display.
14923 This case happens with stealth-fontification. Note that although
14924 the display is unchanged, glyph positions in the matrix have to
14925 be adjusted, of course. */
14926 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14927 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14928 && ((last_changed_charpos < CHARPOS (start)
14929 && CHARPOS (start) == BEGV)
14930 || (last_changed_charpos < CHARPOS (start) - 1
14931 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14932 {
14933 int Z_old, delta, Z_BYTE_old, delta_bytes;
14934 struct glyph_row *r0;
14935
14936 /* Compute how many chars/bytes have been added to or removed
14937 from the buffer. */
14938 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14939 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14940 delta = Z - Z_old;
14941 delta_bytes = Z_BYTE - Z_BYTE_old;
14942
14943 /* Give up if PT is not in the window. Note that it already has
14944 been checked at the start of try_window_id that PT is not in
14945 front of the window start. */
14946 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14947 GIVE_UP (13);
14948
14949 /* If window start is unchanged, we can reuse the whole matrix
14950 as is, after adjusting glyph positions. No need to compute
14951 the window end again, since its offset from Z hasn't changed. */
14952 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14953 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14954 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14955 /* PT must not be in a partially visible line. */
14956 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14957 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14958 {
14959 /* Adjust positions in the glyph matrix. */
14960 if (delta || delta_bytes)
14961 {
14962 struct glyph_row *r1
14963 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14964 increment_matrix_positions (w->current_matrix,
14965 MATRIX_ROW_VPOS (r0, current_matrix),
14966 MATRIX_ROW_VPOS (r1, current_matrix),
14967 delta, delta_bytes);
14968 }
14969
14970 /* Set the cursor. */
14971 row = row_containing_pos (w, PT, r0, NULL, 0);
14972 if (row)
14973 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14974 else
14975 abort ();
14976 return 1;
14977 }
14978 }
14979
14980 /* Handle the case that changes are all below what is displayed in
14981 the window, and that PT is in the window. This shortcut cannot
14982 be taken if ZV is visible in the window, and text has been added
14983 there that is visible in the window. */
14984 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14985 /* ZV is not visible in the window, or there are no
14986 changes at ZV, actually. */
14987 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14988 || first_changed_charpos == last_changed_charpos))
14989 {
14990 struct glyph_row *r0;
14991
14992 /* Give up if PT is not in the window. Note that it already has
14993 been checked at the start of try_window_id that PT is not in
14994 front of the window start. */
14995 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14996 GIVE_UP (14);
14997
14998 /* If window start is unchanged, we can reuse the whole matrix
14999 as is, without changing glyph positions since no text has
15000 been added/removed in front of the window end. */
15001 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
15002 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
15003 /* PT must not be in a partially visible line. */
15004 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
15005 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
15006 {
15007 /* We have to compute the window end anew since text
15008 can have been added/removed after it. */
15009 w->window_end_pos
15010 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15011 w->window_end_bytepos
15012 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15013
15014 /* Set the cursor. */
15015 row = row_containing_pos (w, PT, r0, NULL, 0);
15016 if (row)
15017 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
15018 else
15019 abort ();
15020 return 2;
15021 }
15022 }
15023
15024 /* Give up if window start is in the changed area.
15025
15026 The condition used to read
15027
15028 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15029
15030 but why that was tested escapes me at the moment. */
15031 if (CHARPOS (start) >= first_changed_charpos
15032 && CHARPOS (start) <= last_changed_charpos)
15033 GIVE_UP (15);
15034
15035 /* Check that window start agrees with the start of the first glyph
15036 row in its current matrix. Check this after we know the window
15037 start is not in changed text, otherwise positions would not be
15038 comparable. */
15039 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15040 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15041 GIVE_UP (16);
15042
15043 /* Give up if the window ends in strings. Overlay strings
15044 at the end are difficult to handle, so don't try. */
15045 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15046 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15047 GIVE_UP (20);
15048
15049 /* Compute the position at which we have to start displaying new
15050 lines. Some of the lines at the top of the window might be
15051 reusable because they are not displaying changed text. Find the
15052 last row in W's current matrix not affected by changes at the
15053 start of current_buffer. Value is null if changes start in the
15054 first line of window. */
15055 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15056 if (last_unchanged_at_beg_row)
15057 {
15058 /* Avoid starting to display in the moddle of a character, a TAB
15059 for instance. This is easier than to set up the iterator
15060 exactly, and it's not a frequent case, so the additional
15061 effort wouldn't really pay off. */
15062 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15063 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15064 && last_unchanged_at_beg_row > w->current_matrix->rows)
15065 --last_unchanged_at_beg_row;
15066
15067 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15068 GIVE_UP (17);
15069
15070 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15071 GIVE_UP (18);
15072 start_pos = it.current.pos;
15073
15074 /* Start displaying new lines in the desired matrix at the same
15075 vpos we would use in the current matrix, i.e. below
15076 last_unchanged_at_beg_row. */
15077 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15078 current_matrix);
15079 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15080 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15081
15082 xassert (it.hpos == 0 && it.current_x == 0);
15083 }
15084 else
15085 {
15086 /* There are no reusable lines at the start of the window.
15087 Start displaying in the first text line. */
15088 start_display (&it, w, start);
15089 it.vpos = it.first_vpos;
15090 start_pos = it.current.pos;
15091 }
15092
15093 /* Find the first row that is not affected by changes at the end of
15094 the buffer. Value will be null if there is no unchanged row, in
15095 which case we must redisplay to the end of the window. delta
15096 will be set to the value by which buffer positions beginning with
15097 first_unchanged_at_end_row have to be adjusted due to text
15098 changes. */
15099 first_unchanged_at_end_row
15100 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15101 IF_DEBUG (debug_delta = delta);
15102 IF_DEBUG (debug_delta_bytes = delta_bytes);
15103
15104 /* Set stop_pos to the buffer position up to which we will have to
15105 display new lines. If first_unchanged_at_end_row != NULL, this
15106 is the buffer position of the start of the line displayed in that
15107 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15108 that we don't stop at a buffer position. */
15109 stop_pos = 0;
15110 if (first_unchanged_at_end_row)
15111 {
15112 xassert (last_unchanged_at_beg_row == NULL
15113 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15114
15115 /* If this is a continuation line, move forward to the next one
15116 that isn't. Changes in lines above affect this line.
15117 Caution: this may move first_unchanged_at_end_row to a row
15118 not displaying text. */
15119 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15120 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15121 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15122 < it.last_visible_y))
15123 ++first_unchanged_at_end_row;
15124
15125 if (!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 = NULL;
15129 else
15130 {
15131 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15132 + delta);
15133 first_unchanged_at_end_vpos
15134 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15135 xassert (stop_pos >= Z - END_UNCHANGED);
15136 }
15137 }
15138 else if (last_unchanged_at_beg_row == NULL)
15139 GIVE_UP (19);
15140
15141
15142 #if GLYPH_DEBUG
15143
15144 /* Either there is no unchanged row at the end, or the one we have
15145 now displays text. This is a necessary condition for the window
15146 end pos calculation at the end of this function. */
15147 xassert (first_unchanged_at_end_row == NULL
15148 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15149
15150 debug_last_unchanged_at_beg_vpos
15151 = (last_unchanged_at_beg_row
15152 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15153 : -1);
15154 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15155
15156 #endif /* GLYPH_DEBUG != 0 */
15157
15158
15159 /* Display new lines. Set last_text_row to the last new line
15160 displayed which has text on it, i.e. might end up as being the
15161 line where the window_end_vpos is. */
15162 w->cursor.vpos = -1;
15163 last_text_row = NULL;
15164 overlay_arrow_seen = 0;
15165 while (it.current_y < it.last_visible_y
15166 && !fonts_changed_p
15167 && (first_unchanged_at_end_row == NULL
15168 || IT_CHARPOS (it) < stop_pos))
15169 {
15170 if (display_line (&it))
15171 last_text_row = it.glyph_row - 1;
15172 }
15173
15174 if (fonts_changed_p)
15175 return -1;
15176
15177
15178 /* Compute differences in buffer positions, y-positions etc. for
15179 lines reused at the bottom of the window. Compute what we can
15180 scroll. */
15181 if (first_unchanged_at_end_row
15182 /* No lines reused because we displayed everything up to the
15183 bottom of the window. */
15184 && it.current_y < it.last_visible_y)
15185 {
15186 dvpos = (it.vpos
15187 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15188 current_matrix));
15189 dy = it.current_y - first_unchanged_at_end_row->y;
15190 run.current_y = first_unchanged_at_end_row->y;
15191 run.desired_y = run.current_y + dy;
15192 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15193 }
15194 else
15195 {
15196 delta = delta_bytes = dvpos = dy
15197 = run.current_y = run.desired_y = run.height = 0;
15198 first_unchanged_at_end_row = NULL;
15199 }
15200 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15201
15202
15203 /* Find the cursor if not already found. We have to decide whether
15204 PT will appear on this window (it sometimes doesn't, but this is
15205 not a very frequent case.) This decision has to be made before
15206 the current matrix is altered. A value of cursor.vpos < 0 means
15207 that PT is either in one of the lines beginning at
15208 first_unchanged_at_end_row or below the window. Don't care for
15209 lines that might be displayed later at the window end; as
15210 mentioned, this is not a frequent case. */
15211 if (w->cursor.vpos < 0)
15212 {
15213 /* Cursor in unchanged rows at the top? */
15214 if (PT < CHARPOS (start_pos)
15215 && last_unchanged_at_beg_row)
15216 {
15217 row = row_containing_pos (w, PT,
15218 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15219 last_unchanged_at_beg_row + 1, 0);
15220 if (row)
15221 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15222 }
15223
15224 /* Start from first_unchanged_at_end_row looking for PT. */
15225 else if (first_unchanged_at_end_row)
15226 {
15227 row = row_containing_pos (w, PT - delta,
15228 first_unchanged_at_end_row, NULL, 0);
15229 if (row)
15230 set_cursor_from_row (w, row, w->current_matrix, delta,
15231 delta_bytes, dy, dvpos);
15232 }
15233
15234 /* Give up if cursor was not found. */
15235 if (w->cursor.vpos < 0)
15236 {
15237 clear_glyph_matrix (w->desired_matrix);
15238 return -1;
15239 }
15240 }
15241
15242 /* Don't let the cursor end in the scroll margins. */
15243 {
15244 int this_scroll_margin, cursor_height;
15245
15246 this_scroll_margin = max (0, scroll_margin);
15247 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15248 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15249 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15250
15251 if ((w->cursor.y < this_scroll_margin
15252 && CHARPOS (start) > BEGV)
15253 /* Old redisplay didn't take scroll margin into account at the bottom,
15254 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15255 || (w->cursor.y + (make_cursor_line_fully_visible_p
15256 ? cursor_height + this_scroll_margin
15257 : 1)) > it.last_visible_y)
15258 {
15259 w->cursor.vpos = -1;
15260 clear_glyph_matrix (w->desired_matrix);
15261 return -1;
15262 }
15263 }
15264
15265 /* Scroll the display. Do it before changing the current matrix so
15266 that xterm.c doesn't get confused about where the cursor glyph is
15267 found. */
15268 if (dy && run.height)
15269 {
15270 update_begin (f);
15271
15272 if (FRAME_WINDOW_P (f))
15273 {
15274 FRAME_RIF (f)->update_window_begin_hook (w);
15275 FRAME_RIF (f)->clear_window_mouse_face (w);
15276 FRAME_RIF (f)->scroll_run_hook (w, &run);
15277 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15278 }
15279 else
15280 {
15281 /* Terminal frame. In this case, dvpos gives the number of
15282 lines to scroll by; dvpos < 0 means scroll up. */
15283 int first_unchanged_at_end_vpos
15284 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15285 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15286 int end = (WINDOW_TOP_EDGE_LINE (w)
15287 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15288 + window_internal_height (w));
15289
15290 /* Perform the operation on the screen. */
15291 if (dvpos > 0)
15292 {
15293 /* Scroll last_unchanged_at_beg_row to the end of the
15294 window down dvpos lines. */
15295 set_terminal_window (f, end);
15296
15297 /* On dumb terminals delete dvpos lines at the end
15298 before inserting dvpos empty lines. */
15299 if (!FRAME_SCROLL_REGION_OK (f))
15300 ins_del_lines (f, end - dvpos, -dvpos);
15301
15302 /* Insert dvpos empty lines in front of
15303 last_unchanged_at_beg_row. */
15304 ins_del_lines (f, from, dvpos);
15305 }
15306 else if (dvpos < 0)
15307 {
15308 /* Scroll up last_unchanged_at_beg_vpos to the end of
15309 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15310 set_terminal_window (f, end);
15311
15312 /* Delete dvpos lines in front of
15313 last_unchanged_at_beg_vpos. ins_del_lines will set
15314 the cursor to the given vpos and emit |dvpos| delete
15315 line sequences. */
15316 ins_del_lines (f, from + dvpos, dvpos);
15317
15318 /* On a dumb terminal insert dvpos empty lines at the
15319 end. */
15320 if (!FRAME_SCROLL_REGION_OK (f))
15321 ins_del_lines (f, end + dvpos, -dvpos);
15322 }
15323
15324 set_terminal_window (f, 0);
15325 }
15326
15327 update_end (f);
15328 }
15329
15330 /* Shift reused rows of the current matrix to the right position.
15331 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15332 text. */
15333 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15334 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15335 if (dvpos < 0)
15336 {
15337 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15338 bottom_vpos, dvpos);
15339 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15340 bottom_vpos, 0);
15341 }
15342 else if (dvpos > 0)
15343 {
15344 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15345 bottom_vpos, dvpos);
15346 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15347 first_unchanged_at_end_vpos + dvpos, 0);
15348 }
15349
15350 /* For frame-based redisplay, make sure that current frame and window
15351 matrix are in sync with respect to glyph memory. */
15352 if (!FRAME_WINDOW_P (f))
15353 sync_frame_with_window_matrix_rows (w);
15354
15355 /* Adjust buffer positions in reused rows. */
15356 if (delta || delta_bytes)
15357 increment_matrix_positions (current_matrix,
15358 first_unchanged_at_end_vpos + dvpos,
15359 bottom_vpos, delta, delta_bytes);
15360
15361 /* Adjust Y positions. */
15362 if (dy)
15363 shift_glyph_matrix (w, current_matrix,
15364 first_unchanged_at_end_vpos + dvpos,
15365 bottom_vpos, dy);
15366
15367 if (first_unchanged_at_end_row)
15368 {
15369 first_unchanged_at_end_row += dvpos;
15370 if (first_unchanged_at_end_row->y >= it.last_visible_y
15371 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15372 first_unchanged_at_end_row = NULL;
15373 }
15374
15375 /* If scrolling up, there may be some lines to display at the end of
15376 the window. */
15377 last_text_row_at_end = NULL;
15378 if (dy < 0)
15379 {
15380 /* Scrolling up can leave for example a partially visible line
15381 at the end of the window to be redisplayed. */
15382 /* Set last_row to the glyph row in the current matrix where the
15383 window end line is found. It has been moved up or down in
15384 the matrix by dvpos. */
15385 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15386 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15387
15388 /* If last_row is the window end line, it should display text. */
15389 xassert (last_row->displays_text_p);
15390
15391 /* If window end line was partially visible before, begin
15392 displaying at that line. Otherwise begin displaying with the
15393 line following it. */
15394 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15395 {
15396 init_to_row_start (&it, w, last_row);
15397 it.vpos = last_vpos;
15398 it.current_y = last_row->y;
15399 }
15400 else
15401 {
15402 init_to_row_end (&it, w, last_row);
15403 it.vpos = 1 + last_vpos;
15404 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15405 ++last_row;
15406 }
15407
15408 /* We may start in a continuation line. If so, we have to
15409 get the right continuation_lines_width and current_x. */
15410 it.continuation_lines_width = last_row->continuation_lines_width;
15411 it.hpos = it.current_x = 0;
15412
15413 /* Display the rest of the lines at the window end. */
15414 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15415 while (it.current_y < it.last_visible_y
15416 && !fonts_changed_p)
15417 {
15418 /* Is it always sure that the display agrees with lines in
15419 the current matrix? I don't think so, so we mark rows
15420 displayed invalid in the current matrix by setting their
15421 enabled_p flag to zero. */
15422 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15423 if (display_line (&it))
15424 last_text_row_at_end = it.glyph_row - 1;
15425 }
15426 }
15427
15428 /* Update window_end_pos and window_end_vpos. */
15429 if (first_unchanged_at_end_row
15430 && !last_text_row_at_end)
15431 {
15432 /* Window end line if one of the preserved rows from the current
15433 matrix. Set row to the last row displaying text in current
15434 matrix starting at first_unchanged_at_end_row, after
15435 scrolling. */
15436 xassert (first_unchanged_at_end_row->displays_text_p);
15437 row = find_last_row_displaying_text (w->current_matrix, &it,
15438 first_unchanged_at_end_row);
15439 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15440
15441 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15442 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15443 w->window_end_vpos
15444 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15445 xassert (w->window_end_bytepos >= 0);
15446 IF_DEBUG (debug_method_add (w, "A"));
15447 }
15448 else if (last_text_row_at_end)
15449 {
15450 w->window_end_pos
15451 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15452 w->window_end_bytepos
15453 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15454 w->window_end_vpos
15455 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15456 xassert (w->window_end_bytepos >= 0);
15457 IF_DEBUG (debug_method_add (w, "B"));
15458 }
15459 else if (last_text_row)
15460 {
15461 /* We have displayed either to the end of the window or at the
15462 end of the window, i.e. the last row with text is to be found
15463 in the desired matrix. */
15464 w->window_end_pos
15465 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15466 w->window_end_bytepos
15467 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15468 w->window_end_vpos
15469 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15470 xassert (w->window_end_bytepos >= 0);
15471 }
15472 else if (first_unchanged_at_end_row == NULL
15473 && last_text_row == NULL
15474 && last_text_row_at_end == NULL)
15475 {
15476 /* Displayed to end of window, but no line containing text was
15477 displayed. Lines were deleted at the end of the window. */
15478 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15479 int vpos = XFASTINT (w->window_end_vpos);
15480 struct glyph_row *current_row = current_matrix->rows + vpos;
15481 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15482
15483 for (row = NULL;
15484 row == NULL && vpos >= first_vpos;
15485 --vpos, --current_row, --desired_row)
15486 {
15487 if (desired_row->enabled_p)
15488 {
15489 if (desired_row->displays_text_p)
15490 row = desired_row;
15491 }
15492 else if (current_row->displays_text_p)
15493 row = current_row;
15494 }
15495
15496 xassert (row != NULL);
15497 w->window_end_vpos = make_number (vpos + 1);
15498 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15499 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15500 xassert (w->window_end_bytepos >= 0);
15501 IF_DEBUG (debug_method_add (w, "C"));
15502 }
15503 else
15504 abort ();
15505
15506 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15507 debug_end_vpos = XFASTINT (w->window_end_vpos));
15508
15509 /* Record that display has not been completed. */
15510 w->window_end_valid = Qnil;
15511 w->desired_matrix->no_scrolling_p = 1;
15512 return 3;
15513
15514 #undef GIVE_UP
15515 }
15516
15517
15518 \f
15519 /***********************************************************************
15520 More debugging support
15521 ***********************************************************************/
15522
15523 #if GLYPH_DEBUG
15524
15525 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15526 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15527 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15528
15529
15530 /* Dump the contents of glyph matrix MATRIX on stderr.
15531
15532 GLYPHS 0 means don't show glyph contents.
15533 GLYPHS 1 means show glyphs in short form
15534 GLYPHS > 1 means show glyphs in long form. */
15535
15536 void
15537 dump_glyph_matrix (matrix, glyphs)
15538 struct glyph_matrix *matrix;
15539 int glyphs;
15540 {
15541 int i;
15542 for (i = 0; i < matrix->nrows; ++i)
15543 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15544 }
15545
15546
15547 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15548 the glyph row and area where the glyph comes from. */
15549
15550 void
15551 dump_glyph (row, glyph, area)
15552 struct glyph_row *row;
15553 struct glyph *glyph;
15554 int area;
15555 {
15556 if (glyph->type == CHAR_GLYPH)
15557 {
15558 fprintf (stderr,
15559 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15560 glyph - row->glyphs[TEXT_AREA],
15561 'C',
15562 glyph->charpos,
15563 (BUFFERP (glyph->object)
15564 ? 'B'
15565 : (STRINGP (glyph->object)
15566 ? 'S'
15567 : '-')),
15568 glyph->pixel_width,
15569 glyph->u.ch,
15570 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15571 ? glyph->u.ch
15572 : '.'),
15573 glyph->face_id,
15574 glyph->left_box_line_p,
15575 glyph->right_box_line_p);
15576 }
15577 else if (glyph->type == STRETCH_GLYPH)
15578 {
15579 fprintf (stderr,
15580 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15581 glyph - row->glyphs[TEXT_AREA],
15582 'S',
15583 glyph->charpos,
15584 (BUFFERP (glyph->object)
15585 ? 'B'
15586 : (STRINGP (glyph->object)
15587 ? 'S'
15588 : '-')),
15589 glyph->pixel_width,
15590 0,
15591 '.',
15592 glyph->face_id,
15593 glyph->left_box_line_p,
15594 glyph->right_box_line_p);
15595 }
15596 else if (glyph->type == IMAGE_GLYPH)
15597 {
15598 fprintf (stderr,
15599 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15600 glyph - row->glyphs[TEXT_AREA],
15601 'I',
15602 glyph->charpos,
15603 (BUFFERP (glyph->object)
15604 ? 'B'
15605 : (STRINGP (glyph->object)
15606 ? 'S'
15607 : '-')),
15608 glyph->pixel_width,
15609 glyph->u.img_id,
15610 '.',
15611 glyph->face_id,
15612 glyph->left_box_line_p,
15613 glyph->right_box_line_p);
15614 }
15615 else if (glyph->type == COMPOSITE_GLYPH)
15616 {
15617 fprintf (stderr,
15618 " %5d %4c %6d %c %3d 0x%05x",
15619 glyph - row->glyphs[TEXT_AREA],
15620 '+',
15621 glyph->charpos,
15622 (BUFFERP (glyph->object)
15623 ? 'B'
15624 : (STRINGP (glyph->object)
15625 ? 'S'
15626 : '-')),
15627 glyph->pixel_width,
15628 glyph->u.cmp.id);
15629 if (glyph->u.cmp.automatic)
15630 fprintf (stderr,
15631 "[%d-%d]",
15632 glyph->u.cmp.from, glyph->u.cmp.to);
15633 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15634 glyph->face_id,
15635 glyph->left_box_line_p,
15636 glyph->right_box_line_p);
15637 }
15638 }
15639
15640
15641 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15642 GLYPHS 0 means don't show glyph contents.
15643 GLYPHS 1 means show glyphs in short form
15644 GLYPHS > 1 means show glyphs in long form. */
15645
15646 void
15647 dump_glyph_row (row, vpos, glyphs)
15648 struct glyph_row *row;
15649 int vpos, glyphs;
15650 {
15651 if (glyphs != 1)
15652 {
15653 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15654 fprintf (stderr, "======================================================================\n");
15655
15656 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15657 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15658 vpos,
15659 MATRIX_ROW_START_CHARPOS (row),
15660 MATRIX_ROW_END_CHARPOS (row),
15661 row->used[TEXT_AREA],
15662 row->contains_overlapping_glyphs_p,
15663 row->enabled_p,
15664 row->truncated_on_left_p,
15665 row->truncated_on_right_p,
15666 row->continued_p,
15667 MATRIX_ROW_CONTINUATION_LINE_P (row),
15668 row->displays_text_p,
15669 row->ends_at_zv_p,
15670 row->fill_line_p,
15671 row->ends_in_middle_of_char_p,
15672 row->starts_in_middle_of_char_p,
15673 row->mouse_face_p,
15674 row->x,
15675 row->y,
15676 row->pixel_width,
15677 row->height,
15678 row->visible_height,
15679 row->ascent,
15680 row->phys_ascent);
15681 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15682 row->end.overlay_string_index,
15683 row->continuation_lines_width);
15684 fprintf (stderr, "%9d %5d\n",
15685 CHARPOS (row->start.string_pos),
15686 CHARPOS (row->end.string_pos));
15687 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15688 row->end.dpvec_index);
15689 }
15690
15691 if (glyphs > 1)
15692 {
15693 int area;
15694
15695 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15696 {
15697 struct glyph *glyph = row->glyphs[area];
15698 struct glyph *glyph_end = glyph + row->used[area];
15699
15700 /* Glyph for a line end in text. */
15701 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15702 ++glyph_end;
15703
15704 if (glyph < glyph_end)
15705 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15706
15707 for (; glyph < glyph_end; ++glyph)
15708 dump_glyph (row, glyph, area);
15709 }
15710 }
15711 else if (glyphs == 1)
15712 {
15713 int area;
15714
15715 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15716 {
15717 char *s = (char *) alloca (row->used[area] + 1);
15718 int i;
15719
15720 for (i = 0; i < row->used[area]; ++i)
15721 {
15722 struct glyph *glyph = row->glyphs[area] + i;
15723 if (glyph->type == CHAR_GLYPH
15724 && glyph->u.ch < 0x80
15725 && glyph->u.ch >= ' ')
15726 s[i] = glyph->u.ch;
15727 else
15728 s[i] = '.';
15729 }
15730
15731 s[i] = '\0';
15732 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15733 }
15734 }
15735 }
15736
15737
15738 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15739 Sdump_glyph_matrix, 0, 1, "p",
15740 doc: /* Dump the current matrix of the selected window to stderr.
15741 Shows contents of glyph row structures. With non-nil
15742 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15743 glyphs in short form, otherwise show glyphs in long form. */)
15744 (glyphs)
15745 Lisp_Object glyphs;
15746 {
15747 struct window *w = XWINDOW (selected_window);
15748 struct buffer *buffer = XBUFFER (w->buffer);
15749
15750 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15751 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15752 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15753 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15754 fprintf (stderr, "=============================================\n");
15755 dump_glyph_matrix (w->current_matrix,
15756 NILP (glyphs) ? 0 : XINT (glyphs));
15757 return Qnil;
15758 }
15759
15760
15761 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15762 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15763 ()
15764 {
15765 struct frame *f = XFRAME (selected_frame);
15766 dump_glyph_matrix (f->current_matrix, 1);
15767 return Qnil;
15768 }
15769
15770
15771 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15772 doc: /* Dump glyph row ROW to stderr.
15773 GLYPH 0 means don't dump glyphs.
15774 GLYPH 1 means dump glyphs in short form.
15775 GLYPH > 1 or omitted means dump glyphs in long form. */)
15776 (row, glyphs)
15777 Lisp_Object row, glyphs;
15778 {
15779 struct glyph_matrix *matrix;
15780 int vpos;
15781
15782 CHECK_NUMBER (row);
15783 matrix = XWINDOW (selected_window)->current_matrix;
15784 vpos = XINT (row);
15785 if (vpos >= 0 && vpos < matrix->nrows)
15786 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15787 vpos,
15788 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15789 return Qnil;
15790 }
15791
15792
15793 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15794 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15795 GLYPH 0 means don't dump glyphs.
15796 GLYPH 1 means dump glyphs in short form.
15797 GLYPH > 1 or omitted means dump glyphs in long form. */)
15798 (row, glyphs)
15799 Lisp_Object row, glyphs;
15800 {
15801 struct frame *sf = SELECTED_FRAME ();
15802 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15803 int vpos;
15804
15805 CHECK_NUMBER (row);
15806 vpos = XINT (row);
15807 if (vpos >= 0 && vpos < m->nrows)
15808 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15809 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15810 return Qnil;
15811 }
15812
15813
15814 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15815 doc: /* Toggle tracing of redisplay.
15816 With ARG, turn tracing on if and only if ARG is positive. */)
15817 (arg)
15818 Lisp_Object arg;
15819 {
15820 if (NILP (arg))
15821 trace_redisplay_p = !trace_redisplay_p;
15822 else
15823 {
15824 arg = Fprefix_numeric_value (arg);
15825 trace_redisplay_p = XINT (arg) > 0;
15826 }
15827
15828 return Qnil;
15829 }
15830
15831
15832 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15833 doc: /* Like `format', but print result to stderr.
15834 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15835 (nargs, args)
15836 int nargs;
15837 Lisp_Object *args;
15838 {
15839 Lisp_Object s = Fformat (nargs, args);
15840 fprintf (stderr, "%s", SDATA (s));
15841 return Qnil;
15842 }
15843
15844 #endif /* GLYPH_DEBUG */
15845
15846
15847 \f
15848 /***********************************************************************
15849 Building Desired Matrix Rows
15850 ***********************************************************************/
15851
15852 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15853 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15854
15855 static struct glyph_row *
15856 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15857 struct window *w;
15858 Lisp_Object overlay_arrow_string;
15859 {
15860 struct frame *f = XFRAME (WINDOW_FRAME (w));
15861 struct buffer *buffer = XBUFFER (w->buffer);
15862 struct buffer *old = current_buffer;
15863 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15864 int arrow_len = SCHARS (overlay_arrow_string);
15865 const unsigned char *arrow_end = arrow_string + arrow_len;
15866 const unsigned char *p;
15867 struct it it;
15868 int multibyte_p;
15869 int n_glyphs_before;
15870
15871 set_buffer_temp (buffer);
15872 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15873 it.glyph_row->used[TEXT_AREA] = 0;
15874 SET_TEXT_POS (it.position, 0, 0);
15875
15876 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15877 p = arrow_string;
15878 while (p < arrow_end)
15879 {
15880 Lisp_Object face, ilisp;
15881
15882 /* Get the next character. */
15883 if (multibyte_p)
15884 it.c = it.char_to_display = string_char_and_length (p, &it.len);
15885 else
15886 {
15887 it.c = it.char_to_display = *p, it.len = 1;
15888 if (! ASCII_CHAR_P (it.c))
15889 it.char_to_display = BYTE8_TO_CHAR (it.c);
15890 }
15891 p += it.len;
15892
15893 /* Get its face. */
15894 ilisp = make_number (p - arrow_string);
15895 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15896 it.face_id = compute_char_face (f, it.char_to_display, face);
15897
15898 /* Compute its width, get its glyphs. */
15899 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15900 SET_TEXT_POS (it.position, -1, -1);
15901 PRODUCE_GLYPHS (&it);
15902
15903 /* If this character doesn't fit any more in the line, we have
15904 to remove some glyphs. */
15905 if (it.current_x > it.last_visible_x)
15906 {
15907 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15908 break;
15909 }
15910 }
15911
15912 set_buffer_temp (old);
15913 return it.glyph_row;
15914 }
15915
15916
15917 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15918 glyphs are only inserted for terminal frames since we can't really
15919 win with truncation glyphs when partially visible glyphs are
15920 involved. Which glyphs to insert is determined by
15921 produce_special_glyphs. */
15922
15923 static void
15924 insert_left_trunc_glyphs (it)
15925 struct it *it;
15926 {
15927 struct it truncate_it;
15928 struct glyph *from, *end, *to, *toend;
15929
15930 xassert (!FRAME_WINDOW_P (it->f));
15931
15932 /* Get the truncation glyphs. */
15933 truncate_it = *it;
15934 truncate_it.current_x = 0;
15935 truncate_it.face_id = DEFAULT_FACE_ID;
15936 truncate_it.glyph_row = &scratch_glyph_row;
15937 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15938 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15939 truncate_it.object = make_number (0);
15940 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15941
15942 /* Overwrite glyphs from IT with truncation glyphs. */
15943 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15944 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15945 to = it->glyph_row->glyphs[TEXT_AREA];
15946 toend = to + it->glyph_row->used[TEXT_AREA];
15947
15948 while (from < end)
15949 *to++ = *from++;
15950
15951 /* There may be padding glyphs left over. Overwrite them too. */
15952 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15953 {
15954 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15955 while (from < end)
15956 *to++ = *from++;
15957 }
15958
15959 if (to > toend)
15960 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15961 }
15962
15963
15964 /* Compute the pixel height and width of IT->glyph_row.
15965
15966 Most of the time, ascent and height of a display line will be equal
15967 to the max_ascent and max_height values of the display iterator
15968 structure. This is not the case if
15969
15970 1. We hit ZV without displaying anything. In this case, max_ascent
15971 and max_height will be zero.
15972
15973 2. We have some glyphs that don't contribute to the line height.
15974 (The glyph row flag contributes_to_line_height_p is for future
15975 pixmap extensions).
15976
15977 The first case is easily covered by using default values because in
15978 these cases, the line height does not really matter, except that it
15979 must not be zero. */
15980
15981 static void
15982 compute_line_metrics (it)
15983 struct it *it;
15984 {
15985 struct glyph_row *row = it->glyph_row;
15986 int area, i;
15987
15988 if (FRAME_WINDOW_P (it->f))
15989 {
15990 int i, min_y, max_y;
15991
15992 /* The line may consist of one space only, that was added to
15993 place the cursor on it. If so, the row's height hasn't been
15994 computed yet. */
15995 if (row->height == 0)
15996 {
15997 if (it->max_ascent + it->max_descent == 0)
15998 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15999 row->ascent = it->max_ascent;
16000 row->height = it->max_ascent + it->max_descent;
16001 row->phys_ascent = it->max_phys_ascent;
16002 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16003 row->extra_line_spacing = it->max_extra_line_spacing;
16004 }
16005
16006 /* Compute the width of this line. */
16007 row->pixel_width = row->x;
16008 for (i = 0; i < row->used[TEXT_AREA]; ++i)
16009 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
16010
16011 xassert (row->pixel_width >= 0);
16012 xassert (row->ascent >= 0 && row->height > 0);
16013
16014 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
16015 || MATRIX_ROW_OVERLAPS_PRED_P (row));
16016
16017 /* If first line's physical ascent is larger than its logical
16018 ascent, use the physical ascent, and make the row taller.
16019 This makes accented characters fully visible. */
16020 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16021 && row->phys_ascent > row->ascent)
16022 {
16023 row->height += row->phys_ascent - row->ascent;
16024 row->ascent = row->phys_ascent;
16025 }
16026
16027 /* Compute how much of the line is visible. */
16028 row->visible_height = row->height;
16029
16030 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16031 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16032
16033 if (row->y < min_y)
16034 row->visible_height -= min_y - row->y;
16035 if (row->y + row->height > max_y)
16036 row->visible_height -= row->y + row->height - max_y;
16037 }
16038 else
16039 {
16040 row->pixel_width = row->used[TEXT_AREA];
16041 if (row->continued_p)
16042 row->pixel_width -= it->continuation_pixel_width;
16043 else if (row->truncated_on_right_p)
16044 row->pixel_width -= it->truncation_pixel_width;
16045 row->ascent = row->phys_ascent = 0;
16046 row->height = row->phys_height = row->visible_height = 1;
16047 row->extra_line_spacing = 0;
16048 }
16049
16050 /* Compute a hash code for this row. */
16051 row->hash = 0;
16052 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16053 for (i = 0; i < row->used[area]; ++i)
16054 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16055 + row->glyphs[area][i].u.val
16056 + row->glyphs[area][i].face_id
16057 + row->glyphs[area][i].padding_p
16058 + (row->glyphs[area][i].type << 2));
16059
16060 it->max_ascent = it->max_descent = 0;
16061 it->max_phys_ascent = it->max_phys_descent = 0;
16062 }
16063
16064
16065 /* Append one space to the glyph row of iterator IT if doing a
16066 window-based redisplay. The space has the same face as
16067 IT->face_id. Value is non-zero if a space was added.
16068
16069 This function is called to make sure that there is always one glyph
16070 at the end of a glyph row that the cursor can be set on under
16071 window-systems. (If there weren't such a glyph we would not know
16072 how wide and tall a box cursor should be displayed).
16073
16074 At the same time this space let's a nicely handle clearing to the
16075 end of the line if the row ends in italic text. */
16076
16077 static int
16078 append_space_for_newline (it, default_face_p)
16079 struct it *it;
16080 int default_face_p;
16081 {
16082 if (FRAME_WINDOW_P (it->f))
16083 {
16084 int n = it->glyph_row->used[TEXT_AREA];
16085
16086 if (it->glyph_row->glyphs[TEXT_AREA] + n
16087 < it->glyph_row->glyphs[1 + TEXT_AREA])
16088 {
16089 /* Save some values that must not be changed.
16090 Must save IT->c and IT->len because otherwise
16091 ITERATOR_AT_END_P wouldn't work anymore after
16092 append_space_for_newline has been called. */
16093 enum display_element_type saved_what = it->what;
16094 int saved_c = it->c, saved_len = it->len;
16095 int saved_char_to_display = it->char_to_display;
16096 int saved_x = it->current_x;
16097 int saved_face_id = it->face_id;
16098 struct text_pos saved_pos;
16099 Lisp_Object saved_object;
16100 struct face *face;
16101
16102 saved_object = it->object;
16103 saved_pos = it->position;
16104
16105 it->what = IT_CHARACTER;
16106 bzero (&it->position, sizeof it->position);
16107 it->object = make_number (0);
16108 it->c = it->char_to_display = ' ';
16109 it->len = 1;
16110
16111 if (default_face_p)
16112 it->face_id = DEFAULT_FACE_ID;
16113 else if (it->face_before_selective_p)
16114 it->face_id = it->saved_face_id;
16115 face = FACE_FROM_ID (it->f, it->face_id);
16116 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16117
16118 PRODUCE_GLYPHS (it);
16119
16120 it->override_ascent = -1;
16121 it->constrain_row_ascent_descent_p = 0;
16122 it->current_x = saved_x;
16123 it->object = saved_object;
16124 it->position = saved_pos;
16125 it->what = saved_what;
16126 it->face_id = saved_face_id;
16127 it->len = saved_len;
16128 it->c = saved_c;
16129 it->char_to_display = saved_char_to_display;
16130 return 1;
16131 }
16132 }
16133
16134 return 0;
16135 }
16136
16137
16138 /* Extend the face of the last glyph in the text area of IT->glyph_row
16139 to the end of the display line. Called from display_line.
16140 If the glyph row is empty, add a space glyph to it so that we
16141 know the face to draw. Set the glyph row flag fill_line_p. */
16142
16143 static void
16144 extend_face_to_end_of_line (it)
16145 struct it *it;
16146 {
16147 struct face *face;
16148 struct frame *f = it->f;
16149
16150 /* If line is already filled, do nothing. */
16151 if (it->current_x >= it->last_visible_x)
16152 return;
16153
16154 /* Face extension extends the background and box of IT->face_id
16155 to the end of the line. If the background equals the background
16156 of the frame, we don't have to do anything. */
16157 if (it->face_before_selective_p)
16158 face = FACE_FROM_ID (it->f, it->saved_face_id);
16159 else
16160 face = FACE_FROM_ID (f, it->face_id);
16161
16162 if (FRAME_WINDOW_P (f)
16163 && it->glyph_row->displays_text_p
16164 && face->box == FACE_NO_BOX
16165 && face->background == FRAME_BACKGROUND_PIXEL (f)
16166 && !face->stipple)
16167 return;
16168
16169 /* Set the glyph row flag indicating that the face of the last glyph
16170 in the text area has to be drawn to the end of the text area. */
16171 it->glyph_row->fill_line_p = 1;
16172
16173 /* If current character of IT is not ASCII, make sure we have the
16174 ASCII face. This will be automatically undone the next time
16175 get_next_display_element returns a multibyte character. Note
16176 that the character will always be single byte in unibyte
16177 text. */
16178 if (!ASCII_CHAR_P (it->c))
16179 {
16180 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16181 }
16182
16183 if (FRAME_WINDOW_P (f))
16184 {
16185 /* If the row is empty, add a space with the current face of IT,
16186 so that we know which face to draw. */
16187 if (it->glyph_row->used[TEXT_AREA] == 0)
16188 {
16189 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16190 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16191 it->glyph_row->used[TEXT_AREA] = 1;
16192 }
16193 }
16194 else
16195 {
16196 /* Save some values that must not be changed. */
16197 int saved_x = it->current_x;
16198 struct text_pos saved_pos;
16199 Lisp_Object saved_object;
16200 enum display_element_type saved_what = it->what;
16201 int saved_face_id = it->face_id;
16202
16203 saved_object = it->object;
16204 saved_pos = it->position;
16205
16206 it->what = IT_CHARACTER;
16207 bzero (&it->position, sizeof it->position);
16208 it->object = make_number (0);
16209 it->c = it->char_to_display = ' ';
16210 it->len = 1;
16211 it->face_id = face->id;
16212
16213 PRODUCE_GLYPHS (it);
16214
16215 while (it->current_x <= it->last_visible_x)
16216 PRODUCE_GLYPHS (it);
16217
16218 /* Don't count these blanks really. It would let us insert a left
16219 truncation glyph below and make us set the cursor on them, maybe. */
16220 it->current_x = saved_x;
16221 it->object = saved_object;
16222 it->position = saved_pos;
16223 it->what = saved_what;
16224 it->face_id = saved_face_id;
16225 }
16226 }
16227
16228
16229 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16230 trailing whitespace. */
16231
16232 static int
16233 trailing_whitespace_p (charpos)
16234 int charpos;
16235 {
16236 int bytepos = CHAR_TO_BYTE (charpos);
16237 int c = 0;
16238
16239 while (bytepos < ZV_BYTE
16240 && (c = FETCH_CHAR (bytepos),
16241 c == ' ' || c == '\t'))
16242 ++bytepos;
16243
16244 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16245 {
16246 if (bytepos != PT_BYTE)
16247 return 1;
16248 }
16249 return 0;
16250 }
16251
16252
16253 /* Highlight trailing whitespace, if any, in ROW. */
16254
16255 void
16256 highlight_trailing_whitespace (f, row)
16257 struct frame *f;
16258 struct glyph_row *row;
16259 {
16260 int used = row->used[TEXT_AREA];
16261
16262 if (used)
16263 {
16264 struct glyph *start = row->glyphs[TEXT_AREA];
16265 struct glyph *glyph = start + used - 1;
16266
16267 /* Skip over glyphs inserted to display the cursor at the
16268 end of a line, for extending the face of the last glyph
16269 to the end of the line on terminals, and for truncation
16270 and continuation glyphs. */
16271 while (glyph >= start
16272 && glyph->type == CHAR_GLYPH
16273 && INTEGERP (glyph->object))
16274 --glyph;
16275
16276 /* If last glyph is a space or stretch, and it's trailing
16277 whitespace, set the face of all trailing whitespace glyphs in
16278 IT->glyph_row to `trailing-whitespace'. */
16279 if (glyph >= start
16280 && BUFFERP (glyph->object)
16281 && (glyph->type == STRETCH_GLYPH
16282 || (glyph->type == CHAR_GLYPH
16283 && glyph->u.ch == ' '))
16284 && trailing_whitespace_p (glyph->charpos))
16285 {
16286 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16287 if (face_id < 0)
16288 return;
16289
16290 while (glyph >= start
16291 && BUFFERP (glyph->object)
16292 && (glyph->type == STRETCH_GLYPH
16293 || (glyph->type == CHAR_GLYPH
16294 && glyph->u.ch == ' ')))
16295 (glyph--)->face_id = face_id;
16296 }
16297 }
16298 }
16299
16300
16301 /* Value is non-zero if glyph row ROW in window W should be
16302 used to hold the cursor. */
16303
16304 static int
16305 cursor_row_p (w, row)
16306 struct window *w;
16307 struct glyph_row *row;
16308 {
16309 int cursor_row_p = 1;
16310
16311 if (PT == MATRIX_ROW_END_CHARPOS (row))
16312 {
16313 /* Suppose the row ends on a string.
16314 Unless the row is continued, that means it ends on a newline
16315 in the string. If it's anything other than a display string
16316 (e.g. a before-string from an overlay), we don't want the
16317 cursor there. (This heuristic seems to give the optimal
16318 behavior for the various types of multi-line strings.) */
16319 if (CHARPOS (row->end.string_pos) >= 0)
16320 {
16321 if (row->continued_p)
16322 cursor_row_p = 1;
16323 else
16324 {
16325 /* Check for `display' property. */
16326 struct glyph *beg = row->glyphs[TEXT_AREA];
16327 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16328 struct glyph *glyph;
16329
16330 cursor_row_p = 0;
16331 for (glyph = end; glyph >= beg; --glyph)
16332 if (STRINGP (glyph->object))
16333 {
16334 Lisp_Object prop
16335 = Fget_char_property (make_number (PT),
16336 Qdisplay, Qnil);
16337 cursor_row_p =
16338 (!NILP (prop)
16339 && display_prop_string_p (prop, glyph->object));
16340 break;
16341 }
16342 }
16343 }
16344 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16345 {
16346 /* If the row ends in middle of a real character,
16347 and the line is continued, we want the cursor here.
16348 That's because MATRIX_ROW_END_CHARPOS would equal
16349 PT if PT is before the character. */
16350 if (!row->ends_in_ellipsis_p)
16351 cursor_row_p = row->continued_p;
16352 else
16353 /* If the row ends in an ellipsis, then
16354 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16355 We want that position to be displayed after the ellipsis. */
16356 cursor_row_p = 0;
16357 }
16358 /* If the row ends at ZV, display the cursor at the end of that
16359 row instead of at the start of the row below. */
16360 else if (row->ends_at_zv_p)
16361 cursor_row_p = 1;
16362 else
16363 cursor_row_p = 0;
16364 }
16365
16366 return cursor_row_p;
16367 }
16368
16369 \f
16370
16371 /* Push the display property PROP so that it will be rendered at the
16372 current position in IT. Return 1 if PROP was successfully pushed,
16373 0 otherwise. */
16374
16375 static int
16376 push_display_prop (struct it *it, Lisp_Object prop)
16377 {
16378 push_it (it);
16379
16380 if (STRINGP (prop))
16381 {
16382 if (SCHARS (prop) == 0)
16383 {
16384 pop_it (it);
16385 return 0;
16386 }
16387
16388 it->string = prop;
16389 it->multibyte_p = STRING_MULTIBYTE (it->string);
16390 it->current.overlay_string_index = -1;
16391 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16392 it->end_charpos = it->string_nchars = SCHARS (it->string);
16393 it->method = GET_FROM_STRING;
16394 it->stop_charpos = 0;
16395 }
16396 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16397 {
16398 it->method = GET_FROM_STRETCH;
16399 it->object = prop;
16400 }
16401 #ifdef HAVE_WINDOW_SYSTEM
16402 else if (IMAGEP (prop))
16403 {
16404 it->what = IT_IMAGE;
16405 it->image_id = lookup_image (it->f, prop);
16406 it->method = GET_FROM_IMAGE;
16407 }
16408 #endif /* HAVE_WINDOW_SYSTEM */
16409 else
16410 {
16411 pop_it (it); /* bogus display property, give up */
16412 return 0;
16413 }
16414
16415 return 1;
16416 }
16417
16418 /* Return the character-property PROP at the current position in IT. */
16419
16420 static Lisp_Object
16421 get_it_property (it, prop)
16422 struct it *it;
16423 Lisp_Object prop;
16424 {
16425 Lisp_Object position;
16426
16427 if (STRINGP (it->object))
16428 position = make_number (IT_STRING_CHARPOS (*it));
16429 else if (BUFFERP (it->object))
16430 position = make_number (IT_CHARPOS (*it));
16431 else
16432 return Qnil;
16433
16434 return Fget_char_property (position, prop, it->object);
16435 }
16436
16437 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16438
16439 static void
16440 handle_line_prefix (struct it *it)
16441 {
16442 Lisp_Object prefix;
16443 if (it->continuation_lines_width > 0)
16444 {
16445 prefix = get_it_property (it, Qwrap_prefix);
16446 if (NILP (prefix))
16447 prefix = Vwrap_prefix;
16448 }
16449 else
16450 {
16451 prefix = get_it_property (it, Qline_prefix);
16452 if (NILP (prefix))
16453 prefix = Vline_prefix;
16454 }
16455 if (! NILP (prefix) && push_display_prop (it, prefix))
16456 {
16457 /* If the prefix is wider than the window, and we try to wrap
16458 it, it would acquire its own wrap prefix, and so on till the
16459 iterator stack overflows. So, don't wrap the prefix. */
16460 it->line_wrap = TRUNCATE;
16461 it->avoid_cursor_p = 1;
16462 }
16463 }
16464
16465 \f
16466
16467 /* Construct the glyph row IT->glyph_row in the desired matrix of
16468 IT->w from text at the current position of IT. See dispextern.h
16469 for an overview of struct it. Value is non-zero if
16470 IT->glyph_row displays text, as opposed to a line displaying ZV
16471 only. */
16472
16473 static int
16474 display_line (it)
16475 struct it *it;
16476 {
16477 struct glyph_row *row = it->glyph_row;
16478 Lisp_Object overlay_arrow_string;
16479 struct it wrap_it;
16480 int may_wrap = 0, wrap_x;
16481 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16482 int wrap_row_phys_ascent, wrap_row_phys_height;
16483 int wrap_row_extra_line_spacing;
16484
16485 /* We always start displaying at hpos zero even if hscrolled. */
16486 xassert (it->hpos == 0 && it->current_x == 0);
16487
16488 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16489 >= it->w->desired_matrix->nrows)
16490 {
16491 it->w->nrows_scale_factor++;
16492 fonts_changed_p = 1;
16493 return 0;
16494 }
16495
16496 /* Is IT->w showing the region? */
16497 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16498
16499 /* Clear the result glyph row and enable it. */
16500 prepare_desired_row (row);
16501
16502 row->y = it->current_y;
16503 row->start = it->start;
16504 row->continuation_lines_width = it->continuation_lines_width;
16505 row->displays_text_p = 1;
16506 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16507 it->starts_in_middle_of_char_p = 0;
16508
16509 /* Arrange the overlays nicely for our purposes. Usually, we call
16510 display_line on only one line at a time, in which case this
16511 can't really hurt too much, or we call it on lines which appear
16512 one after another in the buffer, in which case all calls to
16513 recenter_overlay_lists but the first will be pretty cheap. */
16514 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16515
16516 /* Move over display elements that are not visible because we are
16517 hscrolled. This may stop at an x-position < IT->first_visible_x
16518 if the first glyph is partially visible or if we hit a line end. */
16519 if (it->current_x < it->first_visible_x)
16520 {
16521 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16522 MOVE_TO_POS | MOVE_TO_X);
16523 }
16524 else
16525 {
16526 /* We only do this when not calling `move_it_in_display_line_to'
16527 above, because move_it_in_display_line_to calls
16528 handle_line_prefix itself. */
16529 handle_line_prefix (it);
16530 }
16531
16532 /* Get the initial row height. This is either the height of the
16533 text hscrolled, if there is any, or zero. */
16534 row->ascent = it->max_ascent;
16535 row->height = it->max_ascent + it->max_descent;
16536 row->phys_ascent = it->max_phys_ascent;
16537 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16538 row->extra_line_spacing = it->max_extra_line_spacing;
16539
16540 /* Loop generating characters. The loop is left with IT on the next
16541 character to display. */
16542 while (1)
16543 {
16544 int n_glyphs_before, hpos_before, x_before;
16545 int x, i, nglyphs;
16546 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16547
16548 /* Retrieve the next thing to display. Value is zero if end of
16549 buffer reached. */
16550 if (!get_next_display_element (it))
16551 {
16552 /* Maybe add a space at the end of this line that is used to
16553 display the cursor there under X. Set the charpos of the
16554 first glyph of blank lines not corresponding to any text
16555 to -1. */
16556 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16557 row->exact_window_width_line_p = 1;
16558 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16559 || row->used[TEXT_AREA] == 0)
16560 {
16561 row->glyphs[TEXT_AREA]->charpos = -1;
16562 row->displays_text_p = 0;
16563
16564 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16565 && (!MINI_WINDOW_P (it->w)
16566 || (minibuf_level && EQ (it->window, minibuf_window))))
16567 row->indicate_empty_line_p = 1;
16568 }
16569
16570 it->continuation_lines_width = 0;
16571 row->ends_at_zv_p = 1;
16572 break;
16573 }
16574
16575 /* Now, get the metrics of what we want to display. This also
16576 generates glyphs in `row' (which is IT->glyph_row). */
16577 n_glyphs_before = row->used[TEXT_AREA];
16578 x = it->current_x;
16579
16580 /* Remember the line height so far in case the next element doesn't
16581 fit on the line. */
16582 if (it->line_wrap != TRUNCATE)
16583 {
16584 ascent = it->max_ascent;
16585 descent = it->max_descent;
16586 phys_ascent = it->max_phys_ascent;
16587 phys_descent = it->max_phys_descent;
16588
16589 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16590 {
16591 if (IT_DISPLAYING_WHITESPACE (it))
16592 may_wrap = 1;
16593 else if (may_wrap)
16594 {
16595 wrap_it = *it;
16596 wrap_x = x;
16597 wrap_row_used = row->used[TEXT_AREA];
16598 wrap_row_ascent = row->ascent;
16599 wrap_row_height = row->height;
16600 wrap_row_phys_ascent = row->phys_ascent;
16601 wrap_row_phys_height = row->phys_height;
16602 wrap_row_extra_line_spacing = row->extra_line_spacing;
16603 may_wrap = 0;
16604 }
16605 }
16606 }
16607
16608 PRODUCE_GLYPHS (it);
16609
16610 /* If this display element was in marginal areas, continue with
16611 the next one. */
16612 if (it->area != TEXT_AREA)
16613 {
16614 row->ascent = max (row->ascent, it->max_ascent);
16615 row->height = max (row->height, it->max_ascent + it->max_descent);
16616 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16617 row->phys_height = max (row->phys_height,
16618 it->max_phys_ascent + it->max_phys_descent);
16619 row->extra_line_spacing = max (row->extra_line_spacing,
16620 it->max_extra_line_spacing);
16621 set_iterator_to_next (it, 1);
16622 continue;
16623 }
16624
16625 /* Does the display element fit on the line? If we truncate
16626 lines, we should draw past the right edge of the window. If
16627 we don't truncate, we want to stop so that we can display the
16628 continuation glyph before the right margin. If lines are
16629 continued, there are two possible strategies for characters
16630 resulting in more than 1 glyph (e.g. tabs): Display as many
16631 glyphs as possible in this line and leave the rest for the
16632 continuation line, or display the whole element in the next
16633 line. Original redisplay did the former, so we do it also. */
16634 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16635 hpos_before = it->hpos;
16636 x_before = x;
16637
16638 if (/* Not a newline. */
16639 nglyphs > 0
16640 /* Glyphs produced fit entirely in the line. */
16641 && it->current_x < it->last_visible_x)
16642 {
16643 it->hpos += nglyphs;
16644 row->ascent = max (row->ascent, it->max_ascent);
16645 row->height = max (row->height, it->max_ascent + it->max_descent);
16646 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16647 row->phys_height = max (row->phys_height,
16648 it->max_phys_ascent + it->max_phys_descent);
16649 row->extra_line_spacing = max (row->extra_line_spacing,
16650 it->max_extra_line_spacing);
16651 if (it->current_x - it->pixel_width < it->first_visible_x)
16652 row->x = x - it->first_visible_x;
16653 }
16654 else
16655 {
16656 int new_x;
16657 struct glyph *glyph;
16658
16659 for (i = 0; i < nglyphs; ++i, x = new_x)
16660 {
16661 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16662 new_x = x + glyph->pixel_width;
16663
16664 if (/* Lines are continued. */
16665 it->line_wrap != TRUNCATE
16666 && (/* Glyph doesn't fit on the line. */
16667 new_x > it->last_visible_x
16668 /* Or it fits exactly on a window system frame. */
16669 || (new_x == it->last_visible_x
16670 && FRAME_WINDOW_P (it->f))))
16671 {
16672 /* End of a continued line. */
16673
16674 if (it->hpos == 0
16675 || (new_x == it->last_visible_x
16676 && FRAME_WINDOW_P (it->f)))
16677 {
16678 /* Current glyph is the only one on the line or
16679 fits exactly on the line. We must continue
16680 the line because we can't draw the cursor
16681 after the glyph. */
16682 row->continued_p = 1;
16683 it->current_x = new_x;
16684 it->continuation_lines_width += new_x;
16685 ++it->hpos;
16686 if (i == nglyphs - 1)
16687 {
16688 /* If line-wrap is on, check if a previous
16689 wrap point was found. */
16690 if (wrap_row_used > 0
16691 /* Even if there is a previous wrap
16692 point, continue the line here as
16693 usual, if (i) the previous character
16694 was a space or tab AND (ii) the
16695 current character is not. */
16696 && (!may_wrap
16697 || IT_DISPLAYING_WHITESPACE (it)))
16698 goto back_to_wrap;
16699
16700 set_iterator_to_next (it, 1);
16701 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16702 {
16703 if (!get_next_display_element (it))
16704 {
16705 row->exact_window_width_line_p = 1;
16706 it->continuation_lines_width = 0;
16707 row->continued_p = 0;
16708 row->ends_at_zv_p = 1;
16709 }
16710 else if (ITERATOR_AT_END_OF_LINE_P (it))
16711 {
16712 row->continued_p = 0;
16713 row->exact_window_width_line_p = 1;
16714 }
16715 }
16716 }
16717 }
16718 else if (CHAR_GLYPH_PADDING_P (*glyph)
16719 && !FRAME_WINDOW_P (it->f))
16720 {
16721 /* A padding glyph that doesn't fit on this line.
16722 This means the whole character doesn't fit
16723 on the line. */
16724 row->used[TEXT_AREA] = n_glyphs_before;
16725
16726 /* Fill the rest of the row with continuation
16727 glyphs like in 20.x. */
16728 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16729 < row->glyphs[1 + TEXT_AREA])
16730 produce_special_glyphs (it, IT_CONTINUATION);
16731
16732 row->continued_p = 1;
16733 it->current_x = x_before;
16734 it->continuation_lines_width += x_before;
16735
16736 /* Restore the height to what it was before the
16737 element not fitting on the line. */
16738 it->max_ascent = ascent;
16739 it->max_descent = descent;
16740 it->max_phys_ascent = phys_ascent;
16741 it->max_phys_descent = phys_descent;
16742 }
16743 else if (wrap_row_used > 0)
16744 {
16745 back_to_wrap:
16746 *it = wrap_it;
16747 it->continuation_lines_width += wrap_x;
16748 row->used[TEXT_AREA] = wrap_row_used;
16749 row->ascent = wrap_row_ascent;
16750 row->height = wrap_row_height;
16751 row->phys_ascent = wrap_row_phys_ascent;
16752 row->phys_height = wrap_row_phys_height;
16753 row->extra_line_spacing = wrap_row_extra_line_spacing;
16754 row->continued_p = 1;
16755 row->ends_at_zv_p = 0;
16756 row->exact_window_width_line_p = 0;
16757 it->continuation_lines_width += x;
16758
16759 /* Make sure that a non-default face is extended
16760 up to the right margin of the window. */
16761 extend_face_to_end_of_line (it);
16762 }
16763 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16764 {
16765 /* A TAB that extends past the right edge of the
16766 window. This produces a single glyph on
16767 window system frames. We leave the glyph in
16768 this row and let it fill the row, but don't
16769 consume the TAB. */
16770 it->continuation_lines_width += it->last_visible_x;
16771 row->ends_in_middle_of_char_p = 1;
16772 row->continued_p = 1;
16773 glyph->pixel_width = it->last_visible_x - x;
16774 it->starts_in_middle_of_char_p = 1;
16775 }
16776 else
16777 {
16778 /* Something other than a TAB that draws past
16779 the right edge of the window. Restore
16780 positions to values before the element. */
16781 row->used[TEXT_AREA] = n_glyphs_before + i;
16782
16783 /* Display continuation glyphs. */
16784 if (!FRAME_WINDOW_P (it->f))
16785 produce_special_glyphs (it, IT_CONTINUATION);
16786 row->continued_p = 1;
16787
16788 it->current_x = x_before;
16789 it->continuation_lines_width += x;
16790 extend_face_to_end_of_line (it);
16791
16792 if (nglyphs > 1 && i > 0)
16793 {
16794 row->ends_in_middle_of_char_p = 1;
16795 it->starts_in_middle_of_char_p = 1;
16796 }
16797
16798 /* Restore the height to what it was before the
16799 element not fitting on the line. */
16800 it->max_ascent = ascent;
16801 it->max_descent = descent;
16802 it->max_phys_ascent = phys_ascent;
16803 it->max_phys_descent = phys_descent;
16804 }
16805
16806 break;
16807 }
16808 else if (new_x > it->first_visible_x)
16809 {
16810 /* Increment number of glyphs actually displayed. */
16811 ++it->hpos;
16812
16813 if (x < it->first_visible_x)
16814 /* Glyph is partially visible, i.e. row starts at
16815 negative X position. */
16816 row->x = x - it->first_visible_x;
16817 }
16818 else
16819 {
16820 /* Glyph is completely off the left margin of the
16821 window. This should not happen because of the
16822 move_it_in_display_line at the start of this
16823 function, unless the text display area of the
16824 window is empty. */
16825 xassert (it->first_visible_x <= it->last_visible_x);
16826 }
16827 }
16828
16829 row->ascent = max (row->ascent, it->max_ascent);
16830 row->height = max (row->height, it->max_ascent + it->max_descent);
16831 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16832 row->phys_height = max (row->phys_height,
16833 it->max_phys_ascent + it->max_phys_descent);
16834 row->extra_line_spacing = max (row->extra_line_spacing,
16835 it->max_extra_line_spacing);
16836
16837 /* End of this display line if row is continued. */
16838 if (row->continued_p || row->ends_at_zv_p)
16839 break;
16840 }
16841
16842 at_end_of_line:
16843 /* Is this a line end? If yes, we're also done, after making
16844 sure that a non-default face is extended up to the right
16845 margin of the window. */
16846 if (ITERATOR_AT_END_OF_LINE_P (it))
16847 {
16848 int used_before = row->used[TEXT_AREA];
16849
16850 row->ends_in_newline_from_string_p = STRINGP (it->object);
16851
16852 /* Add a space at the end of the line that is used to
16853 display the cursor there. */
16854 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16855 append_space_for_newline (it, 0);
16856
16857 /* Extend the face to the end of the line. */
16858 extend_face_to_end_of_line (it);
16859
16860 /* Make sure we have the position. */
16861 if (used_before == 0)
16862 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16863
16864 /* Consume the line end. This skips over invisible lines. */
16865 set_iterator_to_next (it, 1);
16866 it->continuation_lines_width = 0;
16867 break;
16868 }
16869
16870 /* Proceed with next display element. Note that this skips
16871 over lines invisible because of selective display. */
16872 set_iterator_to_next (it, 1);
16873
16874 /* If we truncate lines, we are done when the last displayed
16875 glyphs reach past the right margin of the window. */
16876 if (it->line_wrap == TRUNCATE
16877 && (FRAME_WINDOW_P (it->f)
16878 ? (it->current_x >= it->last_visible_x)
16879 : (it->current_x > it->last_visible_x)))
16880 {
16881 /* Maybe add truncation glyphs. */
16882 if (!FRAME_WINDOW_P (it->f))
16883 {
16884 int i, n;
16885
16886 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16887 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16888 break;
16889
16890 for (n = row->used[TEXT_AREA]; i < n; ++i)
16891 {
16892 row->used[TEXT_AREA] = i;
16893 produce_special_glyphs (it, IT_TRUNCATION);
16894 }
16895 }
16896 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16897 {
16898 /* Don't truncate if we can overflow newline into fringe. */
16899 if (!get_next_display_element (it))
16900 {
16901 it->continuation_lines_width = 0;
16902 row->ends_at_zv_p = 1;
16903 row->exact_window_width_line_p = 1;
16904 break;
16905 }
16906 if (ITERATOR_AT_END_OF_LINE_P (it))
16907 {
16908 row->exact_window_width_line_p = 1;
16909 goto at_end_of_line;
16910 }
16911 }
16912
16913 row->truncated_on_right_p = 1;
16914 it->continuation_lines_width = 0;
16915 reseat_at_next_visible_line_start (it, 0);
16916 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16917 it->hpos = hpos_before;
16918 it->current_x = x_before;
16919 break;
16920 }
16921 }
16922
16923 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16924 at the left window margin. */
16925 if (it->first_visible_x
16926 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16927 {
16928 if (!FRAME_WINDOW_P (it->f))
16929 insert_left_trunc_glyphs (it);
16930 row->truncated_on_left_p = 1;
16931 }
16932
16933 /* If the start of this line is the overlay arrow-position, then
16934 mark this glyph row as the one containing the overlay arrow.
16935 This is clearly a mess with variable size fonts. It would be
16936 better to let it be displayed like cursors under X. */
16937 if ((row->displays_text_p || !overlay_arrow_seen)
16938 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16939 !NILP (overlay_arrow_string)))
16940 {
16941 /* Overlay arrow in window redisplay is a fringe bitmap. */
16942 if (STRINGP (overlay_arrow_string))
16943 {
16944 struct glyph_row *arrow_row
16945 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16946 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16947 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16948 struct glyph *p = row->glyphs[TEXT_AREA];
16949 struct glyph *p2, *end;
16950
16951 /* Copy the arrow glyphs. */
16952 while (glyph < arrow_end)
16953 *p++ = *glyph++;
16954
16955 /* Throw away padding glyphs. */
16956 p2 = p;
16957 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16958 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16959 ++p2;
16960 if (p2 > p)
16961 {
16962 while (p2 < end)
16963 *p++ = *p2++;
16964 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16965 }
16966 }
16967 else
16968 {
16969 xassert (INTEGERP (overlay_arrow_string));
16970 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16971 }
16972 overlay_arrow_seen = 1;
16973 }
16974
16975 /* Compute pixel dimensions of this line. */
16976 compute_line_metrics (it);
16977
16978 /* Remember the position at which this line ends. */
16979 row->end = it->current;
16980
16981 /* Record whether this row ends inside an ellipsis. */
16982 row->ends_in_ellipsis_p
16983 = (it->method == GET_FROM_DISPLAY_VECTOR
16984 && it->ellipsis_p);
16985
16986 /* Save fringe bitmaps in this row. */
16987 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16988 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16989 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16990 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16991
16992 it->left_user_fringe_bitmap = 0;
16993 it->left_user_fringe_face_id = 0;
16994 it->right_user_fringe_bitmap = 0;
16995 it->right_user_fringe_face_id = 0;
16996
16997 /* Maybe set the cursor. */
16998 if (it->w->cursor.vpos < 0
16999 && PT >= MATRIX_ROW_START_CHARPOS (row)
17000 && PT <= MATRIX_ROW_END_CHARPOS (row)
17001 && cursor_row_p (it->w, row))
17002 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
17003
17004 /* Highlight trailing whitespace. */
17005 if (!NILP (Vshow_trailing_whitespace))
17006 highlight_trailing_whitespace (it->f, it->glyph_row);
17007
17008 /* Prepare for the next line. This line starts horizontally at (X
17009 HPOS) = (0 0). Vertical positions are incremented. As a
17010 convenience for the caller, IT->glyph_row is set to the next
17011 row to be used. */
17012 it->current_x = it->hpos = 0;
17013 it->current_y += row->height;
17014 ++it->vpos;
17015 ++it->glyph_row;
17016 it->start = it->current;
17017 return row->displays_text_p;
17018 }
17019
17020
17021 \f
17022 /***********************************************************************
17023 Menu Bar
17024 ***********************************************************************/
17025
17026 /* Redisplay the menu bar in the frame for window W.
17027
17028 The menu bar of X frames that don't have X toolkit support is
17029 displayed in a special window W->frame->menu_bar_window.
17030
17031 The menu bar of terminal frames is treated specially as far as
17032 glyph matrices are concerned. Menu bar lines are not part of
17033 windows, so the update is done directly on the frame matrix rows
17034 for the menu bar. */
17035
17036 static void
17037 display_menu_bar (w)
17038 struct window *w;
17039 {
17040 struct frame *f = XFRAME (WINDOW_FRAME (w));
17041 struct it it;
17042 Lisp_Object items;
17043 int i;
17044
17045 /* Don't do all this for graphical frames. */
17046 #ifdef HAVE_NTGUI
17047 if (FRAME_W32_P (f))
17048 return;
17049 #endif
17050 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17051 if (FRAME_X_P (f))
17052 return;
17053 #endif
17054
17055 #ifdef HAVE_NS
17056 if (FRAME_NS_P (f))
17057 return;
17058 #endif /* HAVE_NS */
17059
17060 #ifdef USE_X_TOOLKIT
17061 xassert (!FRAME_WINDOW_P (f));
17062 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17063 it.first_visible_x = 0;
17064 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17065 #else /* not USE_X_TOOLKIT */
17066 if (FRAME_WINDOW_P (f))
17067 {
17068 /* Menu bar lines are displayed in the desired matrix of the
17069 dummy window menu_bar_window. */
17070 struct window *menu_w;
17071 xassert (WINDOWP (f->menu_bar_window));
17072 menu_w = XWINDOW (f->menu_bar_window);
17073 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17074 MENU_FACE_ID);
17075 it.first_visible_x = 0;
17076 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17077 }
17078 else
17079 {
17080 /* This is a TTY frame, i.e. character hpos/vpos are used as
17081 pixel x/y. */
17082 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17083 MENU_FACE_ID);
17084 it.first_visible_x = 0;
17085 it.last_visible_x = FRAME_COLS (f);
17086 }
17087 #endif /* not USE_X_TOOLKIT */
17088
17089 if (! mode_line_inverse_video)
17090 /* Force the menu-bar to be displayed in the default face. */
17091 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17092
17093 /* Clear all rows of the menu bar. */
17094 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17095 {
17096 struct glyph_row *row = it.glyph_row + i;
17097 clear_glyph_row (row);
17098 row->enabled_p = 1;
17099 row->full_width_p = 1;
17100 }
17101
17102 /* Display all items of the menu bar. */
17103 items = FRAME_MENU_BAR_ITEMS (it.f);
17104 for (i = 0; i < XVECTOR_SIZE (items); i += 4)
17105 {
17106 Lisp_Object string;
17107
17108 /* Stop at nil string. */
17109 string = AREF (items, i + 1);
17110 if (NILP (string))
17111 break;
17112
17113 /* Remember where item was displayed. */
17114 ASET (items, i + 3, make_number (it.hpos));
17115
17116 /* Display the item, pad with one space. */
17117 if (it.current_x < it.last_visible_x)
17118 display_string (NULL, string, Qnil, 0, 0, &it,
17119 SCHARS (string) + 1, 0, 0, -1);
17120 }
17121
17122 /* Fill out the line with spaces. */
17123 if (it.current_x < it.last_visible_x)
17124 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17125
17126 /* Compute the total height of the lines. */
17127 compute_line_metrics (&it);
17128 }
17129
17130
17131 \f
17132 /***********************************************************************
17133 Mode Line
17134 ***********************************************************************/
17135
17136 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17137 FORCE is non-zero, redisplay mode lines unconditionally.
17138 Otherwise, redisplay only mode lines that are garbaged. Value is
17139 the number of windows whose mode lines were redisplayed. */
17140
17141 static int
17142 redisplay_mode_lines (window, force)
17143 Lisp_Object window;
17144 int force;
17145 {
17146 int nwindows = 0;
17147
17148 while (!NILP (window))
17149 {
17150 struct window *w = XWINDOW (window);
17151
17152 if (WINDOWP (w->hchild))
17153 nwindows += redisplay_mode_lines (w->hchild, force);
17154 else if (WINDOWP (w->vchild))
17155 nwindows += redisplay_mode_lines (w->vchild, force);
17156 else if (force
17157 || FRAME_GARBAGED_P (XFRAME (w->frame))
17158 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17159 {
17160 struct text_pos lpoint;
17161 struct buffer *old = current_buffer;
17162
17163 /* Set the window's buffer for the mode line display. */
17164 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17165 set_buffer_internal_1 (XBUFFER (w->buffer));
17166
17167 /* Point refers normally to the selected window. For any
17168 other window, set up appropriate value. */
17169 if (!EQ (window, selected_window))
17170 {
17171 struct text_pos pt;
17172
17173 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17174 if (CHARPOS (pt) < BEGV)
17175 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17176 else if (CHARPOS (pt) > (ZV - 1))
17177 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17178 else
17179 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17180 }
17181
17182 /* Display mode lines. */
17183 clear_glyph_matrix (w->desired_matrix);
17184 if (display_mode_lines (w))
17185 {
17186 ++nwindows;
17187 w->must_be_updated_p = 1;
17188 }
17189
17190 /* Restore old settings. */
17191 set_buffer_internal_1 (old);
17192 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17193 }
17194
17195 window = w->next;
17196 }
17197
17198 return nwindows;
17199 }
17200
17201
17202 /* Display the mode and/or header line of window W. Value is the
17203 sum number of mode lines and header lines displayed. */
17204
17205 static int
17206 display_mode_lines (w)
17207 struct window *w;
17208 {
17209 Lisp_Object old_selected_window, old_selected_frame;
17210 int n = 0;
17211
17212 old_selected_frame = selected_frame;
17213 selected_frame = w->frame;
17214 old_selected_window = selected_window;
17215 XSETWINDOW (selected_window, w);
17216
17217 /* These will be set while the mode line specs are processed. */
17218 line_number_displayed = 0;
17219 w->column_number_displayed = Qnil;
17220
17221 if (WINDOW_WANTS_MODELINE_P (w))
17222 {
17223 struct window *sel_w = XWINDOW (old_selected_window);
17224
17225 /* Select mode line face based on the real selected window. */
17226 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17227 current_buffer->mode_line_format);
17228 ++n;
17229 }
17230
17231 if (WINDOW_WANTS_HEADER_LINE_P (w))
17232 {
17233 display_mode_line (w, HEADER_LINE_FACE_ID,
17234 current_buffer->header_line_format);
17235 ++n;
17236 }
17237
17238 selected_frame = old_selected_frame;
17239 selected_window = old_selected_window;
17240 return n;
17241 }
17242
17243
17244 /* Display mode or header line of window W. FACE_ID specifies which
17245 line to display; it is either MODE_LINE_FACE_ID or
17246 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17247 display. Value is the pixel height of the mode/header line
17248 displayed. */
17249
17250 static int
17251 display_mode_line (w, face_id, format)
17252 struct window *w;
17253 enum face_id face_id;
17254 Lisp_Object format;
17255 {
17256 struct it it;
17257 struct face *face;
17258 int count = SPECPDL_INDEX ();
17259
17260 init_iterator (&it, w, -1, -1, NULL, face_id);
17261 /* Don't extend on a previously drawn mode-line.
17262 This may happen if called from pos_visible_p. */
17263 it.glyph_row->enabled_p = 0;
17264 prepare_desired_row (it.glyph_row);
17265
17266 it.glyph_row->mode_line_p = 1;
17267
17268 if (! mode_line_inverse_video)
17269 /* Force the mode-line to be displayed in the default face. */
17270 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17271
17272 record_unwind_protect (unwind_format_mode_line,
17273 format_mode_line_unwind_data (NULL, Qnil, 0));
17274
17275 mode_line_target = MODE_LINE_DISPLAY;
17276
17277 /* Temporarily make frame's keyboard the current kboard so that
17278 kboard-local variables in the mode_line_format will get the right
17279 values. */
17280 push_kboard (FRAME_KBOARD (it.f));
17281 record_unwind_save_match_data ();
17282 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17283 pop_kboard ();
17284
17285 unbind_to (count, Qnil);
17286
17287 /* Fill up with spaces. */
17288 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17289
17290 compute_line_metrics (&it);
17291 it.glyph_row->full_width_p = 1;
17292 it.glyph_row->continued_p = 0;
17293 it.glyph_row->truncated_on_left_p = 0;
17294 it.glyph_row->truncated_on_right_p = 0;
17295
17296 /* Make a 3D mode-line have a shadow at its right end. */
17297 face = FACE_FROM_ID (it.f, face_id);
17298 extend_face_to_end_of_line (&it);
17299 if (face->box != FACE_NO_BOX)
17300 {
17301 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17302 + it.glyph_row->used[TEXT_AREA] - 1);
17303 last->right_box_line_p = 1;
17304 }
17305
17306 return it.glyph_row->height;
17307 }
17308
17309 /* Move element ELT in LIST to the front of LIST.
17310 Return the updated list. */
17311
17312 static Lisp_Object
17313 move_elt_to_front (elt, list)
17314 Lisp_Object elt, list;
17315 {
17316 register Lisp_Object tail, prev;
17317 register Lisp_Object tem;
17318
17319 tail = list;
17320 prev = Qnil;
17321 while (CONSP (tail))
17322 {
17323 tem = XCAR (tail);
17324
17325 if (EQ (elt, tem))
17326 {
17327 /* Splice out the link TAIL. */
17328 if (NILP (prev))
17329 list = XCDR (tail);
17330 else
17331 Fsetcdr (prev, XCDR (tail));
17332
17333 /* Now make it the first. */
17334 Fsetcdr (tail, list);
17335 return tail;
17336 }
17337 else
17338 prev = tail;
17339 tail = XCDR (tail);
17340 QUIT;
17341 }
17342
17343 /* Not found--return unchanged LIST. */
17344 return list;
17345 }
17346
17347 /* Contribute ELT to the mode line for window IT->w. How it
17348 translates into text depends on its data type.
17349
17350 IT describes the display environment in which we display, as usual.
17351
17352 DEPTH is the depth in recursion. It is used to prevent
17353 infinite recursion here.
17354
17355 FIELD_WIDTH is the number of characters the display of ELT should
17356 occupy in the mode line, and PRECISION is the maximum number of
17357 characters to display from ELT's representation. See
17358 display_string for details.
17359
17360 Returns the hpos of the end of the text generated by ELT.
17361
17362 PROPS is a property list to add to any string we encounter.
17363
17364 If RISKY is nonzero, remove (disregard) any properties in any string
17365 we encounter, and ignore :eval and :propertize.
17366
17367 The global variable `mode_line_target' determines whether the
17368 output is passed to `store_mode_line_noprop',
17369 `store_mode_line_string', or `display_string'. */
17370
17371 static int
17372 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17373 struct it *it;
17374 int depth;
17375 int field_width, precision;
17376 Lisp_Object elt, props;
17377 int risky;
17378 {
17379 int n = 0, field, prec;
17380 int literal = 0;
17381
17382 tail_recurse:
17383 if (depth > 100)
17384 elt = build_string ("*too-deep*");
17385
17386 depth++;
17387
17388 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17389 {
17390 case Lisp_String:
17391 {
17392 /* A string: output it and check for %-constructs within it. */
17393 unsigned char c;
17394 int offset = 0;
17395
17396 if (SCHARS (elt) > 0
17397 && (!NILP (props) || risky))
17398 {
17399 Lisp_Object oprops, aelt;
17400 oprops = Ftext_properties_at (make_number (0), elt);
17401
17402 /* If the starting string's properties are not what
17403 we want, translate the string. Also, if the string
17404 is risky, do that anyway. */
17405
17406 if (NILP (Fequal (props, oprops)) || risky)
17407 {
17408 /* If the starting string has properties,
17409 merge the specified ones onto the existing ones. */
17410 if (! NILP (oprops) && !risky)
17411 {
17412 Lisp_Object tem;
17413
17414 oprops = Fcopy_sequence (oprops);
17415 tem = props;
17416 while (CONSP (tem))
17417 {
17418 oprops = Fplist_put (oprops, XCAR (tem),
17419 XCAR (XCDR (tem)));
17420 tem = XCDR (XCDR (tem));
17421 }
17422 props = oprops;
17423 }
17424
17425 aelt = Fassoc (elt, mode_line_proptrans_alist);
17426 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17427 {
17428 /* AELT is what we want. Move it to the front
17429 without consing. */
17430 elt = XCAR (aelt);
17431 mode_line_proptrans_alist
17432 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17433 }
17434 else
17435 {
17436 Lisp_Object tem;
17437
17438 /* If AELT has the wrong props, it is useless.
17439 so get rid of it. */
17440 if (! NILP (aelt))
17441 mode_line_proptrans_alist
17442 = Fdelq (aelt, mode_line_proptrans_alist);
17443
17444 elt = Fcopy_sequence (elt);
17445 Fset_text_properties (make_number (0), Flength (elt),
17446 props, elt);
17447 /* Add this item to mode_line_proptrans_alist. */
17448 mode_line_proptrans_alist
17449 = Fcons (Fcons (elt, props),
17450 mode_line_proptrans_alist);
17451 /* Truncate mode_line_proptrans_alist
17452 to at most 50 elements. */
17453 tem = Fnthcdr (make_number (50),
17454 mode_line_proptrans_alist);
17455 if (! NILP (tem))
17456 XSETCDR (tem, Qnil);
17457 }
17458 }
17459 }
17460
17461 offset = 0;
17462
17463 if (literal)
17464 {
17465 prec = precision - n;
17466 switch (mode_line_target)
17467 {
17468 case MODE_LINE_NOPROP:
17469 case MODE_LINE_TITLE:
17470 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17471 break;
17472 case MODE_LINE_STRING:
17473 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17474 break;
17475 case MODE_LINE_DISPLAY:
17476 n += display_string (NULL, elt, Qnil, 0, 0, it,
17477 0, prec, 0, STRING_MULTIBYTE (elt));
17478 break;
17479 }
17480
17481 break;
17482 }
17483
17484 /* Handle the non-literal case. */
17485
17486 while ((precision <= 0 || n < precision)
17487 && SREF (elt, offset) != 0
17488 && (mode_line_target != MODE_LINE_DISPLAY
17489 || it->current_x < it->last_visible_x))
17490 {
17491 int last_offset = offset;
17492
17493 /* Advance to end of string or next format specifier. */
17494 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17495 ;
17496
17497 if (offset - 1 != last_offset)
17498 {
17499 int nchars, nbytes;
17500
17501 /* Output to end of string or up to '%'. Field width
17502 is length of string. Don't output more than
17503 PRECISION allows us. */
17504 offset--;
17505
17506 prec = c_string_width (SDATA (elt) + last_offset,
17507 offset - last_offset, precision - n,
17508 &nchars, &nbytes);
17509
17510 switch (mode_line_target)
17511 {
17512 case MODE_LINE_NOPROP:
17513 case MODE_LINE_TITLE:
17514 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17515 break;
17516 case MODE_LINE_STRING:
17517 {
17518 int bytepos = last_offset;
17519 int charpos = string_byte_to_char (elt, bytepos);
17520 int endpos = (precision <= 0
17521 ? string_byte_to_char (elt, offset)
17522 : charpos + nchars);
17523
17524 n += store_mode_line_string (NULL,
17525 Fsubstring (elt, make_number (charpos),
17526 make_number (endpos)),
17527 0, 0, 0, Qnil);
17528 }
17529 break;
17530 case MODE_LINE_DISPLAY:
17531 {
17532 int bytepos = last_offset;
17533 int charpos = string_byte_to_char (elt, bytepos);
17534
17535 if (precision <= 0)
17536 nchars = string_byte_to_char (elt, offset) - charpos;
17537 n += display_string (NULL, elt, Qnil, 0, charpos,
17538 it, 0, nchars, 0,
17539 STRING_MULTIBYTE (elt));
17540 }
17541 break;
17542 }
17543 }
17544 else /* c == '%' */
17545 {
17546 int percent_position = offset;
17547
17548 /* Get the specified minimum width. Zero means
17549 don't pad. */
17550 field = 0;
17551 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17552 field = field * 10 + c - '0';
17553
17554 /* Don't pad beyond the total padding allowed. */
17555 if (field_width - n > 0 && field > field_width - n)
17556 field = field_width - n;
17557
17558 /* Note that either PRECISION <= 0 or N < PRECISION. */
17559 prec = precision - n;
17560
17561 if (c == 'M')
17562 n += display_mode_element (it, depth, field, prec,
17563 Vglobal_mode_string, props,
17564 risky);
17565 else if (c != 0)
17566 {
17567 int multibyte;
17568 int bytepos, charpos;
17569 unsigned char *spec;
17570 Lisp_Object string;
17571
17572 bytepos = percent_position;
17573 charpos = (STRING_MULTIBYTE (elt)
17574 ? string_byte_to_char (elt, bytepos)
17575 : bytepos);
17576 spec = decode_mode_spec (it->w, c, field, prec, &string);
17577 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
17578
17579 switch (mode_line_target)
17580 {
17581 case MODE_LINE_NOPROP:
17582 case MODE_LINE_TITLE:
17583 n += store_mode_line_noprop (spec, field, prec);
17584 break;
17585 case MODE_LINE_STRING:
17586 {
17587 int len = strlen (spec);
17588 Lisp_Object tem = make_string (spec, len);
17589 props = Ftext_properties_at (make_number (charpos), elt);
17590 /* Should only keep face property in props */
17591 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17592 }
17593 break;
17594 case MODE_LINE_DISPLAY:
17595 {
17596 int nglyphs_before, nwritten;
17597
17598 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17599 nwritten = display_string (spec, string, elt,
17600 charpos, 0, it,
17601 field, prec, 0,
17602 multibyte);
17603
17604 /* Assign to the glyphs written above the
17605 string where the `%x' came from, position
17606 of the `%'. */
17607 if (nwritten > 0)
17608 {
17609 struct glyph *glyph
17610 = (it->glyph_row->glyphs[TEXT_AREA]
17611 + nglyphs_before);
17612 int i;
17613
17614 for (i = 0; i < nwritten; ++i)
17615 {
17616 glyph[i].object = elt;
17617 glyph[i].charpos = charpos;
17618 }
17619
17620 n += nwritten;
17621 }
17622 }
17623 break;
17624 }
17625 }
17626 else /* c == 0 */
17627 break;
17628 }
17629 }
17630 }
17631 break;
17632
17633 case Lisp_Symbol:
17634 /* A symbol: process the value of the symbol recursively
17635 as if it appeared here directly. Avoid error if symbol void.
17636 Special case: if value of symbol is a string, output the string
17637 literally. */
17638 {
17639 register Lisp_Object tem;
17640
17641 /* If the variable is not marked as risky to set
17642 then its contents are risky to use. */
17643 if (NILP (Fget (elt, Qrisky_local_variable)))
17644 risky = 1;
17645
17646 tem = Fboundp (elt);
17647 if (!NILP (tem))
17648 {
17649 tem = Fsymbol_value (elt);
17650 /* If value is a string, output that string literally:
17651 don't check for % within it. */
17652 if (STRINGP (tem))
17653 literal = 1;
17654
17655 if (!EQ (tem, elt))
17656 {
17657 /* Give up right away for nil or t. */
17658 elt = tem;
17659 goto tail_recurse;
17660 }
17661 }
17662 }
17663 break;
17664
17665 case Lisp_Cons:
17666 {
17667 register Lisp_Object car, tem;
17668
17669 /* A cons cell: five distinct cases.
17670 If first element is :eval or :propertize, do something special.
17671 If first element is a string or a cons, process all the elements
17672 and effectively concatenate them.
17673 If first element is a negative number, truncate displaying cdr to
17674 at most that many characters. If positive, pad (with spaces)
17675 to at least that many characters.
17676 If first element is a symbol, process the cadr or caddr recursively
17677 according to whether the symbol's value is non-nil or nil. */
17678 car = XCAR (elt);
17679 if (EQ (car, QCeval))
17680 {
17681 /* An element of the form (:eval FORM) means evaluate FORM
17682 and use the result as mode line elements. */
17683
17684 if (risky)
17685 break;
17686
17687 if (CONSP (XCDR (elt)))
17688 {
17689 Lisp_Object spec;
17690 spec = safe_eval (XCAR (XCDR (elt)));
17691 n += display_mode_element (it, depth, field_width - n,
17692 precision - n, spec, props,
17693 risky);
17694 }
17695 }
17696 else if (EQ (car, QCpropertize))
17697 {
17698 /* An element of the form (:propertize ELT PROPS...)
17699 means display ELT but applying properties PROPS. */
17700
17701 if (risky)
17702 break;
17703
17704 if (CONSP (XCDR (elt)))
17705 n += display_mode_element (it, depth, field_width - n,
17706 precision - n, XCAR (XCDR (elt)),
17707 XCDR (XCDR (elt)), risky);
17708 }
17709 else if (SYMBOLP (car))
17710 {
17711 tem = Fboundp (car);
17712 elt = XCDR (elt);
17713 if (!CONSP (elt))
17714 goto invalid;
17715 /* elt is now the cdr, and we know it is a cons cell.
17716 Use its car if CAR has a non-nil value. */
17717 if (!NILP (tem))
17718 {
17719 tem = Fsymbol_value (car);
17720 if (!NILP (tem))
17721 {
17722 elt = XCAR (elt);
17723 goto tail_recurse;
17724 }
17725 }
17726 /* Symbol's value is nil (or symbol is unbound)
17727 Get the cddr of the original list
17728 and if possible find the caddr and use that. */
17729 elt = XCDR (elt);
17730 if (NILP (elt))
17731 break;
17732 else if (!CONSP (elt))
17733 goto invalid;
17734 elt = XCAR (elt);
17735 goto tail_recurse;
17736 }
17737 else if (INTEGERP (car))
17738 {
17739 register int lim = XINT (car);
17740 elt = XCDR (elt);
17741 if (lim < 0)
17742 {
17743 /* Negative int means reduce maximum width. */
17744 if (precision <= 0)
17745 precision = -lim;
17746 else
17747 precision = min (precision, -lim);
17748 }
17749 else if (lim > 0)
17750 {
17751 /* Padding specified. Don't let it be more than
17752 current maximum. */
17753 if (precision > 0)
17754 lim = min (precision, lim);
17755
17756 /* If that's more padding than already wanted, queue it.
17757 But don't reduce padding already specified even if
17758 that is beyond the current truncation point. */
17759 field_width = max (lim, field_width);
17760 }
17761 goto tail_recurse;
17762 }
17763 else if (STRINGP (car) || CONSP (car))
17764 {
17765 Lisp_Object halftail = elt;
17766 int len = 0;
17767
17768 while (CONSP (elt)
17769 && (precision <= 0 || n < precision))
17770 {
17771 n += display_mode_element (it, depth,
17772 /* Do padding only after the last
17773 element in the list. */
17774 (! CONSP (XCDR (elt))
17775 ? field_width - n
17776 : 0),
17777 precision - n, XCAR (elt),
17778 props, risky);
17779 elt = XCDR (elt);
17780 len++;
17781 if ((len & 1) == 0)
17782 halftail = XCDR (halftail);
17783 /* Check for cycle. */
17784 if (EQ (halftail, elt))
17785 break;
17786 }
17787 }
17788 }
17789 break;
17790
17791 default:
17792 invalid:
17793 elt = build_string ("*invalid*");
17794 goto tail_recurse;
17795 }
17796
17797 /* Pad to FIELD_WIDTH. */
17798 if (field_width > 0 && n < field_width)
17799 {
17800 switch (mode_line_target)
17801 {
17802 case MODE_LINE_NOPROP:
17803 case MODE_LINE_TITLE:
17804 n += store_mode_line_noprop ("", field_width - n, 0);
17805 break;
17806 case MODE_LINE_STRING:
17807 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17808 break;
17809 case MODE_LINE_DISPLAY:
17810 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17811 0, 0, 0);
17812 break;
17813 }
17814 }
17815
17816 return n;
17817 }
17818
17819 /* Store a mode-line string element in mode_line_string_list.
17820
17821 If STRING is non-null, display that C string. Otherwise, the Lisp
17822 string LISP_STRING is displayed.
17823
17824 FIELD_WIDTH is the minimum number of output glyphs to produce.
17825 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17826 with spaces. FIELD_WIDTH <= 0 means don't pad.
17827
17828 PRECISION is the maximum number of characters to output from
17829 STRING. PRECISION <= 0 means don't truncate the string.
17830
17831 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17832 properties to the string.
17833
17834 PROPS are the properties to add to the string.
17835 The mode_line_string_face face property is always added to the string.
17836 */
17837
17838 static int
17839 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17840 char *string;
17841 Lisp_Object lisp_string;
17842 int copy_string;
17843 int field_width;
17844 int precision;
17845 Lisp_Object props;
17846 {
17847 int len;
17848 int n = 0;
17849
17850 if (string != NULL)
17851 {
17852 len = strlen (string);
17853 if (precision > 0 && len > precision)
17854 len = precision;
17855 lisp_string = make_string (string, len);
17856 if (NILP (props))
17857 props = mode_line_string_face_prop;
17858 else if (!NILP (mode_line_string_face))
17859 {
17860 Lisp_Object face = Fplist_get (props, Qface);
17861 props = Fcopy_sequence (props);
17862 if (NILP (face))
17863 face = mode_line_string_face;
17864 else
17865 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17866 props = Fplist_put (props, Qface, face);
17867 }
17868 Fadd_text_properties (make_number (0), make_number (len),
17869 props, lisp_string);
17870 }
17871 else
17872 {
17873 len = XFASTINT (Flength (lisp_string));
17874 if (precision > 0 && len > precision)
17875 {
17876 len = precision;
17877 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17878 precision = -1;
17879 }
17880 if (!NILP (mode_line_string_face))
17881 {
17882 Lisp_Object face;
17883 if (NILP (props))
17884 props = Ftext_properties_at (make_number (0), lisp_string);
17885 face = Fplist_get (props, Qface);
17886 if (NILP (face))
17887 face = mode_line_string_face;
17888 else
17889 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17890 props = Fcons (Qface, Fcons (face, Qnil));
17891 if (copy_string)
17892 lisp_string = Fcopy_sequence (lisp_string);
17893 }
17894 if (!NILP (props))
17895 Fadd_text_properties (make_number (0), make_number (len),
17896 props, lisp_string);
17897 }
17898
17899 if (len > 0)
17900 {
17901 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17902 n += len;
17903 }
17904
17905 if (field_width > len)
17906 {
17907 field_width -= len;
17908 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17909 if (!NILP (props))
17910 Fadd_text_properties (make_number (0), make_number (field_width),
17911 props, lisp_string);
17912 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17913 n += field_width;
17914 }
17915
17916 return n;
17917 }
17918
17919
17920 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17921 1, 4, 0,
17922 doc: /* Format a string out of a mode line format specification.
17923 First arg FORMAT specifies the mode line format (see `mode-line-format'
17924 for details) to use.
17925
17926 By default, the format is evaluated for the currently selected window.
17927
17928 Optional second arg FACE specifies the face property to put on all
17929 characters for which no face is specified. The value nil means the
17930 default face. The value t means whatever face the window's mode line
17931 currently uses (either `mode-line' or `mode-line-inactive',
17932 depending on whether the window is the selected window or not).
17933 An integer value means the value string has no text
17934 properties.
17935
17936 Optional third and fourth args WINDOW and BUFFER specify the window
17937 and buffer to use as the context for the formatting (defaults
17938 are the selected window and the WINDOW's buffer). */)
17939 (format, face, window, buffer)
17940 Lisp_Object format, face, window, buffer;
17941 {
17942 struct it it;
17943 int len;
17944 struct window *w;
17945 struct buffer *old_buffer = NULL;
17946 int face_id;
17947 int no_props = INTEGERP (face);
17948 int count = SPECPDL_INDEX ();
17949 Lisp_Object str;
17950 int string_start = 0;
17951
17952 if (NILP (window))
17953 window = selected_window;
17954 CHECK_WINDOW (window);
17955 w = XWINDOW (window);
17956
17957 if (NILP (buffer))
17958 buffer = w->buffer;
17959 CHECK_BUFFER (buffer);
17960
17961 /* Make formatting the modeline a non-op when noninteractive, otherwise
17962 there will be problems later caused by a partially initialized frame. */
17963 if (NILP (format) || noninteractive)
17964 return empty_unibyte_string;
17965
17966 if (no_props)
17967 face = Qnil;
17968
17969 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
17970 : EQ (face, Qt) ? (EQ (window, selected_window)
17971 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
17972 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
17973 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
17974 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
17975 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
17976 : DEFAULT_FACE_ID;
17977
17978 if (XBUFFER (buffer) != current_buffer)
17979 old_buffer = current_buffer;
17980
17981 /* Save things including mode_line_proptrans_alist,
17982 and set that to nil so that we don't alter the outer value. */
17983 record_unwind_protect (unwind_format_mode_line,
17984 format_mode_line_unwind_data
17985 (old_buffer, selected_window, 1));
17986 mode_line_proptrans_alist = Qnil;
17987
17988 Fselect_window (window, Qt);
17989 if (old_buffer)
17990 set_buffer_internal_1 (XBUFFER (buffer));
17991
17992 init_iterator (&it, w, -1, -1, NULL, face_id);
17993
17994 if (no_props)
17995 {
17996 mode_line_target = MODE_LINE_NOPROP;
17997 mode_line_string_face_prop = Qnil;
17998 mode_line_string_list = Qnil;
17999 string_start = MODE_LINE_NOPROP_LEN (0);
18000 }
18001 else
18002 {
18003 mode_line_target = MODE_LINE_STRING;
18004 mode_line_string_list = Qnil;
18005 mode_line_string_face = face;
18006 mode_line_string_face_prop
18007 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
18008 }
18009
18010 push_kboard (FRAME_KBOARD (it.f));
18011 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
18012 pop_kboard ();
18013
18014 if (no_props)
18015 {
18016 len = MODE_LINE_NOPROP_LEN (string_start);
18017 str = make_string (mode_line_noprop_buf + string_start, len);
18018 }
18019 else
18020 {
18021 mode_line_string_list = Fnreverse (mode_line_string_list);
18022 str = Fmapconcat (intern ("identity"), mode_line_string_list,
18023 empty_unibyte_string);
18024 }
18025
18026 unbind_to (count, Qnil);
18027 return str;
18028 }
18029
18030 /* Write a null-terminated, right justified decimal representation of
18031 the positive integer D to BUF using a minimal field width WIDTH. */
18032
18033 static void
18034 pint2str (buf, width, d)
18035 register char *buf;
18036 register int width;
18037 register int d;
18038 {
18039 register char *p = buf;
18040
18041 if (d <= 0)
18042 *p++ = '0';
18043 else
18044 {
18045 while (d > 0)
18046 {
18047 *p++ = d % 10 + '0';
18048 d /= 10;
18049 }
18050 }
18051
18052 for (width -= (int) (p - buf); width > 0; --width)
18053 *p++ = ' ';
18054 *p-- = '\0';
18055 while (p > buf)
18056 {
18057 d = *buf;
18058 *buf++ = *p;
18059 *p-- = d;
18060 }
18061 }
18062
18063 /* Write a null-terminated, right justified decimal and "human
18064 readable" representation of the nonnegative integer D to BUF using
18065 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18066
18067 static const char power_letter[] =
18068 {
18069 0, /* not used */
18070 'k', /* kilo */
18071 'M', /* mega */
18072 'G', /* giga */
18073 'T', /* tera */
18074 'P', /* peta */
18075 'E', /* exa */
18076 'Z', /* zetta */
18077 'Y' /* yotta */
18078 };
18079
18080 static void
18081 pint2hrstr (buf, width, d)
18082 char *buf;
18083 int width;
18084 int d;
18085 {
18086 /* We aim to represent the nonnegative integer D as
18087 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18088 int quotient = d;
18089 int remainder = 0;
18090 /* -1 means: do not use TENTHS. */
18091 int tenths = -1;
18092 int exponent = 0;
18093
18094 /* Length of QUOTIENT.TENTHS as a string. */
18095 int length;
18096
18097 char * psuffix;
18098 char * p;
18099
18100 if (1000 <= quotient)
18101 {
18102 /* Scale to the appropriate EXPONENT. */
18103 do
18104 {
18105 remainder = quotient % 1000;
18106 quotient /= 1000;
18107 exponent++;
18108 }
18109 while (1000 <= quotient);
18110
18111 /* Round to nearest and decide whether to use TENTHS or not. */
18112 if (quotient <= 9)
18113 {
18114 tenths = remainder / 100;
18115 if (50 <= remainder % 100)
18116 {
18117 if (tenths < 9)
18118 tenths++;
18119 else
18120 {
18121 quotient++;
18122 if (quotient == 10)
18123 tenths = -1;
18124 else
18125 tenths = 0;
18126 }
18127 }
18128 }
18129 else
18130 if (500 <= remainder)
18131 {
18132 if (quotient < 999)
18133 quotient++;
18134 else
18135 {
18136 quotient = 1;
18137 exponent++;
18138 tenths = 0;
18139 }
18140 }
18141 }
18142
18143 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18144 if (tenths == -1 && quotient <= 99)
18145 if (quotient <= 9)
18146 length = 1;
18147 else
18148 length = 2;
18149 else
18150 length = 3;
18151 p = psuffix = buf + max (width, length);
18152
18153 /* Print EXPONENT. */
18154 if (exponent)
18155 *psuffix++ = power_letter[exponent];
18156 *psuffix = '\0';
18157
18158 /* Print TENTHS. */
18159 if (tenths >= 0)
18160 {
18161 *--p = '0' + tenths;
18162 *--p = '.';
18163 }
18164
18165 /* Print QUOTIENT. */
18166 do
18167 {
18168 int digit = quotient % 10;
18169 *--p = '0' + digit;
18170 }
18171 while ((quotient /= 10) != 0);
18172
18173 /* Print leading spaces. */
18174 while (buf < p)
18175 *--p = ' ';
18176 }
18177
18178 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18179 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18180 type of CODING_SYSTEM. Return updated pointer into BUF. */
18181
18182 static unsigned char invalid_eol_type[] = "(*invalid*)";
18183
18184 static char *
18185 decode_mode_spec_coding (coding_system, buf, eol_flag)
18186 Lisp_Object coding_system;
18187 register char *buf;
18188 int eol_flag;
18189 {
18190 Lisp_Object val;
18191 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18192 const unsigned char *eol_str;
18193 int eol_str_len;
18194 /* The EOL conversion we are using. */
18195 Lisp_Object eoltype;
18196
18197 val = CODING_SYSTEM_SPEC (coding_system);
18198 eoltype = Qnil;
18199
18200 if (!VECTORP (val)) /* Not yet decided. */
18201 {
18202 if (multibyte)
18203 *buf++ = '-';
18204 if (eol_flag)
18205 eoltype = eol_mnemonic_undecided;
18206 /* Don't mention EOL conversion if it isn't decided. */
18207 }
18208 else
18209 {
18210 Lisp_Object attrs;
18211 Lisp_Object eolvalue;
18212
18213 attrs = AREF (val, 0);
18214 eolvalue = AREF (val, 2);
18215
18216 if (multibyte)
18217 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18218
18219 if (eol_flag)
18220 {
18221 /* The EOL conversion that is normal on this system. */
18222
18223 if (NILP (eolvalue)) /* Not yet decided. */
18224 eoltype = eol_mnemonic_undecided;
18225 else if (VECTORP (eolvalue)) /* Not yet decided. */
18226 eoltype = eol_mnemonic_undecided;
18227 else /* eolvalue is Qunix, Qdos, or Qmac. */
18228 eoltype = (EQ (eolvalue, Qunix)
18229 ? eol_mnemonic_unix
18230 : (EQ (eolvalue, Qdos) == 1
18231 ? eol_mnemonic_dos : eol_mnemonic_mac));
18232 }
18233 }
18234
18235 if (eol_flag)
18236 {
18237 /* Mention the EOL conversion if it is not the usual one. */
18238 if (STRINGP (eoltype))
18239 {
18240 eol_str = SDATA (eoltype);
18241 eol_str_len = SBYTES (eoltype);
18242 }
18243 else if (CHARACTERP (eoltype))
18244 {
18245 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18246 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18247 eol_str = tmp;
18248 }
18249 else
18250 {
18251 eol_str = invalid_eol_type;
18252 eol_str_len = sizeof (invalid_eol_type) - 1;
18253 }
18254 bcopy (eol_str, buf, eol_str_len);
18255 buf += eol_str_len;
18256 }
18257
18258 return buf;
18259 }
18260
18261 /* Return a string for the output of a mode line %-spec for window W,
18262 generated by character C. PRECISION >= 0 means don't return a
18263 string longer than that value. FIELD_WIDTH > 0 means pad the
18264 string returned with spaces to that value. Return a Lisp string in
18265 *STRING if the resulting string is taken from that Lisp string.
18266
18267 Note we operate on the current buffer for most purposes,
18268 the exception being w->base_line_pos. */
18269
18270 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18271
18272 static char *
18273 decode_mode_spec (w, c, field_width, precision, string)
18274 struct window *w;
18275 register int c;
18276 int field_width, precision;
18277 Lisp_Object *string;
18278 {
18279 Lisp_Object obj;
18280 struct frame *f = XFRAME (WINDOW_FRAME (w));
18281 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18282 struct buffer *b = current_buffer;
18283
18284 obj = Qnil;
18285 *string = Qnil;
18286
18287 switch (c)
18288 {
18289 case '*':
18290 if (!NILP (b->read_only))
18291 return "%";
18292 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18293 return "*";
18294 return "-";
18295
18296 case '+':
18297 /* This differs from %* only for a modified read-only buffer. */
18298 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18299 return "*";
18300 if (!NILP (b->read_only))
18301 return "%";
18302 return "-";
18303
18304 case '&':
18305 /* This differs from %* in ignoring read-only-ness. */
18306 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18307 return "*";
18308 return "-";
18309
18310 case '%':
18311 return "%";
18312
18313 case '[':
18314 {
18315 int i;
18316 char *p;
18317
18318 if (command_loop_level > 5)
18319 return "[[[... ";
18320 p = decode_mode_spec_buf;
18321 for (i = 0; i < command_loop_level; i++)
18322 *p++ = '[';
18323 *p = 0;
18324 return decode_mode_spec_buf;
18325 }
18326
18327 case ']':
18328 {
18329 int i;
18330 char *p;
18331
18332 if (command_loop_level > 5)
18333 return " ...]]]";
18334 p = decode_mode_spec_buf;
18335 for (i = 0; i < command_loop_level; i++)
18336 *p++ = ']';
18337 *p = 0;
18338 return decode_mode_spec_buf;
18339 }
18340
18341 case '-':
18342 {
18343 register int i;
18344
18345 /* Let lots_of_dashes be a string of infinite length. */
18346 if (mode_line_target == MODE_LINE_NOPROP ||
18347 mode_line_target == MODE_LINE_STRING)
18348 return "--";
18349 if (field_width <= 0
18350 || field_width > sizeof (lots_of_dashes))
18351 {
18352 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18353 decode_mode_spec_buf[i] = '-';
18354 decode_mode_spec_buf[i] = '\0';
18355 return decode_mode_spec_buf;
18356 }
18357 else
18358 return lots_of_dashes;
18359 }
18360
18361 case 'b':
18362 obj = b->name;
18363 break;
18364
18365 case 'c':
18366 /* %c and %l are ignored in `frame-title-format'.
18367 (In redisplay_internal, the frame title is drawn _before_ the
18368 windows are updated, so the stuff which depends on actual
18369 window contents (such as %l) may fail to render properly, or
18370 even crash emacs.) */
18371 if (mode_line_target == MODE_LINE_TITLE)
18372 return "";
18373 else
18374 {
18375 int col = (int) current_column (); /* iftc */
18376 w->column_number_displayed = make_number (col);
18377 pint2str (decode_mode_spec_buf, field_width, col);
18378 return decode_mode_spec_buf;
18379 }
18380
18381 case 'e':
18382 #ifndef SYSTEM_MALLOC
18383 {
18384 if (NILP (Vmemory_full))
18385 return "";
18386 else
18387 return "!MEM FULL! ";
18388 }
18389 #else
18390 return "";
18391 #endif
18392
18393 case 'F':
18394 /* %F displays the frame name. */
18395 if (!NILP (f->title))
18396 return (char *) SDATA (f->title);
18397 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18398 return (char *) SDATA (f->name);
18399 return "Emacs";
18400
18401 case 'f':
18402 obj = b->filename;
18403 break;
18404
18405 case 'i':
18406 {
18407 int size = ZV - BEGV;
18408 pint2str (decode_mode_spec_buf, field_width, size);
18409 return decode_mode_spec_buf;
18410 }
18411
18412 case 'I':
18413 {
18414 int size = ZV - BEGV;
18415 pint2hrstr (decode_mode_spec_buf, field_width, size);
18416 return decode_mode_spec_buf;
18417 }
18418
18419 case 'l':
18420 {
18421 int startpos, startpos_byte, line, linepos, linepos_byte;
18422 int topline, nlines, junk, height;
18423
18424 /* %c and %l are ignored in `frame-title-format'. */
18425 if (mode_line_target == MODE_LINE_TITLE)
18426 return "";
18427
18428 startpos = XMARKER (w->start)->charpos;
18429 startpos_byte = marker_byte_position (w->start);
18430 height = WINDOW_TOTAL_LINES (w);
18431
18432 /* If we decided that this buffer isn't suitable for line numbers,
18433 don't forget that too fast. */
18434 if (EQ (w->base_line_pos, w->buffer))
18435 goto no_value;
18436 /* But do forget it, if the window shows a different buffer now. */
18437 else if (BUFFERP (w->base_line_pos))
18438 w->base_line_pos = Qnil;
18439
18440 /* If the buffer is very big, don't waste time. */
18441 if (INTEGERP (Vline_number_display_limit)
18442 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18443 {
18444 w->base_line_pos = Qnil;
18445 w->base_line_number = Qnil;
18446 goto no_value;
18447 }
18448
18449 if (INTEGERP (w->base_line_number)
18450 && INTEGERP (w->base_line_pos)
18451 && XFASTINT (w->base_line_pos) <= startpos)
18452 {
18453 line = XFASTINT (w->base_line_number);
18454 linepos = XFASTINT (w->base_line_pos);
18455 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18456 }
18457 else
18458 {
18459 line = 1;
18460 linepos = BUF_BEGV (b);
18461 linepos_byte = BUF_BEGV_BYTE (b);
18462 }
18463
18464 /* Count lines from base line to window start position. */
18465 nlines = display_count_lines (linepos, linepos_byte,
18466 startpos_byte,
18467 startpos, &junk);
18468
18469 topline = nlines + line;
18470
18471 /* Determine a new base line, if the old one is too close
18472 or too far away, or if we did not have one.
18473 "Too close" means it's plausible a scroll-down would
18474 go back past it. */
18475 if (startpos == BUF_BEGV (b))
18476 {
18477 w->base_line_number = make_number (topline);
18478 w->base_line_pos = make_number (BUF_BEGV (b));
18479 }
18480 else if (nlines < height + 25 || nlines > height * 3 + 50
18481 || linepos == BUF_BEGV (b))
18482 {
18483 int limit = BUF_BEGV (b);
18484 int limit_byte = BUF_BEGV_BYTE (b);
18485 int position;
18486 int distance = (height * 2 + 30) * line_number_display_limit_width;
18487
18488 if (startpos - distance > limit)
18489 {
18490 limit = startpos - distance;
18491 limit_byte = CHAR_TO_BYTE (limit);
18492 }
18493
18494 nlines = display_count_lines (startpos, startpos_byte,
18495 limit_byte,
18496 - (height * 2 + 30),
18497 &position);
18498 /* If we couldn't find the lines we wanted within
18499 line_number_display_limit_width chars per line,
18500 give up on line numbers for this window. */
18501 if (position == limit_byte && limit == startpos - distance)
18502 {
18503 w->base_line_pos = w->buffer;
18504 w->base_line_number = Qnil;
18505 goto no_value;
18506 }
18507
18508 w->base_line_number = make_number (topline - nlines);
18509 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18510 }
18511
18512 /* Now count lines from the start pos to point. */
18513 nlines = display_count_lines (startpos, startpos_byte,
18514 PT_BYTE, PT, &junk);
18515
18516 /* Record that we did display the line number. */
18517 line_number_displayed = 1;
18518
18519 /* Make the string to show. */
18520 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18521 return decode_mode_spec_buf;
18522 no_value:
18523 {
18524 char* p = decode_mode_spec_buf;
18525 int pad = field_width - 2;
18526 while (pad-- > 0)
18527 *p++ = ' ';
18528 *p++ = '?';
18529 *p++ = '?';
18530 *p = '\0';
18531 return decode_mode_spec_buf;
18532 }
18533 }
18534 break;
18535
18536 case 'm':
18537 obj = b->mode_name;
18538 break;
18539
18540 case 'n':
18541 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18542 return " Narrow";
18543 break;
18544
18545 case 'p':
18546 {
18547 int pos = marker_position (w->start);
18548 int total = BUF_ZV (b) - BUF_BEGV (b);
18549
18550 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18551 {
18552 if (pos <= BUF_BEGV (b))
18553 return "All";
18554 else
18555 return "Bottom";
18556 }
18557 else if (pos <= BUF_BEGV (b))
18558 return "Top";
18559 else
18560 {
18561 if (total > 1000000)
18562 /* Do it differently for a large value, to avoid overflow. */
18563 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18564 else
18565 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18566 /* We can't normally display a 3-digit number,
18567 so get us a 2-digit number that is close. */
18568 if (total == 100)
18569 total = 99;
18570 sprintf (decode_mode_spec_buf, "%2d%%", total);
18571 return decode_mode_spec_buf;
18572 }
18573 }
18574
18575 /* Display percentage of size above the bottom of the screen. */
18576 case 'P':
18577 {
18578 int toppos = marker_position (w->start);
18579 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18580 int total = BUF_ZV (b) - BUF_BEGV (b);
18581
18582 if (botpos >= BUF_ZV (b))
18583 {
18584 if (toppos <= BUF_BEGV (b))
18585 return "All";
18586 else
18587 return "Bottom";
18588 }
18589 else
18590 {
18591 if (total > 1000000)
18592 /* Do it differently for a large value, to avoid overflow. */
18593 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18594 else
18595 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18596 /* We can't normally display a 3-digit number,
18597 so get us a 2-digit number that is close. */
18598 if (total == 100)
18599 total = 99;
18600 if (toppos <= BUF_BEGV (b))
18601 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18602 else
18603 sprintf (decode_mode_spec_buf, "%2d%%", total);
18604 return decode_mode_spec_buf;
18605 }
18606 }
18607
18608 case 's':
18609 /* status of process */
18610 obj = Fget_buffer_process (Fcurrent_buffer ());
18611 if (NILP (obj))
18612 return "no process";
18613 #ifdef subprocesses
18614 obj = Fsymbol_name (Fprocess_status (obj));
18615 #endif
18616 break;
18617
18618 case '@':
18619 {
18620 int count = inhibit_garbage_collection ();
18621 Lisp_Object val = call1 (intern ("file-remote-p"),
18622 current_buffer->directory);
18623 unbind_to (count, Qnil);
18624
18625 if (NILP (val))
18626 return "-";
18627 else
18628 return "@";
18629 }
18630
18631 case 't': /* indicate TEXT or BINARY */
18632 #ifdef MODE_LINE_BINARY_TEXT
18633 return MODE_LINE_BINARY_TEXT (b);
18634 #else
18635 return "T";
18636 #endif
18637
18638 case 'z':
18639 /* coding-system (not including end-of-line format) */
18640 case 'Z':
18641 /* coding-system (including end-of-line type) */
18642 {
18643 int eol_flag = (c == 'Z');
18644 char *p = decode_mode_spec_buf;
18645
18646 if (! FRAME_WINDOW_P (f))
18647 {
18648 /* No need to mention EOL here--the terminal never needs
18649 to do EOL conversion. */
18650 p = decode_mode_spec_coding (CODING_ID_NAME
18651 (FRAME_KEYBOARD_CODING (f)->id),
18652 p, 0);
18653 p = decode_mode_spec_coding (CODING_ID_NAME
18654 (FRAME_TERMINAL_CODING (f)->id),
18655 p, 0);
18656 }
18657 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18658 p, eol_flag);
18659
18660 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18661 #ifdef subprocesses
18662 obj = Fget_buffer_process (Fcurrent_buffer ());
18663 if (PROCESSP (obj))
18664 {
18665 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18666 p, eol_flag);
18667 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18668 p, eol_flag);
18669 }
18670 #endif /* subprocesses */
18671 #endif /* 0 */
18672 *p = 0;
18673 return decode_mode_spec_buf;
18674 }
18675 }
18676
18677 if (STRINGP (obj))
18678 {
18679 *string = obj;
18680 return (char *) SDATA (obj);
18681 }
18682 else
18683 return "";
18684 }
18685
18686
18687 /* Count up to COUNT lines starting from START / START_BYTE.
18688 But don't go beyond LIMIT_BYTE.
18689 Return the number of lines thus found (always nonnegative).
18690
18691 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18692
18693 static int
18694 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18695 int start, start_byte, limit_byte, count;
18696 int *byte_pos_ptr;
18697 {
18698 register unsigned char *cursor;
18699 unsigned char *base;
18700
18701 register int ceiling;
18702 register unsigned char *ceiling_addr;
18703 int orig_count = count;
18704
18705 /* If we are not in selective display mode,
18706 check only for newlines. */
18707 int selective_display = (!NILP (current_buffer->selective_display)
18708 && !INTEGERP (current_buffer->selective_display));
18709
18710 if (count > 0)
18711 {
18712 while (start_byte < limit_byte)
18713 {
18714 ceiling = BUFFER_CEILING_OF (start_byte);
18715 ceiling = min (limit_byte - 1, ceiling);
18716 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18717 base = (cursor = BYTE_POS_ADDR (start_byte));
18718 while (1)
18719 {
18720 if (selective_display)
18721 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18722 ;
18723 else
18724 while (*cursor != '\n' && ++cursor != ceiling_addr)
18725 ;
18726
18727 if (cursor != ceiling_addr)
18728 {
18729 if (--count == 0)
18730 {
18731 start_byte += cursor - base + 1;
18732 *byte_pos_ptr = start_byte;
18733 return orig_count;
18734 }
18735 else
18736 if (++cursor == ceiling_addr)
18737 break;
18738 }
18739 else
18740 break;
18741 }
18742 start_byte += cursor - base;
18743 }
18744 }
18745 else
18746 {
18747 while (start_byte > limit_byte)
18748 {
18749 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18750 ceiling = max (limit_byte, ceiling);
18751 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18752 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18753 while (1)
18754 {
18755 if (selective_display)
18756 while (--cursor != ceiling_addr
18757 && *cursor != '\n' && *cursor != 015)
18758 ;
18759 else
18760 while (--cursor != ceiling_addr && *cursor != '\n')
18761 ;
18762
18763 if (cursor != ceiling_addr)
18764 {
18765 if (++count == 0)
18766 {
18767 start_byte += cursor - base + 1;
18768 *byte_pos_ptr = start_byte;
18769 /* When scanning backwards, we should
18770 not count the newline posterior to which we stop. */
18771 return - orig_count - 1;
18772 }
18773 }
18774 else
18775 break;
18776 }
18777 /* Here we add 1 to compensate for the last decrement
18778 of CURSOR, which took it past the valid range. */
18779 start_byte += cursor - base + 1;
18780 }
18781 }
18782
18783 *byte_pos_ptr = limit_byte;
18784
18785 if (count < 0)
18786 return - orig_count + count;
18787 return orig_count - count;
18788
18789 }
18790
18791
18792 \f
18793 /***********************************************************************
18794 Displaying strings
18795 ***********************************************************************/
18796
18797 /* Display a NUL-terminated string, starting with index START.
18798
18799 If STRING is non-null, display that C string. Otherwise, the Lisp
18800 string LISP_STRING is displayed. There's a case that STRING is
18801 non-null and LISP_STRING is not nil. It means STRING is a string
18802 data of LISP_STRING. In that case, we display LISP_STRING while
18803 ignoring its text properties.
18804
18805 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18806 FACE_STRING. Display STRING or LISP_STRING with the face at
18807 FACE_STRING_POS in FACE_STRING:
18808
18809 Display the string in the environment given by IT, but use the
18810 standard display table, temporarily.
18811
18812 FIELD_WIDTH is the minimum number of output glyphs to produce.
18813 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18814 with spaces. If STRING has more characters, more than FIELD_WIDTH
18815 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18816
18817 PRECISION is the maximum number of characters to output from
18818 STRING. PRECISION < 0 means don't truncate the string.
18819
18820 This is roughly equivalent to printf format specifiers:
18821
18822 FIELD_WIDTH PRECISION PRINTF
18823 ----------------------------------------
18824 -1 -1 %s
18825 -1 10 %.10s
18826 10 -1 %10s
18827 20 10 %20.10s
18828
18829 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18830 display them, and < 0 means obey the current buffer's value of
18831 enable_multibyte_characters.
18832
18833 Value is the number of columns displayed. */
18834
18835 static int
18836 display_string (string, lisp_string, face_string, face_string_pos,
18837 start, it, field_width, precision, max_x, multibyte)
18838 unsigned char *string;
18839 Lisp_Object lisp_string;
18840 Lisp_Object face_string;
18841 EMACS_INT face_string_pos;
18842 EMACS_INT start;
18843 struct it *it;
18844 int field_width, precision, max_x;
18845 int multibyte;
18846 {
18847 int hpos_at_start = it->hpos;
18848 int saved_face_id = it->face_id;
18849 struct glyph_row *row = it->glyph_row;
18850
18851 /* Initialize the iterator IT for iteration over STRING beginning
18852 with index START. */
18853 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
18854 precision, field_width, multibyte);
18855 if (string && STRINGP (lisp_string))
18856 /* LISP_STRING is the one returned by decode_mode_spec. We should
18857 ignore its text properties. */
18858 it->stop_charpos = -1;
18859
18860 /* If displaying STRING, set up the face of the iterator
18861 from LISP_STRING, if that's given. */
18862 if (STRINGP (face_string))
18863 {
18864 EMACS_INT endptr;
18865 struct face *face;
18866
18867 it->face_id
18868 = face_at_string_position (it->w, face_string, face_string_pos,
18869 0, it->region_beg_charpos,
18870 it->region_end_charpos,
18871 &endptr, it->base_face_id, 0);
18872 face = FACE_FROM_ID (it->f, it->face_id);
18873 it->face_box_p = face->box != FACE_NO_BOX;
18874 }
18875
18876 /* Set max_x to the maximum allowed X position. Don't let it go
18877 beyond the right edge of the window. */
18878 if (max_x <= 0)
18879 max_x = it->last_visible_x;
18880 else
18881 max_x = min (max_x, it->last_visible_x);
18882
18883 /* Skip over display elements that are not visible. because IT->w is
18884 hscrolled. */
18885 if (it->current_x < it->first_visible_x)
18886 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18887 MOVE_TO_POS | MOVE_TO_X);
18888
18889 row->ascent = it->max_ascent;
18890 row->height = it->max_ascent + it->max_descent;
18891 row->phys_ascent = it->max_phys_ascent;
18892 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18893 row->extra_line_spacing = it->max_extra_line_spacing;
18894
18895 /* This condition is for the case that we are called with current_x
18896 past last_visible_x. */
18897 while (it->current_x < max_x)
18898 {
18899 int x_before, x, n_glyphs_before, i, nglyphs;
18900
18901 /* Get the next display element. */
18902 if (!get_next_display_element (it))
18903 break;
18904
18905 /* Produce glyphs. */
18906 x_before = it->current_x;
18907 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18908 PRODUCE_GLYPHS (it);
18909
18910 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18911 i = 0;
18912 x = x_before;
18913 while (i < nglyphs)
18914 {
18915 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18916
18917 if (it->line_wrap != TRUNCATE
18918 && x + glyph->pixel_width > max_x)
18919 {
18920 /* End of continued line or max_x reached. */
18921 if (CHAR_GLYPH_PADDING_P (*glyph))
18922 {
18923 /* A wide character is unbreakable. */
18924 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18925 it->current_x = x_before;
18926 }
18927 else
18928 {
18929 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18930 it->current_x = x;
18931 }
18932 break;
18933 }
18934 else if (x + glyph->pixel_width >= it->first_visible_x)
18935 {
18936 /* Glyph is at least partially visible. */
18937 ++it->hpos;
18938 if (x < it->first_visible_x)
18939 it->glyph_row->x = x - it->first_visible_x;
18940 }
18941 else
18942 {
18943 /* Glyph is off the left margin of the display area.
18944 Should not happen. */
18945 abort ();
18946 }
18947
18948 row->ascent = max (row->ascent, it->max_ascent);
18949 row->height = max (row->height, it->max_ascent + it->max_descent);
18950 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18951 row->phys_height = max (row->phys_height,
18952 it->max_phys_ascent + it->max_phys_descent);
18953 row->extra_line_spacing = max (row->extra_line_spacing,
18954 it->max_extra_line_spacing);
18955 x += glyph->pixel_width;
18956 ++i;
18957 }
18958
18959 /* Stop if max_x reached. */
18960 if (i < nglyphs)
18961 break;
18962
18963 /* Stop at line ends. */
18964 if (ITERATOR_AT_END_OF_LINE_P (it))
18965 {
18966 it->continuation_lines_width = 0;
18967 break;
18968 }
18969
18970 set_iterator_to_next (it, 1);
18971
18972 /* Stop if truncating at the right edge. */
18973 if (it->line_wrap == TRUNCATE
18974 && it->current_x >= it->last_visible_x)
18975 {
18976 /* Add truncation mark, but don't do it if the line is
18977 truncated at a padding space. */
18978 if (IT_CHARPOS (*it) < it->string_nchars)
18979 {
18980 if (!FRAME_WINDOW_P (it->f))
18981 {
18982 int i, n;
18983
18984 if (it->current_x > it->last_visible_x)
18985 {
18986 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18987 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18988 break;
18989 for (n = row->used[TEXT_AREA]; i < n; ++i)
18990 {
18991 row->used[TEXT_AREA] = i;
18992 produce_special_glyphs (it, IT_TRUNCATION);
18993 }
18994 }
18995 produce_special_glyphs (it, IT_TRUNCATION);
18996 }
18997 it->glyph_row->truncated_on_right_p = 1;
18998 }
18999 break;
19000 }
19001 }
19002
19003 /* Maybe insert a truncation at the left. */
19004 if (it->first_visible_x
19005 && IT_CHARPOS (*it) > 0)
19006 {
19007 if (!FRAME_WINDOW_P (it->f))
19008 insert_left_trunc_glyphs (it);
19009 it->glyph_row->truncated_on_left_p = 1;
19010 }
19011
19012 it->face_id = saved_face_id;
19013
19014 /* Value is number of columns displayed. */
19015 return it->hpos - hpos_at_start;
19016 }
19017
19018
19019 \f
19020 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
19021 appears as an element of LIST or as the car of an element of LIST.
19022 If PROPVAL is a list, compare each element against LIST in that
19023 way, and return 1/2 if any element of PROPVAL is found in LIST.
19024 Otherwise return 0. This function cannot quit.
19025 The return value is 2 if the text is invisible but with an ellipsis
19026 and 1 if it's invisible and without an ellipsis. */
19027
19028 int
19029 invisible_p (propval, list)
19030 register Lisp_Object propval;
19031 Lisp_Object list;
19032 {
19033 register Lisp_Object tail, proptail;
19034
19035 for (tail = list; CONSP (tail); tail = XCDR (tail))
19036 {
19037 register Lisp_Object tem;
19038 tem = XCAR (tail);
19039 if (EQ (propval, tem))
19040 return 1;
19041 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19042 return NILP (XCDR (tem)) ? 1 : 2;
19043 }
19044
19045 if (CONSP (propval))
19046 {
19047 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19048 {
19049 Lisp_Object propelt;
19050 propelt = XCAR (proptail);
19051 for (tail = list; CONSP (tail); tail = XCDR (tail))
19052 {
19053 register Lisp_Object tem;
19054 tem = XCAR (tail);
19055 if (EQ (propelt, tem))
19056 return 1;
19057 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19058 return NILP (XCDR (tem)) ? 1 : 2;
19059 }
19060 }
19061 }
19062
19063 return 0;
19064 }
19065
19066 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19067 doc: /* Non-nil if the property makes the text invisible.
19068 POS-OR-PROP can be a marker or number, in which case it is taken to be
19069 a position in the current buffer and the value of the `invisible' property
19070 is checked; or it can be some other value, which is then presumed to be the
19071 value of the `invisible' property of the text of interest.
19072 The non-nil value returned can be t for truly invisible text or something
19073 else if the text is replaced by an ellipsis. */)
19074 (pos_or_prop)
19075 Lisp_Object pos_or_prop;
19076 {
19077 Lisp_Object prop
19078 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19079 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19080 : pos_or_prop);
19081 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19082 return (invis == 0 ? Qnil
19083 : invis == 1 ? Qt
19084 : make_number (invis));
19085 }
19086
19087 /* Calculate a width or height in pixels from a specification using
19088 the following elements:
19089
19090 SPEC ::=
19091 NUM - a (fractional) multiple of the default font width/height
19092 (NUM) - specifies exactly NUM pixels
19093 UNIT - a fixed number of pixels, see below.
19094 ELEMENT - size of a display element in pixels, see below.
19095 (NUM . SPEC) - equals NUM * SPEC
19096 (+ SPEC SPEC ...) - add pixel values
19097 (- SPEC SPEC ...) - subtract pixel values
19098 (- SPEC) - negate pixel value
19099
19100 NUM ::=
19101 INT or FLOAT - a number constant
19102 SYMBOL - use symbol's (buffer local) variable binding.
19103
19104 UNIT ::=
19105 in - pixels per inch *)
19106 mm - pixels per 1/1000 meter *)
19107 cm - pixels per 1/100 meter *)
19108 width - width of current font in pixels.
19109 height - height of current font in pixels.
19110
19111 *) using the ratio(s) defined in display-pixels-per-inch.
19112
19113 ELEMENT ::=
19114
19115 left-fringe - left fringe width in pixels
19116 right-fringe - right fringe width in pixels
19117
19118 left-margin - left margin width in pixels
19119 right-margin - right margin width in pixels
19120
19121 scroll-bar - scroll-bar area width in pixels
19122
19123 Examples:
19124
19125 Pixels corresponding to 5 inches:
19126 (5 . in)
19127
19128 Total width of non-text areas on left side of window (if scroll-bar is on left):
19129 '(space :width (+ left-fringe left-margin scroll-bar))
19130
19131 Align to first text column (in header line):
19132 '(space :align-to 0)
19133
19134 Align to middle of text area minus half the width of variable `my-image'
19135 containing a loaded image:
19136 '(space :align-to (0.5 . (- text my-image)))
19137
19138 Width of left margin minus width of 1 character in the default font:
19139 '(space :width (- left-margin 1))
19140
19141 Width of left margin minus width of 2 characters in the current font:
19142 '(space :width (- left-margin (2 . width)))
19143
19144 Center 1 character over left-margin (in header line):
19145 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19146
19147 Different ways to express width of left fringe plus left margin minus one pixel:
19148 '(space :width (- (+ left-fringe left-margin) (1)))
19149 '(space :width (+ left-fringe left-margin (- (1))))
19150 '(space :width (+ left-fringe left-margin (-1)))
19151
19152 */
19153
19154 #define NUMVAL(X) \
19155 ((INTEGERP (X) || FLOATP (X)) \
19156 ? XFLOATINT (X) \
19157 : - 1)
19158
19159 int
19160 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19161 double *res;
19162 struct it *it;
19163 Lisp_Object prop;
19164 struct font *font;
19165 int width_p, *align_to;
19166 {
19167 double pixels;
19168
19169 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19170 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19171
19172 if (NILP (prop))
19173 return OK_PIXELS (0);
19174
19175 xassert (FRAME_LIVE_P (it->f));
19176
19177 if (SYMBOLP (prop))
19178 {
19179 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19180 {
19181 char *unit = SDATA (SYMBOL_NAME (prop));
19182
19183 if (unit[0] == 'i' && unit[1] == 'n')
19184 pixels = 1.0;
19185 else if (unit[0] == 'm' && unit[1] == 'm')
19186 pixels = 25.4;
19187 else if (unit[0] == 'c' && unit[1] == 'm')
19188 pixels = 2.54;
19189 else
19190 pixels = 0;
19191 if (pixels > 0)
19192 {
19193 double ppi;
19194 #ifdef HAVE_WINDOW_SYSTEM
19195 if (FRAME_WINDOW_P (it->f)
19196 && (ppi = (width_p
19197 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19198 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19199 ppi > 0))
19200 return OK_PIXELS (ppi / pixels);
19201 #endif
19202
19203 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19204 || (CONSP (Vdisplay_pixels_per_inch)
19205 && (ppi = (width_p
19206 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19207 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19208 ppi > 0)))
19209 return OK_PIXELS (ppi / pixels);
19210
19211 return 0;
19212 }
19213 }
19214
19215 #ifdef HAVE_WINDOW_SYSTEM
19216 if (EQ (prop, Qheight))
19217 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19218 if (EQ (prop, Qwidth))
19219 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19220 #else
19221 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19222 return OK_PIXELS (1);
19223 #endif
19224
19225 if (EQ (prop, Qtext))
19226 return OK_PIXELS (width_p
19227 ? window_box_width (it->w, TEXT_AREA)
19228 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19229
19230 if (align_to && *align_to < 0)
19231 {
19232 *res = 0;
19233 if (EQ (prop, Qleft))
19234 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19235 if (EQ (prop, Qright))
19236 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19237 if (EQ (prop, Qcenter))
19238 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19239 + window_box_width (it->w, TEXT_AREA) / 2);
19240 if (EQ (prop, Qleft_fringe))
19241 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19242 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19243 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19244 if (EQ (prop, Qright_fringe))
19245 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19246 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19247 : window_box_right_offset (it->w, TEXT_AREA));
19248 if (EQ (prop, Qleft_margin))
19249 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19250 if (EQ (prop, Qright_margin))
19251 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19252 if (EQ (prop, Qscroll_bar))
19253 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19254 ? 0
19255 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19256 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19257 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19258 : 0)));
19259 }
19260 else
19261 {
19262 if (EQ (prop, Qleft_fringe))
19263 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19264 if (EQ (prop, Qright_fringe))
19265 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19266 if (EQ (prop, Qleft_margin))
19267 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19268 if (EQ (prop, Qright_margin))
19269 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19270 if (EQ (prop, Qscroll_bar))
19271 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19272 }
19273
19274 prop = Fbuffer_local_value (prop, it->w->buffer);
19275 }
19276
19277 if (INTEGERP (prop) || FLOATP (prop))
19278 {
19279 int base_unit = (width_p
19280 ? FRAME_COLUMN_WIDTH (it->f)
19281 : FRAME_LINE_HEIGHT (it->f));
19282 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19283 }
19284
19285 if (CONSP (prop))
19286 {
19287 Lisp_Object car = XCAR (prop);
19288 Lisp_Object cdr = XCDR (prop);
19289
19290 if (SYMBOLP (car))
19291 {
19292 #ifdef HAVE_WINDOW_SYSTEM
19293 if (FRAME_WINDOW_P (it->f)
19294 && valid_image_p (prop))
19295 {
19296 int id = lookup_image (it->f, prop);
19297 struct image *img = IMAGE_FROM_ID (it->f, id);
19298
19299 return OK_PIXELS (width_p ? img->width : img->height);
19300 }
19301 #endif
19302 if (EQ (car, Qplus) || EQ (car, Qminus))
19303 {
19304 int first = 1;
19305 double px;
19306
19307 pixels = 0;
19308 while (CONSP (cdr))
19309 {
19310 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19311 font, width_p, align_to))
19312 return 0;
19313 if (first)
19314 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19315 else
19316 pixels += px;
19317 cdr = XCDR (cdr);
19318 }
19319 if (EQ (car, Qminus))
19320 pixels = -pixels;
19321 return OK_PIXELS (pixels);
19322 }
19323
19324 car = Fbuffer_local_value (car, it->w->buffer);
19325 }
19326
19327 if (INTEGERP (car) || FLOATP (car))
19328 {
19329 double fact;
19330 pixels = XFLOATINT (car);
19331 if (NILP (cdr))
19332 return OK_PIXELS (pixels);
19333 if (calc_pixel_width_or_height (&fact, it, cdr,
19334 font, width_p, align_to))
19335 return OK_PIXELS (pixels * fact);
19336 return 0;
19337 }
19338
19339 return 0;
19340 }
19341
19342 return 0;
19343 }
19344
19345 \f
19346 /***********************************************************************
19347 Glyph Display
19348 ***********************************************************************/
19349
19350 #ifdef HAVE_WINDOW_SYSTEM
19351
19352 #if GLYPH_DEBUG
19353
19354 void
19355 dump_glyph_string (s)
19356 struct glyph_string *s;
19357 {
19358 fprintf (stderr, "glyph string\n");
19359 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19360 s->x, s->y, s->width, s->height);
19361 fprintf (stderr, " ybase = %d\n", s->ybase);
19362 fprintf (stderr, " hl = %d\n", s->hl);
19363 fprintf (stderr, " left overhang = %d, right = %d\n",
19364 s->left_overhang, s->right_overhang);
19365 fprintf (stderr, " nchars = %d\n", s->nchars);
19366 fprintf (stderr, " extends to end of line = %d\n",
19367 s->extends_to_end_of_line_p);
19368 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19369 fprintf (stderr, " bg width = %d\n", s->background_width);
19370 }
19371
19372 #endif /* GLYPH_DEBUG */
19373
19374 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19375 of XChar2b structures for S; it can't be allocated in
19376 init_glyph_string because it must be allocated via `alloca'. W
19377 is the window on which S is drawn. ROW and AREA are the glyph row
19378 and area within the row from which S is constructed. START is the
19379 index of the first glyph structure covered by S. HL is a
19380 face-override for drawing S. */
19381
19382 #ifdef HAVE_NTGUI
19383 #define OPTIONAL_HDC(hdc) hdc,
19384 #define DECLARE_HDC(hdc) HDC hdc;
19385 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19386 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19387 #endif
19388
19389 #ifndef OPTIONAL_HDC
19390 #define OPTIONAL_HDC(hdc)
19391 #define DECLARE_HDC(hdc)
19392 #define ALLOCATE_HDC(hdc, f)
19393 #define RELEASE_HDC(hdc, f)
19394 #endif
19395
19396 static void
19397 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19398 struct glyph_string *s;
19399 DECLARE_HDC (hdc)
19400 XChar2b *char2b;
19401 struct window *w;
19402 struct glyph_row *row;
19403 enum glyph_row_area area;
19404 int start;
19405 enum draw_glyphs_face hl;
19406 {
19407 bzero (s, sizeof *s);
19408 s->w = w;
19409 s->f = XFRAME (w->frame);
19410 #ifdef HAVE_NTGUI
19411 s->hdc = hdc;
19412 #endif
19413 s->display = FRAME_X_DISPLAY (s->f);
19414 s->window = FRAME_X_WINDOW (s->f);
19415 s->char2b = char2b;
19416 s->hl = hl;
19417 s->row = row;
19418 s->area = area;
19419 s->first_glyph = row->glyphs[area] + start;
19420 s->height = row->height;
19421 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19422 s->ybase = s->y + row->ascent;
19423 }
19424
19425
19426 /* Append the list of glyph strings with head H and tail T to the list
19427 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19428
19429 static INLINE void
19430 append_glyph_string_lists (head, tail, h, t)
19431 struct glyph_string **head, **tail;
19432 struct glyph_string *h, *t;
19433 {
19434 if (h)
19435 {
19436 if (*head)
19437 (*tail)->next = h;
19438 else
19439 *head = h;
19440 h->prev = *tail;
19441 *tail = t;
19442 }
19443 }
19444
19445
19446 /* Prepend the list of glyph strings with head H and tail T to the
19447 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19448 result. */
19449
19450 static INLINE void
19451 prepend_glyph_string_lists (head, tail, h, t)
19452 struct glyph_string **head, **tail;
19453 struct glyph_string *h, *t;
19454 {
19455 if (h)
19456 {
19457 if (*head)
19458 (*head)->prev = t;
19459 else
19460 *tail = t;
19461 t->next = *head;
19462 *head = h;
19463 }
19464 }
19465
19466
19467 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19468 Set *HEAD and *TAIL to the resulting list. */
19469
19470 static INLINE void
19471 append_glyph_string (head, tail, s)
19472 struct glyph_string **head, **tail;
19473 struct glyph_string *s;
19474 {
19475 s->next = s->prev = NULL;
19476 append_glyph_string_lists (head, tail, s, s);
19477 }
19478
19479
19480 /* Get face and two-byte form of character C in face FACE_ID on frame
19481 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19482 means we want to display multibyte text. DISPLAY_P non-zero means
19483 make sure that X resources for the face returned are allocated.
19484 Value is a pointer to a realized face that is ready for display if
19485 DISPLAY_P is non-zero. */
19486
19487 static INLINE struct face *
19488 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19489 struct frame *f;
19490 int c, face_id;
19491 XChar2b *char2b;
19492 int multibyte_p, display_p;
19493 {
19494 struct face *face = FACE_FROM_ID (f, face_id);
19495
19496 if (face->font)
19497 {
19498 unsigned code = face->font->driver->encode_char (face->font, c);
19499
19500 if (code != FONT_INVALID_CODE)
19501 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19502 else
19503 STORE_XCHAR2B (char2b, 0, 0);
19504 }
19505
19506 /* Make sure X resources of the face are allocated. */
19507 #ifdef HAVE_X_WINDOWS
19508 if (display_p)
19509 #endif
19510 {
19511 xassert (face != NULL);
19512 PREPARE_FACE_FOR_DISPLAY (f, face);
19513 }
19514
19515 return face;
19516 }
19517
19518
19519 /* Get face and two-byte form of character glyph GLYPH on frame F.
19520 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19521 a pointer to a realized face that is ready for display. */
19522
19523 static INLINE struct face *
19524 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19525 struct frame *f;
19526 struct glyph *glyph;
19527 XChar2b *char2b;
19528 int *two_byte_p;
19529 {
19530 struct face *face;
19531
19532 xassert (glyph->type == CHAR_GLYPH);
19533 face = FACE_FROM_ID (f, glyph->face_id);
19534
19535 if (two_byte_p)
19536 *two_byte_p = 0;
19537
19538 if (face->font)
19539 {
19540 unsigned code;
19541
19542 if (CHAR_BYTE8_P (glyph->u.ch))
19543 code = CHAR_TO_BYTE8 (glyph->u.ch);
19544 else
19545 code = face->font->driver->encode_char (face->font, glyph->u.ch);
19546
19547 if (code != FONT_INVALID_CODE)
19548 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19549 else
19550 STORE_XCHAR2B (char2b, 0, 0);
19551 }
19552
19553 /* Make sure X resources of the face are allocated. */
19554 xassert (face != NULL);
19555 PREPARE_FACE_FOR_DISPLAY (f, face);
19556 return face;
19557 }
19558
19559
19560 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
19561 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
19562
19563 static INLINE int
19564 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
19565 {
19566 unsigned code;
19567
19568 if (CHAR_BYTE8_P (c))
19569 code = CHAR_TO_BYTE8 (c);
19570 else
19571 code = font->driver->encode_char (font, c);
19572
19573 if (code == FONT_INVALID_CODE)
19574 return 0;
19575 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19576 return 1;
19577 }
19578
19579
19580 /* Fill glyph string S with composition components specified by S->cmp.
19581
19582 BASE_FACE is the base face of the composition.
19583 S->cmp_from is the index of the first component for S.
19584
19585 OVERLAPS non-zero means S should draw the foreground only, and use
19586 its physical height for clipping. See also draw_glyphs.
19587
19588 Value is the index of a component not in S. */
19589
19590 static int
19591 fill_composite_glyph_string (s, base_face, overlaps)
19592 struct glyph_string *s;
19593 struct face *base_face;
19594 int overlaps;
19595 {
19596 int i;
19597 /* For all glyphs of this composition, starting at the offset
19598 S->cmp_from, until we reach the end of the definition or encounter a
19599 glyph that requires the different face, add it to S. */
19600 struct face *face;
19601
19602 xassert (s);
19603
19604 s->for_overlaps = overlaps;
19605 s->face = NULL;
19606 s->font = NULL;
19607 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19608 {
19609 int c = COMPOSITION_GLYPH (s->cmp, i);
19610
19611 if (c != '\t')
19612 {
19613 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19614 -1, Qnil);
19615
19616 face = get_char_face_and_encoding (s->f, c, face_id,
19617 s->char2b + i, 1, 1);
19618 if (face)
19619 {
19620 if (! s->face)
19621 {
19622 s->face = face;
19623 s->font = s->face->font;
19624 }
19625 else if (s->face != face)
19626 break;
19627 }
19628 }
19629 ++s->nchars;
19630 }
19631 s->cmp_to = i;
19632
19633 /* All glyph strings for the same composition has the same width,
19634 i.e. the width set for the first component of the composition. */
19635 s->width = s->first_glyph->pixel_width;
19636
19637 /* If the specified font could not be loaded, use the frame's
19638 default font, but record the fact that we couldn't load it in
19639 the glyph string so that we can draw rectangles for the
19640 characters of the glyph string. */
19641 if (s->font == NULL)
19642 {
19643 s->font_not_found_p = 1;
19644 s->font = FRAME_FONT (s->f);
19645 }
19646
19647 /* Adjust base line for subscript/superscript text. */
19648 s->ybase += s->first_glyph->voffset;
19649
19650 /* This glyph string must always be drawn with 16-bit functions. */
19651 s->two_byte_p = 1;
19652
19653 return s->cmp_to;
19654 }
19655
19656 static int
19657 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19658 struct glyph_string *s;
19659 int face_id;
19660 int start, end, overlaps;
19661 {
19662 struct glyph *glyph, *last;
19663 Lisp_Object lgstring;
19664 int i;
19665
19666 s->for_overlaps = overlaps;
19667 glyph = s->row->glyphs[s->area] + start;
19668 last = s->row->glyphs[s->area] + end;
19669 s->cmp_id = glyph->u.cmp.id;
19670 s->cmp_from = glyph->u.cmp.from;
19671 s->cmp_to = glyph->u.cmp.to + 1;
19672 s->face = FACE_FROM_ID (s->f, face_id);
19673 lgstring = composition_gstring_from_id (s->cmp_id);
19674 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19675 glyph++;
19676 while (glyph < last
19677 && glyph->u.cmp.automatic
19678 && glyph->u.cmp.id == s->cmp_id
19679 && s->cmp_to == glyph->u.cmp.from)
19680 s->cmp_to = (glyph++)->u.cmp.to + 1;
19681
19682 for (i = s->cmp_from; i < s->cmp_to; i++)
19683 {
19684 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19685 unsigned code = LGLYPH_CODE (lglyph);
19686
19687 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19688 }
19689 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19690 return glyph - s->row->glyphs[s->area];
19691 }
19692
19693
19694 /* Fill glyph string S from a sequence of character glyphs.
19695
19696 FACE_ID is the face id of the string. START is the index of the
19697 first glyph to consider, END is the index of the last + 1.
19698 OVERLAPS non-zero means S should draw the foreground only, and use
19699 its physical height for clipping. See also draw_glyphs.
19700
19701 Value is the index of the first glyph not in S. */
19702
19703 static int
19704 fill_glyph_string (s, face_id, start, end, overlaps)
19705 struct glyph_string *s;
19706 int face_id;
19707 int start, end, overlaps;
19708 {
19709 struct glyph *glyph, *last;
19710 int voffset;
19711 int glyph_not_available_p;
19712
19713 xassert (s->f == XFRAME (s->w->frame));
19714 xassert (s->nchars == 0);
19715 xassert (start >= 0 && end > start);
19716
19717 s->for_overlaps = overlaps;
19718 glyph = s->row->glyphs[s->area] + start;
19719 last = s->row->glyphs[s->area] + end;
19720 voffset = glyph->voffset;
19721 s->padding_p = glyph->padding_p;
19722 glyph_not_available_p = glyph->glyph_not_available_p;
19723
19724 while (glyph < last
19725 && glyph->type == CHAR_GLYPH
19726 && glyph->voffset == voffset
19727 /* Same face id implies same font, nowadays. */
19728 && glyph->face_id == face_id
19729 && glyph->glyph_not_available_p == glyph_not_available_p)
19730 {
19731 int two_byte_p;
19732
19733 s->face = get_glyph_face_and_encoding (s->f, glyph,
19734 s->char2b + s->nchars,
19735 &two_byte_p);
19736 s->two_byte_p = two_byte_p;
19737 ++s->nchars;
19738 xassert (s->nchars <= end - start);
19739 s->width += glyph->pixel_width;
19740 if (glyph++->padding_p != s->padding_p)
19741 break;
19742 }
19743
19744 s->font = s->face->font;
19745
19746 /* If the specified font could not be loaded, use the frame's font,
19747 but record the fact that we couldn't load it in
19748 S->font_not_found_p so that we can draw rectangles for the
19749 characters of the glyph string. */
19750 if (s->font == NULL || glyph_not_available_p)
19751 {
19752 s->font_not_found_p = 1;
19753 s->font = FRAME_FONT (s->f);
19754 }
19755
19756 /* Adjust base line for subscript/superscript text. */
19757 s->ybase += voffset;
19758
19759 xassert (s->face && s->face->gc);
19760 return glyph - s->row->glyphs[s->area];
19761 }
19762
19763
19764 /* Fill glyph string S from image glyph S->first_glyph. */
19765
19766 static void
19767 fill_image_glyph_string (s)
19768 struct glyph_string *s;
19769 {
19770 xassert (s->first_glyph->type == IMAGE_GLYPH);
19771 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19772 xassert (s->img);
19773 s->slice = s->first_glyph->slice;
19774 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19775 s->font = s->face->font;
19776 s->width = s->first_glyph->pixel_width;
19777
19778 /* Adjust base line for subscript/superscript text. */
19779 s->ybase += s->first_glyph->voffset;
19780 }
19781
19782
19783 /* Fill glyph string S from a sequence of stretch glyphs.
19784
19785 ROW is the glyph row in which the glyphs are found, AREA is the
19786 area within the row. START is the index of the first glyph to
19787 consider, END is the index of the last + 1.
19788
19789 Value is the index of the first glyph not in S. */
19790
19791 static int
19792 fill_stretch_glyph_string (s, row, area, start, end)
19793 struct glyph_string *s;
19794 struct glyph_row *row;
19795 enum glyph_row_area area;
19796 int start, end;
19797 {
19798 struct glyph *glyph, *last;
19799 int voffset, face_id;
19800
19801 xassert (s->first_glyph->type == STRETCH_GLYPH);
19802
19803 glyph = s->row->glyphs[s->area] + start;
19804 last = s->row->glyphs[s->area] + end;
19805 face_id = glyph->face_id;
19806 s->face = FACE_FROM_ID (s->f, face_id);
19807 s->font = s->face->font;
19808 s->width = glyph->pixel_width;
19809 s->nchars = 1;
19810 voffset = glyph->voffset;
19811
19812 for (++glyph;
19813 (glyph < last
19814 && glyph->type == STRETCH_GLYPH
19815 && glyph->voffset == voffset
19816 && glyph->face_id == face_id);
19817 ++glyph)
19818 s->width += glyph->pixel_width;
19819
19820 /* Adjust base line for subscript/superscript text. */
19821 s->ybase += voffset;
19822
19823 /* The case that face->gc == 0 is handled when drawing the glyph
19824 string by calling PREPARE_FACE_FOR_DISPLAY. */
19825 xassert (s->face);
19826 return glyph - s->row->glyphs[s->area];
19827 }
19828
19829 static struct font_metrics *
19830 get_per_char_metric (f, font, char2b)
19831 struct frame *f;
19832 struct font *font;
19833 XChar2b *char2b;
19834 {
19835 static struct font_metrics metrics;
19836 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19837
19838 if (! font || code == FONT_INVALID_CODE)
19839 return NULL;
19840 font->driver->text_extents (font, &code, 1, &metrics);
19841 return &metrics;
19842 }
19843
19844 /* EXPORT for RIF:
19845 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19846 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19847 assumed to be zero. */
19848
19849 void
19850 x_get_glyph_overhangs (glyph, f, left, right)
19851 struct glyph *glyph;
19852 struct frame *f;
19853 int *left, *right;
19854 {
19855 *left = *right = 0;
19856
19857 if (glyph->type == CHAR_GLYPH)
19858 {
19859 struct face *face;
19860 XChar2b char2b;
19861 struct font_metrics *pcm;
19862
19863 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19864 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19865 {
19866 if (pcm->rbearing > pcm->width)
19867 *right = pcm->rbearing - pcm->width;
19868 if (pcm->lbearing < 0)
19869 *left = -pcm->lbearing;
19870 }
19871 }
19872 else if (glyph->type == COMPOSITE_GLYPH)
19873 {
19874 if (! glyph->u.cmp.automatic)
19875 {
19876 struct composition *cmp = composition_table[glyph->u.cmp.id];
19877
19878 if (cmp->rbearing > cmp->pixel_width)
19879 *right = cmp->rbearing - cmp->pixel_width;
19880 if (cmp->lbearing < 0)
19881 *left = - cmp->lbearing;
19882 }
19883 else
19884 {
19885 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19886 struct font_metrics metrics;
19887
19888 composition_gstring_width (gstring, glyph->u.cmp.from,
19889 glyph->u.cmp.to + 1, &metrics);
19890 if (metrics.rbearing > metrics.width)
19891 *right = metrics.rbearing - metrics.width;
19892 if (metrics.lbearing < 0)
19893 *left = - metrics.lbearing;
19894 }
19895 }
19896 }
19897
19898
19899 /* Return the index of the first glyph preceding glyph string S that
19900 is overwritten by S because of S's left overhang. Value is -1
19901 if no glyphs are overwritten. */
19902
19903 static int
19904 left_overwritten (s)
19905 struct glyph_string *s;
19906 {
19907 int k;
19908
19909 if (s->left_overhang)
19910 {
19911 int x = 0, i;
19912 struct glyph *glyphs = s->row->glyphs[s->area];
19913 int first = s->first_glyph - glyphs;
19914
19915 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19916 x -= glyphs[i].pixel_width;
19917
19918 k = i + 1;
19919 }
19920 else
19921 k = -1;
19922
19923 return k;
19924 }
19925
19926
19927 /* Return the index of the first glyph preceding glyph string S that
19928 is overwriting S because of its right overhang. Value is -1 if no
19929 glyph in front of S overwrites S. */
19930
19931 static int
19932 left_overwriting (s)
19933 struct glyph_string *s;
19934 {
19935 int i, k, x;
19936 struct glyph *glyphs = s->row->glyphs[s->area];
19937 int first = s->first_glyph - glyphs;
19938
19939 k = -1;
19940 x = 0;
19941 for (i = first - 1; i >= 0; --i)
19942 {
19943 int left, right;
19944 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19945 if (x + right > 0)
19946 k = i;
19947 x -= glyphs[i].pixel_width;
19948 }
19949
19950 return k;
19951 }
19952
19953
19954 /* Return the index of the last glyph following glyph string S that is
19955 overwritten by S because of S's right overhang. Value is -1 if
19956 no such glyph is found. */
19957
19958 static int
19959 right_overwritten (s)
19960 struct glyph_string *s;
19961 {
19962 int k = -1;
19963
19964 if (s->right_overhang)
19965 {
19966 int x = 0, i;
19967 struct glyph *glyphs = s->row->glyphs[s->area];
19968 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19969 int end = s->row->used[s->area];
19970
19971 for (i = first; i < end && s->right_overhang > x; ++i)
19972 x += glyphs[i].pixel_width;
19973
19974 k = i;
19975 }
19976
19977 return k;
19978 }
19979
19980
19981 /* Return the index of the last glyph following glyph string S that
19982 overwrites S because of its left overhang. Value is negative
19983 if no such glyph is found. */
19984
19985 static int
19986 right_overwriting (s)
19987 struct glyph_string *s;
19988 {
19989 int i, k, x;
19990 int end = s->row->used[s->area];
19991 struct glyph *glyphs = s->row->glyphs[s->area];
19992 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19993
19994 k = -1;
19995 x = 0;
19996 for (i = first; i < end; ++i)
19997 {
19998 int left, right;
19999 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
20000 if (x - left < 0)
20001 k = i;
20002 x += glyphs[i].pixel_width;
20003 }
20004
20005 return k;
20006 }
20007
20008
20009 /* Set background width of glyph string S. START is the index of the
20010 first glyph following S. LAST_X is the right-most x-position + 1
20011 in the drawing area. */
20012
20013 static INLINE void
20014 set_glyph_string_background_width (s, start, last_x)
20015 struct glyph_string *s;
20016 int start;
20017 int last_x;
20018 {
20019 /* If the face of this glyph string has to be drawn to the end of
20020 the drawing area, set S->extends_to_end_of_line_p. */
20021
20022 if (start == s->row->used[s->area]
20023 && s->area == TEXT_AREA
20024 && ((s->row->fill_line_p
20025 && (s->hl == DRAW_NORMAL_TEXT
20026 || s->hl == DRAW_IMAGE_RAISED
20027 || s->hl == DRAW_IMAGE_SUNKEN))
20028 || s->hl == DRAW_MOUSE_FACE))
20029 s->extends_to_end_of_line_p = 1;
20030
20031 /* If S extends its face to the end of the line, set its
20032 background_width to the distance to the right edge of the drawing
20033 area. */
20034 if (s->extends_to_end_of_line_p)
20035 s->background_width = last_x - s->x + 1;
20036 else
20037 s->background_width = s->width;
20038 }
20039
20040
20041 /* Compute overhangs and x-positions for glyph string S and its
20042 predecessors, or successors. X is the starting x-position for S.
20043 BACKWARD_P non-zero means process predecessors. */
20044
20045 static void
20046 compute_overhangs_and_x (s, x, backward_p)
20047 struct glyph_string *s;
20048 int x;
20049 int backward_p;
20050 {
20051 if (backward_p)
20052 {
20053 while (s)
20054 {
20055 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20056 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20057 x -= s->width;
20058 s->x = x;
20059 s = s->prev;
20060 }
20061 }
20062 else
20063 {
20064 while (s)
20065 {
20066 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20067 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20068 s->x = x;
20069 x += s->width;
20070 s = s->next;
20071 }
20072 }
20073 }
20074
20075
20076
20077 /* The following macros are only called from draw_glyphs below.
20078 They reference the following parameters of that function directly:
20079 `w', `row', `area', and `overlap_p'
20080 as well as the following local variables:
20081 `s', `f', and `hdc' (in W32) */
20082
20083 #ifdef HAVE_NTGUI
20084 /* On W32, silently add local `hdc' variable to argument list of
20085 init_glyph_string. */
20086 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20087 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20088 #else
20089 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20090 init_glyph_string (s, char2b, w, row, area, start, hl)
20091 #endif
20092
20093 /* Add a glyph string for a stretch glyph to the list of strings
20094 between HEAD and TAIL. START is the index of the stretch glyph in
20095 row area AREA of glyph row ROW. END is the index of the last glyph
20096 in that glyph row area. X is the current output position assigned
20097 to the new glyph string constructed. HL overrides that face of the
20098 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20099 is the right-most x-position of the drawing area. */
20100
20101 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20102 and below -- keep them on one line. */
20103 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20104 do \
20105 { \
20106 s = (struct glyph_string *) alloca (sizeof *s); \
20107 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20108 START = fill_stretch_glyph_string (s, row, area, START, END); \
20109 append_glyph_string (&HEAD, &TAIL, s); \
20110 s->x = (X); \
20111 } \
20112 while (0)
20113
20114
20115 /* Add a glyph string for an image glyph to the list of strings
20116 between HEAD and TAIL. START is the index of the image glyph in
20117 row area AREA of glyph row ROW. END is the index of the last glyph
20118 in that glyph row area. X is the current output position assigned
20119 to the new glyph string constructed. HL overrides that face of the
20120 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20121 is the right-most x-position of the drawing area. */
20122
20123 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20124 do \
20125 { \
20126 s = (struct glyph_string *) alloca (sizeof *s); \
20127 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20128 fill_image_glyph_string (s); \
20129 append_glyph_string (&HEAD, &TAIL, s); \
20130 ++START; \
20131 s->x = (X); \
20132 } \
20133 while (0)
20134
20135
20136 /* Add a glyph string for a sequence of character glyphs to the list
20137 of strings between HEAD and TAIL. START is the index of the first
20138 glyph in row area AREA of glyph row ROW that is part of the new
20139 glyph string. END is the index of the last glyph in that glyph row
20140 area. X is the current output position assigned to the new glyph
20141 string constructed. HL overrides that face of the glyph; e.g. it
20142 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20143 right-most x-position of the drawing area. */
20144
20145 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20146 do \
20147 { \
20148 int face_id; \
20149 XChar2b *char2b; \
20150 \
20151 face_id = (row)->glyphs[area][START].face_id; \
20152 \
20153 s = (struct glyph_string *) alloca (sizeof *s); \
20154 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20155 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20156 append_glyph_string (&HEAD, &TAIL, s); \
20157 s->x = (X); \
20158 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20159 } \
20160 while (0)
20161
20162
20163 /* Add a glyph string for a composite sequence to the list of strings
20164 between HEAD and TAIL. START is the index of the first glyph in
20165 row area AREA of glyph row ROW that is part of the new glyph
20166 string. END is the index of the last glyph in that glyph row area.
20167 X is the current output position assigned to the new glyph string
20168 constructed. HL overrides that face of the glyph; e.g. it is
20169 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20170 x-position of the drawing area. */
20171
20172 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20173 do { \
20174 int face_id = (row)->glyphs[area][START].face_id; \
20175 struct face *base_face = FACE_FROM_ID (f, face_id); \
20176 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20177 struct composition *cmp = composition_table[cmp_id]; \
20178 XChar2b *char2b; \
20179 struct glyph_string *first_s; \
20180 int n; \
20181 \
20182 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20183 \
20184 /* Make glyph_strings for each glyph sequence that is drawable by \
20185 the same face, and append them to HEAD/TAIL. */ \
20186 for (n = 0; n < cmp->glyph_len;) \
20187 { \
20188 s = (struct glyph_string *) alloca (sizeof *s); \
20189 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20190 append_glyph_string (&(HEAD), &(TAIL), s); \
20191 s->cmp = cmp; \
20192 s->cmp_from = n; \
20193 s->x = (X); \
20194 if (n == 0) \
20195 first_s = s; \
20196 n = fill_composite_glyph_string (s, base_face, overlaps); \
20197 } \
20198 \
20199 ++START; \
20200 s = first_s; \
20201 } while (0)
20202
20203
20204 /* Add a glyph string for a glyph-string sequence to the list of strings
20205 between HEAD and TAIL. */
20206
20207 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20208 do { \
20209 int face_id; \
20210 XChar2b *char2b; \
20211 Lisp_Object gstring; \
20212 \
20213 face_id = (row)->glyphs[area][START].face_id; \
20214 gstring = (composition_gstring_from_id \
20215 ((row)->glyphs[area][START].u.cmp.id)); \
20216 s = (struct glyph_string *) alloca (sizeof *s); \
20217 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20218 * LGSTRING_GLYPH_LEN (gstring)); \
20219 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20220 append_glyph_string (&(HEAD), &(TAIL), s); \
20221 s->x = (X); \
20222 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20223 } while (0)
20224
20225
20226 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20227 of AREA of glyph row ROW on window W between indices START and END.
20228 HL overrides the face for drawing glyph strings, e.g. it is
20229 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20230 x-positions of the drawing area.
20231
20232 This is an ugly monster macro construct because we must use alloca
20233 to allocate glyph strings (because draw_glyphs can be called
20234 asynchronously). */
20235
20236 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20237 do \
20238 { \
20239 HEAD = TAIL = NULL; \
20240 while (START < END) \
20241 { \
20242 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20243 switch (first_glyph->type) \
20244 { \
20245 case CHAR_GLYPH: \
20246 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20247 HL, X, LAST_X); \
20248 break; \
20249 \
20250 case COMPOSITE_GLYPH: \
20251 if (first_glyph->u.cmp.automatic) \
20252 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20253 HL, X, LAST_X); \
20254 else \
20255 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20256 HL, X, LAST_X); \
20257 break; \
20258 \
20259 case STRETCH_GLYPH: \
20260 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20261 HL, X, LAST_X); \
20262 break; \
20263 \
20264 case IMAGE_GLYPH: \
20265 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20266 HL, X, LAST_X); \
20267 break; \
20268 \
20269 default: \
20270 abort (); \
20271 } \
20272 \
20273 if (s) \
20274 { \
20275 set_glyph_string_background_width (s, START, LAST_X); \
20276 (X) += s->width; \
20277 } \
20278 } \
20279 } while (0)
20280
20281
20282 /* Draw glyphs between START and END in AREA of ROW on window W,
20283 starting at x-position X. X is relative to AREA in W. HL is a
20284 face-override with the following meaning:
20285
20286 DRAW_NORMAL_TEXT draw normally
20287 DRAW_CURSOR draw in cursor face
20288 DRAW_MOUSE_FACE draw in mouse face.
20289 DRAW_INVERSE_VIDEO draw in mode line face
20290 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20291 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20292
20293 If OVERLAPS is non-zero, draw only the foreground of characters and
20294 clip to the physical height of ROW. Non-zero value also defines
20295 the overlapping part to be drawn:
20296
20297 OVERLAPS_PRED overlap with preceding rows
20298 OVERLAPS_SUCC overlap with succeeding rows
20299 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20300 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20301
20302 Value is the x-position reached, relative to AREA of W. */
20303
20304 static int
20305 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20306 struct window *w;
20307 int x;
20308 struct glyph_row *row;
20309 enum glyph_row_area area;
20310 EMACS_INT start, end;
20311 enum draw_glyphs_face hl;
20312 int overlaps;
20313 {
20314 struct glyph_string *head, *tail;
20315 struct glyph_string *s;
20316 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20317 int i, j, x_reached, last_x, area_left = 0;
20318 struct frame *f = XFRAME (WINDOW_FRAME (w));
20319 DECLARE_HDC (hdc);
20320
20321 ALLOCATE_HDC (hdc, f);
20322
20323 /* Let's rather be paranoid than getting a SEGV. */
20324 end = min (end, row->used[area]);
20325 start = max (0, start);
20326 start = min (end, start);
20327
20328 /* Translate X to frame coordinates. Set last_x to the right
20329 end of the drawing area. */
20330 if (row->full_width_p)
20331 {
20332 /* X is relative to the left edge of W, without scroll bars
20333 or fringes. */
20334 area_left = WINDOW_LEFT_EDGE_X (w);
20335 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20336 }
20337 else
20338 {
20339 area_left = window_box_left (w, area);
20340 last_x = area_left + window_box_width (w, area);
20341 }
20342 x += area_left;
20343
20344 /* Build a doubly-linked list of glyph_string structures between
20345 head and tail from what we have to draw. Note that the macro
20346 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20347 the reason we use a separate variable `i'. */
20348 i = start;
20349 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20350 if (tail)
20351 x_reached = tail->x + tail->background_width;
20352 else
20353 x_reached = x;
20354
20355 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20356 the row, redraw some glyphs in front or following the glyph
20357 strings built above. */
20358 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20359 {
20360 struct glyph_string *h, *t;
20361 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20362 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20363 int dummy_x = 0;
20364
20365 /* If mouse highlighting is on, we may need to draw adjacent
20366 glyphs using mouse-face highlighting. */
20367 if (area == TEXT_AREA && row->mouse_face_p)
20368 {
20369 struct glyph_row *mouse_beg_row, *mouse_end_row;
20370
20371 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20372 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20373
20374 if (row >= mouse_beg_row && row <= mouse_end_row)
20375 {
20376 check_mouse_face = 1;
20377 mouse_beg_col = (row == mouse_beg_row)
20378 ? dpyinfo->mouse_face_beg_col : 0;
20379 mouse_end_col = (row == mouse_end_row)
20380 ? dpyinfo->mouse_face_end_col
20381 : row->used[TEXT_AREA];
20382 }
20383 }
20384
20385 /* Compute overhangs for all glyph strings. */
20386 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20387 for (s = head; s; s = s->next)
20388 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20389
20390 /* Prepend glyph strings for glyphs in front of the first glyph
20391 string that are overwritten because of the first glyph
20392 string's left overhang. The background of all strings
20393 prepended must be drawn because the first glyph string
20394 draws over it. */
20395 i = left_overwritten (head);
20396 if (i >= 0)
20397 {
20398 enum draw_glyphs_face overlap_hl;
20399
20400 /* If this row contains mouse highlighting, attempt to draw
20401 the overlapped glyphs with the correct highlight. This
20402 code fails if the overlap encompasses more than one glyph
20403 and mouse-highlight spans only some of these glyphs.
20404 However, making it work perfectly involves a lot more
20405 code, and I don't know if the pathological case occurs in
20406 practice, so we'll stick to this for now. --- cyd */
20407 if (check_mouse_face
20408 && mouse_beg_col < start && mouse_end_col > i)
20409 overlap_hl = DRAW_MOUSE_FACE;
20410 else
20411 overlap_hl = DRAW_NORMAL_TEXT;
20412
20413 j = i;
20414 BUILD_GLYPH_STRINGS (j, start, h, t,
20415 overlap_hl, dummy_x, last_x);
20416 start = i;
20417 compute_overhangs_and_x (t, head->x, 1);
20418 prepend_glyph_string_lists (&head, &tail, h, t);
20419 clip_head = head;
20420 }
20421
20422 /* Prepend glyph strings for glyphs in front of the first glyph
20423 string that overwrite that glyph string because of their
20424 right overhang. For these strings, only the foreground must
20425 be drawn, because it draws over the glyph string at `head'.
20426 The background must not be drawn because this would overwrite
20427 right overhangs of preceding glyphs for which no glyph
20428 strings exist. */
20429 i = left_overwriting (head);
20430 if (i >= 0)
20431 {
20432 enum draw_glyphs_face overlap_hl;
20433
20434 if (check_mouse_face
20435 && mouse_beg_col < start && mouse_end_col > i)
20436 overlap_hl = DRAW_MOUSE_FACE;
20437 else
20438 overlap_hl = DRAW_NORMAL_TEXT;
20439
20440 clip_head = head;
20441 BUILD_GLYPH_STRINGS (i, start, h, t,
20442 overlap_hl, dummy_x, last_x);
20443 for (s = h; s; s = s->next)
20444 s->background_filled_p = 1;
20445 compute_overhangs_and_x (t, head->x, 1);
20446 prepend_glyph_string_lists (&head, &tail, h, t);
20447 }
20448
20449 /* Append glyphs strings for glyphs following the last glyph
20450 string tail that are overwritten by tail. The background of
20451 these strings has to be drawn because tail's foreground draws
20452 over it. */
20453 i = right_overwritten (tail);
20454 if (i >= 0)
20455 {
20456 enum draw_glyphs_face overlap_hl;
20457
20458 if (check_mouse_face
20459 && mouse_beg_col < i && mouse_end_col > end)
20460 overlap_hl = DRAW_MOUSE_FACE;
20461 else
20462 overlap_hl = DRAW_NORMAL_TEXT;
20463
20464 BUILD_GLYPH_STRINGS (end, i, h, t,
20465 overlap_hl, x, last_x);
20466 /* Because BUILD_GLYPH_STRINGS updates the first argument,
20467 we don't have `end = i;' here. */
20468 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20469 append_glyph_string_lists (&head, &tail, h, t);
20470 clip_tail = tail;
20471 }
20472
20473 /* Append glyph strings for glyphs following the last glyph
20474 string tail that overwrite tail. The foreground of such
20475 glyphs has to be drawn because it writes into the background
20476 of tail. The background must not be drawn because it could
20477 paint over the foreground of following glyphs. */
20478 i = right_overwriting (tail);
20479 if (i >= 0)
20480 {
20481 enum draw_glyphs_face overlap_hl;
20482 if (check_mouse_face
20483 && mouse_beg_col < i && mouse_end_col > end)
20484 overlap_hl = DRAW_MOUSE_FACE;
20485 else
20486 overlap_hl = DRAW_NORMAL_TEXT;
20487
20488 clip_tail = tail;
20489 i++; /* We must include the Ith glyph. */
20490 BUILD_GLYPH_STRINGS (end, i, h, t,
20491 overlap_hl, x, last_x);
20492 for (s = h; s; s = s->next)
20493 s->background_filled_p = 1;
20494 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20495 append_glyph_string_lists (&head, &tail, h, t);
20496 }
20497 if (clip_head || clip_tail)
20498 for (s = head; s; s = s->next)
20499 {
20500 s->clip_head = clip_head;
20501 s->clip_tail = clip_tail;
20502 }
20503 }
20504
20505 /* Draw all strings. */
20506 for (s = head; s; s = s->next)
20507 FRAME_RIF (f)->draw_glyph_string (s);
20508
20509 #ifndef HAVE_NS
20510 /* When focus a sole frame and move horizontally, this sets on_p to 0
20511 causing a failure to erase prev cursor position. */
20512 if (area == TEXT_AREA
20513 && !row->full_width_p
20514 /* When drawing overlapping rows, only the glyph strings'
20515 foreground is drawn, which doesn't erase a cursor
20516 completely. */
20517 && !overlaps)
20518 {
20519 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20520 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20521 : (tail ? tail->x + tail->background_width : x));
20522 x0 -= area_left;
20523 x1 -= area_left;
20524
20525 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20526 row->y, MATRIX_ROW_BOTTOM_Y (row));
20527 }
20528 #endif
20529
20530 /* Value is the x-position up to which drawn, relative to AREA of W.
20531 This doesn't include parts drawn because of overhangs. */
20532 if (row->full_width_p)
20533 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20534 else
20535 x_reached -= area_left;
20536
20537 RELEASE_HDC (hdc, f);
20538
20539 return x_reached;
20540 }
20541
20542 /* Expand row matrix if too narrow. Don't expand if area
20543 is not present. */
20544
20545 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20546 { \
20547 if (!fonts_changed_p \
20548 && (it->glyph_row->glyphs[area] \
20549 < it->glyph_row->glyphs[area + 1])) \
20550 { \
20551 it->w->ncols_scale_factor++; \
20552 fonts_changed_p = 1; \
20553 } \
20554 }
20555
20556 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20557 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20558
20559 static INLINE void
20560 append_glyph (it)
20561 struct it *it;
20562 {
20563 struct glyph *glyph;
20564 enum glyph_row_area area = it->area;
20565
20566 xassert (it->glyph_row);
20567 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20568
20569 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20570 if (glyph < it->glyph_row->glyphs[area + 1])
20571 {
20572 glyph->charpos = CHARPOS (it->position);
20573 glyph->object = it->object;
20574 if (it->pixel_width > 0)
20575 {
20576 glyph->pixel_width = it->pixel_width;
20577 glyph->padding_p = 0;
20578 }
20579 else
20580 {
20581 /* Assure at least 1-pixel width. Otherwise, cursor can't
20582 be displayed correctly. */
20583 glyph->pixel_width = 1;
20584 glyph->padding_p = 1;
20585 }
20586 glyph->ascent = it->ascent;
20587 glyph->descent = it->descent;
20588 glyph->voffset = it->voffset;
20589 glyph->type = CHAR_GLYPH;
20590 glyph->avoid_cursor_p = it->avoid_cursor_p;
20591 glyph->multibyte_p = it->multibyte_p;
20592 glyph->left_box_line_p = it->start_of_box_run_p;
20593 glyph->right_box_line_p = it->end_of_box_run_p;
20594 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20595 || it->phys_descent > it->descent);
20596 glyph->glyph_not_available_p = it->glyph_not_available_p;
20597 glyph->face_id = it->face_id;
20598 glyph->u.ch = it->char_to_display;
20599 glyph->slice = null_glyph_slice;
20600 glyph->font_type = FONT_TYPE_UNKNOWN;
20601 ++it->glyph_row->used[area];
20602 }
20603 else
20604 IT_EXPAND_MATRIX_WIDTH (it, area);
20605 }
20606
20607 /* Store one glyph for the composition IT->cmp_it.id in
20608 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20609 non-null. */
20610
20611 static INLINE void
20612 append_composite_glyph (it)
20613 struct it *it;
20614 {
20615 struct glyph *glyph;
20616 enum glyph_row_area area = it->area;
20617
20618 xassert (it->glyph_row);
20619
20620 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20621 if (glyph < it->glyph_row->glyphs[area + 1])
20622 {
20623 glyph->charpos = CHARPOS (it->position);
20624 glyph->object = it->object;
20625 glyph->pixel_width = it->pixel_width;
20626 glyph->ascent = it->ascent;
20627 glyph->descent = it->descent;
20628 glyph->voffset = it->voffset;
20629 glyph->type = COMPOSITE_GLYPH;
20630 if (it->cmp_it.ch < 0)
20631 {
20632 glyph->u.cmp.automatic = 0;
20633 glyph->u.cmp.id = it->cmp_it.id;
20634 }
20635 else
20636 {
20637 glyph->u.cmp.automatic = 1;
20638 glyph->u.cmp.id = it->cmp_it.id;
20639 glyph->u.cmp.from = it->cmp_it.from;
20640 glyph->u.cmp.to = it->cmp_it.to - 1;
20641 }
20642 glyph->avoid_cursor_p = it->avoid_cursor_p;
20643 glyph->multibyte_p = it->multibyte_p;
20644 glyph->left_box_line_p = it->start_of_box_run_p;
20645 glyph->right_box_line_p = it->end_of_box_run_p;
20646 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20647 || it->phys_descent > it->descent);
20648 glyph->padding_p = 0;
20649 glyph->glyph_not_available_p = 0;
20650 glyph->face_id = it->face_id;
20651 glyph->slice = null_glyph_slice;
20652 glyph->font_type = FONT_TYPE_UNKNOWN;
20653 ++it->glyph_row->used[area];
20654 }
20655 else
20656 IT_EXPAND_MATRIX_WIDTH (it, area);
20657 }
20658
20659
20660 /* Change IT->ascent and IT->height according to the setting of
20661 IT->voffset. */
20662
20663 static INLINE void
20664 take_vertical_position_into_account (it)
20665 struct it *it;
20666 {
20667 if (it->voffset)
20668 {
20669 if (it->voffset < 0)
20670 /* Increase the ascent so that we can display the text higher
20671 in the line. */
20672 it->ascent -= it->voffset;
20673 else
20674 /* Increase the descent so that we can display the text lower
20675 in the line. */
20676 it->descent += it->voffset;
20677 }
20678 }
20679
20680
20681 /* Produce glyphs/get display metrics for the image IT is loaded with.
20682 See the description of struct display_iterator in dispextern.h for
20683 an overview of struct display_iterator. */
20684
20685 static void
20686 produce_image_glyph (it)
20687 struct it *it;
20688 {
20689 struct image *img;
20690 struct face *face;
20691 int glyph_ascent, crop;
20692 struct glyph_slice slice;
20693
20694 xassert (it->what == IT_IMAGE);
20695
20696 face = FACE_FROM_ID (it->f, it->face_id);
20697 xassert (face);
20698 /* Make sure X resources of the face is loaded. */
20699 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20700
20701 if (it->image_id < 0)
20702 {
20703 /* Fringe bitmap. */
20704 it->ascent = it->phys_ascent = 0;
20705 it->descent = it->phys_descent = 0;
20706 it->pixel_width = 0;
20707 it->nglyphs = 0;
20708 return;
20709 }
20710
20711 img = IMAGE_FROM_ID (it->f, it->image_id);
20712 xassert (img);
20713 /* Make sure X resources of the image is loaded. */
20714 prepare_image_for_display (it->f, img);
20715
20716 slice.x = slice.y = 0;
20717 slice.width = img->width;
20718 slice.height = img->height;
20719
20720 if (INTEGERP (it->slice.x))
20721 slice.x = XINT (it->slice.x);
20722 else if (FLOATP (it->slice.x))
20723 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20724
20725 if (INTEGERP (it->slice.y))
20726 slice.y = XINT (it->slice.y);
20727 else if (FLOATP (it->slice.y))
20728 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20729
20730 if (INTEGERP (it->slice.width))
20731 slice.width = XINT (it->slice.width);
20732 else if (FLOATP (it->slice.width))
20733 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20734
20735 if (INTEGERP (it->slice.height))
20736 slice.height = XINT (it->slice.height);
20737 else if (FLOATP (it->slice.height))
20738 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20739
20740 if (slice.x >= img->width)
20741 slice.x = img->width;
20742 if (slice.y >= img->height)
20743 slice.y = img->height;
20744 if (slice.x + slice.width >= img->width)
20745 slice.width = img->width - slice.x;
20746 if (slice.y + slice.height > img->height)
20747 slice.height = img->height - slice.y;
20748
20749 if (slice.width == 0 || slice.height == 0)
20750 return;
20751
20752 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20753
20754 it->descent = slice.height - glyph_ascent;
20755 if (slice.y == 0)
20756 it->descent += img->vmargin;
20757 if (slice.y + slice.height == img->height)
20758 it->descent += img->vmargin;
20759 it->phys_descent = it->descent;
20760
20761 it->pixel_width = slice.width;
20762 if (slice.x == 0)
20763 it->pixel_width += img->hmargin;
20764 if (slice.x + slice.width == img->width)
20765 it->pixel_width += img->hmargin;
20766
20767 /* It's quite possible for images to have an ascent greater than
20768 their height, so don't get confused in that case. */
20769 if (it->descent < 0)
20770 it->descent = 0;
20771
20772 it->nglyphs = 1;
20773
20774 if (face->box != FACE_NO_BOX)
20775 {
20776 if (face->box_line_width > 0)
20777 {
20778 if (slice.y == 0)
20779 it->ascent += face->box_line_width;
20780 if (slice.y + slice.height == img->height)
20781 it->descent += face->box_line_width;
20782 }
20783
20784 if (it->start_of_box_run_p && slice.x == 0)
20785 it->pixel_width += eabs (face->box_line_width);
20786 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20787 it->pixel_width += eabs (face->box_line_width);
20788 }
20789
20790 take_vertical_position_into_account (it);
20791
20792 /* Automatically crop wide image glyphs at right edge so we can
20793 draw the cursor on same display row. */
20794 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20795 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20796 {
20797 it->pixel_width -= crop;
20798 slice.width -= crop;
20799 }
20800
20801 if (it->glyph_row)
20802 {
20803 struct glyph *glyph;
20804 enum glyph_row_area area = it->area;
20805
20806 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20807 if (glyph < it->glyph_row->glyphs[area + 1])
20808 {
20809 glyph->charpos = CHARPOS (it->position);
20810 glyph->object = it->object;
20811 glyph->pixel_width = it->pixel_width;
20812 glyph->ascent = glyph_ascent;
20813 glyph->descent = it->descent;
20814 glyph->voffset = it->voffset;
20815 glyph->type = IMAGE_GLYPH;
20816 glyph->avoid_cursor_p = it->avoid_cursor_p;
20817 glyph->multibyte_p = it->multibyte_p;
20818 glyph->left_box_line_p = it->start_of_box_run_p;
20819 glyph->right_box_line_p = it->end_of_box_run_p;
20820 glyph->overlaps_vertically_p = 0;
20821 glyph->padding_p = 0;
20822 glyph->glyph_not_available_p = 0;
20823 glyph->face_id = it->face_id;
20824 glyph->u.img_id = img->id;
20825 glyph->slice = slice;
20826 glyph->font_type = FONT_TYPE_UNKNOWN;
20827 ++it->glyph_row->used[area];
20828 }
20829 else
20830 IT_EXPAND_MATRIX_WIDTH (it, area);
20831 }
20832 }
20833
20834
20835 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20836 of the glyph, WIDTH and HEIGHT are the width and height of the
20837 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20838
20839 static void
20840 append_stretch_glyph (it, object, width, height, ascent)
20841 struct it *it;
20842 Lisp_Object object;
20843 int width, height;
20844 int ascent;
20845 {
20846 struct glyph *glyph;
20847 enum glyph_row_area area = it->area;
20848
20849 xassert (ascent >= 0 && ascent <= height);
20850
20851 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20852 if (glyph < it->glyph_row->glyphs[area + 1])
20853 {
20854 glyph->charpos = CHARPOS (it->position);
20855 glyph->object = object;
20856 glyph->pixel_width = width;
20857 glyph->ascent = ascent;
20858 glyph->descent = height - ascent;
20859 glyph->voffset = it->voffset;
20860 glyph->type = STRETCH_GLYPH;
20861 glyph->avoid_cursor_p = it->avoid_cursor_p;
20862 glyph->multibyte_p = it->multibyte_p;
20863 glyph->left_box_line_p = it->start_of_box_run_p;
20864 glyph->right_box_line_p = it->end_of_box_run_p;
20865 glyph->overlaps_vertically_p = 0;
20866 glyph->padding_p = 0;
20867 glyph->glyph_not_available_p = 0;
20868 glyph->face_id = it->face_id;
20869 glyph->u.stretch.ascent = ascent;
20870 glyph->u.stretch.height = height;
20871 glyph->slice = null_glyph_slice;
20872 glyph->font_type = FONT_TYPE_UNKNOWN;
20873 ++it->glyph_row->used[area];
20874 }
20875 else
20876 IT_EXPAND_MATRIX_WIDTH (it, area);
20877 }
20878
20879
20880 /* Produce a stretch glyph for iterator IT. IT->object is the value
20881 of the glyph property displayed. The value must be a list
20882 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20883 being recognized:
20884
20885 1. `:width WIDTH' specifies that the space should be WIDTH *
20886 canonical char width wide. WIDTH may be an integer or floating
20887 point number.
20888
20889 2. `:relative-width FACTOR' specifies that the width of the stretch
20890 should be computed from the width of the first character having the
20891 `glyph' property, and should be FACTOR times that width.
20892
20893 3. `:align-to HPOS' specifies that the space should be wide enough
20894 to reach HPOS, a value in canonical character units.
20895
20896 Exactly one of the above pairs must be present.
20897
20898 4. `:height HEIGHT' specifies that the height of the stretch produced
20899 should be HEIGHT, measured in canonical character units.
20900
20901 5. `:relative-height FACTOR' specifies that the height of the
20902 stretch should be FACTOR times the height of the characters having
20903 the glyph property.
20904
20905 Either none or exactly one of 4 or 5 must be present.
20906
20907 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20908 of the stretch should be used for the ascent of the stretch.
20909 ASCENT must be in the range 0 <= ASCENT <= 100. */
20910
20911 static void
20912 produce_stretch_glyph (it)
20913 struct it *it;
20914 {
20915 /* (space :width WIDTH :height HEIGHT ...) */
20916 Lisp_Object prop, plist;
20917 int width = 0, height = 0, align_to = -1;
20918 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20919 int ascent = 0;
20920 double tem;
20921 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20922 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20923
20924 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20925
20926 /* List should start with `space'. */
20927 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20928 plist = XCDR (it->object);
20929
20930 /* Compute the width of the stretch. */
20931 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20932 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20933 {
20934 /* Absolute width `:width WIDTH' specified and valid. */
20935 zero_width_ok_p = 1;
20936 width = (int)tem;
20937 }
20938 else if (prop = Fplist_get (plist, QCrelative_width),
20939 NUMVAL (prop) > 0)
20940 {
20941 /* Relative width `:relative-width FACTOR' specified and valid.
20942 Compute the width of the characters having the `glyph'
20943 property. */
20944 struct it it2;
20945 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20946
20947 it2 = *it;
20948 if (it->multibyte_p)
20949 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
20950 else
20951 {
20952 it2.c = it2.char_to_display = *p, it2.len = 1;
20953 if (! ASCII_CHAR_P (it2.c))
20954 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
20955 }
20956
20957 it2.glyph_row = NULL;
20958 it2.what = IT_CHARACTER;
20959 x_produce_glyphs (&it2);
20960 width = NUMVAL (prop) * it2.pixel_width;
20961 }
20962 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20963 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20964 {
20965 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20966 align_to = (align_to < 0
20967 ? 0
20968 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20969 else if (align_to < 0)
20970 align_to = window_box_left_offset (it->w, TEXT_AREA);
20971 width = max (0, (int)tem + align_to - it->current_x);
20972 zero_width_ok_p = 1;
20973 }
20974 else
20975 /* Nothing specified -> width defaults to canonical char width. */
20976 width = FRAME_COLUMN_WIDTH (it->f);
20977
20978 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20979 width = 1;
20980
20981 /* Compute height. */
20982 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20983 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20984 {
20985 height = (int)tem;
20986 zero_height_ok_p = 1;
20987 }
20988 else if (prop = Fplist_get (plist, QCrelative_height),
20989 NUMVAL (prop) > 0)
20990 height = FONT_HEIGHT (font) * NUMVAL (prop);
20991 else
20992 height = FONT_HEIGHT (font);
20993
20994 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20995 height = 1;
20996
20997 /* Compute percentage of height used for ascent. If
20998 `:ascent ASCENT' is present and valid, use that. Otherwise,
20999 derive the ascent from the font in use. */
21000 if (prop = Fplist_get (plist, QCascent),
21001 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
21002 ascent = height * NUMVAL (prop) / 100.0;
21003 else if (!NILP (prop)
21004 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
21005 ascent = min (max (0, (int)tem), height);
21006 else
21007 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
21008
21009 if (width > 0 && it->line_wrap != TRUNCATE
21010 && it->current_x + width > it->last_visible_x)
21011 width = it->last_visible_x - it->current_x - 1;
21012
21013 if (width > 0 && height > 0 && it->glyph_row)
21014 {
21015 Lisp_Object object = it->stack[it->sp - 1].string;
21016 if (!STRINGP (object))
21017 object = it->w->buffer;
21018 append_stretch_glyph (it, object, width, height, ascent);
21019 }
21020
21021 it->pixel_width = width;
21022 it->ascent = it->phys_ascent = ascent;
21023 it->descent = it->phys_descent = height - it->ascent;
21024 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
21025
21026 take_vertical_position_into_account (it);
21027 }
21028
21029 /* Calculate line-height and line-spacing properties.
21030 An integer value specifies explicit pixel value.
21031 A float value specifies relative value to current face height.
21032 A cons (float . face-name) specifies relative value to
21033 height of specified face font.
21034
21035 Returns height in pixels, or nil. */
21036
21037
21038 static Lisp_Object
21039 calc_line_height_property (it, val, font, boff, override)
21040 struct it *it;
21041 Lisp_Object val;
21042 struct font *font;
21043 int boff, override;
21044 {
21045 Lisp_Object face_name = Qnil;
21046 int ascent, descent, height;
21047
21048 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21049 return val;
21050
21051 if (CONSP (val))
21052 {
21053 face_name = XCAR (val);
21054 val = XCDR (val);
21055 if (!NUMBERP (val))
21056 val = make_number (1);
21057 if (NILP (face_name))
21058 {
21059 height = it->ascent + it->descent;
21060 goto scale;
21061 }
21062 }
21063
21064 if (NILP (face_name))
21065 {
21066 font = FRAME_FONT (it->f);
21067 boff = FRAME_BASELINE_OFFSET (it->f);
21068 }
21069 else if (EQ (face_name, Qt))
21070 {
21071 override = 0;
21072 }
21073 else
21074 {
21075 int face_id;
21076 struct face *face;
21077
21078 face_id = lookup_named_face (it->f, face_name, 0);
21079 if (face_id < 0)
21080 return make_number (-1);
21081
21082 face = FACE_FROM_ID (it->f, face_id);
21083 font = face->font;
21084 if (font == NULL)
21085 return make_number (-1);
21086 boff = font->baseline_offset;
21087 if (font->vertical_centering)
21088 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21089 }
21090
21091 ascent = FONT_BASE (font) + boff;
21092 descent = FONT_DESCENT (font) - boff;
21093
21094 if (override)
21095 {
21096 it->override_ascent = ascent;
21097 it->override_descent = descent;
21098 it->override_boff = boff;
21099 }
21100
21101 height = ascent + descent;
21102
21103 scale:
21104 if (FLOATP (val))
21105 height = (int)(XFLOAT_DATA (val) * height);
21106 else if (INTEGERP (val))
21107 height *= XINT (val);
21108
21109 return make_number (height);
21110 }
21111
21112
21113 /* RIF:
21114 Produce glyphs/get display metrics for the display element IT is
21115 loaded with. See the description of struct it in dispextern.h
21116 for an overview of struct it. */
21117
21118 void
21119 x_produce_glyphs (it)
21120 struct it *it;
21121 {
21122 int extra_line_spacing = it->extra_line_spacing;
21123
21124 it->glyph_not_available_p = 0;
21125
21126 if (it->what == IT_CHARACTER)
21127 {
21128 XChar2b char2b;
21129 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21130 struct font *font = face->font;
21131 int font_not_found_p = font == NULL;
21132 struct font_metrics *pcm = NULL;
21133 int boff; /* baseline offset */
21134
21135 if (font_not_found_p)
21136 {
21137 /* When no suitable font found, display an empty box based
21138 on the metrics of the font of the default face (or what
21139 remapped). */
21140 struct face *no_font_face
21141 = FACE_FROM_ID (it->f,
21142 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21143 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21144 font = no_font_face->font;
21145 boff = font->baseline_offset;
21146 }
21147 else
21148 {
21149 boff = font->baseline_offset;
21150 if (font->vertical_centering)
21151 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21152 }
21153
21154 if (it->char_to_display != '\n' && it->char_to_display != '\t')
21155 {
21156 int stretched_p;
21157
21158 it->nglyphs = 1;
21159
21160 if (it->override_ascent >= 0)
21161 {
21162 it->ascent = it->override_ascent;
21163 it->descent = it->override_descent;
21164 boff = it->override_boff;
21165 }
21166 else
21167 {
21168 it->ascent = FONT_BASE (font) + boff;
21169 it->descent = FONT_DESCENT (font) - boff;
21170 }
21171
21172 if (! font_not_found_p
21173 && get_char_glyph_code (it->char_to_display, font, &char2b))
21174 {
21175 pcm = get_per_char_metric (it->f, font, &char2b);
21176 if (pcm->width == 0
21177 && pcm->rbearing == 0 && pcm->lbearing == 0)
21178 pcm = NULL;
21179 }
21180
21181 if (pcm)
21182 {
21183 it->phys_ascent = pcm->ascent + boff;
21184 it->phys_descent = pcm->descent - boff;
21185 it->pixel_width = pcm->width;
21186 }
21187 else
21188 {
21189 it->glyph_not_available_p = 1;
21190 it->phys_ascent = it->ascent;
21191 it->phys_descent = it->descent;
21192 it->pixel_width = font->space_width;
21193 }
21194
21195 if (it->constrain_row_ascent_descent_p)
21196 {
21197 if (it->descent > it->max_descent)
21198 {
21199 it->ascent += it->descent - it->max_descent;
21200 it->descent = it->max_descent;
21201 }
21202 if (it->ascent > it->max_ascent)
21203 {
21204 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21205 it->ascent = it->max_ascent;
21206 }
21207 it->phys_ascent = min (it->phys_ascent, it->ascent);
21208 it->phys_descent = min (it->phys_descent, it->descent);
21209 extra_line_spacing = 0;
21210 }
21211
21212 /* If this is a space inside a region of text with
21213 `space-width' property, change its width. */
21214 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21215 if (stretched_p)
21216 it->pixel_width *= XFLOATINT (it->space_width);
21217
21218 /* If face has a box, add the box thickness to the character
21219 height. If character has a box line to the left and/or
21220 right, add the box line width to the character's width. */
21221 if (face->box != FACE_NO_BOX)
21222 {
21223 int thick = face->box_line_width;
21224
21225 if (thick > 0)
21226 {
21227 it->ascent += thick;
21228 it->descent += thick;
21229 }
21230 else
21231 thick = -thick;
21232
21233 if (it->start_of_box_run_p)
21234 it->pixel_width += thick;
21235 if (it->end_of_box_run_p)
21236 it->pixel_width += thick;
21237 }
21238
21239 /* If face has an overline, add the height of the overline
21240 (1 pixel) and a 1 pixel margin to the character height. */
21241 if (face->overline_p)
21242 it->ascent += overline_margin;
21243
21244 if (it->constrain_row_ascent_descent_p)
21245 {
21246 if (it->ascent > it->max_ascent)
21247 it->ascent = it->max_ascent;
21248 if (it->descent > it->max_descent)
21249 it->descent = it->max_descent;
21250 }
21251
21252 take_vertical_position_into_account (it);
21253
21254 /* If we have to actually produce glyphs, do it. */
21255 if (it->glyph_row)
21256 {
21257 if (stretched_p)
21258 {
21259 /* Translate a space with a `space-width' property
21260 into a stretch glyph. */
21261 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21262 / FONT_HEIGHT (font));
21263 append_stretch_glyph (it, it->object, it->pixel_width,
21264 it->ascent + it->descent, ascent);
21265 }
21266 else
21267 append_glyph (it);
21268
21269 /* If characters with lbearing or rbearing are displayed
21270 in this line, record that fact in a flag of the
21271 glyph row. This is used to optimize X output code. */
21272 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21273 it->glyph_row->contains_overlapping_glyphs_p = 1;
21274 }
21275 if (! stretched_p && it->pixel_width == 0)
21276 /* We assure that all visible glyphs have at least 1-pixel
21277 width. */
21278 it->pixel_width = 1;
21279 }
21280 else if (it->char_to_display == '\n')
21281 {
21282 /* A newline has no width, but we need the height of the
21283 line. But if previous part of the line sets a height,
21284 don't increase that height */
21285
21286 Lisp_Object height;
21287 Lisp_Object total_height = Qnil;
21288
21289 it->override_ascent = -1;
21290 it->pixel_width = 0;
21291 it->nglyphs = 0;
21292
21293 height = get_it_property(it, Qline_height);
21294 /* Split (line-height total-height) list */
21295 if (CONSP (height)
21296 && CONSP (XCDR (height))
21297 && NILP (XCDR (XCDR (height))))
21298 {
21299 total_height = XCAR (XCDR (height));
21300 height = XCAR (height);
21301 }
21302 height = calc_line_height_property(it, height, font, boff, 1);
21303
21304 if (it->override_ascent >= 0)
21305 {
21306 it->ascent = it->override_ascent;
21307 it->descent = it->override_descent;
21308 boff = it->override_boff;
21309 }
21310 else
21311 {
21312 it->ascent = FONT_BASE (font) + boff;
21313 it->descent = FONT_DESCENT (font) - boff;
21314 }
21315
21316 if (EQ (height, Qt))
21317 {
21318 if (it->descent > it->max_descent)
21319 {
21320 it->ascent += it->descent - it->max_descent;
21321 it->descent = it->max_descent;
21322 }
21323 if (it->ascent > it->max_ascent)
21324 {
21325 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21326 it->ascent = it->max_ascent;
21327 }
21328 it->phys_ascent = min (it->phys_ascent, it->ascent);
21329 it->phys_descent = min (it->phys_descent, it->descent);
21330 it->constrain_row_ascent_descent_p = 1;
21331 extra_line_spacing = 0;
21332 }
21333 else
21334 {
21335 Lisp_Object spacing;
21336
21337 it->phys_ascent = it->ascent;
21338 it->phys_descent = it->descent;
21339
21340 if ((it->max_ascent > 0 || it->max_descent > 0)
21341 && face->box != FACE_NO_BOX
21342 && face->box_line_width > 0)
21343 {
21344 it->ascent += face->box_line_width;
21345 it->descent += face->box_line_width;
21346 }
21347 if (!NILP (height)
21348 && XINT (height) > it->ascent + it->descent)
21349 it->ascent = XINT (height) - it->descent;
21350
21351 if (!NILP (total_height))
21352 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21353 else
21354 {
21355 spacing = get_it_property(it, Qline_spacing);
21356 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21357 }
21358 if (INTEGERP (spacing))
21359 {
21360 extra_line_spacing = XINT (spacing);
21361 if (!NILP (total_height))
21362 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21363 }
21364 }
21365 }
21366 else /* i.e. (it->char_to_display == '\t') */
21367 {
21368 if (font->space_width > 0)
21369 {
21370 int tab_width = it->tab_width * font->space_width;
21371 int x = it->current_x + it->continuation_lines_width;
21372 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21373
21374 /* If the distance from the current position to the next tab
21375 stop is less than a space character width, use the
21376 tab stop after that. */
21377 if (next_tab_x - x < font->space_width)
21378 next_tab_x += tab_width;
21379
21380 it->pixel_width = next_tab_x - x;
21381 it->nglyphs = 1;
21382 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21383 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21384
21385 if (it->glyph_row)
21386 {
21387 append_stretch_glyph (it, it->object, it->pixel_width,
21388 it->ascent + it->descent, it->ascent);
21389 }
21390 }
21391 else
21392 {
21393 it->pixel_width = 0;
21394 it->nglyphs = 1;
21395 }
21396 }
21397 }
21398 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21399 {
21400 /* A static composition.
21401
21402 Note: A composition is represented as one glyph in the
21403 glyph matrix. There are no padding glyphs.
21404
21405 Important note: pixel_width, ascent, and descent are the
21406 values of what is drawn by draw_glyphs (i.e. the values of
21407 the overall glyphs composed). */
21408 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21409 int boff; /* baseline offset */
21410 struct composition *cmp = composition_table[it->cmp_it.id];
21411 int glyph_len = cmp->glyph_len;
21412 struct font *font = face->font;
21413
21414 it->nglyphs = 1;
21415
21416 /* If we have not yet calculated pixel size data of glyphs of
21417 the composition for the current face font, calculate them
21418 now. Theoretically, we have to check all fonts for the
21419 glyphs, but that requires much time and memory space. So,
21420 here we check only the font of the first glyph. This may
21421 lead to incorrect display, but it's very rare, and C-l
21422 (recenter-top-bottom) can correct the display anyway. */
21423 if (! cmp->font || cmp->font != font)
21424 {
21425 /* Ascent and descent of the font of the first character
21426 of this composition (adjusted by baseline offset).
21427 Ascent and descent of overall glyphs should not be less
21428 than these, respectively. */
21429 int font_ascent, font_descent, font_height;
21430 /* Bounding box of the overall glyphs. */
21431 int leftmost, rightmost, lowest, highest;
21432 int lbearing, rbearing;
21433 int i, width, ascent, descent;
21434 int left_padded = 0, right_padded = 0;
21435 int c;
21436 XChar2b char2b;
21437 struct font_metrics *pcm;
21438 int font_not_found_p;
21439 int pos;
21440
21441 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21442 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21443 break;
21444 if (glyph_len < cmp->glyph_len)
21445 right_padded = 1;
21446 for (i = 0; i < glyph_len; i++)
21447 {
21448 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21449 break;
21450 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21451 }
21452 if (i > 0)
21453 left_padded = 1;
21454
21455 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21456 : IT_CHARPOS (*it));
21457 /* If no suitable font is found, use the default font. */
21458 font_not_found_p = font == NULL;
21459 if (font_not_found_p)
21460 {
21461 face = face->ascii_face;
21462 font = face->font;
21463 }
21464 boff = font->baseline_offset;
21465 if (font->vertical_centering)
21466 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21467 font_ascent = FONT_BASE (font) + boff;
21468 font_descent = FONT_DESCENT (font) - boff;
21469 font_height = FONT_HEIGHT (font);
21470
21471 cmp->font = (void *) font;
21472
21473 pcm = NULL;
21474 if (! font_not_found_p)
21475 {
21476 get_char_face_and_encoding (it->f, c, it->face_id,
21477 &char2b, it->multibyte_p, 0);
21478 pcm = get_per_char_metric (it->f, font, &char2b);
21479 }
21480
21481 /* Initialize the bounding box. */
21482 if (pcm)
21483 {
21484 width = pcm->width;
21485 ascent = pcm->ascent;
21486 descent = pcm->descent;
21487 lbearing = pcm->lbearing;
21488 rbearing = pcm->rbearing;
21489 }
21490 else
21491 {
21492 width = font->space_width;
21493 ascent = FONT_BASE (font);
21494 descent = FONT_DESCENT (font);
21495 lbearing = 0;
21496 rbearing = width;
21497 }
21498
21499 rightmost = width;
21500 leftmost = 0;
21501 lowest = - descent + boff;
21502 highest = ascent + boff;
21503
21504 if (! font_not_found_p
21505 && font->default_ascent
21506 && CHAR_TABLE_P (Vuse_default_ascent)
21507 && !NILP (Faref (Vuse_default_ascent,
21508 make_number (it->char_to_display))))
21509 highest = font->default_ascent + boff;
21510
21511 /* Draw the first glyph at the normal position. It may be
21512 shifted to right later if some other glyphs are drawn
21513 at the left. */
21514 cmp->offsets[i * 2] = 0;
21515 cmp->offsets[i * 2 + 1] = boff;
21516 cmp->lbearing = lbearing;
21517 cmp->rbearing = rbearing;
21518
21519 /* Set cmp->offsets for the remaining glyphs. */
21520 for (i++; i < glyph_len; i++)
21521 {
21522 int left, right, btm, top;
21523 int ch = COMPOSITION_GLYPH (cmp, i);
21524 int face_id;
21525 struct face *this_face;
21526 int this_boff;
21527
21528 if (ch == '\t')
21529 ch = ' ';
21530 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21531 this_face = FACE_FROM_ID (it->f, face_id);
21532 font = this_face->font;
21533
21534 if (font == NULL)
21535 pcm = NULL;
21536 else
21537 {
21538 this_boff = font->baseline_offset;
21539 if (font->vertical_centering)
21540 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21541 get_char_face_and_encoding (it->f, ch, face_id,
21542 &char2b, it->multibyte_p, 0);
21543 pcm = get_per_char_metric (it->f, font, &char2b);
21544 }
21545 if (! pcm)
21546 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21547 else
21548 {
21549 width = pcm->width;
21550 ascent = pcm->ascent;
21551 descent = pcm->descent;
21552 lbearing = pcm->lbearing;
21553 rbearing = pcm->rbearing;
21554 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21555 {
21556 /* Relative composition with or without
21557 alternate chars. */
21558 left = (leftmost + rightmost - width) / 2;
21559 btm = - descent + boff;
21560 if (font->relative_compose
21561 && (! CHAR_TABLE_P (Vignore_relative_composition)
21562 || NILP (Faref (Vignore_relative_composition,
21563 make_number (ch)))))
21564 {
21565
21566 if (- descent >= font->relative_compose)
21567 /* One extra pixel between two glyphs. */
21568 btm = highest + 1;
21569 else if (ascent <= 0)
21570 /* One extra pixel between two glyphs. */
21571 btm = lowest - 1 - ascent - descent;
21572 }
21573 }
21574 else
21575 {
21576 /* A composition rule is specified by an integer
21577 value that encodes global and new reference
21578 points (GREF and NREF). GREF and NREF are
21579 specified by numbers as below:
21580
21581 0---1---2 -- ascent
21582 | |
21583 | |
21584 | |
21585 9--10--11 -- center
21586 | |
21587 ---3---4---5--- baseline
21588 | |
21589 6---7---8 -- descent
21590 */
21591 int rule = COMPOSITION_RULE (cmp, i);
21592 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21593
21594 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21595 grefx = gref % 3, nrefx = nref % 3;
21596 grefy = gref / 3, nrefy = nref / 3;
21597 if (xoff)
21598 xoff = font_height * (xoff - 128) / 256;
21599 if (yoff)
21600 yoff = font_height * (yoff - 128) / 256;
21601
21602 left = (leftmost
21603 + grefx * (rightmost - leftmost) / 2
21604 - nrefx * width / 2
21605 + xoff);
21606
21607 btm = ((grefy == 0 ? highest
21608 : grefy == 1 ? 0
21609 : grefy == 2 ? lowest
21610 : (highest + lowest) / 2)
21611 - (nrefy == 0 ? ascent + descent
21612 : nrefy == 1 ? descent - boff
21613 : nrefy == 2 ? 0
21614 : (ascent + descent) / 2)
21615 + yoff);
21616 }
21617
21618 cmp->offsets[i * 2] = left;
21619 cmp->offsets[i * 2 + 1] = btm + descent;
21620
21621 /* Update the bounding box of the overall glyphs. */
21622 if (width > 0)
21623 {
21624 right = left + width;
21625 if (left < leftmost)
21626 leftmost = left;
21627 if (right > rightmost)
21628 rightmost = right;
21629 }
21630 top = btm + descent + ascent;
21631 if (top > highest)
21632 highest = top;
21633 if (btm < lowest)
21634 lowest = btm;
21635
21636 if (cmp->lbearing > left + lbearing)
21637 cmp->lbearing = left + lbearing;
21638 if (cmp->rbearing < left + rbearing)
21639 cmp->rbearing = left + rbearing;
21640 }
21641 }
21642
21643 /* If there are glyphs whose x-offsets are negative,
21644 shift all glyphs to the right and make all x-offsets
21645 non-negative. */
21646 if (leftmost < 0)
21647 {
21648 for (i = 0; i < cmp->glyph_len; i++)
21649 cmp->offsets[i * 2] -= leftmost;
21650 rightmost -= leftmost;
21651 cmp->lbearing -= leftmost;
21652 cmp->rbearing -= leftmost;
21653 }
21654
21655 if (left_padded && cmp->lbearing < 0)
21656 {
21657 for (i = 0; i < cmp->glyph_len; i++)
21658 cmp->offsets[i * 2] -= cmp->lbearing;
21659 rightmost -= cmp->lbearing;
21660 cmp->rbearing -= cmp->lbearing;
21661 cmp->lbearing = 0;
21662 }
21663 if (right_padded && rightmost < cmp->rbearing)
21664 {
21665 rightmost = cmp->rbearing;
21666 }
21667
21668 cmp->pixel_width = rightmost;
21669 cmp->ascent = highest;
21670 cmp->descent = - lowest;
21671 if (cmp->ascent < font_ascent)
21672 cmp->ascent = font_ascent;
21673 if (cmp->descent < font_descent)
21674 cmp->descent = font_descent;
21675 }
21676
21677 if (it->glyph_row
21678 && (cmp->lbearing < 0
21679 || cmp->rbearing > cmp->pixel_width))
21680 it->glyph_row->contains_overlapping_glyphs_p = 1;
21681
21682 it->pixel_width = cmp->pixel_width;
21683 it->ascent = it->phys_ascent = cmp->ascent;
21684 it->descent = it->phys_descent = cmp->descent;
21685 if (face->box != FACE_NO_BOX)
21686 {
21687 int thick = face->box_line_width;
21688
21689 if (thick > 0)
21690 {
21691 it->ascent += thick;
21692 it->descent += thick;
21693 }
21694 else
21695 thick = - thick;
21696
21697 if (it->start_of_box_run_p)
21698 it->pixel_width += thick;
21699 if (it->end_of_box_run_p)
21700 it->pixel_width += thick;
21701 }
21702
21703 /* If face has an overline, add the height of the overline
21704 (1 pixel) and a 1 pixel margin to the character height. */
21705 if (face->overline_p)
21706 it->ascent += overline_margin;
21707
21708 take_vertical_position_into_account (it);
21709 if (it->ascent < 0)
21710 it->ascent = 0;
21711 if (it->descent < 0)
21712 it->descent = 0;
21713
21714 if (it->glyph_row)
21715 append_composite_glyph (it);
21716 }
21717 else if (it->what == IT_COMPOSITION)
21718 {
21719 /* A dynamic (automatic) composition. */
21720 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21721 Lisp_Object gstring;
21722 struct font_metrics metrics;
21723
21724 gstring = composition_gstring_from_id (it->cmp_it.id);
21725 it->pixel_width
21726 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21727 &metrics);
21728 if (it->glyph_row
21729 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21730 it->glyph_row->contains_overlapping_glyphs_p = 1;
21731 it->ascent = it->phys_ascent = metrics.ascent;
21732 it->descent = it->phys_descent = metrics.descent;
21733 if (face->box != FACE_NO_BOX)
21734 {
21735 int thick = face->box_line_width;
21736
21737 if (thick > 0)
21738 {
21739 it->ascent += thick;
21740 it->descent += thick;
21741 }
21742 else
21743 thick = - thick;
21744
21745 if (it->start_of_box_run_p)
21746 it->pixel_width += thick;
21747 if (it->end_of_box_run_p)
21748 it->pixel_width += thick;
21749 }
21750 /* If face has an overline, add the height of the overline
21751 (1 pixel) and a 1 pixel margin to the character height. */
21752 if (face->overline_p)
21753 it->ascent += overline_margin;
21754 take_vertical_position_into_account (it);
21755 if (it->ascent < 0)
21756 it->ascent = 0;
21757 if (it->descent < 0)
21758 it->descent = 0;
21759
21760 if (it->glyph_row)
21761 append_composite_glyph (it);
21762 }
21763 else if (it->what == IT_IMAGE)
21764 produce_image_glyph (it);
21765 else if (it->what == IT_STRETCH)
21766 produce_stretch_glyph (it);
21767
21768 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21769 because this isn't true for images with `:ascent 100'. */
21770 xassert (it->ascent >= 0 && it->descent >= 0);
21771 if (it->area == TEXT_AREA)
21772 it->current_x += it->pixel_width;
21773
21774 if (extra_line_spacing > 0)
21775 {
21776 it->descent += extra_line_spacing;
21777 if (extra_line_spacing > it->max_extra_line_spacing)
21778 it->max_extra_line_spacing = extra_line_spacing;
21779 }
21780
21781 it->max_ascent = max (it->max_ascent, it->ascent);
21782 it->max_descent = max (it->max_descent, it->descent);
21783 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21784 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21785 }
21786
21787 /* EXPORT for RIF:
21788 Output LEN glyphs starting at START at the nominal cursor position.
21789 Advance the nominal cursor over the text. The global variable
21790 updated_window contains the window being updated, updated_row is
21791 the glyph row being updated, and updated_area is the area of that
21792 row being updated. */
21793
21794 void
21795 x_write_glyphs (start, len)
21796 struct glyph *start;
21797 int len;
21798 {
21799 int x, hpos;
21800
21801 xassert (updated_window && updated_row);
21802 BLOCK_INPUT;
21803
21804 /* Write glyphs. */
21805
21806 hpos = start - updated_row->glyphs[updated_area];
21807 x = draw_glyphs (updated_window, output_cursor.x,
21808 updated_row, updated_area,
21809 hpos, hpos + len,
21810 DRAW_NORMAL_TEXT, 0);
21811
21812 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21813 if (updated_area == TEXT_AREA
21814 && updated_window->phys_cursor_on_p
21815 && updated_window->phys_cursor.vpos == output_cursor.vpos
21816 && updated_window->phys_cursor.hpos >= hpos
21817 && updated_window->phys_cursor.hpos < hpos + len)
21818 updated_window->phys_cursor_on_p = 0;
21819
21820 UNBLOCK_INPUT;
21821
21822 /* Advance the output cursor. */
21823 output_cursor.hpos += len;
21824 output_cursor.x = x;
21825 }
21826
21827
21828 /* EXPORT for RIF:
21829 Insert LEN glyphs from START at the nominal cursor position. */
21830
21831 void
21832 x_insert_glyphs (start, len)
21833 struct glyph *start;
21834 int len;
21835 {
21836 struct frame *f;
21837 struct window *w;
21838 int line_height, shift_by_width, shifted_region_width;
21839 struct glyph_row *row;
21840 struct glyph *glyph;
21841 int frame_x, frame_y;
21842 EMACS_INT hpos;
21843
21844 xassert (updated_window && updated_row);
21845 BLOCK_INPUT;
21846 w = updated_window;
21847 f = XFRAME (WINDOW_FRAME (w));
21848
21849 /* Get the height of the line we are in. */
21850 row = updated_row;
21851 line_height = row->height;
21852
21853 /* Get the width of the glyphs to insert. */
21854 shift_by_width = 0;
21855 for (glyph = start; glyph < start + len; ++glyph)
21856 shift_by_width += glyph->pixel_width;
21857
21858 /* Get the width of the region to shift right. */
21859 shifted_region_width = (window_box_width (w, updated_area)
21860 - output_cursor.x
21861 - shift_by_width);
21862
21863 /* Shift right. */
21864 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21865 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21866
21867 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21868 line_height, shift_by_width);
21869
21870 /* Write the glyphs. */
21871 hpos = start - row->glyphs[updated_area];
21872 draw_glyphs (w, output_cursor.x, row, updated_area,
21873 hpos, hpos + len,
21874 DRAW_NORMAL_TEXT, 0);
21875
21876 /* Advance the output cursor. */
21877 output_cursor.hpos += len;
21878 output_cursor.x += shift_by_width;
21879 UNBLOCK_INPUT;
21880 }
21881
21882
21883 /* EXPORT for RIF:
21884 Erase the current text line from the nominal cursor position
21885 (inclusive) to pixel column TO_X (exclusive). The idea is that
21886 everything from TO_X onward is already erased.
21887
21888 TO_X is a pixel position relative to updated_area of
21889 updated_window. TO_X == -1 means clear to the end of this area. */
21890
21891 void
21892 x_clear_end_of_line (to_x)
21893 int to_x;
21894 {
21895 struct frame *f;
21896 struct window *w = updated_window;
21897 int max_x, min_y, max_y;
21898 int from_x, from_y, to_y;
21899
21900 xassert (updated_window && updated_row);
21901 f = XFRAME (w->frame);
21902
21903 if (updated_row->full_width_p)
21904 max_x = WINDOW_TOTAL_WIDTH (w);
21905 else
21906 max_x = window_box_width (w, updated_area);
21907 max_y = window_text_bottom_y (w);
21908
21909 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21910 of window. For TO_X > 0, truncate to end of drawing area. */
21911 if (to_x == 0)
21912 return;
21913 else if (to_x < 0)
21914 to_x = max_x;
21915 else
21916 to_x = min (to_x, max_x);
21917
21918 to_y = min (max_y, output_cursor.y + updated_row->height);
21919
21920 /* Notice if the cursor will be cleared by this operation. */
21921 if (!updated_row->full_width_p)
21922 notice_overwritten_cursor (w, updated_area,
21923 output_cursor.x, -1,
21924 updated_row->y,
21925 MATRIX_ROW_BOTTOM_Y (updated_row));
21926
21927 from_x = output_cursor.x;
21928
21929 /* Translate to frame coordinates. */
21930 if (updated_row->full_width_p)
21931 {
21932 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21933 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21934 }
21935 else
21936 {
21937 int area_left = window_box_left (w, updated_area);
21938 from_x += area_left;
21939 to_x += area_left;
21940 }
21941
21942 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21943 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21944 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21945
21946 /* Prevent inadvertently clearing to end of the X window. */
21947 if (to_x > from_x && to_y > from_y)
21948 {
21949 BLOCK_INPUT;
21950 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21951 to_x - from_x, to_y - from_y);
21952 UNBLOCK_INPUT;
21953 }
21954 }
21955
21956 #endif /* HAVE_WINDOW_SYSTEM */
21957
21958
21959 \f
21960 /***********************************************************************
21961 Cursor types
21962 ***********************************************************************/
21963
21964 /* Value is the internal representation of the specified cursor type
21965 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21966 of the bar cursor. */
21967
21968 static enum text_cursor_kinds
21969 get_specified_cursor_type (arg, width)
21970 Lisp_Object arg;
21971 int *width;
21972 {
21973 enum text_cursor_kinds type;
21974
21975 if (NILP (arg))
21976 return NO_CURSOR;
21977
21978 if (EQ (arg, Qbox))
21979 return FILLED_BOX_CURSOR;
21980
21981 if (EQ (arg, Qhollow))
21982 return HOLLOW_BOX_CURSOR;
21983
21984 if (EQ (arg, Qbar))
21985 {
21986 *width = 2;
21987 return BAR_CURSOR;
21988 }
21989
21990 if (CONSP (arg)
21991 && EQ (XCAR (arg), Qbar)
21992 && INTEGERP (XCDR (arg))
21993 && XINT (XCDR (arg)) >= 0)
21994 {
21995 *width = XINT (XCDR (arg));
21996 return BAR_CURSOR;
21997 }
21998
21999 if (EQ (arg, Qhbar))
22000 {
22001 *width = 2;
22002 return HBAR_CURSOR;
22003 }
22004
22005 if (CONSP (arg)
22006 && EQ (XCAR (arg), Qhbar)
22007 && INTEGERP (XCDR (arg))
22008 && XINT (XCDR (arg)) >= 0)
22009 {
22010 *width = XINT (XCDR (arg));
22011 return HBAR_CURSOR;
22012 }
22013
22014 /* Treat anything unknown as "hollow box cursor".
22015 It was bad to signal an error; people have trouble fixing
22016 .Xdefaults with Emacs, when it has something bad in it. */
22017 type = HOLLOW_BOX_CURSOR;
22018
22019 return type;
22020 }
22021
22022 /* Set the default cursor types for specified frame. */
22023 void
22024 set_frame_cursor_types (f, arg)
22025 struct frame *f;
22026 Lisp_Object arg;
22027 {
22028 int width;
22029 Lisp_Object tem;
22030
22031 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
22032 FRAME_CURSOR_WIDTH (f) = width;
22033
22034 /* By default, set up the blink-off state depending on the on-state. */
22035
22036 tem = Fassoc (arg, Vblink_cursor_alist);
22037 if (!NILP (tem))
22038 {
22039 FRAME_BLINK_OFF_CURSOR (f)
22040 = get_specified_cursor_type (XCDR (tem), &width);
22041 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22042 }
22043 else
22044 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22045 }
22046
22047
22048 /* Return the cursor we want to be displayed in window W. Return
22049 width of bar/hbar cursor through WIDTH arg. Return with
22050 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22051 (i.e. if the `system caret' should track this cursor).
22052
22053 In a mini-buffer window, we want the cursor only to appear if we
22054 are reading input from this window. For the selected window, we
22055 want the cursor type given by the frame parameter or buffer local
22056 setting of cursor-type. If explicitly marked off, draw no cursor.
22057 In all other cases, we want a hollow box cursor. */
22058
22059 static enum text_cursor_kinds
22060 get_window_cursor_type (w, glyph, width, active_cursor)
22061 struct window *w;
22062 struct glyph *glyph;
22063 int *width;
22064 int *active_cursor;
22065 {
22066 struct frame *f = XFRAME (w->frame);
22067 struct buffer *b = XBUFFER (w->buffer);
22068 int cursor_type = DEFAULT_CURSOR;
22069 Lisp_Object alt_cursor;
22070 int non_selected = 0;
22071
22072 *active_cursor = 1;
22073
22074 /* Echo area */
22075 if (cursor_in_echo_area
22076 && FRAME_HAS_MINIBUF_P (f)
22077 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22078 {
22079 if (w == XWINDOW (echo_area_window))
22080 {
22081 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22082 {
22083 *width = FRAME_CURSOR_WIDTH (f);
22084 return FRAME_DESIRED_CURSOR (f);
22085 }
22086 else
22087 return get_specified_cursor_type (b->cursor_type, width);
22088 }
22089
22090 *active_cursor = 0;
22091 non_selected = 1;
22092 }
22093
22094 /* Detect a nonselected window or nonselected frame. */
22095 else if (w != XWINDOW (f->selected_window)
22096 #ifdef HAVE_WINDOW_SYSTEM
22097 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22098 #endif
22099 )
22100 {
22101 *active_cursor = 0;
22102
22103 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22104 return NO_CURSOR;
22105
22106 non_selected = 1;
22107 }
22108
22109 /* Never display a cursor in a window in which cursor-type is nil. */
22110 if (NILP (b->cursor_type))
22111 return NO_CURSOR;
22112
22113 /* Get the normal cursor type for this window. */
22114 if (EQ (b->cursor_type, Qt))
22115 {
22116 cursor_type = FRAME_DESIRED_CURSOR (f);
22117 *width = FRAME_CURSOR_WIDTH (f);
22118 }
22119 else
22120 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22121
22122 /* Use cursor-in-non-selected-windows instead
22123 for non-selected window or frame. */
22124 if (non_selected)
22125 {
22126 alt_cursor = b->cursor_in_non_selected_windows;
22127 if (!EQ (Qt, alt_cursor))
22128 return get_specified_cursor_type (alt_cursor, width);
22129 /* t means modify the normal cursor type. */
22130 if (cursor_type == FILLED_BOX_CURSOR)
22131 cursor_type = HOLLOW_BOX_CURSOR;
22132 else if (cursor_type == BAR_CURSOR && *width > 1)
22133 --*width;
22134 return cursor_type;
22135 }
22136
22137 /* Use normal cursor if not blinked off. */
22138 if (!w->cursor_off_p)
22139 {
22140 #ifdef HAVE_WINDOW_SYSTEM
22141 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22142 {
22143 if (cursor_type == FILLED_BOX_CURSOR)
22144 {
22145 /* Using a block cursor on large images can be very annoying.
22146 So use a hollow cursor for "large" images.
22147 If image is not transparent (no mask), also use hollow cursor. */
22148 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22149 if (img != NULL && IMAGEP (img->spec))
22150 {
22151 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22152 where N = size of default frame font size.
22153 This should cover most of the "tiny" icons people may use. */
22154 if (!img->mask
22155 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22156 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22157 cursor_type = HOLLOW_BOX_CURSOR;
22158 }
22159 }
22160 else if (cursor_type != NO_CURSOR)
22161 {
22162 /* Display current only supports BOX and HOLLOW cursors for images.
22163 So for now, unconditionally use a HOLLOW cursor when cursor is
22164 not a solid box cursor. */
22165 cursor_type = HOLLOW_BOX_CURSOR;
22166 }
22167 }
22168 #endif
22169 return cursor_type;
22170 }
22171
22172 /* Cursor is blinked off, so determine how to "toggle" it. */
22173
22174 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22175 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22176 return get_specified_cursor_type (XCDR (alt_cursor), width);
22177
22178 /* Then see if frame has specified a specific blink off cursor type. */
22179 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22180 {
22181 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22182 return FRAME_BLINK_OFF_CURSOR (f);
22183 }
22184
22185 #if 0
22186 /* Some people liked having a permanently visible blinking cursor,
22187 while others had very strong opinions against it. So it was
22188 decided to remove it. KFS 2003-09-03 */
22189
22190 /* Finally perform built-in cursor blinking:
22191 filled box <-> hollow box
22192 wide [h]bar <-> narrow [h]bar
22193 narrow [h]bar <-> no cursor
22194 other type <-> no cursor */
22195
22196 if (cursor_type == FILLED_BOX_CURSOR)
22197 return HOLLOW_BOX_CURSOR;
22198
22199 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22200 {
22201 *width = 1;
22202 return cursor_type;
22203 }
22204 #endif
22205
22206 return NO_CURSOR;
22207 }
22208
22209
22210 #ifdef HAVE_WINDOW_SYSTEM
22211
22212 /* Notice when the text cursor of window W has been completely
22213 overwritten by a drawing operation that outputs glyphs in AREA
22214 starting at X0 and ending at X1 in the line starting at Y0 and
22215 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22216 the rest of the line after X0 has been written. Y coordinates
22217 are window-relative. */
22218
22219 static void
22220 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22221 struct window *w;
22222 enum glyph_row_area area;
22223 int x0, y0, x1, y1;
22224 {
22225 int cx0, cx1, cy0, cy1;
22226 struct glyph_row *row;
22227
22228 if (!w->phys_cursor_on_p)
22229 return;
22230 if (area != TEXT_AREA)
22231 return;
22232
22233 if (w->phys_cursor.vpos < 0
22234 || w->phys_cursor.vpos >= w->current_matrix->nrows
22235 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22236 !(row->enabled_p && row->displays_text_p)))
22237 return;
22238
22239 if (row->cursor_in_fringe_p)
22240 {
22241 row->cursor_in_fringe_p = 0;
22242 draw_fringe_bitmap (w, row, 0);
22243 w->phys_cursor_on_p = 0;
22244 return;
22245 }
22246
22247 cx0 = w->phys_cursor.x;
22248 cx1 = cx0 + w->phys_cursor_width;
22249 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22250 return;
22251
22252 /* The cursor image will be completely removed from the
22253 screen if the output area intersects the cursor area in
22254 y-direction. When we draw in [y0 y1[, and some part of
22255 the cursor is at y < y0, that part must have been drawn
22256 before. When scrolling, the cursor is erased before
22257 actually scrolling, so we don't come here. When not
22258 scrolling, the rows above the old cursor row must have
22259 changed, and in this case these rows must have written
22260 over the cursor image.
22261
22262 Likewise if part of the cursor is below y1, with the
22263 exception of the cursor being in the first blank row at
22264 the buffer and window end because update_text_area
22265 doesn't draw that row. (Except when it does, but
22266 that's handled in update_text_area.) */
22267
22268 cy0 = w->phys_cursor.y;
22269 cy1 = cy0 + w->phys_cursor_height;
22270 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22271 return;
22272
22273 w->phys_cursor_on_p = 0;
22274 }
22275
22276 #endif /* HAVE_WINDOW_SYSTEM */
22277
22278 \f
22279 /************************************************************************
22280 Mouse Face
22281 ************************************************************************/
22282
22283 #ifdef HAVE_WINDOW_SYSTEM
22284
22285 /* EXPORT for RIF:
22286 Fix the display of area AREA of overlapping row ROW in window W
22287 with respect to the overlapping part OVERLAPS. */
22288
22289 void
22290 x_fix_overlapping_area (w, row, area, overlaps)
22291 struct window *w;
22292 struct glyph_row *row;
22293 enum glyph_row_area area;
22294 int overlaps;
22295 {
22296 int i, x;
22297
22298 BLOCK_INPUT;
22299
22300 x = 0;
22301 for (i = 0; i < row->used[area];)
22302 {
22303 if (row->glyphs[area][i].overlaps_vertically_p)
22304 {
22305 int start = i, start_x = x;
22306
22307 do
22308 {
22309 x += row->glyphs[area][i].pixel_width;
22310 ++i;
22311 }
22312 while (i < row->used[area]
22313 && row->glyphs[area][i].overlaps_vertically_p);
22314
22315 draw_glyphs (w, start_x, row, area,
22316 start, i,
22317 DRAW_NORMAL_TEXT, overlaps);
22318 }
22319 else
22320 {
22321 x += row->glyphs[area][i].pixel_width;
22322 ++i;
22323 }
22324 }
22325
22326 UNBLOCK_INPUT;
22327 }
22328
22329
22330 /* EXPORT:
22331 Draw the cursor glyph of window W in glyph row ROW. See the
22332 comment of draw_glyphs for the meaning of HL. */
22333
22334 void
22335 draw_phys_cursor_glyph (w, row, hl)
22336 struct window *w;
22337 struct glyph_row *row;
22338 enum draw_glyphs_face hl;
22339 {
22340 /* If cursor hpos is out of bounds, don't draw garbage. This can
22341 happen in mini-buffer windows when switching between echo area
22342 glyphs and mini-buffer. */
22343 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22344 {
22345 int on_p = w->phys_cursor_on_p;
22346 int x1;
22347 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22348 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22349 hl, 0);
22350 w->phys_cursor_on_p = on_p;
22351
22352 if (hl == DRAW_CURSOR)
22353 w->phys_cursor_width = x1 - w->phys_cursor.x;
22354 /* When we erase the cursor, and ROW is overlapped by other
22355 rows, make sure that these overlapping parts of other rows
22356 are redrawn. */
22357 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22358 {
22359 w->phys_cursor_width = x1 - w->phys_cursor.x;
22360
22361 if (row > w->current_matrix->rows
22362 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22363 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22364 OVERLAPS_ERASED_CURSOR);
22365
22366 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22367 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22368 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22369 OVERLAPS_ERASED_CURSOR);
22370 }
22371 }
22372 }
22373
22374
22375 /* EXPORT:
22376 Erase the image of a cursor of window W from the screen. */
22377
22378 void
22379 erase_phys_cursor (w)
22380 struct window *w;
22381 {
22382 struct frame *f = XFRAME (w->frame);
22383 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22384 int hpos = w->phys_cursor.hpos;
22385 int vpos = w->phys_cursor.vpos;
22386 int mouse_face_here_p = 0;
22387 struct glyph_matrix *active_glyphs = w->current_matrix;
22388 struct glyph_row *cursor_row;
22389 struct glyph *cursor_glyph;
22390 enum draw_glyphs_face hl;
22391
22392 /* No cursor displayed or row invalidated => nothing to do on the
22393 screen. */
22394 if (w->phys_cursor_type == NO_CURSOR)
22395 goto mark_cursor_off;
22396
22397 /* VPOS >= active_glyphs->nrows means that window has been resized.
22398 Don't bother to erase the cursor. */
22399 if (vpos >= active_glyphs->nrows)
22400 goto mark_cursor_off;
22401
22402 /* If row containing cursor is marked invalid, there is nothing we
22403 can do. */
22404 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22405 if (!cursor_row->enabled_p)
22406 goto mark_cursor_off;
22407
22408 /* If line spacing is > 0, old cursor may only be partially visible in
22409 window after split-window. So adjust visible height. */
22410 cursor_row->visible_height = min (cursor_row->visible_height,
22411 window_text_bottom_y (w) - cursor_row->y);
22412
22413 /* If row is completely invisible, don't attempt to delete a cursor which
22414 isn't there. This can happen if cursor is at top of a window, and
22415 we switch to a buffer with a header line in that window. */
22416 if (cursor_row->visible_height <= 0)
22417 goto mark_cursor_off;
22418
22419 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22420 if (cursor_row->cursor_in_fringe_p)
22421 {
22422 cursor_row->cursor_in_fringe_p = 0;
22423 draw_fringe_bitmap (w, cursor_row, 0);
22424 goto mark_cursor_off;
22425 }
22426
22427 /* This can happen when the new row is shorter than the old one.
22428 In this case, either draw_glyphs or clear_end_of_line
22429 should have cleared the cursor. Note that we wouldn't be
22430 able to erase the cursor in this case because we don't have a
22431 cursor glyph at hand. */
22432 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22433 goto mark_cursor_off;
22434
22435 /* If the cursor is in the mouse face area, redisplay that when
22436 we clear the cursor. */
22437 if (! NILP (dpyinfo->mouse_face_window)
22438 && w == XWINDOW (dpyinfo->mouse_face_window)
22439 && (vpos > dpyinfo->mouse_face_beg_row
22440 || (vpos == dpyinfo->mouse_face_beg_row
22441 && hpos >= dpyinfo->mouse_face_beg_col))
22442 && (vpos < dpyinfo->mouse_face_end_row
22443 || (vpos == dpyinfo->mouse_face_end_row
22444 && hpos < dpyinfo->mouse_face_end_col))
22445 /* Don't redraw the cursor's spot in mouse face if it is at the
22446 end of a line (on a newline). The cursor appears there, but
22447 mouse highlighting does not. */
22448 && cursor_row->used[TEXT_AREA] > hpos)
22449 mouse_face_here_p = 1;
22450
22451 /* Maybe clear the display under the cursor. */
22452 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22453 {
22454 int x, y, left_x;
22455 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22456 int width;
22457
22458 cursor_glyph = get_phys_cursor_glyph (w);
22459 if (cursor_glyph == NULL)
22460 goto mark_cursor_off;
22461
22462 width = cursor_glyph->pixel_width;
22463 left_x = window_box_left_offset (w, TEXT_AREA);
22464 x = w->phys_cursor.x;
22465 if (x < left_x)
22466 width -= left_x - x;
22467 width = min (width, window_box_width (w, TEXT_AREA) - x);
22468 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22469 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22470
22471 if (width > 0)
22472 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22473 }
22474
22475 /* Erase the cursor by redrawing the character underneath it. */
22476 if (mouse_face_here_p)
22477 hl = DRAW_MOUSE_FACE;
22478 else
22479 hl = DRAW_NORMAL_TEXT;
22480 draw_phys_cursor_glyph (w, cursor_row, hl);
22481
22482 mark_cursor_off:
22483 w->phys_cursor_on_p = 0;
22484 w->phys_cursor_type = NO_CURSOR;
22485 }
22486
22487
22488 /* EXPORT:
22489 Display or clear cursor of window W. If ON is zero, clear the
22490 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22491 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22492
22493 void
22494 display_and_set_cursor (w, on, hpos, vpos, x, y)
22495 struct window *w;
22496 int on, hpos, vpos, x, y;
22497 {
22498 struct frame *f = XFRAME (w->frame);
22499 int new_cursor_type;
22500 int new_cursor_width;
22501 int active_cursor;
22502 struct glyph_row *glyph_row;
22503 struct glyph *glyph;
22504
22505 /* This is pointless on invisible frames, and dangerous on garbaged
22506 windows and frames; in the latter case, the frame or window may
22507 be in the midst of changing its size, and x and y may be off the
22508 window. */
22509 if (! FRAME_VISIBLE_P (f)
22510 || FRAME_GARBAGED_P (f)
22511 || vpos >= w->current_matrix->nrows
22512 || hpos >= w->current_matrix->matrix_w)
22513 return;
22514
22515 /* If cursor is off and we want it off, return quickly. */
22516 if (!on && !w->phys_cursor_on_p)
22517 return;
22518
22519 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22520 /* If cursor row is not enabled, we don't really know where to
22521 display the cursor. */
22522 if (!glyph_row->enabled_p)
22523 {
22524 w->phys_cursor_on_p = 0;
22525 return;
22526 }
22527
22528 glyph = NULL;
22529 if (!glyph_row->exact_window_width_line_p
22530 || hpos < glyph_row->used[TEXT_AREA])
22531 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22532
22533 xassert (interrupt_input_blocked);
22534
22535 /* Set new_cursor_type to the cursor we want to be displayed. */
22536 new_cursor_type = get_window_cursor_type (w, glyph,
22537 &new_cursor_width, &active_cursor);
22538
22539 /* If cursor is currently being shown and we don't want it to be or
22540 it is in the wrong place, or the cursor type is not what we want,
22541 erase it. */
22542 if (w->phys_cursor_on_p
22543 && (!on
22544 || w->phys_cursor.x != x
22545 || w->phys_cursor.y != y
22546 || new_cursor_type != w->phys_cursor_type
22547 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22548 && new_cursor_width != w->phys_cursor_width)))
22549 erase_phys_cursor (w);
22550
22551 /* Don't check phys_cursor_on_p here because that flag is only set
22552 to zero in some cases where we know that the cursor has been
22553 completely erased, to avoid the extra work of erasing the cursor
22554 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22555 still not be visible, or it has only been partly erased. */
22556 if (on)
22557 {
22558 w->phys_cursor_ascent = glyph_row->ascent;
22559 w->phys_cursor_height = glyph_row->height;
22560
22561 /* Set phys_cursor_.* before x_draw_.* is called because some
22562 of them may need the information. */
22563 w->phys_cursor.x = x;
22564 w->phys_cursor.y = glyph_row->y;
22565 w->phys_cursor.hpos = hpos;
22566 w->phys_cursor.vpos = vpos;
22567 }
22568
22569 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22570 new_cursor_type, new_cursor_width,
22571 on, active_cursor);
22572 }
22573
22574
22575 /* Switch the display of W's cursor on or off, according to the value
22576 of ON. */
22577
22578 void
22579 update_window_cursor (w, on)
22580 struct window *w;
22581 int on;
22582 {
22583 /* Don't update cursor in windows whose frame is in the process
22584 of being deleted. */
22585 if (w->current_matrix)
22586 {
22587 BLOCK_INPUT;
22588 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22589 w->phys_cursor.x, w->phys_cursor.y);
22590 UNBLOCK_INPUT;
22591 }
22592 }
22593
22594
22595 /* Call update_window_cursor with parameter ON_P on all leaf windows
22596 in the window tree rooted at W. */
22597
22598 static void
22599 update_cursor_in_window_tree (w, on_p)
22600 struct window *w;
22601 int on_p;
22602 {
22603 while (w)
22604 {
22605 if (!NILP (w->hchild))
22606 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22607 else if (!NILP (w->vchild))
22608 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22609 else
22610 update_window_cursor (w, on_p);
22611
22612 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22613 }
22614 }
22615
22616
22617 /* EXPORT:
22618 Display the cursor on window W, or clear it, according to ON_P.
22619 Don't change the cursor's position. */
22620
22621 void
22622 x_update_cursor (f, on_p)
22623 struct frame *f;
22624 int on_p;
22625 {
22626 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22627 }
22628
22629
22630 /* EXPORT:
22631 Clear the cursor of window W to background color, and mark the
22632 cursor as not shown. This is used when the text where the cursor
22633 is about to be rewritten. */
22634
22635 void
22636 x_clear_cursor (w)
22637 struct window *w;
22638 {
22639 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22640 update_window_cursor (w, 0);
22641 }
22642
22643
22644 /* EXPORT:
22645 Display the active region described by mouse_face_* according to DRAW. */
22646
22647 void
22648 show_mouse_face (dpyinfo, draw)
22649 Display_Info *dpyinfo;
22650 enum draw_glyphs_face draw;
22651 {
22652 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22653 struct frame *f = XFRAME (WINDOW_FRAME (w));
22654
22655 if (/* If window is in the process of being destroyed, don't bother
22656 to do anything. */
22657 w->current_matrix != NULL
22658 /* Don't update mouse highlight if hidden */
22659 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22660 /* Recognize when we are called to operate on rows that don't exist
22661 anymore. This can happen when a window is split. */
22662 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22663 {
22664 int phys_cursor_on_p = w->phys_cursor_on_p;
22665 struct glyph_row *row, *first, *last;
22666
22667 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22668 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22669
22670 for (row = first; row <= last && row->enabled_p; ++row)
22671 {
22672 int start_hpos, end_hpos, start_x;
22673
22674 /* For all but the first row, the highlight starts at column 0. */
22675 if (row == first)
22676 {
22677 start_hpos = dpyinfo->mouse_face_beg_col;
22678 start_x = dpyinfo->mouse_face_beg_x;
22679 }
22680 else
22681 {
22682 start_hpos = 0;
22683 start_x = 0;
22684 }
22685
22686 if (row == last)
22687 end_hpos = dpyinfo->mouse_face_end_col;
22688 else
22689 {
22690 end_hpos = row->used[TEXT_AREA];
22691 if (draw == DRAW_NORMAL_TEXT)
22692 row->fill_line_p = 1; /* Clear to end of line */
22693 }
22694
22695 if (end_hpos > start_hpos)
22696 {
22697 draw_glyphs (w, start_x, row, TEXT_AREA,
22698 start_hpos, end_hpos,
22699 draw, 0);
22700
22701 row->mouse_face_p
22702 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22703 }
22704 }
22705
22706 /* When we've written over the cursor, arrange for it to
22707 be displayed again. */
22708 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22709 {
22710 BLOCK_INPUT;
22711 display_and_set_cursor (w, 1,
22712 w->phys_cursor.hpos, w->phys_cursor.vpos,
22713 w->phys_cursor.x, w->phys_cursor.y);
22714 UNBLOCK_INPUT;
22715 }
22716 }
22717
22718 /* Change the mouse cursor. */
22719 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22720 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22721 else if (draw == DRAW_MOUSE_FACE)
22722 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22723 else
22724 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22725 }
22726
22727 /* EXPORT:
22728 Clear out the mouse-highlighted active region.
22729 Redraw it un-highlighted first. Value is non-zero if mouse
22730 face was actually drawn unhighlighted. */
22731
22732 int
22733 clear_mouse_face (dpyinfo)
22734 Display_Info *dpyinfo;
22735 {
22736 int cleared = 0;
22737
22738 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22739 {
22740 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22741 cleared = 1;
22742 }
22743
22744 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22745 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22746 dpyinfo->mouse_face_window = Qnil;
22747 dpyinfo->mouse_face_overlay = Qnil;
22748 return cleared;
22749 }
22750
22751
22752 /* EXPORT:
22753 Non-zero if physical cursor of window W is within mouse face. */
22754
22755 int
22756 cursor_in_mouse_face_p (w)
22757 struct window *w;
22758 {
22759 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22760 int in_mouse_face = 0;
22761
22762 if (WINDOWP (dpyinfo->mouse_face_window)
22763 && XWINDOW (dpyinfo->mouse_face_window) == w)
22764 {
22765 int hpos = w->phys_cursor.hpos;
22766 int vpos = w->phys_cursor.vpos;
22767
22768 if (vpos >= dpyinfo->mouse_face_beg_row
22769 && vpos <= dpyinfo->mouse_face_end_row
22770 && (vpos > dpyinfo->mouse_face_beg_row
22771 || hpos >= dpyinfo->mouse_face_beg_col)
22772 && (vpos < dpyinfo->mouse_face_end_row
22773 || hpos < dpyinfo->mouse_face_end_col
22774 || dpyinfo->mouse_face_past_end))
22775 in_mouse_face = 1;
22776 }
22777
22778 return in_mouse_face;
22779 }
22780
22781
22782
22783 \f
22784 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22785 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22786 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22787 for the overlay or run of text properties specifying the mouse
22788 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22789 before-string and after-string that must also be highlighted.
22790 DISPLAY_STRING, if non-nil, is a display string that may cover some
22791 or all of the highlighted text. */
22792
22793 static void
22794 mouse_face_from_buffer_pos (Lisp_Object window,
22795 Display_Info *dpyinfo,
22796 EMACS_INT mouse_charpos,
22797 EMACS_INT start_charpos,
22798 EMACS_INT end_charpos,
22799 Lisp_Object before_string,
22800 Lisp_Object after_string,
22801 Lisp_Object display_string)
22802 {
22803 struct window *w = XWINDOW (window);
22804 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22805 struct glyph_row *row;
22806 struct glyph *glyph, *end;
22807 EMACS_INT ignore;
22808 int x;
22809
22810 xassert (NILP (display_string) || STRINGP (display_string));
22811 xassert (NILP (before_string) || STRINGP (before_string));
22812 xassert (NILP (after_string) || STRINGP (after_string));
22813
22814 /* Find the first highlighted glyph. */
22815 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22816 {
22817 dpyinfo->mouse_face_beg_col = 0;
22818 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22819 dpyinfo->mouse_face_beg_x = first->x;
22820 dpyinfo->mouse_face_beg_y = first->y;
22821 }
22822 else
22823 {
22824 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22825 if (row == NULL)
22826 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22827
22828 /* If the before-string or display-string contains newlines,
22829 row_containing_pos skips to its last row. Move back. */
22830 if (!NILP (before_string) || !NILP (display_string))
22831 {
22832 struct glyph_row *prev;
22833 while ((prev = row - 1, prev >= first)
22834 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22835 && prev->used[TEXT_AREA] > 0)
22836 {
22837 struct glyph *beg = prev->glyphs[TEXT_AREA];
22838 glyph = beg + prev->used[TEXT_AREA];
22839 while (--glyph >= beg && INTEGERP (glyph->object));
22840 if (glyph < beg
22841 || !(EQ (glyph->object, before_string)
22842 || EQ (glyph->object, display_string)))
22843 break;
22844 row = prev;
22845 }
22846 }
22847
22848 glyph = row->glyphs[TEXT_AREA];
22849 end = glyph + row->used[TEXT_AREA];
22850 x = row->x;
22851 dpyinfo->mouse_face_beg_y = row->y;
22852 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22853
22854 /* Skip truncation glyphs at the start of the glyph row. */
22855 if (row->displays_text_p)
22856 for (; glyph < end
22857 && INTEGERP (glyph->object)
22858 && glyph->charpos < 0;
22859 ++glyph)
22860 x += glyph->pixel_width;
22861
22862 /* Scan the glyph row, stopping before BEFORE_STRING or
22863 DISPLAY_STRING or START_CHARPOS. */
22864 for (; glyph < end
22865 && !INTEGERP (glyph->object)
22866 && !EQ (glyph->object, before_string)
22867 && !EQ (glyph->object, display_string)
22868 && !(BUFFERP (glyph->object)
22869 && glyph->charpos >= start_charpos);
22870 ++glyph)
22871 x += glyph->pixel_width;
22872
22873 dpyinfo->mouse_face_beg_x = x;
22874 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22875 }
22876
22877 /* Find the last highlighted glyph. */
22878 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22879 if (row == NULL)
22880 {
22881 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22882 dpyinfo->mouse_face_past_end = 1;
22883 }
22884 else if (!NILP (after_string))
22885 {
22886 /* If the after-string has newlines, advance to its last row. */
22887 struct glyph_row *next;
22888 struct glyph_row *last
22889 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22890
22891 for (next = row + 1;
22892 next <= last
22893 && next->used[TEXT_AREA] > 0
22894 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22895 ++next)
22896 row = next;
22897 }
22898
22899 glyph = row->glyphs[TEXT_AREA];
22900 end = glyph + row->used[TEXT_AREA];
22901 x = row->x;
22902 dpyinfo->mouse_face_end_y = row->y;
22903 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22904
22905 /* Skip truncation glyphs at the start of the row. */
22906 if (row->displays_text_p)
22907 for (; glyph < end
22908 && INTEGERP (glyph->object)
22909 && glyph->charpos < 0;
22910 ++glyph)
22911 x += glyph->pixel_width;
22912
22913 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22914 AFTER_STRING. */
22915 for (; glyph < end
22916 && !INTEGERP (glyph->object)
22917 && !EQ (glyph->object, after_string)
22918 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22919 ++glyph)
22920 x += glyph->pixel_width;
22921
22922 /* If we found AFTER_STRING, consume it and stop. */
22923 if (EQ (glyph->object, after_string))
22924 {
22925 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22926 x += glyph->pixel_width;
22927 }
22928 else
22929 {
22930 /* If there's no after-string, we must check if we overshot,
22931 which might be the case if we stopped after a string glyph.
22932 That glyph may belong to a before-string or display-string
22933 associated with the end position, which must not be
22934 highlighted. */
22935 Lisp_Object prev_object;
22936 int pos;
22937
22938 while (glyph > row->glyphs[TEXT_AREA])
22939 {
22940 prev_object = (glyph - 1)->object;
22941 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
22942 break;
22943
22944 pos = string_buffer_position (w, prev_object, end_charpos);
22945 if (pos && pos < end_charpos)
22946 break;
22947
22948 for (; glyph > row->glyphs[TEXT_AREA]
22949 && EQ ((glyph - 1)->object, prev_object);
22950 --glyph)
22951 x -= (glyph - 1)->pixel_width;
22952 }
22953 }
22954
22955 dpyinfo->mouse_face_end_x = x;
22956 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
22957 dpyinfo->mouse_face_window = window;
22958 dpyinfo->mouse_face_face_id
22959 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
22960 mouse_charpos + 1,
22961 !dpyinfo->mouse_face_hidden, -1);
22962 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22963 }
22964
22965
22966 /* Find the position of the glyph for position POS in OBJECT in
22967 window W's current matrix, and return in *X, *Y the pixel
22968 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22969
22970 RIGHT_P non-zero means return the position of the right edge of the
22971 glyph, RIGHT_P zero means return the left edge position.
22972
22973 If no glyph for POS exists in the matrix, return the position of
22974 the glyph with the next smaller position that is in the matrix, if
22975 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22976 exists in the matrix, return the position of the glyph with the
22977 next larger position in OBJECT.
22978
22979 Value is non-zero if a glyph was found. */
22980
22981 static int
22982 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22983 struct window *w;
22984 EMACS_INT pos;
22985 Lisp_Object object;
22986 int *hpos, *vpos, *x, *y;
22987 int right_p;
22988 {
22989 int yb = window_text_bottom_y (w);
22990 struct glyph_row *r;
22991 struct glyph *best_glyph = NULL;
22992 struct glyph_row *best_row = NULL;
22993 int best_x = 0;
22994
22995 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22996 r->enabled_p && r->y < yb;
22997 ++r)
22998 {
22999 struct glyph *g = r->glyphs[TEXT_AREA];
23000 struct glyph *e = g + r->used[TEXT_AREA];
23001 int gx;
23002
23003 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
23004 if (EQ (g->object, object))
23005 {
23006 if (g->charpos == pos)
23007 {
23008 best_glyph = g;
23009 best_x = gx;
23010 best_row = r;
23011 goto found;
23012 }
23013 else if (best_glyph == NULL
23014 || ((eabs (g->charpos - pos)
23015 < eabs (best_glyph->charpos - pos))
23016 && (right_p
23017 ? g->charpos < pos
23018 : g->charpos > pos)))
23019 {
23020 best_glyph = g;
23021 best_x = gx;
23022 best_row = r;
23023 }
23024 }
23025 }
23026
23027 found:
23028
23029 if (best_glyph)
23030 {
23031 *x = best_x;
23032 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
23033
23034 if (right_p)
23035 {
23036 *x += best_glyph->pixel_width;
23037 ++*hpos;
23038 }
23039
23040 *y = best_row->y;
23041 *vpos = best_row - w->current_matrix->rows;
23042 }
23043
23044 return best_glyph != NULL;
23045 }
23046
23047
23048 /* See if position X, Y is within a hot-spot of an image. */
23049
23050 static int
23051 on_hot_spot_p (hot_spot, x, y)
23052 Lisp_Object hot_spot;
23053 int x, y;
23054 {
23055 if (!CONSP (hot_spot))
23056 return 0;
23057
23058 if (EQ (XCAR (hot_spot), Qrect))
23059 {
23060 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23061 Lisp_Object rect = XCDR (hot_spot);
23062 Lisp_Object tem;
23063 if (!CONSP (rect))
23064 return 0;
23065 if (!CONSP (XCAR (rect)))
23066 return 0;
23067 if (!CONSP (XCDR (rect)))
23068 return 0;
23069 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23070 return 0;
23071 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23072 return 0;
23073 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23074 return 0;
23075 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23076 return 0;
23077 return 1;
23078 }
23079 else if (EQ (XCAR (hot_spot), Qcircle))
23080 {
23081 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23082 Lisp_Object circ = XCDR (hot_spot);
23083 Lisp_Object lr, lx0, ly0;
23084 if (CONSP (circ)
23085 && CONSP (XCAR (circ))
23086 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23087 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23088 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23089 {
23090 double r = XFLOATINT (lr);
23091 double dx = XINT (lx0) - x;
23092 double dy = XINT (ly0) - y;
23093 return (dx * dx + dy * dy <= r * r);
23094 }
23095 }
23096 else if (EQ (XCAR (hot_spot), Qpoly))
23097 {
23098 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23099 if (VECTORP (XCDR (hot_spot)))
23100 {
23101 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23102 Lisp_Object *poly = v->contents;
23103 int n = v->header.size;
23104 int i;
23105 int inside = 0;
23106 Lisp_Object lx, ly;
23107 int x0, y0;
23108
23109 /* Need an even number of coordinates, and at least 3 edges. */
23110 if (n < 6 || n & 1)
23111 return 0;
23112
23113 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23114 If count is odd, we are inside polygon. Pixels on edges
23115 may or may not be included depending on actual geometry of the
23116 polygon. */
23117 if ((lx = poly[n-2], !INTEGERP (lx))
23118 || (ly = poly[n-1], !INTEGERP (lx)))
23119 return 0;
23120 x0 = XINT (lx), y0 = XINT (ly);
23121 for (i = 0; i < n; i += 2)
23122 {
23123 int x1 = x0, y1 = y0;
23124 if ((lx = poly[i], !INTEGERP (lx))
23125 || (ly = poly[i+1], !INTEGERP (ly)))
23126 return 0;
23127 x0 = XINT (lx), y0 = XINT (ly);
23128
23129 /* Does this segment cross the X line? */
23130 if (x0 >= x)
23131 {
23132 if (x1 >= x)
23133 continue;
23134 }
23135 else if (x1 < x)
23136 continue;
23137 if (y > y0 && y > y1)
23138 continue;
23139 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23140 inside = !inside;
23141 }
23142 return inside;
23143 }
23144 }
23145 return 0;
23146 }
23147
23148 Lisp_Object
23149 find_hot_spot (map, x, y)
23150 Lisp_Object map;
23151 int x, y;
23152 {
23153 while (CONSP (map))
23154 {
23155 if (CONSP (XCAR (map))
23156 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23157 return XCAR (map);
23158 map = XCDR (map);
23159 }
23160
23161 return Qnil;
23162 }
23163
23164 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23165 3, 3, 0,
23166 doc: /* Lookup in image map MAP coordinates X and Y.
23167 An image map is an alist where each element has the format (AREA ID PLIST).
23168 An AREA is specified as either a rectangle, a circle, or a polygon:
23169 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23170 pixel coordinates of the upper left and bottom right corners.
23171 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23172 and the radius of the circle; r may be a float or integer.
23173 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23174 vector describes one corner in the polygon.
23175 Returns the alist element for the first matching AREA in MAP. */)
23176 (map, x, y)
23177 Lisp_Object map;
23178 Lisp_Object x, y;
23179 {
23180 if (NILP (map))
23181 return Qnil;
23182
23183 CHECK_NUMBER (x);
23184 CHECK_NUMBER (y);
23185
23186 return find_hot_spot (map, XINT (x), XINT (y));
23187 }
23188
23189
23190 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23191 static void
23192 define_frame_cursor1 (f, cursor, pointer)
23193 struct frame *f;
23194 Cursor cursor;
23195 Lisp_Object pointer;
23196 {
23197 /* Do not change cursor shape while dragging mouse. */
23198 if (!NILP (do_mouse_tracking))
23199 return;
23200
23201 if (!NILP (pointer))
23202 {
23203 if (EQ (pointer, Qarrow))
23204 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23205 else if (EQ (pointer, Qhand))
23206 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23207 else if (EQ (pointer, Qtext))
23208 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23209 else if (EQ (pointer, intern ("hdrag")))
23210 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23211 #ifdef HAVE_X_WINDOWS
23212 else if (EQ (pointer, intern ("vdrag")))
23213 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23214 #endif
23215 else if (EQ (pointer, intern ("hourglass")))
23216 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23217 else if (EQ (pointer, Qmodeline))
23218 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23219 else
23220 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23221 }
23222
23223 if (cursor != No_Cursor)
23224 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23225 }
23226
23227 /* Take proper action when mouse has moved to the mode or header line
23228 or marginal area AREA of window W, x-position X and y-position Y.
23229 X is relative to the start of the text display area of W, so the
23230 width of bitmap areas and scroll bars must be subtracted to get a
23231 position relative to the start of the mode line. */
23232
23233 static void
23234 note_mode_line_or_margin_highlight (window, x, y, area)
23235 Lisp_Object window;
23236 int x, y;
23237 enum window_part area;
23238 {
23239 struct window *w = XWINDOW (window);
23240 struct frame *f = XFRAME (w->frame);
23241 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23242 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23243 Lisp_Object pointer = Qnil;
23244 int charpos, dx, dy, width, height;
23245 Lisp_Object string, object = Qnil;
23246 Lisp_Object pos, help;
23247
23248 Lisp_Object mouse_face;
23249 int original_x_pixel = x;
23250 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23251 struct glyph_row *row;
23252
23253 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23254 {
23255 int x0;
23256 struct glyph *end;
23257
23258 string = mode_line_string (w, area, &x, &y, &charpos,
23259 &object, &dx, &dy, &width, &height);
23260
23261 row = (area == ON_MODE_LINE
23262 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23263 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23264
23265 /* Find glyph */
23266 if (row->mode_line_p && row->enabled_p)
23267 {
23268 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23269 end = glyph + row->used[TEXT_AREA];
23270
23271 for (x0 = original_x_pixel;
23272 glyph < end && x0 >= glyph->pixel_width;
23273 ++glyph)
23274 x0 -= glyph->pixel_width;
23275
23276 if (glyph >= end)
23277 glyph = NULL;
23278 }
23279 }
23280 else
23281 {
23282 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23283 string = marginal_area_string (w, area, &x, &y, &charpos,
23284 &object, &dx, &dy, &width, &height);
23285 }
23286
23287 help = Qnil;
23288
23289 if (IMAGEP (object))
23290 {
23291 Lisp_Object image_map, hotspot;
23292 if ((image_map = Fplist_get (XCDR (object), QCmap),
23293 !NILP (image_map))
23294 && (hotspot = find_hot_spot (image_map, dx, dy),
23295 CONSP (hotspot))
23296 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23297 {
23298 Lisp_Object area_id, plist;
23299
23300 area_id = XCAR (hotspot);
23301 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23302 If so, we could look for mouse-enter, mouse-leave
23303 properties in PLIST (and do something...). */
23304 hotspot = XCDR (hotspot);
23305 if (CONSP (hotspot)
23306 && (plist = XCAR (hotspot), CONSP (plist)))
23307 {
23308 pointer = Fplist_get (plist, Qpointer);
23309 if (NILP (pointer))
23310 pointer = Qhand;
23311 help = Fplist_get (plist, Qhelp_echo);
23312 if (!NILP (help))
23313 {
23314 help_echo_string = help;
23315 /* Is this correct? ++kfs */
23316 XSETWINDOW (help_echo_window, w);
23317 help_echo_object = w->buffer;
23318 help_echo_pos = charpos;
23319 }
23320 }
23321 }
23322 if (NILP (pointer))
23323 pointer = Fplist_get (XCDR (object), QCpointer);
23324 }
23325
23326 if (STRINGP (string))
23327 {
23328 pos = make_number (charpos);
23329 /* If we're on a string with `help-echo' text property, arrange
23330 for the help to be displayed. This is done by setting the
23331 global variable help_echo_string to the help string. */
23332 if (NILP (help))
23333 {
23334 help = Fget_text_property (pos, Qhelp_echo, string);
23335 if (!NILP (help))
23336 {
23337 help_echo_string = help;
23338 XSETWINDOW (help_echo_window, w);
23339 help_echo_object = string;
23340 help_echo_pos = charpos;
23341 }
23342 }
23343
23344 if (NILP (pointer))
23345 pointer = Fget_text_property (pos, Qpointer, string);
23346
23347 /* Change the mouse pointer according to what is under X/Y. */
23348 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23349 {
23350 Lisp_Object map;
23351 map = Fget_text_property (pos, Qlocal_map, string);
23352 if (!KEYMAPP (map))
23353 map = Fget_text_property (pos, Qkeymap, string);
23354 if (!KEYMAPP (map))
23355 cursor = dpyinfo->vertical_scroll_bar_cursor;
23356 }
23357
23358 /* Change the mouse face according to what is under X/Y. */
23359 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23360 if (!NILP (mouse_face)
23361 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23362 && glyph)
23363 {
23364 Lisp_Object b, e;
23365
23366 struct glyph * tmp_glyph;
23367
23368 int gpos;
23369 int gseq_length;
23370 int total_pixel_width;
23371 EMACS_INT ignore;
23372
23373 int vpos, hpos;
23374
23375 b = Fprevious_single_property_change (make_number (charpos + 1),
23376 Qmouse_face, string, Qnil);
23377 if (NILP (b))
23378 b = make_number (0);
23379
23380 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23381 if (NILP (e))
23382 e = make_number (SCHARS (string));
23383
23384 /* Calculate the position(glyph position: GPOS) of GLYPH in
23385 displayed string. GPOS is different from CHARPOS.
23386
23387 CHARPOS is the position of glyph in internal string
23388 object. A mode line string format has structures which
23389 is converted to a flatten by emacs lisp interpreter.
23390 The internal string is an element of the structures.
23391 The displayed string is the flatten string. */
23392 gpos = 0;
23393 if (glyph > row_start_glyph)
23394 {
23395 tmp_glyph = glyph - 1;
23396 while (tmp_glyph >= row_start_glyph
23397 && tmp_glyph->charpos >= XINT (b)
23398 && EQ (tmp_glyph->object, glyph->object))
23399 {
23400 tmp_glyph--;
23401 gpos++;
23402 }
23403 }
23404
23405 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23406 displayed string holding GLYPH.
23407
23408 GSEQ_LENGTH is different from SCHARS (STRING).
23409 SCHARS (STRING) returns the length of the internal string. */
23410 for (tmp_glyph = glyph, gseq_length = gpos;
23411 tmp_glyph->charpos < XINT (e);
23412 tmp_glyph++, gseq_length++)
23413 {
23414 if (!EQ (tmp_glyph->object, glyph->object))
23415 break;
23416 }
23417
23418 total_pixel_width = 0;
23419 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23420 total_pixel_width += tmp_glyph->pixel_width;
23421
23422 /* Pre calculation of re-rendering position */
23423 vpos = (x - gpos);
23424 hpos = (area == ON_MODE_LINE
23425 ? (w->current_matrix)->nrows - 1
23426 : 0);
23427
23428 /* If the re-rendering position is included in the last
23429 re-rendering area, we should do nothing. */
23430 if ( EQ (window, dpyinfo->mouse_face_window)
23431 && dpyinfo->mouse_face_beg_col <= vpos
23432 && vpos < dpyinfo->mouse_face_end_col
23433 && dpyinfo->mouse_face_beg_row == hpos )
23434 return;
23435
23436 if (clear_mouse_face (dpyinfo))
23437 cursor = No_Cursor;
23438
23439 dpyinfo->mouse_face_beg_col = vpos;
23440 dpyinfo->mouse_face_beg_row = hpos;
23441
23442 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23443 dpyinfo->mouse_face_beg_y = 0;
23444
23445 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23446 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23447
23448 dpyinfo->mouse_face_end_x = 0;
23449 dpyinfo->mouse_face_end_y = 0;
23450
23451 dpyinfo->mouse_face_past_end = 0;
23452 dpyinfo->mouse_face_window = window;
23453
23454 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23455 charpos,
23456 0, 0, 0, &ignore,
23457 glyph->face_id, 1);
23458 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23459
23460 if (NILP (pointer))
23461 pointer = Qhand;
23462 }
23463 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23464 clear_mouse_face (dpyinfo);
23465 }
23466 define_frame_cursor1 (f, cursor, pointer);
23467 }
23468
23469
23470 /* EXPORT:
23471 Take proper action when the mouse has moved to position X, Y on
23472 frame F as regards highlighting characters that have mouse-face
23473 properties. Also de-highlighting chars where the mouse was before.
23474 X and Y can be negative or out of range. */
23475
23476 void
23477 note_mouse_highlight (f, x, y)
23478 struct frame *f;
23479 int x, y;
23480 {
23481 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23482 enum window_part part;
23483 Lisp_Object window;
23484 struct window *w;
23485 Cursor cursor = No_Cursor;
23486 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23487 struct buffer *b;
23488
23489 /* When a menu is active, don't highlight because this looks odd. */
23490 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23491 if (popup_activated ())
23492 return;
23493 #endif
23494
23495 if (NILP (Vmouse_highlight)
23496 || !f->glyphs_initialized_p
23497 || f->pointer_invisible)
23498 return;
23499
23500 dpyinfo->mouse_face_mouse_x = x;
23501 dpyinfo->mouse_face_mouse_y = y;
23502 dpyinfo->mouse_face_mouse_frame = f;
23503
23504 if (dpyinfo->mouse_face_defer)
23505 return;
23506
23507 if (gc_in_progress)
23508 {
23509 dpyinfo->mouse_face_deferred_gc = 1;
23510 return;
23511 }
23512
23513 /* Which window is that in? */
23514 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23515
23516 /* If we were displaying active text in another window, clear that.
23517 Also clear if we move out of text area in same window. */
23518 if (! EQ (window, dpyinfo->mouse_face_window)
23519 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23520 && !NILP (dpyinfo->mouse_face_window)))
23521 clear_mouse_face (dpyinfo);
23522
23523 /* Not on a window -> return. */
23524 if (!WINDOWP (window))
23525 return;
23526
23527 /* Reset help_echo_string. It will get recomputed below. */
23528 help_echo_string = Qnil;
23529
23530 /* Convert to window-relative pixel coordinates. */
23531 w = XWINDOW (window);
23532 frame_to_window_pixel_xy (w, &x, &y);
23533
23534 /* Handle tool-bar window differently since it doesn't display a
23535 buffer. */
23536 if (EQ (window, f->tool_bar_window))
23537 {
23538 note_tool_bar_highlight (f, x, y);
23539 return;
23540 }
23541
23542 /* Mouse is on the mode, header line or margin? */
23543 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23544 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23545 {
23546 note_mode_line_or_margin_highlight (window, x, y, part);
23547 return;
23548 }
23549
23550 if (part == ON_VERTICAL_BORDER)
23551 {
23552 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23553 help_echo_string = build_string ("drag-mouse-1: resize");
23554 }
23555 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23556 || part == ON_SCROLL_BAR)
23557 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23558 else
23559 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23560
23561 /* Are we in a window whose display is up to date?
23562 And verify the buffer's text has not changed. */
23563 b = XBUFFER (w->buffer);
23564 if (part == ON_TEXT
23565 && EQ (w->window_end_valid, w->buffer)
23566 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23567 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23568 {
23569 int hpos, vpos, pos, i, dx, dy, area;
23570 struct glyph *glyph;
23571 Lisp_Object object;
23572 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23573 Lisp_Object *overlay_vec = NULL;
23574 int noverlays;
23575 struct buffer *obuf;
23576 int obegv, ozv, same_region;
23577
23578 /* Find the glyph under X/Y. */
23579 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23580
23581 /* Look for :pointer property on image. */
23582 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23583 {
23584 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23585 if (img != NULL && IMAGEP (img->spec))
23586 {
23587 Lisp_Object image_map, hotspot;
23588 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23589 !NILP (image_map))
23590 && (hotspot = find_hot_spot (image_map,
23591 glyph->slice.x + dx,
23592 glyph->slice.y + dy),
23593 CONSP (hotspot))
23594 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23595 {
23596 Lisp_Object area_id, plist;
23597
23598 area_id = XCAR (hotspot);
23599 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23600 If so, we could look for mouse-enter, mouse-leave
23601 properties in PLIST (and do something...). */
23602 hotspot = XCDR (hotspot);
23603 if (CONSP (hotspot)
23604 && (plist = XCAR (hotspot), CONSP (plist)))
23605 {
23606 pointer = Fplist_get (plist, Qpointer);
23607 if (NILP (pointer))
23608 pointer = Qhand;
23609 help_echo_string = Fplist_get (plist, Qhelp_echo);
23610 if (!NILP (help_echo_string))
23611 {
23612 help_echo_window = window;
23613 help_echo_object = glyph->object;
23614 help_echo_pos = glyph->charpos;
23615 }
23616 }
23617 }
23618 if (NILP (pointer))
23619 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23620 }
23621 }
23622
23623 /* Clear mouse face if X/Y not over text. */
23624 if (glyph == NULL
23625 || area != TEXT_AREA
23626 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23627 {
23628 if (clear_mouse_face (dpyinfo))
23629 cursor = No_Cursor;
23630 if (NILP (pointer))
23631 {
23632 if (area != TEXT_AREA)
23633 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23634 else
23635 pointer = Vvoid_text_area_pointer;
23636 }
23637 goto set_cursor;
23638 }
23639
23640 pos = glyph->charpos;
23641 object = glyph->object;
23642 if (!STRINGP (object) && !BUFFERP (object))
23643 goto set_cursor;
23644
23645 /* If we get an out-of-range value, return now; avoid an error. */
23646 if (BUFFERP (object) && pos > BUF_Z (b))
23647 goto set_cursor;
23648
23649 /* Make the window's buffer temporarily current for
23650 overlays_at and compute_char_face. */
23651 obuf = current_buffer;
23652 current_buffer = b;
23653 obegv = BEGV;
23654 ozv = ZV;
23655 BEGV = BEG;
23656 ZV = Z;
23657
23658 /* Is this char mouse-active or does it have help-echo? */
23659 position = make_number (pos);
23660
23661 if (BUFFERP (object))
23662 {
23663 /* Put all the overlays we want in a vector in overlay_vec. */
23664 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23665 /* Sort overlays into increasing priority order. */
23666 noverlays = sort_overlays (overlay_vec, noverlays, w);
23667 }
23668 else
23669 noverlays = 0;
23670
23671 same_region = (EQ (window, dpyinfo->mouse_face_window)
23672 && vpos >= dpyinfo->mouse_face_beg_row
23673 && vpos <= dpyinfo->mouse_face_end_row
23674 && (vpos > dpyinfo->mouse_face_beg_row
23675 || hpos >= dpyinfo->mouse_face_beg_col)
23676 && (vpos < dpyinfo->mouse_face_end_row
23677 || hpos < dpyinfo->mouse_face_end_col
23678 || dpyinfo->mouse_face_past_end));
23679
23680 if (same_region)
23681 cursor = No_Cursor;
23682
23683 /* Check mouse-face highlighting. */
23684 if (! same_region
23685 /* If there exists an overlay with mouse-face overlapping
23686 the one we are currently highlighting, we have to
23687 check if we enter the overlapping overlay, and then
23688 highlight only that. */
23689 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23690 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23691 {
23692 /* Find the highest priority overlay with a mouse-face. */
23693 overlay = Qnil;
23694 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23695 {
23696 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23697 if (!NILP (mouse_face))
23698 overlay = overlay_vec[i];
23699 }
23700
23701 /* If we're highlighting the same overlay as before, there's
23702 no need to do that again. */
23703 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23704 goto check_help_echo;
23705 dpyinfo->mouse_face_overlay = overlay;
23706
23707 /* Clear the display of the old active region, if any. */
23708 if (clear_mouse_face (dpyinfo))
23709 cursor = No_Cursor;
23710
23711 /* If no overlay applies, get a text property. */
23712 if (NILP (overlay))
23713 mouse_face = Fget_text_property (position, Qmouse_face, object);
23714
23715 /* Next, compute the bounds of the mouse highlighting and
23716 display it. */
23717 if (!NILP (mouse_face) && STRINGP (object))
23718 {
23719 /* The mouse-highlighting comes from a display string
23720 with a mouse-face. */
23721 Lisp_Object b, e;
23722 EMACS_INT ignore;
23723
23724 b = Fprevious_single_property_change
23725 (make_number (pos + 1), Qmouse_face, object, Qnil);
23726 e = Fnext_single_property_change
23727 (position, Qmouse_face, object, Qnil);
23728 if (NILP (b))
23729 b = make_number (0);
23730 if (NILP (e))
23731 e = make_number (SCHARS (object) - 1);
23732
23733 fast_find_string_pos (w, XINT (b), object,
23734 &dpyinfo->mouse_face_beg_col,
23735 &dpyinfo->mouse_face_beg_row,
23736 &dpyinfo->mouse_face_beg_x,
23737 &dpyinfo->mouse_face_beg_y, 0);
23738 fast_find_string_pos (w, XINT (e), object,
23739 &dpyinfo->mouse_face_end_col,
23740 &dpyinfo->mouse_face_end_row,
23741 &dpyinfo->mouse_face_end_x,
23742 &dpyinfo->mouse_face_end_y, 1);
23743 dpyinfo->mouse_face_past_end = 0;
23744 dpyinfo->mouse_face_window = window;
23745 dpyinfo->mouse_face_face_id
23746 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23747 glyph->face_id, 1);
23748 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23749 cursor = No_Cursor;
23750 }
23751 else
23752 {
23753 /* The mouse-highlighting, if any, comes from an overlay
23754 or text property in the buffer. */
23755 Lisp_Object buffer, display_string;
23756
23757 if (STRINGP (object))
23758 {
23759 /* If we are on a display string with no mouse-face,
23760 check if the text under it has one. */
23761 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23762 int start = MATRIX_ROW_START_CHARPOS (r);
23763 pos = string_buffer_position (w, object, start);
23764 if (pos > 0)
23765 {
23766 mouse_face = get_char_property_and_overlay
23767 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23768 buffer = w->buffer;
23769 display_string = object;
23770 }
23771 }
23772 else
23773 {
23774 buffer = object;
23775 display_string = Qnil;
23776 }
23777
23778 if (!NILP (mouse_face))
23779 {
23780 Lisp_Object before, after;
23781 Lisp_Object before_string, after_string;
23782
23783 if (NILP (overlay))
23784 {
23785 /* Handle the text property case. */
23786 before = Fprevious_single_property_change
23787 (make_number (pos + 1), Qmouse_face, buffer,
23788 Fmarker_position (w->start));
23789 after = Fnext_single_property_change
23790 (make_number (pos), Qmouse_face, buffer,
23791 make_number (BUF_Z (XBUFFER (buffer))
23792 - XFASTINT (w->window_end_pos)));
23793 before_string = after_string = Qnil;
23794 }
23795 else
23796 {
23797 /* Handle the overlay case. */
23798 before = Foverlay_start (overlay);
23799 after = Foverlay_end (overlay);
23800 before_string = Foverlay_get (overlay, Qbefore_string);
23801 after_string = Foverlay_get (overlay, Qafter_string);
23802
23803 if (!STRINGP (before_string)) before_string = Qnil;
23804 if (!STRINGP (after_string)) after_string = Qnil;
23805 }
23806
23807 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23808 XFASTINT (before),
23809 XFASTINT (after),
23810 before_string, after_string,
23811 display_string);
23812 cursor = No_Cursor;
23813 }
23814 }
23815 }
23816
23817 check_help_echo:
23818
23819 /* Look for a `help-echo' property. */
23820 if (NILP (help_echo_string)) {
23821 Lisp_Object help, overlay;
23822
23823 /* Check overlays first. */
23824 help = overlay = Qnil;
23825 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23826 {
23827 overlay = overlay_vec[i];
23828 help = Foverlay_get (overlay, Qhelp_echo);
23829 }
23830
23831 if (!NILP (help))
23832 {
23833 help_echo_string = help;
23834 help_echo_window = window;
23835 help_echo_object = overlay;
23836 help_echo_pos = pos;
23837 }
23838 else
23839 {
23840 Lisp_Object object = glyph->object;
23841 int charpos = glyph->charpos;
23842
23843 /* Try text properties. */
23844 if (STRINGP (object)
23845 && charpos >= 0
23846 && charpos < SCHARS (object))
23847 {
23848 help = Fget_text_property (make_number (charpos),
23849 Qhelp_echo, object);
23850 if (NILP (help))
23851 {
23852 /* If the string itself doesn't specify a help-echo,
23853 see if the buffer text ``under'' it does. */
23854 struct glyph_row *r
23855 = MATRIX_ROW (w->current_matrix, vpos);
23856 int start = MATRIX_ROW_START_CHARPOS (r);
23857 int pos = string_buffer_position (w, object, start);
23858 if (pos > 0)
23859 {
23860 help = Fget_char_property (make_number (pos),
23861 Qhelp_echo, w->buffer);
23862 if (!NILP (help))
23863 {
23864 charpos = pos;
23865 object = w->buffer;
23866 }
23867 }
23868 }
23869 }
23870 else if (BUFFERP (object)
23871 && charpos >= BEGV
23872 && charpos < ZV)
23873 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23874 object);
23875
23876 if (!NILP (help))
23877 {
23878 help_echo_string = help;
23879 help_echo_window = window;
23880 help_echo_object = object;
23881 help_echo_pos = charpos;
23882 }
23883 }
23884 }
23885
23886 /* Look for a `pointer' property. */
23887 if (NILP (pointer))
23888 {
23889 /* Check overlays first. */
23890 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23891 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23892
23893 if (NILP (pointer))
23894 {
23895 Lisp_Object object = glyph->object;
23896 int charpos = glyph->charpos;
23897
23898 /* Try text properties. */
23899 if (STRINGP (object)
23900 && charpos >= 0
23901 && charpos < SCHARS (object))
23902 {
23903 pointer = Fget_text_property (make_number (charpos),
23904 Qpointer, object);
23905 if (NILP (pointer))
23906 {
23907 /* If the string itself doesn't specify a pointer,
23908 see if the buffer text ``under'' it does. */
23909 struct glyph_row *r
23910 = MATRIX_ROW (w->current_matrix, vpos);
23911 int start = MATRIX_ROW_START_CHARPOS (r);
23912 int pos = string_buffer_position (w, object, start);
23913 if (pos > 0)
23914 pointer = Fget_char_property (make_number (pos),
23915 Qpointer, w->buffer);
23916 }
23917 }
23918 else if (BUFFERP (object)
23919 && charpos >= BEGV
23920 && charpos < ZV)
23921 pointer = Fget_text_property (make_number (charpos),
23922 Qpointer, object);
23923 }
23924 }
23925
23926 BEGV = obegv;
23927 ZV = ozv;
23928 current_buffer = obuf;
23929 }
23930
23931 set_cursor:
23932
23933 define_frame_cursor1 (f, cursor, pointer);
23934 }
23935
23936
23937 /* EXPORT for RIF:
23938 Clear any mouse-face on window W. This function is part of the
23939 redisplay interface, and is called from try_window_id and similar
23940 functions to ensure the mouse-highlight is off. */
23941
23942 void
23943 x_clear_window_mouse_face (w)
23944 struct window *w;
23945 {
23946 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23947 Lisp_Object window;
23948
23949 BLOCK_INPUT;
23950 XSETWINDOW (window, w);
23951 if (EQ (window, dpyinfo->mouse_face_window))
23952 clear_mouse_face (dpyinfo);
23953 UNBLOCK_INPUT;
23954 }
23955
23956
23957 /* EXPORT:
23958 Just discard the mouse face information for frame F, if any.
23959 This is used when the size of F is changed. */
23960
23961 void
23962 cancel_mouse_face (f)
23963 struct frame *f;
23964 {
23965 Lisp_Object window;
23966 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23967
23968 window = dpyinfo->mouse_face_window;
23969 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23970 {
23971 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23972 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23973 dpyinfo->mouse_face_window = Qnil;
23974 }
23975 }
23976
23977
23978 #endif /* HAVE_WINDOW_SYSTEM */
23979
23980 \f
23981 /***********************************************************************
23982 Exposure Events
23983 ***********************************************************************/
23984
23985 #ifdef HAVE_WINDOW_SYSTEM
23986
23987 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23988 which intersects rectangle R. R is in window-relative coordinates. */
23989
23990 static void
23991 expose_area (w, row, r, area)
23992 struct window *w;
23993 struct glyph_row *row;
23994 XRectangle *r;
23995 enum glyph_row_area area;
23996 {
23997 struct glyph *first = row->glyphs[area];
23998 struct glyph *end = row->glyphs[area] + row->used[area];
23999 struct glyph *last;
24000 int first_x, start_x, x;
24001
24002 if (area == TEXT_AREA && row->fill_line_p)
24003 /* If row extends face to end of line write the whole line. */
24004 draw_glyphs (w, 0, row, area,
24005 0, row->used[area],
24006 DRAW_NORMAL_TEXT, 0);
24007 else
24008 {
24009 /* Set START_X to the window-relative start position for drawing glyphs of
24010 AREA. The first glyph of the text area can be partially visible.
24011 The first glyphs of other areas cannot. */
24012 start_x = window_box_left_offset (w, area);
24013 x = start_x;
24014 if (area == TEXT_AREA)
24015 x += row->x;
24016
24017 /* Find the first glyph that must be redrawn. */
24018 while (first < end
24019 && x + first->pixel_width < r->x)
24020 {
24021 x += first->pixel_width;
24022 ++first;
24023 }
24024
24025 /* Find the last one. */
24026 last = first;
24027 first_x = x;
24028 while (last < end
24029 && x < r->x + r->width)
24030 {
24031 x += last->pixel_width;
24032 ++last;
24033 }
24034
24035 /* Repaint. */
24036 if (last > first)
24037 draw_glyphs (w, first_x - start_x, row, area,
24038 first - row->glyphs[area], last - row->glyphs[area],
24039 DRAW_NORMAL_TEXT, 0);
24040 }
24041 }
24042
24043
24044 /* Redraw the parts of the glyph row ROW on window W intersecting
24045 rectangle R. R is in window-relative coordinates. Value is
24046 non-zero if mouse-face was overwritten. */
24047
24048 static int
24049 expose_line (w, row, r)
24050 struct window *w;
24051 struct glyph_row *row;
24052 XRectangle *r;
24053 {
24054 xassert (row->enabled_p);
24055
24056 if (row->mode_line_p || w->pseudo_window_p)
24057 draw_glyphs (w, 0, row, TEXT_AREA,
24058 0, row->used[TEXT_AREA],
24059 DRAW_NORMAL_TEXT, 0);
24060 else
24061 {
24062 if (row->used[LEFT_MARGIN_AREA])
24063 expose_area (w, row, r, LEFT_MARGIN_AREA);
24064 if (row->used[TEXT_AREA])
24065 expose_area (w, row, r, TEXT_AREA);
24066 if (row->used[RIGHT_MARGIN_AREA])
24067 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24068 draw_row_fringe_bitmaps (w, row);
24069 }
24070
24071 return row->mouse_face_p;
24072 }
24073
24074
24075 /* Redraw those parts of glyphs rows during expose event handling that
24076 overlap other rows. Redrawing of an exposed line writes over parts
24077 of lines overlapping that exposed line; this function fixes that.
24078
24079 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24080 row in W's current matrix that is exposed and overlaps other rows.
24081 LAST_OVERLAPPING_ROW is the last such row. */
24082
24083 static void
24084 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24085 struct window *w;
24086 struct glyph_row *first_overlapping_row;
24087 struct glyph_row *last_overlapping_row;
24088 XRectangle *r;
24089 {
24090 struct glyph_row *row;
24091
24092 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24093 if (row->overlapping_p)
24094 {
24095 xassert (row->enabled_p && !row->mode_line_p);
24096
24097 row->clip = r;
24098 if (row->used[LEFT_MARGIN_AREA])
24099 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24100
24101 if (row->used[TEXT_AREA])
24102 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24103
24104 if (row->used[RIGHT_MARGIN_AREA])
24105 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24106 row->clip = NULL;
24107 }
24108 }
24109
24110
24111 /* Return non-zero if W's cursor intersects rectangle R. */
24112
24113 static int
24114 phys_cursor_in_rect_p (w, r)
24115 struct window *w;
24116 XRectangle *r;
24117 {
24118 XRectangle cr, result;
24119 struct glyph *cursor_glyph;
24120 struct glyph_row *row;
24121
24122 if (w->phys_cursor.vpos >= 0
24123 && w->phys_cursor.vpos < w->current_matrix->nrows
24124 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24125 row->enabled_p)
24126 && row->cursor_in_fringe_p)
24127 {
24128 /* Cursor is in the fringe. */
24129 cr.x = window_box_right_offset (w,
24130 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24131 ? RIGHT_MARGIN_AREA
24132 : TEXT_AREA));
24133 cr.y = row->y;
24134 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24135 cr.height = row->height;
24136 return x_intersect_rectangles (&cr, r, &result);
24137 }
24138
24139 cursor_glyph = get_phys_cursor_glyph (w);
24140 if (cursor_glyph)
24141 {
24142 /* r is relative to W's box, but w->phys_cursor.x is relative
24143 to left edge of W's TEXT area. Adjust it. */
24144 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24145 cr.y = w->phys_cursor.y;
24146 cr.width = cursor_glyph->pixel_width;
24147 cr.height = w->phys_cursor_height;
24148 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24149 I assume the effect is the same -- and this is portable. */
24150 return x_intersect_rectangles (&cr, r, &result);
24151 }
24152 /* If we don't understand the format, pretend we're not in the hot-spot. */
24153 return 0;
24154 }
24155
24156
24157 /* EXPORT:
24158 Draw a vertical window border to the right of window W if W doesn't
24159 have vertical scroll bars. */
24160
24161 void
24162 x_draw_vertical_border (w)
24163 struct window *w;
24164 {
24165 struct frame *f = XFRAME (WINDOW_FRAME (w));
24166
24167 /* We could do better, if we knew what type of scroll-bar the adjacent
24168 windows (on either side) have... But we don't :-(
24169 However, I think this works ok. ++KFS 2003-04-25 */
24170
24171 /* Redraw borders between horizontally adjacent windows. Don't
24172 do it for frames with vertical scroll bars because either the
24173 right scroll bar of a window, or the left scroll bar of its
24174 neighbor will suffice as a border. */
24175 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24176 return;
24177
24178 if (!WINDOW_RIGHTMOST_P (w)
24179 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24180 {
24181 int x0, x1, y0, y1;
24182
24183 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24184 y1 -= 1;
24185
24186 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24187 x1 -= 1;
24188
24189 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24190 }
24191 else if (!WINDOW_LEFTMOST_P (w)
24192 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24193 {
24194 int x0, x1, y0, y1;
24195
24196 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24197 y1 -= 1;
24198
24199 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24200 x0 -= 1;
24201
24202 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24203 }
24204 }
24205
24206
24207 /* Redraw the part of window W intersection rectangle FR. Pixel
24208 coordinates in FR are frame-relative. Call this function with
24209 input blocked. Value is non-zero if the exposure overwrites
24210 mouse-face. */
24211
24212 static int
24213 expose_window (w, fr)
24214 struct window *w;
24215 XRectangle *fr;
24216 {
24217 struct frame *f = XFRAME (w->frame);
24218 XRectangle wr, r;
24219 int mouse_face_overwritten_p = 0;
24220
24221 /* If window is not yet fully initialized, do nothing. This can
24222 happen when toolkit scroll bars are used and a window is split.
24223 Reconfiguring the scroll bar will generate an expose for a newly
24224 created window. */
24225 if (w->current_matrix == NULL)
24226 return 0;
24227
24228 /* When we're currently updating the window, display and current
24229 matrix usually don't agree. Arrange for a thorough display
24230 later. */
24231 if (w == updated_window)
24232 {
24233 SET_FRAME_GARBAGED (f);
24234 return 0;
24235 }
24236
24237 /* Frame-relative pixel rectangle of W. */
24238 wr.x = WINDOW_LEFT_EDGE_X (w);
24239 wr.y = WINDOW_TOP_EDGE_Y (w);
24240 wr.width = WINDOW_TOTAL_WIDTH (w);
24241 wr.height = WINDOW_TOTAL_HEIGHT (w);
24242
24243 if (x_intersect_rectangles (fr, &wr, &r))
24244 {
24245 int yb = window_text_bottom_y (w);
24246 struct glyph_row *row;
24247 int cursor_cleared_p;
24248 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24249
24250 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24251 r.x, r.y, r.width, r.height));
24252
24253 /* Convert to window coordinates. */
24254 r.x -= WINDOW_LEFT_EDGE_X (w);
24255 r.y -= WINDOW_TOP_EDGE_Y (w);
24256
24257 /* Turn off the cursor. */
24258 if (!w->pseudo_window_p
24259 && phys_cursor_in_rect_p (w, &r))
24260 {
24261 x_clear_cursor (w);
24262 cursor_cleared_p = 1;
24263 }
24264 else
24265 cursor_cleared_p = 0;
24266
24267 /* Update lines intersecting rectangle R. */
24268 first_overlapping_row = last_overlapping_row = NULL;
24269 for (row = w->current_matrix->rows;
24270 row->enabled_p;
24271 ++row)
24272 {
24273 int y0 = row->y;
24274 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24275
24276 if ((y0 >= r.y && y0 < r.y + r.height)
24277 || (y1 > r.y && y1 < r.y + r.height)
24278 || (r.y >= y0 && r.y < y1)
24279 || (r.y + r.height > y0 && r.y + r.height < y1))
24280 {
24281 /* A header line may be overlapping, but there is no need
24282 to fix overlapping areas for them. KFS 2005-02-12 */
24283 if (row->overlapping_p && !row->mode_line_p)
24284 {
24285 if (first_overlapping_row == NULL)
24286 first_overlapping_row = row;
24287 last_overlapping_row = row;
24288 }
24289
24290 row->clip = fr;
24291 if (expose_line (w, row, &r))
24292 mouse_face_overwritten_p = 1;
24293 row->clip = NULL;
24294 }
24295 else if (row->overlapping_p)
24296 {
24297 /* We must redraw a row overlapping the exposed area. */
24298 if (y0 < r.y
24299 ? y0 + row->phys_height > r.y
24300 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24301 {
24302 if (first_overlapping_row == NULL)
24303 first_overlapping_row = row;
24304 last_overlapping_row = row;
24305 }
24306 }
24307
24308 if (y1 >= yb)
24309 break;
24310 }
24311
24312 /* Display the mode line if there is one. */
24313 if (WINDOW_WANTS_MODELINE_P (w)
24314 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24315 row->enabled_p)
24316 && row->y < r.y + r.height)
24317 {
24318 if (expose_line (w, row, &r))
24319 mouse_face_overwritten_p = 1;
24320 }
24321
24322 if (!w->pseudo_window_p)
24323 {
24324 /* Fix the display of overlapping rows. */
24325 if (first_overlapping_row)
24326 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24327 fr);
24328
24329 /* Draw border between windows. */
24330 x_draw_vertical_border (w);
24331
24332 /* Turn the cursor on again. */
24333 if (cursor_cleared_p)
24334 update_window_cursor (w, 1);
24335 }
24336 }
24337
24338 return mouse_face_overwritten_p;
24339 }
24340
24341
24342
24343 /* Redraw (parts) of all windows in the window tree rooted at W that
24344 intersect R. R contains frame pixel coordinates. Value is
24345 non-zero if the exposure overwrites mouse-face. */
24346
24347 static int
24348 expose_window_tree (w, r)
24349 struct window *w;
24350 XRectangle *r;
24351 {
24352 struct frame *f = XFRAME (w->frame);
24353 int mouse_face_overwritten_p = 0;
24354
24355 while (w && !FRAME_GARBAGED_P (f))
24356 {
24357 if (!NILP (w->hchild))
24358 mouse_face_overwritten_p
24359 |= expose_window_tree (XWINDOW (w->hchild), r);
24360 else if (!NILP (w->vchild))
24361 mouse_face_overwritten_p
24362 |= expose_window_tree (XWINDOW (w->vchild), r);
24363 else
24364 mouse_face_overwritten_p |= expose_window (w, r);
24365
24366 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24367 }
24368
24369 return mouse_face_overwritten_p;
24370 }
24371
24372
24373 /* EXPORT:
24374 Redisplay an exposed area of frame F. X and Y are the upper-left
24375 corner of the exposed rectangle. W and H are width and height of
24376 the exposed area. All are pixel values. W or H zero means redraw
24377 the entire frame. */
24378
24379 void
24380 expose_frame (f, x, y, w, h)
24381 struct frame *f;
24382 int x, y, w, h;
24383 {
24384 XRectangle r;
24385 int mouse_face_overwritten_p = 0;
24386
24387 TRACE ((stderr, "expose_frame "));
24388
24389 /* No need to redraw if frame will be redrawn soon. */
24390 if (FRAME_GARBAGED_P (f))
24391 {
24392 TRACE ((stderr, " garbaged\n"));
24393 return;
24394 }
24395
24396 /* If basic faces haven't been realized yet, there is no point in
24397 trying to redraw anything. This can happen when we get an expose
24398 event while Emacs is starting, e.g. by moving another window. */
24399 if (FRAME_FACE_CACHE (f) == NULL
24400 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24401 {
24402 TRACE ((stderr, " no faces\n"));
24403 return;
24404 }
24405
24406 if (w == 0 || h == 0)
24407 {
24408 r.x = r.y = 0;
24409 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24410 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24411 }
24412 else
24413 {
24414 r.x = x;
24415 r.y = y;
24416 r.width = w;
24417 r.height = h;
24418 }
24419
24420 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24421 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24422
24423 if (WINDOWP (f->tool_bar_window))
24424 mouse_face_overwritten_p
24425 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24426
24427 #ifdef HAVE_X_WINDOWS
24428 #ifndef MSDOS
24429 #ifndef USE_X_TOOLKIT
24430 if (WINDOWP (f->menu_bar_window))
24431 mouse_face_overwritten_p
24432 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24433 #endif /* not USE_X_TOOLKIT */
24434 #endif
24435 #endif
24436
24437 /* Some window managers support a focus-follows-mouse style with
24438 delayed raising of frames. Imagine a partially obscured frame,
24439 and moving the mouse into partially obscured mouse-face on that
24440 frame. The visible part of the mouse-face will be highlighted,
24441 then the WM raises the obscured frame. With at least one WM, KDE
24442 2.1, Emacs is not getting any event for the raising of the frame
24443 (even tried with SubstructureRedirectMask), only Expose events.
24444 These expose events will draw text normally, i.e. not
24445 highlighted. Which means we must redo the highlight here.
24446 Subsume it under ``we love X''. --gerd 2001-08-15 */
24447 /* Included in Windows version because Windows most likely does not
24448 do the right thing if any third party tool offers
24449 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24450 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24451 {
24452 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24453 if (f == dpyinfo->mouse_face_mouse_frame)
24454 {
24455 int x = dpyinfo->mouse_face_mouse_x;
24456 int y = dpyinfo->mouse_face_mouse_y;
24457 clear_mouse_face (dpyinfo);
24458 note_mouse_highlight (f, x, y);
24459 }
24460 }
24461 }
24462
24463
24464 /* EXPORT:
24465 Determine the intersection of two rectangles R1 and R2. Return
24466 the intersection in *RESULT. Value is non-zero if RESULT is not
24467 empty. */
24468
24469 int
24470 x_intersect_rectangles (r1, r2, result)
24471 XRectangle *r1, *r2, *result;
24472 {
24473 XRectangle *left, *right;
24474 XRectangle *upper, *lower;
24475 int intersection_p = 0;
24476
24477 /* Rearrange so that R1 is the left-most rectangle. */
24478 if (r1->x < r2->x)
24479 left = r1, right = r2;
24480 else
24481 left = r2, right = r1;
24482
24483 /* X0 of the intersection is right.x0, if this is inside R1,
24484 otherwise there is no intersection. */
24485 if (right->x <= left->x + left->width)
24486 {
24487 result->x = right->x;
24488
24489 /* The right end of the intersection is the minimum of the
24490 the right ends of left and right. */
24491 result->width = (min (left->x + left->width, right->x + right->width)
24492 - result->x);
24493
24494 /* Same game for Y. */
24495 if (r1->y < r2->y)
24496 upper = r1, lower = r2;
24497 else
24498 upper = r2, lower = r1;
24499
24500 /* The upper end of the intersection is lower.y0, if this is inside
24501 of upper. Otherwise, there is no intersection. */
24502 if (lower->y <= upper->y + upper->height)
24503 {
24504 result->y = lower->y;
24505
24506 /* The lower end of the intersection is the minimum of the lower
24507 ends of upper and lower. */
24508 result->height = (min (lower->y + lower->height,
24509 upper->y + upper->height)
24510 - result->y);
24511 intersection_p = 1;
24512 }
24513 }
24514
24515 return intersection_p;
24516 }
24517
24518 #endif /* HAVE_WINDOW_SYSTEM */
24519
24520 \f
24521 /***********************************************************************
24522 Initialization
24523 ***********************************************************************/
24524
24525 void
24526 syms_of_xdisp ()
24527 {
24528 Vwith_echo_area_save_vector = Qnil;
24529 staticpro (&Vwith_echo_area_save_vector);
24530
24531 Vmessage_stack = Qnil;
24532 staticpro (&Vmessage_stack);
24533
24534 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24535 staticpro (&Qinhibit_redisplay);
24536
24537 message_dolog_marker1 = Fmake_marker ();
24538 staticpro (&message_dolog_marker1);
24539 message_dolog_marker2 = Fmake_marker ();
24540 staticpro (&message_dolog_marker2);
24541 message_dolog_marker3 = Fmake_marker ();
24542 staticpro (&message_dolog_marker3);
24543
24544 #if GLYPH_DEBUG
24545 defsubr (&Sdump_frame_glyph_matrix);
24546 defsubr (&Sdump_glyph_matrix);
24547 defsubr (&Sdump_glyph_row);
24548 defsubr (&Sdump_tool_bar_row);
24549 defsubr (&Strace_redisplay);
24550 defsubr (&Strace_to_stderr);
24551 #endif
24552 #ifdef HAVE_WINDOW_SYSTEM
24553 defsubr (&Stool_bar_lines_needed);
24554 defsubr (&Slookup_image_map);
24555 #endif
24556 defsubr (&Sformat_mode_line);
24557 defsubr (&Sinvisible_p);
24558
24559 staticpro (&Qmenu_bar_update_hook);
24560 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24561
24562 staticpro (&Qoverriding_terminal_local_map);
24563 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24564
24565 staticpro (&Qoverriding_local_map);
24566 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24567
24568 staticpro (&Qwindow_scroll_functions);
24569 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24570
24571 staticpro (&Qwindow_text_change_functions);
24572 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24573
24574 staticpro (&Qredisplay_end_trigger_functions);
24575 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24576
24577 staticpro (&Qinhibit_point_motion_hooks);
24578 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24579
24580 Qeval = intern_c_string ("eval");
24581 staticpro (&Qeval);
24582
24583 QCdata = intern_c_string (":data");
24584 staticpro (&QCdata);
24585 Qdisplay = intern_c_string ("display");
24586 staticpro (&Qdisplay);
24587 Qspace_width = intern_c_string ("space-width");
24588 staticpro (&Qspace_width);
24589 Qraise = intern_c_string ("raise");
24590 staticpro (&Qraise);
24591 Qslice = intern_c_string ("slice");
24592 staticpro (&Qslice);
24593 Qspace = intern_c_string ("space");
24594 staticpro (&Qspace);
24595 Qmargin = intern_c_string ("margin");
24596 staticpro (&Qmargin);
24597 Qpointer = intern_c_string ("pointer");
24598 staticpro (&Qpointer);
24599 Qleft_margin = intern_c_string ("left-margin");
24600 staticpro (&Qleft_margin);
24601 Qright_margin = intern_c_string ("right-margin");
24602 staticpro (&Qright_margin);
24603 Qcenter = intern_c_string ("center");
24604 staticpro (&Qcenter);
24605 Qline_height = intern_c_string ("line-height");
24606 staticpro (&Qline_height);
24607 QCalign_to = intern_c_string (":align-to");
24608 staticpro (&QCalign_to);
24609 QCrelative_width = intern_c_string (":relative-width");
24610 staticpro (&QCrelative_width);
24611 QCrelative_height = intern_c_string (":relative-height");
24612 staticpro (&QCrelative_height);
24613 QCeval = intern_c_string (":eval");
24614 staticpro (&QCeval);
24615 QCpropertize = intern_c_string (":propertize");
24616 staticpro (&QCpropertize);
24617 QCfile = intern_c_string (":file");
24618 staticpro (&QCfile);
24619 Qfontified = intern_c_string ("fontified");
24620 staticpro (&Qfontified);
24621 Qfontification_functions = intern_c_string ("fontification-functions");
24622 staticpro (&Qfontification_functions);
24623 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24624 staticpro (&Qtrailing_whitespace);
24625 Qescape_glyph = intern_c_string ("escape-glyph");
24626 staticpro (&Qescape_glyph);
24627 Qnobreak_space = intern_c_string ("nobreak-space");
24628 staticpro (&Qnobreak_space);
24629 Qimage = intern_c_string ("image");
24630 staticpro (&Qimage);
24631 QCmap = intern_c_string (":map");
24632 staticpro (&QCmap);
24633 QCpointer = intern_c_string (":pointer");
24634 staticpro (&QCpointer);
24635 Qrect = intern_c_string ("rect");
24636 staticpro (&Qrect);
24637 Qcircle = intern_c_string ("circle");
24638 staticpro (&Qcircle);
24639 Qpoly = intern_c_string ("poly");
24640 staticpro (&Qpoly);
24641 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24642 staticpro (&Qmessage_truncate_lines);
24643 Qgrow_only = intern_c_string ("grow-only");
24644 staticpro (&Qgrow_only);
24645 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24646 staticpro (&Qinhibit_menubar_update);
24647 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24648 staticpro (&Qinhibit_eval_during_redisplay);
24649 Qposition = intern_c_string ("position");
24650 staticpro (&Qposition);
24651 Qbuffer_position = intern_c_string ("buffer-position");
24652 staticpro (&Qbuffer_position);
24653 Qobject = intern_c_string ("object");
24654 staticpro (&Qobject);
24655 Qbar = intern_c_string ("bar");
24656 staticpro (&Qbar);
24657 Qhbar = intern_c_string ("hbar");
24658 staticpro (&Qhbar);
24659 Qbox = intern_c_string ("box");
24660 staticpro (&Qbox);
24661 Qhollow = intern_c_string ("hollow");
24662 staticpro (&Qhollow);
24663 Qhand = intern_c_string ("hand");
24664 staticpro (&Qhand);
24665 Qarrow = intern_c_string ("arrow");
24666 staticpro (&Qarrow);
24667 Qtext = intern_c_string ("text");
24668 staticpro (&Qtext);
24669 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24670 staticpro (&Qrisky_local_variable);
24671 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24672 staticpro (&Qinhibit_free_realized_faces);
24673
24674 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24675 Fcons (intern_c_string ("void-variable"), Qnil)),
24676 Qnil);
24677 staticpro (&list_of_error);
24678
24679 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24680 staticpro (&Qlast_arrow_position);
24681 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24682 staticpro (&Qlast_arrow_string);
24683
24684 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24685 staticpro (&Qoverlay_arrow_string);
24686 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24687 staticpro (&Qoverlay_arrow_bitmap);
24688
24689 echo_buffer[0] = echo_buffer[1] = Qnil;
24690 staticpro (&echo_buffer[0]);
24691 staticpro (&echo_buffer[1]);
24692
24693 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24694 staticpro (&echo_area_buffer[0]);
24695 staticpro (&echo_area_buffer[1]);
24696
24697 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24698 staticpro (&Vmessages_buffer_name);
24699
24700 mode_line_proptrans_alist = Qnil;
24701 staticpro (&mode_line_proptrans_alist);
24702 mode_line_string_list = Qnil;
24703 staticpro (&mode_line_string_list);
24704 mode_line_string_face = Qnil;
24705 staticpro (&mode_line_string_face);
24706 mode_line_string_face_prop = Qnil;
24707 staticpro (&mode_line_string_face_prop);
24708 Vmode_line_unwind_vector = Qnil;
24709 staticpro (&Vmode_line_unwind_vector);
24710
24711 help_echo_string = Qnil;
24712 staticpro (&help_echo_string);
24713 help_echo_object = Qnil;
24714 staticpro (&help_echo_object);
24715 help_echo_window = Qnil;
24716 staticpro (&help_echo_window);
24717 previous_help_echo_string = Qnil;
24718 staticpro (&previous_help_echo_string);
24719 help_echo_pos = -1;
24720
24721 #ifdef HAVE_WINDOW_SYSTEM
24722 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24723 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24724 For example, if a block cursor is over a tab, it will be drawn as
24725 wide as that tab on the display. */);
24726 x_stretch_cursor_p = 0;
24727 #endif
24728
24729 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24730 doc: /* *Non-nil means highlight trailing whitespace.
24731 The face used for trailing whitespace is `trailing-whitespace'. */);
24732 Vshow_trailing_whitespace = Qnil;
24733
24734 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24735 doc: /* *Control highlighting of nobreak space and soft hyphen.
24736 A value of t means highlight the character itself (for nobreak space,
24737 use face `nobreak-space').
24738 A value of nil means no highlighting.
24739 Other values mean display the escape glyph followed by an ordinary
24740 space or ordinary hyphen. */);
24741 Vnobreak_char_display = Qt;
24742
24743 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24744 doc: /* *The pointer shape to show in void text areas.
24745 A value of nil means to show the text pointer. Other options are `arrow',
24746 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24747 Vvoid_text_area_pointer = Qarrow;
24748
24749 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24750 doc: /* Non-nil means don't actually do any redisplay.
24751 This is used for internal purposes. */);
24752 Vinhibit_redisplay = Qnil;
24753
24754 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24755 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24756 Vglobal_mode_string = Qnil;
24757
24758 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24759 doc: /* Marker for where to display an arrow on top of the buffer text.
24760 This must be the beginning of a line in order to work.
24761 See also `overlay-arrow-string'. */);
24762 Voverlay_arrow_position = Qnil;
24763
24764 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24765 doc: /* String to display as an arrow in non-window frames.
24766 See also `overlay-arrow-position'. */);
24767 Voverlay_arrow_string = make_pure_c_string ("=>");
24768
24769 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24770 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24771 The symbols on this list are examined during redisplay to determine
24772 where to display overlay arrows. */);
24773 Voverlay_arrow_variable_list
24774 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24775
24776 DEFVAR_INT ("scroll-step", &scroll_step,
24777 doc: /* *The number of lines to try scrolling a window by when point moves out.
24778 If that fails to bring point back on frame, point is centered instead.
24779 If this is zero, point is always centered after it moves off frame.
24780 If you want scrolling to always be a line at a time, you should set
24781 `scroll-conservatively' to a large value rather than set this to 1. */);
24782
24783 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24784 doc: /* *Scroll up to this many lines, to bring point back on screen.
24785 If point moves off-screen, redisplay will scroll by up to
24786 `scroll-conservatively' lines in order to bring point just barely
24787 onto the screen again. If that cannot be done, then redisplay
24788 recenters point as usual.
24789
24790 A value of zero means always recenter point if it moves off screen. */);
24791 scroll_conservatively = 0;
24792
24793 DEFVAR_INT ("scroll-margin", &scroll_margin,
24794 doc: /* *Number of lines of margin at the top and bottom of a window.
24795 Recenter the window whenever point gets within this many lines
24796 of the top or bottom of the window. */);
24797 scroll_margin = 0;
24798
24799 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24800 doc: /* Pixels per inch value for non-window system displays.
24801 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24802 Vdisplay_pixels_per_inch = make_float (72.0);
24803
24804 #if GLYPH_DEBUG
24805 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24806 #endif
24807
24808 DEFVAR_LISP ("truncate-partial-width-windows",
24809 &Vtruncate_partial_width_windows,
24810 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24811 For an integer value, truncate lines in each window narrower than the
24812 full frame width, provided the window width is less than that integer;
24813 otherwise, respect the value of `truncate-lines'.
24814
24815 For any other non-nil value, truncate lines in all windows that do
24816 not span the full frame width.
24817
24818 A value of nil means to respect the value of `truncate-lines'.
24819
24820 If `word-wrap' is enabled, you might want to reduce this. */);
24821 Vtruncate_partial_width_windows = make_number (50);
24822
24823 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24824 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24825 Any other value means to use the appropriate face, `mode-line',
24826 `header-line', or `menu' respectively. */);
24827 mode_line_inverse_video = 1;
24828
24829 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24830 doc: /* *Maximum buffer size for which line number should be displayed.
24831 If the buffer is bigger than this, the line number does not appear
24832 in the mode line. A value of nil means no limit. */);
24833 Vline_number_display_limit = Qnil;
24834
24835 DEFVAR_INT ("line-number-display-limit-width",
24836 &line_number_display_limit_width,
24837 doc: /* *Maximum line width (in characters) for line number display.
24838 If the average length of the lines near point is bigger than this, then the
24839 line number may be omitted from the mode line. */);
24840 line_number_display_limit_width = 200;
24841
24842 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24843 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24844 highlight_nonselected_windows = 0;
24845
24846 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24847 doc: /* Non-nil if more than one frame is visible on this display.
24848 Minibuffer-only frames don't count, but iconified frames do.
24849 This variable is not guaranteed to be accurate except while processing
24850 `frame-title-format' and `icon-title-format'. */);
24851
24852 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24853 doc: /* Template for displaying the title bar of visible frames.
24854 \(Assuming the window manager supports this feature.)
24855
24856 This variable has the same structure as `mode-line-format', except that
24857 the %c and %l constructs are ignored. It is used only on frames for
24858 which no explicit name has been set \(see `modify-frame-parameters'). */);
24859
24860 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24861 doc: /* Template for displaying the title bar of an iconified frame.
24862 \(Assuming the window manager supports this feature.)
24863 This variable has the same structure as `mode-line-format' (which see),
24864 and is used only on frames for which no explicit name has been set
24865 \(see `modify-frame-parameters'). */);
24866 Vicon_title_format
24867 = Vframe_title_format
24868 = pure_cons (intern_c_string ("multiple-frames"),
24869 pure_cons (make_pure_c_string ("%b"),
24870 pure_cons (pure_cons (empty_unibyte_string,
24871 pure_cons (intern_c_string ("invocation-name"),
24872 pure_cons (make_pure_c_string ("@"),
24873 pure_cons (intern_c_string ("system-name"),
24874 Qnil)))),
24875 Qnil)));
24876
24877 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24878 doc: /* Maximum number of lines to keep in the message log buffer.
24879 If nil, disable message logging. If t, log messages but don't truncate
24880 the buffer when it becomes large. */);
24881 Vmessage_log_max = make_number (100);
24882
24883 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24884 doc: /* Functions called before redisplay, if window sizes have changed.
24885 The value should be a list of functions that take one argument.
24886 Just before redisplay, for each frame, if any of its windows have changed
24887 size since the last redisplay, or have been split or deleted,
24888 all the functions in the list are called, with the frame as argument. */);
24889 Vwindow_size_change_functions = Qnil;
24890
24891 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24892 doc: /* List of functions to call before redisplaying a window with scrolling.
24893 Each function is called with two arguments, the window and its new
24894 display-start position. Note that these functions are also called by
24895 `set-window-buffer'. Also note that the value of `window-end' is not
24896 valid when these functions are called. */);
24897 Vwindow_scroll_functions = Qnil;
24898
24899 DEFVAR_LISP ("window-text-change-functions",
24900 &Vwindow_text_change_functions,
24901 doc: /* Functions to call in redisplay when text in the window might change. */);
24902 Vwindow_text_change_functions = Qnil;
24903
24904 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24905 doc: /* Functions called when redisplay of a window reaches the end trigger.
24906 Each function is called with two arguments, the window and the end trigger value.
24907 See `set-window-redisplay-end-trigger'. */);
24908 Vredisplay_end_trigger_functions = Qnil;
24909
24910 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24911 doc: /* *Non-nil means autoselect window with mouse pointer.
24912 If nil, do not autoselect windows.
24913 A positive number means delay autoselection by that many seconds: a
24914 window is autoselected only after the mouse has remained in that
24915 window for the duration of the delay.
24916 A negative number has a similar effect, but causes windows to be
24917 autoselected only after the mouse has stopped moving. \(Because of
24918 the way Emacs compares mouse events, you will occasionally wait twice
24919 that time before the window gets selected.\)
24920 Any other value means to autoselect window instantaneously when the
24921 mouse pointer enters it.
24922
24923 Autoselection selects the minibuffer only if it is active, and never
24924 unselects the minibuffer if it is active.
24925
24926 When customizing this variable make sure that the actual value of
24927 `focus-follows-mouse' matches the behavior of your window manager. */);
24928 Vmouse_autoselect_window = Qnil;
24929
24930 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24931 doc: /* *Non-nil means automatically resize tool-bars.
24932 This dynamically changes the tool-bar's height to the minimum height
24933 that is needed to make all tool-bar items visible.
24934 If value is `grow-only', the tool-bar's height is only increased
24935 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24936 Vauto_resize_tool_bars = Qt;
24937
24938 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24939 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24940 auto_raise_tool_bar_buttons_p = 1;
24941
24942 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24943 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24944 make_cursor_line_fully_visible_p = 1;
24945
24946 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24947 doc: /* *Border below tool-bar in pixels.
24948 If an integer, use it as the height of the border.
24949 If it is one of `internal-border-width' or `border-width', use the
24950 value of the corresponding frame parameter.
24951 Otherwise, no border is added below the tool-bar. */);
24952 Vtool_bar_border = Qinternal_border_width;
24953
24954 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24955 doc: /* *Margin around tool-bar buttons in pixels.
24956 If an integer, use that for both horizontal and vertical margins.
24957 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24958 HORZ specifying the horizontal margin, and VERT specifying the
24959 vertical margin. */);
24960 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24961
24962 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24963 doc: /* *Relief thickness of tool-bar buttons. */);
24964 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24965
24966 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24967 doc: /* List of functions to call to fontify regions of text.
24968 Each function is called with one argument POS. Functions must
24969 fontify a region starting at POS in the current buffer, and give
24970 fontified regions the property `fontified'. */);
24971 Vfontification_functions = Qnil;
24972 Fmake_variable_buffer_local (Qfontification_functions);
24973
24974 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24975 &unibyte_display_via_language_environment,
24976 doc: /* *Non-nil means display unibyte text according to language environment.
24977 Specifically, this means that raw bytes in the range 160-255 decimal
24978 are displayed by converting them to the equivalent multibyte characters
24979 according to the current language environment. As a result, they are
24980 displayed according to the current fontset.
24981
24982 Note that this variable affects only how these bytes are displayed,
24983 but does not change the fact they are interpreted as raw bytes. */);
24984 unibyte_display_via_language_environment = 0;
24985
24986 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24987 doc: /* *Maximum height for resizing mini-windows.
24988 If a float, it specifies a fraction of the mini-window frame's height.
24989 If an integer, it specifies a number of lines. */);
24990 Vmax_mini_window_height = make_float (0.25);
24991
24992 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24993 doc: /* *How to resize mini-windows.
24994 A value of nil means don't automatically resize mini-windows.
24995 A value of t means resize them to fit the text displayed in them.
24996 A value of `grow-only', the default, means let mini-windows grow
24997 only, until their display becomes empty, at which point the windows
24998 go back to their normal size. */);
24999 Vresize_mini_windows = Qgrow_only;
25000
25001 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25002 doc: /* Alist specifying how to blink the cursor off.
25003 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25004 `cursor-type' frame-parameter or variable equals ON-STATE,
25005 comparing using `equal', Emacs uses OFF-STATE to specify
25006 how to blink it off. ON-STATE and OFF-STATE are values for
25007 the `cursor-type' frame parameter.
25008
25009 If a frame's ON-STATE has no entry in this list,
25010 the frame's other specifications determine how to blink the cursor off. */);
25011 Vblink_cursor_alist = Qnil;
25012
25013 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25014 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25015 automatic_hscrolling_p = 1;
25016 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
25017 staticpro (&Qauto_hscroll_mode);
25018
25019 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25020 doc: /* *How many columns away from the window edge point is allowed to get
25021 before automatic hscrolling will horizontally scroll the window. */);
25022 hscroll_margin = 5;
25023
25024 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25025 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25026 When point is less than `hscroll-margin' columns from the window
25027 edge, automatic hscrolling will scroll the window by the amount of columns
25028 determined by this variable. If its value is a positive integer, scroll that
25029 many columns. If it's a positive floating-point number, it specifies the
25030 fraction of the window's width to scroll. If it's nil or zero, point will be
25031 centered horizontally after the scroll. Any other value, including negative
25032 numbers, are treated as if the value were zero.
25033
25034 Automatic hscrolling always moves point outside the scroll margin, so if
25035 point was more than scroll step columns inside the margin, the window will
25036 scroll more than the value given by the scroll step.
25037
25038 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25039 and `scroll-right' overrides this variable's effect. */);
25040 Vhscroll_step = make_number (0);
25041
25042 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25043 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25044 Bind this around calls to `message' to let it take effect. */);
25045 message_truncate_lines = 0;
25046
25047 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25048 doc: /* Normal hook run to update the menu bar definitions.
25049 Redisplay runs this hook before it redisplays the menu bar.
25050 This is used to update submenus such as Buffers,
25051 whose contents depend on various data. */);
25052 Vmenu_bar_update_hook = Qnil;
25053
25054 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25055 doc: /* Frame for which we are updating a menu.
25056 The enable predicate for a menu binding should check this variable. */);
25057 Vmenu_updating_frame = Qnil;
25058
25059 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25060 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25061 inhibit_menubar_update = 0;
25062
25063 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25064 doc: /* Prefix prepended to all continuation lines at display time.
25065 The value may be a string, an image, or a stretch-glyph; it is
25066 interpreted in the same way as the value of a `display' text property.
25067
25068 This variable is overridden by any `wrap-prefix' text or overlay
25069 property.
25070
25071 To add a prefix to non-continuation lines, use `line-prefix'. */);
25072 Vwrap_prefix = Qnil;
25073 staticpro (&Qwrap_prefix);
25074 Qwrap_prefix = intern_c_string ("wrap-prefix");
25075 Fmake_variable_buffer_local (Qwrap_prefix);
25076
25077 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25078 doc: /* Prefix prepended to all non-continuation lines at display time.
25079 The value may be a string, an image, or a stretch-glyph; it is
25080 interpreted in the same way as the value of a `display' text property.
25081
25082 This variable is overridden by any `line-prefix' text or overlay
25083 property.
25084
25085 To add a prefix to continuation lines, use `wrap-prefix'. */);
25086 Vline_prefix = Qnil;
25087 staticpro (&Qline_prefix);
25088 Qline_prefix = intern_c_string ("line-prefix");
25089 Fmake_variable_buffer_local (Qline_prefix);
25090
25091 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25092 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25093 inhibit_eval_during_redisplay = 0;
25094
25095 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25096 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25097 inhibit_free_realized_faces = 0;
25098
25099 #if GLYPH_DEBUG
25100 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25101 doc: /* Inhibit try_window_id display optimization. */);
25102 inhibit_try_window_id = 0;
25103
25104 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25105 doc: /* Inhibit try_window_reusing display optimization. */);
25106 inhibit_try_window_reusing = 0;
25107
25108 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25109 doc: /* Inhibit try_cursor_movement display optimization. */);
25110 inhibit_try_cursor_movement = 0;
25111 #endif /* GLYPH_DEBUG */
25112
25113 DEFVAR_INT ("overline-margin", &overline_margin,
25114 doc: /* *Space between overline and text, in pixels.
25115 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25116 margin to the caracter height. */);
25117 overline_margin = 2;
25118
25119 DEFVAR_INT ("underline-minimum-offset",
25120 &underline_minimum_offset,
25121 doc: /* Minimum distance between baseline and underline.
25122 This can improve legibility of underlined text at small font sizes,
25123 particularly when using variable `x-use-underline-position-properties'
25124 with fonts that specify an UNDERLINE_POSITION relatively close to the
25125 baseline. The default value is 1. */);
25126 underline_minimum_offset = 1;
25127
25128 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25129 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25130 display_hourglass_p = 1;
25131
25132 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25133 doc: /* *Seconds to wait before displaying an hourglass pointer.
25134 Value must be an integer or float. */);
25135 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25136
25137 hourglass_atimer = NULL;
25138 hourglass_shown_p = 0;
25139 }
25140
25141
25142 /* Initialize this module when Emacs starts. */
25143
25144 void
25145 init_xdisp ()
25146 {
25147 Lisp_Object root_window;
25148 struct window *mini_w;
25149
25150 current_header_line_height = current_mode_line_height = -1;
25151
25152 CHARPOS (this_line_start_pos) = 0;
25153
25154 mini_w = XWINDOW (minibuf_window);
25155 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25156 echo_area_window = minibuf_window;
25157
25158 if (!noninteractive)
25159 {
25160 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25161 int i;
25162
25163 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25164 set_window_height (root_window,
25165 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25166 0);
25167 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25168 set_window_height (minibuf_window, 1, 0);
25169
25170 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25171 mini_w->total_cols = make_number (FRAME_COLS (f));
25172
25173 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25174 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25175 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25176
25177 /* The default ellipsis glyphs `...'. */
25178 for (i = 0; i < 3; ++i)
25179 default_invis_vector[i] = make_number ('.');
25180 }
25181
25182 {
25183 /* Allocate the buffer for frame titles.
25184 Also used for `format-mode-line'. */
25185 int size = 100;
25186 mode_line_noprop_buf = (char *) xmalloc (size);
25187 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25188 mode_line_noprop_ptr = mode_line_noprop_buf;
25189 mode_line_target = MODE_LINE_DISPLAY;
25190 }
25191
25192 help_echo_showing_p = 0;
25193 }
25194
25195 /* Since w32 does not support atimers, it defines its own implementation of
25196 the following three functions in w32fns.c. */
25197 #ifndef WINDOWSNT
25198
25199 /* Platform-independent portion of hourglass implementation. */
25200
25201 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25202 int
25203 hourglass_started ()
25204 {
25205 return hourglass_shown_p || hourglass_atimer != NULL;
25206 }
25207
25208 /* Cancel a currently active hourglass timer, and start a new one. */
25209 void
25210 start_hourglass ()
25211 {
25212 #if defined (HAVE_WINDOW_SYSTEM)
25213 EMACS_TIME delay;
25214 int secs, usecs = 0;
25215
25216 cancel_hourglass ();
25217
25218 if (INTEGERP (Vhourglass_delay)
25219 && XINT (Vhourglass_delay) > 0)
25220 secs = XFASTINT (Vhourglass_delay);
25221 else if (FLOATP (Vhourglass_delay)
25222 && XFLOAT_DATA (Vhourglass_delay) > 0)
25223 {
25224 Lisp_Object tem;
25225 tem = Ftruncate (Vhourglass_delay, Qnil);
25226 secs = XFASTINT (tem);
25227 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25228 }
25229 else
25230 secs = DEFAULT_HOURGLASS_DELAY;
25231
25232 EMACS_SET_SECS_USECS (delay, secs, usecs);
25233 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25234 show_hourglass, NULL);
25235 #endif
25236 }
25237
25238
25239 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25240 shown. */
25241 void
25242 cancel_hourglass ()
25243 {
25244 #if defined (HAVE_WINDOW_SYSTEM)
25245 if (hourglass_atimer)
25246 {
25247 cancel_atimer (hourglass_atimer);
25248 hourglass_atimer = NULL;
25249 }
25250
25251 if (hourglass_shown_p)
25252 hide_hourglass ();
25253 #endif
25254 }
25255 #endif /* ! WINDOWSNT */
25256
25257 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25258 (do not change this comment) */