]> code.delx.au - gnu-emacs/blob - src/xdisp.c
merge emacs-23
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 This file is part of GNU Emacs.
8
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171 #include <limits.h>
172 #include <setjmp.h>
173
174 #include "lisp.h"
175 #include "keyboard.h"
176 #include "frame.h"
177 #include "window.h"
178 #include "termchar.h"
179 #include "dispextern.h"
180 #include "buffer.h"
181 #include "character.h"
182 #include "charset.h"
183 #include "indent.h"
184 #include "commands.h"
185 #include "keymap.h"
186 #include "macros.h"
187 #include "disptab.h"
188 #include "termhooks.h"
189 #include "intervals.h"
190 #include "coding.h"
191 #include "process.h"
192 #include "region-cache.h"
193 #include "font.h"
194 #include "fontset.h"
195 #include "blockinput.h"
196
197 #ifdef HAVE_X_WINDOWS
198 #include "xterm.h"
199 #endif
200 #ifdef WINDOWSNT
201 #include "w32term.h"
202 #endif
203 #ifdef HAVE_NS
204 #include "nsterm.h"
205 #endif
206 #ifdef USE_GTK
207 #include "gtkutil.h"
208 #endif
209
210 #include "font.h"
211
212 #ifndef FRAME_X_OUTPUT
213 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
214 #endif
215
216 #define INFINITY 10000000
217
218 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
219 || defined(HAVE_NS) || defined (USE_GTK)
220 extern void set_frame_menubar P_ ((struct frame *f, int, int));
221 extern int pending_menu_activation;
222 #endif
223
224 extern int interrupt_input;
225 extern int command_loop_level;
226
227 extern Lisp_Object do_mouse_tracking;
228
229 extern int minibuffer_auto_raise;
230 extern Lisp_Object Vminibuffer_list;
231
232 extern Lisp_Object Qface;
233 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
234
235 extern Lisp_Object Voverriding_local_map;
236 extern Lisp_Object Voverriding_local_map_menu_flag;
237 extern Lisp_Object Qmenu_item;
238 extern Lisp_Object Qwhen;
239 extern Lisp_Object Qhelp_echo;
240 extern Lisp_Object Qbefore_string, Qafter_string;
241
242 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
243 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
244 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
245 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
246 Lisp_Object Qinhibit_point_motion_hooks;
247 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
248 Lisp_Object Qfontified;
249 Lisp_Object Qgrow_only;
250 Lisp_Object Qinhibit_eval_during_redisplay;
251 Lisp_Object Qbuffer_position, Qposition, Qobject;
252
253 /* Cursor shapes */
254 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
255
256 /* Pointer shapes */
257 Lisp_Object Qarrow, Qhand, Qtext;
258
259 Lisp_Object Qrisky_local_variable;
260
261 /* Holds the list (error). */
262 Lisp_Object list_of_error;
263
264 /* Functions called to fontify regions of text. */
265
266 Lisp_Object Vfontification_functions;
267 Lisp_Object Qfontification_functions;
268
269 /* Non-nil means automatically select any window when the mouse
270 cursor moves into it. */
271 Lisp_Object Vmouse_autoselect_window;
272
273 Lisp_Object Vwrap_prefix, Qwrap_prefix;
274 Lisp_Object Vline_prefix, Qline_prefix;
275
276 /* Non-zero means draw tool bar buttons raised when the mouse moves
277 over them. */
278
279 int auto_raise_tool_bar_buttons_p;
280
281 /* Non-zero means to reposition window if cursor line is only partially visible. */
282
283 int make_cursor_line_fully_visible_p;
284
285 /* Margin below tool bar in pixels. 0 or nil means no margin.
286 If value is `internal-border-width' or `border-width',
287 the corresponding frame parameter is used. */
288
289 Lisp_Object Vtool_bar_border;
290
291 /* Margin around tool bar buttons in pixels. */
292
293 Lisp_Object Vtool_bar_button_margin;
294
295 /* Thickness of shadow to draw around tool bar buttons. */
296
297 EMACS_INT tool_bar_button_relief;
298
299 /* Non-nil means automatically resize tool-bars so that all tool-bar
300 items are visible, and no blank lines remain.
301
302 If value is `grow-only', only make tool-bar bigger. */
303
304 Lisp_Object Vauto_resize_tool_bars;
305
306 /* Non-zero means draw block and hollow cursor as wide as the glyph
307 under it. For example, if a block cursor is over a tab, it will be
308 drawn as wide as that tab on the display. */
309
310 int x_stretch_cursor_p;
311
312 /* Non-nil means don't actually do any redisplay. */
313
314 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
315
316 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
317
318 int inhibit_eval_during_redisplay;
319
320 /* Names of text properties relevant for redisplay. */
321
322 Lisp_Object Qdisplay;
323 extern Lisp_Object Qface, Qinvisible, Qwidth;
324
325 /* Symbols used in text property values. */
326
327 Lisp_Object Vdisplay_pixels_per_inch;
328 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
329 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
330 Lisp_Object Qslice;
331 Lisp_Object Qcenter;
332 Lisp_Object Qmargin, Qpointer;
333 Lisp_Object Qline_height;
334 extern Lisp_Object Qheight;
335 extern Lisp_Object QCwidth, QCheight, QCascent;
336 extern Lisp_Object Qscroll_bar;
337 extern Lisp_Object Qcursor;
338
339 /* Non-nil means highlight trailing whitespace. */
340
341 Lisp_Object Vshow_trailing_whitespace;
342
343 /* Non-nil means escape non-break space and hyphens. */
344
345 Lisp_Object Vnobreak_char_display;
346
347 #ifdef HAVE_WINDOW_SYSTEM
348 extern Lisp_Object Voverflow_newline_into_fringe;
349
350 /* Test if overflow newline into fringe. Called with iterator IT
351 at or past right window margin, and with IT->current_x set. */
352
353 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
354 (!NILP (Voverflow_newline_into_fringe) \
355 && FRAME_WINDOW_P (it->f) \
356 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
357 && it->current_x == it->last_visible_x \
358 && it->line_wrap != WORD_WRAP)
359
360 #else /* !HAVE_WINDOW_SYSTEM */
361 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
362 #endif /* HAVE_WINDOW_SYSTEM */
363
364 /* Test if the display element loaded in IT is a space or tab
365 character. This is used to determine word wrapping. */
366
367 #define IT_DISPLAYING_WHITESPACE(it) \
368 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
369
370 /* Non-nil means show the text cursor in void text areas
371 i.e. in blank areas after eol and eob. This used to be
372 the default in 21.3. */
373
374 Lisp_Object Vvoid_text_area_pointer;
375
376 /* Name of the face used to highlight trailing whitespace. */
377
378 Lisp_Object Qtrailing_whitespace;
379
380 /* Name and number of the face used to highlight escape glyphs. */
381
382 Lisp_Object Qescape_glyph;
383
384 /* Name and number of the face used to highlight non-breaking spaces. */
385
386 Lisp_Object Qnobreak_space;
387
388 /* The symbol `image' which is the car of the lists used to represent
389 images in Lisp. */
390
391 Lisp_Object Qimage;
392
393 /* The image map types. */
394 Lisp_Object QCmap, QCpointer;
395 Lisp_Object Qrect, Qcircle, Qpoly;
396
397 /* Non-zero means print newline to stdout before next mini-buffer
398 message. */
399
400 int noninteractive_need_newline;
401
402 /* Non-zero means print newline to message log before next message. */
403
404 static int message_log_need_newline;
405
406 /* Three markers that message_dolog uses.
407 It could allocate them itself, but that causes trouble
408 in handling memory-full errors. */
409 static Lisp_Object message_dolog_marker1;
410 static Lisp_Object message_dolog_marker2;
411 static Lisp_Object message_dolog_marker3;
412 \f
413 /* The buffer position of the first character appearing entirely or
414 partially on the line of the selected window which contains the
415 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
416 redisplay optimization in redisplay_internal. */
417
418 static struct text_pos this_line_start_pos;
419
420 /* Number of characters past the end of the line above, including the
421 terminating newline. */
422
423 static struct text_pos this_line_end_pos;
424
425 /* The vertical positions and the height of this line. */
426
427 static int this_line_vpos;
428 static int this_line_y;
429 static int this_line_pixel_height;
430
431 /* X position at which this display line starts. Usually zero;
432 negative if first character is partially visible. */
433
434 static int this_line_start_x;
435
436 /* Buffer that this_line_.* variables are referring to. */
437
438 static struct buffer *this_line_buffer;
439
440 /* Nonzero means truncate lines in all windows less wide than the
441 frame. */
442
443 Lisp_Object Vtruncate_partial_width_windows;
444
445 /* A flag to control how to display unibyte 8-bit character. */
446
447 int unibyte_display_via_language_environment;
448
449 /* Nonzero means we have more than one non-mini-buffer-only frame.
450 Not guaranteed to be accurate except while parsing
451 frame-title-format. */
452
453 int multiple_frames;
454
455 Lisp_Object Vglobal_mode_string;
456
457
458 /* List of variables (symbols) which hold markers for overlay arrows.
459 The symbols on this list are examined during redisplay to determine
460 where to display overlay arrows. */
461
462 Lisp_Object Voverlay_arrow_variable_list;
463
464 /* Marker for where to display an arrow on top of the buffer text. */
465
466 Lisp_Object Voverlay_arrow_position;
467
468 /* String to display for the arrow. Only used on terminal frames. */
469
470 Lisp_Object Voverlay_arrow_string;
471
472 /* Values of those variables at last redisplay are stored as
473 properties on `overlay-arrow-position' symbol. However, if
474 Voverlay_arrow_position is a marker, last-arrow-position is its
475 numerical position. */
476
477 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
478
479 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
480 properties on a symbol in overlay-arrow-variable-list. */
481
482 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
483
484 /* Like mode-line-format, but for the title bar on a visible frame. */
485
486 Lisp_Object Vframe_title_format;
487
488 /* Like mode-line-format, but for the title bar on an iconified frame. */
489
490 Lisp_Object Vicon_title_format;
491
492 /* List of functions to call when a window's size changes. These
493 functions get one arg, a frame on which one or more windows' sizes
494 have changed. */
495
496 static Lisp_Object Vwindow_size_change_functions;
497
498 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
499
500 /* Nonzero if an overlay arrow has been displayed in this window. */
501
502 static int overlay_arrow_seen;
503
504 /* Nonzero means highlight the region even in nonselected windows. */
505
506 int highlight_nonselected_windows;
507
508 /* If cursor motion alone moves point off frame, try scrolling this
509 many lines up or down if that will bring it back. */
510
511 static EMACS_INT scroll_step;
512
513 /* Nonzero means scroll just far enough to bring point back on the
514 screen, when appropriate. */
515
516 static EMACS_INT scroll_conservatively;
517
518 /* Recenter the window whenever point gets within this many lines of
519 the top or bottom of the window. This value is translated into a
520 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
521 that there is really a fixed pixel height scroll margin. */
522
523 EMACS_INT scroll_margin;
524
525 /* Number of windows showing the buffer of the selected window (or
526 another buffer with the same base buffer). keyboard.c refers to
527 this. */
528
529 int buffer_shared;
530
531 /* Vector containing glyphs for an ellipsis `...'. */
532
533 static Lisp_Object default_invis_vector[3];
534
535 /* Zero means display the mode-line/header-line/menu-bar in the default face
536 (this slightly odd definition is for compatibility with previous versions
537 of emacs), non-zero means display them using their respective faces.
538
539 This variable is deprecated. */
540
541 int mode_line_inverse_video;
542
543 /* Prompt to display in front of the mini-buffer contents. */
544
545 Lisp_Object minibuf_prompt;
546
547 /* Width of current mini-buffer prompt. Only set after display_line
548 of the line that contains the prompt. */
549
550 int minibuf_prompt_width;
551
552 /* This is the window where the echo area message was displayed. It
553 is always a mini-buffer window, but it may not be the same window
554 currently active as a mini-buffer. */
555
556 Lisp_Object echo_area_window;
557
558 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
559 pushes the current message and the value of
560 message_enable_multibyte on the stack, the function restore_message
561 pops the stack and displays MESSAGE again. */
562
563 Lisp_Object Vmessage_stack;
564
565 /* Nonzero means multibyte characters were enabled when the echo area
566 message was specified. */
567
568 int message_enable_multibyte;
569
570 /* Nonzero if we should redraw the mode lines on the next redisplay. */
571
572 int update_mode_lines;
573
574 /* Nonzero if window sizes or contents have changed since last
575 redisplay that finished. */
576
577 int windows_or_buffers_changed;
578
579 /* Nonzero means a frame's cursor type has been changed. */
580
581 int cursor_type_changed;
582
583 /* Nonzero after display_mode_line if %l was used and it displayed a
584 line number. */
585
586 int line_number_displayed;
587
588 /* Maximum buffer size for which to display line numbers. */
589
590 Lisp_Object Vline_number_display_limit;
591
592 /* Line width to consider when repositioning for line number display. */
593
594 static EMACS_INT line_number_display_limit_width;
595
596 /* Number of lines to keep in the message log buffer. t means
597 infinite. nil means don't log at all. */
598
599 Lisp_Object Vmessage_log_max;
600
601 /* The name of the *Messages* buffer, a string. */
602
603 static Lisp_Object Vmessages_buffer_name;
604
605 /* Current, index 0, and last displayed echo area message. Either
606 buffers from echo_buffers, or nil to indicate no message. */
607
608 Lisp_Object echo_area_buffer[2];
609
610 /* The buffers referenced from echo_area_buffer. */
611
612 static Lisp_Object echo_buffer[2];
613
614 /* A vector saved used in with_area_buffer to reduce consing. */
615
616 static Lisp_Object Vwith_echo_area_save_vector;
617
618 /* Non-zero means display_echo_area should display the last echo area
619 message again. Set by redisplay_preserve_echo_area. */
620
621 static int display_last_displayed_message_p;
622
623 /* Nonzero if echo area is being used by print; zero if being used by
624 message. */
625
626 int message_buf_print;
627
628 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
629
630 Lisp_Object Qinhibit_menubar_update;
631 int inhibit_menubar_update;
632
633 /* When evaluating expressions from menu bar items (enable conditions,
634 for instance), this is the frame they are being processed for. */
635
636 Lisp_Object Vmenu_updating_frame;
637
638 /* Maximum height for resizing mini-windows. Either a float
639 specifying a fraction of the available height, or an integer
640 specifying a number of lines. */
641
642 Lisp_Object Vmax_mini_window_height;
643
644 /* Non-zero means messages should be displayed with truncated
645 lines instead of being continued. */
646
647 int message_truncate_lines;
648 Lisp_Object Qmessage_truncate_lines;
649
650 /* Set to 1 in clear_message to make redisplay_internal aware
651 of an emptied echo area. */
652
653 static int message_cleared_p;
654
655 /* How to blink the default frame cursor off. */
656 Lisp_Object Vblink_cursor_alist;
657
658 /* A scratch glyph row with contents used for generating truncation
659 glyphs. Also used in direct_output_for_insert. */
660
661 #define MAX_SCRATCH_GLYPHS 100
662 struct glyph_row scratch_glyph_row;
663 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
664
665 /* Ascent and height of the last line processed by move_it_to. */
666
667 static int last_max_ascent, last_height;
668
669 /* Non-zero if there's a help-echo in the echo area. */
670
671 int help_echo_showing_p;
672
673 /* If >= 0, computed, exact values of mode-line and header-line height
674 to use in the macros CURRENT_MODE_LINE_HEIGHT and
675 CURRENT_HEADER_LINE_HEIGHT. */
676
677 int current_mode_line_height, current_header_line_height;
678
679 /* The maximum distance to look ahead for text properties. Values
680 that are too small let us call compute_char_face and similar
681 functions too often which is expensive. Values that are too large
682 let us call compute_char_face and alike too often because we
683 might not be interested in text properties that far away. */
684
685 #define TEXT_PROP_DISTANCE_LIMIT 100
686
687 #if GLYPH_DEBUG
688
689 /* Variables to turn off display optimizations from Lisp. */
690
691 int inhibit_try_window_id, inhibit_try_window_reusing;
692 int inhibit_try_cursor_movement;
693
694 /* Non-zero means print traces of redisplay if compiled with
695 GLYPH_DEBUG != 0. */
696
697 int trace_redisplay_p;
698
699 #endif /* GLYPH_DEBUG */
700
701 #ifdef DEBUG_TRACE_MOVE
702 /* Non-zero means trace with TRACE_MOVE to stderr. */
703 int trace_move;
704
705 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
706 #else
707 #define TRACE_MOVE(x) (void) 0
708 #endif
709
710 /* Non-zero means automatically scroll windows horizontally to make
711 point visible. */
712
713 int automatic_hscrolling_p;
714 Lisp_Object Qauto_hscroll_mode;
715
716 /* How close to the margin can point get before the window is scrolled
717 horizontally. */
718 EMACS_INT hscroll_margin;
719
720 /* How much to scroll horizontally when point is inside the above margin. */
721 Lisp_Object Vhscroll_step;
722
723 /* The variable `resize-mini-windows'. If nil, don't resize
724 mini-windows. If t, always resize them to fit the text they
725 display. If `grow-only', let mini-windows grow only until they
726 become empty. */
727
728 Lisp_Object Vresize_mini_windows;
729
730 /* Buffer being redisplayed -- for redisplay_window_error. */
731
732 struct buffer *displayed_buffer;
733
734 /* Space between overline and text. */
735
736 EMACS_INT overline_margin;
737
738 /* Require underline to be at least this many screen pixels below baseline
739 This to avoid underline "merging" with the base of letters at small
740 font sizes, particularly when x_use_underline_position_properties is on. */
741
742 EMACS_INT underline_minimum_offset;
743
744 /* Value returned from text property handlers (see below). */
745
746 enum prop_handled
747 {
748 HANDLED_NORMALLY,
749 HANDLED_RECOMPUTE_PROPS,
750 HANDLED_OVERLAY_STRING_CONSUMED,
751 HANDLED_RETURN
752 };
753
754 /* A description of text properties that redisplay is interested
755 in. */
756
757 struct props
758 {
759 /* The name of the property. */
760 Lisp_Object *name;
761
762 /* A unique index for the property. */
763 enum prop_idx idx;
764
765 /* A handler function called to set up iterator IT from the property
766 at IT's current position. Value is used to steer handle_stop. */
767 enum prop_handled (*handler) P_ ((struct it *it));
768 };
769
770 static enum prop_handled handle_face_prop P_ ((struct it *));
771 static enum prop_handled handle_invisible_prop P_ ((struct it *));
772 static enum prop_handled handle_display_prop P_ ((struct it *));
773 static enum prop_handled handle_composition_prop P_ ((struct it *));
774 static enum prop_handled handle_overlay_change P_ ((struct it *));
775 static enum prop_handled handle_fontified_prop P_ ((struct it *));
776
777 /* Properties handled by iterators. */
778
779 static struct props it_props[] =
780 {
781 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
782 /* Handle `face' before `display' because some sub-properties of
783 `display' need to know the face. */
784 {&Qface, FACE_PROP_IDX, handle_face_prop},
785 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
786 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
787 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
788 {NULL, 0, NULL}
789 };
790
791 /* Value is the position described by X. If X is a marker, value is
792 the marker_position of X. Otherwise, value is X. */
793
794 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
795
796 /* Enumeration returned by some move_it_.* functions internally. */
797
798 enum move_it_result
799 {
800 /* Not used. Undefined value. */
801 MOVE_UNDEFINED,
802
803 /* Move ended at the requested buffer position or ZV. */
804 MOVE_POS_MATCH_OR_ZV,
805
806 /* Move ended at the requested X pixel position. */
807 MOVE_X_REACHED,
808
809 /* Move within a line ended at the end of a line that must be
810 continued. */
811 MOVE_LINE_CONTINUED,
812
813 /* Move within a line ended at the end of a line that would
814 be displayed truncated. */
815 MOVE_LINE_TRUNCATED,
816
817 /* Move within a line ended at a line end. */
818 MOVE_NEWLINE_OR_CR
819 };
820
821 /* This counter is used to clear the face cache every once in a while
822 in redisplay_internal. It is incremented for each redisplay.
823 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
824 cleared. */
825
826 #define CLEAR_FACE_CACHE_COUNT 500
827 static int clear_face_cache_count;
828
829 /* Similarly for the image cache. */
830
831 #ifdef HAVE_WINDOW_SYSTEM
832 #define CLEAR_IMAGE_CACHE_COUNT 101
833 static int clear_image_cache_count;
834 #endif
835
836 /* Non-zero while redisplay_internal is in progress. */
837
838 int redisplaying_p;
839
840 /* Non-zero means don't free realized faces. Bound while freeing
841 realized faces is dangerous because glyph matrices might still
842 reference them. */
843
844 int inhibit_free_realized_faces;
845 Lisp_Object Qinhibit_free_realized_faces;
846
847 /* If a string, XTread_socket generates an event to display that string.
848 (The display is done in read_char.) */
849
850 Lisp_Object help_echo_string;
851 Lisp_Object help_echo_window;
852 Lisp_Object help_echo_object;
853 int help_echo_pos;
854
855 /* Temporary variable for XTread_socket. */
856
857 Lisp_Object previous_help_echo_string;
858
859 /* Null glyph slice */
860
861 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
862
863 /* Platform-independent portion of hourglass implementation. */
864
865 /* Non-zero means we're allowed to display a hourglass pointer. */
866 int display_hourglass_p;
867
868 /* Non-zero means an hourglass cursor is currently shown. */
869 int hourglass_shown_p;
870
871 /* If non-null, an asynchronous timer that, when it expires, displays
872 an hourglass cursor on all frames. */
873 struct atimer *hourglass_atimer;
874
875 /* Number of seconds to wait before displaying an hourglass cursor. */
876 Lisp_Object Vhourglass_delay;
877
878 /* Default number of seconds to wait before displaying an hourglass
879 cursor. */
880 #define DEFAULT_HOURGLASS_DELAY 1
881
882 \f
883 /* Function prototypes. */
884
885 static void setup_for_ellipsis P_ ((struct it *, int));
886 static void mark_window_display_accurate_1 P_ ((struct window *, int));
887 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
888 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
889 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
890 static int redisplay_mode_lines P_ ((Lisp_Object, int));
891 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
892
893 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
894
895 static void handle_line_prefix P_ ((struct it *));
896
897 static void pint2str P_ ((char *, int, int));
898 static void pint2hrstr P_ ((char *, int, int));
899 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
900 struct text_pos));
901 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
902 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
903 static void store_mode_line_noprop_char P_ ((char));
904 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
905 static void x_consider_frame_title P_ ((Lisp_Object));
906 static void handle_stop P_ ((struct it *));
907 static int tool_bar_lines_needed P_ ((struct frame *, int *));
908 static int single_display_spec_intangible_p P_ ((Lisp_Object));
909 static void ensure_echo_area_buffers P_ ((void));
910 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
911 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
912 static int with_echo_area_buffer P_ ((struct window *, int,
913 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
914 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
915 static void clear_garbaged_frames P_ ((void));
916 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
917 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
918 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
919 static int display_echo_area P_ ((struct window *));
920 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
921 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
922 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
923 static int string_char_and_length P_ ((const unsigned char *, int *));
924 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
925 struct text_pos));
926 static int compute_window_start_on_continuation_line P_ ((struct window *));
927 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
928 static void insert_left_trunc_glyphs P_ ((struct it *));
929 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
930 Lisp_Object));
931 static void extend_face_to_end_of_line P_ ((struct it *));
932 static int append_space_for_newline P_ ((struct it *, int));
933 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
934 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
935 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
936 static int trailing_whitespace_p P_ ((int));
937 static int message_log_check_duplicate P_ ((int, int, int, int));
938 static void push_it P_ ((struct it *));
939 static void pop_it P_ ((struct it *));
940 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
941 static void select_frame_for_redisplay P_ ((Lisp_Object));
942 static void redisplay_internal P_ ((int));
943 static int echo_area_display P_ ((int));
944 static void redisplay_windows P_ ((Lisp_Object));
945 static void redisplay_window P_ ((Lisp_Object, int));
946 static Lisp_Object redisplay_window_error ();
947 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
948 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
949 static int update_menu_bar P_ ((struct frame *, int, int));
950 static int try_window_reusing_current_matrix P_ ((struct window *));
951 static int try_window_id P_ ((struct window *));
952 static int display_line P_ ((struct it *));
953 static int display_mode_lines P_ ((struct window *));
954 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
955 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
956 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
957 static char *decode_mode_spec P_ ((struct window *, int, int, int,
958 Lisp_Object *));
959 static void display_menu_bar P_ ((struct window *));
960 static int display_count_lines P_ ((int, int, int, int, int *));
961 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
962 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
963 static void compute_line_metrics P_ ((struct it *));
964 static void run_redisplay_end_trigger_hook P_ ((struct it *));
965 static int get_overlay_strings P_ ((struct it *, int));
966 static int get_overlay_strings_1 P_ ((struct it *, int, int));
967 static void next_overlay_string P_ ((struct it *));
968 static void reseat P_ ((struct it *, struct text_pos, int));
969 static void reseat_1 P_ ((struct it *, struct text_pos, int));
970 static void back_to_previous_visible_line_start P_ ((struct it *));
971 void reseat_at_previous_visible_line_start P_ ((struct it *));
972 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
973 static int next_element_from_ellipsis P_ ((struct it *));
974 static int next_element_from_display_vector P_ ((struct it *));
975 static int next_element_from_string P_ ((struct it *));
976 static int next_element_from_c_string P_ ((struct it *));
977 static int next_element_from_buffer P_ ((struct it *));
978 static int next_element_from_composition P_ ((struct it *));
979 static int next_element_from_image P_ ((struct it *));
980 static int next_element_from_stretch P_ ((struct it *));
981 static void load_overlay_strings P_ ((struct it *, int));
982 static int init_from_display_pos P_ ((struct it *, struct window *,
983 struct display_pos *));
984 static void reseat_to_string P_ ((struct it *, unsigned char *,
985 Lisp_Object, int, int, int, int));
986 static enum move_it_result
987 move_it_in_display_line_to (struct it *, EMACS_INT, int,
988 enum move_operation_enum);
989 void move_it_vertically_backward P_ ((struct it *, int));
990 static void init_to_row_start P_ ((struct it *, struct window *,
991 struct glyph_row *));
992 static int init_to_row_end P_ ((struct it *, struct window *,
993 struct glyph_row *));
994 static void back_to_previous_line_start P_ ((struct it *));
995 static int forward_to_next_line_start P_ ((struct it *, int *));
996 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
997 Lisp_Object, int));
998 static struct text_pos string_pos P_ ((int, Lisp_Object));
999 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
1000 static int number_of_chars P_ ((unsigned char *, int));
1001 static void compute_stop_pos P_ ((struct it *));
1002 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
1003 Lisp_Object));
1004 static int face_before_or_after_it_pos P_ ((struct it *, int));
1005 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
1006 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
1007 Lisp_Object, Lisp_Object,
1008 struct text_pos *, int));
1009 static int underlying_face_id P_ ((struct it *));
1010 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
1011 struct window *));
1012
1013 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
1014 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
1015
1016 #ifdef HAVE_WINDOW_SYSTEM
1017
1018 static void update_tool_bar P_ ((struct frame *, int));
1019 static void build_desired_tool_bar_string P_ ((struct frame *f));
1020 static int redisplay_tool_bar P_ ((struct frame *));
1021 static void display_tool_bar_line P_ ((struct it *, int));
1022 static void notice_overwritten_cursor P_ ((struct window *,
1023 enum glyph_row_area,
1024 int, int, int, int));
1025
1026
1027
1028 #endif /* HAVE_WINDOW_SYSTEM */
1029
1030 \f
1031 /***********************************************************************
1032 Window display dimensions
1033 ***********************************************************************/
1034
1035 /* Return the bottom boundary y-position for text lines in window W.
1036 This is the first y position at which a line cannot start.
1037 It is relative to the top of the window.
1038
1039 This is the height of W minus the height of a mode line, if any. */
1040
1041 INLINE int
1042 window_text_bottom_y (w)
1043 struct window *w;
1044 {
1045 int height = WINDOW_TOTAL_HEIGHT (w);
1046
1047 if (WINDOW_WANTS_MODELINE_P (w))
1048 height -= CURRENT_MODE_LINE_HEIGHT (w);
1049 return height;
1050 }
1051
1052 /* Return the pixel width of display area AREA of window W. AREA < 0
1053 means return the total width of W, not including fringes to
1054 the left and right of the window. */
1055
1056 INLINE int
1057 window_box_width (w, area)
1058 struct window *w;
1059 int area;
1060 {
1061 int cols = XFASTINT (w->total_cols);
1062 int pixels = 0;
1063
1064 if (!w->pseudo_window_p)
1065 {
1066 cols -= WINDOW_SCROLL_BAR_COLS (w);
1067
1068 if (area == TEXT_AREA)
1069 {
1070 if (INTEGERP (w->left_margin_cols))
1071 cols -= XFASTINT (w->left_margin_cols);
1072 if (INTEGERP (w->right_margin_cols))
1073 cols -= XFASTINT (w->right_margin_cols);
1074 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1075 }
1076 else if (area == LEFT_MARGIN_AREA)
1077 {
1078 cols = (INTEGERP (w->left_margin_cols)
1079 ? XFASTINT (w->left_margin_cols) : 0);
1080 pixels = 0;
1081 }
1082 else if (area == RIGHT_MARGIN_AREA)
1083 {
1084 cols = (INTEGERP (w->right_margin_cols)
1085 ? XFASTINT (w->right_margin_cols) : 0);
1086 pixels = 0;
1087 }
1088 }
1089
1090 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1091 }
1092
1093
1094 /* Return the pixel height of the display area of window W, not
1095 including mode lines of W, if any. */
1096
1097 INLINE int
1098 window_box_height (w)
1099 struct window *w;
1100 {
1101 struct frame *f = XFRAME (w->frame);
1102 int height = WINDOW_TOTAL_HEIGHT (w);
1103
1104 xassert (height >= 0);
1105
1106 /* Note: the code below that determines the mode-line/header-line
1107 height is essentially the same as that contained in the macro
1108 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1109 the appropriate glyph row has its `mode_line_p' flag set,
1110 and if it doesn't, uses estimate_mode_line_height instead. */
1111
1112 if (WINDOW_WANTS_MODELINE_P (w))
1113 {
1114 struct glyph_row *ml_row
1115 = (w->current_matrix && w->current_matrix->rows
1116 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1117 : 0);
1118 if (ml_row && ml_row->mode_line_p)
1119 height -= ml_row->height;
1120 else
1121 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1122 }
1123
1124 if (WINDOW_WANTS_HEADER_LINE_P (w))
1125 {
1126 struct glyph_row *hl_row
1127 = (w->current_matrix && w->current_matrix->rows
1128 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1129 : 0);
1130 if (hl_row && hl_row->mode_line_p)
1131 height -= hl_row->height;
1132 else
1133 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1134 }
1135
1136 /* With a very small font and a mode-line that's taller than
1137 default, we might end up with a negative height. */
1138 return max (0, height);
1139 }
1140
1141 /* Return the window-relative coordinate of the left edge of display
1142 area AREA of window W. AREA < 0 means return the left edge of the
1143 whole window, to the right of the left fringe of W. */
1144
1145 INLINE int
1146 window_box_left_offset (w, area)
1147 struct window *w;
1148 int area;
1149 {
1150 int x;
1151
1152 if (w->pseudo_window_p)
1153 return 0;
1154
1155 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1156
1157 if (area == TEXT_AREA)
1158 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1159 + window_box_width (w, LEFT_MARGIN_AREA));
1160 else if (area == RIGHT_MARGIN_AREA)
1161 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1162 + window_box_width (w, LEFT_MARGIN_AREA)
1163 + window_box_width (w, TEXT_AREA)
1164 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1165 ? 0
1166 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1167 else if (area == LEFT_MARGIN_AREA
1168 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1169 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1170
1171 return x;
1172 }
1173
1174
1175 /* Return the window-relative coordinate of the right edge of display
1176 area AREA of window W. AREA < 0 means return the left edge of the
1177 whole window, to the left of the right fringe of W. */
1178
1179 INLINE int
1180 window_box_right_offset (w, area)
1181 struct window *w;
1182 int area;
1183 {
1184 return window_box_left_offset (w, area) + window_box_width (w, area);
1185 }
1186
1187 /* Return the frame-relative coordinate of the left edge of display
1188 area AREA of window W. AREA < 0 means return the left edge of the
1189 whole window, to the right of the left fringe of W. */
1190
1191 INLINE int
1192 window_box_left (w, area)
1193 struct window *w;
1194 int area;
1195 {
1196 struct frame *f = XFRAME (w->frame);
1197 int x;
1198
1199 if (w->pseudo_window_p)
1200 return FRAME_INTERNAL_BORDER_WIDTH (f);
1201
1202 x = (WINDOW_LEFT_EDGE_X (w)
1203 + window_box_left_offset (w, area));
1204
1205 return x;
1206 }
1207
1208
1209 /* Return the frame-relative coordinate of the right edge of display
1210 area AREA of window W. AREA < 0 means return the left edge of the
1211 whole window, to the left of the right fringe of W. */
1212
1213 INLINE int
1214 window_box_right (w, area)
1215 struct window *w;
1216 int area;
1217 {
1218 return window_box_left (w, area) + window_box_width (w, area);
1219 }
1220
1221 /* Get the bounding box of the display area AREA of window W, without
1222 mode lines, in frame-relative coordinates. AREA < 0 means the
1223 whole window, not including the left and right fringes of
1224 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1225 coordinates of the upper-left corner of the box. Return in
1226 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1227
1228 INLINE void
1229 window_box (w, area, box_x, box_y, box_width, box_height)
1230 struct window *w;
1231 int area;
1232 int *box_x, *box_y, *box_width, *box_height;
1233 {
1234 if (box_width)
1235 *box_width = window_box_width (w, area);
1236 if (box_height)
1237 *box_height = window_box_height (w);
1238 if (box_x)
1239 *box_x = window_box_left (w, area);
1240 if (box_y)
1241 {
1242 *box_y = WINDOW_TOP_EDGE_Y (w);
1243 if (WINDOW_WANTS_HEADER_LINE_P (w))
1244 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1245 }
1246 }
1247
1248
1249 /* Get the bounding box of the display area AREA of window W, without
1250 mode lines. AREA < 0 means the whole window, not including the
1251 left and right fringe of the window. Return in *TOP_LEFT_X
1252 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1253 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1254 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1255 box. */
1256
1257 INLINE void
1258 window_box_edges (w, area, top_left_x, top_left_y,
1259 bottom_right_x, bottom_right_y)
1260 struct window *w;
1261 int area;
1262 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1263 {
1264 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1265 bottom_right_y);
1266 *bottom_right_x += *top_left_x;
1267 *bottom_right_y += *top_left_y;
1268 }
1269
1270
1271 \f
1272 /***********************************************************************
1273 Utilities
1274 ***********************************************************************/
1275
1276 /* Return the bottom y-position of the line the iterator IT is in.
1277 This can modify IT's settings. */
1278
1279 int
1280 line_bottom_y (it)
1281 struct it *it;
1282 {
1283 int line_height = it->max_ascent + it->max_descent;
1284 int line_top_y = it->current_y;
1285
1286 if (line_height == 0)
1287 {
1288 if (last_height)
1289 line_height = last_height;
1290 else if (IT_CHARPOS (*it) < ZV)
1291 {
1292 move_it_by_lines (it, 1, 1);
1293 line_height = (it->max_ascent || it->max_descent
1294 ? it->max_ascent + it->max_descent
1295 : last_height);
1296 }
1297 else
1298 {
1299 struct glyph_row *row = it->glyph_row;
1300
1301 /* Use the default character height. */
1302 it->glyph_row = NULL;
1303 it->what = IT_CHARACTER;
1304 it->c = ' ';
1305 it->len = 1;
1306 PRODUCE_GLYPHS (it);
1307 line_height = it->ascent + it->descent;
1308 it->glyph_row = row;
1309 }
1310 }
1311
1312 return line_top_y + line_height;
1313 }
1314
1315
1316 /* Return 1 if position CHARPOS is visible in window W.
1317 CHARPOS < 0 means return info about WINDOW_END position.
1318 If visible, set *X and *Y to pixel coordinates of top left corner.
1319 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1320 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1321
1322 int
1323 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1324 struct window *w;
1325 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1326 {
1327 struct it it;
1328 struct text_pos top;
1329 int visible_p = 0;
1330 struct buffer *old_buffer = NULL;
1331
1332 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1333 return visible_p;
1334
1335 if (XBUFFER (w->buffer) != current_buffer)
1336 {
1337 old_buffer = current_buffer;
1338 set_buffer_internal_1 (XBUFFER (w->buffer));
1339 }
1340
1341 SET_TEXT_POS_FROM_MARKER (top, w->start);
1342
1343 /* Compute exact mode line heights. */
1344 if (WINDOW_WANTS_MODELINE_P (w))
1345 current_mode_line_height
1346 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1347 current_buffer->mode_line_format);
1348
1349 if (WINDOW_WANTS_HEADER_LINE_P (w))
1350 current_header_line_height
1351 = display_mode_line (w, HEADER_LINE_FACE_ID,
1352 current_buffer->header_line_format);
1353
1354 start_display (&it, w, top);
1355 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1356 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1357
1358 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1359 {
1360 /* We have reached CHARPOS, or passed it. How the call to
1361 move_it_to can overshoot: (i) If CHARPOS is on invisible
1362 text, move_it_to stops at the end of the invisible text,
1363 after CHARPOS. (ii) If CHARPOS is in a display vector,
1364 move_it_to stops on its last glyph. */
1365 int top_x = it.current_x;
1366 int top_y = it.current_y;
1367 enum it_method it_method = it.method;
1368 /* Calling line_bottom_y may change it.method, it.position, etc. */
1369 int bottom_y = (last_height = 0, line_bottom_y (&it));
1370 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1371
1372 if (top_y < window_top_y)
1373 visible_p = bottom_y > window_top_y;
1374 else if (top_y < it.last_visible_y)
1375 visible_p = 1;
1376 if (visible_p)
1377 {
1378 if (it_method == GET_FROM_DISPLAY_VECTOR)
1379 {
1380 /* We stopped on the last glyph of a display vector.
1381 Try and recompute. Hack alert! */
1382 if (charpos < 2 || top.charpos >= charpos)
1383 top_x = it.glyph_row->x;
1384 else
1385 {
1386 struct it it2;
1387 start_display (&it2, w, top);
1388 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1389 get_next_display_element (&it2);
1390 PRODUCE_GLYPHS (&it2);
1391 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1392 || it2.current_x > it2.last_visible_x)
1393 top_x = it.glyph_row->x;
1394 else
1395 {
1396 top_x = it2.current_x;
1397 top_y = it2.current_y;
1398 }
1399 }
1400 }
1401
1402 *x = top_x;
1403 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1404 *rtop = max (0, window_top_y - top_y);
1405 *rbot = max (0, bottom_y - it.last_visible_y);
1406 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1407 - max (top_y, window_top_y)));
1408 *vpos = it.vpos;
1409 }
1410 }
1411 else
1412 {
1413 struct it it2;
1414
1415 it2 = it;
1416 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1417 move_it_by_lines (&it, 1, 0);
1418 if (charpos < IT_CHARPOS (it)
1419 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1420 {
1421 visible_p = 1;
1422 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1423 *x = it2.current_x;
1424 *y = it2.current_y + it2.max_ascent - it2.ascent;
1425 *rtop = max (0, -it2.current_y);
1426 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1427 - it.last_visible_y));
1428 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1429 it.last_visible_y)
1430 - max (it2.current_y,
1431 WINDOW_HEADER_LINE_HEIGHT (w))));
1432 *vpos = it2.vpos;
1433 }
1434 }
1435
1436 if (old_buffer)
1437 set_buffer_internal_1 (old_buffer);
1438
1439 current_header_line_height = current_mode_line_height = -1;
1440
1441 if (visible_p && XFASTINT (w->hscroll) > 0)
1442 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1443
1444 #if 0
1445 /* Debugging code. */
1446 if (visible_p)
1447 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1448 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1449 else
1450 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1451 #endif
1452
1453 return visible_p;
1454 }
1455
1456
1457 /* Return the next character from STR which is MAXLEN bytes long.
1458 Return in *LEN the length of the character. This is like
1459 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1460 we find one, we return a `?', but with the length of the invalid
1461 character. */
1462
1463 static INLINE int
1464 string_char_and_length (str, len)
1465 const unsigned char *str;
1466 int *len;
1467 {
1468 int c;
1469
1470 c = STRING_CHAR_AND_LENGTH (str, *len);
1471 if (!CHAR_VALID_P (c, 1))
1472 /* We may not change the length here because other places in Emacs
1473 don't use this function, i.e. they silently accept invalid
1474 characters. */
1475 c = '?';
1476
1477 return c;
1478 }
1479
1480
1481
1482 /* Given a position POS containing a valid character and byte position
1483 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1484
1485 static struct text_pos
1486 string_pos_nchars_ahead (pos, string, nchars)
1487 struct text_pos pos;
1488 Lisp_Object string;
1489 int nchars;
1490 {
1491 xassert (STRINGP (string) && nchars >= 0);
1492
1493 if (STRING_MULTIBYTE (string))
1494 {
1495 int rest = SBYTES (string) - BYTEPOS (pos);
1496 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1497 int len;
1498
1499 while (nchars--)
1500 {
1501 string_char_and_length (p, &len);
1502 p += len, rest -= len;
1503 xassert (rest >= 0);
1504 CHARPOS (pos) += 1;
1505 BYTEPOS (pos) += len;
1506 }
1507 }
1508 else
1509 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1510
1511 return pos;
1512 }
1513
1514
1515 /* Value is the text position, i.e. character and byte position,
1516 for character position CHARPOS in STRING. */
1517
1518 static INLINE struct text_pos
1519 string_pos (charpos, string)
1520 int charpos;
1521 Lisp_Object string;
1522 {
1523 struct text_pos pos;
1524 xassert (STRINGP (string));
1525 xassert (charpos >= 0);
1526 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1527 return pos;
1528 }
1529
1530
1531 /* Value is a text position, i.e. character and byte position, for
1532 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1533 means recognize multibyte characters. */
1534
1535 static struct text_pos
1536 c_string_pos (charpos, s, multibyte_p)
1537 int charpos;
1538 unsigned char *s;
1539 int multibyte_p;
1540 {
1541 struct text_pos pos;
1542
1543 xassert (s != NULL);
1544 xassert (charpos >= 0);
1545
1546 if (multibyte_p)
1547 {
1548 int rest = strlen (s), len;
1549
1550 SET_TEXT_POS (pos, 0, 0);
1551 while (charpos--)
1552 {
1553 string_char_and_length (s, &len);
1554 s += len, rest -= len;
1555 xassert (rest >= 0);
1556 CHARPOS (pos) += 1;
1557 BYTEPOS (pos) += len;
1558 }
1559 }
1560 else
1561 SET_TEXT_POS (pos, charpos, charpos);
1562
1563 return pos;
1564 }
1565
1566
1567 /* Value is the number of characters in C string S. MULTIBYTE_P
1568 non-zero means recognize multibyte characters. */
1569
1570 static int
1571 number_of_chars (s, multibyte_p)
1572 unsigned char *s;
1573 int multibyte_p;
1574 {
1575 int nchars;
1576
1577 if (multibyte_p)
1578 {
1579 int rest = strlen (s), len;
1580 unsigned char *p = (unsigned char *) s;
1581
1582 for (nchars = 0; rest > 0; ++nchars)
1583 {
1584 string_char_and_length (p, &len);
1585 rest -= len, p += len;
1586 }
1587 }
1588 else
1589 nchars = strlen (s);
1590
1591 return nchars;
1592 }
1593
1594
1595 /* Compute byte position NEWPOS->bytepos corresponding to
1596 NEWPOS->charpos. POS is a known position in string STRING.
1597 NEWPOS->charpos must be >= POS.charpos. */
1598
1599 static void
1600 compute_string_pos (newpos, pos, string)
1601 struct text_pos *newpos, pos;
1602 Lisp_Object string;
1603 {
1604 xassert (STRINGP (string));
1605 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1606
1607 if (STRING_MULTIBYTE (string))
1608 *newpos = string_pos_nchars_ahead (pos, string,
1609 CHARPOS (*newpos) - CHARPOS (pos));
1610 else
1611 BYTEPOS (*newpos) = CHARPOS (*newpos);
1612 }
1613
1614 /* EXPORT:
1615 Return an estimation of the pixel height of mode or header lines on
1616 frame F. FACE_ID specifies what line's height to estimate. */
1617
1618 int
1619 estimate_mode_line_height (f, face_id)
1620 struct frame *f;
1621 enum face_id face_id;
1622 {
1623 #ifdef HAVE_WINDOW_SYSTEM
1624 if (FRAME_WINDOW_P (f))
1625 {
1626 int height = FONT_HEIGHT (FRAME_FONT (f));
1627
1628 /* This function is called so early when Emacs starts that the face
1629 cache and mode line face are not yet initialized. */
1630 if (FRAME_FACE_CACHE (f))
1631 {
1632 struct face *face = FACE_FROM_ID (f, face_id);
1633 if (face)
1634 {
1635 if (face->font)
1636 height = FONT_HEIGHT (face->font);
1637 if (face->box_line_width > 0)
1638 height += 2 * face->box_line_width;
1639 }
1640 }
1641
1642 return height;
1643 }
1644 #endif
1645
1646 return 1;
1647 }
1648
1649 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1650 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1651 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1652 not force the value into range. */
1653
1654 void
1655 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1656 FRAME_PTR f;
1657 register int pix_x, pix_y;
1658 int *x, *y;
1659 NativeRectangle *bounds;
1660 int noclip;
1661 {
1662
1663 #ifdef HAVE_WINDOW_SYSTEM
1664 if (FRAME_WINDOW_P (f))
1665 {
1666 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1667 even for negative values. */
1668 if (pix_x < 0)
1669 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1670 if (pix_y < 0)
1671 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1672
1673 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1674 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1675
1676 if (bounds)
1677 STORE_NATIVE_RECT (*bounds,
1678 FRAME_COL_TO_PIXEL_X (f, pix_x),
1679 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1680 FRAME_COLUMN_WIDTH (f) - 1,
1681 FRAME_LINE_HEIGHT (f) - 1);
1682
1683 if (!noclip)
1684 {
1685 if (pix_x < 0)
1686 pix_x = 0;
1687 else if (pix_x > FRAME_TOTAL_COLS (f))
1688 pix_x = FRAME_TOTAL_COLS (f);
1689
1690 if (pix_y < 0)
1691 pix_y = 0;
1692 else if (pix_y > FRAME_LINES (f))
1693 pix_y = FRAME_LINES (f);
1694 }
1695 }
1696 #endif
1697
1698 *x = pix_x;
1699 *y = pix_y;
1700 }
1701
1702
1703 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1704 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1705 can't tell the positions because W's display is not up to date,
1706 return 0. */
1707
1708 int
1709 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1710 struct window *w;
1711 int hpos, vpos;
1712 int *frame_x, *frame_y;
1713 {
1714 #ifdef HAVE_WINDOW_SYSTEM
1715 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1716 {
1717 int success_p;
1718
1719 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1720 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1721
1722 if (display_completed)
1723 {
1724 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1725 struct glyph *glyph = row->glyphs[TEXT_AREA];
1726 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1727
1728 hpos = row->x;
1729 vpos = row->y;
1730 while (glyph < end)
1731 {
1732 hpos += glyph->pixel_width;
1733 ++glyph;
1734 }
1735
1736 /* If first glyph is partially visible, its first visible position is still 0. */
1737 if (hpos < 0)
1738 hpos = 0;
1739
1740 success_p = 1;
1741 }
1742 else
1743 {
1744 hpos = vpos = 0;
1745 success_p = 0;
1746 }
1747
1748 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1749 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1750 return success_p;
1751 }
1752 #endif
1753
1754 *frame_x = hpos;
1755 *frame_y = vpos;
1756 return 1;
1757 }
1758
1759
1760 #ifdef HAVE_WINDOW_SYSTEM
1761
1762 /* Find the glyph under window-relative coordinates X/Y in window W.
1763 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1764 strings. Return in *HPOS and *VPOS the row and column number of
1765 the glyph found. Return in *AREA the glyph area containing X.
1766 Value is a pointer to the glyph found or null if X/Y is not on
1767 text, or we can't tell because W's current matrix is not up to
1768 date. */
1769
1770 static
1771 struct glyph *
1772 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1773 struct window *w;
1774 int x, y;
1775 int *hpos, *vpos, *dx, *dy, *area;
1776 {
1777 struct glyph *glyph, *end;
1778 struct glyph_row *row = NULL;
1779 int x0, i;
1780
1781 /* Find row containing Y. Give up if some row is not enabled. */
1782 for (i = 0; i < w->current_matrix->nrows; ++i)
1783 {
1784 row = MATRIX_ROW (w->current_matrix, i);
1785 if (!row->enabled_p)
1786 return NULL;
1787 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1788 break;
1789 }
1790
1791 *vpos = i;
1792 *hpos = 0;
1793
1794 /* Give up if Y is not in the window. */
1795 if (i == w->current_matrix->nrows)
1796 return NULL;
1797
1798 /* Get the glyph area containing X. */
1799 if (w->pseudo_window_p)
1800 {
1801 *area = TEXT_AREA;
1802 x0 = 0;
1803 }
1804 else
1805 {
1806 if (x < window_box_left_offset (w, TEXT_AREA))
1807 {
1808 *area = LEFT_MARGIN_AREA;
1809 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1810 }
1811 else if (x < window_box_right_offset (w, TEXT_AREA))
1812 {
1813 *area = TEXT_AREA;
1814 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1815 }
1816 else
1817 {
1818 *area = RIGHT_MARGIN_AREA;
1819 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1820 }
1821 }
1822
1823 /* Find glyph containing X. */
1824 glyph = row->glyphs[*area];
1825 end = glyph + row->used[*area];
1826 x -= x0;
1827 while (glyph < end && x >= glyph->pixel_width)
1828 {
1829 x -= glyph->pixel_width;
1830 ++glyph;
1831 }
1832
1833 if (glyph == end)
1834 return NULL;
1835
1836 if (dx)
1837 {
1838 *dx = x;
1839 *dy = y - (row->y + row->ascent - glyph->ascent);
1840 }
1841
1842 *hpos = glyph - row->glyphs[*area];
1843 return glyph;
1844 }
1845
1846
1847 /* EXPORT:
1848 Convert frame-relative x/y to coordinates relative to window W.
1849 Takes pseudo-windows into account. */
1850
1851 void
1852 frame_to_window_pixel_xy (w, x, y)
1853 struct window *w;
1854 int *x, *y;
1855 {
1856 if (w->pseudo_window_p)
1857 {
1858 /* A pseudo-window is always full-width, and starts at the
1859 left edge of the frame, plus a frame border. */
1860 struct frame *f = XFRAME (w->frame);
1861 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1862 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1863 }
1864 else
1865 {
1866 *x -= WINDOW_LEFT_EDGE_X (w);
1867 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1868 }
1869 }
1870
1871 /* EXPORT:
1872 Return in RECTS[] at most N clipping rectangles for glyph string S.
1873 Return the number of stored rectangles. */
1874
1875 int
1876 get_glyph_string_clip_rects (s, rects, n)
1877 struct glyph_string *s;
1878 NativeRectangle *rects;
1879 int n;
1880 {
1881 XRectangle r;
1882
1883 if (n <= 0)
1884 return 0;
1885
1886 if (s->row->full_width_p)
1887 {
1888 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1889 r.x = WINDOW_LEFT_EDGE_X (s->w);
1890 r.width = WINDOW_TOTAL_WIDTH (s->w);
1891
1892 /* Unless displaying a mode or menu bar line, which are always
1893 fully visible, clip to the visible part of the row. */
1894 if (s->w->pseudo_window_p)
1895 r.height = s->row->visible_height;
1896 else
1897 r.height = s->height;
1898 }
1899 else
1900 {
1901 /* This is a text line that may be partially visible. */
1902 r.x = window_box_left (s->w, s->area);
1903 r.width = window_box_width (s->w, s->area);
1904 r.height = s->row->visible_height;
1905 }
1906
1907 if (s->clip_head)
1908 if (r.x < s->clip_head->x)
1909 {
1910 if (r.width >= s->clip_head->x - r.x)
1911 r.width -= s->clip_head->x - r.x;
1912 else
1913 r.width = 0;
1914 r.x = s->clip_head->x;
1915 }
1916 if (s->clip_tail)
1917 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1918 {
1919 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1920 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1921 else
1922 r.width = 0;
1923 }
1924
1925 /* If S draws overlapping rows, it's sufficient to use the top and
1926 bottom of the window for clipping because this glyph string
1927 intentionally draws over other lines. */
1928 if (s->for_overlaps)
1929 {
1930 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1931 r.height = window_text_bottom_y (s->w) - r.y;
1932
1933 /* Alas, the above simple strategy does not work for the
1934 environments with anti-aliased text: if the same text is
1935 drawn onto the same place multiple times, it gets thicker.
1936 If the overlap we are processing is for the erased cursor, we
1937 take the intersection with the rectagle of the cursor. */
1938 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1939 {
1940 XRectangle rc, r_save = r;
1941
1942 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1943 rc.y = s->w->phys_cursor.y;
1944 rc.width = s->w->phys_cursor_width;
1945 rc.height = s->w->phys_cursor_height;
1946
1947 x_intersect_rectangles (&r_save, &rc, &r);
1948 }
1949 }
1950 else
1951 {
1952 /* Don't use S->y for clipping because it doesn't take partially
1953 visible lines into account. For example, it can be negative for
1954 partially visible lines at the top of a window. */
1955 if (!s->row->full_width_p
1956 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1957 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1958 else
1959 r.y = max (0, s->row->y);
1960 }
1961
1962 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1963
1964 /* If drawing the cursor, don't let glyph draw outside its
1965 advertised boundaries. Cleartype does this under some circumstances. */
1966 if (s->hl == DRAW_CURSOR)
1967 {
1968 struct glyph *glyph = s->first_glyph;
1969 int height, max_y;
1970
1971 if (s->x > r.x)
1972 {
1973 r.width -= s->x - r.x;
1974 r.x = s->x;
1975 }
1976 r.width = min (r.width, glyph->pixel_width);
1977
1978 /* If r.y is below window bottom, ensure that we still see a cursor. */
1979 height = min (glyph->ascent + glyph->descent,
1980 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1981 max_y = window_text_bottom_y (s->w) - height;
1982 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1983 if (s->ybase - glyph->ascent > max_y)
1984 {
1985 r.y = max_y;
1986 r.height = height;
1987 }
1988 else
1989 {
1990 /* Don't draw cursor glyph taller than our actual glyph. */
1991 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1992 if (height < r.height)
1993 {
1994 max_y = r.y + r.height;
1995 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1996 r.height = min (max_y - r.y, height);
1997 }
1998 }
1999 }
2000
2001 if (s->row->clip)
2002 {
2003 XRectangle r_save = r;
2004
2005 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2006 r.width = 0;
2007 }
2008
2009 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2010 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2011 {
2012 #ifdef CONVERT_FROM_XRECT
2013 CONVERT_FROM_XRECT (r, *rects);
2014 #else
2015 *rects = r;
2016 #endif
2017 return 1;
2018 }
2019 else
2020 {
2021 /* If we are processing overlapping and allowed to return
2022 multiple clipping rectangles, we exclude the row of the glyph
2023 string from the clipping rectangle. This is to avoid drawing
2024 the same text on the environment with anti-aliasing. */
2025 #ifdef CONVERT_FROM_XRECT
2026 XRectangle rs[2];
2027 #else
2028 XRectangle *rs = rects;
2029 #endif
2030 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2031
2032 if (s->for_overlaps & OVERLAPS_PRED)
2033 {
2034 rs[i] = r;
2035 if (r.y + r.height > row_y)
2036 {
2037 if (r.y < row_y)
2038 rs[i].height = row_y - r.y;
2039 else
2040 rs[i].height = 0;
2041 }
2042 i++;
2043 }
2044 if (s->for_overlaps & OVERLAPS_SUCC)
2045 {
2046 rs[i] = r;
2047 if (r.y < row_y + s->row->visible_height)
2048 {
2049 if (r.y + r.height > row_y + s->row->visible_height)
2050 {
2051 rs[i].y = row_y + s->row->visible_height;
2052 rs[i].height = r.y + r.height - rs[i].y;
2053 }
2054 else
2055 rs[i].height = 0;
2056 }
2057 i++;
2058 }
2059
2060 n = i;
2061 #ifdef CONVERT_FROM_XRECT
2062 for (i = 0; i < n; i++)
2063 CONVERT_FROM_XRECT (rs[i], rects[i]);
2064 #endif
2065 return n;
2066 }
2067 }
2068
2069 /* EXPORT:
2070 Return in *NR the clipping rectangle for glyph string S. */
2071
2072 void
2073 get_glyph_string_clip_rect (s, nr)
2074 struct glyph_string *s;
2075 NativeRectangle *nr;
2076 {
2077 get_glyph_string_clip_rects (s, nr, 1);
2078 }
2079
2080
2081 /* EXPORT:
2082 Return the position and height of the phys cursor in window W.
2083 Set w->phys_cursor_width to width of phys cursor.
2084 */
2085
2086 void
2087 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2088 struct window *w;
2089 struct glyph_row *row;
2090 struct glyph *glyph;
2091 int *xp, *yp, *heightp;
2092 {
2093 struct frame *f = XFRAME (WINDOW_FRAME (w));
2094 int x, y, wd, h, h0, y0;
2095
2096 /* Compute the width of the rectangle to draw. If on a stretch
2097 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2098 rectangle as wide as the glyph, but use a canonical character
2099 width instead. */
2100 wd = glyph->pixel_width - 1;
2101 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
2102 wd++; /* Why? */
2103 #endif
2104
2105 x = w->phys_cursor.x;
2106 if (x < 0)
2107 {
2108 wd += x;
2109 x = 0;
2110 }
2111
2112 if (glyph->type == STRETCH_GLYPH
2113 && !x_stretch_cursor_p)
2114 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2115 w->phys_cursor_width = wd;
2116
2117 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2118
2119 /* If y is below window bottom, ensure that we still see a cursor. */
2120 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2121
2122 h = max (h0, glyph->ascent + glyph->descent);
2123 h0 = min (h0, glyph->ascent + glyph->descent);
2124
2125 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2126 if (y < y0)
2127 {
2128 h = max (h - (y0 - y) + 1, h0);
2129 y = y0 - 1;
2130 }
2131 else
2132 {
2133 y0 = window_text_bottom_y (w) - h0;
2134 if (y > y0)
2135 {
2136 h += y - y0;
2137 y = y0;
2138 }
2139 }
2140
2141 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2142 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2143 *heightp = h;
2144 }
2145
2146 /*
2147 * Remember which glyph the mouse is over.
2148 */
2149
2150 void
2151 remember_mouse_glyph (f, gx, gy, rect)
2152 struct frame *f;
2153 int gx, gy;
2154 NativeRectangle *rect;
2155 {
2156 Lisp_Object window;
2157 struct window *w;
2158 struct glyph_row *r, *gr, *end_row;
2159 enum window_part part;
2160 enum glyph_row_area area;
2161 int x, y, width, height;
2162
2163 /* Try to determine frame pixel position and size of the glyph under
2164 frame pixel coordinates X/Y on frame F. */
2165
2166 if (!f->glyphs_initialized_p
2167 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2168 NILP (window)))
2169 {
2170 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2171 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2172 goto virtual_glyph;
2173 }
2174
2175 w = XWINDOW (window);
2176 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2177 height = WINDOW_FRAME_LINE_HEIGHT (w);
2178
2179 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2180 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2181
2182 if (w->pseudo_window_p)
2183 {
2184 area = TEXT_AREA;
2185 part = ON_MODE_LINE; /* Don't adjust margin. */
2186 goto text_glyph;
2187 }
2188
2189 switch (part)
2190 {
2191 case ON_LEFT_MARGIN:
2192 area = LEFT_MARGIN_AREA;
2193 goto text_glyph;
2194
2195 case ON_RIGHT_MARGIN:
2196 area = RIGHT_MARGIN_AREA;
2197 goto text_glyph;
2198
2199 case ON_HEADER_LINE:
2200 case ON_MODE_LINE:
2201 gr = (part == ON_HEADER_LINE
2202 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2203 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2204 gy = gr->y;
2205 area = TEXT_AREA;
2206 goto text_glyph_row_found;
2207
2208 case ON_TEXT:
2209 area = TEXT_AREA;
2210
2211 text_glyph:
2212 gr = 0; gy = 0;
2213 for (; r <= end_row && r->enabled_p; ++r)
2214 if (r->y + r->height > y)
2215 {
2216 gr = r; gy = r->y;
2217 break;
2218 }
2219
2220 text_glyph_row_found:
2221 if (gr && gy <= y)
2222 {
2223 struct glyph *g = gr->glyphs[area];
2224 struct glyph *end = g + gr->used[area];
2225
2226 height = gr->height;
2227 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2228 if (gx + g->pixel_width > x)
2229 break;
2230
2231 if (g < end)
2232 {
2233 if (g->type == IMAGE_GLYPH)
2234 {
2235 /* Don't remember when mouse is over image, as
2236 image may have hot-spots. */
2237 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2238 return;
2239 }
2240 width = g->pixel_width;
2241 }
2242 else
2243 {
2244 /* Use nominal char spacing at end of line. */
2245 x -= gx;
2246 gx += (x / width) * width;
2247 }
2248
2249 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2250 gx += window_box_left_offset (w, area);
2251 }
2252 else
2253 {
2254 /* Use nominal line height at end of window. */
2255 gx = (x / width) * width;
2256 y -= gy;
2257 gy += (y / height) * height;
2258 }
2259 break;
2260
2261 case ON_LEFT_FRINGE:
2262 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2263 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2264 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2265 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2266 goto row_glyph;
2267
2268 case ON_RIGHT_FRINGE:
2269 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2270 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2271 : window_box_right_offset (w, TEXT_AREA));
2272 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2273 goto row_glyph;
2274
2275 case ON_SCROLL_BAR:
2276 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2277 ? 0
2278 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2279 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2280 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2281 : 0)));
2282 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2283
2284 row_glyph:
2285 gr = 0, gy = 0;
2286 for (; r <= end_row && r->enabled_p; ++r)
2287 if (r->y + r->height > y)
2288 {
2289 gr = r; gy = r->y;
2290 break;
2291 }
2292
2293 if (gr && gy <= y)
2294 height = gr->height;
2295 else
2296 {
2297 /* Use nominal line height at end of window. */
2298 y -= gy;
2299 gy += (y / height) * height;
2300 }
2301 break;
2302
2303 default:
2304 ;
2305 virtual_glyph:
2306 /* If there is no glyph under the mouse, then we divide the screen
2307 into a grid of the smallest glyph in the frame, and use that
2308 as our "glyph". */
2309
2310 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2311 round down even for negative values. */
2312 if (gx < 0)
2313 gx -= width - 1;
2314 if (gy < 0)
2315 gy -= height - 1;
2316
2317 gx = (gx / width) * width;
2318 gy = (gy / height) * height;
2319
2320 goto store_rect;
2321 }
2322
2323 gx += WINDOW_LEFT_EDGE_X (w);
2324 gy += WINDOW_TOP_EDGE_Y (w);
2325
2326 store_rect:
2327 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2328
2329 /* Visible feedback for debugging. */
2330 #if 0
2331 #if HAVE_X_WINDOWS
2332 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2333 f->output_data.x->normal_gc,
2334 gx, gy, width, height);
2335 #endif
2336 #endif
2337 }
2338
2339
2340 #endif /* HAVE_WINDOW_SYSTEM */
2341
2342 \f
2343 /***********************************************************************
2344 Lisp form evaluation
2345 ***********************************************************************/
2346
2347 /* Error handler for safe_eval and safe_call. */
2348
2349 static Lisp_Object
2350 safe_eval_handler (arg)
2351 Lisp_Object arg;
2352 {
2353 add_to_log ("Error during redisplay: %s", arg, Qnil);
2354 return Qnil;
2355 }
2356
2357
2358 /* Evaluate SEXPR and return the result, or nil if something went
2359 wrong. Prevent redisplay during the evaluation. */
2360
2361 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2362 Return the result, or nil if something went wrong. Prevent
2363 redisplay during the evaluation. */
2364
2365 Lisp_Object
2366 safe_call (nargs, args)
2367 int nargs;
2368 Lisp_Object *args;
2369 {
2370 Lisp_Object val;
2371
2372 if (inhibit_eval_during_redisplay)
2373 val = Qnil;
2374 else
2375 {
2376 int count = SPECPDL_INDEX ();
2377 struct gcpro gcpro1;
2378
2379 GCPRO1 (args[0]);
2380 gcpro1.nvars = nargs;
2381 specbind (Qinhibit_redisplay, Qt);
2382 /* Use Qt to ensure debugger does not run,
2383 so there is no possibility of wanting to redisplay. */
2384 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2385 safe_eval_handler);
2386 UNGCPRO;
2387 val = unbind_to (count, val);
2388 }
2389
2390 return val;
2391 }
2392
2393
2394 /* Call function FN with one argument ARG.
2395 Return the result, or nil if something went wrong. */
2396
2397 Lisp_Object
2398 safe_call1 (fn, arg)
2399 Lisp_Object fn, arg;
2400 {
2401 Lisp_Object args[2];
2402 args[0] = fn;
2403 args[1] = arg;
2404 return safe_call (2, args);
2405 }
2406
2407 static Lisp_Object Qeval;
2408
2409 Lisp_Object
2410 safe_eval (Lisp_Object sexpr)
2411 {
2412 return safe_call1 (Qeval, sexpr);
2413 }
2414
2415 /* Call function FN with one argument ARG.
2416 Return the result, or nil if something went wrong. */
2417
2418 Lisp_Object
2419 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2420 {
2421 Lisp_Object args[3];
2422 args[0] = fn;
2423 args[1] = arg1;
2424 args[2] = arg2;
2425 return safe_call (3, args);
2426 }
2427
2428
2429 \f
2430 /***********************************************************************
2431 Debugging
2432 ***********************************************************************/
2433
2434 #if 0
2435
2436 /* Define CHECK_IT to perform sanity checks on iterators.
2437 This is for debugging. It is too slow to do unconditionally. */
2438
2439 static void
2440 check_it (it)
2441 struct it *it;
2442 {
2443 if (it->method == GET_FROM_STRING)
2444 {
2445 xassert (STRINGP (it->string));
2446 xassert (IT_STRING_CHARPOS (*it) >= 0);
2447 }
2448 else
2449 {
2450 xassert (IT_STRING_CHARPOS (*it) < 0);
2451 if (it->method == GET_FROM_BUFFER)
2452 {
2453 /* Check that character and byte positions agree. */
2454 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2455 }
2456 }
2457
2458 if (it->dpvec)
2459 xassert (it->current.dpvec_index >= 0);
2460 else
2461 xassert (it->current.dpvec_index < 0);
2462 }
2463
2464 #define CHECK_IT(IT) check_it ((IT))
2465
2466 #else /* not 0 */
2467
2468 #define CHECK_IT(IT) (void) 0
2469
2470 #endif /* not 0 */
2471
2472
2473 #if GLYPH_DEBUG
2474
2475 /* Check that the window end of window W is what we expect it
2476 to be---the last row in the current matrix displaying text. */
2477
2478 static void
2479 check_window_end (w)
2480 struct window *w;
2481 {
2482 if (!MINI_WINDOW_P (w)
2483 && !NILP (w->window_end_valid))
2484 {
2485 struct glyph_row *row;
2486 xassert ((row = MATRIX_ROW (w->current_matrix,
2487 XFASTINT (w->window_end_vpos)),
2488 !row->enabled_p
2489 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2490 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2491 }
2492 }
2493
2494 #define CHECK_WINDOW_END(W) check_window_end ((W))
2495
2496 #else /* not GLYPH_DEBUG */
2497
2498 #define CHECK_WINDOW_END(W) (void) 0
2499
2500 #endif /* not GLYPH_DEBUG */
2501
2502
2503 \f
2504 /***********************************************************************
2505 Iterator initialization
2506 ***********************************************************************/
2507
2508 /* Initialize IT for displaying current_buffer in window W, starting
2509 at character position CHARPOS. CHARPOS < 0 means that no buffer
2510 position is specified which is useful when the iterator is assigned
2511 a position later. BYTEPOS is the byte position corresponding to
2512 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2513
2514 If ROW is not null, calls to produce_glyphs with IT as parameter
2515 will produce glyphs in that row.
2516
2517 BASE_FACE_ID is the id of a base face to use. It must be one of
2518 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2519 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2520 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2521
2522 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2523 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2524 will be initialized to use the corresponding mode line glyph row of
2525 the desired matrix of W. */
2526
2527 void
2528 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2529 struct it *it;
2530 struct window *w;
2531 int charpos, bytepos;
2532 struct glyph_row *row;
2533 enum face_id base_face_id;
2534 {
2535 int highlight_region_p;
2536 enum face_id remapped_base_face_id = base_face_id;
2537
2538 /* Some precondition checks. */
2539 xassert (w != NULL && it != NULL);
2540 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2541 && charpos <= ZV));
2542
2543 /* If face attributes have been changed since the last redisplay,
2544 free realized faces now because they depend on face definitions
2545 that might have changed. Don't free faces while there might be
2546 desired matrices pending which reference these faces. */
2547 if (face_change_count && !inhibit_free_realized_faces)
2548 {
2549 face_change_count = 0;
2550 free_all_realized_faces (Qnil);
2551 }
2552
2553 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2554 if (! NILP (Vface_remapping_alist))
2555 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2556
2557 /* Use one of the mode line rows of W's desired matrix if
2558 appropriate. */
2559 if (row == NULL)
2560 {
2561 if (base_face_id == MODE_LINE_FACE_ID
2562 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2563 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2564 else if (base_face_id == HEADER_LINE_FACE_ID)
2565 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2566 }
2567
2568 /* Clear IT. */
2569 bzero (it, sizeof *it);
2570 it->current.overlay_string_index = -1;
2571 it->current.dpvec_index = -1;
2572 it->base_face_id = remapped_base_face_id;
2573 it->string = Qnil;
2574 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2575
2576 /* The window in which we iterate over current_buffer: */
2577 XSETWINDOW (it->window, w);
2578 it->w = w;
2579 it->f = XFRAME (w->frame);
2580
2581 it->cmp_it.id = -1;
2582
2583 /* Extra space between lines (on window systems only). */
2584 if (base_face_id == DEFAULT_FACE_ID
2585 && FRAME_WINDOW_P (it->f))
2586 {
2587 if (NATNUMP (current_buffer->extra_line_spacing))
2588 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2589 else if (FLOATP (current_buffer->extra_line_spacing))
2590 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2591 * FRAME_LINE_HEIGHT (it->f));
2592 else if (it->f->extra_line_spacing > 0)
2593 it->extra_line_spacing = it->f->extra_line_spacing;
2594 it->max_extra_line_spacing = 0;
2595 }
2596
2597 /* If realized faces have been removed, e.g. because of face
2598 attribute changes of named faces, recompute them. When running
2599 in batch mode, the face cache of the initial frame is null. If
2600 we happen to get called, make a dummy face cache. */
2601 if (FRAME_FACE_CACHE (it->f) == NULL)
2602 init_frame_faces (it->f);
2603 if (FRAME_FACE_CACHE (it->f)->used == 0)
2604 recompute_basic_faces (it->f);
2605
2606 /* Current value of the `slice', `space-width', and 'height' properties. */
2607 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2608 it->space_width = Qnil;
2609 it->font_height = Qnil;
2610 it->override_ascent = -1;
2611
2612 /* Are control characters displayed as `^C'? */
2613 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2614
2615 /* -1 means everything between a CR and the following line end
2616 is invisible. >0 means lines indented more than this value are
2617 invisible. */
2618 it->selective = (INTEGERP (current_buffer->selective_display)
2619 ? XFASTINT (current_buffer->selective_display)
2620 : (!NILP (current_buffer->selective_display)
2621 ? -1 : 0));
2622 it->selective_display_ellipsis_p
2623 = !NILP (current_buffer->selective_display_ellipses);
2624
2625 /* Display table to use. */
2626 it->dp = window_display_table (w);
2627
2628 /* Are multibyte characters enabled in current_buffer? */
2629 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2630
2631 /* Non-zero if we should highlight the region. */
2632 highlight_region_p
2633 = (!NILP (Vtransient_mark_mode)
2634 && !NILP (current_buffer->mark_active)
2635 && XMARKER (current_buffer->mark)->buffer != 0);
2636
2637 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2638 start and end of a visible region in window IT->w. Set both to
2639 -1 to indicate no region. */
2640 if (highlight_region_p
2641 /* Maybe highlight only in selected window. */
2642 && (/* Either show region everywhere. */
2643 highlight_nonselected_windows
2644 /* Or show region in the selected window. */
2645 || w == XWINDOW (selected_window)
2646 /* Or show the region if we are in the mini-buffer and W is
2647 the window the mini-buffer refers to. */
2648 || (MINI_WINDOW_P (XWINDOW (selected_window))
2649 && WINDOWP (minibuf_selected_window)
2650 && w == XWINDOW (minibuf_selected_window))))
2651 {
2652 int charpos = marker_position (current_buffer->mark);
2653 it->region_beg_charpos = min (PT, charpos);
2654 it->region_end_charpos = max (PT, charpos);
2655 }
2656 else
2657 it->region_beg_charpos = it->region_end_charpos = -1;
2658
2659 /* Get the position at which the redisplay_end_trigger hook should
2660 be run, if it is to be run at all. */
2661 if (MARKERP (w->redisplay_end_trigger)
2662 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2663 it->redisplay_end_trigger_charpos
2664 = marker_position (w->redisplay_end_trigger);
2665 else if (INTEGERP (w->redisplay_end_trigger))
2666 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2667
2668 /* Correct bogus values of tab_width. */
2669 it->tab_width = XINT (current_buffer->tab_width);
2670 if (it->tab_width <= 0 || it->tab_width > 1000)
2671 it->tab_width = 8;
2672
2673 /* Are lines in the display truncated? */
2674 if (base_face_id != DEFAULT_FACE_ID
2675 || XINT (it->w->hscroll)
2676 || (! WINDOW_FULL_WIDTH_P (it->w)
2677 && ((!NILP (Vtruncate_partial_width_windows)
2678 && !INTEGERP (Vtruncate_partial_width_windows))
2679 || (INTEGERP (Vtruncate_partial_width_windows)
2680 && (WINDOW_TOTAL_COLS (it->w)
2681 < XINT (Vtruncate_partial_width_windows))))))
2682 it->line_wrap = TRUNCATE;
2683 else if (NILP (current_buffer->truncate_lines))
2684 it->line_wrap = NILP (current_buffer->word_wrap)
2685 ? WINDOW_WRAP : WORD_WRAP;
2686 else
2687 it->line_wrap = TRUNCATE;
2688
2689 /* Get dimensions of truncation and continuation glyphs. These are
2690 displayed as fringe bitmaps under X, so we don't need them for such
2691 frames. */
2692 if (!FRAME_WINDOW_P (it->f))
2693 {
2694 if (it->line_wrap == TRUNCATE)
2695 {
2696 /* We will need the truncation glyph. */
2697 xassert (it->glyph_row == NULL);
2698 produce_special_glyphs (it, IT_TRUNCATION);
2699 it->truncation_pixel_width = it->pixel_width;
2700 }
2701 else
2702 {
2703 /* We will need the continuation glyph. */
2704 xassert (it->glyph_row == NULL);
2705 produce_special_glyphs (it, IT_CONTINUATION);
2706 it->continuation_pixel_width = it->pixel_width;
2707 }
2708
2709 /* Reset these values to zero because the produce_special_glyphs
2710 above has changed them. */
2711 it->pixel_width = it->ascent = it->descent = 0;
2712 it->phys_ascent = it->phys_descent = 0;
2713 }
2714
2715 /* Set this after getting the dimensions of truncation and
2716 continuation glyphs, so that we don't produce glyphs when calling
2717 produce_special_glyphs, above. */
2718 it->glyph_row = row;
2719 it->area = TEXT_AREA;
2720
2721 /* Get the dimensions of the display area. The display area
2722 consists of the visible window area plus a horizontally scrolled
2723 part to the left of the window. All x-values are relative to the
2724 start of this total display area. */
2725 if (base_face_id != DEFAULT_FACE_ID)
2726 {
2727 /* Mode lines, menu bar in terminal frames. */
2728 it->first_visible_x = 0;
2729 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2730 }
2731 else
2732 {
2733 it->first_visible_x
2734 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2735 it->last_visible_x = (it->first_visible_x
2736 + window_box_width (w, TEXT_AREA));
2737
2738 /* If we truncate lines, leave room for the truncator glyph(s) at
2739 the right margin. Otherwise, leave room for the continuation
2740 glyph(s). Truncation and continuation glyphs are not inserted
2741 for window-based redisplay. */
2742 if (!FRAME_WINDOW_P (it->f))
2743 {
2744 if (it->line_wrap == TRUNCATE)
2745 it->last_visible_x -= it->truncation_pixel_width;
2746 else
2747 it->last_visible_x -= it->continuation_pixel_width;
2748 }
2749
2750 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2751 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2752 }
2753
2754 /* Leave room for a border glyph. */
2755 if (!FRAME_WINDOW_P (it->f)
2756 && !WINDOW_RIGHTMOST_P (it->w))
2757 it->last_visible_x -= 1;
2758
2759 it->last_visible_y = window_text_bottom_y (w);
2760
2761 /* For mode lines and alike, arrange for the first glyph having a
2762 left box line if the face specifies a box. */
2763 if (base_face_id != DEFAULT_FACE_ID)
2764 {
2765 struct face *face;
2766
2767 it->face_id = remapped_base_face_id;
2768
2769 /* If we have a boxed mode line, make the first character appear
2770 with a left box line. */
2771 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2772 if (face->box != FACE_NO_BOX)
2773 it->start_of_box_run_p = 1;
2774 }
2775
2776 /* If a buffer position was specified, set the iterator there,
2777 getting overlays and face properties from that position. */
2778 if (charpos >= BUF_BEG (current_buffer))
2779 {
2780 it->end_charpos = ZV;
2781 it->face_id = -1;
2782 IT_CHARPOS (*it) = charpos;
2783
2784 /* Compute byte position if not specified. */
2785 if (bytepos < charpos)
2786 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2787 else
2788 IT_BYTEPOS (*it) = bytepos;
2789
2790 it->start = it->current;
2791
2792 /* Compute faces etc. */
2793 reseat (it, it->current.pos, 1);
2794 }
2795
2796 CHECK_IT (it);
2797 }
2798
2799
2800 /* Initialize IT for the display of window W with window start POS. */
2801
2802 void
2803 start_display (it, w, pos)
2804 struct it *it;
2805 struct window *w;
2806 struct text_pos pos;
2807 {
2808 struct glyph_row *row;
2809 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2810
2811 row = w->desired_matrix->rows + first_vpos;
2812 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2813 it->first_vpos = first_vpos;
2814
2815 /* Don't reseat to previous visible line start if current start
2816 position is in a string or image. */
2817 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2818 {
2819 int start_at_line_beg_p;
2820 int first_y = it->current_y;
2821
2822 /* If window start is not at a line start, skip forward to POS to
2823 get the correct continuation lines width. */
2824 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2825 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2826 if (!start_at_line_beg_p)
2827 {
2828 int new_x;
2829
2830 reseat_at_previous_visible_line_start (it);
2831 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2832
2833 new_x = it->current_x + it->pixel_width;
2834
2835 /* If lines are continued, this line may end in the middle
2836 of a multi-glyph character (e.g. a control character
2837 displayed as \003, or in the middle of an overlay
2838 string). In this case move_it_to above will not have
2839 taken us to the start of the continuation line but to the
2840 end of the continued line. */
2841 if (it->current_x > 0
2842 && it->line_wrap != TRUNCATE /* Lines are continued. */
2843 && (/* And glyph doesn't fit on the line. */
2844 new_x > it->last_visible_x
2845 /* Or it fits exactly and we're on a window
2846 system frame. */
2847 || (new_x == it->last_visible_x
2848 && FRAME_WINDOW_P (it->f))))
2849 {
2850 if (it->current.dpvec_index >= 0
2851 || it->current.overlay_string_index >= 0)
2852 {
2853 set_iterator_to_next (it, 1);
2854 move_it_in_display_line_to (it, -1, -1, 0);
2855 }
2856
2857 it->continuation_lines_width += it->current_x;
2858 }
2859
2860 /* We're starting a new display line, not affected by the
2861 height of the continued line, so clear the appropriate
2862 fields in the iterator structure. */
2863 it->max_ascent = it->max_descent = 0;
2864 it->max_phys_ascent = it->max_phys_descent = 0;
2865
2866 it->current_y = first_y;
2867 it->vpos = 0;
2868 it->current_x = it->hpos = 0;
2869 }
2870 }
2871 }
2872
2873
2874 /* Return 1 if POS is a position in ellipses displayed for invisible
2875 text. W is the window we display, for text property lookup. */
2876
2877 static int
2878 in_ellipses_for_invisible_text_p (pos, w)
2879 struct display_pos *pos;
2880 struct window *w;
2881 {
2882 Lisp_Object prop, window;
2883 int ellipses_p = 0;
2884 int charpos = CHARPOS (pos->pos);
2885
2886 /* If POS specifies a position in a display vector, this might
2887 be for an ellipsis displayed for invisible text. We won't
2888 get the iterator set up for delivering that ellipsis unless
2889 we make sure that it gets aware of the invisible text. */
2890 if (pos->dpvec_index >= 0
2891 && pos->overlay_string_index < 0
2892 && CHARPOS (pos->string_pos) < 0
2893 && charpos > BEGV
2894 && (XSETWINDOW (window, w),
2895 prop = Fget_char_property (make_number (charpos),
2896 Qinvisible, window),
2897 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2898 {
2899 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2900 window);
2901 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2902 }
2903
2904 return ellipses_p;
2905 }
2906
2907
2908 /* Initialize IT for stepping through current_buffer in window W,
2909 starting at position POS that includes overlay string and display
2910 vector/ control character translation position information. Value
2911 is zero if there are overlay strings with newlines at POS. */
2912
2913 static int
2914 init_from_display_pos (it, w, pos)
2915 struct it *it;
2916 struct window *w;
2917 struct display_pos *pos;
2918 {
2919 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2920 int i, overlay_strings_with_newlines = 0;
2921
2922 /* If POS specifies a position in a display vector, this might
2923 be for an ellipsis displayed for invisible text. We won't
2924 get the iterator set up for delivering that ellipsis unless
2925 we make sure that it gets aware of the invisible text. */
2926 if (in_ellipses_for_invisible_text_p (pos, w))
2927 {
2928 --charpos;
2929 bytepos = 0;
2930 }
2931
2932 /* Keep in mind: the call to reseat in init_iterator skips invisible
2933 text, so we might end up at a position different from POS. This
2934 is only a problem when POS is a row start after a newline and an
2935 overlay starts there with an after-string, and the overlay has an
2936 invisible property. Since we don't skip invisible text in
2937 display_line and elsewhere immediately after consuming the
2938 newline before the row start, such a POS will not be in a string,
2939 but the call to init_iterator below will move us to the
2940 after-string. */
2941 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2942
2943 /* This only scans the current chunk -- it should scan all chunks.
2944 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2945 to 16 in 22.1 to make this a lesser problem. */
2946 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2947 {
2948 const char *s = SDATA (it->overlay_strings[i]);
2949 const char *e = s + SBYTES (it->overlay_strings[i]);
2950
2951 while (s < e && *s != '\n')
2952 ++s;
2953
2954 if (s < e)
2955 {
2956 overlay_strings_with_newlines = 1;
2957 break;
2958 }
2959 }
2960
2961 /* If position is within an overlay string, set up IT to the right
2962 overlay string. */
2963 if (pos->overlay_string_index >= 0)
2964 {
2965 int relative_index;
2966
2967 /* If the first overlay string happens to have a `display'
2968 property for an image, the iterator will be set up for that
2969 image, and we have to undo that setup first before we can
2970 correct the overlay string index. */
2971 if (it->method == GET_FROM_IMAGE)
2972 pop_it (it);
2973
2974 /* We already have the first chunk of overlay strings in
2975 IT->overlay_strings. Load more until the one for
2976 pos->overlay_string_index is in IT->overlay_strings. */
2977 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2978 {
2979 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2980 it->current.overlay_string_index = 0;
2981 while (n--)
2982 {
2983 load_overlay_strings (it, 0);
2984 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2985 }
2986 }
2987
2988 it->current.overlay_string_index = pos->overlay_string_index;
2989 relative_index = (it->current.overlay_string_index
2990 % OVERLAY_STRING_CHUNK_SIZE);
2991 it->string = it->overlay_strings[relative_index];
2992 xassert (STRINGP (it->string));
2993 it->current.string_pos = pos->string_pos;
2994 it->method = GET_FROM_STRING;
2995 }
2996
2997 if (CHARPOS (pos->string_pos) >= 0)
2998 {
2999 /* Recorded position is not in an overlay string, but in another
3000 string. This can only be a string from a `display' property.
3001 IT should already be filled with that string. */
3002 it->current.string_pos = pos->string_pos;
3003 xassert (STRINGP (it->string));
3004 }
3005
3006 /* Restore position in display vector translations, control
3007 character translations or ellipses. */
3008 if (pos->dpvec_index >= 0)
3009 {
3010 if (it->dpvec == NULL)
3011 get_next_display_element (it);
3012 xassert (it->dpvec && it->current.dpvec_index == 0);
3013 it->current.dpvec_index = pos->dpvec_index;
3014 }
3015
3016 CHECK_IT (it);
3017 return !overlay_strings_with_newlines;
3018 }
3019
3020
3021 /* Initialize IT for stepping through current_buffer in window W
3022 starting at ROW->start. */
3023
3024 static void
3025 init_to_row_start (it, w, row)
3026 struct it *it;
3027 struct window *w;
3028 struct glyph_row *row;
3029 {
3030 init_from_display_pos (it, w, &row->start);
3031 it->start = row->start;
3032 it->continuation_lines_width = row->continuation_lines_width;
3033 CHECK_IT (it);
3034 }
3035
3036
3037 /* Initialize IT for stepping through current_buffer in window W
3038 starting in the line following ROW, i.e. starting at ROW->end.
3039 Value is zero if there are overlay strings with newlines at ROW's
3040 end position. */
3041
3042 static int
3043 init_to_row_end (it, w, row)
3044 struct it *it;
3045 struct window *w;
3046 struct glyph_row *row;
3047 {
3048 int success = 0;
3049
3050 if (init_from_display_pos (it, w, &row->end))
3051 {
3052 if (row->continued_p)
3053 it->continuation_lines_width
3054 = row->continuation_lines_width + row->pixel_width;
3055 CHECK_IT (it);
3056 success = 1;
3057 }
3058
3059 return success;
3060 }
3061
3062
3063
3064 \f
3065 /***********************************************************************
3066 Text properties
3067 ***********************************************************************/
3068
3069 /* Called when IT reaches IT->stop_charpos. Handle text property and
3070 overlay changes. Set IT->stop_charpos to the next position where
3071 to stop. */
3072
3073 static void
3074 handle_stop (it)
3075 struct it *it;
3076 {
3077 enum prop_handled handled;
3078 int handle_overlay_change_p;
3079 struct props *p;
3080
3081 it->dpvec = NULL;
3082 it->current.dpvec_index = -1;
3083 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3084 it->ignore_overlay_strings_at_pos_p = 0;
3085 it->ellipsis_p = 0;
3086
3087 /* Use face of preceding text for ellipsis (if invisible) */
3088 if (it->selective_display_ellipsis_p)
3089 it->saved_face_id = it->face_id;
3090
3091 do
3092 {
3093 handled = HANDLED_NORMALLY;
3094
3095 /* Call text property handlers. */
3096 for (p = it_props; p->handler; ++p)
3097 {
3098 handled = p->handler (it);
3099
3100 if (handled == HANDLED_RECOMPUTE_PROPS)
3101 break;
3102 else if (handled == HANDLED_RETURN)
3103 {
3104 /* We still want to show before and after strings from
3105 overlays even if the actual buffer text is replaced. */
3106 if (!handle_overlay_change_p
3107 || it->sp > 1
3108 || !get_overlay_strings_1 (it, 0, 0))
3109 {
3110 if (it->ellipsis_p)
3111 setup_for_ellipsis (it, 0);
3112 /* When handling a display spec, we might load an
3113 empty string. In that case, discard it here. We
3114 used to discard it in handle_single_display_spec,
3115 but that causes get_overlay_strings_1, above, to
3116 ignore overlay strings that we must check. */
3117 if (STRINGP (it->string) && !SCHARS (it->string))
3118 pop_it (it);
3119 return;
3120 }
3121 else if (STRINGP (it->string) && !SCHARS (it->string))
3122 pop_it (it);
3123 else
3124 {
3125 it->ignore_overlay_strings_at_pos_p = 1;
3126 it->string_from_display_prop_p = 0;
3127 handle_overlay_change_p = 0;
3128 }
3129 handled = HANDLED_RECOMPUTE_PROPS;
3130 break;
3131 }
3132 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3133 handle_overlay_change_p = 0;
3134 }
3135
3136 if (handled != HANDLED_RECOMPUTE_PROPS)
3137 {
3138 /* Don't check for overlay strings below when set to deliver
3139 characters from a display vector. */
3140 if (it->method == GET_FROM_DISPLAY_VECTOR)
3141 handle_overlay_change_p = 0;
3142
3143 /* Handle overlay changes.
3144 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3145 if it finds overlays. */
3146 if (handle_overlay_change_p)
3147 handled = handle_overlay_change (it);
3148 }
3149
3150 if (it->ellipsis_p)
3151 {
3152 setup_for_ellipsis (it, 0);
3153 break;
3154 }
3155 }
3156 while (handled == HANDLED_RECOMPUTE_PROPS);
3157
3158 /* Determine where to stop next. */
3159 if (handled == HANDLED_NORMALLY)
3160 compute_stop_pos (it);
3161 }
3162
3163
3164 /* Compute IT->stop_charpos from text property and overlay change
3165 information for IT's current position. */
3166
3167 static void
3168 compute_stop_pos (it)
3169 struct it *it;
3170 {
3171 register INTERVAL iv, next_iv;
3172 Lisp_Object object, limit, position;
3173 EMACS_INT charpos, bytepos;
3174
3175 /* If nowhere else, stop at the end. */
3176 it->stop_charpos = it->end_charpos;
3177
3178 if (STRINGP (it->string))
3179 {
3180 /* Strings are usually short, so don't limit the search for
3181 properties. */
3182 object = it->string;
3183 limit = Qnil;
3184 charpos = IT_STRING_CHARPOS (*it);
3185 bytepos = IT_STRING_BYTEPOS (*it);
3186 }
3187 else
3188 {
3189 EMACS_INT pos;
3190
3191 /* If next overlay change is in front of the current stop pos
3192 (which is IT->end_charpos), stop there. Note: value of
3193 next_overlay_change is point-max if no overlay change
3194 follows. */
3195 charpos = IT_CHARPOS (*it);
3196 bytepos = IT_BYTEPOS (*it);
3197 pos = next_overlay_change (charpos);
3198 if (pos < it->stop_charpos)
3199 it->stop_charpos = pos;
3200
3201 /* If showing the region, we have to stop at the region
3202 start or end because the face might change there. */
3203 if (it->region_beg_charpos > 0)
3204 {
3205 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3206 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3207 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3208 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3209 }
3210
3211 /* Set up variables for computing the stop position from text
3212 property changes. */
3213 XSETBUFFER (object, current_buffer);
3214 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3215 }
3216
3217 /* Get the interval containing IT's position. Value is a null
3218 interval if there isn't such an interval. */
3219 position = make_number (charpos);
3220 iv = validate_interval_range (object, &position, &position, 0);
3221 if (!NULL_INTERVAL_P (iv))
3222 {
3223 Lisp_Object values_here[LAST_PROP_IDX];
3224 struct props *p;
3225
3226 /* Get properties here. */
3227 for (p = it_props; p->handler; ++p)
3228 values_here[p->idx] = textget (iv->plist, *p->name);
3229
3230 /* Look for an interval following iv that has different
3231 properties. */
3232 for (next_iv = next_interval (iv);
3233 (!NULL_INTERVAL_P (next_iv)
3234 && (NILP (limit)
3235 || XFASTINT (limit) > next_iv->position));
3236 next_iv = next_interval (next_iv))
3237 {
3238 for (p = it_props; p->handler; ++p)
3239 {
3240 Lisp_Object new_value;
3241
3242 new_value = textget (next_iv->plist, *p->name);
3243 if (!EQ (values_here[p->idx], new_value))
3244 break;
3245 }
3246
3247 if (p->handler)
3248 break;
3249 }
3250
3251 if (!NULL_INTERVAL_P (next_iv))
3252 {
3253 if (INTEGERP (limit)
3254 && next_iv->position >= XFASTINT (limit))
3255 /* No text property change up to limit. */
3256 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3257 else
3258 /* Text properties change in next_iv. */
3259 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3260 }
3261 }
3262
3263 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3264 it->stop_charpos, it->string);
3265
3266 xassert (STRINGP (it->string)
3267 || (it->stop_charpos >= BEGV
3268 && it->stop_charpos >= IT_CHARPOS (*it)));
3269 }
3270
3271
3272 /* Return the position of the next overlay change after POS in
3273 current_buffer. Value is point-max if no overlay change
3274 follows. This is like `next-overlay-change' but doesn't use
3275 xmalloc. */
3276
3277 static EMACS_INT
3278 next_overlay_change (pos)
3279 EMACS_INT pos;
3280 {
3281 int noverlays;
3282 EMACS_INT endpos;
3283 Lisp_Object *overlays;
3284 int i;
3285
3286 /* Get all overlays at the given position. */
3287 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3288
3289 /* If any of these overlays ends before endpos,
3290 use its ending point instead. */
3291 for (i = 0; i < noverlays; ++i)
3292 {
3293 Lisp_Object oend;
3294 EMACS_INT oendpos;
3295
3296 oend = OVERLAY_END (overlays[i]);
3297 oendpos = OVERLAY_POSITION (oend);
3298 endpos = min (endpos, oendpos);
3299 }
3300
3301 return endpos;
3302 }
3303
3304
3305 \f
3306 /***********************************************************************
3307 Fontification
3308 ***********************************************************************/
3309
3310 /* Handle changes in the `fontified' property of the current buffer by
3311 calling hook functions from Qfontification_functions to fontify
3312 regions of text. */
3313
3314 static enum prop_handled
3315 handle_fontified_prop (it)
3316 struct it *it;
3317 {
3318 Lisp_Object prop, pos;
3319 enum prop_handled handled = HANDLED_NORMALLY;
3320
3321 if (!NILP (Vmemory_full))
3322 return handled;
3323
3324 /* Get the value of the `fontified' property at IT's current buffer
3325 position. (The `fontified' property doesn't have a special
3326 meaning in strings.) If the value is nil, call functions from
3327 Qfontification_functions. */
3328 if (!STRINGP (it->string)
3329 && it->s == NULL
3330 && !NILP (Vfontification_functions)
3331 && !NILP (Vrun_hooks)
3332 && (pos = make_number (IT_CHARPOS (*it)),
3333 prop = Fget_char_property (pos, Qfontified, Qnil),
3334 /* Ignore the special cased nil value always present at EOB since
3335 no amount of fontifying will be able to change it. */
3336 NILP (prop) && IT_CHARPOS (*it) < Z))
3337 {
3338 int count = SPECPDL_INDEX ();
3339 Lisp_Object val;
3340
3341 val = Vfontification_functions;
3342 specbind (Qfontification_functions, Qnil);
3343
3344 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3345 safe_call1 (val, pos);
3346 else
3347 {
3348 Lisp_Object globals, fn;
3349 struct gcpro gcpro1, gcpro2;
3350
3351 globals = Qnil;
3352 GCPRO2 (val, globals);
3353
3354 for (; CONSP (val); val = XCDR (val))
3355 {
3356 fn = XCAR (val);
3357
3358 if (EQ (fn, Qt))
3359 {
3360 /* A value of t indicates this hook has a local
3361 binding; it means to run the global binding too.
3362 In a global value, t should not occur. If it
3363 does, we must ignore it to avoid an endless
3364 loop. */
3365 for (globals = Fdefault_value (Qfontification_functions);
3366 CONSP (globals);
3367 globals = XCDR (globals))
3368 {
3369 fn = XCAR (globals);
3370 if (!EQ (fn, Qt))
3371 safe_call1 (fn, pos);
3372 }
3373 }
3374 else
3375 safe_call1 (fn, pos);
3376 }
3377
3378 UNGCPRO;
3379 }
3380
3381 unbind_to (count, Qnil);
3382
3383 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3384 something. This avoids an endless loop if they failed to
3385 fontify the text for which reason ever. */
3386 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3387 handled = HANDLED_RECOMPUTE_PROPS;
3388 }
3389
3390 return handled;
3391 }
3392
3393
3394 \f
3395 /***********************************************************************
3396 Faces
3397 ***********************************************************************/
3398
3399 /* Set up iterator IT from face properties at its current position.
3400 Called from handle_stop. */
3401
3402 static enum prop_handled
3403 handle_face_prop (it)
3404 struct it *it;
3405 {
3406 int new_face_id;
3407 EMACS_INT next_stop;
3408
3409 if (!STRINGP (it->string))
3410 {
3411 new_face_id
3412 = face_at_buffer_position (it->w,
3413 IT_CHARPOS (*it),
3414 it->region_beg_charpos,
3415 it->region_end_charpos,
3416 &next_stop,
3417 (IT_CHARPOS (*it)
3418 + TEXT_PROP_DISTANCE_LIMIT),
3419 0, it->base_face_id);
3420
3421 /* Is this a start of a run of characters with box face?
3422 Caveat: this can be called for a freshly initialized
3423 iterator; face_id is -1 in this case. We know that the new
3424 face will not change until limit, i.e. if the new face has a
3425 box, all characters up to limit will have one. But, as
3426 usual, we don't know whether limit is really the end. */
3427 if (new_face_id != it->face_id)
3428 {
3429 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3430
3431 /* If new face has a box but old face has not, this is
3432 the start of a run of characters with box, i.e. it has
3433 a shadow on the left side. The value of face_id of the
3434 iterator will be -1 if this is the initial call that gets
3435 the face. In this case, we have to look in front of IT's
3436 position and see whether there is a face != new_face_id. */
3437 it->start_of_box_run_p
3438 = (new_face->box != FACE_NO_BOX
3439 && (it->face_id >= 0
3440 || IT_CHARPOS (*it) == BEG
3441 || new_face_id != face_before_it_pos (it)));
3442 it->face_box_p = new_face->box != FACE_NO_BOX;
3443 }
3444 }
3445 else
3446 {
3447 int base_face_id, bufpos;
3448 int i;
3449 Lisp_Object from_overlay
3450 = (it->current.overlay_string_index >= 0
3451 ? it->string_overlays[it->current.overlay_string_index]
3452 : Qnil);
3453
3454 /* See if we got to this string directly or indirectly from
3455 an overlay property. That includes the before-string or
3456 after-string of an overlay, strings in display properties
3457 provided by an overlay, their text properties, etc.
3458
3459 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3460 if (! NILP (from_overlay))
3461 for (i = it->sp - 1; i >= 0; i--)
3462 {
3463 if (it->stack[i].current.overlay_string_index >= 0)
3464 from_overlay
3465 = it->string_overlays[it->stack[i].current.overlay_string_index];
3466 else if (! NILP (it->stack[i].from_overlay))
3467 from_overlay = it->stack[i].from_overlay;
3468
3469 if (!NILP (from_overlay))
3470 break;
3471 }
3472
3473 if (! NILP (from_overlay))
3474 {
3475 bufpos = IT_CHARPOS (*it);
3476 /* For a string from an overlay, the base face depends
3477 only on text properties and ignores overlays. */
3478 base_face_id
3479 = face_for_overlay_string (it->w,
3480 IT_CHARPOS (*it),
3481 it->region_beg_charpos,
3482 it->region_end_charpos,
3483 &next_stop,
3484 (IT_CHARPOS (*it)
3485 + TEXT_PROP_DISTANCE_LIMIT),
3486 0,
3487 from_overlay);
3488 }
3489 else
3490 {
3491 bufpos = 0;
3492
3493 /* For strings from a `display' property, use the face at
3494 IT's current buffer position as the base face to merge
3495 with, so that overlay strings appear in the same face as
3496 surrounding text, unless they specify their own
3497 faces. */
3498 base_face_id = underlying_face_id (it);
3499 }
3500
3501 new_face_id = face_at_string_position (it->w,
3502 it->string,
3503 IT_STRING_CHARPOS (*it),
3504 bufpos,
3505 it->region_beg_charpos,
3506 it->region_end_charpos,
3507 &next_stop,
3508 base_face_id, 0);
3509
3510 /* Is this a start of a run of characters with box? Caveat:
3511 this can be called for a freshly allocated iterator; face_id
3512 is -1 is this case. We know that the new face will not
3513 change until the next check pos, i.e. if the new face has a
3514 box, all characters up to that position will have a
3515 box. But, as usual, we don't know whether that position
3516 is really the end. */
3517 if (new_face_id != it->face_id)
3518 {
3519 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3520 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3521
3522 /* If new face has a box but old face hasn't, this is the
3523 start of a run of characters with box, i.e. it has a
3524 shadow on the left side. */
3525 it->start_of_box_run_p
3526 = new_face->box && (old_face == NULL || !old_face->box);
3527 it->face_box_p = new_face->box != FACE_NO_BOX;
3528 }
3529 }
3530
3531 it->face_id = new_face_id;
3532 return HANDLED_NORMALLY;
3533 }
3534
3535
3536 /* Return the ID of the face ``underlying'' IT's current position,
3537 which is in a string. If the iterator is associated with a
3538 buffer, return the face at IT's current buffer position.
3539 Otherwise, use the iterator's base_face_id. */
3540
3541 static int
3542 underlying_face_id (it)
3543 struct it *it;
3544 {
3545 int face_id = it->base_face_id, i;
3546
3547 xassert (STRINGP (it->string));
3548
3549 for (i = it->sp - 1; i >= 0; --i)
3550 if (NILP (it->stack[i].string))
3551 face_id = it->stack[i].face_id;
3552
3553 return face_id;
3554 }
3555
3556
3557 /* Compute the face one character before or after the current position
3558 of IT. BEFORE_P non-zero means get the face in front of IT's
3559 position. Value is the id of the face. */
3560
3561 static int
3562 face_before_or_after_it_pos (it, before_p)
3563 struct it *it;
3564 int before_p;
3565 {
3566 int face_id, limit;
3567 EMACS_INT next_check_charpos;
3568 struct text_pos pos;
3569
3570 xassert (it->s == NULL);
3571
3572 if (STRINGP (it->string))
3573 {
3574 int bufpos, base_face_id;
3575
3576 /* No face change past the end of the string (for the case
3577 we are padding with spaces). No face change before the
3578 string start. */
3579 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3580 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3581 return it->face_id;
3582
3583 /* Set pos to the position before or after IT's current position. */
3584 if (before_p)
3585 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3586 else
3587 /* For composition, we must check the character after the
3588 composition. */
3589 pos = (it->what == IT_COMPOSITION
3590 ? string_pos (IT_STRING_CHARPOS (*it)
3591 + it->cmp_it.nchars, it->string)
3592 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3593
3594 if (it->current.overlay_string_index >= 0)
3595 bufpos = IT_CHARPOS (*it);
3596 else
3597 bufpos = 0;
3598
3599 base_face_id = underlying_face_id (it);
3600
3601 /* Get the face for ASCII, or unibyte. */
3602 face_id = face_at_string_position (it->w,
3603 it->string,
3604 CHARPOS (pos),
3605 bufpos,
3606 it->region_beg_charpos,
3607 it->region_end_charpos,
3608 &next_check_charpos,
3609 base_face_id, 0);
3610
3611 /* Correct the face for charsets different from ASCII. Do it
3612 for the multibyte case only. The face returned above is
3613 suitable for unibyte text if IT->string is unibyte. */
3614 if (STRING_MULTIBYTE (it->string))
3615 {
3616 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3617 int rest = SBYTES (it->string) - BYTEPOS (pos);
3618 int c, len;
3619 struct face *face = FACE_FROM_ID (it->f, face_id);
3620
3621 c = string_char_and_length (p, &len);
3622 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3623 }
3624 }
3625 else
3626 {
3627 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3628 || (IT_CHARPOS (*it) <= BEGV && before_p))
3629 return it->face_id;
3630
3631 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3632 pos = it->current.pos;
3633
3634 if (before_p)
3635 DEC_TEXT_POS (pos, it->multibyte_p);
3636 else
3637 {
3638 if (it->what == IT_COMPOSITION)
3639 /* For composition, we must check the position after the
3640 composition. */
3641 pos.charpos += it->cmp_it.nchars, pos.bytepos += it->len;
3642 else
3643 INC_TEXT_POS (pos, it->multibyte_p);
3644 }
3645
3646 /* Determine face for CHARSET_ASCII, or unibyte. */
3647 face_id = face_at_buffer_position (it->w,
3648 CHARPOS (pos),
3649 it->region_beg_charpos,
3650 it->region_end_charpos,
3651 &next_check_charpos,
3652 limit, 0, -1);
3653
3654 /* Correct the face for charsets different from ASCII. Do it
3655 for the multibyte case only. The face returned above is
3656 suitable for unibyte text if current_buffer is unibyte. */
3657 if (it->multibyte_p)
3658 {
3659 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3660 struct face *face = FACE_FROM_ID (it->f, face_id);
3661 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3662 }
3663 }
3664
3665 return face_id;
3666 }
3667
3668
3669 \f
3670 /***********************************************************************
3671 Invisible text
3672 ***********************************************************************/
3673
3674 /* Set up iterator IT from invisible properties at its current
3675 position. Called from handle_stop. */
3676
3677 static enum prop_handled
3678 handle_invisible_prop (it)
3679 struct it *it;
3680 {
3681 enum prop_handled handled = HANDLED_NORMALLY;
3682
3683 if (STRINGP (it->string))
3684 {
3685 extern Lisp_Object Qinvisible;
3686 Lisp_Object prop, end_charpos, limit, charpos;
3687
3688 /* Get the value of the invisible text property at the
3689 current position. Value will be nil if there is no such
3690 property. */
3691 charpos = make_number (IT_STRING_CHARPOS (*it));
3692 prop = Fget_text_property (charpos, Qinvisible, it->string);
3693
3694 if (!NILP (prop)
3695 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3696 {
3697 handled = HANDLED_RECOMPUTE_PROPS;
3698
3699 /* Get the position at which the next change of the
3700 invisible text property can be found in IT->string.
3701 Value will be nil if the property value is the same for
3702 all the rest of IT->string. */
3703 XSETINT (limit, SCHARS (it->string));
3704 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3705 it->string, limit);
3706
3707 /* Text at current position is invisible. The next
3708 change in the property is at position end_charpos.
3709 Move IT's current position to that position. */
3710 if (INTEGERP (end_charpos)
3711 && XFASTINT (end_charpos) < XFASTINT (limit))
3712 {
3713 struct text_pos old;
3714 old = it->current.string_pos;
3715 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3716 compute_string_pos (&it->current.string_pos, old, it->string);
3717 }
3718 else
3719 {
3720 /* The rest of the string is invisible. If this is an
3721 overlay string, proceed with the next overlay string
3722 or whatever comes and return a character from there. */
3723 if (it->current.overlay_string_index >= 0)
3724 {
3725 next_overlay_string (it);
3726 /* Don't check for overlay strings when we just
3727 finished processing them. */
3728 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3729 }
3730 else
3731 {
3732 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3733 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3734 }
3735 }
3736 }
3737 }
3738 else
3739 {
3740 int invis_p;
3741 EMACS_INT newpos, next_stop, start_charpos;
3742 Lisp_Object pos, prop, overlay;
3743
3744 /* First of all, is there invisible text at this position? */
3745 start_charpos = IT_CHARPOS (*it);
3746 pos = make_number (IT_CHARPOS (*it));
3747 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3748 &overlay);
3749 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3750
3751 /* If we are on invisible text, skip over it. */
3752 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3753 {
3754 /* Record whether we have to display an ellipsis for the
3755 invisible text. */
3756 int display_ellipsis_p = invis_p == 2;
3757
3758 handled = HANDLED_RECOMPUTE_PROPS;
3759
3760 /* Loop skipping over invisible text. The loop is left at
3761 ZV or with IT on the first char being visible again. */
3762 do
3763 {
3764 /* Try to skip some invisible text. Return value is the
3765 position reached which can be equal to IT's position
3766 if there is nothing invisible here. This skips both
3767 over invisible text properties and overlays with
3768 invisible property. */
3769 newpos = skip_invisible (IT_CHARPOS (*it),
3770 &next_stop, ZV, it->window);
3771
3772 /* If we skipped nothing at all we weren't at invisible
3773 text in the first place. If everything to the end of
3774 the buffer was skipped, end the loop. */
3775 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3776 invis_p = 0;
3777 else
3778 {
3779 /* We skipped some characters but not necessarily
3780 all there are. Check if we ended up on visible
3781 text. Fget_char_property returns the property of
3782 the char before the given position, i.e. if we
3783 get invis_p = 0, this means that the char at
3784 newpos is visible. */
3785 pos = make_number (newpos);
3786 prop = Fget_char_property (pos, Qinvisible, it->window);
3787 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3788 }
3789
3790 /* If we ended up on invisible text, proceed to
3791 skip starting with next_stop. */
3792 if (invis_p)
3793 IT_CHARPOS (*it) = next_stop;
3794
3795 /* If there are adjacent invisible texts, don't lose the
3796 second one's ellipsis. */
3797 if (invis_p == 2)
3798 display_ellipsis_p = 1;
3799 }
3800 while (invis_p);
3801
3802 /* The position newpos is now either ZV or on visible text. */
3803 IT_CHARPOS (*it) = newpos;
3804 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3805
3806 /* If there are before-strings at the start of invisible
3807 text, and the text is invisible because of a text
3808 property, arrange to show before-strings because 20.x did
3809 it that way. (If the text is invisible because of an
3810 overlay property instead of a text property, this is
3811 already handled in the overlay code.) */
3812 if (NILP (overlay)
3813 && get_overlay_strings (it, start_charpos))
3814 {
3815 handled = HANDLED_RECOMPUTE_PROPS;
3816 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3817 }
3818 else if (display_ellipsis_p)
3819 {
3820 /* Make sure that the glyphs of the ellipsis will get
3821 correct `charpos' values. If we would not update
3822 it->position here, the glyphs would belong to the
3823 last visible character _before_ the invisible
3824 text, which confuses `set_cursor_from_row'.
3825
3826 We use the last invisible position instead of the
3827 first because this way the cursor is always drawn on
3828 the first "." of the ellipsis, whenever PT is inside
3829 the invisible text. Otherwise the cursor would be
3830 placed _after_ the ellipsis when the point is after the
3831 first invisible character. */
3832 if (!STRINGP (it->object))
3833 {
3834 it->position.charpos = IT_CHARPOS (*it) - 1;
3835 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3836 }
3837 it->ellipsis_p = 1;
3838 /* Let the ellipsis display before
3839 considering any properties of the following char.
3840 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3841 handled = HANDLED_RETURN;
3842 }
3843 }
3844 }
3845
3846 return handled;
3847 }
3848
3849
3850 /* Make iterator IT return `...' next.
3851 Replaces LEN characters from buffer. */
3852
3853 static void
3854 setup_for_ellipsis (it, len)
3855 struct it *it;
3856 int len;
3857 {
3858 /* Use the display table definition for `...'. Invalid glyphs
3859 will be handled by the method returning elements from dpvec. */
3860 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3861 {
3862 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3863 it->dpvec = v->contents;
3864 it->dpend = v->contents + v->size;
3865 }
3866 else
3867 {
3868 /* Default `...'. */
3869 it->dpvec = default_invis_vector;
3870 it->dpend = default_invis_vector + 3;
3871 }
3872
3873 it->dpvec_char_len = len;
3874 it->current.dpvec_index = 0;
3875 it->dpvec_face_id = -1;
3876
3877 /* Remember the current face id in case glyphs specify faces.
3878 IT's face is restored in set_iterator_to_next.
3879 saved_face_id was set to preceding char's face in handle_stop. */
3880 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3881 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3882
3883 it->method = GET_FROM_DISPLAY_VECTOR;
3884 it->ellipsis_p = 1;
3885 }
3886
3887
3888 \f
3889 /***********************************************************************
3890 'display' property
3891 ***********************************************************************/
3892
3893 /* Set up iterator IT from `display' property at its current position.
3894 Called from handle_stop.
3895 We return HANDLED_RETURN if some part of the display property
3896 overrides the display of the buffer text itself.
3897 Otherwise we return HANDLED_NORMALLY. */
3898
3899 static enum prop_handled
3900 handle_display_prop (it)
3901 struct it *it;
3902 {
3903 Lisp_Object prop, object, overlay;
3904 struct text_pos *position;
3905 /* Nonzero if some property replaces the display of the text itself. */
3906 int display_replaced_p = 0;
3907
3908 if (STRINGP (it->string))
3909 {
3910 object = it->string;
3911 position = &it->current.string_pos;
3912 }
3913 else
3914 {
3915 XSETWINDOW (object, it->w);
3916 position = &it->current.pos;
3917 }
3918
3919 /* Reset those iterator values set from display property values. */
3920 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3921 it->space_width = Qnil;
3922 it->font_height = Qnil;
3923 it->voffset = 0;
3924
3925 /* We don't support recursive `display' properties, i.e. string
3926 values that have a string `display' property, that have a string
3927 `display' property etc. */
3928 if (!it->string_from_display_prop_p)
3929 it->area = TEXT_AREA;
3930
3931 prop = get_char_property_and_overlay (make_number (position->charpos),
3932 Qdisplay, object, &overlay);
3933 if (NILP (prop))
3934 return HANDLED_NORMALLY;
3935 /* Now OVERLAY is the overlay that gave us this property, or nil
3936 if it was a text property. */
3937
3938 if (!STRINGP (it->string))
3939 object = it->w->buffer;
3940
3941 if (CONSP (prop)
3942 /* Simple properties. */
3943 && !EQ (XCAR (prop), Qimage)
3944 && !EQ (XCAR (prop), Qspace)
3945 && !EQ (XCAR (prop), Qwhen)
3946 && !EQ (XCAR (prop), Qslice)
3947 && !EQ (XCAR (prop), Qspace_width)
3948 && !EQ (XCAR (prop), Qheight)
3949 && !EQ (XCAR (prop), Qraise)
3950 /* Marginal area specifications. */
3951 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3952 && !EQ (XCAR (prop), Qleft_fringe)
3953 && !EQ (XCAR (prop), Qright_fringe)
3954 && !NILP (XCAR (prop)))
3955 {
3956 for (; CONSP (prop); prop = XCDR (prop))
3957 {
3958 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3959 position, display_replaced_p))
3960 {
3961 display_replaced_p = 1;
3962 /* If some text in a string is replaced, `position' no
3963 longer points to the position of `object'. */
3964 if (STRINGP (object))
3965 break;
3966 }
3967 }
3968 }
3969 else if (VECTORP (prop))
3970 {
3971 int i;
3972 for (i = 0; i < ASIZE (prop); ++i)
3973 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3974 position, display_replaced_p))
3975 {
3976 display_replaced_p = 1;
3977 /* If some text in a string is replaced, `position' no
3978 longer points to the position of `object'. */
3979 if (STRINGP (object))
3980 break;
3981 }
3982 }
3983 else
3984 {
3985 if (handle_single_display_spec (it, prop, object, overlay,
3986 position, 0))
3987 display_replaced_p = 1;
3988 }
3989
3990 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3991 }
3992
3993
3994 /* Value is the position of the end of the `display' property starting
3995 at START_POS in OBJECT. */
3996
3997 static struct text_pos
3998 display_prop_end (it, object, start_pos)
3999 struct it *it;
4000 Lisp_Object object;
4001 struct text_pos start_pos;
4002 {
4003 Lisp_Object end;
4004 struct text_pos end_pos;
4005
4006 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4007 Qdisplay, object, Qnil);
4008 CHARPOS (end_pos) = XFASTINT (end);
4009 if (STRINGP (object))
4010 compute_string_pos (&end_pos, start_pos, it->string);
4011 else
4012 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4013
4014 return end_pos;
4015 }
4016
4017
4018 /* Set up IT from a single `display' specification PROP. OBJECT
4019 is the object in which the `display' property was found. *POSITION
4020 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4021 means that we previously saw a display specification which already
4022 replaced text display with something else, for example an image;
4023 we ignore such properties after the first one has been processed.
4024
4025 OVERLAY is the overlay this `display' property came from,
4026 or nil if it was a text property.
4027
4028 If PROP is a `space' or `image' specification, and in some other
4029 cases too, set *POSITION to the position where the `display'
4030 property ends.
4031
4032 Value is non-zero if something was found which replaces the display
4033 of buffer or string text. */
4034
4035 static int
4036 handle_single_display_spec (it, spec, object, overlay, position,
4037 display_replaced_before_p)
4038 struct it *it;
4039 Lisp_Object spec;
4040 Lisp_Object object;
4041 Lisp_Object overlay;
4042 struct text_pos *position;
4043 int display_replaced_before_p;
4044 {
4045 Lisp_Object form;
4046 Lisp_Object location, value;
4047 struct text_pos start_pos, save_pos;
4048 int valid_p;
4049
4050 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4051 If the result is non-nil, use VALUE instead of SPEC. */
4052 form = Qt;
4053 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4054 {
4055 spec = XCDR (spec);
4056 if (!CONSP (spec))
4057 return 0;
4058 form = XCAR (spec);
4059 spec = XCDR (spec);
4060 }
4061
4062 if (!NILP (form) && !EQ (form, Qt))
4063 {
4064 int count = SPECPDL_INDEX ();
4065 struct gcpro gcpro1;
4066
4067 /* Bind `object' to the object having the `display' property, a
4068 buffer or string. Bind `position' to the position in the
4069 object where the property was found, and `buffer-position'
4070 to the current position in the buffer. */
4071 specbind (Qobject, object);
4072 specbind (Qposition, make_number (CHARPOS (*position)));
4073 specbind (Qbuffer_position,
4074 make_number (STRINGP (object)
4075 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4076 GCPRO1 (form);
4077 form = safe_eval (form);
4078 UNGCPRO;
4079 unbind_to (count, Qnil);
4080 }
4081
4082 if (NILP (form))
4083 return 0;
4084
4085 /* Handle `(height HEIGHT)' specifications. */
4086 if (CONSP (spec)
4087 && EQ (XCAR (spec), Qheight)
4088 && CONSP (XCDR (spec)))
4089 {
4090 if (!FRAME_WINDOW_P (it->f))
4091 return 0;
4092
4093 it->font_height = XCAR (XCDR (spec));
4094 if (!NILP (it->font_height))
4095 {
4096 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4097 int new_height = -1;
4098
4099 if (CONSP (it->font_height)
4100 && (EQ (XCAR (it->font_height), Qplus)
4101 || EQ (XCAR (it->font_height), Qminus))
4102 && CONSP (XCDR (it->font_height))
4103 && INTEGERP (XCAR (XCDR (it->font_height))))
4104 {
4105 /* `(+ N)' or `(- N)' where N is an integer. */
4106 int steps = XINT (XCAR (XCDR (it->font_height)));
4107 if (EQ (XCAR (it->font_height), Qplus))
4108 steps = - steps;
4109 it->face_id = smaller_face (it->f, it->face_id, steps);
4110 }
4111 else if (FUNCTIONP (it->font_height))
4112 {
4113 /* Call function with current height as argument.
4114 Value is the new height. */
4115 Lisp_Object height;
4116 height = safe_call1 (it->font_height,
4117 face->lface[LFACE_HEIGHT_INDEX]);
4118 if (NUMBERP (height))
4119 new_height = XFLOATINT (height);
4120 }
4121 else if (NUMBERP (it->font_height))
4122 {
4123 /* Value is a multiple of the canonical char height. */
4124 struct face *face;
4125
4126 face = FACE_FROM_ID (it->f,
4127 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4128 new_height = (XFLOATINT (it->font_height)
4129 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4130 }
4131 else
4132 {
4133 /* Evaluate IT->font_height with `height' bound to the
4134 current specified height to get the new height. */
4135 int count = SPECPDL_INDEX ();
4136
4137 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4138 value = safe_eval (it->font_height);
4139 unbind_to (count, Qnil);
4140
4141 if (NUMBERP (value))
4142 new_height = XFLOATINT (value);
4143 }
4144
4145 if (new_height > 0)
4146 it->face_id = face_with_height (it->f, it->face_id, new_height);
4147 }
4148
4149 return 0;
4150 }
4151
4152 /* Handle `(space-width WIDTH)'. */
4153 if (CONSP (spec)
4154 && EQ (XCAR (spec), Qspace_width)
4155 && CONSP (XCDR (spec)))
4156 {
4157 if (!FRAME_WINDOW_P (it->f))
4158 return 0;
4159
4160 value = XCAR (XCDR (spec));
4161 if (NUMBERP (value) && XFLOATINT (value) > 0)
4162 it->space_width = value;
4163
4164 return 0;
4165 }
4166
4167 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4168 if (CONSP (spec)
4169 && EQ (XCAR (spec), Qslice))
4170 {
4171 Lisp_Object tem;
4172
4173 if (!FRAME_WINDOW_P (it->f))
4174 return 0;
4175
4176 if (tem = XCDR (spec), CONSP (tem))
4177 {
4178 it->slice.x = XCAR (tem);
4179 if (tem = XCDR (tem), CONSP (tem))
4180 {
4181 it->slice.y = XCAR (tem);
4182 if (tem = XCDR (tem), CONSP (tem))
4183 {
4184 it->slice.width = XCAR (tem);
4185 if (tem = XCDR (tem), CONSP (tem))
4186 it->slice.height = XCAR (tem);
4187 }
4188 }
4189 }
4190
4191 return 0;
4192 }
4193
4194 /* Handle `(raise FACTOR)'. */
4195 if (CONSP (spec)
4196 && EQ (XCAR (spec), Qraise)
4197 && CONSP (XCDR (spec)))
4198 {
4199 if (!FRAME_WINDOW_P (it->f))
4200 return 0;
4201
4202 #ifdef HAVE_WINDOW_SYSTEM
4203 value = XCAR (XCDR (spec));
4204 if (NUMBERP (value))
4205 {
4206 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4207 it->voffset = - (XFLOATINT (value)
4208 * (FONT_HEIGHT (face->font)));
4209 }
4210 #endif /* HAVE_WINDOW_SYSTEM */
4211
4212 return 0;
4213 }
4214
4215 /* Don't handle the other kinds of display specifications
4216 inside a string that we got from a `display' property. */
4217 if (it->string_from_display_prop_p)
4218 return 0;
4219
4220 /* Characters having this form of property are not displayed, so
4221 we have to find the end of the property. */
4222 start_pos = *position;
4223 *position = display_prop_end (it, object, start_pos);
4224 value = Qnil;
4225
4226 /* Stop the scan at that end position--we assume that all
4227 text properties change there. */
4228 it->stop_charpos = position->charpos;
4229
4230 /* Handle `(left-fringe BITMAP [FACE])'
4231 and `(right-fringe BITMAP [FACE])'. */
4232 if (CONSP (spec)
4233 && (EQ (XCAR (spec), Qleft_fringe)
4234 || EQ (XCAR (spec), Qright_fringe))
4235 && CONSP (XCDR (spec)))
4236 {
4237 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4238 int fringe_bitmap;
4239
4240 if (!FRAME_WINDOW_P (it->f))
4241 /* If we return here, POSITION has been advanced
4242 across the text with this property. */
4243 return 0;
4244
4245 #ifdef HAVE_WINDOW_SYSTEM
4246 value = XCAR (XCDR (spec));
4247 if (!SYMBOLP (value)
4248 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4249 /* If we return here, POSITION has been advanced
4250 across the text with this property. */
4251 return 0;
4252
4253 if (CONSP (XCDR (XCDR (spec))))
4254 {
4255 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4256 int face_id2 = lookup_derived_face (it->f, face_name,
4257 FRINGE_FACE_ID, 0);
4258 if (face_id2 >= 0)
4259 face_id = face_id2;
4260 }
4261
4262 /* Save current settings of IT so that we can restore them
4263 when we are finished with the glyph property value. */
4264
4265 save_pos = it->position;
4266 it->position = *position;
4267 push_it (it);
4268 it->position = save_pos;
4269
4270 it->area = TEXT_AREA;
4271 it->what = IT_IMAGE;
4272 it->image_id = -1; /* no image */
4273 it->position = start_pos;
4274 it->object = NILP (object) ? it->w->buffer : object;
4275 it->method = GET_FROM_IMAGE;
4276 it->from_overlay = Qnil;
4277 it->face_id = face_id;
4278
4279 /* Say that we haven't consumed the characters with
4280 `display' property yet. The call to pop_it in
4281 set_iterator_to_next will clean this up. */
4282 *position = start_pos;
4283
4284 if (EQ (XCAR (spec), Qleft_fringe))
4285 {
4286 it->left_user_fringe_bitmap = fringe_bitmap;
4287 it->left_user_fringe_face_id = face_id;
4288 }
4289 else
4290 {
4291 it->right_user_fringe_bitmap = fringe_bitmap;
4292 it->right_user_fringe_face_id = face_id;
4293 }
4294 #endif /* HAVE_WINDOW_SYSTEM */
4295 return 1;
4296 }
4297
4298 /* Prepare to handle `((margin left-margin) ...)',
4299 `((margin right-margin) ...)' and `((margin nil) ...)'
4300 prefixes for display specifications. */
4301 location = Qunbound;
4302 if (CONSP (spec) && CONSP (XCAR (spec)))
4303 {
4304 Lisp_Object tem;
4305
4306 value = XCDR (spec);
4307 if (CONSP (value))
4308 value = XCAR (value);
4309
4310 tem = XCAR (spec);
4311 if (EQ (XCAR (tem), Qmargin)
4312 && (tem = XCDR (tem),
4313 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4314 (NILP (tem)
4315 || EQ (tem, Qleft_margin)
4316 || EQ (tem, Qright_margin))))
4317 location = tem;
4318 }
4319
4320 if (EQ (location, Qunbound))
4321 {
4322 location = Qnil;
4323 value = spec;
4324 }
4325
4326 /* After this point, VALUE is the property after any
4327 margin prefix has been stripped. It must be a string,
4328 an image specification, or `(space ...)'.
4329
4330 LOCATION specifies where to display: `left-margin',
4331 `right-margin' or nil. */
4332
4333 valid_p = (STRINGP (value)
4334 #ifdef HAVE_WINDOW_SYSTEM
4335 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4336 #endif /* not HAVE_WINDOW_SYSTEM */
4337 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4338
4339 if (valid_p && !display_replaced_before_p)
4340 {
4341 /* Save current settings of IT so that we can restore them
4342 when we are finished with the glyph property value. */
4343 save_pos = it->position;
4344 it->position = *position;
4345 push_it (it);
4346 it->position = save_pos;
4347 it->from_overlay = overlay;
4348
4349 if (NILP (location))
4350 it->area = TEXT_AREA;
4351 else if (EQ (location, Qleft_margin))
4352 it->area = LEFT_MARGIN_AREA;
4353 else
4354 it->area = RIGHT_MARGIN_AREA;
4355
4356 if (STRINGP (value))
4357 {
4358 it->string = value;
4359 it->multibyte_p = STRING_MULTIBYTE (it->string);
4360 it->current.overlay_string_index = -1;
4361 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4362 it->end_charpos = it->string_nchars = SCHARS (it->string);
4363 it->method = GET_FROM_STRING;
4364 it->stop_charpos = 0;
4365 it->string_from_display_prop_p = 1;
4366 /* Say that we haven't consumed the characters with
4367 `display' property yet. The call to pop_it in
4368 set_iterator_to_next will clean this up. */
4369 if (BUFFERP (object))
4370 *position = start_pos;
4371 }
4372 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4373 {
4374 it->method = GET_FROM_STRETCH;
4375 it->object = value;
4376 *position = it->position = start_pos;
4377 }
4378 #ifdef HAVE_WINDOW_SYSTEM
4379 else
4380 {
4381 it->what = IT_IMAGE;
4382 it->image_id = lookup_image (it->f, value);
4383 it->position = start_pos;
4384 it->object = NILP (object) ? it->w->buffer : object;
4385 it->method = GET_FROM_IMAGE;
4386
4387 /* Say that we haven't consumed the characters with
4388 `display' property yet. The call to pop_it in
4389 set_iterator_to_next will clean this up. */
4390 *position = start_pos;
4391 }
4392 #endif /* HAVE_WINDOW_SYSTEM */
4393
4394 return 1;
4395 }
4396
4397 /* Invalid property or property not supported. Restore
4398 POSITION to what it was before. */
4399 *position = start_pos;
4400 return 0;
4401 }
4402
4403
4404 /* Check if SPEC is a display sub-property value whose text should be
4405 treated as intangible. */
4406
4407 static int
4408 single_display_spec_intangible_p (prop)
4409 Lisp_Object prop;
4410 {
4411 /* Skip over `when FORM'. */
4412 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4413 {
4414 prop = XCDR (prop);
4415 if (!CONSP (prop))
4416 return 0;
4417 prop = XCDR (prop);
4418 }
4419
4420 if (STRINGP (prop))
4421 return 1;
4422
4423 if (!CONSP (prop))
4424 return 0;
4425
4426 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4427 we don't need to treat text as intangible. */
4428 if (EQ (XCAR (prop), Qmargin))
4429 {
4430 prop = XCDR (prop);
4431 if (!CONSP (prop))
4432 return 0;
4433
4434 prop = XCDR (prop);
4435 if (!CONSP (prop)
4436 || EQ (XCAR (prop), Qleft_margin)
4437 || EQ (XCAR (prop), Qright_margin))
4438 return 0;
4439 }
4440
4441 return (CONSP (prop)
4442 && (EQ (XCAR (prop), Qimage)
4443 || EQ (XCAR (prop), Qspace)));
4444 }
4445
4446
4447 /* Check if PROP is a display property value whose text should be
4448 treated as intangible. */
4449
4450 int
4451 display_prop_intangible_p (prop)
4452 Lisp_Object prop;
4453 {
4454 if (CONSP (prop)
4455 && CONSP (XCAR (prop))
4456 && !EQ (Qmargin, XCAR (XCAR (prop))))
4457 {
4458 /* A list of sub-properties. */
4459 while (CONSP (prop))
4460 {
4461 if (single_display_spec_intangible_p (XCAR (prop)))
4462 return 1;
4463 prop = XCDR (prop);
4464 }
4465 }
4466 else if (VECTORP (prop))
4467 {
4468 /* A vector of sub-properties. */
4469 int i;
4470 for (i = 0; i < ASIZE (prop); ++i)
4471 if (single_display_spec_intangible_p (AREF (prop, i)))
4472 return 1;
4473 }
4474 else
4475 return single_display_spec_intangible_p (prop);
4476
4477 return 0;
4478 }
4479
4480
4481 /* Return 1 if PROP is a display sub-property value containing STRING. */
4482
4483 static int
4484 single_display_spec_string_p (prop, string)
4485 Lisp_Object prop, string;
4486 {
4487 if (EQ (string, prop))
4488 return 1;
4489
4490 /* Skip over `when FORM'. */
4491 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4492 {
4493 prop = XCDR (prop);
4494 if (!CONSP (prop))
4495 return 0;
4496 prop = XCDR (prop);
4497 }
4498
4499 if (CONSP (prop))
4500 /* Skip over `margin LOCATION'. */
4501 if (EQ (XCAR (prop), Qmargin))
4502 {
4503 prop = XCDR (prop);
4504 if (!CONSP (prop))
4505 return 0;
4506
4507 prop = XCDR (prop);
4508 if (!CONSP (prop))
4509 return 0;
4510 }
4511
4512 return CONSP (prop) && EQ (XCAR (prop), string);
4513 }
4514
4515
4516 /* Return 1 if STRING appears in the `display' property PROP. */
4517
4518 static int
4519 display_prop_string_p (prop, string)
4520 Lisp_Object prop, string;
4521 {
4522 if (CONSP (prop)
4523 && CONSP (XCAR (prop))
4524 && !EQ (Qmargin, XCAR (XCAR (prop))))
4525 {
4526 /* A list of sub-properties. */
4527 while (CONSP (prop))
4528 {
4529 if (single_display_spec_string_p (XCAR (prop), string))
4530 return 1;
4531 prop = XCDR (prop);
4532 }
4533 }
4534 else if (VECTORP (prop))
4535 {
4536 /* A vector of sub-properties. */
4537 int i;
4538 for (i = 0; i < ASIZE (prop); ++i)
4539 if (single_display_spec_string_p (AREF (prop, i), string))
4540 return 1;
4541 }
4542 else
4543 return single_display_spec_string_p (prop, string);
4544
4545 return 0;
4546 }
4547
4548
4549 /* Determine which buffer position in W's buffer STRING comes from.
4550 AROUND_CHARPOS is an approximate position where it could come from.
4551 Value is the buffer position or 0 if it couldn't be determined.
4552
4553 W's buffer must be current.
4554
4555 This function is necessary because we don't record buffer positions
4556 in glyphs generated from strings (to keep struct glyph small).
4557 This function may only use code that doesn't eval because it is
4558 called asynchronously from note_mouse_highlight. */
4559
4560 int
4561 string_buffer_position (w, string, around_charpos)
4562 struct window *w;
4563 Lisp_Object string;
4564 int around_charpos;
4565 {
4566 Lisp_Object limit, prop, pos;
4567 const int MAX_DISTANCE = 1000;
4568 int found = 0;
4569
4570 pos = make_number (around_charpos);
4571 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4572 while (!found && !EQ (pos, limit))
4573 {
4574 prop = Fget_char_property (pos, Qdisplay, Qnil);
4575 if (!NILP (prop) && display_prop_string_p (prop, string))
4576 found = 1;
4577 else
4578 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4579 }
4580
4581 if (!found)
4582 {
4583 pos = make_number (around_charpos);
4584 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4585 while (!found && !EQ (pos, limit))
4586 {
4587 prop = Fget_char_property (pos, Qdisplay, Qnil);
4588 if (!NILP (prop) && display_prop_string_p (prop, string))
4589 found = 1;
4590 else
4591 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4592 limit);
4593 }
4594 }
4595
4596 return found ? XINT (pos) : 0;
4597 }
4598
4599
4600 \f
4601 /***********************************************************************
4602 `composition' property
4603 ***********************************************************************/
4604
4605 /* Set up iterator IT from `composition' property at its current
4606 position. Called from handle_stop. */
4607
4608 static enum prop_handled
4609 handle_composition_prop (it)
4610 struct it *it;
4611 {
4612 Lisp_Object prop, string;
4613 EMACS_INT pos, pos_byte, start, end;
4614
4615 if (STRINGP (it->string))
4616 {
4617 unsigned char *s;
4618
4619 pos = IT_STRING_CHARPOS (*it);
4620 pos_byte = IT_STRING_BYTEPOS (*it);
4621 string = it->string;
4622 s = SDATA (string) + pos_byte;
4623 it->c = STRING_CHAR (s);
4624 }
4625 else
4626 {
4627 pos = IT_CHARPOS (*it);
4628 pos_byte = IT_BYTEPOS (*it);
4629 string = Qnil;
4630 it->c = FETCH_CHAR (pos_byte);
4631 }
4632
4633 /* If there's a valid composition and point is not inside of the
4634 composition (in the case that the composition is from the current
4635 buffer), draw a glyph composed from the composition components. */
4636 if (find_composition (pos, -1, &start, &end, &prop, string)
4637 && COMPOSITION_VALID_P (start, end, prop)
4638 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4639 {
4640 if (start != pos)
4641 {
4642 if (STRINGP (it->string))
4643 pos_byte = string_char_to_byte (it->string, start);
4644 else
4645 pos_byte = CHAR_TO_BYTE (start);
4646 }
4647 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4648 prop, string);
4649
4650 if (it->cmp_it.id >= 0)
4651 {
4652 it->cmp_it.ch = -1;
4653 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4654 it->cmp_it.nglyphs = -1;
4655 }
4656 }
4657
4658 return HANDLED_NORMALLY;
4659 }
4660
4661
4662 \f
4663 /***********************************************************************
4664 Overlay strings
4665 ***********************************************************************/
4666
4667 /* The following structure is used to record overlay strings for
4668 later sorting in load_overlay_strings. */
4669
4670 struct overlay_entry
4671 {
4672 Lisp_Object overlay;
4673 Lisp_Object string;
4674 int priority;
4675 int after_string_p;
4676 };
4677
4678
4679 /* Set up iterator IT from overlay strings at its current position.
4680 Called from handle_stop. */
4681
4682 static enum prop_handled
4683 handle_overlay_change (it)
4684 struct it *it;
4685 {
4686 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4687 return HANDLED_RECOMPUTE_PROPS;
4688 else
4689 return HANDLED_NORMALLY;
4690 }
4691
4692
4693 /* Set up the next overlay string for delivery by IT, if there is an
4694 overlay string to deliver. Called by set_iterator_to_next when the
4695 end of the current overlay string is reached. If there are more
4696 overlay strings to display, IT->string and
4697 IT->current.overlay_string_index are set appropriately here.
4698 Otherwise IT->string is set to nil. */
4699
4700 static void
4701 next_overlay_string (it)
4702 struct it *it;
4703 {
4704 ++it->current.overlay_string_index;
4705 if (it->current.overlay_string_index == it->n_overlay_strings)
4706 {
4707 /* No more overlay strings. Restore IT's settings to what
4708 they were before overlay strings were processed, and
4709 continue to deliver from current_buffer. */
4710
4711 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4712 pop_it (it);
4713 xassert (it->sp > 0
4714 || (NILP (it->string)
4715 && it->method == GET_FROM_BUFFER
4716 && it->stop_charpos >= BEGV
4717 && it->stop_charpos <= it->end_charpos));
4718 it->current.overlay_string_index = -1;
4719 it->n_overlay_strings = 0;
4720
4721 /* If we're at the end of the buffer, record that we have
4722 processed the overlay strings there already, so that
4723 next_element_from_buffer doesn't try it again. */
4724 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4725 it->overlay_strings_at_end_processed_p = 1;
4726 }
4727 else
4728 {
4729 /* There are more overlay strings to process. If
4730 IT->current.overlay_string_index has advanced to a position
4731 where we must load IT->overlay_strings with more strings, do
4732 it. */
4733 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4734
4735 if (it->current.overlay_string_index && i == 0)
4736 load_overlay_strings (it, 0);
4737
4738 /* Initialize IT to deliver display elements from the overlay
4739 string. */
4740 it->string = it->overlay_strings[i];
4741 it->multibyte_p = STRING_MULTIBYTE (it->string);
4742 SET_TEXT_POS (it->current.string_pos, 0, 0);
4743 it->method = GET_FROM_STRING;
4744 it->stop_charpos = 0;
4745 if (it->cmp_it.stop_pos >= 0)
4746 it->cmp_it.stop_pos = 0;
4747 }
4748
4749 CHECK_IT (it);
4750 }
4751
4752
4753 /* Compare two overlay_entry structures E1 and E2. Used as a
4754 comparison function for qsort in load_overlay_strings. Overlay
4755 strings for the same position are sorted so that
4756
4757 1. All after-strings come in front of before-strings, except
4758 when they come from the same overlay.
4759
4760 2. Within after-strings, strings are sorted so that overlay strings
4761 from overlays with higher priorities come first.
4762
4763 2. Within before-strings, strings are sorted so that overlay
4764 strings from overlays with higher priorities come last.
4765
4766 Value is analogous to strcmp. */
4767
4768
4769 static int
4770 compare_overlay_entries (e1, e2)
4771 void *e1, *e2;
4772 {
4773 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4774 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4775 int result;
4776
4777 if (entry1->after_string_p != entry2->after_string_p)
4778 {
4779 /* Let after-strings appear in front of before-strings if
4780 they come from different overlays. */
4781 if (EQ (entry1->overlay, entry2->overlay))
4782 result = entry1->after_string_p ? 1 : -1;
4783 else
4784 result = entry1->after_string_p ? -1 : 1;
4785 }
4786 else if (entry1->after_string_p)
4787 /* After-strings sorted in order of decreasing priority. */
4788 result = entry2->priority - entry1->priority;
4789 else
4790 /* Before-strings sorted in order of increasing priority. */
4791 result = entry1->priority - entry2->priority;
4792
4793 return result;
4794 }
4795
4796
4797 /* Load the vector IT->overlay_strings with overlay strings from IT's
4798 current buffer position, or from CHARPOS if that is > 0. Set
4799 IT->n_overlays to the total number of overlay strings found.
4800
4801 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4802 a time. On entry into load_overlay_strings,
4803 IT->current.overlay_string_index gives the number of overlay
4804 strings that have already been loaded by previous calls to this
4805 function.
4806
4807 IT->add_overlay_start contains an additional overlay start
4808 position to consider for taking overlay strings from, if non-zero.
4809 This position comes into play when the overlay has an `invisible'
4810 property, and both before and after-strings. When we've skipped to
4811 the end of the overlay, because of its `invisible' property, we
4812 nevertheless want its before-string to appear.
4813 IT->add_overlay_start will contain the overlay start position
4814 in this case.
4815
4816 Overlay strings are sorted so that after-string strings come in
4817 front of before-string strings. Within before and after-strings,
4818 strings are sorted by overlay priority. See also function
4819 compare_overlay_entries. */
4820
4821 static void
4822 load_overlay_strings (it, charpos)
4823 struct it *it;
4824 int charpos;
4825 {
4826 extern Lisp_Object Qwindow, Qpriority;
4827 Lisp_Object overlay, window, str, invisible;
4828 struct Lisp_Overlay *ov;
4829 int start, end;
4830 int size = 20;
4831 int n = 0, i, j, invis_p;
4832 struct overlay_entry *entries
4833 = (struct overlay_entry *) alloca (size * sizeof *entries);
4834
4835 if (charpos <= 0)
4836 charpos = IT_CHARPOS (*it);
4837
4838 /* Append the overlay string STRING of overlay OVERLAY to vector
4839 `entries' which has size `size' and currently contains `n'
4840 elements. AFTER_P non-zero means STRING is an after-string of
4841 OVERLAY. */
4842 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4843 do \
4844 { \
4845 Lisp_Object priority; \
4846 \
4847 if (n == size) \
4848 { \
4849 int new_size = 2 * size; \
4850 struct overlay_entry *old = entries; \
4851 entries = \
4852 (struct overlay_entry *) alloca (new_size \
4853 * sizeof *entries); \
4854 bcopy (old, entries, size * sizeof *entries); \
4855 size = new_size; \
4856 } \
4857 \
4858 entries[n].string = (STRING); \
4859 entries[n].overlay = (OVERLAY); \
4860 priority = Foverlay_get ((OVERLAY), Qpriority); \
4861 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4862 entries[n].after_string_p = (AFTER_P); \
4863 ++n; \
4864 } \
4865 while (0)
4866
4867 /* Process overlay before the overlay center. */
4868 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4869 {
4870 XSETMISC (overlay, ov);
4871 xassert (OVERLAYP (overlay));
4872 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4873 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4874
4875 if (end < charpos)
4876 break;
4877
4878 /* Skip this overlay if it doesn't start or end at IT's current
4879 position. */
4880 if (end != charpos && start != charpos)
4881 continue;
4882
4883 /* Skip this overlay if it doesn't apply to IT->w. */
4884 window = Foverlay_get (overlay, Qwindow);
4885 if (WINDOWP (window) && XWINDOW (window) != it->w)
4886 continue;
4887
4888 /* If the text ``under'' the overlay is invisible, both before-
4889 and after-strings from this overlay are visible; start and
4890 end position are indistinguishable. */
4891 invisible = Foverlay_get (overlay, Qinvisible);
4892 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4893
4894 /* If overlay has a non-empty before-string, record it. */
4895 if ((start == charpos || (end == charpos && invis_p))
4896 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4897 && SCHARS (str))
4898 RECORD_OVERLAY_STRING (overlay, str, 0);
4899
4900 /* If overlay has a non-empty after-string, record it. */
4901 if ((end == charpos || (start == charpos && invis_p))
4902 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4903 && SCHARS (str))
4904 RECORD_OVERLAY_STRING (overlay, str, 1);
4905 }
4906
4907 /* Process overlays after the overlay center. */
4908 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4909 {
4910 XSETMISC (overlay, ov);
4911 xassert (OVERLAYP (overlay));
4912 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4913 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4914
4915 if (start > charpos)
4916 break;
4917
4918 /* Skip this overlay if it doesn't start or end at IT's current
4919 position. */
4920 if (end != charpos && start != charpos)
4921 continue;
4922
4923 /* Skip this overlay if it doesn't apply to IT->w. */
4924 window = Foverlay_get (overlay, Qwindow);
4925 if (WINDOWP (window) && XWINDOW (window) != it->w)
4926 continue;
4927
4928 /* If the text ``under'' the overlay is invisible, it has a zero
4929 dimension, and both before- and after-strings apply. */
4930 invisible = Foverlay_get (overlay, Qinvisible);
4931 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4932
4933 /* If overlay has a non-empty before-string, record it. */
4934 if ((start == charpos || (end == charpos && invis_p))
4935 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4936 && SCHARS (str))
4937 RECORD_OVERLAY_STRING (overlay, str, 0);
4938
4939 /* If overlay has a non-empty after-string, record it. */
4940 if ((end == charpos || (start == charpos && invis_p))
4941 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4942 && SCHARS (str))
4943 RECORD_OVERLAY_STRING (overlay, str, 1);
4944 }
4945
4946 #undef RECORD_OVERLAY_STRING
4947
4948 /* Sort entries. */
4949 if (n > 1)
4950 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4951
4952 /* Record the total number of strings to process. */
4953 it->n_overlay_strings = n;
4954
4955 /* IT->current.overlay_string_index is the number of overlay strings
4956 that have already been consumed by IT. Copy some of the
4957 remaining overlay strings to IT->overlay_strings. */
4958 i = 0;
4959 j = it->current.overlay_string_index;
4960 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4961 {
4962 it->overlay_strings[i] = entries[j].string;
4963 it->string_overlays[i++] = entries[j++].overlay;
4964 }
4965
4966 CHECK_IT (it);
4967 }
4968
4969
4970 /* Get the first chunk of overlay strings at IT's current buffer
4971 position, or at CHARPOS if that is > 0. Value is non-zero if at
4972 least one overlay string was found. */
4973
4974 static int
4975 get_overlay_strings_1 (it, charpos, compute_stop_p)
4976 struct it *it;
4977 int charpos;
4978 int compute_stop_p;
4979 {
4980 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4981 process. This fills IT->overlay_strings with strings, and sets
4982 IT->n_overlay_strings to the total number of strings to process.
4983 IT->pos.overlay_string_index has to be set temporarily to zero
4984 because load_overlay_strings needs this; it must be set to -1
4985 when no overlay strings are found because a zero value would
4986 indicate a position in the first overlay string. */
4987 it->current.overlay_string_index = 0;
4988 load_overlay_strings (it, charpos);
4989
4990 /* If we found overlay strings, set up IT to deliver display
4991 elements from the first one. Otherwise set up IT to deliver
4992 from current_buffer. */
4993 if (it->n_overlay_strings)
4994 {
4995 /* Make sure we know settings in current_buffer, so that we can
4996 restore meaningful values when we're done with the overlay
4997 strings. */
4998 if (compute_stop_p)
4999 compute_stop_pos (it);
5000 xassert (it->face_id >= 0);
5001
5002 /* Save IT's settings. They are restored after all overlay
5003 strings have been processed. */
5004 xassert (!compute_stop_p || it->sp == 0);
5005
5006 /* When called from handle_stop, there might be an empty display
5007 string loaded. In that case, don't bother saving it. */
5008 if (!STRINGP (it->string) || SCHARS (it->string))
5009 push_it (it);
5010
5011 /* Set up IT to deliver display elements from the first overlay
5012 string. */
5013 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5014 it->string = it->overlay_strings[0];
5015 it->from_overlay = Qnil;
5016 it->stop_charpos = 0;
5017 xassert (STRINGP (it->string));
5018 it->end_charpos = SCHARS (it->string);
5019 it->multibyte_p = STRING_MULTIBYTE (it->string);
5020 it->method = GET_FROM_STRING;
5021 return 1;
5022 }
5023
5024 it->current.overlay_string_index = -1;
5025 return 0;
5026 }
5027
5028 static int
5029 get_overlay_strings (it, charpos)
5030 struct it *it;
5031 int charpos;
5032 {
5033 it->string = Qnil;
5034 it->method = GET_FROM_BUFFER;
5035
5036 (void) get_overlay_strings_1 (it, charpos, 1);
5037
5038 CHECK_IT (it);
5039
5040 /* Value is non-zero if we found at least one overlay string. */
5041 return STRINGP (it->string);
5042 }
5043
5044
5045 \f
5046 /***********************************************************************
5047 Saving and restoring state
5048 ***********************************************************************/
5049
5050 /* Save current settings of IT on IT->stack. Called, for example,
5051 before setting up IT for an overlay string, to be able to restore
5052 IT's settings to what they were after the overlay string has been
5053 processed. */
5054
5055 static void
5056 push_it (it)
5057 struct it *it;
5058 {
5059 struct iterator_stack_entry *p;
5060
5061 xassert (it->sp < IT_STACK_SIZE);
5062 p = it->stack + it->sp;
5063
5064 p->stop_charpos = it->stop_charpos;
5065 p->cmp_it = it->cmp_it;
5066 xassert (it->face_id >= 0);
5067 p->face_id = it->face_id;
5068 p->string = it->string;
5069 p->method = it->method;
5070 p->from_overlay = it->from_overlay;
5071 switch (p->method)
5072 {
5073 case GET_FROM_IMAGE:
5074 p->u.image.object = it->object;
5075 p->u.image.image_id = it->image_id;
5076 p->u.image.slice = it->slice;
5077 break;
5078 case GET_FROM_STRETCH:
5079 p->u.stretch.object = it->object;
5080 break;
5081 }
5082 p->position = it->position;
5083 p->current = it->current;
5084 p->end_charpos = it->end_charpos;
5085 p->string_nchars = it->string_nchars;
5086 p->area = it->area;
5087 p->multibyte_p = it->multibyte_p;
5088 p->avoid_cursor_p = it->avoid_cursor_p;
5089 p->space_width = it->space_width;
5090 p->font_height = it->font_height;
5091 p->voffset = it->voffset;
5092 p->string_from_display_prop_p = it->string_from_display_prop_p;
5093 p->display_ellipsis_p = 0;
5094 p->line_wrap = it->line_wrap;
5095 ++it->sp;
5096 }
5097
5098
5099 /* Restore IT's settings from IT->stack. Called, for example, when no
5100 more overlay strings must be processed, and we return to delivering
5101 display elements from a buffer, or when the end of a string from a
5102 `display' property is reached and we return to delivering display
5103 elements from an overlay string, or from a buffer. */
5104
5105 static void
5106 pop_it (it)
5107 struct it *it;
5108 {
5109 struct iterator_stack_entry *p;
5110
5111 xassert (it->sp > 0);
5112 --it->sp;
5113 p = it->stack + it->sp;
5114 it->stop_charpos = p->stop_charpos;
5115 it->cmp_it = p->cmp_it;
5116 it->face_id = p->face_id;
5117 it->current = p->current;
5118 it->position = p->position;
5119 it->string = p->string;
5120 it->from_overlay = p->from_overlay;
5121 if (NILP (it->string))
5122 SET_TEXT_POS (it->current.string_pos, -1, -1);
5123 it->method = p->method;
5124 switch (it->method)
5125 {
5126 case GET_FROM_IMAGE:
5127 it->image_id = p->u.image.image_id;
5128 it->object = p->u.image.object;
5129 it->slice = p->u.image.slice;
5130 break;
5131 case GET_FROM_STRETCH:
5132 it->object = p->u.comp.object;
5133 break;
5134 case GET_FROM_BUFFER:
5135 it->object = it->w->buffer;
5136 break;
5137 case GET_FROM_STRING:
5138 it->object = it->string;
5139 break;
5140 case GET_FROM_DISPLAY_VECTOR:
5141 if (it->s)
5142 it->method = GET_FROM_C_STRING;
5143 else if (STRINGP (it->string))
5144 it->method = GET_FROM_STRING;
5145 else
5146 {
5147 it->method = GET_FROM_BUFFER;
5148 it->object = it->w->buffer;
5149 }
5150 }
5151 it->end_charpos = p->end_charpos;
5152 it->string_nchars = p->string_nchars;
5153 it->area = p->area;
5154 it->multibyte_p = p->multibyte_p;
5155 it->avoid_cursor_p = p->avoid_cursor_p;
5156 it->space_width = p->space_width;
5157 it->font_height = p->font_height;
5158 it->voffset = p->voffset;
5159 it->string_from_display_prop_p = p->string_from_display_prop_p;
5160 it->line_wrap = p->line_wrap;
5161 }
5162
5163
5164 \f
5165 /***********************************************************************
5166 Moving over lines
5167 ***********************************************************************/
5168
5169 /* Set IT's current position to the previous line start. */
5170
5171 static void
5172 back_to_previous_line_start (it)
5173 struct it *it;
5174 {
5175 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5176 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5177 }
5178
5179
5180 /* Move IT to the next line start.
5181
5182 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5183 we skipped over part of the text (as opposed to moving the iterator
5184 continuously over the text). Otherwise, don't change the value
5185 of *SKIPPED_P.
5186
5187 Newlines may come from buffer text, overlay strings, or strings
5188 displayed via the `display' property. That's the reason we can't
5189 simply use find_next_newline_no_quit.
5190
5191 Note that this function may not skip over invisible text that is so
5192 because of text properties and immediately follows a newline. If
5193 it would, function reseat_at_next_visible_line_start, when called
5194 from set_iterator_to_next, would effectively make invisible
5195 characters following a newline part of the wrong glyph row, which
5196 leads to wrong cursor motion. */
5197
5198 static int
5199 forward_to_next_line_start (it, skipped_p)
5200 struct it *it;
5201 int *skipped_p;
5202 {
5203 int old_selective, newline_found_p, n;
5204 const int MAX_NEWLINE_DISTANCE = 500;
5205
5206 /* If already on a newline, just consume it to avoid unintended
5207 skipping over invisible text below. */
5208 if (it->what == IT_CHARACTER
5209 && it->c == '\n'
5210 && CHARPOS (it->position) == IT_CHARPOS (*it))
5211 {
5212 set_iterator_to_next (it, 0);
5213 it->c = 0;
5214 return 1;
5215 }
5216
5217 /* Don't handle selective display in the following. It's (a)
5218 unnecessary because it's done by the caller, and (b) leads to an
5219 infinite recursion because next_element_from_ellipsis indirectly
5220 calls this function. */
5221 old_selective = it->selective;
5222 it->selective = 0;
5223
5224 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5225 from buffer text. */
5226 for (n = newline_found_p = 0;
5227 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5228 n += STRINGP (it->string) ? 0 : 1)
5229 {
5230 if (!get_next_display_element (it))
5231 return 0;
5232 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5233 set_iterator_to_next (it, 0);
5234 }
5235
5236 /* If we didn't find a newline near enough, see if we can use a
5237 short-cut. */
5238 if (!newline_found_p)
5239 {
5240 int start = IT_CHARPOS (*it);
5241 int limit = find_next_newline_no_quit (start, 1);
5242 Lisp_Object pos;
5243
5244 xassert (!STRINGP (it->string));
5245
5246 /* If there isn't any `display' property in sight, and no
5247 overlays, we can just use the position of the newline in
5248 buffer text. */
5249 if (it->stop_charpos >= limit
5250 || ((pos = Fnext_single_property_change (make_number (start),
5251 Qdisplay,
5252 Qnil, make_number (limit)),
5253 NILP (pos))
5254 && next_overlay_change (start) == ZV))
5255 {
5256 IT_CHARPOS (*it) = limit;
5257 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5258 *skipped_p = newline_found_p = 1;
5259 }
5260 else
5261 {
5262 while (get_next_display_element (it)
5263 && !newline_found_p)
5264 {
5265 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5266 set_iterator_to_next (it, 0);
5267 }
5268 }
5269 }
5270
5271 it->selective = old_selective;
5272 return newline_found_p;
5273 }
5274
5275
5276 /* Set IT's current position to the previous visible line start. Skip
5277 invisible text that is so either due to text properties or due to
5278 selective display. Caution: this does not change IT->current_x and
5279 IT->hpos. */
5280
5281 static void
5282 back_to_previous_visible_line_start (it)
5283 struct it *it;
5284 {
5285 while (IT_CHARPOS (*it) > BEGV)
5286 {
5287 back_to_previous_line_start (it);
5288
5289 if (IT_CHARPOS (*it) <= BEGV)
5290 break;
5291
5292 /* If selective > 0, then lines indented more than that values
5293 are invisible. */
5294 if (it->selective > 0
5295 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5296 (double) it->selective)) /* iftc */
5297 continue;
5298
5299 /* Check the newline before point for invisibility. */
5300 {
5301 Lisp_Object prop;
5302 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5303 Qinvisible, it->window);
5304 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5305 continue;
5306 }
5307
5308 if (IT_CHARPOS (*it) <= BEGV)
5309 break;
5310
5311 {
5312 struct it it2;
5313 int pos;
5314 EMACS_INT beg, end;
5315 Lisp_Object val, overlay;
5316
5317 /* If newline is part of a composition, continue from start of composition */
5318 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5319 && beg < IT_CHARPOS (*it))
5320 goto replaced;
5321
5322 /* If newline is replaced by a display property, find start of overlay
5323 or interval and continue search from that point. */
5324 it2 = *it;
5325 pos = --IT_CHARPOS (it2);
5326 --IT_BYTEPOS (it2);
5327 it2.sp = 0;
5328 it2.string_from_display_prop_p = 0;
5329 if (handle_display_prop (&it2) == HANDLED_RETURN
5330 && !NILP (val = get_char_property_and_overlay
5331 (make_number (pos), Qdisplay, Qnil, &overlay))
5332 && (OVERLAYP (overlay)
5333 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5334 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5335 goto replaced;
5336
5337 /* Newline is not replaced by anything -- so we are done. */
5338 break;
5339
5340 replaced:
5341 if (beg < BEGV)
5342 beg = BEGV;
5343 IT_CHARPOS (*it) = beg;
5344 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5345 }
5346 }
5347
5348 it->continuation_lines_width = 0;
5349
5350 xassert (IT_CHARPOS (*it) >= BEGV);
5351 xassert (IT_CHARPOS (*it) == BEGV
5352 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5353 CHECK_IT (it);
5354 }
5355
5356
5357 /* Reseat iterator IT at the previous visible line start. Skip
5358 invisible text that is so either due to text properties or due to
5359 selective display. At the end, update IT's overlay information,
5360 face information etc. */
5361
5362 void
5363 reseat_at_previous_visible_line_start (it)
5364 struct it *it;
5365 {
5366 back_to_previous_visible_line_start (it);
5367 reseat (it, it->current.pos, 1);
5368 CHECK_IT (it);
5369 }
5370
5371
5372 /* Reseat iterator IT on the next visible line start in the current
5373 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5374 preceding the line start. Skip over invisible text that is so
5375 because of selective display. Compute faces, overlays etc at the
5376 new position. Note that this function does not skip over text that
5377 is invisible because of text properties. */
5378
5379 static void
5380 reseat_at_next_visible_line_start (it, on_newline_p)
5381 struct it *it;
5382 int on_newline_p;
5383 {
5384 int newline_found_p, skipped_p = 0;
5385
5386 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5387
5388 /* Skip over lines that are invisible because they are indented
5389 more than the value of IT->selective. */
5390 if (it->selective > 0)
5391 while (IT_CHARPOS (*it) < ZV
5392 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5393 (double) it->selective)) /* iftc */
5394 {
5395 xassert (IT_BYTEPOS (*it) == BEGV
5396 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5397 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5398 }
5399
5400 /* Position on the newline if that's what's requested. */
5401 if (on_newline_p && newline_found_p)
5402 {
5403 if (STRINGP (it->string))
5404 {
5405 if (IT_STRING_CHARPOS (*it) > 0)
5406 {
5407 --IT_STRING_CHARPOS (*it);
5408 --IT_STRING_BYTEPOS (*it);
5409 }
5410 }
5411 else if (IT_CHARPOS (*it) > BEGV)
5412 {
5413 --IT_CHARPOS (*it);
5414 --IT_BYTEPOS (*it);
5415 reseat (it, it->current.pos, 0);
5416 }
5417 }
5418 else if (skipped_p)
5419 reseat (it, it->current.pos, 0);
5420
5421 CHECK_IT (it);
5422 }
5423
5424
5425 \f
5426 /***********************************************************************
5427 Changing an iterator's position
5428 ***********************************************************************/
5429
5430 /* Change IT's current position to POS in current_buffer. If FORCE_P
5431 is non-zero, always check for text properties at the new position.
5432 Otherwise, text properties are only looked up if POS >=
5433 IT->check_charpos of a property. */
5434
5435 static void
5436 reseat (it, pos, force_p)
5437 struct it *it;
5438 struct text_pos pos;
5439 int force_p;
5440 {
5441 int original_pos = IT_CHARPOS (*it);
5442
5443 reseat_1 (it, pos, 0);
5444
5445 /* Determine where to check text properties. Avoid doing it
5446 where possible because text property lookup is very expensive. */
5447 if (force_p
5448 || CHARPOS (pos) > it->stop_charpos
5449 || CHARPOS (pos) < original_pos)
5450 handle_stop (it);
5451
5452 CHECK_IT (it);
5453 }
5454
5455
5456 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5457 IT->stop_pos to POS, also. */
5458
5459 static void
5460 reseat_1 (it, pos, set_stop_p)
5461 struct it *it;
5462 struct text_pos pos;
5463 int set_stop_p;
5464 {
5465 /* Don't call this function when scanning a C string. */
5466 xassert (it->s == NULL);
5467
5468 /* POS must be a reasonable value. */
5469 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5470
5471 it->current.pos = it->position = pos;
5472 it->end_charpos = ZV;
5473 it->dpvec = NULL;
5474 it->current.dpvec_index = -1;
5475 it->current.overlay_string_index = -1;
5476 IT_STRING_CHARPOS (*it) = -1;
5477 IT_STRING_BYTEPOS (*it) = -1;
5478 it->string = Qnil;
5479 it->string_from_display_prop_p = 0;
5480 it->method = GET_FROM_BUFFER;
5481 it->object = it->w->buffer;
5482 it->area = TEXT_AREA;
5483 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5484 it->sp = 0;
5485 it->string_from_display_prop_p = 0;
5486 it->face_before_selective_p = 0;
5487
5488 if (set_stop_p)
5489 it->stop_charpos = CHARPOS (pos);
5490 }
5491
5492
5493 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5494 If S is non-null, it is a C string to iterate over. Otherwise,
5495 STRING gives a Lisp string to iterate over.
5496
5497 If PRECISION > 0, don't return more then PRECISION number of
5498 characters from the string.
5499
5500 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5501 characters have been returned. FIELD_WIDTH < 0 means an infinite
5502 field width.
5503
5504 MULTIBYTE = 0 means disable processing of multibyte characters,
5505 MULTIBYTE > 0 means enable it,
5506 MULTIBYTE < 0 means use IT->multibyte_p.
5507
5508 IT must be initialized via a prior call to init_iterator before
5509 calling this function. */
5510
5511 static void
5512 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5513 struct it *it;
5514 unsigned char *s;
5515 Lisp_Object string;
5516 int charpos;
5517 int precision, field_width, multibyte;
5518 {
5519 /* No region in strings. */
5520 it->region_beg_charpos = it->region_end_charpos = -1;
5521
5522 /* No text property checks performed by default, but see below. */
5523 it->stop_charpos = -1;
5524
5525 /* Set iterator position and end position. */
5526 bzero (&it->current, sizeof it->current);
5527 it->current.overlay_string_index = -1;
5528 it->current.dpvec_index = -1;
5529 xassert (charpos >= 0);
5530
5531 /* If STRING is specified, use its multibyteness, otherwise use the
5532 setting of MULTIBYTE, if specified. */
5533 if (multibyte >= 0)
5534 it->multibyte_p = multibyte > 0;
5535
5536 if (s == NULL)
5537 {
5538 xassert (STRINGP (string));
5539 it->string = string;
5540 it->s = NULL;
5541 it->end_charpos = it->string_nchars = SCHARS (string);
5542 it->method = GET_FROM_STRING;
5543 it->current.string_pos = string_pos (charpos, string);
5544 }
5545 else
5546 {
5547 it->s = s;
5548 it->string = Qnil;
5549
5550 /* Note that we use IT->current.pos, not it->current.string_pos,
5551 for displaying C strings. */
5552 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5553 if (it->multibyte_p)
5554 {
5555 it->current.pos = c_string_pos (charpos, s, 1);
5556 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5557 }
5558 else
5559 {
5560 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5561 it->end_charpos = it->string_nchars = strlen (s);
5562 }
5563
5564 it->method = GET_FROM_C_STRING;
5565 }
5566
5567 /* PRECISION > 0 means don't return more than PRECISION characters
5568 from the string. */
5569 if (precision > 0 && it->end_charpos - charpos > precision)
5570 it->end_charpos = it->string_nchars = charpos + precision;
5571
5572 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5573 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5574 FIELD_WIDTH < 0 means infinite field width. This is useful for
5575 padding with `-' at the end of a mode line. */
5576 if (field_width < 0)
5577 field_width = INFINITY;
5578 if (field_width > it->end_charpos - charpos)
5579 it->end_charpos = charpos + field_width;
5580
5581 /* Use the standard display table for displaying strings. */
5582 if (DISP_TABLE_P (Vstandard_display_table))
5583 it->dp = XCHAR_TABLE (Vstandard_display_table);
5584
5585 it->stop_charpos = charpos;
5586 if (s == NULL && it->multibyte_p)
5587 {
5588 EMACS_INT endpos = SCHARS (it->string);
5589 if (endpos > it->end_charpos)
5590 endpos = it->end_charpos;
5591 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5592 it->string);
5593 }
5594 CHECK_IT (it);
5595 }
5596
5597
5598 \f
5599 /***********************************************************************
5600 Iteration
5601 ***********************************************************************/
5602
5603 /* Map enum it_method value to corresponding next_element_from_* function. */
5604
5605 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5606 {
5607 next_element_from_buffer,
5608 next_element_from_display_vector,
5609 next_element_from_string,
5610 next_element_from_c_string,
5611 next_element_from_image,
5612 next_element_from_stretch
5613 };
5614
5615 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5616
5617
5618 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5619 (possibly with the following characters). */
5620
5621 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5622 ((IT)->cmp_it.id >= 0 \
5623 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5624 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5625 END_CHARPOS, (IT)->w, \
5626 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5627 (IT)->string)))
5628
5629
5630 /* Load IT's display element fields with information about the next
5631 display element from the current position of IT. Value is zero if
5632 end of buffer (or C string) is reached. */
5633
5634 static struct frame *last_escape_glyph_frame = NULL;
5635 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5636 static int last_escape_glyph_merged_face_id = 0;
5637
5638 int
5639 get_next_display_element (it)
5640 struct it *it;
5641 {
5642 /* Non-zero means that we found a display element. Zero means that
5643 we hit the end of what we iterate over. Performance note: the
5644 function pointer `method' used here turns out to be faster than
5645 using a sequence of if-statements. */
5646 int success_p;
5647
5648 get_next:
5649 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5650
5651 if (it->what == IT_CHARACTER)
5652 {
5653 /* Map via display table or translate control characters.
5654 IT->c, IT->len etc. have been set to the next character by
5655 the function call above. If we have a display table, and it
5656 contains an entry for IT->c, translate it. Don't do this if
5657 IT->c itself comes from a display table, otherwise we could
5658 end up in an infinite recursion. (An alternative could be to
5659 count the recursion depth of this function and signal an
5660 error when a certain maximum depth is reached.) Is it worth
5661 it? */
5662 if (success_p && it->dpvec == NULL)
5663 {
5664 Lisp_Object dv;
5665 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
5666 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
5667 nbsp_or_shy = char_is_other;
5668 int c = it->c; /* This is the character to display. */
5669
5670 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
5671 {
5672 xassert (SINGLE_BYTE_CHAR_P (c));
5673 if (unibyte_display_via_language_environment)
5674 {
5675 c = DECODE_CHAR (unibyte, c);
5676 if (c < 0)
5677 c = BYTE8_TO_CHAR (it->c);
5678 }
5679 else
5680 c = BYTE8_TO_CHAR (it->c);
5681 }
5682
5683 if (it->dp
5684 && (dv = DISP_CHAR_VECTOR (it->dp, c),
5685 VECTORP (dv)))
5686 {
5687 struct Lisp_Vector *v = XVECTOR (dv);
5688
5689 /* Return the first character from the display table
5690 entry, if not empty. If empty, don't display the
5691 current character. */
5692 if (v->size)
5693 {
5694 it->dpvec_char_len = it->len;
5695 it->dpvec = v->contents;
5696 it->dpend = v->contents + v->size;
5697 it->current.dpvec_index = 0;
5698 it->dpvec_face_id = -1;
5699 it->saved_face_id = it->face_id;
5700 it->method = GET_FROM_DISPLAY_VECTOR;
5701 it->ellipsis_p = 0;
5702 }
5703 else
5704 {
5705 set_iterator_to_next (it, 0);
5706 }
5707 goto get_next;
5708 }
5709
5710 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
5711 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
5712 : c == 0xAD ? char_is_soft_hyphen
5713 : char_is_other);
5714
5715 /* Translate control characters into `\003' or `^C' form.
5716 Control characters coming from a display table entry are
5717 currently not translated because we use IT->dpvec to hold
5718 the translation. This could easily be changed but I
5719 don't believe that it is worth doing.
5720
5721 NBSP and SOFT-HYPEN are property translated too.
5722
5723 Non-printable characters and raw-byte characters are also
5724 translated to octal form. */
5725 if (((c < ' ' || c == 127) /* ASCII control chars */
5726 ? (it->area != TEXT_AREA
5727 /* In mode line, treat \n, \t like other crl chars. */
5728 || (c != '\t'
5729 && it->glyph_row
5730 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
5731 || (c != '\n' && c != '\t'))
5732 : (nbsp_or_shy
5733 || CHAR_BYTE8_P (c)
5734 || ! CHAR_PRINTABLE_P (c))))
5735 {
5736 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
5737 or a non-printable character which must be displayed
5738 either as '\003' or as `^C' where the '\\' and '^'
5739 can be defined in the display table. Fill
5740 IT->ctl_chars with glyphs for what we have to
5741 display. Then, set IT->dpvec to these glyphs. */
5742 Lisp_Object gc;
5743 int ctl_len;
5744 int face_id, lface_id = 0 ;
5745 int escape_glyph;
5746
5747 /* Handle control characters with ^. */
5748
5749 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
5750 {
5751 int g;
5752
5753 g = '^'; /* default glyph for Control */
5754 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5755 if (it->dp
5756 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5757 && GLYPH_CODE_CHAR_VALID_P (gc))
5758 {
5759 g = GLYPH_CODE_CHAR (gc);
5760 lface_id = GLYPH_CODE_FACE (gc);
5761 }
5762 if (lface_id)
5763 {
5764 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5765 }
5766 else if (it->f == last_escape_glyph_frame
5767 && it->face_id == last_escape_glyph_face_id)
5768 {
5769 face_id = last_escape_glyph_merged_face_id;
5770 }
5771 else
5772 {
5773 /* Merge the escape-glyph face into the current face. */
5774 face_id = merge_faces (it->f, Qescape_glyph, 0,
5775 it->face_id);
5776 last_escape_glyph_frame = it->f;
5777 last_escape_glyph_face_id = it->face_id;
5778 last_escape_glyph_merged_face_id = face_id;
5779 }
5780
5781 XSETINT (it->ctl_chars[0], g);
5782 XSETINT (it->ctl_chars[1], c ^ 0100);
5783 ctl_len = 2;
5784 goto display_control;
5785 }
5786
5787 /* Handle non-break space in the mode where it only gets
5788 highlighting. */
5789
5790 if (EQ (Vnobreak_char_display, Qt)
5791 && nbsp_or_shy == char_is_nbsp)
5792 {
5793 /* Merge the no-break-space face into the current face. */
5794 face_id = merge_faces (it->f, Qnobreak_space, 0,
5795 it->face_id);
5796
5797 c = ' ';
5798 XSETINT (it->ctl_chars[0], ' ');
5799 ctl_len = 1;
5800 goto display_control;
5801 }
5802
5803 /* Handle sequences that start with the "escape glyph". */
5804
5805 /* the default escape glyph is \. */
5806 escape_glyph = '\\';
5807
5808 if (it->dp
5809 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5810 && GLYPH_CODE_CHAR_VALID_P (gc))
5811 {
5812 escape_glyph = GLYPH_CODE_CHAR (gc);
5813 lface_id = GLYPH_CODE_FACE (gc);
5814 }
5815 if (lface_id)
5816 {
5817 /* The display table specified a face.
5818 Merge it into face_id and also into escape_glyph. */
5819 face_id = merge_faces (it->f, Qt, lface_id,
5820 it->face_id);
5821 }
5822 else if (it->f == last_escape_glyph_frame
5823 && it->face_id == last_escape_glyph_face_id)
5824 {
5825 face_id = last_escape_glyph_merged_face_id;
5826 }
5827 else
5828 {
5829 /* Merge the escape-glyph face into the current face. */
5830 face_id = merge_faces (it->f, Qescape_glyph, 0,
5831 it->face_id);
5832 last_escape_glyph_frame = it->f;
5833 last_escape_glyph_face_id = it->face_id;
5834 last_escape_glyph_merged_face_id = face_id;
5835 }
5836
5837 /* Handle soft hyphens in the mode where they only get
5838 highlighting. */
5839
5840 if (EQ (Vnobreak_char_display, Qt)
5841 && nbsp_or_shy == char_is_soft_hyphen)
5842 {
5843 XSETINT (it->ctl_chars[0], '-');
5844 ctl_len = 1;
5845 goto display_control;
5846 }
5847
5848 /* Handle non-break space and soft hyphen
5849 with the escape glyph. */
5850
5851 if (nbsp_or_shy)
5852 {
5853 XSETINT (it->ctl_chars[0], escape_glyph);
5854 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
5855 XSETINT (it->ctl_chars[1], c);
5856 ctl_len = 2;
5857 goto display_control;
5858 }
5859
5860 {
5861 char str[10];
5862 int len, i;
5863
5864 if (CHAR_BYTE8_P (c))
5865 /* Display \200 instead of \17777600. */
5866 c = CHAR_TO_BYTE8 (c);
5867 len = sprintf (str, "%03o", c);
5868
5869 XSETINT (it->ctl_chars[0], escape_glyph);
5870 for (i = 0; i < len; i++)
5871 XSETINT (it->ctl_chars[i + 1], str[i]);
5872 ctl_len = len + 1;
5873 }
5874
5875 display_control:
5876 /* Set up IT->dpvec and return first character from it. */
5877 it->dpvec_char_len = it->len;
5878 it->dpvec = it->ctl_chars;
5879 it->dpend = it->dpvec + ctl_len;
5880 it->current.dpvec_index = 0;
5881 it->dpvec_face_id = face_id;
5882 it->saved_face_id = it->face_id;
5883 it->method = GET_FROM_DISPLAY_VECTOR;
5884 it->ellipsis_p = 0;
5885 goto get_next;
5886 }
5887 it->char_to_display = c;
5888 }
5889 else if (success_p)
5890 {
5891 it->char_to_display = it->c;
5892 }
5893 }
5894
5895 #ifdef HAVE_WINDOW_SYSTEM
5896 /* Adjust face id for a multibyte character. There are no multibyte
5897 character in unibyte text. */
5898 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
5899 && it->multibyte_p
5900 && success_p
5901 && FRAME_WINDOW_P (it->f))
5902 {
5903 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5904
5905 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
5906 {
5907 /* Automatic composition with glyph-string. */
5908 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
5909
5910 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
5911 }
5912 else
5913 {
5914 int pos = (it->s ? -1
5915 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
5916 : IT_CHARPOS (*it));
5917
5918 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
5919 it->string);
5920 }
5921 }
5922 #endif
5923
5924 /* Is this character the last one of a run of characters with
5925 box? If yes, set IT->end_of_box_run_p to 1. */
5926 if (it->face_box_p
5927 && it->s == NULL)
5928 {
5929 if (it->method == GET_FROM_STRING && it->sp)
5930 {
5931 int face_id = underlying_face_id (it);
5932 struct face *face = FACE_FROM_ID (it->f, face_id);
5933
5934 if (face)
5935 {
5936 if (face->box == FACE_NO_BOX)
5937 {
5938 /* If the box comes from face properties in a
5939 display string, check faces in that string. */
5940 int string_face_id = face_after_it_pos (it);
5941 it->end_of_box_run_p
5942 = (FACE_FROM_ID (it->f, string_face_id)->box
5943 == FACE_NO_BOX);
5944 }
5945 /* Otherwise, the box comes from the underlying face.
5946 If this is the last string character displayed, check
5947 the next buffer location. */
5948 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
5949 && (it->current.overlay_string_index
5950 == it->n_overlay_strings - 1))
5951 {
5952 EMACS_INT ignore;
5953 int next_face_id;
5954 struct text_pos pos = it->current.pos;
5955 INC_TEXT_POS (pos, it->multibyte_p);
5956
5957 next_face_id = face_at_buffer_position
5958 (it->w, CHARPOS (pos), it->region_beg_charpos,
5959 it->region_end_charpos, &ignore,
5960 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
5961 -1);
5962 it->end_of_box_run_p
5963 = (FACE_FROM_ID (it->f, next_face_id)->box
5964 == FACE_NO_BOX);
5965 }
5966 }
5967 }
5968 else
5969 {
5970 int face_id = face_after_it_pos (it);
5971 it->end_of_box_run_p
5972 = (face_id != it->face_id
5973 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
5974 }
5975 }
5976
5977 /* Value is 0 if end of buffer or string reached. */
5978 return success_p;
5979 }
5980
5981
5982 /* Move IT to the next display element.
5983
5984 RESEAT_P non-zero means if called on a newline in buffer text,
5985 skip to the next visible line start.
5986
5987 Functions get_next_display_element and set_iterator_to_next are
5988 separate because I find this arrangement easier to handle than a
5989 get_next_display_element function that also increments IT's
5990 position. The way it is we can first look at an iterator's current
5991 display element, decide whether it fits on a line, and if it does,
5992 increment the iterator position. The other way around we probably
5993 would either need a flag indicating whether the iterator has to be
5994 incremented the next time, or we would have to implement a
5995 decrement position function which would not be easy to write. */
5996
5997 void
5998 set_iterator_to_next (it, reseat_p)
5999 struct it *it;
6000 int reseat_p;
6001 {
6002 /* Reset flags indicating start and end of a sequence of characters
6003 with box. Reset them at the start of this function because
6004 moving the iterator to a new position might set them. */
6005 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6006
6007 switch (it->method)
6008 {
6009 case GET_FROM_BUFFER:
6010 /* The current display element of IT is a character from
6011 current_buffer. Advance in the buffer, and maybe skip over
6012 invisible lines that are so because of selective display. */
6013 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6014 reseat_at_next_visible_line_start (it, 0);
6015 else if (it->cmp_it.id >= 0)
6016 {
6017 IT_CHARPOS (*it) += it->cmp_it.nchars;
6018 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6019 if (it->cmp_it.to < it->cmp_it.nglyphs)
6020 it->cmp_it.from = it->cmp_it.to;
6021 else
6022 {
6023 it->cmp_it.id = -1;
6024 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6025 IT_BYTEPOS (*it), it->stop_charpos,
6026 Qnil);
6027 }
6028 }
6029 else
6030 {
6031 xassert (it->len != 0);
6032 IT_BYTEPOS (*it) += it->len;
6033 IT_CHARPOS (*it) += 1;
6034 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6035 }
6036 break;
6037
6038 case GET_FROM_C_STRING:
6039 /* Current display element of IT is from a C string. */
6040 IT_BYTEPOS (*it) += it->len;
6041 IT_CHARPOS (*it) += 1;
6042 break;
6043
6044 case GET_FROM_DISPLAY_VECTOR:
6045 /* Current display element of IT is from a display table entry.
6046 Advance in the display table definition. Reset it to null if
6047 end reached, and continue with characters from buffers/
6048 strings. */
6049 ++it->current.dpvec_index;
6050
6051 /* Restore face of the iterator to what they were before the
6052 display vector entry (these entries may contain faces). */
6053 it->face_id = it->saved_face_id;
6054
6055 if (it->dpvec + it->current.dpvec_index == it->dpend)
6056 {
6057 int recheck_faces = it->ellipsis_p;
6058
6059 if (it->s)
6060 it->method = GET_FROM_C_STRING;
6061 else if (STRINGP (it->string))
6062 it->method = GET_FROM_STRING;
6063 else
6064 {
6065 it->method = GET_FROM_BUFFER;
6066 it->object = it->w->buffer;
6067 }
6068
6069 it->dpvec = NULL;
6070 it->current.dpvec_index = -1;
6071
6072 /* Skip over characters which were displayed via IT->dpvec. */
6073 if (it->dpvec_char_len < 0)
6074 reseat_at_next_visible_line_start (it, 1);
6075 else if (it->dpvec_char_len > 0)
6076 {
6077 if (it->method == GET_FROM_STRING
6078 && it->n_overlay_strings > 0)
6079 it->ignore_overlay_strings_at_pos_p = 1;
6080 it->len = it->dpvec_char_len;
6081 set_iterator_to_next (it, reseat_p);
6082 }
6083
6084 /* Maybe recheck faces after display vector */
6085 if (recheck_faces)
6086 it->stop_charpos = IT_CHARPOS (*it);
6087 }
6088 break;
6089
6090 case GET_FROM_STRING:
6091 /* Current display element is a character from a Lisp string. */
6092 xassert (it->s == NULL && STRINGP (it->string));
6093 if (it->cmp_it.id >= 0)
6094 {
6095 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6096 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6097 if (it->cmp_it.to < it->cmp_it.nglyphs)
6098 it->cmp_it.from = it->cmp_it.to;
6099 else
6100 {
6101 it->cmp_it.id = -1;
6102 composition_compute_stop_pos (&it->cmp_it,
6103 IT_STRING_CHARPOS (*it),
6104 IT_STRING_BYTEPOS (*it),
6105 it->stop_charpos, it->string);
6106 }
6107 }
6108 else
6109 {
6110 IT_STRING_BYTEPOS (*it) += it->len;
6111 IT_STRING_CHARPOS (*it) += 1;
6112 }
6113
6114 consider_string_end:
6115
6116 if (it->current.overlay_string_index >= 0)
6117 {
6118 /* IT->string is an overlay string. Advance to the
6119 next, if there is one. */
6120 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6121 {
6122 it->ellipsis_p = 0;
6123 next_overlay_string (it);
6124 if (it->ellipsis_p)
6125 setup_for_ellipsis (it, 0);
6126 }
6127 }
6128 else
6129 {
6130 /* IT->string is not an overlay string. If we reached
6131 its end, and there is something on IT->stack, proceed
6132 with what is on the stack. This can be either another
6133 string, this time an overlay string, or a buffer. */
6134 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6135 && it->sp > 0)
6136 {
6137 pop_it (it);
6138 if (it->method == GET_FROM_STRING)
6139 goto consider_string_end;
6140 }
6141 }
6142 break;
6143
6144 case GET_FROM_IMAGE:
6145 case GET_FROM_STRETCH:
6146 /* The position etc with which we have to proceed are on
6147 the stack. The position may be at the end of a string,
6148 if the `display' property takes up the whole string. */
6149 xassert (it->sp > 0);
6150 pop_it (it);
6151 if (it->method == GET_FROM_STRING)
6152 goto consider_string_end;
6153 break;
6154
6155 default:
6156 /* There are no other methods defined, so this should be a bug. */
6157 abort ();
6158 }
6159
6160 xassert (it->method != GET_FROM_STRING
6161 || (STRINGP (it->string)
6162 && IT_STRING_CHARPOS (*it) >= 0));
6163 }
6164
6165 /* Load IT's display element fields with information about the next
6166 display element which comes from a display table entry or from the
6167 result of translating a control character to one of the forms `^C'
6168 or `\003'.
6169
6170 IT->dpvec holds the glyphs to return as characters.
6171 IT->saved_face_id holds the face id before the display vector--it
6172 is restored into IT->face_id in set_iterator_to_next. */
6173
6174 static int
6175 next_element_from_display_vector (it)
6176 struct it *it;
6177 {
6178 Lisp_Object gc;
6179
6180 /* Precondition. */
6181 xassert (it->dpvec && it->current.dpvec_index >= 0);
6182
6183 it->face_id = it->saved_face_id;
6184
6185 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6186 That seemed totally bogus - so I changed it... */
6187 gc = it->dpvec[it->current.dpvec_index];
6188
6189 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6190 {
6191 it->c = GLYPH_CODE_CHAR (gc);
6192 it->len = CHAR_BYTES (it->c);
6193
6194 /* The entry may contain a face id to use. Such a face id is
6195 the id of a Lisp face, not a realized face. A face id of
6196 zero means no face is specified. */
6197 if (it->dpvec_face_id >= 0)
6198 it->face_id = it->dpvec_face_id;
6199 else
6200 {
6201 int lface_id = GLYPH_CODE_FACE (gc);
6202 if (lface_id > 0)
6203 it->face_id = merge_faces (it->f, Qt, lface_id,
6204 it->saved_face_id);
6205 }
6206 }
6207 else
6208 /* Display table entry is invalid. Return a space. */
6209 it->c = ' ', it->len = 1;
6210
6211 /* Don't change position and object of the iterator here. They are
6212 still the values of the character that had this display table
6213 entry or was translated, and that's what we want. */
6214 it->what = IT_CHARACTER;
6215 return 1;
6216 }
6217
6218
6219 /* Load IT with the next display element from Lisp string IT->string.
6220 IT->current.string_pos is the current position within the string.
6221 If IT->current.overlay_string_index >= 0, the Lisp string is an
6222 overlay string. */
6223
6224 static int
6225 next_element_from_string (it)
6226 struct it *it;
6227 {
6228 struct text_pos position;
6229
6230 xassert (STRINGP (it->string));
6231 xassert (IT_STRING_CHARPOS (*it) >= 0);
6232 position = it->current.string_pos;
6233
6234 /* Time to check for invisible text? */
6235 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6236 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6237 {
6238 handle_stop (it);
6239
6240 /* Since a handler may have changed IT->method, we must
6241 recurse here. */
6242 return GET_NEXT_DISPLAY_ELEMENT (it);
6243 }
6244
6245 if (it->current.overlay_string_index >= 0)
6246 {
6247 /* Get the next character from an overlay string. In overlay
6248 strings, There is no field width or padding with spaces to
6249 do. */
6250 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6251 {
6252 it->what = IT_EOB;
6253 return 0;
6254 }
6255 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6256 IT_STRING_BYTEPOS (*it), SCHARS (it->string))
6257 && next_element_from_composition (it))
6258 {
6259 return 1;
6260 }
6261 else if (STRING_MULTIBYTE (it->string))
6262 {
6263 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6264 const unsigned char *s = (SDATA (it->string)
6265 + IT_STRING_BYTEPOS (*it));
6266 it->c = string_char_and_length (s, &it->len);
6267 }
6268 else
6269 {
6270 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6271 it->len = 1;
6272 }
6273 }
6274 else
6275 {
6276 /* Get the next character from a Lisp string that is not an
6277 overlay string. Such strings come from the mode line, for
6278 example. We may have to pad with spaces, or truncate the
6279 string. See also next_element_from_c_string. */
6280 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6281 {
6282 it->what = IT_EOB;
6283 return 0;
6284 }
6285 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6286 {
6287 /* Pad with spaces. */
6288 it->c = ' ', it->len = 1;
6289 CHARPOS (position) = BYTEPOS (position) = -1;
6290 }
6291 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
6292 IT_STRING_BYTEPOS (*it), it->string_nchars)
6293 && next_element_from_composition (it))
6294 {
6295 return 1;
6296 }
6297 else if (STRING_MULTIBYTE (it->string))
6298 {
6299 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6300 const unsigned char *s = (SDATA (it->string)
6301 + IT_STRING_BYTEPOS (*it));
6302 it->c = string_char_and_length (s, &it->len);
6303 }
6304 else
6305 {
6306 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6307 it->len = 1;
6308 }
6309 }
6310
6311 /* Record what we have and where it came from. */
6312 it->what = IT_CHARACTER;
6313 it->object = it->string;
6314 it->position = position;
6315 return 1;
6316 }
6317
6318
6319 /* Load IT with next display element from C string IT->s.
6320 IT->string_nchars is the maximum number of characters to return
6321 from the string. IT->end_charpos may be greater than
6322 IT->string_nchars when this function is called, in which case we
6323 may have to return padding spaces. Value is zero if end of string
6324 reached, including padding spaces. */
6325
6326 static int
6327 next_element_from_c_string (it)
6328 struct it *it;
6329 {
6330 int success_p = 1;
6331
6332 xassert (it->s);
6333 it->what = IT_CHARACTER;
6334 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6335 it->object = Qnil;
6336
6337 /* IT's position can be greater IT->string_nchars in case a field
6338 width or precision has been specified when the iterator was
6339 initialized. */
6340 if (IT_CHARPOS (*it) >= it->end_charpos)
6341 {
6342 /* End of the game. */
6343 it->what = IT_EOB;
6344 success_p = 0;
6345 }
6346 else if (IT_CHARPOS (*it) >= it->string_nchars)
6347 {
6348 /* Pad with spaces. */
6349 it->c = ' ', it->len = 1;
6350 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6351 }
6352 else if (it->multibyte_p)
6353 {
6354 /* Implementation note: The calls to strlen apparently aren't a
6355 performance problem because there is no noticeable performance
6356 difference between Emacs running in unibyte or multibyte mode. */
6357 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6358 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
6359 }
6360 else
6361 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6362
6363 return success_p;
6364 }
6365
6366
6367 /* Set up IT to return characters from an ellipsis, if appropriate.
6368 The definition of the ellipsis glyphs may come from a display table
6369 entry. This function fills IT with the first glyph from the
6370 ellipsis if an ellipsis is to be displayed. */
6371
6372 static int
6373 next_element_from_ellipsis (it)
6374 struct it *it;
6375 {
6376 if (it->selective_display_ellipsis_p)
6377 setup_for_ellipsis (it, it->len);
6378 else
6379 {
6380 /* The face at the current position may be different from the
6381 face we find after the invisible text. Remember what it
6382 was in IT->saved_face_id, and signal that it's there by
6383 setting face_before_selective_p. */
6384 it->saved_face_id = it->face_id;
6385 it->method = GET_FROM_BUFFER;
6386 it->object = it->w->buffer;
6387 reseat_at_next_visible_line_start (it, 1);
6388 it->face_before_selective_p = 1;
6389 }
6390
6391 return GET_NEXT_DISPLAY_ELEMENT (it);
6392 }
6393
6394
6395 /* Deliver an image display element. The iterator IT is already
6396 filled with image information (done in handle_display_prop). Value
6397 is always 1. */
6398
6399
6400 static int
6401 next_element_from_image (it)
6402 struct it *it;
6403 {
6404 it->what = IT_IMAGE;
6405 it->ignore_overlay_strings_at_pos_p = 0;
6406 return 1;
6407 }
6408
6409
6410 /* Fill iterator IT with next display element from a stretch glyph
6411 property. IT->object is the value of the text property. Value is
6412 always 1. */
6413
6414 static int
6415 next_element_from_stretch (it)
6416 struct it *it;
6417 {
6418 it->what = IT_STRETCH;
6419 return 1;
6420 }
6421
6422
6423 /* Load IT with the next display element from current_buffer. Value
6424 is zero if end of buffer reached. IT->stop_charpos is the next
6425 position at which to stop and check for text properties or buffer
6426 end. */
6427
6428 static int
6429 next_element_from_buffer (it)
6430 struct it *it;
6431 {
6432 int success_p = 1;
6433
6434 xassert (IT_CHARPOS (*it) >= BEGV);
6435
6436 if (IT_CHARPOS (*it) >= it->stop_charpos)
6437 {
6438 if (IT_CHARPOS (*it) >= it->end_charpos)
6439 {
6440 int overlay_strings_follow_p;
6441
6442 /* End of the game, except when overlay strings follow that
6443 haven't been returned yet. */
6444 if (it->overlay_strings_at_end_processed_p)
6445 overlay_strings_follow_p = 0;
6446 else
6447 {
6448 it->overlay_strings_at_end_processed_p = 1;
6449 overlay_strings_follow_p = get_overlay_strings (it, 0);
6450 }
6451
6452 if (overlay_strings_follow_p)
6453 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6454 else
6455 {
6456 it->what = IT_EOB;
6457 it->position = it->current.pos;
6458 success_p = 0;
6459 }
6460 }
6461 else
6462 {
6463 handle_stop (it);
6464 return GET_NEXT_DISPLAY_ELEMENT (it);
6465 }
6466 }
6467 else
6468 {
6469 /* No face changes, overlays etc. in sight, so just return a
6470 character from current_buffer. */
6471 unsigned char *p;
6472
6473 /* Maybe run the redisplay end trigger hook. Performance note:
6474 This doesn't seem to cost measurable time. */
6475 if (it->redisplay_end_trigger_charpos
6476 && it->glyph_row
6477 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6478 run_redisplay_end_trigger_hook (it);
6479
6480 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
6481 it->end_charpos)
6482 && next_element_from_composition (it))
6483 {
6484 return 1;
6485 }
6486
6487 /* Get the next character, maybe multibyte. */
6488 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6489 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6490 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
6491 else
6492 it->c = *p, it->len = 1;
6493
6494 /* Record what we have and where it came from. */
6495 it->what = IT_CHARACTER;
6496 it->object = it->w->buffer;
6497 it->position = it->current.pos;
6498
6499 /* Normally we return the character found above, except when we
6500 really want to return an ellipsis for selective display. */
6501 if (it->selective)
6502 {
6503 if (it->c == '\n')
6504 {
6505 /* A value of selective > 0 means hide lines indented more
6506 than that number of columns. */
6507 if (it->selective > 0
6508 && IT_CHARPOS (*it) + 1 < ZV
6509 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6510 IT_BYTEPOS (*it) + 1,
6511 (double) it->selective)) /* iftc */
6512 {
6513 success_p = next_element_from_ellipsis (it);
6514 it->dpvec_char_len = -1;
6515 }
6516 }
6517 else if (it->c == '\r' && it->selective == -1)
6518 {
6519 /* A value of selective == -1 means that everything from the
6520 CR to the end of the line is invisible, with maybe an
6521 ellipsis displayed for it. */
6522 success_p = next_element_from_ellipsis (it);
6523 it->dpvec_char_len = -1;
6524 }
6525 }
6526 }
6527
6528 /* Value is zero if end of buffer reached. */
6529 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6530 return success_p;
6531 }
6532
6533
6534 /* Run the redisplay end trigger hook for IT. */
6535
6536 static void
6537 run_redisplay_end_trigger_hook (it)
6538 struct it *it;
6539 {
6540 Lisp_Object args[3];
6541
6542 /* IT->glyph_row should be non-null, i.e. we should be actually
6543 displaying something, or otherwise we should not run the hook. */
6544 xassert (it->glyph_row);
6545
6546 /* Set up hook arguments. */
6547 args[0] = Qredisplay_end_trigger_functions;
6548 args[1] = it->window;
6549 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6550 it->redisplay_end_trigger_charpos = 0;
6551
6552 /* Since we are *trying* to run these functions, don't try to run
6553 them again, even if they get an error. */
6554 it->w->redisplay_end_trigger = Qnil;
6555 Frun_hook_with_args (3, args);
6556
6557 /* Notice if it changed the face of the character we are on. */
6558 handle_face_prop (it);
6559 }
6560
6561
6562 /* Deliver a composition display element. Unlike the other
6563 next_element_from_XXX, this function is not registered in the array
6564 get_next_element[]. It is called from next_element_from_buffer and
6565 next_element_from_string when necessary. */
6566
6567 static int
6568 next_element_from_composition (it)
6569 struct it *it;
6570 {
6571 it->what = IT_COMPOSITION;
6572 it->len = it->cmp_it.nbytes;
6573 if (STRINGP (it->string))
6574 {
6575 if (it->c < 0)
6576 {
6577 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6578 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6579 return 0;
6580 }
6581 it->position = it->current.string_pos;
6582 it->object = it->string;
6583 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
6584 IT_STRING_BYTEPOS (*it), it->string);
6585 }
6586 else
6587 {
6588 if (it->c < 0)
6589 {
6590 IT_CHARPOS (*it) += it->cmp_it.nchars;
6591 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6592 return 0;
6593 }
6594 it->position = it->current.pos;
6595 it->object = it->w->buffer;
6596 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
6597 IT_BYTEPOS (*it), Qnil);
6598 }
6599 return 1;
6600 }
6601
6602
6603 \f
6604 /***********************************************************************
6605 Moving an iterator without producing glyphs
6606 ***********************************************************************/
6607
6608 /* Check if iterator is at a position corresponding to a valid buffer
6609 position after some move_it_ call. */
6610
6611 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6612 ((it)->method == GET_FROM_STRING \
6613 ? IT_STRING_CHARPOS (*it) == 0 \
6614 : 1)
6615
6616
6617 /* Move iterator IT to a specified buffer or X position within one
6618 line on the display without producing glyphs.
6619
6620 OP should be a bit mask including some or all of these bits:
6621 MOVE_TO_X: Stop on reaching x-position TO_X.
6622 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6623 Regardless of OP's value, stop in reaching the end of the display line.
6624
6625 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6626 This means, in particular, that TO_X includes window's horizontal
6627 scroll amount.
6628
6629 The return value has several possible values that
6630 say what condition caused the scan to stop:
6631
6632 MOVE_POS_MATCH_OR_ZV
6633 - when TO_POS or ZV was reached.
6634
6635 MOVE_X_REACHED
6636 -when TO_X was reached before TO_POS or ZV were reached.
6637
6638 MOVE_LINE_CONTINUED
6639 - when we reached the end of the display area and the line must
6640 be continued.
6641
6642 MOVE_LINE_TRUNCATED
6643 - when we reached the end of the display area and the line is
6644 truncated.
6645
6646 MOVE_NEWLINE_OR_CR
6647 - when we stopped at a line end, i.e. a newline or a CR and selective
6648 display is on. */
6649
6650 static enum move_it_result
6651 move_it_in_display_line_to (struct it *it,
6652 EMACS_INT to_charpos, int to_x,
6653 enum move_operation_enum op)
6654 {
6655 enum move_it_result result = MOVE_UNDEFINED;
6656 struct glyph_row *saved_glyph_row;
6657 struct it wrap_it, atpos_it, atx_it;
6658 int may_wrap = 0;
6659
6660 /* Don't produce glyphs in produce_glyphs. */
6661 saved_glyph_row = it->glyph_row;
6662 it->glyph_row = NULL;
6663
6664 /* Use wrap_it to save a copy of IT wherever a word wrap could
6665 occur. Use atpos_it to save a copy of IT at the desired buffer
6666 position, if found, so that we can scan ahead and check if the
6667 word later overshoots the window edge. Use atx_it similarly, for
6668 pixel positions. */
6669 wrap_it.sp = -1;
6670 atpos_it.sp = -1;
6671 atx_it.sp = -1;
6672
6673 #define BUFFER_POS_REACHED_P() \
6674 ((op & MOVE_TO_POS) != 0 \
6675 && BUFFERP (it->object) \
6676 && IT_CHARPOS (*it) >= to_charpos \
6677 && (it->method == GET_FROM_BUFFER \
6678 || (it->method == GET_FROM_DISPLAY_VECTOR \
6679 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6680
6681 /* If there's a line-/wrap-prefix, handle it. */
6682 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6683 && it->current_y < it->last_visible_y)
6684 handle_line_prefix (it);
6685
6686 while (1)
6687 {
6688 int x, i, ascent = 0, descent = 0;
6689
6690 /* Utility macro to reset an iterator with x, ascent, and descent. */
6691 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6692 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6693 (IT)->max_descent = descent)
6694
6695 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6696 glyph). */
6697 if ((op & MOVE_TO_POS) != 0
6698 && BUFFERP (it->object)
6699 && it->method == GET_FROM_BUFFER
6700 && IT_CHARPOS (*it) > to_charpos)
6701 {
6702 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6703 {
6704 result = MOVE_POS_MATCH_OR_ZV;
6705 break;
6706 }
6707 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6708 /* If wrap_it is valid, the current position might be in a
6709 word that is wrapped. So, save the iterator in
6710 atpos_it and continue to see if wrapping happens. */
6711 atpos_it = *it;
6712 }
6713
6714 /* Stop when ZV reached.
6715 We used to stop here when TO_CHARPOS reached as well, but that is
6716 too soon if this glyph does not fit on this line. So we handle it
6717 explicitly below. */
6718 if (!get_next_display_element (it))
6719 {
6720 result = MOVE_POS_MATCH_OR_ZV;
6721 break;
6722 }
6723
6724 if (it->line_wrap == TRUNCATE)
6725 {
6726 if (BUFFER_POS_REACHED_P ())
6727 {
6728 result = MOVE_POS_MATCH_OR_ZV;
6729 break;
6730 }
6731 }
6732 else
6733 {
6734 if (it->line_wrap == WORD_WRAP)
6735 {
6736 if (IT_DISPLAYING_WHITESPACE (it))
6737 may_wrap = 1;
6738 else if (may_wrap)
6739 {
6740 /* We have reached a glyph that follows one or more
6741 whitespace characters. If the position is
6742 already found, we are done. */
6743 if (atpos_it.sp >= 0)
6744 {
6745 *it = atpos_it;
6746 result = MOVE_POS_MATCH_OR_ZV;
6747 goto done;
6748 }
6749 if (atx_it.sp >= 0)
6750 {
6751 *it = atx_it;
6752 result = MOVE_X_REACHED;
6753 goto done;
6754 }
6755 /* Otherwise, we can wrap here. */
6756 wrap_it = *it;
6757 may_wrap = 0;
6758 }
6759 }
6760 }
6761
6762 /* Remember the line height for the current line, in case
6763 the next element doesn't fit on the line. */
6764 ascent = it->max_ascent;
6765 descent = it->max_descent;
6766
6767 /* The call to produce_glyphs will get the metrics of the
6768 display element IT is loaded with. Record the x-position
6769 before this display element, in case it doesn't fit on the
6770 line. */
6771 x = it->current_x;
6772
6773 PRODUCE_GLYPHS (it);
6774
6775 if (it->area != TEXT_AREA)
6776 {
6777 set_iterator_to_next (it, 1);
6778 continue;
6779 }
6780
6781 /* The number of glyphs we get back in IT->nglyphs will normally
6782 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6783 character on a terminal frame, or (iii) a line end. For the
6784 second case, IT->nglyphs - 1 padding glyphs will be present.
6785 (On X frames, there is only one glyph produced for a
6786 composite character.)
6787
6788 The behavior implemented below means, for continuation lines,
6789 that as many spaces of a TAB as fit on the current line are
6790 displayed there. For terminal frames, as many glyphs of a
6791 multi-glyph character are displayed in the current line, too.
6792 This is what the old redisplay code did, and we keep it that
6793 way. Under X, the whole shape of a complex character must
6794 fit on the line or it will be completely displayed in the
6795 next line.
6796
6797 Note that both for tabs and padding glyphs, all glyphs have
6798 the same width. */
6799 if (it->nglyphs)
6800 {
6801 /* More than one glyph or glyph doesn't fit on line. All
6802 glyphs have the same width. */
6803 int single_glyph_width = it->pixel_width / it->nglyphs;
6804 int new_x;
6805 int x_before_this_char = x;
6806 int hpos_before_this_char = it->hpos;
6807
6808 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6809 {
6810 new_x = x + single_glyph_width;
6811
6812 /* We want to leave anything reaching TO_X to the caller. */
6813 if ((op & MOVE_TO_X) && new_x > to_x)
6814 {
6815 if (BUFFER_POS_REACHED_P ())
6816 {
6817 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6818 goto buffer_pos_reached;
6819 if (atpos_it.sp < 0)
6820 {
6821 atpos_it = *it;
6822 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6823 }
6824 }
6825 else
6826 {
6827 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6828 {
6829 it->current_x = x;
6830 result = MOVE_X_REACHED;
6831 break;
6832 }
6833 if (atx_it.sp < 0)
6834 {
6835 atx_it = *it;
6836 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6837 }
6838 }
6839 }
6840
6841 if (/* Lines are continued. */
6842 it->line_wrap != TRUNCATE
6843 && (/* And glyph doesn't fit on the line. */
6844 new_x > it->last_visible_x
6845 /* Or it fits exactly and we're on a window
6846 system frame. */
6847 || (new_x == it->last_visible_x
6848 && FRAME_WINDOW_P (it->f))))
6849 {
6850 if (/* IT->hpos == 0 means the very first glyph
6851 doesn't fit on the line, e.g. a wide image. */
6852 it->hpos == 0
6853 || (new_x == it->last_visible_x
6854 && FRAME_WINDOW_P (it->f)))
6855 {
6856 ++it->hpos;
6857 it->current_x = new_x;
6858
6859 /* The character's last glyph just barely fits
6860 in this row. */
6861 if (i == it->nglyphs - 1)
6862 {
6863 /* If this is the destination position,
6864 return a position *before* it in this row,
6865 now that we know it fits in this row. */
6866 if (BUFFER_POS_REACHED_P ())
6867 {
6868 if (it->line_wrap != WORD_WRAP
6869 || wrap_it.sp < 0)
6870 {
6871 it->hpos = hpos_before_this_char;
6872 it->current_x = x_before_this_char;
6873 result = MOVE_POS_MATCH_OR_ZV;
6874 break;
6875 }
6876 if (it->line_wrap == WORD_WRAP
6877 && atpos_it.sp < 0)
6878 {
6879 atpos_it = *it;
6880 atpos_it.current_x = x_before_this_char;
6881 atpos_it.hpos = hpos_before_this_char;
6882 }
6883 }
6884
6885 set_iterator_to_next (it, 1);
6886 /* On graphical terminals, newlines may
6887 "overflow" into the fringe if
6888 overflow-newline-into-fringe is non-nil.
6889 On text-only terminals, newlines may
6890 overflow into the last glyph on the
6891 display line.*/
6892 if (!FRAME_WINDOW_P (it->f)
6893 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6894 {
6895 if (!get_next_display_element (it))
6896 {
6897 result = MOVE_POS_MATCH_OR_ZV;
6898 break;
6899 }
6900 if (BUFFER_POS_REACHED_P ())
6901 {
6902 if (ITERATOR_AT_END_OF_LINE_P (it))
6903 result = MOVE_POS_MATCH_OR_ZV;
6904 else
6905 result = MOVE_LINE_CONTINUED;
6906 break;
6907 }
6908 if (ITERATOR_AT_END_OF_LINE_P (it))
6909 {
6910 result = MOVE_NEWLINE_OR_CR;
6911 break;
6912 }
6913 }
6914 }
6915 }
6916 else
6917 IT_RESET_X_ASCENT_DESCENT (it);
6918
6919 if (wrap_it.sp >= 0)
6920 {
6921 *it = wrap_it;
6922 atpos_it.sp = -1;
6923 atx_it.sp = -1;
6924 }
6925
6926 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6927 IT_CHARPOS (*it)));
6928 result = MOVE_LINE_CONTINUED;
6929 break;
6930 }
6931
6932 if (BUFFER_POS_REACHED_P ())
6933 {
6934 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6935 goto buffer_pos_reached;
6936 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6937 {
6938 atpos_it = *it;
6939 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6940 }
6941 }
6942
6943 if (new_x > it->first_visible_x)
6944 {
6945 /* Glyph is visible. Increment number of glyphs that
6946 would be displayed. */
6947 ++it->hpos;
6948 }
6949 }
6950
6951 if (result != MOVE_UNDEFINED)
6952 break;
6953 }
6954 else if (BUFFER_POS_REACHED_P ())
6955 {
6956 buffer_pos_reached:
6957 IT_RESET_X_ASCENT_DESCENT (it);
6958 result = MOVE_POS_MATCH_OR_ZV;
6959 break;
6960 }
6961 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6962 {
6963 /* Stop when TO_X specified and reached. This check is
6964 necessary here because of lines consisting of a line end,
6965 only. The line end will not produce any glyphs and we
6966 would never get MOVE_X_REACHED. */
6967 xassert (it->nglyphs == 0);
6968 result = MOVE_X_REACHED;
6969 break;
6970 }
6971
6972 /* Is this a line end? If yes, we're done. */
6973 if (ITERATOR_AT_END_OF_LINE_P (it))
6974 {
6975 result = MOVE_NEWLINE_OR_CR;
6976 break;
6977 }
6978
6979 /* The current display element has been consumed. Advance
6980 to the next. */
6981 set_iterator_to_next (it, 1);
6982
6983 /* Stop if lines are truncated and IT's current x-position is
6984 past the right edge of the window now. */
6985 if (it->line_wrap == TRUNCATE
6986 && it->current_x >= it->last_visible_x)
6987 {
6988 if (!FRAME_WINDOW_P (it->f)
6989 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6990 {
6991 if (!get_next_display_element (it)
6992 || BUFFER_POS_REACHED_P ())
6993 {
6994 result = MOVE_POS_MATCH_OR_ZV;
6995 break;
6996 }
6997 if (ITERATOR_AT_END_OF_LINE_P (it))
6998 {
6999 result = MOVE_NEWLINE_OR_CR;
7000 break;
7001 }
7002 }
7003 result = MOVE_LINE_TRUNCATED;
7004 break;
7005 }
7006 #undef IT_RESET_X_ASCENT_DESCENT
7007 }
7008
7009 #undef BUFFER_POS_REACHED_P
7010
7011 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7012 restore the saved iterator. */
7013 if (atpos_it.sp >= 0)
7014 *it = atpos_it;
7015 else if (atx_it.sp >= 0)
7016 *it = atx_it;
7017
7018 done:
7019
7020 /* Restore the iterator settings altered at the beginning of this
7021 function. */
7022 it->glyph_row = saved_glyph_row;
7023 return result;
7024 }
7025
7026 /* For external use. */
7027 void
7028 move_it_in_display_line (struct it *it,
7029 EMACS_INT to_charpos, int to_x,
7030 enum move_operation_enum op)
7031 {
7032 if (it->line_wrap == WORD_WRAP
7033 && (op & MOVE_TO_X))
7034 {
7035 struct it save_it = *it;
7036 int skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7037 /* When word-wrap is on, TO_X may lie past the end
7038 of a wrapped line. Then it->current is the
7039 character on the next line, so backtrack to the
7040 space before the wrap point. */
7041 if (skip == MOVE_LINE_CONTINUED)
7042 {
7043 int prev_x = max (it->current_x - 1, 0);
7044 *it = save_it;
7045 move_it_in_display_line_to
7046 (it, -1, prev_x, MOVE_TO_X);
7047 }
7048 }
7049 else
7050 move_it_in_display_line_to (it, to_charpos, to_x, op);
7051 }
7052
7053
7054 /* Move IT forward until it satisfies one or more of the criteria in
7055 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7056
7057 OP is a bit-mask that specifies where to stop, and in particular,
7058 which of those four position arguments makes a difference. See the
7059 description of enum move_operation_enum.
7060
7061 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7062 screen line, this function will set IT to the next position >
7063 TO_CHARPOS. */
7064
7065 void
7066 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7067 struct it *it;
7068 int to_charpos, to_x, to_y, to_vpos;
7069 int op;
7070 {
7071 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7072 int line_height, line_start_x = 0, reached = 0;
7073
7074 for (;;)
7075 {
7076 if (op & MOVE_TO_VPOS)
7077 {
7078 /* If no TO_CHARPOS and no TO_X specified, stop at the
7079 start of the line TO_VPOS. */
7080 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7081 {
7082 if (it->vpos == to_vpos)
7083 {
7084 reached = 1;
7085 break;
7086 }
7087 else
7088 skip = move_it_in_display_line_to (it, -1, -1, 0);
7089 }
7090 else
7091 {
7092 /* TO_VPOS >= 0 means stop at TO_X in the line at
7093 TO_VPOS, or at TO_POS, whichever comes first. */
7094 if (it->vpos == to_vpos)
7095 {
7096 reached = 2;
7097 break;
7098 }
7099
7100 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7101
7102 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7103 {
7104 reached = 3;
7105 break;
7106 }
7107 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7108 {
7109 /* We have reached TO_X but not in the line we want. */
7110 skip = move_it_in_display_line_to (it, to_charpos,
7111 -1, MOVE_TO_POS);
7112 if (skip == MOVE_POS_MATCH_OR_ZV)
7113 {
7114 reached = 4;
7115 break;
7116 }
7117 }
7118 }
7119 }
7120 else if (op & MOVE_TO_Y)
7121 {
7122 struct it it_backup;
7123
7124 if (it->line_wrap == WORD_WRAP)
7125 it_backup = *it;
7126
7127 /* TO_Y specified means stop at TO_X in the line containing
7128 TO_Y---or at TO_CHARPOS if this is reached first. The
7129 problem is that we can't really tell whether the line
7130 contains TO_Y before we have completely scanned it, and
7131 this may skip past TO_X. What we do is to first scan to
7132 TO_X.
7133
7134 If TO_X is not specified, use a TO_X of zero. The reason
7135 is to make the outcome of this function more predictable.
7136 If we didn't use TO_X == 0, we would stop at the end of
7137 the line which is probably not what a caller would expect
7138 to happen. */
7139 skip = move_it_in_display_line_to
7140 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
7141 (MOVE_TO_X | (op & MOVE_TO_POS)));
7142
7143 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7144 if (skip == MOVE_POS_MATCH_OR_ZV)
7145 reached = 5;
7146 else if (skip == MOVE_X_REACHED)
7147 {
7148 /* If TO_X was reached, we want to know whether TO_Y is
7149 in the line. We know this is the case if the already
7150 scanned glyphs make the line tall enough. Otherwise,
7151 we must check by scanning the rest of the line. */
7152 line_height = it->max_ascent + it->max_descent;
7153 if (to_y >= it->current_y
7154 && to_y < it->current_y + line_height)
7155 {
7156 reached = 6;
7157 break;
7158 }
7159 it_backup = *it;
7160 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7161 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7162 op & MOVE_TO_POS);
7163 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7164 line_height = it->max_ascent + it->max_descent;
7165 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7166
7167 if (to_y >= it->current_y
7168 && to_y < it->current_y + line_height)
7169 {
7170 /* If TO_Y is in this line and TO_X was reached
7171 above, we scanned too far. We have to restore
7172 IT's settings to the ones before skipping. */
7173 *it = it_backup;
7174 reached = 6;
7175 }
7176 else
7177 {
7178 skip = skip2;
7179 if (skip == MOVE_POS_MATCH_OR_ZV)
7180 reached = 7;
7181 }
7182 }
7183 else
7184 {
7185 /* Check whether TO_Y is in this line. */
7186 line_height = it->max_ascent + it->max_descent;
7187 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7188
7189 if (to_y >= it->current_y
7190 && to_y < it->current_y + line_height)
7191 {
7192 /* When word-wrap is on, TO_X may lie past the end
7193 of a wrapped line. Then it->current is the
7194 character on the next line, so backtrack to the
7195 space before the wrap point. */
7196 if (skip == MOVE_LINE_CONTINUED
7197 && it->line_wrap == WORD_WRAP)
7198 {
7199 int prev_x = max (it->current_x - 1, 0);
7200 *it = it_backup;
7201 skip = move_it_in_display_line_to
7202 (it, -1, prev_x, MOVE_TO_X);
7203 }
7204 reached = 6;
7205 }
7206 }
7207
7208 if (reached)
7209 break;
7210 }
7211 else if (BUFFERP (it->object)
7212 && (it->method == GET_FROM_BUFFER
7213 || it->method == GET_FROM_STRETCH)
7214 && IT_CHARPOS (*it) >= to_charpos)
7215 skip = MOVE_POS_MATCH_OR_ZV;
7216 else
7217 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7218
7219 switch (skip)
7220 {
7221 case MOVE_POS_MATCH_OR_ZV:
7222 reached = 8;
7223 goto out;
7224
7225 case MOVE_NEWLINE_OR_CR:
7226 set_iterator_to_next (it, 1);
7227 it->continuation_lines_width = 0;
7228 break;
7229
7230 case MOVE_LINE_TRUNCATED:
7231 it->continuation_lines_width = 0;
7232 reseat_at_next_visible_line_start (it, 0);
7233 if ((op & MOVE_TO_POS) != 0
7234 && IT_CHARPOS (*it) > to_charpos)
7235 {
7236 reached = 9;
7237 goto out;
7238 }
7239 break;
7240
7241 case MOVE_LINE_CONTINUED:
7242 /* For continued lines ending in a tab, some of the glyphs
7243 associated with the tab are displayed on the current
7244 line. Since it->current_x does not include these glyphs,
7245 we use it->last_visible_x instead. */
7246 if (it->c == '\t')
7247 {
7248 it->continuation_lines_width += it->last_visible_x;
7249 /* When moving by vpos, ensure that the iterator really
7250 advances to the next line (bug#847, bug#969). Fixme:
7251 do we need to do this in other circumstances? */
7252 if (it->current_x != it->last_visible_x
7253 && (op & MOVE_TO_VPOS)
7254 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
7255 {
7256 line_start_x = it->current_x + it->pixel_width
7257 - it->last_visible_x;
7258 set_iterator_to_next (it, 0);
7259 }
7260 }
7261 else
7262 it->continuation_lines_width += it->current_x;
7263 break;
7264
7265 default:
7266 abort ();
7267 }
7268
7269 /* Reset/increment for the next run. */
7270 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7271 it->current_x = line_start_x;
7272 line_start_x = 0;
7273 it->hpos = 0;
7274 it->current_y += it->max_ascent + it->max_descent;
7275 ++it->vpos;
7276 last_height = it->max_ascent + it->max_descent;
7277 last_max_ascent = it->max_ascent;
7278 it->max_ascent = it->max_descent = 0;
7279 }
7280
7281 out:
7282
7283 /* On text terminals, we may stop at the end of a line in the middle
7284 of a multi-character glyph. If the glyph itself is continued,
7285 i.e. it is actually displayed on the next line, don't treat this
7286 stopping point as valid; move to the next line instead (unless
7287 that brings us offscreen). */
7288 if (!FRAME_WINDOW_P (it->f)
7289 && op & MOVE_TO_POS
7290 && IT_CHARPOS (*it) == to_charpos
7291 && it->what == IT_CHARACTER
7292 && it->nglyphs > 1
7293 && it->line_wrap == WINDOW_WRAP
7294 && it->current_x == it->last_visible_x - 1
7295 && it->c != '\n'
7296 && it->c != '\t'
7297 && it->vpos < XFASTINT (it->w->window_end_vpos))
7298 {
7299 it->continuation_lines_width += it->current_x;
7300 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
7301 it->current_y += it->max_ascent + it->max_descent;
7302 ++it->vpos;
7303 last_height = it->max_ascent + it->max_descent;
7304 last_max_ascent = it->max_ascent;
7305 }
7306
7307 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7308 }
7309
7310
7311 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7312
7313 If DY > 0, move IT backward at least that many pixels. DY = 0
7314 means move IT backward to the preceding line start or BEGV. This
7315 function may move over more than DY pixels if IT->current_y - DY
7316 ends up in the middle of a line; in this case IT->current_y will be
7317 set to the top of the line moved to. */
7318
7319 void
7320 move_it_vertically_backward (it, dy)
7321 struct it *it;
7322 int dy;
7323 {
7324 int nlines, h;
7325 struct it it2, it3;
7326 int start_pos;
7327
7328 move_further_back:
7329 xassert (dy >= 0);
7330
7331 start_pos = IT_CHARPOS (*it);
7332
7333 /* Estimate how many newlines we must move back. */
7334 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7335
7336 /* Set the iterator's position that many lines back. */
7337 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7338 back_to_previous_visible_line_start (it);
7339
7340 /* Reseat the iterator here. When moving backward, we don't want
7341 reseat to skip forward over invisible text, set up the iterator
7342 to deliver from overlay strings at the new position etc. So,
7343 use reseat_1 here. */
7344 reseat_1 (it, it->current.pos, 1);
7345
7346 /* We are now surely at a line start. */
7347 it->current_x = it->hpos = 0;
7348 it->continuation_lines_width = 0;
7349
7350 /* Move forward and see what y-distance we moved. First move to the
7351 start of the next line so that we get its height. We need this
7352 height to be able to tell whether we reached the specified
7353 y-distance. */
7354 it2 = *it;
7355 it2.max_ascent = it2.max_descent = 0;
7356 do
7357 {
7358 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7359 MOVE_TO_POS | MOVE_TO_VPOS);
7360 }
7361 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7362 xassert (IT_CHARPOS (*it) >= BEGV);
7363 it3 = it2;
7364
7365 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7366 xassert (IT_CHARPOS (*it) >= BEGV);
7367 /* H is the actual vertical distance from the position in *IT
7368 and the starting position. */
7369 h = it2.current_y - it->current_y;
7370 /* NLINES is the distance in number of lines. */
7371 nlines = it2.vpos - it->vpos;
7372
7373 /* Correct IT's y and vpos position
7374 so that they are relative to the starting point. */
7375 it->vpos -= nlines;
7376 it->current_y -= h;
7377
7378 if (dy == 0)
7379 {
7380 /* DY == 0 means move to the start of the screen line. The
7381 value of nlines is > 0 if continuation lines were involved. */
7382 if (nlines > 0)
7383 move_it_by_lines (it, nlines, 1);
7384 }
7385 else
7386 {
7387 /* The y-position we try to reach, relative to *IT.
7388 Note that H has been subtracted in front of the if-statement. */
7389 int target_y = it->current_y + h - dy;
7390 int y0 = it3.current_y;
7391 int y1 = line_bottom_y (&it3);
7392 int line_height = y1 - y0;
7393
7394 /* If we did not reach target_y, try to move further backward if
7395 we can. If we moved too far backward, try to move forward. */
7396 if (target_y < it->current_y
7397 /* This is heuristic. In a window that's 3 lines high, with
7398 a line height of 13 pixels each, recentering with point
7399 on the bottom line will try to move -39/2 = 19 pixels
7400 backward. Try to avoid moving into the first line. */
7401 && (it->current_y - target_y
7402 > min (window_box_height (it->w), line_height * 2 / 3))
7403 && IT_CHARPOS (*it) > BEGV)
7404 {
7405 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7406 target_y - it->current_y));
7407 dy = it->current_y - target_y;
7408 goto move_further_back;
7409 }
7410 else if (target_y >= it->current_y + line_height
7411 && IT_CHARPOS (*it) < ZV)
7412 {
7413 /* Should move forward by at least one line, maybe more.
7414
7415 Note: Calling move_it_by_lines can be expensive on
7416 terminal frames, where compute_motion is used (via
7417 vmotion) to do the job, when there are very long lines
7418 and truncate-lines is nil. That's the reason for
7419 treating terminal frames specially here. */
7420
7421 if (!FRAME_WINDOW_P (it->f))
7422 move_it_vertically (it, target_y - (it->current_y + line_height));
7423 else
7424 {
7425 do
7426 {
7427 move_it_by_lines (it, 1, 1);
7428 }
7429 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7430 }
7431 }
7432 }
7433 }
7434
7435
7436 /* Move IT by a specified amount of pixel lines DY. DY negative means
7437 move backwards. DY = 0 means move to start of screen line. At the
7438 end, IT will be on the start of a screen line. */
7439
7440 void
7441 move_it_vertically (it, dy)
7442 struct it *it;
7443 int dy;
7444 {
7445 if (dy <= 0)
7446 move_it_vertically_backward (it, -dy);
7447 else
7448 {
7449 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7450 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7451 MOVE_TO_POS | MOVE_TO_Y);
7452 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7453
7454 /* If buffer ends in ZV without a newline, move to the start of
7455 the line to satisfy the post-condition. */
7456 if (IT_CHARPOS (*it) == ZV
7457 && ZV > BEGV
7458 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7459 move_it_by_lines (it, 0, 0);
7460 }
7461 }
7462
7463
7464 /* Move iterator IT past the end of the text line it is in. */
7465
7466 void
7467 move_it_past_eol (it)
7468 struct it *it;
7469 {
7470 enum move_it_result rc;
7471
7472 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7473 if (rc == MOVE_NEWLINE_OR_CR)
7474 set_iterator_to_next (it, 0);
7475 }
7476
7477
7478 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7479 negative means move up. DVPOS == 0 means move to the start of the
7480 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7481 NEED_Y_P is zero, IT->current_y will be left unchanged.
7482
7483 Further optimization ideas: If we would know that IT->f doesn't use
7484 a face with proportional font, we could be faster for
7485 truncate-lines nil. */
7486
7487 void
7488 move_it_by_lines (it, dvpos, need_y_p)
7489 struct it *it;
7490 int dvpos, need_y_p;
7491 {
7492 struct position pos;
7493
7494 /* The commented-out optimization uses vmotion on terminals. This
7495 gives bad results, because elements like it->what, on which
7496 callers such as pos_visible_p rely, aren't updated. */
7497 /* if (!FRAME_WINDOW_P (it->f))
7498 {
7499 struct text_pos textpos;
7500
7501 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7502 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7503 reseat (it, textpos, 1);
7504 it->vpos += pos.vpos;
7505 it->current_y += pos.vpos;
7506 }
7507 else */
7508
7509 if (dvpos == 0)
7510 {
7511 /* DVPOS == 0 means move to the start of the screen line. */
7512 move_it_vertically_backward (it, 0);
7513 xassert (it->current_x == 0 && it->hpos == 0);
7514 /* Let next call to line_bottom_y calculate real line height */
7515 last_height = 0;
7516 }
7517 else if (dvpos > 0)
7518 {
7519 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7520 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7521 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7522 }
7523 else
7524 {
7525 struct it it2;
7526 int start_charpos, i;
7527
7528 /* Start at the beginning of the screen line containing IT's
7529 position. This may actually move vertically backwards,
7530 in case of overlays, so adjust dvpos accordingly. */
7531 dvpos += it->vpos;
7532 move_it_vertically_backward (it, 0);
7533 dvpos -= it->vpos;
7534
7535 /* Go back -DVPOS visible lines and reseat the iterator there. */
7536 start_charpos = IT_CHARPOS (*it);
7537 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7538 back_to_previous_visible_line_start (it);
7539 reseat (it, it->current.pos, 1);
7540
7541 /* Move further back if we end up in a string or an image. */
7542 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7543 {
7544 /* First try to move to start of display line. */
7545 dvpos += it->vpos;
7546 move_it_vertically_backward (it, 0);
7547 dvpos -= it->vpos;
7548 if (IT_POS_VALID_AFTER_MOVE_P (it))
7549 break;
7550 /* If start of line is still in string or image,
7551 move further back. */
7552 back_to_previous_visible_line_start (it);
7553 reseat (it, it->current.pos, 1);
7554 dvpos--;
7555 }
7556
7557 it->current_x = it->hpos = 0;
7558
7559 /* Above call may have moved too far if continuation lines
7560 are involved. Scan forward and see if it did. */
7561 it2 = *it;
7562 it2.vpos = it2.current_y = 0;
7563 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7564 it->vpos -= it2.vpos;
7565 it->current_y -= it2.current_y;
7566 it->current_x = it->hpos = 0;
7567
7568 /* If we moved too far back, move IT some lines forward. */
7569 if (it2.vpos > -dvpos)
7570 {
7571 int delta = it2.vpos + dvpos;
7572 it2 = *it;
7573 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7574 /* Move back again if we got too far ahead. */
7575 if (IT_CHARPOS (*it) >= start_charpos)
7576 *it = it2;
7577 }
7578 }
7579 }
7580
7581 /* Return 1 if IT points into the middle of a display vector. */
7582
7583 int
7584 in_display_vector_p (it)
7585 struct it *it;
7586 {
7587 return (it->method == GET_FROM_DISPLAY_VECTOR
7588 && it->current.dpvec_index > 0
7589 && it->dpvec + it->current.dpvec_index != it->dpend);
7590 }
7591
7592 \f
7593 /***********************************************************************
7594 Messages
7595 ***********************************************************************/
7596
7597
7598 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7599 to *Messages*. */
7600
7601 void
7602 add_to_log (format, arg1, arg2)
7603 char *format;
7604 Lisp_Object arg1, arg2;
7605 {
7606 Lisp_Object args[3];
7607 Lisp_Object msg, fmt;
7608 char *buffer;
7609 int len;
7610 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7611 USE_SAFE_ALLOCA;
7612
7613 /* Do nothing if called asynchronously. Inserting text into
7614 a buffer may call after-change-functions and alike and
7615 that would means running Lisp asynchronously. */
7616 if (handling_signal)
7617 return;
7618
7619 fmt = msg = Qnil;
7620 GCPRO4 (fmt, msg, arg1, arg2);
7621
7622 args[0] = fmt = build_string (format);
7623 args[1] = arg1;
7624 args[2] = arg2;
7625 msg = Fformat (3, args);
7626
7627 len = SBYTES (msg) + 1;
7628 SAFE_ALLOCA (buffer, char *, len);
7629 bcopy (SDATA (msg), buffer, len);
7630
7631 message_dolog (buffer, len - 1, 1, 0);
7632 SAFE_FREE ();
7633
7634 UNGCPRO;
7635 }
7636
7637
7638 /* Output a newline in the *Messages* buffer if "needs" one. */
7639
7640 void
7641 message_log_maybe_newline ()
7642 {
7643 if (message_log_need_newline)
7644 message_dolog ("", 0, 1, 0);
7645 }
7646
7647
7648 /* Add a string M of length NBYTES to the message log, optionally
7649 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7650 nonzero, means interpret the contents of M as multibyte. This
7651 function calls low-level routines in order to bypass text property
7652 hooks, etc. which might not be safe to run.
7653
7654 This may GC (insert may run before/after change hooks),
7655 so the buffer M must NOT point to a Lisp string. */
7656
7657 void
7658 message_dolog (m, nbytes, nlflag, multibyte)
7659 const char *m;
7660 int nbytes, nlflag, multibyte;
7661 {
7662 if (!NILP (Vmemory_full))
7663 return;
7664
7665 if (!NILP (Vmessage_log_max))
7666 {
7667 struct buffer *oldbuf;
7668 Lisp_Object oldpoint, oldbegv, oldzv;
7669 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7670 int point_at_end = 0;
7671 int zv_at_end = 0;
7672 Lisp_Object old_deactivate_mark, tem;
7673 struct gcpro gcpro1;
7674
7675 old_deactivate_mark = Vdeactivate_mark;
7676 oldbuf = current_buffer;
7677 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7678 current_buffer->undo_list = Qt;
7679
7680 oldpoint = message_dolog_marker1;
7681 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7682 oldbegv = message_dolog_marker2;
7683 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7684 oldzv = message_dolog_marker3;
7685 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7686 GCPRO1 (old_deactivate_mark);
7687
7688 if (PT == Z)
7689 point_at_end = 1;
7690 if (ZV == Z)
7691 zv_at_end = 1;
7692
7693 BEGV = BEG;
7694 BEGV_BYTE = BEG_BYTE;
7695 ZV = Z;
7696 ZV_BYTE = Z_BYTE;
7697 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7698
7699 /* Insert the string--maybe converting multibyte to single byte
7700 or vice versa, so that all the text fits the buffer. */
7701 if (multibyte
7702 && NILP (current_buffer->enable_multibyte_characters))
7703 {
7704 int i, c, char_bytes;
7705 unsigned char work[1];
7706
7707 /* Convert a multibyte string to single-byte
7708 for the *Message* buffer. */
7709 for (i = 0; i < nbytes; i += char_bytes)
7710 {
7711 c = string_char_and_length (m + i, &char_bytes);
7712 work[0] = (ASCII_CHAR_P (c)
7713 ? c
7714 : multibyte_char_to_unibyte (c, Qnil));
7715 insert_1_both (work, 1, 1, 1, 0, 0);
7716 }
7717 }
7718 else if (! multibyte
7719 && ! NILP (current_buffer->enable_multibyte_characters))
7720 {
7721 int i, c, char_bytes;
7722 unsigned char *msg = (unsigned char *) m;
7723 unsigned char str[MAX_MULTIBYTE_LENGTH];
7724 /* Convert a single-byte string to multibyte
7725 for the *Message* buffer. */
7726 for (i = 0; i < nbytes; i++)
7727 {
7728 c = msg[i];
7729 MAKE_CHAR_MULTIBYTE (c);
7730 char_bytes = CHAR_STRING (c, str);
7731 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7732 }
7733 }
7734 else if (nbytes)
7735 insert_1 (m, nbytes, 1, 0, 0);
7736
7737 if (nlflag)
7738 {
7739 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7740 insert_1 ("\n", 1, 1, 0, 0);
7741
7742 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7743 this_bol = PT;
7744 this_bol_byte = PT_BYTE;
7745
7746 /* See if this line duplicates the previous one.
7747 If so, combine duplicates. */
7748 if (this_bol > BEG)
7749 {
7750 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7751 prev_bol = PT;
7752 prev_bol_byte = PT_BYTE;
7753
7754 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7755 this_bol, this_bol_byte);
7756 if (dup)
7757 {
7758 del_range_both (prev_bol, prev_bol_byte,
7759 this_bol, this_bol_byte, 0);
7760 if (dup > 1)
7761 {
7762 char dupstr[40];
7763 int duplen;
7764
7765 /* If you change this format, don't forget to also
7766 change message_log_check_duplicate. */
7767 sprintf (dupstr, " [%d times]", dup);
7768 duplen = strlen (dupstr);
7769 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7770 insert_1 (dupstr, duplen, 1, 0, 1);
7771 }
7772 }
7773 }
7774
7775 /* If we have more than the desired maximum number of lines
7776 in the *Messages* buffer now, delete the oldest ones.
7777 This is safe because we don't have undo in this buffer. */
7778
7779 if (NATNUMP (Vmessage_log_max))
7780 {
7781 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7782 -XFASTINT (Vmessage_log_max) - 1, 0);
7783 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7784 }
7785 }
7786 BEGV = XMARKER (oldbegv)->charpos;
7787 BEGV_BYTE = marker_byte_position (oldbegv);
7788
7789 if (zv_at_end)
7790 {
7791 ZV = Z;
7792 ZV_BYTE = Z_BYTE;
7793 }
7794 else
7795 {
7796 ZV = XMARKER (oldzv)->charpos;
7797 ZV_BYTE = marker_byte_position (oldzv);
7798 }
7799
7800 if (point_at_end)
7801 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7802 else
7803 /* We can't do Fgoto_char (oldpoint) because it will run some
7804 Lisp code. */
7805 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7806 XMARKER (oldpoint)->bytepos);
7807
7808 UNGCPRO;
7809 unchain_marker (XMARKER (oldpoint));
7810 unchain_marker (XMARKER (oldbegv));
7811 unchain_marker (XMARKER (oldzv));
7812
7813 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7814 set_buffer_internal (oldbuf);
7815 if (NILP (tem))
7816 windows_or_buffers_changed = old_windows_or_buffers_changed;
7817 message_log_need_newline = !nlflag;
7818 Vdeactivate_mark = old_deactivate_mark;
7819 }
7820 }
7821
7822
7823 /* We are at the end of the buffer after just having inserted a newline.
7824 (Note: We depend on the fact we won't be crossing the gap.)
7825 Check to see if the most recent message looks a lot like the previous one.
7826 Return 0 if different, 1 if the new one should just replace it, or a
7827 value N > 1 if we should also append " [N times]". */
7828
7829 static int
7830 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7831 int prev_bol, this_bol;
7832 int prev_bol_byte, this_bol_byte;
7833 {
7834 int i;
7835 int len = Z_BYTE - 1 - this_bol_byte;
7836 int seen_dots = 0;
7837 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7838 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7839
7840 for (i = 0; i < len; i++)
7841 {
7842 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7843 seen_dots = 1;
7844 if (p1[i] != p2[i])
7845 return seen_dots;
7846 }
7847 p1 += len;
7848 if (*p1 == '\n')
7849 return 2;
7850 if (*p1++ == ' ' && *p1++ == '[')
7851 {
7852 int n = 0;
7853 while (*p1 >= '0' && *p1 <= '9')
7854 n = n * 10 + *p1++ - '0';
7855 if (strncmp (p1, " times]\n", 8) == 0)
7856 return n+1;
7857 }
7858 return 0;
7859 }
7860 \f
7861
7862 /* Display an echo area message M with a specified length of NBYTES
7863 bytes. The string may include null characters. If M is 0, clear
7864 out any existing message, and let the mini-buffer text show
7865 through.
7866
7867 This may GC, so the buffer M must NOT point to a Lisp string. */
7868
7869 void
7870 message2 (m, nbytes, multibyte)
7871 const char *m;
7872 int nbytes;
7873 int multibyte;
7874 {
7875 /* First flush out any partial line written with print. */
7876 message_log_maybe_newline ();
7877 if (m)
7878 message_dolog (m, nbytes, 1, multibyte);
7879 message2_nolog (m, nbytes, multibyte);
7880 }
7881
7882
7883 /* The non-logging counterpart of message2. */
7884
7885 void
7886 message2_nolog (m, nbytes, multibyte)
7887 const char *m;
7888 int nbytes, multibyte;
7889 {
7890 struct frame *sf = SELECTED_FRAME ();
7891 message_enable_multibyte = multibyte;
7892
7893 if (FRAME_INITIAL_P (sf))
7894 {
7895 if (noninteractive_need_newline)
7896 putc ('\n', stderr);
7897 noninteractive_need_newline = 0;
7898 if (m)
7899 fwrite (m, nbytes, 1, stderr);
7900 if (cursor_in_echo_area == 0)
7901 fprintf (stderr, "\n");
7902 fflush (stderr);
7903 }
7904 /* A null message buffer means that the frame hasn't really been
7905 initialized yet. Error messages get reported properly by
7906 cmd_error, so this must be just an informative message; toss it. */
7907 else if (INTERACTIVE
7908 && sf->glyphs_initialized_p
7909 && FRAME_MESSAGE_BUF (sf))
7910 {
7911 Lisp_Object mini_window;
7912 struct frame *f;
7913
7914 /* Get the frame containing the mini-buffer
7915 that the selected frame is using. */
7916 mini_window = FRAME_MINIBUF_WINDOW (sf);
7917 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7918
7919 FRAME_SAMPLE_VISIBILITY (f);
7920 if (FRAME_VISIBLE_P (sf)
7921 && ! FRAME_VISIBLE_P (f))
7922 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7923
7924 if (m)
7925 {
7926 set_message (m, Qnil, nbytes, multibyte);
7927 if (minibuffer_auto_raise)
7928 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7929 }
7930 else
7931 clear_message (1, 1);
7932
7933 do_pending_window_change (0);
7934 echo_area_display (1);
7935 do_pending_window_change (0);
7936 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7937 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7938 }
7939 }
7940
7941
7942 /* Display an echo area message M with a specified length of NBYTES
7943 bytes. The string may include null characters. If M is not a
7944 string, clear out any existing message, and let the mini-buffer
7945 text show through.
7946
7947 This function cancels echoing. */
7948
7949 void
7950 message3 (m, nbytes, multibyte)
7951 Lisp_Object m;
7952 int nbytes;
7953 int multibyte;
7954 {
7955 struct gcpro gcpro1;
7956
7957 GCPRO1 (m);
7958 clear_message (1,1);
7959 cancel_echoing ();
7960
7961 /* First flush out any partial line written with print. */
7962 message_log_maybe_newline ();
7963 if (STRINGP (m))
7964 {
7965 char *buffer;
7966 USE_SAFE_ALLOCA;
7967
7968 SAFE_ALLOCA (buffer, char *, nbytes);
7969 bcopy (SDATA (m), buffer, nbytes);
7970 message_dolog (buffer, nbytes, 1, multibyte);
7971 SAFE_FREE ();
7972 }
7973 message3_nolog (m, nbytes, multibyte);
7974
7975 UNGCPRO;
7976 }
7977
7978
7979 /* The non-logging version of message3.
7980 This does not cancel echoing, because it is used for echoing.
7981 Perhaps we need to make a separate function for echoing
7982 and make this cancel echoing. */
7983
7984 void
7985 message3_nolog (m, nbytes, multibyte)
7986 Lisp_Object m;
7987 int nbytes, multibyte;
7988 {
7989 struct frame *sf = SELECTED_FRAME ();
7990 message_enable_multibyte = multibyte;
7991
7992 if (FRAME_INITIAL_P (sf))
7993 {
7994 if (noninteractive_need_newline)
7995 putc ('\n', stderr);
7996 noninteractive_need_newline = 0;
7997 if (STRINGP (m))
7998 fwrite (SDATA (m), nbytes, 1, stderr);
7999 if (cursor_in_echo_area == 0)
8000 fprintf (stderr, "\n");
8001 fflush (stderr);
8002 }
8003 /* A null message buffer means that the frame hasn't really been
8004 initialized yet. Error messages get reported properly by
8005 cmd_error, so this must be just an informative message; toss it. */
8006 else if (INTERACTIVE
8007 && sf->glyphs_initialized_p
8008 && FRAME_MESSAGE_BUF (sf))
8009 {
8010 Lisp_Object mini_window;
8011 Lisp_Object frame;
8012 struct frame *f;
8013
8014 /* Get the frame containing the mini-buffer
8015 that the selected frame is using. */
8016 mini_window = FRAME_MINIBUF_WINDOW (sf);
8017 frame = XWINDOW (mini_window)->frame;
8018 f = XFRAME (frame);
8019
8020 FRAME_SAMPLE_VISIBILITY (f);
8021 if (FRAME_VISIBLE_P (sf)
8022 && !FRAME_VISIBLE_P (f))
8023 Fmake_frame_visible (frame);
8024
8025 if (STRINGP (m) && SCHARS (m) > 0)
8026 {
8027 set_message (NULL, m, nbytes, multibyte);
8028 if (minibuffer_auto_raise)
8029 Fraise_frame (frame);
8030 /* Assume we are not echoing.
8031 (If we are, echo_now will override this.) */
8032 echo_message_buffer = Qnil;
8033 }
8034 else
8035 clear_message (1, 1);
8036
8037 do_pending_window_change (0);
8038 echo_area_display (1);
8039 do_pending_window_change (0);
8040 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8041 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8042 }
8043 }
8044
8045
8046 /* Display a null-terminated echo area message M. If M is 0, clear
8047 out any existing message, and let the mini-buffer text show through.
8048
8049 The buffer M must continue to exist until after the echo area gets
8050 cleared or some other message gets displayed there. Do not pass
8051 text that is stored in a Lisp string. Do not pass text in a buffer
8052 that was alloca'd. */
8053
8054 void
8055 message1 (m)
8056 char *m;
8057 {
8058 message2 (m, (m ? strlen (m) : 0), 0);
8059 }
8060
8061
8062 /* The non-logging counterpart of message1. */
8063
8064 void
8065 message1_nolog (m)
8066 char *m;
8067 {
8068 message2_nolog (m, (m ? strlen (m) : 0), 0);
8069 }
8070
8071 /* Display a message M which contains a single %s
8072 which gets replaced with STRING. */
8073
8074 void
8075 message_with_string (m, string, log)
8076 char *m;
8077 Lisp_Object string;
8078 int log;
8079 {
8080 CHECK_STRING (string);
8081
8082 if (noninteractive)
8083 {
8084 if (m)
8085 {
8086 if (noninteractive_need_newline)
8087 putc ('\n', stderr);
8088 noninteractive_need_newline = 0;
8089 fprintf (stderr, m, SDATA (string));
8090 if (!cursor_in_echo_area)
8091 fprintf (stderr, "\n");
8092 fflush (stderr);
8093 }
8094 }
8095 else if (INTERACTIVE)
8096 {
8097 /* The frame whose minibuffer we're going to display the message on.
8098 It may be larger than the selected frame, so we need
8099 to use its buffer, not the selected frame's buffer. */
8100 Lisp_Object mini_window;
8101 struct frame *f, *sf = SELECTED_FRAME ();
8102
8103 /* Get the frame containing the minibuffer
8104 that the selected frame is using. */
8105 mini_window = FRAME_MINIBUF_WINDOW (sf);
8106 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8107
8108 /* A null message buffer means that the frame hasn't really been
8109 initialized yet. Error messages get reported properly by
8110 cmd_error, so this must be just an informative message; toss it. */
8111 if (FRAME_MESSAGE_BUF (f))
8112 {
8113 Lisp_Object args[2], message;
8114 struct gcpro gcpro1, gcpro2;
8115
8116 args[0] = build_string (m);
8117 args[1] = message = string;
8118 GCPRO2 (args[0], message);
8119 gcpro1.nvars = 2;
8120
8121 message = Fformat (2, args);
8122
8123 if (log)
8124 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8125 else
8126 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8127
8128 UNGCPRO;
8129
8130 /* Print should start at the beginning of the message
8131 buffer next time. */
8132 message_buf_print = 0;
8133 }
8134 }
8135 }
8136
8137
8138 /* Dump an informative message to the minibuf. If M is 0, clear out
8139 any existing message, and let the mini-buffer text show through. */
8140
8141 /* VARARGS 1 */
8142 void
8143 message (m, a1, a2, a3)
8144 char *m;
8145 EMACS_INT a1, a2, a3;
8146 {
8147 if (noninteractive)
8148 {
8149 if (m)
8150 {
8151 if (noninteractive_need_newline)
8152 putc ('\n', stderr);
8153 noninteractive_need_newline = 0;
8154 fprintf (stderr, m, a1, a2, a3);
8155 if (cursor_in_echo_area == 0)
8156 fprintf (stderr, "\n");
8157 fflush (stderr);
8158 }
8159 }
8160 else if (INTERACTIVE)
8161 {
8162 /* The frame whose mini-buffer we're going to display the message
8163 on. It may be larger than the selected frame, so we need to
8164 use its buffer, not the selected frame's buffer. */
8165 Lisp_Object mini_window;
8166 struct frame *f, *sf = SELECTED_FRAME ();
8167
8168 /* Get the frame containing the mini-buffer
8169 that the selected frame is using. */
8170 mini_window = FRAME_MINIBUF_WINDOW (sf);
8171 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8172
8173 /* A null message buffer means that the frame hasn't really been
8174 initialized yet. Error messages get reported properly by
8175 cmd_error, so this must be just an informative message; toss
8176 it. */
8177 if (FRAME_MESSAGE_BUF (f))
8178 {
8179 if (m)
8180 {
8181 int len;
8182 #ifdef NO_ARG_ARRAY
8183 char *a[3];
8184 a[0] = (char *) a1;
8185 a[1] = (char *) a2;
8186 a[2] = (char *) a3;
8187
8188 len = doprnt (FRAME_MESSAGE_BUF (f),
8189 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8190 #else
8191 len = doprnt (FRAME_MESSAGE_BUF (f),
8192 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8193 (char **) &a1);
8194 #endif /* NO_ARG_ARRAY */
8195
8196 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8197 }
8198 else
8199 message1 (0);
8200
8201 /* Print should start at the beginning of the message
8202 buffer next time. */
8203 message_buf_print = 0;
8204 }
8205 }
8206 }
8207
8208
8209 /* The non-logging version of message. */
8210
8211 void
8212 message_nolog (m, a1, a2, a3)
8213 char *m;
8214 EMACS_INT a1, a2, a3;
8215 {
8216 Lisp_Object old_log_max;
8217 old_log_max = Vmessage_log_max;
8218 Vmessage_log_max = Qnil;
8219 message (m, a1, a2, a3);
8220 Vmessage_log_max = old_log_max;
8221 }
8222
8223
8224 /* Display the current message in the current mini-buffer. This is
8225 only called from error handlers in process.c, and is not time
8226 critical. */
8227
8228 void
8229 update_echo_area ()
8230 {
8231 if (!NILP (echo_area_buffer[0]))
8232 {
8233 Lisp_Object string;
8234 string = Fcurrent_message ();
8235 message3 (string, SBYTES (string),
8236 !NILP (current_buffer->enable_multibyte_characters));
8237 }
8238 }
8239
8240
8241 /* Make sure echo area buffers in `echo_buffers' are live.
8242 If they aren't, make new ones. */
8243
8244 static void
8245 ensure_echo_area_buffers ()
8246 {
8247 int i;
8248
8249 for (i = 0; i < 2; ++i)
8250 if (!BUFFERP (echo_buffer[i])
8251 || NILP (XBUFFER (echo_buffer[i])->name))
8252 {
8253 char name[30];
8254 Lisp_Object old_buffer;
8255 int j;
8256
8257 old_buffer = echo_buffer[i];
8258 sprintf (name, " *Echo Area %d*", i);
8259 echo_buffer[i] = Fget_buffer_create (build_string (name));
8260 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8261 /* to force word wrap in echo area -
8262 it was decided to postpone this*/
8263 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
8264
8265 for (j = 0; j < 2; ++j)
8266 if (EQ (old_buffer, echo_area_buffer[j]))
8267 echo_area_buffer[j] = echo_buffer[i];
8268 }
8269 }
8270
8271
8272 /* Call FN with args A1..A4 with either the current or last displayed
8273 echo_area_buffer as current buffer.
8274
8275 WHICH zero means use the current message buffer
8276 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8277 from echo_buffer[] and clear it.
8278
8279 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8280 suitable buffer from echo_buffer[] and clear it.
8281
8282 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8283 that the current message becomes the last displayed one, make
8284 choose a suitable buffer for echo_area_buffer[0], and clear it.
8285
8286 Value is what FN returns. */
8287
8288 static int
8289 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8290 struct window *w;
8291 int which;
8292 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8293 EMACS_INT a1;
8294 Lisp_Object a2;
8295 EMACS_INT a3, a4;
8296 {
8297 Lisp_Object buffer;
8298 int this_one, the_other, clear_buffer_p, rc;
8299 int count = SPECPDL_INDEX ();
8300
8301 /* If buffers aren't live, make new ones. */
8302 ensure_echo_area_buffers ();
8303
8304 clear_buffer_p = 0;
8305
8306 if (which == 0)
8307 this_one = 0, the_other = 1;
8308 else if (which > 0)
8309 this_one = 1, the_other = 0;
8310 else
8311 {
8312 this_one = 0, the_other = 1;
8313 clear_buffer_p = 1;
8314
8315 /* We need a fresh one in case the current echo buffer equals
8316 the one containing the last displayed echo area message. */
8317 if (!NILP (echo_area_buffer[this_one])
8318 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8319 echo_area_buffer[this_one] = Qnil;
8320 }
8321
8322 /* Choose a suitable buffer from echo_buffer[] is we don't
8323 have one. */
8324 if (NILP (echo_area_buffer[this_one]))
8325 {
8326 echo_area_buffer[this_one]
8327 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8328 ? echo_buffer[the_other]
8329 : echo_buffer[this_one]);
8330 clear_buffer_p = 1;
8331 }
8332
8333 buffer = echo_area_buffer[this_one];
8334
8335 /* Don't get confused by reusing the buffer used for echoing
8336 for a different purpose. */
8337 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8338 cancel_echoing ();
8339
8340 record_unwind_protect (unwind_with_echo_area_buffer,
8341 with_echo_area_buffer_unwind_data (w));
8342
8343 /* Make the echo area buffer current. Note that for display
8344 purposes, it is not necessary that the displayed window's buffer
8345 == current_buffer, except for text property lookup. So, let's
8346 only set that buffer temporarily here without doing a full
8347 Fset_window_buffer. We must also change w->pointm, though,
8348 because otherwise an assertions in unshow_buffer fails, and Emacs
8349 aborts. */
8350 set_buffer_internal_1 (XBUFFER (buffer));
8351 if (w)
8352 {
8353 w->buffer = buffer;
8354 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8355 }
8356
8357 current_buffer->undo_list = Qt;
8358 current_buffer->read_only = Qnil;
8359 specbind (Qinhibit_read_only, Qt);
8360 specbind (Qinhibit_modification_hooks, Qt);
8361
8362 if (clear_buffer_p && Z > BEG)
8363 del_range (BEG, Z);
8364
8365 xassert (BEGV >= BEG);
8366 xassert (ZV <= Z && ZV >= BEGV);
8367
8368 rc = fn (a1, a2, a3, a4);
8369
8370 xassert (BEGV >= BEG);
8371 xassert (ZV <= Z && ZV >= BEGV);
8372
8373 unbind_to (count, Qnil);
8374 return rc;
8375 }
8376
8377
8378 /* Save state that should be preserved around the call to the function
8379 FN called in with_echo_area_buffer. */
8380
8381 static Lisp_Object
8382 with_echo_area_buffer_unwind_data (w)
8383 struct window *w;
8384 {
8385 int i = 0;
8386 Lisp_Object vector, tmp;
8387
8388 /* Reduce consing by keeping one vector in
8389 Vwith_echo_area_save_vector. */
8390 vector = Vwith_echo_area_save_vector;
8391 Vwith_echo_area_save_vector = Qnil;
8392
8393 if (NILP (vector))
8394 vector = Fmake_vector (make_number (7), Qnil);
8395
8396 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8397 ASET (vector, i, Vdeactivate_mark); ++i;
8398 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8399
8400 if (w)
8401 {
8402 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8403 ASET (vector, i, w->buffer); ++i;
8404 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8405 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8406 }
8407 else
8408 {
8409 int end = i + 4;
8410 for (; i < end; ++i)
8411 ASET (vector, i, Qnil);
8412 }
8413
8414 xassert (i == ASIZE (vector));
8415 return vector;
8416 }
8417
8418
8419 /* Restore global state from VECTOR which was created by
8420 with_echo_area_buffer_unwind_data. */
8421
8422 static Lisp_Object
8423 unwind_with_echo_area_buffer (vector)
8424 Lisp_Object vector;
8425 {
8426 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8427 Vdeactivate_mark = AREF (vector, 1);
8428 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8429
8430 if (WINDOWP (AREF (vector, 3)))
8431 {
8432 struct window *w;
8433 Lisp_Object buffer, charpos, bytepos;
8434
8435 w = XWINDOW (AREF (vector, 3));
8436 buffer = AREF (vector, 4);
8437 charpos = AREF (vector, 5);
8438 bytepos = AREF (vector, 6);
8439
8440 w->buffer = buffer;
8441 set_marker_both (w->pointm, buffer,
8442 XFASTINT (charpos), XFASTINT (bytepos));
8443 }
8444
8445 Vwith_echo_area_save_vector = vector;
8446 return Qnil;
8447 }
8448
8449
8450 /* Set up the echo area for use by print functions. MULTIBYTE_P
8451 non-zero means we will print multibyte. */
8452
8453 void
8454 setup_echo_area_for_printing (multibyte_p)
8455 int multibyte_p;
8456 {
8457 /* If we can't find an echo area any more, exit. */
8458 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8459 Fkill_emacs (Qnil);
8460
8461 ensure_echo_area_buffers ();
8462
8463 if (!message_buf_print)
8464 {
8465 /* A message has been output since the last time we printed.
8466 Choose a fresh echo area buffer. */
8467 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8468 echo_area_buffer[0] = echo_buffer[1];
8469 else
8470 echo_area_buffer[0] = echo_buffer[0];
8471
8472 /* Switch to that buffer and clear it. */
8473 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8474 current_buffer->truncate_lines = Qnil;
8475
8476 if (Z > BEG)
8477 {
8478 int count = SPECPDL_INDEX ();
8479 specbind (Qinhibit_read_only, Qt);
8480 /* Note that undo recording is always disabled. */
8481 del_range (BEG, Z);
8482 unbind_to (count, Qnil);
8483 }
8484 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8485
8486 /* Set up the buffer for the multibyteness we need. */
8487 if (multibyte_p
8488 != !NILP (current_buffer->enable_multibyte_characters))
8489 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8490
8491 /* Raise the frame containing the echo area. */
8492 if (minibuffer_auto_raise)
8493 {
8494 struct frame *sf = SELECTED_FRAME ();
8495 Lisp_Object mini_window;
8496 mini_window = FRAME_MINIBUF_WINDOW (sf);
8497 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8498 }
8499
8500 message_log_maybe_newline ();
8501 message_buf_print = 1;
8502 }
8503 else
8504 {
8505 if (NILP (echo_area_buffer[0]))
8506 {
8507 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8508 echo_area_buffer[0] = echo_buffer[1];
8509 else
8510 echo_area_buffer[0] = echo_buffer[0];
8511 }
8512
8513 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8514 {
8515 /* Someone switched buffers between print requests. */
8516 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8517 current_buffer->truncate_lines = Qnil;
8518 }
8519 }
8520 }
8521
8522
8523 /* Display an echo area message in window W. Value is non-zero if W's
8524 height is changed. If display_last_displayed_message_p is
8525 non-zero, display the message that was last displayed, otherwise
8526 display the current message. */
8527
8528 static int
8529 display_echo_area (w)
8530 struct window *w;
8531 {
8532 int i, no_message_p, window_height_changed_p, count;
8533
8534 /* Temporarily disable garbage collections while displaying the echo
8535 area. This is done because a GC can print a message itself.
8536 That message would modify the echo area buffer's contents while a
8537 redisplay of the buffer is going on, and seriously confuse
8538 redisplay. */
8539 count = inhibit_garbage_collection ();
8540
8541 /* If there is no message, we must call display_echo_area_1
8542 nevertheless because it resizes the window. But we will have to
8543 reset the echo_area_buffer in question to nil at the end because
8544 with_echo_area_buffer will sets it to an empty buffer. */
8545 i = display_last_displayed_message_p ? 1 : 0;
8546 no_message_p = NILP (echo_area_buffer[i]);
8547
8548 window_height_changed_p
8549 = with_echo_area_buffer (w, display_last_displayed_message_p,
8550 display_echo_area_1,
8551 (EMACS_INT) w, Qnil, 0, 0);
8552
8553 if (no_message_p)
8554 echo_area_buffer[i] = Qnil;
8555
8556 unbind_to (count, Qnil);
8557 return window_height_changed_p;
8558 }
8559
8560
8561 /* Helper for display_echo_area. Display the current buffer which
8562 contains the current echo area message in window W, a mini-window,
8563 a pointer to which is passed in A1. A2..A4 are currently not used.
8564 Change the height of W so that all of the message is displayed.
8565 Value is non-zero if height of W was changed. */
8566
8567 static int
8568 display_echo_area_1 (a1, a2, a3, a4)
8569 EMACS_INT a1;
8570 Lisp_Object a2;
8571 EMACS_INT a3, a4;
8572 {
8573 struct window *w = (struct window *) a1;
8574 Lisp_Object window;
8575 struct text_pos start;
8576 int window_height_changed_p = 0;
8577
8578 /* Do this before displaying, so that we have a large enough glyph
8579 matrix for the display. If we can't get enough space for the
8580 whole text, display the last N lines. That works by setting w->start. */
8581 window_height_changed_p = resize_mini_window (w, 0);
8582
8583 /* Use the starting position chosen by resize_mini_window. */
8584 SET_TEXT_POS_FROM_MARKER (start, w->start);
8585
8586 /* Display. */
8587 clear_glyph_matrix (w->desired_matrix);
8588 XSETWINDOW (window, w);
8589 try_window (window, start, 0);
8590
8591 return window_height_changed_p;
8592 }
8593
8594
8595 /* Resize the echo area window to exactly the size needed for the
8596 currently displayed message, if there is one. If a mini-buffer
8597 is active, don't shrink it. */
8598
8599 void
8600 resize_echo_area_exactly ()
8601 {
8602 if (BUFFERP (echo_area_buffer[0])
8603 && WINDOWP (echo_area_window))
8604 {
8605 struct window *w = XWINDOW (echo_area_window);
8606 int resized_p;
8607 Lisp_Object resize_exactly;
8608
8609 if (minibuf_level == 0)
8610 resize_exactly = Qt;
8611 else
8612 resize_exactly = Qnil;
8613
8614 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8615 (EMACS_INT) w, resize_exactly, 0, 0);
8616 if (resized_p)
8617 {
8618 ++windows_or_buffers_changed;
8619 ++update_mode_lines;
8620 redisplay_internal (0);
8621 }
8622 }
8623 }
8624
8625
8626 /* Callback function for with_echo_area_buffer, when used from
8627 resize_echo_area_exactly. A1 contains a pointer to the window to
8628 resize, EXACTLY non-nil means resize the mini-window exactly to the
8629 size of the text displayed. A3 and A4 are not used. Value is what
8630 resize_mini_window returns. */
8631
8632 static int
8633 resize_mini_window_1 (a1, exactly, a3, a4)
8634 EMACS_INT a1;
8635 Lisp_Object exactly;
8636 EMACS_INT a3, a4;
8637 {
8638 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8639 }
8640
8641
8642 /* Resize mini-window W to fit the size of its contents. EXACT_P
8643 means size the window exactly to the size needed. Otherwise, it's
8644 only enlarged until W's buffer is empty.
8645
8646 Set W->start to the right place to begin display. If the whole
8647 contents fit, start at the beginning. Otherwise, start so as
8648 to make the end of the contents appear. This is particularly
8649 important for y-or-n-p, but seems desirable generally.
8650
8651 Value is non-zero if the window height has been changed. */
8652
8653 int
8654 resize_mini_window (w, exact_p)
8655 struct window *w;
8656 int exact_p;
8657 {
8658 struct frame *f = XFRAME (w->frame);
8659 int window_height_changed_p = 0;
8660
8661 xassert (MINI_WINDOW_P (w));
8662
8663 /* By default, start display at the beginning. */
8664 set_marker_both (w->start, w->buffer,
8665 BUF_BEGV (XBUFFER (w->buffer)),
8666 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8667
8668 /* Don't resize windows while redisplaying a window; it would
8669 confuse redisplay functions when the size of the window they are
8670 displaying changes from under them. Such a resizing can happen,
8671 for instance, when which-func prints a long message while
8672 we are running fontification-functions. We're running these
8673 functions with safe_call which binds inhibit-redisplay to t. */
8674 if (!NILP (Vinhibit_redisplay))
8675 return 0;
8676
8677 /* Nil means don't try to resize. */
8678 if (NILP (Vresize_mini_windows)
8679 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8680 return 0;
8681
8682 if (!FRAME_MINIBUF_ONLY_P (f))
8683 {
8684 struct it it;
8685 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8686 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8687 int height, max_height;
8688 int unit = FRAME_LINE_HEIGHT (f);
8689 struct text_pos start;
8690 struct buffer *old_current_buffer = NULL;
8691
8692 if (current_buffer != XBUFFER (w->buffer))
8693 {
8694 old_current_buffer = current_buffer;
8695 set_buffer_internal (XBUFFER (w->buffer));
8696 }
8697
8698 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8699
8700 /* Compute the max. number of lines specified by the user. */
8701 if (FLOATP (Vmax_mini_window_height))
8702 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8703 else if (INTEGERP (Vmax_mini_window_height))
8704 max_height = XINT (Vmax_mini_window_height);
8705 else
8706 max_height = total_height / 4;
8707
8708 /* Correct that max. height if it's bogus. */
8709 max_height = max (1, max_height);
8710 max_height = min (total_height, max_height);
8711
8712 /* Find out the height of the text in the window. */
8713 if (it.line_wrap == TRUNCATE)
8714 height = 1;
8715 else
8716 {
8717 last_height = 0;
8718 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8719 if (it.max_ascent == 0 && it.max_descent == 0)
8720 height = it.current_y + last_height;
8721 else
8722 height = it.current_y + it.max_ascent + it.max_descent;
8723 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8724 height = (height + unit - 1) / unit;
8725 }
8726
8727 /* Compute a suitable window start. */
8728 if (height > max_height)
8729 {
8730 height = max_height;
8731 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8732 move_it_vertically_backward (&it, (height - 1) * unit);
8733 start = it.current.pos;
8734 }
8735 else
8736 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8737 SET_MARKER_FROM_TEXT_POS (w->start, start);
8738
8739 if (EQ (Vresize_mini_windows, Qgrow_only))
8740 {
8741 /* Let it grow only, until we display an empty message, in which
8742 case the window shrinks again. */
8743 if (height > WINDOW_TOTAL_LINES (w))
8744 {
8745 int old_height = WINDOW_TOTAL_LINES (w);
8746 freeze_window_starts (f, 1);
8747 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8748 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8749 }
8750 else if (height < WINDOW_TOTAL_LINES (w)
8751 && (exact_p || BEGV == ZV))
8752 {
8753 int old_height = WINDOW_TOTAL_LINES (w);
8754 freeze_window_starts (f, 0);
8755 shrink_mini_window (w);
8756 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8757 }
8758 }
8759 else
8760 {
8761 /* Always resize to exact size needed. */
8762 if (height > WINDOW_TOTAL_LINES (w))
8763 {
8764 int old_height = WINDOW_TOTAL_LINES (w);
8765 freeze_window_starts (f, 1);
8766 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8767 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8768 }
8769 else if (height < WINDOW_TOTAL_LINES (w))
8770 {
8771 int old_height = WINDOW_TOTAL_LINES (w);
8772 freeze_window_starts (f, 0);
8773 shrink_mini_window (w);
8774
8775 if (height)
8776 {
8777 freeze_window_starts (f, 1);
8778 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8779 }
8780
8781 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8782 }
8783 }
8784
8785 if (old_current_buffer)
8786 set_buffer_internal (old_current_buffer);
8787 }
8788
8789 return window_height_changed_p;
8790 }
8791
8792
8793 /* Value is the current message, a string, or nil if there is no
8794 current message. */
8795
8796 Lisp_Object
8797 current_message ()
8798 {
8799 Lisp_Object msg;
8800
8801 if (!BUFFERP (echo_area_buffer[0]))
8802 msg = Qnil;
8803 else
8804 {
8805 with_echo_area_buffer (0, 0, current_message_1,
8806 (EMACS_INT) &msg, Qnil, 0, 0);
8807 if (NILP (msg))
8808 echo_area_buffer[0] = Qnil;
8809 }
8810
8811 return msg;
8812 }
8813
8814
8815 static int
8816 current_message_1 (a1, a2, a3, a4)
8817 EMACS_INT a1;
8818 Lisp_Object a2;
8819 EMACS_INT a3, a4;
8820 {
8821 Lisp_Object *msg = (Lisp_Object *) a1;
8822
8823 if (Z > BEG)
8824 *msg = make_buffer_string (BEG, Z, 1);
8825 else
8826 *msg = Qnil;
8827 return 0;
8828 }
8829
8830
8831 /* Push the current message on Vmessage_stack for later restauration
8832 by restore_message. Value is non-zero if the current message isn't
8833 empty. This is a relatively infrequent operation, so it's not
8834 worth optimizing. */
8835
8836 int
8837 push_message ()
8838 {
8839 Lisp_Object msg;
8840 msg = current_message ();
8841 Vmessage_stack = Fcons (msg, Vmessage_stack);
8842 return STRINGP (msg);
8843 }
8844
8845
8846 /* Restore message display from the top of Vmessage_stack. */
8847
8848 void
8849 restore_message ()
8850 {
8851 Lisp_Object msg;
8852
8853 xassert (CONSP (Vmessage_stack));
8854 msg = XCAR (Vmessage_stack);
8855 if (STRINGP (msg))
8856 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8857 else
8858 message3_nolog (msg, 0, 0);
8859 }
8860
8861
8862 /* Handler for record_unwind_protect calling pop_message. */
8863
8864 Lisp_Object
8865 pop_message_unwind (dummy)
8866 Lisp_Object dummy;
8867 {
8868 pop_message ();
8869 return Qnil;
8870 }
8871
8872 /* Pop the top-most entry off Vmessage_stack. */
8873
8874 void
8875 pop_message ()
8876 {
8877 xassert (CONSP (Vmessage_stack));
8878 Vmessage_stack = XCDR (Vmessage_stack);
8879 }
8880
8881
8882 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8883 exits. If the stack is not empty, we have a missing pop_message
8884 somewhere. */
8885
8886 void
8887 check_message_stack ()
8888 {
8889 if (!NILP (Vmessage_stack))
8890 abort ();
8891 }
8892
8893
8894 /* Truncate to NCHARS what will be displayed in the echo area the next
8895 time we display it---but don't redisplay it now. */
8896
8897 void
8898 truncate_echo_area (nchars)
8899 int nchars;
8900 {
8901 if (nchars == 0)
8902 echo_area_buffer[0] = Qnil;
8903 /* A null message buffer means that the frame hasn't really been
8904 initialized yet. Error messages get reported properly by
8905 cmd_error, so this must be just an informative message; toss it. */
8906 else if (!noninteractive
8907 && INTERACTIVE
8908 && !NILP (echo_area_buffer[0]))
8909 {
8910 struct frame *sf = SELECTED_FRAME ();
8911 if (FRAME_MESSAGE_BUF (sf))
8912 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8913 }
8914 }
8915
8916
8917 /* Helper function for truncate_echo_area. Truncate the current
8918 message to at most NCHARS characters. */
8919
8920 static int
8921 truncate_message_1 (nchars, a2, a3, a4)
8922 EMACS_INT nchars;
8923 Lisp_Object a2;
8924 EMACS_INT a3, a4;
8925 {
8926 if (BEG + nchars < Z)
8927 del_range (BEG + nchars, Z);
8928 if (Z == BEG)
8929 echo_area_buffer[0] = Qnil;
8930 return 0;
8931 }
8932
8933
8934 /* Set the current message to a substring of S or STRING.
8935
8936 If STRING is a Lisp string, set the message to the first NBYTES
8937 bytes from STRING. NBYTES zero means use the whole string. If
8938 STRING is multibyte, the message will be displayed multibyte.
8939
8940 If S is not null, set the message to the first LEN bytes of S. LEN
8941 zero means use the whole string. MULTIBYTE_P non-zero means S is
8942 multibyte. Display the message multibyte in that case.
8943
8944 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8945 to t before calling set_message_1 (which calls insert).
8946 */
8947
8948 void
8949 set_message (s, string, nbytes, multibyte_p)
8950 const char *s;
8951 Lisp_Object string;
8952 int nbytes, multibyte_p;
8953 {
8954 message_enable_multibyte
8955 = ((s && multibyte_p)
8956 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8957
8958 with_echo_area_buffer (0, -1, set_message_1,
8959 (EMACS_INT) s, string, nbytes, multibyte_p);
8960 message_buf_print = 0;
8961 help_echo_showing_p = 0;
8962 }
8963
8964
8965 /* Helper function for set_message. Arguments have the same meaning
8966 as there, with A1 corresponding to S and A2 corresponding to STRING
8967 This function is called with the echo area buffer being
8968 current. */
8969
8970 static int
8971 set_message_1 (a1, a2, nbytes, multibyte_p)
8972 EMACS_INT a1;
8973 Lisp_Object a2;
8974 EMACS_INT nbytes, multibyte_p;
8975 {
8976 const char *s = (const char *) a1;
8977 Lisp_Object string = a2;
8978
8979 /* Change multibyteness of the echo buffer appropriately. */
8980 if (message_enable_multibyte
8981 != !NILP (current_buffer->enable_multibyte_characters))
8982 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8983
8984 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8985
8986 /* Insert new message at BEG. */
8987 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8988
8989 if (STRINGP (string))
8990 {
8991 int nchars;
8992
8993 if (nbytes == 0)
8994 nbytes = SBYTES (string);
8995 nchars = string_byte_to_char (string, nbytes);
8996
8997 /* This function takes care of single/multibyte conversion. We
8998 just have to ensure that the echo area buffer has the right
8999 setting of enable_multibyte_characters. */
9000 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9001 }
9002 else if (s)
9003 {
9004 if (nbytes == 0)
9005 nbytes = strlen (s);
9006
9007 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
9008 {
9009 /* Convert from multi-byte to single-byte. */
9010 int i, c, n;
9011 unsigned char work[1];
9012
9013 /* Convert a multibyte string to single-byte. */
9014 for (i = 0; i < nbytes; i += n)
9015 {
9016 c = string_char_and_length (s + i, &n);
9017 work[0] = (ASCII_CHAR_P (c)
9018 ? c
9019 : multibyte_char_to_unibyte (c, Qnil));
9020 insert_1_both (work, 1, 1, 1, 0, 0);
9021 }
9022 }
9023 else if (!multibyte_p
9024 && !NILP (current_buffer->enable_multibyte_characters))
9025 {
9026 /* Convert from single-byte to multi-byte. */
9027 int i, c, n;
9028 const unsigned char *msg = (const unsigned char *) s;
9029 unsigned char str[MAX_MULTIBYTE_LENGTH];
9030
9031 /* Convert a single-byte string to multibyte. */
9032 for (i = 0; i < nbytes; i++)
9033 {
9034 c = msg[i];
9035 MAKE_CHAR_MULTIBYTE (c);
9036 n = CHAR_STRING (c, str);
9037 insert_1_both (str, 1, n, 1, 0, 0);
9038 }
9039 }
9040 else
9041 insert_1 (s, nbytes, 1, 0, 0);
9042 }
9043
9044 return 0;
9045 }
9046
9047
9048 /* Clear messages. CURRENT_P non-zero means clear the current
9049 message. LAST_DISPLAYED_P non-zero means clear the message
9050 last displayed. */
9051
9052 void
9053 clear_message (current_p, last_displayed_p)
9054 int current_p, last_displayed_p;
9055 {
9056 if (current_p)
9057 {
9058 echo_area_buffer[0] = Qnil;
9059 message_cleared_p = 1;
9060 }
9061
9062 if (last_displayed_p)
9063 echo_area_buffer[1] = Qnil;
9064
9065 message_buf_print = 0;
9066 }
9067
9068 /* Clear garbaged frames.
9069
9070 This function is used where the old redisplay called
9071 redraw_garbaged_frames which in turn called redraw_frame which in
9072 turn called clear_frame. The call to clear_frame was a source of
9073 flickering. I believe a clear_frame is not necessary. It should
9074 suffice in the new redisplay to invalidate all current matrices,
9075 and ensure a complete redisplay of all windows. */
9076
9077 static void
9078 clear_garbaged_frames ()
9079 {
9080 if (frame_garbaged)
9081 {
9082 Lisp_Object tail, frame;
9083 int changed_count = 0;
9084
9085 FOR_EACH_FRAME (tail, frame)
9086 {
9087 struct frame *f = XFRAME (frame);
9088
9089 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9090 {
9091 if (f->resized_p)
9092 {
9093 Fredraw_frame (frame);
9094 f->force_flush_display_p = 1;
9095 }
9096 clear_current_matrices (f);
9097 changed_count++;
9098 f->garbaged = 0;
9099 f->resized_p = 0;
9100 }
9101 }
9102
9103 frame_garbaged = 0;
9104 if (changed_count)
9105 ++windows_or_buffers_changed;
9106 }
9107 }
9108
9109
9110 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9111 is non-zero update selected_frame. Value is non-zero if the
9112 mini-windows height has been changed. */
9113
9114 static int
9115 echo_area_display (update_frame_p)
9116 int update_frame_p;
9117 {
9118 Lisp_Object mini_window;
9119 struct window *w;
9120 struct frame *f;
9121 int window_height_changed_p = 0;
9122 struct frame *sf = SELECTED_FRAME ();
9123
9124 mini_window = FRAME_MINIBUF_WINDOW (sf);
9125 w = XWINDOW (mini_window);
9126 f = XFRAME (WINDOW_FRAME (w));
9127
9128 /* Don't display if frame is invisible or not yet initialized. */
9129 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9130 return 0;
9131
9132 #ifdef HAVE_WINDOW_SYSTEM
9133 /* When Emacs starts, selected_frame may be the initial terminal
9134 frame. If we let this through, a message would be displayed on
9135 the terminal. */
9136 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9137 return 0;
9138 #endif /* HAVE_WINDOW_SYSTEM */
9139
9140 /* Redraw garbaged frames. */
9141 if (frame_garbaged)
9142 clear_garbaged_frames ();
9143
9144 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9145 {
9146 echo_area_window = mini_window;
9147 window_height_changed_p = display_echo_area (w);
9148 w->must_be_updated_p = 1;
9149
9150 /* Update the display, unless called from redisplay_internal.
9151 Also don't update the screen during redisplay itself. The
9152 update will happen at the end of redisplay, and an update
9153 here could cause confusion. */
9154 if (update_frame_p && !redisplaying_p)
9155 {
9156 int n = 0;
9157
9158 /* If the display update has been interrupted by pending
9159 input, update mode lines in the frame. Due to the
9160 pending input, it might have been that redisplay hasn't
9161 been called, so that mode lines above the echo area are
9162 garbaged. This looks odd, so we prevent it here. */
9163 if (!display_completed)
9164 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9165
9166 if (window_height_changed_p
9167 /* Don't do this if Emacs is shutting down. Redisplay
9168 needs to run hooks. */
9169 && !NILP (Vrun_hooks))
9170 {
9171 /* Must update other windows. Likewise as in other
9172 cases, don't let this update be interrupted by
9173 pending input. */
9174 int count = SPECPDL_INDEX ();
9175 specbind (Qredisplay_dont_pause, Qt);
9176 windows_or_buffers_changed = 1;
9177 redisplay_internal (0);
9178 unbind_to (count, Qnil);
9179 }
9180 else if (FRAME_WINDOW_P (f) && n == 0)
9181 {
9182 /* Window configuration is the same as before.
9183 Can do with a display update of the echo area,
9184 unless we displayed some mode lines. */
9185 update_single_window (w, 1);
9186 FRAME_RIF (f)->flush_display (f);
9187 }
9188 else
9189 update_frame (f, 1, 1);
9190
9191 /* If cursor is in the echo area, make sure that the next
9192 redisplay displays the minibuffer, so that the cursor will
9193 be replaced with what the minibuffer wants. */
9194 if (cursor_in_echo_area)
9195 ++windows_or_buffers_changed;
9196 }
9197 }
9198 else if (!EQ (mini_window, selected_window))
9199 windows_or_buffers_changed++;
9200
9201 /* Last displayed message is now the current message. */
9202 echo_area_buffer[1] = echo_area_buffer[0];
9203 /* Inform read_char that we're not echoing. */
9204 echo_message_buffer = Qnil;
9205
9206 /* Prevent redisplay optimization in redisplay_internal by resetting
9207 this_line_start_pos. This is done because the mini-buffer now
9208 displays the message instead of its buffer text. */
9209 if (EQ (mini_window, selected_window))
9210 CHARPOS (this_line_start_pos) = 0;
9211
9212 return window_height_changed_p;
9213 }
9214
9215
9216 \f
9217 /***********************************************************************
9218 Mode Lines and Frame Titles
9219 ***********************************************************************/
9220
9221 /* A buffer for constructing non-propertized mode-line strings and
9222 frame titles in it; allocated from the heap in init_xdisp and
9223 resized as needed in store_mode_line_noprop_char. */
9224
9225 static char *mode_line_noprop_buf;
9226
9227 /* The buffer's end, and a current output position in it. */
9228
9229 static char *mode_line_noprop_buf_end;
9230 static char *mode_line_noprop_ptr;
9231
9232 #define MODE_LINE_NOPROP_LEN(start) \
9233 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9234
9235 static enum {
9236 MODE_LINE_DISPLAY = 0,
9237 MODE_LINE_TITLE,
9238 MODE_LINE_NOPROP,
9239 MODE_LINE_STRING
9240 } mode_line_target;
9241
9242 /* Alist that caches the results of :propertize.
9243 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9244 static Lisp_Object mode_line_proptrans_alist;
9245
9246 /* List of strings making up the mode-line. */
9247 static Lisp_Object mode_line_string_list;
9248
9249 /* Base face property when building propertized mode line string. */
9250 static Lisp_Object mode_line_string_face;
9251 static Lisp_Object mode_line_string_face_prop;
9252
9253
9254 /* Unwind data for mode line strings */
9255
9256 static Lisp_Object Vmode_line_unwind_vector;
9257
9258 static Lisp_Object
9259 format_mode_line_unwind_data (struct buffer *obuf,
9260 Lisp_Object owin,
9261 int save_proptrans)
9262 {
9263 Lisp_Object vector, tmp;
9264
9265 /* Reduce consing by keeping one vector in
9266 Vwith_echo_area_save_vector. */
9267 vector = Vmode_line_unwind_vector;
9268 Vmode_line_unwind_vector = Qnil;
9269
9270 if (NILP (vector))
9271 vector = Fmake_vector (make_number (8), Qnil);
9272
9273 ASET (vector, 0, make_number (mode_line_target));
9274 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9275 ASET (vector, 2, mode_line_string_list);
9276 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9277 ASET (vector, 4, mode_line_string_face);
9278 ASET (vector, 5, mode_line_string_face_prop);
9279
9280 if (obuf)
9281 XSETBUFFER (tmp, obuf);
9282 else
9283 tmp = Qnil;
9284 ASET (vector, 6, tmp);
9285 ASET (vector, 7, owin);
9286
9287 return vector;
9288 }
9289
9290 static Lisp_Object
9291 unwind_format_mode_line (vector)
9292 Lisp_Object vector;
9293 {
9294 mode_line_target = XINT (AREF (vector, 0));
9295 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9296 mode_line_string_list = AREF (vector, 2);
9297 if (! EQ (AREF (vector, 3), Qt))
9298 mode_line_proptrans_alist = AREF (vector, 3);
9299 mode_line_string_face = AREF (vector, 4);
9300 mode_line_string_face_prop = AREF (vector, 5);
9301
9302 if (!NILP (AREF (vector, 7)))
9303 /* Select window before buffer, since it may change the buffer. */
9304 Fselect_window (AREF (vector, 7), Qt);
9305
9306 if (!NILP (AREF (vector, 6)))
9307 {
9308 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9309 ASET (vector, 6, Qnil);
9310 }
9311
9312 Vmode_line_unwind_vector = vector;
9313 return Qnil;
9314 }
9315
9316
9317 /* Store a single character C for the frame title in mode_line_noprop_buf.
9318 Re-allocate mode_line_noprop_buf if necessary. */
9319
9320 static void
9321 #ifdef PROTOTYPES
9322 store_mode_line_noprop_char (char c)
9323 #else
9324 store_mode_line_noprop_char (c)
9325 char c;
9326 #endif
9327 {
9328 /* If output position has reached the end of the allocated buffer,
9329 double the buffer's size. */
9330 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9331 {
9332 int len = MODE_LINE_NOPROP_LEN (0);
9333 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9334 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9335 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9336 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9337 }
9338
9339 *mode_line_noprop_ptr++ = c;
9340 }
9341
9342
9343 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9344 mode_line_noprop_ptr. STR is the string to store. Do not copy
9345 characters that yield more columns than PRECISION; PRECISION <= 0
9346 means copy the whole string. Pad with spaces until FIELD_WIDTH
9347 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9348 pad. Called from display_mode_element when it is used to build a
9349 frame title. */
9350
9351 static int
9352 store_mode_line_noprop (str, field_width, precision)
9353 const unsigned char *str;
9354 int field_width, precision;
9355 {
9356 int n = 0;
9357 int dummy, nbytes;
9358
9359 /* Copy at most PRECISION chars from STR. */
9360 nbytes = strlen (str);
9361 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9362 while (nbytes--)
9363 store_mode_line_noprop_char (*str++);
9364
9365 /* Fill up with spaces until FIELD_WIDTH reached. */
9366 while (field_width > 0
9367 && n < field_width)
9368 {
9369 store_mode_line_noprop_char (' ');
9370 ++n;
9371 }
9372
9373 return n;
9374 }
9375
9376 /***********************************************************************
9377 Frame Titles
9378 ***********************************************************************/
9379
9380 #ifdef HAVE_WINDOW_SYSTEM
9381
9382 /* Set the title of FRAME, if it has changed. The title format is
9383 Vicon_title_format if FRAME is iconified, otherwise it is
9384 frame_title_format. */
9385
9386 static void
9387 x_consider_frame_title (frame)
9388 Lisp_Object frame;
9389 {
9390 struct frame *f = XFRAME (frame);
9391
9392 if (FRAME_WINDOW_P (f)
9393 || FRAME_MINIBUF_ONLY_P (f)
9394 || f->explicit_name)
9395 {
9396 /* Do we have more than one visible frame on this X display? */
9397 Lisp_Object tail;
9398 Lisp_Object fmt;
9399 int title_start;
9400 char *title;
9401 int len;
9402 struct it it;
9403 int count = SPECPDL_INDEX ();
9404
9405 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9406 {
9407 Lisp_Object other_frame = XCAR (tail);
9408 struct frame *tf = XFRAME (other_frame);
9409
9410 if (tf != f
9411 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9412 && !FRAME_MINIBUF_ONLY_P (tf)
9413 && !EQ (other_frame, tip_frame)
9414 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9415 break;
9416 }
9417
9418 /* Set global variable indicating that multiple frames exist. */
9419 multiple_frames = CONSP (tail);
9420
9421 /* Switch to the buffer of selected window of the frame. Set up
9422 mode_line_target so that display_mode_element will output into
9423 mode_line_noprop_buf; then display the title. */
9424 record_unwind_protect (unwind_format_mode_line,
9425 format_mode_line_unwind_data
9426 (current_buffer, selected_window, 0));
9427
9428 Fselect_window (f->selected_window, Qt);
9429 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9430 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9431
9432 mode_line_target = MODE_LINE_TITLE;
9433 title_start = MODE_LINE_NOPROP_LEN (0);
9434 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9435 NULL, DEFAULT_FACE_ID);
9436 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9437 len = MODE_LINE_NOPROP_LEN (title_start);
9438 title = mode_line_noprop_buf + title_start;
9439 unbind_to (count, Qnil);
9440
9441 /* Set the title only if it's changed. This avoids consing in
9442 the common case where it hasn't. (If it turns out that we've
9443 already wasted too much time by walking through the list with
9444 display_mode_element, then we might need to optimize at a
9445 higher level than this.) */
9446 if (! STRINGP (f->name)
9447 || SBYTES (f->name) != len
9448 || bcmp (title, SDATA (f->name), len) != 0)
9449 x_implicitly_set_name (f, make_string (title, len), Qnil);
9450 }
9451 }
9452
9453 #endif /* not HAVE_WINDOW_SYSTEM */
9454
9455
9456
9457 \f
9458 /***********************************************************************
9459 Menu Bars
9460 ***********************************************************************/
9461
9462
9463 /* Prepare for redisplay by updating menu-bar item lists when
9464 appropriate. This can call eval. */
9465
9466 void
9467 prepare_menu_bars ()
9468 {
9469 int all_windows;
9470 struct gcpro gcpro1, gcpro2;
9471 struct frame *f;
9472 Lisp_Object tooltip_frame;
9473
9474 #ifdef HAVE_WINDOW_SYSTEM
9475 tooltip_frame = tip_frame;
9476 #else
9477 tooltip_frame = Qnil;
9478 #endif
9479
9480 /* Update all frame titles based on their buffer names, etc. We do
9481 this before the menu bars so that the buffer-menu will show the
9482 up-to-date frame titles. */
9483 #ifdef HAVE_WINDOW_SYSTEM
9484 if (windows_or_buffers_changed || update_mode_lines)
9485 {
9486 Lisp_Object tail, frame;
9487
9488 FOR_EACH_FRAME (tail, frame)
9489 {
9490 f = XFRAME (frame);
9491 if (!EQ (frame, tooltip_frame)
9492 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9493 x_consider_frame_title (frame);
9494 }
9495 }
9496 #endif /* HAVE_WINDOW_SYSTEM */
9497
9498 /* Update the menu bar item lists, if appropriate. This has to be
9499 done before any actual redisplay or generation of display lines. */
9500 all_windows = (update_mode_lines
9501 || buffer_shared > 1
9502 || windows_or_buffers_changed);
9503 if (all_windows)
9504 {
9505 Lisp_Object tail, frame;
9506 int count = SPECPDL_INDEX ();
9507 /* 1 means that update_menu_bar has run its hooks
9508 so any further calls to update_menu_bar shouldn't do so again. */
9509 int menu_bar_hooks_run = 0;
9510
9511 record_unwind_save_match_data ();
9512
9513 FOR_EACH_FRAME (tail, frame)
9514 {
9515 f = XFRAME (frame);
9516
9517 /* Ignore tooltip frame. */
9518 if (EQ (frame, tooltip_frame))
9519 continue;
9520
9521 /* If a window on this frame changed size, report that to
9522 the user and clear the size-change flag. */
9523 if (FRAME_WINDOW_SIZES_CHANGED (f))
9524 {
9525 Lisp_Object functions;
9526
9527 /* Clear flag first in case we get an error below. */
9528 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9529 functions = Vwindow_size_change_functions;
9530 GCPRO2 (tail, functions);
9531
9532 while (CONSP (functions))
9533 {
9534 if (!EQ (XCAR (functions), Qt))
9535 call1 (XCAR (functions), frame);
9536 functions = XCDR (functions);
9537 }
9538 UNGCPRO;
9539 }
9540
9541 GCPRO1 (tail);
9542 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9543 #ifdef HAVE_WINDOW_SYSTEM
9544 update_tool_bar (f, 0);
9545 #endif
9546 #ifdef HAVE_NS
9547 if (windows_or_buffers_changed
9548 && FRAME_NS_P (f))
9549 ns_set_doc_edited (f, Fbuffer_modified_p
9550 (XWINDOW (f->selected_window)->buffer));
9551 #endif
9552 UNGCPRO;
9553 }
9554
9555 unbind_to (count, Qnil);
9556 }
9557 else
9558 {
9559 struct frame *sf = SELECTED_FRAME ();
9560 update_menu_bar (sf, 1, 0);
9561 #ifdef HAVE_WINDOW_SYSTEM
9562 update_tool_bar (sf, 1);
9563 #endif
9564 }
9565
9566 /* Motif needs this. See comment in xmenu.c. Turn it off when
9567 pending_menu_activation is not defined. */
9568 #ifdef USE_X_TOOLKIT
9569 pending_menu_activation = 0;
9570 #endif
9571 }
9572
9573
9574 /* Update the menu bar item list for frame F. This has to be done
9575 before we start to fill in any display lines, because it can call
9576 eval.
9577
9578 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9579
9580 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9581 already ran the menu bar hooks for this redisplay, so there
9582 is no need to run them again. The return value is the
9583 updated value of this flag, to pass to the next call. */
9584
9585 static int
9586 update_menu_bar (f, save_match_data, hooks_run)
9587 struct frame *f;
9588 int save_match_data;
9589 int hooks_run;
9590 {
9591 Lisp_Object window;
9592 register struct window *w;
9593
9594 /* If called recursively during a menu update, do nothing. This can
9595 happen when, for instance, an activate-menubar-hook causes a
9596 redisplay. */
9597 if (inhibit_menubar_update)
9598 return hooks_run;
9599
9600 window = FRAME_SELECTED_WINDOW (f);
9601 w = XWINDOW (window);
9602
9603 if (FRAME_WINDOW_P (f)
9604 ?
9605 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9606 || defined (HAVE_NS) || defined (USE_GTK)
9607 FRAME_EXTERNAL_MENU_BAR (f)
9608 #else
9609 FRAME_MENU_BAR_LINES (f) > 0
9610 #endif
9611 : FRAME_MENU_BAR_LINES (f) > 0)
9612 {
9613 /* If the user has switched buffers or windows, we need to
9614 recompute to reflect the new bindings. But we'll
9615 recompute when update_mode_lines is set too; that means
9616 that people can use force-mode-line-update to request
9617 that the menu bar be recomputed. The adverse effect on
9618 the rest of the redisplay algorithm is about the same as
9619 windows_or_buffers_changed anyway. */
9620 if (windows_or_buffers_changed
9621 /* This used to test w->update_mode_line, but we believe
9622 there is no need to recompute the menu in that case. */
9623 || update_mode_lines
9624 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9625 < BUF_MODIFF (XBUFFER (w->buffer)))
9626 != !NILP (w->last_had_star))
9627 || ((!NILP (Vtransient_mark_mode)
9628 && !NILP (XBUFFER (w->buffer)->mark_active))
9629 != !NILP (w->region_showing)))
9630 {
9631 struct buffer *prev = current_buffer;
9632 int count = SPECPDL_INDEX ();
9633
9634 specbind (Qinhibit_menubar_update, Qt);
9635
9636 set_buffer_internal_1 (XBUFFER (w->buffer));
9637 if (save_match_data)
9638 record_unwind_save_match_data ();
9639 if (NILP (Voverriding_local_map_menu_flag))
9640 {
9641 specbind (Qoverriding_terminal_local_map, Qnil);
9642 specbind (Qoverriding_local_map, Qnil);
9643 }
9644
9645 if (!hooks_run)
9646 {
9647 /* Run the Lucid hook. */
9648 safe_run_hooks (Qactivate_menubar_hook);
9649
9650 /* If it has changed current-menubar from previous value,
9651 really recompute the menu-bar from the value. */
9652 if (! NILP (Vlucid_menu_bar_dirty_flag))
9653 call0 (Qrecompute_lucid_menubar);
9654
9655 safe_run_hooks (Qmenu_bar_update_hook);
9656
9657 hooks_run = 1;
9658 }
9659
9660 XSETFRAME (Vmenu_updating_frame, f);
9661 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9662
9663 /* Redisplay the menu bar in case we changed it. */
9664 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
9665 || defined (HAVE_NS) || defined (USE_GTK)
9666 if (FRAME_WINDOW_P (f))
9667 {
9668 #if defined (HAVE_NS)
9669 /* All frames on Mac OS share the same menubar. So only
9670 the selected frame should be allowed to set it. */
9671 if (f == SELECTED_FRAME ())
9672 #endif
9673 set_frame_menubar (f, 0, 0);
9674 }
9675 else
9676 /* On a terminal screen, the menu bar is an ordinary screen
9677 line, and this makes it get updated. */
9678 w->update_mode_line = Qt;
9679 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9680 /* In the non-toolkit version, the menu bar is an ordinary screen
9681 line, and this makes it get updated. */
9682 w->update_mode_line = Qt;
9683 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
9684
9685 unbind_to (count, Qnil);
9686 set_buffer_internal_1 (prev);
9687 }
9688 }
9689
9690 return hooks_run;
9691 }
9692
9693
9694 \f
9695 /***********************************************************************
9696 Output Cursor
9697 ***********************************************************************/
9698
9699 #ifdef HAVE_WINDOW_SYSTEM
9700
9701 /* EXPORT:
9702 Nominal cursor position -- where to draw output.
9703 HPOS and VPOS are window relative glyph matrix coordinates.
9704 X and Y are window relative pixel coordinates. */
9705
9706 struct cursor_pos output_cursor;
9707
9708
9709 /* EXPORT:
9710 Set the global variable output_cursor to CURSOR. All cursor
9711 positions are relative to updated_window. */
9712
9713 void
9714 set_output_cursor (cursor)
9715 struct cursor_pos *cursor;
9716 {
9717 output_cursor.hpos = cursor->hpos;
9718 output_cursor.vpos = cursor->vpos;
9719 output_cursor.x = cursor->x;
9720 output_cursor.y = cursor->y;
9721 }
9722
9723
9724 /* EXPORT for RIF:
9725 Set a nominal cursor position.
9726
9727 HPOS and VPOS are column/row positions in a window glyph matrix. X
9728 and Y are window text area relative pixel positions.
9729
9730 If this is done during an update, updated_window will contain the
9731 window that is being updated and the position is the future output
9732 cursor position for that window. If updated_window is null, use
9733 selected_window and display the cursor at the given position. */
9734
9735 void
9736 x_cursor_to (vpos, hpos, y, x)
9737 int vpos, hpos, y, x;
9738 {
9739 struct window *w;
9740
9741 /* If updated_window is not set, work on selected_window. */
9742 if (updated_window)
9743 w = updated_window;
9744 else
9745 w = XWINDOW (selected_window);
9746
9747 /* Set the output cursor. */
9748 output_cursor.hpos = hpos;
9749 output_cursor.vpos = vpos;
9750 output_cursor.x = x;
9751 output_cursor.y = y;
9752
9753 /* If not called as part of an update, really display the cursor.
9754 This will also set the cursor position of W. */
9755 if (updated_window == NULL)
9756 {
9757 BLOCK_INPUT;
9758 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9759 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9760 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9761 UNBLOCK_INPUT;
9762 }
9763 }
9764
9765 #endif /* HAVE_WINDOW_SYSTEM */
9766
9767 \f
9768 /***********************************************************************
9769 Tool-bars
9770 ***********************************************************************/
9771
9772 #ifdef HAVE_WINDOW_SYSTEM
9773
9774 /* Where the mouse was last time we reported a mouse event. */
9775
9776 FRAME_PTR last_mouse_frame;
9777
9778 /* Tool-bar item index of the item on which a mouse button was pressed
9779 or -1. */
9780
9781 int last_tool_bar_item;
9782
9783
9784 static Lisp_Object
9785 update_tool_bar_unwind (frame)
9786 Lisp_Object frame;
9787 {
9788 selected_frame = frame;
9789 return Qnil;
9790 }
9791
9792 /* Update the tool-bar item list for frame F. This has to be done
9793 before we start to fill in any display lines. Called from
9794 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9795 and restore it here. */
9796
9797 static void
9798 update_tool_bar (f, save_match_data)
9799 struct frame *f;
9800 int save_match_data;
9801 {
9802 #if defined (USE_GTK) || defined (HAVE_NS)
9803 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9804 #else
9805 int do_update = WINDOWP (f->tool_bar_window)
9806 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9807 #endif
9808
9809 if (do_update)
9810 {
9811 Lisp_Object window;
9812 struct window *w;
9813
9814 window = FRAME_SELECTED_WINDOW (f);
9815 w = XWINDOW (window);
9816
9817 /* If the user has switched buffers or windows, we need to
9818 recompute to reflect the new bindings. But we'll
9819 recompute when update_mode_lines is set too; that means
9820 that people can use force-mode-line-update to request
9821 that the menu bar be recomputed. The adverse effect on
9822 the rest of the redisplay algorithm is about the same as
9823 windows_or_buffers_changed anyway. */
9824 if (windows_or_buffers_changed
9825 || !NILP (w->update_mode_line)
9826 || update_mode_lines
9827 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9828 < BUF_MODIFF (XBUFFER (w->buffer)))
9829 != !NILP (w->last_had_star))
9830 || ((!NILP (Vtransient_mark_mode)
9831 && !NILP (XBUFFER (w->buffer)->mark_active))
9832 != !NILP (w->region_showing)))
9833 {
9834 struct buffer *prev = current_buffer;
9835 int count = SPECPDL_INDEX ();
9836 Lisp_Object frame, new_tool_bar;
9837 int new_n_tool_bar;
9838 struct gcpro gcpro1;
9839
9840 /* Set current_buffer to the buffer of the selected
9841 window of the frame, so that we get the right local
9842 keymaps. */
9843 set_buffer_internal_1 (XBUFFER (w->buffer));
9844
9845 /* Save match data, if we must. */
9846 if (save_match_data)
9847 record_unwind_save_match_data ();
9848
9849 /* Make sure that we don't accidentally use bogus keymaps. */
9850 if (NILP (Voverriding_local_map_menu_flag))
9851 {
9852 specbind (Qoverriding_terminal_local_map, Qnil);
9853 specbind (Qoverriding_local_map, Qnil);
9854 }
9855
9856 GCPRO1 (new_tool_bar);
9857
9858 /* We must temporarily set the selected frame to this frame
9859 before calling tool_bar_items, because the calculation of
9860 the tool-bar keymap uses the selected frame (see
9861 `tool-bar-make-keymap' in tool-bar.el). */
9862 record_unwind_protect (update_tool_bar_unwind, selected_frame);
9863 XSETFRAME (frame, f);
9864 selected_frame = frame;
9865
9866 /* Build desired tool-bar items from keymaps. */
9867 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9868 &new_n_tool_bar);
9869
9870 /* Redisplay the tool-bar if we changed it. */
9871 if (new_n_tool_bar != f->n_tool_bar_items
9872 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9873 {
9874 /* Redisplay that happens asynchronously due to an expose event
9875 may access f->tool_bar_items. Make sure we update both
9876 variables within BLOCK_INPUT so no such event interrupts. */
9877 BLOCK_INPUT;
9878 f->tool_bar_items = new_tool_bar;
9879 f->n_tool_bar_items = new_n_tool_bar;
9880 w->update_mode_line = Qt;
9881 UNBLOCK_INPUT;
9882 }
9883
9884 UNGCPRO;
9885
9886 unbind_to (count, Qnil);
9887 set_buffer_internal_1 (prev);
9888 }
9889 }
9890 }
9891
9892
9893 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9894 F's desired tool-bar contents. F->tool_bar_items must have
9895 been set up previously by calling prepare_menu_bars. */
9896
9897 static void
9898 build_desired_tool_bar_string (f)
9899 struct frame *f;
9900 {
9901 int i, size, size_needed;
9902 struct gcpro gcpro1, gcpro2, gcpro3;
9903 Lisp_Object image, plist, props;
9904
9905 image = plist = props = Qnil;
9906 GCPRO3 (image, plist, props);
9907
9908 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9909 Otherwise, make a new string. */
9910
9911 /* The size of the string we might be able to reuse. */
9912 size = (STRINGP (f->desired_tool_bar_string)
9913 ? SCHARS (f->desired_tool_bar_string)
9914 : 0);
9915
9916 /* We need one space in the string for each image. */
9917 size_needed = f->n_tool_bar_items;
9918
9919 /* Reuse f->desired_tool_bar_string, if possible. */
9920 if (size < size_needed || NILP (f->desired_tool_bar_string))
9921 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9922 make_number (' '));
9923 else
9924 {
9925 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9926 Fremove_text_properties (make_number (0), make_number (size),
9927 props, f->desired_tool_bar_string);
9928 }
9929
9930 /* Put a `display' property on the string for the images to display,
9931 put a `menu_item' property on tool-bar items with a value that
9932 is the index of the item in F's tool-bar item vector. */
9933 for (i = 0; i < f->n_tool_bar_items; ++i)
9934 {
9935 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9936
9937 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9938 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9939 int hmargin, vmargin, relief, idx, end;
9940 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9941
9942 /* If image is a vector, choose the image according to the
9943 button state. */
9944 image = PROP (TOOL_BAR_ITEM_IMAGES);
9945 if (VECTORP (image))
9946 {
9947 if (enabled_p)
9948 idx = (selected_p
9949 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9950 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9951 else
9952 idx = (selected_p
9953 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9954 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9955
9956 xassert (ASIZE (image) >= idx);
9957 image = AREF (image, idx);
9958 }
9959 else
9960 idx = -1;
9961
9962 /* Ignore invalid image specifications. */
9963 if (!valid_image_p (image))
9964 continue;
9965
9966 /* Display the tool-bar button pressed, or depressed. */
9967 plist = Fcopy_sequence (XCDR (image));
9968
9969 /* Compute margin and relief to draw. */
9970 relief = (tool_bar_button_relief >= 0
9971 ? tool_bar_button_relief
9972 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9973 hmargin = vmargin = relief;
9974
9975 if (INTEGERP (Vtool_bar_button_margin)
9976 && XINT (Vtool_bar_button_margin) > 0)
9977 {
9978 hmargin += XFASTINT (Vtool_bar_button_margin);
9979 vmargin += XFASTINT (Vtool_bar_button_margin);
9980 }
9981 else if (CONSP (Vtool_bar_button_margin))
9982 {
9983 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9984 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9985 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9986
9987 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9988 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9989 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9990 }
9991
9992 if (auto_raise_tool_bar_buttons_p)
9993 {
9994 /* Add a `:relief' property to the image spec if the item is
9995 selected. */
9996 if (selected_p)
9997 {
9998 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9999 hmargin -= relief;
10000 vmargin -= relief;
10001 }
10002 }
10003 else
10004 {
10005 /* If image is selected, display it pressed, i.e. with a
10006 negative relief. If it's not selected, display it with a
10007 raised relief. */
10008 plist = Fplist_put (plist, QCrelief,
10009 (selected_p
10010 ? make_number (-relief)
10011 : make_number (relief)));
10012 hmargin -= relief;
10013 vmargin -= relief;
10014 }
10015
10016 /* Put a margin around the image. */
10017 if (hmargin || vmargin)
10018 {
10019 if (hmargin == vmargin)
10020 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10021 else
10022 plist = Fplist_put (plist, QCmargin,
10023 Fcons (make_number (hmargin),
10024 make_number (vmargin)));
10025 }
10026
10027 /* If button is not enabled, and we don't have special images
10028 for the disabled state, make the image appear disabled by
10029 applying an appropriate algorithm to it. */
10030 if (!enabled_p && idx < 0)
10031 plist = Fplist_put (plist, QCconversion, Qdisabled);
10032
10033 /* Put a `display' text property on the string for the image to
10034 display. Put a `menu-item' property on the string that gives
10035 the start of this item's properties in the tool-bar items
10036 vector. */
10037 image = Fcons (Qimage, plist);
10038 props = list4 (Qdisplay, image,
10039 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10040
10041 /* Let the last image hide all remaining spaces in the tool bar
10042 string. The string can be longer than needed when we reuse a
10043 previous string. */
10044 if (i + 1 == f->n_tool_bar_items)
10045 end = SCHARS (f->desired_tool_bar_string);
10046 else
10047 end = i + 1;
10048 Fadd_text_properties (make_number (i), make_number (end),
10049 props, f->desired_tool_bar_string);
10050 #undef PROP
10051 }
10052
10053 UNGCPRO;
10054 }
10055
10056
10057 /* Display one line of the tool-bar of frame IT->f.
10058
10059 HEIGHT specifies the desired height of the tool-bar line.
10060 If the actual height of the glyph row is less than HEIGHT, the
10061 row's height is increased to HEIGHT, and the icons are centered
10062 vertically in the new height.
10063
10064 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10065 count a final empty row in case the tool-bar width exactly matches
10066 the window width.
10067 */
10068
10069 static void
10070 display_tool_bar_line (it, height)
10071 struct it *it;
10072 int height;
10073 {
10074 struct glyph_row *row = it->glyph_row;
10075 int max_x = it->last_visible_x;
10076 struct glyph *last;
10077
10078 prepare_desired_row (row);
10079 row->y = it->current_y;
10080
10081 /* Note that this isn't made use of if the face hasn't a box,
10082 so there's no need to check the face here. */
10083 it->start_of_box_run_p = 1;
10084
10085 while (it->current_x < max_x)
10086 {
10087 int x, n_glyphs_before, i, nglyphs;
10088 struct it it_before;
10089
10090 /* Get the next display element. */
10091 if (!get_next_display_element (it))
10092 {
10093 /* Don't count empty row if we are counting needed tool-bar lines. */
10094 if (height < 0 && !it->hpos)
10095 return;
10096 break;
10097 }
10098
10099 /* Produce glyphs. */
10100 n_glyphs_before = row->used[TEXT_AREA];
10101 it_before = *it;
10102
10103 PRODUCE_GLYPHS (it);
10104
10105 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10106 i = 0;
10107 x = it_before.current_x;
10108 while (i < nglyphs)
10109 {
10110 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10111
10112 if (x + glyph->pixel_width > max_x)
10113 {
10114 /* Glyph doesn't fit on line. Backtrack. */
10115 row->used[TEXT_AREA] = n_glyphs_before;
10116 *it = it_before;
10117 /* If this is the only glyph on this line, it will never fit on the
10118 toolbar, so skip it. But ensure there is at least one glyph,
10119 so we don't accidentally disable the tool-bar. */
10120 if (n_glyphs_before == 0
10121 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10122 break;
10123 goto out;
10124 }
10125
10126 ++it->hpos;
10127 x += glyph->pixel_width;
10128 ++i;
10129 }
10130
10131 /* Stop at line ends. */
10132 if (ITERATOR_AT_END_OF_LINE_P (it))
10133 break;
10134
10135 set_iterator_to_next (it, 1);
10136 }
10137
10138 out:;
10139
10140 row->displays_text_p = row->used[TEXT_AREA] != 0;
10141
10142 /* Use default face for the border below the tool bar.
10143
10144 FIXME: When auto-resize-tool-bars is grow-only, there is
10145 no additional border below the possibly empty tool-bar lines.
10146 So to make the extra empty lines look "normal", we have to
10147 use the tool-bar face for the border too. */
10148 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10149 it->face_id = DEFAULT_FACE_ID;
10150
10151 extend_face_to_end_of_line (it);
10152 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10153 last->right_box_line_p = 1;
10154 if (last == row->glyphs[TEXT_AREA])
10155 last->left_box_line_p = 1;
10156
10157 /* Make line the desired height and center it vertically. */
10158 if ((height -= it->max_ascent + it->max_descent) > 0)
10159 {
10160 /* Don't add more than one line height. */
10161 height %= FRAME_LINE_HEIGHT (it->f);
10162 it->max_ascent += height / 2;
10163 it->max_descent += (height + 1) / 2;
10164 }
10165
10166 compute_line_metrics (it);
10167
10168 /* If line is empty, make it occupy the rest of the tool-bar. */
10169 if (!row->displays_text_p)
10170 {
10171 row->height = row->phys_height = it->last_visible_y - row->y;
10172 row->visible_height = row->height;
10173 row->ascent = row->phys_ascent = 0;
10174 row->extra_line_spacing = 0;
10175 }
10176
10177 row->full_width_p = 1;
10178 row->continued_p = 0;
10179 row->truncated_on_left_p = 0;
10180 row->truncated_on_right_p = 0;
10181
10182 it->current_x = it->hpos = 0;
10183 it->current_y += row->height;
10184 ++it->vpos;
10185 ++it->glyph_row;
10186 }
10187
10188
10189 /* Max tool-bar height. */
10190
10191 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10192 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10193
10194 /* Value is the number of screen lines needed to make all tool-bar
10195 items of frame F visible. The number of actual rows needed is
10196 returned in *N_ROWS if non-NULL. */
10197
10198 static int
10199 tool_bar_lines_needed (f, n_rows)
10200 struct frame *f;
10201 int *n_rows;
10202 {
10203 struct window *w = XWINDOW (f->tool_bar_window);
10204 struct it it;
10205 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10206 the desired matrix, so use (unused) mode-line row as temporary row to
10207 avoid destroying the first tool-bar row. */
10208 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10209
10210 /* Initialize an iterator for iteration over
10211 F->desired_tool_bar_string in the tool-bar window of frame F. */
10212 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10213 it.first_visible_x = 0;
10214 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10215 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10216
10217 while (!ITERATOR_AT_END_P (&it))
10218 {
10219 clear_glyph_row (temp_row);
10220 it.glyph_row = temp_row;
10221 display_tool_bar_line (&it, -1);
10222 }
10223 clear_glyph_row (temp_row);
10224
10225 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10226 if (n_rows)
10227 *n_rows = it.vpos > 0 ? it.vpos : -1;
10228
10229 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10230 }
10231
10232
10233 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10234 0, 1, 0,
10235 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10236 (frame)
10237 Lisp_Object frame;
10238 {
10239 struct frame *f;
10240 struct window *w;
10241 int nlines = 0;
10242
10243 if (NILP (frame))
10244 frame = selected_frame;
10245 else
10246 CHECK_FRAME (frame);
10247 f = XFRAME (frame);
10248
10249 if (WINDOWP (f->tool_bar_window)
10250 || (w = XWINDOW (f->tool_bar_window),
10251 WINDOW_TOTAL_LINES (w) > 0))
10252 {
10253 update_tool_bar (f, 1);
10254 if (f->n_tool_bar_items)
10255 {
10256 build_desired_tool_bar_string (f);
10257 nlines = tool_bar_lines_needed (f, NULL);
10258 }
10259 }
10260
10261 return make_number (nlines);
10262 }
10263
10264
10265 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10266 height should be changed. */
10267
10268 static int
10269 redisplay_tool_bar (f)
10270 struct frame *f;
10271 {
10272 struct window *w;
10273 struct it it;
10274 struct glyph_row *row;
10275
10276 #if defined (USE_GTK) || defined (HAVE_NS)
10277 if (FRAME_EXTERNAL_TOOL_BAR (f))
10278 update_frame_tool_bar (f);
10279 return 0;
10280 #endif
10281
10282 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10283 do anything. This means you must start with tool-bar-lines
10284 non-zero to get the auto-sizing effect. Or in other words, you
10285 can turn off tool-bars by specifying tool-bar-lines zero. */
10286 if (!WINDOWP (f->tool_bar_window)
10287 || (w = XWINDOW (f->tool_bar_window),
10288 WINDOW_TOTAL_LINES (w) == 0))
10289 return 0;
10290
10291 /* Set up an iterator for the tool-bar window. */
10292 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10293 it.first_visible_x = 0;
10294 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10295 row = it.glyph_row;
10296
10297 /* Build a string that represents the contents of the tool-bar. */
10298 build_desired_tool_bar_string (f);
10299 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10300
10301 if (f->n_tool_bar_rows == 0)
10302 {
10303 int nlines;
10304
10305 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10306 nlines != WINDOW_TOTAL_LINES (w)))
10307 {
10308 extern Lisp_Object Qtool_bar_lines;
10309 Lisp_Object frame;
10310 int old_height = WINDOW_TOTAL_LINES (w);
10311
10312 XSETFRAME (frame, f);
10313 Fmodify_frame_parameters (frame,
10314 Fcons (Fcons (Qtool_bar_lines,
10315 make_number (nlines)),
10316 Qnil));
10317 if (WINDOW_TOTAL_LINES (w) != old_height)
10318 {
10319 clear_glyph_matrix (w->desired_matrix);
10320 fonts_changed_p = 1;
10321 return 1;
10322 }
10323 }
10324 }
10325
10326 /* Display as many lines as needed to display all tool-bar items. */
10327
10328 if (f->n_tool_bar_rows > 0)
10329 {
10330 int border, rows, height, extra;
10331
10332 if (INTEGERP (Vtool_bar_border))
10333 border = XINT (Vtool_bar_border);
10334 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10335 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10336 else if (EQ (Vtool_bar_border, Qborder_width))
10337 border = f->border_width;
10338 else
10339 border = 0;
10340 if (border < 0)
10341 border = 0;
10342
10343 rows = f->n_tool_bar_rows;
10344 height = max (1, (it.last_visible_y - border) / rows);
10345 extra = it.last_visible_y - border - height * rows;
10346
10347 while (it.current_y < it.last_visible_y)
10348 {
10349 int h = 0;
10350 if (extra > 0 && rows-- > 0)
10351 {
10352 h = (extra + rows - 1) / rows;
10353 extra -= h;
10354 }
10355 display_tool_bar_line (&it, height + h);
10356 }
10357 }
10358 else
10359 {
10360 while (it.current_y < it.last_visible_y)
10361 display_tool_bar_line (&it, 0);
10362 }
10363
10364 /* It doesn't make much sense to try scrolling in the tool-bar
10365 window, so don't do it. */
10366 w->desired_matrix->no_scrolling_p = 1;
10367 w->must_be_updated_p = 1;
10368
10369 if (!NILP (Vauto_resize_tool_bars))
10370 {
10371 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10372 int change_height_p = 0;
10373
10374 /* If we couldn't display everything, change the tool-bar's
10375 height if there is room for more. */
10376 if (IT_STRING_CHARPOS (it) < it.end_charpos
10377 && it.current_y < max_tool_bar_height)
10378 change_height_p = 1;
10379
10380 row = it.glyph_row - 1;
10381
10382 /* If there are blank lines at the end, except for a partially
10383 visible blank line at the end that is smaller than
10384 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10385 if (!row->displays_text_p
10386 && row->height >= FRAME_LINE_HEIGHT (f))
10387 change_height_p = 1;
10388
10389 /* If row displays tool-bar items, but is partially visible,
10390 change the tool-bar's height. */
10391 if (row->displays_text_p
10392 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10393 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10394 change_height_p = 1;
10395
10396 /* Resize windows as needed by changing the `tool-bar-lines'
10397 frame parameter. */
10398 if (change_height_p)
10399 {
10400 extern Lisp_Object Qtool_bar_lines;
10401 Lisp_Object frame;
10402 int old_height = WINDOW_TOTAL_LINES (w);
10403 int nrows;
10404 int nlines = tool_bar_lines_needed (f, &nrows);
10405
10406 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10407 && !f->minimize_tool_bar_window_p)
10408 ? (nlines > old_height)
10409 : (nlines != old_height));
10410 f->minimize_tool_bar_window_p = 0;
10411
10412 if (change_height_p)
10413 {
10414 XSETFRAME (frame, f);
10415 Fmodify_frame_parameters (frame,
10416 Fcons (Fcons (Qtool_bar_lines,
10417 make_number (nlines)),
10418 Qnil));
10419 if (WINDOW_TOTAL_LINES (w) != old_height)
10420 {
10421 clear_glyph_matrix (w->desired_matrix);
10422 f->n_tool_bar_rows = nrows;
10423 fonts_changed_p = 1;
10424 return 1;
10425 }
10426 }
10427 }
10428 }
10429
10430 f->minimize_tool_bar_window_p = 0;
10431 return 0;
10432 }
10433
10434
10435 /* Get information about the tool-bar item which is displayed in GLYPH
10436 on frame F. Return in *PROP_IDX the index where tool-bar item
10437 properties start in F->tool_bar_items. Value is zero if
10438 GLYPH doesn't display a tool-bar item. */
10439
10440 static int
10441 tool_bar_item_info (f, glyph, prop_idx)
10442 struct frame *f;
10443 struct glyph *glyph;
10444 int *prop_idx;
10445 {
10446 Lisp_Object prop;
10447 int success_p;
10448 int charpos;
10449
10450 /* This function can be called asynchronously, which means we must
10451 exclude any possibility that Fget_text_property signals an
10452 error. */
10453 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10454 charpos = max (0, charpos);
10455
10456 /* Get the text property `menu-item' at pos. The value of that
10457 property is the start index of this item's properties in
10458 F->tool_bar_items. */
10459 prop = Fget_text_property (make_number (charpos),
10460 Qmenu_item, f->current_tool_bar_string);
10461 if (INTEGERP (prop))
10462 {
10463 *prop_idx = XINT (prop);
10464 success_p = 1;
10465 }
10466 else
10467 success_p = 0;
10468
10469 return success_p;
10470 }
10471
10472 \f
10473 /* Get information about the tool-bar item at position X/Y on frame F.
10474 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10475 the current matrix of the tool-bar window of F, or NULL if not
10476 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10477 item in F->tool_bar_items. Value is
10478
10479 -1 if X/Y is not on a tool-bar item
10480 0 if X/Y is on the same item that was highlighted before.
10481 1 otherwise. */
10482
10483 static int
10484 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10485 struct frame *f;
10486 int x, y;
10487 struct glyph **glyph;
10488 int *hpos, *vpos, *prop_idx;
10489 {
10490 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10491 struct window *w = XWINDOW (f->tool_bar_window);
10492 int area;
10493
10494 /* Find the glyph under X/Y. */
10495 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10496 if (*glyph == NULL)
10497 return -1;
10498
10499 /* Get the start of this tool-bar item's properties in
10500 f->tool_bar_items. */
10501 if (!tool_bar_item_info (f, *glyph, prop_idx))
10502 return -1;
10503
10504 /* Is mouse on the highlighted item? */
10505 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10506 && *vpos >= dpyinfo->mouse_face_beg_row
10507 && *vpos <= dpyinfo->mouse_face_end_row
10508 && (*vpos > dpyinfo->mouse_face_beg_row
10509 || *hpos >= dpyinfo->mouse_face_beg_col)
10510 && (*vpos < dpyinfo->mouse_face_end_row
10511 || *hpos < dpyinfo->mouse_face_end_col
10512 || dpyinfo->mouse_face_past_end))
10513 return 0;
10514
10515 return 1;
10516 }
10517
10518
10519 /* EXPORT:
10520 Handle mouse button event on the tool-bar of frame F, at
10521 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10522 0 for button release. MODIFIERS is event modifiers for button
10523 release. */
10524
10525 void
10526 handle_tool_bar_click (f, x, y, down_p, modifiers)
10527 struct frame *f;
10528 int x, y, down_p;
10529 unsigned int modifiers;
10530 {
10531 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10532 struct window *w = XWINDOW (f->tool_bar_window);
10533 int hpos, vpos, prop_idx;
10534 struct glyph *glyph;
10535 Lisp_Object enabled_p;
10536
10537 /* If not on the highlighted tool-bar item, return. */
10538 frame_to_window_pixel_xy (w, &x, &y);
10539 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10540 return;
10541
10542 /* If item is disabled, do nothing. */
10543 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10544 if (NILP (enabled_p))
10545 return;
10546
10547 if (down_p)
10548 {
10549 /* Show item in pressed state. */
10550 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10551 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10552 last_tool_bar_item = prop_idx;
10553 }
10554 else
10555 {
10556 Lisp_Object key, frame;
10557 struct input_event event;
10558 EVENT_INIT (event);
10559
10560 /* Show item in released state. */
10561 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10562 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10563
10564 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10565
10566 XSETFRAME (frame, f);
10567 event.kind = TOOL_BAR_EVENT;
10568 event.frame_or_window = frame;
10569 event.arg = frame;
10570 kbd_buffer_store_event (&event);
10571
10572 event.kind = TOOL_BAR_EVENT;
10573 event.frame_or_window = frame;
10574 event.arg = key;
10575 event.modifiers = modifiers;
10576 kbd_buffer_store_event (&event);
10577 last_tool_bar_item = -1;
10578 }
10579 }
10580
10581
10582 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10583 tool-bar window-relative coordinates X/Y. Called from
10584 note_mouse_highlight. */
10585
10586 static void
10587 note_tool_bar_highlight (f, x, y)
10588 struct frame *f;
10589 int x, y;
10590 {
10591 Lisp_Object window = f->tool_bar_window;
10592 struct window *w = XWINDOW (window);
10593 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10594 int hpos, vpos;
10595 struct glyph *glyph;
10596 struct glyph_row *row;
10597 int i;
10598 Lisp_Object enabled_p;
10599 int prop_idx;
10600 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10601 int mouse_down_p, rc;
10602
10603 /* Function note_mouse_highlight is called with negative x(y
10604 values when mouse moves outside of the frame. */
10605 if (x <= 0 || y <= 0)
10606 {
10607 clear_mouse_face (dpyinfo);
10608 return;
10609 }
10610
10611 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10612 if (rc < 0)
10613 {
10614 /* Not on tool-bar item. */
10615 clear_mouse_face (dpyinfo);
10616 return;
10617 }
10618 else if (rc == 0)
10619 /* On same tool-bar item as before. */
10620 goto set_help_echo;
10621
10622 clear_mouse_face (dpyinfo);
10623
10624 /* Mouse is down, but on different tool-bar item? */
10625 mouse_down_p = (dpyinfo->grabbed
10626 && f == last_mouse_frame
10627 && FRAME_LIVE_P (f));
10628 if (mouse_down_p
10629 && last_tool_bar_item != prop_idx)
10630 return;
10631
10632 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10633 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10634
10635 /* If tool-bar item is not enabled, don't highlight it. */
10636 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10637 if (!NILP (enabled_p))
10638 {
10639 /* Compute the x-position of the glyph. In front and past the
10640 image is a space. We include this in the highlighted area. */
10641 row = MATRIX_ROW (w->current_matrix, vpos);
10642 for (i = x = 0; i < hpos; ++i)
10643 x += row->glyphs[TEXT_AREA][i].pixel_width;
10644
10645 /* Record this as the current active region. */
10646 dpyinfo->mouse_face_beg_col = hpos;
10647 dpyinfo->mouse_face_beg_row = vpos;
10648 dpyinfo->mouse_face_beg_x = x;
10649 dpyinfo->mouse_face_beg_y = row->y;
10650 dpyinfo->mouse_face_past_end = 0;
10651
10652 dpyinfo->mouse_face_end_col = hpos + 1;
10653 dpyinfo->mouse_face_end_row = vpos;
10654 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10655 dpyinfo->mouse_face_end_y = row->y;
10656 dpyinfo->mouse_face_window = window;
10657 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10658
10659 /* Display it as active. */
10660 show_mouse_face (dpyinfo, draw);
10661 dpyinfo->mouse_face_image_state = draw;
10662 }
10663
10664 set_help_echo:
10665
10666 /* Set help_echo_string to a help string to display for this tool-bar item.
10667 XTread_socket does the rest. */
10668 help_echo_object = help_echo_window = Qnil;
10669 help_echo_pos = -1;
10670 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10671 if (NILP (help_echo_string))
10672 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10673 }
10674
10675 #endif /* HAVE_WINDOW_SYSTEM */
10676
10677
10678 \f
10679 /************************************************************************
10680 Horizontal scrolling
10681 ************************************************************************/
10682
10683 static int hscroll_window_tree P_ ((Lisp_Object));
10684 static int hscroll_windows P_ ((Lisp_Object));
10685
10686 /* For all leaf windows in the window tree rooted at WINDOW, set their
10687 hscroll value so that PT is (i) visible in the window, and (ii) so
10688 that it is not within a certain margin at the window's left and
10689 right border. Value is non-zero if any window's hscroll has been
10690 changed. */
10691
10692 static int
10693 hscroll_window_tree (window)
10694 Lisp_Object window;
10695 {
10696 int hscrolled_p = 0;
10697 int hscroll_relative_p = FLOATP (Vhscroll_step);
10698 int hscroll_step_abs = 0;
10699 double hscroll_step_rel = 0;
10700
10701 if (hscroll_relative_p)
10702 {
10703 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10704 if (hscroll_step_rel < 0)
10705 {
10706 hscroll_relative_p = 0;
10707 hscroll_step_abs = 0;
10708 }
10709 }
10710 else if (INTEGERP (Vhscroll_step))
10711 {
10712 hscroll_step_abs = XINT (Vhscroll_step);
10713 if (hscroll_step_abs < 0)
10714 hscroll_step_abs = 0;
10715 }
10716 else
10717 hscroll_step_abs = 0;
10718
10719 while (WINDOWP (window))
10720 {
10721 struct window *w = XWINDOW (window);
10722
10723 if (WINDOWP (w->hchild))
10724 hscrolled_p |= hscroll_window_tree (w->hchild);
10725 else if (WINDOWP (w->vchild))
10726 hscrolled_p |= hscroll_window_tree (w->vchild);
10727 else if (w->cursor.vpos >= 0)
10728 {
10729 int h_margin;
10730 int text_area_width;
10731 struct glyph_row *current_cursor_row
10732 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10733 struct glyph_row *desired_cursor_row
10734 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10735 struct glyph_row *cursor_row
10736 = (desired_cursor_row->enabled_p
10737 ? desired_cursor_row
10738 : current_cursor_row);
10739
10740 text_area_width = window_box_width (w, TEXT_AREA);
10741
10742 /* Scroll when cursor is inside this scroll margin. */
10743 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10744
10745 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10746 && ((XFASTINT (w->hscroll)
10747 && w->cursor.x <= h_margin)
10748 || (cursor_row->enabled_p
10749 && cursor_row->truncated_on_right_p
10750 && (w->cursor.x >= text_area_width - h_margin))))
10751 {
10752 struct it it;
10753 int hscroll;
10754 struct buffer *saved_current_buffer;
10755 int pt;
10756 int wanted_x;
10757
10758 /* Find point in a display of infinite width. */
10759 saved_current_buffer = current_buffer;
10760 current_buffer = XBUFFER (w->buffer);
10761
10762 if (w == XWINDOW (selected_window))
10763 pt = BUF_PT (current_buffer);
10764 else
10765 {
10766 pt = marker_position (w->pointm);
10767 pt = max (BEGV, pt);
10768 pt = min (ZV, pt);
10769 }
10770
10771 /* Move iterator to pt starting at cursor_row->start in
10772 a line with infinite width. */
10773 init_to_row_start (&it, w, cursor_row);
10774 it.last_visible_x = INFINITY;
10775 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10776 current_buffer = saved_current_buffer;
10777
10778 /* Position cursor in window. */
10779 if (!hscroll_relative_p && hscroll_step_abs == 0)
10780 hscroll = max (0, (it.current_x
10781 - (ITERATOR_AT_END_OF_LINE_P (&it)
10782 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10783 : (text_area_width / 2))))
10784 / FRAME_COLUMN_WIDTH (it.f);
10785 else if (w->cursor.x >= text_area_width - h_margin)
10786 {
10787 if (hscroll_relative_p)
10788 wanted_x = text_area_width * (1 - hscroll_step_rel)
10789 - h_margin;
10790 else
10791 wanted_x = text_area_width
10792 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10793 - h_margin;
10794 hscroll
10795 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10796 }
10797 else
10798 {
10799 if (hscroll_relative_p)
10800 wanted_x = text_area_width * hscroll_step_rel
10801 + h_margin;
10802 else
10803 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10804 + h_margin;
10805 hscroll
10806 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10807 }
10808 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10809
10810 /* Don't call Fset_window_hscroll if value hasn't
10811 changed because it will prevent redisplay
10812 optimizations. */
10813 if (XFASTINT (w->hscroll) != hscroll)
10814 {
10815 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10816 w->hscroll = make_number (hscroll);
10817 hscrolled_p = 1;
10818 }
10819 }
10820 }
10821
10822 window = w->next;
10823 }
10824
10825 /* Value is non-zero if hscroll of any leaf window has been changed. */
10826 return hscrolled_p;
10827 }
10828
10829
10830 /* Set hscroll so that cursor is visible and not inside horizontal
10831 scroll margins for all windows in the tree rooted at WINDOW. See
10832 also hscroll_window_tree above. Value is non-zero if any window's
10833 hscroll has been changed. If it has, desired matrices on the frame
10834 of WINDOW are cleared. */
10835
10836 static int
10837 hscroll_windows (window)
10838 Lisp_Object window;
10839 {
10840 int hscrolled_p = hscroll_window_tree (window);
10841 if (hscrolled_p)
10842 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10843 return hscrolled_p;
10844 }
10845
10846
10847 \f
10848 /************************************************************************
10849 Redisplay
10850 ************************************************************************/
10851
10852 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10853 to a non-zero value. This is sometimes handy to have in a debugger
10854 session. */
10855
10856 #if GLYPH_DEBUG
10857
10858 /* First and last unchanged row for try_window_id. */
10859
10860 int debug_first_unchanged_at_end_vpos;
10861 int debug_last_unchanged_at_beg_vpos;
10862
10863 /* Delta vpos and y. */
10864
10865 int debug_dvpos, debug_dy;
10866
10867 /* Delta in characters and bytes for try_window_id. */
10868
10869 int debug_delta, debug_delta_bytes;
10870
10871 /* Values of window_end_pos and window_end_vpos at the end of
10872 try_window_id. */
10873
10874 EMACS_INT debug_end_pos, debug_end_vpos;
10875
10876 /* Append a string to W->desired_matrix->method. FMT is a printf
10877 format string. A1...A9 are a supplement for a variable-length
10878 argument list. If trace_redisplay_p is non-zero also printf the
10879 resulting string to stderr. */
10880
10881 static void
10882 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10883 struct window *w;
10884 char *fmt;
10885 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10886 {
10887 char buffer[512];
10888 char *method = w->desired_matrix->method;
10889 int len = strlen (method);
10890 int size = sizeof w->desired_matrix->method;
10891 int remaining = size - len - 1;
10892
10893 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10894 if (len && remaining)
10895 {
10896 method[len] = '|';
10897 --remaining, ++len;
10898 }
10899
10900 strncpy (method + len, buffer, remaining);
10901
10902 if (trace_redisplay_p)
10903 fprintf (stderr, "%p (%s): %s\n",
10904 w,
10905 ((BUFFERP (w->buffer)
10906 && STRINGP (XBUFFER (w->buffer)->name))
10907 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10908 : "no buffer"),
10909 buffer);
10910 }
10911
10912 #endif /* GLYPH_DEBUG */
10913
10914
10915 /* Value is non-zero if all changes in window W, which displays
10916 current_buffer, are in the text between START and END. START is a
10917 buffer position, END is given as a distance from Z. Used in
10918 redisplay_internal for display optimization. */
10919
10920 static INLINE int
10921 text_outside_line_unchanged_p (w, start, end)
10922 struct window *w;
10923 int start, end;
10924 {
10925 int unchanged_p = 1;
10926
10927 /* If text or overlays have changed, see where. */
10928 if (XFASTINT (w->last_modified) < MODIFF
10929 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10930 {
10931 /* Gap in the line? */
10932 if (GPT < start || Z - GPT < end)
10933 unchanged_p = 0;
10934
10935 /* Changes start in front of the line, or end after it? */
10936 if (unchanged_p
10937 && (BEG_UNCHANGED < start - 1
10938 || END_UNCHANGED < end))
10939 unchanged_p = 0;
10940
10941 /* If selective display, can't optimize if changes start at the
10942 beginning of the line. */
10943 if (unchanged_p
10944 && INTEGERP (current_buffer->selective_display)
10945 && XINT (current_buffer->selective_display) > 0
10946 && (BEG_UNCHANGED < start || GPT <= start))
10947 unchanged_p = 0;
10948
10949 /* If there are overlays at the start or end of the line, these
10950 may have overlay strings with newlines in them. A change at
10951 START, for instance, may actually concern the display of such
10952 overlay strings as well, and they are displayed on different
10953 lines. So, quickly rule out this case. (For the future, it
10954 might be desirable to implement something more telling than
10955 just BEG/END_UNCHANGED.) */
10956 if (unchanged_p)
10957 {
10958 if (BEG + BEG_UNCHANGED == start
10959 && overlay_touches_p (start))
10960 unchanged_p = 0;
10961 if (END_UNCHANGED == end
10962 && overlay_touches_p (Z - end))
10963 unchanged_p = 0;
10964 }
10965 }
10966
10967 return unchanged_p;
10968 }
10969
10970
10971 /* Do a frame update, taking possible shortcuts into account. This is
10972 the main external entry point for redisplay.
10973
10974 If the last redisplay displayed an echo area message and that message
10975 is no longer requested, we clear the echo area or bring back the
10976 mini-buffer if that is in use. */
10977
10978 void
10979 redisplay ()
10980 {
10981 redisplay_internal (0);
10982 }
10983
10984
10985 static Lisp_Object
10986 overlay_arrow_string_or_property (var)
10987 Lisp_Object var;
10988 {
10989 Lisp_Object val;
10990
10991 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10992 return val;
10993
10994 return Voverlay_arrow_string;
10995 }
10996
10997 /* Return 1 if there are any overlay-arrows in current_buffer. */
10998 static int
10999 overlay_arrow_in_current_buffer_p ()
11000 {
11001 Lisp_Object vlist;
11002
11003 for (vlist = Voverlay_arrow_variable_list;
11004 CONSP (vlist);
11005 vlist = XCDR (vlist))
11006 {
11007 Lisp_Object var = XCAR (vlist);
11008 Lisp_Object val;
11009
11010 if (!SYMBOLP (var))
11011 continue;
11012 val = find_symbol_value (var);
11013 if (MARKERP (val)
11014 && current_buffer == XMARKER (val)->buffer)
11015 return 1;
11016 }
11017 return 0;
11018 }
11019
11020
11021 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11022 has changed. */
11023
11024 static int
11025 overlay_arrows_changed_p ()
11026 {
11027 Lisp_Object vlist;
11028
11029 for (vlist = Voverlay_arrow_variable_list;
11030 CONSP (vlist);
11031 vlist = XCDR (vlist))
11032 {
11033 Lisp_Object var = XCAR (vlist);
11034 Lisp_Object val, pstr;
11035
11036 if (!SYMBOLP (var))
11037 continue;
11038 val = find_symbol_value (var);
11039 if (!MARKERP (val))
11040 continue;
11041 if (! EQ (COERCE_MARKER (val),
11042 Fget (var, Qlast_arrow_position))
11043 || ! (pstr = overlay_arrow_string_or_property (var),
11044 EQ (pstr, Fget (var, Qlast_arrow_string))))
11045 return 1;
11046 }
11047 return 0;
11048 }
11049
11050 /* Mark overlay arrows to be updated on next redisplay. */
11051
11052 static void
11053 update_overlay_arrows (up_to_date)
11054 int up_to_date;
11055 {
11056 Lisp_Object vlist;
11057
11058 for (vlist = Voverlay_arrow_variable_list;
11059 CONSP (vlist);
11060 vlist = XCDR (vlist))
11061 {
11062 Lisp_Object var = XCAR (vlist);
11063
11064 if (!SYMBOLP (var))
11065 continue;
11066
11067 if (up_to_date > 0)
11068 {
11069 Lisp_Object val = find_symbol_value (var);
11070 Fput (var, Qlast_arrow_position,
11071 COERCE_MARKER (val));
11072 Fput (var, Qlast_arrow_string,
11073 overlay_arrow_string_or_property (var));
11074 }
11075 else if (up_to_date < 0
11076 || !NILP (Fget (var, Qlast_arrow_position)))
11077 {
11078 Fput (var, Qlast_arrow_position, Qt);
11079 Fput (var, Qlast_arrow_string, Qt);
11080 }
11081 }
11082 }
11083
11084
11085 /* Return overlay arrow string to display at row.
11086 Return integer (bitmap number) for arrow bitmap in left fringe.
11087 Return nil if no overlay arrow. */
11088
11089 static Lisp_Object
11090 overlay_arrow_at_row (it, row)
11091 struct it *it;
11092 struct glyph_row *row;
11093 {
11094 Lisp_Object vlist;
11095
11096 for (vlist = Voverlay_arrow_variable_list;
11097 CONSP (vlist);
11098 vlist = XCDR (vlist))
11099 {
11100 Lisp_Object var = XCAR (vlist);
11101 Lisp_Object val;
11102
11103 if (!SYMBOLP (var))
11104 continue;
11105
11106 val = find_symbol_value (var);
11107
11108 if (MARKERP (val)
11109 && current_buffer == XMARKER (val)->buffer
11110 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11111 {
11112 if (FRAME_WINDOW_P (it->f)
11113 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11114 {
11115 #ifdef HAVE_WINDOW_SYSTEM
11116 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11117 {
11118 int fringe_bitmap;
11119 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11120 return make_number (fringe_bitmap);
11121 }
11122 #endif
11123 return make_number (-1); /* Use default arrow bitmap */
11124 }
11125 return overlay_arrow_string_or_property (var);
11126 }
11127 }
11128
11129 return Qnil;
11130 }
11131
11132 /* Return 1 if point moved out of or into a composition. Otherwise
11133 return 0. PREV_BUF and PREV_PT are the last point buffer and
11134 position. BUF and PT are the current point buffer and position. */
11135
11136 int
11137 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11138 struct buffer *prev_buf, *buf;
11139 int prev_pt, pt;
11140 {
11141 EMACS_INT start, end;
11142 Lisp_Object prop;
11143 Lisp_Object buffer;
11144
11145 XSETBUFFER (buffer, buf);
11146 /* Check a composition at the last point if point moved within the
11147 same buffer. */
11148 if (prev_buf == buf)
11149 {
11150 if (prev_pt == pt)
11151 /* Point didn't move. */
11152 return 0;
11153
11154 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11155 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11156 && COMPOSITION_VALID_P (start, end, prop)
11157 && start < prev_pt && end > prev_pt)
11158 /* The last point was within the composition. Return 1 iff
11159 point moved out of the composition. */
11160 return (pt <= start || pt >= end);
11161 }
11162
11163 /* Check a composition at the current point. */
11164 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11165 && find_composition (pt, -1, &start, &end, &prop, buffer)
11166 && COMPOSITION_VALID_P (start, end, prop)
11167 && start < pt && end > pt);
11168 }
11169
11170
11171 /* Reconsider the setting of B->clip_changed which is displayed
11172 in window W. */
11173
11174 static INLINE void
11175 reconsider_clip_changes (w, b)
11176 struct window *w;
11177 struct buffer *b;
11178 {
11179 if (b->clip_changed
11180 && !NILP (w->window_end_valid)
11181 && w->current_matrix->buffer == b
11182 && w->current_matrix->zv == BUF_ZV (b)
11183 && w->current_matrix->begv == BUF_BEGV (b))
11184 b->clip_changed = 0;
11185
11186 /* If display wasn't paused, and W is not a tool bar window, see if
11187 point has been moved into or out of a composition. In that case,
11188 we set b->clip_changed to 1 to force updating the screen. If
11189 b->clip_changed has already been set to 1, we can skip this
11190 check. */
11191 if (!b->clip_changed
11192 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11193 {
11194 int pt;
11195
11196 if (w == XWINDOW (selected_window))
11197 pt = BUF_PT (current_buffer);
11198 else
11199 pt = marker_position (w->pointm);
11200
11201 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11202 || pt != XINT (w->last_point))
11203 && check_point_in_composition (w->current_matrix->buffer,
11204 XINT (w->last_point),
11205 XBUFFER (w->buffer), pt))
11206 b->clip_changed = 1;
11207 }
11208 }
11209 \f
11210
11211 /* Select FRAME to forward the values of frame-local variables into C
11212 variables so that the redisplay routines can access those values
11213 directly. */
11214
11215 static void
11216 select_frame_for_redisplay (frame)
11217 Lisp_Object frame;
11218 {
11219 Lisp_Object tail, symbol, val;
11220 Lisp_Object old = selected_frame;
11221 struct Lisp_Symbol *sym;
11222
11223 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11224
11225 selected_frame = frame;
11226
11227 do
11228 {
11229 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11230 if (CONSP (XCAR (tail))
11231 && (symbol = XCAR (XCAR (tail)),
11232 SYMBOLP (symbol))
11233 && (sym = indirect_variable (XSYMBOL (symbol)),
11234 val = sym->value,
11235 (BUFFER_LOCAL_VALUEP (val)))
11236 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11237 /* Use find_symbol_value rather than Fsymbol_value
11238 to avoid an error if it is void. */
11239 find_symbol_value (symbol);
11240 } while (!EQ (frame, old) && (frame = old, 1));
11241 }
11242
11243
11244 #define STOP_POLLING \
11245 do { if (! polling_stopped_here) stop_polling (); \
11246 polling_stopped_here = 1; } while (0)
11247
11248 #define RESUME_POLLING \
11249 do { if (polling_stopped_here) start_polling (); \
11250 polling_stopped_here = 0; } while (0)
11251
11252
11253 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11254 response to any user action; therefore, we should preserve the echo
11255 area. (Actually, our caller does that job.) Perhaps in the future
11256 avoid recentering windows if it is not necessary; currently that
11257 causes some problems. */
11258
11259 static void
11260 redisplay_internal (preserve_echo_area)
11261 int preserve_echo_area;
11262 {
11263 struct window *w = XWINDOW (selected_window);
11264 struct frame *f;
11265 int pause;
11266 int must_finish = 0;
11267 struct text_pos tlbufpos, tlendpos;
11268 int number_of_visible_frames;
11269 int count, count1;
11270 struct frame *sf;
11271 int polling_stopped_here = 0;
11272 Lisp_Object old_frame = selected_frame;
11273
11274 /* Non-zero means redisplay has to consider all windows on all
11275 frames. Zero means, only selected_window is considered. */
11276 int consider_all_windows_p;
11277
11278 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11279
11280 /* No redisplay if running in batch mode or frame is not yet fully
11281 initialized, or redisplay is explicitly turned off by setting
11282 Vinhibit_redisplay. */
11283 if (FRAME_INITIAL_P (SELECTED_FRAME ())
11284 || !NILP (Vinhibit_redisplay))
11285 return;
11286
11287 /* Don't examine these until after testing Vinhibit_redisplay.
11288 When Emacs is shutting down, perhaps because its connection to
11289 X has dropped, we should not look at them at all. */
11290 f = XFRAME (w->frame);
11291 sf = SELECTED_FRAME ();
11292
11293 if (!f->glyphs_initialized_p)
11294 return;
11295
11296 /* The flag redisplay_performed_directly_p is set by
11297 direct_output_for_insert when it already did the whole screen
11298 update necessary. */
11299 if (redisplay_performed_directly_p)
11300 {
11301 redisplay_performed_directly_p = 0;
11302 if (!hscroll_windows (selected_window))
11303 return;
11304 }
11305
11306 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
11307 if (popup_activated ())
11308 return;
11309 #endif
11310
11311 /* I don't think this happens but let's be paranoid. */
11312 if (redisplaying_p)
11313 return;
11314
11315 /* Record a function that resets redisplaying_p to its old value
11316 when we leave this function. */
11317 count = SPECPDL_INDEX ();
11318 record_unwind_protect (unwind_redisplay,
11319 Fcons (make_number (redisplaying_p), selected_frame));
11320 ++redisplaying_p;
11321 specbind (Qinhibit_free_realized_faces, Qnil);
11322
11323 {
11324 Lisp_Object tail, frame;
11325
11326 FOR_EACH_FRAME (tail, frame)
11327 {
11328 struct frame *f = XFRAME (frame);
11329 f->already_hscrolled_p = 0;
11330 }
11331 }
11332
11333 retry:
11334 if (!EQ (old_frame, selected_frame)
11335 && FRAME_LIVE_P (XFRAME (old_frame)))
11336 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11337 selected_frame and selected_window to be temporarily out-of-sync so
11338 when we come back here via `goto retry', we need to resync because we
11339 may need to run Elisp code (via prepare_menu_bars). */
11340 select_frame_for_redisplay (old_frame);
11341
11342 pause = 0;
11343 reconsider_clip_changes (w, current_buffer);
11344 last_escape_glyph_frame = NULL;
11345 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11346
11347 /* If new fonts have been loaded that make a glyph matrix adjustment
11348 necessary, do it. */
11349 if (fonts_changed_p)
11350 {
11351 adjust_glyphs (NULL);
11352 ++windows_or_buffers_changed;
11353 fonts_changed_p = 0;
11354 }
11355
11356 /* If face_change_count is non-zero, init_iterator will free all
11357 realized faces, which includes the faces referenced from current
11358 matrices. So, we can't reuse current matrices in this case. */
11359 if (face_change_count)
11360 ++windows_or_buffers_changed;
11361
11362 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
11363 && FRAME_TTY (sf)->previous_frame != sf)
11364 {
11365 /* Since frames on a single ASCII terminal share the same
11366 display area, displaying a different frame means redisplay
11367 the whole thing. */
11368 windows_or_buffers_changed++;
11369 SET_FRAME_GARBAGED (sf);
11370 #ifndef DOS_NT
11371 set_tty_color_mode (FRAME_TTY (sf), sf);
11372 #endif
11373 FRAME_TTY (sf)->previous_frame = sf;
11374 }
11375
11376 /* Set the visible flags for all frames. Do this before checking
11377 for resized or garbaged frames; they want to know if their frames
11378 are visible. See the comment in frame.h for
11379 FRAME_SAMPLE_VISIBILITY. */
11380 {
11381 Lisp_Object tail, frame;
11382
11383 number_of_visible_frames = 0;
11384
11385 FOR_EACH_FRAME (tail, frame)
11386 {
11387 struct frame *f = XFRAME (frame);
11388
11389 FRAME_SAMPLE_VISIBILITY (f);
11390 if (FRAME_VISIBLE_P (f))
11391 ++number_of_visible_frames;
11392 clear_desired_matrices (f);
11393 }
11394 }
11395
11396 /* Notice any pending interrupt request to change frame size. */
11397 do_pending_window_change (1);
11398
11399 /* Clear frames marked as garbaged. */
11400 if (frame_garbaged)
11401 clear_garbaged_frames ();
11402
11403 /* Build menubar and tool-bar items. */
11404 if (NILP (Vmemory_full))
11405 prepare_menu_bars ();
11406
11407 if (windows_or_buffers_changed)
11408 update_mode_lines++;
11409
11410 /* Detect case that we need to write or remove a star in the mode line. */
11411 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11412 {
11413 w->update_mode_line = Qt;
11414 if (buffer_shared > 1)
11415 update_mode_lines++;
11416 }
11417
11418 /* Avoid invocation of point motion hooks by `current_column' below. */
11419 count1 = SPECPDL_INDEX ();
11420 specbind (Qinhibit_point_motion_hooks, Qt);
11421
11422 /* If %c is in the mode line, update it if needed. */
11423 if (!NILP (w->column_number_displayed)
11424 /* This alternative quickly identifies a common case
11425 where no change is needed. */
11426 && !(PT == XFASTINT (w->last_point)
11427 && XFASTINT (w->last_modified) >= MODIFF
11428 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11429 && (XFASTINT (w->column_number_displayed)
11430 != (int) current_column ())) /* iftc */
11431 w->update_mode_line = Qt;
11432
11433 unbind_to (count1, Qnil);
11434
11435 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11436
11437 /* The variable buffer_shared is set in redisplay_window and
11438 indicates that we redisplay a buffer in different windows. See
11439 there. */
11440 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11441 || cursor_type_changed);
11442
11443 /* If specs for an arrow have changed, do thorough redisplay
11444 to ensure we remove any arrow that should no longer exist. */
11445 if (overlay_arrows_changed_p ())
11446 consider_all_windows_p = windows_or_buffers_changed = 1;
11447
11448 /* Normally the message* functions will have already displayed and
11449 updated the echo area, but the frame may have been trashed, or
11450 the update may have been preempted, so display the echo area
11451 again here. Checking message_cleared_p captures the case that
11452 the echo area should be cleared. */
11453 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11454 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11455 || (message_cleared_p
11456 && minibuf_level == 0
11457 /* If the mini-window is currently selected, this means the
11458 echo-area doesn't show through. */
11459 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11460 {
11461 int window_height_changed_p = echo_area_display (0);
11462 must_finish = 1;
11463
11464 /* If we don't display the current message, don't clear the
11465 message_cleared_p flag, because, if we did, we wouldn't clear
11466 the echo area in the next redisplay which doesn't preserve
11467 the echo area. */
11468 if (!display_last_displayed_message_p)
11469 message_cleared_p = 0;
11470
11471 if (fonts_changed_p)
11472 goto retry;
11473 else if (window_height_changed_p)
11474 {
11475 consider_all_windows_p = 1;
11476 ++update_mode_lines;
11477 ++windows_or_buffers_changed;
11478
11479 /* If window configuration was changed, frames may have been
11480 marked garbaged. Clear them or we will experience
11481 surprises wrt scrolling. */
11482 if (frame_garbaged)
11483 clear_garbaged_frames ();
11484 }
11485 }
11486 else if (EQ (selected_window, minibuf_window)
11487 && (current_buffer->clip_changed
11488 || XFASTINT (w->last_modified) < MODIFF
11489 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11490 && resize_mini_window (w, 0))
11491 {
11492 /* Resized active mini-window to fit the size of what it is
11493 showing if its contents might have changed. */
11494 must_finish = 1;
11495 /* FIXME: this causes all frames to be updated, which seems unnecessary
11496 since only the current frame needs to be considered. This function needs
11497 to be rewritten with two variables, consider_all_windows and
11498 consider_all_frames. */
11499 consider_all_windows_p = 1;
11500 ++windows_or_buffers_changed;
11501 ++update_mode_lines;
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
11511 /* If showing the region, and mark has changed, we must redisplay
11512 the whole window. The assignment to this_line_start_pos prevents
11513 the optimization directly below this if-statement. */
11514 if (((!NILP (Vtransient_mark_mode)
11515 && !NILP (XBUFFER (w->buffer)->mark_active))
11516 != !NILP (w->region_showing))
11517 || (!NILP (w->region_showing)
11518 && !EQ (w->region_showing,
11519 Fmarker_position (XBUFFER (w->buffer)->mark))))
11520 CHARPOS (this_line_start_pos) = 0;
11521
11522 /* Optimize the case that only the line containing the cursor in the
11523 selected window has changed. Variables starting with this_ are
11524 set in display_line and record information about the line
11525 containing the cursor. */
11526 tlbufpos = this_line_start_pos;
11527 tlendpos = this_line_end_pos;
11528 if (!consider_all_windows_p
11529 && CHARPOS (tlbufpos) > 0
11530 && NILP (w->update_mode_line)
11531 && !current_buffer->clip_changed
11532 && !current_buffer->prevent_redisplay_optimizations_p
11533 && FRAME_VISIBLE_P (XFRAME (w->frame))
11534 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11535 /* Make sure recorded data applies to current buffer, etc. */
11536 && this_line_buffer == current_buffer
11537 && current_buffer == XBUFFER (w->buffer)
11538 && NILP (w->force_start)
11539 && NILP (w->optional_new_start)
11540 /* Point must be on the line that we have info recorded about. */
11541 && PT >= CHARPOS (tlbufpos)
11542 && PT <= Z - CHARPOS (tlendpos)
11543 /* All text outside that line, including its final newline,
11544 must be unchanged. */
11545 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11546 CHARPOS (tlendpos)))
11547 {
11548 if (CHARPOS (tlbufpos) > BEGV
11549 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11550 && (CHARPOS (tlbufpos) == ZV
11551 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11552 /* Former continuation line has disappeared by becoming empty. */
11553 goto cancel;
11554 else if (XFASTINT (w->last_modified) < MODIFF
11555 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11556 || MINI_WINDOW_P (w))
11557 {
11558 /* We have to handle the case of continuation around a
11559 wide-column character (see the comment in indent.c around
11560 line 1340).
11561
11562 For instance, in the following case:
11563
11564 -------- Insert --------
11565 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11566 J_I_ ==> J_I_ `^^' are cursors.
11567 ^^ ^^
11568 -------- --------
11569
11570 As we have to redraw the line above, we cannot use this
11571 optimization. */
11572
11573 struct it it;
11574 int line_height_before = this_line_pixel_height;
11575
11576 /* Note that start_display will handle the case that the
11577 line starting at tlbufpos is a continuation line. */
11578 start_display (&it, w, tlbufpos);
11579
11580 /* Implementation note: It this still necessary? */
11581 if (it.current_x != this_line_start_x)
11582 goto cancel;
11583
11584 TRACE ((stderr, "trying display optimization 1\n"));
11585 w->cursor.vpos = -1;
11586 overlay_arrow_seen = 0;
11587 it.vpos = this_line_vpos;
11588 it.current_y = this_line_y;
11589 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11590 display_line (&it);
11591
11592 /* If line contains point, is not continued,
11593 and ends at same distance from eob as before, we win. */
11594 if (w->cursor.vpos >= 0
11595 /* Line is not continued, otherwise this_line_start_pos
11596 would have been set to 0 in display_line. */
11597 && CHARPOS (this_line_start_pos)
11598 /* Line ends as before. */
11599 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11600 /* Line has same height as before. Otherwise other lines
11601 would have to be shifted up or down. */
11602 && this_line_pixel_height == line_height_before)
11603 {
11604 /* If this is not the window's last line, we must adjust
11605 the charstarts of the lines below. */
11606 if (it.current_y < it.last_visible_y)
11607 {
11608 struct glyph_row *row
11609 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11610 int delta, delta_bytes;
11611
11612 /* We used to distinguish between two cases here,
11613 conditioned by Z - CHARPOS (tlendpos) == ZV, for
11614 when the line ends in a newline or the end of the
11615 buffer's accessible portion. But both cases did
11616 the same, so they were collapsed. */
11617 delta = (Z
11618 - CHARPOS (tlendpos)
11619 - MATRIX_ROW_START_CHARPOS (row));
11620 delta_bytes = (Z_BYTE
11621 - BYTEPOS (tlendpos)
11622 - MATRIX_ROW_START_BYTEPOS (row));
11623
11624 increment_matrix_positions (w->current_matrix,
11625 this_line_vpos + 1,
11626 w->current_matrix->nrows,
11627 delta, delta_bytes);
11628 }
11629
11630 /* If this row displays text now but previously didn't,
11631 or vice versa, w->window_end_vpos may have to be
11632 adjusted. */
11633 if ((it.glyph_row - 1)->displays_text_p)
11634 {
11635 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11636 XSETINT (w->window_end_vpos, this_line_vpos);
11637 }
11638 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11639 && this_line_vpos > 0)
11640 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11641 w->window_end_valid = Qnil;
11642
11643 /* Update hint: No need to try to scroll in update_window. */
11644 w->desired_matrix->no_scrolling_p = 1;
11645
11646 #if GLYPH_DEBUG
11647 *w->desired_matrix->method = 0;
11648 debug_method_add (w, "optimization 1");
11649 #endif
11650 #ifdef HAVE_WINDOW_SYSTEM
11651 update_window_fringes (w, 0);
11652 #endif
11653 goto update;
11654 }
11655 else
11656 goto cancel;
11657 }
11658 else if (/* Cursor position hasn't changed. */
11659 PT == XFASTINT (w->last_point)
11660 /* Make sure the cursor was last displayed
11661 in this window. Otherwise we have to reposition it. */
11662 && 0 <= w->cursor.vpos
11663 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11664 {
11665 if (!must_finish)
11666 {
11667 do_pending_window_change (1);
11668
11669 /* We used to always goto end_of_redisplay here, but this
11670 isn't enough if we have a blinking cursor. */
11671 if (w->cursor_off_p == w->last_cursor_off_p)
11672 goto end_of_redisplay;
11673 }
11674 goto update;
11675 }
11676 /* If highlighting the region, or if the cursor is in the echo area,
11677 then we can't just move the cursor. */
11678 else if (! (!NILP (Vtransient_mark_mode)
11679 && !NILP (current_buffer->mark_active))
11680 && (EQ (selected_window, current_buffer->last_selected_window)
11681 || highlight_nonselected_windows)
11682 && NILP (w->region_showing)
11683 && NILP (Vshow_trailing_whitespace)
11684 && !cursor_in_echo_area)
11685 {
11686 struct it it;
11687 struct glyph_row *row;
11688
11689 /* Skip from tlbufpos to PT and see where it is. Note that
11690 PT may be in invisible text. If so, we will end at the
11691 next visible position. */
11692 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11693 NULL, DEFAULT_FACE_ID);
11694 it.current_x = this_line_start_x;
11695 it.current_y = this_line_y;
11696 it.vpos = this_line_vpos;
11697
11698 /* The call to move_it_to stops in front of PT, but
11699 moves over before-strings. */
11700 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11701
11702 if (it.vpos == this_line_vpos
11703 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11704 row->enabled_p))
11705 {
11706 xassert (this_line_vpos == it.vpos);
11707 xassert (this_line_y == it.current_y);
11708 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11709 #if GLYPH_DEBUG
11710 *w->desired_matrix->method = 0;
11711 debug_method_add (w, "optimization 3");
11712 #endif
11713 goto update;
11714 }
11715 else
11716 goto cancel;
11717 }
11718
11719 cancel:
11720 /* Text changed drastically or point moved off of line. */
11721 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11722 }
11723
11724 CHARPOS (this_line_start_pos) = 0;
11725 consider_all_windows_p |= buffer_shared > 1;
11726 ++clear_face_cache_count;
11727 #ifdef HAVE_WINDOW_SYSTEM
11728 ++clear_image_cache_count;
11729 #endif
11730
11731 /* Build desired matrices, and update the display. If
11732 consider_all_windows_p is non-zero, do it for all windows on all
11733 frames. Otherwise do it for selected_window, only. */
11734
11735 if (consider_all_windows_p)
11736 {
11737 Lisp_Object tail, frame;
11738
11739 FOR_EACH_FRAME (tail, frame)
11740 XFRAME (frame)->updated_p = 0;
11741
11742 /* Recompute # windows showing selected buffer. This will be
11743 incremented each time such a window is displayed. */
11744 buffer_shared = 0;
11745
11746 FOR_EACH_FRAME (tail, frame)
11747 {
11748 struct frame *f = XFRAME (frame);
11749
11750 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11751 {
11752 if (! EQ (frame, selected_frame))
11753 /* Select the frame, for the sake of frame-local
11754 variables. */
11755 select_frame_for_redisplay (frame);
11756
11757 /* Mark all the scroll bars to be removed; we'll redeem
11758 the ones we want when we redisplay their windows. */
11759 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11760 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11761
11762 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11763 redisplay_windows (FRAME_ROOT_WINDOW (f));
11764
11765 /* The X error handler may have deleted that frame. */
11766 if (!FRAME_LIVE_P (f))
11767 continue;
11768
11769 /* Any scroll bars which redisplay_windows should have
11770 nuked should now go away. */
11771 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11772 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11773
11774 /* If fonts changed, display again. */
11775 /* ??? rms: I suspect it is a mistake to jump all the way
11776 back to retry here. It should just retry this frame. */
11777 if (fonts_changed_p)
11778 goto retry;
11779
11780 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11781 {
11782 /* See if we have to hscroll. */
11783 if (!f->already_hscrolled_p)
11784 {
11785 f->already_hscrolled_p = 1;
11786 if (hscroll_windows (f->root_window))
11787 goto retry;
11788 }
11789
11790 /* Prevent various kinds of signals during display
11791 update. stdio is not robust about handling
11792 signals, which can cause an apparent I/O
11793 error. */
11794 if (interrupt_input)
11795 unrequest_sigio ();
11796 STOP_POLLING;
11797
11798 /* Update the display. */
11799 set_window_update_flags (XWINDOW (f->root_window), 1);
11800 pause |= update_frame (f, 0, 0);
11801 f->updated_p = 1;
11802 }
11803 }
11804 }
11805
11806 if (!EQ (old_frame, selected_frame)
11807 && FRAME_LIVE_P (XFRAME (old_frame)))
11808 /* We played a bit fast-and-loose above and allowed selected_frame
11809 and selected_window to be temporarily out-of-sync but let's make
11810 sure this stays contained. */
11811 select_frame_for_redisplay (old_frame);
11812 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11813
11814 if (!pause)
11815 {
11816 /* Do the mark_window_display_accurate after all windows have
11817 been redisplayed because this call resets flags in buffers
11818 which are needed for proper redisplay. */
11819 FOR_EACH_FRAME (tail, frame)
11820 {
11821 struct frame *f = XFRAME (frame);
11822 if (f->updated_p)
11823 {
11824 mark_window_display_accurate (f->root_window, 1);
11825 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11826 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11827 }
11828 }
11829 }
11830 }
11831 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11832 {
11833 Lisp_Object mini_window;
11834 struct frame *mini_frame;
11835
11836 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11837 /* Use list_of_error, not Qerror, so that
11838 we catch only errors and don't run the debugger. */
11839 internal_condition_case_1 (redisplay_window_1, selected_window,
11840 list_of_error,
11841 redisplay_window_error);
11842
11843 /* Compare desired and current matrices, perform output. */
11844
11845 update:
11846 /* If fonts changed, display again. */
11847 if (fonts_changed_p)
11848 goto retry;
11849
11850 /* Prevent various kinds of signals during display update.
11851 stdio is not robust about handling signals,
11852 which can cause an apparent I/O error. */
11853 if (interrupt_input)
11854 unrequest_sigio ();
11855 STOP_POLLING;
11856
11857 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11858 {
11859 if (hscroll_windows (selected_window))
11860 goto retry;
11861
11862 XWINDOW (selected_window)->must_be_updated_p = 1;
11863 pause = update_frame (sf, 0, 0);
11864 }
11865
11866 /* We may have called echo_area_display at the top of this
11867 function. If the echo area is on another frame, that may
11868 have put text on a frame other than the selected one, so the
11869 above call to update_frame would not have caught it. Catch
11870 it here. */
11871 mini_window = FRAME_MINIBUF_WINDOW (sf);
11872 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11873
11874 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11875 {
11876 XWINDOW (mini_window)->must_be_updated_p = 1;
11877 pause |= update_frame (mini_frame, 0, 0);
11878 if (!pause && hscroll_windows (mini_window))
11879 goto retry;
11880 }
11881 }
11882
11883 /* If display was paused because of pending input, make sure we do a
11884 thorough update the next time. */
11885 if (pause)
11886 {
11887 /* Prevent the optimization at the beginning of
11888 redisplay_internal that tries a single-line update of the
11889 line containing the cursor in the selected window. */
11890 CHARPOS (this_line_start_pos) = 0;
11891
11892 /* Let the overlay arrow be updated the next time. */
11893 update_overlay_arrows (0);
11894
11895 /* If we pause after scrolling, some rows in the current
11896 matrices of some windows are not valid. */
11897 if (!WINDOW_FULL_WIDTH_P (w)
11898 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11899 update_mode_lines = 1;
11900 }
11901 else
11902 {
11903 if (!consider_all_windows_p)
11904 {
11905 /* This has already been done above if
11906 consider_all_windows_p is set. */
11907 mark_window_display_accurate_1 (w, 1);
11908
11909 /* Say overlay arrows are up to date. */
11910 update_overlay_arrows (1);
11911
11912 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11913 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11914 }
11915
11916 update_mode_lines = 0;
11917 windows_or_buffers_changed = 0;
11918 cursor_type_changed = 0;
11919 }
11920
11921 /* Start SIGIO interrupts coming again. Having them off during the
11922 code above makes it less likely one will discard output, but not
11923 impossible, since there might be stuff in the system buffer here.
11924 But it is much hairier to try to do anything about that. */
11925 if (interrupt_input)
11926 request_sigio ();
11927 RESUME_POLLING;
11928
11929 /* If a frame has become visible which was not before, redisplay
11930 again, so that we display it. Expose events for such a frame
11931 (which it gets when becoming visible) don't call the parts of
11932 redisplay constructing glyphs, so simply exposing a frame won't
11933 display anything in this case. So, we have to display these
11934 frames here explicitly. */
11935 if (!pause)
11936 {
11937 Lisp_Object tail, frame;
11938 int new_count = 0;
11939
11940 FOR_EACH_FRAME (tail, frame)
11941 {
11942 int this_is_visible = 0;
11943
11944 if (XFRAME (frame)->visible)
11945 this_is_visible = 1;
11946 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11947 if (XFRAME (frame)->visible)
11948 this_is_visible = 1;
11949
11950 if (this_is_visible)
11951 new_count++;
11952 }
11953
11954 if (new_count != number_of_visible_frames)
11955 windows_or_buffers_changed++;
11956 }
11957
11958 /* Change frame size now if a change is pending. */
11959 do_pending_window_change (1);
11960
11961 /* If we just did a pending size change, or have additional
11962 visible frames, redisplay again. */
11963 if (windows_or_buffers_changed && !pause)
11964 goto retry;
11965
11966 /* Clear the face cache eventually. */
11967 if (consider_all_windows_p)
11968 {
11969 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11970 {
11971 clear_face_cache (0);
11972 clear_face_cache_count = 0;
11973 }
11974 #ifdef HAVE_WINDOW_SYSTEM
11975 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11976 {
11977 clear_image_caches (Qnil);
11978 clear_image_cache_count = 0;
11979 }
11980 #endif /* HAVE_WINDOW_SYSTEM */
11981 }
11982
11983 end_of_redisplay:
11984 unbind_to (count, Qnil);
11985 RESUME_POLLING;
11986 }
11987
11988
11989 /* Redisplay, but leave alone any recent echo area message unless
11990 another message has been requested in its place.
11991
11992 This is useful in situations where you need to redisplay but no
11993 user action has occurred, making it inappropriate for the message
11994 area to be cleared. See tracking_off and
11995 wait_reading_process_output for examples of these situations.
11996
11997 FROM_WHERE is an integer saying from where this function was
11998 called. This is useful for debugging. */
11999
12000 void
12001 redisplay_preserve_echo_area (from_where)
12002 int from_where;
12003 {
12004 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12005
12006 if (!NILP (echo_area_buffer[1]))
12007 {
12008 /* We have a previously displayed message, but no current
12009 message. Redisplay the previous message. */
12010 display_last_displayed_message_p = 1;
12011 redisplay_internal (1);
12012 display_last_displayed_message_p = 0;
12013 }
12014 else
12015 redisplay_internal (1);
12016
12017 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12018 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12019 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12020 }
12021
12022
12023 /* Function registered with record_unwind_protect in
12024 redisplay_internal. Reset redisplaying_p to the value it had
12025 before redisplay_internal was called, and clear
12026 prevent_freeing_realized_faces_p. It also selects the previously
12027 selected frame, unless it has been deleted (by an X connection
12028 failure during redisplay, for example). */
12029
12030 static Lisp_Object
12031 unwind_redisplay (val)
12032 Lisp_Object val;
12033 {
12034 Lisp_Object old_redisplaying_p, old_frame;
12035
12036 old_redisplaying_p = XCAR (val);
12037 redisplaying_p = XFASTINT (old_redisplaying_p);
12038 old_frame = XCDR (val);
12039 if (! EQ (old_frame, selected_frame)
12040 && FRAME_LIVE_P (XFRAME (old_frame)))
12041 select_frame_for_redisplay (old_frame);
12042 return Qnil;
12043 }
12044
12045
12046 /* Mark the display of window W as accurate or inaccurate. If
12047 ACCURATE_P is non-zero mark display of W as accurate. If
12048 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12049 redisplay_internal is called. */
12050
12051 static void
12052 mark_window_display_accurate_1 (w, accurate_p)
12053 struct window *w;
12054 int accurate_p;
12055 {
12056 if (BUFFERP (w->buffer))
12057 {
12058 struct buffer *b = XBUFFER (w->buffer);
12059
12060 w->last_modified
12061 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12062 w->last_overlay_modified
12063 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12064 w->last_had_star
12065 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12066
12067 if (accurate_p)
12068 {
12069 b->clip_changed = 0;
12070 b->prevent_redisplay_optimizations_p = 0;
12071
12072 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12073 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12074 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12075 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12076
12077 w->current_matrix->buffer = b;
12078 w->current_matrix->begv = BUF_BEGV (b);
12079 w->current_matrix->zv = BUF_ZV (b);
12080
12081 w->last_cursor = w->cursor;
12082 w->last_cursor_off_p = w->cursor_off_p;
12083
12084 if (w == XWINDOW (selected_window))
12085 w->last_point = make_number (BUF_PT (b));
12086 else
12087 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12088 }
12089 }
12090
12091 if (accurate_p)
12092 {
12093 w->window_end_valid = w->buffer;
12094 w->update_mode_line = Qnil;
12095 }
12096 }
12097
12098
12099 /* Mark the display of windows in the window tree rooted at WINDOW as
12100 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12101 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12102 be redisplayed the next time redisplay_internal is called. */
12103
12104 void
12105 mark_window_display_accurate (window, accurate_p)
12106 Lisp_Object window;
12107 int accurate_p;
12108 {
12109 struct window *w;
12110
12111 for (; !NILP (window); window = w->next)
12112 {
12113 w = XWINDOW (window);
12114 mark_window_display_accurate_1 (w, accurate_p);
12115
12116 if (!NILP (w->vchild))
12117 mark_window_display_accurate (w->vchild, accurate_p);
12118 if (!NILP (w->hchild))
12119 mark_window_display_accurate (w->hchild, accurate_p);
12120 }
12121
12122 if (accurate_p)
12123 {
12124 update_overlay_arrows (1);
12125 }
12126 else
12127 {
12128 /* Force a thorough redisplay the next time by setting
12129 last_arrow_position and last_arrow_string to t, which is
12130 unequal to any useful value of Voverlay_arrow_... */
12131 update_overlay_arrows (-1);
12132 }
12133 }
12134
12135
12136 /* Return value in display table DP (Lisp_Char_Table *) for character
12137 C. Since a display table doesn't have any parent, we don't have to
12138 follow parent. Do not call this function directly but use the
12139 macro DISP_CHAR_VECTOR. */
12140
12141 Lisp_Object
12142 disp_char_vector (dp, c)
12143 struct Lisp_Char_Table *dp;
12144 int c;
12145 {
12146 Lisp_Object val;
12147
12148 if (ASCII_CHAR_P (c))
12149 {
12150 val = dp->ascii;
12151 if (SUB_CHAR_TABLE_P (val))
12152 val = XSUB_CHAR_TABLE (val)->contents[c];
12153 }
12154 else
12155 {
12156 Lisp_Object table;
12157
12158 XSETCHAR_TABLE (table, dp);
12159 val = char_table_ref (table, c);
12160 }
12161 if (NILP (val))
12162 val = dp->defalt;
12163 return val;
12164 }
12165
12166
12167 \f
12168 /***********************************************************************
12169 Window Redisplay
12170 ***********************************************************************/
12171
12172 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12173
12174 static void
12175 redisplay_windows (window)
12176 Lisp_Object window;
12177 {
12178 while (!NILP (window))
12179 {
12180 struct window *w = XWINDOW (window);
12181
12182 if (!NILP (w->hchild))
12183 redisplay_windows (w->hchild);
12184 else if (!NILP (w->vchild))
12185 redisplay_windows (w->vchild);
12186 else if (!NILP (w->buffer))
12187 {
12188 displayed_buffer = XBUFFER (w->buffer);
12189 /* Use list_of_error, not Qerror, so that
12190 we catch only errors and don't run the debugger. */
12191 internal_condition_case_1 (redisplay_window_0, window,
12192 list_of_error,
12193 redisplay_window_error);
12194 }
12195
12196 window = w->next;
12197 }
12198 }
12199
12200 static Lisp_Object
12201 redisplay_window_error ()
12202 {
12203 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12204 return Qnil;
12205 }
12206
12207 static Lisp_Object
12208 redisplay_window_0 (window)
12209 Lisp_Object window;
12210 {
12211 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12212 redisplay_window (window, 0);
12213 return Qnil;
12214 }
12215
12216 static Lisp_Object
12217 redisplay_window_1 (window)
12218 Lisp_Object window;
12219 {
12220 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12221 redisplay_window (window, 1);
12222 return Qnil;
12223 }
12224 \f
12225
12226 /* Increment GLYPH until it reaches END or CONDITION fails while
12227 adding (GLYPH)->pixel_width to X. */
12228
12229 #define SKIP_GLYPHS(glyph, end, x, condition) \
12230 do \
12231 { \
12232 (x) += (glyph)->pixel_width; \
12233 ++(glyph); \
12234 } \
12235 while ((glyph) < (end) && (condition))
12236
12237
12238 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12239 DELTA and DELTA_BYTES are the numbers of characters and bytes by
12240 which positions recorded in ROW differ from current buffer
12241 positions.
12242
12243 Return 0 if cursor is not on this row, 1 otherwise. */
12244
12245 int
12246 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12247 struct window *w;
12248 struct glyph_row *row;
12249 struct glyph_matrix *matrix;
12250 int delta, delta_bytes, dy, dvpos;
12251 {
12252 struct glyph *glyph = row->glyphs[TEXT_AREA];
12253 struct glyph *end = glyph + row->used[TEXT_AREA];
12254 struct glyph *cursor = NULL;
12255 /* The first glyph that starts a sequence of glyphs from a string
12256 that is a value of a display property. */
12257 struct glyph *string_start;
12258 /* The X coordinate of string_start. */
12259 int string_start_x;
12260 /* The last known character position in row. */
12261 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12262 /* The last known character position before string_start. */
12263 int string_before_pos;
12264 int x = row->x;
12265 int cursor_x = x;
12266 /* Last buffer position covered by an overlay. */
12267 int cursor_from_overlay_pos = 0;
12268 int pt_old = PT - delta;
12269
12270 /* Skip over glyphs not having an object at the start of the row.
12271 These are special glyphs like truncation marks on terminal
12272 frames. */
12273 if (row->displays_text_p)
12274 while (glyph < end
12275 && INTEGERP (glyph->object)
12276 && glyph->charpos < 0)
12277 {
12278 x += glyph->pixel_width;
12279 ++glyph;
12280 }
12281
12282 string_start = NULL;
12283 while (glyph < end
12284 && !INTEGERP (glyph->object)
12285 && (!BUFFERP (glyph->object)
12286 || (last_pos = glyph->charpos) < pt_old
12287 || glyph->avoid_cursor_p))
12288 {
12289 if (! STRINGP (glyph->object))
12290 {
12291 string_start = NULL;
12292 x += glyph->pixel_width;
12293 ++glyph;
12294 /* If we are beyond the cursor position computed from the
12295 last overlay seen, that overlay is not in effect for
12296 current cursor position. Reset the cursor information
12297 computed from that overlay. */
12298 if (cursor_from_overlay_pos
12299 && last_pos >= cursor_from_overlay_pos)
12300 {
12301 cursor_from_overlay_pos = 0;
12302 cursor = NULL;
12303 }
12304 }
12305 else
12306 {
12307 if (string_start == NULL)
12308 {
12309 string_before_pos = last_pos;
12310 string_start = glyph;
12311 string_start_x = x;
12312 }
12313 /* Skip all glyphs from a string. */
12314 do
12315 {
12316 Lisp_Object cprop;
12317 int pos;
12318 if ((cursor == NULL || glyph > cursor)
12319 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12320 Qcursor, (glyph)->object),
12321 !NILP (cprop))
12322 && (pos = string_buffer_position (w, glyph->object,
12323 string_before_pos),
12324 (pos == 0 /* from overlay */
12325 || pos == pt_old)))
12326 {
12327 /* Compute the first buffer position after the overlay.
12328 If the `cursor' property tells us how many positions
12329 are associated with the overlay, use that. Otherwise,
12330 estimate from the buffer positions of the glyphs
12331 before and after the overlay. */
12332 cursor_from_overlay_pos = (pos ? 0 : last_pos
12333 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12334 cursor = glyph;
12335 cursor_x = x;
12336 }
12337 x += glyph->pixel_width;
12338 ++glyph;
12339 }
12340 while (glyph < end && EQ (glyph->object, string_start->object));
12341 }
12342 }
12343
12344 if (cursor != NULL)
12345 {
12346 glyph = cursor;
12347 x = cursor_x;
12348 }
12349 else if (row->ends_in_ellipsis_p && glyph == end)
12350 {
12351 /* Scan back over the ellipsis glyphs, decrementing positions. */
12352 while (glyph > row->glyphs[TEXT_AREA]
12353 && (glyph - 1)->charpos == last_pos)
12354 glyph--, x -= glyph->pixel_width;
12355 /* That loop always goes one position too far, including the
12356 glyph before the ellipsis. So scan forward over that one. */
12357 x += glyph->pixel_width;
12358 glyph++;
12359 }
12360 else if (string_start
12361 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12362 {
12363 /* We may have skipped over point because the previous glyphs
12364 are from string. As there's no easy way to know the
12365 character position of the current glyph, find the correct
12366 glyph on point by scanning from string_start again. */
12367 Lisp_Object limit;
12368 Lisp_Object string;
12369 struct glyph *stop = glyph;
12370 int pos;
12371
12372 limit = make_number (pt_old + 1);
12373 glyph = string_start;
12374 x = string_start_x;
12375 string = glyph->object;
12376 pos = string_buffer_position (w, string, string_before_pos);
12377 /* If POS == 0, STRING is from overlay. We skip such glyphs
12378 because we always put the cursor after overlay strings. */
12379 while (pos == 0 && glyph < stop)
12380 {
12381 string = glyph->object;
12382 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12383 if (glyph < stop)
12384 pos = string_buffer_position (w, glyph->object, string_before_pos);
12385 }
12386
12387 while (glyph < stop)
12388 {
12389 pos = XINT (Fnext_single_char_property_change
12390 (make_number (pos), Qdisplay, Qnil, limit));
12391 if (pos > pt_old)
12392 break;
12393 /* Skip glyphs from the same string. */
12394 string = glyph->object;
12395 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12396 /* Skip glyphs from an overlay. */
12397 while (glyph < stop
12398 && ! string_buffer_position (w, glyph->object, pos))
12399 {
12400 string = glyph->object;
12401 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12402 }
12403 }
12404
12405 /* If we reached the end of the line, and END was from a string,
12406 the cursor is not on this line. */
12407 if (glyph == end && row->continued_p)
12408 return 0;
12409 }
12410
12411 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12412 w->cursor.x = x;
12413 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12414 w->cursor.y = row->y + dy;
12415
12416 if (w == XWINDOW (selected_window))
12417 {
12418 if (!row->continued_p
12419 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12420 && row->x == 0)
12421 {
12422 this_line_buffer = XBUFFER (w->buffer);
12423
12424 CHARPOS (this_line_start_pos)
12425 = MATRIX_ROW_START_CHARPOS (row) + delta;
12426 BYTEPOS (this_line_start_pos)
12427 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12428
12429 CHARPOS (this_line_end_pos)
12430 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12431 BYTEPOS (this_line_end_pos)
12432 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12433
12434 this_line_y = w->cursor.y;
12435 this_line_pixel_height = row->height;
12436 this_line_vpos = w->cursor.vpos;
12437 this_line_start_x = row->x;
12438 }
12439 else
12440 CHARPOS (this_line_start_pos) = 0;
12441 }
12442
12443 return 1;
12444 }
12445
12446
12447 /* Run window scroll functions, if any, for WINDOW with new window
12448 start STARTP. Sets the window start of WINDOW to that position.
12449
12450 We assume that the window's buffer is really current. */
12451
12452 static INLINE struct text_pos
12453 run_window_scroll_functions (window, startp)
12454 Lisp_Object window;
12455 struct text_pos startp;
12456 {
12457 struct window *w = XWINDOW (window);
12458 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12459
12460 if (current_buffer != XBUFFER (w->buffer))
12461 abort ();
12462
12463 if (!NILP (Vwindow_scroll_functions))
12464 {
12465 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12466 make_number (CHARPOS (startp)));
12467 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12468 /* In case the hook functions switch buffers. */
12469 if (current_buffer != XBUFFER (w->buffer))
12470 set_buffer_internal_1 (XBUFFER (w->buffer));
12471 }
12472
12473 return startp;
12474 }
12475
12476
12477 /* Make sure the line containing the cursor is fully visible.
12478 A value of 1 means there is nothing to be done.
12479 (Either the line is fully visible, or it cannot be made so,
12480 or we cannot tell.)
12481
12482 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12483 is higher than window.
12484
12485 A value of 0 means the caller should do scrolling
12486 as if point had gone off the screen. */
12487
12488 static int
12489 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12490 struct window *w;
12491 int force_p;
12492 int current_matrix_p;
12493 {
12494 struct glyph_matrix *matrix;
12495 struct glyph_row *row;
12496 int window_height;
12497
12498 if (!make_cursor_line_fully_visible_p)
12499 return 1;
12500
12501 /* It's not always possible to find the cursor, e.g, when a window
12502 is full of overlay strings. Don't do anything in that case. */
12503 if (w->cursor.vpos < 0)
12504 return 1;
12505
12506 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12507 row = MATRIX_ROW (matrix, w->cursor.vpos);
12508
12509 /* If the cursor row is not partially visible, there's nothing to do. */
12510 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12511 return 1;
12512
12513 /* If the row the cursor is in is taller than the window's height,
12514 it's not clear what to do, so do nothing. */
12515 window_height = window_box_height (w);
12516 if (row->height >= window_height)
12517 {
12518 if (!force_p || MINI_WINDOW_P (w)
12519 || w->vscroll || w->cursor.vpos == 0)
12520 return 1;
12521 }
12522 return 0;
12523 }
12524
12525
12526 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12527 non-zero means only WINDOW is redisplayed in redisplay_internal.
12528 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12529 in redisplay_window to bring a partially visible line into view in
12530 the case that only the cursor has moved.
12531
12532 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12533 last screen line's vertical height extends past the end of the screen.
12534
12535 Value is
12536
12537 1 if scrolling succeeded
12538
12539 0 if scrolling didn't find point.
12540
12541 -1 if new fonts have been loaded so that we must interrupt
12542 redisplay, adjust glyph matrices, and try again. */
12543
12544 enum
12545 {
12546 SCROLLING_SUCCESS,
12547 SCROLLING_FAILED,
12548 SCROLLING_NEED_LARGER_MATRICES
12549 };
12550
12551 static int
12552 try_scrolling (window, just_this_one_p, scroll_conservatively,
12553 scroll_step, temp_scroll_step, last_line_misfit)
12554 Lisp_Object window;
12555 int just_this_one_p;
12556 EMACS_INT scroll_conservatively, scroll_step;
12557 int temp_scroll_step;
12558 int last_line_misfit;
12559 {
12560 struct window *w = XWINDOW (window);
12561 struct frame *f = XFRAME (w->frame);
12562 struct text_pos pos, startp;
12563 struct it it;
12564 int this_scroll_margin, scroll_max, rc, height;
12565 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
12566 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12567 Lisp_Object aggressive;
12568 int scroll_limit = INT_MAX / FRAME_LINE_HEIGHT (f);
12569
12570 #if GLYPH_DEBUG
12571 debug_method_add (w, "try_scrolling");
12572 #endif
12573
12574 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12575
12576 /* Compute scroll margin height in pixels. We scroll when point is
12577 within this distance from the top or bottom of the window. */
12578 if (scroll_margin > 0)
12579 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
12580 * FRAME_LINE_HEIGHT (f);
12581 else
12582 this_scroll_margin = 0;
12583
12584 /* Force scroll_conservatively to have a reasonable value, to avoid
12585 overflow while computing how much to scroll. Note that the user
12586 can supply scroll-conservatively equal to `most-positive-fixnum',
12587 which can be larger than INT_MAX. */
12588 if (scroll_conservatively > scroll_limit)
12589 {
12590 scroll_conservatively = scroll_limit;
12591 scroll_max = INT_MAX;
12592 }
12593 else if (scroll_step || scroll_conservatively || temp_scroll_step)
12594 /* Compute how much we should try to scroll maximally to bring
12595 point into view. */
12596 scroll_max = (max (scroll_step,
12597 max (scroll_conservatively, temp_scroll_step))
12598 * FRAME_LINE_HEIGHT (f));
12599 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12600 || NUMBERP (current_buffer->scroll_up_aggressively))
12601 /* We're trying to scroll because of aggressive scrolling but no
12602 scroll_step is set. Choose an arbitrary one. */
12603 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
12604 else
12605 scroll_max = 0;
12606
12607 too_near_end:
12608
12609 /* Decide whether to scroll down. */
12610 if (PT > CHARPOS (startp))
12611 {
12612 int scroll_margin_y;
12613
12614 /* Compute the pixel ypos of the scroll margin, then move it to
12615 either that ypos or PT, whichever comes first. */
12616 start_display (&it, w, startp);
12617 scroll_margin_y = it.last_visible_y - this_scroll_margin
12618 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
12619 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
12620 (MOVE_TO_POS | MOVE_TO_Y));
12621
12622 if (PT > CHARPOS (it.current.pos))
12623 {
12624 int y0 = line_bottom_y (&it);
12625
12626 /* Compute the distance from the scroll margin to PT
12627 (including the height of the cursor line). Moving the
12628 iterator unconditionally to PT can be slow if PT is far
12629 away, so stop 10 lines past the window bottom (is there a
12630 way to do the right thing quickly?). */
12631 move_it_to (&it, PT, -1,
12632 it.last_visible_y + 10 * FRAME_LINE_HEIGHT (f),
12633 -1, MOVE_TO_POS | MOVE_TO_Y);
12634 dy = line_bottom_y (&it) - y0;
12635
12636 if (dy > scroll_max)
12637 return SCROLLING_FAILED;
12638
12639 scroll_down_p = 1;
12640 }
12641 }
12642
12643 if (scroll_down_p)
12644 {
12645 /* Point is in or below the bottom scroll margin, so move the
12646 window start down. If scrolling conservatively, move it just
12647 enough down to make point visible. If scroll_step is set,
12648 move it down by scroll_step. */
12649 if (scroll_conservatively)
12650 amount_to_scroll
12651 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12652 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12653 else if (scroll_step || temp_scroll_step)
12654 amount_to_scroll = scroll_max;
12655 else
12656 {
12657 aggressive = current_buffer->scroll_up_aggressively;
12658 height = WINDOW_BOX_TEXT_HEIGHT (w);
12659 if (NUMBERP (aggressive))
12660 {
12661 double float_amount = XFLOATINT (aggressive) * height;
12662 amount_to_scroll = float_amount;
12663 if (amount_to_scroll == 0 && float_amount > 0)
12664 amount_to_scroll = 1;
12665 }
12666 }
12667
12668 if (amount_to_scroll <= 0)
12669 return SCROLLING_FAILED;
12670
12671 start_display (&it, w, startp);
12672 move_it_vertically (&it, amount_to_scroll);
12673
12674 /* If STARTP is unchanged, move it down another screen line. */
12675 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12676 move_it_by_lines (&it, 1, 1);
12677 startp = it.current.pos;
12678 }
12679 else
12680 {
12681 struct text_pos scroll_margin_pos = startp;
12682
12683 /* See if point is inside the scroll margin at the top of the
12684 window. */
12685 if (this_scroll_margin)
12686 {
12687 start_display (&it, w, startp);
12688 move_it_vertically (&it, this_scroll_margin);
12689 scroll_margin_pos = it.current.pos;
12690 }
12691
12692 if (PT < CHARPOS (scroll_margin_pos))
12693 {
12694 /* Point is in the scroll margin at the top of the window or
12695 above what is displayed in the window. */
12696 int y0;
12697
12698 /* Compute the vertical distance from PT to the scroll
12699 margin position. Give up if distance is greater than
12700 scroll_max. */
12701 SET_TEXT_POS (pos, PT, PT_BYTE);
12702 start_display (&it, w, pos);
12703 y0 = it.current_y;
12704 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12705 it.last_visible_y, -1,
12706 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12707 dy = it.current_y - y0;
12708 if (dy > scroll_max)
12709 return SCROLLING_FAILED;
12710
12711 /* Compute new window start. */
12712 start_display (&it, w, startp);
12713
12714 if (scroll_conservatively)
12715 amount_to_scroll
12716 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12717 else if (scroll_step || temp_scroll_step)
12718 amount_to_scroll = scroll_max;
12719 else
12720 {
12721 aggressive = current_buffer->scroll_down_aggressively;
12722 height = WINDOW_BOX_TEXT_HEIGHT (w);
12723 if (NUMBERP (aggressive))
12724 {
12725 double float_amount = XFLOATINT (aggressive) * height;
12726 amount_to_scroll = float_amount;
12727 if (amount_to_scroll == 0 && float_amount > 0)
12728 amount_to_scroll = 1;
12729 }
12730 }
12731
12732 if (amount_to_scroll <= 0)
12733 return SCROLLING_FAILED;
12734
12735 move_it_vertically_backward (&it, amount_to_scroll);
12736 startp = it.current.pos;
12737 }
12738 }
12739
12740 /* Run window scroll functions. */
12741 startp = run_window_scroll_functions (window, startp);
12742
12743 /* Display the window. Give up if new fonts are loaded, or if point
12744 doesn't appear. */
12745 if (!try_window (window, startp, 0))
12746 rc = SCROLLING_NEED_LARGER_MATRICES;
12747 else if (w->cursor.vpos < 0)
12748 {
12749 clear_glyph_matrix (w->desired_matrix);
12750 rc = SCROLLING_FAILED;
12751 }
12752 else
12753 {
12754 /* Maybe forget recorded base line for line number display. */
12755 if (!just_this_one_p
12756 || current_buffer->clip_changed
12757 || BEG_UNCHANGED < CHARPOS (startp))
12758 w->base_line_number = Qnil;
12759
12760 /* If cursor ends up on a partially visible line,
12761 treat that as being off the bottom of the screen. */
12762 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12763 {
12764 clear_glyph_matrix (w->desired_matrix);
12765 ++extra_scroll_margin_lines;
12766 goto too_near_end;
12767 }
12768 rc = SCROLLING_SUCCESS;
12769 }
12770
12771 return rc;
12772 }
12773
12774
12775 /* Compute a suitable window start for window W if display of W starts
12776 on a continuation line. Value is non-zero if a new window start
12777 was computed.
12778
12779 The new window start will be computed, based on W's width, starting
12780 from the start of the continued line. It is the start of the
12781 screen line with the minimum distance from the old start W->start. */
12782
12783 static int
12784 compute_window_start_on_continuation_line (w)
12785 struct window *w;
12786 {
12787 struct text_pos pos, start_pos;
12788 int window_start_changed_p = 0;
12789
12790 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12791
12792 /* If window start is on a continuation line... Window start may be
12793 < BEGV in case there's invisible text at the start of the
12794 buffer (M-x rmail, for example). */
12795 if (CHARPOS (start_pos) > BEGV
12796 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12797 {
12798 struct it it;
12799 struct glyph_row *row;
12800
12801 /* Handle the case that the window start is out of range. */
12802 if (CHARPOS (start_pos) < BEGV)
12803 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12804 else if (CHARPOS (start_pos) > ZV)
12805 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12806
12807 /* Find the start of the continued line. This should be fast
12808 because scan_buffer is fast (newline cache). */
12809 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12810 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12811 row, DEFAULT_FACE_ID);
12812 reseat_at_previous_visible_line_start (&it);
12813
12814 /* If the line start is "too far" away from the window start,
12815 say it takes too much time to compute a new window start. */
12816 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12817 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12818 {
12819 int min_distance, distance;
12820
12821 /* Move forward by display lines to find the new window
12822 start. If window width was enlarged, the new start can
12823 be expected to be > the old start. If window width was
12824 decreased, the new window start will be < the old start.
12825 So, we're looking for the display line start with the
12826 minimum distance from the old window start. */
12827 pos = it.current.pos;
12828 min_distance = INFINITY;
12829 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12830 distance < min_distance)
12831 {
12832 min_distance = distance;
12833 pos = it.current.pos;
12834 move_it_by_lines (&it, 1, 0);
12835 }
12836
12837 /* Set the window start there. */
12838 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12839 window_start_changed_p = 1;
12840 }
12841 }
12842
12843 return window_start_changed_p;
12844 }
12845
12846
12847 /* Try cursor movement in case text has not changed in window WINDOW,
12848 with window start STARTP. Value is
12849
12850 CURSOR_MOVEMENT_SUCCESS if successful
12851
12852 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12853
12854 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12855 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12856 we want to scroll as if scroll-step were set to 1. See the code.
12857
12858 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12859 which case we have to abort this redisplay, and adjust matrices
12860 first. */
12861
12862 enum
12863 {
12864 CURSOR_MOVEMENT_SUCCESS,
12865 CURSOR_MOVEMENT_CANNOT_BE_USED,
12866 CURSOR_MOVEMENT_MUST_SCROLL,
12867 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12868 };
12869
12870 static int
12871 try_cursor_movement (window, startp, scroll_step)
12872 Lisp_Object window;
12873 struct text_pos startp;
12874 int *scroll_step;
12875 {
12876 struct window *w = XWINDOW (window);
12877 struct frame *f = XFRAME (w->frame);
12878 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12879
12880 #if GLYPH_DEBUG
12881 if (inhibit_try_cursor_movement)
12882 return rc;
12883 #endif
12884
12885 /* Handle case where text has not changed, only point, and it has
12886 not moved off the frame. */
12887 if (/* Point may be in this window. */
12888 PT >= CHARPOS (startp)
12889 /* Selective display hasn't changed. */
12890 && !current_buffer->clip_changed
12891 /* Function force-mode-line-update is used to force a thorough
12892 redisplay. It sets either windows_or_buffers_changed or
12893 update_mode_lines. So don't take a shortcut here for these
12894 cases. */
12895 && !update_mode_lines
12896 && !windows_or_buffers_changed
12897 && !cursor_type_changed
12898 /* Can't use this case if highlighting a region. When a
12899 region exists, cursor movement has to do more than just
12900 set the cursor. */
12901 && !(!NILP (Vtransient_mark_mode)
12902 && !NILP (current_buffer->mark_active))
12903 && NILP (w->region_showing)
12904 && NILP (Vshow_trailing_whitespace)
12905 /* Right after splitting windows, last_point may be nil. */
12906 && INTEGERP (w->last_point)
12907 /* This code is not used for mini-buffer for the sake of the case
12908 of redisplaying to replace an echo area message; since in
12909 that case the mini-buffer contents per se are usually
12910 unchanged. This code is of no real use in the mini-buffer
12911 since the handling of this_line_start_pos, etc., in redisplay
12912 handles the same cases. */
12913 && !EQ (window, minibuf_window)
12914 /* When splitting windows or for new windows, it happens that
12915 redisplay is called with a nil window_end_vpos or one being
12916 larger than the window. This should really be fixed in
12917 window.c. I don't have this on my list, now, so we do
12918 approximately the same as the old redisplay code. --gerd. */
12919 && INTEGERP (w->window_end_vpos)
12920 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12921 && (FRAME_WINDOW_P (f)
12922 || !overlay_arrow_in_current_buffer_p ()))
12923 {
12924 int this_scroll_margin, top_scroll_margin;
12925 struct glyph_row *row = NULL;
12926
12927 #if GLYPH_DEBUG
12928 debug_method_add (w, "cursor movement");
12929 #endif
12930
12931 /* Scroll if point within this distance from the top or bottom
12932 of the window. This is a pixel value. */
12933 if (scroll_margin > 0)
12934 {
12935 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12936 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12937 }
12938 else
12939 this_scroll_margin = 0;
12940
12941 top_scroll_margin = this_scroll_margin;
12942 if (WINDOW_WANTS_HEADER_LINE_P (w))
12943 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12944
12945 /* Start with the row the cursor was displayed during the last
12946 not paused redisplay. Give up if that row is not valid. */
12947 if (w->last_cursor.vpos < 0
12948 || w->last_cursor.vpos >= w->current_matrix->nrows)
12949 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12950 else
12951 {
12952 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12953 if (row->mode_line_p)
12954 ++row;
12955 if (!row->enabled_p)
12956 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12957 }
12958
12959 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12960 {
12961 int scroll_p = 0;
12962 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12963
12964 if (PT > XFASTINT (w->last_point))
12965 {
12966 /* Point has moved forward. */
12967 while (MATRIX_ROW_END_CHARPOS (row) < PT
12968 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12969 {
12970 xassert (row->enabled_p);
12971 ++row;
12972 }
12973
12974 /* The end position of a row equals the start position
12975 of the next row. If PT is there, we would rather
12976 display it in the next line. */
12977 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12978 && MATRIX_ROW_END_CHARPOS (row) == PT
12979 && !cursor_row_p (w, row))
12980 ++row;
12981
12982 /* If within the scroll margin, scroll. Note that
12983 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12984 the next line would be drawn, and that
12985 this_scroll_margin can be zero. */
12986 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12987 || PT > MATRIX_ROW_END_CHARPOS (row)
12988 /* Line is completely visible last line in window
12989 and PT is to be set in the next line. */
12990 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12991 && PT == MATRIX_ROW_END_CHARPOS (row)
12992 && !row->ends_at_zv_p
12993 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12994 scroll_p = 1;
12995 }
12996 else if (PT < XFASTINT (w->last_point))
12997 {
12998 /* Cursor has to be moved backward. Note that PT >=
12999 CHARPOS (startp) because of the outer if-statement. */
13000 while (!row->mode_line_p
13001 && (MATRIX_ROW_START_CHARPOS (row) > PT
13002 || (MATRIX_ROW_START_CHARPOS (row) == PT
13003 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13004 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13005 row > w->current_matrix->rows
13006 && (row-1)->ends_in_newline_from_string_p))))
13007 && (row->y > top_scroll_margin
13008 || CHARPOS (startp) == BEGV))
13009 {
13010 xassert (row->enabled_p);
13011 --row;
13012 }
13013
13014 /* Consider the following case: Window starts at BEGV,
13015 there is invisible, intangible text at BEGV, so that
13016 display starts at some point START > BEGV. It can
13017 happen that we are called with PT somewhere between
13018 BEGV and START. Try to handle that case. */
13019 if (row < w->current_matrix->rows
13020 || row->mode_line_p)
13021 {
13022 row = w->current_matrix->rows;
13023 if (row->mode_line_p)
13024 ++row;
13025 }
13026
13027 /* Due to newlines in overlay strings, we may have to
13028 skip forward over overlay strings. */
13029 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13030 && MATRIX_ROW_END_CHARPOS (row) == PT
13031 && !cursor_row_p (w, row))
13032 ++row;
13033
13034 /* If within the scroll margin, scroll. */
13035 if (row->y < top_scroll_margin
13036 && CHARPOS (startp) != BEGV)
13037 scroll_p = 1;
13038 }
13039 else
13040 {
13041 /* Cursor did not move. So don't scroll even if cursor line
13042 is partially visible, as it was so before. */
13043 rc = CURSOR_MOVEMENT_SUCCESS;
13044 }
13045
13046 if (PT < MATRIX_ROW_START_CHARPOS (row)
13047 || PT > MATRIX_ROW_END_CHARPOS (row))
13048 {
13049 /* if PT is not in the glyph row, give up. */
13050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13051 }
13052 else if (rc != CURSOR_MOVEMENT_SUCCESS
13053 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13054 && make_cursor_line_fully_visible_p)
13055 {
13056 if (PT == MATRIX_ROW_END_CHARPOS (row)
13057 && !row->ends_at_zv_p
13058 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13059 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13060 else if (row->height > window_box_height (w))
13061 {
13062 /* If we end up in a partially visible line, let's
13063 make it fully visible, except when it's taller
13064 than the window, in which case we can't do much
13065 about it. */
13066 *scroll_step = 1;
13067 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13068 }
13069 else
13070 {
13071 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13072 if (!cursor_row_fully_visible_p (w, 0, 1))
13073 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13074 else
13075 rc = CURSOR_MOVEMENT_SUCCESS;
13076 }
13077 }
13078 else if (scroll_p)
13079 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13080 else
13081 {
13082 do
13083 {
13084 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13085 {
13086 rc = CURSOR_MOVEMENT_SUCCESS;
13087 break;
13088 }
13089 ++row;
13090 }
13091 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13092 && MATRIX_ROW_START_CHARPOS (row) == PT
13093 && cursor_row_p (w, row));
13094 }
13095 }
13096 }
13097
13098 return rc;
13099 }
13100
13101 void
13102 set_vertical_scroll_bar (w)
13103 struct window *w;
13104 {
13105 int start, end, whole;
13106
13107 /* Calculate the start and end positions for the current window.
13108 At some point, it would be nice to choose between scrollbars
13109 which reflect the whole buffer size, with special markers
13110 indicating narrowing, and scrollbars which reflect only the
13111 visible region.
13112
13113 Note that mini-buffers sometimes aren't displaying any text. */
13114 if (!MINI_WINDOW_P (w)
13115 || (w == XWINDOW (minibuf_window)
13116 && NILP (echo_area_buffer[0])))
13117 {
13118 struct buffer *buf = XBUFFER (w->buffer);
13119 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13120 start = marker_position (w->start) - BUF_BEGV (buf);
13121 /* I don't think this is guaranteed to be right. For the
13122 moment, we'll pretend it is. */
13123 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13124
13125 if (end < start)
13126 end = start;
13127 if (whole < (end - start))
13128 whole = end - start;
13129 }
13130 else
13131 start = end = whole = 0;
13132
13133 /* Indicate what this scroll bar ought to be displaying now. */
13134 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13135 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13136 (w, end - start, whole, start);
13137 }
13138
13139
13140 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13141 selected_window is redisplayed.
13142
13143 We can return without actually redisplaying the window if
13144 fonts_changed_p is nonzero. In that case, redisplay_internal will
13145 retry. */
13146
13147 static void
13148 redisplay_window (window, just_this_one_p)
13149 Lisp_Object window;
13150 int just_this_one_p;
13151 {
13152 struct window *w = XWINDOW (window);
13153 struct frame *f = XFRAME (w->frame);
13154 struct buffer *buffer = XBUFFER (w->buffer);
13155 struct buffer *old = current_buffer;
13156 struct text_pos lpoint, opoint, startp;
13157 int update_mode_line;
13158 int tem;
13159 struct it it;
13160 /* Record it now because it's overwritten. */
13161 int current_matrix_up_to_date_p = 0;
13162 int used_current_matrix_p = 0;
13163 /* This is less strict than current_matrix_up_to_date_p.
13164 It indictes that the buffer contents and narrowing are unchanged. */
13165 int buffer_unchanged_p = 0;
13166 int temp_scroll_step = 0;
13167 int count = SPECPDL_INDEX ();
13168 int rc;
13169 int centering_position = -1;
13170 int last_line_misfit = 0;
13171 int beg_unchanged, end_unchanged;
13172
13173 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13174 opoint = lpoint;
13175
13176 /* W must be a leaf window here. */
13177 xassert (!NILP (w->buffer));
13178 #if GLYPH_DEBUG
13179 *w->desired_matrix->method = 0;
13180 #endif
13181
13182 restart:
13183 reconsider_clip_changes (w, buffer);
13184
13185 /* Has the mode line to be updated? */
13186 update_mode_line = (!NILP (w->update_mode_line)
13187 || update_mode_lines
13188 || buffer->clip_changed
13189 || buffer->prevent_redisplay_optimizations_p);
13190
13191 if (MINI_WINDOW_P (w))
13192 {
13193 if (w == XWINDOW (echo_area_window)
13194 && !NILP (echo_area_buffer[0]))
13195 {
13196 if (update_mode_line)
13197 /* We may have to update a tty frame's menu bar or a
13198 tool-bar. Example `M-x C-h C-h C-g'. */
13199 goto finish_menu_bars;
13200 else
13201 /* We've already displayed the echo area glyphs in this window. */
13202 goto finish_scroll_bars;
13203 }
13204 else if ((w != XWINDOW (minibuf_window)
13205 || minibuf_level == 0)
13206 /* When buffer is nonempty, redisplay window normally. */
13207 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13208 /* Quail displays non-mini buffers in minibuffer window.
13209 In that case, redisplay the window normally. */
13210 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13211 {
13212 /* W is a mini-buffer window, but it's not active, so clear
13213 it. */
13214 int yb = window_text_bottom_y (w);
13215 struct glyph_row *row;
13216 int y;
13217
13218 for (y = 0, row = w->desired_matrix->rows;
13219 y < yb;
13220 y += row->height, ++row)
13221 blank_row (w, row, y);
13222 goto finish_scroll_bars;
13223 }
13224
13225 clear_glyph_matrix (w->desired_matrix);
13226 }
13227
13228 /* Otherwise set up data on this window; select its buffer and point
13229 value. */
13230 /* Really select the buffer, for the sake of buffer-local
13231 variables. */
13232 set_buffer_internal_1 (XBUFFER (w->buffer));
13233
13234 current_matrix_up_to_date_p
13235 = (!NILP (w->window_end_valid)
13236 && !current_buffer->clip_changed
13237 && !current_buffer->prevent_redisplay_optimizations_p
13238 && XFASTINT (w->last_modified) >= MODIFF
13239 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13240
13241 /* Run the window-bottom-change-functions
13242 if it is possible that the text on the screen has changed
13243 (either due to modification of the text, or any other reason). */
13244 if (!current_matrix_up_to_date_p
13245 && !NILP (Vwindow_text_change_functions))
13246 {
13247 safe_run_hooks (Qwindow_text_change_functions);
13248 goto restart;
13249 }
13250
13251 beg_unchanged = BEG_UNCHANGED;
13252 end_unchanged = END_UNCHANGED;
13253
13254 SET_TEXT_POS (opoint, PT, PT_BYTE);
13255
13256 specbind (Qinhibit_point_motion_hooks, Qt);
13257
13258 buffer_unchanged_p
13259 = (!NILP (w->window_end_valid)
13260 && !current_buffer->clip_changed
13261 && XFASTINT (w->last_modified) >= MODIFF
13262 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13263
13264 /* When windows_or_buffers_changed is non-zero, we can't rely on
13265 the window end being valid, so set it to nil there. */
13266 if (windows_or_buffers_changed)
13267 {
13268 /* If window starts on a continuation line, maybe adjust the
13269 window start in case the window's width changed. */
13270 if (XMARKER (w->start)->buffer == current_buffer)
13271 compute_window_start_on_continuation_line (w);
13272
13273 w->window_end_valid = Qnil;
13274 }
13275
13276 /* Some sanity checks. */
13277 CHECK_WINDOW_END (w);
13278 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13279 abort ();
13280 if (BYTEPOS (opoint) < CHARPOS (opoint))
13281 abort ();
13282
13283 /* If %c is in mode line, update it if needed. */
13284 if (!NILP (w->column_number_displayed)
13285 /* This alternative quickly identifies a common case
13286 where no change is needed. */
13287 && !(PT == XFASTINT (w->last_point)
13288 && XFASTINT (w->last_modified) >= MODIFF
13289 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13290 && (XFASTINT (w->column_number_displayed)
13291 != (int) current_column ())) /* iftc */
13292 update_mode_line = 1;
13293
13294 /* Count number of windows showing the selected buffer. An indirect
13295 buffer counts as its base buffer. */
13296 if (!just_this_one_p)
13297 {
13298 struct buffer *current_base, *window_base;
13299 current_base = current_buffer;
13300 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13301 if (current_base->base_buffer)
13302 current_base = current_base->base_buffer;
13303 if (window_base->base_buffer)
13304 window_base = window_base->base_buffer;
13305 if (current_base == window_base)
13306 buffer_shared++;
13307 }
13308
13309 /* Point refers normally to the selected window. For any other
13310 window, set up appropriate value. */
13311 if (!EQ (window, selected_window))
13312 {
13313 int new_pt = XMARKER (w->pointm)->charpos;
13314 int new_pt_byte = marker_byte_position (w->pointm);
13315 if (new_pt < BEGV)
13316 {
13317 new_pt = BEGV;
13318 new_pt_byte = BEGV_BYTE;
13319 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13320 }
13321 else if (new_pt > (ZV - 1))
13322 {
13323 new_pt = ZV;
13324 new_pt_byte = ZV_BYTE;
13325 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13326 }
13327
13328 /* We don't use SET_PT so that the point-motion hooks don't run. */
13329 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13330 }
13331
13332 /* If any of the character widths specified in the display table
13333 have changed, invalidate the width run cache. It's true that
13334 this may be a bit late to catch such changes, but the rest of
13335 redisplay goes (non-fatally) haywire when the display table is
13336 changed, so why should we worry about doing any better? */
13337 if (current_buffer->width_run_cache)
13338 {
13339 struct Lisp_Char_Table *disptab = buffer_display_table ();
13340
13341 if (! disptab_matches_widthtab (disptab,
13342 XVECTOR (current_buffer->width_table)))
13343 {
13344 invalidate_region_cache (current_buffer,
13345 current_buffer->width_run_cache,
13346 BEG, Z);
13347 recompute_width_table (current_buffer, disptab);
13348 }
13349 }
13350
13351 /* If window-start is screwed up, choose a new one. */
13352 if (XMARKER (w->start)->buffer != current_buffer)
13353 goto recenter;
13354
13355 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13356
13357 /* If someone specified a new starting point but did not insist,
13358 check whether it can be used. */
13359 if (!NILP (w->optional_new_start)
13360 && CHARPOS (startp) >= BEGV
13361 && CHARPOS (startp) <= ZV)
13362 {
13363 w->optional_new_start = Qnil;
13364 start_display (&it, w, startp);
13365 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13366 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13367 if (IT_CHARPOS (it) == PT)
13368 w->force_start = Qt;
13369 /* IT may overshoot PT if text at PT is invisible. */
13370 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13371 w->force_start = Qt;
13372 }
13373
13374 force_start:
13375
13376 /* Handle case where place to start displaying has been specified,
13377 unless the specified location is outside the accessible range. */
13378 if (!NILP (w->force_start)
13379 || w->frozen_window_start_p)
13380 {
13381 /* We set this later on if we have to adjust point. */
13382 int new_vpos = -1;
13383
13384 w->force_start = Qnil;
13385 w->vscroll = 0;
13386 w->window_end_valid = Qnil;
13387
13388 /* Forget any recorded base line for line number display. */
13389 if (!buffer_unchanged_p)
13390 w->base_line_number = Qnil;
13391
13392 /* Redisplay the mode line. Select the buffer properly for that.
13393 Also, run the hook window-scroll-functions
13394 because we have scrolled. */
13395 /* Note, we do this after clearing force_start because
13396 if there's an error, it is better to forget about force_start
13397 than to get into an infinite loop calling the hook functions
13398 and having them get more errors. */
13399 if (!update_mode_line
13400 || ! NILP (Vwindow_scroll_functions))
13401 {
13402 update_mode_line = 1;
13403 w->update_mode_line = Qt;
13404 startp = run_window_scroll_functions (window, startp);
13405 }
13406
13407 w->last_modified = make_number (0);
13408 w->last_overlay_modified = make_number (0);
13409 if (CHARPOS (startp) < BEGV)
13410 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13411 else if (CHARPOS (startp) > ZV)
13412 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13413
13414 /* Redisplay, then check if cursor has been set during the
13415 redisplay. Give up if new fonts were loaded. */
13416 /* We used to issue a CHECK_MARGINS argument to try_window here,
13417 but this causes scrolling to fail when point begins inside
13418 the scroll margin (bug#148) -- cyd */
13419 if (!try_window (window, startp, 0))
13420 {
13421 w->force_start = Qt;
13422 clear_glyph_matrix (w->desired_matrix);
13423 goto need_larger_matrices;
13424 }
13425
13426 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13427 {
13428 /* If point does not appear, try to move point so it does
13429 appear. The desired matrix has been built above, so we
13430 can use it here. */
13431 new_vpos = window_box_height (w) / 2;
13432 }
13433
13434 if (!cursor_row_fully_visible_p (w, 0, 0))
13435 {
13436 /* Point does appear, but on a line partly visible at end of window.
13437 Move it back to a fully-visible line. */
13438 new_vpos = window_box_height (w);
13439 }
13440
13441 /* If we need to move point for either of the above reasons,
13442 now actually do it. */
13443 if (new_vpos >= 0)
13444 {
13445 struct glyph_row *row;
13446
13447 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13448 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13449 ++row;
13450
13451 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13452 MATRIX_ROW_START_BYTEPOS (row));
13453
13454 if (w != XWINDOW (selected_window))
13455 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13456 else if (current_buffer == old)
13457 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13458
13459 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13460
13461 /* If we are highlighting the region, then we just changed
13462 the region, so redisplay to show it. */
13463 if (!NILP (Vtransient_mark_mode)
13464 && !NILP (current_buffer->mark_active))
13465 {
13466 clear_glyph_matrix (w->desired_matrix);
13467 if (!try_window (window, startp, 0))
13468 goto need_larger_matrices;
13469 }
13470 }
13471
13472 #if GLYPH_DEBUG
13473 debug_method_add (w, "forced window start");
13474 #endif
13475 goto done;
13476 }
13477
13478 /* Handle case where text has not changed, only point, and it has
13479 not moved off the frame, and we are not retrying after hscroll.
13480 (current_matrix_up_to_date_p is nonzero when retrying.) */
13481 if (current_matrix_up_to_date_p
13482 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13483 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13484 {
13485 switch (rc)
13486 {
13487 case CURSOR_MOVEMENT_SUCCESS:
13488 used_current_matrix_p = 1;
13489 goto done;
13490
13491 case CURSOR_MOVEMENT_MUST_SCROLL:
13492 goto try_to_scroll;
13493
13494 default:
13495 abort ();
13496 }
13497 }
13498 /* If current starting point was originally the beginning of a line
13499 but no longer is, find a new starting point. */
13500 else if (!NILP (w->start_at_line_beg)
13501 && !(CHARPOS (startp) <= BEGV
13502 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13503 {
13504 #if GLYPH_DEBUG
13505 debug_method_add (w, "recenter 1");
13506 #endif
13507 goto recenter;
13508 }
13509
13510 /* Try scrolling with try_window_id. Value is > 0 if update has
13511 been done, it is -1 if we know that the same window start will
13512 not work. It is 0 if unsuccessful for some other reason. */
13513 else if ((tem = try_window_id (w)) != 0)
13514 {
13515 #if GLYPH_DEBUG
13516 debug_method_add (w, "try_window_id %d", tem);
13517 #endif
13518
13519 if (fonts_changed_p)
13520 goto need_larger_matrices;
13521 if (tem > 0)
13522 goto done;
13523
13524 /* Otherwise try_window_id has returned -1 which means that we
13525 don't want the alternative below this comment to execute. */
13526 }
13527 else if (CHARPOS (startp) >= BEGV
13528 && CHARPOS (startp) <= ZV
13529 && PT >= CHARPOS (startp)
13530 && (CHARPOS (startp) < ZV
13531 /* Avoid starting at end of buffer. */
13532 || CHARPOS (startp) == BEGV
13533 || (XFASTINT (w->last_modified) >= MODIFF
13534 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13535 {
13536
13537 /* If first window line is a continuation line, and window start
13538 is inside the modified region, but the first change is before
13539 current window start, we must select a new window start.
13540
13541 However, if this is the result of a down-mouse event (e.g. by
13542 extending the mouse-drag-overlay), we don't want to select a
13543 new window start, since that would change the position under
13544 the mouse, resulting in an unwanted mouse-movement rather
13545 than a simple mouse-click. */
13546 if (NILP (w->start_at_line_beg)
13547 && NILP (do_mouse_tracking)
13548 && CHARPOS (startp) > BEGV
13549 && CHARPOS (startp) > BEG + beg_unchanged
13550 && CHARPOS (startp) <= Z - end_unchanged
13551 /* Even if w->start_at_line_beg is nil, a new window may
13552 start at a line_beg, since that's how set_buffer_window
13553 sets it. So, we need to check the return value of
13554 compute_window_start_on_continuation_line. (See also
13555 bug#197). */
13556 && XMARKER (w->start)->buffer == current_buffer
13557 && compute_window_start_on_continuation_line (w))
13558 {
13559 w->force_start = Qt;
13560 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13561 goto force_start;
13562 }
13563
13564 #if GLYPH_DEBUG
13565 debug_method_add (w, "same window start");
13566 #endif
13567
13568 /* Try to redisplay starting at same place as before.
13569 If point has not moved off frame, accept the results. */
13570 if (!current_matrix_up_to_date_p
13571 /* Don't use try_window_reusing_current_matrix in this case
13572 because a window scroll function can have changed the
13573 buffer. */
13574 || !NILP (Vwindow_scroll_functions)
13575 || MINI_WINDOW_P (w)
13576 || !(used_current_matrix_p
13577 = try_window_reusing_current_matrix (w)))
13578 {
13579 IF_DEBUG (debug_method_add (w, "1"));
13580 if (try_window (window, startp, 1) < 0)
13581 /* -1 means we need to scroll.
13582 0 means we need new matrices, but fonts_changed_p
13583 is set in that case, so we will detect it below. */
13584 goto try_to_scroll;
13585 }
13586
13587 if (fonts_changed_p)
13588 goto need_larger_matrices;
13589
13590 if (w->cursor.vpos >= 0)
13591 {
13592 if (!just_this_one_p
13593 || current_buffer->clip_changed
13594 || BEG_UNCHANGED < CHARPOS (startp))
13595 /* Forget any recorded base line for line number display. */
13596 w->base_line_number = Qnil;
13597
13598 if (!cursor_row_fully_visible_p (w, 1, 0))
13599 {
13600 clear_glyph_matrix (w->desired_matrix);
13601 last_line_misfit = 1;
13602 }
13603 /* Drop through and scroll. */
13604 else
13605 goto done;
13606 }
13607 else
13608 clear_glyph_matrix (w->desired_matrix);
13609 }
13610
13611 try_to_scroll:
13612
13613 w->last_modified = make_number (0);
13614 w->last_overlay_modified = make_number (0);
13615
13616 /* Redisplay the mode line. Select the buffer properly for that. */
13617 if (!update_mode_line)
13618 {
13619 update_mode_line = 1;
13620 w->update_mode_line = Qt;
13621 }
13622
13623 /* Try to scroll by specified few lines. */
13624 if ((scroll_conservatively
13625 || scroll_step
13626 || temp_scroll_step
13627 || NUMBERP (current_buffer->scroll_up_aggressively)
13628 || NUMBERP (current_buffer->scroll_down_aggressively))
13629 && !current_buffer->clip_changed
13630 && CHARPOS (startp) >= BEGV
13631 && CHARPOS (startp) <= ZV)
13632 {
13633 /* The function returns -1 if new fonts were loaded, 1 if
13634 successful, 0 if not successful. */
13635 int rc = try_scrolling (window, just_this_one_p,
13636 scroll_conservatively,
13637 scroll_step,
13638 temp_scroll_step, last_line_misfit);
13639 switch (rc)
13640 {
13641 case SCROLLING_SUCCESS:
13642 goto done;
13643
13644 case SCROLLING_NEED_LARGER_MATRICES:
13645 goto need_larger_matrices;
13646
13647 case SCROLLING_FAILED:
13648 break;
13649
13650 default:
13651 abort ();
13652 }
13653 }
13654
13655 /* Finally, just choose place to start which centers point */
13656
13657 recenter:
13658 if (centering_position < 0)
13659 centering_position = window_box_height (w) / 2;
13660
13661 #if GLYPH_DEBUG
13662 debug_method_add (w, "recenter");
13663 #endif
13664
13665 /* w->vscroll = 0; */
13666
13667 /* Forget any previously recorded base line for line number display. */
13668 if (!buffer_unchanged_p)
13669 w->base_line_number = Qnil;
13670
13671 /* Move backward half the height of the window. */
13672 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13673 it.current_y = it.last_visible_y;
13674 move_it_vertically_backward (&it, centering_position);
13675 xassert (IT_CHARPOS (it) >= BEGV);
13676
13677 /* The function move_it_vertically_backward may move over more
13678 than the specified y-distance. If it->w is small, e.g. a
13679 mini-buffer window, we may end up in front of the window's
13680 display area. Start displaying at the start of the line
13681 containing PT in this case. */
13682 if (it.current_y <= 0)
13683 {
13684 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13685 move_it_vertically_backward (&it, 0);
13686 it.current_y = 0;
13687 }
13688
13689 it.current_x = it.hpos = 0;
13690
13691 /* Set startp here explicitly in case that helps avoid an infinite loop
13692 in case the window-scroll-functions functions get errors. */
13693 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13694
13695 /* Run scroll hooks. */
13696 startp = run_window_scroll_functions (window, it.current.pos);
13697
13698 /* Redisplay the window. */
13699 if (!current_matrix_up_to_date_p
13700 || windows_or_buffers_changed
13701 || cursor_type_changed
13702 /* Don't use try_window_reusing_current_matrix in this case
13703 because it can have changed the buffer. */
13704 || !NILP (Vwindow_scroll_functions)
13705 || !just_this_one_p
13706 || MINI_WINDOW_P (w)
13707 || !(used_current_matrix_p
13708 = try_window_reusing_current_matrix (w)))
13709 try_window (window, startp, 0);
13710
13711 /* If new fonts have been loaded (due to fontsets), give up. We
13712 have to start a new redisplay since we need to re-adjust glyph
13713 matrices. */
13714 if (fonts_changed_p)
13715 goto need_larger_matrices;
13716
13717 /* If cursor did not appear assume that the middle of the window is
13718 in the first line of the window. Do it again with the next line.
13719 (Imagine a window of height 100, displaying two lines of height
13720 60. Moving back 50 from it->last_visible_y will end in the first
13721 line.) */
13722 if (w->cursor.vpos < 0)
13723 {
13724 if (!NILP (w->window_end_valid)
13725 && PT >= Z - XFASTINT (w->window_end_pos))
13726 {
13727 clear_glyph_matrix (w->desired_matrix);
13728 move_it_by_lines (&it, 1, 0);
13729 try_window (window, it.current.pos, 0);
13730 }
13731 else if (PT < IT_CHARPOS (it))
13732 {
13733 clear_glyph_matrix (w->desired_matrix);
13734 move_it_by_lines (&it, -1, 0);
13735 try_window (window, it.current.pos, 0);
13736 }
13737 else
13738 {
13739 /* Not much we can do about it. */
13740 }
13741 }
13742
13743 /* Consider the following case: Window starts at BEGV, there is
13744 invisible, intangible text at BEGV, so that display starts at
13745 some point START > BEGV. It can happen that we are called with
13746 PT somewhere between BEGV and START. Try to handle that case. */
13747 if (w->cursor.vpos < 0)
13748 {
13749 struct glyph_row *row = w->current_matrix->rows;
13750 if (row->mode_line_p)
13751 ++row;
13752 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13753 }
13754
13755 if (!cursor_row_fully_visible_p (w, 0, 0))
13756 {
13757 /* If vscroll is enabled, disable it and try again. */
13758 if (w->vscroll)
13759 {
13760 w->vscroll = 0;
13761 clear_glyph_matrix (w->desired_matrix);
13762 goto recenter;
13763 }
13764
13765 /* If centering point failed to make the whole line visible,
13766 put point at the top instead. That has to make the whole line
13767 visible, if it can be done. */
13768 if (centering_position == 0)
13769 goto done;
13770
13771 clear_glyph_matrix (w->desired_matrix);
13772 centering_position = 0;
13773 goto recenter;
13774 }
13775
13776 done:
13777
13778 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13779 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13780 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13781 ? Qt : Qnil);
13782
13783 /* Display the mode line, if we must. */
13784 if ((update_mode_line
13785 /* If window not full width, must redo its mode line
13786 if (a) the window to its side is being redone and
13787 (b) we do a frame-based redisplay. This is a consequence
13788 of how inverted lines are drawn in frame-based redisplay. */
13789 || (!just_this_one_p
13790 && !FRAME_WINDOW_P (f)
13791 && !WINDOW_FULL_WIDTH_P (w))
13792 /* Line number to display. */
13793 || INTEGERP (w->base_line_pos)
13794 /* Column number is displayed and different from the one displayed. */
13795 || (!NILP (w->column_number_displayed)
13796 && (XFASTINT (w->column_number_displayed)
13797 != (int) current_column ()))) /* iftc */
13798 /* This means that the window has a mode line. */
13799 && (WINDOW_WANTS_MODELINE_P (w)
13800 || WINDOW_WANTS_HEADER_LINE_P (w)))
13801 {
13802 display_mode_lines (w);
13803
13804 /* If mode line height has changed, arrange for a thorough
13805 immediate redisplay using the correct mode line height. */
13806 if (WINDOW_WANTS_MODELINE_P (w)
13807 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13808 {
13809 fonts_changed_p = 1;
13810 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13811 = DESIRED_MODE_LINE_HEIGHT (w);
13812 }
13813
13814 /* If header line height has changed, arrange for a thorough
13815 immediate redisplay using the correct header line height. */
13816 if (WINDOW_WANTS_HEADER_LINE_P (w)
13817 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13818 {
13819 fonts_changed_p = 1;
13820 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13821 = DESIRED_HEADER_LINE_HEIGHT (w);
13822 }
13823
13824 if (fonts_changed_p)
13825 goto need_larger_matrices;
13826 }
13827
13828 if (!line_number_displayed
13829 && !BUFFERP (w->base_line_pos))
13830 {
13831 w->base_line_pos = Qnil;
13832 w->base_line_number = Qnil;
13833 }
13834
13835 finish_menu_bars:
13836
13837 /* When we reach a frame's selected window, redo the frame's menu bar. */
13838 if (update_mode_line
13839 && EQ (FRAME_SELECTED_WINDOW (f), window))
13840 {
13841 int redisplay_menu_p = 0;
13842 int redisplay_tool_bar_p = 0;
13843
13844 if (FRAME_WINDOW_P (f))
13845 {
13846 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
13847 || defined (HAVE_NS) || defined (USE_GTK)
13848 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13849 #else
13850 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13851 #endif
13852 }
13853 else
13854 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13855
13856 if (redisplay_menu_p)
13857 display_menu_bar (w);
13858
13859 #ifdef HAVE_WINDOW_SYSTEM
13860 if (FRAME_WINDOW_P (f))
13861 {
13862 #if defined (USE_GTK) || defined (HAVE_NS)
13863 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13864 #else
13865 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13866 && (FRAME_TOOL_BAR_LINES (f) > 0
13867 || !NILP (Vauto_resize_tool_bars));
13868 #endif
13869
13870 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13871 {
13872 extern int ignore_mouse_drag_p;
13873 ignore_mouse_drag_p = 1;
13874 }
13875 }
13876 #endif
13877 }
13878
13879 #ifdef HAVE_WINDOW_SYSTEM
13880 if (FRAME_WINDOW_P (f)
13881 && update_window_fringes (w, (just_this_one_p
13882 || (!used_current_matrix_p && !overlay_arrow_seen)
13883 || w->pseudo_window_p)))
13884 {
13885 update_begin (f);
13886 BLOCK_INPUT;
13887 if (draw_window_fringes (w, 1))
13888 x_draw_vertical_border (w);
13889 UNBLOCK_INPUT;
13890 update_end (f);
13891 }
13892 #endif /* HAVE_WINDOW_SYSTEM */
13893
13894 /* We go to this label, with fonts_changed_p nonzero,
13895 if it is necessary to try again using larger glyph matrices.
13896 We have to redeem the scroll bar even in this case,
13897 because the loop in redisplay_internal expects that. */
13898 need_larger_matrices:
13899 ;
13900 finish_scroll_bars:
13901
13902 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13903 {
13904 /* Set the thumb's position and size. */
13905 set_vertical_scroll_bar (w);
13906
13907 /* Note that we actually used the scroll bar attached to this
13908 window, so it shouldn't be deleted at the end of redisplay. */
13909 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13910 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13911 }
13912
13913 /* Restore current_buffer and value of point in it. The window
13914 update may have changed the buffer, so first make sure `opoint'
13915 is still valid (Bug#6177). */
13916 if (CHARPOS (opoint) < BEGV)
13917 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
13918 else if (CHARPOS (opoint) > ZV)
13919 TEMP_SET_PT_BOTH (Z, Z_BYTE);
13920 else
13921 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13922
13923 set_buffer_internal_1 (old);
13924 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13925 shorter. This can be caused by log truncation in *Messages*. */
13926 if (CHARPOS (lpoint) <= ZV)
13927 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13928
13929 unbind_to (count, Qnil);
13930 }
13931
13932
13933 /* Build the complete desired matrix of WINDOW with a window start
13934 buffer position POS.
13935
13936 Value is 1 if successful. It is zero if fonts were loaded during
13937 redisplay which makes re-adjusting glyph matrices necessary, and -1
13938 if point would appear in the scroll margins.
13939 (We check that only if CHECK_MARGINS is nonzero. */
13940
13941 int
13942 try_window (window, pos, check_margins)
13943 Lisp_Object window;
13944 struct text_pos pos;
13945 int check_margins;
13946 {
13947 struct window *w = XWINDOW (window);
13948 struct it it;
13949 struct glyph_row *last_text_row = NULL;
13950 struct frame *f = XFRAME (w->frame);
13951
13952 /* Make POS the new window start. */
13953 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13954
13955 /* Mark cursor position as unknown. No overlay arrow seen. */
13956 w->cursor.vpos = -1;
13957 overlay_arrow_seen = 0;
13958
13959 /* Initialize iterator and info to start at POS. */
13960 start_display (&it, w, pos);
13961
13962 /* Display all lines of W. */
13963 while (it.current_y < it.last_visible_y)
13964 {
13965 if (display_line (&it))
13966 last_text_row = it.glyph_row - 1;
13967 if (fonts_changed_p)
13968 return 0;
13969 }
13970
13971 /* Don't let the cursor end in the scroll margins. */
13972 if (check_margins
13973 && !MINI_WINDOW_P (w))
13974 {
13975 int this_scroll_margin;
13976
13977 if (scroll_margin > 0)
13978 {
13979 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13980 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
13981 }
13982 else
13983 this_scroll_margin = 0;
13984
13985 if ((w->cursor.y >= 0 /* not vscrolled */
13986 && w->cursor.y < this_scroll_margin
13987 && CHARPOS (pos) > BEGV
13988 && IT_CHARPOS (it) < ZV)
13989 /* rms: considering make_cursor_line_fully_visible_p here
13990 seems to give wrong results. We don't want to recenter
13991 when the last line is partly visible, we want to allow
13992 that case to be handled in the usual way. */
13993 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
13994 {
13995 w->cursor.vpos = -1;
13996 clear_glyph_matrix (w->desired_matrix);
13997 return -1;
13998 }
13999 }
14000
14001 /* If bottom moved off end of frame, change mode line percentage. */
14002 if (XFASTINT (w->window_end_pos) <= 0
14003 && Z != IT_CHARPOS (it))
14004 w->update_mode_line = Qt;
14005
14006 /* Set window_end_pos to the offset of the last character displayed
14007 on the window from the end of current_buffer. Set
14008 window_end_vpos to its row number. */
14009 if (last_text_row)
14010 {
14011 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14012 w->window_end_bytepos
14013 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14014 w->window_end_pos
14015 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14016 w->window_end_vpos
14017 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14018 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14019 ->displays_text_p);
14020 }
14021 else
14022 {
14023 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14024 w->window_end_pos = make_number (Z - ZV);
14025 w->window_end_vpos = make_number (0);
14026 }
14027
14028 /* But that is not valid info until redisplay finishes. */
14029 w->window_end_valid = Qnil;
14030 return 1;
14031 }
14032
14033
14034 \f
14035 /************************************************************************
14036 Window redisplay reusing current matrix when buffer has not changed
14037 ************************************************************************/
14038
14039 /* Try redisplay of window W showing an unchanged buffer with a
14040 different window start than the last time it was displayed by
14041 reusing its current matrix. Value is non-zero if successful.
14042 W->start is the new window start. */
14043
14044 static int
14045 try_window_reusing_current_matrix (w)
14046 struct window *w;
14047 {
14048 struct frame *f = XFRAME (w->frame);
14049 struct glyph_row *row, *bottom_row;
14050 struct it it;
14051 struct run run;
14052 struct text_pos start, new_start;
14053 int nrows_scrolled, i;
14054 struct glyph_row *last_text_row;
14055 struct glyph_row *last_reused_text_row;
14056 struct glyph_row *start_row;
14057 int start_vpos, min_y, max_y;
14058
14059 #if GLYPH_DEBUG
14060 if (inhibit_try_window_reusing)
14061 return 0;
14062 #endif
14063
14064 if (/* This function doesn't handle terminal frames. */
14065 !FRAME_WINDOW_P (f)
14066 /* Don't try to reuse the display if windows have been split
14067 or such. */
14068 || windows_or_buffers_changed
14069 || cursor_type_changed)
14070 return 0;
14071
14072 /* Can't do this if region may have changed. */
14073 if ((!NILP (Vtransient_mark_mode)
14074 && !NILP (current_buffer->mark_active))
14075 || !NILP (w->region_showing)
14076 || !NILP (Vshow_trailing_whitespace))
14077 return 0;
14078
14079 /* If top-line visibility has changed, give up. */
14080 if (WINDOW_WANTS_HEADER_LINE_P (w)
14081 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14082 return 0;
14083
14084 /* Give up if old or new display is scrolled vertically. We could
14085 make this function handle this, but right now it doesn't. */
14086 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14087 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14088 return 0;
14089
14090 /* The variable new_start now holds the new window start. The old
14091 start `start' can be determined from the current matrix. */
14092 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14093 start = start_row->start.pos;
14094 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14095
14096 /* Clear the desired matrix for the display below. */
14097 clear_glyph_matrix (w->desired_matrix);
14098
14099 if (CHARPOS (new_start) <= CHARPOS (start))
14100 {
14101 int first_row_y;
14102
14103 /* Don't use this method if the display starts with an ellipsis
14104 displayed for invisible text. It's not easy to handle that case
14105 below, and it's certainly not worth the effort since this is
14106 not a frequent case. */
14107 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14108 return 0;
14109
14110 IF_DEBUG (debug_method_add (w, "twu1"));
14111
14112 /* Display up to a row that can be reused. The variable
14113 last_text_row is set to the last row displayed that displays
14114 text. Note that it.vpos == 0 if or if not there is a
14115 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14116 start_display (&it, w, new_start);
14117 first_row_y = it.current_y;
14118 w->cursor.vpos = -1;
14119 last_text_row = last_reused_text_row = NULL;
14120
14121 while (it.current_y < it.last_visible_y
14122 && !fonts_changed_p)
14123 {
14124 /* If we have reached into the characters in the START row,
14125 that means the line boundaries have changed. So we
14126 can't start copying with the row START. Maybe it will
14127 work to start copying with the following row. */
14128 while (IT_CHARPOS (it) > CHARPOS (start))
14129 {
14130 /* Advance to the next row as the "start". */
14131 start_row++;
14132 start = start_row->start.pos;
14133 /* If there are no more rows to try, or just one, give up. */
14134 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14135 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14136 || CHARPOS (start) == ZV)
14137 {
14138 clear_glyph_matrix (w->desired_matrix);
14139 return 0;
14140 }
14141
14142 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14143 }
14144 /* If we have reached alignment,
14145 we can copy the rest of the rows. */
14146 if (IT_CHARPOS (it) == CHARPOS (start))
14147 break;
14148
14149 if (display_line (&it))
14150 last_text_row = it.glyph_row - 1;
14151 }
14152
14153 /* A value of current_y < last_visible_y means that we stopped
14154 at the previous window start, which in turn means that we
14155 have at least one reusable row. */
14156 if (it.current_y < it.last_visible_y)
14157 {
14158 /* IT.vpos always starts from 0; it counts text lines. */
14159 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14160
14161 /* Find PT if not already found in the lines displayed. */
14162 if (w->cursor.vpos < 0)
14163 {
14164 int dy = it.current_y - start_row->y;
14165
14166 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14167 row = row_containing_pos (w, PT, row, NULL, dy);
14168 if (row)
14169 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14170 dy, nrows_scrolled);
14171 else
14172 {
14173 clear_glyph_matrix (w->desired_matrix);
14174 return 0;
14175 }
14176 }
14177
14178 /* Scroll the display. Do it before the current matrix is
14179 changed. The problem here is that update has not yet
14180 run, i.e. part of the current matrix is not up to date.
14181 scroll_run_hook will clear the cursor, and use the
14182 current matrix to get the height of the row the cursor is
14183 in. */
14184 run.current_y = start_row->y;
14185 run.desired_y = it.current_y;
14186 run.height = it.last_visible_y - it.current_y;
14187
14188 if (run.height > 0 && run.current_y != run.desired_y)
14189 {
14190 update_begin (f);
14191 FRAME_RIF (f)->update_window_begin_hook (w);
14192 FRAME_RIF (f)->clear_window_mouse_face (w);
14193 FRAME_RIF (f)->scroll_run_hook (w, &run);
14194 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14195 update_end (f);
14196 }
14197
14198 /* Shift current matrix down by nrows_scrolled lines. */
14199 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14200 rotate_matrix (w->current_matrix,
14201 start_vpos,
14202 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14203 nrows_scrolled);
14204
14205 /* Disable lines that must be updated. */
14206 for (i = 0; i < nrows_scrolled; ++i)
14207 (start_row + i)->enabled_p = 0;
14208
14209 /* Re-compute Y positions. */
14210 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14211 max_y = it.last_visible_y;
14212 for (row = start_row + nrows_scrolled;
14213 row < bottom_row;
14214 ++row)
14215 {
14216 row->y = it.current_y;
14217 row->visible_height = row->height;
14218
14219 if (row->y < min_y)
14220 row->visible_height -= min_y - row->y;
14221 if (row->y + row->height > max_y)
14222 row->visible_height -= row->y + row->height - max_y;
14223 row->redraw_fringe_bitmaps_p = 1;
14224
14225 it.current_y += row->height;
14226
14227 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14228 last_reused_text_row = row;
14229 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14230 break;
14231 }
14232
14233 /* Disable lines in the current matrix which are now
14234 below the window. */
14235 for (++row; row < bottom_row; ++row)
14236 row->enabled_p = row->mode_line_p = 0;
14237 }
14238
14239 /* Update window_end_pos etc.; last_reused_text_row is the last
14240 reused row from the current matrix containing text, if any.
14241 The value of last_text_row is the last displayed line
14242 containing text. */
14243 if (last_reused_text_row)
14244 {
14245 w->window_end_bytepos
14246 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14247 w->window_end_pos
14248 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14249 w->window_end_vpos
14250 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14251 w->current_matrix));
14252 }
14253 else if (last_text_row)
14254 {
14255 w->window_end_bytepos
14256 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14257 w->window_end_pos
14258 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14259 w->window_end_vpos
14260 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14261 }
14262 else
14263 {
14264 /* This window must be completely empty. */
14265 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14266 w->window_end_pos = make_number (Z - ZV);
14267 w->window_end_vpos = make_number (0);
14268 }
14269 w->window_end_valid = Qnil;
14270
14271 /* Update hint: don't try scrolling again in update_window. */
14272 w->desired_matrix->no_scrolling_p = 1;
14273
14274 #if GLYPH_DEBUG
14275 debug_method_add (w, "try_window_reusing_current_matrix 1");
14276 #endif
14277 return 1;
14278 }
14279 else if (CHARPOS (new_start) > CHARPOS (start))
14280 {
14281 struct glyph_row *pt_row, *row;
14282 struct glyph_row *first_reusable_row;
14283 struct glyph_row *first_row_to_display;
14284 int dy;
14285 int yb = window_text_bottom_y (w);
14286
14287 /* Find the row starting at new_start, if there is one. Don't
14288 reuse a partially visible line at the end. */
14289 first_reusable_row = start_row;
14290 while (first_reusable_row->enabled_p
14291 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14292 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14293 < CHARPOS (new_start)))
14294 ++first_reusable_row;
14295
14296 /* Give up if there is no row to reuse. */
14297 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14298 || !first_reusable_row->enabled_p
14299 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14300 != CHARPOS (new_start)))
14301 return 0;
14302
14303 /* We can reuse fully visible rows beginning with
14304 first_reusable_row to the end of the window. Set
14305 first_row_to_display to the first row that cannot be reused.
14306 Set pt_row to the row containing point, if there is any. */
14307 pt_row = NULL;
14308 for (first_row_to_display = first_reusable_row;
14309 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14310 ++first_row_to_display)
14311 {
14312 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14313 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14314 pt_row = first_row_to_display;
14315 }
14316
14317 /* Start displaying at the start of first_row_to_display. */
14318 xassert (first_row_to_display->y < yb);
14319 init_to_row_start (&it, w, first_row_to_display);
14320
14321 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14322 - start_vpos);
14323 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14324 - nrows_scrolled);
14325 it.current_y = (first_row_to_display->y - first_reusable_row->y
14326 + WINDOW_HEADER_LINE_HEIGHT (w));
14327
14328 /* Display lines beginning with first_row_to_display in the
14329 desired matrix. Set last_text_row to the last row displayed
14330 that displays text. */
14331 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14332 if (pt_row == NULL)
14333 w->cursor.vpos = -1;
14334 last_text_row = NULL;
14335 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14336 if (display_line (&it))
14337 last_text_row = it.glyph_row - 1;
14338
14339 /* If point is in a reused row, adjust y and vpos of the cursor
14340 position. */
14341 if (pt_row)
14342 {
14343 w->cursor.vpos -= nrows_scrolled;
14344 w->cursor.y -= first_reusable_row->y - start_row->y;
14345 }
14346
14347 /* Give up if point isn't in a row displayed or reused. (This
14348 also handles the case where w->cursor.vpos < nrows_scrolled
14349 after the calls to display_line, which can happen with scroll
14350 margins. See bug#1295.) */
14351 if (w->cursor.vpos < 0)
14352 {
14353 clear_glyph_matrix (w->desired_matrix);
14354 return 0;
14355 }
14356
14357 /* Scroll the display. */
14358 run.current_y = first_reusable_row->y;
14359 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14360 run.height = it.last_visible_y - run.current_y;
14361 dy = run.current_y - run.desired_y;
14362
14363 if (run.height)
14364 {
14365 update_begin (f);
14366 FRAME_RIF (f)->update_window_begin_hook (w);
14367 FRAME_RIF (f)->clear_window_mouse_face (w);
14368 FRAME_RIF (f)->scroll_run_hook (w, &run);
14369 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14370 update_end (f);
14371 }
14372
14373 /* Adjust Y positions of reused rows. */
14374 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14375 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14376 max_y = it.last_visible_y;
14377 for (row = first_reusable_row; row < first_row_to_display; ++row)
14378 {
14379 row->y -= dy;
14380 row->visible_height = row->height;
14381 if (row->y < min_y)
14382 row->visible_height -= min_y - row->y;
14383 if (row->y + row->height > max_y)
14384 row->visible_height -= row->y + row->height - max_y;
14385 row->redraw_fringe_bitmaps_p = 1;
14386 }
14387
14388 /* Scroll the current matrix. */
14389 xassert (nrows_scrolled > 0);
14390 rotate_matrix (w->current_matrix,
14391 start_vpos,
14392 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14393 -nrows_scrolled);
14394
14395 /* Disable rows not reused. */
14396 for (row -= nrows_scrolled; row < bottom_row; ++row)
14397 row->enabled_p = 0;
14398
14399 /* Point may have moved to a different line, so we cannot assume that
14400 the previous cursor position is valid; locate the correct row. */
14401 if (pt_row)
14402 {
14403 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14404 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14405 row++)
14406 {
14407 w->cursor.vpos++;
14408 w->cursor.y = row->y;
14409 }
14410 if (row < bottom_row)
14411 {
14412 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14413 struct glyph *end = glyph + row->used[TEXT_AREA];
14414
14415 for (; glyph < end
14416 && (!BUFFERP (glyph->object)
14417 || glyph->charpos < PT);
14418 glyph++)
14419 {
14420 w->cursor.hpos++;
14421 w->cursor.x += glyph->pixel_width;
14422 }
14423 }
14424 }
14425
14426 /* Adjust window end. A null value of last_text_row means that
14427 the window end is in reused rows which in turn means that
14428 only its vpos can have changed. */
14429 if (last_text_row)
14430 {
14431 w->window_end_bytepos
14432 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14433 w->window_end_pos
14434 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14435 w->window_end_vpos
14436 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14437 }
14438 else
14439 {
14440 w->window_end_vpos
14441 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14442 }
14443
14444 w->window_end_valid = Qnil;
14445 w->desired_matrix->no_scrolling_p = 1;
14446
14447 #if GLYPH_DEBUG
14448 debug_method_add (w, "try_window_reusing_current_matrix 2");
14449 #endif
14450 return 1;
14451 }
14452
14453 return 0;
14454 }
14455
14456
14457 \f
14458 /************************************************************************
14459 Window redisplay reusing current matrix when buffer has changed
14460 ************************************************************************/
14461
14462 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14463 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14464 int *, int *));
14465 static struct glyph_row *
14466 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14467 struct glyph_row *));
14468
14469
14470 /* Return the last row in MATRIX displaying text. If row START is
14471 non-null, start searching with that row. IT gives the dimensions
14472 of the display. Value is null if matrix is empty; otherwise it is
14473 a pointer to the row found. */
14474
14475 static struct glyph_row *
14476 find_last_row_displaying_text (matrix, it, start)
14477 struct glyph_matrix *matrix;
14478 struct it *it;
14479 struct glyph_row *start;
14480 {
14481 struct glyph_row *row, *row_found;
14482
14483 /* Set row_found to the last row in IT->w's current matrix
14484 displaying text. The loop looks funny but think of partially
14485 visible lines. */
14486 row_found = NULL;
14487 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14488 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14489 {
14490 xassert (row->enabled_p);
14491 row_found = row;
14492 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14493 break;
14494 ++row;
14495 }
14496
14497 return row_found;
14498 }
14499
14500
14501 /* Return the last row in the current matrix of W that is not affected
14502 by changes at the start of current_buffer that occurred since W's
14503 current matrix was built. Value is null if no such row exists.
14504
14505 BEG_UNCHANGED us the number of characters unchanged at the start of
14506 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14507 first changed character in current_buffer. Characters at positions <
14508 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14509 when the current matrix was built. */
14510
14511 static struct glyph_row *
14512 find_last_unchanged_at_beg_row (w)
14513 struct window *w;
14514 {
14515 int first_changed_pos = BEG + BEG_UNCHANGED;
14516 struct glyph_row *row;
14517 struct glyph_row *row_found = NULL;
14518 int yb = window_text_bottom_y (w);
14519
14520 /* Find the last row displaying unchanged text. */
14521 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14522 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14523 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14524 ++row)
14525 {
14526 if (/* If row ends before first_changed_pos, it is unchanged,
14527 except in some case. */
14528 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14529 /* When row ends in ZV and we write at ZV it is not
14530 unchanged. */
14531 && !row->ends_at_zv_p
14532 /* When first_changed_pos is the end of a continued line,
14533 row is not unchanged because it may be no longer
14534 continued. */
14535 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14536 && (row->continued_p
14537 || row->exact_window_width_line_p)))
14538 row_found = row;
14539
14540 /* Stop if last visible row. */
14541 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14542 break;
14543 }
14544
14545 return row_found;
14546 }
14547
14548
14549 /* Find the first glyph row in the current matrix of W that is not
14550 affected by changes at the end of current_buffer since the
14551 time W's current matrix was built.
14552
14553 Return in *DELTA the number of chars by which buffer positions in
14554 unchanged text at the end of current_buffer must be adjusted.
14555
14556 Return in *DELTA_BYTES the corresponding number of bytes.
14557
14558 Value is null if no such row exists, i.e. all rows are affected by
14559 changes. */
14560
14561 static struct glyph_row *
14562 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14563 struct window *w;
14564 int *delta, *delta_bytes;
14565 {
14566 struct glyph_row *row;
14567 struct glyph_row *row_found = NULL;
14568
14569 *delta = *delta_bytes = 0;
14570
14571 /* Display must not have been paused, otherwise the current matrix
14572 is not up to date. */
14573 eassert (!NILP (w->window_end_valid));
14574
14575 /* A value of window_end_pos >= END_UNCHANGED means that the window
14576 end is in the range of changed text. If so, there is no
14577 unchanged row at the end of W's current matrix. */
14578 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14579 return NULL;
14580
14581 /* Set row to the last row in W's current matrix displaying text. */
14582 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14583
14584 /* If matrix is entirely empty, no unchanged row exists. */
14585 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14586 {
14587 /* The value of row is the last glyph row in the matrix having a
14588 meaningful buffer position in it. The end position of row
14589 corresponds to window_end_pos. This allows us to translate
14590 buffer positions in the current matrix to current buffer
14591 positions for characters not in changed text. */
14592 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14593 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14594 int last_unchanged_pos, last_unchanged_pos_old;
14595 struct glyph_row *first_text_row
14596 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14597
14598 *delta = Z - Z_old;
14599 *delta_bytes = Z_BYTE - Z_BYTE_old;
14600
14601 /* Set last_unchanged_pos to the buffer position of the last
14602 character in the buffer that has not been changed. Z is the
14603 index + 1 of the last character in current_buffer, i.e. by
14604 subtracting END_UNCHANGED we get the index of the last
14605 unchanged character, and we have to add BEG to get its buffer
14606 position. */
14607 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14608 last_unchanged_pos_old = last_unchanged_pos - *delta;
14609
14610 /* Search backward from ROW for a row displaying a line that
14611 starts at a minimum position >= last_unchanged_pos_old. */
14612 for (; row > first_text_row; --row)
14613 {
14614 /* This used to abort, but it can happen.
14615 It is ok to just stop the search instead here. KFS. */
14616 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14617 break;
14618
14619 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14620 row_found = row;
14621 }
14622 }
14623
14624 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14625
14626 return row_found;
14627 }
14628
14629
14630 /* Make sure that glyph rows in the current matrix of window W
14631 reference the same glyph memory as corresponding rows in the
14632 frame's frame matrix. This function is called after scrolling W's
14633 current matrix on a terminal frame in try_window_id and
14634 try_window_reusing_current_matrix. */
14635
14636 static void
14637 sync_frame_with_window_matrix_rows (w)
14638 struct window *w;
14639 {
14640 struct frame *f = XFRAME (w->frame);
14641 struct glyph_row *window_row, *window_row_end, *frame_row;
14642
14643 /* Preconditions: W must be a leaf window and full-width. Its frame
14644 must have a frame matrix. */
14645 xassert (NILP (w->hchild) && NILP (w->vchild));
14646 xassert (WINDOW_FULL_WIDTH_P (w));
14647 xassert (!FRAME_WINDOW_P (f));
14648
14649 /* If W is a full-width window, glyph pointers in W's current matrix
14650 have, by definition, to be the same as glyph pointers in the
14651 corresponding frame matrix. Note that frame matrices have no
14652 marginal areas (see build_frame_matrix). */
14653 window_row = w->current_matrix->rows;
14654 window_row_end = window_row + w->current_matrix->nrows;
14655 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14656 while (window_row < window_row_end)
14657 {
14658 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14659 struct glyph *end = window_row->glyphs[LAST_AREA];
14660
14661 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14662 frame_row->glyphs[TEXT_AREA] = start;
14663 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14664 frame_row->glyphs[LAST_AREA] = end;
14665
14666 /* Disable frame rows whose corresponding window rows have
14667 been disabled in try_window_id. */
14668 if (!window_row->enabled_p)
14669 frame_row->enabled_p = 0;
14670
14671 ++window_row, ++frame_row;
14672 }
14673 }
14674
14675
14676 /* Find the glyph row in window W containing CHARPOS. Consider all
14677 rows between START and END (not inclusive). END null means search
14678 all rows to the end of the display area of W. Value is the row
14679 containing CHARPOS or null. */
14680
14681 struct glyph_row *
14682 row_containing_pos (w, charpos, start, end, dy)
14683 struct window *w;
14684 int charpos;
14685 struct glyph_row *start, *end;
14686 int dy;
14687 {
14688 struct glyph_row *row = start;
14689 int last_y;
14690
14691 /* If we happen to start on a header-line, skip that. */
14692 if (row->mode_line_p)
14693 ++row;
14694
14695 if ((end && row >= end) || !row->enabled_p)
14696 return NULL;
14697
14698 last_y = window_text_bottom_y (w) - dy;
14699
14700 while (1)
14701 {
14702 /* Give up if we have gone too far. */
14703 if (end && row >= end)
14704 return NULL;
14705 /* This formerly returned if they were equal.
14706 I think that both quantities are of a "last plus one" type;
14707 if so, when they are equal, the row is within the screen. -- rms. */
14708 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14709 return NULL;
14710
14711 /* If it is in this row, return this row. */
14712 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14713 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14714 /* The end position of a row equals the start
14715 position of the next row. If CHARPOS is there, we
14716 would rather display it in the next line, except
14717 when this line ends in ZV. */
14718 && !row->ends_at_zv_p
14719 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14720 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14721 return row;
14722 ++row;
14723 }
14724 }
14725
14726
14727 /* Try to redisplay window W by reusing its existing display. W's
14728 current matrix must be up to date when this function is called,
14729 i.e. window_end_valid must not be nil.
14730
14731 Value is
14732
14733 1 if display has been updated
14734 0 if otherwise unsuccessful
14735 -1 if redisplay with same window start is known not to succeed
14736
14737 The following steps are performed:
14738
14739 1. Find the last row in the current matrix of W that is not
14740 affected by changes at the start of current_buffer. If no such row
14741 is found, give up.
14742
14743 2. Find the first row in W's current matrix that is not affected by
14744 changes at the end of current_buffer. Maybe there is no such row.
14745
14746 3. Display lines beginning with the row + 1 found in step 1 to the
14747 row found in step 2 or, if step 2 didn't find a row, to the end of
14748 the window.
14749
14750 4. If cursor is not known to appear on the window, give up.
14751
14752 5. If display stopped at the row found in step 2, scroll the
14753 display and current matrix as needed.
14754
14755 6. Maybe display some lines at the end of W, if we must. This can
14756 happen under various circumstances, like a partially visible line
14757 becoming fully visible, or because newly displayed lines are displayed
14758 in smaller font sizes.
14759
14760 7. Update W's window end information. */
14761
14762 static int
14763 try_window_id (w)
14764 struct window *w;
14765 {
14766 struct frame *f = XFRAME (w->frame);
14767 struct glyph_matrix *current_matrix = w->current_matrix;
14768 struct glyph_matrix *desired_matrix = w->desired_matrix;
14769 struct glyph_row *last_unchanged_at_beg_row;
14770 struct glyph_row *first_unchanged_at_end_row;
14771 struct glyph_row *row;
14772 struct glyph_row *bottom_row;
14773 int bottom_vpos;
14774 struct it it;
14775 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14776 struct text_pos start_pos;
14777 struct run run;
14778 int first_unchanged_at_end_vpos = 0;
14779 struct glyph_row *last_text_row, *last_text_row_at_end;
14780 struct text_pos start;
14781 int first_changed_charpos, last_changed_charpos;
14782
14783 #if GLYPH_DEBUG
14784 if (inhibit_try_window_id)
14785 return 0;
14786 #endif
14787
14788 /* This is handy for debugging. */
14789 #if 0
14790 #define GIVE_UP(X) \
14791 do { \
14792 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14793 return 0; \
14794 } while (0)
14795 #else
14796 #define GIVE_UP(X) return 0
14797 #endif
14798
14799 SET_TEXT_POS_FROM_MARKER (start, w->start);
14800
14801 /* Don't use this for mini-windows because these can show
14802 messages and mini-buffers, and we don't handle that here. */
14803 if (MINI_WINDOW_P (w))
14804 GIVE_UP (1);
14805
14806 /* This flag is used to prevent redisplay optimizations. */
14807 if (windows_or_buffers_changed || cursor_type_changed)
14808 GIVE_UP (2);
14809
14810 /* Verify that narrowing has not changed.
14811 Also verify that we were not told to prevent redisplay optimizations.
14812 It would be nice to further
14813 reduce the number of cases where this prevents try_window_id. */
14814 if (current_buffer->clip_changed
14815 || current_buffer->prevent_redisplay_optimizations_p)
14816 GIVE_UP (3);
14817
14818 /* Window must either use window-based redisplay or be full width. */
14819 if (!FRAME_WINDOW_P (f)
14820 && (!FRAME_LINE_INS_DEL_OK (f)
14821 || !WINDOW_FULL_WIDTH_P (w)))
14822 GIVE_UP (4);
14823
14824 /* Give up if point is known NOT to appear in W. */
14825 if (PT < CHARPOS (start))
14826 GIVE_UP (5);
14827
14828 /* Another way to prevent redisplay optimizations. */
14829 if (XFASTINT (w->last_modified) == 0)
14830 GIVE_UP (6);
14831
14832 /* Verify that window is not hscrolled. */
14833 if (XFASTINT (w->hscroll) != 0)
14834 GIVE_UP (7);
14835
14836 /* Verify that display wasn't paused. */
14837 if (NILP (w->window_end_valid))
14838 GIVE_UP (8);
14839
14840 /* Can't use this if highlighting a region because a cursor movement
14841 will do more than just set the cursor. */
14842 if (!NILP (Vtransient_mark_mode)
14843 && !NILP (current_buffer->mark_active))
14844 GIVE_UP (9);
14845
14846 /* Likewise if highlighting trailing whitespace. */
14847 if (!NILP (Vshow_trailing_whitespace))
14848 GIVE_UP (11);
14849
14850 /* Likewise if showing a region. */
14851 if (!NILP (w->region_showing))
14852 GIVE_UP (10);
14853
14854 /* Can't use this if overlay arrow position and/or string have
14855 changed. */
14856 if (overlay_arrows_changed_p ())
14857 GIVE_UP (12);
14858
14859 /* When word-wrap is on, adding a space to the first word of a
14860 wrapped line can change the wrap position, altering the line
14861 above it. It might be worthwhile to handle this more
14862 intelligently, but for now just redisplay from scratch. */
14863 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14864 GIVE_UP (21);
14865
14866 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14867 only if buffer has really changed. The reason is that the gap is
14868 initially at Z for freshly visited files. The code below would
14869 set end_unchanged to 0 in that case. */
14870 if (MODIFF > SAVE_MODIFF
14871 /* This seems to happen sometimes after saving a buffer. */
14872 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14873 {
14874 if (GPT - BEG < BEG_UNCHANGED)
14875 BEG_UNCHANGED = GPT - BEG;
14876 if (Z - GPT < END_UNCHANGED)
14877 END_UNCHANGED = Z - GPT;
14878 }
14879
14880 /* The position of the first and last character that has been changed. */
14881 first_changed_charpos = BEG + BEG_UNCHANGED;
14882 last_changed_charpos = Z - END_UNCHANGED;
14883
14884 /* If window starts after a line end, and the last change is in
14885 front of that newline, then changes don't affect the display.
14886 This case happens with stealth-fontification. Note that although
14887 the display is unchanged, glyph positions in the matrix have to
14888 be adjusted, of course. */
14889 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14890 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14891 && ((last_changed_charpos < CHARPOS (start)
14892 && CHARPOS (start) == BEGV)
14893 || (last_changed_charpos < CHARPOS (start) - 1
14894 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14895 {
14896 int Z_old, delta, Z_BYTE_old, delta_bytes;
14897 struct glyph_row *r0;
14898
14899 /* Compute how many chars/bytes have been added to or removed
14900 from the buffer. */
14901 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14902 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14903 delta = Z - Z_old;
14904 delta_bytes = Z_BYTE - Z_BYTE_old;
14905
14906 /* Give up if PT is not in the window. Note that it already has
14907 been checked at the start of try_window_id that PT is not in
14908 front of the window start. */
14909 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14910 GIVE_UP (13);
14911
14912 /* If window start is unchanged, we can reuse the whole matrix
14913 as is, after adjusting glyph positions. No need to compute
14914 the window end again, since its offset from Z hasn't changed. */
14915 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14916 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14917 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14918 /* PT must not be in a partially visible line. */
14919 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14920 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14921 {
14922 /* Adjust positions in the glyph matrix. */
14923 if (delta || delta_bytes)
14924 {
14925 struct glyph_row *r1
14926 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14927 increment_matrix_positions (w->current_matrix,
14928 MATRIX_ROW_VPOS (r0, current_matrix),
14929 MATRIX_ROW_VPOS (r1, current_matrix),
14930 delta, delta_bytes);
14931 }
14932
14933 /* Set the cursor. */
14934 row = row_containing_pos (w, PT, r0, NULL, 0);
14935 if (row)
14936 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14937 else
14938 abort ();
14939 return 1;
14940 }
14941 }
14942
14943 /* Handle the case that changes are all below what is displayed in
14944 the window, and that PT is in the window. This shortcut cannot
14945 be taken if ZV is visible in the window, and text has been added
14946 there that is visible in the window. */
14947 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14948 /* ZV is not visible in the window, or there are no
14949 changes at ZV, actually. */
14950 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14951 || first_changed_charpos == last_changed_charpos))
14952 {
14953 struct glyph_row *r0;
14954
14955 /* Give up if PT is not in the window. Note that it already has
14956 been checked at the start of try_window_id that PT is not in
14957 front of the window start. */
14958 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14959 GIVE_UP (14);
14960
14961 /* If window start is unchanged, we can reuse the whole matrix
14962 as is, without changing glyph positions since no text has
14963 been added/removed in front of the window end. */
14964 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14965 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14966 /* PT must not be in a partially visible line. */
14967 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14968 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14969 {
14970 /* We have to compute the window end anew since text
14971 can have been added/removed after it. */
14972 w->window_end_pos
14973 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14974 w->window_end_bytepos
14975 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14976
14977 /* Set the cursor. */
14978 row = row_containing_pos (w, PT, r0, NULL, 0);
14979 if (row)
14980 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14981 else
14982 abort ();
14983 return 2;
14984 }
14985 }
14986
14987 /* Give up if window start is in the changed area.
14988
14989 The condition used to read
14990
14991 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14992
14993 but why that was tested escapes me at the moment. */
14994 if (CHARPOS (start) >= first_changed_charpos
14995 && CHARPOS (start) <= last_changed_charpos)
14996 GIVE_UP (15);
14997
14998 /* Check that window start agrees with the start of the first glyph
14999 row in its current matrix. Check this after we know the window
15000 start is not in changed text, otherwise positions would not be
15001 comparable. */
15002 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15003 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15004 GIVE_UP (16);
15005
15006 /* Give up if the window ends in strings. Overlay strings
15007 at the end are difficult to handle, so don't try. */
15008 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15009 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15010 GIVE_UP (20);
15011
15012 /* Compute the position at which we have to start displaying new
15013 lines. Some of the lines at the top of the window might be
15014 reusable because they are not displaying changed text. Find the
15015 last row in W's current matrix not affected by changes at the
15016 start of current_buffer. Value is null if changes start in the
15017 first line of window. */
15018 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15019 if (last_unchanged_at_beg_row)
15020 {
15021 /* Avoid starting to display in the moddle of a character, a TAB
15022 for instance. This is easier than to set up the iterator
15023 exactly, and it's not a frequent case, so the additional
15024 effort wouldn't really pay off. */
15025 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15026 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15027 && last_unchanged_at_beg_row > w->current_matrix->rows)
15028 --last_unchanged_at_beg_row;
15029
15030 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15031 GIVE_UP (17);
15032
15033 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15034 GIVE_UP (18);
15035 start_pos = it.current.pos;
15036
15037 /* Start displaying new lines in the desired matrix at the same
15038 vpos we would use in the current matrix, i.e. below
15039 last_unchanged_at_beg_row. */
15040 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15041 current_matrix);
15042 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15043 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15044
15045 xassert (it.hpos == 0 && it.current_x == 0);
15046 }
15047 else
15048 {
15049 /* There are no reusable lines at the start of the window.
15050 Start displaying in the first text line. */
15051 start_display (&it, w, start);
15052 it.vpos = it.first_vpos;
15053 start_pos = it.current.pos;
15054 }
15055
15056 /* Find the first row that is not affected by changes at the end of
15057 the buffer. Value will be null if there is no unchanged row, in
15058 which case we must redisplay to the end of the window. delta
15059 will be set to the value by which buffer positions beginning with
15060 first_unchanged_at_end_row have to be adjusted due to text
15061 changes. */
15062 first_unchanged_at_end_row
15063 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15064 IF_DEBUG (debug_delta = delta);
15065 IF_DEBUG (debug_delta_bytes = delta_bytes);
15066
15067 /* Set stop_pos to the buffer position up to which we will have to
15068 display new lines. If first_unchanged_at_end_row != NULL, this
15069 is the buffer position of the start of the line displayed in that
15070 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15071 that we don't stop at a buffer position. */
15072 stop_pos = 0;
15073 if (first_unchanged_at_end_row)
15074 {
15075 xassert (last_unchanged_at_beg_row == NULL
15076 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15077
15078 /* If this is a continuation line, move forward to the next one
15079 that isn't. Changes in lines above affect this line.
15080 Caution: this may move first_unchanged_at_end_row to a row
15081 not displaying text. */
15082 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15083 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15084 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15085 < it.last_visible_y))
15086 ++first_unchanged_at_end_row;
15087
15088 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15089 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15090 >= it.last_visible_y))
15091 first_unchanged_at_end_row = NULL;
15092 else
15093 {
15094 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15095 + delta);
15096 first_unchanged_at_end_vpos
15097 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15098 xassert (stop_pos >= Z - END_UNCHANGED);
15099 }
15100 }
15101 else if (last_unchanged_at_beg_row == NULL)
15102 GIVE_UP (19);
15103
15104
15105 #if GLYPH_DEBUG
15106
15107 /* Either there is no unchanged row at the end, or the one we have
15108 now displays text. This is a necessary condition for the window
15109 end pos calculation at the end of this function. */
15110 xassert (first_unchanged_at_end_row == NULL
15111 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15112
15113 debug_last_unchanged_at_beg_vpos
15114 = (last_unchanged_at_beg_row
15115 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15116 : -1);
15117 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15118
15119 #endif /* GLYPH_DEBUG != 0 */
15120
15121
15122 /* Display new lines. Set last_text_row to the last new line
15123 displayed which has text on it, i.e. might end up as being the
15124 line where the window_end_vpos is. */
15125 w->cursor.vpos = -1;
15126 last_text_row = NULL;
15127 overlay_arrow_seen = 0;
15128 while (it.current_y < it.last_visible_y
15129 && !fonts_changed_p
15130 && (first_unchanged_at_end_row == NULL
15131 || IT_CHARPOS (it) < stop_pos))
15132 {
15133 if (display_line (&it))
15134 last_text_row = it.glyph_row - 1;
15135 }
15136
15137 if (fonts_changed_p)
15138 return -1;
15139
15140
15141 /* Compute differences in buffer positions, y-positions etc. for
15142 lines reused at the bottom of the window. Compute what we can
15143 scroll. */
15144 if (first_unchanged_at_end_row
15145 /* No lines reused because we displayed everything up to the
15146 bottom of the window. */
15147 && it.current_y < it.last_visible_y)
15148 {
15149 dvpos = (it.vpos
15150 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15151 current_matrix));
15152 dy = it.current_y - first_unchanged_at_end_row->y;
15153 run.current_y = first_unchanged_at_end_row->y;
15154 run.desired_y = run.current_y + dy;
15155 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15156 }
15157 else
15158 {
15159 delta = delta_bytes = dvpos = dy
15160 = run.current_y = run.desired_y = run.height = 0;
15161 first_unchanged_at_end_row = NULL;
15162 }
15163 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15164
15165
15166 /* Find the cursor if not already found. We have to decide whether
15167 PT will appear on this window (it sometimes doesn't, but this is
15168 not a very frequent case.) This decision has to be made before
15169 the current matrix is altered. A value of cursor.vpos < 0 means
15170 that PT is either in one of the lines beginning at
15171 first_unchanged_at_end_row or below the window. Don't care for
15172 lines that might be displayed later at the window end; as
15173 mentioned, this is not a frequent case. */
15174 if (w->cursor.vpos < 0)
15175 {
15176 /* Cursor in unchanged rows at the top? */
15177 if (PT < CHARPOS (start_pos)
15178 && last_unchanged_at_beg_row)
15179 {
15180 row = row_containing_pos (w, PT,
15181 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15182 last_unchanged_at_beg_row + 1, 0);
15183 if (row)
15184 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15185 }
15186
15187 /* Start from first_unchanged_at_end_row looking for PT. */
15188 else if (first_unchanged_at_end_row)
15189 {
15190 row = row_containing_pos (w, PT - delta,
15191 first_unchanged_at_end_row, NULL, 0);
15192 if (row)
15193 set_cursor_from_row (w, row, w->current_matrix, delta,
15194 delta_bytes, dy, dvpos);
15195 }
15196
15197 /* Give up if cursor was not found. */
15198 if (w->cursor.vpos < 0)
15199 {
15200 clear_glyph_matrix (w->desired_matrix);
15201 return -1;
15202 }
15203 }
15204
15205 /* Don't let the cursor end in the scroll margins. */
15206 {
15207 int this_scroll_margin, cursor_height;
15208
15209 this_scroll_margin = max (0, scroll_margin);
15210 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15211 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15212 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15213
15214 if ((w->cursor.y < this_scroll_margin
15215 && CHARPOS (start) > BEGV)
15216 /* Old redisplay didn't take scroll margin into account at the bottom,
15217 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15218 || (w->cursor.y + (make_cursor_line_fully_visible_p
15219 ? cursor_height + this_scroll_margin
15220 : 1)) > it.last_visible_y)
15221 {
15222 w->cursor.vpos = -1;
15223 clear_glyph_matrix (w->desired_matrix);
15224 return -1;
15225 }
15226 }
15227
15228 /* Scroll the display. Do it before changing the current matrix so
15229 that xterm.c doesn't get confused about where the cursor glyph is
15230 found. */
15231 if (dy && run.height)
15232 {
15233 update_begin (f);
15234
15235 if (FRAME_WINDOW_P (f))
15236 {
15237 FRAME_RIF (f)->update_window_begin_hook (w);
15238 FRAME_RIF (f)->clear_window_mouse_face (w);
15239 FRAME_RIF (f)->scroll_run_hook (w, &run);
15240 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15241 }
15242 else
15243 {
15244 /* Terminal frame. In this case, dvpos gives the number of
15245 lines to scroll by; dvpos < 0 means scroll up. */
15246 int first_unchanged_at_end_vpos
15247 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15248 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15249 int end = (WINDOW_TOP_EDGE_LINE (w)
15250 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15251 + window_internal_height (w));
15252
15253 /* Perform the operation on the screen. */
15254 if (dvpos > 0)
15255 {
15256 /* Scroll last_unchanged_at_beg_row to the end of the
15257 window down dvpos lines. */
15258 set_terminal_window (f, end);
15259
15260 /* On dumb terminals delete dvpos lines at the end
15261 before inserting dvpos empty lines. */
15262 if (!FRAME_SCROLL_REGION_OK (f))
15263 ins_del_lines (f, end - dvpos, -dvpos);
15264
15265 /* Insert dvpos empty lines in front of
15266 last_unchanged_at_beg_row. */
15267 ins_del_lines (f, from, dvpos);
15268 }
15269 else if (dvpos < 0)
15270 {
15271 /* Scroll up last_unchanged_at_beg_vpos to the end of
15272 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15273 set_terminal_window (f, end);
15274
15275 /* Delete dvpos lines in front of
15276 last_unchanged_at_beg_vpos. ins_del_lines will set
15277 the cursor to the given vpos and emit |dvpos| delete
15278 line sequences. */
15279 ins_del_lines (f, from + dvpos, dvpos);
15280
15281 /* On a dumb terminal insert dvpos empty lines at the
15282 end. */
15283 if (!FRAME_SCROLL_REGION_OK (f))
15284 ins_del_lines (f, end + dvpos, -dvpos);
15285 }
15286
15287 set_terminal_window (f, 0);
15288 }
15289
15290 update_end (f);
15291 }
15292
15293 /* Shift reused rows of the current matrix to the right position.
15294 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15295 text. */
15296 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15297 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15298 if (dvpos < 0)
15299 {
15300 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15301 bottom_vpos, dvpos);
15302 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15303 bottom_vpos, 0);
15304 }
15305 else if (dvpos > 0)
15306 {
15307 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15308 bottom_vpos, dvpos);
15309 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15310 first_unchanged_at_end_vpos + dvpos, 0);
15311 }
15312
15313 /* For frame-based redisplay, make sure that current frame and window
15314 matrix are in sync with respect to glyph memory. */
15315 if (!FRAME_WINDOW_P (f))
15316 sync_frame_with_window_matrix_rows (w);
15317
15318 /* Adjust buffer positions in reused rows. */
15319 if (delta || delta_bytes)
15320 increment_matrix_positions (current_matrix,
15321 first_unchanged_at_end_vpos + dvpos,
15322 bottom_vpos, delta, delta_bytes);
15323
15324 /* Adjust Y positions. */
15325 if (dy)
15326 shift_glyph_matrix (w, current_matrix,
15327 first_unchanged_at_end_vpos + dvpos,
15328 bottom_vpos, dy);
15329
15330 if (first_unchanged_at_end_row)
15331 {
15332 first_unchanged_at_end_row += dvpos;
15333 if (first_unchanged_at_end_row->y >= it.last_visible_y
15334 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15335 first_unchanged_at_end_row = NULL;
15336 }
15337
15338 /* If scrolling up, there may be some lines to display at the end of
15339 the window. */
15340 last_text_row_at_end = NULL;
15341 if (dy < 0)
15342 {
15343 /* Scrolling up can leave for example a partially visible line
15344 at the end of the window to be redisplayed. */
15345 /* Set last_row to the glyph row in the current matrix where the
15346 window end line is found. It has been moved up or down in
15347 the matrix by dvpos. */
15348 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15349 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15350
15351 /* If last_row is the window end line, it should display text. */
15352 xassert (last_row->displays_text_p);
15353
15354 /* If window end line was partially visible before, begin
15355 displaying at that line. Otherwise begin displaying with the
15356 line following it. */
15357 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15358 {
15359 init_to_row_start (&it, w, last_row);
15360 it.vpos = last_vpos;
15361 it.current_y = last_row->y;
15362 }
15363 else
15364 {
15365 init_to_row_end (&it, w, last_row);
15366 it.vpos = 1 + last_vpos;
15367 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15368 ++last_row;
15369 }
15370
15371 /* We may start in a continuation line. If so, we have to
15372 get the right continuation_lines_width and current_x. */
15373 it.continuation_lines_width = last_row->continuation_lines_width;
15374 it.hpos = it.current_x = 0;
15375
15376 /* Display the rest of the lines at the window end. */
15377 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15378 while (it.current_y < it.last_visible_y
15379 && !fonts_changed_p)
15380 {
15381 /* Is it always sure that the display agrees with lines in
15382 the current matrix? I don't think so, so we mark rows
15383 displayed invalid in the current matrix by setting their
15384 enabled_p flag to zero. */
15385 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15386 if (display_line (&it))
15387 last_text_row_at_end = it.glyph_row - 1;
15388 }
15389 }
15390
15391 /* Update window_end_pos and window_end_vpos. */
15392 if (first_unchanged_at_end_row
15393 && !last_text_row_at_end)
15394 {
15395 /* Window end line if one of the preserved rows from the current
15396 matrix. Set row to the last row displaying text in current
15397 matrix starting at first_unchanged_at_end_row, after
15398 scrolling. */
15399 xassert (first_unchanged_at_end_row->displays_text_p);
15400 row = find_last_row_displaying_text (w->current_matrix, &it,
15401 first_unchanged_at_end_row);
15402 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15403
15404 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15405 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15406 w->window_end_vpos
15407 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15408 xassert (w->window_end_bytepos >= 0);
15409 IF_DEBUG (debug_method_add (w, "A"));
15410 }
15411 else if (last_text_row_at_end)
15412 {
15413 w->window_end_pos
15414 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15415 w->window_end_bytepos
15416 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15417 w->window_end_vpos
15418 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15419 xassert (w->window_end_bytepos >= 0);
15420 IF_DEBUG (debug_method_add (w, "B"));
15421 }
15422 else if (last_text_row)
15423 {
15424 /* We have displayed either to the end of the window or at the
15425 end of the window, i.e. the last row with text is to be found
15426 in the desired matrix. */
15427 w->window_end_pos
15428 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15429 w->window_end_bytepos
15430 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15431 w->window_end_vpos
15432 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15433 xassert (w->window_end_bytepos >= 0);
15434 }
15435 else if (first_unchanged_at_end_row == NULL
15436 && last_text_row == NULL
15437 && last_text_row_at_end == NULL)
15438 {
15439 /* Displayed to end of window, but no line containing text was
15440 displayed. Lines were deleted at the end of the window. */
15441 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15442 int vpos = XFASTINT (w->window_end_vpos);
15443 struct glyph_row *current_row = current_matrix->rows + vpos;
15444 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15445
15446 for (row = NULL;
15447 row == NULL && vpos >= first_vpos;
15448 --vpos, --current_row, --desired_row)
15449 {
15450 if (desired_row->enabled_p)
15451 {
15452 if (desired_row->displays_text_p)
15453 row = desired_row;
15454 }
15455 else if (current_row->displays_text_p)
15456 row = current_row;
15457 }
15458
15459 xassert (row != NULL);
15460 w->window_end_vpos = make_number (vpos + 1);
15461 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15462 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15463 xassert (w->window_end_bytepos >= 0);
15464 IF_DEBUG (debug_method_add (w, "C"));
15465 }
15466 else
15467 abort ();
15468
15469 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15470 debug_end_vpos = XFASTINT (w->window_end_vpos));
15471
15472 /* Record that display has not been completed. */
15473 w->window_end_valid = Qnil;
15474 w->desired_matrix->no_scrolling_p = 1;
15475 return 3;
15476
15477 #undef GIVE_UP
15478 }
15479
15480
15481 \f
15482 /***********************************************************************
15483 More debugging support
15484 ***********************************************************************/
15485
15486 #if GLYPH_DEBUG
15487
15488 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15489 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15490 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15491
15492
15493 /* Dump the contents of glyph matrix MATRIX on stderr.
15494
15495 GLYPHS 0 means don't show glyph contents.
15496 GLYPHS 1 means show glyphs in short form
15497 GLYPHS > 1 means show glyphs in long form. */
15498
15499 void
15500 dump_glyph_matrix (matrix, glyphs)
15501 struct glyph_matrix *matrix;
15502 int glyphs;
15503 {
15504 int i;
15505 for (i = 0; i < matrix->nrows; ++i)
15506 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15507 }
15508
15509
15510 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15511 the glyph row and area where the glyph comes from. */
15512
15513 void
15514 dump_glyph (row, glyph, area)
15515 struct glyph_row *row;
15516 struct glyph *glyph;
15517 int area;
15518 {
15519 if (glyph->type == CHAR_GLYPH)
15520 {
15521 fprintf (stderr,
15522 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15523 glyph - row->glyphs[TEXT_AREA],
15524 'C',
15525 glyph->charpos,
15526 (BUFFERP (glyph->object)
15527 ? 'B'
15528 : (STRINGP (glyph->object)
15529 ? 'S'
15530 : '-')),
15531 glyph->pixel_width,
15532 glyph->u.ch,
15533 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15534 ? glyph->u.ch
15535 : '.'),
15536 glyph->face_id,
15537 glyph->left_box_line_p,
15538 glyph->right_box_line_p);
15539 }
15540 else if (glyph->type == STRETCH_GLYPH)
15541 {
15542 fprintf (stderr,
15543 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15544 glyph - row->glyphs[TEXT_AREA],
15545 'S',
15546 glyph->charpos,
15547 (BUFFERP (glyph->object)
15548 ? 'B'
15549 : (STRINGP (glyph->object)
15550 ? 'S'
15551 : '-')),
15552 glyph->pixel_width,
15553 0,
15554 '.',
15555 glyph->face_id,
15556 glyph->left_box_line_p,
15557 glyph->right_box_line_p);
15558 }
15559 else if (glyph->type == IMAGE_GLYPH)
15560 {
15561 fprintf (stderr,
15562 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15563 glyph - row->glyphs[TEXT_AREA],
15564 'I',
15565 glyph->charpos,
15566 (BUFFERP (glyph->object)
15567 ? 'B'
15568 : (STRINGP (glyph->object)
15569 ? 'S'
15570 : '-')),
15571 glyph->pixel_width,
15572 glyph->u.img_id,
15573 '.',
15574 glyph->face_id,
15575 glyph->left_box_line_p,
15576 glyph->right_box_line_p);
15577 }
15578 else if (glyph->type == COMPOSITE_GLYPH)
15579 {
15580 fprintf (stderr,
15581 " %5d %4c %6d %c %3d 0x%05x",
15582 glyph - row->glyphs[TEXT_AREA],
15583 '+',
15584 glyph->charpos,
15585 (BUFFERP (glyph->object)
15586 ? 'B'
15587 : (STRINGP (glyph->object)
15588 ? 'S'
15589 : '-')),
15590 glyph->pixel_width,
15591 glyph->u.cmp.id);
15592 if (glyph->u.cmp.automatic)
15593 fprintf (stderr,
15594 "[%d-%d]",
15595 glyph->u.cmp.from, glyph->u.cmp.to);
15596 fprintf (stderr, " . %4d %1.1d%1.1d\n",
15597 glyph->face_id,
15598 glyph->left_box_line_p,
15599 glyph->right_box_line_p);
15600 }
15601 }
15602
15603
15604 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15605 GLYPHS 0 means don't show glyph contents.
15606 GLYPHS 1 means show glyphs in short form
15607 GLYPHS > 1 means show glyphs in long form. */
15608
15609 void
15610 dump_glyph_row (row, vpos, glyphs)
15611 struct glyph_row *row;
15612 int vpos, glyphs;
15613 {
15614 if (glyphs != 1)
15615 {
15616 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15617 fprintf (stderr, "======================================================================\n");
15618
15619 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15620 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15621 vpos,
15622 MATRIX_ROW_START_CHARPOS (row),
15623 MATRIX_ROW_END_CHARPOS (row),
15624 row->used[TEXT_AREA],
15625 row->contains_overlapping_glyphs_p,
15626 row->enabled_p,
15627 row->truncated_on_left_p,
15628 row->truncated_on_right_p,
15629 row->continued_p,
15630 MATRIX_ROW_CONTINUATION_LINE_P (row),
15631 row->displays_text_p,
15632 row->ends_at_zv_p,
15633 row->fill_line_p,
15634 row->ends_in_middle_of_char_p,
15635 row->starts_in_middle_of_char_p,
15636 row->mouse_face_p,
15637 row->x,
15638 row->y,
15639 row->pixel_width,
15640 row->height,
15641 row->visible_height,
15642 row->ascent,
15643 row->phys_ascent);
15644 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15645 row->end.overlay_string_index,
15646 row->continuation_lines_width);
15647 fprintf (stderr, "%9d %5d\n",
15648 CHARPOS (row->start.string_pos),
15649 CHARPOS (row->end.string_pos));
15650 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15651 row->end.dpvec_index);
15652 }
15653
15654 if (glyphs > 1)
15655 {
15656 int area;
15657
15658 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15659 {
15660 struct glyph *glyph = row->glyphs[area];
15661 struct glyph *glyph_end = glyph + row->used[area];
15662
15663 /* Glyph for a line end in text. */
15664 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15665 ++glyph_end;
15666
15667 if (glyph < glyph_end)
15668 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15669
15670 for (; glyph < glyph_end; ++glyph)
15671 dump_glyph (row, glyph, area);
15672 }
15673 }
15674 else if (glyphs == 1)
15675 {
15676 int area;
15677
15678 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15679 {
15680 char *s = (char *) alloca (row->used[area] + 1);
15681 int i;
15682
15683 for (i = 0; i < row->used[area]; ++i)
15684 {
15685 struct glyph *glyph = row->glyphs[area] + i;
15686 if (glyph->type == CHAR_GLYPH
15687 && glyph->u.ch < 0x80
15688 && glyph->u.ch >= ' ')
15689 s[i] = glyph->u.ch;
15690 else
15691 s[i] = '.';
15692 }
15693
15694 s[i] = '\0';
15695 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15696 }
15697 }
15698 }
15699
15700
15701 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15702 Sdump_glyph_matrix, 0, 1, "p",
15703 doc: /* Dump the current matrix of the selected window to stderr.
15704 Shows contents of glyph row structures. With non-nil
15705 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15706 glyphs in short form, otherwise show glyphs in long form. */)
15707 (glyphs)
15708 Lisp_Object glyphs;
15709 {
15710 struct window *w = XWINDOW (selected_window);
15711 struct buffer *buffer = XBUFFER (w->buffer);
15712
15713 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15714 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15715 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15716 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15717 fprintf (stderr, "=============================================\n");
15718 dump_glyph_matrix (w->current_matrix,
15719 NILP (glyphs) ? 0 : XINT (glyphs));
15720 return Qnil;
15721 }
15722
15723
15724 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15725 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15726 ()
15727 {
15728 struct frame *f = XFRAME (selected_frame);
15729 dump_glyph_matrix (f->current_matrix, 1);
15730 return Qnil;
15731 }
15732
15733
15734 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15735 doc: /* Dump glyph row ROW to stderr.
15736 GLYPH 0 means don't dump glyphs.
15737 GLYPH 1 means dump glyphs in short form.
15738 GLYPH > 1 or omitted means dump glyphs in long form. */)
15739 (row, glyphs)
15740 Lisp_Object row, glyphs;
15741 {
15742 struct glyph_matrix *matrix;
15743 int vpos;
15744
15745 CHECK_NUMBER (row);
15746 matrix = XWINDOW (selected_window)->current_matrix;
15747 vpos = XINT (row);
15748 if (vpos >= 0 && vpos < matrix->nrows)
15749 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15750 vpos,
15751 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15752 return Qnil;
15753 }
15754
15755
15756 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15757 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15758 GLYPH 0 means don't dump glyphs.
15759 GLYPH 1 means dump glyphs in short form.
15760 GLYPH > 1 or omitted means dump glyphs in long form. */)
15761 (row, glyphs)
15762 Lisp_Object row, glyphs;
15763 {
15764 struct frame *sf = SELECTED_FRAME ();
15765 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15766 int vpos;
15767
15768 CHECK_NUMBER (row);
15769 vpos = XINT (row);
15770 if (vpos >= 0 && vpos < m->nrows)
15771 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15772 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15773 return Qnil;
15774 }
15775
15776
15777 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15778 doc: /* Toggle tracing of redisplay.
15779 With ARG, turn tracing on if and only if ARG is positive. */)
15780 (arg)
15781 Lisp_Object arg;
15782 {
15783 if (NILP (arg))
15784 trace_redisplay_p = !trace_redisplay_p;
15785 else
15786 {
15787 arg = Fprefix_numeric_value (arg);
15788 trace_redisplay_p = XINT (arg) > 0;
15789 }
15790
15791 return Qnil;
15792 }
15793
15794
15795 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15796 doc: /* Like `format', but print result to stderr.
15797 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15798 (nargs, args)
15799 int nargs;
15800 Lisp_Object *args;
15801 {
15802 Lisp_Object s = Fformat (nargs, args);
15803 fprintf (stderr, "%s", SDATA (s));
15804 return Qnil;
15805 }
15806
15807 #endif /* GLYPH_DEBUG */
15808
15809
15810 \f
15811 /***********************************************************************
15812 Building Desired Matrix Rows
15813 ***********************************************************************/
15814
15815 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15816 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15817
15818 static struct glyph_row *
15819 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15820 struct window *w;
15821 Lisp_Object overlay_arrow_string;
15822 {
15823 struct frame *f = XFRAME (WINDOW_FRAME (w));
15824 struct buffer *buffer = XBUFFER (w->buffer);
15825 struct buffer *old = current_buffer;
15826 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15827 int arrow_len = SCHARS (overlay_arrow_string);
15828 const unsigned char *arrow_end = arrow_string + arrow_len;
15829 const unsigned char *p;
15830 struct it it;
15831 int multibyte_p;
15832 int n_glyphs_before;
15833
15834 set_buffer_temp (buffer);
15835 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15836 it.glyph_row->used[TEXT_AREA] = 0;
15837 SET_TEXT_POS (it.position, 0, 0);
15838
15839 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15840 p = arrow_string;
15841 while (p < arrow_end)
15842 {
15843 Lisp_Object face, ilisp;
15844
15845 /* Get the next character. */
15846 if (multibyte_p)
15847 it.c = it.char_to_display = string_char_and_length (p, &it.len);
15848 else
15849 {
15850 it.c = it.char_to_display = *p, it.len = 1;
15851 if (! ASCII_CHAR_P (it.c))
15852 it.char_to_display = BYTE8_TO_CHAR (it.c);
15853 }
15854 p += it.len;
15855
15856 /* Get its face. */
15857 ilisp = make_number (p - arrow_string);
15858 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15859 it.face_id = compute_char_face (f, it.char_to_display, face);
15860
15861 /* Compute its width, get its glyphs. */
15862 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15863 SET_TEXT_POS (it.position, -1, -1);
15864 PRODUCE_GLYPHS (&it);
15865
15866 /* If this character doesn't fit any more in the line, we have
15867 to remove some glyphs. */
15868 if (it.current_x > it.last_visible_x)
15869 {
15870 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15871 break;
15872 }
15873 }
15874
15875 set_buffer_temp (old);
15876 return it.glyph_row;
15877 }
15878
15879
15880 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15881 glyphs are only inserted for terminal frames since we can't really
15882 win with truncation glyphs when partially visible glyphs are
15883 involved. Which glyphs to insert is determined by
15884 produce_special_glyphs. */
15885
15886 static void
15887 insert_left_trunc_glyphs (it)
15888 struct it *it;
15889 {
15890 struct it truncate_it;
15891 struct glyph *from, *end, *to, *toend;
15892
15893 xassert (!FRAME_WINDOW_P (it->f));
15894
15895 /* Get the truncation glyphs. */
15896 truncate_it = *it;
15897 truncate_it.current_x = 0;
15898 truncate_it.face_id = DEFAULT_FACE_ID;
15899 truncate_it.glyph_row = &scratch_glyph_row;
15900 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15901 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15902 truncate_it.object = make_number (0);
15903 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15904
15905 /* Overwrite glyphs from IT with truncation glyphs. */
15906 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15907 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15908 to = it->glyph_row->glyphs[TEXT_AREA];
15909 toend = to + it->glyph_row->used[TEXT_AREA];
15910
15911 while (from < end)
15912 *to++ = *from++;
15913
15914 /* There may be padding glyphs left over. Overwrite them too. */
15915 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15916 {
15917 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15918 while (from < end)
15919 *to++ = *from++;
15920 }
15921
15922 if (to > toend)
15923 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15924 }
15925
15926
15927 /* Compute the pixel height and width of IT->glyph_row.
15928
15929 Most of the time, ascent and height of a display line will be equal
15930 to the max_ascent and max_height values of the display iterator
15931 structure. This is not the case if
15932
15933 1. We hit ZV without displaying anything. In this case, max_ascent
15934 and max_height will be zero.
15935
15936 2. We have some glyphs that don't contribute to the line height.
15937 (The glyph row flag contributes_to_line_height_p is for future
15938 pixmap extensions).
15939
15940 The first case is easily covered by using default values because in
15941 these cases, the line height does not really matter, except that it
15942 must not be zero. */
15943
15944 static void
15945 compute_line_metrics (it)
15946 struct it *it;
15947 {
15948 struct glyph_row *row = it->glyph_row;
15949 int area, i;
15950
15951 if (FRAME_WINDOW_P (it->f))
15952 {
15953 int i, min_y, max_y;
15954
15955 /* The line may consist of one space only, that was added to
15956 place the cursor on it. If so, the row's height hasn't been
15957 computed yet. */
15958 if (row->height == 0)
15959 {
15960 if (it->max_ascent + it->max_descent == 0)
15961 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15962 row->ascent = it->max_ascent;
15963 row->height = it->max_ascent + it->max_descent;
15964 row->phys_ascent = it->max_phys_ascent;
15965 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15966 row->extra_line_spacing = it->max_extra_line_spacing;
15967 }
15968
15969 /* Compute the width of this line. */
15970 row->pixel_width = row->x;
15971 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15972 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15973
15974 xassert (row->pixel_width >= 0);
15975 xassert (row->ascent >= 0 && row->height > 0);
15976
15977 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15978 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15979
15980 /* If first line's physical ascent is larger than its logical
15981 ascent, use the physical ascent, and make the row taller.
15982 This makes accented characters fully visible. */
15983 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15984 && row->phys_ascent > row->ascent)
15985 {
15986 row->height += row->phys_ascent - row->ascent;
15987 row->ascent = row->phys_ascent;
15988 }
15989
15990 /* Compute how much of the line is visible. */
15991 row->visible_height = row->height;
15992
15993 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15994 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15995
15996 if (row->y < min_y)
15997 row->visible_height -= min_y - row->y;
15998 if (row->y + row->height > max_y)
15999 row->visible_height -= row->y + row->height - max_y;
16000 }
16001 else
16002 {
16003 row->pixel_width = row->used[TEXT_AREA];
16004 if (row->continued_p)
16005 row->pixel_width -= it->continuation_pixel_width;
16006 else if (row->truncated_on_right_p)
16007 row->pixel_width -= it->truncation_pixel_width;
16008 row->ascent = row->phys_ascent = 0;
16009 row->height = row->phys_height = row->visible_height = 1;
16010 row->extra_line_spacing = 0;
16011 }
16012
16013 /* Compute a hash code for this row. */
16014 row->hash = 0;
16015 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16016 for (i = 0; i < row->used[area]; ++i)
16017 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16018 + row->glyphs[area][i].u.val
16019 + row->glyphs[area][i].face_id
16020 + row->glyphs[area][i].padding_p
16021 + (row->glyphs[area][i].type << 2));
16022
16023 it->max_ascent = it->max_descent = 0;
16024 it->max_phys_ascent = it->max_phys_descent = 0;
16025 }
16026
16027
16028 /* Append one space to the glyph row of iterator IT if doing a
16029 window-based redisplay. The space has the same face as
16030 IT->face_id. Value is non-zero if a space was added.
16031
16032 This function is called to make sure that there is always one glyph
16033 at the end of a glyph row that the cursor can be set on under
16034 window-systems. (If there weren't such a glyph we would not know
16035 how wide and tall a box cursor should be displayed).
16036
16037 At the same time this space let's a nicely handle clearing to the
16038 end of the line if the row ends in italic text. */
16039
16040 static int
16041 append_space_for_newline (it, default_face_p)
16042 struct it *it;
16043 int default_face_p;
16044 {
16045 if (FRAME_WINDOW_P (it->f))
16046 {
16047 int n = it->glyph_row->used[TEXT_AREA];
16048
16049 if (it->glyph_row->glyphs[TEXT_AREA] + n
16050 < it->glyph_row->glyphs[1 + TEXT_AREA])
16051 {
16052 /* Save some values that must not be changed.
16053 Must save IT->c and IT->len because otherwise
16054 ITERATOR_AT_END_P wouldn't work anymore after
16055 append_space_for_newline has been called. */
16056 enum display_element_type saved_what = it->what;
16057 int saved_c = it->c, saved_len = it->len;
16058 int saved_char_to_display = it->char_to_display;
16059 int saved_x = it->current_x;
16060 int saved_face_id = it->face_id;
16061 struct text_pos saved_pos;
16062 Lisp_Object saved_object;
16063 struct face *face;
16064
16065 saved_object = it->object;
16066 saved_pos = it->position;
16067
16068 it->what = IT_CHARACTER;
16069 bzero (&it->position, sizeof it->position);
16070 it->object = make_number (0);
16071 it->c = it->char_to_display = ' ';
16072 it->len = 1;
16073
16074 if (default_face_p)
16075 it->face_id = DEFAULT_FACE_ID;
16076 else if (it->face_before_selective_p)
16077 it->face_id = it->saved_face_id;
16078 face = FACE_FROM_ID (it->f, it->face_id);
16079 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16080
16081 PRODUCE_GLYPHS (it);
16082
16083 it->override_ascent = -1;
16084 it->constrain_row_ascent_descent_p = 0;
16085 it->current_x = saved_x;
16086 it->object = saved_object;
16087 it->position = saved_pos;
16088 it->what = saved_what;
16089 it->face_id = saved_face_id;
16090 it->len = saved_len;
16091 it->c = saved_c;
16092 it->char_to_display = saved_char_to_display;
16093 return 1;
16094 }
16095 }
16096
16097 return 0;
16098 }
16099
16100
16101 /* Extend the face of the last glyph in the text area of IT->glyph_row
16102 to the end of the display line. Called from display_line.
16103 If the glyph row is empty, add a space glyph to it so that we
16104 know the face to draw. Set the glyph row flag fill_line_p. */
16105
16106 static void
16107 extend_face_to_end_of_line (it)
16108 struct it *it;
16109 {
16110 struct face *face;
16111 struct frame *f = it->f;
16112
16113 /* If line is already filled, do nothing. */
16114 if (it->current_x >= it->last_visible_x)
16115 return;
16116
16117 /* Face extension extends the background and box of IT->face_id
16118 to the end of the line. If the background equals the background
16119 of the frame, we don't have to do anything. */
16120 if (it->face_before_selective_p)
16121 face = FACE_FROM_ID (it->f, it->saved_face_id);
16122 else
16123 face = FACE_FROM_ID (f, it->face_id);
16124
16125 if (FRAME_WINDOW_P (f)
16126 && it->glyph_row->displays_text_p
16127 && face->box == FACE_NO_BOX
16128 && face->background == FRAME_BACKGROUND_PIXEL (f)
16129 && !face->stipple)
16130 return;
16131
16132 /* Set the glyph row flag indicating that the face of the last glyph
16133 in the text area has to be drawn to the end of the text area. */
16134 it->glyph_row->fill_line_p = 1;
16135
16136 /* If current character of IT is not ASCII, make sure we have the
16137 ASCII face. This will be automatically undone the next time
16138 get_next_display_element returns a multibyte character. Note
16139 that the character will always be single byte in unibyte
16140 text. */
16141 if (!ASCII_CHAR_P (it->c))
16142 {
16143 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16144 }
16145
16146 if (FRAME_WINDOW_P (f))
16147 {
16148 /* If the row is empty, add a space with the current face of IT,
16149 so that we know which face to draw. */
16150 if (it->glyph_row->used[TEXT_AREA] == 0)
16151 {
16152 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16153 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16154 it->glyph_row->used[TEXT_AREA] = 1;
16155 }
16156 }
16157 else
16158 {
16159 /* Save some values that must not be changed. */
16160 int saved_x = it->current_x;
16161 struct text_pos saved_pos;
16162 Lisp_Object saved_object;
16163 enum display_element_type saved_what = it->what;
16164 int saved_face_id = it->face_id;
16165
16166 saved_object = it->object;
16167 saved_pos = it->position;
16168
16169 it->what = IT_CHARACTER;
16170 bzero (&it->position, sizeof it->position);
16171 it->object = make_number (0);
16172 it->c = it->char_to_display = ' ';
16173 it->len = 1;
16174 it->face_id = face->id;
16175
16176 PRODUCE_GLYPHS (it);
16177
16178 while (it->current_x <= it->last_visible_x)
16179 PRODUCE_GLYPHS (it);
16180
16181 /* Don't count these blanks really. It would let us insert a left
16182 truncation glyph below and make us set the cursor on them, maybe. */
16183 it->current_x = saved_x;
16184 it->object = saved_object;
16185 it->position = saved_pos;
16186 it->what = saved_what;
16187 it->face_id = saved_face_id;
16188 }
16189 }
16190
16191
16192 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16193 trailing whitespace. */
16194
16195 static int
16196 trailing_whitespace_p (charpos)
16197 int charpos;
16198 {
16199 int bytepos = CHAR_TO_BYTE (charpos);
16200 int c = 0;
16201
16202 while (bytepos < ZV_BYTE
16203 && (c = FETCH_CHAR (bytepos),
16204 c == ' ' || c == '\t'))
16205 ++bytepos;
16206
16207 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16208 {
16209 if (bytepos != PT_BYTE)
16210 return 1;
16211 }
16212 return 0;
16213 }
16214
16215
16216 /* Highlight trailing whitespace, if any, in ROW. */
16217
16218 void
16219 highlight_trailing_whitespace (f, row)
16220 struct frame *f;
16221 struct glyph_row *row;
16222 {
16223 int used = row->used[TEXT_AREA];
16224
16225 if (used)
16226 {
16227 struct glyph *start = row->glyphs[TEXT_AREA];
16228 struct glyph *glyph = start + used - 1;
16229
16230 /* Skip over glyphs inserted to display the cursor at the
16231 end of a line, for extending the face of the last glyph
16232 to the end of the line on terminals, and for truncation
16233 and continuation glyphs. */
16234 while (glyph >= start
16235 && glyph->type == CHAR_GLYPH
16236 && INTEGERP (glyph->object))
16237 --glyph;
16238
16239 /* If last glyph is a space or stretch, and it's trailing
16240 whitespace, set the face of all trailing whitespace glyphs in
16241 IT->glyph_row to `trailing-whitespace'. */
16242 if (glyph >= start
16243 && BUFFERP (glyph->object)
16244 && (glyph->type == STRETCH_GLYPH
16245 || (glyph->type == CHAR_GLYPH
16246 && glyph->u.ch == ' '))
16247 && trailing_whitespace_p (glyph->charpos))
16248 {
16249 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16250 if (face_id < 0)
16251 return;
16252
16253 while (glyph >= start
16254 && BUFFERP (glyph->object)
16255 && (glyph->type == STRETCH_GLYPH
16256 || (glyph->type == CHAR_GLYPH
16257 && glyph->u.ch == ' ')))
16258 (glyph--)->face_id = face_id;
16259 }
16260 }
16261 }
16262
16263
16264 /* Value is non-zero if glyph row ROW in window W should be
16265 used to hold the cursor. */
16266
16267 static int
16268 cursor_row_p (w, row)
16269 struct window *w;
16270 struct glyph_row *row;
16271 {
16272 int cursor_row_p = 1;
16273
16274 if (PT == MATRIX_ROW_END_CHARPOS (row))
16275 {
16276 /* Suppose the row ends on a string.
16277 Unless the row is continued, that means it ends on a newline
16278 in the string. If it's anything other than a display string
16279 (e.g. a before-string from an overlay), we don't want the
16280 cursor there. (This heuristic seems to give the optimal
16281 behavior for the various types of multi-line strings.) */
16282 if (CHARPOS (row->end.string_pos) >= 0)
16283 {
16284 if (row->continued_p)
16285 cursor_row_p = 1;
16286 else
16287 {
16288 /* Check for `display' property. */
16289 struct glyph *beg = row->glyphs[TEXT_AREA];
16290 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16291 struct glyph *glyph;
16292
16293 cursor_row_p = 0;
16294 for (glyph = end; glyph >= beg; --glyph)
16295 if (STRINGP (glyph->object))
16296 {
16297 Lisp_Object prop
16298 = Fget_char_property (make_number (PT),
16299 Qdisplay, Qnil);
16300 cursor_row_p =
16301 (!NILP (prop)
16302 && display_prop_string_p (prop, glyph->object));
16303 break;
16304 }
16305 }
16306 }
16307 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16308 {
16309 /* If the row ends in middle of a real character,
16310 and the line is continued, we want the cursor here.
16311 That's because MATRIX_ROW_END_CHARPOS would equal
16312 PT if PT is before the character. */
16313 if (!row->ends_in_ellipsis_p)
16314 cursor_row_p = row->continued_p;
16315 else
16316 /* If the row ends in an ellipsis, then
16317 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16318 We want that position to be displayed after the ellipsis. */
16319 cursor_row_p = 0;
16320 }
16321 /* If the row ends at ZV, display the cursor at the end of that
16322 row instead of at the start of the row below. */
16323 else if (row->ends_at_zv_p)
16324 cursor_row_p = 1;
16325 else
16326 cursor_row_p = 0;
16327 }
16328
16329 return cursor_row_p;
16330 }
16331
16332 \f
16333
16334 /* Push the display property PROP so that it will be rendered at the
16335 current position in IT. Return 1 if PROP was successfully pushed,
16336 0 otherwise. */
16337
16338 static int
16339 push_display_prop (struct it *it, Lisp_Object prop)
16340 {
16341 push_it (it);
16342
16343 if (STRINGP (prop))
16344 {
16345 if (SCHARS (prop) == 0)
16346 {
16347 pop_it (it);
16348 return 0;
16349 }
16350
16351 it->string = prop;
16352 it->multibyte_p = STRING_MULTIBYTE (it->string);
16353 it->current.overlay_string_index = -1;
16354 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16355 it->end_charpos = it->string_nchars = SCHARS (it->string);
16356 it->method = GET_FROM_STRING;
16357 it->stop_charpos = 0;
16358 }
16359 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16360 {
16361 it->method = GET_FROM_STRETCH;
16362 it->object = prop;
16363 }
16364 #ifdef HAVE_WINDOW_SYSTEM
16365 else if (IMAGEP (prop))
16366 {
16367 it->what = IT_IMAGE;
16368 it->image_id = lookup_image (it->f, prop);
16369 it->method = GET_FROM_IMAGE;
16370 }
16371 #endif /* HAVE_WINDOW_SYSTEM */
16372 else
16373 {
16374 pop_it (it); /* bogus display property, give up */
16375 return 0;
16376 }
16377
16378 return 1;
16379 }
16380
16381 /* Return the character-property PROP at the current position in IT. */
16382
16383 static Lisp_Object
16384 get_it_property (it, prop)
16385 struct it *it;
16386 Lisp_Object prop;
16387 {
16388 Lisp_Object position;
16389
16390 if (STRINGP (it->object))
16391 position = make_number (IT_STRING_CHARPOS (*it));
16392 else if (BUFFERP (it->object))
16393 position = make_number (IT_CHARPOS (*it));
16394 else
16395 return Qnil;
16396
16397 return Fget_char_property (position, prop, it->object);
16398 }
16399
16400 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16401
16402 static void
16403 handle_line_prefix (struct it *it)
16404 {
16405 Lisp_Object prefix;
16406 if (it->continuation_lines_width > 0)
16407 {
16408 prefix = get_it_property (it, Qwrap_prefix);
16409 if (NILP (prefix))
16410 prefix = Vwrap_prefix;
16411 }
16412 else
16413 {
16414 prefix = get_it_property (it, Qline_prefix);
16415 if (NILP (prefix))
16416 prefix = Vline_prefix;
16417 }
16418 if (! NILP (prefix) && push_display_prop (it, prefix))
16419 {
16420 /* If the prefix is wider than the window, and we try to wrap
16421 it, it would acquire its own wrap prefix, and so on till the
16422 iterator stack overflows. So, don't wrap the prefix. */
16423 it->line_wrap = TRUNCATE;
16424 it->avoid_cursor_p = 1;
16425 }
16426 }
16427
16428 \f
16429
16430 /* Construct the glyph row IT->glyph_row in the desired matrix of
16431 IT->w from text at the current position of IT. See dispextern.h
16432 for an overview of struct it. Value is non-zero if
16433 IT->glyph_row displays text, as opposed to a line displaying ZV
16434 only. */
16435
16436 static int
16437 display_line (it)
16438 struct it *it;
16439 {
16440 struct glyph_row *row = it->glyph_row;
16441 Lisp_Object overlay_arrow_string;
16442 struct it wrap_it;
16443 int may_wrap = 0, wrap_x;
16444 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16445 int wrap_row_phys_ascent, wrap_row_phys_height;
16446 int wrap_row_extra_line_spacing;
16447
16448 /* We always start displaying at hpos zero even if hscrolled. */
16449 xassert (it->hpos == 0 && it->current_x == 0);
16450
16451 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16452 >= it->w->desired_matrix->nrows)
16453 {
16454 it->w->nrows_scale_factor++;
16455 fonts_changed_p = 1;
16456 return 0;
16457 }
16458
16459 /* Is IT->w showing the region? */
16460 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16461
16462 /* Clear the result glyph row and enable it. */
16463 prepare_desired_row (row);
16464
16465 row->y = it->current_y;
16466 row->start = it->start;
16467 row->continuation_lines_width = it->continuation_lines_width;
16468 row->displays_text_p = 1;
16469 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16470 it->starts_in_middle_of_char_p = 0;
16471
16472 /* Arrange the overlays nicely for our purposes. Usually, we call
16473 display_line on only one line at a time, in which case this
16474 can't really hurt too much, or we call it on lines which appear
16475 one after another in the buffer, in which case all calls to
16476 recenter_overlay_lists but the first will be pretty cheap. */
16477 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16478
16479 /* Move over display elements that are not visible because we are
16480 hscrolled. This may stop at an x-position < IT->first_visible_x
16481 if the first glyph is partially visible or if we hit a line end. */
16482 if (it->current_x < it->first_visible_x)
16483 {
16484 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16485 MOVE_TO_POS | MOVE_TO_X);
16486 }
16487 else
16488 {
16489 /* We only do this when not calling `move_it_in_display_line_to'
16490 above, because move_it_in_display_line_to calls
16491 handle_line_prefix itself. */
16492 handle_line_prefix (it);
16493 }
16494
16495 /* Get the initial row height. This is either the height of the
16496 text hscrolled, if there is any, or zero. */
16497 row->ascent = it->max_ascent;
16498 row->height = it->max_ascent + it->max_descent;
16499 row->phys_ascent = it->max_phys_ascent;
16500 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16501 row->extra_line_spacing = it->max_extra_line_spacing;
16502
16503 /* Loop generating characters. The loop is left with IT on the next
16504 character to display. */
16505 while (1)
16506 {
16507 int n_glyphs_before, hpos_before, x_before;
16508 int x, i, nglyphs;
16509 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16510
16511 /* Retrieve the next thing to display. Value is zero if end of
16512 buffer reached. */
16513 if (!get_next_display_element (it))
16514 {
16515 /* Maybe add a space at the end of this line that is used to
16516 display the cursor there under X. Set the charpos of the
16517 first glyph of blank lines not corresponding to any text
16518 to -1. */
16519 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16520 row->exact_window_width_line_p = 1;
16521 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16522 || row->used[TEXT_AREA] == 0)
16523 {
16524 row->glyphs[TEXT_AREA]->charpos = -1;
16525 row->displays_text_p = 0;
16526
16527 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16528 && (!MINI_WINDOW_P (it->w)
16529 || (minibuf_level && EQ (it->window, minibuf_window))))
16530 row->indicate_empty_line_p = 1;
16531 }
16532
16533 it->continuation_lines_width = 0;
16534 row->ends_at_zv_p = 1;
16535 break;
16536 }
16537
16538 /* Now, get the metrics of what we want to display. This also
16539 generates glyphs in `row' (which is IT->glyph_row). */
16540 n_glyphs_before = row->used[TEXT_AREA];
16541 x = it->current_x;
16542
16543 /* Remember the line height so far in case the next element doesn't
16544 fit on the line. */
16545 if (it->line_wrap != TRUNCATE)
16546 {
16547 ascent = it->max_ascent;
16548 descent = it->max_descent;
16549 phys_ascent = it->max_phys_ascent;
16550 phys_descent = it->max_phys_descent;
16551
16552 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16553 {
16554 if (IT_DISPLAYING_WHITESPACE (it))
16555 may_wrap = 1;
16556 else if (may_wrap)
16557 {
16558 wrap_it = *it;
16559 wrap_x = x;
16560 wrap_row_used = row->used[TEXT_AREA];
16561 wrap_row_ascent = row->ascent;
16562 wrap_row_height = row->height;
16563 wrap_row_phys_ascent = row->phys_ascent;
16564 wrap_row_phys_height = row->phys_height;
16565 wrap_row_extra_line_spacing = row->extra_line_spacing;
16566 may_wrap = 0;
16567 }
16568 }
16569 }
16570
16571 PRODUCE_GLYPHS (it);
16572
16573 /* If this display element was in marginal areas, continue with
16574 the next one. */
16575 if (it->area != TEXT_AREA)
16576 {
16577 row->ascent = max (row->ascent, it->max_ascent);
16578 row->height = max (row->height, it->max_ascent + it->max_descent);
16579 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16580 row->phys_height = max (row->phys_height,
16581 it->max_phys_ascent + it->max_phys_descent);
16582 row->extra_line_spacing = max (row->extra_line_spacing,
16583 it->max_extra_line_spacing);
16584 set_iterator_to_next (it, 1);
16585 continue;
16586 }
16587
16588 /* Does the display element fit on the line? If we truncate
16589 lines, we should draw past the right edge of the window. If
16590 we don't truncate, we want to stop so that we can display the
16591 continuation glyph before the right margin. If lines are
16592 continued, there are two possible strategies for characters
16593 resulting in more than 1 glyph (e.g. tabs): Display as many
16594 glyphs as possible in this line and leave the rest for the
16595 continuation line, or display the whole element in the next
16596 line. Original redisplay did the former, so we do it also. */
16597 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16598 hpos_before = it->hpos;
16599 x_before = x;
16600
16601 if (/* Not a newline. */
16602 nglyphs > 0
16603 /* Glyphs produced fit entirely in the line. */
16604 && it->current_x < it->last_visible_x)
16605 {
16606 it->hpos += nglyphs;
16607 row->ascent = max (row->ascent, it->max_ascent);
16608 row->height = max (row->height, it->max_ascent + it->max_descent);
16609 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16610 row->phys_height = max (row->phys_height,
16611 it->max_phys_ascent + it->max_phys_descent);
16612 row->extra_line_spacing = max (row->extra_line_spacing,
16613 it->max_extra_line_spacing);
16614 if (it->current_x - it->pixel_width < it->first_visible_x)
16615 row->x = x - it->first_visible_x;
16616 }
16617 else
16618 {
16619 int new_x;
16620 struct glyph *glyph;
16621
16622 for (i = 0; i < nglyphs; ++i, x = new_x)
16623 {
16624 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16625 new_x = x + glyph->pixel_width;
16626
16627 if (/* Lines are continued. */
16628 it->line_wrap != TRUNCATE
16629 && (/* Glyph doesn't fit on the line. */
16630 new_x > it->last_visible_x
16631 /* Or it fits exactly on a window system frame. */
16632 || (new_x == it->last_visible_x
16633 && FRAME_WINDOW_P (it->f))))
16634 {
16635 /* End of a continued line. */
16636
16637 if (it->hpos == 0
16638 || (new_x == it->last_visible_x
16639 && FRAME_WINDOW_P (it->f)))
16640 {
16641 /* Current glyph is the only one on the line or
16642 fits exactly on the line. We must continue
16643 the line because we can't draw the cursor
16644 after the glyph. */
16645 row->continued_p = 1;
16646 it->current_x = new_x;
16647 it->continuation_lines_width += new_x;
16648 ++it->hpos;
16649 if (i == nglyphs - 1)
16650 {
16651 /* If line-wrap is on, check if a previous
16652 wrap point was found. */
16653 if (wrap_row_used > 0
16654 /* Even if there is a previous wrap
16655 point, continue the line here as
16656 usual, if (i) the previous character
16657 was a space or tab AND (ii) the
16658 current character is not. */
16659 && (!may_wrap
16660 || IT_DISPLAYING_WHITESPACE (it)))
16661 goto back_to_wrap;
16662
16663 set_iterator_to_next (it, 1);
16664 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16665 {
16666 if (!get_next_display_element (it))
16667 {
16668 row->exact_window_width_line_p = 1;
16669 it->continuation_lines_width = 0;
16670 row->continued_p = 0;
16671 row->ends_at_zv_p = 1;
16672 }
16673 else if (ITERATOR_AT_END_OF_LINE_P (it))
16674 {
16675 row->continued_p = 0;
16676 row->exact_window_width_line_p = 1;
16677 }
16678 }
16679 }
16680 }
16681 else if (CHAR_GLYPH_PADDING_P (*glyph)
16682 && !FRAME_WINDOW_P (it->f))
16683 {
16684 /* A padding glyph that doesn't fit on this line.
16685 This means the whole character doesn't fit
16686 on the line. */
16687 row->used[TEXT_AREA] = n_glyphs_before;
16688
16689 /* Fill the rest of the row with continuation
16690 glyphs like in 20.x. */
16691 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16692 < row->glyphs[1 + TEXT_AREA])
16693 produce_special_glyphs (it, IT_CONTINUATION);
16694
16695 row->continued_p = 1;
16696 it->current_x = x_before;
16697 it->continuation_lines_width += x_before;
16698
16699 /* Restore the height to what it was before the
16700 element not fitting on the line. */
16701 it->max_ascent = ascent;
16702 it->max_descent = descent;
16703 it->max_phys_ascent = phys_ascent;
16704 it->max_phys_descent = phys_descent;
16705 }
16706 else if (wrap_row_used > 0)
16707 {
16708 back_to_wrap:
16709 *it = wrap_it;
16710 it->continuation_lines_width += wrap_x;
16711 row->used[TEXT_AREA] = wrap_row_used;
16712 row->ascent = wrap_row_ascent;
16713 row->height = wrap_row_height;
16714 row->phys_ascent = wrap_row_phys_ascent;
16715 row->phys_height = wrap_row_phys_height;
16716 row->extra_line_spacing = wrap_row_extra_line_spacing;
16717 row->continued_p = 1;
16718 row->ends_at_zv_p = 0;
16719 row->exact_window_width_line_p = 0;
16720 it->continuation_lines_width += x;
16721
16722 /* Make sure that a non-default face is extended
16723 up to the right margin of the window. */
16724 extend_face_to_end_of_line (it);
16725 }
16726 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16727 {
16728 /* A TAB that extends past the right edge of the
16729 window. This produces a single glyph on
16730 window system frames. We leave the glyph in
16731 this row and let it fill the row, but don't
16732 consume the TAB. */
16733 it->continuation_lines_width += it->last_visible_x;
16734 row->ends_in_middle_of_char_p = 1;
16735 row->continued_p = 1;
16736 glyph->pixel_width = it->last_visible_x - x;
16737 it->starts_in_middle_of_char_p = 1;
16738 }
16739 else
16740 {
16741 /* Something other than a TAB that draws past
16742 the right edge of the window. Restore
16743 positions to values before the element. */
16744 row->used[TEXT_AREA] = n_glyphs_before + i;
16745
16746 /* Display continuation glyphs. */
16747 if (!FRAME_WINDOW_P (it->f))
16748 produce_special_glyphs (it, IT_CONTINUATION);
16749 row->continued_p = 1;
16750
16751 it->current_x = x_before;
16752 it->continuation_lines_width += x;
16753 extend_face_to_end_of_line (it);
16754
16755 if (nglyphs > 1 && i > 0)
16756 {
16757 row->ends_in_middle_of_char_p = 1;
16758 it->starts_in_middle_of_char_p = 1;
16759 }
16760
16761 /* Restore the height to what it was before the
16762 element not fitting on the line. */
16763 it->max_ascent = ascent;
16764 it->max_descent = descent;
16765 it->max_phys_ascent = phys_ascent;
16766 it->max_phys_descent = phys_descent;
16767 }
16768
16769 break;
16770 }
16771 else if (new_x > it->first_visible_x)
16772 {
16773 /* Increment number of glyphs actually displayed. */
16774 ++it->hpos;
16775
16776 if (x < it->first_visible_x)
16777 /* Glyph is partially visible, i.e. row starts at
16778 negative X position. */
16779 row->x = x - it->first_visible_x;
16780 }
16781 else
16782 {
16783 /* Glyph is completely off the left margin of the
16784 window. This should not happen because of the
16785 move_it_in_display_line at the start of this
16786 function, unless the text display area of the
16787 window is empty. */
16788 xassert (it->first_visible_x <= it->last_visible_x);
16789 }
16790 }
16791
16792 row->ascent = max (row->ascent, it->max_ascent);
16793 row->height = max (row->height, it->max_ascent + it->max_descent);
16794 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16795 row->phys_height = max (row->phys_height,
16796 it->max_phys_ascent + it->max_phys_descent);
16797 row->extra_line_spacing = max (row->extra_line_spacing,
16798 it->max_extra_line_spacing);
16799
16800 /* End of this display line if row is continued. */
16801 if (row->continued_p || row->ends_at_zv_p)
16802 break;
16803 }
16804
16805 at_end_of_line:
16806 /* Is this a line end? If yes, we're also done, after making
16807 sure that a non-default face is extended up to the right
16808 margin of the window. */
16809 if (ITERATOR_AT_END_OF_LINE_P (it))
16810 {
16811 int used_before = row->used[TEXT_AREA];
16812
16813 row->ends_in_newline_from_string_p = STRINGP (it->object);
16814
16815 /* Add a space at the end of the line that is used to
16816 display the cursor there. */
16817 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16818 append_space_for_newline (it, 0);
16819
16820 /* Extend the face to the end of the line. */
16821 extend_face_to_end_of_line (it);
16822
16823 /* Make sure we have the position. */
16824 if (used_before == 0)
16825 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16826
16827 /* Consume the line end. This skips over invisible lines. */
16828 set_iterator_to_next (it, 1);
16829 it->continuation_lines_width = 0;
16830 break;
16831 }
16832
16833 /* Proceed with next display element. Note that this skips
16834 over lines invisible because of selective display. */
16835 set_iterator_to_next (it, 1);
16836
16837 /* If we truncate lines, we are done when the last displayed
16838 glyphs reach past the right margin of the window. */
16839 if (it->line_wrap == TRUNCATE
16840 && (FRAME_WINDOW_P (it->f)
16841 ? (it->current_x >= it->last_visible_x)
16842 : (it->current_x > it->last_visible_x)))
16843 {
16844 /* Maybe add truncation glyphs. */
16845 if (!FRAME_WINDOW_P (it->f))
16846 {
16847 int i, n;
16848
16849 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16850 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16851 break;
16852
16853 for (n = row->used[TEXT_AREA]; i < n; ++i)
16854 {
16855 row->used[TEXT_AREA] = i;
16856 produce_special_glyphs (it, IT_TRUNCATION);
16857 }
16858 }
16859 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16860 {
16861 /* Don't truncate if we can overflow newline into fringe. */
16862 if (!get_next_display_element (it))
16863 {
16864 it->continuation_lines_width = 0;
16865 row->ends_at_zv_p = 1;
16866 row->exact_window_width_line_p = 1;
16867 break;
16868 }
16869 if (ITERATOR_AT_END_OF_LINE_P (it))
16870 {
16871 row->exact_window_width_line_p = 1;
16872 goto at_end_of_line;
16873 }
16874 }
16875
16876 row->truncated_on_right_p = 1;
16877 it->continuation_lines_width = 0;
16878 reseat_at_next_visible_line_start (it, 0);
16879 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16880 it->hpos = hpos_before;
16881 it->current_x = x_before;
16882 break;
16883 }
16884 }
16885
16886 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16887 at the left window margin. */
16888 if (it->first_visible_x
16889 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16890 {
16891 if (!FRAME_WINDOW_P (it->f))
16892 insert_left_trunc_glyphs (it);
16893 row->truncated_on_left_p = 1;
16894 }
16895
16896 /* If the start of this line is the overlay arrow-position, then
16897 mark this glyph row as the one containing the overlay arrow.
16898 This is clearly a mess with variable size fonts. It would be
16899 better to let it be displayed like cursors under X. */
16900 if ((row->displays_text_p || !overlay_arrow_seen)
16901 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16902 !NILP (overlay_arrow_string)))
16903 {
16904 /* Overlay arrow in window redisplay is a fringe bitmap. */
16905 if (STRINGP (overlay_arrow_string))
16906 {
16907 struct glyph_row *arrow_row
16908 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16909 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16910 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16911 struct glyph *p = row->glyphs[TEXT_AREA];
16912 struct glyph *p2, *end;
16913
16914 /* Copy the arrow glyphs. */
16915 while (glyph < arrow_end)
16916 *p++ = *glyph++;
16917
16918 /* Throw away padding glyphs. */
16919 p2 = p;
16920 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16921 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16922 ++p2;
16923 if (p2 > p)
16924 {
16925 while (p2 < end)
16926 *p++ = *p2++;
16927 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16928 }
16929 }
16930 else
16931 {
16932 xassert (INTEGERP (overlay_arrow_string));
16933 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16934 }
16935 overlay_arrow_seen = 1;
16936 }
16937
16938 /* Compute pixel dimensions of this line. */
16939 compute_line_metrics (it);
16940
16941 /* Remember the position at which this line ends. */
16942 row->end = it->current;
16943
16944 /* Record whether this row ends inside an ellipsis. */
16945 row->ends_in_ellipsis_p
16946 = (it->method == GET_FROM_DISPLAY_VECTOR
16947 && it->ellipsis_p);
16948
16949 /* Save fringe bitmaps in this row. */
16950 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16951 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16952 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16953 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16954
16955 it->left_user_fringe_bitmap = 0;
16956 it->left_user_fringe_face_id = 0;
16957 it->right_user_fringe_bitmap = 0;
16958 it->right_user_fringe_face_id = 0;
16959
16960 /* Maybe set the cursor. */
16961 if (it->w->cursor.vpos < 0
16962 && PT >= MATRIX_ROW_START_CHARPOS (row)
16963 && PT <= MATRIX_ROW_END_CHARPOS (row)
16964 && cursor_row_p (it->w, row))
16965 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16966
16967 /* Highlight trailing whitespace. */
16968 if (!NILP (Vshow_trailing_whitespace))
16969 highlight_trailing_whitespace (it->f, it->glyph_row);
16970
16971 /* Prepare for the next line. This line starts horizontally at (X
16972 HPOS) = (0 0). Vertical positions are incremented. As a
16973 convenience for the caller, IT->glyph_row is set to the next
16974 row to be used. */
16975 it->current_x = it->hpos = 0;
16976 it->current_y += row->height;
16977 ++it->vpos;
16978 ++it->glyph_row;
16979 it->start = it->current;
16980 return row->displays_text_p;
16981 }
16982
16983
16984 \f
16985 /***********************************************************************
16986 Menu Bar
16987 ***********************************************************************/
16988
16989 /* Redisplay the menu bar in the frame for window W.
16990
16991 The menu bar of X frames that don't have X toolkit support is
16992 displayed in a special window W->frame->menu_bar_window.
16993
16994 The menu bar of terminal frames is treated specially as far as
16995 glyph matrices are concerned. Menu bar lines are not part of
16996 windows, so the update is done directly on the frame matrix rows
16997 for the menu bar. */
16998
16999 static void
17000 display_menu_bar (w)
17001 struct window *w;
17002 {
17003 struct frame *f = XFRAME (WINDOW_FRAME (w));
17004 struct it it;
17005 Lisp_Object items;
17006 int i;
17007
17008 /* Don't do all this for graphical frames. */
17009 #ifdef HAVE_NTGUI
17010 if (FRAME_W32_P (f))
17011 return;
17012 #endif
17013 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17014 if (FRAME_X_P (f))
17015 return;
17016 #endif
17017
17018 #ifdef HAVE_NS
17019 if (FRAME_NS_P (f))
17020 return;
17021 #endif /* HAVE_NS */
17022
17023 #ifdef USE_X_TOOLKIT
17024 xassert (!FRAME_WINDOW_P (f));
17025 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17026 it.first_visible_x = 0;
17027 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17028 #else /* not USE_X_TOOLKIT */
17029 if (FRAME_WINDOW_P (f))
17030 {
17031 /* Menu bar lines are displayed in the desired matrix of the
17032 dummy window menu_bar_window. */
17033 struct window *menu_w;
17034 xassert (WINDOWP (f->menu_bar_window));
17035 menu_w = XWINDOW (f->menu_bar_window);
17036 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17037 MENU_FACE_ID);
17038 it.first_visible_x = 0;
17039 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17040 }
17041 else
17042 {
17043 /* This is a TTY frame, i.e. character hpos/vpos are used as
17044 pixel x/y. */
17045 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17046 MENU_FACE_ID);
17047 it.first_visible_x = 0;
17048 it.last_visible_x = FRAME_COLS (f);
17049 }
17050 #endif /* not USE_X_TOOLKIT */
17051
17052 if (! mode_line_inverse_video)
17053 /* Force the menu-bar to be displayed in the default face. */
17054 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17055
17056 /* Clear all rows of the menu bar. */
17057 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17058 {
17059 struct glyph_row *row = it.glyph_row + i;
17060 clear_glyph_row (row);
17061 row->enabled_p = 1;
17062 row->full_width_p = 1;
17063 }
17064
17065 /* Display all items of the menu bar. */
17066 items = FRAME_MENU_BAR_ITEMS (it.f);
17067 for (i = 0; i < XVECTOR (items)->size; i += 4)
17068 {
17069 Lisp_Object string;
17070
17071 /* Stop at nil string. */
17072 string = AREF (items, i + 1);
17073 if (NILP (string))
17074 break;
17075
17076 /* Remember where item was displayed. */
17077 ASET (items, i + 3, make_number (it.hpos));
17078
17079 /* Display the item, pad with one space. */
17080 if (it.current_x < it.last_visible_x)
17081 display_string (NULL, string, Qnil, 0, 0, &it,
17082 SCHARS (string) + 1, 0, 0, -1);
17083 }
17084
17085 /* Fill out the line with spaces. */
17086 if (it.current_x < it.last_visible_x)
17087 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17088
17089 /* Compute the total height of the lines. */
17090 compute_line_metrics (&it);
17091 }
17092
17093
17094 \f
17095 /***********************************************************************
17096 Mode Line
17097 ***********************************************************************/
17098
17099 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17100 FORCE is non-zero, redisplay mode lines unconditionally.
17101 Otherwise, redisplay only mode lines that are garbaged. Value is
17102 the number of windows whose mode lines were redisplayed. */
17103
17104 static int
17105 redisplay_mode_lines (window, force)
17106 Lisp_Object window;
17107 int force;
17108 {
17109 int nwindows = 0;
17110
17111 while (!NILP (window))
17112 {
17113 struct window *w = XWINDOW (window);
17114
17115 if (WINDOWP (w->hchild))
17116 nwindows += redisplay_mode_lines (w->hchild, force);
17117 else if (WINDOWP (w->vchild))
17118 nwindows += redisplay_mode_lines (w->vchild, force);
17119 else if (force
17120 || FRAME_GARBAGED_P (XFRAME (w->frame))
17121 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17122 {
17123 struct text_pos lpoint;
17124 struct buffer *old = current_buffer;
17125
17126 /* Set the window's buffer for the mode line display. */
17127 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17128 set_buffer_internal_1 (XBUFFER (w->buffer));
17129
17130 /* Point refers normally to the selected window. For any
17131 other window, set up appropriate value. */
17132 if (!EQ (window, selected_window))
17133 {
17134 struct text_pos pt;
17135
17136 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17137 if (CHARPOS (pt) < BEGV)
17138 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17139 else if (CHARPOS (pt) > (ZV - 1))
17140 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17141 else
17142 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17143 }
17144
17145 /* Display mode lines. */
17146 clear_glyph_matrix (w->desired_matrix);
17147 if (display_mode_lines (w))
17148 {
17149 ++nwindows;
17150 w->must_be_updated_p = 1;
17151 }
17152
17153 /* Restore old settings. */
17154 set_buffer_internal_1 (old);
17155 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17156 }
17157
17158 window = w->next;
17159 }
17160
17161 return nwindows;
17162 }
17163
17164
17165 /* Display the mode and/or header line of window W. Value is the
17166 sum number of mode lines and header lines displayed. */
17167
17168 static int
17169 display_mode_lines (w)
17170 struct window *w;
17171 {
17172 Lisp_Object old_selected_window, old_selected_frame;
17173 int n = 0;
17174
17175 old_selected_frame = selected_frame;
17176 selected_frame = w->frame;
17177 old_selected_window = selected_window;
17178 XSETWINDOW (selected_window, w);
17179
17180 /* These will be set while the mode line specs are processed. */
17181 line_number_displayed = 0;
17182 w->column_number_displayed = Qnil;
17183
17184 if (WINDOW_WANTS_MODELINE_P (w))
17185 {
17186 struct window *sel_w = XWINDOW (old_selected_window);
17187
17188 /* Select mode line face based on the real selected window. */
17189 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17190 current_buffer->mode_line_format);
17191 ++n;
17192 }
17193
17194 if (WINDOW_WANTS_HEADER_LINE_P (w))
17195 {
17196 display_mode_line (w, HEADER_LINE_FACE_ID,
17197 current_buffer->header_line_format);
17198 ++n;
17199 }
17200
17201 selected_frame = old_selected_frame;
17202 selected_window = old_selected_window;
17203 return n;
17204 }
17205
17206
17207 /* Display mode or header line of window W. FACE_ID specifies which
17208 line to display; it is either MODE_LINE_FACE_ID or
17209 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
17210 display. Value is the pixel height of the mode/header line
17211 displayed. */
17212
17213 static int
17214 display_mode_line (w, face_id, format)
17215 struct window *w;
17216 enum face_id face_id;
17217 Lisp_Object format;
17218 {
17219 struct it it;
17220 struct face *face;
17221 int count = SPECPDL_INDEX ();
17222
17223 init_iterator (&it, w, -1, -1, NULL, face_id);
17224 /* Don't extend on a previously drawn mode-line.
17225 This may happen if called from pos_visible_p. */
17226 it.glyph_row->enabled_p = 0;
17227 prepare_desired_row (it.glyph_row);
17228
17229 it.glyph_row->mode_line_p = 1;
17230
17231 if (! mode_line_inverse_video)
17232 /* Force the mode-line to be displayed in the default face. */
17233 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17234
17235 record_unwind_protect (unwind_format_mode_line,
17236 format_mode_line_unwind_data (NULL, Qnil, 0));
17237
17238 mode_line_target = MODE_LINE_DISPLAY;
17239
17240 /* Temporarily make frame's keyboard the current kboard so that
17241 kboard-local variables in the mode_line_format will get the right
17242 values. */
17243 push_kboard (FRAME_KBOARD (it.f));
17244 record_unwind_save_match_data ();
17245 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17246 pop_kboard ();
17247
17248 unbind_to (count, Qnil);
17249
17250 /* Fill up with spaces. */
17251 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17252
17253 compute_line_metrics (&it);
17254 it.glyph_row->full_width_p = 1;
17255 it.glyph_row->continued_p = 0;
17256 it.glyph_row->truncated_on_left_p = 0;
17257 it.glyph_row->truncated_on_right_p = 0;
17258
17259 /* Make a 3D mode-line have a shadow at its right end. */
17260 face = FACE_FROM_ID (it.f, face_id);
17261 extend_face_to_end_of_line (&it);
17262 if (face->box != FACE_NO_BOX)
17263 {
17264 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17265 + it.glyph_row->used[TEXT_AREA] - 1);
17266 last->right_box_line_p = 1;
17267 }
17268
17269 return it.glyph_row->height;
17270 }
17271
17272 /* Move element ELT in LIST to the front of LIST.
17273 Return the updated list. */
17274
17275 static Lisp_Object
17276 move_elt_to_front (elt, list)
17277 Lisp_Object elt, list;
17278 {
17279 register Lisp_Object tail, prev;
17280 register Lisp_Object tem;
17281
17282 tail = list;
17283 prev = Qnil;
17284 while (CONSP (tail))
17285 {
17286 tem = XCAR (tail);
17287
17288 if (EQ (elt, tem))
17289 {
17290 /* Splice out the link TAIL. */
17291 if (NILP (prev))
17292 list = XCDR (tail);
17293 else
17294 Fsetcdr (prev, XCDR (tail));
17295
17296 /* Now make it the first. */
17297 Fsetcdr (tail, list);
17298 return tail;
17299 }
17300 else
17301 prev = tail;
17302 tail = XCDR (tail);
17303 QUIT;
17304 }
17305
17306 /* Not found--return unchanged LIST. */
17307 return list;
17308 }
17309
17310 /* Contribute ELT to the mode line for window IT->w. How it
17311 translates into text depends on its data type.
17312
17313 IT describes the display environment in which we display, as usual.
17314
17315 DEPTH is the depth in recursion. It is used to prevent
17316 infinite recursion here.
17317
17318 FIELD_WIDTH is the number of characters the display of ELT should
17319 occupy in the mode line, and PRECISION is the maximum number of
17320 characters to display from ELT's representation. See
17321 display_string for details.
17322
17323 Returns the hpos of the end of the text generated by ELT.
17324
17325 PROPS is a property list to add to any string we encounter.
17326
17327 If RISKY is nonzero, remove (disregard) any properties in any string
17328 we encounter, and ignore :eval and :propertize.
17329
17330 The global variable `mode_line_target' determines whether the
17331 output is passed to `store_mode_line_noprop',
17332 `store_mode_line_string', or `display_string'. */
17333
17334 static int
17335 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17336 struct it *it;
17337 int depth;
17338 int field_width, precision;
17339 Lisp_Object elt, props;
17340 int risky;
17341 {
17342 int n = 0, field, prec;
17343 int literal = 0;
17344
17345 tail_recurse:
17346 if (depth > 100)
17347 elt = build_string ("*too-deep*");
17348
17349 depth++;
17350
17351 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17352 {
17353 case Lisp_String:
17354 {
17355 /* A string: output it and check for %-constructs within it. */
17356 unsigned char c;
17357 int offset = 0;
17358
17359 if (SCHARS (elt) > 0
17360 && (!NILP (props) || risky))
17361 {
17362 Lisp_Object oprops, aelt;
17363 oprops = Ftext_properties_at (make_number (0), elt);
17364
17365 /* If the starting string's properties are not what
17366 we want, translate the string. Also, if the string
17367 is risky, do that anyway. */
17368
17369 if (NILP (Fequal (props, oprops)) || risky)
17370 {
17371 /* If the starting string has properties,
17372 merge the specified ones onto the existing ones. */
17373 if (! NILP (oprops) && !risky)
17374 {
17375 Lisp_Object tem;
17376
17377 oprops = Fcopy_sequence (oprops);
17378 tem = props;
17379 while (CONSP (tem))
17380 {
17381 oprops = Fplist_put (oprops, XCAR (tem),
17382 XCAR (XCDR (tem)));
17383 tem = XCDR (XCDR (tem));
17384 }
17385 props = oprops;
17386 }
17387
17388 aelt = Fassoc (elt, mode_line_proptrans_alist);
17389 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17390 {
17391 /* AELT is what we want. Move it to the front
17392 without consing. */
17393 elt = XCAR (aelt);
17394 mode_line_proptrans_alist
17395 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17396 }
17397 else
17398 {
17399 Lisp_Object tem;
17400
17401 /* If AELT has the wrong props, it is useless.
17402 so get rid of it. */
17403 if (! NILP (aelt))
17404 mode_line_proptrans_alist
17405 = Fdelq (aelt, mode_line_proptrans_alist);
17406
17407 elt = Fcopy_sequence (elt);
17408 Fset_text_properties (make_number (0), Flength (elt),
17409 props, elt);
17410 /* Add this item to mode_line_proptrans_alist. */
17411 mode_line_proptrans_alist
17412 = Fcons (Fcons (elt, props),
17413 mode_line_proptrans_alist);
17414 /* Truncate mode_line_proptrans_alist
17415 to at most 50 elements. */
17416 tem = Fnthcdr (make_number (50),
17417 mode_line_proptrans_alist);
17418 if (! NILP (tem))
17419 XSETCDR (tem, Qnil);
17420 }
17421 }
17422 }
17423
17424 offset = 0;
17425
17426 if (literal)
17427 {
17428 prec = precision - n;
17429 switch (mode_line_target)
17430 {
17431 case MODE_LINE_NOPROP:
17432 case MODE_LINE_TITLE:
17433 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17434 break;
17435 case MODE_LINE_STRING:
17436 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17437 break;
17438 case MODE_LINE_DISPLAY:
17439 n += display_string (NULL, elt, Qnil, 0, 0, it,
17440 0, prec, 0, STRING_MULTIBYTE (elt));
17441 break;
17442 }
17443
17444 break;
17445 }
17446
17447 /* Handle the non-literal case. */
17448
17449 while ((precision <= 0 || n < precision)
17450 && SREF (elt, offset) != 0
17451 && (mode_line_target != MODE_LINE_DISPLAY
17452 || it->current_x < it->last_visible_x))
17453 {
17454 int last_offset = offset;
17455
17456 /* Advance to end of string or next format specifier. */
17457 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17458 ;
17459
17460 if (offset - 1 != last_offset)
17461 {
17462 int nchars, nbytes;
17463
17464 /* Output to end of string or up to '%'. Field width
17465 is length of string. Don't output more than
17466 PRECISION allows us. */
17467 offset--;
17468
17469 prec = c_string_width (SDATA (elt) + last_offset,
17470 offset - last_offset, precision - n,
17471 &nchars, &nbytes);
17472
17473 switch (mode_line_target)
17474 {
17475 case MODE_LINE_NOPROP:
17476 case MODE_LINE_TITLE:
17477 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17478 break;
17479 case MODE_LINE_STRING:
17480 {
17481 int bytepos = last_offset;
17482 int charpos = string_byte_to_char (elt, bytepos);
17483 int endpos = (precision <= 0
17484 ? string_byte_to_char (elt, offset)
17485 : charpos + nchars);
17486
17487 n += store_mode_line_string (NULL,
17488 Fsubstring (elt, make_number (charpos),
17489 make_number (endpos)),
17490 0, 0, 0, Qnil);
17491 }
17492 break;
17493 case MODE_LINE_DISPLAY:
17494 {
17495 int bytepos = last_offset;
17496 int charpos = string_byte_to_char (elt, bytepos);
17497
17498 if (precision <= 0)
17499 nchars = string_byte_to_char (elt, offset) - charpos;
17500 n += display_string (NULL, elt, Qnil, 0, charpos,
17501 it, 0, nchars, 0,
17502 STRING_MULTIBYTE (elt));
17503 }
17504 break;
17505 }
17506 }
17507 else /* c == '%' */
17508 {
17509 int percent_position = offset;
17510
17511 /* Get the specified minimum width. Zero means
17512 don't pad. */
17513 field = 0;
17514 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17515 field = field * 10 + c - '0';
17516
17517 /* Don't pad beyond the total padding allowed. */
17518 if (field_width - n > 0 && field > field_width - n)
17519 field = field_width - n;
17520
17521 /* Note that either PRECISION <= 0 or N < PRECISION. */
17522 prec = precision - n;
17523
17524 if (c == 'M')
17525 n += display_mode_element (it, depth, field, prec,
17526 Vglobal_mode_string, props,
17527 risky);
17528 else if (c != 0)
17529 {
17530 int multibyte;
17531 int bytepos, charpos;
17532 unsigned char *spec;
17533 Lisp_Object string;
17534
17535 bytepos = percent_position;
17536 charpos = (STRING_MULTIBYTE (elt)
17537 ? string_byte_to_char (elt, bytepos)
17538 : bytepos);
17539 spec = decode_mode_spec (it->w, c, field, prec, &string);
17540 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
17541
17542 switch (mode_line_target)
17543 {
17544 case MODE_LINE_NOPROP:
17545 case MODE_LINE_TITLE:
17546 n += store_mode_line_noprop (spec, field, prec);
17547 break;
17548 case MODE_LINE_STRING:
17549 {
17550 int len = strlen (spec);
17551 Lisp_Object tem = make_string (spec, len);
17552 props = Ftext_properties_at (make_number (charpos), elt);
17553 /* Should only keep face property in props */
17554 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17555 }
17556 break;
17557 case MODE_LINE_DISPLAY:
17558 {
17559 int nglyphs_before, nwritten;
17560
17561 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17562 nwritten = display_string (spec, string, elt,
17563 charpos, 0, it,
17564 field, prec, 0,
17565 multibyte);
17566
17567 /* Assign to the glyphs written above the
17568 string where the `%x' came from, position
17569 of the `%'. */
17570 if (nwritten > 0)
17571 {
17572 struct glyph *glyph
17573 = (it->glyph_row->glyphs[TEXT_AREA]
17574 + nglyphs_before);
17575 int i;
17576
17577 for (i = 0; i < nwritten; ++i)
17578 {
17579 glyph[i].object = elt;
17580 glyph[i].charpos = charpos;
17581 }
17582
17583 n += nwritten;
17584 }
17585 }
17586 break;
17587 }
17588 }
17589 else /* c == 0 */
17590 break;
17591 }
17592 }
17593 }
17594 break;
17595
17596 case Lisp_Symbol:
17597 /* A symbol: process the value of the symbol recursively
17598 as if it appeared here directly. Avoid error if symbol void.
17599 Special case: if value of symbol is a string, output the string
17600 literally. */
17601 {
17602 register Lisp_Object tem;
17603
17604 /* If the variable is not marked as risky to set
17605 then its contents are risky to use. */
17606 if (NILP (Fget (elt, Qrisky_local_variable)))
17607 risky = 1;
17608
17609 tem = Fboundp (elt);
17610 if (!NILP (tem))
17611 {
17612 tem = Fsymbol_value (elt);
17613 /* If value is a string, output that string literally:
17614 don't check for % within it. */
17615 if (STRINGP (tem))
17616 literal = 1;
17617
17618 if (!EQ (tem, elt))
17619 {
17620 /* Give up right away for nil or t. */
17621 elt = tem;
17622 goto tail_recurse;
17623 }
17624 }
17625 }
17626 break;
17627
17628 case Lisp_Cons:
17629 {
17630 register Lisp_Object car, tem;
17631
17632 /* A cons cell: five distinct cases.
17633 If first element is :eval or :propertize, do something special.
17634 If first element is a string or a cons, process all the elements
17635 and effectively concatenate them.
17636 If first element is a negative number, truncate displaying cdr to
17637 at most that many characters. If positive, pad (with spaces)
17638 to at least that many characters.
17639 If first element is a symbol, process the cadr or caddr recursively
17640 according to whether the symbol's value is non-nil or nil. */
17641 car = XCAR (elt);
17642 if (EQ (car, QCeval))
17643 {
17644 /* An element of the form (:eval FORM) means evaluate FORM
17645 and use the result as mode line elements. */
17646
17647 if (risky)
17648 break;
17649
17650 if (CONSP (XCDR (elt)))
17651 {
17652 Lisp_Object spec;
17653 spec = safe_eval (XCAR (XCDR (elt)));
17654 n += display_mode_element (it, depth, field_width - n,
17655 precision - n, spec, props,
17656 risky);
17657 }
17658 }
17659 else if (EQ (car, QCpropertize))
17660 {
17661 /* An element of the form (:propertize ELT PROPS...)
17662 means display ELT but applying properties PROPS. */
17663
17664 if (risky)
17665 break;
17666
17667 if (CONSP (XCDR (elt)))
17668 n += display_mode_element (it, depth, field_width - n,
17669 precision - n, XCAR (XCDR (elt)),
17670 XCDR (XCDR (elt)), risky);
17671 }
17672 else if (SYMBOLP (car))
17673 {
17674 tem = Fboundp (car);
17675 elt = XCDR (elt);
17676 if (!CONSP (elt))
17677 goto invalid;
17678 /* elt is now the cdr, and we know it is a cons cell.
17679 Use its car if CAR has a non-nil value. */
17680 if (!NILP (tem))
17681 {
17682 tem = Fsymbol_value (car);
17683 if (!NILP (tem))
17684 {
17685 elt = XCAR (elt);
17686 goto tail_recurse;
17687 }
17688 }
17689 /* Symbol's value is nil (or symbol is unbound)
17690 Get the cddr of the original list
17691 and if possible find the caddr and use that. */
17692 elt = XCDR (elt);
17693 if (NILP (elt))
17694 break;
17695 else if (!CONSP (elt))
17696 goto invalid;
17697 elt = XCAR (elt);
17698 goto tail_recurse;
17699 }
17700 else if (INTEGERP (car))
17701 {
17702 register int lim = XINT (car);
17703 elt = XCDR (elt);
17704 if (lim < 0)
17705 {
17706 /* Negative int means reduce maximum width. */
17707 if (precision <= 0)
17708 precision = -lim;
17709 else
17710 precision = min (precision, -lim);
17711 }
17712 else if (lim > 0)
17713 {
17714 /* Padding specified. Don't let it be more than
17715 current maximum. */
17716 if (precision > 0)
17717 lim = min (precision, lim);
17718
17719 /* If that's more padding than already wanted, queue it.
17720 But don't reduce padding already specified even if
17721 that is beyond the current truncation point. */
17722 field_width = max (lim, field_width);
17723 }
17724 goto tail_recurse;
17725 }
17726 else if (STRINGP (car) || CONSP (car))
17727 {
17728 Lisp_Object halftail = elt;
17729 int len = 0;
17730
17731 while (CONSP (elt)
17732 && (precision <= 0 || n < precision))
17733 {
17734 n += display_mode_element (it, depth,
17735 /* Do padding only after the last
17736 element in the list. */
17737 (! CONSP (XCDR (elt))
17738 ? field_width - n
17739 : 0),
17740 precision - n, XCAR (elt),
17741 props, risky);
17742 elt = XCDR (elt);
17743 len++;
17744 if ((len & 1) == 0)
17745 halftail = XCDR (halftail);
17746 /* Check for cycle. */
17747 if (EQ (halftail, elt))
17748 break;
17749 }
17750 }
17751 }
17752 break;
17753
17754 default:
17755 invalid:
17756 elt = build_string ("*invalid*");
17757 goto tail_recurse;
17758 }
17759
17760 /* Pad to FIELD_WIDTH. */
17761 if (field_width > 0 && n < field_width)
17762 {
17763 switch (mode_line_target)
17764 {
17765 case MODE_LINE_NOPROP:
17766 case MODE_LINE_TITLE:
17767 n += store_mode_line_noprop ("", field_width - n, 0);
17768 break;
17769 case MODE_LINE_STRING:
17770 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17771 break;
17772 case MODE_LINE_DISPLAY:
17773 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17774 0, 0, 0);
17775 break;
17776 }
17777 }
17778
17779 return n;
17780 }
17781
17782 /* Store a mode-line string element in mode_line_string_list.
17783
17784 If STRING is non-null, display that C string. Otherwise, the Lisp
17785 string LISP_STRING is displayed.
17786
17787 FIELD_WIDTH is the minimum number of output glyphs to produce.
17788 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17789 with spaces. FIELD_WIDTH <= 0 means don't pad.
17790
17791 PRECISION is the maximum number of characters to output from
17792 STRING. PRECISION <= 0 means don't truncate the string.
17793
17794 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17795 properties to the string.
17796
17797 PROPS are the properties to add to the string.
17798 The mode_line_string_face face property is always added to the string.
17799 */
17800
17801 static int
17802 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17803 char *string;
17804 Lisp_Object lisp_string;
17805 int copy_string;
17806 int field_width;
17807 int precision;
17808 Lisp_Object props;
17809 {
17810 int len;
17811 int n = 0;
17812
17813 if (string != NULL)
17814 {
17815 len = strlen (string);
17816 if (precision > 0 && len > precision)
17817 len = precision;
17818 lisp_string = make_string (string, len);
17819 if (NILP (props))
17820 props = mode_line_string_face_prop;
17821 else if (!NILP (mode_line_string_face))
17822 {
17823 Lisp_Object face = Fplist_get (props, Qface);
17824 props = Fcopy_sequence (props);
17825 if (NILP (face))
17826 face = mode_line_string_face;
17827 else
17828 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17829 props = Fplist_put (props, Qface, face);
17830 }
17831 Fadd_text_properties (make_number (0), make_number (len),
17832 props, lisp_string);
17833 }
17834 else
17835 {
17836 len = XFASTINT (Flength (lisp_string));
17837 if (precision > 0 && len > precision)
17838 {
17839 len = precision;
17840 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17841 precision = -1;
17842 }
17843 if (!NILP (mode_line_string_face))
17844 {
17845 Lisp_Object face;
17846 if (NILP (props))
17847 props = Ftext_properties_at (make_number (0), lisp_string);
17848 face = Fplist_get (props, Qface);
17849 if (NILP (face))
17850 face = mode_line_string_face;
17851 else
17852 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17853 props = Fcons (Qface, Fcons (face, Qnil));
17854 if (copy_string)
17855 lisp_string = Fcopy_sequence (lisp_string);
17856 }
17857 if (!NILP (props))
17858 Fadd_text_properties (make_number (0), make_number (len),
17859 props, lisp_string);
17860 }
17861
17862 if (len > 0)
17863 {
17864 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17865 n += len;
17866 }
17867
17868 if (field_width > len)
17869 {
17870 field_width -= len;
17871 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17872 if (!NILP (props))
17873 Fadd_text_properties (make_number (0), make_number (field_width),
17874 props, lisp_string);
17875 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17876 n += field_width;
17877 }
17878
17879 return n;
17880 }
17881
17882
17883 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17884 1, 4, 0,
17885 doc: /* Format a string out of a mode line format specification.
17886 First arg FORMAT specifies the mode line format (see `mode-line-format'
17887 for details) to use.
17888
17889 Optional second arg FACE specifies the face property to put
17890 on all characters for which no face is specified.
17891 The value t means whatever face the window's mode line currently uses
17892 \(either `mode-line' or `mode-line-inactive', depending).
17893 A value of nil means the default is no face property.
17894 If FACE is an integer, the value string has no text properties.
17895
17896 Optional third and fourth args WINDOW and BUFFER specify the window
17897 and buffer to use as the context for the formatting (defaults
17898 are the selected window and the window's buffer). */)
17899 (format, face, window, buffer)
17900 Lisp_Object format, face, window, buffer;
17901 {
17902 struct it it;
17903 int len;
17904 struct window *w;
17905 struct buffer *old_buffer = NULL;
17906 int face_id = -1;
17907 int no_props = INTEGERP (face);
17908 int count = SPECPDL_INDEX ();
17909 Lisp_Object str;
17910 int string_start = 0;
17911
17912 if (NILP (window))
17913 window = selected_window;
17914 CHECK_WINDOW (window);
17915 w = XWINDOW (window);
17916
17917 if (NILP (buffer))
17918 buffer = w->buffer;
17919 CHECK_BUFFER (buffer);
17920
17921 /* Make formatting the modeline a non-op when noninteractive, otherwise
17922 there will be problems later caused by a partially initialized frame. */
17923 if (NILP (format) || noninteractive)
17924 return empty_unibyte_string;
17925
17926 if (no_props)
17927 face = Qnil;
17928
17929 if (!NILP (face))
17930 {
17931 if (EQ (face, Qt))
17932 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17933 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17934 }
17935
17936 if (face_id < 0)
17937 face_id = DEFAULT_FACE_ID;
17938
17939 if (XBUFFER (buffer) != current_buffer)
17940 old_buffer = current_buffer;
17941
17942 /* Save things including mode_line_proptrans_alist,
17943 and set that to nil so that we don't alter the outer value. */
17944 record_unwind_protect (unwind_format_mode_line,
17945 format_mode_line_unwind_data
17946 (old_buffer, selected_window, 1));
17947 mode_line_proptrans_alist = Qnil;
17948
17949 Fselect_window (window, Qt);
17950 if (old_buffer)
17951 set_buffer_internal_1 (XBUFFER (buffer));
17952
17953 init_iterator (&it, w, -1, -1, NULL, face_id);
17954
17955 if (no_props)
17956 {
17957 mode_line_target = MODE_LINE_NOPROP;
17958 mode_line_string_face_prop = Qnil;
17959 mode_line_string_list = Qnil;
17960 string_start = MODE_LINE_NOPROP_LEN (0);
17961 }
17962 else
17963 {
17964 mode_line_target = MODE_LINE_STRING;
17965 mode_line_string_list = Qnil;
17966 mode_line_string_face = face;
17967 mode_line_string_face_prop
17968 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17969 }
17970
17971 push_kboard (FRAME_KBOARD (it.f));
17972 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17973 pop_kboard ();
17974
17975 if (no_props)
17976 {
17977 len = MODE_LINE_NOPROP_LEN (string_start);
17978 str = make_string (mode_line_noprop_buf + string_start, len);
17979 }
17980 else
17981 {
17982 mode_line_string_list = Fnreverse (mode_line_string_list);
17983 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17984 empty_unibyte_string);
17985 }
17986
17987 unbind_to (count, Qnil);
17988 return str;
17989 }
17990
17991 /* Write a null-terminated, right justified decimal representation of
17992 the positive integer D to BUF using a minimal field width WIDTH. */
17993
17994 static void
17995 pint2str (buf, width, d)
17996 register char *buf;
17997 register int width;
17998 register int d;
17999 {
18000 register char *p = buf;
18001
18002 if (d <= 0)
18003 *p++ = '0';
18004 else
18005 {
18006 while (d > 0)
18007 {
18008 *p++ = d % 10 + '0';
18009 d /= 10;
18010 }
18011 }
18012
18013 for (width -= (int) (p - buf); width > 0; --width)
18014 *p++ = ' ';
18015 *p-- = '\0';
18016 while (p > buf)
18017 {
18018 d = *buf;
18019 *buf++ = *p;
18020 *p-- = d;
18021 }
18022 }
18023
18024 /* Write a null-terminated, right justified decimal and "human
18025 readable" representation of the nonnegative integer D to BUF using
18026 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18027
18028 static const char power_letter[] =
18029 {
18030 0, /* not used */
18031 'k', /* kilo */
18032 'M', /* mega */
18033 'G', /* giga */
18034 'T', /* tera */
18035 'P', /* peta */
18036 'E', /* exa */
18037 'Z', /* zetta */
18038 'Y' /* yotta */
18039 };
18040
18041 static void
18042 pint2hrstr (buf, width, d)
18043 char *buf;
18044 int width;
18045 int d;
18046 {
18047 /* We aim to represent the nonnegative integer D as
18048 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18049 int quotient = d;
18050 int remainder = 0;
18051 /* -1 means: do not use TENTHS. */
18052 int tenths = -1;
18053 int exponent = 0;
18054
18055 /* Length of QUOTIENT.TENTHS as a string. */
18056 int length;
18057
18058 char * psuffix;
18059 char * p;
18060
18061 if (1000 <= quotient)
18062 {
18063 /* Scale to the appropriate EXPONENT. */
18064 do
18065 {
18066 remainder = quotient % 1000;
18067 quotient /= 1000;
18068 exponent++;
18069 }
18070 while (1000 <= quotient);
18071
18072 /* Round to nearest and decide whether to use TENTHS or not. */
18073 if (quotient <= 9)
18074 {
18075 tenths = remainder / 100;
18076 if (50 <= remainder % 100)
18077 {
18078 if (tenths < 9)
18079 tenths++;
18080 else
18081 {
18082 quotient++;
18083 if (quotient == 10)
18084 tenths = -1;
18085 else
18086 tenths = 0;
18087 }
18088 }
18089 }
18090 else
18091 if (500 <= remainder)
18092 {
18093 if (quotient < 999)
18094 quotient++;
18095 else
18096 {
18097 quotient = 1;
18098 exponent++;
18099 tenths = 0;
18100 }
18101 }
18102 }
18103
18104 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18105 if (tenths == -1 && quotient <= 99)
18106 if (quotient <= 9)
18107 length = 1;
18108 else
18109 length = 2;
18110 else
18111 length = 3;
18112 p = psuffix = buf + max (width, length);
18113
18114 /* Print EXPONENT. */
18115 if (exponent)
18116 *psuffix++ = power_letter[exponent];
18117 *psuffix = '\0';
18118
18119 /* Print TENTHS. */
18120 if (tenths >= 0)
18121 {
18122 *--p = '0' + tenths;
18123 *--p = '.';
18124 }
18125
18126 /* Print QUOTIENT. */
18127 do
18128 {
18129 int digit = quotient % 10;
18130 *--p = '0' + digit;
18131 }
18132 while ((quotient /= 10) != 0);
18133
18134 /* Print leading spaces. */
18135 while (buf < p)
18136 *--p = ' ';
18137 }
18138
18139 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18140 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18141 type of CODING_SYSTEM. Return updated pointer into BUF. */
18142
18143 static unsigned char invalid_eol_type[] = "(*invalid*)";
18144
18145 static char *
18146 decode_mode_spec_coding (coding_system, buf, eol_flag)
18147 Lisp_Object coding_system;
18148 register char *buf;
18149 int eol_flag;
18150 {
18151 Lisp_Object val;
18152 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18153 const unsigned char *eol_str;
18154 int eol_str_len;
18155 /* The EOL conversion we are using. */
18156 Lisp_Object eoltype;
18157
18158 val = CODING_SYSTEM_SPEC (coding_system);
18159 eoltype = Qnil;
18160
18161 if (!VECTORP (val)) /* Not yet decided. */
18162 {
18163 if (multibyte)
18164 *buf++ = '-';
18165 if (eol_flag)
18166 eoltype = eol_mnemonic_undecided;
18167 /* Don't mention EOL conversion if it isn't decided. */
18168 }
18169 else
18170 {
18171 Lisp_Object attrs;
18172 Lisp_Object eolvalue;
18173
18174 attrs = AREF (val, 0);
18175 eolvalue = AREF (val, 2);
18176
18177 if (multibyte)
18178 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18179
18180 if (eol_flag)
18181 {
18182 /* The EOL conversion that is normal on this system. */
18183
18184 if (NILP (eolvalue)) /* Not yet decided. */
18185 eoltype = eol_mnemonic_undecided;
18186 else if (VECTORP (eolvalue)) /* Not yet decided. */
18187 eoltype = eol_mnemonic_undecided;
18188 else /* eolvalue is Qunix, Qdos, or Qmac. */
18189 eoltype = (EQ (eolvalue, Qunix)
18190 ? eol_mnemonic_unix
18191 : (EQ (eolvalue, Qdos) == 1
18192 ? eol_mnemonic_dos : eol_mnemonic_mac));
18193 }
18194 }
18195
18196 if (eol_flag)
18197 {
18198 /* Mention the EOL conversion if it is not the usual one. */
18199 if (STRINGP (eoltype))
18200 {
18201 eol_str = SDATA (eoltype);
18202 eol_str_len = SBYTES (eoltype);
18203 }
18204 else if (CHARACTERP (eoltype))
18205 {
18206 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18207 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18208 eol_str = tmp;
18209 }
18210 else
18211 {
18212 eol_str = invalid_eol_type;
18213 eol_str_len = sizeof (invalid_eol_type) - 1;
18214 }
18215 bcopy (eol_str, buf, eol_str_len);
18216 buf += eol_str_len;
18217 }
18218
18219 return buf;
18220 }
18221
18222 /* Return a string for the output of a mode line %-spec for window W,
18223 generated by character C. PRECISION >= 0 means don't return a
18224 string longer than that value. FIELD_WIDTH > 0 means pad the
18225 string returned with spaces to that value. Return a Lisp string in
18226 *STRING if the resulting string is taken from that Lisp string.
18227
18228 Note we operate on the current buffer for most purposes,
18229 the exception being w->base_line_pos. */
18230
18231 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18232
18233 static char *
18234 decode_mode_spec (w, c, field_width, precision, string)
18235 struct window *w;
18236 register int c;
18237 int field_width, precision;
18238 Lisp_Object *string;
18239 {
18240 Lisp_Object obj;
18241 struct frame *f = XFRAME (WINDOW_FRAME (w));
18242 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18243 struct buffer *b = current_buffer;
18244
18245 obj = Qnil;
18246 *string = Qnil;
18247
18248 switch (c)
18249 {
18250 case '*':
18251 if (!NILP (b->read_only))
18252 return "%";
18253 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18254 return "*";
18255 return "-";
18256
18257 case '+':
18258 /* This differs from %* only for a modified read-only buffer. */
18259 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18260 return "*";
18261 if (!NILP (b->read_only))
18262 return "%";
18263 return "-";
18264
18265 case '&':
18266 /* This differs from %* in ignoring read-only-ness. */
18267 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18268 return "*";
18269 return "-";
18270
18271 case '%':
18272 return "%";
18273
18274 case '[':
18275 {
18276 int i;
18277 char *p;
18278
18279 if (command_loop_level > 5)
18280 return "[[[... ";
18281 p = decode_mode_spec_buf;
18282 for (i = 0; i < command_loop_level; i++)
18283 *p++ = '[';
18284 *p = 0;
18285 return decode_mode_spec_buf;
18286 }
18287
18288 case ']':
18289 {
18290 int i;
18291 char *p;
18292
18293 if (command_loop_level > 5)
18294 return " ...]]]";
18295 p = decode_mode_spec_buf;
18296 for (i = 0; i < command_loop_level; i++)
18297 *p++ = ']';
18298 *p = 0;
18299 return decode_mode_spec_buf;
18300 }
18301
18302 case '-':
18303 {
18304 register int i;
18305
18306 /* Let lots_of_dashes be a string of infinite length. */
18307 if (mode_line_target == MODE_LINE_NOPROP ||
18308 mode_line_target == MODE_LINE_STRING)
18309 return "--";
18310 if (field_width <= 0
18311 || field_width > sizeof (lots_of_dashes))
18312 {
18313 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18314 decode_mode_spec_buf[i] = '-';
18315 decode_mode_spec_buf[i] = '\0';
18316 return decode_mode_spec_buf;
18317 }
18318 else
18319 return lots_of_dashes;
18320 }
18321
18322 case 'b':
18323 obj = b->name;
18324 break;
18325
18326 case 'c':
18327 /* %c and %l are ignored in `frame-title-format'.
18328 (In redisplay_internal, the frame title is drawn _before_ the
18329 windows are updated, so the stuff which depends on actual
18330 window contents (such as %l) may fail to render properly, or
18331 even crash emacs.) */
18332 if (mode_line_target == MODE_LINE_TITLE)
18333 return "";
18334 else
18335 {
18336 int col = (int) current_column (); /* iftc */
18337 w->column_number_displayed = make_number (col);
18338 pint2str (decode_mode_spec_buf, field_width, col);
18339 return decode_mode_spec_buf;
18340 }
18341
18342 case 'e':
18343 #ifndef SYSTEM_MALLOC
18344 {
18345 if (NILP (Vmemory_full))
18346 return "";
18347 else
18348 return "!MEM FULL! ";
18349 }
18350 #else
18351 return "";
18352 #endif
18353
18354 case 'F':
18355 /* %F displays the frame name. */
18356 if (!NILP (f->title))
18357 return (char *) SDATA (f->title);
18358 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18359 return (char *) SDATA (f->name);
18360 return "Emacs";
18361
18362 case 'f':
18363 obj = b->filename;
18364 break;
18365
18366 case 'i':
18367 {
18368 int size = ZV - BEGV;
18369 pint2str (decode_mode_spec_buf, field_width, size);
18370 return decode_mode_spec_buf;
18371 }
18372
18373 case 'I':
18374 {
18375 int size = ZV - BEGV;
18376 pint2hrstr (decode_mode_spec_buf, field_width, size);
18377 return decode_mode_spec_buf;
18378 }
18379
18380 case 'l':
18381 {
18382 int startpos, startpos_byte, line, linepos, linepos_byte;
18383 int topline, nlines, junk, height;
18384
18385 /* %c and %l are ignored in `frame-title-format'. */
18386 if (mode_line_target == MODE_LINE_TITLE)
18387 return "";
18388
18389 startpos = XMARKER (w->start)->charpos;
18390 startpos_byte = marker_byte_position (w->start);
18391 height = WINDOW_TOTAL_LINES (w);
18392
18393 /* If we decided that this buffer isn't suitable for line numbers,
18394 don't forget that too fast. */
18395 if (EQ (w->base_line_pos, w->buffer))
18396 goto no_value;
18397 /* But do forget it, if the window shows a different buffer now. */
18398 else if (BUFFERP (w->base_line_pos))
18399 w->base_line_pos = Qnil;
18400
18401 /* If the buffer is very big, don't waste time. */
18402 if (INTEGERP (Vline_number_display_limit)
18403 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18404 {
18405 w->base_line_pos = Qnil;
18406 w->base_line_number = Qnil;
18407 goto no_value;
18408 }
18409
18410 if (INTEGERP (w->base_line_number)
18411 && INTEGERP (w->base_line_pos)
18412 && XFASTINT (w->base_line_pos) <= startpos)
18413 {
18414 line = XFASTINT (w->base_line_number);
18415 linepos = XFASTINT (w->base_line_pos);
18416 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18417 }
18418 else
18419 {
18420 line = 1;
18421 linepos = BUF_BEGV (b);
18422 linepos_byte = BUF_BEGV_BYTE (b);
18423 }
18424
18425 /* Count lines from base line to window start position. */
18426 nlines = display_count_lines (linepos, linepos_byte,
18427 startpos_byte,
18428 startpos, &junk);
18429
18430 topline = nlines + line;
18431
18432 /* Determine a new base line, if the old one is too close
18433 or too far away, or if we did not have one.
18434 "Too close" means it's plausible a scroll-down would
18435 go back past it. */
18436 if (startpos == BUF_BEGV (b))
18437 {
18438 w->base_line_number = make_number (topline);
18439 w->base_line_pos = make_number (BUF_BEGV (b));
18440 }
18441 else if (nlines < height + 25 || nlines > height * 3 + 50
18442 || linepos == BUF_BEGV (b))
18443 {
18444 int limit = BUF_BEGV (b);
18445 int limit_byte = BUF_BEGV_BYTE (b);
18446 int position;
18447 int distance = (height * 2 + 30) * line_number_display_limit_width;
18448
18449 if (startpos - distance > limit)
18450 {
18451 limit = startpos - distance;
18452 limit_byte = CHAR_TO_BYTE (limit);
18453 }
18454
18455 nlines = display_count_lines (startpos, startpos_byte,
18456 limit_byte,
18457 - (height * 2 + 30),
18458 &position);
18459 /* If we couldn't find the lines we wanted within
18460 line_number_display_limit_width chars per line,
18461 give up on line numbers for this window. */
18462 if (position == limit_byte && limit == startpos - distance)
18463 {
18464 w->base_line_pos = w->buffer;
18465 w->base_line_number = Qnil;
18466 goto no_value;
18467 }
18468
18469 w->base_line_number = make_number (topline - nlines);
18470 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18471 }
18472
18473 /* Now count lines from the start pos to point. */
18474 nlines = display_count_lines (startpos, startpos_byte,
18475 PT_BYTE, PT, &junk);
18476
18477 /* Record that we did display the line number. */
18478 line_number_displayed = 1;
18479
18480 /* Make the string to show. */
18481 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18482 return decode_mode_spec_buf;
18483 no_value:
18484 {
18485 char* p = decode_mode_spec_buf;
18486 int pad = field_width - 2;
18487 while (pad-- > 0)
18488 *p++ = ' ';
18489 *p++ = '?';
18490 *p++ = '?';
18491 *p = '\0';
18492 return decode_mode_spec_buf;
18493 }
18494 }
18495 break;
18496
18497 case 'm':
18498 obj = b->mode_name;
18499 break;
18500
18501 case 'n':
18502 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18503 return " Narrow";
18504 break;
18505
18506 case 'p':
18507 {
18508 int pos = marker_position (w->start);
18509 int total = BUF_ZV (b) - BUF_BEGV (b);
18510
18511 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18512 {
18513 if (pos <= BUF_BEGV (b))
18514 return "All";
18515 else
18516 return "Bottom";
18517 }
18518 else if (pos <= BUF_BEGV (b))
18519 return "Top";
18520 else
18521 {
18522 if (total > 1000000)
18523 /* Do it differently for a large value, to avoid overflow. */
18524 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18525 else
18526 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18527 /* We can't normally display a 3-digit number,
18528 so get us a 2-digit number that is close. */
18529 if (total == 100)
18530 total = 99;
18531 sprintf (decode_mode_spec_buf, "%2d%%", total);
18532 return decode_mode_spec_buf;
18533 }
18534 }
18535
18536 /* Display percentage of size above the bottom of the screen. */
18537 case 'P':
18538 {
18539 int toppos = marker_position (w->start);
18540 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18541 int total = BUF_ZV (b) - BUF_BEGV (b);
18542
18543 if (botpos >= BUF_ZV (b))
18544 {
18545 if (toppos <= BUF_BEGV (b))
18546 return "All";
18547 else
18548 return "Bottom";
18549 }
18550 else
18551 {
18552 if (total > 1000000)
18553 /* Do it differently for a large value, to avoid overflow. */
18554 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18555 else
18556 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18557 /* We can't normally display a 3-digit number,
18558 so get us a 2-digit number that is close. */
18559 if (total == 100)
18560 total = 99;
18561 if (toppos <= BUF_BEGV (b))
18562 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18563 else
18564 sprintf (decode_mode_spec_buf, "%2d%%", total);
18565 return decode_mode_spec_buf;
18566 }
18567 }
18568
18569 case 's':
18570 /* status of process */
18571 obj = Fget_buffer_process (Fcurrent_buffer ());
18572 if (NILP (obj))
18573 return "no process";
18574 #ifdef subprocesses
18575 obj = Fsymbol_name (Fprocess_status (obj));
18576 #endif
18577 break;
18578
18579 case '@':
18580 {
18581 int count = inhibit_garbage_collection ();
18582 Lisp_Object val = call1 (intern ("file-remote-p"),
18583 current_buffer->directory);
18584 unbind_to (count, Qnil);
18585
18586 if (NILP (val))
18587 return "-";
18588 else
18589 return "@";
18590 }
18591
18592 case 't': /* indicate TEXT or BINARY */
18593 #ifdef MODE_LINE_BINARY_TEXT
18594 return MODE_LINE_BINARY_TEXT (b);
18595 #else
18596 return "T";
18597 #endif
18598
18599 case 'z':
18600 /* coding-system (not including end-of-line format) */
18601 case 'Z':
18602 /* coding-system (including end-of-line type) */
18603 {
18604 int eol_flag = (c == 'Z');
18605 char *p = decode_mode_spec_buf;
18606
18607 if (! FRAME_WINDOW_P (f))
18608 {
18609 /* No need to mention EOL here--the terminal never needs
18610 to do EOL conversion. */
18611 p = decode_mode_spec_coding (CODING_ID_NAME
18612 (FRAME_KEYBOARD_CODING (f)->id),
18613 p, 0);
18614 p = decode_mode_spec_coding (CODING_ID_NAME
18615 (FRAME_TERMINAL_CODING (f)->id),
18616 p, 0);
18617 }
18618 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18619 p, eol_flag);
18620
18621 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18622 #ifdef subprocesses
18623 obj = Fget_buffer_process (Fcurrent_buffer ());
18624 if (PROCESSP (obj))
18625 {
18626 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18627 p, eol_flag);
18628 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18629 p, eol_flag);
18630 }
18631 #endif /* subprocesses */
18632 #endif /* 0 */
18633 *p = 0;
18634 return decode_mode_spec_buf;
18635 }
18636 }
18637
18638 if (STRINGP (obj))
18639 {
18640 *string = obj;
18641 return (char *) SDATA (obj);
18642 }
18643 else
18644 return "";
18645 }
18646
18647
18648 /* Count up to COUNT lines starting from START / START_BYTE.
18649 But don't go beyond LIMIT_BYTE.
18650 Return the number of lines thus found (always nonnegative).
18651
18652 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18653
18654 static int
18655 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18656 int start, start_byte, limit_byte, count;
18657 int *byte_pos_ptr;
18658 {
18659 register unsigned char *cursor;
18660 unsigned char *base;
18661
18662 register int ceiling;
18663 register unsigned char *ceiling_addr;
18664 int orig_count = count;
18665
18666 /* If we are not in selective display mode,
18667 check only for newlines. */
18668 int selective_display = (!NILP (current_buffer->selective_display)
18669 && !INTEGERP (current_buffer->selective_display));
18670
18671 if (count > 0)
18672 {
18673 while (start_byte < limit_byte)
18674 {
18675 ceiling = BUFFER_CEILING_OF (start_byte);
18676 ceiling = min (limit_byte - 1, ceiling);
18677 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18678 base = (cursor = BYTE_POS_ADDR (start_byte));
18679 while (1)
18680 {
18681 if (selective_display)
18682 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18683 ;
18684 else
18685 while (*cursor != '\n' && ++cursor != ceiling_addr)
18686 ;
18687
18688 if (cursor != ceiling_addr)
18689 {
18690 if (--count == 0)
18691 {
18692 start_byte += cursor - base + 1;
18693 *byte_pos_ptr = start_byte;
18694 return orig_count;
18695 }
18696 else
18697 if (++cursor == ceiling_addr)
18698 break;
18699 }
18700 else
18701 break;
18702 }
18703 start_byte += cursor - base;
18704 }
18705 }
18706 else
18707 {
18708 while (start_byte > limit_byte)
18709 {
18710 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18711 ceiling = max (limit_byte, ceiling);
18712 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18713 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18714 while (1)
18715 {
18716 if (selective_display)
18717 while (--cursor != ceiling_addr
18718 && *cursor != '\n' && *cursor != 015)
18719 ;
18720 else
18721 while (--cursor != ceiling_addr && *cursor != '\n')
18722 ;
18723
18724 if (cursor != ceiling_addr)
18725 {
18726 if (++count == 0)
18727 {
18728 start_byte += cursor - base + 1;
18729 *byte_pos_ptr = start_byte;
18730 /* When scanning backwards, we should
18731 not count the newline posterior to which we stop. */
18732 return - orig_count - 1;
18733 }
18734 }
18735 else
18736 break;
18737 }
18738 /* Here we add 1 to compensate for the last decrement
18739 of CURSOR, which took it past the valid range. */
18740 start_byte += cursor - base + 1;
18741 }
18742 }
18743
18744 *byte_pos_ptr = limit_byte;
18745
18746 if (count < 0)
18747 return - orig_count + count;
18748 return orig_count - count;
18749
18750 }
18751
18752
18753 \f
18754 /***********************************************************************
18755 Displaying strings
18756 ***********************************************************************/
18757
18758 /* Display a NUL-terminated string, starting with index START.
18759
18760 If STRING is non-null, display that C string. Otherwise, the Lisp
18761 string LISP_STRING is displayed. There's a case that STRING is
18762 non-null and LISP_STRING is not nil. It means STRING is a string
18763 data of LISP_STRING. In that case, we display LISP_STRING while
18764 ignoring its text properties.
18765
18766 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18767 FACE_STRING. Display STRING or LISP_STRING with the face at
18768 FACE_STRING_POS in FACE_STRING:
18769
18770 Display the string in the environment given by IT, but use the
18771 standard display table, temporarily.
18772
18773 FIELD_WIDTH is the minimum number of output glyphs to produce.
18774 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18775 with spaces. If STRING has more characters, more than FIELD_WIDTH
18776 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18777
18778 PRECISION is the maximum number of characters to output from
18779 STRING. PRECISION < 0 means don't truncate the string.
18780
18781 This is roughly equivalent to printf format specifiers:
18782
18783 FIELD_WIDTH PRECISION PRINTF
18784 ----------------------------------------
18785 -1 -1 %s
18786 -1 10 %.10s
18787 10 -1 %10s
18788 20 10 %20.10s
18789
18790 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18791 display them, and < 0 means obey the current buffer's value of
18792 enable_multibyte_characters.
18793
18794 Value is the number of columns displayed. */
18795
18796 static int
18797 display_string (string, lisp_string, face_string, face_string_pos,
18798 start, it, field_width, precision, max_x, multibyte)
18799 unsigned char *string;
18800 Lisp_Object lisp_string;
18801 Lisp_Object face_string;
18802 EMACS_INT face_string_pos;
18803 EMACS_INT start;
18804 struct it *it;
18805 int field_width, precision, max_x;
18806 int multibyte;
18807 {
18808 int hpos_at_start = it->hpos;
18809 int saved_face_id = it->face_id;
18810 struct glyph_row *row = it->glyph_row;
18811
18812 /* Initialize the iterator IT for iteration over STRING beginning
18813 with index START. */
18814 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
18815 precision, field_width, multibyte);
18816 if (string && STRINGP (lisp_string))
18817 /* LISP_STRING is the one returned by decode_mode_spec. We should
18818 ignore its text properties. */
18819 it->stop_charpos = -1;
18820
18821 /* If displaying STRING, set up the face of the iterator
18822 from LISP_STRING, if that's given. */
18823 if (STRINGP (face_string))
18824 {
18825 EMACS_INT endptr;
18826 struct face *face;
18827
18828 it->face_id
18829 = face_at_string_position (it->w, face_string, face_string_pos,
18830 0, it->region_beg_charpos,
18831 it->region_end_charpos,
18832 &endptr, it->base_face_id, 0);
18833 face = FACE_FROM_ID (it->f, it->face_id);
18834 it->face_box_p = face->box != FACE_NO_BOX;
18835 }
18836
18837 /* Set max_x to the maximum allowed X position. Don't let it go
18838 beyond the right edge of the window. */
18839 if (max_x <= 0)
18840 max_x = it->last_visible_x;
18841 else
18842 max_x = min (max_x, it->last_visible_x);
18843
18844 /* Skip over display elements that are not visible. because IT->w is
18845 hscrolled. */
18846 if (it->current_x < it->first_visible_x)
18847 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18848 MOVE_TO_POS | MOVE_TO_X);
18849
18850 row->ascent = it->max_ascent;
18851 row->height = it->max_ascent + it->max_descent;
18852 row->phys_ascent = it->max_phys_ascent;
18853 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18854 row->extra_line_spacing = it->max_extra_line_spacing;
18855
18856 /* This condition is for the case that we are called with current_x
18857 past last_visible_x. */
18858 while (it->current_x < max_x)
18859 {
18860 int x_before, x, n_glyphs_before, i, nglyphs;
18861
18862 /* Get the next display element. */
18863 if (!get_next_display_element (it))
18864 break;
18865
18866 /* Produce glyphs. */
18867 x_before = it->current_x;
18868 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18869 PRODUCE_GLYPHS (it);
18870
18871 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18872 i = 0;
18873 x = x_before;
18874 while (i < nglyphs)
18875 {
18876 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18877
18878 if (it->line_wrap != TRUNCATE
18879 && x + glyph->pixel_width > max_x)
18880 {
18881 /* End of continued line or max_x reached. */
18882 if (CHAR_GLYPH_PADDING_P (*glyph))
18883 {
18884 /* A wide character is unbreakable. */
18885 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18886 it->current_x = x_before;
18887 }
18888 else
18889 {
18890 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18891 it->current_x = x;
18892 }
18893 break;
18894 }
18895 else if (x + glyph->pixel_width >= it->first_visible_x)
18896 {
18897 /* Glyph is at least partially visible. */
18898 ++it->hpos;
18899 if (x < it->first_visible_x)
18900 it->glyph_row->x = x - it->first_visible_x;
18901 }
18902 else
18903 {
18904 /* Glyph is off the left margin of the display area.
18905 Should not happen. */
18906 abort ();
18907 }
18908
18909 row->ascent = max (row->ascent, it->max_ascent);
18910 row->height = max (row->height, it->max_ascent + it->max_descent);
18911 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18912 row->phys_height = max (row->phys_height,
18913 it->max_phys_ascent + it->max_phys_descent);
18914 row->extra_line_spacing = max (row->extra_line_spacing,
18915 it->max_extra_line_spacing);
18916 x += glyph->pixel_width;
18917 ++i;
18918 }
18919
18920 /* Stop if max_x reached. */
18921 if (i < nglyphs)
18922 break;
18923
18924 /* Stop at line ends. */
18925 if (ITERATOR_AT_END_OF_LINE_P (it))
18926 {
18927 it->continuation_lines_width = 0;
18928 break;
18929 }
18930
18931 set_iterator_to_next (it, 1);
18932
18933 /* Stop if truncating at the right edge. */
18934 if (it->line_wrap == TRUNCATE
18935 && it->current_x >= it->last_visible_x)
18936 {
18937 /* Add truncation mark, but don't do it if the line is
18938 truncated at a padding space. */
18939 if (IT_CHARPOS (*it) < it->string_nchars)
18940 {
18941 if (!FRAME_WINDOW_P (it->f))
18942 {
18943 int i, n;
18944
18945 if (it->current_x > it->last_visible_x)
18946 {
18947 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18948 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18949 break;
18950 for (n = row->used[TEXT_AREA]; i < n; ++i)
18951 {
18952 row->used[TEXT_AREA] = i;
18953 produce_special_glyphs (it, IT_TRUNCATION);
18954 }
18955 }
18956 produce_special_glyphs (it, IT_TRUNCATION);
18957 }
18958 it->glyph_row->truncated_on_right_p = 1;
18959 }
18960 break;
18961 }
18962 }
18963
18964 /* Maybe insert a truncation at the left. */
18965 if (it->first_visible_x
18966 && IT_CHARPOS (*it) > 0)
18967 {
18968 if (!FRAME_WINDOW_P (it->f))
18969 insert_left_trunc_glyphs (it);
18970 it->glyph_row->truncated_on_left_p = 1;
18971 }
18972
18973 it->face_id = saved_face_id;
18974
18975 /* Value is number of columns displayed. */
18976 return it->hpos - hpos_at_start;
18977 }
18978
18979
18980 \f
18981 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18982 appears as an element of LIST or as the car of an element of LIST.
18983 If PROPVAL is a list, compare each element against LIST in that
18984 way, and return 1/2 if any element of PROPVAL is found in LIST.
18985 Otherwise return 0. This function cannot quit.
18986 The return value is 2 if the text is invisible but with an ellipsis
18987 and 1 if it's invisible and without an ellipsis. */
18988
18989 int
18990 invisible_p (propval, list)
18991 register Lisp_Object propval;
18992 Lisp_Object list;
18993 {
18994 register Lisp_Object tail, proptail;
18995
18996 for (tail = list; CONSP (tail); tail = XCDR (tail))
18997 {
18998 register Lisp_Object tem;
18999 tem = XCAR (tail);
19000 if (EQ (propval, tem))
19001 return 1;
19002 if (CONSP (tem) && EQ (propval, XCAR (tem)))
19003 return NILP (XCDR (tem)) ? 1 : 2;
19004 }
19005
19006 if (CONSP (propval))
19007 {
19008 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
19009 {
19010 Lisp_Object propelt;
19011 propelt = XCAR (proptail);
19012 for (tail = list; CONSP (tail); tail = XCDR (tail))
19013 {
19014 register Lisp_Object tem;
19015 tem = XCAR (tail);
19016 if (EQ (propelt, tem))
19017 return 1;
19018 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
19019 return NILP (XCDR (tem)) ? 1 : 2;
19020 }
19021 }
19022 }
19023
19024 return 0;
19025 }
19026
19027 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
19028 doc: /* Non-nil if the property makes the text invisible.
19029 POS-OR-PROP can be a marker or number, in which case it is taken to be
19030 a position in the current buffer and the value of the `invisible' property
19031 is checked; or it can be some other value, which is then presumed to be the
19032 value of the `invisible' property of the text of interest.
19033 The non-nil value returned can be t for truly invisible text or something
19034 else if the text is replaced by an ellipsis. */)
19035 (pos_or_prop)
19036 Lisp_Object pos_or_prop;
19037 {
19038 Lisp_Object prop
19039 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
19040 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
19041 : pos_or_prop);
19042 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
19043 return (invis == 0 ? Qnil
19044 : invis == 1 ? Qt
19045 : make_number (invis));
19046 }
19047
19048 /* Calculate a width or height in pixels from a specification using
19049 the following elements:
19050
19051 SPEC ::=
19052 NUM - a (fractional) multiple of the default font width/height
19053 (NUM) - specifies exactly NUM pixels
19054 UNIT - a fixed number of pixels, see below.
19055 ELEMENT - size of a display element in pixels, see below.
19056 (NUM . SPEC) - equals NUM * SPEC
19057 (+ SPEC SPEC ...) - add pixel values
19058 (- SPEC SPEC ...) - subtract pixel values
19059 (- SPEC) - negate pixel value
19060
19061 NUM ::=
19062 INT or FLOAT - a number constant
19063 SYMBOL - use symbol's (buffer local) variable binding.
19064
19065 UNIT ::=
19066 in - pixels per inch *)
19067 mm - pixels per 1/1000 meter *)
19068 cm - pixels per 1/100 meter *)
19069 width - width of current font in pixels.
19070 height - height of current font in pixels.
19071
19072 *) using the ratio(s) defined in display-pixels-per-inch.
19073
19074 ELEMENT ::=
19075
19076 left-fringe - left fringe width in pixels
19077 right-fringe - right fringe width in pixels
19078
19079 left-margin - left margin width in pixels
19080 right-margin - right margin width in pixels
19081
19082 scroll-bar - scroll-bar area width in pixels
19083
19084 Examples:
19085
19086 Pixels corresponding to 5 inches:
19087 (5 . in)
19088
19089 Total width of non-text areas on left side of window (if scroll-bar is on left):
19090 '(space :width (+ left-fringe left-margin scroll-bar))
19091
19092 Align to first text column (in header line):
19093 '(space :align-to 0)
19094
19095 Align to middle of text area minus half the width of variable `my-image'
19096 containing a loaded image:
19097 '(space :align-to (0.5 . (- text my-image)))
19098
19099 Width of left margin minus width of 1 character in the default font:
19100 '(space :width (- left-margin 1))
19101
19102 Width of left margin minus width of 2 characters in the current font:
19103 '(space :width (- left-margin (2 . width)))
19104
19105 Center 1 character over left-margin (in header line):
19106 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
19107
19108 Different ways to express width of left fringe plus left margin minus one pixel:
19109 '(space :width (- (+ left-fringe left-margin) (1)))
19110 '(space :width (+ left-fringe left-margin (- (1))))
19111 '(space :width (+ left-fringe left-margin (-1)))
19112
19113 */
19114
19115 #define NUMVAL(X) \
19116 ((INTEGERP (X) || FLOATP (X)) \
19117 ? XFLOATINT (X) \
19118 : - 1)
19119
19120 int
19121 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
19122 double *res;
19123 struct it *it;
19124 Lisp_Object prop;
19125 struct font *font;
19126 int width_p, *align_to;
19127 {
19128 double pixels;
19129
19130 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
19131 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
19132
19133 if (NILP (prop))
19134 return OK_PIXELS (0);
19135
19136 xassert (FRAME_LIVE_P (it->f));
19137
19138 if (SYMBOLP (prop))
19139 {
19140 if (SCHARS (SYMBOL_NAME (prop)) == 2)
19141 {
19142 char *unit = SDATA (SYMBOL_NAME (prop));
19143
19144 if (unit[0] == 'i' && unit[1] == 'n')
19145 pixels = 1.0;
19146 else if (unit[0] == 'm' && unit[1] == 'm')
19147 pixels = 25.4;
19148 else if (unit[0] == 'c' && unit[1] == 'm')
19149 pixels = 2.54;
19150 else
19151 pixels = 0;
19152 if (pixels > 0)
19153 {
19154 double ppi;
19155 #ifdef HAVE_WINDOW_SYSTEM
19156 if (FRAME_WINDOW_P (it->f)
19157 && (ppi = (width_p
19158 ? FRAME_X_DISPLAY_INFO (it->f)->resx
19159 : FRAME_X_DISPLAY_INFO (it->f)->resy),
19160 ppi > 0))
19161 return OK_PIXELS (ppi / pixels);
19162 #endif
19163
19164 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
19165 || (CONSP (Vdisplay_pixels_per_inch)
19166 && (ppi = (width_p
19167 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
19168 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
19169 ppi > 0)))
19170 return OK_PIXELS (ppi / pixels);
19171
19172 return 0;
19173 }
19174 }
19175
19176 #ifdef HAVE_WINDOW_SYSTEM
19177 if (EQ (prop, Qheight))
19178 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
19179 if (EQ (prop, Qwidth))
19180 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
19181 #else
19182 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
19183 return OK_PIXELS (1);
19184 #endif
19185
19186 if (EQ (prop, Qtext))
19187 return OK_PIXELS (width_p
19188 ? window_box_width (it->w, TEXT_AREA)
19189 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
19190
19191 if (align_to && *align_to < 0)
19192 {
19193 *res = 0;
19194 if (EQ (prop, Qleft))
19195 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
19196 if (EQ (prop, Qright))
19197 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
19198 if (EQ (prop, Qcenter))
19199 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
19200 + window_box_width (it->w, TEXT_AREA) / 2);
19201 if (EQ (prop, Qleft_fringe))
19202 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19203 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
19204 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
19205 if (EQ (prop, Qright_fringe))
19206 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19207 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19208 : window_box_right_offset (it->w, TEXT_AREA));
19209 if (EQ (prop, Qleft_margin))
19210 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
19211 if (EQ (prop, Qright_margin))
19212 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
19213 if (EQ (prop, Qscroll_bar))
19214 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
19215 ? 0
19216 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
19217 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
19218 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19219 : 0)));
19220 }
19221 else
19222 {
19223 if (EQ (prop, Qleft_fringe))
19224 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
19225 if (EQ (prop, Qright_fringe))
19226 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
19227 if (EQ (prop, Qleft_margin))
19228 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
19229 if (EQ (prop, Qright_margin))
19230 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
19231 if (EQ (prop, Qscroll_bar))
19232 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
19233 }
19234
19235 prop = Fbuffer_local_value (prop, it->w->buffer);
19236 }
19237
19238 if (INTEGERP (prop) || FLOATP (prop))
19239 {
19240 int base_unit = (width_p
19241 ? FRAME_COLUMN_WIDTH (it->f)
19242 : FRAME_LINE_HEIGHT (it->f));
19243 return OK_PIXELS (XFLOATINT (prop) * base_unit);
19244 }
19245
19246 if (CONSP (prop))
19247 {
19248 Lisp_Object car = XCAR (prop);
19249 Lisp_Object cdr = XCDR (prop);
19250
19251 if (SYMBOLP (car))
19252 {
19253 #ifdef HAVE_WINDOW_SYSTEM
19254 if (FRAME_WINDOW_P (it->f)
19255 && valid_image_p (prop))
19256 {
19257 int id = lookup_image (it->f, prop);
19258 struct image *img = IMAGE_FROM_ID (it->f, id);
19259
19260 return OK_PIXELS (width_p ? img->width : img->height);
19261 }
19262 #endif
19263 if (EQ (car, Qplus) || EQ (car, Qminus))
19264 {
19265 int first = 1;
19266 double px;
19267
19268 pixels = 0;
19269 while (CONSP (cdr))
19270 {
19271 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
19272 font, width_p, align_to))
19273 return 0;
19274 if (first)
19275 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
19276 else
19277 pixels += px;
19278 cdr = XCDR (cdr);
19279 }
19280 if (EQ (car, Qminus))
19281 pixels = -pixels;
19282 return OK_PIXELS (pixels);
19283 }
19284
19285 car = Fbuffer_local_value (car, it->w->buffer);
19286 }
19287
19288 if (INTEGERP (car) || FLOATP (car))
19289 {
19290 double fact;
19291 pixels = XFLOATINT (car);
19292 if (NILP (cdr))
19293 return OK_PIXELS (pixels);
19294 if (calc_pixel_width_or_height (&fact, it, cdr,
19295 font, width_p, align_to))
19296 return OK_PIXELS (pixels * fact);
19297 return 0;
19298 }
19299
19300 return 0;
19301 }
19302
19303 return 0;
19304 }
19305
19306 \f
19307 /***********************************************************************
19308 Glyph Display
19309 ***********************************************************************/
19310
19311 #ifdef HAVE_WINDOW_SYSTEM
19312
19313 #if GLYPH_DEBUG
19314
19315 void
19316 dump_glyph_string (s)
19317 struct glyph_string *s;
19318 {
19319 fprintf (stderr, "glyph string\n");
19320 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
19321 s->x, s->y, s->width, s->height);
19322 fprintf (stderr, " ybase = %d\n", s->ybase);
19323 fprintf (stderr, " hl = %d\n", s->hl);
19324 fprintf (stderr, " left overhang = %d, right = %d\n",
19325 s->left_overhang, s->right_overhang);
19326 fprintf (stderr, " nchars = %d\n", s->nchars);
19327 fprintf (stderr, " extends to end of line = %d\n",
19328 s->extends_to_end_of_line_p);
19329 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
19330 fprintf (stderr, " bg width = %d\n", s->background_width);
19331 }
19332
19333 #endif /* GLYPH_DEBUG */
19334
19335 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
19336 of XChar2b structures for S; it can't be allocated in
19337 init_glyph_string because it must be allocated via `alloca'. W
19338 is the window on which S is drawn. ROW and AREA are the glyph row
19339 and area within the row from which S is constructed. START is the
19340 index of the first glyph structure covered by S. HL is a
19341 face-override for drawing S. */
19342
19343 #ifdef HAVE_NTGUI
19344 #define OPTIONAL_HDC(hdc) hdc,
19345 #define DECLARE_HDC(hdc) HDC hdc;
19346 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
19347 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
19348 #endif
19349
19350 #ifndef OPTIONAL_HDC
19351 #define OPTIONAL_HDC(hdc)
19352 #define DECLARE_HDC(hdc)
19353 #define ALLOCATE_HDC(hdc, f)
19354 #define RELEASE_HDC(hdc, f)
19355 #endif
19356
19357 static void
19358 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
19359 struct glyph_string *s;
19360 DECLARE_HDC (hdc)
19361 XChar2b *char2b;
19362 struct window *w;
19363 struct glyph_row *row;
19364 enum glyph_row_area area;
19365 int start;
19366 enum draw_glyphs_face hl;
19367 {
19368 bzero (s, sizeof *s);
19369 s->w = w;
19370 s->f = XFRAME (w->frame);
19371 #ifdef HAVE_NTGUI
19372 s->hdc = hdc;
19373 #endif
19374 s->display = FRAME_X_DISPLAY (s->f);
19375 s->window = FRAME_X_WINDOW (s->f);
19376 s->char2b = char2b;
19377 s->hl = hl;
19378 s->row = row;
19379 s->area = area;
19380 s->first_glyph = row->glyphs[area] + start;
19381 s->height = row->height;
19382 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
19383 s->ybase = s->y + row->ascent;
19384 }
19385
19386
19387 /* Append the list of glyph strings with head H and tail T to the list
19388 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19389
19390 static INLINE void
19391 append_glyph_string_lists (head, tail, h, t)
19392 struct glyph_string **head, **tail;
19393 struct glyph_string *h, *t;
19394 {
19395 if (h)
19396 {
19397 if (*head)
19398 (*tail)->next = h;
19399 else
19400 *head = h;
19401 h->prev = *tail;
19402 *tail = t;
19403 }
19404 }
19405
19406
19407 /* Prepend the list of glyph strings with head H and tail T to the
19408 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19409 result. */
19410
19411 static INLINE void
19412 prepend_glyph_string_lists (head, tail, h, t)
19413 struct glyph_string **head, **tail;
19414 struct glyph_string *h, *t;
19415 {
19416 if (h)
19417 {
19418 if (*head)
19419 (*head)->prev = t;
19420 else
19421 *tail = t;
19422 t->next = *head;
19423 *head = h;
19424 }
19425 }
19426
19427
19428 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19429 Set *HEAD and *TAIL to the resulting list. */
19430
19431 static INLINE void
19432 append_glyph_string (head, tail, s)
19433 struct glyph_string **head, **tail;
19434 struct glyph_string *s;
19435 {
19436 s->next = s->prev = NULL;
19437 append_glyph_string_lists (head, tail, s, s);
19438 }
19439
19440
19441 /* Get face and two-byte form of character C in face FACE_ID on frame
19442 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19443 means we want to display multibyte text. DISPLAY_P non-zero means
19444 make sure that X resources for the face returned are allocated.
19445 Value is a pointer to a realized face that is ready for display if
19446 DISPLAY_P is non-zero. */
19447
19448 static INLINE struct face *
19449 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19450 struct frame *f;
19451 int c, face_id;
19452 XChar2b *char2b;
19453 int multibyte_p, display_p;
19454 {
19455 struct face *face = FACE_FROM_ID (f, face_id);
19456
19457 if (face->font)
19458 {
19459 unsigned code = face->font->driver->encode_char (face->font, c);
19460
19461 if (code != FONT_INVALID_CODE)
19462 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19463 else
19464 STORE_XCHAR2B (char2b, 0, 0);
19465 }
19466
19467 /* Make sure X resources of the face are allocated. */
19468 #ifdef HAVE_X_WINDOWS
19469 if (display_p)
19470 #endif
19471 {
19472 xassert (face != NULL);
19473 PREPARE_FACE_FOR_DISPLAY (f, face);
19474 }
19475
19476 return face;
19477 }
19478
19479
19480 /* Get face and two-byte form of character glyph GLYPH on frame F.
19481 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19482 a pointer to a realized face that is ready for display. */
19483
19484 static INLINE struct face *
19485 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19486 struct frame *f;
19487 struct glyph *glyph;
19488 XChar2b *char2b;
19489 int *two_byte_p;
19490 {
19491 struct face *face;
19492
19493 xassert (glyph->type == CHAR_GLYPH);
19494 face = FACE_FROM_ID (f, glyph->face_id);
19495
19496 if (two_byte_p)
19497 *two_byte_p = 0;
19498
19499 if (face->font)
19500 {
19501 unsigned code;
19502
19503 if (CHAR_BYTE8_P (glyph->u.ch))
19504 code = CHAR_TO_BYTE8 (glyph->u.ch);
19505 else
19506 code = face->font->driver->encode_char (face->font, glyph->u.ch);
19507
19508 if (code != FONT_INVALID_CODE)
19509 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19510 else
19511 STORE_XCHAR2B (char2b, 0, 0);
19512 }
19513
19514 /* Make sure X resources of the face are allocated. */
19515 xassert (face != NULL);
19516 PREPARE_FACE_FOR_DISPLAY (f, face);
19517 return face;
19518 }
19519
19520
19521 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
19522 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
19523
19524 static INLINE int
19525 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
19526 {
19527 unsigned code;
19528
19529 if (CHAR_BYTE8_P (c))
19530 code = CHAR_TO_BYTE8 (c);
19531 else
19532 code = font->driver->encode_char (font, c);
19533
19534 if (code == FONT_INVALID_CODE)
19535 return 0;
19536 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19537 return 1;
19538 }
19539
19540
19541 /* Fill glyph string S with composition components specified by S->cmp.
19542
19543 BASE_FACE is the base face of the composition.
19544 S->cmp_from is the index of the first component for S.
19545
19546 OVERLAPS non-zero means S should draw the foreground only, and use
19547 its physical height for clipping. See also draw_glyphs.
19548
19549 Value is the index of a component not in S. */
19550
19551 static int
19552 fill_composite_glyph_string (s, base_face, overlaps)
19553 struct glyph_string *s;
19554 struct face *base_face;
19555 int overlaps;
19556 {
19557 int i;
19558 /* For all glyphs of this composition, starting at the offset
19559 S->cmp_from, until we reach the end of the definition or encounter a
19560 glyph that requires the different face, add it to S. */
19561 struct face *face;
19562
19563 xassert (s);
19564
19565 s->for_overlaps = overlaps;
19566 s->face = NULL;
19567 s->font = NULL;
19568 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
19569 {
19570 int c = COMPOSITION_GLYPH (s->cmp, i);
19571
19572 if (c != '\t')
19573 {
19574 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19575 -1, Qnil);
19576
19577 face = get_char_face_and_encoding (s->f, c, face_id,
19578 s->char2b + i, 1, 1);
19579 if (face)
19580 {
19581 if (! s->face)
19582 {
19583 s->face = face;
19584 s->font = s->face->font;
19585 }
19586 else if (s->face != face)
19587 break;
19588 }
19589 }
19590 ++s->nchars;
19591 }
19592 s->cmp_to = i;
19593
19594 /* All glyph strings for the same composition has the same width,
19595 i.e. the width set for the first component of the composition. */
19596 s->width = s->first_glyph->pixel_width;
19597
19598 /* If the specified font could not be loaded, use the frame's
19599 default font, but record the fact that we couldn't load it in
19600 the glyph string so that we can draw rectangles for the
19601 characters of the glyph string. */
19602 if (s->font == NULL)
19603 {
19604 s->font_not_found_p = 1;
19605 s->font = FRAME_FONT (s->f);
19606 }
19607
19608 /* Adjust base line for subscript/superscript text. */
19609 s->ybase += s->first_glyph->voffset;
19610
19611 /* This glyph string must always be drawn with 16-bit functions. */
19612 s->two_byte_p = 1;
19613
19614 return s->cmp_to;
19615 }
19616
19617 static int
19618 fill_gstring_glyph_string (s, face_id, start, end, overlaps)
19619 struct glyph_string *s;
19620 int face_id;
19621 int start, end, overlaps;
19622 {
19623 struct glyph *glyph, *last;
19624 Lisp_Object lgstring;
19625 int i;
19626
19627 s->for_overlaps = overlaps;
19628 glyph = s->row->glyphs[s->area] + start;
19629 last = s->row->glyphs[s->area] + end;
19630 s->cmp_id = glyph->u.cmp.id;
19631 s->cmp_from = glyph->u.cmp.from;
19632 s->cmp_to = glyph->u.cmp.to + 1;
19633 s->face = FACE_FROM_ID (s->f, face_id);
19634 lgstring = composition_gstring_from_id (s->cmp_id);
19635 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
19636 glyph++;
19637 while (glyph < last
19638 && glyph->u.cmp.automatic
19639 && glyph->u.cmp.id == s->cmp_id
19640 && s->cmp_to == glyph->u.cmp.from)
19641 s->cmp_to = (glyph++)->u.cmp.to + 1;
19642
19643 for (i = s->cmp_from; i < s->cmp_to; i++)
19644 {
19645 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
19646 unsigned code = LGLYPH_CODE (lglyph);
19647
19648 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
19649 }
19650 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
19651 return glyph - s->row->glyphs[s->area];
19652 }
19653
19654
19655 /* Fill glyph string S from a sequence of character glyphs.
19656
19657 FACE_ID is the face id of the string. START is the index of the
19658 first glyph to consider, END is the index of the last + 1.
19659 OVERLAPS non-zero means S should draw the foreground only, and use
19660 its physical height for clipping. See also draw_glyphs.
19661
19662 Value is the index of the first glyph not in S. */
19663
19664 static int
19665 fill_glyph_string (s, face_id, start, end, overlaps)
19666 struct glyph_string *s;
19667 int face_id;
19668 int start, end, overlaps;
19669 {
19670 struct glyph *glyph, *last;
19671 int voffset;
19672 int glyph_not_available_p;
19673
19674 xassert (s->f == XFRAME (s->w->frame));
19675 xassert (s->nchars == 0);
19676 xassert (start >= 0 && end > start);
19677
19678 s->for_overlaps = overlaps;
19679 glyph = s->row->glyphs[s->area] + start;
19680 last = s->row->glyphs[s->area] + end;
19681 voffset = glyph->voffset;
19682 s->padding_p = glyph->padding_p;
19683 glyph_not_available_p = glyph->glyph_not_available_p;
19684
19685 while (glyph < last
19686 && glyph->type == CHAR_GLYPH
19687 && glyph->voffset == voffset
19688 /* Same face id implies same font, nowadays. */
19689 && glyph->face_id == face_id
19690 && glyph->glyph_not_available_p == glyph_not_available_p)
19691 {
19692 int two_byte_p;
19693
19694 s->face = get_glyph_face_and_encoding (s->f, glyph,
19695 s->char2b + s->nchars,
19696 &two_byte_p);
19697 s->two_byte_p = two_byte_p;
19698 ++s->nchars;
19699 xassert (s->nchars <= end - start);
19700 s->width += glyph->pixel_width;
19701 if (glyph++->padding_p != s->padding_p)
19702 break;
19703 }
19704
19705 s->font = s->face->font;
19706
19707 /* If the specified font could not be loaded, use the frame's font,
19708 but record the fact that we couldn't load it in
19709 S->font_not_found_p so that we can draw rectangles for the
19710 characters of the glyph string. */
19711 if (s->font == NULL || glyph_not_available_p)
19712 {
19713 s->font_not_found_p = 1;
19714 s->font = FRAME_FONT (s->f);
19715 }
19716
19717 /* Adjust base line for subscript/superscript text. */
19718 s->ybase += voffset;
19719
19720 xassert (s->face && s->face->gc);
19721 return glyph - s->row->glyphs[s->area];
19722 }
19723
19724
19725 /* Fill glyph string S from image glyph S->first_glyph. */
19726
19727 static void
19728 fill_image_glyph_string (s)
19729 struct glyph_string *s;
19730 {
19731 xassert (s->first_glyph->type == IMAGE_GLYPH);
19732 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19733 xassert (s->img);
19734 s->slice = s->first_glyph->slice;
19735 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19736 s->font = s->face->font;
19737 s->width = s->first_glyph->pixel_width;
19738
19739 /* Adjust base line for subscript/superscript text. */
19740 s->ybase += s->first_glyph->voffset;
19741 }
19742
19743
19744 /* Fill glyph string S from a sequence of stretch glyphs.
19745
19746 ROW is the glyph row in which the glyphs are found, AREA is the
19747 area within the row. START is the index of the first glyph to
19748 consider, END is the index of the last + 1.
19749
19750 Value is the index of the first glyph not in S. */
19751
19752 static int
19753 fill_stretch_glyph_string (s, row, area, start, end)
19754 struct glyph_string *s;
19755 struct glyph_row *row;
19756 enum glyph_row_area area;
19757 int start, end;
19758 {
19759 struct glyph *glyph, *last;
19760 int voffset, face_id;
19761
19762 xassert (s->first_glyph->type == STRETCH_GLYPH);
19763
19764 glyph = s->row->glyphs[s->area] + start;
19765 last = s->row->glyphs[s->area] + end;
19766 face_id = glyph->face_id;
19767 s->face = FACE_FROM_ID (s->f, face_id);
19768 s->font = s->face->font;
19769 s->width = glyph->pixel_width;
19770 s->nchars = 1;
19771 voffset = glyph->voffset;
19772
19773 for (++glyph;
19774 (glyph < last
19775 && glyph->type == STRETCH_GLYPH
19776 && glyph->voffset == voffset
19777 && glyph->face_id == face_id);
19778 ++glyph)
19779 s->width += glyph->pixel_width;
19780
19781 /* Adjust base line for subscript/superscript text. */
19782 s->ybase += voffset;
19783
19784 /* The case that face->gc == 0 is handled when drawing the glyph
19785 string by calling PREPARE_FACE_FOR_DISPLAY. */
19786 xassert (s->face);
19787 return glyph - s->row->glyphs[s->area];
19788 }
19789
19790 static struct font_metrics *
19791 get_per_char_metric (f, font, char2b)
19792 struct frame *f;
19793 struct font *font;
19794 XChar2b *char2b;
19795 {
19796 static struct font_metrics metrics;
19797 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19798
19799 if (! font || code == FONT_INVALID_CODE)
19800 return NULL;
19801 font->driver->text_extents (font, &code, 1, &metrics);
19802 return &metrics;
19803 }
19804
19805 /* EXPORT for RIF:
19806 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19807 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19808 assumed to be zero. */
19809
19810 void
19811 x_get_glyph_overhangs (glyph, f, left, right)
19812 struct glyph *glyph;
19813 struct frame *f;
19814 int *left, *right;
19815 {
19816 *left = *right = 0;
19817
19818 if (glyph->type == CHAR_GLYPH)
19819 {
19820 struct face *face;
19821 XChar2b char2b;
19822 struct font_metrics *pcm;
19823
19824 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19825 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19826 {
19827 if (pcm->rbearing > pcm->width)
19828 *right = pcm->rbearing - pcm->width;
19829 if (pcm->lbearing < 0)
19830 *left = -pcm->lbearing;
19831 }
19832 }
19833 else if (glyph->type == COMPOSITE_GLYPH)
19834 {
19835 if (! glyph->u.cmp.automatic)
19836 {
19837 struct composition *cmp = composition_table[glyph->u.cmp.id];
19838
19839 if (cmp->rbearing > cmp->pixel_width)
19840 *right = cmp->rbearing - cmp->pixel_width;
19841 if (cmp->lbearing < 0)
19842 *left = - cmp->lbearing;
19843 }
19844 else
19845 {
19846 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
19847 struct font_metrics metrics;
19848
19849 composition_gstring_width (gstring, glyph->u.cmp.from,
19850 glyph->u.cmp.to + 1, &metrics);
19851 if (metrics.rbearing > metrics.width)
19852 *right = metrics.rbearing - metrics.width;
19853 if (metrics.lbearing < 0)
19854 *left = - metrics.lbearing;
19855 }
19856 }
19857 }
19858
19859
19860 /* Return the index of the first glyph preceding glyph string S that
19861 is overwritten by S because of S's left overhang. Value is -1
19862 if no glyphs are overwritten. */
19863
19864 static int
19865 left_overwritten (s)
19866 struct glyph_string *s;
19867 {
19868 int k;
19869
19870 if (s->left_overhang)
19871 {
19872 int x = 0, i;
19873 struct glyph *glyphs = s->row->glyphs[s->area];
19874 int first = s->first_glyph - glyphs;
19875
19876 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19877 x -= glyphs[i].pixel_width;
19878
19879 k = i + 1;
19880 }
19881 else
19882 k = -1;
19883
19884 return k;
19885 }
19886
19887
19888 /* Return the index of the first glyph preceding glyph string S that
19889 is overwriting S because of its right overhang. Value is -1 if no
19890 glyph in front of S overwrites S. */
19891
19892 static int
19893 left_overwriting (s)
19894 struct glyph_string *s;
19895 {
19896 int i, k, x;
19897 struct glyph *glyphs = s->row->glyphs[s->area];
19898 int first = s->first_glyph - glyphs;
19899
19900 k = -1;
19901 x = 0;
19902 for (i = first - 1; i >= 0; --i)
19903 {
19904 int left, right;
19905 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19906 if (x + right > 0)
19907 k = i;
19908 x -= glyphs[i].pixel_width;
19909 }
19910
19911 return k;
19912 }
19913
19914
19915 /* Return the index of the last glyph following glyph string S that is
19916 overwritten by S because of S's right overhang. Value is -1 if
19917 no such glyph is found. */
19918
19919 static int
19920 right_overwritten (s)
19921 struct glyph_string *s;
19922 {
19923 int k = -1;
19924
19925 if (s->right_overhang)
19926 {
19927 int x = 0, i;
19928 struct glyph *glyphs = s->row->glyphs[s->area];
19929 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19930 int end = s->row->used[s->area];
19931
19932 for (i = first; i < end && s->right_overhang > x; ++i)
19933 x += glyphs[i].pixel_width;
19934
19935 k = i;
19936 }
19937
19938 return k;
19939 }
19940
19941
19942 /* Return the index of the last glyph following glyph string S that
19943 overwrites S because of its left overhang. Value is negative
19944 if no such glyph is found. */
19945
19946 static int
19947 right_overwriting (s)
19948 struct glyph_string *s;
19949 {
19950 int i, k, x;
19951 int end = s->row->used[s->area];
19952 struct glyph *glyphs = s->row->glyphs[s->area];
19953 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19954
19955 k = -1;
19956 x = 0;
19957 for (i = first; i < end; ++i)
19958 {
19959 int left, right;
19960 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19961 if (x - left < 0)
19962 k = i;
19963 x += glyphs[i].pixel_width;
19964 }
19965
19966 return k;
19967 }
19968
19969
19970 /* Set background width of glyph string S. START is the index of the
19971 first glyph following S. LAST_X is the right-most x-position + 1
19972 in the drawing area. */
19973
19974 static INLINE void
19975 set_glyph_string_background_width (s, start, last_x)
19976 struct glyph_string *s;
19977 int start;
19978 int last_x;
19979 {
19980 /* If the face of this glyph string has to be drawn to the end of
19981 the drawing area, set S->extends_to_end_of_line_p. */
19982
19983 if (start == s->row->used[s->area]
19984 && s->area == TEXT_AREA
19985 && ((s->row->fill_line_p
19986 && (s->hl == DRAW_NORMAL_TEXT
19987 || s->hl == DRAW_IMAGE_RAISED
19988 || s->hl == DRAW_IMAGE_SUNKEN))
19989 || s->hl == DRAW_MOUSE_FACE))
19990 s->extends_to_end_of_line_p = 1;
19991
19992 /* If S extends its face to the end of the line, set its
19993 background_width to the distance to the right edge of the drawing
19994 area. */
19995 if (s->extends_to_end_of_line_p)
19996 s->background_width = last_x - s->x + 1;
19997 else
19998 s->background_width = s->width;
19999 }
20000
20001
20002 /* Compute overhangs and x-positions for glyph string S and its
20003 predecessors, or successors. X is the starting x-position for S.
20004 BACKWARD_P non-zero means process predecessors. */
20005
20006 static void
20007 compute_overhangs_and_x (s, x, backward_p)
20008 struct glyph_string *s;
20009 int x;
20010 int backward_p;
20011 {
20012 if (backward_p)
20013 {
20014 while (s)
20015 {
20016 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20017 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20018 x -= s->width;
20019 s->x = x;
20020 s = s->prev;
20021 }
20022 }
20023 else
20024 {
20025 while (s)
20026 {
20027 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
20028 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
20029 s->x = x;
20030 x += s->width;
20031 s = s->next;
20032 }
20033 }
20034 }
20035
20036
20037
20038 /* The following macros are only called from draw_glyphs below.
20039 They reference the following parameters of that function directly:
20040 `w', `row', `area', and `overlap_p'
20041 as well as the following local variables:
20042 `s', `f', and `hdc' (in W32) */
20043
20044 #ifdef HAVE_NTGUI
20045 /* On W32, silently add local `hdc' variable to argument list of
20046 init_glyph_string. */
20047 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20048 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20049 #else
20050 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20051 init_glyph_string (s, char2b, w, row, area, start, hl)
20052 #endif
20053
20054 /* Add a glyph string for a stretch glyph to the list of strings
20055 between HEAD and TAIL. START is the index of the stretch glyph in
20056 row area AREA of glyph row ROW. END is the index of the last glyph
20057 in that glyph row area. X is the current output position assigned
20058 to the new glyph string constructed. HL overrides that face of the
20059 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20060 is the right-most x-position of the drawing area. */
20061
20062 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20063 and below -- keep them on one line. */
20064 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20065 do \
20066 { \
20067 s = (struct glyph_string *) alloca (sizeof *s); \
20068 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20069 START = fill_stretch_glyph_string (s, row, area, START, END); \
20070 append_glyph_string (&HEAD, &TAIL, s); \
20071 s->x = (X); \
20072 } \
20073 while (0)
20074
20075
20076 /* Add a glyph string for an image glyph to the list of strings
20077 between HEAD and TAIL. START is the index of the image glyph in
20078 row area AREA of glyph row ROW. END is the index of the last glyph
20079 in that glyph row area. X is the current output position assigned
20080 to the new glyph string constructed. HL overrides that face of the
20081 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20082 is the right-most x-position of the drawing area. */
20083
20084 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20085 do \
20086 { \
20087 s = (struct glyph_string *) alloca (sizeof *s); \
20088 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20089 fill_image_glyph_string (s); \
20090 append_glyph_string (&HEAD, &TAIL, s); \
20091 ++START; \
20092 s->x = (X); \
20093 } \
20094 while (0)
20095
20096
20097 /* Add a glyph string for a sequence of character glyphs to the list
20098 of strings between HEAD and TAIL. START is the index of the first
20099 glyph in row area AREA of glyph row ROW that is part of the new
20100 glyph string. END is the index of the last glyph in that glyph row
20101 area. X is the current output position assigned to the new glyph
20102 string constructed. HL overrides that face of the glyph; e.g. it
20103 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20104 right-most x-position of the drawing area. */
20105
20106 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20107 do \
20108 { \
20109 int face_id; \
20110 XChar2b *char2b; \
20111 \
20112 face_id = (row)->glyphs[area][START].face_id; \
20113 \
20114 s = (struct glyph_string *) alloca (sizeof *s); \
20115 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20116 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20117 append_glyph_string (&HEAD, &TAIL, s); \
20118 s->x = (X); \
20119 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20120 } \
20121 while (0)
20122
20123
20124 /* Add a glyph string for a composite sequence to the list of strings
20125 between HEAD and TAIL. START is the index of the first glyph in
20126 row area AREA of glyph row ROW that is part of the new glyph
20127 string. END is the index of the last glyph in that glyph row area.
20128 X is the current output position assigned to the new glyph string
20129 constructed. HL overrides that face of the glyph; e.g. it is
20130 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20131 x-position of the drawing area. */
20132
20133 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20134 do { \
20135 int face_id = (row)->glyphs[area][START].face_id; \
20136 struct face *base_face = FACE_FROM_ID (f, face_id); \
20137 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
20138 struct composition *cmp = composition_table[cmp_id]; \
20139 XChar2b *char2b; \
20140 struct glyph_string *first_s; \
20141 int n; \
20142 \
20143 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20144 \
20145 /* Make glyph_strings for each glyph sequence that is drawable by \
20146 the same face, and append them to HEAD/TAIL. */ \
20147 for (n = 0; n < cmp->glyph_len;) \
20148 { \
20149 s = (struct glyph_string *) alloca (sizeof *s); \
20150 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20151 append_glyph_string (&(HEAD), &(TAIL), s); \
20152 s->cmp = cmp; \
20153 s->cmp_from = n; \
20154 s->x = (X); \
20155 if (n == 0) \
20156 first_s = s; \
20157 n = fill_composite_glyph_string (s, base_face, overlaps); \
20158 } \
20159 \
20160 ++START; \
20161 s = first_s; \
20162 } while (0)
20163
20164
20165 /* Add a glyph string for a glyph-string sequence to the list of strings
20166 between HEAD and TAIL. */
20167
20168 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20169 do { \
20170 int face_id; \
20171 XChar2b *char2b; \
20172 Lisp_Object gstring; \
20173 \
20174 face_id = (row)->glyphs[area][START].face_id; \
20175 gstring = (composition_gstring_from_id \
20176 ((row)->glyphs[area][START].u.cmp.id)); \
20177 s = (struct glyph_string *) alloca (sizeof *s); \
20178 char2b = (XChar2b *) alloca ((sizeof *char2b) \
20179 * LGSTRING_GLYPH_LEN (gstring)); \
20180 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20181 append_glyph_string (&(HEAD), &(TAIL), s); \
20182 s->x = (X); \
20183 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
20184 } while (0)
20185
20186
20187 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20188 of AREA of glyph row ROW on window W between indices START and END.
20189 HL overrides the face for drawing glyph strings, e.g. it is
20190 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20191 x-positions of the drawing area.
20192
20193 This is an ugly monster macro construct because we must use alloca
20194 to allocate glyph strings (because draw_glyphs can be called
20195 asynchronously). */
20196
20197 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20198 do \
20199 { \
20200 HEAD = TAIL = NULL; \
20201 while (START < END) \
20202 { \
20203 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20204 switch (first_glyph->type) \
20205 { \
20206 case CHAR_GLYPH: \
20207 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20208 HL, X, LAST_X); \
20209 break; \
20210 \
20211 case COMPOSITE_GLYPH: \
20212 if (first_glyph->u.cmp.automatic) \
20213 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
20214 HL, X, LAST_X); \
20215 else \
20216 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20217 HL, X, LAST_X); \
20218 break; \
20219 \
20220 case STRETCH_GLYPH: \
20221 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20222 HL, X, LAST_X); \
20223 break; \
20224 \
20225 case IMAGE_GLYPH: \
20226 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20227 HL, X, LAST_X); \
20228 break; \
20229 \
20230 default: \
20231 abort (); \
20232 } \
20233 \
20234 if (s) \
20235 { \
20236 set_glyph_string_background_width (s, START, LAST_X); \
20237 (X) += s->width; \
20238 } \
20239 } \
20240 } while (0)
20241
20242
20243 /* Draw glyphs between START and END in AREA of ROW on window W,
20244 starting at x-position X. X is relative to AREA in W. HL is a
20245 face-override with the following meaning:
20246
20247 DRAW_NORMAL_TEXT draw normally
20248 DRAW_CURSOR draw in cursor face
20249 DRAW_MOUSE_FACE draw in mouse face.
20250 DRAW_INVERSE_VIDEO draw in mode line face
20251 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20252 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20253
20254 If OVERLAPS is non-zero, draw only the foreground of characters and
20255 clip to the physical height of ROW. Non-zero value also defines
20256 the overlapping part to be drawn:
20257
20258 OVERLAPS_PRED overlap with preceding rows
20259 OVERLAPS_SUCC overlap with succeeding rows
20260 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20261 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20262
20263 Value is the x-position reached, relative to AREA of W. */
20264
20265 static int
20266 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20267 struct window *w;
20268 int x;
20269 struct glyph_row *row;
20270 enum glyph_row_area area;
20271 EMACS_INT start, end;
20272 enum draw_glyphs_face hl;
20273 int overlaps;
20274 {
20275 struct glyph_string *head, *tail;
20276 struct glyph_string *s;
20277 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20278 int i, j, x_reached, last_x, area_left = 0;
20279 struct frame *f = XFRAME (WINDOW_FRAME (w));
20280 DECLARE_HDC (hdc);
20281
20282 ALLOCATE_HDC (hdc, f);
20283
20284 /* Let's rather be paranoid than getting a SEGV. */
20285 end = min (end, row->used[area]);
20286 start = max (0, start);
20287 start = min (end, start);
20288
20289 /* Translate X to frame coordinates. Set last_x to the right
20290 end of the drawing area. */
20291 if (row->full_width_p)
20292 {
20293 /* X is relative to the left edge of W, without scroll bars
20294 or fringes. */
20295 area_left = WINDOW_LEFT_EDGE_X (w);
20296 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20297 }
20298 else
20299 {
20300 area_left = window_box_left (w, area);
20301 last_x = area_left + window_box_width (w, area);
20302 }
20303 x += area_left;
20304
20305 /* Build a doubly-linked list of glyph_string structures between
20306 head and tail from what we have to draw. Note that the macro
20307 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20308 the reason we use a separate variable `i'. */
20309 i = start;
20310 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20311 if (tail)
20312 x_reached = tail->x + tail->background_width;
20313 else
20314 x_reached = x;
20315
20316 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20317 the row, redraw some glyphs in front or following the glyph
20318 strings built above. */
20319 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20320 {
20321 struct glyph_string *h, *t;
20322 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20323 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20324 int dummy_x = 0;
20325
20326 /* If mouse highlighting is on, we may need to draw adjacent
20327 glyphs using mouse-face highlighting. */
20328 if (area == TEXT_AREA && row->mouse_face_p)
20329 {
20330 struct glyph_row *mouse_beg_row, *mouse_end_row;
20331
20332 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20333 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20334
20335 if (row >= mouse_beg_row && row <= mouse_end_row)
20336 {
20337 check_mouse_face = 1;
20338 mouse_beg_col = (row == mouse_beg_row)
20339 ? dpyinfo->mouse_face_beg_col : 0;
20340 mouse_end_col = (row == mouse_end_row)
20341 ? dpyinfo->mouse_face_end_col
20342 : row->used[TEXT_AREA];
20343 }
20344 }
20345
20346 /* Compute overhangs for all glyph strings. */
20347 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20348 for (s = head; s; s = s->next)
20349 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20350
20351 /* Prepend glyph strings for glyphs in front of the first glyph
20352 string that are overwritten because of the first glyph
20353 string's left overhang. The background of all strings
20354 prepended must be drawn because the first glyph string
20355 draws over it. */
20356 i = left_overwritten (head);
20357 if (i >= 0)
20358 {
20359 enum draw_glyphs_face overlap_hl;
20360
20361 /* If this row contains mouse highlighting, attempt to draw
20362 the overlapped glyphs with the correct highlight. This
20363 code fails if the overlap encompasses more than one glyph
20364 and mouse-highlight spans only some of these glyphs.
20365 However, making it work perfectly involves a lot more
20366 code, and I don't know if the pathological case occurs in
20367 practice, so we'll stick to this for now. --- cyd */
20368 if (check_mouse_face
20369 && mouse_beg_col < start && mouse_end_col > i)
20370 overlap_hl = DRAW_MOUSE_FACE;
20371 else
20372 overlap_hl = DRAW_NORMAL_TEXT;
20373
20374 j = i;
20375 BUILD_GLYPH_STRINGS (j, start, h, t,
20376 overlap_hl, dummy_x, last_x);
20377 start = i;
20378 compute_overhangs_and_x (t, head->x, 1);
20379 prepend_glyph_string_lists (&head, &tail, h, t);
20380 clip_head = head;
20381 }
20382
20383 /* Prepend glyph strings for glyphs in front of the first glyph
20384 string that overwrite that glyph string because of their
20385 right overhang. For these strings, only the foreground must
20386 be drawn, because it draws over the glyph string at `head'.
20387 The background must not be drawn because this would overwrite
20388 right overhangs of preceding glyphs for which no glyph
20389 strings exist. */
20390 i = left_overwriting (head);
20391 if (i >= 0)
20392 {
20393 enum draw_glyphs_face overlap_hl;
20394
20395 if (check_mouse_face
20396 && mouse_beg_col < start && mouse_end_col > i)
20397 overlap_hl = DRAW_MOUSE_FACE;
20398 else
20399 overlap_hl = DRAW_NORMAL_TEXT;
20400
20401 clip_head = head;
20402 BUILD_GLYPH_STRINGS (i, start, h, t,
20403 overlap_hl, dummy_x, last_x);
20404 for (s = h; s; s = s->next)
20405 s->background_filled_p = 1;
20406 compute_overhangs_and_x (t, head->x, 1);
20407 prepend_glyph_string_lists (&head, &tail, h, t);
20408 }
20409
20410 /* Append glyphs strings for glyphs following the last glyph
20411 string tail that are overwritten by tail. The background of
20412 these strings has to be drawn because tail's foreground draws
20413 over it. */
20414 i = right_overwritten (tail);
20415 if (i >= 0)
20416 {
20417 enum draw_glyphs_face overlap_hl;
20418
20419 if (check_mouse_face
20420 && mouse_beg_col < i && mouse_end_col > end)
20421 overlap_hl = DRAW_MOUSE_FACE;
20422 else
20423 overlap_hl = DRAW_NORMAL_TEXT;
20424
20425 BUILD_GLYPH_STRINGS (end, i, h, t,
20426 overlap_hl, x, last_x);
20427 /* Because BUILD_GLYPH_STRINGS updates the first argument,
20428 we don't have `end = i;' here. */
20429 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20430 append_glyph_string_lists (&head, &tail, h, t);
20431 clip_tail = tail;
20432 }
20433
20434 /* Append glyph strings for glyphs following the last glyph
20435 string tail that overwrite tail. The foreground of such
20436 glyphs has to be drawn because it writes into the background
20437 of tail. The background must not be drawn because it could
20438 paint over the foreground of following glyphs. */
20439 i = right_overwriting (tail);
20440 if (i >= 0)
20441 {
20442 enum draw_glyphs_face overlap_hl;
20443 if (check_mouse_face
20444 && mouse_beg_col < i && mouse_end_col > end)
20445 overlap_hl = DRAW_MOUSE_FACE;
20446 else
20447 overlap_hl = DRAW_NORMAL_TEXT;
20448
20449 clip_tail = tail;
20450 i++; /* We must include the Ith glyph. */
20451 BUILD_GLYPH_STRINGS (end, i, h, t,
20452 overlap_hl, x, last_x);
20453 for (s = h; s; s = s->next)
20454 s->background_filled_p = 1;
20455 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20456 append_glyph_string_lists (&head, &tail, h, t);
20457 }
20458 if (clip_head || clip_tail)
20459 for (s = head; s; s = s->next)
20460 {
20461 s->clip_head = clip_head;
20462 s->clip_tail = clip_tail;
20463 }
20464 }
20465
20466 /* Draw all strings. */
20467 for (s = head; s; s = s->next)
20468 FRAME_RIF (f)->draw_glyph_string (s);
20469
20470 #ifndef HAVE_NS
20471 /* When focus a sole frame and move horizontally, this sets on_p to 0
20472 causing a failure to erase prev cursor position. */
20473 if (area == TEXT_AREA
20474 && !row->full_width_p
20475 /* When drawing overlapping rows, only the glyph strings'
20476 foreground is drawn, which doesn't erase a cursor
20477 completely. */
20478 && !overlaps)
20479 {
20480 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20481 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20482 : (tail ? tail->x + tail->background_width : x));
20483 x0 -= area_left;
20484 x1 -= area_left;
20485
20486 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20487 row->y, MATRIX_ROW_BOTTOM_Y (row));
20488 }
20489 #endif
20490
20491 /* Value is the x-position up to which drawn, relative to AREA of W.
20492 This doesn't include parts drawn because of overhangs. */
20493 if (row->full_width_p)
20494 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20495 else
20496 x_reached -= area_left;
20497
20498 RELEASE_HDC (hdc, f);
20499
20500 return x_reached;
20501 }
20502
20503 /* Expand row matrix if too narrow. Don't expand if area
20504 is not present. */
20505
20506 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20507 { \
20508 if (!fonts_changed_p \
20509 && (it->glyph_row->glyphs[area] \
20510 < it->glyph_row->glyphs[area + 1])) \
20511 { \
20512 it->w->ncols_scale_factor++; \
20513 fonts_changed_p = 1; \
20514 } \
20515 }
20516
20517 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20518 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20519
20520 static INLINE void
20521 append_glyph (it)
20522 struct it *it;
20523 {
20524 struct glyph *glyph;
20525 enum glyph_row_area area = it->area;
20526
20527 xassert (it->glyph_row);
20528 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20529
20530 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20531 if (glyph < it->glyph_row->glyphs[area + 1])
20532 {
20533 glyph->charpos = CHARPOS (it->position);
20534 glyph->object = it->object;
20535 if (it->pixel_width > 0)
20536 {
20537 glyph->pixel_width = it->pixel_width;
20538 glyph->padding_p = 0;
20539 }
20540 else
20541 {
20542 /* Assure at least 1-pixel width. Otherwise, cursor can't
20543 be displayed correctly. */
20544 glyph->pixel_width = 1;
20545 glyph->padding_p = 1;
20546 }
20547 glyph->ascent = it->ascent;
20548 glyph->descent = it->descent;
20549 glyph->voffset = it->voffset;
20550 glyph->type = CHAR_GLYPH;
20551 glyph->avoid_cursor_p = it->avoid_cursor_p;
20552 glyph->multibyte_p = it->multibyte_p;
20553 glyph->left_box_line_p = it->start_of_box_run_p;
20554 glyph->right_box_line_p = it->end_of_box_run_p;
20555 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20556 || it->phys_descent > it->descent);
20557 glyph->glyph_not_available_p = it->glyph_not_available_p;
20558 glyph->face_id = it->face_id;
20559 glyph->u.ch = it->char_to_display;
20560 glyph->slice = null_glyph_slice;
20561 glyph->font_type = FONT_TYPE_UNKNOWN;
20562 ++it->glyph_row->used[area];
20563 }
20564 else
20565 IT_EXPAND_MATRIX_WIDTH (it, area);
20566 }
20567
20568 /* Store one glyph for the composition IT->cmp_it.id in
20569 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
20570 non-null. */
20571
20572 static INLINE void
20573 append_composite_glyph (it)
20574 struct it *it;
20575 {
20576 struct glyph *glyph;
20577 enum glyph_row_area area = it->area;
20578
20579 xassert (it->glyph_row);
20580
20581 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20582 if (glyph < it->glyph_row->glyphs[area + 1])
20583 {
20584 glyph->charpos = CHARPOS (it->position);
20585 glyph->object = it->object;
20586 glyph->pixel_width = it->pixel_width;
20587 glyph->ascent = it->ascent;
20588 glyph->descent = it->descent;
20589 glyph->voffset = it->voffset;
20590 glyph->type = COMPOSITE_GLYPH;
20591 if (it->cmp_it.ch < 0)
20592 {
20593 glyph->u.cmp.automatic = 0;
20594 glyph->u.cmp.id = it->cmp_it.id;
20595 }
20596 else
20597 {
20598 glyph->u.cmp.automatic = 1;
20599 glyph->u.cmp.id = it->cmp_it.id;
20600 glyph->u.cmp.from = it->cmp_it.from;
20601 glyph->u.cmp.to = it->cmp_it.to - 1;
20602 }
20603 glyph->avoid_cursor_p = it->avoid_cursor_p;
20604 glyph->multibyte_p = it->multibyte_p;
20605 glyph->left_box_line_p = it->start_of_box_run_p;
20606 glyph->right_box_line_p = it->end_of_box_run_p;
20607 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20608 || it->phys_descent > it->descent);
20609 glyph->padding_p = 0;
20610 glyph->glyph_not_available_p = 0;
20611 glyph->face_id = it->face_id;
20612 glyph->slice = null_glyph_slice;
20613 glyph->font_type = FONT_TYPE_UNKNOWN;
20614 ++it->glyph_row->used[area];
20615 }
20616 else
20617 IT_EXPAND_MATRIX_WIDTH (it, area);
20618 }
20619
20620
20621 /* Change IT->ascent and IT->height according to the setting of
20622 IT->voffset. */
20623
20624 static INLINE void
20625 take_vertical_position_into_account (it)
20626 struct it *it;
20627 {
20628 if (it->voffset)
20629 {
20630 if (it->voffset < 0)
20631 /* Increase the ascent so that we can display the text higher
20632 in the line. */
20633 it->ascent -= it->voffset;
20634 else
20635 /* Increase the descent so that we can display the text lower
20636 in the line. */
20637 it->descent += it->voffset;
20638 }
20639 }
20640
20641
20642 /* Produce glyphs/get display metrics for the image IT is loaded with.
20643 See the description of struct display_iterator in dispextern.h for
20644 an overview of struct display_iterator. */
20645
20646 static void
20647 produce_image_glyph (it)
20648 struct it *it;
20649 {
20650 struct image *img;
20651 struct face *face;
20652 int glyph_ascent, crop;
20653 struct glyph_slice slice;
20654
20655 xassert (it->what == IT_IMAGE);
20656
20657 face = FACE_FROM_ID (it->f, it->face_id);
20658 xassert (face);
20659 /* Make sure X resources of the face is loaded. */
20660 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20661
20662 if (it->image_id < 0)
20663 {
20664 /* Fringe bitmap. */
20665 it->ascent = it->phys_ascent = 0;
20666 it->descent = it->phys_descent = 0;
20667 it->pixel_width = 0;
20668 it->nglyphs = 0;
20669 return;
20670 }
20671
20672 img = IMAGE_FROM_ID (it->f, it->image_id);
20673 xassert (img);
20674 /* Make sure X resources of the image is loaded. */
20675 prepare_image_for_display (it->f, img);
20676
20677 slice.x = slice.y = 0;
20678 slice.width = img->width;
20679 slice.height = img->height;
20680
20681 if (INTEGERP (it->slice.x))
20682 slice.x = XINT (it->slice.x);
20683 else if (FLOATP (it->slice.x))
20684 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20685
20686 if (INTEGERP (it->slice.y))
20687 slice.y = XINT (it->slice.y);
20688 else if (FLOATP (it->slice.y))
20689 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20690
20691 if (INTEGERP (it->slice.width))
20692 slice.width = XINT (it->slice.width);
20693 else if (FLOATP (it->slice.width))
20694 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20695
20696 if (INTEGERP (it->slice.height))
20697 slice.height = XINT (it->slice.height);
20698 else if (FLOATP (it->slice.height))
20699 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20700
20701 if (slice.x >= img->width)
20702 slice.x = img->width;
20703 if (slice.y >= img->height)
20704 slice.y = img->height;
20705 if (slice.x + slice.width >= img->width)
20706 slice.width = img->width - slice.x;
20707 if (slice.y + slice.height > img->height)
20708 slice.height = img->height - slice.y;
20709
20710 if (slice.width == 0 || slice.height == 0)
20711 return;
20712
20713 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20714
20715 it->descent = slice.height - glyph_ascent;
20716 if (slice.y == 0)
20717 it->descent += img->vmargin;
20718 if (slice.y + slice.height == img->height)
20719 it->descent += img->vmargin;
20720 it->phys_descent = it->descent;
20721
20722 it->pixel_width = slice.width;
20723 if (slice.x == 0)
20724 it->pixel_width += img->hmargin;
20725 if (slice.x + slice.width == img->width)
20726 it->pixel_width += img->hmargin;
20727
20728 /* It's quite possible for images to have an ascent greater than
20729 their height, so don't get confused in that case. */
20730 if (it->descent < 0)
20731 it->descent = 0;
20732
20733 it->nglyphs = 1;
20734
20735 if (face->box != FACE_NO_BOX)
20736 {
20737 if (face->box_line_width > 0)
20738 {
20739 if (slice.y == 0)
20740 it->ascent += face->box_line_width;
20741 if (slice.y + slice.height == img->height)
20742 it->descent += face->box_line_width;
20743 }
20744
20745 if (it->start_of_box_run_p && slice.x == 0)
20746 it->pixel_width += eabs (face->box_line_width);
20747 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20748 it->pixel_width += eabs (face->box_line_width);
20749 }
20750
20751 take_vertical_position_into_account (it);
20752
20753 /* Automatically crop wide image glyphs at right edge so we can
20754 draw the cursor on same display row. */
20755 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20756 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20757 {
20758 it->pixel_width -= crop;
20759 slice.width -= crop;
20760 }
20761
20762 if (it->glyph_row)
20763 {
20764 struct glyph *glyph;
20765 enum glyph_row_area area = it->area;
20766
20767 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20768 if (glyph < it->glyph_row->glyphs[area + 1])
20769 {
20770 glyph->charpos = CHARPOS (it->position);
20771 glyph->object = it->object;
20772 glyph->pixel_width = it->pixel_width;
20773 glyph->ascent = glyph_ascent;
20774 glyph->descent = it->descent;
20775 glyph->voffset = it->voffset;
20776 glyph->type = IMAGE_GLYPH;
20777 glyph->avoid_cursor_p = it->avoid_cursor_p;
20778 glyph->multibyte_p = it->multibyte_p;
20779 glyph->left_box_line_p = it->start_of_box_run_p;
20780 glyph->right_box_line_p = it->end_of_box_run_p;
20781 glyph->overlaps_vertically_p = 0;
20782 glyph->padding_p = 0;
20783 glyph->glyph_not_available_p = 0;
20784 glyph->face_id = it->face_id;
20785 glyph->u.img_id = img->id;
20786 glyph->slice = slice;
20787 glyph->font_type = FONT_TYPE_UNKNOWN;
20788 ++it->glyph_row->used[area];
20789 }
20790 else
20791 IT_EXPAND_MATRIX_WIDTH (it, area);
20792 }
20793 }
20794
20795
20796 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20797 of the glyph, WIDTH and HEIGHT are the width and height of the
20798 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20799
20800 static void
20801 append_stretch_glyph (it, object, width, height, ascent)
20802 struct it *it;
20803 Lisp_Object object;
20804 int width, height;
20805 int ascent;
20806 {
20807 struct glyph *glyph;
20808 enum glyph_row_area area = it->area;
20809
20810 xassert (ascent >= 0 && ascent <= height);
20811
20812 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20813 if (glyph < it->glyph_row->glyphs[area + 1])
20814 {
20815 glyph->charpos = CHARPOS (it->position);
20816 glyph->object = object;
20817 glyph->pixel_width = width;
20818 glyph->ascent = ascent;
20819 glyph->descent = height - ascent;
20820 glyph->voffset = it->voffset;
20821 glyph->type = STRETCH_GLYPH;
20822 glyph->avoid_cursor_p = it->avoid_cursor_p;
20823 glyph->multibyte_p = it->multibyte_p;
20824 glyph->left_box_line_p = it->start_of_box_run_p;
20825 glyph->right_box_line_p = it->end_of_box_run_p;
20826 glyph->overlaps_vertically_p = 0;
20827 glyph->padding_p = 0;
20828 glyph->glyph_not_available_p = 0;
20829 glyph->face_id = it->face_id;
20830 glyph->u.stretch.ascent = ascent;
20831 glyph->u.stretch.height = height;
20832 glyph->slice = null_glyph_slice;
20833 glyph->font_type = FONT_TYPE_UNKNOWN;
20834 ++it->glyph_row->used[area];
20835 }
20836 else
20837 IT_EXPAND_MATRIX_WIDTH (it, area);
20838 }
20839
20840
20841 /* Produce a stretch glyph for iterator IT. IT->object is the value
20842 of the glyph property displayed. The value must be a list
20843 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20844 being recognized:
20845
20846 1. `:width WIDTH' specifies that the space should be WIDTH *
20847 canonical char width wide. WIDTH may be an integer or floating
20848 point number.
20849
20850 2. `:relative-width FACTOR' specifies that the width of the stretch
20851 should be computed from the width of the first character having the
20852 `glyph' property, and should be FACTOR times that width.
20853
20854 3. `:align-to HPOS' specifies that the space should be wide enough
20855 to reach HPOS, a value in canonical character units.
20856
20857 Exactly one of the above pairs must be present.
20858
20859 4. `:height HEIGHT' specifies that the height of the stretch produced
20860 should be HEIGHT, measured in canonical character units.
20861
20862 5. `:relative-height FACTOR' specifies that the height of the
20863 stretch should be FACTOR times the height of the characters having
20864 the glyph property.
20865
20866 Either none or exactly one of 4 or 5 must be present.
20867
20868 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20869 of the stretch should be used for the ascent of the stretch.
20870 ASCENT must be in the range 0 <= ASCENT <= 100. */
20871
20872 static void
20873 produce_stretch_glyph (it)
20874 struct it *it;
20875 {
20876 /* (space :width WIDTH :height HEIGHT ...) */
20877 Lisp_Object prop, plist;
20878 int width = 0, height = 0, align_to = -1;
20879 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20880 int ascent = 0;
20881 double tem;
20882 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20883 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20884
20885 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20886
20887 /* List should start with `space'. */
20888 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20889 plist = XCDR (it->object);
20890
20891 /* Compute the width of the stretch. */
20892 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20893 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20894 {
20895 /* Absolute width `:width WIDTH' specified and valid. */
20896 zero_width_ok_p = 1;
20897 width = (int)tem;
20898 }
20899 else if (prop = Fplist_get (plist, QCrelative_width),
20900 NUMVAL (prop) > 0)
20901 {
20902 /* Relative width `:relative-width FACTOR' specified and valid.
20903 Compute the width of the characters having the `glyph'
20904 property. */
20905 struct it it2;
20906 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20907
20908 it2 = *it;
20909 if (it->multibyte_p)
20910 {
20911 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20912 - IT_BYTEPOS (*it));
20913 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
20914 }
20915 else
20916 {
20917 it2.c = it2.char_to_display = *p, it2.len = 1;
20918 if (! ASCII_CHAR_P (it2.c))
20919 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
20920 }
20921
20922 it2.glyph_row = NULL;
20923 it2.what = IT_CHARACTER;
20924 x_produce_glyphs (&it2);
20925 width = NUMVAL (prop) * it2.pixel_width;
20926 }
20927 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20928 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20929 {
20930 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20931 align_to = (align_to < 0
20932 ? 0
20933 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20934 else if (align_to < 0)
20935 align_to = window_box_left_offset (it->w, TEXT_AREA);
20936 width = max (0, (int)tem + align_to - it->current_x);
20937 zero_width_ok_p = 1;
20938 }
20939 else
20940 /* Nothing specified -> width defaults to canonical char width. */
20941 width = FRAME_COLUMN_WIDTH (it->f);
20942
20943 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20944 width = 1;
20945
20946 /* Compute height. */
20947 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20948 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20949 {
20950 height = (int)tem;
20951 zero_height_ok_p = 1;
20952 }
20953 else if (prop = Fplist_get (plist, QCrelative_height),
20954 NUMVAL (prop) > 0)
20955 height = FONT_HEIGHT (font) * NUMVAL (prop);
20956 else
20957 height = FONT_HEIGHT (font);
20958
20959 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20960 height = 1;
20961
20962 /* Compute percentage of height used for ascent. If
20963 `:ascent ASCENT' is present and valid, use that. Otherwise,
20964 derive the ascent from the font in use. */
20965 if (prop = Fplist_get (plist, QCascent),
20966 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20967 ascent = height * NUMVAL (prop) / 100.0;
20968 else if (!NILP (prop)
20969 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20970 ascent = min (max (0, (int)tem), height);
20971 else
20972 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20973
20974 if (width > 0 && it->line_wrap != TRUNCATE
20975 && it->current_x + width > it->last_visible_x)
20976 width = it->last_visible_x - it->current_x - 1;
20977
20978 if (width > 0 && height > 0 && it->glyph_row)
20979 {
20980 Lisp_Object object = it->stack[it->sp - 1].string;
20981 if (!STRINGP (object))
20982 object = it->w->buffer;
20983 append_stretch_glyph (it, object, width, height, ascent);
20984 }
20985
20986 it->pixel_width = width;
20987 it->ascent = it->phys_ascent = ascent;
20988 it->descent = it->phys_descent = height - it->ascent;
20989 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20990
20991 take_vertical_position_into_account (it);
20992 }
20993
20994 /* Calculate line-height and line-spacing properties.
20995 An integer value specifies explicit pixel value.
20996 A float value specifies relative value to current face height.
20997 A cons (float . face-name) specifies relative value to
20998 height of specified face font.
20999
21000 Returns height in pixels, or nil. */
21001
21002
21003 static Lisp_Object
21004 calc_line_height_property (it, val, font, boff, override)
21005 struct it *it;
21006 Lisp_Object val;
21007 struct font *font;
21008 int boff, override;
21009 {
21010 Lisp_Object face_name = Qnil;
21011 int ascent, descent, height;
21012
21013 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
21014 return val;
21015
21016 if (CONSP (val))
21017 {
21018 face_name = XCAR (val);
21019 val = XCDR (val);
21020 if (!NUMBERP (val))
21021 val = make_number (1);
21022 if (NILP (face_name))
21023 {
21024 height = it->ascent + it->descent;
21025 goto scale;
21026 }
21027 }
21028
21029 if (NILP (face_name))
21030 {
21031 font = FRAME_FONT (it->f);
21032 boff = FRAME_BASELINE_OFFSET (it->f);
21033 }
21034 else if (EQ (face_name, Qt))
21035 {
21036 override = 0;
21037 }
21038 else
21039 {
21040 int face_id;
21041 struct face *face;
21042
21043 face_id = lookup_named_face (it->f, face_name, 0);
21044 if (face_id < 0)
21045 return make_number (-1);
21046
21047 face = FACE_FROM_ID (it->f, face_id);
21048 font = face->font;
21049 if (font == NULL)
21050 return make_number (-1);
21051 boff = font->baseline_offset;
21052 if (font->vertical_centering)
21053 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21054 }
21055
21056 ascent = FONT_BASE (font) + boff;
21057 descent = FONT_DESCENT (font) - boff;
21058
21059 if (override)
21060 {
21061 it->override_ascent = ascent;
21062 it->override_descent = descent;
21063 it->override_boff = boff;
21064 }
21065
21066 height = ascent + descent;
21067
21068 scale:
21069 if (FLOATP (val))
21070 height = (int)(XFLOAT_DATA (val) * height);
21071 else if (INTEGERP (val))
21072 height *= XINT (val);
21073
21074 return make_number (height);
21075 }
21076
21077
21078 /* RIF:
21079 Produce glyphs/get display metrics for the display element IT is
21080 loaded with. See the description of struct it in dispextern.h
21081 for an overview of struct it. */
21082
21083 void
21084 x_produce_glyphs (it)
21085 struct it *it;
21086 {
21087 int extra_line_spacing = it->extra_line_spacing;
21088
21089 it->glyph_not_available_p = 0;
21090
21091 if (it->what == IT_CHARACTER)
21092 {
21093 XChar2b char2b;
21094 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21095 struct font *font = face->font;
21096 int font_not_found_p = font == NULL;
21097 struct font_metrics *pcm = NULL;
21098 int boff; /* baseline offset */
21099
21100 if (font_not_found_p)
21101 {
21102 /* When no suitable font found, display an empty box based
21103 on the metrics of the font of the default face (or what
21104 remapped). */
21105 struct face *no_font_face
21106 = FACE_FROM_ID (it->f,
21107 NILP (Vface_remapping_alist) ? DEFAULT_FACE_ID
21108 : lookup_basic_face (it->f, DEFAULT_FACE_ID));
21109 font = no_font_face->font;
21110 boff = font->baseline_offset;
21111 }
21112 else
21113 {
21114 boff = font->baseline_offset;
21115 if (font->vertical_centering)
21116 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21117 }
21118
21119 if (it->char_to_display != '\n' && it->char_to_display != '\t')
21120 {
21121 int stretched_p;
21122
21123 it->nglyphs = 1;
21124
21125 if (it->override_ascent >= 0)
21126 {
21127 it->ascent = it->override_ascent;
21128 it->descent = it->override_descent;
21129 boff = it->override_boff;
21130 }
21131 else
21132 {
21133 it->ascent = FONT_BASE (font) + boff;
21134 it->descent = FONT_DESCENT (font) - boff;
21135 }
21136
21137 if (! font_not_found_p
21138 && get_char_glyph_code (it->char_to_display, font, &char2b))
21139 {
21140 pcm = get_per_char_metric (it->f, font, &char2b);
21141 if (pcm->width == 0
21142 && pcm->rbearing == 0 && pcm->lbearing == 0)
21143 pcm = NULL;
21144 }
21145
21146 if (pcm)
21147 {
21148 it->phys_ascent = pcm->ascent + boff;
21149 it->phys_descent = pcm->descent - boff;
21150 it->pixel_width = pcm->width;
21151 }
21152 else
21153 {
21154 it->glyph_not_available_p = 1;
21155 it->phys_ascent = it->ascent;
21156 it->phys_descent = it->descent;
21157 it->pixel_width = font->space_width;
21158 }
21159
21160 if (it->constrain_row_ascent_descent_p)
21161 {
21162 if (it->descent > it->max_descent)
21163 {
21164 it->ascent += it->descent - it->max_descent;
21165 it->descent = it->max_descent;
21166 }
21167 if (it->ascent > it->max_ascent)
21168 {
21169 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21170 it->ascent = it->max_ascent;
21171 }
21172 it->phys_ascent = min (it->phys_ascent, it->ascent);
21173 it->phys_descent = min (it->phys_descent, it->descent);
21174 extra_line_spacing = 0;
21175 }
21176
21177 /* If this is a space inside a region of text with
21178 `space-width' property, change its width. */
21179 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21180 if (stretched_p)
21181 it->pixel_width *= XFLOATINT (it->space_width);
21182
21183 /* If face has a box, add the box thickness to the character
21184 height. If character has a box line to the left and/or
21185 right, add the box line width to the character's width. */
21186 if (face->box != FACE_NO_BOX)
21187 {
21188 int thick = face->box_line_width;
21189
21190 if (thick > 0)
21191 {
21192 it->ascent += thick;
21193 it->descent += thick;
21194 }
21195 else
21196 thick = -thick;
21197
21198 if (it->start_of_box_run_p)
21199 it->pixel_width += thick;
21200 if (it->end_of_box_run_p)
21201 it->pixel_width += thick;
21202 }
21203
21204 /* If face has an overline, add the height of the overline
21205 (1 pixel) and a 1 pixel margin to the character height. */
21206 if (face->overline_p)
21207 it->ascent += overline_margin;
21208
21209 if (it->constrain_row_ascent_descent_p)
21210 {
21211 if (it->ascent > it->max_ascent)
21212 it->ascent = it->max_ascent;
21213 if (it->descent > it->max_descent)
21214 it->descent = it->max_descent;
21215 }
21216
21217 take_vertical_position_into_account (it);
21218
21219 /* If we have to actually produce glyphs, do it. */
21220 if (it->glyph_row)
21221 {
21222 if (stretched_p)
21223 {
21224 /* Translate a space with a `space-width' property
21225 into a stretch glyph. */
21226 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21227 / FONT_HEIGHT (font));
21228 append_stretch_glyph (it, it->object, it->pixel_width,
21229 it->ascent + it->descent, ascent);
21230 }
21231 else
21232 append_glyph (it);
21233
21234 /* If characters with lbearing or rbearing are displayed
21235 in this line, record that fact in a flag of the
21236 glyph row. This is used to optimize X output code. */
21237 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21238 it->glyph_row->contains_overlapping_glyphs_p = 1;
21239 }
21240 if (! stretched_p && it->pixel_width == 0)
21241 /* We assure that all visible glyphs have at least 1-pixel
21242 width. */
21243 it->pixel_width = 1;
21244 }
21245 else if (it->char_to_display == '\n')
21246 {
21247 /* A newline has no width, but we need the height of the
21248 line. But if previous part of the line sets a height,
21249 don't increase that height */
21250
21251 Lisp_Object height;
21252 Lisp_Object total_height = Qnil;
21253
21254 it->override_ascent = -1;
21255 it->pixel_width = 0;
21256 it->nglyphs = 0;
21257
21258 height = get_it_property(it, Qline_height);
21259 /* Split (line-height total-height) list */
21260 if (CONSP (height)
21261 && CONSP (XCDR (height))
21262 && NILP (XCDR (XCDR (height))))
21263 {
21264 total_height = XCAR (XCDR (height));
21265 height = XCAR (height);
21266 }
21267 height = calc_line_height_property(it, height, font, boff, 1);
21268
21269 if (it->override_ascent >= 0)
21270 {
21271 it->ascent = it->override_ascent;
21272 it->descent = it->override_descent;
21273 boff = it->override_boff;
21274 }
21275 else
21276 {
21277 it->ascent = FONT_BASE (font) + boff;
21278 it->descent = FONT_DESCENT (font) - boff;
21279 }
21280
21281 if (EQ (height, Qt))
21282 {
21283 if (it->descent > it->max_descent)
21284 {
21285 it->ascent += it->descent - it->max_descent;
21286 it->descent = it->max_descent;
21287 }
21288 if (it->ascent > it->max_ascent)
21289 {
21290 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21291 it->ascent = it->max_ascent;
21292 }
21293 it->phys_ascent = min (it->phys_ascent, it->ascent);
21294 it->phys_descent = min (it->phys_descent, it->descent);
21295 it->constrain_row_ascent_descent_p = 1;
21296 extra_line_spacing = 0;
21297 }
21298 else
21299 {
21300 Lisp_Object spacing;
21301
21302 it->phys_ascent = it->ascent;
21303 it->phys_descent = it->descent;
21304
21305 if ((it->max_ascent > 0 || it->max_descent > 0)
21306 && face->box != FACE_NO_BOX
21307 && face->box_line_width > 0)
21308 {
21309 it->ascent += face->box_line_width;
21310 it->descent += face->box_line_width;
21311 }
21312 if (!NILP (height)
21313 && XINT (height) > it->ascent + it->descent)
21314 it->ascent = XINT (height) - it->descent;
21315
21316 if (!NILP (total_height))
21317 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21318 else
21319 {
21320 spacing = get_it_property(it, Qline_spacing);
21321 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21322 }
21323 if (INTEGERP (spacing))
21324 {
21325 extra_line_spacing = XINT (spacing);
21326 if (!NILP (total_height))
21327 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21328 }
21329 }
21330 }
21331 else /* i.e. (it->char_to_display == '\t') */
21332 {
21333 if (font->space_width > 0)
21334 {
21335 int tab_width = it->tab_width * font->space_width;
21336 int x = it->current_x + it->continuation_lines_width;
21337 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21338
21339 /* If the distance from the current position to the next tab
21340 stop is less than a space character width, use the
21341 tab stop after that. */
21342 if (next_tab_x - x < font->space_width)
21343 next_tab_x += tab_width;
21344
21345 it->pixel_width = next_tab_x - x;
21346 it->nglyphs = 1;
21347 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21348 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21349
21350 if (it->glyph_row)
21351 {
21352 append_stretch_glyph (it, it->object, it->pixel_width,
21353 it->ascent + it->descent, it->ascent);
21354 }
21355 }
21356 else
21357 {
21358 it->pixel_width = 0;
21359 it->nglyphs = 1;
21360 }
21361 }
21362 }
21363 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
21364 {
21365 /* A static composition.
21366
21367 Note: A composition is represented as one glyph in the
21368 glyph matrix. There are no padding glyphs.
21369
21370 Important note: pixel_width, ascent, and descent are the
21371 values of what is drawn by draw_glyphs (i.e. the values of
21372 the overall glyphs composed). */
21373 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21374 int boff; /* baseline offset */
21375 struct composition *cmp = composition_table[it->cmp_it.id];
21376 int glyph_len = cmp->glyph_len;
21377 struct font *font = face->font;
21378
21379 it->nglyphs = 1;
21380
21381 /* If we have not yet calculated pixel size data of glyphs of
21382 the composition for the current face font, calculate them
21383 now. Theoretically, we have to check all fonts for the
21384 glyphs, but that requires much time and memory space. So,
21385 here we check only the font of the first glyph. This may
21386 lead to incorrect display, but it's very rare, and C-l
21387 (recenter-top-bottom) can correct the display anyway. */
21388 if (! cmp->font || cmp->font != font)
21389 {
21390 /* Ascent and descent of the font of the first character
21391 of this composition (adjusted by baseline offset).
21392 Ascent and descent of overall glyphs should not be less
21393 than these, respectively. */
21394 int font_ascent, font_descent, font_height;
21395 /* Bounding box of the overall glyphs. */
21396 int leftmost, rightmost, lowest, highest;
21397 int lbearing, rbearing;
21398 int i, width, ascent, descent;
21399 int left_padded = 0, right_padded = 0;
21400 int c;
21401 XChar2b char2b;
21402 struct font_metrics *pcm;
21403 int font_not_found_p;
21404 int pos;
21405
21406 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21407 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21408 break;
21409 if (glyph_len < cmp->glyph_len)
21410 right_padded = 1;
21411 for (i = 0; i < glyph_len; i++)
21412 {
21413 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21414 break;
21415 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21416 }
21417 if (i > 0)
21418 left_padded = 1;
21419
21420 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21421 : IT_CHARPOS (*it));
21422 /* If no suitable font is found, use the default font. */
21423 font_not_found_p = font == NULL;
21424 if (font_not_found_p)
21425 {
21426 face = face->ascii_face;
21427 font = face->font;
21428 }
21429 boff = font->baseline_offset;
21430 if (font->vertical_centering)
21431 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21432 font_ascent = FONT_BASE (font) + boff;
21433 font_descent = FONT_DESCENT (font) - boff;
21434 font_height = FONT_HEIGHT (font);
21435
21436 cmp->font = (void *) font;
21437
21438 pcm = NULL;
21439 if (! font_not_found_p)
21440 {
21441 get_char_face_and_encoding (it->f, c, it->face_id,
21442 &char2b, it->multibyte_p, 0);
21443 pcm = get_per_char_metric (it->f, font, &char2b);
21444 }
21445
21446 /* Initialize the bounding box. */
21447 if (pcm)
21448 {
21449 width = pcm->width;
21450 ascent = pcm->ascent;
21451 descent = pcm->descent;
21452 lbearing = pcm->lbearing;
21453 rbearing = pcm->rbearing;
21454 }
21455 else
21456 {
21457 width = font->space_width;
21458 ascent = FONT_BASE (font);
21459 descent = FONT_DESCENT (font);
21460 lbearing = 0;
21461 rbearing = width;
21462 }
21463
21464 rightmost = width;
21465 leftmost = 0;
21466 lowest = - descent + boff;
21467 highest = ascent + boff;
21468
21469 if (! font_not_found_p
21470 && font->default_ascent
21471 && CHAR_TABLE_P (Vuse_default_ascent)
21472 && !NILP (Faref (Vuse_default_ascent,
21473 make_number (it->char_to_display))))
21474 highest = font->default_ascent + boff;
21475
21476 /* Draw the first glyph at the normal position. It may be
21477 shifted to right later if some other glyphs are drawn
21478 at the left. */
21479 cmp->offsets[i * 2] = 0;
21480 cmp->offsets[i * 2 + 1] = boff;
21481 cmp->lbearing = lbearing;
21482 cmp->rbearing = rbearing;
21483
21484 /* Set cmp->offsets for the remaining glyphs. */
21485 for (i++; i < glyph_len; i++)
21486 {
21487 int left, right, btm, top;
21488 int ch = COMPOSITION_GLYPH (cmp, i);
21489 int face_id;
21490 struct face *this_face;
21491 int this_boff;
21492
21493 if (ch == '\t')
21494 ch = ' ';
21495 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21496 this_face = FACE_FROM_ID (it->f, face_id);
21497 font = this_face->font;
21498
21499 if (font == NULL)
21500 pcm = NULL;
21501 else
21502 {
21503 this_boff = font->baseline_offset;
21504 if (font->vertical_centering)
21505 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21506 get_char_face_and_encoding (it->f, ch, face_id,
21507 &char2b, it->multibyte_p, 0);
21508 pcm = get_per_char_metric (it->f, font, &char2b);
21509 }
21510 if (! pcm)
21511 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21512 else
21513 {
21514 width = pcm->width;
21515 ascent = pcm->ascent;
21516 descent = pcm->descent;
21517 lbearing = pcm->lbearing;
21518 rbearing = pcm->rbearing;
21519 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21520 {
21521 /* Relative composition with or without
21522 alternate chars. */
21523 left = (leftmost + rightmost - width) / 2;
21524 btm = - descent + boff;
21525 if (font->relative_compose
21526 && (! CHAR_TABLE_P (Vignore_relative_composition)
21527 || NILP (Faref (Vignore_relative_composition,
21528 make_number (ch)))))
21529 {
21530
21531 if (- descent >= font->relative_compose)
21532 /* One extra pixel between two glyphs. */
21533 btm = highest + 1;
21534 else if (ascent <= 0)
21535 /* One extra pixel between two glyphs. */
21536 btm = lowest - 1 - ascent - descent;
21537 }
21538 }
21539 else
21540 {
21541 /* A composition rule is specified by an integer
21542 value that encodes global and new reference
21543 points (GREF and NREF). GREF and NREF are
21544 specified by numbers as below:
21545
21546 0---1---2 -- ascent
21547 | |
21548 | |
21549 | |
21550 9--10--11 -- center
21551 | |
21552 ---3---4---5--- baseline
21553 | |
21554 6---7---8 -- descent
21555 */
21556 int rule = COMPOSITION_RULE (cmp, i);
21557 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21558
21559 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21560 grefx = gref % 3, nrefx = nref % 3;
21561 grefy = gref / 3, nrefy = nref / 3;
21562 if (xoff)
21563 xoff = font_height * (xoff - 128) / 256;
21564 if (yoff)
21565 yoff = font_height * (yoff - 128) / 256;
21566
21567 left = (leftmost
21568 + grefx * (rightmost - leftmost) / 2
21569 - nrefx * width / 2
21570 + xoff);
21571
21572 btm = ((grefy == 0 ? highest
21573 : grefy == 1 ? 0
21574 : grefy == 2 ? lowest
21575 : (highest + lowest) / 2)
21576 - (nrefy == 0 ? ascent + descent
21577 : nrefy == 1 ? descent - boff
21578 : nrefy == 2 ? 0
21579 : (ascent + descent) / 2)
21580 + yoff);
21581 }
21582
21583 cmp->offsets[i * 2] = left;
21584 cmp->offsets[i * 2 + 1] = btm + descent;
21585
21586 /* Update the bounding box of the overall glyphs. */
21587 if (width > 0)
21588 {
21589 right = left + width;
21590 if (left < leftmost)
21591 leftmost = left;
21592 if (right > rightmost)
21593 rightmost = right;
21594 }
21595 top = btm + descent + ascent;
21596 if (top > highest)
21597 highest = top;
21598 if (btm < lowest)
21599 lowest = btm;
21600
21601 if (cmp->lbearing > left + lbearing)
21602 cmp->lbearing = left + lbearing;
21603 if (cmp->rbearing < left + rbearing)
21604 cmp->rbearing = left + rbearing;
21605 }
21606 }
21607
21608 /* If there are glyphs whose x-offsets are negative,
21609 shift all glyphs to the right and make all x-offsets
21610 non-negative. */
21611 if (leftmost < 0)
21612 {
21613 for (i = 0; i < cmp->glyph_len; i++)
21614 cmp->offsets[i * 2] -= leftmost;
21615 rightmost -= leftmost;
21616 cmp->lbearing -= leftmost;
21617 cmp->rbearing -= leftmost;
21618 }
21619
21620 if (left_padded && cmp->lbearing < 0)
21621 {
21622 for (i = 0; i < cmp->glyph_len; i++)
21623 cmp->offsets[i * 2] -= cmp->lbearing;
21624 rightmost -= cmp->lbearing;
21625 cmp->rbearing -= cmp->lbearing;
21626 cmp->lbearing = 0;
21627 }
21628 if (right_padded && rightmost < cmp->rbearing)
21629 {
21630 rightmost = cmp->rbearing;
21631 }
21632
21633 cmp->pixel_width = rightmost;
21634 cmp->ascent = highest;
21635 cmp->descent = - lowest;
21636 if (cmp->ascent < font_ascent)
21637 cmp->ascent = font_ascent;
21638 if (cmp->descent < font_descent)
21639 cmp->descent = font_descent;
21640 }
21641
21642 if (it->glyph_row
21643 && (cmp->lbearing < 0
21644 || cmp->rbearing > cmp->pixel_width))
21645 it->glyph_row->contains_overlapping_glyphs_p = 1;
21646
21647 it->pixel_width = cmp->pixel_width;
21648 it->ascent = it->phys_ascent = cmp->ascent;
21649 it->descent = it->phys_descent = cmp->descent;
21650 if (face->box != FACE_NO_BOX)
21651 {
21652 int thick = face->box_line_width;
21653
21654 if (thick > 0)
21655 {
21656 it->ascent += thick;
21657 it->descent += thick;
21658 }
21659 else
21660 thick = - thick;
21661
21662 if (it->start_of_box_run_p)
21663 it->pixel_width += thick;
21664 if (it->end_of_box_run_p)
21665 it->pixel_width += thick;
21666 }
21667
21668 /* If face has an overline, add the height of the overline
21669 (1 pixel) and a 1 pixel margin to the character height. */
21670 if (face->overline_p)
21671 it->ascent += overline_margin;
21672
21673 take_vertical_position_into_account (it);
21674 if (it->ascent < 0)
21675 it->ascent = 0;
21676 if (it->descent < 0)
21677 it->descent = 0;
21678
21679 if (it->glyph_row)
21680 append_composite_glyph (it);
21681 }
21682 else if (it->what == IT_COMPOSITION)
21683 {
21684 /* A dynamic (automatic) composition. */
21685 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21686 Lisp_Object gstring;
21687 struct font_metrics metrics;
21688
21689 gstring = composition_gstring_from_id (it->cmp_it.id);
21690 it->pixel_width
21691 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
21692 &metrics);
21693 if (it->glyph_row
21694 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
21695 it->glyph_row->contains_overlapping_glyphs_p = 1;
21696 it->ascent = it->phys_ascent = metrics.ascent;
21697 it->descent = it->phys_descent = metrics.descent;
21698 if (face->box != FACE_NO_BOX)
21699 {
21700 int thick = face->box_line_width;
21701
21702 if (thick > 0)
21703 {
21704 it->ascent += thick;
21705 it->descent += thick;
21706 }
21707 else
21708 thick = - thick;
21709
21710 if (it->start_of_box_run_p)
21711 it->pixel_width += thick;
21712 if (it->end_of_box_run_p)
21713 it->pixel_width += thick;
21714 }
21715 /* If face has an overline, add the height of the overline
21716 (1 pixel) and a 1 pixel margin to the character height. */
21717 if (face->overline_p)
21718 it->ascent += overline_margin;
21719 take_vertical_position_into_account (it);
21720 if (it->ascent < 0)
21721 it->ascent = 0;
21722 if (it->descent < 0)
21723 it->descent = 0;
21724
21725 if (it->glyph_row)
21726 append_composite_glyph (it);
21727 }
21728 else if (it->what == IT_IMAGE)
21729 produce_image_glyph (it);
21730 else if (it->what == IT_STRETCH)
21731 produce_stretch_glyph (it);
21732
21733 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21734 because this isn't true for images with `:ascent 100'. */
21735 xassert (it->ascent >= 0 && it->descent >= 0);
21736 if (it->area == TEXT_AREA)
21737 it->current_x += it->pixel_width;
21738
21739 if (extra_line_spacing > 0)
21740 {
21741 it->descent += extra_line_spacing;
21742 if (extra_line_spacing > it->max_extra_line_spacing)
21743 it->max_extra_line_spacing = extra_line_spacing;
21744 }
21745
21746 it->max_ascent = max (it->max_ascent, it->ascent);
21747 it->max_descent = max (it->max_descent, it->descent);
21748 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21749 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21750 }
21751
21752 /* EXPORT for RIF:
21753 Output LEN glyphs starting at START at the nominal cursor position.
21754 Advance the nominal cursor over the text. The global variable
21755 updated_window contains the window being updated, updated_row is
21756 the glyph row being updated, and updated_area is the area of that
21757 row being updated. */
21758
21759 void
21760 x_write_glyphs (start, len)
21761 struct glyph *start;
21762 int len;
21763 {
21764 int x, hpos;
21765
21766 xassert (updated_window && updated_row);
21767 BLOCK_INPUT;
21768
21769 /* Write glyphs. */
21770
21771 hpos = start - updated_row->glyphs[updated_area];
21772 x = draw_glyphs (updated_window, output_cursor.x,
21773 updated_row, updated_area,
21774 hpos, hpos + len,
21775 DRAW_NORMAL_TEXT, 0);
21776
21777 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21778 if (updated_area == TEXT_AREA
21779 && updated_window->phys_cursor_on_p
21780 && updated_window->phys_cursor.vpos == output_cursor.vpos
21781 && updated_window->phys_cursor.hpos >= hpos
21782 && updated_window->phys_cursor.hpos < hpos + len)
21783 updated_window->phys_cursor_on_p = 0;
21784
21785 UNBLOCK_INPUT;
21786
21787 /* Advance the output cursor. */
21788 output_cursor.hpos += len;
21789 output_cursor.x = x;
21790 }
21791
21792
21793 /* EXPORT for RIF:
21794 Insert LEN glyphs from START at the nominal cursor position. */
21795
21796 void
21797 x_insert_glyphs (start, len)
21798 struct glyph *start;
21799 int len;
21800 {
21801 struct frame *f;
21802 struct window *w;
21803 int line_height, shift_by_width, shifted_region_width;
21804 struct glyph_row *row;
21805 struct glyph *glyph;
21806 int frame_x, frame_y;
21807 EMACS_INT hpos;
21808
21809 xassert (updated_window && updated_row);
21810 BLOCK_INPUT;
21811 w = updated_window;
21812 f = XFRAME (WINDOW_FRAME (w));
21813
21814 /* Get the height of the line we are in. */
21815 row = updated_row;
21816 line_height = row->height;
21817
21818 /* Get the width of the glyphs to insert. */
21819 shift_by_width = 0;
21820 for (glyph = start; glyph < start + len; ++glyph)
21821 shift_by_width += glyph->pixel_width;
21822
21823 /* Get the width of the region to shift right. */
21824 shifted_region_width = (window_box_width (w, updated_area)
21825 - output_cursor.x
21826 - shift_by_width);
21827
21828 /* Shift right. */
21829 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21830 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21831
21832 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21833 line_height, shift_by_width);
21834
21835 /* Write the glyphs. */
21836 hpos = start - row->glyphs[updated_area];
21837 draw_glyphs (w, output_cursor.x, row, updated_area,
21838 hpos, hpos + len,
21839 DRAW_NORMAL_TEXT, 0);
21840
21841 /* Advance the output cursor. */
21842 output_cursor.hpos += len;
21843 output_cursor.x += shift_by_width;
21844 UNBLOCK_INPUT;
21845 }
21846
21847
21848 /* EXPORT for RIF:
21849 Erase the current text line from the nominal cursor position
21850 (inclusive) to pixel column TO_X (exclusive). The idea is that
21851 everything from TO_X onward is already erased.
21852
21853 TO_X is a pixel position relative to updated_area of
21854 updated_window. TO_X == -1 means clear to the end of this area. */
21855
21856 void
21857 x_clear_end_of_line (to_x)
21858 int to_x;
21859 {
21860 struct frame *f;
21861 struct window *w = updated_window;
21862 int max_x, min_y, max_y;
21863 int from_x, from_y, to_y;
21864
21865 xassert (updated_window && updated_row);
21866 f = XFRAME (w->frame);
21867
21868 if (updated_row->full_width_p)
21869 max_x = WINDOW_TOTAL_WIDTH (w);
21870 else
21871 max_x = window_box_width (w, updated_area);
21872 max_y = window_text_bottom_y (w);
21873
21874 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21875 of window. For TO_X > 0, truncate to end of drawing area. */
21876 if (to_x == 0)
21877 return;
21878 else if (to_x < 0)
21879 to_x = max_x;
21880 else
21881 to_x = min (to_x, max_x);
21882
21883 to_y = min (max_y, output_cursor.y + updated_row->height);
21884
21885 /* Notice if the cursor will be cleared by this operation. */
21886 if (!updated_row->full_width_p)
21887 notice_overwritten_cursor (w, updated_area,
21888 output_cursor.x, -1,
21889 updated_row->y,
21890 MATRIX_ROW_BOTTOM_Y (updated_row));
21891
21892 from_x = output_cursor.x;
21893
21894 /* Translate to frame coordinates. */
21895 if (updated_row->full_width_p)
21896 {
21897 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21898 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21899 }
21900 else
21901 {
21902 int area_left = window_box_left (w, updated_area);
21903 from_x += area_left;
21904 to_x += area_left;
21905 }
21906
21907 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21908 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21909 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21910
21911 /* Prevent inadvertently clearing to end of the X window. */
21912 if (to_x > from_x && to_y > from_y)
21913 {
21914 BLOCK_INPUT;
21915 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21916 to_x - from_x, to_y - from_y);
21917 UNBLOCK_INPUT;
21918 }
21919 }
21920
21921 #endif /* HAVE_WINDOW_SYSTEM */
21922
21923
21924 \f
21925 /***********************************************************************
21926 Cursor types
21927 ***********************************************************************/
21928
21929 /* Value is the internal representation of the specified cursor type
21930 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21931 of the bar cursor. */
21932
21933 static enum text_cursor_kinds
21934 get_specified_cursor_type (arg, width)
21935 Lisp_Object arg;
21936 int *width;
21937 {
21938 enum text_cursor_kinds type;
21939
21940 if (NILP (arg))
21941 return NO_CURSOR;
21942
21943 if (EQ (arg, Qbox))
21944 return FILLED_BOX_CURSOR;
21945
21946 if (EQ (arg, Qhollow))
21947 return HOLLOW_BOX_CURSOR;
21948
21949 if (EQ (arg, Qbar))
21950 {
21951 *width = 2;
21952 return BAR_CURSOR;
21953 }
21954
21955 if (CONSP (arg)
21956 && EQ (XCAR (arg), Qbar)
21957 && INTEGERP (XCDR (arg))
21958 && XINT (XCDR (arg)) >= 0)
21959 {
21960 *width = XINT (XCDR (arg));
21961 return BAR_CURSOR;
21962 }
21963
21964 if (EQ (arg, Qhbar))
21965 {
21966 *width = 2;
21967 return HBAR_CURSOR;
21968 }
21969
21970 if (CONSP (arg)
21971 && EQ (XCAR (arg), Qhbar)
21972 && INTEGERP (XCDR (arg))
21973 && XINT (XCDR (arg)) >= 0)
21974 {
21975 *width = XINT (XCDR (arg));
21976 return HBAR_CURSOR;
21977 }
21978
21979 /* Treat anything unknown as "hollow box cursor".
21980 It was bad to signal an error; people have trouble fixing
21981 .Xdefaults with Emacs, when it has something bad in it. */
21982 type = HOLLOW_BOX_CURSOR;
21983
21984 return type;
21985 }
21986
21987 /* Set the default cursor types for specified frame. */
21988 void
21989 set_frame_cursor_types (f, arg)
21990 struct frame *f;
21991 Lisp_Object arg;
21992 {
21993 int width;
21994 Lisp_Object tem;
21995
21996 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21997 FRAME_CURSOR_WIDTH (f) = width;
21998
21999 /* By default, set up the blink-off state depending on the on-state. */
22000
22001 tem = Fassoc (arg, Vblink_cursor_alist);
22002 if (!NILP (tem))
22003 {
22004 FRAME_BLINK_OFF_CURSOR (f)
22005 = get_specified_cursor_type (XCDR (tem), &width);
22006 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
22007 }
22008 else
22009 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
22010 }
22011
22012
22013 /* Return the cursor we want to be displayed in window W. Return
22014 width of bar/hbar cursor through WIDTH arg. Return with
22015 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
22016 (i.e. if the `system caret' should track this cursor).
22017
22018 In a mini-buffer window, we want the cursor only to appear if we
22019 are reading input from this window. For the selected window, we
22020 want the cursor type given by the frame parameter or buffer local
22021 setting of cursor-type. If explicitly marked off, draw no cursor.
22022 In all other cases, we want a hollow box cursor. */
22023
22024 static enum text_cursor_kinds
22025 get_window_cursor_type (w, glyph, width, active_cursor)
22026 struct window *w;
22027 struct glyph *glyph;
22028 int *width;
22029 int *active_cursor;
22030 {
22031 struct frame *f = XFRAME (w->frame);
22032 struct buffer *b = XBUFFER (w->buffer);
22033 int cursor_type = DEFAULT_CURSOR;
22034 Lisp_Object alt_cursor;
22035 int non_selected = 0;
22036
22037 *active_cursor = 1;
22038
22039 /* Echo area */
22040 if (cursor_in_echo_area
22041 && FRAME_HAS_MINIBUF_P (f)
22042 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22043 {
22044 if (w == XWINDOW (echo_area_window))
22045 {
22046 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22047 {
22048 *width = FRAME_CURSOR_WIDTH (f);
22049 return FRAME_DESIRED_CURSOR (f);
22050 }
22051 else
22052 return get_specified_cursor_type (b->cursor_type, width);
22053 }
22054
22055 *active_cursor = 0;
22056 non_selected = 1;
22057 }
22058
22059 /* Detect a nonselected window or nonselected frame. */
22060 else if (w != XWINDOW (f->selected_window)
22061 #ifdef HAVE_WINDOW_SYSTEM
22062 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22063 #endif
22064 )
22065 {
22066 *active_cursor = 0;
22067
22068 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22069 return NO_CURSOR;
22070
22071 non_selected = 1;
22072 }
22073
22074 /* Never display a cursor in a window in which cursor-type is nil. */
22075 if (NILP (b->cursor_type))
22076 return NO_CURSOR;
22077
22078 /* Get the normal cursor type for this window. */
22079 if (EQ (b->cursor_type, Qt))
22080 {
22081 cursor_type = FRAME_DESIRED_CURSOR (f);
22082 *width = FRAME_CURSOR_WIDTH (f);
22083 }
22084 else
22085 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22086
22087 /* Use cursor-in-non-selected-windows instead
22088 for non-selected window or frame. */
22089 if (non_selected)
22090 {
22091 alt_cursor = b->cursor_in_non_selected_windows;
22092 if (!EQ (Qt, alt_cursor))
22093 return get_specified_cursor_type (alt_cursor, width);
22094 /* t means modify the normal cursor type. */
22095 if (cursor_type == FILLED_BOX_CURSOR)
22096 cursor_type = HOLLOW_BOX_CURSOR;
22097 else if (cursor_type == BAR_CURSOR && *width > 1)
22098 --*width;
22099 return cursor_type;
22100 }
22101
22102 /* Use normal cursor if not blinked off. */
22103 if (!w->cursor_off_p)
22104 {
22105 #ifdef HAVE_WINDOW_SYSTEM
22106 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22107 {
22108 if (cursor_type == FILLED_BOX_CURSOR)
22109 {
22110 /* Using a block cursor on large images can be very annoying.
22111 So use a hollow cursor for "large" images.
22112 If image is not transparent (no mask), also use hollow cursor. */
22113 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22114 if (img != NULL && IMAGEP (img->spec))
22115 {
22116 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22117 where N = size of default frame font size.
22118 This should cover most of the "tiny" icons people may use. */
22119 if (!img->mask
22120 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22121 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22122 cursor_type = HOLLOW_BOX_CURSOR;
22123 }
22124 }
22125 else if (cursor_type != NO_CURSOR)
22126 {
22127 /* Display current only supports BOX and HOLLOW cursors for images.
22128 So for now, unconditionally use a HOLLOW cursor when cursor is
22129 not a solid box cursor. */
22130 cursor_type = HOLLOW_BOX_CURSOR;
22131 }
22132 }
22133 #endif
22134 return cursor_type;
22135 }
22136
22137 /* Cursor is blinked off, so determine how to "toggle" it. */
22138
22139 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22140 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22141 return get_specified_cursor_type (XCDR (alt_cursor), width);
22142
22143 /* Then see if frame has specified a specific blink off cursor type. */
22144 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22145 {
22146 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22147 return FRAME_BLINK_OFF_CURSOR (f);
22148 }
22149
22150 #if 0
22151 /* Some people liked having a permanently visible blinking cursor,
22152 while others had very strong opinions against it. So it was
22153 decided to remove it. KFS 2003-09-03 */
22154
22155 /* Finally perform built-in cursor blinking:
22156 filled box <-> hollow box
22157 wide [h]bar <-> narrow [h]bar
22158 narrow [h]bar <-> no cursor
22159 other type <-> no cursor */
22160
22161 if (cursor_type == FILLED_BOX_CURSOR)
22162 return HOLLOW_BOX_CURSOR;
22163
22164 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22165 {
22166 *width = 1;
22167 return cursor_type;
22168 }
22169 #endif
22170
22171 return NO_CURSOR;
22172 }
22173
22174
22175 #ifdef HAVE_WINDOW_SYSTEM
22176
22177 /* Notice when the text cursor of window W has been completely
22178 overwritten by a drawing operation that outputs glyphs in AREA
22179 starting at X0 and ending at X1 in the line starting at Y0 and
22180 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22181 the rest of the line after X0 has been written. Y coordinates
22182 are window-relative. */
22183
22184 static void
22185 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22186 struct window *w;
22187 enum glyph_row_area area;
22188 int x0, y0, x1, y1;
22189 {
22190 int cx0, cx1, cy0, cy1;
22191 struct glyph_row *row;
22192
22193 if (!w->phys_cursor_on_p)
22194 return;
22195 if (area != TEXT_AREA)
22196 return;
22197
22198 if (w->phys_cursor.vpos < 0
22199 || w->phys_cursor.vpos >= w->current_matrix->nrows
22200 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22201 !(row->enabled_p && row->displays_text_p)))
22202 return;
22203
22204 if (row->cursor_in_fringe_p)
22205 {
22206 row->cursor_in_fringe_p = 0;
22207 draw_fringe_bitmap (w, row, 0);
22208 w->phys_cursor_on_p = 0;
22209 return;
22210 }
22211
22212 cx0 = w->phys_cursor.x;
22213 cx1 = cx0 + w->phys_cursor_width;
22214 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22215 return;
22216
22217 /* The cursor image will be completely removed from the
22218 screen if the output area intersects the cursor area in
22219 y-direction. When we draw in [y0 y1[, and some part of
22220 the cursor is at y < y0, that part must have been drawn
22221 before. When scrolling, the cursor is erased before
22222 actually scrolling, so we don't come here. When not
22223 scrolling, the rows above the old cursor row must have
22224 changed, and in this case these rows must have written
22225 over the cursor image.
22226
22227 Likewise if part of the cursor is below y1, with the
22228 exception of the cursor being in the first blank row at
22229 the buffer and window end because update_text_area
22230 doesn't draw that row. (Except when it does, but
22231 that's handled in update_text_area.) */
22232
22233 cy0 = w->phys_cursor.y;
22234 cy1 = cy0 + w->phys_cursor_height;
22235 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22236 return;
22237
22238 w->phys_cursor_on_p = 0;
22239 }
22240
22241 #endif /* HAVE_WINDOW_SYSTEM */
22242
22243 \f
22244 /************************************************************************
22245 Mouse Face
22246 ************************************************************************/
22247
22248 #ifdef HAVE_WINDOW_SYSTEM
22249
22250 /* EXPORT for RIF:
22251 Fix the display of area AREA of overlapping row ROW in window W
22252 with respect to the overlapping part OVERLAPS. */
22253
22254 void
22255 x_fix_overlapping_area (w, row, area, overlaps)
22256 struct window *w;
22257 struct glyph_row *row;
22258 enum glyph_row_area area;
22259 int overlaps;
22260 {
22261 int i, x;
22262
22263 BLOCK_INPUT;
22264
22265 x = 0;
22266 for (i = 0; i < row->used[area];)
22267 {
22268 if (row->glyphs[area][i].overlaps_vertically_p)
22269 {
22270 int start = i, start_x = x;
22271
22272 do
22273 {
22274 x += row->glyphs[area][i].pixel_width;
22275 ++i;
22276 }
22277 while (i < row->used[area]
22278 && row->glyphs[area][i].overlaps_vertically_p);
22279
22280 draw_glyphs (w, start_x, row, area,
22281 start, i,
22282 DRAW_NORMAL_TEXT, overlaps);
22283 }
22284 else
22285 {
22286 x += row->glyphs[area][i].pixel_width;
22287 ++i;
22288 }
22289 }
22290
22291 UNBLOCK_INPUT;
22292 }
22293
22294
22295 /* EXPORT:
22296 Draw the cursor glyph of window W in glyph row ROW. See the
22297 comment of draw_glyphs for the meaning of HL. */
22298
22299 void
22300 draw_phys_cursor_glyph (w, row, hl)
22301 struct window *w;
22302 struct glyph_row *row;
22303 enum draw_glyphs_face hl;
22304 {
22305 /* If cursor hpos is out of bounds, don't draw garbage. This can
22306 happen in mini-buffer windows when switching between echo area
22307 glyphs and mini-buffer. */
22308 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22309 {
22310 int on_p = w->phys_cursor_on_p;
22311 int x1;
22312 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22313 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22314 hl, 0);
22315 w->phys_cursor_on_p = on_p;
22316
22317 if (hl == DRAW_CURSOR)
22318 w->phys_cursor_width = x1 - w->phys_cursor.x;
22319 /* When we erase the cursor, and ROW is overlapped by other
22320 rows, make sure that these overlapping parts of other rows
22321 are redrawn. */
22322 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22323 {
22324 w->phys_cursor_width = x1 - w->phys_cursor.x;
22325
22326 if (row > w->current_matrix->rows
22327 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22328 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22329 OVERLAPS_ERASED_CURSOR);
22330
22331 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22332 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22333 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22334 OVERLAPS_ERASED_CURSOR);
22335 }
22336 }
22337 }
22338
22339
22340 /* EXPORT:
22341 Erase the image of a cursor of window W from the screen. */
22342
22343 void
22344 erase_phys_cursor (w)
22345 struct window *w;
22346 {
22347 struct frame *f = XFRAME (w->frame);
22348 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22349 int hpos = w->phys_cursor.hpos;
22350 int vpos = w->phys_cursor.vpos;
22351 int mouse_face_here_p = 0;
22352 struct glyph_matrix *active_glyphs = w->current_matrix;
22353 struct glyph_row *cursor_row;
22354 struct glyph *cursor_glyph;
22355 enum draw_glyphs_face hl;
22356
22357 /* No cursor displayed or row invalidated => nothing to do on the
22358 screen. */
22359 if (w->phys_cursor_type == NO_CURSOR)
22360 goto mark_cursor_off;
22361
22362 /* VPOS >= active_glyphs->nrows means that window has been resized.
22363 Don't bother to erase the cursor. */
22364 if (vpos >= active_glyphs->nrows)
22365 goto mark_cursor_off;
22366
22367 /* If row containing cursor is marked invalid, there is nothing we
22368 can do. */
22369 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22370 if (!cursor_row->enabled_p)
22371 goto mark_cursor_off;
22372
22373 /* If line spacing is > 0, old cursor may only be partially visible in
22374 window after split-window. So adjust visible height. */
22375 cursor_row->visible_height = min (cursor_row->visible_height,
22376 window_text_bottom_y (w) - cursor_row->y);
22377
22378 /* If row is completely invisible, don't attempt to delete a cursor which
22379 isn't there. This can happen if cursor is at top of a window, and
22380 we switch to a buffer with a header line in that window. */
22381 if (cursor_row->visible_height <= 0)
22382 goto mark_cursor_off;
22383
22384 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22385 if (cursor_row->cursor_in_fringe_p)
22386 {
22387 cursor_row->cursor_in_fringe_p = 0;
22388 draw_fringe_bitmap (w, cursor_row, 0);
22389 goto mark_cursor_off;
22390 }
22391
22392 /* This can happen when the new row is shorter than the old one.
22393 In this case, either draw_glyphs or clear_end_of_line
22394 should have cleared the cursor. Note that we wouldn't be
22395 able to erase the cursor in this case because we don't have a
22396 cursor glyph at hand. */
22397 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22398 goto mark_cursor_off;
22399
22400 /* If the cursor is in the mouse face area, redisplay that when
22401 we clear the cursor. */
22402 if (! NILP (dpyinfo->mouse_face_window)
22403 && w == XWINDOW (dpyinfo->mouse_face_window)
22404 && (vpos > dpyinfo->mouse_face_beg_row
22405 || (vpos == dpyinfo->mouse_face_beg_row
22406 && hpos >= dpyinfo->mouse_face_beg_col))
22407 && (vpos < dpyinfo->mouse_face_end_row
22408 || (vpos == dpyinfo->mouse_face_end_row
22409 && hpos < dpyinfo->mouse_face_end_col))
22410 /* Don't redraw the cursor's spot in mouse face if it is at the
22411 end of a line (on a newline). The cursor appears there, but
22412 mouse highlighting does not. */
22413 && cursor_row->used[TEXT_AREA] > hpos)
22414 mouse_face_here_p = 1;
22415
22416 /* Maybe clear the display under the cursor. */
22417 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22418 {
22419 int x, y, left_x;
22420 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22421 int width;
22422
22423 cursor_glyph = get_phys_cursor_glyph (w);
22424 if (cursor_glyph == NULL)
22425 goto mark_cursor_off;
22426
22427 width = cursor_glyph->pixel_width;
22428 left_x = window_box_left_offset (w, TEXT_AREA);
22429 x = w->phys_cursor.x;
22430 if (x < left_x)
22431 width -= left_x - x;
22432 width = min (width, window_box_width (w, TEXT_AREA) - x);
22433 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22434 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22435
22436 if (width > 0)
22437 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22438 }
22439
22440 /* Erase the cursor by redrawing the character underneath it. */
22441 if (mouse_face_here_p)
22442 hl = DRAW_MOUSE_FACE;
22443 else
22444 hl = DRAW_NORMAL_TEXT;
22445 draw_phys_cursor_glyph (w, cursor_row, hl);
22446
22447 mark_cursor_off:
22448 w->phys_cursor_on_p = 0;
22449 w->phys_cursor_type = NO_CURSOR;
22450 }
22451
22452
22453 /* EXPORT:
22454 Display or clear cursor of window W. If ON is zero, clear the
22455 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22456 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22457
22458 void
22459 display_and_set_cursor (w, on, hpos, vpos, x, y)
22460 struct window *w;
22461 int on, hpos, vpos, x, y;
22462 {
22463 struct frame *f = XFRAME (w->frame);
22464 int new_cursor_type;
22465 int new_cursor_width;
22466 int active_cursor;
22467 struct glyph_row *glyph_row;
22468 struct glyph *glyph;
22469
22470 /* This is pointless on invisible frames, and dangerous on garbaged
22471 windows and frames; in the latter case, the frame or window may
22472 be in the midst of changing its size, and x and y may be off the
22473 window. */
22474 if (! FRAME_VISIBLE_P (f)
22475 || FRAME_GARBAGED_P (f)
22476 || vpos >= w->current_matrix->nrows
22477 || hpos >= w->current_matrix->matrix_w)
22478 return;
22479
22480 /* If cursor is off and we want it off, return quickly. */
22481 if (!on && !w->phys_cursor_on_p)
22482 return;
22483
22484 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22485 /* If cursor row is not enabled, we don't really know where to
22486 display the cursor. */
22487 if (!glyph_row->enabled_p)
22488 {
22489 w->phys_cursor_on_p = 0;
22490 return;
22491 }
22492
22493 glyph = NULL;
22494 if (!glyph_row->exact_window_width_line_p
22495 || hpos < glyph_row->used[TEXT_AREA])
22496 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22497
22498 xassert (interrupt_input_blocked);
22499
22500 /* Set new_cursor_type to the cursor we want to be displayed. */
22501 new_cursor_type = get_window_cursor_type (w, glyph,
22502 &new_cursor_width, &active_cursor);
22503
22504 /* If cursor is currently being shown and we don't want it to be or
22505 it is in the wrong place, or the cursor type is not what we want,
22506 erase it. */
22507 if (w->phys_cursor_on_p
22508 && (!on
22509 || w->phys_cursor.x != x
22510 || w->phys_cursor.y != y
22511 || new_cursor_type != w->phys_cursor_type
22512 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22513 && new_cursor_width != w->phys_cursor_width)))
22514 erase_phys_cursor (w);
22515
22516 /* Don't check phys_cursor_on_p here because that flag is only set
22517 to zero in some cases where we know that the cursor has been
22518 completely erased, to avoid the extra work of erasing the cursor
22519 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22520 still not be visible, or it has only been partly erased. */
22521 if (on)
22522 {
22523 w->phys_cursor_ascent = glyph_row->ascent;
22524 w->phys_cursor_height = glyph_row->height;
22525
22526 /* Set phys_cursor_.* before x_draw_.* is called because some
22527 of them may need the information. */
22528 w->phys_cursor.x = x;
22529 w->phys_cursor.y = glyph_row->y;
22530 w->phys_cursor.hpos = hpos;
22531 w->phys_cursor.vpos = vpos;
22532 }
22533
22534 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22535 new_cursor_type, new_cursor_width,
22536 on, active_cursor);
22537 }
22538
22539
22540 /* Switch the display of W's cursor on or off, according to the value
22541 of ON. */
22542
22543 void
22544 update_window_cursor (w, on)
22545 struct window *w;
22546 int on;
22547 {
22548 /* Don't update cursor in windows whose frame is in the process
22549 of being deleted. */
22550 if (w->current_matrix)
22551 {
22552 BLOCK_INPUT;
22553 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22554 w->phys_cursor.x, w->phys_cursor.y);
22555 UNBLOCK_INPUT;
22556 }
22557 }
22558
22559
22560 /* Call update_window_cursor with parameter ON_P on all leaf windows
22561 in the window tree rooted at W. */
22562
22563 static void
22564 update_cursor_in_window_tree (w, on_p)
22565 struct window *w;
22566 int on_p;
22567 {
22568 while (w)
22569 {
22570 if (!NILP (w->hchild))
22571 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22572 else if (!NILP (w->vchild))
22573 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22574 else
22575 update_window_cursor (w, on_p);
22576
22577 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22578 }
22579 }
22580
22581
22582 /* EXPORT:
22583 Display the cursor on window W, or clear it, according to ON_P.
22584 Don't change the cursor's position. */
22585
22586 void
22587 x_update_cursor (f, on_p)
22588 struct frame *f;
22589 int on_p;
22590 {
22591 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22592 }
22593
22594
22595 /* EXPORT:
22596 Clear the cursor of window W to background color, and mark the
22597 cursor as not shown. This is used when the text where the cursor
22598 is about to be rewritten. */
22599
22600 void
22601 x_clear_cursor (w)
22602 struct window *w;
22603 {
22604 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22605 update_window_cursor (w, 0);
22606 }
22607
22608
22609 /* EXPORT:
22610 Display the active region described by mouse_face_* according to DRAW. */
22611
22612 void
22613 show_mouse_face (dpyinfo, draw)
22614 Display_Info *dpyinfo;
22615 enum draw_glyphs_face draw;
22616 {
22617 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22618 struct frame *f = XFRAME (WINDOW_FRAME (w));
22619
22620 if (/* If window is in the process of being destroyed, don't bother
22621 to do anything. */
22622 w->current_matrix != NULL
22623 /* Don't update mouse highlight if hidden */
22624 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22625 /* Recognize when we are called to operate on rows that don't exist
22626 anymore. This can happen when a window is split. */
22627 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22628 {
22629 int phys_cursor_on_p = w->phys_cursor_on_p;
22630 struct glyph_row *row, *first, *last;
22631
22632 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22633 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22634
22635 for (row = first; row <= last && row->enabled_p; ++row)
22636 {
22637 int start_hpos, end_hpos, start_x;
22638
22639 /* For all but the first row, the highlight starts at column 0. */
22640 if (row == first)
22641 {
22642 start_hpos = dpyinfo->mouse_face_beg_col;
22643 start_x = dpyinfo->mouse_face_beg_x;
22644 }
22645 else
22646 {
22647 start_hpos = 0;
22648 start_x = 0;
22649 }
22650
22651 if (row == last)
22652 end_hpos = dpyinfo->mouse_face_end_col;
22653 else
22654 {
22655 end_hpos = row->used[TEXT_AREA];
22656 if (draw == DRAW_NORMAL_TEXT)
22657 row->fill_line_p = 1; /* Clear to end of line */
22658 }
22659
22660 if (end_hpos > start_hpos)
22661 {
22662 draw_glyphs (w, start_x, row, TEXT_AREA,
22663 start_hpos, end_hpos,
22664 draw, 0);
22665
22666 row->mouse_face_p
22667 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22668 }
22669 }
22670
22671 /* When we've written over the cursor, arrange for it to
22672 be displayed again. */
22673 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22674 {
22675 BLOCK_INPUT;
22676 display_and_set_cursor (w, 1,
22677 w->phys_cursor.hpos, w->phys_cursor.vpos,
22678 w->phys_cursor.x, w->phys_cursor.y);
22679 UNBLOCK_INPUT;
22680 }
22681 }
22682
22683 /* Change the mouse cursor. */
22684 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22685 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22686 else if (draw == DRAW_MOUSE_FACE)
22687 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22688 else
22689 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22690 }
22691
22692 /* EXPORT:
22693 Clear out the mouse-highlighted active region.
22694 Redraw it un-highlighted first. Value is non-zero if mouse
22695 face was actually drawn unhighlighted. */
22696
22697 int
22698 clear_mouse_face (dpyinfo)
22699 Display_Info *dpyinfo;
22700 {
22701 int cleared = 0;
22702
22703 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22704 {
22705 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22706 cleared = 1;
22707 }
22708
22709 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22710 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22711 dpyinfo->mouse_face_window = Qnil;
22712 dpyinfo->mouse_face_overlay = Qnil;
22713 return cleared;
22714 }
22715
22716
22717 /* EXPORT:
22718 Non-zero if physical cursor of window W is within mouse face. */
22719
22720 int
22721 cursor_in_mouse_face_p (w)
22722 struct window *w;
22723 {
22724 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22725 int in_mouse_face = 0;
22726
22727 if (WINDOWP (dpyinfo->mouse_face_window)
22728 && XWINDOW (dpyinfo->mouse_face_window) == w)
22729 {
22730 int hpos = w->phys_cursor.hpos;
22731 int vpos = w->phys_cursor.vpos;
22732
22733 if (vpos >= dpyinfo->mouse_face_beg_row
22734 && vpos <= dpyinfo->mouse_face_end_row
22735 && (vpos > dpyinfo->mouse_face_beg_row
22736 || hpos >= dpyinfo->mouse_face_beg_col)
22737 && (vpos < dpyinfo->mouse_face_end_row
22738 || hpos < dpyinfo->mouse_face_end_col
22739 || dpyinfo->mouse_face_past_end))
22740 in_mouse_face = 1;
22741 }
22742
22743 return in_mouse_face;
22744 }
22745
22746
22747
22748 \f
22749 /* This function sets the mouse_face_* elements of DPYINFO, assuming
22750 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
22751 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
22752 for the overlay or run of text properties specifying the mouse
22753 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
22754 before-string and after-string that must also be highlighted.
22755 DISPLAY_STRING, if non-nil, is a display string that may cover some
22756 or all of the highlighted text. */
22757
22758 static void
22759 mouse_face_from_buffer_pos (Lisp_Object window,
22760 Display_Info *dpyinfo,
22761 EMACS_INT mouse_charpos,
22762 EMACS_INT start_charpos,
22763 EMACS_INT end_charpos,
22764 Lisp_Object before_string,
22765 Lisp_Object after_string,
22766 Lisp_Object display_string)
22767 {
22768 struct window *w = XWINDOW (window);
22769 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22770 struct glyph_row *row;
22771 struct glyph *glyph, *end;
22772 EMACS_INT ignore;
22773 int x;
22774
22775 xassert (NILP (display_string) || STRINGP (display_string));
22776 xassert (NILP (before_string) || STRINGP (before_string));
22777 xassert (NILP (after_string) || STRINGP (after_string));
22778
22779 /* Find the first highlighted glyph. */
22780 if (start_charpos < MATRIX_ROW_START_CHARPOS (first))
22781 {
22782 dpyinfo->mouse_face_beg_col = 0;
22783 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (first, w->current_matrix);
22784 dpyinfo->mouse_face_beg_x = first->x;
22785 dpyinfo->mouse_face_beg_y = first->y;
22786 }
22787 else
22788 {
22789 row = row_containing_pos (w, start_charpos, first, NULL, 0);
22790 if (row == NULL)
22791 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22792
22793 /* If the before-string or display-string contains newlines,
22794 row_containing_pos skips to its last row. Move back. */
22795 if (!NILP (before_string) || !NILP (display_string))
22796 {
22797 struct glyph_row *prev;
22798 while ((prev = row - 1, prev >= first)
22799 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
22800 && prev->used[TEXT_AREA] > 0)
22801 {
22802 struct glyph *beg = prev->glyphs[TEXT_AREA];
22803 glyph = beg + prev->used[TEXT_AREA];
22804 while (--glyph >= beg && INTEGERP (glyph->object));
22805 if (glyph < beg
22806 || !(EQ (glyph->object, before_string)
22807 || EQ (glyph->object, display_string)))
22808 break;
22809 row = prev;
22810 }
22811 }
22812
22813 glyph = row->glyphs[TEXT_AREA];
22814 end = glyph + row->used[TEXT_AREA];
22815 x = row->x;
22816 dpyinfo->mouse_face_beg_y = row->y;
22817 dpyinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22818
22819 /* Skip truncation glyphs at the start of the glyph row. */
22820 if (row->displays_text_p)
22821 for (; glyph < end
22822 && INTEGERP (glyph->object)
22823 && glyph->charpos < 0;
22824 ++glyph)
22825 x += glyph->pixel_width;
22826
22827 /* Scan the glyph row, stopping before BEFORE_STRING or
22828 DISPLAY_STRING or START_CHARPOS. */
22829 for (; glyph < end
22830 && !INTEGERP (glyph->object)
22831 && !EQ (glyph->object, before_string)
22832 && !EQ (glyph->object, display_string)
22833 && !(BUFFERP (glyph->object)
22834 && glyph->charpos >= start_charpos);
22835 ++glyph)
22836 x += glyph->pixel_width;
22837
22838 dpyinfo->mouse_face_beg_x = x;
22839 dpyinfo->mouse_face_beg_col = glyph - row->glyphs[TEXT_AREA];
22840 }
22841
22842 /* Find the last highlighted glyph. */
22843 row = row_containing_pos (w, end_charpos, first, NULL, 0);
22844 if (row == NULL)
22845 {
22846 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22847 dpyinfo->mouse_face_past_end = 1;
22848 }
22849 else if (!NILP (after_string))
22850 {
22851 /* If the after-string has newlines, advance to its last row. */
22852 struct glyph_row *next;
22853 struct glyph_row *last
22854 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22855
22856 for (next = row + 1;
22857 next <= last
22858 && next->used[TEXT_AREA] > 0
22859 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
22860 ++next)
22861 row = next;
22862 }
22863
22864 glyph = row->glyphs[TEXT_AREA];
22865 end = glyph + row->used[TEXT_AREA];
22866 x = row->x;
22867 dpyinfo->mouse_face_end_y = row->y;
22868 dpyinfo->mouse_face_end_row = MATRIX_ROW_VPOS (row, w->current_matrix);
22869
22870 /* Skip truncation glyphs at the start of the row. */
22871 if (row->displays_text_p)
22872 for (; glyph < end
22873 && INTEGERP (glyph->object)
22874 && glyph->charpos < 0;
22875 ++glyph)
22876 x += glyph->pixel_width;
22877
22878 /* Scan the glyph row, stopping at END_CHARPOS or when we encounter
22879 AFTER_STRING. */
22880 for (; glyph < end
22881 && !INTEGERP (glyph->object)
22882 && !EQ (glyph->object, after_string)
22883 && !(BUFFERP (glyph->object) && glyph->charpos >= end_charpos);
22884 ++glyph)
22885 x += glyph->pixel_width;
22886
22887 /* If we found AFTER_STRING, consume it and stop. */
22888 if (EQ (glyph->object, after_string))
22889 {
22890 for (; EQ (glyph->object, after_string) && glyph < end; ++glyph)
22891 x += glyph->pixel_width;
22892 }
22893 else
22894 {
22895 /* If there's no after-string, we must check if we overshot,
22896 which might be the case if we stopped after a string glyph.
22897 That glyph may belong to a before-string or display-string
22898 associated with the end position, which must not be
22899 highlighted. */
22900 Lisp_Object prev_object;
22901 int pos;
22902
22903 while (glyph > row->glyphs[TEXT_AREA])
22904 {
22905 prev_object = (glyph - 1)->object;
22906 if (!STRINGP (prev_object) || EQ (prev_object, display_string))
22907 break;
22908
22909 pos = string_buffer_position (w, prev_object, end_charpos);
22910 if (pos && pos < end_charpos)
22911 break;
22912
22913 for (; glyph > row->glyphs[TEXT_AREA]
22914 && EQ ((glyph - 1)->object, prev_object);
22915 --glyph)
22916 x -= (glyph - 1)->pixel_width;
22917 }
22918 }
22919
22920 dpyinfo->mouse_face_end_x = x;
22921 dpyinfo->mouse_face_end_col = glyph - row->glyphs[TEXT_AREA];
22922 dpyinfo->mouse_face_window = window;
22923 dpyinfo->mouse_face_face_id
22924 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
22925 mouse_charpos + 1,
22926 !dpyinfo->mouse_face_hidden, -1);
22927 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22928 }
22929
22930
22931 /* Find the position of the glyph for position POS in OBJECT in
22932 window W's current matrix, and return in *X, *Y the pixel
22933 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22934
22935 RIGHT_P non-zero means return the position of the right edge of the
22936 glyph, RIGHT_P zero means return the left edge position.
22937
22938 If no glyph for POS exists in the matrix, return the position of
22939 the glyph with the next smaller position that is in the matrix, if
22940 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22941 exists in the matrix, return the position of the glyph with the
22942 next larger position in OBJECT.
22943
22944 Value is non-zero if a glyph was found. */
22945
22946 static int
22947 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22948 struct window *w;
22949 EMACS_INT pos;
22950 Lisp_Object object;
22951 int *hpos, *vpos, *x, *y;
22952 int right_p;
22953 {
22954 int yb = window_text_bottom_y (w);
22955 struct glyph_row *r;
22956 struct glyph *best_glyph = NULL;
22957 struct glyph_row *best_row = NULL;
22958 int best_x = 0;
22959
22960 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22961 r->enabled_p && r->y < yb;
22962 ++r)
22963 {
22964 struct glyph *g = r->glyphs[TEXT_AREA];
22965 struct glyph *e = g + r->used[TEXT_AREA];
22966 int gx;
22967
22968 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22969 if (EQ (g->object, object))
22970 {
22971 if (g->charpos == pos)
22972 {
22973 best_glyph = g;
22974 best_x = gx;
22975 best_row = r;
22976 goto found;
22977 }
22978 else if (best_glyph == NULL
22979 || ((eabs (g->charpos - pos)
22980 < eabs (best_glyph->charpos - pos))
22981 && (right_p
22982 ? g->charpos < pos
22983 : g->charpos > pos)))
22984 {
22985 best_glyph = g;
22986 best_x = gx;
22987 best_row = r;
22988 }
22989 }
22990 }
22991
22992 found:
22993
22994 if (best_glyph)
22995 {
22996 *x = best_x;
22997 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22998
22999 if (right_p)
23000 {
23001 *x += best_glyph->pixel_width;
23002 ++*hpos;
23003 }
23004
23005 *y = best_row->y;
23006 *vpos = best_row - w->current_matrix->rows;
23007 }
23008
23009 return best_glyph != NULL;
23010 }
23011
23012
23013 /* See if position X, Y is within a hot-spot of an image. */
23014
23015 static int
23016 on_hot_spot_p (hot_spot, x, y)
23017 Lisp_Object hot_spot;
23018 int x, y;
23019 {
23020 if (!CONSP (hot_spot))
23021 return 0;
23022
23023 if (EQ (XCAR (hot_spot), Qrect))
23024 {
23025 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23026 Lisp_Object rect = XCDR (hot_spot);
23027 Lisp_Object tem;
23028 if (!CONSP (rect))
23029 return 0;
23030 if (!CONSP (XCAR (rect)))
23031 return 0;
23032 if (!CONSP (XCDR (rect)))
23033 return 0;
23034 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23035 return 0;
23036 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23037 return 0;
23038 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23039 return 0;
23040 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23041 return 0;
23042 return 1;
23043 }
23044 else if (EQ (XCAR (hot_spot), Qcircle))
23045 {
23046 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23047 Lisp_Object circ = XCDR (hot_spot);
23048 Lisp_Object lr, lx0, ly0;
23049 if (CONSP (circ)
23050 && CONSP (XCAR (circ))
23051 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23052 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23053 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23054 {
23055 double r = XFLOATINT (lr);
23056 double dx = XINT (lx0) - x;
23057 double dy = XINT (ly0) - y;
23058 return (dx * dx + dy * dy <= r * r);
23059 }
23060 }
23061 else if (EQ (XCAR (hot_spot), Qpoly))
23062 {
23063 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23064 if (VECTORP (XCDR (hot_spot)))
23065 {
23066 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23067 Lisp_Object *poly = v->contents;
23068 int n = v->size;
23069 int i;
23070 int inside = 0;
23071 Lisp_Object lx, ly;
23072 int x0, y0;
23073
23074 /* Need an even number of coordinates, and at least 3 edges. */
23075 if (n < 6 || n & 1)
23076 return 0;
23077
23078 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23079 If count is odd, we are inside polygon. Pixels on edges
23080 may or may not be included depending on actual geometry of the
23081 polygon. */
23082 if ((lx = poly[n-2], !INTEGERP (lx))
23083 || (ly = poly[n-1], !INTEGERP (lx)))
23084 return 0;
23085 x0 = XINT (lx), y0 = XINT (ly);
23086 for (i = 0; i < n; i += 2)
23087 {
23088 int x1 = x0, y1 = y0;
23089 if ((lx = poly[i], !INTEGERP (lx))
23090 || (ly = poly[i+1], !INTEGERP (ly)))
23091 return 0;
23092 x0 = XINT (lx), y0 = XINT (ly);
23093
23094 /* Does this segment cross the X line? */
23095 if (x0 >= x)
23096 {
23097 if (x1 >= x)
23098 continue;
23099 }
23100 else if (x1 < x)
23101 continue;
23102 if (y > y0 && y > y1)
23103 continue;
23104 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23105 inside = !inside;
23106 }
23107 return inside;
23108 }
23109 }
23110 return 0;
23111 }
23112
23113 Lisp_Object
23114 find_hot_spot (map, x, y)
23115 Lisp_Object map;
23116 int x, y;
23117 {
23118 while (CONSP (map))
23119 {
23120 if (CONSP (XCAR (map))
23121 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23122 return XCAR (map);
23123 map = XCDR (map);
23124 }
23125
23126 return Qnil;
23127 }
23128
23129 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23130 3, 3, 0,
23131 doc: /* Lookup in image map MAP coordinates X and Y.
23132 An image map is an alist where each element has the format (AREA ID PLIST).
23133 An AREA is specified as either a rectangle, a circle, or a polygon:
23134 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23135 pixel coordinates of the upper left and bottom right corners.
23136 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23137 and the radius of the circle; r may be a float or integer.
23138 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23139 vector describes one corner in the polygon.
23140 Returns the alist element for the first matching AREA in MAP. */)
23141 (map, x, y)
23142 Lisp_Object map;
23143 Lisp_Object x, y;
23144 {
23145 if (NILP (map))
23146 return Qnil;
23147
23148 CHECK_NUMBER (x);
23149 CHECK_NUMBER (y);
23150
23151 return find_hot_spot (map, XINT (x), XINT (y));
23152 }
23153
23154
23155 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23156 static void
23157 define_frame_cursor1 (f, cursor, pointer)
23158 struct frame *f;
23159 Cursor cursor;
23160 Lisp_Object pointer;
23161 {
23162 /* Do not change cursor shape while dragging mouse. */
23163 if (!NILP (do_mouse_tracking))
23164 return;
23165
23166 if (!NILP (pointer))
23167 {
23168 if (EQ (pointer, Qarrow))
23169 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23170 else if (EQ (pointer, Qhand))
23171 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23172 else if (EQ (pointer, Qtext))
23173 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23174 else if (EQ (pointer, intern ("hdrag")))
23175 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23176 #ifdef HAVE_X_WINDOWS
23177 else if (EQ (pointer, intern ("vdrag")))
23178 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23179 #endif
23180 else if (EQ (pointer, intern ("hourglass")))
23181 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23182 else if (EQ (pointer, Qmodeline))
23183 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23184 else
23185 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23186 }
23187
23188 if (cursor != No_Cursor)
23189 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23190 }
23191
23192 /* Take proper action when mouse has moved to the mode or header line
23193 or marginal area AREA of window W, x-position X and y-position Y.
23194 X is relative to the start of the text display area of W, so the
23195 width of bitmap areas and scroll bars must be subtracted to get a
23196 position relative to the start of the mode line. */
23197
23198 static void
23199 note_mode_line_or_margin_highlight (window, x, y, area)
23200 Lisp_Object window;
23201 int x, y;
23202 enum window_part area;
23203 {
23204 struct window *w = XWINDOW (window);
23205 struct frame *f = XFRAME (w->frame);
23206 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23207 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23208 Lisp_Object pointer = Qnil;
23209 int charpos, dx, dy, width, height;
23210 Lisp_Object string, object = Qnil;
23211 Lisp_Object pos, help;
23212
23213 Lisp_Object mouse_face;
23214 int original_x_pixel = x;
23215 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23216 struct glyph_row *row;
23217
23218 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23219 {
23220 int x0;
23221 struct glyph *end;
23222
23223 string = mode_line_string (w, area, &x, &y, &charpos,
23224 &object, &dx, &dy, &width, &height);
23225
23226 row = (area == ON_MODE_LINE
23227 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23228 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23229
23230 /* Find glyph */
23231 if (row->mode_line_p && row->enabled_p)
23232 {
23233 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23234 end = glyph + row->used[TEXT_AREA];
23235
23236 for (x0 = original_x_pixel;
23237 glyph < end && x0 >= glyph->pixel_width;
23238 ++glyph)
23239 x0 -= glyph->pixel_width;
23240
23241 if (glyph >= end)
23242 glyph = NULL;
23243 }
23244 }
23245 else
23246 {
23247 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23248 string = marginal_area_string (w, area, &x, &y, &charpos,
23249 &object, &dx, &dy, &width, &height);
23250 }
23251
23252 help = Qnil;
23253
23254 if (IMAGEP (object))
23255 {
23256 Lisp_Object image_map, hotspot;
23257 if ((image_map = Fplist_get (XCDR (object), QCmap),
23258 !NILP (image_map))
23259 && (hotspot = find_hot_spot (image_map, dx, dy),
23260 CONSP (hotspot))
23261 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23262 {
23263 Lisp_Object area_id, plist;
23264
23265 area_id = XCAR (hotspot);
23266 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23267 If so, we could look for mouse-enter, mouse-leave
23268 properties in PLIST (and do something...). */
23269 hotspot = XCDR (hotspot);
23270 if (CONSP (hotspot)
23271 && (plist = XCAR (hotspot), CONSP (plist)))
23272 {
23273 pointer = Fplist_get (plist, Qpointer);
23274 if (NILP (pointer))
23275 pointer = Qhand;
23276 help = Fplist_get (plist, Qhelp_echo);
23277 if (!NILP (help))
23278 {
23279 help_echo_string = help;
23280 /* Is this correct? ++kfs */
23281 XSETWINDOW (help_echo_window, w);
23282 help_echo_object = w->buffer;
23283 help_echo_pos = charpos;
23284 }
23285 }
23286 }
23287 if (NILP (pointer))
23288 pointer = Fplist_get (XCDR (object), QCpointer);
23289 }
23290
23291 if (STRINGP (string))
23292 {
23293 pos = make_number (charpos);
23294 /* If we're on a string with `help-echo' text property, arrange
23295 for the help to be displayed. This is done by setting the
23296 global variable help_echo_string to the help string. */
23297 if (NILP (help))
23298 {
23299 help = Fget_text_property (pos, Qhelp_echo, string);
23300 if (!NILP (help))
23301 {
23302 help_echo_string = help;
23303 XSETWINDOW (help_echo_window, w);
23304 help_echo_object = string;
23305 help_echo_pos = charpos;
23306 }
23307 }
23308
23309 if (NILP (pointer))
23310 pointer = Fget_text_property (pos, Qpointer, string);
23311
23312 /* Change the mouse pointer according to what is under X/Y. */
23313 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23314 {
23315 Lisp_Object map;
23316 map = Fget_text_property (pos, Qlocal_map, string);
23317 if (!KEYMAPP (map))
23318 map = Fget_text_property (pos, Qkeymap, string);
23319 if (!KEYMAPP (map))
23320 cursor = dpyinfo->vertical_scroll_bar_cursor;
23321 }
23322
23323 /* Change the mouse face according to what is under X/Y. */
23324 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23325 if (!NILP (mouse_face)
23326 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23327 && glyph)
23328 {
23329 Lisp_Object b, e;
23330
23331 struct glyph * tmp_glyph;
23332
23333 int gpos;
23334 int gseq_length;
23335 int total_pixel_width;
23336 EMACS_INT ignore;
23337
23338 int vpos, hpos;
23339
23340 b = Fprevious_single_property_change (make_number (charpos + 1),
23341 Qmouse_face, string, Qnil);
23342 if (NILP (b))
23343 b = make_number (0);
23344
23345 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23346 if (NILP (e))
23347 e = make_number (SCHARS (string));
23348
23349 /* Calculate the position(glyph position: GPOS) of GLYPH in
23350 displayed string. GPOS is different from CHARPOS.
23351
23352 CHARPOS is the position of glyph in internal string
23353 object. A mode line string format has structures which
23354 is converted to a flatten by emacs lisp interpreter.
23355 The internal string is an element of the structures.
23356 The displayed string is the flatten string. */
23357 gpos = 0;
23358 if (glyph > row_start_glyph)
23359 {
23360 tmp_glyph = glyph - 1;
23361 while (tmp_glyph >= row_start_glyph
23362 && tmp_glyph->charpos >= XINT (b)
23363 && EQ (tmp_glyph->object, glyph->object))
23364 {
23365 tmp_glyph--;
23366 gpos++;
23367 }
23368 }
23369
23370 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23371 displayed string holding GLYPH.
23372
23373 GSEQ_LENGTH is different from SCHARS (STRING).
23374 SCHARS (STRING) returns the length of the internal string. */
23375 for (tmp_glyph = glyph, gseq_length = gpos;
23376 tmp_glyph->charpos < XINT (e);
23377 tmp_glyph++, gseq_length++)
23378 {
23379 if (!EQ (tmp_glyph->object, glyph->object))
23380 break;
23381 }
23382
23383 total_pixel_width = 0;
23384 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23385 total_pixel_width += tmp_glyph->pixel_width;
23386
23387 /* Pre calculation of re-rendering position */
23388 vpos = (x - gpos);
23389 hpos = (area == ON_MODE_LINE
23390 ? (w->current_matrix)->nrows - 1
23391 : 0);
23392
23393 /* If the re-rendering position is included in the last
23394 re-rendering area, we should do nothing. */
23395 if ( EQ (window, dpyinfo->mouse_face_window)
23396 && dpyinfo->mouse_face_beg_col <= vpos
23397 && vpos < dpyinfo->mouse_face_end_col
23398 && dpyinfo->mouse_face_beg_row == hpos )
23399 return;
23400
23401 if (clear_mouse_face (dpyinfo))
23402 cursor = No_Cursor;
23403
23404 dpyinfo->mouse_face_beg_col = vpos;
23405 dpyinfo->mouse_face_beg_row = hpos;
23406
23407 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23408 dpyinfo->mouse_face_beg_y = 0;
23409
23410 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23411 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23412
23413 dpyinfo->mouse_face_end_x = 0;
23414 dpyinfo->mouse_face_end_y = 0;
23415
23416 dpyinfo->mouse_face_past_end = 0;
23417 dpyinfo->mouse_face_window = window;
23418
23419 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23420 charpos,
23421 0, 0, 0, &ignore,
23422 glyph->face_id, 1);
23423 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23424
23425 if (NILP (pointer))
23426 pointer = Qhand;
23427 }
23428 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23429 clear_mouse_face (dpyinfo);
23430 }
23431 define_frame_cursor1 (f, cursor, pointer);
23432 }
23433
23434
23435 /* EXPORT:
23436 Take proper action when the mouse has moved to position X, Y on
23437 frame F as regards highlighting characters that have mouse-face
23438 properties. Also de-highlighting chars where the mouse was before.
23439 X and Y can be negative or out of range. */
23440
23441 void
23442 note_mouse_highlight (f, x, y)
23443 struct frame *f;
23444 int x, y;
23445 {
23446 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23447 enum window_part part;
23448 Lisp_Object window;
23449 struct window *w;
23450 Cursor cursor = No_Cursor;
23451 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23452 struct buffer *b;
23453
23454 /* When a menu is active, don't highlight because this looks odd. */
23455 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
23456 if (popup_activated ())
23457 return;
23458 #endif
23459
23460 if (NILP (Vmouse_highlight)
23461 || !f->glyphs_initialized_p
23462 || f->pointer_invisible)
23463 return;
23464
23465 dpyinfo->mouse_face_mouse_x = x;
23466 dpyinfo->mouse_face_mouse_y = y;
23467 dpyinfo->mouse_face_mouse_frame = f;
23468
23469 if (dpyinfo->mouse_face_defer)
23470 return;
23471
23472 if (gc_in_progress)
23473 {
23474 dpyinfo->mouse_face_deferred_gc = 1;
23475 return;
23476 }
23477
23478 /* Which window is that in? */
23479 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23480
23481 /* If we were displaying active text in another window, clear that.
23482 Also clear if we move out of text area in same window. */
23483 if (! EQ (window, dpyinfo->mouse_face_window)
23484 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23485 && !NILP (dpyinfo->mouse_face_window)))
23486 clear_mouse_face (dpyinfo);
23487
23488 /* Not on a window -> return. */
23489 if (!WINDOWP (window))
23490 return;
23491
23492 /* Reset help_echo_string. It will get recomputed below. */
23493 help_echo_string = Qnil;
23494
23495 /* Convert to window-relative pixel coordinates. */
23496 w = XWINDOW (window);
23497 frame_to_window_pixel_xy (w, &x, &y);
23498
23499 /* Handle tool-bar window differently since it doesn't display a
23500 buffer. */
23501 if (EQ (window, f->tool_bar_window))
23502 {
23503 note_tool_bar_highlight (f, x, y);
23504 return;
23505 }
23506
23507 /* Mouse is on the mode, header line or margin? */
23508 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23509 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23510 {
23511 note_mode_line_or_margin_highlight (window, x, y, part);
23512 return;
23513 }
23514
23515 if (part == ON_VERTICAL_BORDER)
23516 {
23517 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23518 help_echo_string = build_string ("drag-mouse-1: resize");
23519 }
23520 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23521 || part == ON_SCROLL_BAR)
23522 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23523 else
23524 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23525
23526 /* Are we in a window whose display is up to date?
23527 And verify the buffer's text has not changed. */
23528 b = XBUFFER (w->buffer);
23529 if (part == ON_TEXT
23530 && EQ (w->window_end_valid, w->buffer)
23531 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23532 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23533 {
23534 int hpos, vpos, pos, i, dx, dy, area;
23535 struct glyph *glyph;
23536 Lisp_Object object;
23537 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23538 Lisp_Object *overlay_vec = NULL;
23539 int noverlays;
23540 struct buffer *obuf;
23541 int obegv, ozv, same_region;
23542
23543 /* Find the glyph under X/Y. */
23544 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23545
23546 /* Look for :pointer property on image. */
23547 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23548 {
23549 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23550 if (img != NULL && IMAGEP (img->spec))
23551 {
23552 Lisp_Object image_map, hotspot;
23553 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23554 !NILP (image_map))
23555 && (hotspot = find_hot_spot (image_map,
23556 glyph->slice.x + dx,
23557 glyph->slice.y + dy),
23558 CONSP (hotspot))
23559 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23560 {
23561 Lisp_Object area_id, plist;
23562
23563 area_id = XCAR (hotspot);
23564 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23565 If so, we could look for mouse-enter, mouse-leave
23566 properties in PLIST (and do something...). */
23567 hotspot = XCDR (hotspot);
23568 if (CONSP (hotspot)
23569 && (plist = XCAR (hotspot), CONSP (plist)))
23570 {
23571 pointer = Fplist_get (plist, Qpointer);
23572 if (NILP (pointer))
23573 pointer = Qhand;
23574 help_echo_string = Fplist_get (plist, Qhelp_echo);
23575 if (!NILP (help_echo_string))
23576 {
23577 help_echo_window = window;
23578 help_echo_object = glyph->object;
23579 help_echo_pos = glyph->charpos;
23580 }
23581 }
23582 }
23583 if (NILP (pointer))
23584 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23585 }
23586 }
23587
23588 /* Clear mouse face if X/Y not over text. */
23589 if (glyph == NULL
23590 || area != TEXT_AREA
23591 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23592 {
23593 if (clear_mouse_face (dpyinfo))
23594 cursor = No_Cursor;
23595 if (NILP (pointer))
23596 {
23597 if (area != TEXT_AREA)
23598 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23599 else
23600 pointer = Vvoid_text_area_pointer;
23601 }
23602 goto set_cursor;
23603 }
23604
23605 pos = glyph->charpos;
23606 object = glyph->object;
23607 if (!STRINGP (object) && !BUFFERP (object))
23608 goto set_cursor;
23609
23610 /* If we get an out-of-range value, return now; avoid an error. */
23611 if (BUFFERP (object) && pos > BUF_Z (b))
23612 goto set_cursor;
23613
23614 /* Make the window's buffer temporarily current for
23615 overlays_at and compute_char_face. */
23616 obuf = current_buffer;
23617 current_buffer = b;
23618 obegv = BEGV;
23619 ozv = ZV;
23620 BEGV = BEG;
23621 ZV = Z;
23622
23623 /* Is this char mouse-active or does it have help-echo? */
23624 position = make_number (pos);
23625
23626 if (BUFFERP (object))
23627 {
23628 /* Put all the overlays we want in a vector in overlay_vec. */
23629 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23630 /* Sort overlays into increasing priority order. */
23631 noverlays = sort_overlays (overlay_vec, noverlays, w);
23632 }
23633 else
23634 noverlays = 0;
23635
23636 same_region = (EQ (window, dpyinfo->mouse_face_window)
23637 && vpos >= dpyinfo->mouse_face_beg_row
23638 && vpos <= dpyinfo->mouse_face_end_row
23639 && (vpos > dpyinfo->mouse_face_beg_row
23640 || hpos >= dpyinfo->mouse_face_beg_col)
23641 && (vpos < dpyinfo->mouse_face_end_row
23642 || hpos < dpyinfo->mouse_face_end_col
23643 || dpyinfo->mouse_face_past_end));
23644
23645 if (same_region)
23646 cursor = No_Cursor;
23647
23648 /* Check mouse-face highlighting. */
23649 if (! same_region
23650 /* If there exists an overlay with mouse-face overlapping
23651 the one we are currently highlighting, we have to
23652 check if we enter the overlapping overlay, and then
23653 highlight only that. */
23654 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23655 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23656 {
23657 /* Find the highest priority overlay with a mouse-face. */
23658 overlay = Qnil;
23659 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23660 {
23661 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23662 if (!NILP (mouse_face))
23663 overlay = overlay_vec[i];
23664 }
23665
23666 /* If we're highlighting the same overlay as before, there's
23667 no need to do that again. */
23668 if (!NILP (overlay) && EQ (overlay, dpyinfo->mouse_face_overlay))
23669 goto check_help_echo;
23670 dpyinfo->mouse_face_overlay = overlay;
23671
23672 /* Clear the display of the old active region, if any. */
23673 if (clear_mouse_face (dpyinfo))
23674 cursor = No_Cursor;
23675
23676 /* If no overlay applies, get a text property. */
23677 if (NILP (overlay))
23678 mouse_face = Fget_text_property (position, Qmouse_face, object);
23679
23680 /* Next, compute the bounds of the mouse highlighting and
23681 display it. */
23682 if (!NILP (mouse_face) && STRINGP (object))
23683 {
23684 /* The mouse-highlighting comes from a display string
23685 with a mouse-face. */
23686 Lisp_Object b, e;
23687 EMACS_INT ignore;
23688
23689 b = Fprevious_single_property_change
23690 (make_number (pos + 1), Qmouse_face, object, Qnil);
23691 e = Fnext_single_property_change
23692 (position, Qmouse_face, object, Qnil);
23693 if (NILP (b))
23694 b = make_number (0);
23695 if (NILP (e))
23696 e = make_number (SCHARS (object) - 1);
23697
23698 fast_find_string_pos (w, XINT (b), object,
23699 &dpyinfo->mouse_face_beg_col,
23700 &dpyinfo->mouse_face_beg_row,
23701 &dpyinfo->mouse_face_beg_x,
23702 &dpyinfo->mouse_face_beg_y, 0);
23703 fast_find_string_pos (w, XINT (e), object,
23704 &dpyinfo->mouse_face_end_col,
23705 &dpyinfo->mouse_face_end_row,
23706 &dpyinfo->mouse_face_end_x,
23707 &dpyinfo->mouse_face_end_y, 1);
23708 dpyinfo->mouse_face_past_end = 0;
23709 dpyinfo->mouse_face_window = window;
23710 dpyinfo->mouse_face_face_id
23711 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23712 glyph->face_id, 1);
23713 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23714 cursor = No_Cursor;
23715 }
23716 else
23717 {
23718 /* The mouse-highlighting, if any, comes from an overlay
23719 or text property in the buffer. */
23720 Lisp_Object buffer, display_string;
23721
23722 if (STRINGP (object))
23723 {
23724 /* If we are on a display string with no mouse-face,
23725 check if the text under it has one. */
23726 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23727 int start = MATRIX_ROW_START_CHARPOS (r);
23728 pos = string_buffer_position (w, object, start);
23729 if (pos > 0)
23730 {
23731 mouse_face = get_char_property_and_overlay
23732 (make_number (pos), Qmouse_face, w->buffer, &overlay);
23733 buffer = w->buffer;
23734 display_string = object;
23735 }
23736 }
23737 else
23738 {
23739 buffer = object;
23740 display_string = Qnil;
23741 }
23742
23743 if (!NILP (mouse_face))
23744 {
23745 Lisp_Object before, after;
23746 Lisp_Object before_string, after_string;
23747
23748 if (NILP (overlay))
23749 {
23750 /* Handle the text property case. */
23751 before = Fprevious_single_property_change
23752 (make_number (pos + 1), Qmouse_face, buffer,
23753 Fmarker_position (w->start));
23754 after = Fnext_single_property_change
23755 (make_number (pos), Qmouse_face, buffer,
23756 make_number (BUF_Z (XBUFFER (buffer))
23757 - XFASTINT (w->window_end_pos)));
23758 before_string = after_string = Qnil;
23759 }
23760 else
23761 {
23762 /* Handle the overlay case. */
23763 before = Foverlay_start (overlay);
23764 after = Foverlay_end (overlay);
23765 before_string = Foverlay_get (overlay, Qbefore_string);
23766 after_string = Foverlay_get (overlay, Qafter_string);
23767
23768 if (!STRINGP (before_string)) before_string = Qnil;
23769 if (!STRINGP (after_string)) after_string = Qnil;
23770 }
23771
23772 mouse_face_from_buffer_pos (window, dpyinfo, pos,
23773 XFASTINT (before),
23774 XFASTINT (after),
23775 before_string, after_string,
23776 display_string);
23777 cursor = No_Cursor;
23778 }
23779 }
23780 }
23781
23782 check_help_echo:
23783
23784 /* Look for a `help-echo' property. */
23785 if (NILP (help_echo_string)) {
23786 Lisp_Object help, overlay;
23787
23788 /* Check overlays first. */
23789 help = overlay = Qnil;
23790 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23791 {
23792 overlay = overlay_vec[i];
23793 help = Foverlay_get (overlay, Qhelp_echo);
23794 }
23795
23796 if (!NILP (help))
23797 {
23798 help_echo_string = help;
23799 help_echo_window = window;
23800 help_echo_object = overlay;
23801 help_echo_pos = pos;
23802 }
23803 else
23804 {
23805 Lisp_Object object = glyph->object;
23806 int charpos = glyph->charpos;
23807
23808 /* Try text properties. */
23809 if (STRINGP (object)
23810 && charpos >= 0
23811 && charpos < SCHARS (object))
23812 {
23813 help = Fget_text_property (make_number (charpos),
23814 Qhelp_echo, object);
23815 if (NILP (help))
23816 {
23817 /* If the string itself doesn't specify a help-echo,
23818 see if the buffer text ``under'' it does. */
23819 struct glyph_row *r
23820 = MATRIX_ROW (w->current_matrix, vpos);
23821 int start = MATRIX_ROW_START_CHARPOS (r);
23822 int pos = string_buffer_position (w, object, start);
23823 if (pos > 0)
23824 {
23825 help = Fget_char_property (make_number (pos),
23826 Qhelp_echo, w->buffer);
23827 if (!NILP (help))
23828 {
23829 charpos = pos;
23830 object = w->buffer;
23831 }
23832 }
23833 }
23834 }
23835 else if (BUFFERP (object)
23836 && charpos >= BEGV
23837 && charpos < ZV)
23838 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23839 object);
23840
23841 if (!NILP (help))
23842 {
23843 help_echo_string = help;
23844 help_echo_window = window;
23845 help_echo_object = object;
23846 help_echo_pos = charpos;
23847 }
23848 }
23849 }
23850
23851 /* Look for a `pointer' property. */
23852 if (NILP (pointer))
23853 {
23854 /* Check overlays first. */
23855 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23856 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23857
23858 if (NILP (pointer))
23859 {
23860 Lisp_Object object = glyph->object;
23861 int charpos = glyph->charpos;
23862
23863 /* Try text properties. */
23864 if (STRINGP (object)
23865 && charpos >= 0
23866 && charpos < SCHARS (object))
23867 {
23868 pointer = Fget_text_property (make_number (charpos),
23869 Qpointer, object);
23870 if (NILP (pointer))
23871 {
23872 /* If the string itself doesn't specify a pointer,
23873 see if the buffer text ``under'' it does. */
23874 struct glyph_row *r
23875 = MATRIX_ROW (w->current_matrix, vpos);
23876 int start = MATRIX_ROW_START_CHARPOS (r);
23877 int pos = string_buffer_position (w, object, start);
23878 if (pos > 0)
23879 pointer = Fget_char_property (make_number (pos),
23880 Qpointer, w->buffer);
23881 }
23882 }
23883 else if (BUFFERP (object)
23884 && charpos >= BEGV
23885 && charpos < ZV)
23886 pointer = Fget_text_property (make_number (charpos),
23887 Qpointer, object);
23888 }
23889 }
23890
23891 BEGV = obegv;
23892 ZV = ozv;
23893 current_buffer = obuf;
23894 }
23895
23896 set_cursor:
23897
23898 define_frame_cursor1 (f, cursor, pointer);
23899 }
23900
23901
23902 /* EXPORT for RIF:
23903 Clear any mouse-face on window W. This function is part of the
23904 redisplay interface, and is called from try_window_id and similar
23905 functions to ensure the mouse-highlight is off. */
23906
23907 void
23908 x_clear_window_mouse_face (w)
23909 struct window *w;
23910 {
23911 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23912 Lisp_Object window;
23913
23914 BLOCK_INPUT;
23915 XSETWINDOW (window, w);
23916 if (EQ (window, dpyinfo->mouse_face_window))
23917 clear_mouse_face (dpyinfo);
23918 UNBLOCK_INPUT;
23919 }
23920
23921
23922 /* EXPORT:
23923 Just discard the mouse face information for frame F, if any.
23924 This is used when the size of F is changed. */
23925
23926 void
23927 cancel_mouse_face (f)
23928 struct frame *f;
23929 {
23930 Lisp_Object window;
23931 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23932
23933 window = dpyinfo->mouse_face_window;
23934 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23935 {
23936 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23937 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23938 dpyinfo->mouse_face_window = Qnil;
23939 }
23940 }
23941
23942
23943 #endif /* HAVE_WINDOW_SYSTEM */
23944
23945 \f
23946 /***********************************************************************
23947 Exposure Events
23948 ***********************************************************************/
23949
23950 #ifdef HAVE_WINDOW_SYSTEM
23951
23952 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23953 which intersects rectangle R. R is in window-relative coordinates. */
23954
23955 static void
23956 expose_area (w, row, r, area)
23957 struct window *w;
23958 struct glyph_row *row;
23959 XRectangle *r;
23960 enum glyph_row_area area;
23961 {
23962 struct glyph *first = row->glyphs[area];
23963 struct glyph *end = row->glyphs[area] + row->used[area];
23964 struct glyph *last;
23965 int first_x, start_x, x;
23966
23967 if (area == TEXT_AREA && row->fill_line_p)
23968 /* If row extends face to end of line write the whole line. */
23969 draw_glyphs (w, 0, row, area,
23970 0, row->used[area],
23971 DRAW_NORMAL_TEXT, 0);
23972 else
23973 {
23974 /* Set START_X to the window-relative start position for drawing glyphs of
23975 AREA. The first glyph of the text area can be partially visible.
23976 The first glyphs of other areas cannot. */
23977 start_x = window_box_left_offset (w, area);
23978 x = start_x;
23979 if (area == TEXT_AREA)
23980 x += row->x;
23981
23982 /* Find the first glyph that must be redrawn. */
23983 while (first < end
23984 && x + first->pixel_width < r->x)
23985 {
23986 x += first->pixel_width;
23987 ++first;
23988 }
23989
23990 /* Find the last one. */
23991 last = first;
23992 first_x = x;
23993 while (last < end
23994 && x < r->x + r->width)
23995 {
23996 x += last->pixel_width;
23997 ++last;
23998 }
23999
24000 /* Repaint. */
24001 if (last > first)
24002 draw_glyphs (w, first_x - start_x, row, area,
24003 first - row->glyphs[area], last - row->glyphs[area],
24004 DRAW_NORMAL_TEXT, 0);
24005 }
24006 }
24007
24008
24009 /* Redraw the parts of the glyph row ROW on window W intersecting
24010 rectangle R. R is in window-relative coordinates. Value is
24011 non-zero if mouse-face was overwritten. */
24012
24013 static int
24014 expose_line (w, row, r)
24015 struct window *w;
24016 struct glyph_row *row;
24017 XRectangle *r;
24018 {
24019 xassert (row->enabled_p);
24020
24021 if (row->mode_line_p || w->pseudo_window_p)
24022 draw_glyphs (w, 0, row, TEXT_AREA,
24023 0, row->used[TEXT_AREA],
24024 DRAW_NORMAL_TEXT, 0);
24025 else
24026 {
24027 if (row->used[LEFT_MARGIN_AREA])
24028 expose_area (w, row, r, LEFT_MARGIN_AREA);
24029 if (row->used[TEXT_AREA])
24030 expose_area (w, row, r, TEXT_AREA);
24031 if (row->used[RIGHT_MARGIN_AREA])
24032 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24033 draw_row_fringe_bitmaps (w, row);
24034 }
24035
24036 return row->mouse_face_p;
24037 }
24038
24039
24040 /* Redraw those parts of glyphs rows during expose event handling that
24041 overlap other rows. Redrawing of an exposed line writes over parts
24042 of lines overlapping that exposed line; this function fixes that.
24043
24044 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24045 row in W's current matrix that is exposed and overlaps other rows.
24046 LAST_OVERLAPPING_ROW is the last such row. */
24047
24048 static void
24049 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24050 struct window *w;
24051 struct glyph_row *first_overlapping_row;
24052 struct glyph_row *last_overlapping_row;
24053 XRectangle *r;
24054 {
24055 struct glyph_row *row;
24056
24057 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24058 if (row->overlapping_p)
24059 {
24060 xassert (row->enabled_p && !row->mode_line_p);
24061
24062 row->clip = r;
24063 if (row->used[LEFT_MARGIN_AREA])
24064 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24065
24066 if (row->used[TEXT_AREA])
24067 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24068
24069 if (row->used[RIGHT_MARGIN_AREA])
24070 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24071 row->clip = NULL;
24072 }
24073 }
24074
24075
24076 /* Return non-zero if W's cursor intersects rectangle R. */
24077
24078 static int
24079 phys_cursor_in_rect_p (w, r)
24080 struct window *w;
24081 XRectangle *r;
24082 {
24083 XRectangle cr, result;
24084 struct glyph *cursor_glyph;
24085 struct glyph_row *row;
24086
24087 if (w->phys_cursor.vpos >= 0
24088 && w->phys_cursor.vpos < w->current_matrix->nrows
24089 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24090 row->enabled_p)
24091 && row->cursor_in_fringe_p)
24092 {
24093 /* Cursor is in the fringe. */
24094 cr.x = window_box_right_offset (w,
24095 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24096 ? RIGHT_MARGIN_AREA
24097 : TEXT_AREA));
24098 cr.y = row->y;
24099 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24100 cr.height = row->height;
24101 return x_intersect_rectangles (&cr, r, &result);
24102 }
24103
24104 cursor_glyph = get_phys_cursor_glyph (w);
24105 if (cursor_glyph)
24106 {
24107 /* r is relative to W's box, but w->phys_cursor.x is relative
24108 to left edge of W's TEXT area. Adjust it. */
24109 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24110 cr.y = w->phys_cursor.y;
24111 cr.width = cursor_glyph->pixel_width;
24112 cr.height = w->phys_cursor_height;
24113 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24114 I assume the effect is the same -- and this is portable. */
24115 return x_intersect_rectangles (&cr, r, &result);
24116 }
24117 /* If we don't understand the format, pretend we're not in the hot-spot. */
24118 return 0;
24119 }
24120
24121
24122 /* EXPORT:
24123 Draw a vertical window border to the right of window W if W doesn't
24124 have vertical scroll bars. */
24125
24126 void
24127 x_draw_vertical_border (w)
24128 struct window *w;
24129 {
24130 struct frame *f = XFRAME (WINDOW_FRAME (w));
24131
24132 /* We could do better, if we knew what type of scroll-bar the adjacent
24133 windows (on either side) have... But we don't :-(
24134 However, I think this works ok. ++KFS 2003-04-25 */
24135
24136 /* Redraw borders between horizontally adjacent windows. Don't
24137 do it for frames with vertical scroll bars because either the
24138 right scroll bar of a window, or the left scroll bar of its
24139 neighbor will suffice as a border. */
24140 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24141 return;
24142
24143 if (!WINDOW_RIGHTMOST_P (w)
24144 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24145 {
24146 int x0, x1, y0, y1;
24147
24148 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24149 y1 -= 1;
24150
24151 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24152 x1 -= 1;
24153
24154 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24155 }
24156 else if (!WINDOW_LEFTMOST_P (w)
24157 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24158 {
24159 int x0, x1, y0, y1;
24160
24161 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24162 y1 -= 1;
24163
24164 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24165 x0 -= 1;
24166
24167 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24168 }
24169 }
24170
24171
24172 /* Redraw the part of window W intersection rectangle FR. Pixel
24173 coordinates in FR are frame-relative. Call this function with
24174 input blocked. Value is non-zero if the exposure overwrites
24175 mouse-face. */
24176
24177 static int
24178 expose_window (w, fr)
24179 struct window *w;
24180 XRectangle *fr;
24181 {
24182 struct frame *f = XFRAME (w->frame);
24183 XRectangle wr, r;
24184 int mouse_face_overwritten_p = 0;
24185
24186 /* If window is not yet fully initialized, do nothing. This can
24187 happen when toolkit scroll bars are used and a window is split.
24188 Reconfiguring the scroll bar will generate an expose for a newly
24189 created window. */
24190 if (w->current_matrix == NULL)
24191 return 0;
24192
24193 /* When we're currently updating the window, display and current
24194 matrix usually don't agree. Arrange for a thorough display
24195 later. */
24196 if (w == updated_window)
24197 {
24198 SET_FRAME_GARBAGED (f);
24199 return 0;
24200 }
24201
24202 /* Frame-relative pixel rectangle of W. */
24203 wr.x = WINDOW_LEFT_EDGE_X (w);
24204 wr.y = WINDOW_TOP_EDGE_Y (w);
24205 wr.width = WINDOW_TOTAL_WIDTH (w);
24206 wr.height = WINDOW_TOTAL_HEIGHT (w);
24207
24208 if (x_intersect_rectangles (fr, &wr, &r))
24209 {
24210 int yb = window_text_bottom_y (w);
24211 struct glyph_row *row;
24212 int cursor_cleared_p;
24213 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24214
24215 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24216 r.x, r.y, r.width, r.height));
24217
24218 /* Convert to window coordinates. */
24219 r.x -= WINDOW_LEFT_EDGE_X (w);
24220 r.y -= WINDOW_TOP_EDGE_Y (w);
24221
24222 /* Turn off the cursor. */
24223 if (!w->pseudo_window_p
24224 && phys_cursor_in_rect_p (w, &r))
24225 {
24226 x_clear_cursor (w);
24227 cursor_cleared_p = 1;
24228 }
24229 else
24230 cursor_cleared_p = 0;
24231
24232 /* Update lines intersecting rectangle R. */
24233 first_overlapping_row = last_overlapping_row = NULL;
24234 for (row = w->current_matrix->rows;
24235 row->enabled_p;
24236 ++row)
24237 {
24238 int y0 = row->y;
24239 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24240
24241 if ((y0 >= r.y && y0 < r.y + r.height)
24242 || (y1 > r.y && y1 < r.y + r.height)
24243 || (r.y >= y0 && r.y < y1)
24244 || (r.y + r.height > y0 && r.y + r.height < y1))
24245 {
24246 /* A header line may be overlapping, but there is no need
24247 to fix overlapping areas for them. KFS 2005-02-12 */
24248 if (row->overlapping_p && !row->mode_line_p)
24249 {
24250 if (first_overlapping_row == NULL)
24251 first_overlapping_row = row;
24252 last_overlapping_row = row;
24253 }
24254
24255 row->clip = fr;
24256 if (expose_line (w, row, &r))
24257 mouse_face_overwritten_p = 1;
24258 row->clip = NULL;
24259 }
24260 else if (row->overlapping_p)
24261 {
24262 /* We must redraw a row overlapping the exposed area. */
24263 if (y0 < r.y
24264 ? y0 + row->phys_height > r.y
24265 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24266 {
24267 if (first_overlapping_row == NULL)
24268 first_overlapping_row = row;
24269 last_overlapping_row = row;
24270 }
24271 }
24272
24273 if (y1 >= yb)
24274 break;
24275 }
24276
24277 /* Display the mode line if there is one. */
24278 if (WINDOW_WANTS_MODELINE_P (w)
24279 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24280 row->enabled_p)
24281 && row->y < r.y + r.height)
24282 {
24283 if (expose_line (w, row, &r))
24284 mouse_face_overwritten_p = 1;
24285 }
24286
24287 if (!w->pseudo_window_p)
24288 {
24289 /* Fix the display of overlapping rows. */
24290 if (first_overlapping_row)
24291 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24292 fr);
24293
24294 /* Draw border between windows. */
24295 x_draw_vertical_border (w);
24296
24297 /* Turn the cursor on again. */
24298 if (cursor_cleared_p)
24299 update_window_cursor (w, 1);
24300 }
24301 }
24302
24303 return mouse_face_overwritten_p;
24304 }
24305
24306
24307
24308 /* Redraw (parts) of all windows in the window tree rooted at W that
24309 intersect R. R contains frame pixel coordinates. Value is
24310 non-zero if the exposure overwrites mouse-face. */
24311
24312 static int
24313 expose_window_tree (w, r)
24314 struct window *w;
24315 XRectangle *r;
24316 {
24317 struct frame *f = XFRAME (w->frame);
24318 int mouse_face_overwritten_p = 0;
24319
24320 while (w && !FRAME_GARBAGED_P (f))
24321 {
24322 if (!NILP (w->hchild))
24323 mouse_face_overwritten_p
24324 |= expose_window_tree (XWINDOW (w->hchild), r);
24325 else if (!NILP (w->vchild))
24326 mouse_face_overwritten_p
24327 |= expose_window_tree (XWINDOW (w->vchild), r);
24328 else
24329 mouse_face_overwritten_p |= expose_window (w, r);
24330
24331 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24332 }
24333
24334 return mouse_face_overwritten_p;
24335 }
24336
24337
24338 /* EXPORT:
24339 Redisplay an exposed area of frame F. X and Y are the upper-left
24340 corner of the exposed rectangle. W and H are width and height of
24341 the exposed area. All are pixel values. W or H zero means redraw
24342 the entire frame. */
24343
24344 void
24345 expose_frame (f, x, y, w, h)
24346 struct frame *f;
24347 int x, y, w, h;
24348 {
24349 XRectangle r;
24350 int mouse_face_overwritten_p = 0;
24351
24352 TRACE ((stderr, "expose_frame "));
24353
24354 /* No need to redraw if frame will be redrawn soon. */
24355 if (FRAME_GARBAGED_P (f))
24356 {
24357 TRACE ((stderr, " garbaged\n"));
24358 return;
24359 }
24360
24361 /* If basic faces haven't been realized yet, there is no point in
24362 trying to redraw anything. This can happen when we get an expose
24363 event while Emacs is starting, e.g. by moving another window. */
24364 if (FRAME_FACE_CACHE (f) == NULL
24365 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24366 {
24367 TRACE ((stderr, " no faces\n"));
24368 return;
24369 }
24370
24371 if (w == 0 || h == 0)
24372 {
24373 r.x = r.y = 0;
24374 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24375 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24376 }
24377 else
24378 {
24379 r.x = x;
24380 r.y = y;
24381 r.width = w;
24382 r.height = h;
24383 }
24384
24385 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24386 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24387
24388 if (WINDOWP (f->tool_bar_window))
24389 mouse_face_overwritten_p
24390 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24391
24392 #ifdef HAVE_X_WINDOWS
24393 #ifndef MSDOS
24394 #ifndef USE_X_TOOLKIT
24395 if (WINDOWP (f->menu_bar_window))
24396 mouse_face_overwritten_p
24397 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24398 #endif /* not USE_X_TOOLKIT */
24399 #endif
24400 #endif
24401
24402 /* Some window managers support a focus-follows-mouse style with
24403 delayed raising of frames. Imagine a partially obscured frame,
24404 and moving the mouse into partially obscured mouse-face on that
24405 frame. The visible part of the mouse-face will be highlighted,
24406 then the WM raises the obscured frame. With at least one WM, KDE
24407 2.1, Emacs is not getting any event for the raising of the frame
24408 (even tried with SubstructureRedirectMask), only Expose events.
24409 These expose events will draw text normally, i.e. not
24410 highlighted. Which means we must redo the highlight here.
24411 Subsume it under ``we love X''. --gerd 2001-08-15 */
24412 /* Included in Windows version because Windows most likely does not
24413 do the right thing if any third party tool offers
24414 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24415 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24416 {
24417 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24418 if (f == dpyinfo->mouse_face_mouse_frame)
24419 {
24420 int x = dpyinfo->mouse_face_mouse_x;
24421 int y = dpyinfo->mouse_face_mouse_y;
24422 clear_mouse_face (dpyinfo);
24423 note_mouse_highlight (f, x, y);
24424 }
24425 }
24426 }
24427
24428
24429 /* EXPORT:
24430 Determine the intersection of two rectangles R1 and R2. Return
24431 the intersection in *RESULT. Value is non-zero if RESULT is not
24432 empty. */
24433
24434 int
24435 x_intersect_rectangles (r1, r2, result)
24436 XRectangle *r1, *r2, *result;
24437 {
24438 XRectangle *left, *right;
24439 XRectangle *upper, *lower;
24440 int intersection_p = 0;
24441
24442 /* Rearrange so that R1 is the left-most rectangle. */
24443 if (r1->x < r2->x)
24444 left = r1, right = r2;
24445 else
24446 left = r2, right = r1;
24447
24448 /* X0 of the intersection is right.x0, if this is inside R1,
24449 otherwise there is no intersection. */
24450 if (right->x <= left->x + left->width)
24451 {
24452 result->x = right->x;
24453
24454 /* The right end of the intersection is the minimum of the
24455 the right ends of left and right. */
24456 result->width = (min (left->x + left->width, right->x + right->width)
24457 - result->x);
24458
24459 /* Same game for Y. */
24460 if (r1->y < r2->y)
24461 upper = r1, lower = r2;
24462 else
24463 upper = r2, lower = r1;
24464
24465 /* The upper end of the intersection is lower.y0, if this is inside
24466 of upper. Otherwise, there is no intersection. */
24467 if (lower->y <= upper->y + upper->height)
24468 {
24469 result->y = lower->y;
24470
24471 /* The lower end of the intersection is the minimum of the lower
24472 ends of upper and lower. */
24473 result->height = (min (lower->y + lower->height,
24474 upper->y + upper->height)
24475 - result->y);
24476 intersection_p = 1;
24477 }
24478 }
24479
24480 return intersection_p;
24481 }
24482
24483 #endif /* HAVE_WINDOW_SYSTEM */
24484
24485 \f
24486 /***********************************************************************
24487 Initialization
24488 ***********************************************************************/
24489
24490 void
24491 syms_of_xdisp ()
24492 {
24493 Vwith_echo_area_save_vector = Qnil;
24494 staticpro (&Vwith_echo_area_save_vector);
24495
24496 Vmessage_stack = Qnil;
24497 staticpro (&Vmessage_stack);
24498
24499 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
24500 staticpro (&Qinhibit_redisplay);
24501
24502 message_dolog_marker1 = Fmake_marker ();
24503 staticpro (&message_dolog_marker1);
24504 message_dolog_marker2 = Fmake_marker ();
24505 staticpro (&message_dolog_marker2);
24506 message_dolog_marker3 = Fmake_marker ();
24507 staticpro (&message_dolog_marker3);
24508
24509 #if GLYPH_DEBUG
24510 defsubr (&Sdump_frame_glyph_matrix);
24511 defsubr (&Sdump_glyph_matrix);
24512 defsubr (&Sdump_glyph_row);
24513 defsubr (&Sdump_tool_bar_row);
24514 defsubr (&Strace_redisplay);
24515 defsubr (&Strace_to_stderr);
24516 #endif
24517 #ifdef HAVE_WINDOW_SYSTEM
24518 defsubr (&Stool_bar_lines_needed);
24519 defsubr (&Slookup_image_map);
24520 #endif
24521 defsubr (&Sformat_mode_line);
24522 defsubr (&Sinvisible_p);
24523
24524 staticpro (&Qmenu_bar_update_hook);
24525 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
24526
24527 staticpro (&Qoverriding_terminal_local_map);
24528 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
24529
24530 staticpro (&Qoverriding_local_map);
24531 Qoverriding_local_map = intern_c_string ("overriding-local-map");
24532
24533 staticpro (&Qwindow_scroll_functions);
24534 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
24535
24536 staticpro (&Qwindow_text_change_functions);
24537 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
24538
24539 staticpro (&Qredisplay_end_trigger_functions);
24540 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
24541
24542 staticpro (&Qinhibit_point_motion_hooks);
24543 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
24544
24545 Qeval = intern_c_string ("eval");
24546 staticpro (&Qeval);
24547
24548 QCdata = intern_c_string (":data");
24549 staticpro (&QCdata);
24550 Qdisplay = intern_c_string ("display");
24551 staticpro (&Qdisplay);
24552 Qspace_width = intern_c_string ("space-width");
24553 staticpro (&Qspace_width);
24554 Qraise = intern_c_string ("raise");
24555 staticpro (&Qraise);
24556 Qslice = intern_c_string ("slice");
24557 staticpro (&Qslice);
24558 Qspace = intern_c_string ("space");
24559 staticpro (&Qspace);
24560 Qmargin = intern_c_string ("margin");
24561 staticpro (&Qmargin);
24562 Qpointer = intern_c_string ("pointer");
24563 staticpro (&Qpointer);
24564 Qleft_margin = intern_c_string ("left-margin");
24565 staticpro (&Qleft_margin);
24566 Qright_margin = intern_c_string ("right-margin");
24567 staticpro (&Qright_margin);
24568 Qcenter = intern_c_string ("center");
24569 staticpro (&Qcenter);
24570 Qline_height = intern_c_string ("line-height");
24571 staticpro (&Qline_height);
24572 QCalign_to = intern_c_string (":align-to");
24573 staticpro (&QCalign_to);
24574 QCrelative_width = intern_c_string (":relative-width");
24575 staticpro (&QCrelative_width);
24576 QCrelative_height = intern_c_string (":relative-height");
24577 staticpro (&QCrelative_height);
24578 QCeval = intern_c_string (":eval");
24579 staticpro (&QCeval);
24580 QCpropertize = intern_c_string (":propertize");
24581 staticpro (&QCpropertize);
24582 QCfile = intern_c_string (":file");
24583 staticpro (&QCfile);
24584 Qfontified = intern_c_string ("fontified");
24585 staticpro (&Qfontified);
24586 Qfontification_functions = intern_c_string ("fontification-functions");
24587 staticpro (&Qfontification_functions);
24588 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
24589 staticpro (&Qtrailing_whitespace);
24590 Qescape_glyph = intern_c_string ("escape-glyph");
24591 staticpro (&Qescape_glyph);
24592 Qnobreak_space = intern_c_string ("nobreak-space");
24593 staticpro (&Qnobreak_space);
24594 Qimage = intern_c_string ("image");
24595 staticpro (&Qimage);
24596 QCmap = intern_c_string (":map");
24597 staticpro (&QCmap);
24598 QCpointer = intern_c_string (":pointer");
24599 staticpro (&QCpointer);
24600 Qrect = intern_c_string ("rect");
24601 staticpro (&Qrect);
24602 Qcircle = intern_c_string ("circle");
24603 staticpro (&Qcircle);
24604 Qpoly = intern_c_string ("poly");
24605 staticpro (&Qpoly);
24606 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
24607 staticpro (&Qmessage_truncate_lines);
24608 Qgrow_only = intern_c_string ("grow-only");
24609 staticpro (&Qgrow_only);
24610 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
24611 staticpro (&Qinhibit_menubar_update);
24612 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
24613 staticpro (&Qinhibit_eval_during_redisplay);
24614 Qposition = intern_c_string ("position");
24615 staticpro (&Qposition);
24616 Qbuffer_position = intern_c_string ("buffer-position");
24617 staticpro (&Qbuffer_position);
24618 Qobject = intern_c_string ("object");
24619 staticpro (&Qobject);
24620 Qbar = intern_c_string ("bar");
24621 staticpro (&Qbar);
24622 Qhbar = intern_c_string ("hbar");
24623 staticpro (&Qhbar);
24624 Qbox = intern_c_string ("box");
24625 staticpro (&Qbox);
24626 Qhollow = intern_c_string ("hollow");
24627 staticpro (&Qhollow);
24628 Qhand = intern_c_string ("hand");
24629 staticpro (&Qhand);
24630 Qarrow = intern_c_string ("arrow");
24631 staticpro (&Qarrow);
24632 Qtext = intern_c_string ("text");
24633 staticpro (&Qtext);
24634 Qrisky_local_variable = intern_c_string ("risky-local-variable");
24635 staticpro (&Qrisky_local_variable);
24636 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
24637 staticpro (&Qinhibit_free_realized_faces);
24638
24639 list_of_error = Fcons (Fcons (intern_c_string ("error"),
24640 Fcons (intern_c_string ("void-variable"), Qnil)),
24641 Qnil);
24642 staticpro (&list_of_error);
24643
24644 Qlast_arrow_position = intern_c_string ("last-arrow-position");
24645 staticpro (&Qlast_arrow_position);
24646 Qlast_arrow_string = intern_c_string ("last-arrow-string");
24647 staticpro (&Qlast_arrow_string);
24648
24649 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
24650 staticpro (&Qoverlay_arrow_string);
24651 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
24652 staticpro (&Qoverlay_arrow_bitmap);
24653
24654 echo_buffer[0] = echo_buffer[1] = Qnil;
24655 staticpro (&echo_buffer[0]);
24656 staticpro (&echo_buffer[1]);
24657
24658 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24659 staticpro (&echo_area_buffer[0]);
24660 staticpro (&echo_area_buffer[1]);
24661
24662 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
24663 staticpro (&Vmessages_buffer_name);
24664
24665 mode_line_proptrans_alist = Qnil;
24666 staticpro (&mode_line_proptrans_alist);
24667 mode_line_string_list = Qnil;
24668 staticpro (&mode_line_string_list);
24669 mode_line_string_face = Qnil;
24670 staticpro (&mode_line_string_face);
24671 mode_line_string_face_prop = Qnil;
24672 staticpro (&mode_line_string_face_prop);
24673 Vmode_line_unwind_vector = Qnil;
24674 staticpro (&Vmode_line_unwind_vector);
24675
24676 help_echo_string = Qnil;
24677 staticpro (&help_echo_string);
24678 help_echo_object = Qnil;
24679 staticpro (&help_echo_object);
24680 help_echo_window = Qnil;
24681 staticpro (&help_echo_window);
24682 previous_help_echo_string = Qnil;
24683 staticpro (&previous_help_echo_string);
24684 help_echo_pos = -1;
24685
24686 #ifdef HAVE_WINDOW_SYSTEM
24687 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24688 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24689 For example, if a block cursor is over a tab, it will be drawn as
24690 wide as that tab on the display. */);
24691 x_stretch_cursor_p = 0;
24692 #endif
24693
24694 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24695 doc: /* *Non-nil means highlight trailing whitespace.
24696 The face used for trailing whitespace is `trailing-whitespace'. */);
24697 Vshow_trailing_whitespace = Qnil;
24698
24699 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24700 doc: /* *Control highlighting of nobreak space and soft hyphen.
24701 A value of t means highlight the character itself (for nobreak space,
24702 use face `nobreak-space').
24703 A value of nil means no highlighting.
24704 Other values mean display the escape glyph followed by an ordinary
24705 space or ordinary hyphen. */);
24706 Vnobreak_char_display = Qt;
24707
24708 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24709 doc: /* *The pointer shape to show in void text areas.
24710 A value of nil means to show the text pointer. Other options are `arrow',
24711 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24712 Vvoid_text_area_pointer = Qarrow;
24713
24714 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24715 doc: /* Non-nil means don't actually do any redisplay.
24716 This is used for internal purposes. */);
24717 Vinhibit_redisplay = Qnil;
24718
24719 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24720 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24721 Vglobal_mode_string = Qnil;
24722
24723 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24724 doc: /* Marker for where to display an arrow on top of the buffer text.
24725 This must be the beginning of a line in order to work.
24726 See also `overlay-arrow-string'. */);
24727 Voverlay_arrow_position = Qnil;
24728
24729 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24730 doc: /* String to display as an arrow in non-window frames.
24731 See also `overlay-arrow-position'. */);
24732 Voverlay_arrow_string = make_pure_c_string ("=>");
24733
24734 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24735 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24736 The symbols on this list are examined during redisplay to determine
24737 where to display overlay arrows. */);
24738 Voverlay_arrow_variable_list
24739 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
24740
24741 DEFVAR_INT ("scroll-step", &scroll_step,
24742 doc: /* *The number of lines to try scrolling a window by when point moves out.
24743 If that fails to bring point back on frame, point is centered instead.
24744 If this is zero, point is always centered after it moves off frame.
24745 If you want scrolling to always be a line at a time, you should set
24746 `scroll-conservatively' to a large value rather than set this to 1. */);
24747
24748 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24749 doc: /* *Scroll up to this many lines, to bring point back on screen.
24750 If point moves off-screen, redisplay will scroll by up to
24751 `scroll-conservatively' lines in order to bring point just barely
24752 onto the screen again. If that cannot be done, then redisplay
24753 recenters point as usual.
24754
24755 A value of zero means always recenter point if it moves off screen. */);
24756 scroll_conservatively = 0;
24757
24758 DEFVAR_INT ("scroll-margin", &scroll_margin,
24759 doc: /* *Number of lines of margin at the top and bottom of a window.
24760 Recenter the window whenever point gets within this many lines
24761 of the top or bottom of the window. */);
24762 scroll_margin = 0;
24763
24764 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24765 doc: /* Pixels per inch value for non-window system displays.
24766 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24767 Vdisplay_pixels_per_inch = make_float (72.0);
24768
24769 #if GLYPH_DEBUG
24770 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24771 #endif
24772
24773 DEFVAR_LISP ("truncate-partial-width-windows",
24774 &Vtruncate_partial_width_windows,
24775 doc: /* Non-nil means truncate lines in windows narrower than the frame.
24776 For an integer value, truncate lines in each window narrower than the
24777 full frame width, provided the window width is less than that integer;
24778 otherwise, respect the value of `truncate-lines'.
24779
24780 For any other non-nil value, truncate lines in all windows that do
24781 not span the full frame width.
24782
24783 A value of nil means to respect the value of `truncate-lines'.
24784
24785 If `word-wrap' is enabled, you might want to reduce this. */);
24786 Vtruncate_partial_width_windows = make_number (50);
24787
24788 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24789 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24790 Any other value means to use the appropriate face, `mode-line',
24791 `header-line', or `menu' respectively. */);
24792 mode_line_inverse_video = 1;
24793
24794 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24795 doc: /* *Maximum buffer size for which line number should be displayed.
24796 If the buffer is bigger than this, the line number does not appear
24797 in the mode line. A value of nil means no limit. */);
24798 Vline_number_display_limit = Qnil;
24799
24800 DEFVAR_INT ("line-number-display-limit-width",
24801 &line_number_display_limit_width,
24802 doc: /* *Maximum line width (in characters) for line number display.
24803 If the average length of the lines near point is bigger than this, then the
24804 line number may be omitted from the mode line. */);
24805 line_number_display_limit_width = 200;
24806
24807 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24808 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24809 highlight_nonselected_windows = 0;
24810
24811 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24812 doc: /* Non-nil if more than one frame is visible on this display.
24813 Minibuffer-only frames don't count, but iconified frames do.
24814 This variable is not guaranteed to be accurate except while processing
24815 `frame-title-format' and `icon-title-format'. */);
24816
24817 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24818 doc: /* Template for displaying the title bar of visible frames.
24819 \(Assuming the window manager supports this feature.)
24820
24821 This variable has the same structure as `mode-line-format', except that
24822 the %c and %l constructs are ignored. It is used only on frames for
24823 which no explicit name has been set \(see `modify-frame-parameters'). */);
24824
24825 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24826 doc: /* Template for displaying the title bar of an iconified frame.
24827 \(Assuming the window manager supports this feature.)
24828 This variable has the same structure as `mode-line-format' (which see),
24829 and is used only on frames for which no explicit name has been set
24830 \(see `modify-frame-parameters'). */);
24831 Vicon_title_format
24832 = Vframe_title_format
24833 = pure_cons (intern_c_string ("multiple-frames"),
24834 pure_cons (make_pure_c_string ("%b"),
24835 pure_cons (pure_cons (empty_unibyte_string,
24836 pure_cons (intern_c_string ("invocation-name"),
24837 pure_cons (make_pure_c_string ("@"),
24838 pure_cons (intern_c_string ("system-name"),
24839 Qnil)))),
24840 Qnil)));
24841
24842 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24843 doc: /* Maximum number of lines to keep in the message log buffer.
24844 If nil, disable message logging. If t, log messages but don't truncate
24845 the buffer when it becomes large. */);
24846 Vmessage_log_max = make_number (100);
24847
24848 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24849 doc: /* Functions called before redisplay, if window sizes have changed.
24850 The value should be a list of functions that take one argument.
24851 Just before redisplay, for each frame, if any of its windows have changed
24852 size since the last redisplay, or have been split or deleted,
24853 all the functions in the list are called, with the frame as argument. */);
24854 Vwindow_size_change_functions = Qnil;
24855
24856 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24857 doc: /* List of functions to call before redisplaying a window with scrolling.
24858 Each function is called with two arguments, the window and its new
24859 display-start position. Note that these functions are also called by
24860 `set-window-buffer'. Also note that the value of `window-end' is not
24861 valid when these functions are called. */);
24862 Vwindow_scroll_functions = Qnil;
24863
24864 DEFVAR_LISP ("window-text-change-functions",
24865 &Vwindow_text_change_functions,
24866 doc: /* Functions to call in redisplay when text in the window might change. */);
24867 Vwindow_text_change_functions = Qnil;
24868
24869 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24870 doc: /* Functions called when redisplay of a window reaches the end trigger.
24871 Each function is called with two arguments, the window and the end trigger value.
24872 See `set-window-redisplay-end-trigger'. */);
24873 Vredisplay_end_trigger_functions = Qnil;
24874
24875 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24876 doc: /* *Non-nil means autoselect window with mouse pointer.
24877 If nil, do not autoselect windows.
24878 A positive number means delay autoselection by that many seconds: a
24879 window is autoselected only after the mouse has remained in that
24880 window for the duration of the delay.
24881 A negative number has a similar effect, but causes windows to be
24882 autoselected only after the mouse has stopped moving. \(Because of
24883 the way Emacs compares mouse events, you will occasionally wait twice
24884 that time before the window gets selected.\)
24885 Any other value means to autoselect window instantaneously when the
24886 mouse pointer enters it.
24887
24888 Autoselection selects the minibuffer only if it is active, and never
24889 unselects the minibuffer if it is active.
24890
24891 When customizing this variable make sure that the actual value of
24892 `focus-follows-mouse' matches the behavior of your window manager. */);
24893 Vmouse_autoselect_window = Qnil;
24894
24895 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24896 doc: /* *Non-nil means automatically resize tool-bars.
24897 This dynamically changes the tool-bar's height to the minimum height
24898 that is needed to make all tool-bar items visible.
24899 If value is `grow-only', the tool-bar's height is only increased
24900 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24901 Vauto_resize_tool_bars = Qt;
24902
24903 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24904 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24905 auto_raise_tool_bar_buttons_p = 1;
24906
24907 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24908 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24909 make_cursor_line_fully_visible_p = 1;
24910
24911 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24912 doc: /* *Border below tool-bar in pixels.
24913 If an integer, use it as the height of the border.
24914 If it is one of `internal-border-width' or `border-width', use the
24915 value of the corresponding frame parameter.
24916 Otherwise, no border is added below the tool-bar. */);
24917 Vtool_bar_border = Qinternal_border_width;
24918
24919 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24920 doc: /* *Margin around tool-bar buttons in pixels.
24921 If an integer, use that for both horizontal and vertical margins.
24922 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24923 HORZ specifying the horizontal margin, and VERT specifying the
24924 vertical margin. */);
24925 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24926
24927 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24928 doc: /* *Relief thickness of tool-bar buttons. */);
24929 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24930
24931 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24932 doc: /* List of functions to call to fontify regions of text.
24933 Each function is called with one argument POS. Functions must
24934 fontify a region starting at POS in the current buffer, and give
24935 fontified regions the property `fontified'. */);
24936 Vfontification_functions = Qnil;
24937 Fmake_variable_buffer_local (Qfontification_functions);
24938
24939 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24940 &unibyte_display_via_language_environment,
24941 doc: /* *Non-nil means display unibyte text according to language environment.
24942 Specifically, this means that raw bytes in the range 160-255 decimal
24943 are displayed by converting them to the equivalent multibyte characters
24944 according to the current language environment. As a result, they are
24945 displayed according to the current fontset.
24946
24947 Note that this variable affects only how these bytes are displayed,
24948 but does not change the fact they are interpreted as raw bytes. */);
24949 unibyte_display_via_language_environment = 0;
24950
24951 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24952 doc: /* *Maximum height for resizing mini-windows.
24953 If a float, it specifies a fraction of the mini-window frame's height.
24954 If an integer, it specifies a number of lines. */);
24955 Vmax_mini_window_height = make_float (0.25);
24956
24957 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24958 doc: /* *How to resize mini-windows.
24959 A value of nil means don't automatically resize mini-windows.
24960 A value of t means resize them to fit the text displayed in them.
24961 A value of `grow-only', the default, means let mini-windows grow
24962 only, until their display becomes empty, at which point the windows
24963 go back to their normal size. */);
24964 Vresize_mini_windows = Qgrow_only;
24965
24966 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24967 doc: /* Alist specifying how to blink the cursor off.
24968 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24969 `cursor-type' frame-parameter or variable equals ON-STATE,
24970 comparing using `equal', Emacs uses OFF-STATE to specify
24971 how to blink it off. ON-STATE and OFF-STATE are values for
24972 the `cursor-type' frame parameter.
24973
24974 If a frame's ON-STATE has no entry in this list,
24975 the frame's other specifications determine how to blink the cursor off. */);
24976 Vblink_cursor_alist = Qnil;
24977
24978 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24979 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24980 automatic_hscrolling_p = 1;
24981 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
24982 staticpro (&Qauto_hscroll_mode);
24983
24984 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24985 doc: /* *How many columns away from the window edge point is allowed to get
24986 before automatic hscrolling will horizontally scroll the window. */);
24987 hscroll_margin = 5;
24988
24989 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24990 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24991 When point is less than `hscroll-margin' columns from the window
24992 edge, automatic hscrolling will scroll the window by the amount of columns
24993 determined by this variable. If its value is a positive integer, scroll that
24994 many columns. If it's a positive floating-point number, it specifies the
24995 fraction of the window's width to scroll. If it's nil or zero, point will be
24996 centered horizontally after the scroll. Any other value, including negative
24997 numbers, are treated as if the value were zero.
24998
24999 Automatic hscrolling always moves point outside the scroll margin, so if
25000 point was more than scroll step columns inside the margin, the window will
25001 scroll more than the value given by the scroll step.
25002
25003 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25004 and `scroll-right' overrides this variable's effect. */);
25005 Vhscroll_step = make_number (0);
25006
25007 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25008 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25009 Bind this around calls to `message' to let it take effect. */);
25010 message_truncate_lines = 0;
25011
25012 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25013 doc: /* Normal hook run to update the menu bar definitions.
25014 Redisplay runs this hook before it redisplays the menu bar.
25015 This is used to update submenus such as Buffers,
25016 whose contents depend on various data. */);
25017 Vmenu_bar_update_hook = Qnil;
25018
25019 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25020 doc: /* Frame for which we are updating a menu.
25021 The enable predicate for a menu binding should check this variable. */);
25022 Vmenu_updating_frame = Qnil;
25023
25024 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25025 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25026 inhibit_menubar_update = 0;
25027
25028 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25029 doc: /* Prefix prepended to all continuation lines at display time.
25030 The value may be a string, an image, or a stretch-glyph; it is
25031 interpreted in the same way as the value of a `display' text property.
25032
25033 This variable is overridden by any `wrap-prefix' text or overlay
25034 property.
25035
25036 To add a prefix to non-continuation lines, use `line-prefix'. */);
25037 Vwrap_prefix = Qnil;
25038 staticpro (&Qwrap_prefix);
25039 Qwrap_prefix = intern_c_string ("wrap-prefix");
25040 Fmake_variable_buffer_local (Qwrap_prefix);
25041
25042 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25043 doc: /* Prefix prepended to all non-continuation lines at display time.
25044 The value may be a string, an image, or a stretch-glyph; it is
25045 interpreted in the same way as the value of a `display' text property.
25046
25047 This variable is overridden by any `line-prefix' text or overlay
25048 property.
25049
25050 To add a prefix to continuation lines, use `wrap-prefix'. */);
25051 Vline_prefix = Qnil;
25052 staticpro (&Qline_prefix);
25053 Qline_prefix = intern_c_string ("line-prefix");
25054 Fmake_variable_buffer_local (Qline_prefix);
25055
25056 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25057 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25058 inhibit_eval_during_redisplay = 0;
25059
25060 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25061 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25062 inhibit_free_realized_faces = 0;
25063
25064 #if GLYPH_DEBUG
25065 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25066 doc: /* Inhibit try_window_id display optimization. */);
25067 inhibit_try_window_id = 0;
25068
25069 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25070 doc: /* Inhibit try_window_reusing display optimization. */);
25071 inhibit_try_window_reusing = 0;
25072
25073 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25074 doc: /* Inhibit try_cursor_movement display optimization. */);
25075 inhibit_try_cursor_movement = 0;
25076 #endif /* GLYPH_DEBUG */
25077
25078 DEFVAR_INT ("overline-margin", &overline_margin,
25079 doc: /* *Space between overline and text, in pixels.
25080 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25081 margin to the caracter height. */);
25082 overline_margin = 2;
25083
25084 DEFVAR_INT ("underline-minimum-offset",
25085 &underline_minimum_offset,
25086 doc: /* Minimum distance between baseline and underline.
25087 This can improve legibility of underlined text at small font sizes,
25088 particularly when using variable `x-use-underline-position-properties'
25089 with fonts that specify an UNDERLINE_POSITION relatively close to the
25090 baseline. The default value is 1. */);
25091 underline_minimum_offset = 1;
25092
25093 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
25094 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
25095 display_hourglass_p = 1;
25096
25097 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
25098 doc: /* *Seconds to wait before displaying an hourglass pointer.
25099 Value must be an integer or float. */);
25100 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
25101
25102 hourglass_atimer = NULL;
25103 hourglass_shown_p = 0;
25104 }
25105
25106
25107 /* Initialize this module when Emacs starts. */
25108
25109 void
25110 init_xdisp ()
25111 {
25112 Lisp_Object root_window;
25113 struct window *mini_w;
25114
25115 current_header_line_height = current_mode_line_height = -1;
25116
25117 CHARPOS (this_line_start_pos) = 0;
25118
25119 mini_w = XWINDOW (minibuf_window);
25120 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25121
25122 if (!noninteractive)
25123 {
25124 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25125 int i;
25126
25127 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25128 set_window_height (root_window,
25129 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25130 0);
25131 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25132 set_window_height (minibuf_window, 1, 0);
25133
25134 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25135 mini_w->total_cols = make_number (FRAME_COLS (f));
25136
25137 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25138 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25139 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25140
25141 /* The default ellipsis glyphs `...'. */
25142 for (i = 0; i < 3; ++i)
25143 default_invis_vector[i] = make_number ('.');
25144 }
25145
25146 {
25147 /* Allocate the buffer for frame titles.
25148 Also used for `format-mode-line'. */
25149 int size = 100;
25150 mode_line_noprop_buf = (char *) xmalloc (size);
25151 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25152 mode_line_noprop_ptr = mode_line_noprop_buf;
25153 mode_line_target = MODE_LINE_DISPLAY;
25154 }
25155
25156 help_echo_showing_p = 0;
25157 }
25158
25159 /* Since w32 does not support atimers, it defines its own implementation of
25160 the following three functions in w32fns.c. */
25161 #ifndef WINDOWSNT
25162
25163 /* Platform-independent portion of hourglass implementation. */
25164
25165 /* Return non-zero if houglass timer has been started or hourglass is shown. */
25166 int
25167 hourglass_started ()
25168 {
25169 return hourglass_shown_p || hourglass_atimer != NULL;
25170 }
25171
25172 /* Cancel a currently active hourglass timer, and start a new one. */
25173 void
25174 start_hourglass ()
25175 {
25176 #if defined (HAVE_WINDOW_SYSTEM)
25177 EMACS_TIME delay;
25178 int secs, usecs = 0;
25179
25180 cancel_hourglass ();
25181
25182 if (INTEGERP (Vhourglass_delay)
25183 && XINT (Vhourglass_delay) > 0)
25184 secs = XFASTINT (Vhourglass_delay);
25185 else if (FLOATP (Vhourglass_delay)
25186 && XFLOAT_DATA (Vhourglass_delay) > 0)
25187 {
25188 Lisp_Object tem;
25189 tem = Ftruncate (Vhourglass_delay, Qnil);
25190 secs = XFASTINT (tem);
25191 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
25192 }
25193 else
25194 secs = DEFAULT_HOURGLASS_DELAY;
25195
25196 EMACS_SET_SECS_USECS (delay, secs, usecs);
25197 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
25198 show_hourglass, NULL);
25199 #endif
25200 }
25201
25202
25203 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
25204 shown. */
25205 void
25206 cancel_hourglass ()
25207 {
25208 #if defined (HAVE_WINDOW_SYSTEM)
25209 if (hourglass_atimer)
25210 {
25211 cancel_atimer (hourglass_atimer);
25212 hourglass_atimer = NULL;
25213 }
25214
25215 if (hourglass_shown_p)
25216 hide_hourglass ();
25217 #endif
25218 }
25219 #endif /* ! WINDOWSNT */
25220
25221 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25222 (do not change this comment) */