]> code.delx.au - gnu-emacs/blob - src/xdisp.c
* unexec.c:
[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 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code. (Or,
35 let's say almost---see the description of direct update
36 operations, below.)
37
38 The following diagram shows how redisplay code is invoked. As you
39 can see, Lisp calls redisplay and vice versa. Under window systems
40 like X, some portions of the redisplay code are also called
41 asynchronously during mouse movement or expose events. It is very
42 important that these code parts do NOT use the C library (malloc,
43 free) because many C libraries under Unix are not reentrant. They
44 may also NOT call functions of the Lisp interpreter which could
45 change the interpreter's state. If you don't follow these rules,
46 you will encounter bugs which are very hard to explain.
47
48 (Direct functions, see below)
49 direct_output_for_insert,
50 direct_forward_char (dispnew.c)
51 +---------------------------------+
52 | |
53 | V
54 +--------------+ redisplay +----------------+
55 | Lisp machine |---------------->| Redisplay code |<--+
56 +--------------+ (xdisp.c) +----------------+ |
57 ^ | |
58 +----------------------------------+ |
59 Don't use this path when called |
60 asynchronously! |
61 |
62 expose_window (asynchronous) |
63 |
64 X expose events -----+
65
66 What does redisplay do? Obviously, it has to figure out somehow what
67 has been changed since the last time the display has been updated,
68 and to make these changes visible. Preferably it would do that in
69 a moderately intelligent way, i.e. fast.
70
71 Changes in buffer text can be deduced from window and buffer
72 structures, and from some global variables like `beg_unchanged' and
73 `end_unchanged'. The contents of the display are additionally
74 recorded in a `glyph matrix', a two-dimensional matrix of glyph
75 structures. Each row in such a matrix corresponds to a line on the
76 display, and each glyph in a row corresponds to a column displaying
77 a character, an image, or what else. This matrix is called the
78 `current glyph matrix' or `current matrix' in redisplay
79 terminology.
80
81 For buffer parts that have been changed since the last update, a
82 second glyph matrix is constructed, the so called `desired glyph
83 matrix' or short `desired matrix'. Current and desired matrix are
84 then compared to find a cheap way to update the display, e.g. by
85 reusing part of the display by scrolling lines.
86
87
88 Direct operations.
89
90 You will find a lot of redisplay optimizations when you start
91 looking at the innards of redisplay. The overall goal of all these
92 optimizations is to make redisplay fast because it is done
93 frequently.
94
95 Two optimizations are not found in xdisp.c. These are the direct
96 operations mentioned above. As the name suggests they follow a
97 different principle than the rest of redisplay. Instead of
98 building a desired matrix and then comparing it with the current
99 display, they perform their actions directly on the display and on
100 the current matrix.
101
102 One direct operation updates the display after one character has
103 been entered. The other one moves the cursor by one position
104 forward or backward. You find these functions under the names
105 `direct_output_for_insert' and `direct_output_forward_char' in
106 dispnew.c.
107
108
109 Desired matrices.
110
111 Desired matrices are always built per Emacs window. The function
112 `display_line' is the central function to look at if you are
113 interested. It constructs one row in a desired matrix given an
114 iterator structure containing both a buffer position and a
115 description of the environment in which the text is to be
116 displayed. But this is too early, read on.
117
118 Characters and pixmaps displayed for a range of buffer text depend
119 on various settings of buffers and windows, on overlays and text
120 properties, on display tables, on selective display. The good news
121 is that all this hairy stuff is hidden behind a small set of
122 interface functions taking an iterator structure (struct it)
123 argument.
124
125 Iteration over things to be displayed is then simple. It is
126 started by initializing an iterator with a call to init_iterator.
127 Calls to get_next_display_element fill the iterator structure with
128 relevant information about the next thing to display. Calls to
129 set_iterator_to_next move the iterator to the next thing.
130
131 Besides this, an iterator also contains information about the
132 display environment in which glyphs for display elements are to be
133 produced. It has fields for the width and height of the display,
134 the information whether long lines are truncated or continued, a
135 current X and Y position, and lots of other stuff you can better
136 see in dispextern.h.
137
138 Glyphs in a desired matrix are normally constructed in a loop
139 calling get_next_display_element and then produce_glyphs. The call
140 to produce_glyphs will fill the iterator structure with pixel
141 information about the element being displayed and at the same time
142 produce glyphs for it. If the display element fits on the line
143 being displayed, set_iterator_to_next is called next, otherwise the
144 glyphs produced are discarded.
145
146
147 Frame matrices.
148
149 That just couldn't be all, could it? What about terminal types not
150 supporting operations on sub-windows of the screen? To update the
151 display on such a terminal, window-based glyph matrices are not
152 well suited. To be able to reuse part of the display (scrolling
153 lines up and down), we must instead have a view of the whole
154 screen. This is what `frame matrices' are for. They are a trick.
155
156 Frames on terminals like above have a glyph pool. Windows on such
157 a frame sub-allocate their glyph memory from their frame's glyph
158 pool. The frame itself is given its own glyph matrices. By
159 coincidence---or maybe something else---rows in window glyph
160 matrices are slices of corresponding rows in frame matrices. Thus
161 writing to window matrices implicitly updates a frame matrix which
162 provides us with the view of the whole screen that we originally
163 wanted to have without having to move many bytes around. To be
164 honest, there is a little bit more done, but not much more. If you
165 plan to extend that code, take a look at dispnew.c. The function
166 build_frame_matrix is a good starting point. */
167
168 #include <config.h>
169 #include <stdio.h>
170
171 #include "lisp.h"
172 #include "keyboard.h"
173 #include "frame.h"
174 #include "window.h"
175 #include "termchar.h"
176 #include "dispextern.h"
177 #include "buffer.h"
178 #include "character.h"
179 #include "charset.h"
180 #include "indent.h"
181 #include "commands.h"
182 #include "keymap.h"
183 #include "macros.h"
184 #include "disptab.h"
185 #include "termhooks.h"
186 #include "intervals.h"
187 #include "coding.h"
188 #include "process.h"
189 #include "region-cache.h"
190 #include "fontset.h"
191 #include "blockinput.h"
192
193 #ifdef HAVE_X_WINDOWS
194 #include "xterm.h"
195 #endif
196 #ifdef WINDOWSNT
197 #include "w32term.h"
198 #endif
199 #ifdef MAC_OS
200 #include "macterm.h"
201 #endif
202
203 #include "font.h"
204
205 #ifndef FRAME_X_OUTPUT
206 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
207 #endif
208
209 #define INFINITY 10000000
210
211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
212 || defined (USE_GTK)
213 extern void set_frame_menubar P_ ((struct frame *f, int, int));
214 extern int pending_menu_activation;
215 #endif
216
217 extern int interrupt_input;
218 extern int command_loop_level;
219
220 extern Lisp_Object do_mouse_tracking;
221
222 extern int minibuffer_auto_raise;
223 extern Lisp_Object Vminibuffer_list;
224
225 extern Lisp_Object Qface;
226 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
227
228 extern Lisp_Object Voverriding_local_map;
229 extern Lisp_Object Voverriding_local_map_menu_flag;
230 extern Lisp_Object Qmenu_item;
231 extern Lisp_Object Qwhen;
232 extern Lisp_Object Qhelp_echo;
233
234 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
235 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
236 Lisp_Object Qwindow_text_change_functions, Vwindow_text_change_functions;
237 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
238 Lisp_Object Qinhibit_point_motion_hooks;
239 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
240 Lisp_Object Qfontified;
241 Lisp_Object Qgrow_only;
242 Lisp_Object Qinhibit_eval_during_redisplay;
243 Lisp_Object Qbuffer_position, Qposition, Qobject;
244
245 /* Cursor shapes */
246 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
247
248 /* Pointer shapes */
249 Lisp_Object Qarrow, Qhand, Qtext;
250
251 Lisp_Object Qrisky_local_variable;
252
253 /* Holds the list (error). */
254 Lisp_Object list_of_error;
255
256 /* Functions called to fontify regions of text. */
257
258 Lisp_Object Vfontification_functions;
259 Lisp_Object Qfontification_functions;
260
261 /* Non-nil means automatically select any window when the mouse
262 cursor moves into it. */
263 Lisp_Object Vmouse_autoselect_window;
264
265 Lisp_Object Vwrap_prefix, Qwrap_prefix;
266 Lisp_Object Vline_prefix, Qline_prefix;
267
268 /* Non-zero means draw tool bar buttons raised when the mouse moves
269 over them. */
270
271 int auto_raise_tool_bar_buttons_p;
272
273 /* Non-zero means to reposition window if cursor line is only partially visible. */
274
275 int make_cursor_line_fully_visible_p;
276
277 /* Margin below tool bar in pixels. 0 or nil means no margin.
278 If value is `internal-border-width' or `border-width',
279 the corresponding frame parameter is used. */
280
281 Lisp_Object Vtool_bar_border;
282
283 /* Margin around tool bar buttons in pixels. */
284
285 Lisp_Object Vtool_bar_button_margin;
286
287 /* Thickness of shadow to draw around tool bar buttons. */
288
289 EMACS_INT tool_bar_button_relief;
290
291 /* Non-nil means automatically resize tool-bars so that all tool-bar
292 items are visible, and no blank lines remain.
293
294 If value is `grow-only', only make tool-bar bigger. */
295
296 Lisp_Object Vauto_resize_tool_bars;
297
298 /* Non-zero means draw block and hollow cursor as wide as the glyph
299 under it. For example, if a block cursor is over a tab, it will be
300 drawn as wide as that tab on the display. */
301
302 int x_stretch_cursor_p;
303
304 /* Non-nil means don't actually do any redisplay. */
305
306 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
307
308 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
309
310 int inhibit_eval_during_redisplay;
311
312 /* Names of text properties relevant for redisplay. */
313
314 Lisp_Object Qdisplay;
315 extern Lisp_Object Qface, Qinvisible, Qwidth;
316
317 /* Symbols used in text property values. */
318
319 Lisp_Object Vdisplay_pixels_per_inch;
320 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
321 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
322 Lisp_Object Qslice;
323 Lisp_Object Qcenter;
324 Lisp_Object Qmargin, Qpointer;
325 Lisp_Object Qline_height;
326 extern Lisp_Object Qheight;
327 extern Lisp_Object QCwidth, QCheight, QCascent;
328 extern Lisp_Object Qscroll_bar;
329 extern Lisp_Object Qcursor;
330
331 /* Non-nil means highlight trailing whitespace. */
332
333 Lisp_Object Vshow_trailing_whitespace;
334
335 /* Non-nil means escape non-break space and hyphens. */
336
337 Lisp_Object Vnobreak_char_display;
338
339 #ifdef HAVE_WINDOW_SYSTEM
340 extern Lisp_Object Voverflow_newline_into_fringe;
341
342 /* Test if overflow newline into fringe. Called with iterator IT
343 at or past right window margin, and with IT->current_x set. */
344
345 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
346 (!NILP (Voverflow_newline_into_fringe) \
347 && FRAME_WINDOW_P (it->f) \
348 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
349 && it->current_x == it->last_visible_x \
350 && it->line_wrap != WORD_WRAP)
351
352 #endif /* HAVE_WINDOW_SYSTEM */
353
354 /* Test if the display element loaded in IT is a space or tab
355 character. This is used to determine word wrapping. */
356
357 #define IT_DISPLAYING_WHITESPACE(it) \
358 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
359
360 /* Non-nil means show the text cursor in void text areas
361 i.e. in blank areas after eol and eob. This used to be
362 the default in 21.3. */
363
364 Lisp_Object Vvoid_text_area_pointer;
365
366 /* Name of the face used to highlight trailing whitespace. */
367
368 Lisp_Object Qtrailing_whitespace;
369
370 /* Name and number of the face used to highlight escape glyphs. */
371
372 Lisp_Object Qescape_glyph;
373
374 /* Name and number of the face used to highlight non-breaking spaces. */
375
376 Lisp_Object Qnobreak_space;
377
378 /* The symbol `image' which is the car of the lists used to represent
379 images in Lisp. */
380
381 Lisp_Object Qimage;
382
383 /* The image map types. */
384 Lisp_Object QCmap, QCpointer;
385 Lisp_Object Qrect, Qcircle, Qpoly;
386
387 /* Non-zero means print newline to stdout before next mini-buffer
388 message. */
389
390 int noninteractive_need_newline;
391
392 /* Non-zero means print newline to message log before next message. */
393
394 static int message_log_need_newline;
395
396 /* Three markers that message_dolog uses.
397 It could allocate them itself, but that causes trouble
398 in handling memory-full errors. */
399 static Lisp_Object message_dolog_marker1;
400 static Lisp_Object message_dolog_marker2;
401 static Lisp_Object message_dolog_marker3;
402 \f
403 /* The buffer position of the first character appearing entirely or
404 partially on the line of the selected window which contains the
405 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
406 redisplay optimization in redisplay_internal. */
407
408 static struct text_pos this_line_start_pos;
409
410 /* Number of characters past the end of the line above, including the
411 terminating newline. */
412
413 static struct text_pos this_line_end_pos;
414
415 /* The vertical positions and the height of this line. */
416
417 static int this_line_vpos;
418 static int this_line_y;
419 static int this_line_pixel_height;
420
421 /* X position at which this display line starts. Usually zero;
422 negative if first character is partially visible. */
423
424 static int this_line_start_x;
425
426 /* Buffer that this_line_.* variables are referring to. */
427
428 static struct buffer *this_line_buffer;
429
430 /* Nonzero means truncate lines in all windows less wide than the
431 frame. */
432
433 Lisp_Object Vtruncate_partial_width_windows;
434
435 /* A flag to control how to display unibyte 8-bit character. */
436
437 int unibyte_display_via_language_environment;
438
439 /* Nonzero means we have more than one non-mini-buffer-only frame.
440 Not guaranteed to be accurate except while parsing
441 frame-title-format. */
442
443 int multiple_frames;
444
445 Lisp_Object Vglobal_mode_string;
446
447
448 /* List of variables (symbols) which hold markers for overlay arrows.
449 The symbols on this list are examined during redisplay to determine
450 where to display overlay arrows. */
451
452 Lisp_Object Voverlay_arrow_variable_list;
453
454 /* Marker for where to display an arrow on top of the buffer text. */
455
456 Lisp_Object Voverlay_arrow_position;
457
458 /* String to display for the arrow. Only used on terminal frames. */
459
460 Lisp_Object Voverlay_arrow_string;
461
462 /* Values of those variables at last redisplay are stored as
463 properties on `overlay-arrow-position' symbol. However, if
464 Voverlay_arrow_position is a marker, last-arrow-position is its
465 numerical position. */
466
467 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
468
469 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
470 properties on a symbol in overlay-arrow-variable-list. */
471
472 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
473
474 /* Like mode-line-format, but for the title bar on a visible frame. */
475
476 Lisp_Object Vframe_title_format;
477
478 /* Like mode-line-format, but for the title bar on an iconified frame. */
479
480 Lisp_Object Vicon_title_format;
481
482 /* List of functions to call when a window's size changes. These
483 functions get one arg, a frame on which one or more windows' sizes
484 have changed. */
485
486 static Lisp_Object Vwindow_size_change_functions;
487
488 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
489
490 /* Nonzero if an overlay arrow has been displayed in this window. */
491
492 static int overlay_arrow_seen;
493
494 /* Nonzero means highlight the region even in nonselected windows. */
495
496 int highlight_nonselected_windows;
497
498 /* If cursor motion alone moves point off frame, try scrolling this
499 many lines up or down if that will bring it back. */
500
501 static EMACS_INT scroll_step;
502
503 /* Nonzero means scroll just far enough to bring point back on the
504 screen, when appropriate. */
505
506 static EMACS_INT scroll_conservatively;
507
508 /* Recenter the window whenever point gets within this many lines of
509 the top or bottom of the window. This value is translated into a
510 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
511 that there is really a fixed pixel height scroll margin. */
512
513 EMACS_INT scroll_margin;
514
515 /* Number of windows showing the buffer of the selected window (or
516 another buffer with the same base buffer). keyboard.c refers to
517 this. */
518
519 int buffer_shared;
520
521 /* Vector containing glyphs for an ellipsis `...'. */
522
523 static Lisp_Object default_invis_vector[3];
524
525 /* Zero means display the mode-line/header-line/menu-bar in the default face
526 (this slightly odd definition is for compatibility with previous versions
527 of emacs), non-zero means display them using their respective faces.
528
529 This variable is deprecated. */
530
531 int mode_line_inverse_video;
532
533 /* Prompt to display in front of the mini-buffer contents. */
534
535 Lisp_Object minibuf_prompt;
536
537 /* Width of current mini-buffer prompt. Only set after display_line
538 of the line that contains the prompt. */
539
540 int minibuf_prompt_width;
541
542 /* This is the window where the echo area message was displayed. It
543 is always a mini-buffer window, but it may not be the same window
544 currently active as a mini-buffer. */
545
546 Lisp_Object echo_area_window;
547
548 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
549 pushes the current message and the value of
550 message_enable_multibyte on the stack, the function restore_message
551 pops the stack and displays MESSAGE again. */
552
553 Lisp_Object Vmessage_stack;
554
555 /* Nonzero means multibyte characters were enabled when the echo area
556 message was specified. */
557
558 int message_enable_multibyte;
559
560 /* Nonzero if we should redraw the mode lines on the next redisplay. */
561
562 int update_mode_lines;
563
564 /* Nonzero if window sizes or contents have changed since last
565 redisplay that finished. */
566
567 int windows_or_buffers_changed;
568
569 /* Nonzero means a frame's cursor type has been changed. */
570
571 int cursor_type_changed;
572
573 /* Nonzero after display_mode_line if %l was used and it displayed a
574 line number. */
575
576 int line_number_displayed;
577
578 /* Maximum buffer size for which to display line numbers. */
579
580 Lisp_Object Vline_number_display_limit;
581
582 /* Line width to consider when repositioning for line number display. */
583
584 static EMACS_INT line_number_display_limit_width;
585
586 /* Number of lines to keep in the message log buffer. t means
587 infinite. nil means don't log at all. */
588
589 Lisp_Object Vmessage_log_max;
590
591 /* The name of the *Messages* buffer, a string. */
592
593 static Lisp_Object Vmessages_buffer_name;
594
595 /* Current, index 0, and last displayed echo area message. Either
596 buffers from echo_buffers, or nil to indicate no message. */
597
598 Lisp_Object echo_area_buffer[2];
599
600 /* The buffers referenced from echo_area_buffer. */
601
602 static Lisp_Object echo_buffer[2];
603
604 /* A vector saved used in with_area_buffer to reduce consing. */
605
606 static Lisp_Object Vwith_echo_area_save_vector;
607
608 /* Non-zero means display_echo_area should display the last echo area
609 message again. Set by redisplay_preserve_echo_area. */
610
611 static int display_last_displayed_message_p;
612
613 /* Nonzero if echo area is being used by print; zero if being used by
614 message. */
615
616 int message_buf_print;
617
618 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
619
620 Lisp_Object Qinhibit_menubar_update;
621 int inhibit_menubar_update;
622
623 /* When evaluating expressions from menu bar items (enable conditions,
624 for instance), this is the frame they are being processed for. */
625
626 Lisp_Object Vmenu_updating_frame;
627
628 /* Maximum height for resizing mini-windows. Either a float
629 specifying a fraction of the available height, or an integer
630 specifying a number of lines. */
631
632 Lisp_Object Vmax_mini_window_height;
633
634 /* Non-zero means messages should be displayed with truncated
635 lines instead of being continued. */
636
637 int message_truncate_lines;
638 Lisp_Object Qmessage_truncate_lines;
639
640 /* Set to 1 in clear_message to make redisplay_internal aware
641 of an emptied echo area. */
642
643 static int message_cleared_p;
644
645 /* How to blink the default frame cursor off. */
646 Lisp_Object Vblink_cursor_alist;
647
648 /* A scratch glyph row with contents used for generating truncation
649 glyphs. Also used in direct_output_for_insert. */
650
651 #define MAX_SCRATCH_GLYPHS 100
652 struct glyph_row scratch_glyph_row;
653 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
654
655 /* Ascent and height of the last line processed by move_it_to. */
656
657 static int last_max_ascent, last_height;
658
659 /* Non-zero if there's a help-echo in the echo area. */
660
661 int help_echo_showing_p;
662
663 /* If >= 0, computed, exact values of mode-line and header-line height
664 to use in the macros CURRENT_MODE_LINE_HEIGHT and
665 CURRENT_HEADER_LINE_HEIGHT. */
666
667 int current_mode_line_height, current_header_line_height;
668
669 /* The maximum distance to look ahead for text properties. Values
670 that are too small let us call compute_char_face and similar
671 functions too often which is expensive. Values that are too large
672 let us call compute_char_face and alike too often because we
673 might not be interested in text properties that far away. */
674
675 #define TEXT_PROP_DISTANCE_LIMIT 100
676
677 #if GLYPH_DEBUG
678
679 /* Variables to turn off display optimizations from Lisp. */
680
681 int inhibit_try_window_id, inhibit_try_window_reusing;
682 int inhibit_try_cursor_movement;
683
684 /* Non-zero means print traces of redisplay if compiled with
685 GLYPH_DEBUG != 0. */
686
687 int trace_redisplay_p;
688
689 #endif /* GLYPH_DEBUG */
690
691 #ifdef DEBUG_TRACE_MOVE
692 /* Non-zero means trace with TRACE_MOVE to stderr. */
693 int trace_move;
694
695 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
696 #else
697 #define TRACE_MOVE(x) (void) 0
698 #endif
699
700 /* Non-zero means automatically scroll windows horizontally to make
701 point visible. */
702
703 int automatic_hscrolling_p;
704 Lisp_Object Qauto_hscroll_mode;
705
706 /* How close to the margin can point get before the window is scrolled
707 horizontally. */
708 EMACS_INT hscroll_margin;
709
710 /* How much to scroll horizontally when point is inside the above margin. */
711 Lisp_Object Vhscroll_step;
712
713 /* The variable `resize-mini-windows'. If nil, don't resize
714 mini-windows. If t, always resize them to fit the text they
715 display. If `grow-only', let mini-windows grow only until they
716 become empty. */
717
718 Lisp_Object Vresize_mini_windows;
719
720 /* Buffer being redisplayed -- for redisplay_window_error. */
721
722 struct buffer *displayed_buffer;
723
724 /* Space between overline and text. */
725
726 EMACS_INT overline_margin;
727
728 /* Require underline to be at least this many screen pixels below baseline
729 This to avoid underline "merging" with the base of letters at small
730 font sizes, particularly when x_use_underline_position_properties is on. */
731
732 EMACS_INT underline_minimum_offset;
733
734 /* Value returned from text property handlers (see below). */
735
736 enum prop_handled
737 {
738 HANDLED_NORMALLY,
739 HANDLED_RECOMPUTE_PROPS,
740 HANDLED_OVERLAY_STRING_CONSUMED,
741 HANDLED_RETURN
742 };
743
744 /* A description of text properties that redisplay is interested
745 in. */
746
747 struct props
748 {
749 /* The name of the property. */
750 Lisp_Object *name;
751
752 /* A unique index for the property. */
753 enum prop_idx idx;
754
755 /* A handler function called to set up iterator IT from the property
756 at IT's current position. Value is used to steer handle_stop. */
757 enum prop_handled (*handler) P_ ((struct it *it));
758 };
759
760 static enum prop_handled handle_face_prop P_ ((struct it *));
761 static enum prop_handled handle_invisible_prop P_ ((struct it *));
762 static enum prop_handled handle_display_prop P_ ((struct it *));
763 static enum prop_handled handle_composition_prop P_ ((struct it *));
764 static enum prop_handled handle_overlay_change P_ ((struct it *));
765 static enum prop_handled handle_fontified_prop P_ ((struct it *));
766 static enum prop_handled handle_auto_composed_prop P_ ((struct it *));
767
768 /* Properties handled by iterators. */
769
770 static struct props it_props[] =
771 {
772 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
773 /* Handle `face' before `display' because some sub-properties of
774 `display' need to know the face. */
775 {&Qface, FACE_PROP_IDX, handle_face_prop},
776 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
777 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
778 {&Qauto_composed, AUTO_COMPOSED_PROP_IDX, handle_auto_composed_prop},
779 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
780 {NULL, 0, NULL}
781 };
782
783 /* Value is the position described by X. If X is a marker, value is
784 the marker_position of X. Otherwise, value is X. */
785
786 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
787
788 /* Enumeration returned by some move_it_.* functions internally. */
789
790 enum move_it_result
791 {
792 /* Not used. Undefined value. */
793 MOVE_UNDEFINED,
794
795 /* Move ended at the requested buffer position or ZV. */
796 MOVE_POS_MATCH_OR_ZV,
797
798 /* Move ended at the requested X pixel position. */
799 MOVE_X_REACHED,
800
801 /* Move within a line ended at the end of a line that must be
802 continued. */
803 MOVE_LINE_CONTINUED,
804
805 /* Move within a line ended at the end of a line that would
806 be displayed truncated. */
807 MOVE_LINE_TRUNCATED,
808
809 /* Move within a line ended at a line end. */
810 MOVE_NEWLINE_OR_CR
811 };
812
813 /* This counter is used to clear the face cache every once in a while
814 in redisplay_internal. It is incremented for each redisplay.
815 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
816 cleared. */
817
818 #define CLEAR_FACE_CACHE_COUNT 500
819 static int clear_face_cache_count;
820
821 /* Similarly for the image cache. */
822
823 #ifdef HAVE_WINDOW_SYSTEM
824 #define CLEAR_IMAGE_CACHE_COUNT 101
825 static int clear_image_cache_count;
826 #endif
827
828 /* Non-zero while redisplay_internal is in progress. */
829
830 int redisplaying_p;
831
832 /* Non-zero means don't free realized faces. Bound while freeing
833 realized faces is dangerous because glyph matrices might still
834 reference them. */
835
836 int inhibit_free_realized_faces;
837 Lisp_Object Qinhibit_free_realized_faces;
838
839 /* If a string, XTread_socket generates an event to display that string.
840 (The display is done in read_char.) */
841
842 Lisp_Object help_echo_string;
843 Lisp_Object help_echo_window;
844 Lisp_Object help_echo_object;
845 int help_echo_pos;
846
847 /* Temporary variable for XTread_socket. */
848
849 Lisp_Object previous_help_echo_string;
850
851 /* Null glyph slice */
852
853 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
854
855 \f
856 /* Function prototypes. */
857
858 static void setup_for_ellipsis P_ ((struct it *, int));
859 static void mark_window_display_accurate_1 P_ ((struct window *, int));
860 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
861 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
862 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
863 static int redisplay_mode_lines P_ ((Lisp_Object, int));
864 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
865
866 static Lisp_Object get_it_property P_ ((struct it *it, Lisp_Object prop));
867
868 static void handle_line_prefix P_ ((struct it *));
869
870 #if 0
871 static int invisible_text_between_p P_ ((struct it *, int, int));
872 #endif
873
874 static void pint2str P_ ((char *, int, int));
875 static void pint2hrstr P_ ((char *, int, int));
876 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
877 struct text_pos));
878 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
879 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
880 static void store_mode_line_noprop_char P_ ((char));
881 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
882 static void x_consider_frame_title P_ ((Lisp_Object));
883 static void handle_stop P_ ((struct it *));
884 static int tool_bar_lines_needed P_ ((struct frame *, int *));
885 static int single_display_spec_intangible_p P_ ((Lisp_Object));
886 static void ensure_echo_area_buffers P_ ((void));
887 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
888 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
889 static int with_echo_area_buffer P_ ((struct window *, int,
890 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
891 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
892 static void clear_garbaged_frames P_ ((void));
893 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
894 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
895 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
896 static int display_echo_area P_ ((struct window *));
897 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
898 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
899 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
900 static int string_char_and_length P_ ((const unsigned char *, int, int *));
901 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
902 struct text_pos));
903 static int compute_window_start_on_continuation_line P_ ((struct window *));
904 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
905 static void insert_left_trunc_glyphs P_ ((struct it *));
906 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
907 Lisp_Object));
908 static void extend_face_to_end_of_line P_ ((struct it *));
909 static int append_space_for_newline P_ ((struct it *, int));
910 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
911 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
912 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
913 static int trailing_whitespace_p P_ ((int));
914 static int message_log_check_duplicate P_ ((int, int, int, int));
915 static void push_it P_ ((struct it *));
916 static void pop_it P_ ((struct it *));
917 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
918 static void select_frame_for_redisplay P_ ((Lisp_Object));
919 static void redisplay_internal P_ ((int));
920 static int echo_area_display P_ ((int));
921 static void redisplay_windows P_ ((Lisp_Object));
922 static void redisplay_window P_ ((Lisp_Object, int));
923 static Lisp_Object redisplay_window_error ();
924 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
925 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
926 static int update_menu_bar P_ ((struct frame *, int, int));
927 static int try_window_reusing_current_matrix P_ ((struct window *));
928 static int try_window_id P_ ((struct window *));
929 static int display_line P_ ((struct it *));
930 static int display_mode_lines P_ ((struct window *));
931 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
932 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
933 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
934 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
935 static void display_menu_bar P_ ((struct window *));
936 static int display_count_lines P_ ((int, int, int, int, int *));
937 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
938 EMACS_INT, EMACS_INT, struct it *, int, int, int, int));
939 static void compute_line_metrics P_ ((struct it *));
940 static void run_redisplay_end_trigger_hook P_ ((struct it *));
941 static int get_overlay_strings P_ ((struct it *, int));
942 static int get_overlay_strings_1 P_ ((struct it *, int, int));
943 static void next_overlay_string P_ ((struct it *));
944 static void reseat P_ ((struct it *, struct text_pos, int));
945 static void reseat_1 P_ ((struct it *, struct text_pos, int));
946 static void back_to_previous_visible_line_start P_ ((struct it *));
947 void reseat_at_previous_visible_line_start P_ ((struct it *));
948 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
949 static int next_element_from_ellipsis P_ ((struct it *));
950 static int next_element_from_display_vector P_ ((struct it *));
951 static int next_element_from_string P_ ((struct it *));
952 static int next_element_from_c_string P_ ((struct it *));
953 static int next_element_from_buffer P_ ((struct it *));
954 static int next_element_from_composition P_ ((struct it *));
955 static int next_element_from_image P_ ((struct it *));
956 static int next_element_from_stretch P_ ((struct it *));
957 static void load_overlay_strings P_ ((struct it *, int));
958 static int init_from_display_pos P_ ((struct it *, struct window *,
959 struct display_pos *));
960 static void reseat_to_string P_ ((struct it *, unsigned char *,
961 Lisp_Object, int, int, int, int));
962 static enum move_it_result
963 move_it_in_display_line_to (struct it *, EMACS_INT, int,
964 enum move_operation_enum);
965 void move_it_vertically_backward P_ ((struct it *, int));
966 static void init_to_row_start P_ ((struct it *, struct window *,
967 struct glyph_row *));
968 static int init_to_row_end P_ ((struct it *, struct window *,
969 struct glyph_row *));
970 static void back_to_previous_line_start P_ ((struct it *));
971 static int forward_to_next_line_start P_ ((struct it *, int *));
972 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
973 Lisp_Object, int));
974 static struct text_pos string_pos P_ ((int, Lisp_Object));
975 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
976 static int number_of_chars P_ ((unsigned char *, int));
977 static void compute_stop_pos P_ ((struct it *));
978 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
979 Lisp_Object));
980 static int face_before_or_after_it_pos P_ ((struct it *, int));
981 static EMACS_INT next_overlay_change P_ ((EMACS_INT));
982 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
983 Lisp_Object, Lisp_Object,
984 struct text_pos *, int));
985 static int underlying_face_id P_ ((struct it *));
986 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
987 struct window *));
988
989 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
990 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
991
992 #ifdef HAVE_WINDOW_SYSTEM
993
994 static void update_tool_bar P_ ((struct frame *, int));
995 static void build_desired_tool_bar_string P_ ((struct frame *f));
996 static int redisplay_tool_bar P_ ((struct frame *));
997 static void display_tool_bar_line P_ ((struct it *, int));
998 static void notice_overwritten_cursor P_ ((struct window *,
999 enum glyph_row_area,
1000 int, int, int, int));
1001
1002
1003
1004 #endif /* HAVE_WINDOW_SYSTEM */
1005
1006 \f
1007 /***********************************************************************
1008 Window display dimensions
1009 ***********************************************************************/
1010
1011 /* Return the bottom boundary y-position for text lines in window W.
1012 This is the first y position at which a line cannot start.
1013 It is relative to the top of the window.
1014
1015 This is the height of W minus the height of a mode line, if any. */
1016
1017 INLINE int
1018 window_text_bottom_y (w)
1019 struct window *w;
1020 {
1021 int height = WINDOW_TOTAL_HEIGHT (w);
1022
1023 if (WINDOW_WANTS_MODELINE_P (w))
1024 height -= CURRENT_MODE_LINE_HEIGHT (w);
1025 return height;
1026 }
1027
1028 /* Return the pixel width of display area AREA of window W. AREA < 0
1029 means return the total width of W, not including fringes to
1030 the left and right of the window. */
1031
1032 INLINE int
1033 window_box_width (w, area)
1034 struct window *w;
1035 int area;
1036 {
1037 int cols = XFASTINT (w->total_cols);
1038 int pixels = 0;
1039
1040 if (!w->pseudo_window_p)
1041 {
1042 cols -= WINDOW_SCROLL_BAR_COLS (w);
1043
1044 if (area == TEXT_AREA)
1045 {
1046 if (INTEGERP (w->left_margin_cols))
1047 cols -= XFASTINT (w->left_margin_cols);
1048 if (INTEGERP (w->right_margin_cols))
1049 cols -= XFASTINT (w->right_margin_cols);
1050 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1051 }
1052 else if (area == LEFT_MARGIN_AREA)
1053 {
1054 cols = (INTEGERP (w->left_margin_cols)
1055 ? XFASTINT (w->left_margin_cols) : 0);
1056 pixels = 0;
1057 }
1058 else if (area == RIGHT_MARGIN_AREA)
1059 {
1060 cols = (INTEGERP (w->right_margin_cols)
1061 ? XFASTINT (w->right_margin_cols) : 0);
1062 pixels = 0;
1063 }
1064 }
1065
1066 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1067 }
1068
1069
1070 /* Return the pixel height of the display area of window W, not
1071 including mode lines of W, if any. */
1072
1073 INLINE int
1074 window_box_height (w)
1075 struct window *w;
1076 {
1077 struct frame *f = XFRAME (w->frame);
1078 int height = WINDOW_TOTAL_HEIGHT (w);
1079
1080 xassert (height >= 0);
1081
1082 /* Note: the code below that determines the mode-line/header-line
1083 height is essentially the same as that contained in the macro
1084 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1085 the appropriate glyph row has its `mode_line_p' flag set,
1086 and if it doesn't, uses estimate_mode_line_height instead. */
1087
1088 if (WINDOW_WANTS_MODELINE_P (w))
1089 {
1090 struct glyph_row *ml_row
1091 = (w->current_matrix && w->current_matrix->rows
1092 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1093 : 0);
1094 if (ml_row && ml_row->mode_line_p)
1095 height -= ml_row->height;
1096 else
1097 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1098 }
1099
1100 if (WINDOW_WANTS_HEADER_LINE_P (w))
1101 {
1102 struct glyph_row *hl_row
1103 = (w->current_matrix && w->current_matrix->rows
1104 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1105 : 0);
1106 if (hl_row && hl_row->mode_line_p)
1107 height -= hl_row->height;
1108 else
1109 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1110 }
1111
1112 /* With a very small font and a mode-line that's taller than
1113 default, we might end up with a negative height. */
1114 return max (0, height);
1115 }
1116
1117 /* Return the window-relative coordinate of the left edge of display
1118 area AREA of window W. AREA < 0 means return the left edge of the
1119 whole window, to the right of the left fringe of W. */
1120
1121 INLINE int
1122 window_box_left_offset (w, area)
1123 struct window *w;
1124 int area;
1125 {
1126 int x;
1127
1128 if (w->pseudo_window_p)
1129 return 0;
1130
1131 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1132
1133 if (area == TEXT_AREA)
1134 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1135 + window_box_width (w, LEFT_MARGIN_AREA));
1136 else if (area == RIGHT_MARGIN_AREA)
1137 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1138 + window_box_width (w, LEFT_MARGIN_AREA)
1139 + window_box_width (w, TEXT_AREA)
1140 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1141 ? 0
1142 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1143 else if (area == LEFT_MARGIN_AREA
1144 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1145 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1146
1147 return x;
1148 }
1149
1150
1151 /* Return the window-relative coordinate of the right edge of display
1152 area AREA of window W. AREA < 0 means return the left edge of the
1153 whole window, to the left of the right fringe of W. */
1154
1155 INLINE int
1156 window_box_right_offset (w, area)
1157 struct window *w;
1158 int area;
1159 {
1160 return window_box_left_offset (w, area) + window_box_width (w, area);
1161 }
1162
1163 /* Return the frame-relative coordinate of the left edge of display
1164 area AREA of window W. AREA < 0 means return the left edge of the
1165 whole window, to the right of the left fringe of W. */
1166
1167 INLINE int
1168 window_box_left (w, area)
1169 struct window *w;
1170 int area;
1171 {
1172 struct frame *f = XFRAME (w->frame);
1173 int x;
1174
1175 if (w->pseudo_window_p)
1176 return FRAME_INTERNAL_BORDER_WIDTH (f);
1177
1178 x = (WINDOW_LEFT_EDGE_X (w)
1179 + window_box_left_offset (w, area));
1180
1181 return x;
1182 }
1183
1184
1185 /* Return the frame-relative coordinate of the right edge of display
1186 area AREA of window W. AREA < 0 means return the left edge of the
1187 whole window, to the left of the right fringe of W. */
1188
1189 INLINE int
1190 window_box_right (w, area)
1191 struct window *w;
1192 int area;
1193 {
1194 return window_box_left (w, area) + window_box_width (w, area);
1195 }
1196
1197 /* Get the bounding box of the display area AREA of window W, without
1198 mode lines, in frame-relative coordinates. AREA < 0 means the
1199 whole window, not including the left and right fringes of
1200 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1201 coordinates of the upper-left corner of the box. Return in
1202 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1203
1204 INLINE void
1205 window_box (w, area, box_x, box_y, box_width, box_height)
1206 struct window *w;
1207 int area;
1208 int *box_x, *box_y, *box_width, *box_height;
1209 {
1210 if (box_width)
1211 *box_width = window_box_width (w, area);
1212 if (box_height)
1213 *box_height = window_box_height (w);
1214 if (box_x)
1215 *box_x = window_box_left (w, area);
1216 if (box_y)
1217 {
1218 *box_y = WINDOW_TOP_EDGE_Y (w);
1219 if (WINDOW_WANTS_HEADER_LINE_P (w))
1220 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1221 }
1222 }
1223
1224
1225 /* Get the bounding box of the display area AREA of window W, without
1226 mode lines. AREA < 0 means the whole window, not including the
1227 left and right fringe of the window. Return in *TOP_LEFT_X
1228 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1229 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1230 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1231 box. */
1232
1233 INLINE void
1234 window_box_edges (w, area, top_left_x, top_left_y,
1235 bottom_right_x, bottom_right_y)
1236 struct window *w;
1237 int area;
1238 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1239 {
1240 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1241 bottom_right_y);
1242 *bottom_right_x += *top_left_x;
1243 *bottom_right_y += *top_left_y;
1244 }
1245
1246
1247 \f
1248 /***********************************************************************
1249 Utilities
1250 ***********************************************************************/
1251
1252 /* Return the bottom y-position of the line the iterator IT is in.
1253 This can modify IT's settings. */
1254
1255 int
1256 line_bottom_y (it)
1257 struct it *it;
1258 {
1259 int line_height = it->max_ascent + it->max_descent;
1260 int line_top_y = it->current_y;
1261
1262 if (line_height == 0)
1263 {
1264 if (last_height)
1265 line_height = last_height;
1266 else if (IT_CHARPOS (*it) < ZV)
1267 {
1268 move_it_by_lines (it, 1, 1);
1269 line_height = (it->max_ascent || it->max_descent
1270 ? it->max_ascent + it->max_descent
1271 : last_height);
1272 }
1273 else
1274 {
1275 struct glyph_row *row = it->glyph_row;
1276
1277 /* Use the default character height. */
1278 it->glyph_row = NULL;
1279 it->what = IT_CHARACTER;
1280 it->c = ' ';
1281 it->len = 1;
1282 PRODUCE_GLYPHS (it);
1283 line_height = it->ascent + it->descent;
1284 it->glyph_row = row;
1285 }
1286 }
1287
1288 return line_top_y + line_height;
1289 }
1290
1291
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1297
1298 int
1299 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1300 struct window *w;
1301 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1302 {
1303 struct it it;
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307
1308 if (noninteractive)
1309 return visible_p;
1310
1311 if (XBUFFER (w->buffer) != current_buffer)
1312 {
1313 old_buffer = current_buffer;
1314 set_buffer_internal_1 (XBUFFER (w->buffer));
1315 }
1316
1317 SET_TEXT_POS_FROM_MARKER (top, w->start);
1318
1319 /* Compute exact mode line heights. */
1320 if (WINDOW_WANTS_MODELINE_P (w))
1321 current_mode_line_height
1322 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1323 current_buffer->mode_line_format);
1324
1325 if (WINDOW_WANTS_HEADER_LINE_P (w))
1326 current_header_line_height
1327 = display_mode_line (w, HEADER_LINE_FACE_ID,
1328 current_buffer->header_line_format);
1329
1330 start_display (&it, w, top);
1331 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1332 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1333
1334 /* Note that we may overshoot because of invisible text. */
1335 if (charpos >= 0 && IT_CHARPOS (it) >= charpos)
1336 {
1337 int top_x = it.current_x;
1338 int top_y = it.current_y;
1339 int bottom_y = (last_height = 0, line_bottom_y (&it));
1340 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1341
1342 if (top_y < window_top_y)
1343 visible_p = bottom_y > window_top_y;
1344 else if (top_y < it.last_visible_y)
1345 visible_p = 1;
1346 if (visible_p)
1347 {
1348 if (it.method == GET_FROM_BUFFER)
1349 {
1350 Lisp_Object window, prop;
1351
1352 XSETWINDOW (window, w);
1353 prop = Fget_char_property (make_number (it.position.charpos),
1354 Qinvisible, window);
1355
1356 /* If charpos coincides with invisible text covered with an
1357 ellipsis, use the first glyph of the ellipsis to compute
1358 the pixel positions. */
1359 if (TEXT_PROP_MEANS_INVISIBLE (prop) == 2)
1360 {
1361 struct glyph_row *row = it.glyph_row;
1362 struct glyph *glyph = row->glyphs[TEXT_AREA];
1363 struct glyph *end = glyph + row->used[TEXT_AREA];
1364 int x = row->x;
1365
1366 for (; glyph < end && glyph->charpos < charpos; glyph++)
1367 x += glyph->pixel_width;
1368
1369 top_x = x;
1370 }
1371 }
1372
1373 *x = top_x;
1374 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1375 *rtop = max (0, window_top_y - top_y);
1376 *rbot = max (0, bottom_y - it.last_visible_y);
1377 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1378 - max (top_y, window_top_y)));
1379 *vpos = it.vpos;
1380 }
1381 }
1382 else
1383 {
1384 struct it it2;
1385
1386 it2 = it;
1387 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1388 move_it_by_lines (&it, 1, 0);
1389 if (charpos < IT_CHARPOS (it)
1390 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1391 {
1392 visible_p = 1;
1393 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1394 *x = it2.current_x;
1395 *y = it2.current_y + it2.max_ascent - it2.ascent;
1396 *rtop = max (0, -it2.current_y);
1397 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1398 - it.last_visible_y));
1399 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1400 it.last_visible_y)
1401 - max (it2.current_y,
1402 WINDOW_HEADER_LINE_HEIGHT (w))));
1403 *vpos = it2.vpos;
1404 }
1405 }
1406
1407 if (old_buffer)
1408 set_buffer_internal_1 (old_buffer);
1409
1410 current_header_line_height = current_mode_line_height = -1;
1411
1412 if (visible_p && XFASTINT (w->hscroll) > 0)
1413 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1414
1415 #if 0
1416 /* Debugging code. */
1417 if (visible_p)
1418 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1419 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1420 else
1421 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1422 #endif
1423
1424 return visible_p;
1425 }
1426
1427
1428 /* Return the next character from STR which is MAXLEN bytes long.
1429 Return in *LEN the length of the character. This is like
1430 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1431 we find one, we return a `?', but with the length of the invalid
1432 character. */
1433
1434 static INLINE int
1435 string_char_and_length (str, maxlen, len)
1436 const unsigned char *str;
1437 int maxlen, *len;
1438 {
1439 int c;
1440
1441 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1442 if (!CHAR_VALID_P (c, 1))
1443 /* We may not change the length here because other places in Emacs
1444 don't use this function, i.e. they silently accept invalid
1445 characters. */
1446 c = '?';
1447
1448 return c;
1449 }
1450
1451
1452
1453 /* Given a position POS containing a valid character and byte position
1454 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1455
1456 static struct text_pos
1457 string_pos_nchars_ahead (pos, string, nchars)
1458 struct text_pos pos;
1459 Lisp_Object string;
1460 int nchars;
1461 {
1462 xassert (STRINGP (string) && nchars >= 0);
1463
1464 if (STRING_MULTIBYTE (string))
1465 {
1466 int rest = SBYTES (string) - BYTEPOS (pos);
1467 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1468 int len;
1469
1470 while (nchars--)
1471 {
1472 string_char_and_length (p, rest, &len);
1473 p += len, rest -= len;
1474 xassert (rest >= 0);
1475 CHARPOS (pos) += 1;
1476 BYTEPOS (pos) += len;
1477 }
1478 }
1479 else
1480 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1481
1482 return pos;
1483 }
1484
1485
1486 /* Value is the text position, i.e. character and byte position,
1487 for character position CHARPOS in STRING. */
1488
1489 static INLINE struct text_pos
1490 string_pos (charpos, string)
1491 int charpos;
1492 Lisp_Object string;
1493 {
1494 struct text_pos pos;
1495 xassert (STRINGP (string));
1496 xassert (charpos >= 0);
1497 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1498 return pos;
1499 }
1500
1501
1502 /* Value is a text position, i.e. character and byte position, for
1503 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1504 means recognize multibyte characters. */
1505
1506 static struct text_pos
1507 c_string_pos (charpos, s, multibyte_p)
1508 int charpos;
1509 unsigned char *s;
1510 int multibyte_p;
1511 {
1512 struct text_pos pos;
1513
1514 xassert (s != NULL);
1515 xassert (charpos >= 0);
1516
1517 if (multibyte_p)
1518 {
1519 int rest = strlen (s), len;
1520
1521 SET_TEXT_POS (pos, 0, 0);
1522 while (charpos--)
1523 {
1524 string_char_and_length (s, rest, &len);
1525 s += len, rest -= len;
1526 xassert (rest >= 0);
1527 CHARPOS (pos) += 1;
1528 BYTEPOS (pos) += len;
1529 }
1530 }
1531 else
1532 SET_TEXT_POS (pos, charpos, charpos);
1533
1534 return pos;
1535 }
1536
1537
1538 /* Value is the number of characters in C string S. MULTIBYTE_P
1539 non-zero means recognize multibyte characters. */
1540
1541 static int
1542 number_of_chars (s, multibyte_p)
1543 unsigned char *s;
1544 int multibyte_p;
1545 {
1546 int nchars;
1547
1548 if (multibyte_p)
1549 {
1550 int rest = strlen (s), len;
1551 unsigned char *p = (unsigned char *) s;
1552
1553 for (nchars = 0; rest > 0; ++nchars)
1554 {
1555 string_char_and_length (p, rest, &len);
1556 rest -= len, p += len;
1557 }
1558 }
1559 else
1560 nchars = strlen (s);
1561
1562 return nchars;
1563 }
1564
1565
1566 /* Compute byte position NEWPOS->bytepos corresponding to
1567 NEWPOS->charpos. POS is a known position in string STRING.
1568 NEWPOS->charpos must be >= POS.charpos. */
1569
1570 static void
1571 compute_string_pos (newpos, pos, string)
1572 struct text_pos *newpos, pos;
1573 Lisp_Object string;
1574 {
1575 xassert (STRINGP (string));
1576 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1577
1578 if (STRING_MULTIBYTE (string))
1579 *newpos = string_pos_nchars_ahead (pos, string,
1580 CHARPOS (*newpos) - CHARPOS (pos));
1581 else
1582 BYTEPOS (*newpos) = CHARPOS (*newpos);
1583 }
1584
1585 /* EXPORT:
1586 Return an estimation of the pixel height of mode or top lines on
1587 frame F. FACE_ID specifies what line's height to estimate. */
1588
1589 int
1590 estimate_mode_line_height (f, face_id)
1591 struct frame *f;
1592 enum face_id face_id;
1593 {
1594 #ifdef HAVE_WINDOW_SYSTEM
1595 if (FRAME_WINDOW_P (f))
1596 {
1597 int height = FONT_HEIGHT (FRAME_FONT (f));
1598
1599 /* This function is called so early when Emacs starts that the face
1600 cache and mode line face are not yet initialized. */
1601 if (FRAME_FACE_CACHE (f))
1602 {
1603 struct face *face = FACE_FROM_ID (f, face_id);
1604 if (face)
1605 {
1606 if (face->font)
1607 height = FONT_HEIGHT (face->font);
1608 if (face->box_line_width > 0)
1609 height += 2 * face->box_line_width;
1610 }
1611 }
1612
1613 return height;
1614 }
1615 #endif
1616
1617 return 1;
1618 }
1619
1620 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1621 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1622 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1623 not force the value into range. */
1624
1625 void
1626 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1627 FRAME_PTR f;
1628 register int pix_x, pix_y;
1629 int *x, *y;
1630 NativeRectangle *bounds;
1631 int noclip;
1632 {
1633
1634 #ifdef HAVE_WINDOW_SYSTEM
1635 if (FRAME_WINDOW_P (f))
1636 {
1637 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1638 even for negative values. */
1639 if (pix_x < 0)
1640 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1641 if (pix_y < 0)
1642 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1643
1644 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1645 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1646
1647 if (bounds)
1648 STORE_NATIVE_RECT (*bounds,
1649 FRAME_COL_TO_PIXEL_X (f, pix_x),
1650 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1651 FRAME_COLUMN_WIDTH (f) - 1,
1652 FRAME_LINE_HEIGHT (f) - 1);
1653
1654 if (!noclip)
1655 {
1656 if (pix_x < 0)
1657 pix_x = 0;
1658 else if (pix_x > FRAME_TOTAL_COLS (f))
1659 pix_x = FRAME_TOTAL_COLS (f);
1660
1661 if (pix_y < 0)
1662 pix_y = 0;
1663 else if (pix_y > FRAME_LINES (f))
1664 pix_y = FRAME_LINES (f);
1665 }
1666 }
1667 #endif
1668
1669 *x = pix_x;
1670 *y = pix_y;
1671 }
1672
1673
1674 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1675 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1676 can't tell the positions because W's display is not up to date,
1677 return 0. */
1678
1679 int
1680 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1681 struct window *w;
1682 int hpos, vpos;
1683 int *frame_x, *frame_y;
1684 {
1685 #ifdef HAVE_WINDOW_SYSTEM
1686 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1687 {
1688 int success_p;
1689
1690 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1691 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1692
1693 if (display_completed)
1694 {
1695 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1696 struct glyph *glyph = row->glyphs[TEXT_AREA];
1697 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1698
1699 hpos = row->x;
1700 vpos = row->y;
1701 while (glyph < end)
1702 {
1703 hpos += glyph->pixel_width;
1704 ++glyph;
1705 }
1706
1707 /* If first glyph is partially visible, its first visible position is still 0. */
1708 if (hpos < 0)
1709 hpos = 0;
1710
1711 success_p = 1;
1712 }
1713 else
1714 {
1715 hpos = vpos = 0;
1716 success_p = 0;
1717 }
1718
1719 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1720 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1721 return success_p;
1722 }
1723 #endif
1724
1725 *frame_x = hpos;
1726 *frame_y = vpos;
1727 return 1;
1728 }
1729
1730
1731 #ifdef HAVE_WINDOW_SYSTEM
1732
1733 /* Find the glyph under window-relative coordinates X/Y in window W.
1734 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1735 strings. Return in *HPOS and *VPOS the row and column number of
1736 the glyph found. Return in *AREA the glyph area containing X.
1737 Value is a pointer to the glyph found or null if X/Y is not on
1738 text, or we can't tell because W's current matrix is not up to
1739 date. */
1740
1741 #ifndef HAVE_CARBON
1742 static
1743 #endif
1744 struct glyph *
1745 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1746 struct window *w;
1747 int x, y;
1748 int *hpos, *vpos, *dx, *dy, *area;
1749 {
1750 struct glyph *glyph, *end;
1751 struct glyph_row *row = NULL;
1752 int x0, i;
1753
1754 /* Find row containing Y. Give up if some row is not enabled. */
1755 for (i = 0; i < w->current_matrix->nrows; ++i)
1756 {
1757 row = MATRIX_ROW (w->current_matrix, i);
1758 if (!row->enabled_p)
1759 return NULL;
1760 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1761 break;
1762 }
1763
1764 *vpos = i;
1765 *hpos = 0;
1766
1767 /* Give up if Y is not in the window. */
1768 if (i == w->current_matrix->nrows)
1769 return NULL;
1770
1771 /* Get the glyph area containing X. */
1772 if (w->pseudo_window_p)
1773 {
1774 *area = TEXT_AREA;
1775 x0 = 0;
1776 }
1777 else
1778 {
1779 if (x < window_box_left_offset (w, TEXT_AREA))
1780 {
1781 *area = LEFT_MARGIN_AREA;
1782 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1783 }
1784 else if (x < window_box_right_offset (w, TEXT_AREA))
1785 {
1786 *area = TEXT_AREA;
1787 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1788 }
1789 else
1790 {
1791 *area = RIGHT_MARGIN_AREA;
1792 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1793 }
1794 }
1795
1796 /* Find glyph containing X. */
1797 glyph = row->glyphs[*area];
1798 end = glyph + row->used[*area];
1799 x -= x0;
1800 while (glyph < end && x >= glyph->pixel_width)
1801 {
1802 x -= glyph->pixel_width;
1803 ++glyph;
1804 }
1805
1806 if (glyph == end)
1807 return NULL;
1808
1809 if (dx)
1810 {
1811 *dx = x;
1812 *dy = y - (row->y + row->ascent - glyph->ascent);
1813 }
1814
1815 *hpos = glyph - row->glyphs[*area];
1816 return glyph;
1817 }
1818
1819
1820 /* EXPORT:
1821 Convert frame-relative x/y to coordinates relative to window W.
1822 Takes pseudo-windows into account. */
1823
1824 void
1825 frame_to_window_pixel_xy (w, x, y)
1826 struct window *w;
1827 int *x, *y;
1828 {
1829 if (w->pseudo_window_p)
1830 {
1831 /* A pseudo-window is always full-width, and starts at the
1832 left edge of the frame, plus a frame border. */
1833 struct frame *f = XFRAME (w->frame);
1834 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1835 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1836 }
1837 else
1838 {
1839 *x -= WINDOW_LEFT_EDGE_X (w);
1840 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1841 }
1842 }
1843
1844 /* EXPORT:
1845 Return in RECTS[] at most N clipping rectangles for glyph string S.
1846 Return the number of stored rectangles. */
1847
1848 int
1849 get_glyph_string_clip_rects (s, rects, n)
1850 struct glyph_string *s;
1851 NativeRectangle *rects;
1852 int n;
1853 {
1854 XRectangle r;
1855
1856 if (n <= 0)
1857 return 0;
1858
1859 if (s->row->full_width_p)
1860 {
1861 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1862 r.x = WINDOW_LEFT_EDGE_X (s->w);
1863 r.width = WINDOW_TOTAL_WIDTH (s->w);
1864
1865 /* Unless displaying a mode or menu bar line, which are always
1866 fully visible, clip to the visible part of the row. */
1867 if (s->w->pseudo_window_p)
1868 r.height = s->row->visible_height;
1869 else
1870 r.height = s->height;
1871 }
1872 else
1873 {
1874 /* This is a text line that may be partially visible. */
1875 r.x = window_box_left (s->w, s->area);
1876 r.width = window_box_width (s->w, s->area);
1877 r.height = s->row->visible_height;
1878 }
1879
1880 if (s->clip_head)
1881 if (r.x < s->clip_head->x)
1882 {
1883 if (r.width >= s->clip_head->x - r.x)
1884 r.width -= s->clip_head->x - r.x;
1885 else
1886 r.width = 0;
1887 r.x = s->clip_head->x;
1888 }
1889 if (s->clip_tail)
1890 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1891 {
1892 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1893 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1894 else
1895 r.width = 0;
1896 }
1897
1898 /* If S draws overlapping rows, it's sufficient to use the top and
1899 bottom of the window for clipping because this glyph string
1900 intentionally draws over other lines. */
1901 if (s->for_overlaps)
1902 {
1903 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1904 r.height = window_text_bottom_y (s->w) - r.y;
1905
1906 /* Alas, the above simple strategy does not work for the
1907 environments with anti-aliased text: if the same text is
1908 drawn onto the same place multiple times, it gets thicker.
1909 If the overlap we are processing is for the erased cursor, we
1910 take the intersection with the rectagle of the cursor. */
1911 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1912 {
1913 XRectangle rc, r_save = r;
1914
1915 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1916 rc.y = s->w->phys_cursor.y;
1917 rc.width = s->w->phys_cursor_width;
1918 rc.height = s->w->phys_cursor_height;
1919
1920 x_intersect_rectangles (&r_save, &rc, &r);
1921 }
1922 }
1923 else
1924 {
1925 /* Don't use S->y for clipping because it doesn't take partially
1926 visible lines into account. For example, it can be negative for
1927 partially visible lines at the top of a window. */
1928 if (!s->row->full_width_p
1929 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1930 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1931 else
1932 r.y = max (0, s->row->y);
1933
1934 /* If drawing a tool-bar window, draw it over the internal border
1935 at the top of the window. */
1936 if (WINDOWP (s->f->tool_bar_window)
1937 && s->w == XWINDOW (s->f->tool_bar_window))
1938 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1939 }
1940
1941 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1942
1943 /* If drawing the cursor, don't let glyph draw outside its
1944 advertised boundaries. Cleartype does this under some circumstances. */
1945 if (s->hl == DRAW_CURSOR)
1946 {
1947 struct glyph *glyph = s->first_glyph;
1948 int height, max_y;
1949
1950 if (s->x > r.x)
1951 {
1952 r.width -= s->x - r.x;
1953 r.x = s->x;
1954 }
1955 r.width = min (r.width, glyph->pixel_width);
1956
1957 /* If r.y is below window bottom, ensure that we still see a cursor. */
1958 height = min (glyph->ascent + glyph->descent,
1959 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1960 max_y = window_text_bottom_y (s->w) - height;
1961 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1962 if (s->ybase - glyph->ascent > max_y)
1963 {
1964 r.y = max_y;
1965 r.height = height;
1966 }
1967 else
1968 {
1969 /* Don't draw cursor glyph taller than our actual glyph. */
1970 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1971 if (height < r.height)
1972 {
1973 max_y = r.y + r.height;
1974 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1975 r.height = min (max_y - r.y, height);
1976 }
1977 }
1978 }
1979
1980 if (s->row->clip)
1981 {
1982 XRectangle r_save = r;
1983
1984 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1985 r.width = 0;
1986 }
1987
1988 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1989 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1990 {
1991 #ifdef CONVERT_FROM_XRECT
1992 CONVERT_FROM_XRECT (r, *rects);
1993 #else
1994 *rects = r;
1995 #endif
1996 return 1;
1997 }
1998 else
1999 {
2000 /* If we are processing overlapping and allowed to return
2001 multiple clipping rectangles, we exclude the row of the glyph
2002 string from the clipping rectangle. This is to avoid drawing
2003 the same text on the environment with anti-aliasing. */
2004 #ifdef CONVERT_FROM_XRECT
2005 XRectangle rs[2];
2006 #else
2007 XRectangle *rs = rects;
2008 #endif
2009 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2010
2011 if (s->for_overlaps & OVERLAPS_PRED)
2012 {
2013 rs[i] = r;
2014 if (r.y + r.height > row_y)
2015 {
2016 if (r.y < row_y)
2017 rs[i].height = row_y - r.y;
2018 else
2019 rs[i].height = 0;
2020 }
2021 i++;
2022 }
2023 if (s->for_overlaps & OVERLAPS_SUCC)
2024 {
2025 rs[i] = r;
2026 if (r.y < row_y + s->row->visible_height)
2027 {
2028 if (r.y + r.height > row_y + s->row->visible_height)
2029 {
2030 rs[i].y = row_y + s->row->visible_height;
2031 rs[i].height = r.y + r.height - rs[i].y;
2032 }
2033 else
2034 rs[i].height = 0;
2035 }
2036 i++;
2037 }
2038
2039 n = i;
2040 #ifdef CONVERT_FROM_XRECT
2041 for (i = 0; i < n; i++)
2042 CONVERT_FROM_XRECT (rs[i], rects[i]);
2043 #endif
2044 return n;
2045 }
2046 }
2047
2048 /* EXPORT:
2049 Return in *NR the clipping rectangle for glyph string S. */
2050
2051 void
2052 get_glyph_string_clip_rect (s, nr)
2053 struct glyph_string *s;
2054 NativeRectangle *nr;
2055 {
2056 get_glyph_string_clip_rects (s, nr, 1);
2057 }
2058
2059
2060 /* EXPORT:
2061 Return the position and height of the phys cursor in window W.
2062 Set w->phys_cursor_width to width of phys cursor.
2063 */
2064
2065 void
2066 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2067 struct window *w;
2068 struct glyph_row *row;
2069 struct glyph *glyph;
2070 int *xp, *yp, *heightp;
2071 {
2072 struct frame *f = XFRAME (WINDOW_FRAME (w));
2073 int x, y, wd, h, h0, y0;
2074
2075 /* Compute the width of the rectangle to draw. If on a stretch
2076 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2077 rectangle as wide as the glyph, but use a canonical character
2078 width instead. */
2079 wd = glyph->pixel_width - 1;
2080 #ifdef HAVE_NTGUI
2081 wd++; /* Why? */
2082 #endif
2083
2084 x = w->phys_cursor.x;
2085 if (x < 0)
2086 {
2087 wd += x;
2088 x = 0;
2089 }
2090
2091 if (glyph->type == STRETCH_GLYPH
2092 && !x_stretch_cursor_p)
2093 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2094 w->phys_cursor_width = wd;
2095
2096 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2097
2098 /* If y is below window bottom, ensure that we still see a cursor. */
2099 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2100
2101 h = max (h0, glyph->ascent + glyph->descent);
2102 h0 = min (h0, glyph->ascent + glyph->descent);
2103
2104 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2105 if (y < y0)
2106 {
2107 h = max (h - (y0 - y) + 1, h0);
2108 y = y0 - 1;
2109 }
2110 else
2111 {
2112 y0 = window_text_bottom_y (w) - h0;
2113 if (y > y0)
2114 {
2115 h += y - y0;
2116 y = y0;
2117 }
2118 }
2119
2120 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2121 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2122 *heightp = h;
2123 }
2124
2125 /*
2126 * Remember which glyph the mouse is over.
2127 */
2128
2129 void
2130 remember_mouse_glyph (f, gx, gy, rect)
2131 struct frame *f;
2132 int gx, gy;
2133 NativeRectangle *rect;
2134 {
2135 Lisp_Object window;
2136 struct window *w;
2137 struct glyph_row *r, *gr, *end_row;
2138 enum window_part part;
2139 enum glyph_row_area area;
2140 int x, y, width, height;
2141
2142 /* Try to determine frame pixel position and size of the glyph under
2143 frame pixel coordinates X/Y on frame F. */
2144
2145 if (!f->glyphs_initialized_p
2146 || (window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0),
2147 NILP (window)))
2148 {
2149 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2150 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2151 goto virtual_glyph;
2152 }
2153
2154 w = XWINDOW (window);
2155 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2156 height = WINDOW_FRAME_LINE_HEIGHT (w);
2157
2158 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2159 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2160
2161 if (w->pseudo_window_p)
2162 {
2163 area = TEXT_AREA;
2164 part = ON_MODE_LINE; /* Don't adjust margin. */
2165 goto text_glyph;
2166 }
2167
2168 switch (part)
2169 {
2170 case ON_LEFT_MARGIN:
2171 area = LEFT_MARGIN_AREA;
2172 goto text_glyph;
2173
2174 case ON_RIGHT_MARGIN:
2175 area = RIGHT_MARGIN_AREA;
2176 goto text_glyph;
2177
2178 case ON_HEADER_LINE:
2179 case ON_MODE_LINE:
2180 gr = (part == ON_HEADER_LINE
2181 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2182 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2183 gy = gr->y;
2184 area = TEXT_AREA;
2185 goto text_glyph_row_found;
2186
2187 case ON_TEXT:
2188 area = TEXT_AREA;
2189
2190 text_glyph:
2191 gr = 0; gy = 0;
2192 for (; r <= end_row && r->enabled_p; ++r)
2193 if (r->y + r->height > y)
2194 {
2195 gr = r; gy = r->y;
2196 break;
2197 }
2198
2199 text_glyph_row_found:
2200 if (gr && gy <= y)
2201 {
2202 struct glyph *g = gr->glyphs[area];
2203 struct glyph *end = g + gr->used[area];
2204
2205 height = gr->height;
2206 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2207 if (gx + g->pixel_width > x)
2208 break;
2209
2210 if (g < end)
2211 {
2212 if (g->type == IMAGE_GLYPH)
2213 {
2214 /* Don't remember when mouse is over image, as
2215 image may have hot-spots. */
2216 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2217 return;
2218 }
2219 width = g->pixel_width;
2220 }
2221 else
2222 {
2223 /* Use nominal char spacing at end of line. */
2224 x -= gx;
2225 gx += (x / width) * width;
2226 }
2227
2228 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2229 gx += window_box_left_offset (w, area);
2230 }
2231 else
2232 {
2233 /* Use nominal line height at end of window. */
2234 gx = (x / width) * width;
2235 y -= gy;
2236 gy += (y / height) * height;
2237 }
2238 break;
2239
2240 case ON_LEFT_FRINGE:
2241 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2242 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2243 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2244 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2245 goto row_glyph;
2246
2247 case ON_RIGHT_FRINGE:
2248 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2249 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2250 : window_box_right_offset (w, TEXT_AREA));
2251 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2252 goto row_glyph;
2253
2254 case ON_SCROLL_BAR:
2255 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2256 ? 0
2257 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2258 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2259 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2260 : 0)));
2261 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2262
2263 row_glyph:
2264 gr = 0, gy = 0;
2265 for (; r <= end_row && r->enabled_p; ++r)
2266 if (r->y + r->height > y)
2267 {
2268 gr = r; gy = r->y;
2269 break;
2270 }
2271
2272 if (gr && gy <= y)
2273 height = gr->height;
2274 else
2275 {
2276 /* Use nominal line height at end of window. */
2277 y -= gy;
2278 gy += (y / height) * height;
2279 }
2280 break;
2281
2282 default:
2283 ;
2284 virtual_glyph:
2285 /* If there is no glyph under the mouse, then we divide the screen
2286 into a grid of the smallest glyph in the frame, and use that
2287 as our "glyph". */
2288
2289 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2290 round down even for negative values. */
2291 if (gx < 0)
2292 gx -= width - 1;
2293 if (gy < 0)
2294 gy -= height - 1;
2295
2296 gx = (gx / width) * width;
2297 gy = (gy / height) * height;
2298
2299 goto store_rect;
2300 }
2301
2302 gx += WINDOW_LEFT_EDGE_X (w);
2303 gy += WINDOW_TOP_EDGE_Y (w);
2304
2305 store_rect:
2306 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2307
2308 /* Visible feedback for debugging. */
2309 #if 0
2310 #if HAVE_X_WINDOWS
2311 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2312 f->output_data.x->normal_gc,
2313 gx, gy, width, height);
2314 #endif
2315 #endif
2316 }
2317
2318
2319 #endif /* HAVE_WINDOW_SYSTEM */
2320
2321 \f
2322 /***********************************************************************
2323 Lisp form evaluation
2324 ***********************************************************************/
2325
2326 /* Error handler for safe_eval and safe_call. */
2327
2328 static Lisp_Object
2329 safe_eval_handler (arg)
2330 Lisp_Object arg;
2331 {
2332 add_to_log ("Error during redisplay: %s", arg, Qnil);
2333 return Qnil;
2334 }
2335
2336
2337 /* Evaluate SEXPR and return the result, or nil if something went
2338 wrong. Prevent redisplay during the evaluation. */
2339
2340 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2341 Return the result, or nil if something went wrong. Prevent
2342 redisplay during the evaluation. */
2343
2344 Lisp_Object
2345 safe_call (nargs, args)
2346 int nargs;
2347 Lisp_Object *args;
2348 {
2349 Lisp_Object val;
2350
2351 if (inhibit_eval_during_redisplay)
2352 val = Qnil;
2353 else
2354 {
2355 int count = SPECPDL_INDEX ();
2356 struct gcpro gcpro1;
2357
2358 GCPRO1 (args[0]);
2359 gcpro1.nvars = nargs;
2360 specbind (Qinhibit_redisplay, Qt);
2361 /* Use Qt to ensure debugger does not run,
2362 so there is no possibility of wanting to redisplay. */
2363 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2364 safe_eval_handler);
2365 UNGCPRO;
2366 val = unbind_to (count, val);
2367 }
2368
2369 return val;
2370 }
2371
2372
2373 /* Call function FN with one argument ARG.
2374 Return the result, or nil if something went wrong. */
2375
2376 Lisp_Object
2377 safe_call1 (fn, arg)
2378 Lisp_Object fn, arg;
2379 {
2380 Lisp_Object args[2];
2381 args[0] = fn;
2382 args[1] = arg;
2383 return safe_call (2, args);
2384 }
2385
2386 static Lisp_Object Qeval;
2387
2388 Lisp_Object
2389 safe_eval (Lisp_Object sexpr)
2390 {
2391 return safe_call1 (Qeval, sexpr);
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_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2399 {
2400 Lisp_Object args[3];
2401 args[0] = fn;
2402 args[1] = arg1;
2403 args[2] = arg2;
2404 return safe_call (3, args);
2405 }
2406
2407
2408 \f
2409 /***********************************************************************
2410 Debugging
2411 ***********************************************************************/
2412
2413 #if 0
2414
2415 /* Define CHECK_IT to perform sanity checks on iterators.
2416 This is for debugging. It is too slow to do unconditionally. */
2417
2418 static void
2419 check_it (it)
2420 struct it *it;
2421 {
2422 if (it->method == GET_FROM_STRING)
2423 {
2424 xassert (STRINGP (it->string));
2425 xassert (IT_STRING_CHARPOS (*it) >= 0);
2426 }
2427 else
2428 {
2429 xassert (IT_STRING_CHARPOS (*it) < 0);
2430 if (it->method == GET_FROM_BUFFER)
2431 {
2432 /* Check that character and byte positions agree. */
2433 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2434 }
2435 }
2436
2437 if (it->dpvec)
2438 xassert (it->current.dpvec_index >= 0);
2439 else
2440 xassert (it->current.dpvec_index < 0);
2441 }
2442
2443 #define CHECK_IT(IT) check_it ((IT))
2444
2445 #else /* not 0 */
2446
2447 #define CHECK_IT(IT) (void) 0
2448
2449 #endif /* not 0 */
2450
2451
2452 #if GLYPH_DEBUG
2453
2454 /* Check that the window end of window W is what we expect it
2455 to be---the last row in the current matrix displaying text. */
2456
2457 static void
2458 check_window_end (w)
2459 struct window *w;
2460 {
2461 if (!MINI_WINDOW_P (w)
2462 && !NILP (w->window_end_valid))
2463 {
2464 struct glyph_row *row;
2465 xassert ((row = MATRIX_ROW (w->current_matrix,
2466 XFASTINT (w->window_end_vpos)),
2467 !row->enabled_p
2468 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2469 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2470 }
2471 }
2472
2473 #define CHECK_WINDOW_END(W) check_window_end ((W))
2474
2475 #else /* not GLYPH_DEBUG */
2476
2477 #define CHECK_WINDOW_END(W) (void) 0
2478
2479 #endif /* not GLYPH_DEBUG */
2480
2481
2482 \f
2483 /***********************************************************************
2484 Iterator initialization
2485 ***********************************************************************/
2486
2487 /* Initialize IT for displaying current_buffer in window W, starting
2488 at character position CHARPOS. CHARPOS < 0 means that no buffer
2489 position is specified which is useful when the iterator is assigned
2490 a position later. BYTEPOS is the byte position corresponding to
2491 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2492
2493 If ROW is not null, calls to produce_glyphs with IT as parameter
2494 will produce glyphs in that row.
2495
2496 BASE_FACE_ID is the id of a base face to use. It must be one of
2497 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2498 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2499 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2500
2501 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2502 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2503 will be initialized to use the corresponding mode line glyph row of
2504 the desired matrix of W. */
2505
2506 void
2507 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2508 struct it *it;
2509 struct window *w;
2510 int charpos, bytepos;
2511 struct glyph_row *row;
2512 enum face_id base_face_id;
2513 {
2514 int highlight_region_p;
2515 enum face_id remapped_base_face_id = base_face_id;
2516
2517 /* Some precondition checks. */
2518 xassert (w != NULL && it != NULL);
2519 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2520 && charpos <= ZV));
2521
2522 /* If face attributes have been changed since the last redisplay,
2523 free realized faces now because they depend on face definitions
2524 that might have changed. Don't free faces while there might be
2525 desired matrices pending which reference these faces. */
2526 if (face_change_count && !inhibit_free_realized_faces)
2527 {
2528 face_change_count = 0;
2529 free_all_realized_faces (Qnil);
2530 }
2531
2532 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2533 if (! NILP (Vface_remapping_alist))
2534 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2535
2536 /* Use one of the mode line rows of W's desired matrix if
2537 appropriate. */
2538 if (row == NULL)
2539 {
2540 if (base_face_id == MODE_LINE_FACE_ID
2541 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2542 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2543 else if (base_face_id == HEADER_LINE_FACE_ID)
2544 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2545 }
2546
2547 /* Clear IT. */
2548 bzero (it, sizeof *it);
2549 it->current.overlay_string_index = -1;
2550 it->current.dpvec_index = -1;
2551 it->base_face_id = remapped_base_face_id;
2552 it->string = Qnil;
2553 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2554
2555 /* The window in which we iterate over current_buffer: */
2556 XSETWINDOW (it->window, w);
2557 it->w = w;
2558 it->f = XFRAME (w->frame);
2559
2560 /* Extra space between lines (on window systems only). */
2561 if (base_face_id == DEFAULT_FACE_ID
2562 && FRAME_WINDOW_P (it->f))
2563 {
2564 if (NATNUMP (current_buffer->extra_line_spacing))
2565 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2566 else if (FLOATP (current_buffer->extra_line_spacing))
2567 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2568 * FRAME_LINE_HEIGHT (it->f));
2569 else if (it->f->extra_line_spacing > 0)
2570 it->extra_line_spacing = it->f->extra_line_spacing;
2571 it->max_extra_line_spacing = 0;
2572 }
2573
2574 /* If realized faces have been removed, e.g. because of face
2575 attribute changes of named faces, recompute them. When running
2576 in batch mode, the face cache of the initial frame is null. If
2577 we happen to get called, make a dummy face cache. */
2578 if (FRAME_FACE_CACHE (it->f) == NULL)
2579 init_frame_faces (it->f);
2580 if (FRAME_FACE_CACHE (it->f)->used == 0)
2581 recompute_basic_faces (it->f);
2582
2583 /* Current value of the `slice', `space-width', and 'height' properties. */
2584 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2585 it->space_width = Qnil;
2586 it->font_height = Qnil;
2587 it->override_ascent = -1;
2588
2589 /* Are control characters displayed as `^C'? */
2590 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2591
2592 /* -1 means everything between a CR and the following line end
2593 is invisible. >0 means lines indented more than this value are
2594 invisible. */
2595 it->selective = (INTEGERP (current_buffer->selective_display)
2596 ? XFASTINT (current_buffer->selective_display)
2597 : (!NILP (current_buffer->selective_display)
2598 ? -1 : 0));
2599 it->selective_display_ellipsis_p
2600 = !NILP (current_buffer->selective_display_ellipses);
2601
2602 /* Display table to use. */
2603 it->dp = window_display_table (w);
2604
2605 /* Are multibyte characters enabled in current_buffer? */
2606 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2607
2608 /* Non-zero if we should highlight the region. */
2609 highlight_region_p
2610 = (!NILP (Vtransient_mark_mode)
2611 && !NILP (current_buffer->mark_active)
2612 && XMARKER (current_buffer->mark)->buffer != 0);
2613
2614 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2615 start and end of a visible region in window IT->w. Set both to
2616 -1 to indicate no region. */
2617 if (highlight_region_p
2618 /* Maybe highlight only in selected window. */
2619 && (/* Either show region everywhere. */
2620 highlight_nonselected_windows
2621 /* Or show region in the selected window. */
2622 || w == XWINDOW (selected_window)
2623 /* Or show the region if we are in the mini-buffer and W is
2624 the window the mini-buffer refers to. */
2625 || (MINI_WINDOW_P (XWINDOW (selected_window))
2626 && WINDOWP (minibuf_selected_window)
2627 && w == XWINDOW (minibuf_selected_window))))
2628 {
2629 int charpos = marker_position (current_buffer->mark);
2630 it->region_beg_charpos = min (PT, charpos);
2631 it->region_end_charpos = max (PT, charpos);
2632 }
2633 else
2634 it->region_beg_charpos = it->region_end_charpos = -1;
2635
2636 /* Get the position at which the redisplay_end_trigger hook should
2637 be run, if it is to be run at all. */
2638 if (MARKERP (w->redisplay_end_trigger)
2639 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2640 it->redisplay_end_trigger_charpos
2641 = marker_position (w->redisplay_end_trigger);
2642 else if (INTEGERP (w->redisplay_end_trigger))
2643 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2644
2645 /* Correct bogus values of tab_width. */
2646 it->tab_width = XINT (current_buffer->tab_width);
2647 if (it->tab_width <= 0 || it->tab_width > 1000)
2648 it->tab_width = 8;
2649
2650 /* Are lines in the display truncated? */
2651 if (base_face_id != DEFAULT_FACE_ID
2652 || XINT (it->w->hscroll)
2653 || (! WINDOW_FULL_WIDTH_P (it->w)
2654 && ((!NILP (Vtruncate_partial_width_windows)
2655 && !INTEGERP (Vtruncate_partial_width_windows))
2656 || (INTEGERP (Vtruncate_partial_width_windows)
2657 && (WINDOW_TOTAL_COLS (it->w)
2658 < XINT (Vtruncate_partial_width_windows))))))
2659 it->line_wrap = TRUNCATE;
2660 else if (NILP (current_buffer->truncate_lines))
2661 it->line_wrap = NILP (current_buffer->word_wrap)
2662 ? WINDOW_WRAP : WORD_WRAP;
2663 else
2664 it->line_wrap = TRUNCATE;
2665
2666 /* Get dimensions of truncation and continuation glyphs. These are
2667 displayed as fringe bitmaps under X, so we don't need them for such
2668 frames. */
2669 if (!FRAME_WINDOW_P (it->f))
2670 {
2671 if (it->line_wrap == TRUNCATE)
2672 {
2673 /* We will need the truncation glyph. */
2674 xassert (it->glyph_row == NULL);
2675 produce_special_glyphs (it, IT_TRUNCATION);
2676 it->truncation_pixel_width = it->pixel_width;
2677 }
2678 else
2679 {
2680 /* We will need the continuation glyph. */
2681 xassert (it->glyph_row == NULL);
2682 produce_special_glyphs (it, IT_CONTINUATION);
2683 it->continuation_pixel_width = it->pixel_width;
2684 }
2685
2686 /* Reset these values to zero because the produce_special_glyphs
2687 above has changed them. */
2688 it->pixel_width = it->ascent = it->descent = 0;
2689 it->phys_ascent = it->phys_descent = 0;
2690 }
2691
2692 /* Set this after getting the dimensions of truncation and
2693 continuation glyphs, so that we don't produce glyphs when calling
2694 produce_special_glyphs, above. */
2695 it->glyph_row = row;
2696 it->area = TEXT_AREA;
2697
2698 /* Get the dimensions of the display area. The display area
2699 consists of the visible window area plus a horizontally scrolled
2700 part to the left of the window. All x-values are relative to the
2701 start of this total display area. */
2702 if (base_face_id != DEFAULT_FACE_ID)
2703 {
2704 /* Mode lines, menu bar in terminal frames. */
2705 it->first_visible_x = 0;
2706 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2707 }
2708 else
2709 {
2710 it->first_visible_x
2711 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2712 it->last_visible_x = (it->first_visible_x
2713 + window_box_width (w, TEXT_AREA));
2714
2715 /* If we truncate lines, leave room for the truncator glyph(s) at
2716 the right margin. Otherwise, leave room for the continuation
2717 glyph(s). Truncation and continuation glyphs are not inserted
2718 for window-based redisplay. */
2719 if (!FRAME_WINDOW_P (it->f))
2720 {
2721 if (it->line_wrap == TRUNCATE)
2722 it->last_visible_x -= it->truncation_pixel_width;
2723 else
2724 it->last_visible_x -= it->continuation_pixel_width;
2725 }
2726
2727 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2728 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2729 }
2730
2731 /* Leave room for a border glyph. */
2732 if (!FRAME_WINDOW_P (it->f)
2733 && !WINDOW_RIGHTMOST_P (it->w))
2734 it->last_visible_x -= 1;
2735
2736 it->last_visible_y = window_text_bottom_y (w);
2737
2738 /* For mode lines and alike, arrange for the first glyph having a
2739 left box line if the face specifies a box. */
2740 if (base_face_id != DEFAULT_FACE_ID)
2741 {
2742 struct face *face;
2743
2744 it->face_id = remapped_base_face_id;
2745
2746 /* If we have a boxed mode line, make the first character appear
2747 with a left box line. */
2748 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2749 if (face->box != FACE_NO_BOX)
2750 it->start_of_box_run_p = 1;
2751 }
2752
2753 /* If a buffer position was specified, set the iterator there,
2754 getting overlays and face properties from that position. */
2755 if (charpos >= BUF_BEG (current_buffer))
2756 {
2757 it->end_charpos = ZV;
2758 it->face_id = -1;
2759 IT_CHARPOS (*it) = charpos;
2760
2761 /* Compute byte position if not specified. */
2762 if (bytepos < charpos)
2763 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2764 else
2765 IT_BYTEPOS (*it) = bytepos;
2766
2767 it->start = it->current;
2768
2769 /* Compute faces etc. */
2770 reseat (it, it->current.pos, 1);
2771 }
2772
2773 CHECK_IT (it);
2774 }
2775
2776
2777 /* Initialize IT for the display of window W with window start POS. */
2778
2779 void
2780 start_display (it, w, pos)
2781 struct it *it;
2782 struct window *w;
2783 struct text_pos pos;
2784 {
2785 struct glyph_row *row;
2786 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2787
2788 row = w->desired_matrix->rows + first_vpos;
2789 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2790 it->first_vpos = first_vpos;
2791
2792 /* Don't reseat to previous visible line start if current start
2793 position is in a string or image. */
2794 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2795 {
2796 int start_at_line_beg_p;
2797 int first_y = it->current_y;
2798
2799 /* If window start is not at a line start, skip forward to POS to
2800 get the correct continuation lines width. */
2801 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2802 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2803 if (!start_at_line_beg_p)
2804 {
2805 int new_x;
2806
2807 reseat_at_previous_visible_line_start (it);
2808 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2809
2810 new_x = it->current_x + it->pixel_width;
2811
2812 /* If lines are continued, this line may end in the middle
2813 of a multi-glyph character (e.g. a control character
2814 displayed as \003, or in the middle of an overlay
2815 string). In this case move_it_to above will not have
2816 taken us to the start of the continuation line but to the
2817 end of the continued line. */
2818 if (it->current_x > 0
2819 && it->line_wrap != TRUNCATE /* Lines are continued. */
2820 && (/* And glyph doesn't fit on the line. */
2821 new_x > it->last_visible_x
2822 /* Or it fits exactly and we're on a window
2823 system frame. */
2824 || (new_x == it->last_visible_x
2825 && FRAME_WINDOW_P (it->f))))
2826 {
2827 if (it->current.dpvec_index >= 0
2828 || it->current.overlay_string_index >= 0)
2829 {
2830 set_iterator_to_next (it, 1);
2831 move_it_in_display_line_to (it, -1, -1, 0);
2832 }
2833
2834 it->continuation_lines_width += it->current_x;
2835 }
2836
2837 /* We're starting a new display line, not affected by the
2838 height of the continued line, so clear the appropriate
2839 fields in the iterator structure. */
2840 it->max_ascent = it->max_descent = 0;
2841 it->max_phys_ascent = it->max_phys_descent = 0;
2842
2843 it->current_y = first_y;
2844 it->vpos = 0;
2845 it->current_x = it->hpos = 0;
2846 }
2847 }
2848
2849 #if 0 /* Don't assert the following because start_display is sometimes
2850 called intentionally with a window start that is not at a
2851 line start. Please leave this code in as a comment. */
2852
2853 /* Window start should be on a line start, now. */
2854 xassert (it->continuation_lines_width
2855 || IT_CHARPOS (it) == BEGV
2856 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2857 #endif /* 0 */
2858 }
2859
2860
2861 /* Return 1 if POS is a position in ellipses displayed for invisible
2862 text. W is the window we display, for text property lookup. */
2863
2864 static int
2865 in_ellipses_for_invisible_text_p (pos, w)
2866 struct display_pos *pos;
2867 struct window *w;
2868 {
2869 Lisp_Object prop, window;
2870 int ellipses_p = 0;
2871 int charpos = CHARPOS (pos->pos);
2872
2873 /* If POS specifies a position in a display vector, this might
2874 be for an ellipsis displayed for invisible text. We won't
2875 get the iterator set up for delivering that ellipsis unless
2876 we make sure that it gets aware of the invisible text. */
2877 if (pos->dpvec_index >= 0
2878 && pos->overlay_string_index < 0
2879 && CHARPOS (pos->string_pos) < 0
2880 && charpos > BEGV
2881 && (XSETWINDOW (window, w),
2882 prop = Fget_char_property (make_number (charpos),
2883 Qinvisible, window),
2884 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2885 {
2886 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2887 window);
2888 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2889 }
2890
2891 return ellipses_p;
2892 }
2893
2894
2895 /* Initialize IT for stepping through current_buffer in window W,
2896 starting at position POS that includes overlay string and display
2897 vector/ control character translation position information. Value
2898 is zero if there are overlay strings with newlines at POS. */
2899
2900 static int
2901 init_from_display_pos (it, w, pos)
2902 struct it *it;
2903 struct window *w;
2904 struct display_pos *pos;
2905 {
2906 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2907 int i, overlay_strings_with_newlines = 0;
2908
2909 /* If POS specifies a position in a display vector, this might
2910 be for an ellipsis displayed for invisible text. We won't
2911 get the iterator set up for delivering that ellipsis unless
2912 we make sure that it gets aware of the invisible text. */
2913 if (in_ellipses_for_invisible_text_p (pos, w))
2914 {
2915 --charpos;
2916 bytepos = 0;
2917 }
2918
2919 /* Keep in mind: the call to reseat in init_iterator skips invisible
2920 text, so we might end up at a position different from POS. This
2921 is only a problem when POS is a row start after a newline and an
2922 overlay starts there with an after-string, and the overlay has an
2923 invisible property. Since we don't skip invisible text in
2924 display_line and elsewhere immediately after consuming the
2925 newline before the row start, such a POS will not be in a string,
2926 but the call to init_iterator below will move us to the
2927 after-string. */
2928 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2929
2930 /* This only scans the current chunk -- it should scan all chunks.
2931 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2932 to 16 in 22.1 to make this a lesser problem. */
2933 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2934 {
2935 const char *s = SDATA (it->overlay_strings[i]);
2936 const char *e = s + SBYTES (it->overlay_strings[i]);
2937
2938 while (s < e && *s != '\n')
2939 ++s;
2940
2941 if (s < e)
2942 {
2943 overlay_strings_with_newlines = 1;
2944 break;
2945 }
2946 }
2947
2948 /* If position is within an overlay string, set up IT to the right
2949 overlay string. */
2950 if (pos->overlay_string_index >= 0)
2951 {
2952 int relative_index;
2953
2954 /* If the first overlay string happens to have a `display'
2955 property for an image, the iterator will be set up for that
2956 image, and we have to undo that setup first before we can
2957 correct the overlay string index. */
2958 if (it->method == GET_FROM_IMAGE)
2959 pop_it (it);
2960
2961 /* We already have the first chunk of overlay strings in
2962 IT->overlay_strings. Load more until the one for
2963 pos->overlay_string_index is in IT->overlay_strings. */
2964 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2965 {
2966 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2967 it->current.overlay_string_index = 0;
2968 while (n--)
2969 {
2970 load_overlay_strings (it, 0);
2971 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2972 }
2973 }
2974
2975 it->current.overlay_string_index = pos->overlay_string_index;
2976 relative_index = (it->current.overlay_string_index
2977 % OVERLAY_STRING_CHUNK_SIZE);
2978 it->string = it->overlay_strings[relative_index];
2979 xassert (STRINGP (it->string));
2980 it->current.string_pos = pos->string_pos;
2981 it->method = GET_FROM_STRING;
2982 }
2983
2984 #if 0 /* This is bogus because POS not having an overlay string
2985 position does not mean it's after the string. Example: A
2986 line starting with a before-string and initialization of IT
2987 to the previous row's end position. */
2988 else if (it->current.overlay_string_index >= 0)
2989 {
2990 /* If POS says we're already after an overlay string ending at
2991 POS, make sure to pop the iterator because it will be in
2992 front of that overlay string. When POS is ZV, we've thereby
2993 also ``processed'' overlay strings at ZV. */
2994 while (it->sp)
2995 pop_it (it);
2996 xassert (it->current.overlay_string_index == -1);
2997 xassert (it->method == GET_FROM_BUFFER);
2998 if (CHARPOS (pos->pos) == ZV)
2999 it->overlay_strings_at_end_processed_p = 1;
3000 }
3001 #endif /* 0 */
3002
3003 if (CHARPOS (pos->string_pos) >= 0)
3004 {
3005 /* Recorded position is not in an overlay string, but in another
3006 string. This can only be a string from a `display' property.
3007 IT should already be filled with that string. */
3008 it->current.string_pos = pos->string_pos;
3009 xassert (STRINGP (it->string));
3010 }
3011
3012 /* Restore position in display vector translations, control
3013 character translations or ellipses. */
3014 if (pos->dpvec_index >= 0)
3015 {
3016 if (it->dpvec == NULL)
3017 get_next_display_element (it);
3018 xassert (it->dpvec && it->current.dpvec_index == 0);
3019 it->current.dpvec_index = pos->dpvec_index;
3020 }
3021
3022 CHECK_IT (it);
3023 return !overlay_strings_with_newlines;
3024 }
3025
3026
3027 /* Initialize IT for stepping through current_buffer in window W
3028 starting at ROW->start. */
3029
3030 static void
3031 init_to_row_start (it, w, row)
3032 struct it *it;
3033 struct window *w;
3034 struct glyph_row *row;
3035 {
3036 init_from_display_pos (it, w, &row->start);
3037 it->start = row->start;
3038 it->continuation_lines_width = row->continuation_lines_width;
3039 CHECK_IT (it);
3040 }
3041
3042
3043 /* Initialize IT for stepping through current_buffer in window W
3044 starting in the line following ROW, i.e. starting at ROW->end.
3045 Value is zero if there are overlay strings with newlines at ROW's
3046 end position. */
3047
3048 static int
3049 init_to_row_end (it, w, row)
3050 struct it *it;
3051 struct window *w;
3052 struct glyph_row *row;
3053 {
3054 int success = 0;
3055
3056 if (init_from_display_pos (it, w, &row->end))
3057 {
3058 if (row->continued_p)
3059 it->continuation_lines_width
3060 = row->continuation_lines_width + row->pixel_width;
3061 CHECK_IT (it);
3062 success = 1;
3063 }
3064
3065 return success;
3066 }
3067
3068
3069
3070 \f
3071 /***********************************************************************
3072 Text properties
3073 ***********************************************************************/
3074
3075 /* Called when IT reaches IT->stop_charpos. Handle text property and
3076 overlay changes. Set IT->stop_charpos to the next position where
3077 to stop. */
3078
3079 static void
3080 handle_stop (it)
3081 struct it *it;
3082 {
3083 enum prop_handled handled;
3084 int handle_overlay_change_p;
3085 struct props *p;
3086
3087 it->dpvec = NULL;
3088 it->current.dpvec_index = -1;
3089 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3090 it->ignore_overlay_strings_at_pos_p = 0;
3091
3092 /* Use face of preceding text for ellipsis (if invisible) */
3093 if (it->selective_display_ellipsis_p)
3094 it->saved_face_id = it->face_id;
3095
3096 do
3097 {
3098 handled = HANDLED_NORMALLY;
3099
3100 /* Call text property handlers. */
3101 for (p = it_props; p->handler; ++p)
3102 {
3103 handled = p->handler (it);
3104
3105 if (handled == HANDLED_RECOMPUTE_PROPS)
3106 break;
3107 else if (handled == HANDLED_RETURN)
3108 {
3109 /* We still want to show before and after strings from
3110 overlays even if the actual buffer text is replaced. */
3111 if (!handle_overlay_change_p || it->sp > 1)
3112 return;
3113 if (!get_overlay_strings_1 (it, 0, 0))
3114 return;
3115 it->ignore_overlay_strings_at_pos_p = 1;
3116 it->string_from_display_prop_p = 0;
3117 handle_overlay_change_p = 0;
3118 handled = HANDLED_RECOMPUTE_PROPS;
3119 break;
3120 }
3121 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3122 handle_overlay_change_p = 0;
3123 }
3124
3125 if (handled != HANDLED_RECOMPUTE_PROPS)
3126 {
3127 /* Don't check for overlay strings below when set to deliver
3128 characters from a display vector. */
3129 if (it->method == GET_FROM_DISPLAY_VECTOR)
3130 handle_overlay_change_p = 0;
3131
3132 /* Handle overlay changes.
3133 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3134 if it finds overlays. */
3135 if (handle_overlay_change_p)
3136 handled = handle_overlay_change (it);
3137 }
3138 }
3139 while (handled == HANDLED_RECOMPUTE_PROPS);
3140
3141 /* Determine where to stop next. */
3142 if (handled == HANDLED_NORMALLY)
3143 compute_stop_pos (it);
3144 }
3145
3146
3147 /* Compute IT->stop_charpos from text property and overlay change
3148 information for IT's current position. */
3149
3150 static void
3151 compute_stop_pos (it)
3152 struct it *it;
3153 {
3154 register INTERVAL iv, next_iv;
3155 Lisp_Object object, limit, position;
3156
3157 /* If nowhere else, stop at the end. */
3158 it->stop_charpos = it->end_charpos;
3159
3160 if (STRINGP (it->string))
3161 {
3162 /* Strings are usually short, so don't limit the search for
3163 properties. */
3164 object = it->string;
3165 limit = Qnil;
3166 position = make_number (IT_STRING_CHARPOS (*it));
3167 }
3168 else
3169 {
3170 int charpos;
3171
3172 /* If next overlay change is in front of the current stop pos
3173 (which is IT->end_charpos), stop there. Note: value of
3174 next_overlay_change is point-max if no overlay change
3175 follows. */
3176 charpos = next_overlay_change (IT_CHARPOS (*it));
3177 if (charpos < it->stop_charpos)
3178 it->stop_charpos = charpos;
3179
3180 /* If showing the region, we have to stop at the region
3181 start or end because the face might change there. */
3182 if (it->region_beg_charpos > 0)
3183 {
3184 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3185 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3186 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3187 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3188 }
3189
3190 /* Set up variables for computing the stop position from text
3191 property changes. */
3192 XSETBUFFER (object, current_buffer);
3193 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3194 position = make_number (IT_CHARPOS (*it));
3195
3196 }
3197
3198 /* Get the interval containing IT's position. Value is a null
3199 interval if there isn't such an interval. */
3200 iv = validate_interval_range (object, &position, &position, 0);
3201 if (!NULL_INTERVAL_P (iv))
3202 {
3203 Lisp_Object values_here[LAST_PROP_IDX];
3204 struct props *p;
3205
3206 /* Get properties here. */
3207 for (p = it_props; p->handler; ++p)
3208 values_here[p->idx] = textget (iv->plist, *p->name);
3209
3210 /* Look for an interval following iv that has different
3211 properties. */
3212 for (next_iv = next_interval (iv);
3213 (!NULL_INTERVAL_P (next_iv)
3214 && (NILP (limit)
3215 || XFASTINT (limit) > next_iv->position));
3216 next_iv = next_interval (next_iv))
3217 {
3218 for (p = it_props; p->handler; ++p)
3219 {
3220 Lisp_Object new_value;
3221
3222 new_value = textget (next_iv->plist, *p->name);
3223 if (!EQ (values_here[p->idx], new_value))
3224 break;
3225 }
3226
3227 if (p->handler)
3228 break;
3229 }
3230
3231 if (!NULL_INTERVAL_P (next_iv))
3232 {
3233 if (INTEGERP (limit)
3234 && next_iv->position >= XFASTINT (limit))
3235 /* No text property change up to limit. */
3236 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3237 else
3238 /* Text properties change in next_iv. */
3239 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3240 }
3241 }
3242
3243 xassert (STRINGP (it->string)
3244 || (it->stop_charpos >= BEGV
3245 && it->stop_charpos >= IT_CHARPOS (*it)));
3246 }
3247
3248
3249 /* Return the position of the next overlay change after POS in
3250 current_buffer. Value is point-max if no overlay change
3251 follows. This is like `next-overlay-change' but doesn't use
3252 xmalloc. */
3253
3254 static EMACS_INT
3255 next_overlay_change (pos)
3256 EMACS_INT pos;
3257 {
3258 int noverlays;
3259 EMACS_INT endpos;
3260 Lisp_Object *overlays;
3261 int i;
3262
3263 /* Get all overlays at the given position. */
3264 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3265
3266 /* If any of these overlays ends before endpos,
3267 use its ending point instead. */
3268 for (i = 0; i < noverlays; ++i)
3269 {
3270 Lisp_Object oend;
3271 EMACS_INT oendpos;
3272
3273 oend = OVERLAY_END (overlays[i]);
3274 oendpos = OVERLAY_POSITION (oend);
3275 endpos = min (endpos, oendpos);
3276 }
3277
3278 return endpos;
3279 }
3280
3281
3282 \f
3283 /***********************************************************************
3284 Fontification
3285 ***********************************************************************/
3286
3287 /* Handle changes in the `fontified' property of the current buffer by
3288 calling hook functions from Qfontification_functions to fontify
3289 regions of text. */
3290
3291 static enum prop_handled
3292 handle_fontified_prop (it)
3293 struct it *it;
3294 {
3295 Lisp_Object prop, pos;
3296 enum prop_handled handled = HANDLED_NORMALLY;
3297
3298 if (!NILP (Vmemory_full))
3299 return handled;
3300
3301 /* Get the value of the `fontified' property at IT's current buffer
3302 position. (The `fontified' property doesn't have a special
3303 meaning in strings.) If the value is nil, call functions from
3304 Qfontification_functions. */
3305 if (!STRINGP (it->string)
3306 && it->s == NULL
3307 && !NILP (Vfontification_functions)
3308 && !NILP (Vrun_hooks)
3309 && (pos = make_number (IT_CHARPOS (*it)),
3310 prop = Fget_char_property (pos, Qfontified, Qnil),
3311 /* Ignore the special cased nil value always present at EOB since
3312 no amount of fontifying will be able to change it. */
3313 NILP (prop) && IT_CHARPOS (*it) < Z))
3314 {
3315 int count = SPECPDL_INDEX ();
3316 Lisp_Object val;
3317
3318 val = Vfontification_functions;
3319 specbind (Qfontification_functions, Qnil);
3320
3321 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3322 safe_call1 (val, pos);
3323 else
3324 {
3325 Lisp_Object globals, fn;
3326 struct gcpro gcpro1, gcpro2;
3327
3328 globals = Qnil;
3329 GCPRO2 (val, globals);
3330
3331 for (; CONSP (val); val = XCDR (val))
3332 {
3333 fn = XCAR (val);
3334
3335 if (EQ (fn, Qt))
3336 {
3337 /* A value of t indicates this hook has a local
3338 binding; it means to run the global binding too.
3339 In a global value, t should not occur. If it
3340 does, we must ignore it to avoid an endless
3341 loop. */
3342 for (globals = Fdefault_value (Qfontification_functions);
3343 CONSP (globals);
3344 globals = XCDR (globals))
3345 {
3346 fn = XCAR (globals);
3347 if (!EQ (fn, Qt))
3348 safe_call1 (fn, pos);
3349 }
3350 }
3351 else
3352 safe_call1 (fn, pos);
3353 }
3354
3355 UNGCPRO;
3356 }
3357
3358 unbind_to (count, Qnil);
3359
3360 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3361 something. This avoids an endless loop if they failed to
3362 fontify the text for which reason ever. */
3363 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3364 handled = HANDLED_RECOMPUTE_PROPS;
3365 }
3366
3367 return handled;
3368 }
3369
3370
3371 \f
3372 /***********************************************************************
3373 Faces
3374 ***********************************************************************/
3375
3376 /* Set up iterator IT from face properties at its current position.
3377 Called from handle_stop. */
3378
3379 static enum prop_handled
3380 handle_face_prop (it)
3381 struct it *it;
3382 {
3383 int new_face_id;
3384 EMACS_INT next_stop;
3385
3386 if (!STRINGP (it->string))
3387 {
3388 new_face_id
3389 = face_at_buffer_position (it->w,
3390 IT_CHARPOS (*it),
3391 it->region_beg_charpos,
3392 it->region_end_charpos,
3393 &next_stop,
3394 (IT_CHARPOS (*it)
3395 + TEXT_PROP_DISTANCE_LIMIT),
3396 0);
3397
3398 /* Is this a start of a run of characters with box face?
3399 Caveat: this can be called for a freshly initialized
3400 iterator; face_id is -1 in this case. We know that the new
3401 face will not change until limit, i.e. if the new face has a
3402 box, all characters up to limit will have one. But, as
3403 usual, we don't know whether limit is really the end. */
3404 if (new_face_id != it->face_id)
3405 {
3406 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3407
3408 /* If new face has a box but old face has not, this is
3409 the start of a run of characters with box, i.e. it has
3410 a shadow on the left side. The value of face_id of the
3411 iterator will be -1 if this is the initial call that gets
3412 the face. In this case, we have to look in front of IT's
3413 position and see whether there is a face != new_face_id. */
3414 it->start_of_box_run_p
3415 = (new_face->box != FACE_NO_BOX
3416 && (it->face_id >= 0
3417 || IT_CHARPOS (*it) == BEG
3418 || new_face_id != face_before_it_pos (it)));
3419 it->face_box_p = new_face->box != FACE_NO_BOX;
3420 }
3421 }
3422 else
3423 {
3424 int base_face_id, bufpos;
3425 int i;
3426 Lisp_Object from_overlay
3427 = (it->current.overlay_string_index >= 0
3428 ? it->string_overlays[it->current.overlay_string_index]
3429 : Qnil);
3430
3431 /* See if we got to this string directly or indirectly from
3432 an overlay property. That includes the before-string or
3433 after-string of an overlay, strings in display properties
3434 provided by an overlay, their text properties, etc.
3435
3436 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3437 if (! NILP (from_overlay))
3438 for (i = it->sp - 1; i >= 0; i--)
3439 {
3440 if (it->stack[i].current.overlay_string_index >= 0)
3441 from_overlay
3442 = it->string_overlays[it->stack[i].current.overlay_string_index];
3443 else if (! NILP (it->stack[i].from_overlay))
3444 from_overlay = it->stack[i].from_overlay;
3445
3446 if (!NILP (from_overlay))
3447 break;
3448 }
3449
3450 if (! NILP (from_overlay))
3451 {
3452 bufpos = IT_CHARPOS (*it);
3453 /* For a string from an overlay, the base face depends
3454 only on text properties and ignores overlays. */
3455 base_face_id
3456 = face_for_overlay_string (it->w,
3457 IT_CHARPOS (*it),
3458 it->region_beg_charpos,
3459 it->region_end_charpos,
3460 &next_stop,
3461 (IT_CHARPOS (*it)
3462 + TEXT_PROP_DISTANCE_LIMIT),
3463 0,
3464 from_overlay);
3465 }
3466 else
3467 {
3468 bufpos = 0;
3469
3470 /* For strings from a `display' property, use the face at
3471 IT's current buffer position as the base face to merge
3472 with, so that overlay strings appear in the same face as
3473 surrounding text, unless they specify their own
3474 faces. */
3475 base_face_id = underlying_face_id (it);
3476 }
3477
3478 new_face_id = face_at_string_position (it->w,
3479 it->string,
3480 IT_STRING_CHARPOS (*it),
3481 bufpos,
3482 it->region_beg_charpos,
3483 it->region_end_charpos,
3484 &next_stop,
3485 base_face_id, 0);
3486
3487 #if 0 /* This shouldn't be neccessary. Let's check it. */
3488 /* If IT is used to display a mode line we would really like to
3489 use the mode line face instead of the frame's default face. */
3490 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3491 && new_face_id == DEFAULT_FACE_ID)
3492 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3493 #endif
3494
3495 /* Is this a start of a run of characters with box? Caveat:
3496 this can be called for a freshly allocated iterator; face_id
3497 is -1 is this case. We know that the new face will not
3498 change until the next check pos, i.e. if the new face has a
3499 box, all characters up to that position will have a
3500 box. But, as usual, we don't know whether that position
3501 is really the end. */
3502 if (new_face_id != it->face_id)
3503 {
3504 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3505 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3506
3507 /* If new face has a box but old face hasn't, this is the
3508 start of a run of characters with box, i.e. it has a
3509 shadow on the left side. */
3510 it->start_of_box_run_p
3511 = new_face->box && (old_face == NULL || !old_face->box);
3512 it->face_box_p = new_face->box != FACE_NO_BOX;
3513 }
3514 }
3515
3516 it->face_id = new_face_id;
3517 return HANDLED_NORMALLY;
3518 }
3519
3520
3521 /* Return the ID of the face ``underlying'' IT's current position,
3522 which is in a string. If the iterator is associated with a
3523 buffer, return the face at IT's current buffer position.
3524 Otherwise, use the iterator's base_face_id. */
3525
3526 static int
3527 underlying_face_id (it)
3528 struct it *it;
3529 {
3530 int face_id = it->base_face_id, i;
3531
3532 xassert (STRINGP (it->string));
3533
3534 for (i = it->sp - 1; i >= 0; --i)
3535 if (NILP (it->stack[i].string))
3536 face_id = it->stack[i].face_id;
3537
3538 return face_id;
3539 }
3540
3541
3542 /* Compute the face one character before or after the current position
3543 of IT. BEFORE_P non-zero means get the face in front of IT's
3544 position. Value is the id of the face. */
3545
3546 static int
3547 face_before_or_after_it_pos (it, before_p)
3548 struct it *it;
3549 int before_p;
3550 {
3551 int face_id, limit;
3552 EMACS_INT next_check_charpos;
3553 struct text_pos pos;
3554
3555 xassert (it->s == NULL);
3556
3557 if (STRINGP (it->string))
3558 {
3559 int bufpos, base_face_id;
3560
3561 /* No face change past the end of the string (for the case
3562 we are padding with spaces). No face change before the
3563 string start. */
3564 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3565 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3566 return it->face_id;
3567
3568 /* Set pos to the position before or after IT's current position. */
3569 if (before_p)
3570 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3571 else
3572 /* For composition, we must check the character after the
3573 composition. */
3574 pos = (it->what == IT_COMPOSITION
3575 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3576 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3577
3578 if (it->current.overlay_string_index >= 0)
3579 bufpos = IT_CHARPOS (*it);
3580 else
3581 bufpos = 0;
3582
3583 base_face_id = underlying_face_id (it);
3584
3585 /* Get the face for ASCII, or unibyte. */
3586 face_id = face_at_string_position (it->w,
3587 it->string,
3588 CHARPOS (pos),
3589 bufpos,
3590 it->region_beg_charpos,
3591 it->region_end_charpos,
3592 &next_check_charpos,
3593 base_face_id, 0);
3594
3595 /* Correct the face for charsets different from ASCII. Do it
3596 for the multibyte case only. The face returned above is
3597 suitable for unibyte text if IT->string is unibyte. */
3598 if (STRING_MULTIBYTE (it->string))
3599 {
3600 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3601 int rest = SBYTES (it->string) - BYTEPOS (pos);
3602 int c, len;
3603 struct face *face = FACE_FROM_ID (it->f, face_id);
3604
3605 c = string_char_and_length (p, rest, &len);
3606 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), it->string);
3607 }
3608 }
3609 else
3610 {
3611 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3612 || (IT_CHARPOS (*it) <= BEGV && before_p))
3613 return it->face_id;
3614
3615 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3616 pos = it->current.pos;
3617
3618 if (before_p)
3619 DEC_TEXT_POS (pos, it->multibyte_p);
3620 else
3621 {
3622 if (it->what == IT_COMPOSITION)
3623 /* For composition, we must check the position after the
3624 composition. */
3625 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3626 else
3627 INC_TEXT_POS (pos, it->multibyte_p);
3628 }
3629
3630 /* Determine face for CHARSET_ASCII, or unibyte. */
3631 face_id = face_at_buffer_position (it->w,
3632 CHARPOS (pos),
3633 it->region_beg_charpos,
3634 it->region_end_charpos,
3635 &next_check_charpos,
3636 limit, 0);
3637
3638 /* Correct the face for charsets different from ASCII. Do it
3639 for the multibyte case only. The face returned above is
3640 suitable for unibyte text if current_buffer is unibyte. */
3641 if (it->multibyte_p)
3642 {
3643 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3644 struct face *face = FACE_FROM_ID (it->f, face_id);
3645 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3646 }
3647 }
3648
3649 return face_id;
3650 }
3651
3652
3653 \f
3654 /***********************************************************************
3655 Invisible text
3656 ***********************************************************************/
3657
3658 /* Set up iterator IT from invisible properties at its current
3659 position. Called from handle_stop. */
3660
3661 static enum prop_handled
3662 handle_invisible_prop (it)
3663 struct it *it;
3664 {
3665 enum prop_handled handled = HANDLED_NORMALLY;
3666
3667 if (STRINGP (it->string))
3668 {
3669 extern Lisp_Object Qinvisible;
3670 Lisp_Object prop, end_charpos, limit, charpos;
3671
3672 /* Get the value of the invisible text property at the
3673 current position. Value will be nil if there is no such
3674 property. */
3675 charpos = make_number (IT_STRING_CHARPOS (*it));
3676 prop = Fget_text_property (charpos, Qinvisible, it->string);
3677
3678 if (!NILP (prop)
3679 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3680 {
3681 handled = HANDLED_RECOMPUTE_PROPS;
3682
3683 /* Get the position at which the next change of the
3684 invisible text property can be found in IT->string.
3685 Value will be nil if the property value is the same for
3686 all the rest of IT->string. */
3687 XSETINT (limit, SCHARS (it->string));
3688 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3689 it->string, limit);
3690
3691 /* Text at current position is invisible. The next
3692 change in the property is at position end_charpos.
3693 Move IT's current position to that position. */
3694 if (INTEGERP (end_charpos)
3695 && XFASTINT (end_charpos) < XFASTINT (limit))
3696 {
3697 struct text_pos old;
3698 old = it->current.string_pos;
3699 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3700 compute_string_pos (&it->current.string_pos, old, it->string);
3701 }
3702 else
3703 {
3704 /* The rest of the string is invisible. If this is an
3705 overlay string, proceed with the next overlay string
3706 or whatever comes and return a character from there. */
3707 if (it->current.overlay_string_index >= 0)
3708 {
3709 next_overlay_string (it);
3710 /* Don't check for overlay strings when we just
3711 finished processing them. */
3712 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3713 }
3714 else
3715 {
3716 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3717 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3718 }
3719 }
3720 }
3721 }
3722 else
3723 {
3724 int invis_p;
3725 EMACS_INT newpos, next_stop, start_charpos;
3726 Lisp_Object pos, prop, overlay;
3727
3728 /* First of all, is there invisible text at this position? */
3729 start_charpos = IT_CHARPOS (*it);
3730 pos = make_number (IT_CHARPOS (*it));
3731 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3732 &overlay);
3733 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3734
3735 /* If we are on invisible text, skip over it. */
3736 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3737 {
3738 /* Record whether we have to display an ellipsis for the
3739 invisible text. */
3740 int display_ellipsis_p = invis_p == 2;
3741
3742 handled = HANDLED_RECOMPUTE_PROPS;
3743
3744 /* Loop skipping over invisible text. The loop is left at
3745 ZV or with IT on the first char being visible again. */
3746 do
3747 {
3748 /* Try to skip some invisible text. Return value is the
3749 position reached which can be equal to IT's position
3750 if there is nothing invisible here. This skips both
3751 over invisible text properties and overlays with
3752 invisible property. */
3753 newpos = skip_invisible (IT_CHARPOS (*it),
3754 &next_stop, ZV, it->window);
3755
3756 /* If we skipped nothing at all we weren't at invisible
3757 text in the first place. If everything to the end of
3758 the buffer was skipped, end the loop. */
3759 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3760 invis_p = 0;
3761 else
3762 {
3763 /* We skipped some characters but not necessarily
3764 all there are. Check if we ended up on visible
3765 text. Fget_char_property returns the property of
3766 the char before the given position, i.e. if we
3767 get invis_p = 0, this means that the char at
3768 newpos is visible. */
3769 pos = make_number (newpos);
3770 prop = Fget_char_property (pos, Qinvisible, it->window);
3771 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3772 }
3773
3774 /* If we ended up on invisible text, proceed to
3775 skip starting with next_stop. */
3776 if (invis_p)
3777 IT_CHARPOS (*it) = next_stop;
3778
3779 /* If there are adjacent invisible texts, don't lose the
3780 second one's ellipsis. */
3781 if (invis_p == 2)
3782 display_ellipsis_p = 1;
3783 }
3784 while (invis_p);
3785
3786 /* The position newpos is now either ZV or on visible text. */
3787 IT_CHARPOS (*it) = newpos;
3788 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3789
3790 /* If there are before-strings at the start of invisible
3791 text, and the text is invisible because of a text
3792 property, arrange to show before-strings because 20.x did
3793 it that way. (If the text is invisible because of an
3794 overlay property instead of a text property, this is
3795 already handled in the overlay code.) */
3796 if (NILP (overlay)
3797 && get_overlay_strings (it, start_charpos))
3798 {
3799 handled = HANDLED_RECOMPUTE_PROPS;
3800 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3801 }
3802 else if (display_ellipsis_p)
3803 {
3804 /* Make sure that the glyphs of the ellipsis will get
3805 correct `charpos' values. If we would not update
3806 it->position here, the glyphs would belong to the
3807 last visible character _before_ the invisible
3808 text, which confuses `set_cursor_from_row'.
3809
3810 We use the last invisible position instead of the
3811 first because this way the cursor is always drawn on
3812 the first "." of the ellipsis, whenever PT is inside
3813 the invisible text. Otherwise the cursor would be
3814 placed _after_ the ellipsis when the point is after the
3815 first invisible character. */
3816 if (!STRINGP (it->object))
3817 {
3818 it->position.charpos = IT_CHARPOS (*it) - 1;
3819 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3820 }
3821 setup_for_ellipsis (it, 0);
3822 /* Let the ellipsis display before
3823 considering any properties of the following char.
3824 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3825 handled = HANDLED_RETURN;
3826 }
3827 }
3828 }
3829
3830 return handled;
3831 }
3832
3833
3834 /* Make iterator IT return `...' next.
3835 Replaces LEN characters from buffer. */
3836
3837 static void
3838 setup_for_ellipsis (it, len)
3839 struct it *it;
3840 int len;
3841 {
3842 /* Use the display table definition for `...'. Invalid glyphs
3843 will be handled by the method returning elements from dpvec. */
3844 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3845 {
3846 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3847 it->dpvec = v->contents;
3848 it->dpend = v->contents + v->size;
3849 }
3850 else
3851 {
3852 /* Default `...'. */
3853 it->dpvec = default_invis_vector;
3854 it->dpend = default_invis_vector + 3;
3855 }
3856
3857 it->dpvec_char_len = len;
3858 it->current.dpvec_index = 0;
3859 it->dpvec_face_id = -1;
3860
3861 /* Remember the current face id in case glyphs specify faces.
3862 IT's face is restored in set_iterator_to_next.
3863 saved_face_id was set to preceding char's face in handle_stop. */
3864 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3865 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3866
3867 it->method = GET_FROM_DISPLAY_VECTOR;
3868 it->ellipsis_p = 1;
3869 }
3870
3871
3872 \f
3873 /***********************************************************************
3874 'display' property
3875 ***********************************************************************/
3876
3877 /* Set up iterator IT from `display' property at its current position.
3878 Called from handle_stop.
3879 We return HANDLED_RETURN if some part of the display property
3880 overrides the display of the buffer text itself.
3881 Otherwise we return HANDLED_NORMALLY. */
3882
3883 static enum prop_handled
3884 handle_display_prop (it)
3885 struct it *it;
3886 {
3887 Lisp_Object prop, object, overlay;
3888 struct text_pos *position;
3889 /* Nonzero if some property replaces the display of the text itself. */
3890 int display_replaced_p = 0;
3891
3892 if (STRINGP (it->string))
3893 {
3894 object = it->string;
3895 position = &it->current.string_pos;
3896 }
3897 else
3898 {
3899 XSETWINDOW (object, it->w);
3900 position = &it->current.pos;
3901 }
3902
3903 /* Reset those iterator values set from display property values. */
3904 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3905 it->space_width = Qnil;
3906 it->font_height = Qnil;
3907 it->voffset = 0;
3908
3909 /* We don't support recursive `display' properties, i.e. string
3910 values that have a string `display' property, that have a string
3911 `display' property etc. */
3912 if (!it->string_from_display_prop_p)
3913 it->area = TEXT_AREA;
3914
3915 prop = get_char_property_and_overlay (make_number (position->charpos),
3916 Qdisplay, object, &overlay);
3917 if (NILP (prop))
3918 return HANDLED_NORMALLY;
3919 /* Now OVERLAY is the overlay that gave us this property, or nil
3920 if it was a text property. */
3921
3922 if (!STRINGP (it->string))
3923 object = it->w->buffer;
3924
3925 if (CONSP (prop)
3926 /* Simple properties. */
3927 && !EQ (XCAR (prop), Qimage)
3928 && !EQ (XCAR (prop), Qspace)
3929 && !EQ (XCAR (prop), Qwhen)
3930 && !EQ (XCAR (prop), Qslice)
3931 && !EQ (XCAR (prop), Qspace_width)
3932 && !EQ (XCAR (prop), Qheight)
3933 && !EQ (XCAR (prop), Qraise)
3934 /* Marginal area specifications. */
3935 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3936 && !EQ (XCAR (prop), Qleft_fringe)
3937 && !EQ (XCAR (prop), Qright_fringe)
3938 && !NILP (XCAR (prop)))
3939 {
3940 for (; CONSP (prop); prop = XCDR (prop))
3941 {
3942 if (handle_single_display_spec (it, XCAR (prop), object, overlay,
3943 position, display_replaced_p))
3944 {
3945 display_replaced_p = 1;
3946 /* If some text in a string is replaced, `position' no
3947 longer points to the position of `object'. */
3948 if (STRINGP (object))
3949 break;
3950 }
3951 }
3952 }
3953 else if (VECTORP (prop))
3954 {
3955 int i;
3956 for (i = 0; i < ASIZE (prop); ++i)
3957 if (handle_single_display_spec (it, AREF (prop, i), object, overlay,
3958 position, display_replaced_p))
3959 {
3960 display_replaced_p = 1;
3961 /* If some text in a string is replaced, `position' no
3962 longer points to the position of `object'. */
3963 if (STRINGP (object))
3964 break;
3965 }
3966 }
3967 else
3968 {
3969 int ret = handle_single_display_spec (it, prop, object, overlay,
3970 position, 0);
3971 if (ret < 0) /* Replaced by "", i.e. nothing. */
3972 return HANDLED_RECOMPUTE_PROPS;
3973 if (ret)
3974 display_replaced_p = 1;
3975 }
3976
3977 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3978 }
3979
3980
3981 /* Value is the position of the end of the `display' property starting
3982 at START_POS in OBJECT. */
3983
3984 static struct text_pos
3985 display_prop_end (it, object, start_pos)
3986 struct it *it;
3987 Lisp_Object object;
3988 struct text_pos start_pos;
3989 {
3990 Lisp_Object end;
3991 struct text_pos end_pos;
3992
3993 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3994 Qdisplay, object, Qnil);
3995 CHARPOS (end_pos) = XFASTINT (end);
3996 if (STRINGP (object))
3997 compute_string_pos (&end_pos, start_pos, it->string);
3998 else
3999 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4000
4001 return end_pos;
4002 }
4003
4004
4005 /* Set up IT from a single `display' specification PROP. OBJECT
4006 is the object in which the `display' property was found. *POSITION
4007 is the position at which it was found. DISPLAY_REPLACED_P non-zero
4008 means that we previously saw a display specification which already
4009 replaced text display with something else, for example an image;
4010 we ignore such properties after the first one has been processed.
4011
4012 OVERLAY is the overlay this `display' property came from,
4013 or nil if it was a text property.
4014
4015 If PROP is a `space' or `image' specification, and in some other
4016 cases too, set *POSITION to the position where the `display'
4017 property ends.
4018
4019 Value is non-zero if something was found which replaces the display
4020 of buffer or string text. Specifically, the value is -1 if that
4021 "something" is "nothing". */
4022
4023 static int
4024 handle_single_display_spec (it, spec, object, overlay, position,
4025 display_replaced_before_p)
4026 struct it *it;
4027 Lisp_Object spec;
4028 Lisp_Object object;
4029 Lisp_Object overlay;
4030 struct text_pos *position;
4031 int display_replaced_before_p;
4032 {
4033 Lisp_Object form;
4034 Lisp_Object location, value;
4035 struct text_pos start_pos, save_pos;
4036 int valid_p;
4037
4038 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4039 If the result is non-nil, use VALUE instead of SPEC. */
4040 form = Qt;
4041 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4042 {
4043 spec = XCDR (spec);
4044 if (!CONSP (spec))
4045 return 0;
4046 form = XCAR (spec);
4047 spec = XCDR (spec);
4048 }
4049
4050 if (!NILP (form) && !EQ (form, Qt))
4051 {
4052 int count = SPECPDL_INDEX ();
4053 struct gcpro gcpro1;
4054
4055 /* Bind `object' to the object having the `display' property, a
4056 buffer or string. Bind `position' to the position in the
4057 object where the property was found, and `buffer-position'
4058 to the current position in the buffer. */
4059 specbind (Qobject, object);
4060 specbind (Qposition, make_number (CHARPOS (*position)));
4061 specbind (Qbuffer_position,
4062 make_number (STRINGP (object)
4063 ? IT_CHARPOS (*it) : CHARPOS (*position)));
4064 GCPRO1 (form);
4065 form = safe_eval (form);
4066 UNGCPRO;
4067 unbind_to (count, Qnil);
4068 }
4069
4070 if (NILP (form))
4071 return 0;
4072
4073 /* Handle `(height HEIGHT)' specifications. */
4074 if (CONSP (spec)
4075 && EQ (XCAR (spec), Qheight)
4076 && CONSP (XCDR (spec)))
4077 {
4078 if (!FRAME_WINDOW_P (it->f))
4079 return 0;
4080
4081 it->font_height = XCAR (XCDR (spec));
4082 if (!NILP (it->font_height))
4083 {
4084 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4085 int new_height = -1;
4086
4087 if (CONSP (it->font_height)
4088 && (EQ (XCAR (it->font_height), Qplus)
4089 || EQ (XCAR (it->font_height), Qminus))
4090 && CONSP (XCDR (it->font_height))
4091 && INTEGERP (XCAR (XCDR (it->font_height))))
4092 {
4093 /* `(+ N)' or `(- N)' where N is an integer. */
4094 int steps = XINT (XCAR (XCDR (it->font_height)));
4095 if (EQ (XCAR (it->font_height), Qplus))
4096 steps = - steps;
4097 it->face_id = smaller_face (it->f, it->face_id, steps);
4098 }
4099 else if (FUNCTIONP (it->font_height))
4100 {
4101 /* Call function with current height as argument.
4102 Value is the new height. */
4103 Lisp_Object height;
4104 height = safe_call1 (it->font_height,
4105 face->lface[LFACE_HEIGHT_INDEX]);
4106 if (NUMBERP (height))
4107 new_height = XFLOATINT (height);
4108 }
4109 else if (NUMBERP (it->font_height))
4110 {
4111 /* Value is a multiple of the canonical char height. */
4112 struct face *face;
4113
4114 face = FACE_FROM_ID (it->f,
4115 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4116 new_height = (XFLOATINT (it->font_height)
4117 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
4118 }
4119 else
4120 {
4121 /* Evaluate IT->font_height with `height' bound to the
4122 current specified height to get the new height. */
4123 int count = SPECPDL_INDEX ();
4124
4125 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4126 value = safe_eval (it->font_height);
4127 unbind_to (count, Qnil);
4128
4129 if (NUMBERP (value))
4130 new_height = XFLOATINT (value);
4131 }
4132
4133 if (new_height > 0)
4134 it->face_id = face_with_height (it->f, it->face_id, new_height);
4135 }
4136
4137 return 0;
4138 }
4139
4140 /* Handle `(space-width WIDTH)'. */
4141 if (CONSP (spec)
4142 && EQ (XCAR (spec), Qspace_width)
4143 && CONSP (XCDR (spec)))
4144 {
4145 if (!FRAME_WINDOW_P (it->f))
4146 return 0;
4147
4148 value = XCAR (XCDR (spec));
4149 if (NUMBERP (value) && XFLOATINT (value) > 0)
4150 it->space_width = value;
4151
4152 return 0;
4153 }
4154
4155 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4156 if (CONSP (spec)
4157 && EQ (XCAR (spec), Qslice))
4158 {
4159 Lisp_Object tem;
4160
4161 if (!FRAME_WINDOW_P (it->f))
4162 return 0;
4163
4164 if (tem = XCDR (spec), CONSP (tem))
4165 {
4166 it->slice.x = XCAR (tem);
4167 if (tem = XCDR (tem), CONSP (tem))
4168 {
4169 it->slice.y = XCAR (tem);
4170 if (tem = XCDR (tem), CONSP (tem))
4171 {
4172 it->slice.width = XCAR (tem);
4173 if (tem = XCDR (tem), CONSP (tem))
4174 it->slice.height = XCAR (tem);
4175 }
4176 }
4177 }
4178
4179 return 0;
4180 }
4181
4182 /* Handle `(raise FACTOR)'. */
4183 if (CONSP (spec)
4184 && EQ (XCAR (spec), Qraise)
4185 && CONSP (XCDR (spec)))
4186 {
4187 if (!FRAME_WINDOW_P (it->f))
4188 return 0;
4189
4190 #ifdef HAVE_WINDOW_SYSTEM
4191 value = XCAR (XCDR (spec));
4192 if (NUMBERP (value))
4193 {
4194 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4195 it->voffset = - (XFLOATINT (value)
4196 * (FONT_HEIGHT (face->font)));
4197 }
4198 #endif /* HAVE_WINDOW_SYSTEM */
4199
4200 return 0;
4201 }
4202
4203 /* Don't handle the other kinds of display specifications
4204 inside a string that we got from a `display' property. */
4205 if (it->string_from_display_prop_p)
4206 return 0;
4207
4208 /* Characters having this form of property are not displayed, so
4209 we have to find the end of the property. */
4210 start_pos = *position;
4211 *position = display_prop_end (it, object, start_pos);
4212 value = Qnil;
4213
4214 /* Stop the scan at that end position--we assume that all
4215 text properties change there. */
4216 it->stop_charpos = position->charpos;
4217
4218 /* Handle `(left-fringe BITMAP [FACE])'
4219 and `(right-fringe BITMAP [FACE])'. */
4220 if (CONSP (spec)
4221 && (EQ (XCAR (spec), Qleft_fringe)
4222 || EQ (XCAR (spec), Qright_fringe))
4223 && CONSP (XCDR (spec)))
4224 {
4225 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
4226 int fringe_bitmap;
4227
4228 if (!FRAME_WINDOW_P (it->f))
4229 /* If we return here, POSITION has been advanced
4230 across the text with this property. */
4231 return 0;
4232
4233 #ifdef HAVE_WINDOW_SYSTEM
4234 value = XCAR (XCDR (spec));
4235 if (!SYMBOLP (value)
4236 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4237 /* If we return here, POSITION has been advanced
4238 across the text with this property. */
4239 return 0;
4240
4241 if (CONSP (XCDR (XCDR (spec))))
4242 {
4243 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4244 int face_id2 = lookup_derived_face (it->f, face_name,
4245 FRINGE_FACE_ID, 0);
4246 if (face_id2 >= 0)
4247 face_id = face_id2;
4248 }
4249
4250 /* Save current settings of IT so that we can restore them
4251 when we are finished with the glyph property value. */
4252
4253 save_pos = it->position;
4254 it->position = *position;
4255 push_it (it);
4256 it->position = save_pos;
4257
4258 it->area = TEXT_AREA;
4259 it->what = IT_IMAGE;
4260 it->image_id = -1; /* no image */
4261 it->position = start_pos;
4262 it->object = NILP (object) ? it->w->buffer : object;
4263 it->method = GET_FROM_IMAGE;
4264 it->from_overlay = Qnil;
4265 it->face_id = face_id;
4266
4267 /* Say that we haven't consumed the characters with
4268 `display' property yet. The call to pop_it in
4269 set_iterator_to_next will clean this up. */
4270 *position = start_pos;
4271
4272 if (EQ (XCAR (spec), Qleft_fringe))
4273 {
4274 it->left_user_fringe_bitmap = fringe_bitmap;
4275 it->left_user_fringe_face_id = face_id;
4276 }
4277 else
4278 {
4279 it->right_user_fringe_bitmap = fringe_bitmap;
4280 it->right_user_fringe_face_id = face_id;
4281 }
4282 #endif /* HAVE_WINDOW_SYSTEM */
4283 return 1;
4284 }
4285
4286 /* Prepare to handle `((margin left-margin) ...)',
4287 `((margin right-margin) ...)' and `((margin nil) ...)'
4288 prefixes for display specifications. */
4289 location = Qunbound;
4290 if (CONSP (spec) && CONSP (XCAR (spec)))
4291 {
4292 Lisp_Object tem;
4293
4294 value = XCDR (spec);
4295 if (CONSP (value))
4296 value = XCAR (value);
4297
4298 tem = XCAR (spec);
4299 if (EQ (XCAR (tem), Qmargin)
4300 && (tem = XCDR (tem),
4301 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4302 (NILP (tem)
4303 || EQ (tem, Qleft_margin)
4304 || EQ (tem, Qright_margin))))
4305 location = tem;
4306 }
4307
4308 if (EQ (location, Qunbound))
4309 {
4310 location = Qnil;
4311 value = spec;
4312 }
4313
4314 /* After this point, VALUE is the property after any
4315 margin prefix has been stripped. It must be a string,
4316 an image specification, or `(space ...)'.
4317
4318 LOCATION specifies where to display: `left-margin',
4319 `right-margin' or nil. */
4320
4321 valid_p = (STRINGP (value)
4322 #ifdef HAVE_WINDOW_SYSTEM
4323 || (FRAME_WINDOW_P (it->f) && valid_image_p (value))
4324 #endif /* not HAVE_WINDOW_SYSTEM */
4325 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4326
4327 if (valid_p && !display_replaced_before_p)
4328 {
4329 /* Save current settings of IT so that we can restore them
4330 when we are finished with the glyph property value. */
4331 save_pos = it->position;
4332 it->position = *position;
4333 push_it (it);
4334 it->position = save_pos;
4335 it->from_overlay = overlay;
4336
4337 if (NILP (location))
4338 it->area = TEXT_AREA;
4339 else if (EQ (location, Qleft_margin))
4340 it->area = LEFT_MARGIN_AREA;
4341 else
4342 it->area = RIGHT_MARGIN_AREA;
4343
4344 if (STRINGP (value))
4345 {
4346 if (SCHARS (value) == 0)
4347 {
4348 pop_it (it);
4349 return -1; /* Replaced by "", i.e. nothing. */
4350 }
4351 it->string = value;
4352 it->multibyte_p = STRING_MULTIBYTE (it->string);
4353 it->current.overlay_string_index = -1;
4354 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4355 it->end_charpos = it->string_nchars = SCHARS (it->string);
4356 it->method = GET_FROM_STRING;
4357 it->stop_charpos = 0;
4358 it->string_from_display_prop_p = 1;
4359 /* Say that we haven't consumed the characters with
4360 `display' property yet. The call to pop_it in
4361 set_iterator_to_next will clean this up. */
4362 if (BUFFERP (object))
4363 *position = start_pos;
4364 }
4365 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4366 {
4367 it->method = GET_FROM_STRETCH;
4368 it->object = value;
4369 *position = it->position = start_pos;
4370 }
4371 #ifdef HAVE_WINDOW_SYSTEM
4372 else
4373 {
4374 it->what = IT_IMAGE;
4375 it->image_id = lookup_image (it->f, value);
4376 it->position = start_pos;
4377 it->object = NILP (object) ? it->w->buffer : object;
4378 it->method = GET_FROM_IMAGE;
4379
4380 /* Say that we haven't consumed the characters with
4381 `display' property yet. The call to pop_it in
4382 set_iterator_to_next will clean this up. */
4383 *position = start_pos;
4384 }
4385 #endif /* HAVE_WINDOW_SYSTEM */
4386
4387 return 1;
4388 }
4389
4390 /* Invalid property or property not supported. Restore
4391 POSITION to what it was before. */
4392 *position = start_pos;
4393 return 0;
4394 }
4395
4396
4397 /* Check if SPEC is a display sub-property value whose text should be
4398 treated as intangible. */
4399
4400 static int
4401 single_display_spec_intangible_p (prop)
4402 Lisp_Object prop;
4403 {
4404 /* Skip over `when FORM'. */
4405 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4406 {
4407 prop = XCDR (prop);
4408 if (!CONSP (prop))
4409 return 0;
4410 prop = XCDR (prop);
4411 }
4412
4413 if (STRINGP (prop))
4414 return 1;
4415
4416 if (!CONSP (prop))
4417 return 0;
4418
4419 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4420 we don't need to treat text as intangible. */
4421 if (EQ (XCAR (prop), Qmargin))
4422 {
4423 prop = XCDR (prop);
4424 if (!CONSP (prop))
4425 return 0;
4426
4427 prop = XCDR (prop);
4428 if (!CONSP (prop)
4429 || EQ (XCAR (prop), Qleft_margin)
4430 || EQ (XCAR (prop), Qright_margin))
4431 return 0;
4432 }
4433
4434 return (CONSP (prop)
4435 && (EQ (XCAR (prop), Qimage)
4436 || EQ (XCAR (prop), Qspace)));
4437 }
4438
4439
4440 /* Check if PROP is a display property value whose text should be
4441 treated as intangible. */
4442
4443 int
4444 display_prop_intangible_p (prop)
4445 Lisp_Object prop;
4446 {
4447 if (CONSP (prop)
4448 && CONSP (XCAR (prop))
4449 && !EQ (Qmargin, XCAR (XCAR (prop))))
4450 {
4451 /* A list of sub-properties. */
4452 while (CONSP (prop))
4453 {
4454 if (single_display_spec_intangible_p (XCAR (prop)))
4455 return 1;
4456 prop = XCDR (prop);
4457 }
4458 }
4459 else if (VECTORP (prop))
4460 {
4461 /* A vector of sub-properties. */
4462 int i;
4463 for (i = 0; i < ASIZE (prop); ++i)
4464 if (single_display_spec_intangible_p (AREF (prop, i)))
4465 return 1;
4466 }
4467 else
4468 return single_display_spec_intangible_p (prop);
4469
4470 return 0;
4471 }
4472
4473
4474 /* Return 1 if PROP is a display sub-property value containing STRING. */
4475
4476 static int
4477 single_display_spec_string_p (prop, string)
4478 Lisp_Object prop, string;
4479 {
4480 if (EQ (string, prop))
4481 return 1;
4482
4483 /* Skip over `when FORM'. */
4484 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4485 {
4486 prop = XCDR (prop);
4487 if (!CONSP (prop))
4488 return 0;
4489 prop = XCDR (prop);
4490 }
4491
4492 if (CONSP (prop))
4493 /* Skip over `margin LOCATION'. */
4494 if (EQ (XCAR (prop), Qmargin))
4495 {
4496 prop = XCDR (prop);
4497 if (!CONSP (prop))
4498 return 0;
4499
4500 prop = XCDR (prop);
4501 if (!CONSP (prop))
4502 return 0;
4503 }
4504
4505 return CONSP (prop) && EQ (XCAR (prop), string);
4506 }
4507
4508
4509 /* Return 1 if STRING appears in the `display' property PROP. */
4510
4511 static int
4512 display_prop_string_p (prop, string)
4513 Lisp_Object prop, string;
4514 {
4515 if (CONSP (prop)
4516 && CONSP (XCAR (prop))
4517 && !EQ (Qmargin, XCAR (XCAR (prop))))
4518 {
4519 /* A list of sub-properties. */
4520 while (CONSP (prop))
4521 {
4522 if (single_display_spec_string_p (XCAR (prop), string))
4523 return 1;
4524 prop = XCDR (prop);
4525 }
4526 }
4527 else if (VECTORP (prop))
4528 {
4529 /* A vector of sub-properties. */
4530 int i;
4531 for (i = 0; i < ASIZE (prop); ++i)
4532 if (single_display_spec_string_p (AREF (prop, i), string))
4533 return 1;
4534 }
4535 else
4536 return single_display_spec_string_p (prop, string);
4537
4538 return 0;
4539 }
4540
4541
4542 /* Determine from which buffer position in W's buffer STRING comes
4543 from. AROUND_CHARPOS is an approximate position where it could
4544 be from. Value is the buffer position or 0 if it couldn't be
4545 determined.
4546
4547 W's buffer must be current.
4548
4549 This function is necessary because we don't record buffer positions
4550 in glyphs generated from strings (to keep struct glyph small).
4551 This function may only use code that doesn't eval because it is
4552 called asynchronously from note_mouse_highlight. */
4553
4554 int
4555 string_buffer_position (w, string, around_charpos)
4556 struct window *w;
4557 Lisp_Object string;
4558 int around_charpos;
4559 {
4560 Lisp_Object limit, prop, pos;
4561 const int MAX_DISTANCE = 1000;
4562 int found = 0;
4563
4564 pos = make_number (around_charpos);
4565 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4566 while (!found && !EQ (pos, limit))
4567 {
4568 prop = Fget_char_property (pos, Qdisplay, Qnil);
4569 if (!NILP (prop) && display_prop_string_p (prop, string))
4570 found = 1;
4571 else
4572 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4573 }
4574
4575 if (!found)
4576 {
4577 pos = make_number (around_charpos);
4578 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4579 while (!found && !EQ (pos, limit))
4580 {
4581 prop = Fget_char_property (pos, Qdisplay, Qnil);
4582 if (!NILP (prop) && display_prop_string_p (prop, string))
4583 found = 1;
4584 else
4585 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4586 limit);
4587 }
4588 }
4589
4590 return found ? XINT (pos) : 0;
4591 }
4592
4593
4594 \f
4595 /***********************************************************************
4596 `composition' property
4597 ***********************************************************************/
4598
4599 static enum prop_handled
4600 handle_auto_composed_prop (it)
4601 struct it *it;
4602 {
4603 enum prop_handled handled = HANDLED_NORMALLY;
4604
4605 if (FUNCTIONP (Vauto_composition_function))
4606 {
4607 Lisp_Object val = Qnil;
4608 EMACS_INT pos, limit = -1;
4609
4610 if (STRINGP (it->string))
4611 pos = IT_STRING_CHARPOS (*it);
4612 else
4613 pos = IT_CHARPOS (*it);
4614
4615 val = Fget_text_property (make_number (pos), Qauto_composed, it->string);
4616 if (! NILP (val))
4617 {
4618 Lisp_Object cmp_prop;
4619 EMACS_INT cmp_start, cmp_end;
4620
4621 if (get_property_and_range (pos, Qcomposition, &cmp_prop,
4622 &cmp_start, &cmp_end, it->string)
4623 && cmp_start == pos
4624 && COMPOSITION_METHOD (cmp_prop) == COMPOSITION_WITH_GLYPH_STRING)
4625 {
4626 Lisp_Object gstring = COMPOSITION_COMPONENTS (cmp_prop);
4627 Lisp_Object font_object = LGSTRING_FONT (gstring);
4628
4629 if (! EQ (font_object,
4630 font_at (-1, pos, FACE_FROM_ID (it->f, it->face_id),
4631 it->w, it->string)))
4632 /* We must re-compute the composition for the
4633 different font. */
4634 val = Qnil;
4635 }
4636
4637 if (! NILP (val))
4638 {
4639 Lisp_Object end;
4640
4641 /* As Fnext_single_char_property_change is very slow, we
4642 limit the search to the current line. */
4643 if (STRINGP (it->string))
4644 limit = SCHARS (it->string);
4645 else
4646 limit = find_next_newline_no_quit (pos, 1);
4647 end = Fnext_single_char_property_change (make_number (pos),
4648 Qauto_composed,
4649 it->string,
4650 make_number (limit));
4651
4652 if (XINT (end) < limit)
4653 /* The current point is auto-composed, but there exist
4654 characters not yet composed beyond the
4655 auto-composed region. There's a possiblity that
4656 the last characters in the region may be newly
4657 composed. */
4658 val = Qnil;
4659 }
4660 }
4661 if (NILP (val) && ! STRINGP (it->string))
4662 {
4663 if (limit < 0)
4664 limit = (STRINGP (it->string) ? SCHARS (it->string)
4665 : find_next_newline_no_quit (pos, 1));
4666 if (pos < limit)
4667 {
4668 int count = SPECPDL_INDEX ();
4669 Lisp_Object args[5];
4670
4671 if (FRAME_WINDOW_P (it->f))
4672 limit = font_range (pos, limit,
4673 FACE_FROM_ID (it->f, it->face_id),
4674 it->f, it->string);
4675 args[0] = Vauto_composition_function;
4676 specbind (Qauto_composition_function, Qnil);
4677 args[1] = make_number (pos);
4678 args[2] = make_number (limit);
4679 args[3] = it->window;
4680 args[4] = it->string;
4681 safe_call (5, args);
4682 unbind_to (count, Qnil);
4683 }
4684 }
4685 }
4686
4687 return handled;
4688 }
4689
4690 /* Set up iterator IT from `composition' property at its current
4691 position. Called from handle_stop. */
4692
4693 static enum prop_handled
4694 handle_composition_prop (it)
4695 struct it *it;
4696 {
4697 Lisp_Object prop, string;
4698 EMACS_INT pos, pos_byte, start, end;
4699 enum prop_handled handled = HANDLED_NORMALLY;
4700
4701 if (STRINGP (it->string))
4702 {
4703 unsigned char *s;
4704
4705 pos = IT_STRING_CHARPOS (*it);
4706 pos_byte = IT_STRING_BYTEPOS (*it);
4707 string = it->string;
4708 s = SDATA (string) + pos_byte;
4709 it->c = STRING_CHAR (s, 0);
4710 }
4711 else
4712 {
4713 pos = IT_CHARPOS (*it);
4714 pos_byte = IT_BYTEPOS (*it);
4715 string = Qnil;
4716 it->c = FETCH_CHAR (pos_byte);
4717 }
4718
4719 /* If there's a valid composition and point is not inside of the
4720 composition (in the case that the composition is from the current
4721 buffer), draw a glyph composed from the composition components. */
4722 if (find_composition (pos, -1, &start, &end, &prop, string)
4723 && COMPOSITION_VALID_P (start, end, prop)
4724 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4725 {
4726 int id;
4727
4728 if (start != pos)
4729 {
4730 if (STRINGP (it->string))
4731 pos_byte = string_char_to_byte (it->string, start);
4732 else
4733 pos_byte = CHAR_TO_BYTE (start);
4734 }
4735 id = get_composition_id (start, pos_byte, end - start, prop, string);
4736
4737 if (id >= 0)
4738 {
4739 struct composition *cmp = composition_table[id];
4740
4741 if (cmp->glyph_len == 0)
4742 {
4743 /* No glyph. */
4744 if (STRINGP (it->string))
4745 {
4746 IT_STRING_CHARPOS (*it) = end;
4747 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4748 end);
4749 }
4750 else
4751 {
4752 IT_CHARPOS (*it) = end;
4753 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4754 }
4755 return HANDLED_RECOMPUTE_PROPS;
4756 }
4757
4758 it->stop_charpos = end;
4759 push_it (it);
4760
4761 it->method = GET_FROM_COMPOSITION;
4762 it->cmp_id = id;
4763 it->cmp_len = COMPOSITION_LENGTH (prop);
4764 /* For a terminal, draw only the first (non-TAB) character
4765 of the components. */
4766 if (composition_table[id]->method == COMPOSITION_WITH_GLYPH_STRING)
4767 {
4768 /* FIXME: This doesn't do anything!?! */
4769 Lisp_Object lgstring = AREF (XHASH_TABLE (composition_hash_table)
4770 ->key_and_value,
4771 cmp->hash_index * 2);
4772 }
4773 else
4774 {
4775 int i;
4776
4777 for (i = 0; i < cmp->glyph_len; i++)
4778 if ((it->c = COMPOSITION_GLYPH (composition_table[id], i))
4779 != '\t')
4780 break;
4781 }
4782 if (it->c == '\t')
4783 it->c = ' ';
4784 it->len = (STRINGP (it->string)
4785 ? string_char_to_byte (it->string, end)
4786 : CHAR_TO_BYTE (end)) - pos_byte;
4787 handled = HANDLED_RETURN;
4788 }
4789 }
4790
4791 return handled;
4792 }
4793
4794
4795 \f
4796 /***********************************************************************
4797 Overlay strings
4798 ***********************************************************************/
4799
4800 /* The following structure is used to record overlay strings for
4801 later sorting in load_overlay_strings. */
4802
4803 struct overlay_entry
4804 {
4805 Lisp_Object overlay;
4806 Lisp_Object string;
4807 int priority;
4808 int after_string_p;
4809 };
4810
4811
4812 /* Set up iterator IT from overlay strings at its current position.
4813 Called from handle_stop. */
4814
4815 static enum prop_handled
4816 handle_overlay_change (it)
4817 struct it *it;
4818 {
4819 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4820 return HANDLED_RECOMPUTE_PROPS;
4821 else
4822 return HANDLED_NORMALLY;
4823 }
4824
4825
4826 /* Set up the next overlay string for delivery by IT, if there is an
4827 overlay string to deliver. Called by set_iterator_to_next when the
4828 end of the current overlay string is reached. If there are more
4829 overlay strings to display, IT->string and
4830 IT->current.overlay_string_index are set appropriately here.
4831 Otherwise IT->string is set to nil. */
4832
4833 static void
4834 next_overlay_string (it)
4835 struct it *it;
4836 {
4837 ++it->current.overlay_string_index;
4838 if (it->current.overlay_string_index == it->n_overlay_strings)
4839 {
4840 /* No more overlay strings. Restore IT's settings to what
4841 they were before overlay strings were processed, and
4842 continue to deliver from current_buffer. */
4843 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4844
4845 pop_it (it);
4846 xassert (it->sp > 0
4847 || it->method == GET_FROM_COMPOSITION
4848 || (NILP (it->string)
4849 && it->method == GET_FROM_BUFFER
4850 && it->stop_charpos >= BEGV
4851 && it->stop_charpos <= it->end_charpos));
4852 it->current.overlay_string_index = -1;
4853 it->n_overlay_strings = 0;
4854
4855 /* If we're at the end of the buffer, record that we have
4856 processed the overlay strings there already, so that
4857 next_element_from_buffer doesn't try it again. */
4858 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4859 it->overlay_strings_at_end_processed_p = 1;
4860
4861 /* If we have to display `...' for invisible text, set
4862 the iterator up for that. */
4863 if (display_ellipsis_p)
4864 setup_for_ellipsis (it, 0);
4865 }
4866 else
4867 {
4868 /* There are more overlay strings to process. If
4869 IT->current.overlay_string_index has advanced to a position
4870 where we must load IT->overlay_strings with more strings, do
4871 it. */
4872 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4873
4874 if (it->current.overlay_string_index && i == 0)
4875 load_overlay_strings (it, 0);
4876
4877 /* Initialize IT to deliver display elements from the overlay
4878 string. */
4879 it->string = it->overlay_strings[i];
4880 it->multibyte_p = STRING_MULTIBYTE (it->string);
4881 SET_TEXT_POS (it->current.string_pos, 0, 0);
4882 it->method = GET_FROM_STRING;
4883 it->stop_charpos = 0;
4884 }
4885
4886 CHECK_IT (it);
4887 }
4888
4889
4890 /* Compare two overlay_entry structures E1 and E2. Used as a
4891 comparison function for qsort in load_overlay_strings. Overlay
4892 strings for the same position are sorted so that
4893
4894 1. All after-strings come in front of before-strings, except
4895 when they come from the same overlay.
4896
4897 2. Within after-strings, strings are sorted so that overlay strings
4898 from overlays with higher priorities come first.
4899
4900 2. Within before-strings, strings are sorted so that overlay
4901 strings from overlays with higher priorities come last.
4902
4903 Value is analogous to strcmp. */
4904
4905
4906 static int
4907 compare_overlay_entries (e1, e2)
4908 void *e1, *e2;
4909 {
4910 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4911 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4912 int result;
4913
4914 if (entry1->after_string_p != entry2->after_string_p)
4915 {
4916 /* Let after-strings appear in front of before-strings if
4917 they come from different overlays. */
4918 if (EQ (entry1->overlay, entry2->overlay))
4919 result = entry1->after_string_p ? 1 : -1;
4920 else
4921 result = entry1->after_string_p ? -1 : 1;
4922 }
4923 else if (entry1->after_string_p)
4924 /* After-strings sorted in order of decreasing priority. */
4925 result = entry2->priority - entry1->priority;
4926 else
4927 /* Before-strings sorted in order of increasing priority. */
4928 result = entry1->priority - entry2->priority;
4929
4930 return result;
4931 }
4932
4933
4934 /* Load the vector IT->overlay_strings with overlay strings from IT's
4935 current buffer position, or from CHARPOS if that is > 0. Set
4936 IT->n_overlays to the total number of overlay strings found.
4937
4938 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4939 a time. On entry into load_overlay_strings,
4940 IT->current.overlay_string_index gives the number of overlay
4941 strings that have already been loaded by previous calls to this
4942 function.
4943
4944 IT->add_overlay_start contains an additional overlay start
4945 position to consider for taking overlay strings from, if non-zero.
4946 This position comes into play when the overlay has an `invisible'
4947 property, and both before and after-strings. When we've skipped to
4948 the end of the overlay, because of its `invisible' property, we
4949 nevertheless want its before-string to appear.
4950 IT->add_overlay_start will contain the overlay start position
4951 in this case.
4952
4953 Overlay strings are sorted so that after-string strings come in
4954 front of before-string strings. Within before and after-strings,
4955 strings are sorted by overlay priority. See also function
4956 compare_overlay_entries. */
4957
4958 static void
4959 load_overlay_strings (it, charpos)
4960 struct it *it;
4961 int charpos;
4962 {
4963 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4964 Lisp_Object overlay, window, str, invisible;
4965 struct Lisp_Overlay *ov;
4966 int start, end;
4967 int size = 20;
4968 int n = 0, i, j, invis_p;
4969 struct overlay_entry *entries
4970 = (struct overlay_entry *) alloca (size * sizeof *entries);
4971
4972 if (charpos <= 0)
4973 charpos = IT_CHARPOS (*it);
4974
4975 /* Append the overlay string STRING of overlay OVERLAY to vector
4976 `entries' which has size `size' and currently contains `n'
4977 elements. AFTER_P non-zero means STRING is an after-string of
4978 OVERLAY. */
4979 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4980 do \
4981 { \
4982 Lisp_Object priority; \
4983 \
4984 if (n == size) \
4985 { \
4986 int new_size = 2 * size; \
4987 struct overlay_entry *old = entries; \
4988 entries = \
4989 (struct overlay_entry *) alloca (new_size \
4990 * sizeof *entries); \
4991 bcopy (old, entries, size * sizeof *entries); \
4992 size = new_size; \
4993 } \
4994 \
4995 entries[n].string = (STRING); \
4996 entries[n].overlay = (OVERLAY); \
4997 priority = Foverlay_get ((OVERLAY), Qpriority); \
4998 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4999 entries[n].after_string_p = (AFTER_P); \
5000 ++n; \
5001 } \
5002 while (0)
5003
5004 /* Process overlay before the overlay center. */
5005 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5006 {
5007 XSETMISC (overlay, ov);
5008 xassert (OVERLAYP (overlay));
5009 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5010 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5011
5012 if (end < charpos)
5013 break;
5014
5015 /* Skip this overlay if it doesn't start or end at IT's current
5016 position. */
5017 if (end != charpos && start != charpos)
5018 continue;
5019
5020 /* Skip this overlay if it doesn't apply to IT->w. */
5021 window = Foverlay_get (overlay, Qwindow);
5022 if (WINDOWP (window) && XWINDOW (window) != it->w)
5023 continue;
5024
5025 /* If the text ``under'' the overlay is invisible, both before-
5026 and after-strings from this overlay are visible; start and
5027 end position are indistinguishable. */
5028 invisible = Foverlay_get (overlay, Qinvisible);
5029 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5030
5031 /* If overlay has a non-empty before-string, record it. */
5032 if ((start == charpos || (end == charpos && invis_p))
5033 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5034 && SCHARS (str))
5035 RECORD_OVERLAY_STRING (overlay, str, 0);
5036
5037 /* If overlay has a non-empty after-string, record it. */
5038 if ((end == charpos || (start == charpos && invis_p))
5039 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5040 && SCHARS (str))
5041 RECORD_OVERLAY_STRING (overlay, str, 1);
5042 }
5043
5044 /* Process overlays after the overlay center. */
5045 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5046 {
5047 XSETMISC (overlay, ov);
5048 xassert (OVERLAYP (overlay));
5049 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5050 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5051
5052 if (start > charpos)
5053 break;
5054
5055 /* Skip this overlay if it doesn't start or end at IT's current
5056 position. */
5057 if (end != charpos && start != charpos)
5058 continue;
5059
5060 /* Skip this overlay if it doesn't apply to IT->w. */
5061 window = Foverlay_get (overlay, Qwindow);
5062 if (WINDOWP (window) && XWINDOW (window) != it->w)
5063 continue;
5064
5065 /* If the text ``under'' the overlay is invisible, it has a zero
5066 dimension, and both before- and after-strings apply. */
5067 invisible = Foverlay_get (overlay, Qinvisible);
5068 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5069
5070 /* If overlay has a non-empty before-string, record it. */
5071 if ((start == charpos || (end == charpos && invis_p))
5072 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5073 && SCHARS (str))
5074 RECORD_OVERLAY_STRING (overlay, str, 0);
5075
5076 /* If overlay has a non-empty after-string, record it. */
5077 if ((end == charpos || (start == charpos && invis_p))
5078 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5079 && SCHARS (str))
5080 RECORD_OVERLAY_STRING (overlay, str, 1);
5081 }
5082
5083 #undef RECORD_OVERLAY_STRING
5084
5085 /* Sort entries. */
5086 if (n > 1)
5087 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5088
5089 /* Record the total number of strings to process. */
5090 it->n_overlay_strings = n;
5091
5092 /* IT->current.overlay_string_index is the number of overlay strings
5093 that have already been consumed by IT. Copy some of the
5094 remaining overlay strings to IT->overlay_strings. */
5095 i = 0;
5096 j = it->current.overlay_string_index;
5097 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5098 {
5099 it->overlay_strings[i] = entries[j].string;
5100 it->string_overlays[i++] = entries[j++].overlay;
5101 }
5102
5103 CHECK_IT (it);
5104 }
5105
5106
5107 /* Get the first chunk of overlay strings at IT's current buffer
5108 position, or at CHARPOS if that is > 0. Value is non-zero if at
5109 least one overlay string was found. */
5110
5111 static int
5112 get_overlay_strings_1 (it, charpos, compute_stop_p)
5113 struct it *it;
5114 int charpos;
5115 int compute_stop_p;
5116 {
5117 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5118 process. This fills IT->overlay_strings with strings, and sets
5119 IT->n_overlay_strings to the total number of strings to process.
5120 IT->pos.overlay_string_index has to be set temporarily to zero
5121 because load_overlay_strings needs this; it must be set to -1
5122 when no overlay strings are found because a zero value would
5123 indicate a position in the first overlay string. */
5124 it->current.overlay_string_index = 0;
5125 load_overlay_strings (it, charpos);
5126
5127 /* If we found overlay strings, set up IT to deliver display
5128 elements from the first one. Otherwise set up IT to deliver
5129 from current_buffer. */
5130 if (it->n_overlay_strings)
5131 {
5132 /* Make sure we know settings in current_buffer, so that we can
5133 restore meaningful values when we're done with the overlay
5134 strings. */
5135 if (compute_stop_p)
5136 compute_stop_pos (it);
5137 xassert (it->face_id >= 0);
5138
5139 /* Save IT's settings. They are restored after all overlay
5140 strings have been processed. */
5141 xassert (!compute_stop_p || it->sp == 0);
5142 push_it (it);
5143
5144 /* Set up IT to deliver display elements from the first overlay
5145 string. */
5146 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5147 it->string = it->overlay_strings[0];
5148 it->from_overlay = Qnil;
5149 it->stop_charpos = 0;
5150 xassert (STRINGP (it->string));
5151 it->end_charpos = SCHARS (it->string);
5152 it->multibyte_p = STRING_MULTIBYTE (it->string);
5153 it->method = GET_FROM_STRING;
5154 return 1;
5155 }
5156
5157 it->current.overlay_string_index = -1;
5158 return 0;
5159 }
5160
5161 static int
5162 get_overlay_strings (it, charpos)
5163 struct it *it;
5164 int charpos;
5165 {
5166 it->string = Qnil;
5167 it->method = GET_FROM_BUFFER;
5168
5169 (void) get_overlay_strings_1 (it, charpos, 1);
5170
5171 CHECK_IT (it);
5172
5173 /* Value is non-zero if we found at least one overlay string. */
5174 return STRINGP (it->string);
5175 }
5176
5177
5178 \f
5179 /***********************************************************************
5180 Saving and restoring state
5181 ***********************************************************************/
5182
5183 /* Save current settings of IT on IT->stack. Called, for example,
5184 before setting up IT for an overlay string, to be able to restore
5185 IT's settings to what they were after the overlay string has been
5186 processed. */
5187
5188 static void
5189 push_it (it)
5190 struct it *it;
5191 {
5192 struct iterator_stack_entry *p;
5193
5194 xassert (it->sp < IT_STACK_SIZE);
5195 p = it->stack + it->sp;
5196
5197 p->stop_charpos = it->stop_charpos;
5198 xassert (it->face_id >= 0);
5199 p->face_id = it->face_id;
5200 p->string = it->string;
5201 p->method = it->method;
5202 p->from_overlay = it->from_overlay;
5203 switch (p->method)
5204 {
5205 case GET_FROM_IMAGE:
5206 p->u.image.object = it->object;
5207 p->u.image.image_id = it->image_id;
5208 p->u.image.slice = it->slice;
5209 break;
5210 case GET_FROM_COMPOSITION:
5211 p->u.comp.object = it->object;
5212 p->u.comp.c = it->c;
5213 p->u.comp.len = it->len;
5214 p->u.comp.cmp_id = it->cmp_id;
5215 p->u.comp.cmp_len = it->cmp_len;
5216 break;
5217 case GET_FROM_STRETCH:
5218 p->u.stretch.object = it->object;
5219 break;
5220 }
5221 p->position = it->position;
5222 p->current = it->current;
5223 p->end_charpos = it->end_charpos;
5224 p->string_nchars = it->string_nchars;
5225 p->area = it->area;
5226 p->multibyte_p = it->multibyte_p;
5227 p->avoid_cursor_p = it->avoid_cursor_p;
5228 p->space_width = it->space_width;
5229 p->font_height = it->font_height;
5230 p->voffset = it->voffset;
5231 p->string_from_display_prop_p = it->string_from_display_prop_p;
5232 p->display_ellipsis_p = 0;
5233 ++it->sp;
5234 }
5235
5236
5237 /* Restore IT's settings from IT->stack. Called, for example, when no
5238 more overlay strings must be processed, and we return to delivering
5239 display elements from a buffer, or when the end of a string from a
5240 `display' property is reached and we return to delivering display
5241 elements from an overlay string, or from a buffer. */
5242
5243 static void
5244 pop_it (it)
5245 struct it *it;
5246 {
5247 struct iterator_stack_entry *p;
5248
5249 xassert (it->sp > 0);
5250 --it->sp;
5251 p = it->stack + it->sp;
5252 it->stop_charpos = p->stop_charpos;
5253 it->face_id = p->face_id;
5254 it->current = p->current;
5255 it->position = p->position;
5256 it->string = p->string;
5257 it->from_overlay = p->from_overlay;
5258 if (NILP (it->string))
5259 SET_TEXT_POS (it->current.string_pos, -1, -1);
5260 it->method = p->method;
5261 switch (it->method)
5262 {
5263 case GET_FROM_IMAGE:
5264 it->image_id = p->u.image.image_id;
5265 it->object = p->u.image.object;
5266 it->slice = p->u.image.slice;
5267 break;
5268 case GET_FROM_COMPOSITION:
5269 it->object = p->u.comp.object;
5270 it->c = p->u.comp.c;
5271 it->len = p->u.comp.len;
5272 it->cmp_id = p->u.comp.cmp_id;
5273 it->cmp_len = p->u.comp.cmp_len;
5274 break;
5275 case GET_FROM_STRETCH:
5276 it->object = p->u.comp.object;
5277 break;
5278 case GET_FROM_BUFFER:
5279 it->object = it->w->buffer;
5280 break;
5281 case GET_FROM_STRING:
5282 it->object = it->string;
5283 break;
5284 }
5285 it->end_charpos = p->end_charpos;
5286 it->string_nchars = p->string_nchars;
5287 it->area = p->area;
5288 it->multibyte_p = p->multibyte_p;
5289 it->avoid_cursor_p = p->avoid_cursor_p;
5290 it->space_width = p->space_width;
5291 it->font_height = p->font_height;
5292 it->voffset = p->voffset;
5293 it->string_from_display_prop_p = p->string_from_display_prop_p;
5294 }
5295
5296
5297 \f
5298 /***********************************************************************
5299 Moving over lines
5300 ***********************************************************************/
5301
5302 /* Set IT's current position to the previous line start. */
5303
5304 static void
5305 back_to_previous_line_start (it)
5306 struct it *it;
5307 {
5308 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5309 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5310 }
5311
5312
5313 /* Move IT to the next line start.
5314
5315 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5316 we skipped over part of the text (as opposed to moving the iterator
5317 continuously over the text). Otherwise, don't change the value
5318 of *SKIPPED_P.
5319
5320 Newlines may come from buffer text, overlay strings, or strings
5321 displayed via the `display' property. That's the reason we can't
5322 simply use find_next_newline_no_quit.
5323
5324 Note that this function may not skip over invisible text that is so
5325 because of text properties and immediately follows a newline. If
5326 it would, function reseat_at_next_visible_line_start, when called
5327 from set_iterator_to_next, would effectively make invisible
5328 characters following a newline part of the wrong glyph row, which
5329 leads to wrong cursor motion. */
5330
5331 static int
5332 forward_to_next_line_start (it, skipped_p)
5333 struct it *it;
5334 int *skipped_p;
5335 {
5336 int old_selective, newline_found_p, n;
5337 const int MAX_NEWLINE_DISTANCE = 500;
5338
5339 /* If already on a newline, just consume it to avoid unintended
5340 skipping over invisible text below. */
5341 if (it->what == IT_CHARACTER
5342 && it->c == '\n'
5343 && CHARPOS (it->position) == IT_CHARPOS (*it))
5344 {
5345 set_iterator_to_next (it, 0);
5346 it->c = 0;
5347 return 1;
5348 }
5349
5350 /* Don't handle selective display in the following. It's (a)
5351 unnecessary because it's done by the caller, and (b) leads to an
5352 infinite recursion because next_element_from_ellipsis indirectly
5353 calls this function. */
5354 old_selective = it->selective;
5355 it->selective = 0;
5356
5357 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5358 from buffer text. */
5359 for (n = newline_found_p = 0;
5360 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5361 n += STRINGP (it->string) ? 0 : 1)
5362 {
5363 if (!get_next_display_element (it))
5364 return 0;
5365 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5366 set_iterator_to_next (it, 0);
5367 }
5368
5369 /* If we didn't find a newline near enough, see if we can use a
5370 short-cut. */
5371 if (!newline_found_p)
5372 {
5373 int start = IT_CHARPOS (*it);
5374 int limit = find_next_newline_no_quit (start, 1);
5375 Lisp_Object pos;
5376
5377 xassert (!STRINGP (it->string));
5378
5379 /* If there isn't any `display' property in sight, and no
5380 overlays, we can just use the position of the newline in
5381 buffer text. */
5382 if (it->stop_charpos >= limit
5383 || ((pos = Fnext_single_property_change (make_number (start),
5384 Qdisplay,
5385 Qnil, make_number (limit)),
5386 NILP (pos))
5387 && next_overlay_change (start) == ZV))
5388 {
5389 IT_CHARPOS (*it) = limit;
5390 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5391 *skipped_p = newline_found_p = 1;
5392 }
5393 else
5394 {
5395 while (get_next_display_element (it)
5396 && !newline_found_p)
5397 {
5398 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5399 set_iterator_to_next (it, 0);
5400 }
5401 }
5402 }
5403
5404 it->selective = old_selective;
5405 return newline_found_p;
5406 }
5407
5408
5409 /* Set IT's current position to the previous visible line start. Skip
5410 invisible text that is so either due to text properties or due to
5411 selective display. Caution: this does not change IT->current_x and
5412 IT->hpos. */
5413
5414 static void
5415 back_to_previous_visible_line_start (it)
5416 struct it *it;
5417 {
5418 while (IT_CHARPOS (*it) > BEGV)
5419 {
5420 back_to_previous_line_start (it);
5421
5422 if (IT_CHARPOS (*it) <= BEGV)
5423 break;
5424
5425 /* If selective > 0, then lines indented more than that values
5426 are invisible. */
5427 if (it->selective > 0
5428 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5429 (double) it->selective)) /* iftc */
5430 continue;
5431
5432 /* Check the newline before point for invisibility. */
5433 {
5434 Lisp_Object prop;
5435 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5436 Qinvisible, it->window);
5437 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5438 continue;
5439 }
5440
5441 if (IT_CHARPOS (*it) <= BEGV)
5442 break;
5443
5444 {
5445 struct it it2;
5446 int pos;
5447 EMACS_INT beg, end;
5448 Lisp_Object val, overlay;
5449
5450 /* If newline is part of a composition, continue from start of composition */
5451 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5452 && beg < IT_CHARPOS (*it))
5453 goto replaced;
5454
5455 /* If newline is replaced by a display property, find start of overlay
5456 or interval and continue search from that point. */
5457 it2 = *it;
5458 pos = --IT_CHARPOS (it2);
5459 --IT_BYTEPOS (it2);
5460 it2.sp = 0;
5461 it2.string_from_display_prop_p = 0;
5462 if (handle_display_prop (&it2) == HANDLED_RETURN
5463 && !NILP (val = get_char_property_and_overlay
5464 (make_number (pos), Qdisplay, Qnil, &overlay))
5465 && (OVERLAYP (overlay)
5466 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5467 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5468 goto replaced;
5469
5470 /* Newline is not replaced by anything -- so we are done. */
5471 break;
5472
5473 replaced:
5474 if (beg < BEGV)
5475 beg = BEGV;
5476 IT_CHARPOS (*it) = beg;
5477 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5478 }
5479 }
5480
5481 it->continuation_lines_width = 0;
5482
5483 xassert (IT_CHARPOS (*it) >= BEGV);
5484 xassert (IT_CHARPOS (*it) == BEGV
5485 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5486 CHECK_IT (it);
5487 }
5488
5489
5490 /* Reseat iterator IT at the previous visible line start. Skip
5491 invisible text that is so either due to text properties or due to
5492 selective display. At the end, update IT's overlay information,
5493 face information etc. */
5494
5495 void
5496 reseat_at_previous_visible_line_start (it)
5497 struct it *it;
5498 {
5499 back_to_previous_visible_line_start (it);
5500 reseat (it, it->current.pos, 1);
5501 CHECK_IT (it);
5502 }
5503
5504
5505 /* Reseat iterator IT on the next visible line start in the current
5506 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5507 preceding the line start. Skip over invisible text that is so
5508 because of selective display. Compute faces, overlays etc at the
5509 new position. Note that this function does not skip over text that
5510 is invisible because of text properties. */
5511
5512 static void
5513 reseat_at_next_visible_line_start (it, on_newline_p)
5514 struct it *it;
5515 int on_newline_p;
5516 {
5517 int newline_found_p, skipped_p = 0;
5518
5519 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5520
5521 /* Skip over lines that are invisible because they are indented
5522 more than the value of IT->selective. */
5523 if (it->selective > 0)
5524 while (IT_CHARPOS (*it) < ZV
5525 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5526 (double) it->selective)) /* iftc */
5527 {
5528 xassert (IT_BYTEPOS (*it) == BEGV
5529 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5530 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5531 }
5532
5533 /* Position on the newline if that's what's requested. */
5534 if (on_newline_p && newline_found_p)
5535 {
5536 if (STRINGP (it->string))
5537 {
5538 if (IT_STRING_CHARPOS (*it) > 0)
5539 {
5540 --IT_STRING_CHARPOS (*it);
5541 --IT_STRING_BYTEPOS (*it);
5542 }
5543 }
5544 else if (IT_CHARPOS (*it) > BEGV)
5545 {
5546 --IT_CHARPOS (*it);
5547 --IT_BYTEPOS (*it);
5548 reseat (it, it->current.pos, 0);
5549 }
5550 }
5551 else if (skipped_p)
5552 reseat (it, it->current.pos, 0);
5553
5554 CHECK_IT (it);
5555 }
5556
5557
5558 \f
5559 /***********************************************************************
5560 Changing an iterator's position
5561 ***********************************************************************/
5562
5563 /* Change IT's current position to POS in current_buffer. If FORCE_P
5564 is non-zero, always check for text properties at the new position.
5565 Otherwise, text properties are only looked up if POS >=
5566 IT->check_charpos of a property. */
5567
5568 static void
5569 reseat (it, pos, force_p)
5570 struct it *it;
5571 struct text_pos pos;
5572 int force_p;
5573 {
5574 int original_pos = IT_CHARPOS (*it);
5575
5576 reseat_1 (it, pos, 0);
5577
5578 /* Determine where to check text properties. Avoid doing it
5579 where possible because text property lookup is very expensive. */
5580 if (force_p
5581 || CHARPOS (pos) > it->stop_charpos
5582 || CHARPOS (pos) < original_pos)
5583 handle_stop (it);
5584
5585 CHECK_IT (it);
5586 }
5587
5588
5589 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5590 IT->stop_pos to POS, also. */
5591
5592 static void
5593 reseat_1 (it, pos, set_stop_p)
5594 struct it *it;
5595 struct text_pos pos;
5596 int set_stop_p;
5597 {
5598 /* Don't call this function when scanning a C string. */
5599 xassert (it->s == NULL);
5600
5601 /* POS must be a reasonable value. */
5602 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5603
5604 it->current.pos = it->position = pos;
5605 it->end_charpos = ZV;
5606 it->dpvec = NULL;
5607 it->current.dpvec_index = -1;
5608 it->current.overlay_string_index = -1;
5609 IT_STRING_CHARPOS (*it) = -1;
5610 IT_STRING_BYTEPOS (*it) = -1;
5611 it->string = Qnil;
5612 it->string_from_display_prop_p = 0;
5613 it->method = GET_FROM_BUFFER;
5614 it->object = it->w->buffer;
5615 it->area = TEXT_AREA;
5616 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5617 it->sp = 0;
5618 it->string_from_display_prop_p = 0;
5619 it->face_before_selective_p = 0;
5620
5621 if (set_stop_p)
5622 it->stop_charpos = CHARPOS (pos);
5623 }
5624
5625
5626 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5627 If S is non-null, it is a C string to iterate over. Otherwise,
5628 STRING gives a Lisp string to iterate over.
5629
5630 If PRECISION > 0, don't return more then PRECISION number of
5631 characters from the string.
5632
5633 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5634 characters have been returned. FIELD_WIDTH < 0 means an infinite
5635 field width.
5636
5637 MULTIBYTE = 0 means disable processing of multibyte characters,
5638 MULTIBYTE > 0 means enable it,
5639 MULTIBYTE < 0 means use IT->multibyte_p.
5640
5641 IT must be initialized via a prior call to init_iterator before
5642 calling this function. */
5643
5644 static void
5645 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5646 struct it *it;
5647 unsigned char *s;
5648 Lisp_Object string;
5649 int charpos;
5650 int precision, field_width, multibyte;
5651 {
5652 /* No region in strings. */
5653 it->region_beg_charpos = it->region_end_charpos = -1;
5654
5655 /* No text property checks performed by default, but see below. */
5656 it->stop_charpos = -1;
5657
5658 /* Set iterator position and end position. */
5659 bzero (&it->current, sizeof it->current);
5660 it->current.overlay_string_index = -1;
5661 it->current.dpvec_index = -1;
5662 xassert (charpos >= 0);
5663
5664 /* If STRING is specified, use its multibyteness, otherwise use the
5665 setting of MULTIBYTE, if specified. */
5666 if (multibyte >= 0)
5667 it->multibyte_p = multibyte > 0;
5668
5669 if (s == NULL)
5670 {
5671 xassert (STRINGP (string));
5672 it->string = string;
5673 it->s = NULL;
5674 it->end_charpos = it->string_nchars = SCHARS (string);
5675 it->method = GET_FROM_STRING;
5676 it->current.string_pos = string_pos (charpos, string);
5677 }
5678 else
5679 {
5680 it->s = s;
5681 it->string = Qnil;
5682
5683 /* Note that we use IT->current.pos, not it->current.string_pos,
5684 for displaying C strings. */
5685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5686 if (it->multibyte_p)
5687 {
5688 it->current.pos = c_string_pos (charpos, s, 1);
5689 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5690 }
5691 else
5692 {
5693 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5694 it->end_charpos = it->string_nchars = strlen (s);
5695 }
5696
5697 it->method = GET_FROM_C_STRING;
5698 }
5699
5700 /* PRECISION > 0 means don't return more than PRECISION characters
5701 from the string. */
5702 if (precision > 0 && it->end_charpos - charpos > precision)
5703 it->end_charpos = it->string_nchars = charpos + precision;
5704
5705 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5706 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5707 FIELD_WIDTH < 0 means infinite field width. This is useful for
5708 padding with `-' at the end of a mode line. */
5709 if (field_width < 0)
5710 field_width = INFINITY;
5711 if (field_width > it->end_charpos - charpos)
5712 it->end_charpos = charpos + field_width;
5713
5714 /* Use the standard display table for displaying strings. */
5715 if (DISP_TABLE_P (Vstandard_display_table))
5716 it->dp = XCHAR_TABLE (Vstandard_display_table);
5717
5718 it->stop_charpos = charpos;
5719 CHECK_IT (it);
5720 }
5721
5722
5723 \f
5724 /***********************************************************************
5725 Iteration
5726 ***********************************************************************/
5727
5728 /* Map enum it_method value to corresponding next_element_from_* function. */
5729
5730 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5731 {
5732 next_element_from_buffer,
5733 next_element_from_display_vector,
5734 next_element_from_composition,
5735 next_element_from_string,
5736 next_element_from_c_string,
5737 next_element_from_image,
5738 next_element_from_stretch
5739 };
5740
5741 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5742
5743 /* Load IT's display element fields with information about the next
5744 display element from the current position of IT. Value is zero if
5745 end of buffer (or C string) is reached. */
5746
5747 static struct frame *last_escape_glyph_frame = NULL;
5748 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5749 static int last_escape_glyph_merged_face_id = 0;
5750
5751 int
5752 get_next_display_element (it)
5753 struct it *it;
5754 {
5755 /* Non-zero means that we found a display element. Zero means that
5756 we hit the end of what we iterate over. Performance note: the
5757 function pointer `method' used here turns out to be faster than
5758 using a sequence of if-statements. */
5759 int success_p;
5760
5761 get_next:
5762 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
5763
5764 if (it->what == IT_CHARACTER)
5765 {
5766 /* Map via display table or translate control characters.
5767 IT->c, IT->len etc. have been set to the next character by
5768 the function call above. If we have a display table, and it
5769 contains an entry for IT->c, translate it. Don't do this if
5770 IT->c itself comes from a display table, otherwise we could
5771 end up in an infinite recursion. (An alternative could be to
5772 count the recursion depth of this function and signal an
5773 error when a certain maximum depth is reached.) Is it worth
5774 it? */
5775 if (success_p && it->dpvec == NULL)
5776 {
5777 Lisp_Object dv;
5778
5779 if (it->dp
5780 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5781 VECTORP (dv)))
5782 {
5783 struct Lisp_Vector *v = XVECTOR (dv);
5784
5785 /* Return the first character from the display table
5786 entry, if not empty. If empty, don't display the
5787 current character. */
5788 if (v->size)
5789 {
5790 it->dpvec_char_len = it->len;
5791 it->dpvec = v->contents;
5792 it->dpend = v->contents + v->size;
5793 it->current.dpvec_index = 0;
5794 it->dpvec_face_id = -1;
5795 it->saved_face_id = it->face_id;
5796 it->method = GET_FROM_DISPLAY_VECTOR;
5797 it->ellipsis_p = 0;
5798 }
5799 else
5800 {
5801 set_iterator_to_next (it, 0);
5802 }
5803 goto get_next;
5804 }
5805
5806 /* Translate control characters into `\003' or `^C' form.
5807 Control characters coming from a display table entry are
5808 currently not translated because we use IT->dpvec to hold
5809 the translation. This could easily be changed but I
5810 don't believe that it is worth doing.
5811
5812 If it->multibyte_p is nonzero, non-printable non-ASCII
5813 characters are also translated to octal form.
5814
5815 If it->multibyte_p is zero, eight-bit characters that
5816 don't have corresponding multibyte char code are also
5817 translated to octal form. */
5818 else if ((it->c < ' '
5819 ? (it->area != TEXT_AREA
5820 /* In mode line, treat \n, \t like other crl chars. */
5821 || (it->c != '\t'
5822 && it->glyph_row && it->glyph_row->mode_line_p)
5823 || (it->c != '\n' && it->c != '\t'))
5824 : (it->multibyte_p
5825 ? (!CHAR_PRINTABLE_P (it->c)
5826 || (!NILP (Vnobreak_char_display)
5827 && (it->c == 0xA0 /* NO-BREAK SPACE */
5828 || it->c == 0xAD /* SOFT HYPHEN */)))
5829 : (it->c >= 127
5830 && (! unibyte_display_via_language_environment
5831 || (UNIBYTE_CHAR_HAS_MULTIBYTE_P (it->c)))))))
5832 {
5833 /* IT->c is a control character which must be displayed
5834 either as '\003' or as `^C' where the '\\' and '^'
5835 can be defined in the display table. Fill
5836 IT->ctl_chars with glyphs for what we have to
5837 display. Then, set IT->dpvec to these glyphs. */
5838 Lisp_Object gc;
5839 int ctl_len;
5840 int face_id, lface_id = 0 ;
5841 int escape_glyph;
5842
5843 /* Handle control characters with ^. */
5844
5845 if (it->c < 128 && it->ctl_arrow_p)
5846 {
5847 int g;
5848
5849 g = '^'; /* default glyph for Control */
5850 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5851 if (it->dp
5852 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
5853 && GLYPH_CODE_CHAR_VALID_P (gc))
5854 {
5855 g = GLYPH_CODE_CHAR (gc);
5856 lface_id = GLYPH_CODE_FACE (gc);
5857 }
5858 if (lface_id)
5859 {
5860 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
5861 }
5862 else if (it->f == last_escape_glyph_frame
5863 && it->face_id == last_escape_glyph_face_id)
5864 {
5865 face_id = last_escape_glyph_merged_face_id;
5866 }
5867 else
5868 {
5869 /* Merge the escape-glyph face into the current face. */
5870 face_id = merge_faces (it->f, Qescape_glyph, 0,
5871 it->face_id);
5872 last_escape_glyph_frame = it->f;
5873 last_escape_glyph_face_id = it->face_id;
5874 last_escape_glyph_merged_face_id = face_id;
5875 }
5876
5877 XSETINT (it->ctl_chars[0], g);
5878 XSETINT (it->ctl_chars[1], it->c ^ 0100);
5879 ctl_len = 2;
5880 goto display_control;
5881 }
5882
5883 /* Handle non-break space in the mode where it only gets
5884 highlighting. */
5885
5886 if (EQ (Vnobreak_char_display, Qt)
5887 && it->c == 0xA0)
5888 {
5889 /* Merge the no-break-space face into the current face. */
5890 face_id = merge_faces (it->f, Qnobreak_space, 0,
5891 it->face_id);
5892
5893 it->c = ' ';
5894 XSETINT (it->ctl_chars[0], ' ');
5895 ctl_len = 1;
5896 goto display_control;
5897 }
5898
5899 /* Handle sequences that start with the "escape glyph". */
5900
5901 /* the default escape glyph is \. */
5902 escape_glyph = '\\';
5903
5904 if (it->dp
5905 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
5906 && GLYPH_CODE_CHAR_VALID_P (gc))
5907 {
5908 escape_glyph = GLYPH_CODE_CHAR (gc);
5909 lface_id = GLYPH_CODE_FACE (gc);
5910 }
5911 if (lface_id)
5912 {
5913 /* The display table specified a face.
5914 Merge it into face_id and also into escape_glyph. */
5915 face_id = merge_faces (it->f, Qt, lface_id,
5916 it->face_id);
5917 }
5918 else if (it->f == last_escape_glyph_frame
5919 && it->face_id == last_escape_glyph_face_id)
5920 {
5921 face_id = last_escape_glyph_merged_face_id;
5922 }
5923 else
5924 {
5925 /* Merge the escape-glyph face into the current face. */
5926 face_id = merge_faces (it->f, Qescape_glyph, 0,
5927 it->face_id);
5928 last_escape_glyph_frame = it->f;
5929 last_escape_glyph_face_id = it->face_id;
5930 last_escape_glyph_merged_face_id = face_id;
5931 }
5932
5933 /* Handle soft hyphens in the mode where they only get
5934 highlighting. */
5935
5936 if (EQ (Vnobreak_char_display, Qt)
5937 && it->c == 0xAD)
5938 {
5939 it->c = '-';
5940 XSETINT (it->ctl_chars[0], '-');
5941 ctl_len = 1;
5942 goto display_control;
5943 }
5944
5945 /* Handle non-break space and soft hyphen
5946 with the escape glyph. */
5947
5948 if (it->c == 0xA0 || it->c == 0xAD)
5949 {
5950 XSETINT (it->ctl_chars[0], escape_glyph);
5951 it->c = (it->c == 0xA0 ? ' ' : '-');
5952 XSETINT (it->ctl_chars[1], it->c);
5953 ctl_len = 2;
5954 goto display_control;
5955 }
5956
5957 {
5958 unsigned char str[MAX_MULTIBYTE_LENGTH];
5959 int len;
5960 int i;
5961
5962 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5963 if (CHAR_BYTE8_P (it->c))
5964 {
5965 str[0] = CHAR_TO_BYTE8 (it->c);
5966 len = 1;
5967 }
5968 else if (it->c < 256)
5969 {
5970 str[0] = it->c;
5971 len = 1;
5972 }
5973 else
5974 {
5975 /* It's an invalid character, which shouldn't
5976 happen actually, but due to bugs it may
5977 happen. Let's print the char as is, there's
5978 not much meaningful we can do with it. */
5979 str[0] = it->c;
5980 str[1] = it->c >> 8;
5981 str[2] = it->c >> 16;
5982 str[3] = it->c >> 24;
5983 len = 4;
5984 }
5985
5986 for (i = 0; i < len; i++)
5987 {
5988 int g;
5989 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5990 /* Insert three more glyphs into IT->ctl_chars for
5991 the octal display of the character. */
5992 g = ((str[i] >> 6) & 7) + '0';
5993 XSETINT (it->ctl_chars[i * 4 + 1], g);
5994 g = ((str[i] >> 3) & 7) + '0';
5995 XSETINT (it->ctl_chars[i * 4 + 2], g);
5996 g = (str[i] & 7) + '0';
5997 XSETINT (it->ctl_chars[i * 4 + 3], g);
5998 }
5999 ctl_len = len * 4;
6000 }
6001
6002 display_control:
6003 /* Set up IT->dpvec and return first character from it. */
6004 it->dpvec_char_len = it->len;
6005 it->dpvec = it->ctl_chars;
6006 it->dpend = it->dpvec + ctl_len;
6007 it->current.dpvec_index = 0;
6008 it->dpvec_face_id = face_id;
6009 it->saved_face_id = it->face_id;
6010 it->method = GET_FROM_DISPLAY_VECTOR;
6011 it->ellipsis_p = 0;
6012 goto get_next;
6013 }
6014 }
6015 }
6016
6017 /* Adjust face id for a multibyte character. There are no multibyte
6018 character in unibyte text. */
6019 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6020 && it->multibyte_p
6021 && success_p
6022 && FRAME_WINDOW_P (it->f))
6023 {
6024 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6025 int pos = (it->s ? -1
6026 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6027 : IT_CHARPOS (*it));
6028
6029 it->face_id = FACE_FOR_CHAR (it->f, face, it->c, pos, it->string);
6030 }
6031
6032 /* Is this character the last one of a run of characters with
6033 box? If yes, set IT->end_of_box_run_p to 1. */
6034 if (it->face_box_p
6035 && it->s == NULL)
6036 {
6037 int face_id;
6038 struct face *face;
6039
6040 it->end_of_box_run_p
6041 = ((face_id = face_after_it_pos (it),
6042 face_id != it->face_id)
6043 && (face = FACE_FROM_ID (it->f, face_id),
6044 face->box == FACE_NO_BOX));
6045 }
6046
6047 /* Value is 0 if end of buffer or string reached. */
6048 return success_p;
6049 }
6050
6051
6052 /* Move IT to the next display element.
6053
6054 RESEAT_P non-zero means if called on a newline in buffer text,
6055 skip to the next visible line start.
6056
6057 Functions get_next_display_element and set_iterator_to_next are
6058 separate because I find this arrangement easier to handle than a
6059 get_next_display_element function that also increments IT's
6060 position. The way it is we can first look at an iterator's current
6061 display element, decide whether it fits on a line, and if it does,
6062 increment the iterator position. The other way around we probably
6063 would either need a flag indicating whether the iterator has to be
6064 incremented the next time, or we would have to implement a
6065 decrement position function which would not be easy to write. */
6066
6067 void
6068 set_iterator_to_next (it, reseat_p)
6069 struct it *it;
6070 int reseat_p;
6071 {
6072 /* Reset flags indicating start and end of a sequence of characters
6073 with box. Reset them at the start of this function because
6074 moving the iterator to a new position might set them. */
6075 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6076
6077 switch (it->method)
6078 {
6079 case GET_FROM_BUFFER:
6080 /* The current display element of IT is a character from
6081 current_buffer. Advance in the buffer, and maybe skip over
6082 invisible lines that are so because of selective display. */
6083 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6084 reseat_at_next_visible_line_start (it, 0);
6085 else
6086 {
6087 xassert (it->len != 0);
6088 IT_BYTEPOS (*it) += it->len;
6089 IT_CHARPOS (*it) += 1;
6090 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6091 }
6092 break;
6093
6094 case GET_FROM_COMPOSITION:
6095 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
6096 xassert (it->sp > 0);
6097 pop_it (it);
6098 if (it->method == GET_FROM_STRING)
6099 {
6100 IT_STRING_BYTEPOS (*it) += it->len;
6101 IT_STRING_CHARPOS (*it) += it->cmp_len;
6102 goto consider_string_end;
6103 }
6104 else if (it->method == GET_FROM_BUFFER)
6105 {
6106 IT_BYTEPOS (*it) += it->len;
6107 IT_CHARPOS (*it) += it->cmp_len;
6108 }
6109 break;
6110
6111 case GET_FROM_C_STRING:
6112 /* Current display element of IT is from a C string. */
6113 IT_BYTEPOS (*it) += it->len;
6114 IT_CHARPOS (*it) += 1;
6115 break;
6116
6117 case GET_FROM_DISPLAY_VECTOR:
6118 /* Current display element of IT is from a display table entry.
6119 Advance in the display table definition. Reset it to null if
6120 end reached, and continue with characters from buffers/
6121 strings. */
6122 ++it->current.dpvec_index;
6123
6124 /* Restore face of the iterator to what they were before the
6125 display vector entry (these entries may contain faces). */
6126 it->face_id = it->saved_face_id;
6127
6128 if (it->dpvec + it->current.dpvec_index == it->dpend)
6129 {
6130 int recheck_faces = it->ellipsis_p;
6131
6132 if (it->s)
6133 it->method = GET_FROM_C_STRING;
6134 else if (STRINGP (it->string))
6135 it->method = GET_FROM_STRING;
6136 else
6137 {
6138 it->method = GET_FROM_BUFFER;
6139 it->object = it->w->buffer;
6140 }
6141
6142 it->dpvec = NULL;
6143 it->current.dpvec_index = -1;
6144
6145 /* Skip over characters which were displayed via IT->dpvec. */
6146 if (it->dpvec_char_len < 0)
6147 reseat_at_next_visible_line_start (it, 1);
6148 else if (it->dpvec_char_len > 0)
6149 {
6150 if (it->method == GET_FROM_STRING
6151 && it->n_overlay_strings > 0)
6152 it->ignore_overlay_strings_at_pos_p = 1;
6153 it->len = it->dpvec_char_len;
6154 set_iterator_to_next (it, reseat_p);
6155 }
6156
6157 /* Maybe recheck faces after display vector */
6158 if (recheck_faces)
6159 it->stop_charpos = IT_CHARPOS (*it);
6160 }
6161 break;
6162
6163 case GET_FROM_STRING:
6164 /* Current display element is a character from a Lisp string. */
6165 xassert (it->s == NULL && STRINGP (it->string));
6166 IT_STRING_BYTEPOS (*it) += it->len;
6167 IT_STRING_CHARPOS (*it) += 1;
6168
6169 consider_string_end:
6170
6171 if (it->current.overlay_string_index >= 0)
6172 {
6173 /* IT->string is an overlay string. Advance to the
6174 next, if there is one. */
6175 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6176 next_overlay_string (it);
6177 }
6178 else
6179 {
6180 /* IT->string is not an overlay string. If we reached
6181 its end, and there is something on IT->stack, proceed
6182 with what is on the stack. This can be either another
6183 string, this time an overlay string, or a buffer. */
6184 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6185 && it->sp > 0)
6186 {
6187 pop_it (it);
6188 if (it->method == GET_FROM_STRING)
6189 goto consider_string_end;
6190 }
6191 }
6192 break;
6193
6194 case GET_FROM_IMAGE:
6195 case GET_FROM_STRETCH:
6196 /* The position etc with which we have to proceed are on
6197 the stack. The position may be at the end of a string,
6198 if the `display' property takes up the whole string. */
6199 xassert (it->sp > 0);
6200 pop_it (it);
6201 if (it->method == GET_FROM_STRING)
6202 goto consider_string_end;
6203 break;
6204
6205 default:
6206 /* There are no other methods defined, so this should be a bug. */
6207 abort ();
6208 }
6209
6210 xassert (it->method != GET_FROM_STRING
6211 || (STRINGP (it->string)
6212 && IT_STRING_CHARPOS (*it) >= 0));
6213 }
6214
6215 /* Load IT's display element fields with information about the next
6216 display element which comes from a display table entry or from the
6217 result of translating a control character to one of the forms `^C'
6218 or `\003'.
6219
6220 IT->dpvec holds the glyphs to return as characters.
6221 IT->saved_face_id holds the face id before the display vector--
6222 it is restored into IT->face_idin set_iterator_to_next. */
6223
6224 static int
6225 next_element_from_display_vector (it)
6226 struct it *it;
6227 {
6228 Lisp_Object gc;
6229
6230 /* Precondition. */
6231 xassert (it->dpvec && it->current.dpvec_index >= 0);
6232
6233 it->face_id = it->saved_face_id;
6234
6235 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6236 That seemed totally bogus - so I changed it... */
6237 gc = it->dpvec[it->current.dpvec_index];
6238
6239 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6240 {
6241 it->c = GLYPH_CODE_CHAR (gc);
6242 it->len = CHAR_BYTES (it->c);
6243
6244 /* The entry may contain a face id to use. Such a face id is
6245 the id of a Lisp face, not a realized face. A face id of
6246 zero means no face is specified. */
6247 if (it->dpvec_face_id >= 0)
6248 it->face_id = it->dpvec_face_id;
6249 else
6250 {
6251 int lface_id = GLYPH_CODE_FACE (gc);
6252 if (lface_id > 0)
6253 it->face_id = merge_faces (it->f, Qt, lface_id,
6254 it->saved_face_id);
6255 }
6256 }
6257 else
6258 /* Display table entry is invalid. Return a space. */
6259 it->c = ' ', it->len = 1;
6260
6261 /* Don't change position and object of the iterator here. They are
6262 still the values of the character that had this display table
6263 entry or was translated, and that's what we want. */
6264 it->what = IT_CHARACTER;
6265 return 1;
6266 }
6267
6268
6269 /* Load IT with the next display element from Lisp string IT->string.
6270 IT->current.string_pos is the current position within the string.
6271 If IT->current.overlay_string_index >= 0, the Lisp string is an
6272 overlay string. */
6273
6274 static int
6275 next_element_from_string (it)
6276 struct it *it;
6277 {
6278 struct text_pos position;
6279
6280 xassert (STRINGP (it->string));
6281 xassert (IT_STRING_CHARPOS (*it) >= 0);
6282 position = it->current.string_pos;
6283
6284 /* Time to check for invisible text? */
6285 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6286 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6287 {
6288 handle_stop (it);
6289
6290 /* Since a handler may have changed IT->method, we must
6291 recurse here. */
6292 return GET_NEXT_DISPLAY_ELEMENT (it);
6293 }
6294
6295 if (it->current.overlay_string_index >= 0)
6296 {
6297 /* Get the next character from an overlay string. In overlay
6298 strings, There is no field width or padding with spaces to
6299 do. */
6300 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6301 {
6302 it->what = IT_EOB;
6303 return 0;
6304 }
6305 else if (STRING_MULTIBYTE (it->string))
6306 {
6307 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6308 const unsigned char *s = (SDATA (it->string)
6309 + IT_STRING_BYTEPOS (*it));
6310 it->c = string_char_and_length (s, remaining, &it->len);
6311 }
6312 else
6313 {
6314 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6315 it->len = 1;
6316 }
6317 }
6318 else
6319 {
6320 /* Get the next character from a Lisp string that is not an
6321 overlay string. Such strings come from the mode line, for
6322 example. We may have to pad with spaces, or truncate the
6323 string. See also next_element_from_c_string. */
6324 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6325 {
6326 it->what = IT_EOB;
6327 return 0;
6328 }
6329 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6330 {
6331 /* Pad with spaces. */
6332 it->c = ' ', it->len = 1;
6333 CHARPOS (position) = BYTEPOS (position) = -1;
6334 }
6335 else if (STRING_MULTIBYTE (it->string))
6336 {
6337 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6338 const unsigned char *s = (SDATA (it->string)
6339 + IT_STRING_BYTEPOS (*it));
6340 it->c = string_char_and_length (s, maxlen, &it->len);
6341 }
6342 else
6343 {
6344 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6345 it->len = 1;
6346 }
6347 }
6348
6349 /* Record what we have and where it came from. */
6350 it->what = IT_CHARACTER;
6351 it->object = it->string;
6352 it->position = position;
6353 return 1;
6354 }
6355
6356
6357 /* Load IT with next display element from C string IT->s.
6358 IT->string_nchars is the maximum number of characters to return
6359 from the string. IT->end_charpos may be greater than
6360 IT->string_nchars when this function is called, in which case we
6361 may have to return padding spaces. Value is zero if end of string
6362 reached, including padding spaces. */
6363
6364 static int
6365 next_element_from_c_string (it)
6366 struct it *it;
6367 {
6368 int success_p = 1;
6369
6370 xassert (it->s);
6371 it->what = IT_CHARACTER;
6372 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6373 it->object = Qnil;
6374
6375 /* IT's position can be greater IT->string_nchars in case a field
6376 width or precision has been specified when the iterator was
6377 initialized. */
6378 if (IT_CHARPOS (*it) >= it->end_charpos)
6379 {
6380 /* End of the game. */
6381 it->what = IT_EOB;
6382 success_p = 0;
6383 }
6384 else if (IT_CHARPOS (*it) >= it->string_nchars)
6385 {
6386 /* Pad with spaces. */
6387 it->c = ' ', it->len = 1;
6388 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6389 }
6390 else if (it->multibyte_p)
6391 {
6392 /* Implementation note: The calls to strlen apparently aren't a
6393 performance problem because there is no noticeable performance
6394 difference between Emacs running in unibyte or multibyte mode. */
6395 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6396 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6397 maxlen, &it->len);
6398 }
6399 else
6400 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6401
6402 return success_p;
6403 }
6404
6405
6406 /* Set up IT to return characters from an ellipsis, if appropriate.
6407 The definition of the ellipsis glyphs may come from a display table
6408 entry. This function Fills IT with the first glyph from the
6409 ellipsis if an ellipsis is to be displayed. */
6410
6411 static int
6412 next_element_from_ellipsis (it)
6413 struct it *it;
6414 {
6415 if (it->selective_display_ellipsis_p)
6416 setup_for_ellipsis (it, it->len);
6417 else
6418 {
6419 /* The face at the current position may be different from the
6420 face we find after the invisible text. Remember what it
6421 was in IT->saved_face_id, and signal that it's there by
6422 setting face_before_selective_p. */
6423 it->saved_face_id = it->face_id;
6424 it->method = GET_FROM_BUFFER;
6425 it->object = it->w->buffer;
6426 reseat_at_next_visible_line_start (it, 1);
6427 it->face_before_selective_p = 1;
6428 }
6429
6430 return GET_NEXT_DISPLAY_ELEMENT (it);
6431 }
6432
6433
6434 /* Deliver an image display element. The iterator IT is already
6435 filled with image information (done in handle_display_prop). Value
6436 is always 1. */
6437
6438
6439 static int
6440 next_element_from_image (it)
6441 struct it *it;
6442 {
6443 it->what = IT_IMAGE;
6444 return 1;
6445 }
6446
6447
6448 /* Fill iterator IT with next display element from a stretch glyph
6449 property. IT->object is the value of the text property. Value is
6450 always 1. */
6451
6452 static int
6453 next_element_from_stretch (it)
6454 struct it *it;
6455 {
6456 it->what = IT_STRETCH;
6457 return 1;
6458 }
6459
6460
6461 /* Load IT with the next display element from current_buffer. Value
6462 is zero if end of buffer reached. IT->stop_charpos is the next
6463 position at which to stop and check for text properties or buffer
6464 end. */
6465
6466 static int
6467 next_element_from_buffer (it)
6468 struct it *it;
6469 {
6470 int success_p = 1;
6471
6472 /* Check this assumption, otherwise, we would never enter the
6473 if-statement, below. */
6474 xassert (IT_CHARPOS (*it) >= BEGV
6475 && IT_CHARPOS (*it) <= it->stop_charpos);
6476
6477 if (IT_CHARPOS (*it) >= it->stop_charpos)
6478 {
6479 if (IT_CHARPOS (*it) >= it->end_charpos)
6480 {
6481 int overlay_strings_follow_p;
6482
6483 /* End of the game, except when overlay strings follow that
6484 haven't been returned yet. */
6485 if (it->overlay_strings_at_end_processed_p)
6486 overlay_strings_follow_p = 0;
6487 else
6488 {
6489 it->overlay_strings_at_end_processed_p = 1;
6490 overlay_strings_follow_p = get_overlay_strings (it, 0);
6491 }
6492
6493 if (overlay_strings_follow_p)
6494 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6495 else
6496 {
6497 it->what = IT_EOB;
6498 it->position = it->current.pos;
6499 success_p = 0;
6500 }
6501 }
6502 else
6503 {
6504 handle_stop (it);
6505 return GET_NEXT_DISPLAY_ELEMENT (it);
6506 }
6507 }
6508 else
6509 {
6510 /* No face changes, overlays etc. in sight, so just return a
6511 character from current_buffer. */
6512 unsigned char *p;
6513
6514 /* Maybe run the redisplay end trigger hook. Performance note:
6515 This doesn't seem to cost measurable time. */
6516 if (it->redisplay_end_trigger_charpos
6517 && it->glyph_row
6518 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6519 run_redisplay_end_trigger_hook (it);
6520
6521 /* Get the next character, maybe multibyte. */
6522 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6523 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6524 {
6525 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6526 - IT_BYTEPOS (*it));
6527 it->c = string_char_and_length (p, maxlen, &it->len);
6528 }
6529 else
6530 it->c = *p, it->len = 1;
6531
6532 /* Record what we have and where it came from. */
6533 it->what = IT_CHARACTER;
6534 it->object = it->w->buffer;
6535 it->position = it->current.pos;
6536
6537 /* Normally we return the character found above, except when we
6538 really want to return an ellipsis for selective display. */
6539 if (it->selective)
6540 {
6541 if (it->c == '\n')
6542 {
6543 /* A value of selective > 0 means hide lines indented more
6544 than that number of columns. */
6545 if (it->selective > 0
6546 && IT_CHARPOS (*it) + 1 < ZV
6547 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6548 IT_BYTEPOS (*it) + 1,
6549 (double) it->selective)) /* iftc */
6550 {
6551 success_p = next_element_from_ellipsis (it);
6552 it->dpvec_char_len = -1;
6553 }
6554 }
6555 else if (it->c == '\r' && it->selective == -1)
6556 {
6557 /* A value of selective == -1 means that everything from the
6558 CR to the end of the line is invisible, with maybe an
6559 ellipsis displayed for it. */
6560 success_p = next_element_from_ellipsis (it);
6561 it->dpvec_char_len = -1;
6562 }
6563 }
6564 }
6565
6566 /* Value is zero if end of buffer reached. */
6567 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6568 return success_p;
6569 }
6570
6571
6572 /* Run the redisplay end trigger hook for IT. */
6573
6574 static void
6575 run_redisplay_end_trigger_hook (it)
6576 struct it *it;
6577 {
6578 Lisp_Object args[3];
6579
6580 /* IT->glyph_row should be non-null, i.e. we should be actually
6581 displaying something, or otherwise we should not run the hook. */
6582 xassert (it->glyph_row);
6583
6584 /* Set up hook arguments. */
6585 args[0] = Qredisplay_end_trigger_functions;
6586 args[1] = it->window;
6587 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6588 it->redisplay_end_trigger_charpos = 0;
6589
6590 /* Since we are *trying* to run these functions, don't try to run
6591 them again, even if they get an error. */
6592 it->w->redisplay_end_trigger = Qnil;
6593 Frun_hook_with_args (3, args);
6594
6595 /* Notice if it changed the face of the character we are on. */
6596 handle_face_prop (it);
6597 }
6598
6599
6600 /* Deliver a composition display element. The iterator IT is already
6601 filled with composition information (done in
6602 handle_composition_prop). Value is always 1. */
6603
6604 static int
6605 next_element_from_composition (it)
6606 struct it *it;
6607 {
6608 it->what = IT_COMPOSITION;
6609 it->position = (STRINGP (it->string)
6610 ? it->current.string_pos
6611 : it->current.pos);
6612 if (STRINGP (it->string))
6613 it->object = it->string;
6614 else
6615 it->object = it->w->buffer;
6616 return 1;
6617 }
6618
6619
6620 \f
6621 /***********************************************************************
6622 Moving an iterator without producing glyphs
6623 ***********************************************************************/
6624
6625 /* Check if iterator is at a position corresponding to a valid buffer
6626 position after some move_it_ call. */
6627
6628 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6629 ((it)->method == GET_FROM_STRING \
6630 ? IT_STRING_CHARPOS (*it) == 0 \
6631 : 1)
6632
6633
6634 /* Move iterator IT to a specified buffer or X position within one
6635 line on the display without producing glyphs.
6636
6637 OP should be a bit mask including some or all of these bits:
6638 MOVE_TO_X: Stop on reaching x-position TO_X.
6639 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6640 Regardless of OP's value, stop in reaching the end of the display line.
6641
6642 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6643 This means, in particular, that TO_X includes window's horizontal
6644 scroll amount.
6645
6646 The return value has several possible values that
6647 say what condition caused the scan to stop:
6648
6649 MOVE_POS_MATCH_OR_ZV
6650 - when TO_POS or ZV was reached.
6651
6652 MOVE_X_REACHED
6653 -when TO_X was reached before TO_POS or ZV were reached.
6654
6655 MOVE_LINE_CONTINUED
6656 - when we reached the end of the display area and the line must
6657 be continued.
6658
6659 MOVE_LINE_TRUNCATED
6660 - when we reached the end of the display area and the line is
6661 truncated.
6662
6663 MOVE_NEWLINE_OR_CR
6664 - when we stopped at a line end, i.e. a newline or a CR and selective
6665 display is on. */
6666
6667 static enum move_it_result
6668 move_it_in_display_line_to (struct it *it,
6669 EMACS_INT to_charpos, int to_x,
6670 enum move_operation_enum op)
6671 {
6672 enum move_it_result result = MOVE_UNDEFINED;
6673 struct glyph_row *saved_glyph_row;
6674 struct it wrap_it, atpos_it, atx_it;
6675 int may_wrap = 0;
6676
6677 /* Don't produce glyphs in produce_glyphs. */
6678 saved_glyph_row = it->glyph_row;
6679 it->glyph_row = NULL;
6680
6681 /* Use wrap_it to save a copy of IT wherever a word wrap could
6682 occur. Use atpos_it to save a copy of IT at the desired buffer
6683 position, if found, so that we can scan ahead and check if the
6684 word later overshoots the window edge. Use atx_it similarly, for
6685 pixel positions. */
6686 wrap_it.sp = -1;
6687 atpos_it.sp = -1;
6688 atx_it.sp = -1;
6689
6690 #define BUFFER_POS_REACHED_P() \
6691 ((op & MOVE_TO_POS) != 0 \
6692 && BUFFERP (it->object) \
6693 && IT_CHARPOS (*it) >= to_charpos \
6694 && (it->method == GET_FROM_BUFFER \
6695 || (it->method == GET_FROM_DISPLAY_VECTOR \
6696 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6697
6698 /* If there's a line-/wrap-prefix, handle it. */
6699 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
6700 && it->current_y < it->last_visible_y)
6701 handle_line_prefix (it);
6702
6703 while (1)
6704 {
6705 int x, i, ascent = 0, descent = 0;
6706
6707 /* Utility macro to reset an iterator with x, ascent, and descent. */
6708 #define IT_RESET_X_ASCENT_DESCENT(IT) \
6709 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
6710 (IT)->max_descent = descent)
6711
6712 /* Stop if we move beyond TO_CHARPOS (after an image or stretch
6713 glyph). */
6714 if ((op & MOVE_TO_POS) != 0
6715 && BUFFERP (it->object)
6716 && it->method == GET_FROM_BUFFER
6717 && IT_CHARPOS (*it) > to_charpos)
6718 {
6719 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6720 {
6721 result = MOVE_POS_MATCH_OR_ZV;
6722 break;
6723 }
6724 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6725 /* If wrap_it is valid, the current position might be in a
6726 word that is wrapped. So, save the iterator in
6727 atpos_it and continue to see if wrapping happens. */
6728 atpos_it = *it;
6729 }
6730
6731 /* Stop when ZV reached.
6732 We used to stop here when TO_CHARPOS reached as well, but that is
6733 too soon if this glyph does not fit on this line. So we handle it
6734 explicitly below. */
6735 if (!get_next_display_element (it))
6736 {
6737 result = MOVE_POS_MATCH_OR_ZV;
6738 break;
6739 }
6740
6741 if (it->line_wrap == TRUNCATE)
6742 {
6743 if (BUFFER_POS_REACHED_P ())
6744 {
6745 result = MOVE_POS_MATCH_OR_ZV;
6746 break;
6747 }
6748 }
6749 else
6750 {
6751 if (it->line_wrap == WORD_WRAP)
6752 {
6753 if (IT_DISPLAYING_WHITESPACE (it))
6754 may_wrap = 1;
6755 else if (may_wrap)
6756 {
6757 /* We have reached a glyph that follows one or more
6758 whitespace characters. If the position is
6759 already found, we are done. */
6760 if (atpos_it.sp >= 0)
6761 {
6762 *it = atpos_it;
6763 result = MOVE_POS_MATCH_OR_ZV;
6764 goto done;
6765 }
6766 if (atx_it.sp >= 0)
6767 {
6768 *it = atx_it;
6769 result = MOVE_X_REACHED;
6770 goto done;
6771 }
6772 /* Otherwise, we can wrap here. */
6773 wrap_it = *it;
6774 may_wrap = 0;
6775 }
6776 }
6777 }
6778
6779 /* Remember the line height for the current line, in case
6780 the next element doesn't fit on the line. */
6781 ascent = it->max_ascent;
6782 descent = it->max_descent;
6783
6784 /* The call to produce_glyphs will get the metrics of the
6785 display element IT is loaded with. Record the x-position
6786 before this display element, in case it doesn't fit on the
6787 line. */
6788 x = it->current_x;
6789
6790 PRODUCE_GLYPHS (it);
6791
6792 if (it->area != TEXT_AREA)
6793 {
6794 set_iterator_to_next (it, 1);
6795 continue;
6796 }
6797
6798 /* The number of glyphs we get back in IT->nglyphs will normally
6799 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6800 character on a terminal frame, or (iii) a line end. For the
6801 second case, IT->nglyphs - 1 padding glyphs will be present
6802 (on X frames, there is only one glyph produced for a
6803 composite character.
6804
6805 The behavior implemented below means, for continuation lines,
6806 that as many spaces of a TAB as fit on the current line are
6807 displayed there. For terminal frames, as many glyphs of a
6808 multi-glyph character are displayed in the current line, too.
6809 This is what the old redisplay code did, and we keep it that
6810 way. Under X, the whole shape of a complex character must
6811 fit on the line or it will be completely displayed in the
6812 next line.
6813
6814 Note that both for tabs and padding glyphs, all glyphs have
6815 the same width. */
6816 if (it->nglyphs)
6817 {
6818 /* More than one glyph or glyph doesn't fit on line. All
6819 glyphs have the same width. */
6820 int single_glyph_width = it->pixel_width / it->nglyphs;
6821 int new_x;
6822 int x_before_this_char = x;
6823 int hpos_before_this_char = it->hpos;
6824
6825 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6826 {
6827 new_x = x + single_glyph_width;
6828
6829 /* We want to leave anything reaching TO_X to the caller. */
6830 if ((op & MOVE_TO_X) && new_x > to_x)
6831 {
6832 if (BUFFER_POS_REACHED_P ())
6833 {
6834 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6835 goto buffer_pos_reached;
6836 if (atpos_it.sp < 0)
6837 {
6838 atpos_it = *it;
6839 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6840 }
6841 }
6842 else
6843 {
6844 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6845 {
6846 it->current_x = x;
6847 result = MOVE_X_REACHED;
6848 break;
6849 }
6850 if (atx_it.sp < 0)
6851 {
6852 atx_it = *it;
6853 IT_RESET_X_ASCENT_DESCENT (&atx_it);
6854 }
6855 }
6856 }
6857
6858 if (/* Lines are continued. */
6859 it->line_wrap != TRUNCATE
6860 && (/* And glyph doesn't fit on the line. */
6861 new_x > it->last_visible_x
6862 /* Or it fits exactly and we're on a window
6863 system frame. */
6864 || (new_x == it->last_visible_x
6865 && FRAME_WINDOW_P (it->f))))
6866 {
6867 if (/* IT->hpos == 0 means the very first glyph
6868 doesn't fit on the line, e.g. a wide image. */
6869 it->hpos == 0
6870 || (new_x == it->last_visible_x
6871 && FRAME_WINDOW_P (it->f)))
6872 {
6873 ++it->hpos;
6874 it->current_x = new_x;
6875
6876 /* The character's last glyph just barely fits
6877 in this row. */
6878 if (i == it->nglyphs - 1)
6879 {
6880 /* If this is the destination position,
6881 return a position *before* it in this row,
6882 now that we know it fits in this row. */
6883 if (BUFFER_POS_REACHED_P ())
6884 {
6885 if (it->line_wrap != WORD_WRAP
6886 || wrap_it.sp < 0)
6887 {
6888 it->hpos = hpos_before_this_char;
6889 it->current_x = x_before_this_char;
6890 result = MOVE_POS_MATCH_OR_ZV;
6891 break;
6892 }
6893 if (it->line_wrap == WORD_WRAP
6894 && atpos_it.sp < 0)
6895 {
6896 atpos_it = *it;
6897 atpos_it.current_x = x_before_this_char;
6898 atpos_it.hpos = hpos_before_this_char;
6899 }
6900 }
6901
6902 set_iterator_to_next (it, 1);
6903 #ifdef HAVE_WINDOW_SYSTEM
6904 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6905 {
6906 if (!get_next_display_element (it))
6907 {
6908 result = MOVE_POS_MATCH_OR_ZV;
6909 break;
6910 }
6911 if (BUFFER_POS_REACHED_P ())
6912 {
6913 if (ITERATOR_AT_END_OF_LINE_P (it))
6914 result = MOVE_POS_MATCH_OR_ZV;
6915 else
6916 result = MOVE_LINE_CONTINUED;
6917 break;
6918 }
6919 if (ITERATOR_AT_END_OF_LINE_P (it))
6920 {
6921 result = MOVE_NEWLINE_OR_CR;
6922 break;
6923 }
6924 }
6925 #endif /* HAVE_WINDOW_SYSTEM */
6926 }
6927 }
6928 else
6929 IT_RESET_X_ASCENT_DESCENT (it);
6930
6931 if (wrap_it.sp >= 0)
6932 {
6933 *it = wrap_it;
6934 atpos_it.sp = -1;
6935 atx_it.sp = -1;
6936 }
6937
6938 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6939 IT_CHARPOS (*it)));
6940 result = MOVE_LINE_CONTINUED;
6941 break;
6942 }
6943
6944 if (BUFFER_POS_REACHED_P ())
6945 {
6946 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
6947 goto buffer_pos_reached;
6948 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
6949 {
6950 atpos_it = *it;
6951 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
6952 }
6953 }
6954
6955 if (new_x > it->first_visible_x)
6956 {
6957 /* Glyph is visible. Increment number of glyphs that
6958 would be displayed. */
6959 ++it->hpos;
6960 }
6961 }
6962
6963 if (result != MOVE_UNDEFINED)
6964 break;
6965 }
6966 else if (BUFFER_POS_REACHED_P ())
6967 {
6968 buffer_pos_reached:
6969 IT_RESET_X_ASCENT_DESCENT (it);
6970 result = MOVE_POS_MATCH_OR_ZV;
6971 break;
6972 }
6973 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6974 {
6975 /* Stop when TO_X specified and reached. This check is
6976 necessary here because of lines consisting of a line end,
6977 only. The line end will not produce any glyphs and we
6978 would never get MOVE_X_REACHED. */
6979 xassert (it->nglyphs == 0);
6980 result = MOVE_X_REACHED;
6981 break;
6982 }
6983
6984 /* Is this a line end? If yes, we're done. */
6985 if (ITERATOR_AT_END_OF_LINE_P (it))
6986 {
6987 result = MOVE_NEWLINE_OR_CR;
6988 break;
6989 }
6990
6991 /* The current display element has been consumed. Advance
6992 to the next. */
6993 set_iterator_to_next (it, 1);
6994
6995 /* Stop if lines are truncated and IT's current x-position is
6996 past the right edge of the window now. */
6997 if (it->line_wrap == TRUNCATE
6998 && it->current_x >= it->last_visible_x)
6999 {
7000 #ifdef HAVE_WINDOW_SYSTEM
7001 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7002 {
7003 if (!get_next_display_element (it)
7004 || BUFFER_POS_REACHED_P ())
7005 {
7006 result = MOVE_POS_MATCH_OR_ZV;
7007 break;
7008 }
7009 if (ITERATOR_AT_END_OF_LINE_P (it))
7010 {
7011 result = MOVE_NEWLINE_OR_CR;
7012 break;
7013 }
7014 }
7015 #endif /* HAVE_WINDOW_SYSTEM */
7016 result = MOVE_LINE_TRUNCATED;
7017 break;
7018 }
7019 #undef IT_RESET_X_ASCENT_DESCENT
7020 }
7021
7022 #undef BUFFER_POS_REACHED_P
7023
7024 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7025 restore the saved iterator. */
7026 if (atpos_it.sp >= 0)
7027 *it = atpos_it;
7028 else if (atx_it.sp >= 0)
7029 *it = atx_it;
7030
7031 done:
7032
7033 /* Restore the iterator settings altered at the beginning of this
7034 function. */
7035 it->glyph_row = saved_glyph_row;
7036 return result;
7037 }
7038
7039 /* For external use. */
7040 void
7041 move_it_in_display_line (struct it *it,
7042 EMACS_INT to_charpos, int to_x,
7043 enum move_operation_enum op)
7044 {
7045 move_it_in_display_line_to (it, to_charpos, to_x, op);
7046 }
7047
7048
7049 /* Move IT forward until it satisfies one or more of the criteria in
7050 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7051
7052 OP is a bit-mask that specifies where to stop, and in particular,
7053 which of those four position arguments makes a difference. See the
7054 description of enum move_operation_enum.
7055
7056 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7057 screen line, this function will set IT to the next position >
7058 TO_CHARPOS. */
7059
7060 void
7061 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
7062 struct it *it;
7063 int to_charpos, to_x, to_y, to_vpos;
7064 int op;
7065 {
7066 enum move_it_result skip, skip2 = MOVE_X_REACHED;
7067 int line_height;
7068 int reached = 0;
7069
7070 for (;;)
7071 {
7072 if (op & MOVE_TO_VPOS)
7073 {
7074 /* If no TO_CHARPOS and no TO_X specified, stop at the
7075 start of the line TO_VPOS. */
7076 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
7077 {
7078 if (it->vpos == to_vpos)
7079 {
7080 reached = 1;
7081 break;
7082 }
7083 else
7084 skip = move_it_in_display_line_to (it, -1, -1, 0);
7085 }
7086 else
7087 {
7088 /* TO_VPOS >= 0 means stop at TO_X in the line at
7089 TO_VPOS, or at TO_POS, whichever comes first. */
7090 if (it->vpos == to_vpos)
7091 {
7092 reached = 2;
7093 break;
7094 }
7095
7096 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7097
7098 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
7099 {
7100 reached = 3;
7101 break;
7102 }
7103 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
7104 {
7105 /* We have reached TO_X but not in the line we want. */
7106 skip = move_it_in_display_line_to (it, to_charpos,
7107 -1, MOVE_TO_POS);
7108 if (skip == MOVE_POS_MATCH_OR_ZV)
7109 {
7110 reached = 4;
7111 break;
7112 }
7113 }
7114 }
7115 }
7116 else if (op & MOVE_TO_Y)
7117 {
7118 struct it it_backup;
7119
7120 /* TO_Y specified means stop at TO_X in the line containing
7121 TO_Y---or at TO_CHARPOS if this is reached first. The
7122 problem is that we can't really tell whether the line
7123 contains TO_Y before we have completely scanned it, and
7124 this may skip past TO_X. What we do is to first scan to
7125 TO_X.
7126
7127 If TO_X is not specified, use a TO_X of zero. The reason
7128 is to make the outcome of this function more predictable.
7129 If we didn't use TO_X == 0, we would stop at the end of
7130 the line which is probably not what a caller would expect
7131 to happen. */
7132 skip = move_it_in_display_line_to (it, to_charpos,
7133 ((op & MOVE_TO_X)
7134 ? to_x : 0),
7135 (MOVE_TO_X
7136 | (op & MOVE_TO_POS)));
7137
7138 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
7139 if (skip == MOVE_POS_MATCH_OR_ZV)
7140 {
7141 reached = 5;
7142 break;
7143 }
7144
7145 /* If TO_X was reached, we would like to know whether TO_Y
7146 is in the line. This can only be said if we know the
7147 total line height which requires us to scan the rest of
7148 the line. */
7149 if (skip == MOVE_X_REACHED)
7150 {
7151 /* Wait! We can conclude that TO_Y is in the line if
7152 the already scanned glyphs make the line tall enough
7153 because further scanning doesn't make it shorter. */
7154 line_height = it->max_ascent + it->max_descent;
7155 if (to_y >= it->current_y
7156 && to_y < it->current_y + line_height)
7157 {
7158 reached = 6;
7159 break;
7160 }
7161 it_backup = *it;
7162 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
7163 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
7164 op & MOVE_TO_POS);
7165 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
7166 }
7167
7168 /* Now, decide whether TO_Y is in this line. */
7169 line_height = it->max_ascent + it->max_descent;
7170 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
7171
7172 if (to_y >= it->current_y
7173 && to_y < it->current_y + line_height)
7174 {
7175 if (skip == MOVE_X_REACHED)
7176 /* If TO_Y is in this line and TO_X was reached above,
7177 we scanned too far. We have to restore IT's settings
7178 to the ones before skipping. */
7179 *it = it_backup;
7180 reached = 6;
7181 }
7182 else if (skip == MOVE_X_REACHED)
7183 {
7184 skip = skip2;
7185 if (skip == MOVE_POS_MATCH_OR_ZV)
7186 reached = 7;
7187 }
7188
7189 if (reached)
7190 break;
7191 }
7192 else if (BUFFERP (it->object)
7193 && it->method == GET_FROM_BUFFER
7194 && IT_CHARPOS (*it) >= to_charpos)
7195 skip = MOVE_POS_MATCH_OR_ZV;
7196 else
7197 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
7198
7199 switch (skip)
7200 {
7201 case MOVE_POS_MATCH_OR_ZV:
7202 reached = 8;
7203 goto out;
7204
7205 case MOVE_NEWLINE_OR_CR:
7206 set_iterator_to_next (it, 1);
7207 it->continuation_lines_width = 0;
7208 break;
7209
7210 case MOVE_LINE_TRUNCATED:
7211 it->continuation_lines_width = 0;
7212 reseat_at_next_visible_line_start (it, 0);
7213 if ((op & MOVE_TO_POS) != 0
7214 && IT_CHARPOS (*it) > to_charpos)
7215 {
7216 reached = 9;
7217 goto out;
7218 }
7219 break;
7220
7221 case MOVE_LINE_CONTINUED:
7222 /* For continued lines ending in a tab, some of the glyphs
7223 associated with the tab are displayed on the current
7224 line. Since it->current_x does not include these glyphs,
7225 we use it->last_visible_x instead. */
7226 it->continuation_lines_width +=
7227 (it->c == '\t') ? it->last_visible_x : it->current_x;
7228 break;
7229
7230 default:
7231 abort ();
7232 }
7233
7234 /* Reset/increment for the next run. */
7235 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
7236 it->current_x = it->hpos = 0;
7237 it->current_y += it->max_ascent + it->max_descent;
7238 ++it->vpos;
7239 last_height = it->max_ascent + it->max_descent;
7240 last_max_ascent = it->max_ascent;
7241 it->max_ascent = it->max_descent = 0;
7242 }
7243
7244 out:
7245
7246 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
7247 }
7248
7249
7250 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
7251
7252 If DY > 0, move IT backward at least that many pixels. DY = 0
7253 means move IT backward to the preceding line start or BEGV. This
7254 function may move over more than DY pixels if IT->current_y - DY
7255 ends up in the middle of a line; in this case IT->current_y will be
7256 set to the top of the line moved to. */
7257
7258 void
7259 move_it_vertically_backward (it, dy)
7260 struct it *it;
7261 int dy;
7262 {
7263 int nlines, h;
7264 struct it it2, it3;
7265 int start_pos;
7266
7267 move_further_back:
7268 xassert (dy >= 0);
7269
7270 start_pos = IT_CHARPOS (*it);
7271
7272 /* Estimate how many newlines we must move back. */
7273 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
7274
7275 /* Set the iterator's position that many lines back. */
7276 while (nlines-- && IT_CHARPOS (*it) > BEGV)
7277 back_to_previous_visible_line_start (it);
7278
7279 /* Reseat the iterator here. When moving backward, we don't want
7280 reseat to skip forward over invisible text, set up the iterator
7281 to deliver from overlay strings at the new position etc. So,
7282 use reseat_1 here. */
7283 reseat_1 (it, it->current.pos, 1);
7284
7285 /* We are now surely at a line start. */
7286 it->current_x = it->hpos = 0;
7287 it->continuation_lines_width = 0;
7288
7289 /* Move forward and see what y-distance we moved. First move to the
7290 start of the next line so that we get its height. We need this
7291 height to be able to tell whether we reached the specified
7292 y-distance. */
7293 it2 = *it;
7294 it2.max_ascent = it2.max_descent = 0;
7295 do
7296 {
7297 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
7298 MOVE_TO_POS | MOVE_TO_VPOS);
7299 }
7300 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
7301 xassert (IT_CHARPOS (*it) >= BEGV);
7302 it3 = it2;
7303
7304 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
7305 xassert (IT_CHARPOS (*it) >= BEGV);
7306 /* H is the actual vertical distance from the position in *IT
7307 and the starting position. */
7308 h = it2.current_y - it->current_y;
7309 /* NLINES is the distance in number of lines. */
7310 nlines = it2.vpos - it->vpos;
7311
7312 /* Correct IT's y and vpos position
7313 so that they are relative to the starting point. */
7314 it->vpos -= nlines;
7315 it->current_y -= h;
7316
7317 if (dy == 0)
7318 {
7319 /* DY == 0 means move to the start of the screen line. The
7320 value of nlines is > 0 if continuation lines were involved. */
7321 if (nlines > 0)
7322 move_it_by_lines (it, nlines, 1);
7323 #if 0
7324 /* I think this assert is bogus if buffer contains
7325 invisible text or images. KFS. */
7326 xassert (IT_CHARPOS (*it) <= start_pos);
7327 #endif
7328 }
7329 else
7330 {
7331 /* The y-position we try to reach, relative to *IT.
7332 Note that H has been subtracted in front of the if-statement. */
7333 int target_y = it->current_y + h - dy;
7334 int y0 = it3.current_y;
7335 int y1 = line_bottom_y (&it3);
7336 int line_height = y1 - y0;
7337
7338 /* If we did not reach target_y, try to move further backward if
7339 we can. If we moved too far backward, try to move forward. */
7340 if (target_y < it->current_y
7341 /* This is heuristic. In a window that's 3 lines high, with
7342 a line height of 13 pixels each, recentering with point
7343 on the bottom line will try to move -39/2 = 19 pixels
7344 backward. Try to avoid moving into the first line. */
7345 && (it->current_y - target_y
7346 > min (window_box_height (it->w), line_height * 2 / 3))
7347 && IT_CHARPOS (*it) > BEGV)
7348 {
7349 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
7350 target_y - it->current_y));
7351 dy = it->current_y - target_y;
7352 goto move_further_back;
7353 }
7354 else if (target_y >= it->current_y + line_height
7355 && IT_CHARPOS (*it) < ZV)
7356 {
7357 /* Should move forward by at least one line, maybe more.
7358
7359 Note: Calling move_it_by_lines can be expensive on
7360 terminal frames, where compute_motion is used (via
7361 vmotion) to do the job, when there are very long lines
7362 and truncate-lines is nil. That's the reason for
7363 treating terminal frames specially here. */
7364
7365 if (!FRAME_WINDOW_P (it->f))
7366 move_it_vertically (it, target_y - (it->current_y + line_height));
7367 else
7368 {
7369 do
7370 {
7371 move_it_by_lines (it, 1, 1);
7372 }
7373 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
7374 }
7375
7376 #if 0
7377 /* I think this assert is bogus if buffer contains
7378 invisible text or images. KFS. */
7379 xassert (IT_CHARPOS (*it) >= BEGV);
7380 #endif
7381 }
7382 }
7383 }
7384
7385
7386 /* Move IT by a specified amount of pixel lines DY. DY negative means
7387 move backwards. DY = 0 means move to start of screen line. At the
7388 end, IT will be on the start of a screen line. */
7389
7390 void
7391 move_it_vertically (it, dy)
7392 struct it *it;
7393 int dy;
7394 {
7395 if (dy <= 0)
7396 move_it_vertically_backward (it, -dy);
7397 else
7398 {
7399 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
7400 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7401 MOVE_TO_POS | MOVE_TO_Y);
7402 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7403
7404 /* If buffer ends in ZV without a newline, move to the start of
7405 the line to satisfy the post-condition. */
7406 if (IT_CHARPOS (*it) == ZV
7407 && ZV > BEGV
7408 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7409 move_it_by_lines (it, 0, 0);
7410 }
7411 }
7412
7413
7414 /* Move iterator IT past the end of the text line it is in. */
7415
7416 void
7417 move_it_past_eol (it)
7418 struct it *it;
7419 {
7420 enum move_it_result rc;
7421
7422 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7423 if (rc == MOVE_NEWLINE_OR_CR)
7424 set_iterator_to_next (it, 0);
7425 }
7426
7427
7428 #if 0 /* Currently not used. */
7429
7430 /* Return non-zero if some text between buffer positions START_CHARPOS
7431 and END_CHARPOS is invisible. IT->window is the window for text
7432 property lookup. */
7433
7434 static int
7435 invisible_text_between_p (it, start_charpos, end_charpos)
7436 struct it *it;
7437 int start_charpos, end_charpos;
7438 {
7439 Lisp_Object prop, limit;
7440 int invisible_found_p;
7441
7442 xassert (it != NULL && start_charpos <= end_charpos);
7443
7444 /* Is text at START invisible? */
7445 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7446 it->window);
7447 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7448 invisible_found_p = 1;
7449 else
7450 {
7451 limit = Fnext_single_char_property_change (make_number (start_charpos),
7452 Qinvisible, Qnil,
7453 make_number (end_charpos));
7454 invisible_found_p = XFASTINT (limit) < end_charpos;
7455 }
7456
7457 return invisible_found_p;
7458 }
7459
7460 #endif /* 0 */
7461
7462
7463 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7464 negative means move up. DVPOS == 0 means move to the start of the
7465 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7466 NEED_Y_P is zero, IT->current_y will be left unchanged.
7467
7468 Further optimization ideas: If we would know that IT->f doesn't use
7469 a face with proportional font, we could be faster for
7470 truncate-lines nil. */
7471
7472 void
7473 move_it_by_lines (it, dvpos, need_y_p)
7474 struct it *it;
7475 int dvpos, need_y_p;
7476 {
7477 struct position pos;
7478
7479 /* The commented-out optimization uses vmotion on terminals. This
7480 gives bad results, because elements like it->what, on which
7481 callers such as pos_visible_p rely, aren't updated. */
7482 /* if (!FRAME_WINDOW_P (it->f))
7483 {
7484 struct text_pos textpos;
7485
7486 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7487 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7488 reseat (it, textpos, 1);
7489 it->vpos += pos.vpos;
7490 it->current_y += pos.vpos;
7491 }
7492 else */
7493
7494 if (dvpos == 0)
7495 {
7496 /* DVPOS == 0 means move to the start of the screen line. */
7497 move_it_vertically_backward (it, 0);
7498 xassert (it->current_x == 0 && it->hpos == 0);
7499 /* Let next call to line_bottom_y calculate real line height */
7500 last_height = 0;
7501 }
7502 else if (dvpos > 0)
7503 {
7504 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7505 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7506 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7507 }
7508 else
7509 {
7510 struct it it2;
7511 int start_charpos, i;
7512
7513 /* Start at the beginning of the screen line containing IT's
7514 position. This may actually move vertically backwards,
7515 in case of overlays, so adjust dvpos accordingly. */
7516 dvpos += it->vpos;
7517 move_it_vertically_backward (it, 0);
7518 dvpos -= it->vpos;
7519
7520 /* Go back -DVPOS visible lines and reseat the iterator there. */
7521 start_charpos = IT_CHARPOS (*it);
7522 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7523 back_to_previous_visible_line_start (it);
7524 reseat (it, it->current.pos, 1);
7525
7526 /* Move further back if we end up in a string or an image. */
7527 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7528 {
7529 /* First try to move to start of display line. */
7530 dvpos += it->vpos;
7531 move_it_vertically_backward (it, 0);
7532 dvpos -= it->vpos;
7533 if (IT_POS_VALID_AFTER_MOVE_P (it))
7534 break;
7535 /* If start of line is still in string or image,
7536 move further back. */
7537 back_to_previous_visible_line_start (it);
7538 reseat (it, it->current.pos, 1);
7539 dvpos--;
7540 }
7541
7542 it->current_x = it->hpos = 0;
7543
7544 /* Above call may have moved too far if continuation lines
7545 are involved. Scan forward and see if it did. */
7546 it2 = *it;
7547 it2.vpos = it2.current_y = 0;
7548 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7549 it->vpos -= it2.vpos;
7550 it->current_y -= it2.current_y;
7551 it->current_x = it->hpos = 0;
7552
7553 /* If we moved too far back, move IT some lines forward. */
7554 if (it2.vpos > -dvpos)
7555 {
7556 int delta = it2.vpos + dvpos;
7557 it2 = *it;
7558 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7559 /* Move back again if we got too far ahead. */
7560 if (IT_CHARPOS (*it) >= start_charpos)
7561 *it = it2;
7562 }
7563 }
7564 }
7565
7566 /* Return 1 if IT points into the middle of a display vector. */
7567
7568 int
7569 in_display_vector_p (it)
7570 struct it *it;
7571 {
7572 return (it->method == GET_FROM_DISPLAY_VECTOR
7573 && it->current.dpvec_index > 0
7574 && it->dpvec + it->current.dpvec_index != it->dpend);
7575 }
7576
7577 \f
7578 /***********************************************************************
7579 Messages
7580 ***********************************************************************/
7581
7582
7583 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7584 to *Messages*. */
7585
7586 void
7587 add_to_log (format, arg1, arg2)
7588 char *format;
7589 Lisp_Object arg1, arg2;
7590 {
7591 Lisp_Object args[3];
7592 Lisp_Object msg, fmt;
7593 char *buffer;
7594 int len;
7595 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7596 USE_SAFE_ALLOCA;
7597
7598 /* Do nothing if called asynchronously. Inserting text into
7599 a buffer may call after-change-functions and alike and
7600 that would means running Lisp asynchronously. */
7601 if (handling_signal)
7602 return;
7603
7604 fmt = msg = Qnil;
7605 GCPRO4 (fmt, msg, arg1, arg2);
7606
7607 args[0] = fmt = build_string (format);
7608 args[1] = arg1;
7609 args[2] = arg2;
7610 msg = Fformat (3, args);
7611
7612 len = SBYTES (msg) + 1;
7613 SAFE_ALLOCA (buffer, char *, len);
7614 bcopy (SDATA (msg), buffer, len);
7615
7616 message_dolog (buffer, len - 1, 1, 0);
7617 SAFE_FREE ();
7618
7619 UNGCPRO;
7620 }
7621
7622
7623 /* Output a newline in the *Messages* buffer if "needs" one. */
7624
7625 void
7626 message_log_maybe_newline ()
7627 {
7628 if (message_log_need_newline)
7629 message_dolog ("", 0, 1, 0);
7630 }
7631
7632
7633 /* Add a string M of length NBYTES to the message log, optionally
7634 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7635 nonzero, means interpret the contents of M as multibyte. This
7636 function calls low-level routines in order to bypass text property
7637 hooks, etc. which might not be safe to run.
7638
7639 This may GC (insert may run before/after change hooks),
7640 so the buffer M must NOT point to a Lisp string. */
7641
7642 void
7643 message_dolog (m, nbytes, nlflag, multibyte)
7644 const char *m;
7645 int nbytes, nlflag, multibyte;
7646 {
7647 if (!NILP (Vmemory_full))
7648 return;
7649
7650 if (!NILP (Vmessage_log_max))
7651 {
7652 struct buffer *oldbuf;
7653 Lisp_Object oldpoint, oldbegv, oldzv;
7654 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7655 int point_at_end = 0;
7656 int zv_at_end = 0;
7657 Lisp_Object old_deactivate_mark, tem;
7658 struct gcpro gcpro1;
7659
7660 old_deactivate_mark = Vdeactivate_mark;
7661 oldbuf = current_buffer;
7662 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7663 current_buffer->undo_list = Qt;
7664
7665 oldpoint = message_dolog_marker1;
7666 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7667 oldbegv = message_dolog_marker2;
7668 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7669 oldzv = message_dolog_marker3;
7670 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7671 GCPRO1 (old_deactivate_mark);
7672
7673 if (PT == Z)
7674 point_at_end = 1;
7675 if (ZV == Z)
7676 zv_at_end = 1;
7677
7678 BEGV = BEG;
7679 BEGV_BYTE = BEG_BYTE;
7680 ZV = Z;
7681 ZV_BYTE = Z_BYTE;
7682 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7683
7684 /* Insert the string--maybe converting multibyte to single byte
7685 or vice versa, so that all the text fits the buffer. */
7686 if (multibyte
7687 && NILP (current_buffer->enable_multibyte_characters))
7688 {
7689 int i, c, char_bytes;
7690 unsigned char work[1];
7691
7692 /* Convert a multibyte string to single-byte
7693 for the *Message* buffer. */
7694 for (i = 0; i < nbytes; i += char_bytes)
7695 {
7696 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7697 work[0] = (ASCII_CHAR_P (c)
7698 ? c
7699 : multibyte_char_to_unibyte (c, Qnil));
7700 insert_1_both (work, 1, 1, 1, 0, 0);
7701 }
7702 }
7703 else if (! multibyte
7704 && ! NILP (current_buffer->enable_multibyte_characters))
7705 {
7706 int i, c, char_bytes;
7707 unsigned char *msg = (unsigned char *) m;
7708 unsigned char str[MAX_MULTIBYTE_LENGTH];
7709 /* Convert a single-byte string to multibyte
7710 for the *Message* buffer. */
7711 for (i = 0; i < nbytes; i++)
7712 {
7713 c = msg[i];
7714 c = unibyte_char_to_multibyte (c);
7715 char_bytes = CHAR_STRING (c, str);
7716 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7717 }
7718 }
7719 else if (nbytes)
7720 insert_1 (m, nbytes, 1, 0, 0);
7721
7722 if (nlflag)
7723 {
7724 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7725 insert_1 ("\n", 1, 1, 0, 0);
7726
7727 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7728 this_bol = PT;
7729 this_bol_byte = PT_BYTE;
7730
7731 /* See if this line duplicates the previous one.
7732 If so, combine duplicates. */
7733 if (this_bol > BEG)
7734 {
7735 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7736 prev_bol = PT;
7737 prev_bol_byte = PT_BYTE;
7738
7739 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7740 this_bol, this_bol_byte);
7741 if (dup)
7742 {
7743 del_range_both (prev_bol, prev_bol_byte,
7744 this_bol, this_bol_byte, 0);
7745 if (dup > 1)
7746 {
7747 char dupstr[40];
7748 int duplen;
7749
7750 /* If you change this format, don't forget to also
7751 change message_log_check_duplicate. */
7752 sprintf (dupstr, " [%d times]", dup);
7753 duplen = strlen (dupstr);
7754 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7755 insert_1 (dupstr, duplen, 1, 0, 1);
7756 }
7757 }
7758 }
7759
7760 /* If we have more than the desired maximum number of lines
7761 in the *Messages* buffer now, delete the oldest ones.
7762 This is safe because we don't have undo in this buffer. */
7763
7764 if (NATNUMP (Vmessage_log_max))
7765 {
7766 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7767 -XFASTINT (Vmessage_log_max) - 1, 0);
7768 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7769 }
7770 }
7771 BEGV = XMARKER (oldbegv)->charpos;
7772 BEGV_BYTE = marker_byte_position (oldbegv);
7773
7774 if (zv_at_end)
7775 {
7776 ZV = Z;
7777 ZV_BYTE = Z_BYTE;
7778 }
7779 else
7780 {
7781 ZV = XMARKER (oldzv)->charpos;
7782 ZV_BYTE = marker_byte_position (oldzv);
7783 }
7784
7785 if (point_at_end)
7786 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7787 else
7788 /* We can't do Fgoto_char (oldpoint) because it will run some
7789 Lisp code. */
7790 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7791 XMARKER (oldpoint)->bytepos);
7792
7793 UNGCPRO;
7794 unchain_marker (XMARKER (oldpoint));
7795 unchain_marker (XMARKER (oldbegv));
7796 unchain_marker (XMARKER (oldzv));
7797
7798 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7799 set_buffer_internal (oldbuf);
7800 if (NILP (tem))
7801 windows_or_buffers_changed = old_windows_or_buffers_changed;
7802 message_log_need_newline = !nlflag;
7803 Vdeactivate_mark = old_deactivate_mark;
7804 }
7805 }
7806
7807
7808 /* We are at the end of the buffer after just having inserted a newline.
7809 (Note: We depend on the fact we won't be crossing the gap.)
7810 Check to see if the most recent message looks a lot like the previous one.
7811 Return 0 if different, 1 if the new one should just replace it, or a
7812 value N > 1 if we should also append " [N times]". */
7813
7814 static int
7815 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7816 int prev_bol, this_bol;
7817 int prev_bol_byte, this_bol_byte;
7818 {
7819 int i;
7820 int len = Z_BYTE - 1 - this_bol_byte;
7821 int seen_dots = 0;
7822 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7823 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7824
7825 for (i = 0; i < len; i++)
7826 {
7827 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7828 seen_dots = 1;
7829 if (p1[i] != p2[i])
7830 return seen_dots;
7831 }
7832 p1 += len;
7833 if (*p1 == '\n')
7834 return 2;
7835 if (*p1++ == ' ' && *p1++ == '[')
7836 {
7837 int n = 0;
7838 while (*p1 >= '0' && *p1 <= '9')
7839 n = n * 10 + *p1++ - '0';
7840 if (strncmp (p1, " times]\n", 8) == 0)
7841 return n+1;
7842 }
7843 return 0;
7844 }
7845 \f
7846
7847 /* Display an echo area message M with a specified length of NBYTES
7848 bytes. The string may include null characters. If M is 0, clear
7849 out any existing message, and let the mini-buffer text show
7850 through.
7851
7852 This may GC, so the buffer M must NOT point to a Lisp string. */
7853
7854 void
7855 message2 (m, nbytes, multibyte)
7856 const char *m;
7857 int nbytes;
7858 int multibyte;
7859 {
7860 /* First flush out any partial line written with print. */
7861 message_log_maybe_newline ();
7862 if (m)
7863 message_dolog (m, nbytes, 1, multibyte);
7864 message2_nolog (m, nbytes, multibyte);
7865 }
7866
7867
7868 /* The non-logging counterpart of message2. */
7869
7870 void
7871 message2_nolog (m, nbytes, multibyte)
7872 const char *m;
7873 int nbytes, multibyte;
7874 {
7875 struct frame *sf = SELECTED_FRAME ();
7876 message_enable_multibyte = multibyte;
7877
7878 if (noninteractive)
7879 {
7880 if (noninteractive_need_newline)
7881 putc ('\n', stderr);
7882 noninteractive_need_newline = 0;
7883 if (m)
7884 fwrite (m, nbytes, 1, stderr);
7885 if (cursor_in_echo_area == 0)
7886 fprintf (stderr, "\n");
7887 fflush (stderr);
7888 }
7889 /* A null message buffer means that the frame hasn't really been
7890 initialized yet. Error messages get reported properly by
7891 cmd_error, so this must be just an informative message; toss it. */
7892 else if (INTERACTIVE
7893 && sf->glyphs_initialized_p
7894 && FRAME_MESSAGE_BUF (sf))
7895 {
7896 Lisp_Object mini_window;
7897 struct frame *f;
7898
7899 /* Get the frame containing the mini-buffer
7900 that the selected frame is using. */
7901 mini_window = FRAME_MINIBUF_WINDOW (sf);
7902 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7903
7904 FRAME_SAMPLE_VISIBILITY (f);
7905 if (FRAME_VISIBLE_P (sf)
7906 && ! FRAME_VISIBLE_P (f))
7907 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7908
7909 if (m)
7910 {
7911 set_message (m, Qnil, nbytes, multibyte);
7912 if (minibuffer_auto_raise)
7913 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7914 }
7915 else
7916 clear_message (1, 1);
7917
7918 do_pending_window_change (0);
7919 echo_area_display (1);
7920 do_pending_window_change (0);
7921 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
7922 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
7923 }
7924 }
7925
7926
7927 /* Display an echo area message M with a specified length of NBYTES
7928 bytes. The string may include null characters. If M is not a
7929 string, clear out any existing message, and let the mini-buffer
7930 text show through.
7931
7932 This function cancels echoing. */
7933
7934 void
7935 message3 (m, nbytes, multibyte)
7936 Lisp_Object m;
7937 int nbytes;
7938 int multibyte;
7939 {
7940 struct gcpro gcpro1;
7941
7942 GCPRO1 (m);
7943 clear_message (1,1);
7944 cancel_echoing ();
7945
7946 /* First flush out any partial line written with print. */
7947 message_log_maybe_newline ();
7948 if (STRINGP (m))
7949 {
7950 char *buffer;
7951 USE_SAFE_ALLOCA;
7952
7953 SAFE_ALLOCA (buffer, char *, nbytes);
7954 bcopy (SDATA (m), buffer, nbytes);
7955 message_dolog (buffer, nbytes, 1, multibyte);
7956 SAFE_FREE ();
7957 }
7958 message3_nolog (m, nbytes, multibyte);
7959
7960 UNGCPRO;
7961 }
7962
7963
7964 /* The non-logging version of message3.
7965 This does not cancel echoing, because it is used for echoing.
7966 Perhaps we need to make a separate function for echoing
7967 and make this cancel echoing. */
7968
7969 void
7970 message3_nolog (m, nbytes, multibyte)
7971 Lisp_Object m;
7972 int nbytes, multibyte;
7973 {
7974 struct frame *sf = SELECTED_FRAME ();
7975 message_enable_multibyte = multibyte;
7976
7977 if (noninteractive)
7978 {
7979 if (noninteractive_need_newline)
7980 putc ('\n', stderr);
7981 noninteractive_need_newline = 0;
7982 if (STRINGP (m))
7983 fwrite (SDATA (m), nbytes, 1, stderr);
7984 if (cursor_in_echo_area == 0)
7985 fprintf (stderr, "\n");
7986 fflush (stderr);
7987 }
7988 /* A null message buffer means that the frame hasn't really been
7989 initialized yet. Error messages get reported properly by
7990 cmd_error, so this must be just an informative message; toss it. */
7991 else if (INTERACTIVE
7992 && sf->glyphs_initialized_p
7993 && FRAME_MESSAGE_BUF (sf))
7994 {
7995 Lisp_Object mini_window;
7996 Lisp_Object frame;
7997 struct frame *f;
7998
7999 /* Get the frame containing the mini-buffer
8000 that the selected frame is using. */
8001 mini_window = FRAME_MINIBUF_WINDOW (sf);
8002 frame = XWINDOW (mini_window)->frame;
8003 f = XFRAME (frame);
8004
8005 FRAME_SAMPLE_VISIBILITY (f);
8006 if (FRAME_VISIBLE_P (sf)
8007 && !FRAME_VISIBLE_P (f))
8008 Fmake_frame_visible (frame);
8009
8010 if (STRINGP (m) && SCHARS (m) > 0)
8011 {
8012 set_message (NULL, m, nbytes, multibyte);
8013 if (minibuffer_auto_raise)
8014 Fraise_frame (frame);
8015 /* Assume we are not echoing.
8016 (If we are, echo_now will override this.) */
8017 echo_message_buffer = Qnil;
8018 }
8019 else
8020 clear_message (1, 1);
8021
8022 do_pending_window_change (0);
8023 echo_area_display (1);
8024 do_pending_window_change (0);
8025 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8026 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8027 }
8028 }
8029
8030
8031 /* Display a null-terminated echo area message M. If M is 0, clear
8032 out any existing message, and let the mini-buffer text show through.
8033
8034 The buffer M must continue to exist until after the echo area gets
8035 cleared or some other message gets displayed there. Do not pass
8036 text that is stored in a Lisp string. Do not pass text in a buffer
8037 that was alloca'd. */
8038
8039 void
8040 message1 (m)
8041 char *m;
8042 {
8043 message2 (m, (m ? strlen (m) : 0), 0);
8044 }
8045
8046
8047 /* The non-logging counterpart of message1. */
8048
8049 void
8050 message1_nolog (m)
8051 char *m;
8052 {
8053 message2_nolog (m, (m ? strlen (m) : 0), 0);
8054 }
8055
8056 /* Display a message M which contains a single %s
8057 which gets replaced with STRING. */
8058
8059 void
8060 message_with_string (m, string, log)
8061 char *m;
8062 Lisp_Object string;
8063 int log;
8064 {
8065 CHECK_STRING (string);
8066
8067 if (noninteractive)
8068 {
8069 if (m)
8070 {
8071 if (noninteractive_need_newline)
8072 putc ('\n', stderr);
8073 noninteractive_need_newline = 0;
8074 fprintf (stderr, m, SDATA (string));
8075 if (cursor_in_echo_area == 0)
8076 fprintf (stderr, "\n");
8077 fflush (stderr);
8078 }
8079 }
8080 else if (INTERACTIVE)
8081 {
8082 /* The frame whose minibuffer we're going to display the message on.
8083 It may be larger than the selected frame, so we need
8084 to use its buffer, not the selected frame's buffer. */
8085 Lisp_Object mini_window;
8086 struct frame *f, *sf = SELECTED_FRAME ();
8087
8088 /* Get the frame containing the minibuffer
8089 that the selected frame is using. */
8090 mini_window = FRAME_MINIBUF_WINDOW (sf);
8091 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8092
8093 /* A null message buffer means that the frame hasn't really been
8094 initialized yet. Error messages get reported properly by
8095 cmd_error, so this must be just an informative message; toss it. */
8096 if (FRAME_MESSAGE_BUF (f))
8097 {
8098 Lisp_Object args[2], message;
8099 struct gcpro gcpro1, gcpro2;
8100
8101 args[0] = build_string (m);
8102 args[1] = message = string;
8103 GCPRO2 (args[0], message);
8104 gcpro1.nvars = 2;
8105
8106 message = Fformat (2, args);
8107
8108 if (log)
8109 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
8110 else
8111 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
8112
8113 UNGCPRO;
8114
8115 /* Print should start at the beginning of the message
8116 buffer next time. */
8117 message_buf_print = 0;
8118 }
8119 }
8120 }
8121
8122
8123 /* Dump an informative message to the minibuf. If M is 0, clear out
8124 any existing message, and let the mini-buffer text show through. */
8125
8126 /* VARARGS 1 */
8127 void
8128 message (m, a1, a2, a3)
8129 char *m;
8130 EMACS_INT a1, a2, a3;
8131 {
8132 if (noninteractive)
8133 {
8134 if (m)
8135 {
8136 if (noninteractive_need_newline)
8137 putc ('\n', stderr);
8138 noninteractive_need_newline = 0;
8139 fprintf (stderr, m, a1, a2, a3);
8140 if (cursor_in_echo_area == 0)
8141 fprintf (stderr, "\n");
8142 fflush (stderr);
8143 }
8144 }
8145 else if (INTERACTIVE)
8146 {
8147 /* The frame whose mini-buffer we're going to display the message
8148 on. It may be larger than the selected frame, so we need to
8149 use its buffer, not the selected frame's buffer. */
8150 Lisp_Object mini_window;
8151 struct frame *f, *sf = SELECTED_FRAME ();
8152
8153 /* Get the frame containing the mini-buffer
8154 that the selected frame is using. */
8155 mini_window = FRAME_MINIBUF_WINDOW (sf);
8156 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8157
8158 /* A null message buffer means that the frame hasn't really been
8159 initialized yet. Error messages get reported properly by
8160 cmd_error, so this must be just an informative message; toss
8161 it. */
8162 if (FRAME_MESSAGE_BUF (f))
8163 {
8164 if (m)
8165 {
8166 int len;
8167 #ifdef NO_ARG_ARRAY
8168 char *a[3];
8169 a[0] = (char *) a1;
8170 a[1] = (char *) a2;
8171 a[2] = (char *) a3;
8172
8173 len = doprnt (FRAME_MESSAGE_BUF (f),
8174 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
8175 #else
8176 len = doprnt (FRAME_MESSAGE_BUF (f),
8177 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
8178 (char **) &a1);
8179 #endif /* NO_ARG_ARRAY */
8180
8181 message2 (FRAME_MESSAGE_BUF (f), len, 0);
8182 }
8183 else
8184 message1 (0);
8185
8186 /* Print should start at the beginning of the message
8187 buffer next time. */
8188 message_buf_print = 0;
8189 }
8190 }
8191 }
8192
8193
8194 /* The non-logging version of message. */
8195
8196 void
8197 message_nolog (m, a1, a2, a3)
8198 char *m;
8199 EMACS_INT a1, a2, a3;
8200 {
8201 Lisp_Object old_log_max;
8202 old_log_max = Vmessage_log_max;
8203 Vmessage_log_max = Qnil;
8204 message (m, a1, a2, a3);
8205 Vmessage_log_max = old_log_max;
8206 }
8207
8208
8209 /* Display the current message in the current mini-buffer. This is
8210 only called from error handlers in process.c, and is not time
8211 critical. */
8212
8213 void
8214 update_echo_area ()
8215 {
8216 if (!NILP (echo_area_buffer[0]))
8217 {
8218 Lisp_Object string;
8219 string = Fcurrent_message ();
8220 message3 (string, SBYTES (string),
8221 !NILP (current_buffer->enable_multibyte_characters));
8222 }
8223 }
8224
8225
8226 /* Make sure echo area buffers in `echo_buffers' are live.
8227 If they aren't, make new ones. */
8228
8229 static void
8230 ensure_echo_area_buffers ()
8231 {
8232 int i;
8233
8234 for (i = 0; i < 2; ++i)
8235 if (!BUFFERP (echo_buffer[i])
8236 || NILP (XBUFFER (echo_buffer[i])->name))
8237 {
8238 char name[30];
8239 Lisp_Object old_buffer;
8240 int j;
8241
8242 old_buffer = echo_buffer[i];
8243 sprintf (name, " *Echo Area %d*", i);
8244 echo_buffer[i] = Fget_buffer_create (build_string (name));
8245 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
8246
8247 for (j = 0; j < 2; ++j)
8248 if (EQ (old_buffer, echo_area_buffer[j]))
8249 echo_area_buffer[j] = echo_buffer[i];
8250 }
8251 }
8252
8253
8254 /* Call FN with args A1..A4 with either the current or last displayed
8255 echo_area_buffer as current buffer.
8256
8257 WHICH zero means use the current message buffer
8258 echo_area_buffer[0]. If that is nil, choose a suitable buffer
8259 from echo_buffer[] and clear it.
8260
8261 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
8262 suitable buffer from echo_buffer[] and clear it.
8263
8264 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
8265 that the current message becomes the last displayed one, make
8266 choose a suitable buffer for echo_area_buffer[0], and clear it.
8267
8268 Value is what FN returns. */
8269
8270 static int
8271 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
8272 struct window *w;
8273 int which;
8274 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
8275 EMACS_INT a1;
8276 Lisp_Object a2;
8277 EMACS_INT a3, a4;
8278 {
8279 Lisp_Object buffer;
8280 int this_one, the_other, clear_buffer_p, rc;
8281 int count = SPECPDL_INDEX ();
8282
8283 /* If buffers aren't live, make new ones. */
8284 ensure_echo_area_buffers ();
8285
8286 clear_buffer_p = 0;
8287
8288 if (which == 0)
8289 this_one = 0, the_other = 1;
8290 else if (which > 0)
8291 this_one = 1, the_other = 0;
8292 else
8293 {
8294 this_one = 0, the_other = 1;
8295 clear_buffer_p = 1;
8296
8297 /* We need a fresh one in case the current echo buffer equals
8298 the one containing the last displayed echo area message. */
8299 if (!NILP (echo_area_buffer[this_one])
8300 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
8301 echo_area_buffer[this_one] = Qnil;
8302 }
8303
8304 /* Choose a suitable buffer from echo_buffer[] is we don't
8305 have one. */
8306 if (NILP (echo_area_buffer[this_one]))
8307 {
8308 echo_area_buffer[this_one]
8309 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
8310 ? echo_buffer[the_other]
8311 : echo_buffer[this_one]);
8312 clear_buffer_p = 1;
8313 }
8314
8315 buffer = echo_area_buffer[this_one];
8316
8317 /* Don't get confused by reusing the buffer used for echoing
8318 for a different purpose. */
8319 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
8320 cancel_echoing ();
8321
8322 record_unwind_protect (unwind_with_echo_area_buffer,
8323 with_echo_area_buffer_unwind_data (w));
8324
8325 /* Make the echo area buffer current. Note that for display
8326 purposes, it is not necessary that the displayed window's buffer
8327 == current_buffer, except for text property lookup. So, let's
8328 only set that buffer temporarily here without doing a full
8329 Fset_window_buffer. We must also change w->pointm, though,
8330 because otherwise an assertions in unshow_buffer fails, and Emacs
8331 aborts. */
8332 set_buffer_internal_1 (XBUFFER (buffer));
8333 if (w)
8334 {
8335 w->buffer = buffer;
8336 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
8337 }
8338
8339 current_buffer->undo_list = Qt;
8340 current_buffer->read_only = Qnil;
8341 specbind (Qinhibit_read_only, Qt);
8342 specbind (Qinhibit_modification_hooks, Qt);
8343
8344 if (clear_buffer_p && Z > BEG)
8345 del_range (BEG, Z);
8346
8347 xassert (BEGV >= BEG);
8348 xassert (ZV <= Z && ZV >= BEGV);
8349
8350 rc = fn (a1, a2, a3, a4);
8351
8352 xassert (BEGV >= BEG);
8353 xassert (ZV <= Z && ZV >= BEGV);
8354
8355 unbind_to (count, Qnil);
8356 return rc;
8357 }
8358
8359
8360 /* Save state that should be preserved around the call to the function
8361 FN called in with_echo_area_buffer. */
8362
8363 static Lisp_Object
8364 with_echo_area_buffer_unwind_data (w)
8365 struct window *w;
8366 {
8367 int i = 0;
8368 Lisp_Object vector, tmp;
8369
8370 /* Reduce consing by keeping one vector in
8371 Vwith_echo_area_save_vector. */
8372 vector = Vwith_echo_area_save_vector;
8373 Vwith_echo_area_save_vector = Qnil;
8374
8375 if (NILP (vector))
8376 vector = Fmake_vector (make_number (7), Qnil);
8377
8378 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
8379 ASET (vector, i, Vdeactivate_mark); ++i;
8380 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
8381
8382 if (w)
8383 {
8384 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
8385 ASET (vector, i, w->buffer); ++i;
8386 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
8387 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
8388 }
8389 else
8390 {
8391 int end = i + 4;
8392 for (; i < end; ++i)
8393 ASET (vector, i, Qnil);
8394 }
8395
8396 xassert (i == ASIZE (vector));
8397 return vector;
8398 }
8399
8400
8401 /* Restore global state from VECTOR which was created by
8402 with_echo_area_buffer_unwind_data. */
8403
8404 static Lisp_Object
8405 unwind_with_echo_area_buffer (vector)
8406 Lisp_Object vector;
8407 {
8408 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
8409 Vdeactivate_mark = AREF (vector, 1);
8410 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
8411
8412 if (WINDOWP (AREF (vector, 3)))
8413 {
8414 struct window *w;
8415 Lisp_Object buffer, charpos, bytepos;
8416
8417 w = XWINDOW (AREF (vector, 3));
8418 buffer = AREF (vector, 4);
8419 charpos = AREF (vector, 5);
8420 bytepos = AREF (vector, 6);
8421
8422 w->buffer = buffer;
8423 set_marker_both (w->pointm, buffer,
8424 XFASTINT (charpos), XFASTINT (bytepos));
8425 }
8426
8427 Vwith_echo_area_save_vector = vector;
8428 return Qnil;
8429 }
8430
8431
8432 /* Set up the echo area for use by print functions. MULTIBYTE_P
8433 non-zero means we will print multibyte. */
8434
8435 void
8436 setup_echo_area_for_printing (multibyte_p)
8437 int multibyte_p;
8438 {
8439 /* If we can't find an echo area any more, exit. */
8440 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8441 Fkill_emacs (Qnil);
8442
8443 ensure_echo_area_buffers ();
8444
8445 if (!message_buf_print)
8446 {
8447 /* A message has been output since the last time we printed.
8448 Choose a fresh echo area buffer. */
8449 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8450 echo_area_buffer[0] = echo_buffer[1];
8451 else
8452 echo_area_buffer[0] = echo_buffer[0];
8453
8454 /* Switch to that buffer and clear it. */
8455 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8456 current_buffer->truncate_lines = Qnil;
8457
8458 if (Z > BEG)
8459 {
8460 int count = SPECPDL_INDEX ();
8461 specbind (Qinhibit_read_only, Qt);
8462 /* Note that undo recording is always disabled. */
8463 del_range (BEG, Z);
8464 unbind_to (count, Qnil);
8465 }
8466 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8467
8468 /* Set up the buffer for the multibyteness we need. */
8469 if (multibyte_p
8470 != !NILP (current_buffer->enable_multibyte_characters))
8471 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8472
8473 /* Raise the frame containing the echo area. */
8474 if (minibuffer_auto_raise)
8475 {
8476 struct frame *sf = SELECTED_FRAME ();
8477 Lisp_Object mini_window;
8478 mini_window = FRAME_MINIBUF_WINDOW (sf);
8479 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8480 }
8481
8482 message_log_maybe_newline ();
8483 message_buf_print = 1;
8484 }
8485 else
8486 {
8487 if (NILP (echo_area_buffer[0]))
8488 {
8489 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8490 echo_area_buffer[0] = echo_buffer[1];
8491 else
8492 echo_area_buffer[0] = echo_buffer[0];
8493 }
8494
8495 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8496 {
8497 /* Someone switched buffers between print requests. */
8498 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8499 current_buffer->truncate_lines = Qnil;
8500 }
8501 }
8502 }
8503
8504
8505 /* Display an echo area message in window W. Value is non-zero if W's
8506 height is changed. If display_last_displayed_message_p is
8507 non-zero, display the message that was last displayed, otherwise
8508 display the current message. */
8509
8510 static int
8511 display_echo_area (w)
8512 struct window *w;
8513 {
8514 int i, no_message_p, window_height_changed_p, count;
8515
8516 /* Temporarily disable garbage collections while displaying the echo
8517 area. This is done because a GC can print a message itself.
8518 That message would modify the echo area buffer's contents while a
8519 redisplay of the buffer is going on, and seriously confuse
8520 redisplay. */
8521 count = inhibit_garbage_collection ();
8522
8523 /* If there is no message, we must call display_echo_area_1
8524 nevertheless because it resizes the window. But we will have to
8525 reset the echo_area_buffer in question to nil at the end because
8526 with_echo_area_buffer will sets it to an empty buffer. */
8527 i = display_last_displayed_message_p ? 1 : 0;
8528 no_message_p = NILP (echo_area_buffer[i]);
8529
8530 window_height_changed_p
8531 = with_echo_area_buffer (w, display_last_displayed_message_p,
8532 display_echo_area_1,
8533 (EMACS_INT) w, Qnil, 0, 0);
8534
8535 if (no_message_p)
8536 echo_area_buffer[i] = Qnil;
8537
8538 unbind_to (count, Qnil);
8539 return window_height_changed_p;
8540 }
8541
8542
8543 /* Helper for display_echo_area. Display the current buffer which
8544 contains the current echo area message in window W, a mini-window,
8545 a pointer to which is passed in A1. A2..A4 are currently not used.
8546 Change the height of W so that all of the message is displayed.
8547 Value is non-zero if height of W was changed. */
8548
8549 static int
8550 display_echo_area_1 (a1, a2, a3, a4)
8551 EMACS_INT a1;
8552 Lisp_Object a2;
8553 EMACS_INT a3, a4;
8554 {
8555 struct window *w = (struct window *) a1;
8556 Lisp_Object window;
8557 struct text_pos start;
8558 int window_height_changed_p = 0;
8559
8560 /* Do this before displaying, so that we have a large enough glyph
8561 matrix for the display. If we can't get enough space for the
8562 whole text, display the last N lines. That works by setting w->start. */
8563 window_height_changed_p = resize_mini_window (w, 0);
8564
8565 /* Use the starting position chosen by resize_mini_window. */
8566 SET_TEXT_POS_FROM_MARKER (start, w->start);
8567
8568 /* Display. */
8569 clear_glyph_matrix (w->desired_matrix);
8570 XSETWINDOW (window, w);
8571 try_window (window, start, 0);
8572
8573 return window_height_changed_p;
8574 }
8575
8576
8577 /* Resize the echo area window to exactly the size needed for the
8578 currently displayed message, if there is one. If a mini-buffer
8579 is active, don't shrink it. */
8580
8581 void
8582 resize_echo_area_exactly ()
8583 {
8584 if (BUFFERP (echo_area_buffer[0])
8585 && WINDOWP (echo_area_window))
8586 {
8587 struct window *w = XWINDOW (echo_area_window);
8588 int resized_p;
8589 Lisp_Object resize_exactly;
8590
8591 if (minibuf_level == 0)
8592 resize_exactly = Qt;
8593 else
8594 resize_exactly = Qnil;
8595
8596 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8597 (EMACS_INT) w, resize_exactly, 0, 0);
8598 if (resized_p)
8599 {
8600 ++windows_or_buffers_changed;
8601 ++update_mode_lines;
8602 redisplay_internal (0);
8603 }
8604 }
8605 }
8606
8607
8608 /* Callback function for with_echo_area_buffer, when used from
8609 resize_echo_area_exactly. A1 contains a pointer to the window to
8610 resize, EXACTLY non-nil means resize the mini-window exactly to the
8611 size of the text displayed. A3 and A4 are not used. Value is what
8612 resize_mini_window returns. */
8613
8614 static int
8615 resize_mini_window_1 (a1, exactly, a3, a4)
8616 EMACS_INT a1;
8617 Lisp_Object exactly;
8618 EMACS_INT a3, a4;
8619 {
8620 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8621 }
8622
8623
8624 /* Resize mini-window W to fit the size of its contents. EXACT_P
8625 means size the window exactly to the size needed. Otherwise, it's
8626 only enlarged until W's buffer is empty.
8627
8628 Set W->start to the right place to begin display. If the whole
8629 contents fit, start at the beginning. Otherwise, start so as
8630 to make the end of the contents appear. This is particularly
8631 important for y-or-n-p, but seems desirable generally.
8632
8633 Value is non-zero if the window height has been changed. */
8634
8635 int
8636 resize_mini_window (w, exact_p)
8637 struct window *w;
8638 int exact_p;
8639 {
8640 struct frame *f = XFRAME (w->frame);
8641 int window_height_changed_p = 0;
8642
8643 xassert (MINI_WINDOW_P (w));
8644
8645 /* By default, start display at the beginning. */
8646 set_marker_both (w->start, w->buffer,
8647 BUF_BEGV (XBUFFER (w->buffer)),
8648 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8649
8650 /* Don't resize windows while redisplaying a window; it would
8651 confuse redisplay functions when the size of the window they are
8652 displaying changes from under them. Such a resizing can happen,
8653 for instance, when which-func prints a long message while
8654 we are running fontification-functions. We're running these
8655 functions with safe_call which binds inhibit-redisplay to t. */
8656 if (!NILP (Vinhibit_redisplay))
8657 return 0;
8658
8659 /* Nil means don't try to resize. */
8660 if (NILP (Vresize_mini_windows)
8661 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8662 return 0;
8663
8664 if (!FRAME_MINIBUF_ONLY_P (f))
8665 {
8666 struct it it;
8667 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8668 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8669 int height, max_height;
8670 int unit = FRAME_LINE_HEIGHT (f);
8671 struct text_pos start;
8672 struct buffer *old_current_buffer = NULL;
8673
8674 if (current_buffer != XBUFFER (w->buffer))
8675 {
8676 old_current_buffer = current_buffer;
8677 set_buffer_internal (XBUFFER (w->buffer));
8678 }
8679
8680 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8681
8682 /* Compute the max. number of lines specified by the user. */
8683 if (FLOATP (Vmax_mini_window_height))
8684 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8685 else if (INTEGERP (Vmax_mini_window_height))
8686 max_height = XINT (Vmax_mini_window_height);
8687 else
8688 max_height = total_height / 4;
8689
8690 /* Correct that max. height if it's bogus. */
8691 max_height = max (1, max_height);
8692 max_height = min (total_height, max_height);
8693
8694 /* Find out the height of the text in the window. */
8695 if (it.line_wrap == TRUNCATE)
8696 height = 1;
8697 else
8698 {
8699 last_height = 0;
8700 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8701 if (it.max_ascent == 0 && it.max_descent == 0)
8702 height = it.current_y + last_height;
8703 else
8704 height = it.current_y + it.max_ascent + it.max_descent;
8705 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8706 height = (height + unit - 1) / unit;
8707 }
8708
8709 /* Compute a suitable window start. */
8710 if (height > max_height)
8711 {
8712 height = max_height;
8713 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8714 move_it_vertically_backward (&it, (height - 1) * unit);
8715 start = it.current.pos;
8716 }
8717 else
8718 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8719 SET_MARKER_FROM_TEXT_POS (w->start, start);
8720
8721 if (EQ (Vresize_mini_windows, Qgrow_only))
8722 {
8723 /* Let it grow only, until we display an empty message, in which
8724 case the window shrinks again. */
8725 if (height > WINDOW_TOTAL_LINES (w))
8726 {
8727 int old_height = WINDOW_TOTAL_LINES (w);
8728 freeze_window_starts (f, 1);
8729 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8730 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8731 }
8732 else if (height < WINDOW_TOTAL_LINES (w)
8733 && (exact_p || BEGV == ZV))
8734 {
8735 int old_height = WINDOW_TOTAL_LINES (w);
8736 freeze_window_starts (f, 0);
8737 shrink_mini_window (w);
8738 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8739 }
8740 }
8741 else
8742 {
8743 /* Always resize to exact size needed. */
8744 if (height > WINDOW_TOTAL_LINES (w))
8745 {
8746 int old_height = WINDOW_TOTAL_LINES (w);
8747 freeze_window_starts (f, 1);
8748 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8749 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8750 }
8751 else if (height < WINDOW_TOTAL_LINES (w))
8752 {
8753 int old_height = WINDOW_TOTAL_LINES (w);
8754 freeze_window_starts (f, 0);
8755 shrink_mini_window (w);
8756
8757 if (height)
8758 {
8759 freeze_window_starts (f, 1);
8760 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8761 }
8762
8763 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8764 }
8765 }
8766
8767 if (old_current_buffer)
8768 set_buffer_internal (old_current_buffer);
8769 }
8770
8771 return window_height_changed_p;
8772 }
8773
8774
8775 /* Value is the current message, a string, or nil if there is no
8776 current message. */
8777
8778 Lisp_Object
8779 current_message ()
8780 {
8781 Lisp_Object msg;
8782
8783 if (!BUFFERP (echo_area_buffer[0]))
8784 msg = Qnil;
8785 else
8786 {
8787 with_echo_area_buffer (0, 0, current_message_1,
8788 (EMACS_INT) &msg, Qnil, 0, 0);
8789 if (NILP (msg))
8790 echo_area_buffer[0] = Qnil;
8791 }
8792
8793 return msg;
8794 }
8795
8796
8797 static int
8798 current_message_1 (a1, a2, a3, a4)
8799 EMACS_INT a1;
8800 Lisp_Object a2;
8801 EMACS_INT a3, a4;
8802 {
8803 Lisp_Object *msg = (Lisp_Object *) a1;
8804
8805 if (Z > BEG)
8806 *msg = make_buffer_string (BEG, Z, 1);
8807 else
8808 *msg = Qnil;
8809 return 0;
8810 }
8811
8812
8813 /* Push the current message on Vmessage_stack for later restauration
8814 by restore_message. Value is non-zero if the current message isn't
8815 empty. This is a relatively infrequent operation, so it's not
8816 worth optimizing. */
8817
8818 int
8819 push_message ()
8820 {
8821 Lisp_Object msg;
8822 msg = current_message ();
8823 Vmessage_stack = Fcons (msg, Vmessage_stack);
8824 return STRINGP (msg);
8825 }
8826
8827
8828 /* Restore message display from the top of Vmessage_stack. */
8829
8830 void
8831 restore_message ()
8832 {
8833 Lisp_Object msg;
8834
8835 xassert (CONSP (Vmessage_stack));
8836 msg = XCAR (Vmessage_stack);
8837 if (STRINGP (msg))
8838 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8839 else
8840 message3_nolog (msg, 0, 0);
8841 }
8842
8843
8844 /* Handler for record_unwind_protect calling pop_message. */
8845
8846 Lisp_Object
8847 pop_message_unwind (dummy)
8848 Lisp_Object dummy;
8849 {
8850 pop_message ();
8851 return Qnil;
8852 }
8853
8854 /* Pop the top-most entry off Vmessage_stack. */
8855
8856 void
8857 pop_message ()
8858 {
8859 xassert (CONSP (Vmessage_stack));
8860 Vmessage_stack = XCDR (Vmessage_stack);
8861 }
8862
8863
8864 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8865 exits. If the stack is not empty, we have a missing pop_message
8866 somewhere. */
8867
8868 void
8869 check_message_stack ()
8870 {
8871 if (!NILP (Vmessage_stack))
8872 abort ();
8873 }
8874
8875
8876 /* Truncate to NCHARS what will be displayed in the echo area the next
8877 time we display it---but don't redisplay it now. */
8878
8879 void
8880 truncate_echo_area (nchars)
8881 int nchars;
8882 {
8883 if (nchars == 0)
8884 echo_area_buffer[0] = Qnil;
8885 /* A null message buffer means that the frame hasn't really been
8886 initialized yet. Error messages get reported properly by
8887 cmd_error, so this must be just an informative message; toss it. */
8888 else if (!noninteractive
8889 && INTERACTIVE
8890 && !NILP (echo_area_buffer[0]))
8891 {
8892 struct frame *sf = SELECTED_FRAME ();
8893 if (FRAME_MESSAGE_BUF (sf))
8894 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8895 }
8896 }
8897
8898
8899 /* Helper function for truncate_echo_area. Truncate the current
8900 message to at most NCHARS characters. */
8901
8902 static int
8903 truncate_message_1 (nchars, a2, a3, a4)
8904 EMACS_INT nchars;
8905 Lisp_Object a2;
8906 EMACS_INT a3, a4;
8907 {
8908 if (BEG + nchars < Z)
8909 del_range (BEG + nchars, Z);
8910 if (Z == BEG)
8911 echo_area_buffer[0] = Qnil;
8912 return 0;
8913 }
8914
8915
8916 /* Set the current message to a substring of S or STRING.
8917
8918 If STRING is a Lisp string, set the message to the first NBYTES
8919 bytes from STRING. NBYTES zero means use the whole string. If
8920 STRING is multibyte, the message will be displayed multibyte.
8921
8922 If S is not null, set the message to the first LEN bytes of S. LEN
8923 zero means use the whole string. MULTIBYTE_P non-zero means S is
8924 multibyte. Display the message multibyte in that case.
8925
8926 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8927 to t before calling set_message_1 (which calls insert).
8928 */
8929
8930 void
8931 set_message (s, string, nbytes, multibyte_p)
8932 const char *s;
8933 Lisp_Object string;
8934 int nbytes, multibyte_p;
8935 {
8936 message_enable_multibyte
8937 = ((s && multibyte_p)
8938 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8939
8940 with_echo_area_buffer (0, -1, set_message_1,
8941 (EMACS_INT) s, string, nbytes, multibyte_p);
8942 message_buf_print = 0;
8943 help_echo_showing_p = 0;
8944 }
8945
8946
8947 /* Helper function for set_message. Arguments have the same meaning
8948 as there, with A1 corresponding to S and A2 corresponding to STRING
8949 This function is called with the echo area buffer being
8950 current. */
8951
8952 static int
8953 set_message_1 (a1, a2, nbytes, multibyte_p)
8954 EMACS_INT a1;
8955 Lisp_Object a2;
8956 EMACS_INT nbytes, multibyte_p;
8957 {
8958 const char *s = (const char *) a1;
8959 Lisp_Object string = a2;
8960
8961 /* Change multibyteness of the echo buffer appropriately. */
8962 if (message_enable_multibyte
8963 != !NILP (current_buffer->enable_multibyte_characters))
8964 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8965
8966 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8967
8968 /* Insert new message at BEG. */
8969 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8970
8971 if (STRINGP (string))
8972 {
8973 int nchars;
8974
8975 if (nbytes == 0)
8976 nbytes = SBYTES (string);
8977 nchars = string_byte_to_char (string, nbytes);
8978
8979 /* This function takes care of single/multibyte conversion. We
8980 just have to ensure that the echo area buffer has the right
8981 setting of enable_multibyte_characters. */
8982 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8983 }
8984 else if (s)
8985 {
8986 if (nbytes == 0)
8987 nbytes = strlen (s);
8988
8989 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8990 {
8991 /* Convert from multi-byte to single-byte. */
8992 int i, c, n;
8993 unsigned char work[1];
8994
8995 /* Convert a multibyte string to single-byte. */
8996 for (i = 0; i < nbytes; i += n)
8997 {
8998 c = string_char_and_length (s + i, nbytes - i, &n);
8999 work[0] = (ASCII_CHAR_P (c)
9000 ? c
9001 : multibyte_char_to_unibyte (c, Qnil));
9002 insert_1_both (work, 1, 1, 1, 0, 0);
9003 }
9004 }
9005 else if (!multibyte_p
9006 && !NILP (current_buffer->enable_multibyte_characters))
9007 {
9008 /* Convert from single-byte to multi-byte. */
9009 int i, c, n;
9010 const unsigned char *msg = (const unsigned char *) s;
9011 unsigned char str[MAX_MULTIBYTE_LENGTH];
9012
9013 /* Convert a single-byte string to multibyte. */
9014 for (i = 0; i < nbytes; i++)
9015 {
9016 c = msg[i];
9017 c = unibyte_char_to_multibyte (c);
9018 n = CHAR_STRING (c, str);
9019 insert_1_both (str, 1, n, 1, 0, 0);
9020 }
9021 }
9022 else
9023 insert_1 (s, nbytes, 1, 0, 0);
9024 }
9025
9026 return 0;
9027 }
9028
9029
9030 /* Clear messages. CURRENT_P non-zero means clear the current
9031 message. LAST_DISPLAYED_P non-zero means clear the message
9032 last displayed. */
9033
9034 void
9035 clear_message (current_p, last_displayed_p)
9036 int current_p, last_displayed_p;
9037 {
9038 if (current_p)
9039 {
9040 echo_area_buffer[0] = Qnil;
9041 message_cleared_p = 1;
9042 }
9043
9044 if (last_displayed_p)
9045 echo_area_buffer[1] = Qnil;
9046
9047 message_buf_print = 0;
9048 }
9049
9050 /* Clear garbaged frames.
9051
9052 This function is used where the old redisplay called
9053 redraw_garbaged_frames which in turn called redraw_frame which in
9054 turn called clear_frame. The call to clear_frame was a source of
9055 flickering. I believe a clear_frame is not necessary. It should
9056 suffice in the new redisplay to invalidate all current matrices,
9057 and ensure a complete redisplay of all windows. */
9058
9059 static void
9060 clear_garbaged_frames ()
9061 {
9062 if (frame_garbaged)
9063 {
9064 Lisp_Object tail, frame;
9065 int changed_count = 0;
9066
9067 FOR_EACH_FRAME (tail, frame)
9068 {
9069 struct frame *f = XFRAME (frame);
9070
9071 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9072 {
9073 if (f->resized_p)
9074 {
9075 Fredraw_frame (frame);
9076 f->force_flush_display_p = 1;
9077 }
9078 clear_current_matrices (f);
9079 changed_count++;
9080 f->garbaged = 0;
9081 f->resized_p = 0;
9082 }
9083 }
9084
9085 frame_garbaged = 0;
9086 if (changed_count)
9087 ++windows_or_buffers_changed;
9088 }
9089 }
9090
9091
9092 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
9093 is non-zero update selected_frame. Value is non-zero if the
9094 mini-windows height has been changed. */
9095
9096 static int
9097 echo_area_display (update_frame_p)
9098 int update_frame_p;
9099 {
9100 Lisp_Object mini_window;
9101 struct window *w;
9102 struct frame *f;
9103 int window_height_changed_p = 0;
9104 struct frame *sf = SELECTED_FRAME ();
9105
9106 mini_window = FRAME_MINIBUF_WINDOW (sf);
9107 w = XWINDOW (mini_window);
9108 f = XFRAME (WINDOW_FRAME (w));
9109
9110 /* Don't display if frame is invisible or not yet initialized. */
9111 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
9112 return 0;
9113
9114 #ifdef HAVE_WINDOW_SYSTEM
9115 /* When Emacs starts, selected_frame may be the initial terminal
9116 frame. If we let this through, a message would be displayed on
9117 the terminal. */
9118 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
9119 return 0;
9120 #endif /* HAVE_WINDOW_SYSTEM */
9121
9122 /* Redraw garbaged frames. */
9123 if (frame_garbaged)
9124 clear_garbaged_frames ();
9125
9126 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
9127 {
9128 echo_area_window = mini_window;
9129 window_height_changed_p = display_echo_area (w);
9130 w->must_be_updated_p = 1;
9131
9132 /* Update the display, unless called from redisplay_internal.
9133 Also don't update the screen during redisplay itself. The
9134 update will happen at the end of redisplay, and an update
9135 here could cause confusion. */
9136 if (update_frame_p && !redisplaying_p)
9137 {
9138 int n = 0;
9139
9140 /* If the display update has been interrupted by pending
9141 input, update mode lines in the frame. Due to the
9142 pending input, it might have been that redisplay hasn't
9143 been called, so that mode lines above the echo area are
9144 garbaged. This looks odd, so we prevent it here. */
9145 if (!display_completed)
9146 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
9147
9148 if (window_height_changed_p
9149 /* Don't do this if Emacs is shutting down. Redisplay
9150 needs to run hooks. */
9151 && !NILP (Vrun_hooks))
9152 {
9153 /* Must update other windows. Likewise as in other
9154 cases, don't let this update be interrupted by
9155 pending input. */
9156 int count = SPECPDL_INDEX ();
9157 specbind (Qredisplay_dont_pause, Qt);
9158 windows_or_buffers_changed = 1;
9159 redisplay_internal (0);
9160 unbind_to (count, Qnil);
9161 }
9162 else if (FRAME_WINDOW_P (f) && n == 0)
9163 {
9164 /* Window configuration is the same as before.
9165 Can do with a display update of the echo area,
9166 unless we displayed some mode lines. */
9167 update_single_window (w, 1);
9168 FRAME_RIF (f)->flush_display (f);
9169 }
9170 else
9171 update_frame (f, 1, 1);
9172
9173 /* If cursor is in the echo area, make sure that the next
9174 redisplay displays the minibuffer, so that the cursor will
9175 be replaced with what the minibuffer wants. */
9176 if (cursor_in_echo_area)
9177 ++windows_or_buffers_changed;
9178 }
9179 }
9180 else if (!EQ (mini_window, selected_window))
9181 windows_or_buffers_changed++;
9182
9183 /* Last displayed message is now the current message. */
9184 echo_area_buffer[1] = echo_area_buffer[0];
9185 /* Inform read_char that we're not echoing. */
9186 echo_message_buffer = Qnil;
9187
9188 /* Prevent redisplay optimization in redisplay_internal by resetting
9189 this_line_start_pos. This is done because the mini-buffer now
9190 displays the message instead of its buffer text. */
9191 if (EQ (mini_window, selected_window))
9192 CHARPOS (this_line_start_pos) = 0;
9193
9194 return window_height_changed_p;
9195 }
9196
9197
9198 \f
9199 /***********************************************************************
9200 Mode Lines and Frame Titles
9201 ***********************************************************************/
9202
9203 /* A buffer for constructing non-propertized mode-line strings and
9204 frame titles in it; allocated from the heap in init_xdisp and
9205 resized as needed in store_mode_line_noprop_char. */
9206
9207 static char *mode_line_noprop_buf;
9208
9209 /* The buffer's end, and a current output position in it. */
9210
9211 static char *mode_line_noprop_buf_end;
9212 static char *mode_line_noprop_ptr;
9213
9214 #define MODE_LINE_NOPROP_LEN(start) \
9215 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
9216
9217 static enum {
9218 MODE_LINE_DISPLAY = 0,
9219 MODE_LINE_TITLE,
9220 MODE_LINE_NOPROP,
9221 MODE_LINE_STRING
9222 } mode_line_target;
9223
9224 /* Alist that caches the results of :propertize.
9225 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
9226 static Lisp_Object mode_line_proptrans_alist;
9227
9228 /* List of strings making up the mode-line. */
9229 static Lisp_Object mode_line_string_list;
9230
9231 /* Base face property when building propertized mode line string. */
9232 static Lisp_Object mode_line_string_face;
9233 static Lisp_Object mode_line_string_face_prop;
9234
9235
9236 /* Unwind data for mode line strings */
9237
9238 static Lisp_Object Vmode_line_unwind_vector;
9239
9240 static Lisp_Object
9241 format_mode_line_unwind_data (struct buffer *obuf,
9242 Lisp_Object owin,
9243 int save_proptrans)
9244 {
9245 Lisp_Object vector, tmp;
9246
9247 /* Reduce consing by keeping one vector in
9248 Vwith_echo_area_save_vector. */
9249 vector = Vmode_line_unwind_vector;
9250 Vmode_line_unwind_vector = Qnil;
9251
9252 if (NILP (vector))
9253 vector = Fmake_vector (make_number (8), Qnil);
9254
9255 ASET (vector, 0, make_number (mode_line_target));
9256 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
9257 ASET (vector, 2, mode_line_string_list);
9258 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
9259 ASET (vector, 4, mode_line_string_face);
9260 ASET (vector, 5, mode_line_string_face_prop);
9261
9262 if (obuf)
9263 XSETBUFFER (tmp, obuf);
9264 else
9265 tmp = Qnil;
9266 ASET (vector, 6, tmp);
9267 ASET (vector, 7, owin);
9268
9269 return vector;
9270 }
9271
9272 static Lisp_Object
9273 unwind_format_mode_line (vector)
9274 Lisp_Object vector;
9275 {
9276 mode_line_target = XINT (AREF (vector, 0));
9277 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
9278 mode_line_string_list = AREF (vector, 2);
9279 if (! EQ (AREF (vector, 3), Qt))
9280 mode_line_proptrans_alist = AREF (vector, 3);
9281 mode_line_string_face = AREF (vector, 4);
9282 mode_line_string_face_prop = AREF (vector, 5);
9283
9284 if (!NILP (AREF (vector, 7)))
9285 /* Select window before buffer, since it may change the buffer. */
9286 Fselect_window (AREF (vector, 7), Qt);
9287
9288 if (!NILP (AREF (vector, 6)))
9289 {
9290 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
9291 ASET (vector, 6, Qnil);
9292 }
9293
9294 Vmode_line_unwind_vector = vector;
9295 return Qnil;
9296 }
9297
9298
9299 /* Store a single character C for the frame title in mode_line_noprop_buf.
9300 Re-allocate mode_line_noprop_buf if necessary. */
9301
9302 static void
9303 #ifdef PROTOTYPES
9304 store_mode_line_noprop_char (char c)
9305 #else
9306 store_mode_line_noprop_char (c)
9307 char c;
9308 #endif
9309 {
9310 /* If output position has reached the end of the allocated buffer,
9311 double the buffer's size. */
9312 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
9313 {
9314 int len = MODE_LINE_NOPROP_LEN (0);
9315 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
9316 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
9317 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
9318 mode_line_noprop_ptr = mode_line_noprop_buf + len;
9319 }
9320
9321 *mode_line_noprop_ptr++ = c;
9322 }
9323
9324
9325 /* Store part of a frame title in mode_line_noprop_buf, beginning at
9326 mode_line_noprop_ptr. STR is the string to store. Do not copy
9327 characters that yield more columns than PRECISION; PRECISION <= 0
9328 means copy the whole string. Pad with spaces until FIELD_WIDTH
9329 number of characters have been copied; FIELD_WIDTH <= 0 means don't
9330 pad. Called from display_mode_element when it is used to build a
9331 frame title. */
9332
9333 static int
9334 store_mode_line_noprop (str, field_width, precision)
9335 const unsigned char *str;
9336 int field_width, precision;
9337 {
9338 int n = 0;
9339 int dummy, nbytes;
9340
9341 /* Copy at most PRECISION chars from STR. */
9342 nbytes = strlen (str);
9343 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
9344 while (nbytes--)
9345 store_mode_line_noprop_char (*str++);
9346
9347 /* Fill up with spaces until FIELD_WIDTH reached. */
9348 while (field_width > 0
9349 && n < field_width)
9350 {
9351 store_mode_line_noprop_char (' ');
9352 ++n;
9353 }
9354
9355 return n;
9356 }
9357
9358 /***********************************************************************
9359 Frame Titles
9360 ***********************************************************************/
9361
9362 #ifdef HAVE_WINDOW_SYSTEM
9363
9364 /* Set the title of FRAME, if it has changed. The title format is
9365 Vicon_title_format if FRAME is iconified, otherwise it is
9366 frame_title_format. */
9367
9368 static void
9369 x_consider_frame_title (frame)
9370 Lisp_Object frame;
9371 {
9372 struct frame *f = XFRAME (frame);
9373
9374 if (FRAME_WINDOW_P (f)
9375 || FRAME_MINIBUF_ONLY_P (f)
9376 || f->explicit_name)
9377 {
9378 /* Do we have more than one visible frame on this X display? */
9379 Lisp_Object tail;
9380 Lisp_Object fmt;
9381 int title_start;
9382 char *title;
9383 int len;
9384 struct it it;
9385 int count = SPECPDL_INDEX ();
9386
9387 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
9388 {
9389 Lisp_Object other_frame = XCAR (tail);
9390 struct frame *tf = XFRAME (other_frame);
9391
9392 if (tf != f
9393 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
9394 && !FRAME_MINIBUF_ONLY_P (tf)
9395 && !EQ (other_frame, tip_frame)
9396 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
9397 break;
9398 }
9399
9400 /* Set global variable indicating that multiple frames exist. */
9401 multiple_frames = CONSP (tail);
9402
9403 /* Switch to the buffer of selected window of the frame. Set up
9404 mode_line_target so that display_mode_element will output into
9405 mode_line_noprop_buf; then display the title. */
9406 record_unwind_protect (unwind_format_mode_line,
9407 format_mode_line_unwind_data
9408 (current_buffer, selected_window, 0));
9409
9410 Fselect_window (f->selected_window, Qt);
9411 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
9412 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
9413
9414 mode_line_target = MODE_LINE_TITLE;
9415 title_start = MODE_LINE_NOPROP_LEN (0);
9416 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
9417 NULL, DEFAULT_FACE_ID);
9418 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
9419 len = MODE_LINE_NOPROP_LEN (title_start);
9420 title = mode_line_noprop_buf + title_start;
9421 unbind_to (count, Qnil);
9422
9423 /* Set the title only if it's changed. This avoids consing in
9424 the common case where it hasn't. (If it turns out that we've
9425 already wasted too much time by walking through the list with
9426 display_mode_element, then we might need to optimize at a
9427 higher level than this.) */
9428 if (! STRINGP (f->name)
9429 || SBYTES (f->name) != len
9430 || bcmp (title, SDATA (f->name), len) != 0)
9431 x_implicitly_set_name (f, make_string (title, len), Qnil);
9432 }
9433 }
9434
9435 #endif /* not HAVE_WINDOW_SYSTEM */
9436
9437
9438
9439 \f
9440 /***********************************************************************
9441 Menu Bars
9442 ***********************************************************************/
9443
9444
9445 /* Prepare for redisplay by updating menu-bar item lists when
9446 appropriate. This can call eval. */
9447
9448 void
9449 prepare_menu_bars ()
9450 {
9451 int all_windows;
9452 struct gcpro gcpro1, gcpro2;
9453 struct frame *f;
9454 Lisp_Object tooltip_frame;
9455
9456 #ifdef HAVE_WINDOW_SYSTEM
9457 tooltip_frame = tip_frame;
9458 #else
9459 tooltip_frame = Qnil;
9460 #endif
9461
9462 /* Update all frame titles based on their buffer names, etc. We do
9463 this before the menu bars so that the buffer-menu will show the
9464 up-to-date frame titles. */
9465 #ifdef HAVE_WINDOW_SYSTEM
9466 if (windows_or_buffers_changed || update_mode_lines)
9467 {
9468 Lisp_Object tail, frame;
9469
9470 FOR_EACH_FRAME (tail, frame)
9471 {
9472 f = XFRAME (frame);
9473 if (!EQ (frame, tooltip_frame)
9474 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9475 x_consider_frame_title (frame);
9476 }
9477 }
9478 #endif /* HAVE_WINDOW_SYSTEM */
9479
9480 /* Update the menu bar item lists, if appropriate. This has to be
9481 done before any actual redisplay or generation of display lines. */
9482 all_windows = (update_mode_lines
9483 || buffer_shared > 1
9484 || windows_or_buffers_changed);
9485 if (all_windows)
9486 {
9487 Lisp_Object tail, frame;
9488 int count = SPECPDL_INDEX ();
9489 /* 1 means that update_menu_bar has run its hooks
9490 so any further calls to update_menu_bar shouldn't do so again. */
9491 int menu_bar_hooks_run = 0;
9492
9493 record_unwind_save_match_data ();
9494
9495 FOR_EACH_FRAME (tail, frame)
9496 {
9497 f = XFRAME (frame);
9498
9499 /* Ignore tooltip frame. */
9500 if (EQ (frame, tooltip_frame))
9501 continue;
9502
9503 /* If a window on this frame changed size, report that to
9504 the user and clear the size-change flag. */
9505 if (FRAME_WINDOW_SIZES_CHANGED (f))
9506 {
9507 Lisp_Object functions;
9508
9509 /* Clear flag first in case we get an error below. */
9510 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9511 functions = Vwindow_size_change_functions;
9512 GCPRO2 (tail, functions);
9513
9514 while (CONSP (functions))
9515 {
9516 call1 (XCAR (functions), frame);
9517 functions = XCDR (functions);
9518 }
9519 UNGCPRO;
9520 }
9521
9522 GCPRO1 (tail);
9523 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9524 #ifdef HAVE_WINDOW_SYSTEM
9525 update_tool_bar (f, 0);
9526 #ifdef MAC_OS
9527 mac_update_title_bar (f, 0);
9528 #endif
9529 #endif
9530 UNGCPRO;
9531 }
9532
9533 unbind_to (count, Qnil);
9534 }
9535 else
9536 {
9537 struct frame *sf = SELECTED_FRAME ();
9538 update_menu_bar (sf, 1, 0);
9539 #ifdef HAVE_WINDOW_SYSTEM
9540 update_tool_bar (sf, 1);
9541 #ifdef MAC_OS
9542 mac_update_title_bar (sf, 1);
9543 #endif
9544 #endif
9545 }
9546
9547 /* Motif needs this. See comment in xmenu.c. Turn it off when
9548 pending_menu_activation is not defined. */
9549 #ifdef USE_X_TOOLKIT
9550 pending_menu_activation = 0;
9551 #endif
9552 }
9553
9554
9555 /* Update the menu bar item list for frame F. This has to be done
9556 before we start to fill in any display lines, because it can call
9557 eval.
9558
9559 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9560
9561 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9562 already ran the menu bar hooks for this redisplay, so there
9563 is no need to run them again. The return value is the
9564 updated value of this flag, to pass to the next call. */
9565
9566 static int
9567 update_menu_bar (f, save_match_data, hooks_run)
9568 struct frame *f;
9569 int save_match_data;
9570 int hooks_run;
9571 {
9572 Lisp_Object window;
9573 register struct window *w;
9574
9575 /* If called recursively during a menu update, do nothing. This can
9576 happen when, for instance, an activate-menubar-hook causes a
9577 redisplay. */
9578 if (inhibit_menubar_update)
9579 return hooks_run;
9580
9581 window = FRAME_SELECTED_WINDOW (f);
9582 w = XWINDOW (window);
9583
9584 #if 0 /* The if statement below this if statement used to include the
9585 condition !NILP (w->update_mode_line), rather than using
9586 update_mode_lines directly, and this if statement may have
9587 been added to make that condition work. Now the if
9588 statement below matches its comment, this isn't needed. */
9589 if (update_mode_lines)
9590 w->update_mode_line = Qt;
9591 #endif
9592
9593 if (FRAME_WINDOW_P (f)
9594 ?
9595 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9596 || defined (USE_GTK)
9597 FRAME_EXTERNAL_MENU_BAR (f)
9598 #else
9599 FRAME_MENU_BAR_LINES (f) > 0
9600 #endif
9601 : FRAME_MENU_BAR_LINES (f) > 0)
9602 {
9603 /* If the user has switched buffers or windows, we need to
9604 recompute to reflect the new bindings. But we'll
9605 recompute when update_mode_lines is set too; that means
9606 that people can use force-mode-line-update to request
9607 that the menu bar be recomputed. The adverse effect on
9608 the rest of the redisplay algorithm is about the same as
9609 windows_or_buffers_changed anyway. */
9610 if (windows_or_buffers_changed
9611 /* This used to test w->update_mode_line, but we believe
9612 there is no need to recompute the menu in that case. */
9613 || update_mode_lines
9614 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9615 < BUF_MODIFF (XBUFFER (w->buffer)))
9616 != !NILP (w->last_had_star))
9617 || ((!NILP (Vtransient_mark_mode)
9618 && !NILP (XBUFFER (w->buffer)->mark_active))
9619 != !NILP (w->region_showing)))
9620 {
9621 struct buffer *prev = current_buffer;
9622 int count = SPECPDL_INDEX ();
9623
9624 specbind (Qinhibit_menubar_update, Qt);
9625
9626 set_buffer_internal_1 (XBUFFER (w->buffer));
9627 if (save_match_data)
9628 record_unwind_save_match_data ();
9629 if (NILP (Voverriding_local_map_menu_flag))
9630 {
9631 specbind (Qoverriding_terminal_local_map, Qnil);
9632 specbind (Qoverriding_local_map, Qnil);
9633 }
9634
9635 if (!hooks_run)
9636 {
9637 /* Run the Lucid hook. */
9638 safe_run_hooks (Qactivate_menubar_hook);
9639
9640 /* If it has changed current-menubar from previous value,
9641 really recompute the menu-bar from the value. */
9642 if (! NILP (Vlucid_menu_bar_dirty_flag))
9643 call0 (Qrecompute_lucid_menubar);
9644
9645 safe_run_hooks (Qmenu_bar_update_hook);
9646
9647 hooks_run = 1;
9648 }
9649
9650 XSETFRAME (Vmenu_updating_frame, f);
9651 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9652
9653 /* Redisplay the menu bar in case we changed it. */
9654 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9655 || defined (USE_GTK)
9656 if (FRAME_WINDOW_P (f))
9657 {
9658 #ifdef MAC_OS
9659 /* All frames on Mac OS share the same menubar. So only
9660 the selected frame should be allowed to set it. */
9661 if (f == SELECTED_FRAME ())
9662 #endif
9663 set_frame_menubar (f, 0, 0);
9664 }
9665 else
9666 /* On a terminal screen, the menu bar is an ordinary screen
9667 line, and this makes it get updated. */
9668 w->update_mode_line = Qt;
9669 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9670 /* In the non-toolkit version, the menu bar is an ordinary screen
9671 line, and this makes it get updated. */
9672 w->update_mode_line = Qt;
9673 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9674
9675 unbind_to (count, Qnil);
9676 set_buffer_internal_1 (prev);
9677 }
9678 }
9679
9680 return hooks_run;
9681 }
9682
9683
9684 \f
9685 /***********************************************************************
9686 Output Cursor
9687 ***********************************************************************/
9688
9689 #ifdef HAVE_WINDOW_SYSTEM
9690
9691 /* EXPORT:
9692 Nominal cursor position -- where to draw output.
9693 HPOS and VPOS are window relative glyph matrix coordinates.
9694 X and Y are window relative pixel coordinates. */
9695
9696 struct cursor_pos output_cursor;
9697
9698
9699 /* EXPORT:
9700 Set the global variable output_cursor to CURSOR. All cursor
9701 positions are relative to updated_window. */
9702
9703 void
9704 set_output_cursor (cursor)
9705 struct cursor_pos *cursor;
9706 {
9707 output_cursor.hpos = cursor->hpos;
9708 output_cursor.vpos = cursor->vpos;
9709 output_cursor.x = cursor->x;
9710 output_cursor.y = cursor->y;
9711 }
9712
9713
9714 /* EXPORT for RIF:
9715 Set a nominal cursor position.
9716
9717 HPOS and VPOS are column/row positions in a window glyph matrix. X
9718 and Y are window text area relative pixel positions.
9719
9720 If this is done during an update, updated_window will contain the
9721 window that is being updated and the position is the future output
9722 cursor position for that window. If updated_window is null, use
9723 selected_window and display the cursor at the given position. */
9724
9725 void
9726 x_cursor_to (vpos, hpos, y, x)
9727 int vpos, hpos, y, x;
9728 {
9729 struct window *w;
9730
9731 /* If updated_window is not set, work on selected_window. */
9732 if (updated_window)
9733 w = updated_window;
9734 else
9735 w = XWINDOW (selected_window);
9736
9737 /* Set the output cursor. */
9738 output_cursor.hpos = hpos;
9739 output_cursor.vpos = vpos;
9740 output_cursor.x = x;
9741 output_cursor.y = y;
9742
9743 /* If not called as part of an update, really display the cursor.
9744 This will also set the cursor position of W. */
9745 if (updated_window == NULL)
9746 {
9747 BLOCK_INPUT;
9748 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9749 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
9750 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
9751 UNBLOCK_INPUT;
9752 }
9753 }
9754
9755 #endif /* HAVE_WINDOW_SYSTEM */
9756
9757 \f
9758 /***********************************************************************
9759 Tool-bars
9760 ***********************************************************************/
9761
9762 #ifdef HAVE_WINDOW_SYSTEM
9763
9764 /* Where the mouse was last time we reported a mouse event. */
9765
9766 FRAME_PTR last_mouse_frame;
9767
9768 /* Tool-bar item index of the item on which a mouse button was pressed
9769 or -1. */
9770
9771 int last_tool_bar_item;
9772
9773
9774 /* Update the tool-bar item list for frame F. This has to be done
9775 before we start to fill in any display lines. Called from
9776 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9777 and restore it here. */
9778
9779 static void
9780 update_tool_bar (f, save_match_data)
9781 struct frame *f;
9782 int save_match_data;
9783 {
9784 #if defined (USE_GTK) || USE_MAC_TOOLBAR
9785 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9786 #else
9787 int do_update = WINDOWP (f->tool_bar_window)
9788 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9789 #endif
9790
9791 if (do_update)
9792 {
9793 Lisp_Object window;
9794 struct window *w;
9795
9796 window = FRAME_SELECTED_WINDOW (f);
9797 w = XWINDOW (window);
9798
9799 /* If the user has switched buffers or windows, we need to
9800 recompute to reflect the new bindings. But we'll
9801 recompute when update_mode_lines is set too; that means
9802 that people can use force-mode-line-update to request
9803 that the menu bar be recomputed. The adverse effect on
9804 the rest of the redisplay algorithm is about the same as
9805 windows_or_buffers_changed anyway. */
9806 if (windows_or_buffers_changed
9807 || !NILP (w->update_mode_line)
9808 || update_mode_lines
9809 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9810 < BUF_MODIFF (XBUFFER (w->buffer)))
9811 != !NILP (w->last_had_star))
9812 || ((!NILP (Vtransient_mark_mode)
9813 && !NILP (XBUFFER (w->buffer)->mark_active))
9814 != !NILP (w->region_showing)))
9815 {
9816 struct buffer *prev = current_buffer;
9817 int count = SPECPDL_INDEX ();
9818 Lisp_Object new_tool_bar;
9819 int new_n_tool_bar;
9820 struct gcpro gcpro1;
9821
9822 /* Set current_buffer to the buffer of the selected
9823 window of the frame, so that we get the right local
9824 keymaps. */
9825 set_buffer_internal_1 (XBUFFER (w->buffer));
9826
9827 /* Save match data, if we must. */
9828 if (save_match_data)
9829 record_unwind_save_match_data ();
9830
9831 /* Make sure that we don't accidentally use bogus keymaps. */
9832 if (NILP (Voverriding_local_map_menu_flag))
9833 {
9834 specbind (Qoverriding_terminal_local_map, Qnil);
9835 specbind (Qoverriding_local_map, Qnil);
9836 }
9837
9838 GCPRO1 (new_tool_bar);
9839
9840 /* Build desired tool-bar items from keymaps. */
9841 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9842 &new_n_tool_bar);
9843
9844 /* Redisplay the tool-bar if we changed it. */
9845 if (new_n_tool_bar != f->n_tool_bar_items
9846 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9847 {
9848 /* Redisplay that happens asynchronously due to an expose event
9849 may access f->tool_bar_items. Make sure we update both
9850 variables within BLOCK_INPUT so no such event interrupts. */
9851 BLOCK_INPUT;
9852 f->tool_bar_items = new_tool_bar;
9853 f->n_tool_bar_items = new_n_tool_bar;
9854 w->update_mode_line = Qt;
9855 UNBLOCK_INPUT;
9856 }
9857
9858 UNGCPRO;
9859
9860 unbind_to (count, Qnil);
9861 set_buffer_internal_1 (prev);
9862 }
9863 }
9864 }
9865
9866
9867 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9868 F's desired tool-bar contents. F->tool_bar_items must have
9869 been set up previously by calling prepare_menu_bars. */
9870
9871 static void
9872 build_desired_tool_bar_string (f)
9873 struct frame *f;
9874 {
9875 int i, size, size_needed;
9876 struct gcpro gcpro1, gcpro2, gcpro3;
9877 Lisp_Object image, plist, props;
9878
9879 image = plist = props = Qnil;
9880 GCPRO3 (image, plist, props);
9881
9882 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9883 Otherwise, make a new string. */
9884
9885 /* The size of the string we might be able to reuse. */
9886 size = (STRINGP (f->desired_tool_bar_string)
9887 ? SCHARS (f->desired_tool_bar_string)
9888 : 0);
9889
9890 /* We need one space in the string for each image. */
9891 size_needed = f->n_tool_bar_items;
9892
9893 /* Reuse f->desired_tool_bar_string, if possible. */
9894 if (size < size_needed || NILP (f->desired_tool_bar_string))
9895 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9896 make_number (' '));
9897 else
9898 {
9899 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9900 Fremove_text_properties (make_number (0), make_number (size),
9901 props, f->desired_tool_bar_string);
9902 }
9903
9904 /* Put a `display' property on the string for the images to display,
9905 put a `menu_item' property on tool-bar items with a value that
9906 is the index of the item in F's tool-bar item vector. */
9907 for (i = 0; i < f->n_tool_bar_items; ++i)
9908 {
9909 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9910
9911 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9912 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9913 int hmargin, vmargin, relief, idx, end;
9914 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9915
9916 /* If image is a vector, choose the image according to the
9917 button state. */
9918 image = PROP (TOOL_BAR_ITEM_IMAGES);
9919 if (VECTORP (image))
9920 {
9921 if (enabled_p)
9922 idx = (selected_p
9923 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9924 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9925 else
9926 idx = (selected_p
9927 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9928 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9929
9930 xassert (ASIZE (image) >= idx);
9931 image = AREF (image, idx);
9932 }
9933 else
9934 idx = -1;
9935
9936 /* Ignore invalid image specifications. */
9937 if (!valid_image_p (image))
9938 continue;
9939
9940 /* Display the tool-bar button pressed, or depressed. */
9941 plist = Fcopy_sequence (XCDR (image));
9942
9943 /* Compute margin and relief to draw. */
9944 relief = (tool_bar_button_relief >= 0
9945 ? tool_bar_button_relief
9946 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9947 hmargin = vmargin = relief;
9948
9949 if (INTEGERP (Vtool_bar_button_margin)
9950 && XINT (Vtool_bar_button_margin) > 0)
9951 {
9952 hmargin += XFASTINT (Vtool_bar_button_margin);
9953 vmargin += XFASTINT (Vtool_bar_button_margin);
9954 }
9955 else if (CONSP (Vtool_bar_button_margin))
9956 {
9957 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9958 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9959 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9960
9961 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9962 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9963 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9964 }
9965
9966 if (auto_raise_tool_bar_buttons_p)
9967 {
9968 /* Add a `:relief' property to the image spec if the item is
9969 selected. */
9970 if (selected_p)
9971 {
9972 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9973 hmargin -= relief;
9974 vmargin -= relief;
9975 }
9976 }
9977 else
9978 {
9979 /* If image is selected, display it pressed, i.e. with a
9980 negative relief. If it's not selected, display it with a
9981 raised relief. */
9982 plist = Fplist_put (plist, QCrelief,
9983 (selected_p
9984 ? make_number (-relief)
9985 : make_number (relief)));
9986 hmargin -= relief;
9987 vmargin -= relief;
9988 }
9989
9990 /* Put a margin around the image. */
9991 if (hmargin || vmargin)
9992 {
9993 if (hmargin == vmargin)
9994 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9995 else
9996 plist = Fplist_put (plist, QCmargin,
9997 Fcons (make_number (hmargin),
9998 make_number (vmargin)));
9999 }
10000
10001 /* If button is not enabled, and we don't have special images
10002 for the disabled state, make the image appear disabled by
10003 applying an appropriate algorithm to it. */
10004 if (!enabled_p && idx < 0)
10005 plist = Fplist_put (plist, QCconversion, Qdisabled);
10006
10007 /* Put a `display' text property on the string for the image to
10008 display. Put a `menu-item' property on the string that gives
10009 the start of this item's properties in the tool-bar items
10010 vector. */
10011 image = Fcons (Qimage, plist);
10012 props = list4 (Qdisplay, image,
10013 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10014
10015 /* Let the last image hide all remaining spaces in the tool bar
10016 string. The string can be longer than needed when we reuse a
10017 previous string. */
10018 if (i + 1 == f->n_tool_bar_items)
10019 end = SCHARS (f->desired_tool_bar_string);
10020 else
10021 end = i + 1;
10022 Fadd_text_properties (make_number (i), make_number (end),
10023 props, f->desired_tool_bar_string);
10024 #undef PROP
10025 }
10026
10027 UNGCPRO;
10028 }
10029
10030
10031 /* Display one line of the tool-bar of frame IT->f.
10032
10033 HEIGHT specifies the desired height of the tool-bar line.
10034 If the actual height of the glyph row is less than HEIGHT, the
10035 row's height is increased to HEIGHT, and the icons are centered
10036 vertically in the new height.
10037
10038 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10039 count a final empty row in case the tool-bar width exactly matches
10040 the window width.
10041 */
10042
10043 static void
10044 display_tool_bar_line (it, height)
10045 struct it *it;
10046 int height;
10047 {
10048 struct glyph_row *row = it->glyph_row;
10049 int max_x = it->last_visible_x;
10050 struct glyph *last;
10051
10052 prepare_desired_row (row);
10053 row->y = it->current_y;
10054
10055 /* Note that this isn't made use of if the face hasn't a box,
10056 so there's no need to check the face here. */
10057 it->start_of_box_run_p = 1;
10058
10059 while (it->current_x < max_x)
10060 {
10061 int x, n_glyphs_before, i, nglyphs;
10062 struct it it_before;
10063
10064 /* Get the next display element. */
10065 if (!get_next_display_element (it))
10066 {
10067 /* Don't count empty row if we are counting needed tool-bar lines. */
10068 if (height < 0 && !it->hpos)
10069 return;
10070 break;
10071 }
10072
10073 /* Produce glyphs. */
10074 n_glyphs_before = row->used[TEXT_AREA];
10075 it_before = *it;
10076
10077 PRODUCE_GLYPHS (it);
10078
10079 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10080 i = 0;
10081 x = it_before.current_x;
10082 while (i < nglyphs)
10083 {
10084 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10085
10086 if (x + glyph->pixel_width > max_x)
10087 {
10088 /* Glyph doesn't fit on line. Backtrack. */
10089 row->used[TEXT_AREA] = n_glyphs_before;
10090 *it = it_before;
10091 /* If this is the only glyph on this line, it will never fit on the
10092 toolbar, so skip it. But ensure there is at least one glyph,
10093 so we don't accidentally disable the tool-bar. */
10094 if (n_glyphs_before == 0
10095 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10096 break;
10097 goto out;
10098 }
10099
10100 ++it->hpos;
10101 x += glyph->pixel_width;
10102 ++i;
10103 }
10104
10105 /* Stop at line ends. */
10106 if (ITERATOR_AT_END_OF_LINE_P (it))
10107 break;
10108
10109 set_iterator_to_next (it, 1);
10110 }
10111
10112 out:;
10113
10114 row->displays_text_p = row->used[TEXT_AREA] != 0;
10115
10116 /* Use default face for the border below the tool bar.
10117
10118 FIXME: When auto-resize-tool-bars is grow-only, there is
10119 no additional border below the possibly empty tool-bar lines.
10120 So to make the extra empty lines look "normal", we have to
10121 use the tool-bar face for the border too. */
10122 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
10123 it->face_id = DEFAULT_FACE_ID;
10124
10125 extend_face_to_end_of_line (it);
10126 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
10127 last->right_box_line_p = 1;
10128 if (last == row->glyphs[TEXT_AREA])
10129 last->left_box_line_p = 1;
10130
10131 /* Make line the desired height and center it vertically. */
10132 if ((height -= it->max_ascent + it->max_descent) > 0)
10133 {
10134 /* Don't add more than one line height. */
10135 height %= FRAME_LINE_HEIGHT (it->f);
10136 it->max_ascent += height / 2;
10137 it->max_descent += (height + 1) / 2;
10138 }
10139
10140 compute_line_metrics (it);
10141
10142 /* If line is empty, make it occupy the rest of the tool-bar. */
10143 if (!row->displays_text_p)
10144 {
10145 row->height = row->phys_height = it->last_visible_y - row->y;
10146 row->visible_height = row->height;
10147 row->ascent = row->phys_ascent = 0;
10148 row->extra_line_spacing = 0;
10149 }
10150
10151 row->full_width_p = 1;
10152 row->continued_p = 0;
10153 row->truncated_on_left_p = 0;
10154 row->truncated_on_right_p = 0;
10155
10156 it->current_x = it->hpos = 0;
10157 it->current_y += row->height;
10158 ++it->vpos;
10159 ++it->glyph_row;
10160 }
10161
10162
10163 /* Max tool-bar height. */
10164
10165 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
10166 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
10167
10168 /* Value is the number of screen lines needed to make all tool-bar
10169 items of frame F visible. The number of actual rows needed is
10170 returned in *N_ROWS if non-NULL. */
10171
10172 static int
10173 tool_bar_lines_needed (f, n_rows)
10174 struct frame *f;
10175 int *n_rows;
10176 {
10177 struct window *w = XWINDOW (f->tool_bar_window);
10178 struct it it;
10179 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
10180 the desired matrix, so use (unused) mode-line row as temporary row to
10181 avoid destroying the first tool-bar row. */
10182 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
10183
10184 /* Initialize an iterator for iteration over
10185 F->desired_tool_bar_string in the tool-bar window of frame F. */
10186 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
10187 it.first_visible_x = 0;
10188 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10189 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10190
10191 while (!ITERATOR_AT_END_P (&it))
10192 {
10193 clear_glyph_row (temp_row);
10194 it.glyph_row = temp_row;
10195 display_tool_bar_line (&it, -1);
10196 }
10197 clear_glyph_row (temp_row);
10198
10199 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
10200 if (n_rows)
10201 *n_rows = it.vpos > 0 ? it.vpos : -1;
10202
10203 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
10204 }
10205
10206
10207 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
10208 0, 1, 0,
10209 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
10210 (frame)
10211 Lisp_Object frame;
10212 {
10213 struct frame *f;
10214 struct window *w;
10215 int nlines = 0;
10216
10217 if (NILP (frame))
10218 frame = selected_frame;
10219 else
10220 CHECK_FRAME (frame);
10221 f = XFRAME (frame);
10222
10223 if (WINDOWP (f->tool_bar_window)
10224 || (w = XWINDOW (f->tool_bar_window),
10225 WINDOW_TOTAL_LINES (w) > 0))
10226 {
10227 update_tool_bar (f, 1);
10228 if (f->n_tool_bar_items)
10229 {
10230 build_desired_tool_bar_string (f);
10231 nlines = tool_bar_lines_needed (f, NULL);
10232 }
10233 }
10234
10235 return make_number (nlines);
10236 }
10237
10238
10239 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
10240 height should be changed. */
10241
10242 static int
10243 redisplay_tool_bar (f)
10244 struct frame *f;
10245 {
10246 struct window *w;
10247 struct it it;
10248 struct glyph_row *row;
10249
10250 #if defined (USE_GTK) || USE_MAC_TOOLBAR
10251 if (FRAME_EXTERNAL_TOOL_BAR (f))
10252 update_frame_tool_bar (f);
10253 return 0;
10254 #endif
10255
10256 /* If frame hasn't a tool-bar window or if it is zero-height, don't
10257 do anything. This means you must start with tool-bar-lines
10258 non-zero to get the auto-sizing effect. Or in other words, you
10259 can turn off tool-bars by specifying tool-bar-lines zero. */
10260 if (!WINDOWP (f->tool_bar_window)
10261 || (w = XWINDOW (f->tool_bar_window),
10262 WINDOW_TOTAL_LINES (w) == 0))
10263 return 0;
10264
10265 /* Set up an iterator for the tool-bar window. */
10266 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
10267 it.first_visible_x = 0;
10268 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
10269 row = it.glyph_row;
10270
10271 /* Build a string that represents the contents of the tool-bar. */
10272 build_desired_tool_bar_string (f);
10273 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
10274
10275 if (f->n_tool_bar_rows == 0)
10276 {
10277 int nlines;
10278
10279 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
10280 nlines != WINDOW_TOTAL_LINES (w)))
10281 {
10282 extern Lisp_Object Qtool_bar_lines;
10283 Lisp_Object frame;
10284 int old_height = WINDOW_TOTAL_LINES (w);
10285
10286 XSETFRAME (frame, f);
10287 Fmodify_frame_parameters (frame,
10288 Fcons (Fcons (Qtool_bar_lines,
10289 make_number (nlines)),
10290 Qnil));
10291 if (WINDOW_TOTAL_LINES (w) != old_height)
10292 {
10293 clear_glyph_matrix (w->desired_matrix);
10294 fonts_changed_p = 1;
10295 return 1;
10296 }
10297 }
10298 }
10299
10300 /* Display as many lines as needed to display all tool-bar items. */
10301
10302 if (f->n_tool_bar_rows > 0)
10303 {
10304 int border, rows, height, extra;
10305
10306 if (INTEGERP (Vtool_bar_border))
10307 border = XINT (Vtool_bar_border);
10308 else if (EQ (Vtool_bar_border, Qinternal_border_width))
10309 border = FRAME_INTERNAL_BORDER_WIDTH (f);
10310 else if (EQ (Vtool_bar_border, Qborder_width))
10311 border = f->border_width;
10312 else
10313 border = 0;
10314 if (border < 0)
10315 border = 0;
10316
10317 rows = f->n_tool_bar_rows;
10318 height = max (1, (it.last_visible_y - border) / rows);
10319 extra = it.last_visible_y - border - height * rows;
10320
10321 while (it.current_y < it.last_visible_y)
10322 {
10323 int h = 0;
10324 if (extra > 0 && rows-- > 0)
10325 {
10326 h = (extra + rows - 1) / rows;
10327 extra -= h;
10328 }
10329 display_tool_bar_line (&it, height + h);
10330 }
10331 }
10332 else
10333 {
10334 while (it.current_y < it.last_visible_y)
10335 display_tool_bar_line (&it, 0);
10336 }
10337
10338 /* It doesn't make much sense to try scrolling in the tool-bar
10339 window, so don't do it. */
10340 w->desired_matrix->no_scrolling_p = 1;
10341 w->must_be_updated_p = 1;
10342
10343 if (!NILP (Vauto_resize_tool_bars))
10344 {
10345 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
10346 int change_height_p = 0;
10347
10348 /* If we couldn't display everything, change the tool-bar's
10349 height if there is room for more. */
10350 if (IT_STRING_CHARPOS (it) < it.end_charpos
10351 && it.current_y < max_tool_bar_height)
10352 change_height_p = 1;
10353
10354 row = it.glyph_row - 1;
10355
10356 /* If there are blank lines at the end, except for a partially
10357 visible blank line at the end that is smaller than
10358 FRAME_LINE_HEIGHT, change the tool-bar's height. */
10359 if (!row->displays_text_p
10360 && row->height >= FRAME_LINE_HEIGHT (f))
10361 change_height_p = 1;
10362
10363 /* If row displays tool-bar items, but is partially visible,
10364 change the tool-bar's height. */
10365 if (row->displays_text_p
10366 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
10367 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
10368 change_height_p = 1;
10369
10370 /* Resize windows as needed by changing the `tool-bar-lines'
10371 frame parameter. */
10372 if (change_height_p)
10373 {
10374 extern Lisp_Object Qtool_bar_lines;
10375 Lisp_Object frame;
10376 int old_height = WINDOW_TOTAL_LINES (w);
10377 int nrows;
10378 int nlines = tool_bar_lines_needed (f, &nrows);
10379
10380 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
10381 && !f->minimize_tool_bar_window_p)
10382 ? (nlines > old_height)
10383 : (nlines != old_height));
10384 f->minimize_tool_bar_window_p = 0;
10385
10386 if (change_height_p)
10387 {
10388 XSETFRAME (frame, f);
10389 Fmodify_frame_parameters (frame,
10390 Fcons (Fcons (Qtool_bar_lines,
10391 make_number (nlines)),
10392 Qnil));
10393 if (WINDOW_TOTAL_LINES (w) != old_height)
10394 {
10395 clear_glyph_matrix (w->desired_matrix);
10396 f->n_tool_bar_rows = nrows;
10397 fonts_changed_p = 1;
10398 return 1;
10399 }
10400 }
10401 }
10402 }
10403
10404 f->minimize_tool_bar_window_p = 0;
10405 return 0;
10406 }
10407
10408
10409 /* Get information about the tool-bar item which is displayed in GLYPH
10410 on frame F. Return in *PROP_IDX the index where tool-bar item
10411 properties start in F->tool_bar_items. Value is zero if
10412 GLYPH doesn't display a tool-bar item. */
10413
10414 static int
10415 tool_bar_item_info (f, glyph, prop_idx)
10416 struct frame *f;
10417 struct glyph *glyph;
10418 int *prop_idx;
10419 {
10420 Lisp_Object prop;
10421 int success_p;
10422 int charpos;
10423
10424 /* This function can be called asynchronously, which means we must
10425 exclude any possibility that Fget_text_property signals an
10426 error. */
10427 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
10428 charpos = max (0, charpos);
10429
10430 /* Get the text property `menu-item' at pos. The value of that
10431 property is the start index of this item's properties in
10432 F->tool_bar_items. */
10433 prop = Fget_text_property (make_number (charpos),
10434 Qmenu_item, f->current_tool_bar_string);
10435 if (INTEGERP (prop))
10436 {
10437 *prop_idx = XINT (prop);
10438 success_p = 1;
10439 }
10440 else
10441 success_p = 0;
10442
10443 return success_p;
10444 }
10445
10446 \f
10447 /* Get information about the tool-bar item at position X/Y on frame F.
10448 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10449 the current matrix of the tool-bar window of F, or NULL if not
10450 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10451 item in F->tool_bar_items. Value is
10452
10453 -1 if X/Y is not on a tool-bar item
10454 0 if X/Y is on the same item that was highlighted before.
10455 1 otherwise. */
10456
10457 static int
10458 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10459 struct frame *f;
10460 int x, y;
10461 struct glyph **glyph;
10462 int *hpos, *vpos, *prop_idx;
10463 {
10464 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10465 struct window *w = XWINDOW (f->tool_bar_window);
10466 int area;
10467
10468 /* Find the glyph under X/Y. */
10469 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10470 if (*glyph == NULL)
10471 return -1;
10472
10473 /* Get the start of this tool-bar item's properties in
10474 f->tool_bar_items. */
10475 if (!tool_bar_item_info (f, *glyph, prop_idx))
10476 return -1;
10477
10478 /* Is mouse on the highlighted item? */
10479 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10480 && *vpos >= dpyinfo->mouse_face_beg_row
10481 && *vpos <= dpyinfo->mouse_face_end_row
10482 && (*vpos > dpyinfo->mouse_face_beg_row
10483 || *hpos >= dpyinfo->mouse_face_beg_col)
10484 && (*vpos < dpyinfo->mouse_face_end_row
10485 || *hpos < dpyinfo->mouse_face_end_col
10486 || dpyinfo->mouse_face_past_end))
10487 return 0;
10488
10489 return 1;
10490 }
10491
10492
10493 /* EXPORT:
10494 Handle mouse button event on the tool-bar of frame F, at
10495 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10496 0 for button release. MODIFIERS is event modifiers for button
10497 release. */
10498
10499 void
10500 handle_tool_bar_click (f, x, y, down_p, modifiers)
10501 struct frame *f;
10502 int x, y, down_p;
10503 unsigned int modifiers;
10504 {
10505 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10506 struct window *w = XWINDOW (f->tool_bar_window);
10507 int hpos, vpos, prop_idx;
10508 struct glyph *glyph;
10509 Lisp_Object enabled_p;
10510
10511 /* If not on the highlighted tool-bar item, return. */
10512 frame_to_window_pixel_xy (w, &x, &y);
10513 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10514 return;
10515
10516 /* If item is disabled, do nothing. */
10517 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10518 if (NILP (enabled_p))
10519 return;
10520
10521 if (down_p)
10522 {
10523 /* Show item in pressed state. */
10524 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10525 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10526 last_tool_bar_item = prop_idx;
10527 }
10528 else
10529 {
10530 Lisp_Object key, frame;
10531 struct input_event event;
10532 EVENT_INIT (event);
10533
10534 /* Show item in released state. */
10535 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10536 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10537
10538 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10539
10540 XSETFRAME (frame, f);
10541 event.kind = TOOL_BAR_EVENT;
10542 event.frame_or_window = frame;
10543 event.arg = frame;
10544 kbd_buffer_store_event (&event);
10545
10546 event.kind = TOOL_BAR_EVENT;
10547 event.frame_or_window = frame;
10548 event.arg = key;
10549 event.modifiers = modifiers;
10550 kbd_buffer_store_event (&event);
10551 last_tool_bar_item = -1;
10552 }
10553 }
10554
10555
10556 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10557 tool-bar window-relative coordinates X/Y. Called from
10558 note_mouse_highlight. */
10559
10560 static void
10561 note_tool_bar_highlight (f, x, y)
10562 struct frame *f;
10563 int x, y;
10564 {
10565 Lisp_Object window = f->tool_bar_window;
10566 struct window *w = XWINDOW (window);
10567 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10568 int hpos, vpos;
10569 struct glyph *glyph;
10570 struct glyph_row *row;
10571 int i;
10572 Lisp_Object enabled_p;
10573 int prop_idx;
10574 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10575 int mouse_down_p, rc;
10576
10577 /* Function note_mouse_highlight is called with negative x(y
10578 values when mouse moves outside of the frame. */
10579 if (x <= 0 || y <= 0)
10580 {
10581 clear_mouse_face (dpyinfo);
10582 return;
10583 }
10584
10585 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10586 if (rc < 0)
10587 {
10588 /* Not on tool-bar item. */
10589 clear_mouse_face (dpyinfo);
10590 return;
10591 }
10592 else if (rc == 0)
10593 /* On same tool-bar item as before. */
10594 goto set_help_echo;
10595
10596 clear_mouse_face (dpyinfo);
10597
10598 /* Mouse is down, but on different tool-bar item? */
10599 mouse_down_p = (dpyinfo->grabbed
10600 && f == last_mouse_frame
10601 && FRAME_LIVE_P (f));
10602 if (mouse_down_p
10603 && last_tool_bar_item != prop_idx)
10604 return;
10605
10606 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10607 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10608
10609 /* If tool-bar item is not enabled, don't highlight it. */
10610 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10611 if (!NILP (enabled_p))
10612 {
10613 /* Compute the x-position of the glyph. In front and past the
10614 image is a space. We include this in the highlighted area. */
10615 row = MATRIX_ROW (w->current_matrix, vpos);
10616 for (i = x = 0; i < hpos; ++i)
10617 x += row->glyphs[TEXT_AREA][i].pixel_width;
10618
10619 /* Record this as the current active region. */
10620 dpyinfo->mouse_face_beg_col = hpos;
10621 dpyinfo->mouse_face_beg_row = vpos;
10622 dpyinfo->mouse_face_beg_x = x;
10623 dpyinfo->mouse_face_beg_y = row->y;
10624 dpyinfo->mouse_face_past_end = 0;
10625
10626 dpyinfo->mouse_face_end_col = hpos + 1;
10627 dpyinfo->mouse_face_end_row = vpos;
10628 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10629 dpyinfo->mouse_face_end_y = row->y;
10630 dpyinfo->mouse_face_window = window;
10631 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10632
10633 /* Display it as active. */
10634 show_mouse_face (dpyinfo, draw);
10635 dpyinfo->mouse_face_image_state = draw;
10636 }
10637
10638 set_help_echo:
10639
10640 /* Set help_echo_string to a help string to display for this tool-bar item.
10641 XTread_socket does the rest. */
10642 help_echo_object = help_echo_window = Qnil;
10643 help_echo_pos = -1;
10644 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10645 if (NILP (help_echo_string))
10646 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10647 }
10648
10649 #endif /* HAVE_WINDOW_SYSTEM */
10650
10651
10652 \f
10653 /************************************************************************
10654 Horizontal scrolling
10655 ************************************************************************/
10656
10657 static int hscroll_window_tree P_ ((Lisp_Object));
10658 static int hscroll_windows P_ ((Lisp_Object));
10659
10660 /* For all leaf windows in the window tree rooted at WINDOW, set their
10661 hscroll value so that PT is (i) visible in the window, and (ii) so
10662 that it is not within a certain margin at the window's left and
10663 right border. Value is non-zero if any window's hscroll has been
10664 changed. */
10665
10666 static int
10667 hscroll_window_tree (window)
10668 Lisp_Object window;
10669 {
10670 int hscrolled_p = 0;
10671 int hscroll_relative_p = FLOATP (Vhscroll_step);
10672 int hscroll_step_abs = 0;
10673 double hscroll_step_rel = 0;
10674
10675 if (hscroll_relative_p)
10676 {
10677 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10678 if (hscroll_step_rel < 0)
10679 {
10680 hscroll_relative_p = 0;
10681 hscroll_step_abs = 0;
10682 }
10683 }
10684 else if (INTEGERP (Vhscroll_step))
10685 {
10686 hscroll_step_abs = XINT (Vhscroll_step);
10687 if (hscroll_step_abs < 0)
10688 hscroll_step_abs = 0;
10689 }
10690 else
10691 hscroll_step_abs = 0;
10692
10693 while (WINDOWP (window))
10694 {
10695 struct window *w = XWINDOW (window);
10696
10697 if (WINDOWP (w->hchild))
10698 hscrolled_p |= hscroll_window_tree (w->hchild);
10699 else if (WINDOWP (w->vchild))
10700 hscrolled_p |= hscroll_window_tree (w->vchild);
10701 else if (w->cursor.vpos >= 0)
10702 {
10703 int h_margin;
10704 int text_area_width;
10705 struct glyph_row *current_cursor_row
10706 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10707 struct glyph_row *desired_cursor_row
10708 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10709 struct glyph_row *cursor_row
10710 = (desired_cursor_row->enabled_p
10711 ? desired_cursor_row
10712 : current_cursor_row);
10713
10714 text_area_width = window_box_width (w, TEXT_AREA);
10715
10716 /* Scroll when cursor is inside this scroll margin. */
10717 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10718
10719 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
10720 && ((XFASTINT (w->hscroll)
10721 && w->cursor.x <= h_margin)
10722 || (cursor_row->enabled_p
10723 && cursor_row->truncated_on_right_p
10724 && (w->cursor.x >= text_area_width - h_margin))))
10725 {
10726 struct it it;
10727 int hscroll;
10728 struct buffer *saved_current_buffer;
10729 int pt;
10730 int wanted_x;
10731
10732 /* Find point in a display of infinite width. */
10733 saved_current_buffer = current_buffer;
10734 current_buffer = XBUFFER (w->buffer);
10735
10736 if (w == XWINDOW (selected_window))
10737 pt = BUF_PT (current_buffer);
10738 else
10739 {
10740 pt = marker_position (w->pointm);
10741 pt = max (BEGV, pt);
10742 pt = min (ZV, pt);
10743 }
10744
10745 /* Move iterator to pt starting at cursor_row->start in
10746 a line with infinite width. */
10747 init_to_row_start (&it, w, cursor_row);
10748 it.last_visible_x = INFINITY;
10749 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10750 current_buffer = saved_current_buffer;
10751
10752 /* Position cursor in window. */
10753 if (!hscroll_relative_p && hscroll_step_abs == 0)
10754 hscroll = max (0, (it.current_x
10755 - (ITERATOR_AT_END_OF_LINE_P (&it)
10756 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10757 : (text_area_width / 2))))
10758 / FRAME_COLUMN_WIDTH (it.f);
10759 else if (w->cursor.x >= text_area_width - h_margin)
10760 {
10761 if (hscroll_relative_p)
10762 wanted_x = text_area_width * (1 - hscroll_step_rel)
10763 - h_margin;
10764 else
10765 wanted_x = text_area_width
10766 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10767 - h_margin;
10768 hscroll
10769 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10770 }
10771 else
10772 {
10773 if (hscroll_relative_p)
10774 wanted_x = text_area_width * hscroll_step_rel
10775 + h_margin;
10776 else
10777 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10778 + h_margin;
10779 hscroll
10780 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10781 }
10782 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10783
10784 /* Don't call Fset_window_hscroll if value hasn't
10785 changed because it will prevent redisplay
10786 optimizations. */
10787 if (XFASTINT (w->hscroll) != hscroll)
10788 {
10789 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10790 w->hscroll = make_number (hscroll);
10791 hscrolled_p = 1;
10792 }
10793 }
10794 }
10795
10796 window = w->next;
10797 }
10798
10799 /* Value is non-zero if hscroll of any leaf window has been changed. */
10800 return hscrolled_p;
10801 }
10802
10803
10804 /* Set hscroll so that cursor is visible and not inside horizontal
10805 scroll margins for all windows in the tree rooted at WINDOW. See
10806 also hscroll_window_tree above. Value is non-zero if any window's
10807 hscroll has been changed. If it has, desired matrices on the frame
10808 of WINDOW are cleared. */
10809
10810 static int
10811 hscroll_windows (window)
10812 Lisp_Object window;
10813 {
10814 int hscrolled_p = hscroll_window_tree (window);
10815 if (hscrolled_p)
10816 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10817 return hscrolled_p;
10818 }
10819
10820
10821 \f
10822 /************************************************************************
10823 Redisplay
10824 ************************************************************************/
10825
10826 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10827 to a non-zero value. This is sometimes handy to have in a debugger
10828 session. */
10829
10830 #if GLYPH_DEBUG
10831
10832 /* First and last unchanged row for try_window_id. */
10833
10834 int debug_first_unchanged_at_end_vpos;
10835 int debug_last_unchanged_at_beg_vpos;
10836
10837 /* Delta vpos and y. */
10838
10839 int debug_dvpos, debug_dy;
10840
10841 /* Delta in characters and bytes for try_window_id. */
10842
10843 int debug_delta, debug_delta_bytes;
10844
10845 /* Values of window_end_pos and window_end_vpos at the end of
10846 try_window_id. */
10847
10848 EMACS_INT debug_end_pos, debug_end_vpos;
10849
10850 /* Append a string to W->desired_matrix->method. FMT is a printf
10851 format string. A1...A9 are a supplement for a variable-length
10852 argument list. If trace_redisplay_p is non-zero also printf the
10853 resulting string to stderr. */
10854
10855 static void
10856 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10857 struct window *w;
10858 char *fmt;
10859 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10860 {
10861 char buffer[512];
10862 char *method = w->desired_matrix->method;
10863 int len = strlen (method);
10864 int size = sizeof w->desired_matrix->method;
10865 int remaining = size - len - 1;
10866
10867 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10868 if (len && remaining)
10869 {
10870 method[len] = '|';
10871 --remaining, ++len;
10872 }
10873
10874 strncpy (method + len, buffer, remaining);
10875
10876 if (trace_redisplay_p)
10877 fprintf (stderr, "%p (%s): %s\n",
10878 w,
10879 ((BUFFERP (w->buffer)
10880 && STRINGP (XBUFFER (w->buffer)->name))
10881 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10882 : "no buffer"),
10883 buffer);
10884 }
10885
10886 #endif /* GLYPH_DEBUG */
10887
10888
10889 /* Value is non-zero if all changes in window W, which displays
10890 current_buffer, are in the text between START and END. START is a
10891 buffer position, END is given as a distance from Z. Used in
10892 redisplay_internal for display optimization. */
10893
10894 static INLINE int
10895 text_outside_line_unchanged_p (w, start, end)
10896 struct window *w;
10897 int start, end;
10898 {
10899 int unchanged_p = 1;
10900
10901 /* If text or overlays have changed, see where. */
10902 if (XFASTINT (w->last_modified) < MODIFF
10903 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10904 {
10905 /* Gap in the line? */
10906 if (GPT < start || Z - GPT < end)
10907 unchanged_p = 0;
10908
10909 /* Changes start in front of the line, or end after it? */
10910 if (unchanged_p
10911 && (BEG_UNCHANGED < start - 1
10912 || END_UNCHANGED < end))
10913 unchanged_p = 0;
10914
10915 /* If selective display, can't optimize if changes start at the
10916 beginning of the line. */
10917 if (unchanged_p
10918 && INTEGERP (current_buffer->selective_display)
10919 && XINT (current_buffer->selective_display) > 0
10920 && (BEG_UNCHANGED < start || GPT <= start))
10921 unchanged_p = 0;
10922
10923 /* If there are overlays at the start or end of the line, these
10924 may have overlay strings with newlines in them. A change at
10925 START, for instance, may actually concern the display of such
10926 overlay strings as well, and they are displayed on different
10927 lines. So, quickly rule out this case. (For the future, it
10928 might be desirable to implement something more telling than
10929 just BEG/END_UNCHANGED.) */
10930 if (unchanged_p)
10931 {
10932 if (BEG + BEG_UNCHANGED == start
10933 && overlay_touches_p (start))
10934 unchanged_p = 0;
10935 if (END_UNCHANGED == end
10936 && overlay_touches_p (Z - end))
10937 unchanged_p = 0;
10938 }
10939 }
10940
10941 return unchanged_p;
10942 }
10943
10944
10945 /* Do a frame update, taking possible shortcuts into account. This is
10946 the main external entry point for redisplay.
10947
10948 If the last redisplay displayed an echo area message and that message
10949 is no longer requested, we clear the echo area or bring back the
10950 mini-buffer if that is in use. */
10951
10952 void
10953 redisplay ()
10954 {
10955 redisplay_internal (0);
10956 }
10957
10958
10959 static Lisp_Object
10960 overlay_arrow_string_or_property (var)
10961 Lisp_Object var;
10962 {
10963 Lisp_Object val;
10964
10965 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10966 return val;
10967
10968 return Voverlay_arrow_string;
10969 }
10970
10971 /* Return 1 if there are any overlay-arrows in current_buffer. */
10972 static int
10973 overlay_arrow_in_current_buffer_p ()
10974 {
10975 Lisp_Object vlist;
10976
10977 for (vlist = Voverlay_arrow_variable_list;
10978 CONSP (vlist);
10979 vlist = XCDR (vlist))
10980 {
10981 Lisp_Object var = XCAR (vlist);
10982 Lisp_Object val;
10983
10984 if (!SYMBOLP (var))
10985 continue;
10986 val = find_symbol_value (var);
10987 if (MARKERP (val)
10988 && current_buffer == XMARKER (val)->buffer)
10989 return 1;
10990 }
10991 return 0;
10992 }
10993
10994
10995 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10996 has changed. */
10997
10998 static int
10999 overlay_arrows_changed_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, pstr;
11009
11010 if (!SYMBOLP (var))
11011 continue;
11012 val = find_symbol_value (var);
11013 if (!MARKERP (val))
11014 continue;
11015 if (! EQ (COERCE_MARKER (val),
11016 Fget (var, Qlast_arrow_position))
11017 || ! (pstr = overlay_arrow_string_or_property (var),
11018 EQ (pstr, Fget (var, Qlast_arrow_string))))
11019 return 1;
11020 }
11021 return 0;
11022 }
11023
11024 /* Mark overlay arrows to be updated on next redisplay. */
11025
11026 static void
11027 update_overlay_arrows (up_to_date)
11028 int up_to_date;
11029 {
11030 Lisp_Object vlist;
11031
11032 for (vlist = Voverlay_arrow_variable_list;
11033 CONSP (vlist);
11034 vlist = XCDR (vlist))
11035 {
11036 Lisp_Object var = XCAR (vlist);
11037
11038 if (!SYMBOLP (var))
11039 continue;
11040
11041 if (up_to_date > 0)
11042 {
11043 Lisp_Object val = find_symbol_value (var);
11044 Fput (var, Qlast_arrow_position,
11045 COERCE_MARKER (val));
11046 Fput (var, Qlast_arrow_string,
11047 overlay_arrow_string_or_property (var));
11048 }
11049 else if (up_to_date < 0
11050 || !NILP (Fget (var, Qlast_arrow_position)))
11051 {
11052 Fput (var, Qlast_arrow_position, Qt);
11053 Fput (var, Qlast_arrow_string, Qt);
11054 }
11055 }
11056 }
11057
11058
11059 /* Return overlay arrow string to display at row.
11060 Return integer (bitmap number) for arrow bitmap in left fringe.
11061 Return nil if no overlay arrow. */
11062
11063 static Lisp_Object
11064 overlay_arrow_at_row (it, row)
11065 struct it *it;
11066 struct glyph_row *row;
11067 {
11068 Lisp_Object vlist;
11069
11070 for (vlist = Voverlay_arrow_variable_list;
11071 CONSP (vlist);
11072 vlist = XCDR (vlist))
11073 {
11074 Lisp_Object var = XCAR (vlist);
11075 Lisp_Object val;
11076
11077 if (!SYMBOLP (var))
11078 continue;
11079
11080 val = find_symbol_value (var);
11081
11082 if (MARKERP (val)
11083 && current_buffer == XMARKER (val)->buffer
11084 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11085 {
11086 if (FRAME_WINDOW_P (it->f)
11087 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11088 {
11089 #ifdef HAVE_WINDOW_SYSTEM
11090 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11091 {
11092 int fringe_bitmap;
11093 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11094 return make_number (fringe_bitmap);
11095 }
11096 #endif
11097 return make_number (-1); /* Use default arrow bitmap */
11098 }
11099 return overlay_arrow_string_or_property (var);
11100 }
11101 }
11102
11103 return Qnil;
11104 }
11105
11106 /* Return 1 if point moved out of or into a composition. Otherwise
11107 return 0. PREV_BUF and PREV_PT are the last point buffer and
11108 position. BUF and PT are the current point buffer and position. */
11109
11110 int
11111 check_point_in_composition (prev_buf, prev_pt, buf, pt)
11112 struct buffer *prev_buf, *buf;
11113 int prev_pt, pt;
11114 {
11115 EMACS_INT start, end;
11116 Lisp_Object prop;
11117 Lisp_Object buffer;
11118
11119 XSETBUFFER (buffer, buf);
11120 /* Check a composition at the last point if point moved within the
11121 same buffer. */
11122 if (prev_buf == buf)
11123 {
11124 if (prev_pt == pt)
11125 /* Point didn't move. */
11126 return 0;
11127
11128 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
11129 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
11130 && COMPOSITION_VALID_P (start, end, prop)
11131 && start < prev_pt && end > prev_pt)
11132 /* The last point was within the composition. Return 1 iff
11133 point moved out of the composition. */
11134 return (pt <= start || pt >= end);
11135 }
11136
11137 /* Check a composition at the current point. */
11138 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
11139 && find_composition (pt, -1, &start, &end, &prop, buffer)
11140 && COMPOSITION_VALID_P (start, end, prop)
11141 && start < pt && end > pt);
11142 }
11143
11144
11145 /* Reconsider the setting of B->clip_changed which is displayed
11146 in window W. */
11147
11148 static INLINE void
11149 reconsider_clip_changes (w, b)
11150 struct window *w;
11151 struct buffer *b;
11152 {
11153 if (b->clip_changed
11154 && !NILP (w->window_end_valid)
11155 && w->current_matrix->buffer == b
11156 && w->current_matrix->zv == BUF_ZV (b)
11157 && w->current_matrix->begv == BUF_BEGV (b))
11158 b->clip_changed = 0;
11159
11160 /* If display wasn't paused, and W is not a tool bar window, see if
11161 point has been moved into or out of a composition. In that case,
11162 we set b->clip_changed to 1 to force updating the screen. If
11163 b->clip_changed has already been set to 1, we can skip this
11164 check. */
11165 if (!b->clip_changed
11166 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
11167 {
11168 int pt;
11169
11170 if (w == XWINDOW (selected_window))
11171 pt = BUF_PT (current_buffer);
11172 else
11173 pt = marker_position (w->pointm);
11174
11175 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
11176 || pt != XINT (w->last_point))
11177 && check_point_in_composition (w->current_matrix->buffer,
11178 XINT (w->last_point),
11179 XBUFFER (w->buffer), pt))
11180 b->clip_changed = 1;
11181 }
11182 }
11183 \f
11184
11185 /* Select FRAME to forward the values of frame-local variables into C
11186 variables so that the redisplay routines can access those values
11187 directly. */
11188
11189 static void
11190 select_frame_for_redisplay (frame)
11191 Lisp_Object frame;
11192 {
11193 Lisp_Object tail, symbol, val;
11194 Lisp_Object old = selected_frame;
11195 struct Lisp_Symbol *sym;
11196
11197 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
11198
11199 selected_frame = frame;
11200
11201 do
11202 {
11203 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
11204 if (CONSP (XCAR (tail))
11205 && (symbol = XCAR (XCAR (tail)),
11206 SYMBOLP (symbol))
11207 && (sym = indirect_variable (XSYMBOL (symbol)),
11208 val = sym->value,
11209 (BUFFER_LOCAL_VALUEP (val)))
11210 && XBUFFER_LOCAL_VALUE (val)->check_frame)
11211 /* Use find_symbol_value rather than Fsymbol_value
11212 to avoid an error if it is void. */
11213 find_symbol_value (symbol);
11214 } while (!EQ (frame, old) && (frame = old, 1));
11215 }
11216
11217
11218 #define STOP_POLLING \
11219 do { if (! polling_stopped_here) stop_polling (); \
11220 polling_stopped_here = 1; } while (0)
11221
11222 #define RESUME_POLLING \
11223 do { if (polling_stopped_here) start_polling (); \
11224 polling_stopped_here = 0; } while (0)
11225
11226
11227 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
11228 response to any user action; therefore, we should preserve the echo
11229 area. (Actually, our caller does that job.) Perhaps in the future
11230 avoid recentering windows if it is not necessary; currently that
11231 causes some problems. */
11232
11233 static void
11234 redisplay_internal (preserve_echo_area)
11235 int preserve_echo_area;
11236 {
11237 struct window *w = XWINDOW (selected_window);
11238 struct frame *f;
11239 int pause;
11240 int must_finish = 0;
11241 struct text_pos tlbufpos, tlendpos;
11242 int number_of_visible_frames;
11243 int count, count1;
11244 struct frame *sf;
11245 int polling_stopped_here = 0;
11246 Lisp_Object old_frame = selected_frame;
11247
11248 /* Non-zero means redisplay has to consider all windows on all
11249 frames. Zero means, only selected_window is considered. */
11250 int consider_all_windows_p;
11251
11252 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
11253
11254 /* No redisplay if running in batch mode or frame is not yet fully
11255 initialized, or redisplay is explicitly turned off by setting
11256 Vinhibit_redisplay. */
11257 if (noninteractive
11258 || !NILP (Vinhibit_redisplay))
11259 return;
11260
11261 /* Don't examine these until after testing Vinhibit_redisplay.
11262 When Emacs is shutting down, perhaps because its connection to
11263 X has dropped, we should not look at them at all. */
11264 f = XFRAME (w->frame);
11265 sf = SELECTED_FRAME ();
11266
11267 if (!f->glyphs_initialized_p)
11268 return;
11269
11270 /* The flag redisplay_performed_directly_p is set by
11271 direct_output_for_insert when it already did the whole screen
11272 update necessary. */
11273 if (redisplay_performed_directly_p)
11274 {
11275 redisplay_performed_directly_p = 0;
11276 if (!hscroll_windows (selected_window))
11277 return;
11278 }
11279
11280 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
11281 if (popup_activated ())
11282 return;
11283 #endif
11284
11285 /* I don't think this happens but let's be paranoid. */
11286 if (redisplaying_p)
11287 return;
11288
11289 /* Record a function that resets redisplaying_p to its old value
11290 when we leave this function. */
11291 count = SPECPDL_INDEX ();
11292 record_unwind_protect (unwind_redisplay,
11293 Fcons (make_number (redisplaying_p), selected_frame));
11294 ++redisplaying_p;
11295 specbind (Qinhibit_free_realized_faces, Qnil);
11296
11297 {
11298 Lisp_Object tail, frame;
11299
11300 FOR_EACH_FRAME (tail, frame)
11301 {
11302 struct frame *f = XFRAME (frame);
11303 f->already_hscrolled_p = 0;
11304 }
11305 }
11306
11307 retry:
11308 if (!EQ (old_frame, selected_frame)
11309 && FRAME_LIVE_P (XFRAME (old_frame)))
11310 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
11311 selected_frame and selected_window to be temporarily out-of-sync so
11312 when we come back here via `goto retry', we need to resync because we
11313 may need to run Elisp code (via prepare_menu_bars). */
11314 select_frame_for_redisplay (old_frame);
11315
11316 pause = 0;
11317 reconsider_clip_changes (w, current_buffer);
11318 last_escape_glyph_frame = NULL;
11319 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
11320
11321 /* If new fonts have been loaded that make a glyph matrix adjustment
11322 necessary, do it. */
11323 if (fonts_changed_p)
11324 {
11325 adjust_glyphs (NULL);
11326 ++windows_or_buffers_changed;
11327 fonts_changed_p = 0;
11328 }
11329
11330 /* If face_change_count is non-zero, init_iterator will free all
11331 realized faces, which includes the faces referenced from current
11332 matrices. So, we can't reuse current matrices in this case. */
11333 if (face_change_count)
11334 ++windows_or_buffers_changed;
11335
11336 if (FRAME_TERMCAP_P (sf)
11337 && FRAME_TTY (sf)->previous_frame != sf)
11338 {
11339 /* Since frames on a single ASCII terminal share the same
11340 display area, displaying a different frame means redisplay
11341 the whole thing. */
11342 windows_or_buffers_changed++;
11343 SET_FRAME_GARBAGED (sf);
11344 #ifndef WINDOWSNT
11345 set_tty_color_mode (FRAME_TTY (sf), sf);
11346 #endif
11347 FRAME_TTY (sf)->previous_frame = sf;
11348 }
11349
11350 /* Set the visible flags for all frames. Do this before checking
11351 for resized or garbaged frames; they want to know if their frames
11352 are visible. See the comment in frame.h for
11353 FRAME_SAMPLE_VISIBILITY. */
11354 {
11355 Lisp_Object tail, frame;
11356
11357 number_of_visible_frames = 0;
11358
11359 FOR_EACH_FRAME (tail, frame)
11360 {
11361 struct frame *f = XFRAME (frame);
11362
11363 FRAME_SAMPLE_VISIBILITY (f);
11364 if (FRAME_VISIBLE_P (f))
11365 ++number_of_visible_frames;
11366 clear_desired_matrices (f);
11367 }
11368 }
11369
11370
11371 /* Notice any pending interrupt request to change frame size. */
11372 do_pending_window_change (1);
11373
11374 /* Clear frames marked as garbaged. */
11375 if (frame_garbaged)
11376 clear_garbaged_frames ();
11377
11378 /* Build menubar and tool-bar items. */
11379 if (NILP (Vmemory_full))
11380 prepare_menu_bars ();
11381
11382 if (windows_or_buffers_changed)
11383 update_mode_lines++;
11384
11385 /* Detect case that we need to write or remove a star in the mode line. */
11386 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
11387 {
11388 w->update_mode_line = Qt;
11389 if (buffer_shared > 1)
11390 update_mode_lines++;
11391 }
11392
11393 /* Avoid invocation of point motion hooks by `current_column' below. */
11394 count1 = SPECPDL_INDEX ();
11395 specbind (Qinhibit_point_motion_hooks, Qt);
11396
11397 /* If %c is in the mode line, update it if needed. */
11398 if (!NILP (w->column_number_displayed)
11399 /* This alternative quickly identifies a common case
11400 where no change is needed. */
11401 && !(PT == XFASTINT (w->last_point)
11402 && XFASTINT (w->last_modified) >= MODIFF
11403 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11404 && (XFASTINT (w->column_number_displayed)
11405 != (int) current_column ())) /* iftc */
11406 w->update_mode_line = Qt;
11407
11408 unbind_to (count1, Qnil);
11409
11410 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
11411
11412 /* The variable buffer_shared is set in redisplay_window and
11413 indicates that we redisplay a buffer in different windows. See
11414 there. */
11415 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
11416 || cursor_type_changed);
11417
11418 /* If specs for an arrow have changed, do thorough redisplay
11419 to ensure we remove any arrow that should no longer exist. */
11420 if (overlay_arrows_changed_p ())
11421 consider_all_windows_p = windows_or_buffers_changed = 1;
11422
11423 /* Normally the message* functions will have already displayed and
11424 updated the echo area, but the frame may have been trashed, or
11425 the update may have been preempted, so display the echo area
11426 again here. Checking message_cleared_p captures the case that
11427 the echo area should be cleared. */
11428 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
11429 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
11430 || (message_cleared_p
11431 && minibuf_level == 0
11432 /* If the mini-window is currently selected, this means the
11433 echo-area doesn't show through. */
11434 && !MINI_WINDOW_P (XWINDOW (selected_window))))
11435 {
11436 int window_height_changed_p = echo_area_display (0);
11437 must_finish = 1;
11438
11439 /* If we don't display the current message, don't clear the
11440 message_cleared_p flag, because, if we did, we wouldn't clear
11441 the echo area in the next redisplay which doesn't preserve
11442 the echo area. */
11443 if (!display_last_displayed_message_p)
11444 message_cleared_p = 0;
11445
11446 if (fonts_changed_p)
11447 goto retry;
11448 else if (window_height_changed_p)
11449 {
11450 consider_all_windows_p = 1;
11451 ++update_mode_lines;
11452 ++windows_or_buffers_changed;
11453
11454 /* If window configuration was changed, frames may have been
11455 marked garbaged. Clear them or we will experience
11456 surprises wrt scrolling. */
11457 if (frame_garbaged)
11458 clear_garbaged_frames ();
11459 }
11460 }
11461 else if (EQ (selected_window, minibuf_window)
11462 && (current_buffer->clip_changed
11463 || XFASTINT (w->last_modified) < MODIFF
11464 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11465 && resize_mini_window (w, 0))
11466 {
11467 /* Resized active mini-window to fit the size of what it is
11468 showing if its contents might have changed. */
11469 must_finish = 1;
11470 consider_all_windows_p = 1;
11471 ++windows_or_buffers_changed;
11472 ++update_mode_lines;
11473
11474 /* If window configuration was changed, frames may have been
11475 marked garbaged. Clear them or we will experience
11476 surprises wrt scrolling. */
11477 if (frame_garbaged)
11478 clear_garbaged_frames ();
11479 }
11480
11481
11482 /* If showing the region, and mark has changed, we must redisplay
11483 the whole window. The assignment to this_line_start_pos prevents
11484 the optimization directly below this if-statement. */
11485 if (((!NILP (Vtransient_mark_mode)
11486 && !NILP (XBUFFER (w->buffer)->mark_active))
11487 != !NILP (w->region_showing))
11488 || (!NILP (w->region_showing)
11489 && !EQ (w->region_showing,
11490 Fmarker_position (XBUFFER (w->buffer)->mark))))
11491 CHARPOS (this_line_start_pos) = 0;
11492
11493 /* Optimize the case that only the line containing the cursor in the
11494 selected window has changed. Variables starting with this_ are
11495 set in display_line and record information about the line
11496 containing the cursor. */
11497 tlbufpos = this_line_start_pos;
11498 tlendpos = this_line_end_pos;
11499 if (!consider_all_windows_p
11500 && CHARPOS (tlbufpos) > 0
11501 && NILP (w->update_mode_line)
11502 && !current_buffer->clip_changed
11503 && !current_buffer->prevent_redisplay_optimizations_p
11504 && FRAME_VISIBLE_P (XFRAME (w->frame))
11505 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11506 /* Make sure recorded data applies to current buffer, etc. */
11507 && this_line_buffer == current_buffer
11508 && current_buffer == XBUFFER (w->buffer)
11509 && NILP (w->force_start)
11510 && NILP (w->optional_new_start)
11511 /* Point must be on the line that we have info recorded about. */
11512 && PT >= CHARPOS (tlbufpos)
11513 && PT <= Z - CHARPOS (tlendpos)
11514 /* All text outside that line, including its final newline,
11515 must be unchanged */
11516 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11517 CHARPOS (tlendpos)))
11518 {
11519 if (CHARPOS (tlbufpos) > BEGV
11520 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11521 && (CHARPOS (tlbufpos) == ZV
11522 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11523 /* Former continuation line has disappeared by becoming empty */
11524 goto cancel;
11525 else if (XFASTINT (w->last_modified) < MODIFF
11526 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11527 || MINI_WINDOW_P (w))
11528 {
11529 /* We have to handle the case of continuation around a
11530 wide-column character (See the comment in indent.c around
11531 line 885).
11532
11533 For instance, in the following case:
11534
11535 -------- Insert --------
11536 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11537 J_I_ ==> J_I_ `^^' are cursors.
11538 ^^ ^^
11539 -------- --------
11540
11541 As we have to redraw the line above, we should goto cancel. */
11542
11543 struct it it;
11544 int line_height_before = this_line_pixel_height;
11545
11546 /* Note that start_display will handle the case that the
11547 line starting at tlbufpos is a continuation lines. */
11548 start_display (&it, w, tlbufpos);
11549
11550 /* Implementation note: It this still necessary? */
11551 if (it.current_x != this_line_start_x)
11552 goto cancel;
11553
11554 TRACE ((stderr, "trying display optimization 1\n"));
11555 w->cursor.vpos = -1;
11556 overlay_arrow_seen = 0;
11557 it.vpos = this_line_vpos;
11558 it.current_y = this_line_y;
11559 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11560 display_line (&it);
11561
11562 /* If line contains point, is not continued,
11563 and ends at same distance from eob as before, we win */
11564 if (w->cursor.vpos >= 0
11565 /* Line is not continued, otherwise this_line_start_pos
11566 would have been set to 0 in display_line. */
11567 && CHARPOS (this_line_start_pos)
11568 /* Line ends as before. */
11569 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11570 /* Line has same height as before. Otherwise other lines
11571 would have to be shifted up or down. */
11572 && this_line_pixel_height == line_height_before)
11573 {
11574 /* If this is not the window's last line, we must adjust
11575 the charstarts of the lines below. */
11576 if (it.current_y < it.last_visible_y)
11577 {
11578 struct glyph_row *row
11579 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11580 int delta, delta_bytes;
11581
11582 if (Z - CHARPOS (tlendpos) == ZV)
11583 {
11584 /* This line ends at end of (accessible part of)
11585 buffer. There is no newline to count. */
11586 delta = (Z
11587 - CHARPOS (tlendpos)
11588 - MATRIX_ROW_START_CHARPOS (row));
11589 delta_bytes = (Z_BYTE
11590 - BYTEPOS (tlendpos)
11591 - MATRIX_ROW_START_BYTEPOS (row));
11592 }
11593 else
11594 {
11595 /* This line ends in a newline. Must take
11596 account of the newline and the rest of the
11597 text that follows. */
11598 delta = (Z
11599 - CHARPOS (tlendpos)
11600 - MATRIX_ROW_START_CHARPOS (row));
11601 delta_bytes = (Z_BYTE
11602 - BYTEPOS (tlendpos)
11603 - MATRIX_ROW_START_BYTEPOS (row));
11604 }
11605
11606 increment_matrix_positions (w->current_matrix,
11607 this_line_vpos + 1,
11608 w->current_matrix->nrows,
11609 delta, delta_bytes);
11610 }
11611
11612 /* If this row displays text now but previously didn't,
11613 or vice versa, w->window_end_vpos may have to be
11614 adjusted. */
11615 if ((it.glyph_row - 1)->displays_text_p)
11616 {
11617 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11618 XSETINT (w->window_end_vpos, this_line_vpos);
11619 }
11620 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11621 && this_line_vpos > 0)
11622 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11623 w->window_end_valid = Qnil;
11624
11625 /* Update hint: No need to try to scroll in update_window. */
11626 w->desired_matrix->no_scrolling_p = 1;
11627
11628 #if GLYPH_DEBUG
11629 *w->desired_matrix->method = 0;
11630 debug_method_add (w, "optimization 1");
11631 #endif
11632 #ifdef HAVE_WINDOW_SYSTEM
11633 update_window_fringes (w, 0);
11634 #endif
11635 goto update;
11636 }
11637 else
11638 goto cancel;
11639 }
11640 else if (/* Cursor position hasn't changed. */
11641 PT == XFASTINT (w->last_point)
11642 /* Make sure the cursor was last displayed
11643 in this window. Otherwise we have to reposition it. */
11644 && 0 <= w->cursor.vpos
11645 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11646 {
11647 if (!must_finish)
11648 {
11649 do_pending_window_change (1);
11650
11651 /* We used to always goto end_of_redisplay here, but this
11652 isn't enough if we have a blinking cursor. */
11653 if (w->cursor_off_p == w->last_cursor_off_p)
11654 goto end_of_redisplay;
11655 }
11656 goto update;
11657 }
11658 /* If highlighting the region, or if the cursor is in the echo area,
11659 then we can't just move the cursor. */
11660 else if (! (!NILP (Vtransient_mark_mode)
11661 && !NILP (current_buffer->mark_active))
11662 && (EQ (selected_window, current_buffer->last_selected_window)
11663 || highlight_nonselected_windows)
11664 && NILP (w->region_showing)
11665 && NILP (Vshow_trailing_whitespace)
11666 && !cursor_in_echo_area)
11667 {
11668 struct it it;
11669 struct glyph_row *row;
11670
11671 /* Skip from tlbufpos to PT and see where it is. Note that
11672 PT may be in invisible text. If so, we will end at the
11673 next visible position. */
11674 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11675 NULL, DEFAULT_FACE_ID);
11676 it.current_x = this_line_start_x;
11677 it.current_y = this_line_y;
11678 it.vpos = this_line_vpos;
11679
11680 /* The call to move_it_to stops in front of PT, but
11681 moves over before-strings. */
11682 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11683
11684 if (it.vpos == this_line_vpos
11685 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11686 row->enabled_p))
11687 {
11688 xassert (this_line_vpos == it.vpos);
11689 xassert (this_line_y == it.current_y);
11690 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11691 #if GLYPH_DEBUG
11692 *w->desired_matrix->method = 0;
11693 debug_method_add (w, "optimization 3");
11694 #endif
11695 goto update;
11696 }
11697 else
11698 goto cancel;
11699 }
11700
11701 cancel:
11702 /* Text changed drastically or point moved off of line. */
11703 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11704 }
11705
11706 CHARPOS (this_line_start_pos) = 0;
11707 consider_all_windows_p |= buffer_shared > 1;
11708 ++clear_face_cache_count;
11709 #ifdef HAVE_WINDOW_SYSTEM
11710 ++clear_image_cache_count;
11711 #endif
11712
11713 /* Build desired matrices, and update the display. If
11714 consider_all_windows_p is non-zero, do it for all windows on all
11715 frames. Otherwise do it for selected_window, only. */
11716
11717 if (consider_all_windows_p)
11718 {
11719 Lisp_Object tail, frame;
11720
11721 FOR_EACH_FRAME (tail, frame)
11722 XFRAME (frame)->updated_p = 0;
11723
11724 /* Recompute # windows showing selected buffer. This will be
11725 incremented each time such a window is displayed. */
11726 buffer_shared = 0;
11727
11728 FOR_EACH_FRAME (tail, frame)
11729 {
11730 struct frame *f = XFRAME (frame);
11731
11732 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
11733 {
11734 if (! EQ (frame, selected_frame))
11735 /* Select the frame, for the sake of frame-local
11736 variables. */
11737 select_frame_for_redisplay (frame);
11738
11739 /* Mark all the scroll bars to be removed; we'll redeem
11740 the ones we want when we redisplay their windows. */
11741 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
11742 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
11743
11744 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11745 redisplay_windows (FRAME_ROOT_WINDOW (f));
11746
11747 /* Any scroll bars which redisplay_windows should have
11748 nuked should now go away. */
11749 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
11750 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
11751
11752 /* If fonts changed, display again. */
11753 /* ??? rms: I suspect it is a mistake to jump all the way
11754 back to retry here. It should just retry this frame. */
11755 if (fonts_changed_p)
11756 goto retry;
11757
11758 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11759 {
11760 /* See if we have to hscroll. */
11761 if (!f->already_hscrolled_p)
11762 {
11763 f->already_hscrolled_p = 1;
11764 if (hscroll_windows (f->root_window))
11765 goto retry;
11766 }
11767
11768 /* Prevent various kinds of signals during display
11769 update. stdio is not robust about handling
11770 signals, which can cause an apparent I/O
11771 error. */
11772 if (interrupt_input)
11773 unrequest_sigio ();
11774 STOP_POLLING;
11775
11776 /* Update the display. */
11777 set_window_update_flags (XWINDOW (f->root_window), 1);
11778 pause |= update_frame (f, 0, 0);
11779 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11780 if (pause)
11781 break;
11782 #endif
11783
11784 f->updated_p = 1;
11785 }
11786 }
11787 }
11788
11789 if (!EQ (old_frame, selected_frame)
11790 && FRAME_LIVE_P (XFRAME (old_frame)))
11791 /* We played a bit fast-and-loose above and allowed selected_frame
11792 and selected_window to be temporarily out-of-sync but let's make
11793 sure this stays contained. */
11794 select_frame_for_redisplay (old_frame);
11795 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
11796
11797 if (!pause)
11798 {
11799 /* Do the mark_window_display_accurate after all windows have
11800 been redisplayed because this call resets flags in buffers
11801 which are needed for proper redisplay. */
11802 FOR_EACH_FRAME (tail, frame)
11803 {
11804 struct frame *f = XFRAME (frame);
11805 if (f->updated_p)
11806 {
11807 mark_window_display_accurate (f->root_window, 1);
11808 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
11809 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
11810 }
11811 }
11812 }
11813 }
11814 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11815 {
11816 Lisp_Object mini_window;
11817 struct frame *mini_frame;
11818
11819 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11820 /* Use list_of_error, not Qerror, so that
11821 we catch only errors and don't run the debugger. */
11822 internal_condition_case_1 (redisplay_window_1, selected_window,
11823 list_of_error,
11824 redisplay_window_error);
11825
11826 /* Compare desired and current matrices, perform output. */
11827
11828 update:
11829 /* If fonts changed, display again. */
11830 if (fonts_changed_p)
11831 goto retry;
11832
11833 /* Prevent various kinds of signals during display update.
11834 stdio is not robust about handling signals,
11835 which can cause an apparent I/O error. */
11836 if (interrupt_input)
11837 unrequest_sigio ();
11838 STOP_POLLING;
11839
11840 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11841 {
11842 if (hscroll_windows (selected_window))
11843 goto retry;
11844
11845 XWINDOW (selected_window)->must_be_updated_p = 1;
11846 pause = update_frame (sf, 0, 0);
11847 }
11848
11849 /* We may have called echo_area_display at the top of this
11850 function. If the echo area is on another frame, that may
11851 have put text on a frame other than the selected one, so the
11852 above call to update_frame would not have caught it. Catch
11853 it here. */
11854 mini_window = FRAME_MINIBUF_WINDOW (sf);
11855 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11856
11857 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11858 {
11859 XWINDOW (mini_window)->must_be_updated_p = 1;
11860 pause |= update_frame (mini_frame, 0, 0);
11861 if (!pause && hscroll_windows (mini_window))
11862 goto retry;
11863 }
11864 }
11865
11866 /* If display was paused because of pending input, make sure we do a
11867 thorough update the next time. */
11868 if (pause)
11869 {
11870 /* Prevent the optimization at the beginning of
11871 redisplay_internal that tries a single-line update of the
11872 line containing the cursor in the selected window. */
11873 CHARPOS (this_line_start_pos) = 0;
11874
11875 /* Let the overlay arrow be updated the next time. */
11876 update_overlay_arrows (0);
11877
11878 /* If we pause after scrolling, some rows in the current
11879 matrices of some windows are not valid. */
11880 if (!WINDOW_FULL_WIDTH_P (w)
11881 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11882 update_mode_lines = 1;
11883 }
11884 else
11885 {
11886 if (!consider_all_windows_p)
11887 {
11888 /* This has already been done above if
11889 consider_all_windows_p is set. */
11890 mark_window_display_accurate_1 (w, 1);
11891
11892 /* Say overlay arrows are up to date. */
11893 update_overlay_arrows (1);
11894
11895 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
11896 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
11897 }
11898
11899 update_mode_lines = 0;
11900 windows_or_buffers_changed = 0;
11901 cursor_type_changed = 0;
11902 }
11903
11904 /* Start SIGIO interrupts coming again. Having them off during the
11905 code above makes it less likely one will discard output, but not
11906 impossible, since there might be stuff in the system buffer here.
11907 But it is much hairier to try to do anything about that. */
11908 if (interrupt_input)
11909 request_sigio ();
11910 RESUME_POLLING;
11911
11912 /* If a frame has become visible which was not before, redisplay
11913 again, so that we display it. Expose events for such a frame
11914 (which it gets when becoming visible) don't call the parts of
11915 redisplay constructing glyphs, so simply exposing a frame won't
11916 display anything in this case. So, we have to display these
11917 frames here explicitly. */
11918 if (!pause)
11919 {
11920 Lisp_Object tail, frame;
11921 int new_count = 0;
11922
11923 FOR_EACH_FRAME (tail, frame)
11924 {
11925 int this_is_visible = 0;
11926
11927 if (XFRAME (frame)->visible)
11928 this_is_visible = 1;
11929 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11930 if (XFRAME (frame)->visible)
11931 this_is_visible = 1;
11932
11933 if (this_is_visible)
11934 new_count++;
11935 }
11936
11937 if (new_count != number_of_visible_frames)
11938 windows_or_buffers_changed++;
11939 }
11940
11941 /* Change frame size now if a change is pending. */
11942 do_pending_window_change (1);
11943
11944 /* If we just did a pending size change, or have additional
11945 visible frames, redisplay again. */
11946 if (windows_or_buffers_changed && !pause)
11947 goto retry;
11948
11949 /* Clear the face cache eventually. */
11950 if (consider_all_windows_p)
11951 {
11952 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11953 {
11954 clear_face_cache (0);
11955 clear_face_cache_count = 0;
11956 }
11957 #ifdef HAVE_WINDOW_SYSTEM
11958 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11959 {
11960 clear_image_caches (Qnil);
11961 clear_image_cache_count = 0;
11962 }
11963 #endif /* HAVE_WINDOW_SYSTEM */
11964 }
11965
11966 end_of_redisplay:
11967 unbind_to (count, Qnil);
11968 RESUME_POLLING;
11969 }
11970
11971
11972 /* Redisplay, but leave alone any recent echo area message unless
11973 another message has been requested in its place.
11974
11975 This is useful in situations where you need to redisplay but no
11976 user action has occurred, making it inappropriate for the message
11977 area to be cleared. See tracking_off and
11978 wait_reading_process_output for examples of these situations.
11979
11980 FROM_WHERE is an integer saying from where this function was
11981 called. This is useful for debugging. */
11982
11983 void
11984 redisplay_preserve_echo_area (from_where)
11985 int from_where;
11986 {
11987 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11988
11989 if (!NILP (echo_area_buffer[1]))
11990 {
11991 /* We have a previously displayed message, but no current
11992 message. Redisplay the previous message. */
11993 display_last_displayed_message_p = 1;
11994 redisplay_internal (1);
11995 display_last_displayed_message_p = 0;
11996 }
11997 else
11998 redisplay_internal (1);
11999
12000 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12001 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12002 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12003 }
12004
12005
12006 /* Function registered with record_unwind_protect in
12007 redisplay_internal. Reset redisplaying_p to the value it had
12008 before redisplay_internal was called, and clear
12009 prevent_freeing_realized_faces_p. It also selects the previously
12010 selected frame, unless it has been deleted (by an X connection
12011 failure during redisplay, for example). */
12012
12013 static Lisp_Object
12014 unwind_redisplay (val)
12015 Lisp_Object val;
12016 {
12017 Lisp_Object old_redisplaying_p, old_frame;
12018
12019 old_redisplaying_p = XCAR (val);
12020 redisplaying_p = XFASTINT (old_redisplaying_p);
12021 old_frame = XCDR (val);
12022 if (! EQ (old_frame, selected_frame)
12023 && FRAME_LIVE_P (XFRAME (old_frame)))
12024 select_frame_for_redisplay (old_frame);
12025 return Qnil;
12026 }
12027
12028
12029 /* Mark the display of window W as accurate or inaccurate. If
12030 ACCURATE_P is non-zero mark display of W as accurate. If
12031 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12032 redisplay_internal is called. */
12033
12034 static void
12035 mark_window_display_accurate_1 (w, accurate_p)
12036 struct window *w;
12037 int accurate_p;
12038 {
12039 if (BUFFERP (w->buffer))
12040 {
12041 struct buffer *b = XBUFFER (w->buffer);
12042
12043 w->last_modified
12044 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12045 w->last_overlay_modified
12046 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12047 w->last_had_star
12048 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12049
12050 if (accurate_p)
12051 {
12052 b->clip_changed = 0;
12053 b->prevent_redisplay_optimizations_p = 0;
12054
12055 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12056 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12057 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12058 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12059
12060 w->current_matrix->buffer = b;
12061 w->current_matrix->begv = BUF_BEGV (b);
12062 w->current_matrix->zv = BUF_ZV (b);
12063
12064 w->last_cursor = w->cursor;
12065 w->last_cursor_off_p = w->cursor_off_p;
12066
12067 if (w == XWINDOW (selected_window))
12068 w->last_point = make_number (BUF_PT (b));
12069 else
12070 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12071 }
12072 }
12073
12074 if (accurate_p)
12075 {
12076 w->window_end_valid = w->buffer;
12077 #if 0 /* This is incorrect with variable-height lines. */
12078 xassert (XINT (w->window_end_vpos)
12079 < (WINDOW_TOTAL_LINES (w)
12080 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
12081 #endif
12082 w->update_mode_line = Qnil;
12083 }
12084 }
12085
12086
12087 /* Mark the display of windows in the window tree rooted at WINDOW as
12088 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12089 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12090 be redisplayed the next time redisplay_internal is called. */
12091
12092 void
12093 mark_window_display_accurate (window, accurate_p)
12094 Lisp_Object window;
12095 int accurate_p;
12096 {
12097 struct window *w;
12098
12099 for (; !NILP (window); window = w->next)
12100 {
12101 w = XWINDOW (window);
12102 mark_window_display_accurate_1 (w, accurate_p);
12103
12104 if (!NILP (w->vchild))
12105 mark_window_display_accurate (w->vchild, accurate_p);
12106 if (!NILP (w->hchild))
12107 mark_window_display_accurate (w->hchild, accurate_p);
12108 }
12109
12110 if (accurate_p)
12111 {
12112 update_overlay_arrows (1);
12113 }
12114 else
12115 {
12116 /* Force a thorough redisplay the next time by setting
12117 last_arrow_position and last_arrow_string to t, which is
12118 unequal to any useful value of Voverlay_arrow_... */
12119 update_overlay_arrows (-1);
12120 }
12121 }
12122
12123
12124 /* Return value in display table DP (Lisp_Char_Table *) for character
12125 C. Since a display table doesn't have any parent, we don't have to
12126 follow parent. Do not call this function directly but use the
12127 macro DISP_CHAR_VECTOR. */
12128
12129 Lisp_Object
12130 disp_char_vector (dp, c)
12131 struct Lisp_Char_Table *dp;
12132 int c;
12133 {
12134 Lisp_Object val;
12135
12136 if (ASCII_CHAR_P (c))
12137 {
12138 val = dp->ascii;
12139 if (SUB_CHAR_TABLE_P (val))
12140 val = XSUB_CHAR_TABLE (val)->contents[c];
12141 }
12142 else
12143 {
12144 Lisp_Object table;
12145
12146 XSETCHAR_TABLE (table, dp);
12147 val = char_table_ref (table, c);
12148 }
12149 if (NILP (val))
12150 val = dp->defalt;
12151 return val;
12152 }
12153
12154
12155 \f
12156 /***********************************************************************
12157 Window Redisplay
12158 ***********************************************************************/
12159
12160 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
12161
12162 static void
12163 redisplay_windows (window)
12164 Lisp_Object window;
12165 {
12166 while (!NILP (window))
12167 {
12168 struct window *w = XWINDOW (window);
12169
12170 if (!NILP (w->hchild))
12171 redisplay_windows (w->hchild);
12172 else if (!NILP (w->vchild))
12173 redisplay_windows (w->vchild);
12174 else
12175 {
12176 displayed_buffer = XBUFFER (w->buffer);
12177 /* Use list_of_error, not Qerror, so that
12178 we catch only errors and don't run the debugger. */
12179 internal_condition_case_1 (redisplay_window_0, window,
12180 list_of_error,
12181 redisplay_window_error);
12182 }
12183
12184 window = w->next;
12185 }
12186 }
12187
12188 static Lisp_Object
12189 redisplay_window_error ()
12190 {
12191 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
12192 return Qnil;
12193 }
12194
12195 static Lisp_Object
12196 redisplay_window_0 (window)
12197 Lisp_Object window;
12198 {
12199 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12200 redisplay_window (window, 0);
12201 return Qnil;
12202 }
12203
12204 static Lisp_Object
12205 redisplay_window_1 (window)
12206 Lisp_Object window;
12207 {
12208 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
12209 redisplay_window (window, 1);
12210 return Qnil;
12211 }
12212 \f
12213
12214 /* Increment GLYPH until it reaches END or CONDITION fails while
12215 adding (GLYPH)->pixel_width to X. */
12216
12217 #define SKIP_GLYPHS(glyph, end, x, condition) \
12218 do \
12219 { \
12220 (x) += (glyph)->pixel_width; \
12221 ++(glyph); \
12222 } \
12223 while ((glyph) < (end) && (condition))
12224
12225
12226 /* Set cursor position of W. PT is assumed to be displayed in ROW.
12227 DELTA is the number of bytes by which positions recorded in ROW
12228 differ from current buffer positions.
12229
12230 Return 0 if cursor is not on this row. 1 otherwise. */
12231
12232 int
12233 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
12234 struct window *w;
12235 struct glyph_row *row;
12236 struct glyph_matrix *matrix;
12237 int delta, delta_bytes, dy, dvpos;
12238 {
12239 struct glyph *glyph = row->glyphs[TEXT_AREA];
12240 struct glyph *end = glyph + row->used[TEXT_AREA];
12241 struct glyph *cursor = NULL;
12242 /* The first glyph that starts a sequence of glyphs from string. */
12243 struct glyph *string_start;
12244 /* The X coordinate of string_start. */
12245 int string_start_x;
12246 /* The last known character position. */
12247 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
12248 /* The last known character position before string_start. */
12249 int string_before_pos;
12250 int x = row->x;
12251 int cursor_x = x;
12252 int cursor_from_overlay_pos = 0;
12253 int pt_old = PT - delta;
12254
12255 /* Skip over glyphs not having an object at the start of the row.
12256 These are special glyphs like truncation marks on terminal
12257 frames. */
12258 if (row->displays_text_p)
12259 while (glyph < end
12260 && INTEGERP (glyph->object)
12261 && glyph->charpos < 0)
12262 {
12263 x += glyph->pixel_width;
12264 ++glyph;
12265 }
12266
12267 string_start = NULL;
12268 while (glyph < end
12269 && !INTEGERP (glyph->object)
12270 && (!BUFFERP (glyph->object)
12271 || (last_pos = glyph->charpos) < pt_old
12272 || glyph->avoid_cursor_p))
12273 {
12274 if (! STRINGP (glyph->object))
12275 {
12276 string_start = NULL;
12277 x += glyph->pixel_width;
12278 ++glyph;
12279 if (cursor_from_overlay_pos
12280 && last_pos >= cursor_from_overlay_pos)
12281 {
12282 cursor_from_overlay_pos = 0;
12283 cursor = 0;
12284 }
12285 }
12286 else
12287 {
12288 if (string_start == NULL)
12289 {
12290 string_before_pos = last_pos;
12291 string_start = glyph;
12292 string_start_x = x;
12293 }
12294 /* Skip all glyphs from string. */
12295 do
12296 {
12297 Lisp_Object cprop;
12298 int pos;
12299 if ((cursor == NULL || glyph > cursor)
12300 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
12301 Qcursor, (glyph)->object),
12302 !NILP (cprop))
12303 && (pos = string_buffer_position (w, glyph->object,
12304 string_before_pos),
12305 (pos == 0 /* From overlay */
12306 || pos == pt_old)))
12307 {
12308 /* Estimate overlay buffer position from the buffer
12309 positions of the glyphs before and after the overlay.
12310 Add 1 to last_pos so that if point corresponds to the
12311 glyph right after the overlay, we still use a 'cursor'
12312 property found in that overlay. */
12313 cursor_from_overlay_pos = (pos ? 0 : last_pos
12314 + (INTEGERP (cprop) ? XINT (cprop) : 0));
12315 cursor = glyph;
12316 cursor_x = x;
12317 }
12318 x += glyph->pixel_width;
12319 ++glyph;
12320 }
12321 while (glyph < end && EQ (glyph->object, string_start->object));
12322 }
12323 }
12324
12325 if (cursor != NULL)
12326 {
12327 glyph = cursor;
12328 x = cursor_x;
12329 }
12330 else if (row->ends_in_ellipsis_p && glyph == end)
12331 {
12332 /* Scan back over the ellipsis glyphs, decrementing positions. */
12333 while (glyph > row->glyphs[TEXT_AREA]
12334 && (glyph - 1)->charpos == last_pos)
12335 glyph--, x -= glyph->pixel_width;
12336 /* That loop always goes one position too far,
12337 including the glyph before the ellipsis.
12338 So scan forward over that one. */
12339 x += glyph->pixel_width;
12340 glyph++;
12341 }
12342 else if (string_start
12343 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
12344 {
12345 /* We may have skipped over point because the previous glyphs
12346 are from string. As there's no easy way to know the
12347 character position of the current glyph, find the correct
12348 glyph on point by scanning from string_start again. */
12349 Lisp_Object limit;
12350 Lisp_Object string;
12351 struct glyph *stop = glyph;
12352 int pos;
12353
12354 limit = make_number (pt_old + 1);
12355 glyph = string_start;
12356 x = string_start_x;
12357 string = glyph->object;
12358 pos = string_buffer_position (w, string, string_before_pos);
12359 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
12360 because we always put cursor after overlay strings. */
12361 while (pos == 0 && glyph < stop)
12362 {
12363 string = glyph->object;
12364 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12365 if (glyph < stop)
12366 pos = string_buffer_position (w, glyph->object, string_before_pos);
12367 }
12368
12369 while (glyph < stop)
12370 {
12371 pos = XINT (Fnext_single_char_property_change
12372 (make_number (pos), Qdisplay, Qnil, limit));
12373 if (pos > pt_old)
12374 break;
12375 /* Skip glyphs from the same string. */
12376 string = glyph->object;
12377 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12378 /* Skip glyphs from an overlay. */
12379 while (glyph < stop
12380 && ! string_buffer_position (w, glyph->object, pos))
12381 {
12382 string = glyph->object;
12383 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
12384 }
12385 }
12386
12387 /* If we reached the end of the line, and end was from a string,
12388 cursor is not on this line. */
12389 if (glyph == end && row->continued_p)
12390 return 0;
12391 }
12392
12393 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
12394 w->cursor.x = x;
12395 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
12396 w->cursor.y = row->y + dy;
12397
12398 if (w == XWINDOW (selected_window))
12399 {
12400 if (!row->continued_p
12401 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
12402 && row->x == 0)
12403 {
12404 this_line_buffer = XBUFFER (w->buffer);
12405
12406 CHARPOS (this_line_start_pos)
12407 = MATRIX_ROW_START_CHARPOS (row) + delta;
12408 BYTEPOS (this_line_start_pos)
12409 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
12410
12411 CHARPOS (this_line_end_pos)
12412 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
12413 BYTEPOS (this_line_end_pos)
12414 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
12415
12416 this_line_y = w->cursor.y;
12417 this_line_pixel_height = row->height;
12418 this_line_vpos = w->cursor.vpos;
12419 this_line_start_x = row->x;
12420 }
12421 else
12422 CHARPOS (this_line_start_pos) = 0;
12423 }
12424
12425 return 1;
12426 }
12427
12428
12429 /* Run window scroll functions, if any, for WINDOW with new window
12430 start STARTP. Sets the window start of WINDOW to that position.
12431
12432 We assume that the window's buffer is really current. */
12433
12434 static INLINE struct text_pos
12435 run_window_scroll_functions (window, startp)
12436 Lisp_Object window;
12437 struct text_pos startp;
12438 {
12439 struct window *w = XWINDOW (window);
12440 SET_MARKER_FROM_TEXT_POS (w->start, startp);
12441
12442 if (current_buffer != XBUFFER (w->buffer))
12443 abort ();
12444
12445 if (!NILP (Vwindow_scroll_functions))
12446 {
12447 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12448 make_number (CHARPOS (startp)));
12449 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12450 /* In case the hook functions switch buffers. */
12451 if (current_buffer != XBUFFER (w->buffer))
12452 set_buffer_internal_1 (XBUFFER (w->buffer));
12453 }
12454
12455 return startp;
12456 }
12457
12458
12459 /* Make sure the line containing the cursor is fully visible.
12460 A value of 1 means there is nothing to be done.
12461 (Either the line is fully visible, or it cannot be made so,
12462 or we cannot tell.)
12463
12464 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12465 is higher than window.
12466
12467 A value of 0 means the caller should do scrolling
12468 as if point had gone off the screen. */
12469
12470 static int
12471 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12472 struct window *w;
12473 int force_p;
12474 int current_matrix_p;
12475 {
12476 struct glyph_matrix *matrix;
12477 struct glyph_row *row;
12478 int window_height;
12479
12480 if (!make_cursor_line_fully_visible_p)
12481 return 1;
12482
12483 /* It's not always possible to find the cursor, e.g, when a window
12484 is full of overlay strings. Don't do anything in that case. */
12485 if (w->cursor.vpos < 0)
12486 return 1;
12487
12488 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12489 row = MATRIX_ROW (matrix, w->cursor.vpos);
12490
12491 /* If the cursor row is not partially visible, there's nothing to do. */
12492 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12493 return 1;
12494
12495 /* If the row the cursor is in is taller than the window's height,
12496 it's not clear what to do, so do nothing. */
12497 window_height = window_box_height (w);
12498 if (row->height >= window_height)
12499 {
12500 if (!force_p || MINI_WINDOW_P (w)
12501 || w->vscroll || w->cursor.vpos == 0)
12502 return 1;
12503 }
12504 return 0;
12505
12506 #if 0
12507 /* This code used to try to scroll the window just enough to make
12508 the line visible. It returned 0 to say that the caller should
12509 allocate larger glyph matrices. */
12510
12511 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12512 {
12513 int dy = row->height - row->visible_height;
12514 w->vscroll = 0;
12515 w->cursor.y += dy;
12516 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12517 }
12518 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12519 {
12520 int dy = - (row->height - row->visible_height);
12521 w->vscroll = dy;
12522 w->cursor.y += dy;
12523 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12524 }
12525
12526 /* When we change the cursor y-position of the selected window,
12527 change this_line_y as well so that the display optimization for
12528 the cursor line of the selected window in redisplay_internal uses
12529 the correct y-position. */
12530 if (w == XWINDOW (selected_window))
12531 this_line_y = w->cursor.y;
12532
12533 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12534 redisplay with larger matrices. */
12535 if (matrix->nrows < required_matrix_height (w))
12536 {
12537 fonts_changed_p = 1;
12538 return 0;
12539 }
12540
12541 return 1;
12542 #endif /* 0 */
12543 }
12544
12545
12546 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12547 non-zero means only WINDOW is redisplayed in redisplay_internal.
12548 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12549 in redisplay_window to bring a partially visible line into view in
12550 the case that only the cursor has moved.
12551
12552 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12553 last screen line's vertical height extends past the end of the screen.
12554
12555 Value is
12556
12557 1 if scrolling succeeded
12558
12559 0 if scrolling didn't find point.
12560
12561 -1 if new fonts have been loaded so that we must interrupt
12562 redisplay, adjust glyph matrices, and try again. */
12563
12564 enum
12565 {
12566 SCROLLING_SUCCESS,
12567 SCROLLING_FAILED,
12568 SCROLLING_NEED_LARGER_MATRICES
12569 };
12570
12571 static int
12572 try_scrolling (window, just_this_one_p, scroll_conservatively,
12573 scroll_step, temp_scroll_step, last_line_misfit)
12574 Lisp_Object window;
12575 int just_this_one_p;
12576 EMACS_INT scroll_conservatively, scroll_step;
12577 int temp_scroll_step;
12578 int last_line_misfit;
12579 {
12580 struct window *w = XWINDOW (window);
12581 struct frame *f = XFRAME (w->frame);
12582 struct text_pos scroll_margin_pos;
12583 struct text_pos pos;
12584 struct text_pos startp;
12585 struct it it;
12586 Lisp_Object window_end;
12587 int this_scroll_margin;
12588 int dy = 0;
12589 int scroll_max;
12590 int rc;
12591 int amount_to_scroll = 0;
12592 Lisp_Object aggressive;
12593 int height;
12594 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12595
12596 #if GLYPH_DEBUG
12597 debug_method_add (w, "try_scrolling");
12598 #endif
12599
12600 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12601
12602 /* Compute scroll margin height in pixels. We scroll when point is
12603 within this distance from the top or bottom of the window. */
12604 if (scroll_margin > 0)
12605 {
12606 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12607 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12608 }
12609 else
12610 this_scroll_margin = 0;
12611
12612 /* Force scroll_conservatively to have a reasonable value so it doesn't
12613 cause an overflow while computing how much to scroll. */
12614 if (scroll_conservatively)
12615 scroll_conservatively = min (scroll_conservatively,
12616 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12617
12618 /* Compute how much we should try to scroll maximally to bring point
12619 into view. */
12620 if (scroll_step || scroll_conservatively || temp_scroll_step)
12621 scroll_max = max (scroll_step,
12622 max (scroll_conservatively, temp_scroll_step));
12623 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12624 || NUMBERP (current_buffer->scroll_up_aggressively))
12625 /* We're trying to scroll because of aggressive scrolling
12626 but no scroll_step is set. Choose an arbitrary one. Maybe
12627 there should be a variable for this. */
12628 scroll_max = 10;
12629 else
12630 scroll_max = 0;
12631 scroll_max *= FRAME_LINE_HEIGHT (f);
12632
12633 /* Decide whether we have to scroll down. Start at the window end
12634 and move this_scroll_margin up to find the position of the scroll
12635 margin. */
12636 window_end = Fwindow_end (window, Qt);
12637
12638 too_near_end:
12639
12640 CHARPOS (scroll_margin_pos) = XINT (window_end);
12641 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12642
12643 if (this_scroll_margin || extra_scroll_margin_lines)
12644 {
12645 start_display (&it, w, scroll_margin_pos);
12646 if (this_scroll_margin)
12647 move_it_vertically_backward (&it, this_scroll_margin);
12648 if (extra_scroll_margin_lines)
12649 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12650 scroll_margin_pos = it.current.pos;
12651 }
12652
12653 if (PT >= CHARPOS (scroll_margin_pos))
12654 {
12655 int y0;
12656
12657 /* Point is in the scroll margin at the bottom of the window, or
12658 below. Compute a new window start that makes point visible. */
12659
12660 /* Compute the distance from the scroll margin to PT.
12661 Give up if the distance is greater than scroll_max. */
12662 start_display (&it, w, scroll_margin_pos);
12663 y0 = it.current_y;
12664 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12665 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12666
12667 /* To make point visible, we have to move the window start
12668 down so that the line the cursor is in is visible, which
12669 means we have to add in the height of the cursor line. */
12670 dy = line_bottom_y (&it) - y0;
12671
12672 if (dy > scroll_max)
12673 return SCROLLING_FAILED;
12674
12675 /* Move the window start down. If scrolling conservatively,
12676 move it just enough down to make point visible. If
12677 scroll_step is set, move it down by scroll_step. */
12678 start_display (&it, w, startp);
12679
12680 if (scroll_conservatively)
12681 /* Set AMOUNT_TO_SCROLL to at least one line,
12682 and at most scroll_conservatively lines. */
12683 amount_to_scroll
12684 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12685 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12686 else if (scroll_step || temp_scroll_step)
12687 amount_to_scroll = scroll_max;
12688 else
12689 {
12690 aggressive = current_buffer->scroll_up_aggressively;
12691 height = WINDOW_BOX_TEXT_HEIGHT (w);
12692 if (NUMBERP (aggressive))
12693 {
12694 double float_amount = XFLOATINT (aggressive) * height;
12695 amount_to_scroll = float_amount;
12696 if (amount_to_scroll == 0 && float_amount > 0)
12697 amount_to_scroll = 1;
12698 }
12699 }
12700
12701 if (amount_to_scroll <= 0)
12702 return SCROLLING_FAILED;
12703
12704 /* If moving by amount_to_scroll leaves STARTP unchanged,
12705 move it down one screen line. */
12706
12707 move_it_vertically (&it, amount_to_scroll);
12708 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12709 move_it_by_lines (&it, 1, 1);
12710 startp = it.current.pos;
12711 }
12712 else
12713 {
12714 /* See if point is inside the scroll margin at the top of the
12715 window. */
12716 scroll_margin_pos = startp;
12717 if (this_scroll_margin)
12718 {
12719 start_display (&it, w, startp);
12720 move_it_vertically (&it, this_scroll_margin);
12721 scroll_margin_pos = it.current.pos;
12722 }
12723
12724 if (PT < CHARPOS (scroll_margin_pos))
12725 {
12726 /* Point is in the scroll margin at the top of the window or
12727 above what is displayed in the window. */
12728 int y0;
12729
12730 /* Compute the vertical distance from PT to the scroll
12731 margin position. Give up if distance is greater than
12732 scroll_max. */
12733 SET_TEXT_POS (pos, PT, PT_BYTE);
12734 start_display (&it, w, pos);
12735 y0 = it.current_y;
12736 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12737 it.last_visible_y, -1,
12738 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12739 dy = it.current_y - y0;
12740 if (dy > scroll_max)
12741 return SCROLLING_FAILED;
12742
12743 /* Compute new window start. */
12744 start_display (&it, w, startp);
12745
12746 if (scroll_conservatively)
12747 amount_to_scroll
12748 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12749 else if (scroll_step || temp_scroll_step)
12750 amount_to_scroll = scroll_max;
12751 else
12752 {
12753 aggressive = current_buffer->scroll_down_aggressively;
12754 height = WINDOW_BOX_TEXT_HEIGHT (w);
12755 if (NUMBERP (aggressive))
12756 {
12757 double float_amount = XFLOATINT (aggressive) * height;
12758 amount_to_scroll = float_amount;
12759 if (amount_to_scroll == 0 && float_amount > 0)
12760 amount_to_scroll = 1;
12761 }
12762 }
12763
12764 if (amount_to_scroll <= 0)
12765 return SCROLLING_FAILED;
12766
12767 move_it_vertically_backward (&it, amount_to_scroll);
12768 startp = it.current.pos;
12769 }
12770 }
12771
12772 /* Run window scroll functions. */
12773 startp = run_window_scroll_functions (window, startp);
12774
12775 /* Display the window. Give up if new fonts are loaded, or if point
12776 doesn't appear. */
12777 if (!try_window (window, startp, 0))
12778 rc = SCROLLING_NEED_LARGER_MATRICES;
12779 else if (w->cursor.vpos < 0)
12780 {
12781 clear_glyph_matrix (w->desired_matrix);
12782 rc = SCROLLING_FAILED;
12783 }
12784 else
12785 {
12786 /* Maybe forget recorded base line for line number display. */
12787 if (!just_this_one_p
12788 || current_buffer->clip_changed
12789 || BEG_UNCHANGED < CHARPOS (startp))
12790 w->base_line_number = Qnil;
12791
12792 /* If cursor ends up on a partially visible line,
12793 treat that as being off the bottom of the screen. */
12794 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12795 {
12796 clear_glyph_matrix (w->desired_matrix);
12797 ++extra_scroll_margin_lines;
12798 goto too_near_end;
12799 }
12800 rc = SCROLLING_SUCCESS;
12801 }
12802
12803 return rc;
12804 }
12805
12806
12807 /* Compute a suitable window start for window W if display of W starts
12808 on a continuation line. Value is non-zero if a new window start
12809 was computed.
12810
12811 The new window start will be computed, based on W's width, starting
12812 from the start of the continued line. It is the start of the
12813 screen line with the minimum distance from the old start W->start. */
12814
12815 static int
12816 compute_window_start_on_continuation_line (w)
12817 struct window *w;
12818 {
12819 struct text_pos pos, start_pos;
12820 int window_start_changed_p = 0;
12821
12822 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12823
12824 /* If window start is on a continuation line... Window start may be
12825 < BEGV in case there's invisible text at the start of the
12826 buffer (M-x rmail, for example). */
12827 if (CHARPOS (start_pos) > BEGV
12828 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12829 {
12830 struct it it;
12831 struct glyph_row *row;
12832
12833 /* Handle the case that the window start is out of range. */
12834 if (CHARPOS (start_pos) < BEGV)
12835 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12836 else if (CHARPOS (start_pos) > ZV)
12837 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12838
12839 /* Find the start of the continued line. This should be fast
12840 because scan_buffer is fast (newline cache). */
12841 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12842 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12843 row, DEFAULT_FACE_ID);
12844 reseat_at_previous_visible_line_start (&it);
12845
12846 /* If the line start is "too far" away from the window start,
12847 say it takes too much time to compute a new window start. */
12848 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12849 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12850 {
12851 int min_distance, distance;
12852
12853 /* Move forward by display lines to find the new window
12854 start. If window width was enlarged, the new start can
12855 be expected to be > the old start. If window width was
12856 decreased, the new window start will be < the old start.
12857 So, we're looking for the display line start with the
12858 minimum distance from the old window start. */
12859 pos = it.current.pos;
12860 min_distance = INFINITY;
12861 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12862 distance < min_distance)
12863 {
12864 min_distance = distance;
12865 pos = it.current.pos;
12866 move_it_by_lines (&it, 1, 0);
12867 }
12868
12869 /* Set the window start there. */
12870 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12871 window_start_changed_p = 1;
12872 }
12873 }
12874
12875 return window_start_changed_p;
12876 }
12877
12878
12879 /* Try cursor movement in case text has not changed in window WINDOW,
12880 with window start STARTP. Value is
12881
12882 CURSOR_MOVEMENT_SUCCESS if successful
12883
12884 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12885
12886 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12887 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12888 we want to scroll as if scroll-step were set to 1. See the code.
12889
12890 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12891 which case we have to abort this redisplay, and adjust matrices
12892 first. */
12893
12894 enum
12895 {
12896 CURSOR_MOVEMENT_SUCCESS,
12897 CURSOR_MOVEMENT_CANNOT_BE_USED,
12898 CURSOR_MOVEMENT_MUST_SCROLL,
12899 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12900 };
12901
12902 static int
12903 try_cursor_movement (window, startp, scroll_step)
12904 Lisp_Object window;
12905 struct text_pos startp;
12906 int *scroll_step;
12907 {
12908 struct window *w = XWINDOW (window);
12909 struct frame *f = XFRAME (w->frame);
12910 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12911
12912 #if GLYPH_DEBUG
12913 if (inhibit_try_cursor_movement)
12914 return rc;
12915 #endif
12916
12917 /* Handle case where text has not changed, only point, and it has
12918 not moved off the frame. */
12919 if (/* Point may be in this window. */
12920 PT >= CHARPOS (startp)
12921 /* Selective display hasn't changed. */
12922 && !current_buffer->clip_changed
12923 /* Function force-mode-line-update is used to force a thorough
12924 redisplay. It sets either windows_or_buffers_changed or
12925 update_mode_lines. So don't take a shortcut here for these
12926 cases. */
12927 && !update_mode_lines
12928 && !windows_or_buffers_changed
12929 && !cursor_type_changed
12930 /* Can't use this case if highlighting a region. When a
12931 region exists, cursor movement has to do more than just
12932 set the cursor. */
12933 && !(!NILP (Vtransient_mark_mode)
12934 && !NILP (current_buffer->mark_active))
12935 && NILP (w->region_showing)
12936 && NILP (Vshow_trailing_whitespace)
12937 /* Right after splitting windows, last_point may be nil. */
12938 && INTEGERP (w->last_point)
12939 /* This code is not used for mini-buffer for the sake of the case
12940 of redisplaying to replace an echo area message; since in
12941 that case the mini-buffer contents per se are usually
12942 unchanged. This code is of no real use in the mini-buffer
12943 since the handling of this_line_start_pos, etc., in redisplay
12944 handles the same cases. */
12945 && !EQ (window, minibuf_window)
12946 /* When splitting windows or for new windows, it happens that
12947 redisplay is called with a nil window_end_vpos or one being
12948 larger than the window. This should really be fixed in
12949 window.c. I don't have this on my list, now, so we do
12950 approximately the same as the old redisplay code. --gerd. */
12951 && INTEGERP (w->window_end_vpos)
12952 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12953 && (FRAME_WINDOW_P (f)
12954 || !overlay_arrow_in_current_buffer_p ()))
12955 {
12956 int this_scroll_margin, top_scroll_margin;
12957 struct glyph_row *row = NULL;
12958
12959 #if GLYPH_DEBUG
12960 debug_method_add (w, "cursor movement");
12961 #endif
12962
12963 /* Scroll if point within this distance from the top or bottom
12964 of the window. This is a pixel value. */
12965 this_scroll_margin = max (0, scroll_margin);
12966 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12967 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12968
12969 top_scroll_margin = this_scroll_margin;
12970 if (WINDOW_WANTS_HEADER_LINE_P (w))
12971 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12972
12973 /* Start with the row the cursor was displayed during the last
12974 not paused redisplay. Give up if that row is not valid. */
12975 if (w->last_cursor.vpos < 0
12976 || w->last_cursor.vpos >= w->current_matrix->nrows)
12977 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12978 else
12979 {
12980 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12981 if (row->mode_line_p)
12982 ++row;
12983 if (!row->enabled_p)
12984 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12985 }
12986
12987 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12988 {
12989 int scroll_p = 0;
12990 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12991
12992 if (PT > XFASTINT (w->last_point))
12993 {
12994 /* Point has moved forward. */
12995 while (MATRIX_ROW_END_CHARPOS (row) < PT
12996 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12997 {
12998 xassert (row->enabled_p);
12999 ++row;
13000 }
13001
13002 /* The end position of a row equals the start position
13003 of the next row. If PT is there, we would rather
13004 display it in the next line. */
13005 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13006 && MATRIX_ROW_END_CHARPOS (row) == PT
13007 && !cursor_row_p (w, row))
13008 ++row;
13009
13010 /* If within the scroll margin, scroll. Note that
13011 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
13012 the next line would be drawn, and that
13013 this_scroll_margin can be zero. */
13014 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
13015 || PT > MATRIX_ROW_END_CHARPOS (row)
13016 /* Line is completely visible last line in window
13017 and PT is to be set in the next line. */
13018 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
13019 && PT == MATRIX_ROW_END_CHARPOS (row)
13020 && !row->ends_at_zv_p
13021 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13022 scroll_p = 1;
13023 }
13024 else if (PT < XFASTINT (w->last_point))
13025 {
13026 /* Cursor has to be moved backward. Note that PT >=
13027 CHARPOS (startp) because of the outer if-statement. */
13028 while (!row->mode_line_p
13029 && (MATRIX_ROW_START_CHARPOS (row) > PT
13030 || (MATRIX_ROW_START_CHARPOS (row) == PT
13031 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
13032 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
13033 row > w->current_matrix->rows
13034 && (row-1)->ends_in_newline_from_string_p))))
13035 && (row->y > top_scroll_margin
13036 || CHARPOS (startp) == BEGV))
13037 {
13038 xassert (row->enabled_p);
13039 --row;
13040 }
13041
13042 /* Consider the following case: Window starts at BEGV,
13043 there is invisible, intangible text at BEGV, so that
13044 display starts at some point START > BEGV. It can
13045 happen that we are called with PT somewhere between
13046 BEGV and START. Try to handle that case. */
13047 if (row < w->current_matrix->rows
13048 || row->mode_line_p)
13049 {
13050 row = w->current_matrix->rows;
13051 if (row->mode_line_p)
13052 ++row;
13053 }
13054
13055 /* Due to newlines in overlay strings, we may have to
13056 skip forward over overlay strings. */
13057 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13058 && MATRIX_ROW_END_CHARPOS (row) == PT
13059 && !cursor_row_p (w, row))
13060 ++row;
13061
13062 /* If within the scroll margin, scroll. */
13063 if (row->y < top_scroll_margin
13064 && CHARPOS (startp) != BEGV)
13065 scroll_p = 1;
13066 }
13067 else
13068 {
13069 /* Cursor did not move. So don't scroll even if cursor line
13070 is partially visible, as it was so before. */
13071 rc = CURSOR_MOVEMENT_SUCCESS;
13072 }
13073
13074 if (PT < MATRIX_ROW_START_CHARPOS (row)
13075 || PT > MATRIX_ROW_END_CHARPOS (row))
13076 {
13077 /* if PT is not in the glyph row, give up. */
13078 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13079 }
13080 else if (rc != CURSOR_MOVEMENT_SUCCESS
13081 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
13082 && make_cursor_line_fully_visible_p)
13083 {
13084 if (PT == MATRIX_ROW_END_CHARPOS (row)
13085 && !row->ends_at_zv_p
13086 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
13087 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13088 else if (row->height > window_box_height (w))
13089 {
13090 /* If we end up in a partially visible line, let's
13091 make it fully visible, except when it's taller
13092 than the window, in which case we can't do much
13093 about it. */
13094 *scroll_step = 1;
13095 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13096 }
13097 else
13098 {
13099 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13100 if (!cursor_row_fully_visible_p (w, 0, 1))
13101 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13102 else
13103 rc = CURSOR_MOVEMENT_SUCCESS;
13104 }
13105 }
13106 else if (scroll_p)
13107 rc = CURSOR_MOVEMENT_MUST_SCROLL;
13108 else
13109 {
13110 do
13111 {
13112 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
13113 {
13114 rc = CURSOR_MOVEMENT_SUCCESS;
13115 break;
13116 }
13117 ++row;
13118 }
13119 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
13120 && MATRIX_ROW_START_CHARPOS (row) == PT
13121 && cursor_row_p (w, row));
13122 }
13123 }
13124 }
13125
13126 return rc;
13127 }
13128
13129 void
13130 set_vertical_scroll_bar (w)
13131 struct window *w;
13132 {
13133 int start, end, whole;
13134
13135 /* Calculate the start and end positions for the current window.
13136 At some point, it would be nice to choose between scrollbars
13137 which reflect the whole buffer size, with special markers
13138 indicating narrowing, and scrollbars which reflect only the
13139 visible region.
13140
13141 Note that mini-buffers sometimes aren't displaying any text. */
13142 if (!MINI_WINDOW_P (w)
13143 || (w == XWINDOW (minibuf_window)
13144 && NILP (echo_area_buffer[0])))
13145 {
13146 struct buffer *buf = XBUFFER (w->buffer);
13147 whole = BUF_ZV (buf) - BUF_BEGV (buf);
13148 start = marker_position (w->start) - BUF_BEGV (buf);
13149 /* I don't think this is guaranteed to be right. For the
13150 moment, we'll pretend it is. */
13151 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
13152
13153 if (end < start)
13154 end = start;
13155 if (whole < (end - start))
13156 whole = end - start;
13157 }
13158 else
13159 start = end = whole = 0;
13160
13161 /* Indicate what this scroll bar ought to be displaying now. */
13162 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13163 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
13164 (w, end - start, whole, start);
13165 }
13166
13167
13168 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
13169 selected_window is redisplayed.
13170
13171 We can return without actually redisplaying the window if
13172 fonts_changed_p is nonzero. In that case, redisplay_internal will
13173 retry. */
13174
13175 static void
13176 redisplay_window (window, just_this_one_p)
13177 Lisp_Object window;
13178 int just_this_one_p;
13179 {
13180 struct window *w = XWINDOW (window);
13181 struct frame *f = XFRAME (w->frame);
13182 struct buffer *buffer = XBUFFER (w->buffer);
13183 struct buffer *old = current_buffer;
13184 struct text_pos lpoint, opoint, startp;
13185 int update_mode_line;
13186 int tem;
13187 struct it it;
13188 /* Record it now because it's overwritten. */
13189 int current_matrix_up_to_date_p = 0;
13190 int used_current_matrix_p = 0;
13191 /* This is less strict than current_matrix_up_to_date_p.
13192 It indictes that the buffer contents and narrowing are unchanged. */
13193 int buffer_unchanged_p = 0;
13194 int temp_scroll_step = 0;
13195 int count = SPECPDL_INDEX ();
13196 int rc;
13197 int centering_position = -1;
13198 int last_line_misfit = 0;
13199 int beg_unchanged, end_unchanged;
13200
13201 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13202 opoint = lpoint;
13203
13204 /* W must be a leaf window here. */
13205 xassert (!NILP (w->buffer));
13206 #if GLYPH_DEBUG
13207 *w->desired_matrix->method = 0;
13208 #endif
13209
13210 restart:
13211 reconsider_clip_changes (w, buffer);
13212
13213 /* Has the mode line to be updated? */
13214 update_mode_line = (!NILP (w->update_mode_line)
13215 || update_mode_lines
13216 || buffer->clip_changed
13217 || buffer->prevent_redisplay_optimizations_p);
13218
13219 if (MINI_WINDOW_P (w))
13220 {
13221 if (w == XWINDOW (echo_area_window)
13222 && !NILP (echo_area_buffer[0]))
13223 {
13224 if (update_mode_line)
13225 /* We may have to update a tty frame's menu bar or a
13226 tool-bar. Example `M-x C-h C-h C-g'. */
13227 goto finish_menu_bars;
13228 else
13229 /* We've already displayed the echo area glyphs in this window. */
13230 goto finish_scroll_bars;
13231 }
13232 else if ((w != XWINDOW (minibuf_window)
13233 || minibuf_level == 0)
13234 /* When buffer is nonempty, redisplay window normally. */
13235 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
13236 /* Quail displays non-mini buffers in minibuffer window.
13237 In that case, redisplay the window normally. */
13238 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
13239 {
13240 /* W is a mini-buffer window, but it's not active, so clear
13241 it. */
13242 int yb = window_text_bottom_y (w);
13243 struct glyph_row *row;
13244 int y;
13245
13246 for (y = 0, row = w->desired_matrix->rows;
13247 y < yb;
13248 y += row->height, ++row)
13249 blank_row (w, row, y);
13250 goto finish_scroll_bars;
13251 }
13252
13253 clear_glyph_matrix (w->desired_matrix);
13254 }
13255
13256 /* Otherwise set up data on this window; select its buffer and point
13257 value. */
13258 /* Really select the buffer, for the sake of buffer-local
13259 variables. */
13260 set_buffer_internal_1 (XBUFFER (w->buffer));
13261
13262 current_matrix_up_to_date_p
13263 = (!NILP (w->window_end_valid)
13264 && !current_buffer->clip_changed
13265 && !current_buffer->prevent_redisplay_optimizations_p
13266 && XFASTINT (w->last_modified) >= MODIFF
13267 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13268
13269 /* Run the window-bottom-change-functions
13270 if it is possible that the text on the screen has changed
13271 (either due to modification of the text, or any other reason). */
13272 if (!current_matrix_up_to_date_p
13273 && !NILP (Vwindow_text_change_functions))
13274 {
13275 safe_run_hooks (Qwindow_text_change_functions);
13276 goto restart;
13277 }
13278
13279 beg_unchanged = BEG_UNCHANGED;
13280 end_unchanged = END_UNCHANGED;
13281
13282 SET_TEXT_POS (opoint, PT, PT_BYTE);
13283
13284 specbind (Qinhibit_point_motion_hooks, Qt);
13285
13286 buffer_unchanged_p
13287 = (!NILP (w->window_end_valid)
13288 && !current_buffer->clip_changed
13289 && XFASTINT (w->last_modified) >= MODIFF
13290 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
13291
13292 /* When windows_or_buffers_changed is non-zero, we can't rely on
13293 the window end being valid, so set it to nil there. */
13294 if (windows_or_buffers_changed)
13295 {
13296 /* If window starts on a continuation line, maybe adjust the
13297 window start in case the window's width changed. */
13298 if (XMARKER (w->start)->buffer == current_buffer)
13299 compute_window_start_on_continuation_line (w);
13300
13301 w->window_end_valid = Qnil;
13302 }
13303
13304 /* Some sanity checks. */
13305 CHECK_WINDOW_END (w);
13306 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
13307 abort ();
13308 if (BYTEPOS (opoint) < CHARPOS (opoint))
13309 abort ();
13310
13311 /* If %c is in mode line, update it if needed. */
13312 if (!NILP (w->column_number_displayed)
13313 /* This alternative quickly identifies a common case
13314 where no change is needed. */
13315 && !(PT == XFASTINT (w->last_point)
13316 && XFASTINT (w->last_modified) >= MODIFF
13317 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
13318 && (XFASTINT (w->column_number_displayed)
13319 != (int) current_column ())) /* iftc */
13320 update_mode_line = 1;
13321
13322 /* Count number of windows showing the selected buffer. An indirect
13323 buffer counts as its base buffer. */
13324 if (!just_this_one_p)
13325 {
13326 struct buffer *current_base, *window_base;
13327 current_base = current_buffer;
13328 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
13329 if (current_base->base_buffer)
13330 current_base = current_base->base_buffer;
13331 if (window_base->base_buffer)
13332 window_base = window_base->base_buffer;
13333 if (current_base == window_base)
13334 buffer_shared++;
13335 }
13336
13337 /* Point refers normally to the selected window. For any other
13338 window, set up appropriate value. */
13339 if (!EQ (window, selected_window))
13340 {
13341 int new_pt = XMARKER (w->pointm)->charpos;
13342 int new_pt_byte = marker_byte_position (w->pointm);
13343 if (new_pt < BEGV)
13344 {
13345 new_pt = BEGV;
13346 new_pt_byte = BEGV_BYTE;
13347 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
13348 }
13349 else if (new_pt > (ZV - 1))
13350 {
13351 new_pt = ZV;
13352 new_pt_byte = ZV_BYTE;
13353 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
13354 }
13355
13356 /* We don't use SET_PT so that the point-motion hooks don't run. */
13357 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
13358 }
13359
13360 /* If any of the character widths specified in the display table
13361 have changed, invalidate the width run cache. It's true that
13362 this may be a bit late to catch such changes, but the rest of
13363 redisplay goes (non-fatally) haywire when the display table is
13364 changed, so why should we worry about doing any better? */
13365 if (current_buffer->width_run_cache)
13366 {
13367 struct Lisp_Char_Table *disptab = buffer_display_table ();
13368
13369 if (! disptab_matches_widthtab (disptab,
13370 XVECTOR (current_buffer->width_table)))
13371 {
13372 invalidate_region_cache (current_buffer,
13373 current_buffer->width_run_cache,
13374 BEG, Z);
13375 recompute_width_table (current_buffer, disptab);
13376 }
13377 }
13378
13379 /* If window-start is screwed up, choose a new one. */
13380 if (XMARKER (w->start)->buffer != current_buffer)
13381 goto recenter;
13382
13383 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13384
13385 /* If someone specified a new starting point but did not insist,
13386 check whether it can be used. */
13387 if (!NILP (w->optional_new_start)
13388 && CHARPOS (startp) >= BEGV
13389 && CHARPOS (startp) <= ZV)
13390 {
13391 w->optional_new_start = Qnil;
13392 start_display (&it, w, startp);
13393 move_it_to (&it, PT, 0, it.last_visible_y, -1,
13394 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13395 if (IT_CHARPOS (it) == PT)
13396 w->force_start = Qt;
13397 /* IT may overshoot PT if text at PT is invisible. */
13398 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
13399 w->force_start = Qt;
13400 }
13401
13402 force_start:
13403
13404 /* Handle case where place to start displaying has been specified,
13405 unless the specified location is outside the accessible range. */
13406 if (!NILP (w->force_start)
13407 || w->frozen_window_start_p)
13408 {
13409 /* We set this later on if we have to adjust point. */
13410 int new_vpos = -1;
13411 int val;
13412
13413 w->force_start = Qnil;
13414 w->vscroll = 0;
13415 w->window_end_valid = Qnil;
13416
13417 /* Forget any recorded base line for line number display. */
13418 if (!buffer_unchanged_p)
13419 w->base_line_number = Qnil;
13420
13421 /* Redisplay the mode line. Select the buffer properly for that.
13422 Also, run the hook window-scroll-functions
13423 because we have scrolled. */
13424 /* Note, we do this after clearing force_start because
13425 if there's an error, it is better to forget about force_start
13426 than to get into an infinite loop calling the hook functions
13427 and having them get more errors. */
13428 if (!update_mode_line
13429 || ! NILP (Vwindow_scroll_functions))
13430 {
13431 update_mode_line = 1;
13432 w->update_mode_line = Qt;
13433 startp = run_window_scroll_functions (window, startp);
13434 }
13435
13436 w->last_modified = make_number (0);
13437 w->last_overlay_modified = make_number (0);
13438 if (CHARPOS (startp) < BEGV)
13439 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
13440 else if (CHARPOS (startp) > ZV)
13441 SET_TEXT_POS (startp, ZV, ZV_BYTE);
13442
13443 /* Redisplay, then check if cursor has been set during the
13444 redisplay. Give up if new fonts were loaded. */
13445 val = try_window (window, startp, 1);
13446 if (!val)
13447 {
13448 w->force_start = Qt;
13449 clear_glyph_matrix (w->desired_matrix);
13450 goto need_larger_matrices;
13451 }
13452 /* Point was outside the scroll margins. */
13453 if (val < 0)
13454 new_vpos = window_box_height (w) / 2;
13455
13456 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
13457 {
13458 /* If point does not appear, try to move point so it does
13459 appear. The desired matrix has been built above, so we
13460 can use it here. */
13461 new_vpos = window_box_height (w) / 2;
13462 }
13463
13464 if (!cursor_row_fully_visible_p (w, 0, 0))
13465 {
13466 /* Point does appear, but on a line partly visible at end of window.
13467 Move it back to a fully-visible line. */
13468 new_vpos = window_box_height (w);
13469 }
13470
13471 /* If we need to move point for either of the above reasons,
13472 now actually do it. */
13473 if (new_vpos >= 0)
13474 {
13475 struct glyph_row *row;
13476
13477 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13478 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13479 ++row;
13480
13481 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13482 MATRIX_ROW_START_BYTEPOS (row));
13483
13484 if (w != XWINDOW (selected_window))
13485 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13486 else if (current_buffer == old)
13487 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13488
13489 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13490
13491 /* If we are highlighting the region, then we just changed
13492 the region, so redisplay to show it. */
13493 if (!NILP (Vtransient_mark_mode)
13494 && !NILP (current_buffer->mark_active))
13495 {
13496 clear_glyph_matrix (w->desired_matrix);
13497 if (!try_window (window, startp, 0))
13498 goto need_larger_matrices;
13499 }
13500 }
13501
13502 #if GLYPH_DEBUG
13503 debug_method_add (w, "forced window start");
13504 #endif
13505 goto done;
13506 }
13507
13508 /* Handle case where text has not changed, only point, and it has
13509 not moved off the frame, and we are not retrying after hscroll.
13510 (current_matrix_up_to_date_p is nonzero when retrying.) */
13511 if (current_matrix_up_to_date_p
13512 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13513 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13514 {
13515 switch (rc)
13516 {
13517 case CURSOR_MOVEMENT_SUCCESS:
13518 used_current_matrix_p = 1;
13519 goto done;
13520
13521 #if 0 /* try_cursor_movement never returns this value. */
13522 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13523 goto need_larger_matrices;
13524 #endif
13525
13526 case CURSOR_MOVEMENT_MUST_SCROLL:
13527 goto try_to_scroll;
13528
13529 default:
13530 abort ();
13531 }
13532 }
13533 /* If current starting point was originally the beginning of a line
13534 but no longer is, find a new starting point. */
13535 else if (!NILP (w->start_at_line_beg)
13536 && !(CHARPOS (startp) <= BEGV
13537 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13538 {
13539 #if GLYPH_DEBUG
13540 debug_method_add (w, "recenter 1");
13541 #endif
13542 goto recenter;
13543 }
13544
13545 /* Try scrolling with try_window_id. Value is > 0 if update has
13546 been done, it is -1 if we know that the same window start will
13547 not work. It is 0 if unsuccessful for some other reason. */
13548 else if ((tem = try_window_id (w)) != 0)
13549 {
13550 #if GLYPH_DEBUG
13551 debug_method_add (w, "try_window_id %d", tem);
13552 #endif
13553
13554 if (fonts_changed_p)
13555 goto need_larger_matrices;
13556 if (tem > 0)
13557 goto done;
13558
13559 /* Otherwise try_window_id has returned -1 which means that we
13560 don't want the alternative below this comment to execute. */
13561 }
13562 else if (CHARPOS (startp) >= BEGV
13563 && CHARPOS (startp) <= ZV
13564 && PT >= CHARPOS (startp)
13565 && (CHARPOS (startp) < ZV
13566 /* Avoid starting at end of buffer. */
13567 || CHARPOS (startp) == BEGV
13568 || (XFASTINT (w->last_modified) >= MODIFF
13569 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13570 {
13571
13572 /* If first window line is a continuation line, and window start
13573 is inside the modified region, but the first change is before
13574 current window start, we must select a new window start.
13575
13576 However, if this is the result of a down-mouse event (e.g. by
13577 extending the mouse-drag-overlay), we don't want to select a
13578 new window start, since that would change the position under
13579 the mouse, resulting in an unwanted mouse-movement rather
13580 than a simple mouse-click. */
13581 if (NILP (w->start_at_line_beg)
13582 && NILP (do_mouse_tracking)
13583 && CHARPOS (startp) > BEGV
13584 && CHARPOS (startp) > BEG + beg_unchanged
13585 && CHARPOS (startp) <= Z - end_unchanged)
13586 {
13587 w->force_start = Qt;
13588 if (XMARKER (w->start)->buffer == current_buffer)
13589 compute_window_start_on_continuation_line (w);
13590 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13591 goto force_start;
13592 }
13593
13594 #if GLYPH_DEBUG
13595 debug_method_add (w, "same window start");
13596 #endif
13597
13598 /* Try to redisplay starting at same place as before.
13599 If point has not moved off frame, accept the results. */
13600 if (!current_matrix_up_to_date_p
13601 /* Don't use try_window_reusing_current_matrix in this case
13602 because a window scroll function can have changed the
13603 buffer. */
13604 || !NILP (Vwindow_scroll_functions)
13605 || MINI_WINDOW_P (w)
13606 || !(used_current_matrix_p
13607 = try_window_reusing_current_matrix (w)))
13608 {
13609 IF_DEBUG (debug_method_add (w, "1"));
13610 if (try_window (window, startp, 1) < 0)
13611 /* -1 means we need to scroll.
13612 0 means we need new matrices, but fonts_changed_p
13613 is set in that case, so we will detect it below. */
13614 goto try_to_scroll;
13615 }
13616
13617 if (fonts_changed_p)
13618 goto need_larger_matrices;
13619
13620 if (w->cursor.vpos >= 0)
13621 {
13622 if (!just_this_one_p
13623 || current_buffer->clip_changed
13624 || BEG_UNCHANGED < CHARPOS (startp))
13625 /* Forget any recorded base line for line number display. */
13626 w->base_line_number = Qnil;
13627
13628 if (!cursor_row_fully_visible_p (w, 1, 0))
13629 {
13630 clear_glyph_matrix (w->desired_matrix);
13631 last_line_misfit = 1;
13632 }
13633 /* Drop through and scroll. */
13634 else
13635 goto done;
13636 }
13637 else
13638 clear_glyph_matrix (w->desired_matrix);
13639 }
13640
13641 try_to_scroll:
13642
13643 w->last_modified = make_number (0);
13644 w->last_overlay_modified = make_number (0);
13645
13646 /* Redisplay the mode line. Select the buffer properly for that. */
13647 if (!update_mode_line)
13648 {
13649 update_mode_line = 1;
13650 w->update_mode_line = Qt;
13651 }
13652
13653 /* Try to scroll by specified few lines. */
13654 if ((scroll_conservatively
13655 || scroll_step
13656 || temp_scroll_step
13657 || NUMBERP (current_buffer->scroll_up_aggressively)
13658 || NUMBERP (current_buffer->scroll_down_aggressively))
13659 && !current_buffer->clip_changed
13660 && CHARPOS (startp) >= BEGV
13661 && CHARPOS (startp) <= ZV)
13662 {
13663 /* The function returns -1 if new fonts were loaded, 1 if
13664 successful, 0 if not successful. */
13665 int rc = try_scrolling (window, just_this_one_p,
13666 scroll_conservatively,
13667 scroll_step,
13668 temp_scroll_step, last_line_misfit);
13669 switch (rc)
13670 {
13671 case SCROLLING_SUCCESS:
13672 goto done;
13673
13674 case SCROLLING_NEED_LARGER_MATRICES:
13675 goto need_larger_matrices;
13676
13677 case SCROLLING_FAILED:
13678 break;
13679
13680 default:
13681 abort ();
13682 }
13683 }
13684
13685 /* Finally, just choose place to start which centers point */
13686
13687 recenter:
13688 if (centering_position < 0)
13689 centering_position = window_box_height (w) / 2;
13690
13691 #if GLYPH_DEBUG
13692 debug_method_add (w, "recenter");
13693 #endif
13694
13695 /* w->vscroll = 0; */
13696
13697 /* Forget any previously recorded base line for line number display. */
13698 if (!buffer_unchanged_p)
13699 w->base_line_number = Qnil;
13700
13701 /* Move backward half the height of the window. */
13702 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13703 it.current_y = it.last_visible_y;
13704 move_it_vertically_backward (&it, centering_position);
13705 xassert (IT_CHARPOS (it) >= BEGV);
13706
13707 /* The function move_it_vertically_backward may move over more
13708 than the specified y-distance. If it->w is small, e.g. a
13709 mini-buffer window, we may end up in front of the window's
13710 display area. Start displaying at the start of the line
13711 containing PT in this case. */
13712 if (it.current_y <= 0)
13713 {
13714 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13715 move_it_vertically_backward (&it, 0);
13716 #if 0
13717 /* I think this assert is bogus if buffer contains
13718 invisible text or images. KFS. */
13719 xassert (IT_CHARPOS (it) <= PT);
13720 #endif
13721 it.current_y = 0;
13722 }
13723
13724 it.current_x = it.hpos = 0;
13725
13726 /* Set startp here explicitly in case that helps avoid an infinite loop
13727 in case the window-scroll-functions functions get errors. */
13728 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13729
13730 /* Run scroll hooks. */
13731 startp = run_window_scroll_functions (window, it.current.pos);
13732
13733 /* Redisplay the window. */
13734 if (!current_matrix_up_to_date_p
13735 || windows_or_buffers_changed
13736 || cursor_type_changed
13737 /* Don't use try_window_reusing_current_matrix in this case
13738 because it can have changed the buffer. */
13739 || !NILP (Vwindow_scroll_functions)
13740 || !just_this_one_p
13741 || MINI_WINDOW_P (w)
13742 || !(used_current_matrix_p
13743 = try_window_reusing_current_matrix (w)))
13744 try_window (window, startp, 0);
13745
13746 /* If new fonts have been loaded (due to fontsets), give up. We
13747 have to start a new redisplay since we need to re-adjust glyph
13748 matrices. */
13749 if (fonts_changed_p)
13750 goto need_larger_matrices;
13751
13752 /* If cursor did not appear assume that the middle of the window is
13753 in the first line of the window. Do it again with the next line.
13754 (Imagine a window of height 100, displaying two lines of height
13755 60. Moving back 50 from it->last_visible_y will end in the first
13756 line.) */
13757 if (w->cursor.vpos < 0)
13758 {
13759 if (!NILP (w->window_end_valid)
13760 && PT >= Z - XFASTINT (w->window_end_pos))
13761 {
13762 clear_glyph_matrix (w->desired_matrix);
13763 move_it_by_lines (&it, 1, 0);
13764 try_window (window, it.current.pos, 0);
13765 }
13766 else if (PT < IT_CHARPOS (it))
13767 {
13768 clear_glyph_matrix (w->desired_matrix);
13769 move_it_by_lines (&it, -1, 0);
13770 try_window (window, it.current.pos, 0);
13771 }
13772 else
13773 {
13774 /* Not much we can do about it. */
13775 }
13776 }
13777
13778 /* Consider the following case: Window starts at BEGV, there is
13779 invisible, intangible text at BEGV, so that display starts at
13780 some point START > BEGV. It can happen that we are called with
13781 PT somewhere between BEGV and START. Try to handle that case. */
13782 if (w->cursor.vpos < 0)
13783 {
13784 struct glyph_row *row = w->current_matrix->rows;
13785 if (row->mode_line_p)
13786 ++row;
13787 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13788 }
13789
13790 if (!cursor_row_fully_visible_p (w, 0, 0))
13791 {
13792 /* If vscroll is enabled, disable it and try again. */
13793 if (w->vscroll)
13794 {
13795 w->vscroll = 0;
13796 clear_glyph_matrix (w->desired_matrix);
13797 goto recenter;
13798 }
13799
13800 /* If centering point failed to make the whole line visible,
13801 put point at the top instead. That has to make the whole line
13802 visible, if it can be done. */
13803 if (centering_position == 0)
13804 goto done;
13805
13806 clear_glyph_matrix (w->desired_matrix);
13807 centering_position = 0;
13808 goto recenter;
13809 }
13810
13811 done:
13812
13813 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13814 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13815 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13816 ? Qt : Qnil);
13817
13818 /* Display the mode line, if we must. */
13819 if ((update_mode_line
13820 /* If window not full width, must redo its mode line
13821 if (a) the window to its side is being redone and
13822 (b) we do a frame-based redisplay. This is a consequence
13823 of how inverted lines are drawn in frame-based redisplay. */
13824 || (!just_this_one_p
13825 && !FRAME_WINDOW_P (f)
13826 && !WINDOW_FULL_WIDTH_P (w))
13827 /* Line number to display. */
13828 || INTEGERP (w->base_line_pos)
13829 /* Column number is displayed and different from the one displayed. */
13830 || (!NILP (w->column_number_displayed)
13831 && (XFASTINT (w->column_number_displayed)
13832 != (int) current_column ()))) /* iftc */
13833 /* This means that the window has a mode line. */
13834 && (WINDOW_WANTS_MODELINE_P (w)
13835 || WINDOW_WANTS_HEADER_LINE_P (w)))
13836 {
13837 display_mode_lines (w);
13838
13839 /* If mode line height has changed, arrange for a thorough
13840 immediate redisplay using the correct mode line height. */
13841 if (WINDOW_WANTS_MODELINE_P (w)
13842 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13843 {
13844 fonts_changed_p = 1;
13845 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13846 = DESIRED_MODE_LINE_HEIGHT (w);
13847 }
13848
13849 /* If top line height has changed, arrange for a thorough
13850 immediate redisplay using the correct mode line height. */
13851 if (WINDOW_WANTS_HEADER_LINE_P (w)
13852 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13853 {
13854 fonts_changed_p = 1;
13855 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13856 = DESIRED_HEADER_LINE_HEIGHT (w);
13857 }
13858
13859 if (fonts_changed_p)
13860 goto need_larger_matrices;
13861 }
13862
13863 if (!line_number_displayed
13864 && !BUFFERP (w->base_line_pos))
13865 {
13866 w->base_line_pos = Qnil;
13867 w->base_line_number = Qnil;
13868 }
13869
13870 finish_menu_bars:
13871
13872 /* When we reach a frame's selected window, redo the frame's menu bar. */
13873 if (update_mode_line
13874 && EQ (FRAME_SELECTED_WINDOW (f), window))
13875 {
13876 int redisplay_menu_p = 0;
13877 int redisplay_tool_bar_p = 0;
13878
13879 if (FRAME_WINDOW_P (f))
13880 {
13881 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13882 || defined (USE_GTK)
13883 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13884 #else
13885 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13886 #endif
13887 }
13888 else
13889 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13890
13891 if (redisplay_menu_p)
13892 display_menu_bar (w);
13893
13894 #ifdef HAVE_WINDOW_SYSTEM
13895 if (FRAME_WINDOW_P (f))
13896 {
13897 #if defined (USE_GTK) || USE_MAC_TOOLBAR
13898 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13899 #else
13900 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13901 && (FRAME_TOOL_BAR_LINES (f) > 0
13902 || !NILP (Vauto_resize_tool_bars));
13903 #endif
13904
13905 if (redisplay_tool_bar_p && redisplay_tool_bar (f))
13906 {
13907 extern int ignore_mouse_drag_p;
13908 ignore_mouse_drag_p = 1;
13909 }
13910 }
13911 #endif
13912 }
13913
13914 #ifdef HAVE_WINDOW_SYSTEM
13915 if (FRAME_WINDOW_P (f)
13916 && update_window_fringes (w, (just_this_one_p
13917 || (!used_current_matrix_p && !overlay_arrow_seen)
13918 || w->pseudo_window_p)))
13919 {
13920 update_begin (f);
13921 BLOCK_INPUT;
13922 if (draw_window_fringes (w, 1))
13923 x_draw_vertical_border (w);
13924 UNBLOCK_INPUT;
13925 update_end (f);
13926 }
13927 #endif /* HAVE_WINDOW_SYSTEM */
13928
13929 /* We go to this label, with fonts_changed_p nonzero,
13930 if it is necessary to try again using larger glyph matrices.
13931 We have to redeem the scroll bar even in this case,
13932 because the loop in redisplay_internal expects that. */
13933 need_larger_matrices:
13934 ;
13935 finish_scroll_bars:
13936
13937 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13938 {
13939 /* Set the thumb's position and size. */
13940 set_vertical_scroll_bar (w);
13941
13942 /* Note that we actually used the scroll bar attached to this
13943 window, so it shouldn't be deleted at the end of redisplay. */
13944 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
13945 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
13946 }
13947
13948 /* Restore current_buffer and value of point in it. */
13949 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13950 set_buffer_internal_1 (old);
13951 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
13952 shorter. This can be caused by log truncation in *Messages*. */
13953 if (CHARPOS (lpoint) <= ZV)
13954 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13955
13956 unbind_to (count, Qnil);
13957 }
13958
13959
13960 /* Build the complete desired matrix of WINDOW with a window start
13961 buffer position POS.
13962
13963 Value is 1 if successful. It is zero if fonts were loaded during
13964 redisplay which makes re-adjusting glyph matrices necessary, and -1
13965 if point would appear in the scroll margins.
13966 (We check that only if CHECK_MARGINS is nonzero. */
13967
13968 int
13969 try_window (window, pos, check_margins)
13970 Lisp_Object window;
13971 struct text_pos pos;
13972 int check_margins;
13973 {
13974 struct window *w = XWINDOW (window);
13975 struct it it;
13976 struct glyph_row *last_text_row = NULL;
13977 struct frame *f = XFRAME (w->frame);
13978
13979 /* Make POS the new window start. */
13980 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13981
13982 /* Mark cursor position as unknown. No overlay arrow seen. */
13983 w->cursor.vpos = -1;
13984 overlay_arrow_seen = 0;
13985
13986 /* Initialize iterator and info to start at POS. */
13987 start_display (&it, w, pos);
13988
13989 /* Display all lines of W. */
13990 while (it.current_y < it.last_visible_y)
13991 {
13992 if (display_line (&it))
13993 last_text_row = it.glyph_row - 1;
13994 if (fonts_changed_p)
13995 return 0;
13996 }
13997
13998 /* Don't let the cursor end in the scroll margins. */
13999 if (check_margins
14000 && !MINI_WINDOW_P (w))
14001 {
14002 int this_scroll_margin;
14003
14004 this_scroll_margin = max (0, scroll_margin);
14005 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14006 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14007
14008 if ((w->cursor.y >= 0 /* not vscrolled */
14009 && w->cursor.y < this_scroll_margin
14010 && CHARPOS (pos) > BEGV
14011 && IT_CHARPOS (it) < ZV)
14012 /* rms: considering make_cursor_line_fully_visible_p here
14013 seems to give wrong results. We don't want to recenter
14014 when the last line is partly visible, we want to allow
14015 that case to be handled in the usual way. */
14016 || (w->cursor.y + 1) > it.last_visible_y)
14017 {
14018 w->cursor.vpos = -1;
14019 clear_glyph_matrix (w->desired_matrix);
14020 return -1;
14021 }
14022 }
14023
14024 /* If bottom moved off end of frame, change mode line percentage. */
14025 if (XFASTINT (w->window_end_pos) <= 0
14026 && Z != IT_CHARPOS (it))
14027 w->update_mode_line = Qt;
14028
14029 /* Set window_end_pos to the offset of the last character displayed
14030 on the window from the end of current_buffer. Set
14031 window_end_vpos to its row number. */
14032 if (last_text_row)
14033 {
14034 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
14035 w->window_end_bytepos
14036 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14037 w->window_end_pos
14038 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14039 w->window_end_vpos
14040 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14041 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
14042 ->displays_text_p);
14043 }
14044 else
14045 {
14046 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14047 w->window_end_pos = make_number (Z - ZV);
14048 w->window_end_vpos = make_number (0);
14049 }
14050
14051 /* But that is not valid info until redisplay finishes. */
14052 w->window_end_valid = Qnil;
14053 return 1;
14054 }
14055
14056
14057 \f
14058 /************************************************************************
14059 Window redisplay reusing current matrix when buffer has not changed
14060 ************************************************************************/
14061
14062 /* Try redisplay of window W showing an unchanged buffer with a
14063 different window start than the last time it was displayed by
14064 reusing its current matrix. Value is non-zero if successful.
14065 W->start is the new window start. */
14066
14067 static int
14068 try_window_reusing_current_matrix (w)
14069 struct window *w;
14070 {
14071 struct frame *f = XFRAME (w->frame);
14072 struct glyph_row *row, *bottom_row;
14073 struct it it;
14074 struct run run;
14075 struct text_pos start, new_start;
14076 int nrows_scrolled, i;
14077 struct glyph_row *last_text_row;
14078 struct glyph_row *last_reused_text_row;
14079 struct glyph_row *start_row;
14080 int start_vpos, min_y, max_y;
14081
14082 #if GLYPH_DEBUG
14083 if (inhibit_try_window_reusing)
14084 return 0;
14085 #endif
14086
14087 if (/* This function doesn't handle terminal frames. */
14088 !FRAME_WINDOW_P (f)
14089 /* Don't try to reuse the display if windows have been split
14090 or such. */
14091 || windows_or_buffers_changed
14092 || cursor_type_changed)
14093 return 0;
14094
14095 /* Can't do this if region may have changed. */
14096 if ((!NILP (Vtransient_mark_mode)
14097 && !NILP (current_buffer->mark_active))
14098 || !NILP (w->region_showing)
14099 || !NILP (Vshow_trailing_whitespace))
14100 return 0;
14101
14102 /* If top-line visibility has changed, give up. */
14103 if (WINDOW_WANTS_HEADER_LINE_P (w)
14104 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
14105 return 0;
14106
14107 /* Give up if old or new display is scrolled vertically. We could
14108 make this function handle this, but right now it doesn't. */
14109 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14110 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
14111 return 0;
14112
14113 /* The variable new_start now holds the new window start. The old
14114 start `start' can be determined from the current matrix. */
14115 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
14116 start = start_row->start.pos;
14117 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14118
14119 /* Clear the desired matrix for the display below. */
14120 clear_glyph_matrix (w->desired_matrix);
14121
14122 if (CHARPOS (new_start) <= CHARPOS (start))
14123 {
14124 int first_row_y;
14125
14126 /* Don't use this method if the display starts with an ellipsis
14127 displayed for invisible text. It's not easy to handle that case
14128 below, and it's certainly not worth the effort since this is
14129 not a frequent case. */
14130 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
14131 return 0;
14132
14133 IF_DEBUG (debug_method_add (w, "twu1"));
14134
14135 /* Display up to a row that can be reused. The variable
14136 last_text_row is set to the last row displayed that displays
14137 text. Note that it.vpos == 0 if or if not there is a
14138 header-line; it's not the same as the MATRIX_ROW_VPOS! */
14139 start_display (&it, w, new_start);
14140 first_row_y = it.current_y;
14141 w->cursor.vpos = -1;
14142 last_text_row = last_reused_text_row = NULL;
14143
14144 while (it.current_y < it.last_visible_y
14145 && !fonts_changed_p)
14146 {
14147 /* If we have reached into the characters in the START row,
14148 that means the line boundaries have changed. So we
14149 can't start copying with the row START. Maybe it will
14150 work to start copying with the following row. */
14151 while (IT_CHARPOS (it) > CHARPOS (start))
14152 {
14153 /* Advance to the next row as the "start". */
14154 start_row++;
14155 start = start_row->start.pos;
14156 /* If there are no more rows to try, or just one, give up. */
14157 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
14158 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
14159 || CHARPOS (start) == ZV)
14160 {
14161 clear_glyph_matrix (w->desired_matrix);
14162 return 0;
14163 }
14164
14165 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
14166 }
14167 /* If we have reached alignment,
14168 we can copy the rest of the rows. */
14169 if (IT_CHARPOS (it) == CHARPOS (start))
14170 break;
14171
14172 if (display_line (&it))
14173 last_text_row = it.glyph_row - 1;
14174 }
14175
14176 /* A value of current_y < last_visible_y means that we stopped
14177 at the previous window start, which in turn means that we
14178 have at least one reusable row. */
14179 if (it.current_y < it.last_visible_y)
14180 {
14181 /* IT.vpos always starts from 0; it counts text lines. */
14182 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
14183
14184 /* Find PT if not already found in the lines displayed. */
14185 if (w->cursor.vpos < 0)
14186 {
14187 int dy = it.current_y - start_row->y;
14188
14189 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14190 row = row_containing_pos (w, PT, row, NULL, dy);
14191 if (row)
14192 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
14193 dy, nrows_scrolled);
14194 else
14195 {
14196 clear_glyph_matrix (w->desired_matrix);
14197 return 0;
14198 }
14199 }
14200
14201 /* Scroll the display. Do it before the current matrix is
14202 changed. The problem here is that update has not yet
14203 run, i.e. part of the current matrix is not up to date.
14204 scroll_run_hook will clear the cursor, and use the
14205 current matrix to get the height of the row the cursor is
14206 in. */
14207 run.current_y = start_row->y;
14208 run.desired_y = it.current_y;
14209 run.height = it.last_visible_y - it.current_y;
14210
14211 if (run.height > 0 && run.current_y != run.desired_y)
14212 {
14213 update_begin (f);
14214 FRAME_RIF (f)->update_window_begin_hook (w);
14215 FRAME_RIF (f)->clear_window_mouse_face (w);
14216 FRAME_RIF (f)->scroll_run_hook (w, &run);
14217 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14218 update_end (f);
14219 }
14220
14221 /* Shift current matrix down by nrows_scrolled lines. */
14222 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14223 rotate_matrix (w->current_matrix,
14224 start_vpos,
14225 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14226 nrows_scrolled);
14227
14228 /* Disable lines that must be updated. */
14229 for (i = 0; i < nrows_scrolled; ++i)
14230 (start_row + i)->enabled_p = 0;
14231
14232 /* Re-compute Y positions. */
14233 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14234 max_y = it.last_visible_y;
14235 for (row = start_row + nrows_scrolled;
14236 row < bottom_row;
14237 ++row)
14238 {
14239 row->y = it.current_y;
14240 row->visible_height = row->height;
14241
14242 if (row->y < min_y)
14243 row->visible_height -= min_y - row->y;
14244 if (row->y + row->height > max_y)
14245 row->visible_height -= row->y + row->height - max_y;
14246 row->redraw_fringe_bitmaps_p = 1;
14247
14248 it.current_y += row->height;
14249
14250 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14251 last_reused_text_row = row;
14252 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
14253 break;
14254 }
14255
14256 /* Disable lines in the current matrix which are now
14257 below the window. */
14258 for (++row; row < bottom_row; ++row)
14259 row->enabled_p = row->mode_line_p = 0;
14260 }
14261
14262 /* Update window_end_pos etc.; last_reused_text_row is the last
14263 reused row from the current matrix containing text, if any.
14264 The value of last_text_row is the last displayed line
14265 containing text. */
14266 if (last_reused_text_row)
14267 {
14268 w->window_end_bytepos
14269 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
14270 w->window_end_pos
14271 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
14272 w->window_end_vpos
14273 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
14274 w->current_matrix));
14275 }
14276 else if (last_text_row)
14277 {
14278 w->window_end_bytepos
14279 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14280 w->window_end_pos
14281 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14282 w->window_end_vpos
14283 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14284 }
14285 else
14286 {
14287 /* This window must be completely empty. */
14288 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
14289 w->window_end_pos = make_number (Z - ZV);
14290 w->window_end_vpos = make_number (0);
14291 }
14292 w->window_end_valid = Qnil;
14293
14294 /* Update hint: don't try scrolling again in update_window. */
14295 w->desired_matrix->no_scrolling_p = 1;
14296
14297 #if GLYPH_DEBUG
14298 debug_method_add (w, "try_window_reusing_current_matrix 1");
14299 #endif
14300 return 1;
14301 }
14302 else if (CHARPOS (new_start) > CHARPOS (start))
14303 {
14304 struct glyph_row *pt_row, *row;
14305 struct glyph_row *first_reusable_row;
14306 struct glyph_row *first_row_to_display;
14307 int dy;
14308 int yb = window_text_bottom_y (w);
14309
14310 /* Find the row starting at new_start, if there is one. Don't
14311 reuse a partially visible line at the end. */
14312 first_reusable_row = start_row;
14313 while (first_reusable_row->enabled_p
14314 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
14315 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14316 < CHARPOS (new_start)))
14317 ++first_reusable_row;
14318
14319 /* Give up if there is no row to reuse. */
14320 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
14321 || !first_reusable_row->enabled_p
14322 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
14323 != CHARPOS (new_start)))
14324 return 0;
14325
14326 /* We can reuse fully visible rows beginning with
14327 first_reusable_row to the end of the window. Set
14328 first_row_to_display to the first row that cannot be reused.
14329 Set pt_row to the row containing point, if there is any. */
14330 pt_row = NULL;
14331 for (first_row_to_display = first_reusable_row;
14332 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
14333 ++first_row_to_display)
14334 {
14335 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
14336 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
14337 pt_row = first_row_to_display;
14338 }
14339
14340 /* Start displaying at the start of first_row_to_display. */
14341 xassert (first_row_to_display->y < yb);
14342 init_to_row_start (&it, w, first_row_to_display);
14343
14344 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
14345 - start_vpos);
14346 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
14347 - nrows_scrolled);
14348 it.current_y = (first_row_to_display->y - first_reusable_row->y
14349 + WINDOW_HEADER_LINE_HEIGHT (w));
14350
14351 /* Display lines beginning with first_row_to_display in the
14352 desired matrix. Set last_text_row to the last row displayed
14353 that displays text. */
14354 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
14355 if (pt_row == NULL)
14356 w->cursor.vpos = -1;
14357 last_text_row = NULL;
14358 while (it.current_y < it.last_visible_y && !fonts_changed_p)
14359 if (display_line (&it))
14360 last_text_row = it.glyph_row - 1;
14361
14362 /* Give up If point isn't in a row displayed or reused. */
14363 if (w->cursor.vpos < 0)
14364 {
14365 clear_glyph_matrix (w->desired_matrix);
14366 return 0;
14367 }
14368
14369 /* If point is in a reused row, adjust y and vpos of the cursor
14370 position. */
14371 if (pt_row)
14372 {
14373 w->cursor.vpos -= nrows_scrolled;
14374 w->cursor.y -= first_reusable_row->y - start_row->y;
14375 }
14376
14377 /* Scroll the display. */
14378 run.current_y = first_reusable_row->y;
14379 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
14380 run.height = it.last_visible_y - run.current_y;
14381 dy = run.current_y - run.desired_y;
14382
14383 if (run.height)
14384 {
14385 update_begin (f);
14386 FRAME_RIF (f)->update_window_begin_hook (w);
14387 FRAME_RIF (f)->clear_window_mouse_face (w);
14388 FRAME_RIF (f)->scroll_run_hook (w, &run);
14389 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
14390 update_end (f);
14391 }
14392
14393 /* Adjust Y positions of reused rows. */
14394 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
14395 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
14396 max_y = it.last_visible_y;
14397 for (row = first_reusable_row; row < first_row_to_display; ++row)
14398 {
14399 row->y -= dy;
14400 row->visible_height = row->height;
14401 if (row->y < min_y)
14402 row->visible_height -= min_y - row->y;
14403 if (row->y + row->height > max_y)
14404 row->visible_height -= row->y + row->height - max_y;
14405 row->redraw_fringe_bitmaps_p = 1;
14406 }
14407
14408 /* Scroll the current matrix. */
14409 xassert (nrows_scrolled > 0);
14410 rotate_matrix (w->current_matrix,
14411 start_vpos,
14412 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
14413 -nrows_scrolled);
14414
14415 /* Disable rows not reused. */
14416 for (row -= nrows_scrolled; row < bottom_row; ++row)
14417 row->enabled_p = 0;
14418
14419 /* Point may have moved to a different line, so we cannot assume that
14420 the previous cursor position is valid; locate the correct row. */
14421 if (pt_row)
14422 {
14423 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14424 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
14425 row++)
14426 {
14427 w->cursor.vpos++;
14428 w->cursor.y = row->y;
14429 }
14430 if (row < bottom_row)
14431 {
14432 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
14433 while (glyph->charpos < PT)
14434 {
14435 w->cursor.hpos++;
14436 w->cursor.x += glyph->pixel_width;
14437 glyph++;
14438 }
14439 }
14440 }
14441
14442 /* Adjust window end. A null value of last_text_row means that
14443 the window end is in reused rows which in turn means that
14444 only its vpos can have changed. */
14445 if (last_text_row)
14446 {
14447 w->window_end_bytepos
14448 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14449 w->window_end_pos
14450 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14451 w->window_end_vpos
14452 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
14453 }
14454 else
14455 {
14456 w->window_end_vpos
14457 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
14458 }
14459
14460 w->window_end_valid = Qnil;
14461 w->desired_matrix->no_scrolling_p = 1;
14462
14463 #if GLYPH_DEBUG
14464 debug_method_add (w, "try_window_reusing_current_matrix 2");
14465 #endif
14466 return 1;
14467 }
14468
14469 return 0;
14470 }
14471
14472
14473 \f
14474 /************************************************************************
14475 Window redisplay reusing current matrix when buffer has changed
14476 ************************************************************************/
14477
14478 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14479 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14480 int *, int *));
14481 static struct glyph_row *
14482 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14483 struct glyph_row *));
14484
14485
14486 /* Return the last row in MATRIX displaying text. If row START is
14487 non-null, start searching with that row. IT gives the dimensions
14488 of the display. Value is null if matrix is empty; otherwise it is
14489 a pointer to the row found. */
14490
14491 static struct glyph_row *
14492 find_last_row_displaying_text (matrix, it, start)
14493 struct glyph_matrix *matrix;
14494 struct it *it;
14495 struct glyph_row *start;
14496 {
14497 struct glyph_row *row, *row_found;
14498
14499 /* Set row_found to the last row in IT->w's current matrix
14500 displaying text. The loop looks funny but think of partially
14501 visible lines. */
14502 row_found = NULL;
14503 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14504 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14505 {
14506 xassert (row->enabled_p);
14507 row_found = row;
14508 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14509 break;
14510 ++row;
14511 }
14512
14513 return row_found;
14514 }
14515
14516
14517 /* Return the last row in the current matrix of W that is not affected
14518 by changes at the start of current_buffer that occurred since W's
14519 current matrix was built. Value is null if no such row exists.
14520
14521 BEG_UNCHANGED us the number of characters unchanged at the start of
14522 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14523 first changed character in current_buffer. Characters at positions <
14524 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14525 when the current matrix was built. */
14526
14527 static struct glyph_row *
14528 find_last_unchanged_at_beg_row (w)
14529 struct window *w;
14530 {
14531 int first_changed_pos = BEG + BEG_UNCHANGED;
14532 struct glyph_row *row;
14533 struct glyph_row *row_found = NULL;
14534 int yb = window_text_bottom_y (w);
14535
14536 /* Find the last row displaying unchanged text. */
14537 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14538 MATRIX_ROW_DISPLAYS_TEXT_P (row)
14539 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
14540 ++row)
14541 {
14542 if (/* If row ends before first_changed_pos, it is unchanged,
14543 except in some case. */
14544 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14545 /* When row ends in ZV and we write at ZV it is not
14546 unchanged. */
14547 && !row->ends_at_zv_p
14548 /* When first_changed_pos is the end of a continued line,
14549 row is not unchanged because it may be no longer
14550 continued. */
14551 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14552 && (row->continued_p
14553 || row->exact_window_width_line_p)))
14554 row_found = row;
14555
14556 /* Stop if last visible row. */
14557 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14558 break;
14559 }
14560
14561 return row_found;
14562 }
14563
14564
14565 /* Find the first glyph row in the current matrix of W that is not
14566 affected by changes at the end of current_buffer since the
14567 time W's current matrix was built.
14568
14569 Return in *DELTA the number of chars by which buffer positions in
14570 unchanged text at the end of current_buffer must be adjusted.
14571
14572 Return in *DELTA_BYTES the corresponding number of bytes.
14573
14574 Value is null if no such row exists, i.e. all rows are affected by
14575 changes. */
14576
14577 static struct glyph_row *
14578 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14579 struct window *w;
14580 int *delta, *delta_bytes;
14581 {
14582 struct glyph_row *row;
14583 struct glyph_row *row_found = NULL;
14584
14585 *delta = *delta_bytes = 0;
14586
14587 /* Display must not have been paused, otherwise the current matrix
14588 is not up to date. */
14589 eassert (!NILP (w->window_end_valid));
14590
14591 /* A value of window_end_pos >= END_UNCHANGED means that the window
14592 end is in the range of changed text. If so, there is no
14593 unchanged row at the end of W's current matrix. */
14594 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14595 return NULL;
14596
14597 /* Set row to the last row in W's current matrix displaying text. */
14598 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14599
14600 /* If matrix is entirely empty, no unchanged row exists. */
14601 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14602 {
14603 /* The value of row is the last glyph row in the matrix having a
14604 meaningful buffer position in it. The end position of row
14605 corresponds to window_end_pos. This allows us to translate
14606 buffer positions in the current matrix to current buffer
14607 positions for characters not in changed text. */
14608 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14609 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14610 int last_unchanged_pos, last_unchanged_pos_old;
14611 struct glyph_row *first_text_row
14612 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14613
14614 *delta = Z - Z_old;
14615 *delta_bytes = Z_BYTE - Z_BYTE_old;
14616
14617 /* Set last_unchanged_pos to the buffer position of the last
14618 character in the buffer that has not been changed. Z is the
14619 index + 1 of the last character in current_buffer, i.e. by
14620 subtracting END_UNCHANGED we get the index of the last
14621 unchanged character, and we have to add BEG to get its buffer
14622 position. */
14623 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14624 last_unchanged_pos_old = last_unchanged_pos - *delta;
14625
14626 /* Search backward from ROW for a row displaying a line that
14627 starts at a minimum position >= last_unchanged_pos_old. */
14628 for (; row > first_text_row; --row)
14629 {
14630 /* This used to abort, but it can happen.
14631 It is ok to just stop the search instead here. KFS. */
14632 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14633 break;
14634
14635 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14636 row_found = row;
14637 }
14638 }
14639
14640 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
14641
14642 return row_found;
14643 }
14644
14645
14646 /* Make sure that glyph rows in the current matrix of window W
14647 reference the same glyph memory as corresponding rows in the
14648 frame's frame matrix. This function is called after scrolling W's
14649 current matrix on a terminal frame in try_window_id and
14650 try_window_reusing_current_matrix. */
14651
14652 static void
14653 sync_frame_with_window_matrix_rows (w)
14654 struct window *w;
14655 {
14656 struct frame *f = XFRAME (w->frame);
14657 struct glyph_row *window_row, *window_row_end, *frame_row;
14658
14659 /* Preconditions: W must be a leaf window and full-width. Its frame
14660 must have a frame matrix. */
14661 xassert (NILP (w->hchild) && NILP (w->vchild));
14662 xassert (WINDOW_FULL_WIDTH_P (w));
14663 xassert (!FRAME_WINDOW_P (f));
14664
14665 /* If W is a full-width window, glyph pointers in W's current matrix
14666 have, by definition, to be the same as glyph pointers in the
14667 corresponding frame matrix. Note that frame matrices have no
14668 marginal areas (see build_frame_matrix). */
14669 window_row = w->current_matrix->rows;
14670 window_row_end = window_row + w->current_matrix->nrows;
14671 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14672 while (window_row < window_row_end)
14673 {
14674 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14675 struct glyph *end = window_row->glyphs[LAST_AREA];
14676
14677 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14678 frame_row->glyphs[TEXT_AREA] = start;
14679 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14680 frame_row->glyphs[LAST_AREA] = end;
14681
14682 /* Disable frame rows whose corresponding window rows have
14683 been disabled in try_window_id. */
14684 if (!window_row->enabled_p)
14685 frame_row->enabled_p = 0;
14686
14687 ++window_row, ++frame_row;
14688 }
14689 }
14690
14691
14692 /* Find the glyph row in window W containing CHARPOS. Consider all
14693 rows between START and END (not inclusive). END null means search
14694 all rows to the end of the display area of W. Value is the row
14695 containing CHARPOS or null. */
14696
14697 struct glyph_row *
14698 row_containing_pos (w, charpos, start, end, dy)
14699 struct window *w;
14700 int charpos;
14701 struct glyph_row *start, *end;
14702 int dy;
14703 {
14704 struct glyph_row *row = start;
14705 int last_y;
14706
14707 /* If we happen to start on a header-line, skip that. */
14708 if (row->mode_line_p)
14709 ++row;
14710
14711 if ((end && row >= end) || !row->enabled_p)
14712 return NULL;
14713
14714 last_y = window_text_bottom_y (w) - dy;
14715
14716 while (1)
14717 {
14718 /* Give up if we have gone too far. */
14719 if (end && row >= end)
14720 return NULL;
14721 /* This formerly returned if they were equal.
14722 I think that both quantities are of a "last plus one" type;
14723 if so, when they are equal, the row is within the screen. -- rms. */
14724 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14725 return NULL;
14726
14727 /* If it is in this row, return this row. */
14728 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14729 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14730 /* The end position of a row equals the start
14731 position of the next row. If CHARPOS is there, we
14732 would rather display it in the next line, except
14733 when this line ends in ZV. */
14734 && !row->ends_at_zv_p
14735 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14736 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14737 return row;
14738 ++row;
14739 }
14740 }
14741
14742
14743 /* Try to redisplay window W by reusing its existing display. W's
14744 current matrix must be up to date when this function is called,
14745 i.e. window_end_valid must not be nil.
14746
14747 Value is
14748
14749 1 if display has been updated
14750 0 if otherwise unsuccessful
14751 -1 if redisplay with same window start is known not to succeed
14752
14753 The following steps are performed:
14754
14755 1. Find the last row in the current matrix of W that is not
14756 affected by changes at the start of current_buffer. If no such row
14757 is found, give up.
14758
14759 2. Find the first row in W's current matrix that is not affected by
14760 changes at the end of current_buffer. Maybe there is no such row.
14761
14762 3. Display lines beginning with the row + 1 found in step 1 to the
14763 row found in step 2 or, if step 2 didn't find a row, to the end of
14764 the window.
14765
14766 4. If cursor is not known to appear on the window, give up.
14767
14768 5. If display stopped at the row found in step 2, scroll the
14769 display and current matrix as needed.
14770
14771 6. Maybe display some lines at the end of W, if we must. This can
14772 happen under various circumstances, like a partially visible line
14773 becoming fully visible, or because newly displayed lines are displayed
14774 in smaller font sizes.
14775
14776 7. Update W's window end information. */
14777
14778 static int
14779 try_window_id (w)
14780 struct window *w;
14781 {
14782 struct frame *f = XFRAME (w->frame);
14783 struct glyph_matrix *current_matrix = w->current_matrix;
14784 struct glyph_matrix *desired_matrix = w->desired_matrix;
14785 struct glyph_row *last_unchanged_at_beg_row;
14786 struct glyph_row *first_unchanged_at_end_row;
14787 struct glyph_row *row;
14788 struct glyph_row *bottom_row;
14789 int bottom_vpos;
14790 struct it it;
14791 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14792 struct text_pos start_pos;
14793 struct run run;
14794 int first_unchanged_at_end_vpos = 0;
14795 struct glyph_row *last_text_row, *last_text_row_at_end;
14796 struct text_pos start;
14797 int first_changed_charpos, last_changed_charpos;
14798
14799 #if GLYPH_DEBUG
14800 if (inhibit_try_window_id)
14801 return 0;
14802 #endif
14803
14804 /* This is handy for debugging. */
14805 #if 0
14806 #define GIVE_UP(X) \
14807 do { \
14808 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14809 return 0; \
14810 } while (0)
14811 #else
14812 #define GIVE_UP(X) return 0
14813 #endif
14814
14815 SET_TEXT_POS_FROM_MARKER (start, w->start);
14816
14817 /* Don't use this for mini-windows because these can show
14818 messages and mini-buffers, and we don't handle that here. */
14819 if (MINI_WINDOW_P (w))
14820 GIVE_UP (1);
14821
14822 /* This flag is used to prevent redisplay optimizations. */
14823 if (windows_or_buffers_changed || cursor_type_changed)
14824 GIVE_UP (2);
14825
14826 /* Verify that narrowing has not changed.
14827 Also verify that we were not told to prevent redisplay optimizations.
14828 It would be nice to further
14829 reduce the number of cases where this prevents try_window_id. */
14830 if (current_buffer->clip_changed
14831 || current_buffer->prevent_redisplay_optimizations_p)
14832 GIVE_UP (3);
14833
14834 /* Window must either use window-based redisplay or be full width. */
14835 if (!FRAME_WINDOW_P (f)
14836 && (!FRAME_LINE_INS_DEL_OK (f)
14837 || !WINDOW_FULL_WIDTH_P (w)))
14838 GIVE_UP (4);
14839
14840 /* Give up if point is not known NOT to appear in W. */
14841 if (PT < CHARPOS (start))
14842 GIVE_UP (5);
14843
14844 /* Another way to prevent redisplay optimizations. */
14845 if (XFASTINT (w->last_modified) == 0)
14846 GIVE_UP (6);
14847
14848 /* Verify that window is not hscrolled. */
14849 if (XFASTINT (w->hscroll) != 0)
14850 GIVE_UP (7);
14851
14852 /* Verify that display wasn't paused. */
14853 if (NILP (w->window_end_valid))
14854 GIVE_UP (8);
14855
14856 /* Can't use this if highlighting a region because a cursor movement
14857 will do more than just set the cursor. */
14858 if (!NILP (Vtransient_mark_mode)
14859 && !NILP (current_buffer->mark_active))
14860 GIVE_UP (9);
14861
14862 /* Likewise if highlighting trailing whitespace. */
14863 if (!NILP (Vshow_trailing_whitespace))
14864 GIVE_UP (11);
14865
14866 /* Likewise if showing a region. */
14867 if (!NILP (w->region_showing))
14868 GIVE_UP (10);
14869
14870 /* Can use this if overlay arrow position and or string have changed. */
14871 if (overlay_arrows_changed_p ())
14872 GIVE_UP (12);
14873
14874 /* When word-wrap is on, adding a space to the first word of a
14875 wrapped line can change the wrap position, altering the line
14876 above it. It might be worthwhile to handle this more
14877 intelligently, but for now just redisplay from scratch. */
14878 if (!NILP (XBUFFER (w->buffer)->word_wrap))
14879 GIVE_UP (21);
14880
14881 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14882 only if buffer has really changed. The reason is that the gap is
14883 initially at Z for freshly visited files. The code below would
14884 set end_unchanged to 0 in that case. */
14885 if (MODIFF > SAVE_MODIFF
14886 /* This seems to happen sometimes after saving a buffer. */
14887 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14888 {
14889 if (GPT - BEG < BEG_UNCHANGED)
14890 BEG_UNCHANGED = GPT - BEG;
14891 if (Z - GPT < END_UNCHANGED)
14892 END_UNCHANGED = Z - GPT;
14893 }
14894
14895 /* The position of the first and last character that has been changed. */
14896 first_changed_charpos = BEG + BEG_UNCHANGED;
14897 last_changed_charpos = Z - END_UNCHANGED;
14898
14899 /* If window starts after a line end, and the last change is in
14900 front of that newline, then changes don't affect the display.
14901 This case happens with stealth-fontification. Note that although
14902 the display is unchanged, glyph positions in the matrix have to
14903 be adjusted, of course. */
14904 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14905 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14906 && ((last_changed_charpos < CHARPOS (start)
14907 && CHARPOS (start) == BEGV)
14908 || (last_changed_charpos < CHARPOS (start) - 1
14909 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14910 {
14911 int Z_old, delta, Z_BYTE_old, delta_bytes;
14912 struct glyph_row *r0;
14913
14914 /* Compute how many chars/bytes have been added to or removed
14915 from the buffer. */
14916 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14917 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14918 delta = Z - Z_old;
14919 delta_bytes = Z_BYTE - Z_BYTE_old;
14920
14921 /* Give up if PT is not in the window. Note that it already has
14922 been checked at the start of try_window_id that PT is not in
14923 front of the window start. */
14924 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14925 GIVE_UP (13);
14926
14927 /* If window start is unchanged, we can reuse the whole matrix
14928 as is, after adjusting glyph positions. No need to compute
14929 the window end again, since its offset from Z hasn't changed. */
14930 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14931 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14932 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14933 /* PT must not be in a partially visible line. */
14934 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14935 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14936 {
14937 /* Adjust positions in the glyph matrix. */
14938 if (delta || delta_bytes)
14939 {
14940 struct glyph_row *r1
14941 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14942 increment_matrix_positions (w->current_matrix,
14943 MATRIX_ROW_VPOS (r0, current_matrix),
14944 MATRIX_ROW_VPOS (r1, current_matrix),
14945 delta, delta_bytes);
14946 }
14947
14948 /* Set the cursor. */
14949 row = row_containing_pos (w, PT, r0, NULL, 0);
14950 if (row)
14951 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14952 else
14953 abort ();
14954 return 1;
14955 }
14956 }
14957
14958 /* Handle the case that changes are all below what is displayed in
14959 the window, and that PT is in the window. This shortcut cannot
14960 be taken if ZV is visible in the window, and text has been added
14961 there that is visible in the window. */
14962 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14963 /* ZV is not visible in the window, or there are no
14964 changes at ZV, actually. */
14965 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14966 || first_changed_charpos == last_changed_charpos))
14967 {
14968 struct glyph_row *r0;
14969
14970 /* Give up if PT is not in the window. Note that it already has
14971 been checked at the start of try_window_id that PT is not in
14972 front of the window start. */
14973 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14974 GIVE_UP (14);
14975
14976 /* If window start is unchanged, we can reuse the whole matrix
14977 as is, without changing glyph positions since no text has
14978 been added/removed in front of the window end. */
14979 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14980 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14981 /* PT must not be in a partially visible line. */
14982 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14983 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14984 {
14985 /* We have to compute the window end anew since text
14986 can have been added/removed after it. */
14987 w->window_end_pos
14988 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14989 w->window_end_bytepos
14990 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14991
14992 /* Set the cursor. */
14993 row = row_containing_pos (w, PT, r0, NULL, 0);
14994 if (row)
14995 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14996 else
14997 abort ();
14998 return 2;
14999 }
15000 }
15001
15002 /* Give up if window start is in the changed area.
15003
15004 The condition used to read
15005
15006 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
15007
15008 but why that was tested escapes me at the moment. */
15009 if (CHARPOS (start) >= first_changed_charpos
15010 && CHARPOS (start) <= last_changed_charpos)
15011 GIVE_UP (15);
15012
15013 /* Check that window start agrees with the start of the first glyph
15014 row in its current matrix. Check this after we know the window
15015 start is not in changed text, otherwise positions would not be
15016 comparable. */
15017 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
15018 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
15019 GIVE_UP (16);
15020
15021 /* Give up if the window ends in strings. Overlay strings
15022 at the end are difficult to handle, so don't try. */
15023 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
15024 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
15025 GIVE_UP (20);
15026
15027 /* Compute the position at which we have to start displaying new
15028 lines. Some of the lines at the top of the window might be
15029 reusable because they are not displaying changed text. Find the
15030 last row in W's current matrix not affected by changes at the
15031 start of current_buffer. Value is null if changes start in the
15032 first line of window. */
15033 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
15034 if (last_unchanged_at_beg_row)
15035 {
15036 /* Avoid starting to display in the moddle of a character, a TAB
15037 for instance. This is easier than to set up the iterator
15038 exactly, and it's not a frequent case, so the additional
15039 effort wouldn't really pay off. */
15040 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
15041 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
15042 && last_unchanged_at_beg_row > w->current_matrix->rows)
15043 --last_unchanged_at_beg_row;
15044
15045 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
15046 GIVE_UP (17);
15047
15048 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
15049 GIVE_UP (18);
15050 start_pos = it.current.pos;
15051
15052 /* Start displaying new lines in the desired matrix at the same
15053 vpos we would use in the current matrix, i.e. below
15054 last_unchanged_at_beg_row. */
15055 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
15056 current_matrix);
15057 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15058 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
15059
15060 xassert (it.hpos == 0 && it.current_x == 0);
15061 }
15062 else
15063 {
15064 /* There are no reusable lines at the start of the window.
15065 Start displaying in the first text line. */
15066 start_display (&it, w, start);
15067 it.vpos = it.first_vpos;
15068 start_pos = it.current.pos;
15069 }
15070
15071 /* Find the first row that is not affected by changes at the end of
15072 the buffer. Value will be null if there is no unchanged row, in
15073 which case we must redisplay to the end of the window. delta
15074 will be set to the value by which buffer positions beginning with
15075 first_unchanged_at_end_row have to be adjusted due to text
15076 changes. */
15077 first_unchanged_at_end_row
15078 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
15079 IF_DEBUG (debug_delta = delta);
15080 IF_DEBUG (debug_delta_bytes = delta_bytes);
15081
15082 /* Set stop_pos to the buffer position up to which we will have to
15083 display new lines. If first_unchanged_at_end_row != NULL, this
15084 is the buffer position of the start of the line displayed in that
15085 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
15086 that we don't stop at a buffer position. */
15087 stop_pos = 0;
15088 if (first_unchanged_at_end_row)
15089 {
15090 xassert (last_unchanged_at_beg_row == NULL
15091 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
15092
15093 /* If this is a continuation line, move forward to the next one
15094 that isn't. Changes in lines above affect this line.
15095 Caution: this may move first_unchanged_at_end_row to a row
15096 not displaying text. */
15097 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
15098 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15099 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15100 < it.last_visible_y))
15101 ++first_unchanged_at_end_row;
15102
15103 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
15104 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
15105 >= it.last_visible_y))
15106 first_unchanged_at_end_row = NULL;
15107 else
15108 {
15109 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
15110 + delta);
15111 first_unchanged_at_end_vpos
15112 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
15113 xassert (stop_pos >= Z - END_UNCHANGED);
15114 }
15115 }
15116 else if (last_unchanged_at_beg_row == NULL)
15117 GIVE_UP (19);
15118
15119
15120 #if GLYPH_DEBUG
15121
15122 /* Either there is no unchanged row at the end, or the one we have
15123 now displays text. This is a necessary condition for the window
15124 end pos calculation at the end of this function. */
15125 xassert (first_unchanged_at_end_row == NULL
15126 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
15127
15128 debug_last_unchanged_at_beg_vpos
15129 = (last_unchanged_at_beg_row
15130 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
15131 : -1);
15132 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
15133
15134 #endif /* GLYPH_DEBUG != 0 */
15135
15136
15137 /* Display new lines. Set last_text_row to the last new line
15138 displayed which has text on it, i.e. might end up as being the
15139 line where the window_end_vpos is. */
15140 w->cursor.vpos = -1;
15141 last_text_row = NULL;
15142 overlay_arrow_seen = 0;
15143 while (it.current_y < it.last_visible_y
15144 && !fonts_changed_p
15145 && (first_unchanged_at_end_row == NULL
15146 || IT_CHARPOS (it) < stop_pos))
15147 {
15148 if (display_line (&it))
15149 last_text_row = it.glyph_row - 1;
15150 }
15151
15152 if (fonts_changed_p)
15153 return -1;
15154
15155
15156 /* Compute differences in buffer positions, y-positions etc. for
15157 lines reused at the bottom of the window. Compute what we can
15158 scroll. */
15159 if (first_unchanged_at_end_row
15160 /* No lines reused because we displayed everything up to the
15161 bottom of the window. */
15162 && it.current_y < it.last_visible_y)
15163 {
15164 dvpos = (it.vpos
15165 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
15166 current_matrix));
15167 dy = it.current_y - first_unchanged_at_end_row->y;
15168 run.current_y = first_unchanged_at_end_row->y;
15169 run.desired_y = run.current_y + dy;
15170 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
15171 }
15172 else
15173 {
15174 delta = delta_bytes = dvpos = dy
15175 = run.current_y = run.desired_y = run.height = 0;
15176 first_unchanged_at_end_row = NULL;
15177 }
15178 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
15179
15180
15181 /* Find the cursor if not already found. We have to decide whether
15182 PT will appear on this window (it sometimes doesn't, but this is
15183 not a very frequent case.) This decision has to be made before
15184 the current matrix is altered. A value of cursor.vpos < 0 means
15185 that PT is either in one of the lines beginning at
15186 first_unchanged_at_end_row or below the window. Don't care for
15187 lines that might be displayed later at the window end; as
15188 mentioned, this is not a frequent case. */
15189 if (w->cursor.vpos < 0)
15190 {
15191 /* Cursor in unchanged rows at the top? */
15192 if (PT < CHARPOS (start_pos)
15193 && last_unchanged_at_beg_row)
15194 {
15195 row = row_containing_pos (w, PT,
15196 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
15197 last_unchanged_at_beg_row + 1, 0);
15198 if (row)
15199 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15200 }
15201
15202 /* Start from first_unchanged_at_end_row looking for PT. */
15203 else if (first_unchanged_at_end_row)
15204 {
15205 row = row_containing_pos (w, PT - delta,
15206 first_unchanged_at_end_row, NULL, 0);
15207 if (row)
15208 set_cursor_from_row (w, row, w->current_matrix, delta,
15209 delta_bytes, dy, dvpos);
15210 }
15211
15212 /* Give up if cursor was not found. */
15213 if (w->cursor.vpos < 0)
15214 {
15215 clear_glyph_matrix (w->desired_matrix);
15216 return -1;
15217 }
15218 }
15219
15220 /* Don't let the cursor end in the scroll margins. */
15221 {
15222 int this_scroll_margin, cursor_height;
15223
15224 this_scroll_margin = max (0, scroll_margin);
15225 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15226 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
15227 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
15228
15229 if ((w->cursor.y < this_scroll_margin
15230 && CHARPOS (start) > BEGV)
15231 /* Old redisplay didn't take scroll margin into account at the bottom,
15232 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
15233 || (w->cursor.y + (make_cursor_line_fully_visible_p
15234 ? cursor_height + this_scroll_margin
15235 : 1)) > it.last_visible_y)
15236 {
15237 w->cursor.vpos = -1;
15238 clear_glyph_matrix (w->desired_matrix);
15239 return -1;
15240 }
15241 }
15242
15243 /* Scroll the display. Do it before changing the current matrix so
15244 that xterm.c doesn't get confused about where the cursor glyph is
15245 found. */
15246 if (dy && run.height)
15247 {
15248 update_begin (f);
15249
15250 if (FRAME_WINDOW_P (f))
15251 {
15252 FRAME_RIF (f)->update_window_begin_hook (w);
15253 FRAME_RIF (f)->clear_window_mouse_face (w);
15254 FRAME_RIF (f)->scroll_run_hook (w, &run);
15255 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15256 }
15257 else
15258 {
15259 /* Terminal frame. In this case, dvpos gives the number of
15260 lines to scroll by; dvpos < 0 means scroll up. */
15261 int first_unchanged_at_end_vpos
15262 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
15263 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
15264 int end = (WINDOW_TOP_EDGE_LINE (w)
15265 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
15266 + window_internal_height (w));
15267
15268 /* Perform the operation on the screen. */
15269 if (dvpos > 0)
15270 {
15271 /* Scroll last_unchanged_at_beg_row to the end of the
15272 window down dvpos lines. */
15273 set_terminal_window (f, end);
15274
15275 /* On dumb terminals delete dvpos lines at the end
15276 before inserting dvpos empty lines. */
15277 if (!FRAME_SCROLL_REGION_OK (f))
15278 ins_del_lines (f, end - dvpos, -dvpos);
15279
15280 /* Insert dvpos empty lines in front of
15281 last_unchanged_at_beg_row. */
15282 ins_del_lines (f, from, dvpos);
15283 }
15284 else if (dvpos < 0)
15285 {
15286 /* Scroll up last_unchanged_at_beg_vpos to the end of
15287 the window to last_unchanged_at_beg_vpos - |dvpos|. */
15288 set_terminal_window (f, end);
15289
15290 /* Delete dvpos lines in front of
15291 last_unchanged_at_beg_vpos. ins_del_lines will set
15292 the cursor to the given vpos and emit |dvpos| delete
15293 line sequences. */
15294 ins_del_lines (f, from + dvpos, dvpos);
15295
15296 /* On a dumb terminal insert dvpos empty lines at the
15297 end. */
15298 if (!FRAME_SCROLL_REGION_OK (f))
15299 ins_del_lines (f, end + dvpos, -dvpos);
15300 }
15301
15302 set_terminal_window (f, 0);
15303 }
15304
15305 update_end (f);
15306 }
15307
15308 /* Shift reused rows of the current matrix to the right position.
15309 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
15310 text. */
15311 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
15312 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
15313 if (dvpos < 0)
15314 {
15315 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
15316 bottom_vpos, dvpos);
15317 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
15318 bottom_vpos, 0);
15319 }
15320 else if (dvpos > 0)
15321 {
15322 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
15323 bottom_vpos, dvpos);
15324 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
15325 first_unchanged_at_end_vpos + dvpos, 0);
15326 }
15327
15328 /* For frame-based redisplay, make sure that current frame and window
15329 matrix are in sync with respect to glyph memory. */
15330 if (!FRAME_WINDOW_P (f))
15331 sync_frame_with_window_matrix_rows (w);
15332
15333 /* Adjust buffer positions in reused rows. */
15334 if (delta || delta_bytes)
15335 increment_matrix_positions (current_matrix,
15336 first_unchanged_at_end_vpos + dvpos,
15337 bottom_vpos, delta, delta_bytes);
15338
15339 /* Adjust Y positions. */
15340 if (dy)
15341 shift_glyph_matrix (w, current_matrix,
15342 first_unchanged_at_end_vpos + dvpos,
15343 bottom_vpos, dy);
15344
15345 if (first_unchanged_at_end_row)
15346 {
15347 first_unchanged_at_end_row += dvpos;
15348 if (first_unchanged_at_end_row->y >= it.last_visible_y
15349 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
15350 first_unchanged_at_end_row = NULL;
15351 }
15352
15353 /* If scrolling up, there may be some lines to display at the end of
15354 the window. */
15355 last_text_row_at_end = NULL;
15356 if (dy < 0)
15357 {
15358 /* Scrolling up can leave for example a partially visible line
15359 at the end of the window to be redisplayed. */
15360 /* Set last_row to the glyph row in the current matrix where the
15361 window end line is found. It has been moved up or down in
15362 the matrix by dvpos. */
15363 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
15364 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
15365
15366 /* If last_row is the window end line, it should display text. */
15367 xassert (last_row->displays_text_p);
15368
15369 /* If window end line was partially visible before, begin
15370 displaying at that line. Otherwise begin displaying with the
15371 line following it. */
15372 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
15373 {
15374 init_to_row_start (&it, w, last_row);
15375 it.vpos = last_vpos;
15376 it.current_y = last_row->y;
15377 }
15378 else
15379 {
15380 init_to_row_end (&it, w, last_row);
15381 it.vpos = 1 + last_vpos;
15382 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
15383 ++last_row;
15384 }
15385
15386 /* We may start in a continuation line. If so, we have to
15387 get the right continuation_lines_width and current_x. */
15388 it.continuation_lines_width = last_row->continuation_lines_width;
15389 it.hpos = it.current_x = 0;
15390
15391 /* Display the rest of the lines at the window end. */
15392 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
15393 while (it.current_y < it.last_visible_y
15394 && !fonts_changed_p)
15395 {
15396 /* Is it always sure that the display agrees with lines in
15397 the current matrix? I don't think so, so we mark rows
15398 displayed invalid in the current matrix by setting their
15399 enabled_p flag to zero. */
15400 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
15401 if (display_line (&it))
15402 last_text_row_at_end = it.glyph_row - 1;
15403 }
15404 }
15405
15406 /* Update window_end_pos and window_end_vpos. */
15407 if (first_unchanged_at_end_row
15408 && !last_text_row_at_end)
15409 {
15410 /* Window end line if one of the preserved rows from the current
15411 matrix. Set row to the last row displaying text in current
15412 matrix starting at first_unchanged_at_end_row, after
15413 scrolling. */
15414 xassert (first_unchanged_at_end_row->displays_text_p);
15415 row = find_last_row_displaying_text (w->current_matrix, &it,
15416 first_unchanged_at_end_row);
15417 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
15418
15419 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15420 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15421 w->window_end_vpos
15422 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
15423 xassert (w->window_end_bytepos >= 0);
15424 IF_DEBUG (debug_method_add (w, "A"));
15425 }
15426 else if (last_text_row_at_end)
15427 {
15428 w->window_end_pos
15429 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
15430 w->window_end_bytepos
15431 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
15432 w->window_end_vpos
15433 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
15434 xassert (w->window_end_bytepos >= 0);
15435 IF_DEBUG (debug_method_add (w, "B"));
15436 }
15437 else if (last_text_row)
15438 {
15439 /* We have displayed either to the end of the window or at the
15440 end of the window, i.e. the last row with text is to be found
15441 in the desired matrix. */
15442 w->window_end_pos
15443 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15444 w->window_end_bytepos
15445 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15446 w->window_end_vpos
15447 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
15448 xassert (w->window_end_bytepos >= 0);
15449 }
15450 else if (first_unchanged_at_end_row == NULL
15451 && last_text_row == NULL
15452 && last_text_row_at_end == NULL)
15453 {
15454 /* Displayed to end of window, but no line containing text was
15455 displayed. Lines were deleted at the end of the window. */
15456 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
15457 int vpos = XFASTINT (w->window_end_vpos);
15458 struct glyph_row *current_row = current_matrix->rows + vpos;
15459 struct glyph_row *desired_row = desired_matrix->rows + vpos;
15460
15461 for (row = NULL;
15462 row == NULL && vpos >= first_vpos;
15463 --vpos, --current_row, --desired_row)
15464 {
15465 if (desired_row->enabled_p)
15466 {
15467 if (desired_row->displays_text_p)
15468 row = desired_row;
15469 }
15470 else if (current_row->displays_text_p)
15471 row = current_row;
15472 }
15473
15474 xassert (row != NULL);
15475 w->window_end_vpos = make_number (vpos + 1);
15476 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15477 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15478 xassert (w->window_end_bytepos >= 0);
15479 IF_DEBUG (debug_method_add (w, "C"));
15480 }
15481 else
15482 abort ();
15483
15484 #if 0 /* This leads to problems, for instance when the cursor is
15485 at ZV, and the cursor line displays no text. */
15486 /* Disable rows below what's displayed in the window. This makes
15487 debugging easier. */
15488 enable_glyph_matrix_rows (current_matrix,
15489 XFASTINT (w->window_end_vpos) + 1,
15490 bottom_vpos, 0);
15491 #endif
15492
15493 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15494 debug_end_vpos = XFASTINT (w->window_end_vpos));
15495
15496 /* Record that display has not been completed. */
15497 w->window_end_valid = Qnil;
15498 w->desired_matrix->no_scrolling_p = 1;
15499 return 3;
15500
15501 #undef GIVE_UP
15502 }
15503
15504
15505 \f
15506 /***********************************************************************
15507 More debugging support
15508 ***********************************************************************/
15509
15510 #if GLYPH_DEBUG
15511
15512 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15513 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15514 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15515
15516
15517 /* Dump the contents of glyph matrix MATRIX on stderr.
15518
15519 GLYPHS 0 means don't show glyph contents.
15520 GLYPHS 1 means show glyphs in short form
15521 GLYPHS > 1 means show glyphs in long form. */
15522
15523 void
15524 dump_glyph_matrix (matrix, glyphs)
15525 struct glyph_matrix *matrix;
15526 int glyphs;
15527 {
15528 int i;
15529 for (i = 0; i < matrix->nrows; ++i)
15530 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15531 }
15532
15533
15534 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15535 the glyph row and area where the glyph comes from. */
15536
15537 void
15538 dump_glyph (row, glyph, area)
15539 struct glyph_row *row;
15540 struct glyph *glyph;
15541 int area;
15542 {
15543 if (glyph->type == CHAR_GLYPH)
15544 {
15545 fprintf (stderr,
15546 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15547 glyph - row->glyphs[TEXT_AREA],
15548 'C',
15549 glyph->charpos,
15550 (BUFFERP (glyph->object)
15551 ? 'B'
15552 : (STRINGP (glyph->object)
15553 ? 'S'
15554 : '-')),
15555 glyph->pixel_width,
15556 glyph->u.ch,
15557 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15558 ? glyph->u.ch
15559 : '.'),
15560 glyph->face_id,
15561 glyph->left_box_line_p,
15562 glyph->right_box_line_p);
15563 }
15564 else if (glyph->type == STRETCH_GLYPH)
15565 {
15566 fprintf (stderr,
15567 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15568 glyph - row->glyphs[TEXT_AREA],
15569 'S',
15570 glyph->charpos,
15571 (BUFFERP (glyph->object)
15572 ? 'B'
15573 : (STRINGP (glyph->object)
15574 ? 'S'
15575 : '-')),
15576 glyph->pixel_width,
15577 0,
15578 '.',
15579 glyph->face_id,
15580 glyph->left_box_line_p,
15581 glyph->right_box_line_p);
15582 }
15583 else if (glyph->type == IMAGE_GLYPH)
15584 {
15585 fprintf (stderr,
15586 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15587 glyph - row->glyphs[TEXT_AREA],
15588 'I',
15589 glyph->charpos,
15590 (BUFFERP (glyph->object)
15591 ? 'B'
15592 : (STRINGP (glyph->object)
15593 ? 'S'
15594 : '-')),
15595 glyph->pixel_width,
15596 glyph->u.img_id,
15597 '.',
15598 glyph->face_id,
15599 glyph->left_box_line_p,
15600 glyph->right_box_line_p);
15601 }
15602 else if (glyph->type == COMPOSITE_GLYPH)
15603 {
15604 fprintf (stderr,
15605 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15606 glyph - row->glyphs[TEXT_AREA],
15607 '+',
15608 glyph->charpos,
15609 (BUFFERP (glyph->object)
15610 ? 'B'
15611 : (STRINGP (glyph->object)
15612 ? 'S'
15613 : '-')),
15614 glyph->pixel_width,
15615 glyph->u.cmp_id,
15616 '.',
15617 glyph->face_id,
15618 glyph->left_box_line_p,
15619 glyph->right_box_line_p);
15620 }
15621 }
15622
15623
15624 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15625 GLYPHS 0 means don't show glyph contents.
15626 GLYPHS 1 means show glyphs in short form
15627 GLYPHS > 1 means show glyphs in long form. */
15628
15629 void
15630 dump_glyph_row (row, vpos, glyphs)
15631 struct glyph_row *row;
15632 int vpos, glyphs;
15633 {
15634 if (glyphs != 1)
15635 {
15636 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15637 fprintf (stderr, "======================================================================\n");
15638
15639 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15640 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15641 vpos,
15642 MATRIX_ROW_START_CHARPOS (row),
15643 MATRIX_ROW_END_CHARPOS (row),
15644 row->used[TEXT_AREA],
15645 row->contains_overlapping_glyphs_p,
15646 row->enabled_p,
15647 row->truncated_on_left_p,
15648 row->truncated_on_right_p,
15649 row->continued_p,
15650 MATRIX_ROW_CONTINUATION_LINE_P (row),
15651 row->displays_text_p,
15652 row->ends_at_zv_p,
15653 row->fill_line_p,
15654 row->ends_in_middle_of_char_p,
15655 row->starts_in_middle_of_char_p,
15656 row->mouse_face_p,
15657 row->x,
15658 row->y,
15659 row->pixel_width,
15660 row->height,
15661 row->visible_height,
15662 row->ascent,
15663 row->phys_ascent);
15664 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15665 row->end.overlay_string_index,
15666 row->continuation_lines_width);
15667 fprintf (stderr, "%9d %5d\n",
15668 CHARPOS (row->start.string_pos),
15669 CHARPOS (row->end.string_pos));
15670 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15671 row->end.dpvec_index);
15672 }
15673
15674 if (glyphs > 1)
15675 {
15676 int area;
15677
15678 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15679 {
15680 struct glyph *glyph = row->glyphs[area];
15681 struct glyph *glyph_end = glyph + row->used[area];
15682
15683 /* Glyph for a line end in text. */
15684 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15685 ++glyph_end;
15686
15687 if (glyph < glyph_end)
15688 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15689
15690 for (; glyph < glyph_end; ++glyph)
15691 dump_glyph (row, glyph, area);
15692 }
15693 }
15694 else if (glyphs == 1)
15695 {
15696 int area;
15697
15698 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15699 {
15700 char *s = (char *) alloca (row->used[area] + 1);
15701 int i;
15702
15703 for (i = 0; i < row->used[area]; ++i)
15704 {
15705 struct glyph *glyph = row->glyphs[area] + i;
15706 if (glyph->type == CHAR_GLYPH
15707 && glyph->u.ch < 0x80
15708 && glyph->u.ch >= ' ')
15709 s[i] = glyph->u.ch;
15710 else
15711 s[i] = '.';
15712 }
15713
15714 s[i] = '\0';
15715 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15716 }
15717 }
15718 }
15719
15720
15721 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15722 Sdump_glyph_matrix, 0, 1, "p",
15723 doc: /* Dump the current matrix of the selected window to stderr.
15724 Shows contents of glyph row structures. With non-nil
15725 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15726 glyphs in short form, otherwise show glyphs in long form. */)
15727 (glyphs)
15728 Lisp_Object glyphs;
15729 {
15730 struct window *w = XWINDOW (selected_window);
15731 struct buffer *buffer = XBUFFER (w->buffer);
15732
15733 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15734 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15735 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15736 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15737 fprintf (stderr, "=============================================\n");
15738 dump_glyph_matrix (w->current_matrix,
15739 NILP (glyphs) ? 0 : XINT (glyphs));
15740 return Qnil;
15741 }
15742
15743
15744 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15745 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15746 ()
15747 {
15748 struct frame *f = XFRAME (selected_frame);
15749 dump_glyph_matrix (f->current_matrix, 1);
15750 return Qnil;
15751 }
15752
15753
15754 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15755 doc: /* Dump glyph row ROW to stderr.
15756 GLYPH 0 means don't dump glyphs.
15757 GLYPH 1 means dump glyphs in short form.
15758 GLYPH > 1 or omitted means dump glyphs in long form. */)
15759 (row, glyphs)
15760 Lisp_Object row, glyphs;
15761 {
15762 struct glyph_matrix *matrix;
15763 int vpos;
15764
15765 CHECK_NUMBER (row);
15766 matrix = XWINDOW (selected_window)->current_matrix;
15767 vpos = XINT (row);
15768 if (vpos >= 0 && vpos < matrix->nrows)
15769 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15770 vpos,
15771 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15772 return Qnil;
15773 }
15774
15775
15776 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15777 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15778 GLYPH 0 means don't dump glyphs.
15779 GLYPH 1 means dump glyphs in short form.
15780 GLYPH > 1 or omitted means dump glyphs in long form. */)
15781 (row, glyphs)
15782 Lisp_Object row, glyphs;
15783 {
15784 struct frame *sf = SELECTED_FRAME ();
15785 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15786 int vpos;
15787
15788 CHECK_NUMBER (row);
15789 vpos = XINT (row);
15790 if (vpos >= 0 && vpos < m->nrows)
15791 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15792 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15793 return Qnil;
15794 }
15795
15796
15797 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15798 doc: /* Toggle tracing of redisplay.
15799 With ARG, turn tracing on if and only if ARG is positive. */)
15800 (arg)
15801 Lisp_Object arg;
15802 {
15803 if (NILP (arg))
15804 trace_redisplay_p = !trace_redisplay_p;
15805 else
15806 {
15807 arg = Fprefix_numeric_value (arg);
15808 trace_redisplay_p = XINT (arg) > 0;
15809 }
15810
15811 return Qnil;
15812 }
15813
15814
15815 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15816 doc: /* Like `format', but print result to stderr.
15817 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15818 (nargs, args)
15819 int nargs;
15820 Lisp_Object *args;
15821 {
15822 Lisp_Object s = Fformat (nargs, args);
15823 fprintf (stderr, "%s", SDATA (s));
15824 return Qnil;
15825 }
15826
15827 #endif /* GLYPH_DEBUG */
15828
15829
15830 \f
15831 /***********************************************************************
15832 Building Desired Matrix Rows
15833 ***********************************************************************/
15834
15835 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15836 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15837
15838 static struct glyph_row *
15839 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15840 struct window *w;
15841 Lisp_Object overlay_arrow_string;
15842 {
15843 struct frame *f = XFRAME (WINDOW_FRAME (w));
15844 struct buffer *buffer = XBUFFER (w->buffer);
15845 struct buffer *old = current_buffer;
15846 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15847 int arrow_len = SCHARS (overlay_arrow_string);
15848 const unsigned char *arrow_end = arrow_string + arrow_len;
15849 const unsigned char *p;
15850 struct it it;
15851 int multibyte_p;
15852 int n_glyphs_before;
15853
15854 set_buffer_temp (buffer);
15855 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15856 it.glyph_row->used[TEXT_AREA] = 0;
15857 SET_TEXT_POS (it.position, 0, 0);
15858
15859 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15860 p = arrow_string;
15861 while (p < arrow_end)
15862 {
15863 Lisp_Object face, ilisp;
15864
15865 /* Get the next character. */
15866 if (multibyte_p)
15867 it.c = string_char_and_length (p, arrow_len, &it.len);
15868 else
15869 it.c = *p, it.len = 1;
15870 p += it.len;
15871
15872 /* Get its face. */
15873 ilisp = make_number (p - arrow_string);
15874 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15875 it.face_id = compute_char_face (f, it.c, face);
15876
15877 /* Compute its width, get its glyphs. */
15878 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15879 SET_TEXT_POS (it.position, -1, -1);
15880 PRODUCE_GLYPHS (&it);
15881
15882 /* If this character doesn't fit any more in the line, we have
15883 to remove some glyphs. */
15884 if (it.current_x > it.last_visible_x)
15885 {
15886 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15887 break;
15888 }
15889 }
15890
15891 set_buffer_temp (old);
15892 return it.glyph_row;
15893 }
15894
15895
15896 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15897 glyphs are only inserted for terminal frames since we can't really
15898 win with truncation glyphs when partially visible glyphs are
15899 involved. Which glyphs to insert is determined by
15900 produce_special_glyphs. */
15901
15902 static void
15903 insert_left_trunc_glyphs (it)
15904 struct it *it;
15905 {
15906 struct it truncate_it;
15907 struct glyph *from, *end, *to, *toend;
15908
15909 xassert (!FRAME_WINDOW_P (it->f));
15910
15911 /* Get the truncation glyphs. */
15912 truncate_it = *it;
15913 truncate_it.current_x = 0;
15914 truncate_it.face_id = DEFAULT_FACE_ID;
15915 truncate_it.glyph_row = &scratch_glyph_row;
15916 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15917 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15918 truncate_it.object = make_number (0);
15919 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15920
15921 /* Overwrite glyphs from IT with truncation glyphs. */
15922 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15923 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15924 to = it->glyph_row->glyphs[TEXT_AREA];
15925 toend = to + it->glyph_row->used[TEXT_AREA];
15926
15927 while (from < end)
15928 *to++ = *from++;
15929
15930 /* There may be padding glyphs left over. Overwrite them too. */
15931 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15932 {
15933 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15934 while (from < end)
15935 *to++ = *from++;
15936 }
15937
15938 if (to > toend)
15939 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15940 }
15941
15942
15943 /* Compute the pixel height and width of IT->glyph_row.
15944
15945 Most of the time, ascent and height of a display line will be equal
15946 to the max_ascent and max_height values of the display iterator
15947 structure. This is not the case if
15948
15949 1. We hit ZV without displaying anything. In this case, max_ascent
15950 and max_height will be zero.
15951
15952 2. We have some glyphs that don't contribute to the line height.
15953 (The glyph row flag contributes_to_line_height_p is for future
15954 pixmap extensions).
15955
15956 The first case is easily covered by using default values because in
15957 these cases, the line height does not really matter, except that it
15958 must not be zero. */
15959
15960 static void
15961 compute_line_metrics (it)
15962 struct it *it;
15963 {
15964 struct glyph_row *row = it->glyph_row;
15965 int area, i;
15966
15967 if (FRAME_WINDOW_P (it->f))
15968 {
15969 int i, min_y, max_y;
15970
15971 /* The line may consist of one space only, that was added to
15972 place the cursor on it. If so, the row's height hasn't been
15973 computed yet. */
15974 if (row->height == 0)
15975 {
15976 if (it->max_ascent + it->max_descent == 0)
15977 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15978 row->ascent = it->max_ascent;
15979 row->height = it->max_ascent + it->max_descent;
15980 row->phys_ascent = it->max_phys_ascent;
15981 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15982 row->extra_line_spacing = it->max_extra_line_spacing;
15983 }
15984
15985 /* Compute the width of this line. */
15986 row->pixel_width = row->x;
15987 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15988 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15989
15990 xassert (row->pixel_width >= 0);
15991 xassert (row->ascent >= 0 && row->height > 0);
15992
15993 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15994 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15995
15996 /* If first line's physical ascent is larger than its logical
15997 ascent, use the physical ascent, and make the row taller.
15998 This makes accented characters fully visible. */
15999 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
16000 && row->phys_ascent > row->ascent)
16001 {
16002 row->height += row->phys_ascent - row->ascent;
16003 row->ascent = row->phys_ascent;
16004 }
16005
16006 /* Compute how much of the line is visible. */
16007 row->visible_height = row->height;
16008
16009 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
16010 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
16011
16012 if (row->y < min_y)
16013 row->visible_height -= min_y - row->y;
16014 if (row->y + row->height > max_y)
16015 row->visible_height -= row->y + row->height - max_y;
16016 }
16017 else
16018 {
16019 row->pixel_width = row->used[TEXT_AREA];
16020 if (row->continued_p)
16021 row->pixel_width -= it->continuation_pixel_width;
16022 else if (row->truncated_on_right_p)
16023 row->pixel_width -= it->truncation_pixel_width;
16024 row->ascent = row->phys_ascent = 0;
16025 row->height = row->phys_height = row->visible_height = 1;
16026 row->extra_line_spacing = 0;
16027 }
16028
16029 /* Compute a hash code for this row. */
16030 row->hash = 0;
16031 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
16032 for (i = 0; i < row->used[area]; ++i)
16033 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
16034 + row->glyphs[area][i].u.val
16035 + row->glyphs[area][i].face_id
16036 + row->glyphs[area][i].padding_p
16037 + (row->glyphs[area][i].type << 2));
16038
16039 it->max_ascent = it->max_descent = 0;
16040 it->max_phys_ascent = it->max_phys_descent = 0;
16041 }
16042
16043
16044 /* Append one space to the glyph row of iterator IT if doing a
16045 window-based redisplay. The space has the same face as
16046 IT->face_id. Value is non-zero if a space was added.
16047
16048 This function is called to make sure that there is always one glyph
16049 at the end of a glyph row that the cursor can be set on under
16050 window-systems. (If there weren't such a glyph we would not know
16051 how wide and tall a box cursor should be displayed).
16052
16053 At the same time this space let's a nicely handle clearing to the
16054 end of the line if the row ends in italic text. */
16055
16056 static int
16057 append_space_for_newline (it, default_face_p)
16058 struct it *it;
16059 int default_face_p;
16060 {
16061 if (FRAME_WINDOW_P (it->f))
16062 {
16063 int n = it->glyph_row->used[TEXT_AREA];
16064
16065 if (it->glyph_row->glyphs[TEXT_AREA] + n
16066 < it->glyph_row->glyphs[1 + TEXT_AREA])
16067 {
16068 /* Save some values that must not be changed.
16069 Must save IT->c and IT->len because otherwise
16070 ITERATOR_AT_END_P wouldn't work anymore after
16071 append_space_for_newline has been called. */
16072 enum display_element_type saved_what = it->what;
16073 int saved_c = it->c, saved_len = it->len;
16074 int saved_x = it->current_x;
16075 int saved_face_id = it->face_id;
16076 struct text_pos saved_pos;
16077 Lisp_Object saved_object;
16078 struct face *face;
16079
16080 saved_object = it->object;
16081 saved_pos = it->position;
16082
16083 it->what = IT_CHARACTER;
16084 bzero (&it->position, sizeof it->position);
16085 it->object = make_number (0);
16086 it->c = ' ';
16087 it->len = 1;
16088
16089 if (default_face_p)
16090 it->face_id = DEFAULT_FACE_ID;
16091 else if (it->face_before_selective_p)
16092 it->face_id = it->saved_face_id;
16093 face = FACE_FROM_ID (it->f, it->face_id);
16094 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
16095
16096 PRODUCE_GLYPHS (it);
16097
16098 it->override_ascent = -1;
16099 it->constrain_row_ascent_descent_p = 0;
16100 it->current_x = saved_x;
16101 it->object = saved_object;
16102 it->position = saved_pos;
16103 it->what = saved_what;
16104 it->face_id = saved_face_id;
16105 it->len = saved_len;
16106 it->c = saved_c;
16107 return 1;
16108 }
16109 }
16110
16111 return 0;
16112 }
16113
16114
16115 /* Extend the face of the last glyph in the text area of IT->glyph_row
16116 to the end of the display line. Called from display_line.
16117 If the glyph row is empty, add a space glyph to it so that we
16118 know the face to draw. Set the glyph row flag fill_line_p. */
16119
16120 static void
16121 extend_face_to_end_of_line (it)
16122 struct it *it;
16123 {
16124 struct face *face;
16125 struct frame *f = it->f;
16126
16127 /* If line is already filled, do nothing. */
16128 if (it->current_x >= it->last_visible_x)
16129 return;
16130
16131 /* Face extension extends the background and box of IT->face_id
16132 to the end of the line. If the background equals the background
16133 of the frame, we don't have to do anything. */
16134 if (it->face_before_selective_p)
16135 face = FACE_FROM_ID (it->f, it->saved_face_id);
16136 else
16137 face = FACE_FROM_ID (f, it->face_id);
16138
16139 if (FRAME_WINDOW_P (f)
16140 && it->glyph_row->displays_text_p
16141 && face->box == FACE_NO_BOX
16142 && face->background == FRAME_BACKGROUND_PIXEL (f)
16143 && !face->stipple)
16144 return;
16145
16146 /* Set the glyph row flag indicating that the face of the last glyph
16147 in the text area has to be drawn to the end of the text area. */
16148 it->glyph_row->fill_line_p = 1;
16149
16150 /* If current character of IT is not ASCII, make sure we have the
16151 ASCII face. This will be automatically undone the next time
16152 get_next_display_element returns a multibyte character. Note
16153 that the character will always be single byte in unibyte text. */
16154 if (!ASCII_CHAR_P (it->c))
16155 {
16156 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
16157 }
16158
16159 if (FRAME_WINDOW_P (f))
16160 {
16161 /* If the row is empty, add a space with the current face of IT,
16162 so that we know which face to draw. */
16163 if (it->glyph_row->used[TEXT_AREA] == 0)
16164 {
16165 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
16166 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
16167 it->glyph_row->used[TEXT_AREA] = 1;
16168 }
16169 }
16170 else
16171 {
16172 /* Save some values that must not be changed. */
16173 int saved_x = it->current_x;
16174 struct text_pos saved_pos;
16175 Lisp_Object saved_object;
16176 enum display_element_type saved_what = it->what;
16177 int saved_face_id = it->face_id;
16178
16179 saved_object = it->object;
16180 saved_pos = it->position;
16181
16182 it->what = IT_CHARACTER;
16183 bzero (&it->position, sizeof it->position);
16184 it->object = make_number (0);
16185 it->c = ' ';
16186 it->len = 1;
16187 it->face_id = face->id;
16188
16189 PRODUCE_GLYPHS (it);
16190
16191 while (it->current_x <= it->last_visible_x)
16192 PRODUCE_GLYPHS (it);
16193
16194 /* Don't count these blanks really. It would let us insert a left
16195 truncation glyph below and make us set the cursor on them, maybe. */
16196 it->current_x = saved_x;
16197 it->object = saved_object;
16198 it->position = saved_pos;
16199 it->what = saved_what;
16200 it->face_id = saved_face_id;
16201 }
16202 }
16203
16204
16205 /* Value is non-zero if text starting at CHARPOS in current_buffer is
16206 trailing whitespace. */
16207
16208 static int
16209 trailing_whitespace_p (charpos)
16210 int charpos;
16211 {
16212 int bytepos = CHAR_TO_BYTE (charpos);
16213 int c = 0;
16214
16215 while (bytepos < ZV_BYTE
16216 && (c = FETCH_CHAR (bytepos),
16217 c == ' ' || c == '\t'))
16218 ++bytepos;
16219
16220 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
16221 {
16222 if (bytepos != PT_BYTE)
16223 return 1;
16224 }
16225 return 0;
16226 }
16227
16228
16229 /* Highlight trailing whitespace, if any, in ROW. */
16230
16231 void
16232 highlight_trailing_whitespace (f, row)
16233 struct frame *f;
16234 struct glyph_row *row;
16235 {
16236 int used = row->used[TEXT_AREA];
16237
16238 if (used)
16239 {
16240 struct glyph *start = row->glyphs[TEXT_AREA];
16241 struct glyph *glyph = start + used - 1;
16242
16243 /* Skip over glyphs inserted to display the cursor at the
16244 end of a line, for extending the face of the last glyph
16245 to the end of the line on terminals, and for truncation
16246 and continuation glyphs. */
16247 while (glyph >= start
16248 && glyph->type == CHAR_GLYPH
16249 && INTEGERP (glyph->object))
16250 --glyph;
16251
16252 /* If last glyph is a space or stretch, and it's trailing
16253 whitespace, set the face of all trailing whitespace glyphs in
16254 IT->glyph_row to `trailing-whitespace'. */
16255 if (glyph >= start
16256 && BUFFERP (glyph->object)
16257 && (glyph->type == STRETCH_GLYPH
16258 || (glyph->type == CHAR_GLYPH
16259 && glyph->u.ch == ' '))
16260 && trailing_whitespace_p (glyph->charpos))
16261 {
16262 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
16263 if (face_id < 0)
16264 return;
16265
16266 while (glyph >= start
16267 && BUFFERP (glyph->object)
16268 && (glyph->type == STRETCH_GLYPH
16269 || (glyph->type == CHAR_GLYPH
16270 && glyph->u.ch == ' ')))
16271 (glyph--)->face_id = face_id;
16272 }
16273 }
16274 }
16275
16276
16277 /* Value is non-zero if glyph row ROW in window W should be
16278 used to hold the cursor. */
16279
16280 static int
16281 cursor_row_p (w, row)
16282 struct window *w;
16283 struct glyph_row *row;
16284 {
16285 int cursor_row_p = 1;
16286
16287 if (PT == MATRIX_ROW_END_CHARPOS (row))
16288 {
16289 /* Suppose the row ends on a string.
16290 Unless the row is continued, that means it ends on a newline
16291 in the string. If it's anything other than a display string
16292 (e.g. a before-string from an overlay), we don't want the
16293 cursor there. (This heuristic seems to give the optimal
16294 behavior for the various types of multi-line strings.) */
16295 if (CHARPOS (row->end.string_pos) >= 0)
16296 {
16297 if (row->continued_p)
16298 cursor_row_p = 1;
16299 else
16300 {
16301 /* Check for `display' property. */
16302 struct glyph *beg = row->glyphs[TEXT_AREA];
16303 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
16304 struct glyph *glyph;
16305
16306 cursor_row_p = 0;
16307 for (glyph = end; glyph >= beg; --glyph)
16308 if (STRINGP (glyph->object))
16309 {
16310 Lisp_Object prop
16311 = Fget_char_property (make_number (PT),
16312 Qdisplay, Qnil);
16313 cursor_row_p =
16314 (!NILP (prop)
16315 && display_prop_string_p (prop, glyph->object));
16316 break;
16317 }
16318 }
16319 }
16320 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
16321 {
16322 /* If the row ends in middle of a real character,
16323 and the line is continued, we want the cursor here.
16324 That's because MATRIX_ROW_END_CHARPOS would equal
16325 PT if PT is before the character. */
16326 if (!row->ends_in_ellipsis_p)
16327 cursor_row_p = row->continued_p;
16328 else
16329 /* If the row ends in an ellipsis, then
16330 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
16331 We want that position to be displayed after the ellipsis. */
16332 cursor_row_p = 0;
16333 }
16334 /* If the row ends at ZV, display the cursor at the end of that
16335 row instead of at the start of the row below. */
16336 else if (row->ends_at_zv_p)
16337 cursor_row_p = 1;
16338 else
16339 cursor_row_p = 0;
16340 }
16341
16342 return cursor_row_p;
16343 }
16344
16345 \f
16346
16347 /* Push the display property PROP so that it will be rendered at the
16348 current position in IT. */
16349
16350 static void
16351 push_display_prop (struct it *it, Lisp_Object prop)
16352 {
16353 push_it (it);
16354
16355 /* Never display a cursor on the prefix. */
16356 it->avoid_cursor_p = 1;
16357
16358 if (STRINGP (prop))
16359 {
16360 if (SCHARS (prop) == 0)
16361 {
16362 pop_it (it);
16363 return;
16364 }
16365
16366 it->string = prop;
16367 it->multibyte_p = STRING_MULTIBYTE (it->string);
16368 it->current.overlay_string_index = -1;
16369 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
16370 it->end_charpos = it->string_nchars = SCHARS (it->string);
16371 it->method = GET_FROM_STRING;
16372 it->stop_charpos = 0;
16373 }
16374 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
16375 {
16376 it->method = GET_FROM_STRETCH;
16377 it->object = prop;
16378 }
16379 #ifdef HAVE_WINDOW_SYSTEM
16380 else if (IMAGEP (prop))
16381 {
16382 it->what = IT_IMAGE;
16383 it->image_id = lookup_image (it->f, prop);
16384 it->method = GET_FROM_IMAGE;
16385 }
16386 #endif /* HAVE_WINDOW_SYSTEM */
16387 else
16388 {
16389 pop_it (it); /* bogus display property, give up */
16390 return;
16391 }
16392 }
16393
16394 /* Return the character-property PROP at the current position in IT. */
16395
16396 static Lisp_Object
16397 get_it_property (it, prop)
16398 struct it *it;
16399 Lisp_Object prop;
16400 {
16401 Lisp_Object position;
16402
16403 if (STRINGP (it->object))
16404 position = make_number (IT_STRING_CHARPOS (*it));
16405 else if (BUFFERP (it->object))
16406 position = make_number (IT_CHARPOS (*it));
16407 else
16408 return Qnil;
16409
16410 return Fget_char_property (position, prop, it->object);
16411 }
16412
16413 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
16414
16415 static void
16416 handle_line_prefix (struct it *it)
16417 {
16418 Lisp_Object prefix;
16419 if (it->continuation_lines_width > 0)
16420 {
16421 prefix = get_it_property (it, Qwrap_prefix);
16422 if (NILP (prefix))
16423 prefix = Vwrap_prefix;
16424 }
16425 else
16426 {
16427 prefix = get_it_property (it, Qline_prefix);
16428 if (NILP (prefix))
16429 prefix = Vline_prefix;
16430 }
16431 if (! NILP (prefix))
16432 push_display_prop (it, prefix);
16433 }
16434
16435 \f
16436
16437 /* Construct the glyph row IT->glyph_row in the desired matrix of
16438 IT->w from text at the current position of IT. See dispextern.h
16439 for an overview of struct it. Value is non-zero if
16440 IT->glyph_row displays text, as opposed to a line displaying ZV
16441 only. */
16442
16443 static int
16444 display_line (it)
16445 struct it *it;
16446 {
16447 struct glyph_row *row = it->glyph_row;
16448 Lisp_Object overlay_arrow_string;
16449 struct it wrap_it;
16450 int may_wrap = 0, wrap_x;
16451 int wrap_row_used = -1, wrap_row_ascent, wrap_row_height;
16452 int wrap_row_phys_ascent, wrap_row_phys_height;
16453 int wrap_row_extra_line_spacing;
16454
16455 /* We always start displaying at hpos zero even if hscrolled. */
16456 xassert (it->hpos == 0 && it->current_x == 0);
16457
16458 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
16459 >= it->w->desired_matrix->nrows)
16460 {
16461 it->w->nrows_scale_factor++;
16462 fonts_changed_p = 1;
16463 return 0;
16464 }
16465
16466 /* Is IT->w showing the region? */
16467 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
16468
16469 /* Clear the result glyph row and enable it. */
16470 prepare_desired_row (row);
16471
16472 row->y = it->current_y;
16473 row->start = it->start;
16474 row->continuation_lines_width = it->continuation_lines_width;
16475 row->displays_text_p = 1;
16476 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
16477 it->starts_in_middle_of_char_p = 0;
16478
16479 /* Arrange the overlays nicely for our purposes. Usually, we call
16480 display_line on only one line at a time, in which case this
16481 can't really hurt too much, or we call it on lines which appear
16482 one after another in the buffer, in which case all calls to
16483 recenter_overlay_lists but the first will be pretty cheap. */
16484 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
16485
16486 /* Move over display elements that are not visible because we are
16487 hscrolled. This may stop at an x-position < IT->first_visible_x
16488 if the first glyph is partially visible or if we hit a line end. */
16489 if (it->current_x < it->first_visible_x)
16490 {
16491 move_it_in_display_line_to (it, ZV, it->first_visible_x,
16492 MOVE_TO_POS | MOVE_TO_X);
16493 }
16494 else
16495 {
16496 /* We only do this when not calling `move_it_in_display_line_to'
16497 above, because move_it_in_display_line_to calls
16498 handle_line_prefix itself. */
16499 handle_line_prefix (it);
16500 }
16501
16502 /* Get the initial row height. This is either the height of the
16503 text hscrolled, if there is any, or zero. */
16504 row->ascent = it->max_ascent;
16505 row->height = it->max_ascent + it->max_descent;
16506 row->phys_ascent = it->max_phys_ascent;
16507 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
16508 row->extra_line_spacing = it->max_extra_line_spacing;
16509
16510 /* Loop generating characters. The loop is left with IT on the next
16511 character to display. */
16512 while (1)
16513 {
16514 int n_glyphs_before, hpos_before, x_before;
16515 int x, i, nglyphs;
16516 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
16517
16518 /* Retrieve the next thing to display. Value is zero if end of
16519 buffer reached. */
16520 if (!get_next_display_element (it))
16521 {
16522 /* Maybe add a space at the end of this line that is used to
16523 display the cursor there under X. Set the charpos of the
16524 first glyph of blank lines not corresponding to any text
16525 to -1. */
16526 #ifdef HAVE_WINDOW_SYSTEM
16527 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16528 row->exact_window_width_line_p = 1;
16529 else
16530 #endif /* HAVE_WINDOW_SYSTEM */
16531 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
16532 || row->used[TEXT_AREA] == 0)
16533 {
16534 row->glyphs[TEXT_AREA]->charpos = -1;
16535 row->displays_text_p = 0;
16536
16537 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
16538 && (!MINI_WINDOW_P (it->w)
16539 || (minibuf_level && EQ (it->window, minibuf_window))))
16540 row->indicate_empty_line_p = 1;
16541 }
16542
16543 it->continuation_lines_width = 0;
16544 row->ends_at_zv_p = 1;
16545 break;
16546 }
16547
16548 /* Now, get the metrics of what we want to display. This also
16549 generates glyphs in `row' (which is IT->glyph_row). */
16550 n_glyphs_before = row->used[TEXT_AREA];
16551 x = it->current_x;
16552
16553 /* Remember the line height so far in case the next element doesn't
16554 fit on the line. */
16555 if (it->line_wrap != TRUNCATE)
16556 {
16557 ascent = it->max_ascent;
16558 descent = it->max_descent;
16559 phys_ascent = it->max_phys_ascent;
16560 phys_descent = it->max_phys_descent;
16561
16562 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
16563 {
16564 if (IT_DISPLAYING_WHITESPACE (it))
16565 may_wrap = 1;
16566 else if (may_wrap)
16567 {
16568 wrap_it = *it;
16569 wrap_x = x;
16570 wrap_row_used = row->used[TEXT_AREA];
16571 wrap_row_ascent = row->ascent;
16572 wrap_row_height = row->height;
16573 wrap_row_phys_ascent = row->phys_ascent;
16574 wrap_row_phys_height = row->phys_height;
16575 wrap_row_extra_line_spacing = row->extra_line_spacing;
16576 may_wrap = 0;
16577 }
16578 }
16579 }
16580
16581 PRODUCE_GLYPHS (it);
16582
16583 /* If this display element was in marginal areas, continue with
16584 the next one. */
16585 if (it->area != TEXT_AREA)
16586 {
16587 row->ascent = max (row->ascent, it->max_ascent);
16588 row->height = max (row->height, it->max_ascent + it->max_descent);
16589 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16590 row->phys_height = max (row->phys_height,
16591 it->max_phys_ascent + it->max_phys_descent);
16592 row->extra_line_spacing = max (row->extra_line_spacing,
16593 it->max_extra_line_spacing);
16594 set_iterator_to_next (it, 1);
16595 continue;
16596 }
16597
16598 /* Does the display element fit on the line? If we truncate
16599 lines, we should draw past the right edge of the window. If
16600 we don't truncate, we want to stop so that we can display the
16601 continuation glyph before the right margin. If lines are
16602 continued, there are two possible strategies for characters
16603 resulting in more than 1 glyph (e.g. tabs): Display as many
16604 glyphs as possible in this line and leave the rest for the
16605 continuation line, or display the whole element in the next
16606 line. Original redisplay did the former, so we do it also. */
16607 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
16608 hpos_before = it->hpos;
16609 x_before = x;
16610
16611 if (/* Not a newline. */
16612 nglyphs > 0
16613 /* Glyphs produced fit entirely in the line. */
16614 && it->current_x < it->last_visible_x)
16615 {
16616 it->hpos += nglyphs;
16617 row->ascent = max (row->ascent, it->max_ascent);
16618 row->height = max (row->height, it->max_ascent + it->max_descent);
16619 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16620 row->phys_height = max (row->phys_height,
16621 it->max_phys_ascent + it->max_phys_descent);
16622 row->extra_line_spacing = max (row->extra_line_spacing,
16623 it->max_extra_line_spacing);
16624 if (it->current_x - it->pixel_width < it->first_visible_x)
16625 row->x = x - it->first_visible_x;
16626 }
16627 else
16628 {
16629 int new_x;
16630 struct glyph *glyph;
16631
16632 for (i = 0; i < nglyphs; ++i, x = new_x)
16633 {
16634 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16635 new_x = x + glyph->pixel_width;
16636
16637 if (/* Lines are continued. */
16638 it->line_wrap != TRUNCATE
16639 && (/* Glyph doesn't fit on the line. */
16640 new_x > it->last_visible_x
16641 /* Or it fits exactly on a window system frame. */
16642 || (new_x == it->last_visible_x
16643 && FRAME_WINDOW_P (it->f))))
16644 {
16645 /* End of a continued line. */
16646
16647 if (it->hpos == 0
16648 || (new_x == it->last_visible_x
16649 && FRAME_WINDOW_P (it->f)))
16650 {
16651 /* Current glyph is the only one on the line or
16652 fits exactly on the line. We must continue
16653 the line because we can't draw the cursor
16654 after the glyph. */
16655 row->continued_p = 1;
16656 it->current_x = new_x;
16657 it->continuation_lines_width += new_x;
16658 ++it->hpos;
16659 if (i == nglyphs - 1)
16660 {
16661 /* If line-wrap is on, check if a previous
16662 wrap point was found. */
16663 if (wrap_row_used > 0
16664 /* Even if there is a previous wrap
16665 point, continue the line here as
16666 usual, if (i) the previous character
16667 was a space or tab AND (ii) the
16668 current character is not. */
16669 && (!may_wrap
16670 || IT_DISPLAYING_WHITESPACE (it)))
16671 goto back_to_wrap;
16672
16673 set_iterator_to_next (it, 1);
16674 #ifdef HAVE_WINDOW_SYSTEM
16675 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16676 {
16677 if (!get_next_display_element (it))
16678 {
16679 row->exact_window_width_line_p = 1;
16680 it->continuation_lines_width = 0;
16681 row->continued_p = 0;
16682 row->ends_at_zv_p = 1;
16683 }
16684 else if (ITERATOR_AT_END_OF_LINE_P (it))
16685 {
16686 row->continued_p = 0;
16687 row->exact_window_width_line_p = 1;
16688 }
16689 }
16690 #endif /* HAVE_WINDOW_SYSTEM */
16691 }
16692 }
16693 else if (CHAR_GLYPH_PADDING_P (*glyph)
16694 && !FRAME_WINDOW_P (it->f))
16695 {
16696 /* A padding glyph that doesn't fit on this line.
16697 This means the whole character doesn't fit
16698 on the line. */
16699 row->used[TEXT_AREA] = n_glyphs_before;
16700
16701 /* Fill the rest of the row with continuation
16702 glyphs like in 20.x. */
16703 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16704 < row->glyphs[1 + TEXT_AREA])
16705 produce_special_glyphs (it, IT_CONTINUATION);
16706
16707 row->continued_p = 1;
16708 it->current_x = x_before;
16709 it->continuation_lines_width += x_before;
16710
16711 /* Restore the height to what it was before the
16712 element not fitting on the line. */
16713 it->max_ascent = ascent;
16714 it->max_descent = descent;
16715 it->max_phys_ascent = phys_ascent;
16716 it->max_phys_descent = phys_descent;
16717 }
16718 else if (wrap_row_used > 0)
16719 {
16720 back_to_wrap:
16721 *it = wrap_it;
16722 it->continuation_lines_width += wrap_x;
16723 row->used[TEXT_AREA] = wrap_row_used;
16724 row->ascent = wrap_row_ascent;
16725 row->height = wrap_row_height;
16726 row->phys_ascent = wrap_row_phys_ascent;
16727 row->phys_height = wrap_row_phys_height;
16728 row->extra_line_spacing = wrap_row_extra_line_spacing;
16729 row->continued_p = 1;
16730 row->ends_at_zv_p = 0;
16731 row->exact_window_width_line_p = 0;
16732 it->continuation_lines_width += x;
16733
16734 /* Make sure that a non-default face is extended
16735 up to the right margin of the window. */
16736 extend_face_to_end_of_line (it);
16737 }
16738 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16739 {
16740 /* A TAB that extends past the right edge of the
16741 window. This produces a single glyph on
16742 window system frames. We leave the glyph in
16743 this row and let it fill the row, but don't
16744 consume the TAB. */
16745 it->continuation_lines_width += it->last_visible_x;
16746 row->ends_in_middle_of_char_p = 1;
16747 row->continued_p = 1;
16748 glyph->pixel_width = it->last_visible_x - x;
16749 it->starts_in_middle_of_char_p = 1;
16750 }
16751 else
16752 {
16753 /* Something other than a TAB that draws past
16754 the right edge of the window. Restore
16755 positions to values before the element. */
16756 row->used[TEXT_AREA] = n_glyphs_before + i;
16757
16758 /* Display continuation glyphs. */
16759 if (!FRAME_WINDOW_P (it->f))
16760 produce_special_glyphs (it, IT_CONTINUATION);
16761 row->continued_p = 1;
16762
16763 it->current_x = x_before;
16764 it->continuation_lines_width += x;
16765 extend_face_to_end_of_line (it);
16766
16767 if (nglyphs > 1 && i > 0)
16768 {
16769 row->ends_in_middle_of_char_p = 1;
16770 it->starts_in_middle_of_char_p = 1;
16771 }
16772
16773 /* Restore the height to what it was before the
16774 element not fitting on the line. */
16775 it->max_ascent = ascent;
16776 it->max_descent = descent;
16777 it->max_phys_ascent = phys_ascent;
16778 it->max_phys_descent = phys_descent;
16779 }
16780
16781 break;
16782 }
16783 else if (new_x > it->first_visible_x)
16784 {
16785 /* Increment number of glyphs actually displayed. */
16786 ++it->hpos;
16787
16788 if (x < it->first_visible_x)
16789 /* Glyph is partially visible, i.e. row starts at
16790 negative X position. */
16791 row->x = x - it->first_visible_x;
16792 }
16793 else
16794 {
16795 /* Glyph is completely off the left margin of the
16796 window. This should not happen because of the
16797 move_it_in_display_line at the start of this
16798 function, unless the text display area of the
16799 window is empty. */
16800 xassert (it->first_visible_x <= it->last_visible_x);
16801 }
16802 }
16803
16804 row->ascent = max (row->ascent, it->max_ascent);
16805 row->height = max (row->height, it->max_ascent + it->max_descent);
16806 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16807 row->phys_height = max (row->phys_height,
16808 it->max_phys_ascent + it->max_phys_descent);
16809 row->extra_line_spacing = max (row->extra_line_spacing,
16810 it->max_extra_line_spacing);
16811
16812 /* End of this display line if row is continued. */
16813 if (row->continued_p || row->ends_at_zv_p)
16814 break;
16815 }
16816
16817 at_end_of_line:
16818 /* Is this a line end? If yes, we're also done, after making
16819 sure that a non-default face is extended up to the right
16820 margin of the window. */
16821 if (ITERATOR_AT_END_OF_LINE_P (it))
16822 {
16823 int used_before = row->used[TEXT_AREA];
16824
16825 row->ends_in_newline_from_string_p = STRINGP (it->object);
16826
16827 #ifdef HAVE_WINDOW_SYSTEM
16828 /* Add a space at the end of the line that is used to
16829 display the cursor there. */
16830 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16831 append_space_for_newline (it, 0);
16832 #endif /* HAVE_WINDOW_SYSTEM */
16833
16834 /* Extend the face to the end of the line. */
16835 extend_face_to_end_of_line (it);
16836
16837 /* Make sure we have the position. */
16838 if (used_before == 0)
16839 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16840
16841 /* Consume the line end. This skips over invisible lines. */
16842 set_iterator_to_next (it, 1);
16843 it->continuation_lines_width = 0;
16844 break;
16845 }
16846
16847 /* Proceed with next display element. Note that this skips
16848 over lines invisible because of selective display. */
16849 set_iterator_to_next (it, 1);
16850
16851 /* If we truncate lines, we are done when the last displayed
16852 glyphs reach past the right margin of the window. */
16853 if (it->line_wrap == TRUNCATE
16854 && (FRAME_WINDOW_P (it->f)
16855 ? (it->current_x >= it->last_visible_x)
16856 : (it->current_x > it->last_visible_x)))
16857 {
16858 /* Maybe add truncation glyphs. */
16859 if (!FRAME_WINDOW_P (it->f))
16860 {
16861 int i, n;
16862
16863 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16864 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16865 break;
16866
16867 for (n = row->used[TEXT_AREA]; i < n; ++i)
16868 {
16869 row->used[TEXT_AREA] = i;
16870 produce_special_glyphs (it, IT_TRUNCATION);
16871 }
16872 }
16873 #ifdef HAVE_WINDOW_SYSTEM
16874 else
16875 {
16876 /* Don't truncate if we can overflow newline into fringe. */
16877 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16878 {
16879 if (!get_next_display_element (it))
16880 {
16881 it->continuation_lines_width = 0;
16882 row->ends_at_zv_p = 1;
16883 row->exact_window_width_line_p = 1;
16884 break;
16885 }
16886 if (ITERATOR_AT_END_OF_LINE_P (it))
16887 {
16888 row->exact_window_width_line_p = 1;
16889 goto at_end_of_line;
16890 }
16891 }
16892 }
16893 #endif /* HAVE_WINDOW_SYSTEM */
16894
16895 row->truncated_on_right_p = 1;
16896 it->continuation_lines_width = 0;
16897 reseat_at_next_visible_line_start (it, 0);
16898 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16899 it->hpos = hpos_before;
16900 it->current_x = x_before;
16901 break;
16902 }
16903 }
16904
16905 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16906 at the left window margin. */
16907 if (it->first_visible_x
16908 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16909 {
16910 if (!FRAME_WINDOW_P (it->f))
16911 insert_left_trunc_glyphs (it);
16912 row->truncated_on_left_p = 1;
16913 }
16914
16915 /* If the start of this line is the overlay arrow-position, then
16916 mark this glyph row as the one containing the overlay arrow.
16917 This is clearly a mess with variable size fonts. It would be
16918 better to let it be displayed like cursors under X. */
16919 if ((row->displays_text_p || !overlay_arrow_seen)
16920 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16921 !NILP (overlay_arrow_string)))
16922 {
16923 /* Overlay arrow in window redisplay is a fringe bitmap. */
16924 if (STRINGP (overlay_arrow_string))
16925 {
16926 struct glyph_row *arrow_row
16927 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16928 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16929 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16930 struct glyph *p = row->glyphs[TEXT_AREA];
16931 struct glyph *p2, *end;
16932
16933 /* Copy the arrow glyphs. */
16934 while (glyph < arrow_end)
16935 *p++ = *glyph++;
16936
16937 /* Throw away padding glyphs. */
16938 p2 = p;
16939 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16940 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16941 ++p2;
16942 if (p2 > p)
16943 {
16944 while (p2 < end)
16945 *p++ = *p2++;
16946 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16947 }
16948 }
16949 else
16950 {
16951 xassert (INTEGERP (overlay_arrow_string));
16952 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16953 }
16954 overlay_arrow_seen = 1;
16955 }
16956
16957 /* Compute pixel dimensions of this line. */
16958 compute_line_metrics (it);
16959
16960 /* Remember the position at which this line ends. */
16961 row->end = it->current;
16962
16963 /* Record whether this row ends inside an ellipsis. */
16964 row->ends_in_ellipsis_p
16965 = (it->method == GET_FROM_DISPLAY_VECTOR
16966 && it->ellipsis_p);
16967
16968 /* Save fringe bitmaps in this row. */
16969 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16970 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16971 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16972 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16973
16974 it->left_user_fringe_bitmap = 0;
16975 it->left_user_fringe_face_id = 0;
16976 it->right_user_fringe_bitmap = 0;
16977 it->right_user_fringe_face_id = 0;
16978
16979 /* Maybe set the cursor. */
16980 if (it->w->cursor.vpos < 0
16981 && PT >= MATRIX_ROW_START_CHARPOS (row)
16982 && PT <= MATRIX_ROW_END_CHARPOS (row)
16983 && cursor_row_p (it->w, row))
16984 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16985
16986 /* Highlight trailing whitespace. */
16987 if (!NILP (Vshow_trailing_whitespace))
16988 highlight_trailing_whitespace (it->f, it->glyph_row);
16989
16990 /* Prepare for the next line. This line starts horizontally at (X
16991 HPOS) = (0 0). Vertical positions are incremented. As a
16992 convenience for the caller, IT->glyph_row is set to the next
16993 row to be used. */
16994 it->current_x = it->hpos = 0;
16995 it->current_y += row->height;
16996 ++it->vpos;
16997 ++it->glyph_row;
16998 it->start = it->current;
16999 return row->displays_text_p;
17000 }
17001
17002
17003 \f
17004 /***********************************************************************
17005 Menu Bar
17006 ***********************************************************************/
17007
17008 /* Redisplay the menu bar in the frame for window W.
17009
17010 The menu bar of X frames that don't have X toolkit support is
17011 displayed in a special window W->frame->menu_bar_window.
17012
17013 The menu bar of terminal frames is treated specially as far as
17014 glyph matrices are concerned. Menu bar lines are not part of
17015 windows, so the update is done directly on the frame matrix rows
17016 for the menu bar. */
17017
17018 static void
17019 display_menu_bar (w)
17020 struct window *w;
17021 {
17022 struct frame *f = XFRAME (WINDOW_FRAME (w));
17023 struct it it;
17024 Lisp_Object items;
17025 int i;
17026
17027 /* Don't do all this for graphical frames. */
17028 #ifdef HAVE_NTGUI
17029 if (FRAME_W32_P (f))
17030 return;
17031 #endif
17032 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
17033 if (FRAME_X_P (f))
17034 return;
17035 #endif
17036 #ifdef MAC_OS
17037 if (FRAME_MAC_P (f))
17038 return;
17039 #endif
17040
17041 #ifdef USE_X_TOOLKIT
17042 xassert (!FRAME_WINDOW_P (f));
17043 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
17044 it.first_visible_x = 0;
17045 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17046 #else /* not USE_X_TOOLKIT */
17047 if (FRAME_WINDOW_P (f))
17048 {
17049 /* Menu bar lines are displayed in the desired matrix of the
17050 dummy window menu_bar_window. */
17051 struct window *menu_w;
17052 xassert (WINDOWP (f->menu_bar_window));
17053 menu_w = XWINDOW (f->menu_bar_window);
17054 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
17055 MENU_FACE_ID);
17056 it.first_visible_x = 0;
17057 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
17058 }
17059 else
17060 {
17061 /* This is a TTY frame, i.e. character hpos/vpos are used as
17062 pixel x/y. */
17063 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
17064 MENU_FACE_ID);
17065 it.first_visible_x = 0;
17066 it.last_visible_x = FRAME_COLS (f);
17067 }
17068 #endif /* not USE_X_TOOLKIT */
17069
17070 if (! mode_line_inverse_video)
17071 /* Force the menu-bar to be displayed in the default face. */
17072 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17073
17074 /* Clear all rows of the menu bar. */
17075 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
17076 {
17077 struct glyph_row *row = it.glyph_row + i;
17078 clear_glyph_row (row);
17079 row->enabled_p = 1;
17080 row->full_width_p = 1;
17081 }
17082
17083 /* Display all items of the menu bar. */
17084 items = FRAME_MENU_BAR_ITEMS (it.f);
17085 for (i = 0; i < XVECTOR (items)->size; i += 4)
17086 {
17087 Lisp_Object string;
17088
17089 /* Stop at nil string. */
17090 string = AREF (items, i + 1);
17091 if (NILP (string))
17092 break;
17093
17094 /* Remember where item was displayed. */
17095 ASET (items, i + 3, make_number (it.hpos));
17096
17097 /* Display the item, pad with one space. */
17098 if (it.current_x < it.last_visible_x)
17099 display_string (NULL, string, Qnil, 0, 0, &it,
17100 SCHARS (string) + 1, 0, 0, -1);
17101 }
17102
17103 /* Fill out the line with spaces. */
17104 if (it.current_x < it.last_visible_x)
17105 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
17106
17107 /* Compute the total height of the lines. */
17108 compute_line_metrics (&it);
17109 }
17110
17111
17112 \f
17113 /***********************************************************************
17114 Mode Line
17115 ***********************************************************************/
17116
17117 /* Redisplay mode lines in the window tree whose root is WINDOW. If
17118 FORCE is non-zero, redisplay mode lines unconditionally.
17119 Otherwise, redisplay only mode lines that are garbaged. Value is
17120 the number of windows whose mode lines were redisplayed. */
17121
17122 static int
17123 redisplay_mode_lines (window, force)
17124 Lisp_Object window;
17125 int force;
17126 {
17127 int nwindows = 0;
17128
17129 while (!NILP (window))
17130 {
17131 struct window *w = XWINDOW (window);
17132
17133 if (WINDOWP (w->hchild))
17134 nwindows += redisplay_mode_lines (w->hchild, force);
17135 else if (WINDOWP (w->vchild))
17136 nwindows += redisplay_mode_lines (w->vchild, force);
17137 else if (force
17138 || FRAME_GARBAGED_P (XFRAME (w->frame))
17139 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
17140 {
17141 struct text_pos lpoint;
17142 struct buffer *old = current_buffer;
17143
17144 /* Set the window's buffer for the mode line display. */
17145 SET_TEXT_POS (lpoint, PT, PT_BYTE);
17146 set_buffer_internal_1 (XBUFFER (w->buffer));
17147
17148 /* Point refers normally to the selected window. For any
17149 other window, set up appropriate value. */
17150 if (!EQ (window, selected_window))
17151 {
17152 struct text_pos pt;
17153
17154 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
17155 if (CHARPOS (pt) < BEGV)
17156 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17157 else if (CHARPOS (pt) > (ZV - 1))
17158 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
17159 else
17160 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
17161 }
17162
17163 /* Display mode lines. */
17164 clear_glyph_matrix (w->desired_matrix);
17165 if (display_mode_lines (w))
17166 {
17167 ++nwindows;
17168 w->must_be_updated_p = 1;
17169 }
17170
17171 /* Restore old settings. */
17172 set_buffer_internal_1 (old);
17173 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17174 }
17175
17176 window = w->next;
17177 }
17178
17179 return nwindows;
17180 }
17181
17182
17183 /* Display the mode and/or top line of window W. Value is the number
17184 of mode lines displayed. */
17185
17186 static int
17187 display_mode_lines (w)
17188 struct window *w;
17189 {
17190 Lisp_Object old_selected_window, old_selected_frame;
17191 int n = 0;
17192
17193 old_selected_frame = selected_frame;
17194 selected_frame = w->frame;
17195 old_selected_window = selected_window;
17196 XSETWINDOW (selected_window, w);
17197
17198 /* These will be set while the mode line specs are processed. */
17199 line_number_displayed = 0;
17200 w->column_number_displayed = Qnil;
17201
17202 if (WINDOW_WANTS_MODELINE_P (w))
17203 {
17204 struct window *sel_w = XWINDOW (old_selected_window);
17205
17206 /* Select mode line face based on the real selected window. */
17207 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
17208 current_buffer->mode_line_format);
17209 ++n;
17210 }
17211
17212 if (WINDOW_WANTS_HEADER_LINE_P (w))
17213 {
17214 display_mode_line (w, HEADER_LINE_FACE_ID,
17215 current_buffer->header_line_format);
17216 ++n;
17217 }
17218
17219 selected_frame = old_selected_frame;
17220 selected_window = old_selected_window;
17221 return n;
17222 }
17223
17224
17225 /* Display mode or top line of window W. FACE_ID specifies which line
17226 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
17227 FORMAT is the mode line format to display. Value is the pixel
17228 height of the mode line displayed. */
17229
17230 static int
17231 display_mode_line (w, face_id, format)
17232 struct window *w;
17233 enum face_id face_id;
17234 Lisp_Object format;
17235 {
17236 struct it it;
17237 struct face *face;
17238 int count = SPECPDL_INDEX ();
17239
17240 init_iterator (&it, w, -1, -1, NULL, face_id);
17241 /* Don't extend on a previously drawn mode-line.
17242 This may happen if called from pos_visible_p. */
17243 it.glyph_row->enabled_p = 0;
17244 prepare_desired_row (it.glyph_row);
17245
17246 it.glyph_row->mode_line_p = 1;
17247
17248 if (! mode_line_inverse_video)
17249 /* Force the mode-line to be displayed in the default face. */
17250 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
17251
17252 record_unwind_protect (unwind_format_mode_line,
17253 format_mode_line_unwind_data (NULL, Qnil, 0));
17254
17255 mode_line_target = MODE_LINE_DISPLAY;
17256
17257 /* Temporarily make frame's keyboard the current kboard so that
17258 kboard-local variables in the mode_line_format will get the right
17259 values. */
17260 push_kboard (FRAME_KBOARD (it.f));
17261 record_unwind_save_match_data ();
17262 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17263 pop_kboard ();
17264
17265 unbind_to (count, Qnil);
17266
17267 /* Fill up with spaces. */
17268 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
17269
17270 compute_line_metrics (&it);
17271 it.glyph_row->full_width_p = 1;
17272 it.glyph_row->continued_p = 0;
17273 it.glyph_row->truncated_on_left_p = 0;
17274 it.glyph_row->truncated_on_right_p = 0;
17275
17276 /* Make a 3D mode-line have a shadow at its right end. */
17277 face = FACE_FROM_ID (it.f, face_id);
17278 extend_face_to_end_of_line (&it);
17279 if (face->box != FACE_NO_BOX)
17280 {
17281 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
17282 + it.glyph_row->used[TEXT_AREA] - 1);
17283 last->right_box_line_p = 1;
17284 }
17285
17286 return it.glyph_row->height;
17287 }
17288
17289 /* Move element ELT in LIST to the front of LIST.
17290 Return the updated list. */
17291
17292 static Lisp_Object
17293 move_elt_to_front (elt, list)
17294 Lisp_Object elt, list;
17295 {
17296 register Lisp_Object tail, prev;
17297 register Lisp_Object tem;
17298
17299 tail = list;
17300 prev = Qnil;
17301 while (CONSP (tail))
17302 {
17303 tem = XCAR (tail);
17304
17305 if (EQ (elt, tem))
17306 {
17307 /* Splice out the link TAIL. */
17308 if (NILP (prev))
17309 list = XCDR (tail);
17310 else
17311 Fsetcdr (prev, XCDR (tail));
17312
17313 /* Now make it the first. */
17314 Fsetcdr (tail, list);
17315 return tail;
17316 }
17317 else
17318 prev = tail;
17319 tail = XCDR (tail);
17320 QUIT;
17321 }
17322
17323 /* Not found--return unchanged LIST. */
17324 return list;
17325 }
17326
17327 /* Contribute ELT to the mode line for window IT->w. How it
17328 translates into text depends on its data type.
17329
17330 IT describes the display environment in which we display, as usual.
17331
17332 DEPTH is the depth in recursion. It is used to prevent
17333 infinite recursion here.
17334
17335 FIELD_WIDTH is the number of characters the display of ELT should
17336 occupy in the mode line, and PRECISION is the maximum number of
17337 characters to display from ELT's representation. See
17338 display_string for details.
17339
17340 Returns the hpos of the end of the text generated by ELT.
17341
17342 PROPS is a property list to add to any string we encounter.
17343
17344 If RISKY is nonzero, remove (disregard) any properties in any string
17345 we encounter, and ignore :eval and :propertize.
17346
17347 The global variable `mode_line_target' determines whether the
17348 output is passed to `store_mode_line_noprop',
17349 `store_mode_line_string', or `display_string'. */
17350
17351 static int
17352 display_mode_element (it, depth, field_width, precision, elt, props, risky)
17353 struct it *it;
17354 int depth;
17355 int field_width, precision;
17356 Lisp_Object elt, props;
17357 int risky;
17358 {
17359 int n = 0, field, prec;
17360 int literal = 0;
17361
17362 tail_recurse:
17363 if (depth > 100)
17364 elt = build_string ("*too-deep*");
17365
17366 depth++;
17367
17368 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
17369 {
17370 case Lisp_String:
17371 {
17372 /* A string: output it and check for %-constructs within it. */
17373 unsigned char c;
17374 int offset = 0;
17375
17376 if (SCHARS (elt) > 0
17377 && (!NILP (props) || risky))
17378 {
17379 Lisp_Object oprops, aelt;
17380 oprops = Ftext_properties_at (make_number (0), elt);
17381
17382 /* If the starting string's properties are not what
17383 we want, translate the string. Also, if the string
17384 is risky, do that anyway. */
17385
17386 if (NILP (Fequal (props, oprops)) || risky)
17387 {
17388 /* If the starting string has properties,
17389 merge the specified ones onto the existing ones. */
17390 if (! NILP (oprops) && !risky)
17391 {
17392 Lisp_Object tem;
17393
17394 oprops = Fcopy_sequence (oprops);
17395 tem = props;
17396 while (CONSP (tem))
17397 {
17398 oprops = Fplist_put (oprops, XCAR (tem),
17399 XCAR (XCDR (tem)));
17400 tem = XCDR (XCDR (tem));
17401 }
17402 props = oprops;
17403 }
17404
17405 aelt = Fassoc (elt, mode_line_proptrans_alist);
17406 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
17407 {
17408 /* AELT is what we want. Move it to the front
17409 without consing. */
17410 elt = XCAR (aelt);
17411 mode_line_proptrans_alist
17412 = move_elt_to_front (aelt, mode_line_proptrans_alist);
17413 }
17414 else
17415 {
17416 Lisp_Object tem;
17417
17418 /* If AELT has the wrong props, it is useless.
17419 so get rid of it. */
17420 if (! NILP (aelt))
17421 mode_line_proptrans_alist
17422 = Fdelq (aelt, mode_line_proptrans_alist);
17423
17424 elt = Fcopy_sequence (elt);
17425 Fset_text_properties (make_number (0), Flength (elt),
17426 props, elt);
17427 /* Add this item to mode_line_proptrans_alist. */
17428 mode_line_proptrans_alist
17429 = Fcons (Fcons (elt, props),
17430 mode_line_proptrans_alist);
17431 /* Truncate mode_line_proptrans_alist
17432 to at most 50 elements. */
17433 tem = Fnthcdr (make_number (50),
17434 mode_line_proptrans_alist);
17435 if (! NILP (tem))
17436 XSETCDR (tem, Qnil);
17437 }
17438 }
17439 }
17440
17441 offset = 0;
17442
17443 if (literal)
17444 {
17445 prec = precision - n;
17446 switch (mode_line_target)
17447 {
17448 case MODE_LINE_NOPROP:
17449 case MODE_LINE_TITLE:
17450 n += store_mode_line_noprop (SDATA (elt), -1, prec);
17451 break;
17452 case MODE_LINE_STRING:
17453 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
17454 break;
17455 case MODE_LINE_DISPLAY:
17456 n += display_string (NULL, elt, Qnil, 0, 0, it,
17457 0, prec, 0, STRING_MULTIBYTE (elt));
17458 break;
17459 }
17460
17461 break;
17462 }
17463
17464 /* Handle the non-literal case. */
17465
17466 while ((precision <= 0 || n < precision)
17467 && SREF (elt, offset) != 0
17468 && (mode_line_target != MODE_LINE_DISPLAY
17469 || it->current_x < it->last_visible_x))
17470 {
17471 int last_offset = offset;
17472
17473 /* Advance to end of string or next format specifier. */
17474 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
17475 ;
17476
17477 if (offset - 1 != last_offset)
17478 {
17479 int nchars, nbytes;
17480
17481 /* Output to end of string or up to '%'. Field width
17482 is length of string. Don't output more than
17483 PRECISION allows us. */
17484 offset--;
17485
17486 prec = c_string_width (SDATA (elt) + last_offset,
17487 offset - last_offset, precision - n,
17488 &nchars, &nbytes);
17489
17490 switch (mode_line_target)
17491 {
17492 case MODE_LINE_NOPROP:
17493 case MODE_LINE_TITLE:
17494 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
17495 break;
17496 case MODE_LINE_STRING:
17497 {
17498 int bytepos = last_offset;
17499 int charpos = string_byte_to_char (elt, bytepos);
17500 int endpos = (precision <= 0
17501 ? string_byte_to_char (elt, offset)
17502 : charpos + nchars);
17503
17504 n += store_mode_line_string (NULL,
17505 Fsubstring (elt, make_number (charpos),
17506 make_number (endpos)),
17507 0, 0, 0, Qnil);
17508 }
17509 break;
17510 case MODE_LINE_DISPLAY:
17511 {
17512 int bytepos = last_offset;
17513 int charpos = string_byte_to_char (elt, bytepos);
17514
17515 if (precision <= 0)
17516 nchars = string_byte_to_char (elt, offset) - charpos;
17517 n += display_string (NULL, elt, Qnil, 0, charpos,
17518 it, 0, nchars, 0,
17519 STRING_MULTIBYTE (elt));
17520 }
17521 break;
17522 }
17523 }
17524 else /* c == '%' */
17525 {
17526 int percent_position = offset;
17527
17528 /* Get the specified minimum width. Zero means
17529 don't pad. */
17530 field = 0;
17531 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
17532 field = field * 10 + c - '0';
17533
17534 /* Don't pad beyond the total padding allowed. */
17535 if (field_width - n > 0 && field > field_width - n)
17536 field = field_width - n;
17537
17538 /* Note that either PRECISION <= 0 or N < PRECISION. */
17539 prec = precision - n;
17540
17541 if (c == 'M')
17542 n += display_mode_element (it, depth, field, prec,
17543 Vglobal_mode_string, props,
17544 risky);
17545 else if (c != 0)
17546 {
17547 int multibyte;
17548 int bytepos, charpos;
17549 unsigned char *spec;
17550
17551 bytepos = percent_position;
17552 charpos = (STRING_MULTIBYTE (elt)
17553 ? string_byte_to_char (elt, bytepos)
17554 : bytepos);
17555 spec
17556 = decode_mode_spec (it->w, c, field, prec, &multibyte);
17557
17558 switch (mode_line_target)
17559 {
17560 case MODE_LINE_NOPROP:
17561 case MODE_LINE_TITLE:
17562 n += store_mode_line_noprop (spec, field, prec);
17563 break;
17564 case MODE_LINE_STRING:
17565 {
17566 int len = strlen (spec);
17567 Lisp_Object tem = make_string (spec, len);
17568 props = Ftext_properties_at (make_number (charpos), elt);
17569 /* Should only keep face property in props */
17570 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
17571 }
17572 break;
17573 case MODE_LINE_DISPLAY:
17574 {
17575 int nglyphs_before, nwritten;
17576
17577 nglyphs_before = it->glyph_row->used[TEXT_AREA];
17578 nwritten = display_string (spec, Qnil, elt,
17579 charpos, 0, it,
17580 field, prec, 0,
17581 multibyte);
17582
17583 /* Assign to the glyphs written above the
17584 string where the `%x' came from, position
17585 of the `%'. */
17586 if (nwritten > 0)
17587 {
17588 struct glyph *glyph
17589 = (it->glyph_row->glyphs[TEXT_AREA]
17590 + nglyphs_before);
17591 int i;
17592
17593 for (i = 0; i < nwritten; ++i)
17594 {
17595 glyph[i].object = elt;
17596 glyph[i].charpos = charpos;
17597 }
17598
17599 n += nwritten;
17600 }
17601 }
17602 break;
17603 }
17604 }
17605 else /* c == 0 */
17606 break;
17607 }
17608 }
17609 }
17610 break;
17611
17612 case Lisp_Symbol:
17613 /* A symbol: process the value of the symbol recursively
17614 as if it appeared here directly. Avoid error if symbol void.
17615 Special case: if value of symbol is a string, output the string
17616 literally. */
17617 {
17618 register Lisp_Object tem;
17619
17620 /* If the variable is not marked as risky to set
17621 then its contents are risky to use. */
17622 if (NILP (Fget (elt, Qrisky_local_variable)))
17623 risky = 1;
17624
17625 tem = Fboundp (elt);
17626 if (!NILP (tem))
17627 {
17628 tem = Fsymbol_value (elt);
17629 /* If value is a string, output that string literally:
17630 don't check for % within it. */
17631 if (STRINGP (tem))
17632 literal = 1;
17633
17634 if (!EQ (tem, elt))
17635 {
17636 /* Give up right away for nil or t. */
17637 elt = tem;
17638 goto tail_recurse;
17639 }
17640 }
17641 }
17642 break;
17643
17644 case Lisp_Cons:
17645 {
17646 register Lisp_Object car, tem;
17647
17648 /* A cons cell: five distinct cases.
17649 If first element is :eval or :propertize, do something special.
17650 If first element is a string or a cons, process all the elements
17651 and effectively concatenate them.
17652 If first element is a negative number, truncate displaying cdr to
17653 at most that many characters. If positive, pad (with spaces)
17654 to at least that many characters.
17655 If first element is a symbol, process the cadr or caddr recursively
17656 according to whether the symbol's value is non-nil or nil. */
17657 car = XCAR (elt);
17658 if (EQ (car, QCeval))
17659 {
17660 /* An element of the form (:eval FORM) means evaluate FORM
17661 and use the result as mode line elements. */
17662
17663 if (risky)
17664 break;
17665
17666 if (CONSP (XCDR (elt)))
17667 {
17668 Lisp_Object spec;
17669 spec = safe_eval (XCAR (XCDR (elt)));
17670 n += display_mode_element (it, depth, field_width - n,
17671 precision - n, spec, props,
17672 risky);
17673 }
17674 }
17675 else if (EQ (car, QCpropertize))
17676 {
17677 /* An element of the form (:propertize ELT PROPS...)
17678 means display ELT but applying properties PROPS. */
17679
17680 if (risky)
17681 break;
17682
17683 if (CONSP (XCDR (elt)))
17684 n += display_mode_element (it, depth, field_width - n,
17685 precision - n, XCAR (XCDR (elt)),
17686 XCDR (XCDR (elt)), risky);
17687 }
17688 else if (SYMBOLP (car))
17689 {
17690 tem = Fboundp (car);
17691 elt = XCDR (elt);
17692 if (!CONSP (elt))
17693 goto invalid;
17694 /* elt is now the cdr, and we know it is a cons cell.
17695 Use its car if CAR has a non-nil value. */
17696 if (!NILP (tem))
17697 {
17698 tem = Fsymbol_value (car);
17699 if (!NILP (tem))
17700 {
17701 elt = XCAR (elt);
17702 goto tail_recurse;
17703 }
17704 }
17705 /* Symbol's value is nil (or symbol is unbound)
17706 Get the cddr of the original list
17707 and if possible find the caddr and use that. */
17708 elt = XCDR (elt);
17709 if (NILP (elt))
17710 break;
17711 else if (!CONSP (elt))
17712 goto invalid;
17713 elt = XCAR (elt);
17714 goto tail_recurse;
17715 }
17716 else if (INTEGERP (car))
17717 {
17718 register int lim = XINT (car);
17719 elt = XCDR (elt);
17720 if (lim < 0)
17721 {
17722 /* Negative int means reduce maximum width. */
17723 if (precision <= 0)
17724 precision = -lim;
17725 else
17726 precision = min (precision, -lim);
17727 }
17728 else if (lim > 0)
17729 {
17730 /* Padding specified. Don't let it be more than
17731 current maximum. */
17732 if (precision > 0)
17733 lim = min (precision, lim);
17734
17735 /* If that's more padding than already wanted, queue it.
17736 But don't reduce padding already specified even if
17737 that is beyond the current truncation point. */
17738 field_width = max (lim, field_width);
17739 }
17740 goto tail_recurse;
17741 }
17742 else if (STRINGP (car) || CONSP (car))
17743 {
17744 register int limit = 50;
17745 /* Limit is to protect against circular lists. */
17746 while (CONSP (elt)
17747 && --limit > 0
17748 && (precision <= 0 || n < precision))
17749 {
17750 n += display_mode_element (it, depth,
17751 /* Do padding only after the last
17752 element in the list. */
17753 (! CONSP (XCDR (elt))
17754 ? field_width - n
17755 : 0),
17756 precision - n, XCAR (elt),
17757 props, risky);
17758 elt = XCDR (elt);
17759 }
17760 }
17761 }
17762 break;
17763
17764 default:
17765 invalid:
17766 elt = build_string ("*invalid*");
17767 goto tail_recurse;
17768 }
17769
17770 /* Pad to FIELD_WIDTH. */
17771 if (field_width > 0 && n < field_width)
17772 {
17773 switch (mode_line_target)
17774 {
17775 case MODE_LINE_NOPROP:
17776 case MODE_LINE_TITLE:
17777 n += store_mode_line_noprop ("", field_width - n, 0);
17778 break;
17779 case MODE_LINE_STRING:
17780 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17781 break;
17782 case MODE_LINE_DISPLAY:
17783 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17784 0, 0, 0);
17785 break;
17786 }
17787 }
17788
17789 return n;
17790 }
17791
17792 /* Store a mode-line string element in mode_line_string_list.
17793
17794 If STRING is non-null, display that C string. Otherwise, the Lisp
17795 string LISP_STRING is displayed.
17796
17797 FIELD_WIDTH is the minimum number of output glyphs to produce.
17798 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17799 with spaces. FIELD_WIDTH <= 0 means don't pad.
17800
17801 PRECISION is the maximum number of characters to output from
17802 STRING. PRECISION <= 0 means don't truncate the string.
17803
17804 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17805 properties to the string.
17806
17807 PROPS are the properties to add to the string.
17808 The mode_line_string_face face property is always added to the string.
17809 */
17810
17811 static int
17812 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17813 char *string;
17814 Lisp_Object lisp_string;
17815 int copy_string;
17816 int field_width;
17817 int precision;
17818 Lisp_Object props;
17819 {
17820 int len;
17821 int n = 0;
17822
17823 if (string != NULL)
17824 {
17825 len = strlen (string);
17826 if (precision > 0 && len > precision)
17827 len = precision;
17828 lisp_string = make_string (string, len);
17829 if (NILP (props))
17830 props = mode_line_string_face_prop;
17831 else if (!NILP (mode_line_string_face))
17832 {
17833 Lisp_Object face = Fplist_get (props, Qface);
17834 props = Fcopy_sequence (props);
17835 if (NILP (face))
17836 face = mode_line_string_face;
17837 else
17838 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17839 props = Fplist_put (props, Qface, face);
17840 }
17841 Fadd_text_properties (make_number (0), make_number (len),
17842 props, lisp_string);
17843 }
17844 else
17845 {
17846 len = XFASTINT (Flength (lisp_string));
17847 if (precision > 0 && len > precision)
17848 {
17849 len = precision;
17850 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17851 precision = -1;
17852 }
17853 if (!NILP (mode_line_string_face))
17854 {
17855 Lisp_Object face;
17856 if (NILP (props))
17857 props = Ftext_properties_at (make_number (0), lisp_string);
17858 face = Fplist_get (props, Qface);
17859 if (NILP (face))
17860 face = mode_line_string_face;
17861 else
17862 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17863 props = Fcons (Qface, Fcons (face, Qnil));
17864 if (copy_string)
17865 lisp_string = Fcopy_sequence (lisp_string);
17866 }
17867 if (!NILP (props))
17868 Fadd_text_properties (make_number (0), make_number (len),
17869 props, lisp_string);
17870 }
17871
17872 if (len > 0)
17873 {
17874 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17875 n += len;
17876 }
17877
17878 if (field_width > len)
17879 {
17880 field_width -= len;
17881 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17882 if (!NILP (props))
17883 Fadd_text_properties (make_number (0), make_number (field_width),
17884 props, lisp_string);
17885 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17886 n += field_width;
17887 }
17888
17889 return n;
17890 }
17891
17892
17893 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17894 1, 4, 0,
17895 doc: /* Format a string out of a mode line format specification.
17896 First arg FORMAT specifies the mode line format (see `mode-line-format'
17897 for details) to use.
17898
17899 Optional second arg FACE specifies the face property to put
17900 on all characters for which no face is specified.
17901 The value t means whatever face the window's mode line currently uses
17902 \(either `mode-line' or `mode-line-inactive', depending).
17903 A value of nil means the default is no face property.
17904 If FACE is an integer, the value string has no text properties.
17905
17906 Optional third and fourth args WINDOW and BUFFER specify the window
17907 and buffer to use as the context for the formatting (defaults
17908 are the selected window and the window's buffer). */)
17909 (format, face, window, buffer)
17910 Lisp_Object format, face, window, buffer;
17911 {
17912 struct it it;
17913 int len;
17914 struct window *w;
17915 struct buffer *old_buffer = NULL;
17916 int face_id = -1;
17917 int no_props = INTEGERP (face);
17918 int count = SPECPDL_INDEX ();
17919 Lisp_Object str;
17920 int string_start = 0;
17921
17922 if (NILP (window))
17923 window = selected_window;
17924 CHECK_WINDOW (window);
17925 w = XWINDOW (window);
17926
17927 if (NILP (buffer))
17928 buffer = w->buffer;
17929 CHECK_BUFFER (buffer);
17930
17931 /* Make formatting the modeline a non-op when noninteractive, otherwise
17932 there will be problems later caused by a partially initialized frame. */
17933 if (NILP (format) || noninteractive)
17934 return empty_unibyte_string;
17935
17936 if (no_props)
17937 face = Qnil;
17938
17939 if (!NILP (face))
17940 {
17941 if (EQ (face, Qt))
17942 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17943 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0);
17944 }
17945
17946 if (face_id < 0)
17947 face_id = DEFAULT_FACE_ID;
17948
17949 if (XBUFFER (buffer) != current_buffer)
17950 old_buffer = current_buffer;
17951
17952 /* Save things including mode_line_proptrans_alist,
17953 and set that to nil so that we don't alter the outer value. */
17954 record_unwind_protect (unwind_format_mode_line,
17955 format_mode_line_unwind_data
17956 (old_buffer, selected_window, 1));
17957 mode_line_proptrans_alist = Qnil;
17958
17959 Fselect_window (window, Qt);
17960 if (old_buffer)
17961 set_buffer_internal_1 (XBUFFER (buffer));
17962
17963 init_iterator (&it, w, -1, -1, NULL, face_id);
17964
17965 if (no_props)
17966 {
17967 mode_line_target = MODE_LINE_NOPROP;
17968 mode_line_string_face_prop = Qnil;
17969 mode_line_string_list = Qnil;
17970 string_start = MODE_LINE_NOPROP_LEN (0);
17971 }
17972 else
17973 {
17974 mode_line_target = MODE_LINE_STRING;
17975 mode_line_string_list = Qnil;
17976 mode_line_string_face = face;
17977 mode_line_string_face_prop
17978 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17979 }
17980
17981 push_kboard (FRAME_KBOARD (it.f));
17982 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17983 pop_kboard ();
17984
17985 if (no_props)
17986 {
17987 len = MODE_LINE_NOPROP_LEN (string_start);
17988 str = make_string (mode_line_noprop_buf + string_start, len);
17989 }
17990 else
17991 {
17992 mode_line_string_list = Fnreverse (mode_line_string_list);
17993 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17994 empty_unibyte_string);
17995 }
17996
17997 unbind_to (count, Qnil);
17998 return str;
17999 }
18000
18001 /* Write a null-terminated, right justified decimal representation of
18002 the positive integer D to BUF using a minimal field width WIDTH. */
18003
18004 static void
18005 pint2str (buf, width, d)
18006 register char *buf;
18007 register int width;
18008 register int d;
18009 {
18010 register char *p = buf;
18011
18012 if (d <= 0)
18013 *p++ = '0';
18014 else
18015 {
18016 while (d > 0)
18017 {
18018 *p++ = d % 10 + '0';
18019 d /= 10;
18020 }
18021 }
18022
18023 for (width -= (int) (p - buf); width > 0; --width)
18024 *p++ = ' ';
18025 *p-- = '\0';
18026 while (p > buf)
18027 {
18028 d = *buf;
18029 *buf++ = *p;
18030 *p-- = d;
18031 }
18032 }
18033
18034 /* Write a null-terminated, right justified decimal and "human
18035 readable" representation of the nonnegative integer D to BUF using
18036 a minimal field width WIDTH. D should be smaller than 999.5e24. */
18037
18038 static const char power_letter[] =
18039 {
18040 0, /* not used */
18041 'k', /* kilo */
18042 'M', /* mega */
18043 'G', /* giga */
18044 'T', /* tera */
18045 'P', /* peta */
18046 'E', /* exa */
18047 'Z', /* zetta */
18048 'Y' /* yotta */
18049 };
18050
18051 static void
18052 pint2hrstr (buf, width, d)
18053 char *buf;
18054 int width;
18055 int d;
18056 {
18057 /* We aim to represent the nonnegative integer D as
18058 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
18059 int quotient = d;
18060 int remainder = 0;
18061 /* -1 means: do not use TENTHS. */
18062 int tenths = -1;
18063 int exponent = 0;
18064
18065 /* Length of QUOTIENT.TENTHS as a string. */
18066 int length;
18067
18068 char * psuffix;
18069 char * p;
18070
18071 if (1000 <= quotient)
18072 {
18073 /* Scale to the appropriate EXPONENT. */
18074 do
18075 {
18076 remainder = quotient % 1000;
18077 quotient /= 1000;
18078 exponent++;
18079 }
18080 while (1000 <= quotient);
18081
18082 /* Round to nearest and decide whether to use TENTHS or not. */
18083 if (quotient <= 9)
18084 {
18085 tenths = remainder / 100;
18086 if (50 <= remainder % 100)
18087 {
18088 if (tenths < 9)
18089 tenths++;
18090 else
18091 {
18092 quotient++;
18093 if (quotient == 10)
18094 tenths = -1;
18095 else
18096 tenths = 0;
18097 }
18098 }
18099 }
18100 else
18101 if (500 <= remainder)
18102 {
18103 if (quotient < 999)
18104 quotient++;
18105 else
18106 {
18107 quotient = 1;
18108 exponent++;
18109 tenths = 0;
18110 }
18111 }
18112 }
18113
18114 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
18115 if (tenths == -1 && quotient <= 99)
18116 if (quotient <= 9)
18117 length = 1;
18118 else
18119 length = 2;
18120 else
18121 length = 3;
18122 p = psuffix = buf + max (width, length);
18123
18124 /* Print EXPONENT. */
18125 if (exponent)
18126 *psuffix++ = power_letter[exponent];
18127 *psuffix = '\0';
18128
18129 /* Print TENTHS. */
18130 if (tenths >= 0)
18131 {
18132 *--p = '0' + tenths;
18133 *--p = '.';
18134 }
18135
18136 /* Print QUOTIENT. */
18137 do
18138 {
18139 int digit = quotient % 10;
18140 *--p = '0' + digit;
18141 }
18142 while ((quotient /= 10) != 0);
18143
18144 /* Print leading spaces. */
18145 while (buf < p)
18146 *--p = ' ';
18147 }
18148
18149 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
18150 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
18151 type of CODING_SYSTEM. Return updated pointer into BUF. */
18152
18153 static unsigned char invalid_eol_type[] = "(*invalid*)";
18154
18155 static char *
18156 decode_mode_spec_coding (coding_system, buf, eol_flag)
18157 Lisp_Object coding_system;
18158 register char *buf;
18159 int eol_flag;
18160 {
18161 Lisp_Object val;
18162 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
18163 const unsigned char *eol_str;
18164 int eol_str_len;
18165 /* The EOL conversion we are using. */
18166 Lisp_Object eoltype;
18167
18168 val = CODING_SYSTEM_SPEC (coding_system);
18169 eoltype = Qnil;
18170
18171 if (!VECTORP (val)) /* Not yet decided. */
18172 {
18173 if (multibyte)
18174 *buf++ = '-';
18175 if (eol_flag)
18176 eoltype = eol_mnemonic_undecided;
18177 /* Don't mention EOL conversion if it isn't decided. */
18178 }
18179 else
18180 {
18181 Lisp_Object attrs;
18182 Lisp_Object eolvalue;
18183
18184 attrs = AREF (val, 0);
18185 eolvalue = AREF (val, 2);
18186
18187 if (multibyte)
18188 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
18189
18190 if (eol_flag)
18191 {
18192 /* The EOL conversion that is normal on this system. */
18193
18194 if (NILP (eolvalue)) /* Not yet decided. */
18195 eoltype = eol_mnemonic_undecided;
18196 else if (VECTORP (eolvalue)) /* Not yet decided. */
18197 eoltype = eol_mnemonic_undecided;
18198 else /* eolvalue is Qunix, Qdos, or Qmac. */
18199 eoltype = (EQ (eolvalue, Qunix)
18200 ? eol_mnemonic_unix
18201 : (EQ (eolvalue, Qdos) == 1
18202 ? eol_mnemonic_dos : eol_mnemonic_mac));
18203 }
18204 }
18205
18206 if (eol_flag)
18207 {
18208 /* Mention the EOL conversion if it is not the usual one. */
18209 if (STRINGP (eoltype))
18210 {
18211 eol_str = SDATA (eoltype);
18212 eol_str_len = SBYTES (eoltype);
18213 }
18214 else if (CHARACTERP (eoltype))
18215 {
18216 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
18217 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
18218 eol_str = tmp;
18219 }
18220 else
18221 {
18222 eol_str = invalid_eol_type;
18223 eol_str_len = sizeof (invalid_eol_type) - 1;
18224 }
18225 bcopy (eol_str, buf, eol_str_len);
18226 buf += eol_str_len;
18227 }
18228
18229 return buf;
18230 }
18231
18232 /* Return a string for the output of a mode line %-spec for window W,
18233 generated by character C. PRECISION >= 0 means don't return a
18234 string longer than that value. FIELD_WIDTH > 0 means pad the
18235 string returned with spaces to that value. Return 1 in *MULTIBYTE
18236 if the result is multibyte text.
18237
18238 Note we operate on the current buffer for most purposes,
18239 the exception being w->base_line_pos. */
18240
18241 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
18242
18243 static char *
18244 decode_mode_spec (w, c, field_width, precision, multibyte)
18245 struct window *w;
18246 register int c;
18247 int field_width, precision;
18248 int *multibyte;
18249 {
18250 Lisp_Object obj;
18251 struct frame *f = XFRAME (WINDOW_FRAME (w));
18252 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
18253 struct buffer *b = current_buffer;
18254
18255 obj = Qnil;
18256 *multibyte = 0;
18257
18258 switch (c)
18259 {
18260 case '*':
18261 if (!NILP (b->read_only))
18262 return "%";
18263 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18264 return "*";
18265 return "-";
18266
18267 case '+':
18268 /* This differs from %* only for a modified read-only buffer. */
18269 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18270 return "*";
18271 if (!NILP (b->read_only))
18272 return "%";
18273 return "-";
18274
18275 case '&':
18276 /* This differs from %* in ignoring read-only-ness. */
18277 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
18278 return "*";
18279 return "-";
18280
18281 case '%':
18282 return "%";
18283
18284 case '[':
18285 {
18286 int i;
18287 char *p;
18288
18289 if (command_loop_level > 5)
18290 return "[[[... ";
18291 p = decode_mode_spec_buf;
18292 for (i = 0; i < command_loop_level; i++)
18293 *p++ = '[';
18294 *p = 0;
18295 return decode_mode_spec_buf;
18296 }
18297
18298 case ']':
18299 {
18300 int i;
18301 char *p;
18302
18303 if (command_loop_level > 5)
18304 return " ...]]]";
18305 p = decode_mode_spec_buf;
18306 for (i = 0; i < command_loop_level; i++)
18307 *p++ = ']';
18308 *p = 0;
18309 return decode_mode_spec_buf;
18310 }
18311
18312 case '-':
18313 {
18314 register int i;
18315
18316 /* Let lots_of_dashes be a string of infinite length. */
18317 if (mode_line_target == MODE_LINE_NOPROP ||
18318 mode_line_target == MODE_LINE_STRING)
18319 return "--";
18320 if (field_width <= 0
18321 || field_width > sizeof (lots_of_dashes))
18322 {
18323 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
18324 decode_mode_spec_buf[i] = '-';
18325 decode_mode_spec_buf[i] = '\0';
18326 return decode_mode_spec_buf;
18327 }
18328 else
18329 return lots_of_dashes;
18330 }
18331
18332 case 'b':
18333 obj = b->name;
18334 break;
18335
18336 case 'c':
18337 /* %c and %l are ignored in `frame-title-format'.
18338 (In redisplay_internal, the frame title is drawn _before_ the
18339 windows are updated, so the stuff which depends on actual
18340 window contents (such as %l) may fail to render properly, or
18341 even crash emacs.) */
18342 if (mode_line_target == MODE_LINE_TITLE)
18343 return "";
18344 else
18345 {
18346 int col = (int) current_column (); /* iftc */
18347 w->column_number_displayed = make_number (col);
18348 pint2str (decode_mode_spec_buf, field_width, col);
18349 return decode_mode_spec_buf;
18350 }
18351
18352 case 'e':
18353 #ifndef SYSTEM_MALLOC
18354 {
18355 if (NILP (Vmemory_full))
18356 return "";
18357 else
18358 return "!MEM FULL! ";
18359 }
18360 #else
18361 return "";
18362 #endif
18363
18364 case 'F':
18365 /* %F displays the frame name. */
18366 if (!NILP (f->title))
18367 return (char *) SDATA (f->title);
18368 if (f->explicit_name || ! FRAME_WINDOW_P (f))
18369 return (char *) SDATA (f->name);
18370 return "Emacs";
18371
18372 case 'f':
18373 obj = b->filename;
18374 break;
18375
18376 case 'i':
18377 {
18378 int size = ZV - BEGV;
18379 pint2str (decode_mode_spec_buf, field_width, size);
18380 return decode_mode_spec_buf;
18381 }
18382
18383 case 'I':
18384 {
18385 int size = ZV - BEGV;
18386 pint2hrstr (decode_mode_spec_buf, field_width, size);
18387 return decode_mode_spec_buf;
18388 }
18389
18390 case 'l':
18391 {
18392 int startpos, startpos_byte, line, linepos, linepos_byte;
18393 int topline, nlines, junk, height;
18394
18395 /* %c and %l are ignored in `frame-title-format'. */
18396 if (mode_line_target == MODE_LINE_TITLE)
18397 return "";
18398
18399 startpos = XMARKER (w->start)->charpos;
18400 startpos_byte = marker_byte_position (w->start);
18401 height = WINDOW_TOTAL_LINES (w);
18402
18403 /* If we decided that this buffer isn't suitable for line numbers,
18404 don't forget that too fast. */
18405 if (EQ (w->base_line_pos, w->buffer))
18406 goto no_value;
18407 /* But do forget it, if the window shows a different buffer now. */
18408 else if (BUFFERP (w->base_line_pos))
18409 w->base_line_pos = Qnil;
18410
18411 /* If the buffer is very big, don't waste time. */
18412 if (INTEGERP (Vline_number_display_limit)
18413 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
18414 {
18415 w->base_line_pos = Qnil;
18416 w->base_line_number = Qnil;
18417 goto no_value;
18418 }
18419
18420 if (INTEGERP (w->base_line_number)
18421 && INTEGERP (w->base_line_pos)
18422 && XFASTINT (w->base_line_pos) <= startpos)
18423 {
18424 line = XFASTINT (w->base_line_number);
18425 linepos = XFASTINT (w->base_line_pos);
18426 linepos_byte = buf_charpos_to_bytepos (b, linepos);
18427 }
18428 else
18429 {
18430 line = 1;
18431 linepos = BUF_BEGV (b);
18432 linepos_byte = BUF_BEGV_BYTE (b);
18433 }
18434
18435 /* Count lines from base line to window start position. */
18436 nlines = display_count_lines (linepos, linepos_byte,
18437 startpos_byte,
18438 startpos, &junk);
18439
18440 topline = nlines + line;
18441
18442 /* Determine a new base line, if the old one is too close
18443 or too far away, or if we did not have one.
18444 "Too close" means it's plausible a scroll-down would
18445 go back past it. */
18446 if (startpos == BUF_BEGV (b))
18447 {
18448 w->base_line_number = make_number (topline);
18449 w->base_line_pos = make_number (BUF_BEGV (b));
18450 }
18451 else if (nlines < height + 25 || nlines > height * 3 + 50
18452 || linepos == BUF_BEGV (b))
18453 {
18454 int limit = BUF_BEGV (b);
18455 int limit_byte = BUF_BEGV_BYTE (b);
18456 int position;
18457 int distance = (height * 2 + 30) * line_number_display_limit_width;
18458
18459 if (startpos - distance > limit)
18460 {
18461 limit = startpos - distance;
18462 limit_byte = CHAR_TO_BYTE (limit);
18463 }
18464
18465 nlines = display_count_lines (startpos, startpos_byte,
18466 limit_byte,
18467 - (height * 2 + 30),
18468 &position);
18469 /* If we couldn't find the lines we wanted within
18470 line_number_display_limit_width chars per line,
18471 give up on line numbers for this window. */
18472 if (position == limit_byte && limit == startpos - distance)
18473 {
18474 w->base_line_pos = w->buffer;
18475 w->base_line_number = Qnil;
18476 goto no_value;
18477 }
18478
18479 w->base_line_number = make_number (topline - nlines);
18480 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
18481 }
18482
18483 /* Now count lines from the start pos to point. */
18484 nlines = display_count_lines (startpos, startpos_byte,
18485 PT_BYTE, PT, &junk);
18486
18487 /* Record that we did display the line number. */
18488 line_number_displayed = 1;
18489
18490 /* Make the string to show. */
18491 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
18492 return decode_mode_spec_buf;
18493 no_value:
18494 {
18495 char* p = decode_mode_spec_buf;
18496 int pad = field_width - 2;
18497 while (pad-- > 0)
18498 *p++ = ' ';
18499 *p++ = '?';
18500 *p++ = '?';
18501 *p = '\0';
18502 return decode_mode_spec_buf;
18503 }
18504 }
18505 break;
18506
18507 case 'm':
18508 obj = b->mode_name;
18509 break;
18510
18511 case 'n':
18512 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
18513 return " Narrow";
18514 break;
18515
18516 case 'p':
18517 {
18518 int pos = marker_position (w->start);
18519 int total = BUF_ZV (b) - BUF_BEGV (b);
18520
18521 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
18522 {
18523 if (pos <= BUF_BEGV (b))
18524 return "All";
18525 else
18526 return "Bottom";
18527 }
18528 else if (pos <= BUF_BEGV (b))
18529 return "Top";
18530 else
18531 {
18532 if (total > 1000000)
18533 /* Do it differently for a large value, to avoid overflow. */
18534 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18535 else
18536 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
18537 /* We can't normally display a 3-digit number,
18538 so get us a 2-digit number that is close. */
18539 if (total == 100)
18540 total = 99;
18541 sprintf (decode_mode_spec_buf, "%2d%%", total);
18542 return decode_mode_spec_buf;
18543 }
18544 }
18545
18546 /* Display percentage of size above the bottom of the screen. */
18547 case 'P':
18548 {
18549 int toppos = marker_position (w->start);
18550 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
18551 int total = BUF_ZV (b) - BUF_BEGV (b);
18552
18553 if (botpos >= BUF_ZV (b))
18554 {
18555 if (toppos <= BUF_BEGV (b))
18556 return "All";
18557 else
18558 return "Bottom";
18559 }
18560 else
18561 {
18562 if (total > 1000000)
18563 /* Do it differently for a large value, to avoid overflow. */
18564 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
18565 else
18566 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
18567 /* We can't normally display a 3-digit number,
18568 so get us a 2-digit number that is close. */
18569 if (total == 100)
18570 total = 99;
18571 if (toppos <= BUF_BEGV (b))
18572 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
18573 else
18574 sprintf (decode_mode_spec_buf, "%2d%%", total);
18575 return decode_mode_spec_buf;
18576 }
18577 }
18578
18579 case 's':
18580 /* status of process */
18581 obj = Fget_buffer_process (Fcurrent_buffer ());
18582 if (NILP (obj))
18583 return "no process";
18584 #ifdef subprocesses
18585 obj = Fsymbol_name (Fprocess_status (obj));
18586 #endif
18587 break;
18588
18589 case '@':
18590 {
18591 Lisp_Object val;
18592 val = call1 (intern ("file-remote-p"), current_buffer->directory);
18593 if (NILP (val))
18594 return "-";
18595 else
18596 return "@";
18597 }
18598
18599 case 't': /* indicate TEXT or BINARY */
18600 #ifdef MODE_LINE_BINARY_TEXT
18601 return MODE_LINE_BINARY_TEXT (b);
18602 #else
18603 return "T";
18604 #endif
18605
18606 case 'z':
18607 /* coding-system (not including end-of-line format) */
18608 case 'Z':
18609 /* coding-system (including end-of-line type) */
18610 {
18611 int eol_flag = (c == 'Z');
18612 char *p = decode_mode_spec_buf;
18613
18614 if (! FRAME_WINDOW_P (f))
18615 {
18616 /* No need to mention EOL here--the terminal never needs
18617 to do EOL conversion. */
18618 p = decode_mode_spec_coding (CODING_ID_NAME
18619 (FRAME_KEYBOARD_CODING (f)->id),
18620 p, 0);
18621 p = decode_mode_spec_coding (CODING_ID_NAME
18622 (FRAME_TERMINAL_CODING (f)->id),
18623 p, 0);
18624 }
18625 p = decode_mode_spec_coding (b->buffer_file_coding_system,
18626 p, eol_flag);
18627
18628 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
18629 #ifdef subprocesses
18630 obj = Fget_buffer_process (Fcurrent_buffer ());
18631 if (PROCESSP (obj))
18632 {
18633 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
18634 p, eol_flag);
18635 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
18636 p, eol_flag);
18637 }
18638 #endif /* subprocesses */
18639 #endif /* 0 */
18640 *p = 0;
18641 return decode_mode_spec_buf;
18642 }
18643 }
18644
18645 if (STRINGP (obj))
18646 {
18647 *multibyte = STRING_MULTIBYTE (obj);
18648 return (char *) SDATA (obj);
18649 }
18650 else
18651 return "";
18652 }
18653
18654
18655 /* Count up to COUNT lines starting from START / START_BYTE.
18656 But don't go beyond LIMIT_BYTE.
18657 Return the number of lines thus found (always nonnegative).
18658
18659 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
18660
18661 static int
18662 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
18663 int start, start_byte, limit_byte, count;
18664 int *byte_pos_ptr;
18665 {
18666 register unsigned char *cursor;
18667 unsigned char *base;
18668
18669 register int ceiling;
18670 register unsigned char *ceiling_addr;
18671 int orig_count = count;
18672
18673 /* If we are not in selective display mode,
18674 check only for newlines. */
18675 int selective_display = (!NILP (current_buffer->selective_display)
18676 && !INTEGERP (current_buffer->selective_display));
18677
18678 if (count > 0)
18679 {
18680 while (start_byte < limit_byte)
18681 {
18682 ceiling = BUFFER_CEILING_OF (start_byte);
18683 ceiling = min (limit_byte - 1, ceiling);
18684 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18685 base = (cursor = BYTE_POS_ADDR (start_byte));
18686 while (1)
18687 {
18688 if (selective_display)
18689 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18690 ;
18691 else
18692 while (*cursor != '\n' && ++cursor != ceiling_addr)
18693 ;
18694
18695 if (cursor != ceiling_addr)
18696 {
18697 if (--count == 0)
18698 {
18699 start_byte += cursor - base + 1;
18700 *byte_pos_ptr = start_byte;
18701 return orig_count;
18702 }
18703 else
18704 if (++cursor == ceiling_addr)
18705 break;
18706 }
18707 else
18708 break;
18709 }
18710 start_byte += cursor - base;
18711 }
18712 }
18713 else
18714 {
18715 while (start_byte > limit_byte)
18716 {
18717 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18718 ceiling = max (limit_byte, ceiling);
18719 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18720 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18721 while (1)
18722 {
18723 if (selective_display)
18724 while (--cursor != ceiling_addr
18725 && *cursor != '\n' && *cursor != 015)
18726 ;
18727 else
18728 while (--cursor != ceiling_addr && *cursor != '\n')
18729 ;
18730
18731 if (cursor != ceiling_addr)
18732 {
18733 if (++count == 0)
18734 {
18735 start_byte += cursor - base + 1;
18736 *byte_pos_ptr = start_byte;
18737 /* When scanning backwards, we should
18738 not count the newline posterior to which we stop. */
18739 return - orig_count - 1;
18740 }
18741 }
18742 else
18743 break;
18744 }
18745 /* Here we add 1 to compensate for the last decrement
18746 of CURSOR, which took it past the valid range. */
18747 start_byte += cursor - base + 1;
18748 }
18749 }
18750
18751 *byte_pos_ptr = limit_byte;
18752
18753 if (count < 0)
18754 return - orig_count + count;
18755 return orig_count - count;
18756
18757 }
18758
18759
18760 \f
18761 /***********************************************************************
18762 Displaying strings
18763 ***********************************************************************/
18764
18765 /* Display a NUL-terminated string, starting with index START.
18766
18767 If STRING is non-null, display that C string. Otherwise, the Lisp
18768 string LISP_STRING is displayed.
18769
18770 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18771 FACE_STRING. Display STRING or LISP_STRING with the face at
18772 FACE_STRING_POS in FACE_STRING:
18773
18774 Display the string in the environment given by IT, but use the
18775 standard display table, temporarily.
18776
18777 FIELD_WIDTH is the minimum number of output glyphs to produce.
18778 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18779 with spaces. If STRING has more characters, more than FIELD_WIDTH
18780 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18781
18782 PRECISION is the maximum number of characters to output from
18783 STRING. PRECISION < 0 means don't truncate the string.
18784
18785 This is roughly equivalent to printf format specifiers:
18786
18787 FIELD_WIDTH PRECISION PRINTF
18788 ----------------------------------------
18789 -1 -1 %s
18790 -1 10 %.10s
18791 10 -1 %10s
18792 20 10 %20.10s
18793
18794 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18795 display them, and < 0 means obey the current buffer's value of
18796 enable_multibyte_characters.
18797
18798 Value is the number of columns displayed. */
18799
18800 static int
18801 display_string (string, lisp_string, face_string, face_string_pos,
18802 start, it, field_width, precision, max_x, multibyte)
18803 unsigned char *string;
18804 Lisp_Object lisp_string;
18805 Lisp_Object face_string;
18806 EMACS_INT face_string_pos;
18807 EMACS_INT start;
18808 struct it *it;
18809 int field_width, precision, max_x;
18810 int multibyte;
18811 {
18812 int hpos_at_start = it->hpos;
18813 int saved_face_id = it->face_id;
18814 struct glyph_row *row = it->glyph_row;
18815
18816 /* Initialize the iterator IT for iteration over STRING beginning
18817 with index START. */
18818 reseat_to_string (it, string, lisp_string, start,
18819 precision, field_width, multibyte);
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
19384 /* Display the internal border below the tool-bar window. */
19385 if (WINDOWP (s->f->tool_bar_window)
19386 && s->w == XWINDOW (s->f->tool_bar_window))
19387 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
19388
19389 s->ybase = s->y + row->ascent;
19390 }
19391
19392
19393 /* Append the list of glyph strings with head H and tail T to the list
19394 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
19395
19396 static INLINE void
19397 append_glyph_string_lists (head, tail, h, t)
19398 struct glyph_string **head, **tail;
19399 struct glyph_string *h, *t;
19400 {
19401 if (h)
19402 {
19403 if (*head)
19404 (*tail)->next = h;
19405 else
19406 *head = h;
19407 h->prev = *tail;
19408 *tail = t;
19409 }
19410 }
19411
19412
19413 /* Prepend the list of glyph strings with head H and tail T to the
19414 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
19415 result. */
19416
19417 static INLINE void
19418 prepend_glyph_string_lists (head, tail, h, t)
19419 struct glyph_string **head, **tail;
19420 struct glyph_string *h, *t;
19421 {
19422 if (h)
19423 {
19424 if (*head)
19425 (*head)->prev = t;
19426 else
19427 *tail = t;
19428 t->next = *head;
19429 *head = h;
19430 }
19431 }
19432
19433
19434 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
19435 Set *HEAD and *TAIL to the resulting list. */
19436
19437 static INLINE void
19438 append_glyph_string (head, tail, s)
19439 struct glyph_string **head, **tail;
19440 struct glyph_string *s;
19441 {
19442 s->next = s->prev = NULL;
19443 append_glyph_string_lists (head, tail, s, s);
19444 }
19445
19446
19447 /* Get face and two-byte form of character C in face FACE_ID on frame
19448 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19449 means we want to display multibyte text. DISPLAY_P non-zero means
19450 make sure that X resources for the face returned are allocated.
19451 Value is a pointer to a realized face that is ready for display if
19452 DISPLAY_P is non-zero. */
19453
19454 static INLINE struct face *
19455 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19456 struct frame *f;
19457 int c, face_id;
19458 XChar2b *char2b;
19459 int multibyte_p, display_p;
19460 {
19461 struct face *face = FACE_FROM_ID (f, face_id);
19462
19463 if (face->font)
19464 {
19465 unsigned code = face->font->driver->encode_char (face->font, c);
19466
19467 if (code != FONT_INVALID_CODE)
19468 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19469 else
19470 STORE_XCHAR2B (char2b, 0, 0);
19471 }
19472
19473 /* Make sure X resources of the face are allocated. */
19474 #ifdef HAVE_X_WINDOWS
19475 if (display_p)
19476 #endif
19477 {
19478 xassert (face != NULL);
19479 PREPARE_FACE_FOR_DISPLAY (f, face);
19480 }
19481
19482 return face;
19483 }
19484
19485
19486 /* Get face and two-byte form of character glyph GLYPH on frame F.
19487 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
19488 a pointer to a realized face that is ready for display. */
19489
19490 static INLINE struct face *
19491 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
19492 struct frame *f;
19493 struct glyph *glyph;
19494 XChar2b *char2b;
19495 int *two_byte_p;
19496 {
19497 struct face *face;
19498
19499 xassert (glyph->type == CHAR_GLYPH);
19500 face = FACE_FROM_ID (f, glyph->face_id);
19501
19502 if (two_byte_p)
19503 *two_byte_p = 0;
19504
19505 if (face->font)
19506 {
19507 unsigned code = face->font->driver->encode_char (face->font, glyph->u.ch);
19508
19509 if (code != FONT_INVALID_CODE)
19510 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
19511 else
19512 STORE_XCHAR2B (char2b, 0, 0);
19513 }
19514
19515 /* Make sure X resources of the face are allocated. */
19516 xassert (face != NULL);
19517 PREPARE_FACE_FOR_DISPLAY (f, face);
19518 return face;
19519 }
19520
19521
19522 /* Fill glyph string S with composition components specified by S->cmp.
19523
19524 BASE_FACE is the base face of the composition.
19525 S->gidx is the index of the first component for S.
19526
19527 OVERLAPS non-zero means S should draw the foreground only, and use
19528 its physical height for clipping. See also draw_glyphs.
19529
19530 Value is the index of a component not in S. */
19531
19532 static int
19533 fill_composite_glyph_string (s, base_face, overlaps)
19534 struct glyph_string *s;
19535 struct face *base_face;
19536 int overlaps;
19537 {
19538 int i;
19539
19540 xassert (s);
19541
19542 s->for_overlaps = overlaps;
19543
19544 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
19545 {
19546 Lisp_Object gstring
19547 = AREF (XHASH_TABLE (composition_hash_table)->key_and_value,
19548 s->cmp->hash_index * 2);
19549
19550 s->face = base_face;
19551 s->font = base_face->font;
19552 for (i = 0, s->nchars = 0; i < s->cmp->glyph_len; i++, s->nchars++)
19553 {
19554 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
19555 unsigned code;
19556 XChar2b * store_pos;
19557 if (NILP (g))
19558 break;
19559 code = LGLYPH_CODE (g);
19560 store_pos = s->char2b + i;
19561 STORE_XCHAR2B (store_pos, code >> 8, code & 0xFF);
19562 }
19563 s->width = s->cmp->pixel_width;
19564 }
19565 else
19566 {
19567 /* For all glyphs of this composition, starting at the offset
19568 S->gidx, until we reach the end of the definition or encounter a
19569 glyph that requires the different face, add it to S. */
19570 struct face *face;
19571
19572 s->face = NULL;
19573 s->font = NULL;
19574 for (i = s->gidx; i < s->cmp->glyph_len; i++)
19575 {
19576 int c = COMPOSITION_GLYPH (s->cmp, i);
19577
19578 if (c != '\t')
19579 {
19580 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
19581 -1, Qnil);
19582
19583 face = get_char_face_and_encoding (s->f, c, face_id,
19584 s->char2b + i, 1, 1);
19585 if (face)
19586 {
19587 if (! s->face)
19588 {
19589 s->face = face;
19590 s->font = s->face->font;
19591 }
19592 else if (s->face != face)
19593 break;
19594 }
19595 }
19596 ++s->nchars;
19597 }
19598
19599 /* All glyph strings for the same composition has the same width,
19600 i.e. the width set for the first component of the composition. */
19601 s->width = s->first_glyph->pixel_width;
19602 }
19603
19604 /* If the specified font could not be loaded, use the frame's
19605 default font, but record the fact that we couldn't load it in
19606 the glyph string so that we can draw rectangles for the
19607 characters of the glyph string. */
19608 if (s->font == NULL)
19609 {
19610 s->font_not_found_p = 1;
19611 s->font = FRAME_FONT (s->f);
19612 }
19613
19614 /* Adjust base line for subscript/superscript text. */
19615 s->ybase += s->first_glyph->voffset;
19616
19617 /* This glyph string must always be drawn with 16-bit functions. */
19618 s->two_byte_p = 1;
19619
19620 return s->gidx + s->nchars;
19621 }
19622
19623
19624 /* Fill glyph string S from a sequence of character glyphs.
19625
19626 FACE_ID is the face id of the string. START is the index of the
19627 first glyph to consider, END is the index of the last + 1.
19628 OVERLAPS non-zero means S should draw the foreground only, and use
19629 its physical height for clipping. See also draw_glyphs.
19630
19631 Value is the index of the first glyph not in S. */
19632
19633 static int
19634 fill_glyph_string (s, face_id, start, end, overlaps)
19635 struct glyph_string *s;
19636 int face_id;
19637 int start, end, overlaps;
19638 {
19639 struct glyph *glyph, *last;
19640 int voffset;
19641 int glyph_not_available_p;
19642
19643 xassert (s->f == XFRAME (s->w->frame));
19644 xassert (s->nchars == 0);
19645 xassert (start >= 0 && end > start);
19646
19647 s->for_overlaps = overlaps,
19648 glyph = s->row->glyphs[s->area] + start;
19649 last = s->row->glyphs[s->area] + end;
19650 voffset = glyph->voffset;
19651 s->padding_p = glyph->padding_p;
19652 glyph_not_available_p = glyph->glyph_not_available_p;
19653
19654 while (glyph < last
19655 && glyph->type == CHAR_GLYPH
19656 && glyph->voffset == voffset
19657 /* Same face id implies same font, nowadays. */
19658 && glyph->face_id == face_id
19659 && glyph->glyph_not_available_p == glyph_not_available_p)
19660 {
19661 int two_byte_p;
19662
19663 s->face = get_glyph_face_and_encoding (s->f, glyph,
19664 s->char2b + s->nchars,
19665 &two_byte_p);
19666 s->two_byte_p = two_byte_p;
19667 ++s->nchars;
19668 xassert (s->nchars <= end - start);
19669 s->width += glyph->pixel_width;
19670 if (glyph++->padding_p != s->padding_p)
19671 break;
19672 }
19673
19674 s->font = s->face->font;
19675
19676 /* If the specified font could not be loaded, use the frame's font,
19677 but record the fact that we couldn't load it in
19678 S->font_not_found_p so that we can draw rectangles for the
19679 characters of the glyph string. */
19680 if (s->font == NULL || glyph_not_available_p)
19681 {
19682 s->font_not_found_p = 1;
19683 s->font = FRAME_FONT (s->f);
19684 }
19685
19686 /* Adjust base line for subscript/superscript text. */
19687 s->ybase += voffset;
19688
19689 xassert (s->face && s->face->gc);
19690 return glyph - s->row->glyphs[s->area];
19691 }
19692
19693
19694 /* Fill glyph string S from image glyph S->first_glyph. */
19695
19696 static void
19697 fill_image_glyph_string (s)
19698 struct glyph_string *s;
19699 {
19700 xassert (s->first_glyph->type == IMAGE_GLYPH);
19701 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
19702 xassert (s->img);
19703 s->slice = s->first_glyph->slice;
19704 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
19705 s->font = s->face->font;
19706 s->width = s->first_glyph->pixel_width;
19707
19708 /* Adjust base line for subscript/superscript text. */
19709 s->ybase += s->first_glyph->voffset;
19710 }
19711
19712
19713 /* Fill glyph string S from a sequence of stretch glyphs.
19714
19715 ROW is the glyph row in which the glyphs are found, AREA is the
19716 area within the row. START is the index of the first glyph to
19717 consider, END is the index of the last + 1.
19718
19719 Value is the index of the first glyph not in S. */
19720
19721 static int
19722 fill_stretch_glyph_string (s, row, area, start, end)
19723 struct glyph_string *s;
19724 struct glyph_row *row;
19725 enum glyph_row_area area;
19726 int start, end;
19727 {
19728 struct glyph *glyph, *last;
19729 int voffset, face_id;
19730
19731 xassert (s->first_glyph->type == STRETCH_GLYPH);
19732
19733 glyph = s->row->glyphs[s->area] + start;
19734 last = s->row->glyphs[s->area] + end;
19735 face_id = glyph->face_id;
19736 s->face = FACE_FROM_ID (s->f, face_id);
19737 s->font = s->face->font;
19738 s->width = glyph->pixel_width;
19739 s->nchars = 1;
19740 voffset = glyph->voffset;
19741
19742 for (++glyph;
19743 (glyph < last
19744 && glyph->type == STRETCH_GLYPH
19745 && glyph->voffset == voffset
19746 && glyph->face_id == face_id);
19747 ++glyph)
19748 s->width += glyph->pixel_width;
19749
19750 /* Adjust base line for subscript/superscript text. */
19751 s->ybase += voffset;
19752
19753 /* The case that face->gc == 0 is handled when drawing the glyph
19754 string by calling PREPARE_FACE_FOR_DISPLAY. */
19755 xassert (s->face);
19756 return glyph - s->row->glyphs[s->area];
19757 }
19758
19759 static struct font_metrics *
19760 get_per_char_metric (f, font, char2b)
19761 struct frame *f;
19762 struct font *font;
19763 XChar2b *char2b;
19764 {
19765 static struct font_metrics metrics;
19766 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
19767 struct font *fontp;
19768
19769 if (! font || code == FONT_INVALID_CODE)
19770 return NULL;
19771 font->driver->text_extents (font, &code, 1, &metrics);
19772 return &metrics;
19773 }
19774
19775 /* EXPORT for RIF:
19776 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
19777 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
19778 assumed to be zero. */
19779
19780 void
19781 x_get_glyph_overhangs (glyph, f, left, right)
19782 struct glyph *glyph;
19783 struct frame *f;
19784 int *left, *right;
19785 {
19786 *left = *right = 0;
19787
19788 if (glyph->type == CHAR_GLYPH)
19789 {
19790 struct face *face;
19791 XChar2b char2b;
19792 struct font_metrics *pcm;
19793
19794 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19795 if (face->font && (pcm = get_per_char_metric (f, face->font, &char2b)))
19796 {
19797 if (pcm->rbearing > pcm->width)
19798 *right = pcm->rbearing - pcm->width;
19799 if (pcm->lbearing < 0)
19800 *left = -pcm->lbearing;
19801 }
19802 }
19803 else if (glyph->type == COMPOSITE_GLYPH)
19804 {
19805 struct composition *cmp = composition_table[glyph->u.cmp_id];
19806
19807 *right = cmp->rbearing - cmp->pixel_width;
19808 *left = - cmp->lbearing;
19809 }
19810 }
19811
19812
19813 /* Return the index of the first glyph preceding glyph string S that
19814 is overwritten by S because of S's left overhang. Value is -1
19815 if no glyphs are overwritten. */
19816
19817 static int
19818 left_overwritten (s)
19819 struct glyph_string *s;
19820 {
19821 int k;
19822
19823 if (s->left_overhang)
19824 {
19825 int x = 0, i;
19826 struct glyph *glyphs = s->row->glyphs[s->area];
19827 int first = s->first_glyph - glyphs;
19828
19829 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19830 x -= glyphs[i].pixel_width;
19831
19832 k = i + 1;
19833 }
19834 else
19835 k = -1;
19836
19837 return k;
19838 }
19839
19840
19841 /* Return the index of the first glyph preceding glyph string S that
19842 is overwriting S because of its right overhang. Value is -1 if no
19843 glyph in front of S overwrites S. */
19844
19845 static int
19846 left_overwriting (s)
19847 struct glyph_string *s;
19848 {
19849 int i, k, x;
19850 struct glyph *glyphs = s->row->glyphs[s->area];
19851 int first = s->first_glyph - glyphs;
19852
19853 k = -1;
19854 x = 0;
19855 for (i = first - 1; i >= 0; --i)
19856 {
19857 int left, right;
19858 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19859 if (x + right > 0)
19860 k = i;
19861 x -= glyphs[i].pixel_width;
19862 }
19863
19864 return k;
19865 }
19866
19867
19868 /* Return the index of the last glyph following glyph string S that is
19869 not overwritten by S because of S's right overhang. Value is -1 if
19870 no such glyph is found. */
19871
19872 static int
19873 right_overwritten (s)
19874 struct glyph_string *s;
19875 {
19876 int k = -1;
19877
19878 if (s->right_overhang)
19879 {
19880 int x = 0, i;
19881 struct glyph *glyphs = s->row->glyphs[s->area];
19882 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19883 int end = s->row->used[s->area];
19884
19885 for (i = first; i < end && s->right_overhang > x; ++i)
19886 x += glyphs[i].pixel_width;
19887
19888 k = i;
19889 }
19890
19891 return k;
19892 }
19893
19894
19895 /* Return the index of the last glyph following glyph string S that
19896 overwrites S because of its left overhang. Value is negative
19897 if no such glyph is found. */
19898
19899 static int
19900 right_overwriting (s)
19901 struct glyph_string *s;
19902 {
19903 int i, k, x;
19904 int end = s->row->used[s->area];
19905 struct glyph *glyphs = s->row->glyphs[s->area];
19906 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19907
19908 k = -1;
19909 x = 0;
19910 for (i = first; i < end; ++i)
19911 {
19912 int left, right;
19913 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19914 if (x - left < 0)
19915 k = i;
19916 x += glyphs[i].pixel_width;
19917 }
19918
19919 return k;
19920 }
19921
19922
19923 /* Set background width of glyph string S. START is the index of the
19924 first glyph following S. LAST_X is the right-most x-position + 1
19925 in the drawing area. */
19926
19927 static INLINE void
19928 set_glyph_string_background_width (s, start, last_x)
19929 struct glyph_string *s;
19930 int start;
19931 int last_x;
19932 {
19933 /* If the face of this glyph string has to be drawn to the end of
19934 the drawing area, set S->extends_to_end_of_line_p. */
19935
19936 if (start == s->row->used[s->area]
19937 && s->area == TEXT_AREA
19938 && ((s->row->fill_line_p
19939 && (s->hl == DRAW_NORMAL_TEXT
19940 || s->hl == DRAW_IMAGE_RAISED
19941 || s->hl == DRAW_IMAGE_SUNKEN))
19942 || s->hl == DRAW_MOUSE_FACE))
19943 s->extends_to_end_of_line_p = 1;
19944
19945 /* If S extends its face to the end of the line, set its
19946 background_width to the distance to the right edge of the drawing
19947 area. */
19948 if (s->extends_to_end_of_line_p)
19949 s->background_width = last_x - s->x + 1;
19950 else
19951 s->background_width = s->width;
19952 }
19953
19954
19955 /* Compute overhangs and x-positions for glyph string S and its
19956 predecessors, or successors. X is the starting x-position for S.
19957 BACKWARD_P non-zero means process predecessors. */
19958
19959 static void
19960 compute_overhangs_and_x (s, x, backward_p)
19961 struct glyph_string *s;
19962 int x;
19963 int backward_p;
19964 {
19965 if (backward_p)
19966 {
19967 while (s)
19968 {
19969 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19970 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19971 x -= s->width;
19972 s->x = x;
19973 s = s->prev;
19974 }
19975 }
19976 else
19977 {
19978 while (s)
19979 {
19980 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
19981 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
19982 s->x = x;
19983 x += s->width;
19984 s = s->next;
19985 }
19986 }
19987 }
19988
19989
19990
19991 /* The following macros are only called from draw_glyphs below.
19992 They reference the following parameters of that function directly:
19993 `w', `row', `area', and `overlap_p'
19994 as well as the following local variables:
19995 `s', `f', and `hdc' (in W32) */
19996
19997 #ifdef HAVE_NTGUI
19998 /* On W32, silently add local `hdc' variable to argument list of
19999 init_glyph_string. */
20000 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20001 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
20002 #else
20003 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
20004 init_glyph_string (s, char2b, w, row, area, start, hl)
20005 #endif
20006
20007 /* Add a glyph string for a stretch glyph to the list of strings
20008 between HEAD and TAIL. START is the index of the stretch glyph in
20009 row area AREA of glyph row ROW. END is the index of the last glyph
20010 in that glyph row area. X is the current output position assigned
20011 to the new glyph string constructed. HL overrides that face of the
20012 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20013 is the right-most x-position of the drawing area. */
20014
20015 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
20016 and below -- keep them on one line. */
20017 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20018 do \
20019 { \
20020 s = (struct glyph_string *) alloca (sizeof *s); \
20021 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20022 START = fill_stretch_glyph_string (s, row, area, START, END); \
20023 append_glyph_string (&HEAD, &TAIL, s); \
20024 s->x = (X); \
20025 } \
20026 while (0)
20027
20028
20029 /* Add a glyph string for an image glyph to the list of strings
20030 between HEAD and TAIL. START is the index of the image glyph in
20031 row area AREA of glyph row ROW. END is the index of the last glyph
20032 in that glyph row area. X is the current output position assigned
20033 to the new glyph string constructed. HL overrides that face of the
20034 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
20035 is the right-most x-position of the drawing area. */
20036
20037 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20038 do \
20039 { \
20040 s = (struct glyph_string *) alloca (sizeof *s); \
20041 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
20042 fill_image_glyph_string (s); \
20043 append_glyph_string (&HEAD, &TAIL, s); \
20044 ++START; \
20045 s->x = (X); \
20046 } \
20047 while (0)
20048
20049
20050 /* Add a glyph string for a sequence of character glyphs to the list
20051 of strings between HEAD and TAIL. START is the index of the first
20052 glyph in row area AREA of glyph row ROW that is part of the new
20053 glyph string. END is the index of the last glyph in that glyph row
20054 area. X is the current output position assigned to the new glyph
20055 string constructed. HL overrides that face of the glyph; e.g. it
20056 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
20057 right-most x-position of the drawing area. */
20058
20059 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20060 do \
20061 { \
20062 int face_id; \
20063 XChar2b *char2b; \
20064 \
20065 face_id = (row)->glyphs[area][START].face_id; \
20066 \
20067 s = (struct glyph_string *) alloca (sizeof *s); \
20068 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
20069 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20070 append_glyph_string (&HEAD, &TAIL, s); \
20071 s->x = (X); \
20072 START = fill_glyph_string (s, face_id, START, END, overlaps); \
20073 } \
20074 while (0)
20075
20076
20077 /* Add a glyph string for a composite sequence to the list of strings
20078 between HEAD and TAIL. START is the index of the first glyph in
20079 row area AREA of glyph row ROW that is part of the new glyph
20080 string. END is the index of the last glyph in that glyph row area.
20081 X is the current output position assigned to the new glyph string
20082 constructed. HL overrides that face of the glyph; e.g. it is
20083 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
20084 x-position of the drawing area. */
20085
20086 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
20087 do { \
20088 int face_id = (row)->glyphs[area][START].face_id; \
20089 struct face *base_face = FACE_FROM_ID (f, face_id); \
20090 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
20091 struct composition *cmp = composition_table[cmp_id]; \
20092 XChar2b *char2b; \
20093 struct glyph_string *first_s; \
20094 int n; \
20095 \
20096 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
20097 \
20098 /* Make glyph_strings for each glyph sequence that is drawable by \
20099 the same face, and append them to HEAD/TAIL. */ \
20100 for (n = 0; n < cmp->glyph_len;) \
20101 { \
20102 s = (struct glyph_string *) alloca (sizeof *s); \
20103 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
20104 append_glyph_string (&(HEAD), &(TAIL), s); \
20105 s->cmp = cmp; \
20106 s->gidx = n; \
20107 s->x = (X); \
20108 if (n == 0) \
20109 first_s = s; \
20110 n = fill_composite_glyph_string (s, base_face, overlaps); \
20111 } \
20112 \
20113 ++START; \
20114 s = first_s; \
20115 } while (0)
20116
20117
20118 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
20119 of AREA of glyph row ROW on window W between indices START and END.
20120 HL overrides the face for drawing glyph strings, e.g. it is
20121 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
20122 x-positions of the drawing area.
20123
20124 This is an ugly monster macro construct because we must use alloca
20125 to allocate glyph strings (because draw_glyphs can be called
20126 asynchronously). */
20127
20128 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
20129 do \
20130 { \
20131 HEAD = TAIL = NULL; \
20132 while (START < END) \
20133 { \
20134 struct glyph *first_glyph = (row)->glyphs[area] + START; \
20135 switch (first_glyph->type) \
20136 { \
20137 case CHAR_GLYPH: \
20138 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
20139 HL, X, LAST_X); \
20140 break; \
20141 \
20142 case COMPOSITE_GLYPH: \
20143 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
20144 HL, X, LAST_X); \
20145 break; \
20146 \
20147 case STRETCH_GLYPH: \
20148 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
20149 HL, X, LAST_X); \
20150 break; \
20151 \
20152 case IMAGE_GLYPH: \
20153 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
20154 HL, X, LAST_X); \
20155 break; \
20156 \
20157 default: \
20158 abort (); \
20159 } \
20160 \
20161 if (s) \
20162 { \
20163 set_glyph_string_background_width (s, START, LAST_X); \
20164 (X) += s->width; \
20165 } \
20166 } \
20167 } \
20168 while (0)
20169
20170
20171 /* Draw glyphs between START and END in AREA of ROW on window W,
20172 starting at x-position X. X is relative to AREA in W. HL is a
20173 face-override with the following meaning:
20174
20175 DRAW_NORMAL_TEXT draw normally
20176 DRAW_CURSOR draw in cursor face
20177 DRAW_MOUSE_FACE draw in mouse face.
20178 DRAW_INVERSE_VIDEO draw in mode line face
20179 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
20180 DRAW_IMAGE_RAISED draw an image with a raised relief around it
20181
20182 If OVERLAPS is non-zero, draw only the foreground of characters and
20183 clip to the physical height of ROW. Non-zero value also defines
20184 the overlapping part to be drawn:
20185
20186 OVERLAPS_PRED overlap with preceding rows
20187 OVERLAPS_SUCC overlap with succeeding rows
20188 OVERLAPS_BOTH overlap with both preceding/succeeding rows
20189 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
20190
20191 Value is the x-position reached, relative to AREA of W. */
20192
20193 static int
20194 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
20195 struct window *w;
20196 int x;
20197 struct glyph_row *row;
20198 enum glyph_row_area area;
20199 EMACS_INT start, end;
20200 enum draw_glyphs_face hl;
20201 int overlaps;
20202 {
20203 struct glyph_string *head, *tail;
20204 struct glyph_string *s;
20205 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
20206 int i, j, x_reached, last_x, area_left = 0;
20207 struct frame *f = XFRAME (WINDOW_FRAME (w));
20208 DECLARE_HDC (hdc);
20209
20210 ALLOCATE_HDC (hdc, f);
20211
20212 /* Let's rather be paranoid than getting a SEGV. */
20213 end = min (end, row->used[area]);
20214 start = max (0, start);
20215 start = min (end, start);
20216
20217 /* Translate X to frame coordinates. Set last_x to the right
20218 end of the drawing area. */
20219 if (row->full_width_p)
20220 {
20221 /* X is relative to the left edge of W, without scroll bars
20222 or fringes. */
20223 area_left = WINDOW_LEFT_EDGE_X (w);
20224 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
20225 }
20226 else
20227 {
20228 area_left = window_box_left (w, area);
20229 last_x = area_left + window_box_width (w, area);
20230 }
20231 x += area_left;
20232
20233 /* Build a doubly-linked list of glyph_string structures between
20234 head and tail from what we have to draw. Note that the macro
20235 BUILD_GLYPH_STRINGS will modify its start parameter. That's
20236 the reason we use a separate variable `i'. */
20237 i = start;
20238 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
20239 if (tail)
20240 x_reached = tail->x + tail->background_width;
20241 else
20242 x_reached = x;
20243
20244 /* If there are any glyphs with lbearing < 0 or rbearing > width in
20245 the row, redraw some glyphs in front or following the glyph
20246 strings built above. */
20247 if (head && !overlaps && row->contains_overlapping_glyphs_p)
20248 {
20249 struct glyph_string *h, *t;
20250 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20251 int mouse_beg_col, mouse_end_col, check_mouse_face = 0;
20252 int dummy_x = 0;
20253
20254 /* If mouse highlighting is on, we may need to draw adjacent
20255 glyphs using mouse-face highlighting. */
20256 if (area == TEXT_AREA && row->mouse_face_p)
20257 {
20258 struct glyph_row *mouse_beg_row, *mouse_end_row;
20259
20260 mouse_beg_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20261 mouse_end_row = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20262
20263 if (row >= mouse_beg_row && row <= mouse_end_row)
20264 {
20265 check_mouse_face = 1;
20266 mouse_beg_col = (row == mouse_beg_row)
20267 ? dpyinfo->mouse_face_beg_col : 0;
20268 mouse_end_col = (row == mouse_end_row)
20269 ? dpyinfo->mouse_face_end_col
20270 : row->used[TEXT_AREA];
20271 }
20272 }
20273
20274 /* Compute overhangs for all glyph strings. */
20275 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
20276 for (s = head; s; s = s->next)
20277 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
20278
20279 /* Prepend glyph strings for glyphs in front of the first glyph
20280 string that are overwritten because of the first glyph
20281 string's left overhang. The background of all strings
20282 prepended must be drawn because the first glyph string
20283 draws over it. */
20284 i = left_overwritten (head);
20285 if (i >= 0)
20286 {
20287 enum draw_glyphs_face overlap_hl;
20288
20289 /* If this row contains mouse highlighting, attempt to draw
20290 the overlapped glyphs with the correct highlight. This
20291 code fails if the overlap encompasses more than one glyph
20292 and mouse-highlight spans only some of these glyphs.
20293 However, making it work perfectly involves a lot more
20294 code, and I don't know if the pathological case occurs in
20295 practice, so we'll stick to this for now. --- cyd */
20296 if (check_mouse_face
20297 && mouse_beg_col < start && mouse_end_col > i)
20298 overlap_hl = DRAW_MOUSE_FACE;
20299 else
20300 overlap_hl = DRAW_NORMAL_TEXT;
20301
20302 j = i;
20303 BUILD_GLYPH_STRINGS (j, start, h, t,
20304 overlap_hl, dummy_x, last_x);
20305 compute_overhangs_and_x (t, head->x, 1);
20306 prepend_glyph_string_lists (&head, &tail, h, t);
20307 clip_head = head;
20308 }
20309
20310 /* Prepend glyph strings for glyphs in front of the first glyph
20311 string that overwrite that glyph string because of their
20312 right overhang. For these strings, only the foreground must
20313 be drawn, because it draws over the glyph string at `head'.
20314 The background must not be drawn because this would overwrite
20315 right overhangs of preceding glyphs for which no glyph
20316 strings exist. */
20317 i = left_overwriting (head);
20318 if (i >= 0)
20319 {
20320 enum draw_glyphs_face overlap_hl;
20321
20322 if (check_mouse_face
20323 && mouse_beg_col < start && mouse_end_col > i)
20324 overlap_hl = DRAW_MOUSE_FACE;
20325 else
20326 overlap_hl = DRAW_NORMAL_TEXT;
20327
20328 clip_head = head;
20329 BUILD_GLYPH_STRINGS (i, start, h, t,
20330 overlap_hl, dummy_x, last_x);
20331 for (s = h; s; s = s->next)
20332 s->background_filled_p = 1;
20333 compute_overhangs_and_x (t, head->x, 1);
20334 prepend_glyph_string_lists (&head, &tail, h, t);
20335 }
20336
20337 /* Append glyphs strings for glyphs following the last glyph
20338 string tail that are overwritten by tail. The background of
20339 these strings has to be drawn because tail's foreground draws
20340 over it. */
20341 i = right_overwritten (tail);
20342 if (i >= 0)
20343 {
20344 enum draw_glyphs_face overlap_hl;
20345
20346 if (check_mouse_face
20347 && mouse_beg_col < i && mouse_end_col > end)
20348 overlap_hl = DRAW_MOUSE_FACE;
20349 else
20350 overlap_hl = DRAW_NORMAL_TEXT;
20351
20352 BUILD_GLYPH_STRINGS (end, i, h, t,
20353 overlap_hl, x, last_x);
20354 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20355 append_glyph_string_lists (&head, &tail, h, t);
20356 clip_tail = tail;
20357 }
20358
20359 /* Append glyph strings for glyphs following the last glyph
20360 string tail that overwrite tail. The foreground of such
20361 glyphs has to be drawn because it writes into the background
20362 of tail. The background must not be drawn because it could
20363 paint over the foreground of following glyphs. */
20364 i = right_overwriting (tail);
20365 if (i >= 0)
20366 {
20367 enum draw_glyphs_face overlap_hl;
20368 if (check_mouse_face
20369 && mouse_beg_col < i && mouse_end_col > end)
20370 overlap_hl = DRAW_MOUSE_FACE;
20371 else
20372 overlap_hl = DRAW_NORMAL_TEXT;
20373
20374 clip_tail = tail;
20375 i++; /* We must include the Ith glyph. */
20376 BUILD_GLYPH_STRINGS (end, i, h, t,
20377 overlap_hl, x, last_x);
20378 for (s = h; s; s = s->next)
20379 s->background_filled_p = 1;
20380 compute_overhangs_and_x (h, tail->x + tail->width, 0);
20381 append_glyph_string_lists (&head, &tail, h, t);
20382 }
20383 if (clip_head || clip_tail)
20384 for (s = head; s; s = s->next)
20385 {
20386 s->clip_head = clip_head;
20387 s->clip_tail = clip_tail;
20388 }
20389 }
20390
20391 /* Draw all strings. */
20392 for (s = head; s; s = s->next)
20393 FRAME_RIF (f)->draw_glyph_string (s);
20394
20395 if (area == TEXT_AREA
20396 && !row->full_width_p
20397 /* When drawing overlapping rows, only the glyph strings'
20398 foreground is drawn, which doesn't erase a cursor
20399 completely. */
20400 && !overlaps)
20401 {
20402 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
20403 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
20404 : (tail ? tail->x + tail->background_width : x));
20405 x0 -= area_left;
20406 x1 -= area_left;
20407
20408 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
20409 row->y, MATRIX_ROW_BOTTOM_Y (row));
20410 }
20411
20412 /* Value is the x-position up to which drawn, relative to AREA of W.
20413 This doesn't include parts drawn because of overhangs. */
20414 if (row->full_width_p)
20415 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
20416 else
20417 x_reached -= area_left;
20418
20419 RELEASE_HDC (hdc, f);
20420
20421 return x_reached;
20422 }
20423
20424 /* Expand row matrix if too narrow. Don't expand if area
20425 is not present. */
20426
20427 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
20428 { \
20429 if (!fonts_changed_p \
20430 && (it->glyph_row->glyphs[area] \
20431 < it->glyph_row->glyphs[area + 1])) \
20432 { \
20433 it->w->ncols_scale_factor++; \
20434 fonts_changed_p = 1; \
20435 } \
20436 }
20437
20438 /* Store one glyph for IT->char_to_display in IT->glyph_row.
20439 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20440
20441 static INLINE void
20442 append_glyph (it)
20443 struct it *it;
20444 {
20445 struct glyph *glyph;
20446 enum glyph_row_area area = it->area;
20447
20448 xassert (it->glyph_row);
20449 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
20450
20451 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20452 if (glyph < it->glyph_row->glyphs[area + 1])
20453 {
20454 glyph->charpos = CHARPOS (it->position);
20455 glyph->object = it->object;
20456 if (it->pixel_width > 0)
20457 {
20458 glyph->pixel_width = it->pixel_width;
20459 glyph->padding_p = 0;
20460 }
20461 else
20462 {
20463 /* Assure at least 1-pixel width. Otherwise, cursor can't
20464 be displayed correctly. */
20465 glyph->pixel_width = 1;
20466 glyph->padding_p = 1;
20467 }
20468 glyph->ascent = it->ascent;
20469 glyph->descent = it->descent;
20470 glyph->voffset = it->voffset;
20471 glyph->type = CHAR_GLYPH;
20472 glyph->avoid_cursor_p = it->avoid_cursor_p;
20473 glyph->multibyte_p = it->multibyte_p;
20474 glyph->left_box_line_p = it->start_of_box_run_p;
20475 glyph->right_box_line_p = it->end_of_box_run_p;
20476 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20477 || it->phys_descent > it->descent);
20478 glyph->glyph_not_available_p = it->glyph_not_available_p;
20479 glyph->face_id = it->face_id;
20480 glyph->u.ch = it->char_to_display;
20481 glyph->slice = null_glyph_slice;
20482 glyph->font_type = FONT_TYPE_UNKNOWN;
20483 ++it->glyph_row->used[area];
20484 }
20485 else
20486 IT_EXPAND_MATRIX_WIDTH (it, area);
20487 }
20488
20489 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
20490 Called from x_produce_glyphs when IT->glyph_row is non-null. */
20491
20492 static INLINE void
20493 append_composite_glyph (it)
20494 struct it *it;
20495 {
20496 struct glyph *glyph;
20497 enum glyph_row_area area = it->area;
20498
20499 xassert (it->glyph_row);
20500
20501 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20502 if (glyph < it->glyph_row->glyphs[area + 1])
20503 {
20504 glyph->charpos = CHARPOS (it->position);
20505 glyph->object = it->object;
20506 glyph->pixel_width = it->pixel_width;
20507 glyph->ascent = it->ascent;
20508 glyph->descent = it->descent;
20509 glyph->voffset = it->voffset;
20510 glyph->type = COMPOSITE_GLYPH;
20511 glyph->avoid_cursor_p = it->avoid_cursor_p;
20512 glyph->multibyte_p = it->multibyte_p;
20513 glyph->left_box_line_p = it->start_of_box_run_p;
20514 glyph->right_box_line_p = it->end_of_box_run_p;
20515 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
20516 || it->phys_descent > it->descent);
20517 glyph->padding_p = 0;
20518 glyph->glyph_not_available_p = 0;
20519 glyph->face_id = it->face_id;
20520 glyph->u.cmp_id = it->cmp_id;
20521 glyph->slice = null_glyph_slice;
20522 glyph->font_type = FONT_TYPE_UNKNOWN;
20523 ++it->glyph_row->used[area];
20524 }
20525 else
20526 IT_EXPAND_MATRIX_WIDTH (it, area);
20527 }
20528
20529
20530 /* Change IT->ascent and IT->height according to the setting of
20531 IT->voffset. */
20532
20533 static INLINE void
20534 take_vertical_position_into_account (it)
20535 struct it *it;
20536 {
20537 if (it->voffset)
20538 {
20539 if (it->voffset < 0)
20540 /* Increase the ascent so that we can display the text higher
20541 in the line. */
20542 it->ascent -= it->voffset;
20543 else
20544 /* Increase the descent so that we can display the text lower
20545 in the line. */
20546 it->descent += it->voffset;
20547 }
20548 }
20549
20550
20551 /* Produce glyphs/get display metrics for the image IT is loaded with.
20552 See the description of struct display_iterator in dispextern.h for
20553 an overview of struct display_iterator. */
20554
20555 static void
20556 produce_image_glyph (it)
20557 struct it *it;
20558 {
20559 struct image *img;
20560 struct face *face;
20561 int glyph_ascent, crop;
20562 struct glyph_slice slice;
20563
20564 xassert (it->what == IT_IMAGE);
20565
20566 face = FACE_FROM_ID (it->f, it->face_id);
20567 xassert (face);
20568 /* Make sure X resources of the face is loaded. */
20569 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20570
20571 if (it->image_id < 0)
20572 {
20573 /* Fringe bitmap. */
20574 it->ascent = it->phys_ascent = 0;
20575 it->descent = it->phys_descent = 0;
20576 it->pixel_width = 0;
20577 it->nglyphs = 0;
20578 return;
20579 }
20580
20581 img = IMAGE_FROM_ID (it->f, it->image_id);
20582 xassert (img);
20583 /* Make sure X resources of the image is loaded. */
20584 prepare_image_for_display (it->f, img);
20585
20586 slice.x = slice.y = 0;
20587 slice.width = img->width;
20588 slice.height = img->height;
20589
20590 if (INTEGERP (it->slice.x))
20591 slice.x = XINT (it->slice.x);
20592 else if (FLOATP (it->slice.x))
20593 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
20594
20595 if (INTEGERP (it->slice.y))
20596 slice.y = XINT (it->slice.y);
20597 else if (FLOATP (it->slice.y))
20598 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
20599
20600 if (INTEGERP (it->slice.width))
20601 slice.width = XINT (it->slice.width);
20602 else if (FLOATP (it->slice.width))
20603 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
20604
20605 if (INTEGERP (it->slice.height))
20606 slice.height = XINT (it->slice.height);
20607 else if (FLOATP (it->slice.height))
20608 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
20609
20610 if (slice.x >= img->width)
20611 slice.x = img->width;
20612 if (slice.y >= img->height)
20613 slice.y = img->height;
20614 if (slice.x + slice.width >= img->width)
20615 slice.width = img->width - slice.x;
20616 if (slice.y + slice.height > img->height)
20617 slice.height = img->height - slice.y;
20618
20619 if (slice.width == 0 || slice.height == 0)
20620 return;
20621
20622 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
20623
20624 it->descent = slice.height - glyph_ascent;
20625 if (slice.y == 0)
20626 it->descent += img->vmargin;
20627 if (slice.y + slice.height == img->height)
20628 it->descent += img->vmargin;
20629 it->phys_descent = it->descent;
20630
20631 it->pixel_width = slice.width;
20632 if (slice.x == 0)
20633 it->pixel_width += img->hmargin;
20634 if (slice.x + slice.width == img->width)
20635 it->pixel_width += img->hmargin;
20636
20637 /* It's quite possible for images to have an ascent greater than
20638 their height, so don't get confused in that case. */
20639 if (it->descent < 0)
20640 it->descent = 0;
20641
20642 #if 0 /* this breaks image tiling */
20643 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
20644 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
20645 if (face_ascent > it->ascent)
20646 it->ascent = it->phys_ascent = face_ascent;
20647 #endif
20648
20649 it->nglyphs = 1;
20650
20651 if (face->box != FACE_NO_BOX)
20652 {
20653 if (face->box_line_width > 0)
20654 {
20655 if (slice.y == 0)
20656 it->ascent += face->box_line_width;
20657 if (slice.y + slice.height == img->height)
20658 it->descent += face->box_line_width;
20659 }
20660
20661 if (it->start_of_box_run_p && slice.x == 0)
20662 it->pixel_width += eabs (face->box_line_width);
20663 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
20664 it->pixel_width += eabs (face->box_line_width);
20665 }
20666
20667 take_vertical_position_into_account (it);
20668
20669 /* Automatically crop wide image glyphs at right edge so we can
20670 draw the cursor on same display row. */
20671 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
20672 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
20673 {
20674 it->pixel_width -= crop;
20675 slice.width -= crop;
20676 }
20677
20678 if (it->glyph_row)
20679 {
20680 struct glyph *glyph;
20681 enum glyph_row_area area = it->area;
20682
20683 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20684 if (glyph < it->glyph_row->glyphs[area + 1])
20685 {
20686 glyph->charpos = CHARPOS (it->position);
20687 glyph->object = it->object;
20688 glyph->pixel_width = it->pixel_width;
20689 glyph->ascent = glyph_ascent;
20690 glyph->descent = it->descent;
20691 glyph->voffset = it->voffset;
20692 glyph->type = IMAGE_GLYPH;
20693 glyph->avoid_cursor_p = it->avoid_cursor_p;
20694 glyph->multibyte_p = it->multibyte_p;
20695 glyph->left_box_line_p = it->start_of_box_run_p;
20696 glyph->right_box_line_p = it->end_of_box_run_p;
20697 glyph->overlaps_vertically_p = 0;
20698 glyph->padding_p = 0;
20699 glyph->glyph_not_available_p = 0;
20700 glyph->face_id = it->face_id;
20701 glyph->u.img_id = img->id;
20702 glyph->slice = slice;
20703 glyph->font_type = FONT_TYPE_UNKNOWN;
20704 ++it->glyph_row->used[area];
20705 }
20706 else
20707 IT_EXPAND_MATRIX_WIDTH (it, area);
20708 }
20709 }
20710
20711
20712 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
20713 of the glyph, WIDTH and HEIGHT are the width and height of the
20714 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
20715
20716 static void
20717 append_stretch_glyph (it, object, width, height, ascent)
20718 struct it *it;
20719 Lisp_Object object;
20720 int width, height;
20721 int ascent;
20722 {
20723 struct glyph *glyph;
20724 enum glyph_row_area area = it->area;
20725
20726 xassert (ascent >= 0 && ascent <= height);
20727
20728 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
20729 if (glyph < it->glyph_row->glyphs[area + 1])
20730 {
20731 glyph->charpos = CHARPOS (it->position);
20732 glyph->object = object;
20733 glyph->pixel_width = width;
20734 glyph->ascent = ascent;
20735 glyph->descent = height - ascent;
20736 glyph->voffset = it->voffset;
20737 glyph->type = STRETCH_GLYPH;
20738 glyph->avoid_cursor_p = it->avoid_cursor_p;
20739 glyph->multibyte_p = it->multibyte_p;
20740 glyph->left_box_line_p = it->start_of_box_run_p;
20741 glyph->right_box_line_p = it->end_of_box_run_p;
20742 glyph->overlaps_vertically_p = 0;
20743 glyph->padding_p = 0;
20744 glyph->glyph_not_available_p = 0;
20745 glyph->face_id = it->face_id;
20746 glyph->u.stretch.ascent = ascent;
20747 glyph->u.stretch.height = height;
20748 glyph->slice = null_glyph_slice;
20749 glyph->font_type = FONT_TYPE_UNKNOWN;
20750 ++it->glyph_row->used[area];
20751 }
20752 else
20753 IT_EXPAND_MATRIX_WIDTH (it, area);
20754 }
20755
20756
20757 /* Produce a stretch glyph for iterator IT. IT->object is the value
20758 of the glyph property displayed. The value must be a list
20759 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
20760 being recognized:
20761
20762 1. `:width WIDTH' specifies that the space should be WIDTH *
20763 canonical char width wide. WIDTH may be an integer or floating
20764 point number.
20765
20766 2. `:relative-width FACTOR' specifies that the width of the stretch
20767 should be computed from the width of the first character having the
20768 `glyph' property, and should be FACTOR times that width.
20769
20770 3. `:align-to HPOS' specifies that the space should be wide enough
20771 to reach HPOS, a value in canonical character units.
20772
20773 Exactly one of the above pairs must be present.
20774
20775 4. `:height HEIGHT' specifies that the height of the stretch produced
20776 should be HEIGHT, measured in canonical character units.
20777
20778 5. `:relative-height FACTOR' specifies that the height of the
20779 stretch should be FACTOR times the height of the characters having
20780 the glyph property.
20781
20782 Either none or exactly one of 4 or 5 must be present.
20783
20784 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20785 of the stretch should be used for the ascent of the stretch.
20786 ASCENT must be in the range 0 <= ASCENT <= 100. */
20787
20788 static void
20789 produce_stretch_glyph (it)
20790 struct it *it;
20791 {
20792 /* (space :width WIDTH :height HEIGHT ...) */
20793 Lisp_Object prop, plist;
20794 int width = 0, height = 0, align_to = -1;
20795 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20796 int ascent = 0;
20797 double tem;
20798 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20799 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
20800
20801 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20802
20803 /* List should start with `space'. */
20804 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20805 plist = XCDR (it->object);
20806
20807 /* Compute the width of the stretch. */
20808 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20809 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20810 {
20811 /* Absolute width `:width WIDTH' specified and valid. */
20812 zero_width_ok_p = 1;
20813 width = (int)tem;
20814 }
20815 else if (prop = Fplist_get (plist, QCrelative_width),
20816 NUMVAL (prop) > 0)
20817 {
20818 /* Relative width `:relative-width FACTOR' specified and valid.
20819 Compute the width of the characters having the `glyph'
20820 property. */
20821 struct it it2;
20822 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20823
20824 it2 = *it;
20825 if (it->multibyte_p)
20826 {
20827 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20828 - IT_BYTEPOS (*it));
20829 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20830 }
20831 else
20832 it2.c = *p, it2.len = 1;
20833
20834 it2.glyph_row = NULL;
20835 it2.what = IT_CHARACTER;
20836 x_produce_glyphs (&it2);
20837 width = NUMVAL (prop) * it2.pixel_width;
20838 }
20839 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20840 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20841 {
20842 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20843 align_to = (align_to < 0
20844 ? 0
20845 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20846 else if (align_to < 0)
20847 align_to = window_box_left_offset (it->w, TEXT_AREA);
20848 width = max (0, (int)tem + align_to - it->current_x);
20849 zero_width_ok_p = 1;
20850 }
20851 else
20852 /* Nothing specified -> width defaults to canonical char width. */
20853 width = FRAME_COLUMN_WIDTH (it->f);
20854
20855 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20856 width = 1;
20857
20858 /* Compute height. */
20859 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20860 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20861 {
20862 height = (int)tem;
20863 zero_height_ok_p = 1;
20864 }
20865 else if (prop = Fplist_get (plist, QCrelative_height),
20866 NUMVAL (prop) > 0)
20867 height = FONT_HEIGHT (font) * NUMVAL (prop);
20868 else
20869 height = FONT_HEIGHT (font);
20870
20871 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20872 height = 1;
20873
20874 /* Compute percentage of height used for ascent. If
20875 `:ascent ASCENT' is present and valid, use that. Otherwise,
20876 derive the ascent from the font in use. */
20877 if (prop = Fplist_get (plist, QCascent),
20878 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20879 ascent = height * NUMVAL (prop) / 100.0;
20880 else if (!NILP (prop)
20881 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20882 ascent = min (max (0, (int)tem), height);
20883 else
20884 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20885
20886 if (width > 0 && it->line_wrap != TRUNCATE
20887 && it->current_x + width > it->last_visible_x)
20888 width = it->last_visible_x - it->current_x - 1;
20889
20890 if (width > 0 && height > 0 && it->glyph_row)
20891 {
20892 Lisp_Object object = it->stack[it->sp - 1].string;
20893 if (!STRINGP (object))
20894 object = it->w->buffer;
20895 append_stretch_glyph (it, object, width, height, ascent);
20896 }
20897
20898 it->pixel_width = width;
20899 it->ascent = it->phys_ascent = ascent;
20900 it->descent = it->phys_descent = height - it->ascent;
20901 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20902
20903 take_vertical_position_into_account (it);
20904 }
20905
20906 /* Calculate line-height and line-spacing properties.
20907 An integer value specifies explicit pixel value.
20908 A float value specifies relative value to current face height.
20909 A cons (float . face-name) specifies relative value to
20910 height of specified face font.
20911
20912 Returns height in pixels, or nil. */
20913
20914
20915 static Lisp_Object
20916 calc_line_height_property (it, val, font, boff, override)
20917 struct it *it;
20918 Lisp_Object val;
20919 struct font *font;
20920 int boff, override;
20921 {
20922 Lisp_Object face_name = Qnil;
20923 int ascent, descent, height;
20924
20925 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20926 return val;
20927
20928 if (CONSP (val))
20929 {
20930 face_name = XCAR (val);
20931 val = XCDR (val);
20932 if (!NUMBERP (val))
20933 val = make_number (1);
20934 if (NILP (face_name))
20935 {
20936 height = it->ascent + it->descent;
20937 goto scale;
20938 }
20939 }
20940
20941 if (NILP (face_name))
20942 {
20943 font = FRAME_FONT (it->f);
20944 boff = FRAME_BASELINE_OFFSET (it->f);
20945 }
20946 else if (EQ (face_name, Qt))
20947 {
20948 override = 0;
20949 }
20950 else
20951 {
20952 int face_id;
20953 struct face *face;
20954
20955 face_id = lookup_named_face (it->f, face_name, 0);
20956 if (face_id < 0)
20957 return make_number (-1);
20958
20959 face = FACE_FROM_ID (it->f, face_id);
20960 font = face->font;
20961 if (font == NULL)
20962 return make_number (-1);
20963 boff = font->baseline_offset;
20964 if (font->vertical_centering)
20965 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20966 }
20967
20968 ascent = FONT_BASE (font) + boff;
20969 descent = FONT_DESCENT (font) - boff;
20970
20971 if (override)
20972 {
20973 it->override_ascent = ascent;
20974 it->override_descent = descent;
20975 it->override_boff = boff;
20976 }
20977
20978 height = ascent + descent;
20979
20980 scale:
20981 if (FLOATP (val))
20982 height = (int)(XFLOAT_DATA (val) * height);
20983 else if (INTEGERP (val))
20984 height *= XINT (val);
20985
20986 return make_number (height);
20987 }
20988
20989
20990 /* RIF:
20991 Produce glyphs/get display metrics for the display element IT is
20992 loaded with. See the description of struct it in dispextern.h
20993 for an overview of struct it. */
20994
20995 void
20996 x_produce_glyphs (it)
20997 struct it *it;
20998 {
20999 int extra_line_spacing = it->extra_line_spacing;
21000
21001 it->glyph_not_available_p = 0;
21002
21003 if (it->what == IT_CHARACTER)
21004 {
21005 XChar2b char2b;
21006 struct font *font;
21007 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21008 struct font_metrics *pcm;
21009 int font_not_found_p;
21010 int boff; /* baseline offset */
21011 /* We may change it->multibyte_p upon unibyte<->multibyte
21012 conversion. So, save the current value now and restore it
21013 later.
21014
21015 Note: It seems that we don't have to record multibyte_p in
21016 struct glyph because the character code itself tells if or
21017 not the character is multibyte. Thus, in the future, we must
21018 consider eliminating the field `multibyte_p' in the struct
21019 glyph. */
21020 int saved_multibyte_p = it->multibyte_p;
21021
21022 /* Maybe translate single-byte characters to multibyte, or the
21023 other way. */
21024 it->char_to_display = it->c;
21025 if (!ASCII_BYTE_P (it->c)
21026 && ! it->multibyte_p)
21027 {
21028 if (SINGLE_BYTE_CHAR_P (it->c)
21029 && unibyte_display_via_language_environment)
21030 it->char_to_display = unibyte_char_to_multibyte (it->c);
21031 if (! SINGLE_BYTE_CHAR_P (it->char_to_display))
21032 {
21033 it->multibyte_p = 1;
21034 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display,
21035 -1, Qnil);
21036 face = FACE_FROM_ID (it->f, it->face_id);
21037 }
21038 }
21039
21040 /* Get font to use. Encode IT->char_to_display. */
21041 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
21042 &char2b, it->multibyte_p, 0);
21043 font = face->font;
21044
21045 /* When no suitable font found, use the default font. */
21046 font_not_found_p = font == NULL;
21047 if (font_not_found_p)
21048 {
21049 font = FRAME_FONT (it->f);
21050 boff = FRAME_BASELINE_OFFSET (it->f);
21051 }
21052 else
21053 {
21054 boff = font->baseline_offset;
21055 if (font->vertical_centering)
21056 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21057 }
21058
21059 if (it->char_to_display >= ' '
21060 && (!it->multibyte_p || it->char_to_display < 128))
21061 {
21062 /* Either unibyte or ASCII. */
21063 int stretched_p;
21064
21065 it->nglyphs = 1;
21066
21067 pcm = get_per_char_metric (it->f, font, &char2b);
21068
21069 if (it->override_ascent >= 0)
21070 {
21071 it->ascent = it->override_ascent;
21072 it->descent = it->override_descent;
21073 boff = it->override_boff;
21074 }
21075 else
21076 {
21077 it->ascent = FONT_BASE (font) + boff;
21078 it->descent = FONT_DESCENT (font) - boff;
21079 }
21080
21081 if (pcm)
21082 {
21083 it->phys_ascent = pcm->ascent + boff;
21084 it->phys_descent = pcm->descent - boff;
21085 it->pixel_width = pcm->width;
21086 }
21087 else
21088 {
21089 it->glyph_not_available_p = 1;
21090 it->phys_ascent = it->ascent;
21091 it->phys_descent = it->descent;
21092 it->pixel_width = FONT_WIDTH (font);
21093 }
21094
21095 if (it->constrain_row_ascent_descent_p)
21096 {
21097 if (it->descent > it->max_descent)
21098 {
21099 it->ascent += it->descent - it->max_descent;
21100 it->descent = it->max_descent;
21101 }
21102 if (it->ascent > it->max_ascent)
21103 {
21104 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21105 it->ascent = it->max_ascent;
21106 }
21107 it->phys_ascent = min (it->phys_ascent, it->ascent);
21108 it->phys_descent = min (it->phys_descent, it->descent);
21109 extra_line_spacing = 0;
21110 }
21111
21112 /* If this is a space inside a region of text with
21113 `space-width' property, change its width. */
21114 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
21115 if (stretched_p)
21116 it->pixel_width *= XFLOATINT (it->space_width);
21117
21118 /* If face has a box, add the box thickness to the character
21119 height. If character has a box line to the left and/or
21120 right, add the box line width to the character's width. */
21121 if (face->box != FACE_NO_BOX)
21122 {
21123 int thick = face->box_line_width;
21124
21125 if (thick > 0)
21126 {
21127 it->ascent += thick;
21128 it->descent += thick;
21129 }
21130 else
21131 thick = -thick;
21132
21133 if (it->start_of_box_run_p)
21134 it->pixel_width += thick;
21135 if (it->end_of_box_run_p)
21136 it->pixel_width += thick;
21137 }
21138
21139 /* If face has an overline, add the height of the overline
21140 (1 pixel) and a 1 pixel margin to the character height. */
21141 if (face->overline_p)
21142 it->ascent += overline_margin;
21143
21144 if (it->constrain_row_ascent_descent_p)
21145 {
21146 if (it->ascent > it->max_ascent)
21147 it->ascent = it->max_ascent;
21148 if (it->descent > it->max_descent)
21149 it->descent = it->max_descent;
21150 }
21151
21152 take_vertical_position_into_account (it);
21153
21154 /* If we have to actually produce glyphs, do it. */
21155 if (it->glyph_row)
21156 {
21157 if (stretched_p)
21158 {
21159 /* Translate a space with a `space-width' property
21160 into a stretch glyph. */
21161 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
21162 / FONT_HEIGHT (font));
21163 append_stretch_glyph (it, it->object, it->pixel_width,
21164 it->ascent + it->descent, ascent);
21165 }
21166 else
21167 append_glyph (it);
21168
21169 /* If characters with lbearing or rbearing are displayed
21170 in this line, record that fact in a flag of the
21171 glyph row. This is used to optimize X output code. */
21172 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
21173 it->glyph_row->contains_overlapping_glyphs_p = 1;
21174 }
21175 if (! stretched_p && it->pixel_width == 0)
21176 /* We assure that all visible glyphs have at least 1-pixel
21177 width. */
21178 it->pixel_width = 1;
21179 }
21180 else if (it->char_to_display == '\n')
21181 {
21182 /* A newline has no width but we need the height of the line.
21183 But if previous part of the line set a height, don't
21184 increase that height */
21185
21186 Lisp_Object height;
21187 Lisp_Object total_height = Qnil;
21188
21189 it->override_ascent = -1;
21190 it->pixel_width = 0;
21191 it->nglyphs = 0;
21192
21193 height = get_it_property(it, Qline_height);
21194 /* Split (line-height total-height) list */
21195 if (CONSP (height)
21196 && CONSP (XCDR (height))
21197 && NILP (XCDR (XCDR (height))))
21198 {
21199 total_height = XCAR (XCDR (height));
21200 height = XCAR (height);
21201 }
21202 height = calc_line_height_property(it, height, font, boff, 1);
21203
21204 if (it->override_ascent >= 0)
21205 {
21206 it->ascent = it->override_ascent;
21207 it->descent = it->override_descent;
21208 boff = it->override_boff;
21209 }
21210 else
21211 {
21212 it->ascent = FONT_BASE (font) + boff;
21213 it->descent = FONT_DESCENT (font) - boff;
21214 }
21215
21216 if (EQ (height, Qt))
21217 {
21218 if (it->descent > it->max_descent)
21219 {
21220 it->ascent += it->descent - it->max_descent;
21221 it->descent = it->max_descent;
21222 }
21223 if (it->ascent > it->max_ascent)
21224 {
21225 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
21226 it->ascent = it->max_ascent;
21227 }
21228 it->phys_ascent = min (it->phys_ascent, it->ascent);
21229 it->phys_descent = min (it->phys_descent, it->descent);
21230 it->constrain_row_ascent_descent_p = 1;
21231 extra_line_spacing = 0;
21232 }
21233 else
21234 {
21235 Lisp_Object spacing;
21236
21237 it->phys_ascent = it->ascent;
21238 it->phys_descent = it->descent;
21239
21240 if ((it->max_ascent > 0 || it->max_descent > 0)
21241 && face->box != FACE_NO_BOX
21242 && face->box_line_width > 0)
21243 {
21244 it->ascent += face->box_line_width;
21245 it->descent += face->box_line_width;
21246 }
21247 if (!NILP (height)
21248 && XINT (height) > it->ascent + it->descent)
21249 it->ascent = XINT (height) - it->descent;
21250
21251 if (!NILP (total_height))
21252 spacing = calc_line_height_property(it, total_height, font, boff, 0);
21253 else
21254 {
21255 spacing = get_it_property(it, Qline_spacing);
21256 spacing = calc_line_height_property(it, spacing, font, boff, 0);
21257 }
21258 if (INTEGERP (spacing))
21259 {
21260 extra_line_spacing = XINT (spacing);
21261 if (!NILP (total_height))
21262 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
21263 }
21264 }
21265 }
21266 else if (it->char_to_display == '\t')
21267 {
21268 int tab_width = it->tab_width * font->space_width;
21269 int x = it->current_x + it->continuation_lines_width;
21270 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
21271
21272 /* If the distance from the current position to the next tab
21273 stop is less than a space character width, use the
21274 tab stop after that. */
21275 if (next_tab_x - x < font->space_width)
21276 next_tab_x += tab_width;
21277
21278 it->pixel_width = next_tab_x - x;
21279 it->nglyphs = 1;
21280 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
21281 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
21282
21283 if (it->glyph_row)
21284 {
21285 append_stretch_glyph (it, it->object, it->pixel_width,
21286 it->ascent + it->descent, it->ascent);
21287 }
21288 }
21289 else
21290 {
21291 /* A multi-byte character. Assume that the display width of the
21292 character is the width of the character multiplied by the
21293 width of the font. */
21294
21295 /* If we found a font, this font should give us the right
21296 metrics. If we didn't find a font, use the frame's
21297 default font and calculate the width of the character by
21298 multiplying the width of font by the width of the
21299 character. */
21300
21301 pcm = get_per_char_metric (it->f, font, &char2b);
21302
21303 if (font_not_found_p || !pcm)
21304 {
21305 int char_width = CHAR_WIDTH (it->char_to_display);
21306
21307 if (char_width == 0)
21308 /* This is a non spacing character. But, as we are
21309 going to display an empty box, the box must occupy
21310 at least one column. */
21311 char_width = 1;
21312 it->glyph_not_available_p = 1;
21313 it->pixel_width = FRAME_COLUMN_WIDTH (it->f) * char_width;
21314 it->phys_ascent = FONT_BASE (font) + boff;
21315 it->phys_descent = FONT_DESCENT (font) - boff;
21316 }
21317 else
21318 {
21319 it->pixel_width = pcm->width;
21320 it->phys_ascent = pcm->ascent + boff;
21321 it->phys_descent = pcm->descent - boff;
21322 if (it->glyph_row
21323 && (pcm->lbearing < 0
21324 || pcm->rbearing > pcm->width))
21325 it->glyph_row->contains_overlapping_glyphs_p = 1;
21326 }
21327 it->nglyphs = 1;
21328 it->ascent = FONT_BASE (font) + boff;
21329 it->descent = FONT_DESCENT (font) - boff;
21330 if (face->box != FACE_NO_BOX)
21331 {
21332 int thick = face->box_line_width;
21333
21334 if (thick > 0)
21335 {
21336 it->ascent += thick;
21337 it->descent += thick;
21338 }
21339 else
21340 thick = - thick;
21341
21342 if (it->start_of_box_run_p)
21343 it->pixel_width += thick;
21344 if (it->end_of_box_run_p)
21345 it->pixel_width += thick;
21346 }
21347
21348 /* If face has an overline, add the height of the overline
21349 (1 pixel) and a 1 pixel margin to the character height. */
21350 if (face->overline_p)
21351 it->ascent += overline_margin;
21352
21353 take_vertical_position_into_account (it);
21354
21355 if (it->ascent < 0)
21356 it->ascent = 0;
21357 if (it->descent < 0)
21358 it->descent = 0;
21359
21360 if (it->glyph_row)
21361 append_glyph (it);
21362 if (it->pixel_width == 0)
21363 /* We assure that all visible glyphs have at least 1-pixel
21364 width. */
21365 it->pixel_width = 1;
21366 }
21367 it->multibyte_p = saved_multibyte_p;
21368 }
21369 else if (it->what == IT_COMPOSITION)
21370 {
21371 /* Note: A composition is represented as one glyph in the
21372 glyph matrix. There are no padding glyphs.
21373
21374 Important is that pixel_width, ascent, and descent are the
21375 values of what is drawn by draw_glyphs (i.e. the values of
21376 the overall glyphs composed). */
21377 struct face *face = FACE_FROM_ID (it->f, it->face_id);
21378 int boff; /* baseline offset */
21379 struct composition *cmp = composition_table[it->cmp_id];
21380 int glyph_len = cmp->glyph_len;
21381 struct font *font = face->font;
21382
21383 it->nglyphs = 1;
21384
21385 if (cmp->method == COMPOSITION_WITH_GLYPH_STRING)
21386 {
21387 PREPARE_FACE_FOR_DISPLAY (it->f, face);
21388 font_prepare_composition (cmp, it->f);
21389 }
21390 else
21391 /* If we have not yet calculated pixel size data of glyphs of
21392 the composition for the current face font, calculate them
21393 now. Theoretically, we have to check all fonts for the
21394 glyphs, but that requires much time and memory space. So,
21395 here we check only the font of the first glyph. This leads
21396 to incorrect display, but it's very rare, and C-l (recenter)
21397 can correct the display anyway. */
21398 if (! cmp->font || cmp->font != font)
21399 {
21400 /* Ascent and descent of the font of the first character
21401 of this composition (adjusted by baseline offset).
21402 Ascent and descent of overall glyphs should not be less
21403 than them respectively. */
21404 int font_ascent, font_descent, font_height;
21405 /* Bounding box of the overall glyphs. */
21406 int leftmost, rightmost, lowest, highest;
21407 int lbearing, rbearing;
21408 int i, width, ascent, descent;
21409 int left_padded = 0, right_padded = 0;
21410 int face_id;
21411 int c;
21412 XChar2b char2b;
21413 struct font_metrics *pcm;
21414 int font_not_found_p;
21415 int pos;
21416
21417 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
21418 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
21419 break;
21420 if (glyph_len < cmp->glyph_len)
21421 right_padded = 1;
21422 for (i = 0; i < glyph_len; i++)
21423 {
21424 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
21425 break;
21426 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21427 }
21428 if (i > 0)
21429 left_padded = 1;
21430
21431 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
21432 : IT_CHARPOS (*it));
21433 /* When no suitable font found, use the default font. */
21434 font_not_found_p = font == NULL;
21435 if (font_not_found_p)
21436 {
21437 face = face->ascii_face;
21438 font = face->font;
21439 }
21440 boff = font->baseline_offset;
21441 if (font->vertical_centering)
21442 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21443 font_ascent = FONT_BASE (font) + boff;
21444 font_descent = FONT_DESCENT (font) - boff;
21445 font_height = FONT_HEIGHT (font);
21446
21447 cmp->font = (void *) font;
21448
21449 pcm = NULL;
21450 if (! font_not_found_p)
21451 {
21452 get_char_face_and_encoding (it->f, c, it->face_id,
21453 &char2b, it->multibyte_p, 0);
21454 pcm = get_per_char_metric (it->f, font, &char2b);
21455 }
21456
21457 /* Initialize the bounding box. */
21458 if (pcm)
21459 {
21460 width = pcm->width;
21461 ascent = pcm->ascent;
21462 descent = pcm->descent;
21463 lbearing = pcm->lbearing;
21464 rbearing = pcm->rbearing;
21465 }
21466 else
21467 {
21468 width = FONT_WIDTH (font);
21469 ascent = FONT_BASE (font);
21470 descent = FONT_DESCENT (font);
21471 lbearing = 0;
21472 rbearing = width;
21473 }
21474
21475 rightmost = width;
21476 leftmost = 0;
21477 lowest = - descent + boff;
21478 highest = ascent + boff;
21479
21480 if (! font_not_found_p
21481 && font->default_ascent
21482 && CHAR_TABLE_P (Vuse_default_ascent)
21483 && !NILP (Faref (Vuse_default_ascent,
21484 make_number (it->char_to_display))))
21485 highest = font->default_ascent + boff;
21486
21487 /* Draw the first glyph at the normal position. It may be
21488 shifted to right later if some other glyphs are drawn
21489 at the left. */
21490 cmp->offsets[i * 2] = 0;
21491 cmp->offsets[i * 2 + 1] = boff;
21492 cmp->lbearing = lbearing;
21493 cmp->rbearing = rbearing;
21494
21495 /* Set cmp->offsets for the remaining glyphs. */
21496 for (i++; i < glyph_len; i++)
21497 {
21498 int left, right, btm, top;
21499 int ch = COMPOSITION_GLYPH (cmp, i);
21500 int face_id;
21501 struct face *this_face;
21502 int this_boff;
21503
21504 if (ch == '\t')
21505 ch = ' ';
21506 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
21507 this_face = FACE_FROM_ID (it->f, face_id);
21508 font = this_face->font;
21509
21510 if (font == NULL)
21511 pcm = NULL;
21512 else
21513 {
21514 this_boff = font->baseline_offset;
21515 if (font->vertical_centering)
21516 this_boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
21517 get_char_face_and_encoding (it->f, ch, face_id,
21518 &char2b, it->multibyte_p, 0);
21519 pcm = get_per_char_metric (it->f, font, &char2b);
21520 }
21521 if (! pcm)
21522 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
21523 else
21524 {
21525 width = pcm->width;
21526 ascent = pcm->ascent;
21527 descent = pcm->descent;
21528 lbearing = pcm->lbearing;
21529 rbearing = pcm->rbearing;
21530 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
21531 {
21532 /* Relative composition with or without
21533 alternate chars. */
21534 left = (leftmost + rightmost - width) / 2;
21535 btm = - descent + boff;
21536 if (font->relative_compose
21537 && (! CHAR_TABLE_P (Vignore_relative_composition)
21538 || NILP (Faref (Vignore_relative_composition,
21539 make_number (ch)))))
21540 {
21541
21542 if (- descent >= font->relative_compose)
21543 /* One extra pixel between two glyphs. */
21544 btm = highest + 1;
21545 else if (ascent <= 0)
21546 /* One extra pixel between two glyphs. */
21547 btm = lowest - 1 - ascent - descent;
21548 }
21549 }
21550 else
21551 {
21552 /* A composition rule is specified by an integer
21553 value that encodes global and new reference
21554 points (GREF and NREF). GREF and NREF are
21555 specified by numbers as below:
21556
21557 0---1---2 -- ascent
21558 | |
21559 | |
21560 | |
21561 9--10--11 -- center
21562 | |
21563 ---3---4---5--- baseline
21564 | |
21565 6---7---8 -- descent
21566 */
21567 int rule = COMPOSITION_RULE (cmp, i);
21568 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
21569
21570 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
21571 grefx = gref % 3, nrefx = nref % 3;
21572 grefy = gref / 3, nrefy = nref / 3;
21573 if (xoff)
21574 xoff = font_height * (xoff - 128) / 256;
21575 if (yoff)
21576 yoff = font_height * (yoff - 128) / 256;
21577
21578 left = (leftmost
21579 + grefx * (rightmost - leftmost) / 2
21580 - nrefx * width / 2
21581 + xoff);
21582
21583 btm = ((grefy == 0 ? highest
21584 : grefy == 1 ? 0
21585 : grefy == 2 ? lowest
21586 : (highest + lowest) / 2)
21587 - (nrefy == 0 ? ascent + descent
21588 : nrefy == 1 ? descent - boff
21589 : nrefy == 2 ? 0
21590 : (ascent + descent) / 2)
21591 + yoff);
21592 }
21593
21594 cmp->offsets[i * 2] = left;
21595 cmp->offsets[i * 2 + 1] = btm + descent;
21596
21597 /* Update the bounding box of the overall glyphs. */
21598 if (width > 0)
21599 {
21600 right = left + width;
21601 if (left < leftmost)
21602 leftmost = left;
21603 if (right > rightmost)
21604 rightmost = right;
21605 }
21606 top = btm + descent + ascent;
21607 if (top > highest)
21608 highest = top;
21609 if (btm < lowest)
21610 lowest = btm;
21611
21612 if (cmp->lbearing > left + lbearing)
21613 cmp->lbearing = left + lbearing;
21614 if (cmp->rbearing < left + rbearing)
21615 cmp->rbearing = left + rbearing;
21616 }
21617 }
21618
21619 /* If there are glyphs whose x-offsets are negative,
21620 shift all glyphs to the right and make all x-offsets
21621 non-negative. */
21622 if (leftmost < 0)
21623 {
21624 for (i = 0; i < cmp->glyph_len; i++)
21625 cmp->offsets[i * 2] -= leftmost;
21626 rightmost -= leftmost;
21627 cmp->lbearing -= leftmost;
21628 cmp->rbearing -= leftmost;
21629 }
21630
21631 if (left_padded && cmp->lbearing < 0)
21632 {
21633 for (i = 0; i < cmp->glyph_len; i++)
21634 cmp->offsets[i * 2] -= cmp->lbearing;
21635 rightmost -= cmp->lbearing;
21636 cmp->rbearing -= cmp->lbearing;
21637 cmp->lbearing = 0;
21638 }
21639 if (right_padded && rightmost < cmp->rbearing)
21640 {
21641 rightmost = cmp->rbearing;
21642 }
21643
21644 cmp->pixel_width = rightmost;
21645 cmp->ascent = highest;
21646 cmp->descent = - lowest;
21647 if (cmp->ascent < font_ascent)
21648 cmp->ascent = font_ascent;
21649 if (cmp->descent < font_descent)
21650 cmp->descent = font_descent;
21651 }
21652
21653 if (it->glyph_row
21654 && (cmp->lbearing < 0
21655 || cmp->rbearing > cmp->pixel_width))
21656 it->glyph_row->contains_overlapping_glyphs_p = 1;
21657
21658 it->pixel_width = cmp->pixel_width;
21659 it->ascent = it->phys_ascent = cmp->ascent;
21660 it->descent = it->phys_descent = cmp->descent;
21661 if (face->box != FACE_NO_BOX)
21662 {
21663 int thick = face->box_line_width;
21664
21665 if (thick > 0)
21666 {
21667 it->ascent += thick;
21668 it->descent += thick;
21669 }
21670 else
21671 thick = - thick;
21672
21673 if (it->start_of_box_run_p)
21674 it->pixel_width += thick;
21675 if (it->end_of_box_run_p)
21676 it->pixel_width += thick;
21677 }
21678
21679 /* If face has an overline, add the height of the overline
21680 (1 pixel) and a 1 pixel margin to the character height. */
21681 if (face->overline_p)
21682 it->ascent += overline_margin;
21683
21684 take_vertical_position_into_account (it);
21685 if (it->ascent < 0)
21686 it->ascent = 0;
21687 if (it->descent < 0)
21688 it->descent = 0;
21689
21690 if (it->glyph_row)
21691 append_composite_glyph (it);
21692 }
21693 else if (it->what == IT_IMAGE)
21694 produce_image_glyph (it);
21695 else if (it->what == IT_STRETCH)
21696 produce_stretch_glyph (it);
21697
21698 /* Accumulate dimensions. Note: can't assume that it->descent > 0
21699 because this isn't true for images with `:ascent 100'. */
21700 xassert (it->ascent >= 0 && it->descent >= 0);
21701 if (it->area == TEXT_AREA)
21702 it->current_x += it->pixel_width;
21703
21704 if (extra_line_spacing > 0)
21705 {
21706 it->descent += extra_line_spacing;
21707 if (extra_line_spacing > it->max_extra_line_spacing)
21708 it->max_extra_line_spacing = extra_line_spacing;
21709 }
21710
21711 it->max_ascent = max (it->max_ascent, it->ascent);
21712 it->max_descent = max (it->max_descent, it->descent);
21713 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
21714 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
21715 }
21716
21717 /* EXPORT for RIF:
21718 Output LEN glyphs starting at START at the nominal cursor position.
21719 Advance the nominal cursor over the text. The global variable
21720 updated_window contains the window being updated, updated_row is
21721 the glyph row being updated, and updated_area is the area of that
21722 row being updated. */
21723
21724 void
21725 x_write_glyphs (start, len)
21726 struct glyph *start;
21727 int len;
21728 {
21729 int x, hpos;
21730
21731 xassert (updated_window && updated_row);
21732 BLOCK_INPUT;
21733
21734 /* Write glyphs. */
21735
21736 hpos = start - updated_row->glyphs[updated_area];
21737 x = draw_glyphs (updated_window, output_cursor.x,
21738 updated_row, updated_area,
21739 hpos, hpos + len,
21740 DRAW_NORMAL_TEXT, 0);
21741
21742 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
21743 if (updated_area == TEXT_AREA
21744 && updated_window->phys_cursor_on_p
21745 && updated_window->phys_cursor.vpos == output_cursor.vpos
21746 && updated_window->phys_cursor.hpos >= hpos
21747 && updated_window->phys_cursor.hpos < hpos + len)
21748 updated_window->phys_cursor_on_p = 0;
21749
21750 UNBLOCK_INPUT;
21751
21752 /* Advance the output cursor. */
21753 output_cursor.hpos += len;
21754 output_cursor.x = x;
21755 }
21756
21757
21758 /* EXPORT for RIF:
21759 Insert LEN glyphs from START at the nominal cursor position. */
21760
21761 void
21762 x_insert_glyphs (start, len)
21763 struct glyph *start;
21764 int len;
21765 {
21766 struct frame *f;
21767 struct window *w;
21768 int line_height, shift_by_width, shifted_region_width;
21769 struct glyph_row *row;
21770 struct glyph *glyph;
21771 int frame_x, frame_y;
21772 EMACS_INT hpos;
21773
21774 xassert (updated_window && updated_row);
21775 BLOCK_INPUT;
21776 w = updated_window;
21777 f = XFRAME (WINDOW_FRAME (w));
21778
21779 /* Get the height of the line we are in. */
21780 row = updated_row;
21781 line_height = row->height;
21782
21783 /* Get the width of the glyphs to insert. */
21784 shift_by_width = 0;
21785 for (glyph = start; glyph < start + len; ++glyph)
21786 shift_by_width += glyph->pixel_width;
21787
21788 /* Get the width of the region to shift right. */
21789 shifted_region_width = (window_box_width (w, updated_area)
21790 - output_cursor.x
21791 - shift_by_width);
21792
21793 /* Shift right. */
21794 frame_x = window_box_left (w, updated_area) + output_cursor.x;
21795 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
21796
21797 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
21798 line_height, shift_by_width);
21799
21800 /* Write the glyphs. */
21801 hpos = start - row->glyphs[updated_area];
21802 draw_glyphs (w, output_cursor.x, row, updated_area,
21803 hpos, hpos + len,
21804 DRAW_NORMAL_TEXT, 0);
21805
21806 /* Advance the output cursor. */
21807 output_cursor.hpos += len;
21808 output_cursor.x += shift_by_width;
21809 UNBLOCK_INPUT;
21810 }
21811
21812
21813 /* EXPORT for RIF:
21814 Erase the current text line from the nominal cursor position
21815 (inclusive) to pixel column TO_X (exclusive). The idea is that
21816 everything from TO_X onward is already erased.
21817
21818 TO_X is a pixel position relative to updated_area of
21819 updated_window. TO_X == -1 means clear to the end of this area. */
21820
21821 void
21822 x_clear_end_of_line (to_x)
21823 int to_x;
21824 {
21825 struct frame *f;
21826 struct window *w = updated_window;
21827 int max_x, min_y, max_y;
21828 int from_x, from_y, to_y;
21829
21830 xassert (updated_window && updated_row);
21831 f = XFRAME (w->frame);
21832
21833 if (updated_row->full_width_p)
21834 max_x = WINDOW_TOTAL_WIDTH (w);
21835 else
21836 max_x = window_box_width (w, updated_area);
21837 max_y = window_text_bottom_y (w);
21838
21839 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21840 of window. For TO_X > 0, truncate to end of drawing area. */
21841 if (to_x == 0)
21842 return;
21843 else if (to_x < 0)
21844 to_x = max_x;
21845 else
21846 to_x = min (to_x, max_x);
21847
21848 to_y = min (max_y, output_cursor.y + updated_row->height);
21849
21850 /* Notice if the cursor will be cleared by this operation. */
21851 if (!updated_row->full_width_p)
21852 notice_overwritten_cursor (w, updated_area,
21853 output_cursor.x, -1,
21854 updated_row->y,
21855 MATRIX_ROW_BOTTOM_Y (updated_row));
21856
21857 from_x = output_cursor.x;
21858
21859 /* Translate to frame coordinates. */
21860 if (updated_row->full_width_p)
21861 {
21862 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21863 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21864 }
21865 else
21866 {
21867 int area_left = window_box_left (w, updated_area);
21868 from_x += area_left;
21869 to_x += area_left;
21870 }
21871
21872 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21873 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21874 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21875
21876 /* Prevent inadvertently clearing to end of the X window. */
21877 if (to_x > from_x && to_y > from_y)
21878 {
21879 BLOCK_INPUT;
21880 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
21881 to_x - from_x, to_y - from_y);
21882 UNBLOCK_INPUT;
21883 }
21884 }
21885
21886 #endif /* HAVE_WINDOW_SYSTEM */
21887
21888
21889 \f
21890 /***********************************************************************
21891 Cursor types
21892 ***********************************************************************/
21893
21894 /* Value is the internal representation of the specified cursor type
21895 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21896 of the bar cursor. */
21897
21898 static enum text_cursor_kinds
21899 get_specified_cursor_type (arg, width)
21900 Lisp_Object arg;
21901 int *width;
21902 {
21903 enum text_cursor_kinds type;
21904
21905 if (NILP (arg))
21906 return NO_CURSOR;
21907
21908 if (EQ (arg, Qbox))
21909 return FILLED_BOX_CURSOR;
21910
21911 if (EQ (arg, Qhollow))
21912 return HOLLOW_BOX_CURSOR;
21913
21914 if (EQ (arg, Qbar))
21915 {
21916 *width = 2;
21917 return BAR_CURSOR;
21918 }
21919
21920 if (CONSP (arg)
21921 && EQ (XCAR (arg), Qbar)
21922 && INTEGERP (XCDR (arg))
21923 && XINT (XCDR (arg)) >= 0)
21924 {
21925 *width = XINT (XCDR (arg));
21926 return BAR_CURSOR;
21927 }
21928
21929 if (EQ (arg, Qhbar))
21930 {
21931 *width = 2;
21932 return HBAR_CURSOR;
21933 }
21934
21935 if (CONSP (arg)
21936 && EQ (XCAR (arg), Qhbar)
21937 && INTEGERP (XCDR (arg))
21938 && XINT (XCDR (arg)) >= 0)
21939 {
21940 *width = XINT (XCDR (arg));
21941 return HBAR_CURSOR;
21942 }
21943
21944 /* Treat anything unknown as "hollow box cursor".
21945 It was bad to signal an error; people have trouble fixing
21946 .Xdefaults with Emacs, when it has something bad in it. */
21947 type = HOLLOW_BOX_CURSOR;
21948
21949 return type;
21950 }
21951
21952 /* Set the default cursor types for specified frame. */
21953 void
21954 set_frame_cursor_types (f, arg)
21955 struct frame *f;
21956 Lisp_Object arg;
21957 {
21958 int width;
21959 Lisp_Object tem;
21960
21961 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21962 FRAME_CURSOR_WIDTH (f) = width;
21963
21964 /* By default, set up the blink-off state depending on the on-state. */
21965
21966 tem = Fassoc (arg, Vblink_cursor_alist);
21967 if (!NILP (tem))
21968 {
21969 FRAME_BLINK_OFF_CURSOR (f)
21970 = get_specified_cursor_type (XCDR (tem), &width);
21971 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21972 }
21973 else
21974 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21975 }
21976
21977
21978 /* Return the cursor we want to be displayed in window W. Return
21979 width of bar/hbar cursor through WIDTH arg. Return with
21980 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21981 (i.e. if the `system caret' should track this cursor).
21982
21983 In a mini-buffer window, we want the cursor only to appear if we
21984 are reading input from this window. For the selected window, we
21985 want the cursor type given by the frame parameter or buffer local
21986 setting of cursor-type. If explicitly marked off, draw no cursor.
21987 In all other cases, we want a hollow box cursor. */
21988
21989 static enum text_cursor_kinds
21990 get_window_cursor_type (w, glyph, width, active_cursor)
21991 struct window *w;
21992 struct glyph *glyph;
21993 int *width;
21994 int *active_cursor;
21995 {
21996 struct frame *f = XFRAME (w->frame);
21997 struct buffer *b = XBUFFER (w->buffer);
21998 int cursor_type = DEFAULT_CURSOR;
21999 Lisp_Object alt_cursor;
22000 int non_selected = 0;
22001
22002 *active_cursor = 1;
22003
22004 /* Echo area */
22005 if (cursor_in_echo_area
22006 && FRAME_HAS_MINIBUF_P (f)
22007 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
22008 {
22009 if (w == XWINDOW (echo_area_window))
22010 {
22011 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
22012 {
22013 *width = FRAME_CURSOR_WIDTH (f);
22014 return FRAME_DESIRED_CURSOR (f);
22015 }
22016 else
22017 return get_specified_cursor_type (b->cursor_type, width);
22018 }
22019
22020 *active_cursor = 0;
22021 non_selected = 1;
22022 }
22023
22024 /* Detect a nonselected window or nonselected frame. */
22025 else if (w != XWINDOW (f->selected_window)
22026 #ifdef HAVE_WINDOW_SYSTEM
22027 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
22028 #endif
22029 )
22030 {
22031 *active_cursor = 0;
22032
22033 if (MINI_WINDOW_P (w) && minibuf_level == 0)
22034 return NO_CURSOR;
22035
22036 non_selected = 1;
22037 }
22038
22039 /* Never display a cursor in a window in which cursor-type is nil. */
22040 if (NILP (b->cursor_type))
22041 return NO_CURSOR;
22042
22043 /* Get the normal cursor type for this window. */
22044 if (EQ (b->cursor_type, Qt))
22045 {
22046 cursor_type = FRAME_DESIRED_CURSOR (f);
22047 *width = FRAME_CURSOR_WIDTH (f);
22048 }
22049 else
22050 cursor_type = get_specified_cursor_type (b->cursor_type, width);
22051
22052 /* Use cursor-in-non-selected-windows instead
22053 for non-selected window or frame. */
22054 if (non_selected)
22055 {
22056 alt_cursor = b->cursor_in_non_selected_windows;
22057 if (!EQ (Qt, alt_cursor))
22058 return get_specified_cursor_type (alt_cursor, width);
22059 /* t means modify the normal cursor type. */
22060 if (cursor_type == FILLED_BOX_CURSOR)
22061 cursor_type = HOLLOW_BOX_CURSOR;
22062 else if (cursor_type == BAR_CURSOR && *width > 1)
22063 --*width;
22064 return cursor_type;
22065 }
22066
22067 /* Use normal cursor if not blinked off. */
22068 if (!w->cursor_off_p)
22069 {
22070 #ifdef HAVE_WINDOW_SYSTEM
22071 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22072 {
22073 if (cursor_type == FILLED_BOX_CURSOR)
22074 {
22075 /* Using a block cursor on large images can be very annoying.
22076 So use a hollow cursor for "large" images.
22077 If image is not transparent (no mask), also use hollow cursor. */
22078 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22079 if (img != NULL && IMAGEP (img->spec))
22080 {
22081 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
22082 where N = size of default frame font size.
22083 This should cover most of the "tiny" icons people may use. */
22084 if (!img->mask
22085 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
22086 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
22087 cursor_type = HOLLOW_BOX_CURSOR;
22088 }
22089 }
22090 else if (cursor_type != NO_CURSOR)
22091 {
22092 /* Display current only supports BOX and HOLLOW cursors for images.
22093 So for now, unconditionally use a HOLLOW cursor when cursor is
22094 not a solid box cursor. */
22095 cursor_type = HOLLOW_BOX_CURSOR;
22096 }
22097 }
22098 #endif
22099 return cursor_type;
22100 }
22101
22102 /* Cursor is blinked off, so determine how to "toggle" it. */
22103
22104 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
22105 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
22106 return get_specified_cursor_type (XCDR (alt_cursor), width);
22107
22108 /* Then see if frame has specified a specific blink off cursor type. */
22109 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
22110 {
22111 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
22112 return FRAME_BLINK_OFF_CURSOR (f);
22113 }
22114
22115 #if 0
22116 /* Some people liked having a permanently visible blinking cursor,
22117 while others had very strong opinions against it. So it was
22118 decided to remove it. KFS 2003-09-03 */
22119
22120 /* Finally perform built-in cursor blinking:
22121 filled box <-> hollow box
22122 wide [h]bar <-> narrow [h]bar
22123 narrow [h]bar <-> no cursor
22124 other type <-> no cursor */
22125
22126 if (cursor_type == FILLED_BOX_CURSOR)
22127 return HOLLOW_BOX_CURSOR;
22128
22129 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
22130 {
22131 *width = 1;
22132 return cursor_type;
22133 }
22134 #endif
22135
22136 return NO_CURSOR;
22137 }
22138
22139
22140 #ifdef HAVE_WINDOW_SYSTEM
22141
22142 /* Notice when the text cursor of window W has been completely
22143 overwritten by a drawing operation that outputs glyphs in AREA
22144 starting at X0 and ending at X1 in the line starting at Y0 and
22145 ending at Y1. X coordinates are area-relative. X1 < 0 means all
22146 the rest of the line after X0 has been written. Y coordinates
22147 are window-relative. */
22148
22149 static void
22150 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
22151 struct window *w;
22152 enum glyph_row_area area;
22153 int x0, y0, x1, y1;
22154 {
22155 int cx0, cx1, cy0, cy1;
22156 struct glyph_row *row;
22157
22158 if (!w->phys_cursor_on_p)
22159 return;
22160 if (area != TEXT_AREA)
22161 return;
22162
22163 if (w->phys_cursor.vpos < 0
22164 || w->phys_cursor.vpos >= w->current_matrix->nrows
22165 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
22166 !(row->enabled_p && row->displays_text_p)))
22167 return;
22168
22169 if (row->cursor_in_fringe_p)
22170 {
22171 row->cursor_in_fringe_p = 0;
22172 draw_fringe_bitmap (w, row, 0);
22173 w->phys_cursor_on_p = 0;
22174 return;
22175 }
22176
22177 cx0 = w->phys_cursor.x;
22178 cx1 = cx0 + w->phys_cursor_width;
22179 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
22180 return;
22181
22182 /* The cursor image will be completely removed from the
22183 screen if the output area intersects the cursor area in
22184 y-direction. When we draw in [y0 y1[, and some part of
22185 the cursor is at y < y0, that part must have been drawn
22186 before. When scrolling, the cursor is erased before
22187 actually scrolling, so we don't come here. When not
22188 scrolling, the rows above the old cursor row must have
22189 changed, and in this case these rows must have written
22190 over the cursor image.
22191
22192 Likewise if part of the cursor is below y1, with the
22193 exception of the cursor being in the first blank row at
22194 the buffer and window end because update_text_area
22195 doesn't draw that row. (Except when it does, but
22196 that's handled in update_text_area.) */
22197
22198 cy0 = w->phys_cursor.y;
22199 cy1 = cy0 + w->phys_cursor_height;
22200 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
22201 return;
22202
22203 w->phys_cursor_on_p = 0;
22204 }
22205
22206 #endif /* HAVE_WINDOW_SYSTEM */
22207
22208 \f
22209 /************************************************************************
22210 Mouse Face
22211 ************************************************************************/
22212
22213 #ifdef HAVE_WINDOW_SYSTEM
22214
22215 /* EXPORT for RIF:
22216 Fix the display of area AREA of overlapping row ROW in window W
22217 with respect to the overlapping part OVERLAPS. */
22218
22219 void
22220 x_fix_overlapping_area (w, row, area, overlaps)
22221 struct window *w;
22222 struct glyph_row *row;
22223 enum glyph_row_area area;
22224 int overlaps;
22225 {
22226 int i, x;
22227
22228 BLOCK_INPUT;
22229
22230 x = 0;
22231 for (i = 0; i < row->used[area];)
22232 {
22233 if (row->glyphs[area][i].overlaps_vertically_p)
22234 {
22235 int start = i, start_x = x;
22236
22237 do
22238 {
22239 x += row->glyphs[area][i].pixel_width;
22240 ++i;
22241 }
22242 while (i < row->used[area]
22243 && row->glyphs[area][i].overlaps_vertically_p);
22244
22245 draw_glyphs (w, start_x, row, area,
22246 start, i,
22247 DRAW_NORMAL_TEXT, overlaps);
22248 }
22249 else
22250 {
22251 x += row->glyphs[area][i].pixel_width;
22252 ++i;
22253 }
22254 }
22255
22256 UNBLOCK_INPUT;
22257 }
22258
22259
22260 /* EXPORT:
22261 Draw the cursor glyph of window W in glyph row ROW. See the
22262 comment of draw_glyphs for the meaning of HL. */
22263
22264 void
22265 draw_phys_cursor_glyph (w, row, hl)
22266 struct window *w;
22267 struct glyph_row *row;
22268 enum draw_glyphs_face hl;
22269 {
22270 /* If cursor hpos is out of bounds, don't draw garbage. This can
22271 happen in mini-buffer windows when switching between echo area
22272 glyphs and mini-buffer. */
22273 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
22274 {
22275 int on_p = w->phys_cursor_on_p;
22276 int x1;
22277 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
22278 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
22279 hl, 0);
22280 w->phys_cursor_on_p = on_p;
22281
22282 if (hl == DRAW_CURSOR)
22283 w->phys_cursor_width = x1 - w->phys_cursor.x;
22284 /* When we erase the cursor, and ROW is overlapped by other
22285 rows, make sure that these overlapping parts of other rows
22286 are redrawn. */
22287 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
22288 {
22289 w->phys_cursor_width = x1 - w->phys_cursor.x;
22290
22291 if (row > w->current_matrix->rows
22292 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
22293 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
22294 OVERLAPS_ERASED_CURSOR);
22295
22296 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
22297 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
22298 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
22299 OVERLAPS_ERASED_CURSOR);
22300 }
22301 }
22302 }
22303
22304
22305 /* EXPORT:
22306 Erase the image of a cursor of window W from the screen. */
22307
22308 void
22309 erase_phys_cursor (w)
22310 struct window *w;
22311 {
22312 struct frame *f = XFRAME (w->frame);
22313 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22314 int hpos = w->phys_cursor.hpos;
22315 int vpos = w->phys_cursor.vpos;
22316 int mouse_face_here_p = 0;
22317 struct glyph_matrix *active_glyphs = w->current_matrix;
22318 struct glyph_row *cursor_row;
22319 struct glyph *cursor_glyph;
22320 enum draw_glyphs_face hl;
22321
22322 /* No cursor displayed or row invalidated => nothing to do on the
22323 screen. */
22324 if (w->phys_cursor_type == NO_CURSOR)
22325 goto mark_cursor_off;
22326
22327 /* VPOS >= active_glyphs->nrows means that window has been resized.
22328 Don't bother to erase the cursor. */
22329 if (vpos >= active_glyphs->nrows)
22330 goto mark_cursor_off;
22331
22332 /* If row containing cursor is marked invalid, there is nothing we
22333 can do. */
22334 cursor_row = MATRIX_ROW (active_glyphs, vpos);
22335 if (!cursor_row->enabled_p)
22336 goto mark_cursor_off;
22337
22338 /* If line spacing is > 0, old cursor may only be partially visible in
22339 window after split-window. So adjust visible height. */
22340 cursor_row->visible_height = min (cursor_row->visible_height,
22341 window_text_bottom_y (w) - cursor_row->y);
22342
22343 /* If row is completely invisible, don't attempt to delete a cursor which
22344 isn't there. This can happen if cursor is at top of a window, and
22345 we switch to a buffer with a header line in that window. */
22346 if (cursor_row->visible_height <= 0)
22347 goto mark_cursor_off;
22348
22349 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
22350 if (cursor_row->cursor_in_fringe_p)
22351 {
22352 cursor_row->cursor_in_fringe_p = 0;
22353 draw_fringe_bitmap (w, cursor_row, 0);
22354 goto mark_cursor_off;
22355 }
22356
22357 /* This can happen when the new row is shorter than the old one.
22358 In this case, either draw_glyphs or clear_end_of_line
22359 should have cleared the cursor. Note that we wouldn't be
22360 able to erase the cursor in this case because we don't have a
22361 cursor glyph at hand. */
22362 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
22363 goto mark_cursor_off;
22364
22365 /* If the cursor is in the mouse face area, redisplay that when
22366 we clear the cursor. */
22367 if (! NILP (dpyinfo->mouse_face_window)
22368 && w == XWINDOW (dpyinfo->mouse_face_window)
22369 && (vpos > dpyinfo->mouse_face_beg_row
22370 || (vpos == dpyinfo->mouse_face_beg_row
22371 && hpos >= dpyinfo->mouse_face_beg_col))
22372 && (vpos < dpyinfo->mouse_face_end_row
22373 || (vpos == dpyinfo->mouse_face_end_row
22374 && hpos < dpyinfo->mouse_face_end_col))
22375 /* Don't redraw the cursor's spot in mouse face if it is at the
22376 end of a line (on a newline). The cursor appears there, but
22377 mouse highlighting does not. */
22378 && cursor_row->used[TEXT_AREA] > hpos)
22379 mouse_face_here_p = 1;
22380
22381 /* Maybe clear the display under the cursor. */
22382 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
22383 {
22384 int x, y, left_x;
22385 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
22386 int width;
22387
22388 cursor_glyph = get_phys_cursor_glyph (w);
22389 if (cursor_glyph == NULL)
22390 goto mark_cursor_off;
22391
22392 width = cursor_glyph->pixel_width;
22393 left_x = window_box_left_offset (w, TEXT_AREA);
22394 x = w->phys_cursor.x;
22395 if (x < left_x)
22396 width -= left_x - x;
22397 width = min (width, window_box_width (w, TEXT_AREA) - x);
22398 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
22399 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
22400
22401 if (width > 0)
22402 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
22403 }
22404
22405 /* Erase the cursor by redrawing the character underneath it. */
22406 if (mouse_face_here_p)
22407 hl = DRAW_MOUSE_FACE;
22408 else
22409 hl = DRAW_NORMAL_TEXT;
22410 draw_phys_cursor_glyph (w, cursor_row, hl);
22411
22412 mark_cursor_off:
22413 w->phys_cursor_on_p = 0;
22414 w->phys_cursor_type = NO_CURSOR;
22415 }
22416
22417
22418 /* EXPORT:
22419 Display or clear cursor of window W. If ON is zero, clear the
22420 cursor. If it is non-zero, display the cursor. If ON is nonzero,
22421 where to put the cursor is specified by HPOS, VPOS, X and Y. */
22422
22423 void
22424 display_and_set_cursor (w, on, hpos, vpos, x, y)
22425 struct window *w;
22426 int on, hpos, vpos, x, y;
22427 {
22428 struct frame *f = XFRAME (w->frame);
22429 int new_cursor_type;
22430 int new_cursor_width;
22431 int active_cursor;
22432 struct glyph_row *glyph_row;
22433 struct glyph *glyph;
22434
22435 /* This is pointless on invisible frames, and dangerous on garbaged
22436 windows and frames; in the latter case, the frame or window may
22437 be in the midst of changing its size, and x and y may be off the
22438 window. */
22439 if (! FRAME_VISIBLE_P (f)
22440 || FRAME_GARBAGED_P (f)
22441 || vpos >= w->current_matrix->nrows
22442 || hpos >= w->current_matrix->matrix_w)
22443 return;
22444
22445 /* If cursor is off and we want it off, return quickly. */
22446 if (!on && !w->phys_cursor_on_p)
22447 return;
22448
22449 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
22450 /* If cursor row is not enabled, we don't really know where to
22451 display the cursor. */
22452 if (!glyph_row->enabled_p)
22453 {
22454 w->phys_cursor_on_p = 0;
22455 return;
22456 }
22457
22458 glyph = NULL;
22459 if (!glyph_row->exact_window_width_line_p
22460 || hpos < glyph_row->used[TEXT_AREA])
22461 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
22462
22463 xassert (interrupt_input_blocked);
22464
22465 /* Set new_cursor_type to the cursor we want to be displayed. */
22466 new_cursor_type = get_window_cursor_type (w, glyph,
22467 &new_cursor_width, &active_cursor);
22468
22469 /* If cursor is currently being shown and we don't want it to be or
22470 it is in the wrong place, or the cursor type is not what we want,
22471 erase it. */
22472 if (w->phys_cursor_on_p
22473 && (!on
22474 || w->phys_cursor.x != x
22475 || w->phys_cursor.y != y
22476 || new_cursor_type != w->phys_cursor_type
22477 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
22478 && new_cursor_width != w->phys_cursor_width)))
22479 erase_phys_cursor (w);
22480
22481 /* Don't check phys_cursor_on_p here because that flag is only set
22482 to zero in some cases where we know that the cursor has been
22483 completely erased, to avoid the extra work of erasing the cursor
22484 twice. In other words, phys_cursor_on_p can be 1 and the cursor
22485 still not be visible, or it has only been partly erased. */
22486 if (on)
22487 {
22488 w->phys_cursor_ascent = glyph_row->ascent;
22489 w->phys_cursor_height = glyph_row->height;
22490
22491 /* Set phys_cursor_.* before x_draw_.* is called because some
22492 of them may need the information. */
22493 w->phys_cursor.x = x;
22494 w->phys_cursor.y = glyph_row->y;
22495 w->phys_cursor.hpos = hpos;
22496 w->phys_cursor.vpos = vpos;
22497 }
22498
22499 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
22500 new_cursor_type, new_cursor_width,
22501 on, active_cursor);
22502 }
22503
22504
22505 /* Switch the display of W's cursor on or off, according to the value
22506 of ON. */
22507
22508 static void
22509 update_window_cursor (w, on)
22510 struct window *w;
22511 int on;
22512 {
22513 /* Don't update cursor in windows whose frame is in the process
22514 of being deleted. */
22515 if (w->current_matrix)
22516 {
22517 BLOCK_INPUT;
22518 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
22519 w->phys_cursor.x, w->phys_cursor.y);
22520 UNBLOCK_INPUT;
22521 }
22522 }
22523
22524
22525 /* Call update_window_cursor with parameter ON_P on all leaf windows
22526 in the window tree rooted at W. */
22527
22528 static void
22529 update_cursor_in_window_tree (w, on_p)
22530 struct window *w;
22531 int on_p;
22532 {
22533 while (w)
22534 {
22535 if (!NILP (w->hchild))
22536 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
22537 else if (!NILP (w->vchild))
22538 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
22539 else
22540 update_window_cursor (w, on_p);
22541
22542 w = NILP (w->next) ? 0 : XWINDOW (w->next);
22543 }
22544 }
22545
22546
22547 /* EXPORT:
22548 Display the cursor on window W, or clear it, according to ON_P.
22549 Don't change the cursor's position. */
22550
22551 void
22552 x_update_cursor (f, on_p)
22553 struct frame *f;
22554 int on_p;
22555 {
22556 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
22557 }
22558
22559
22560 /* EXPORT:
22561 Clear the cursor of window W to background color, and mark the
22562 cursor as not shown. This is used when the text where the cursor
22563 is is about to be rewritten. */
22564
22565 void
22566 x_clear_cursor (w)
22567 struct window *w;
22568 {
22569 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
22570 update_window_cursor (w, 0);
22571 }
22572
22573
22574 /* EXPORT:
22575 Display the active region described by mouse_face_* according to DRAW. */
22576
22577 void
22578 show_mouse_face (dpyinfo, draw)
22579 Display_Info *dpyinfo;
22580 enum draw_glyphs_face draw;
22581 {
22582 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
22583 struct frame *f = XFRAME (WINDOW_FRAME (w));
22584
22585 if (/* If window is in the process of being destroyed, don't bother
22586 to do anything. */
22587 w->current_matrix != NULL
22588 /* Don't update mouse highlight if hidden */
22589 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
22590 /* Recognize when we are called to operate on rows that don't exist
22591 anymore. This can happen when a window is split. */
22592 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
22593 {
22594 int phys_cursor_on_p = w->phys_cursor_on_p;
22595 struct glyph_row *row, *first, *last;
22596
22597 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
22598 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
22599
22600 for (row = first; row <= last && row->enabled_p; ++row)
22601 {
22602 int start_hpos, end_hpos, start_x;
22603
22604 /* For all but the first row, the highlight starts at column 0. */
22605 if (row == first)
22606 {
22607 start_hpos = dpyinfo->mouse_face_beg_col;
22608 start_x = dpyinfo->mouse_face_beg_x;
22609 }
22610 else
22611 {
22612 start_hpos = 0;
22613 start_x = 0;
22614 }
22615
22616 if (row == last)
22617 end_hpos = dpyinfo->mouse_face_end_col;
22618 else
22619 {
22620 end_hpos = row->used[TEXT_AREA];
22621 if (draw == DRAW_NORMAL_TEXT)
22622 row->fill_line_p = 1; /* Clear to end of line */
22623 }
22624
22625 if (end_hpos > start_hpos)
22626 {
22627 draw_glyphs (w, start_x, row, TEXT_AREA,
22628 start_hpos, end_hpos,
22629 draw, 0);
22630
22631 row->mouse_face_p
22632 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
22633 }
22634 }
22635
22636 /* When we've written over the cursor, arrange for it to
22637 be displayed again. */
22638 if (phys_cursor_on_p && !w->phys_cursor_on_p)
22639 {
22640 BLOCK_INPUT;
22641 display_and_set_cursor (w, 1,
22642 w->phys_cursor.hpos, w->phys_cursor.vpos,
22643 w->phys_cursor.x, w->phys_cursor.y);
22644 UNBLOCK_INPUT;
22645 }
22646 }
22647
22648 /* Change the mouse cursor. */
22649 if (draw == DRAW_NORMAL_TEXT && !EQ (dpyinfo->mouse_face_window, f->tool_bar_window))
22650 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
22651 else if (draw == DRAW_MOUSE_FACE)
22652 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
22653 else
22654 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
22655 }
22656
22657 /* EXPORT:
22658 Clear out the mouse-highlighted active region.
22659 Redraw it un-highlighted first. Value is non-zero if mouse
22660 face was actually drawn unhighlighted. */
22661
22662 int
22663 clear_mouse_face (dpyinfo)
22664 Display_Info *dpyinfo;
22665 {
22666 int cleared = 0;
22667
22668 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
22669 {
22670 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
22671 cleared = 1;
22672 }
22673
22674 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22675 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22676 dpyinfo->mouse_face_window = Qnil;
22677 dpyinfo->mouse_face_overlay = Qnil;
22678 return cleared;
22679 }
22680
22681
22682 /* EXPORT:
22683 Non-zero if physical cursor of window W is within mouse face. */
22684
22685 int
22686 cursor_in_mouse_face_p (w)
22687 struct window *w;
22688 {
22689 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22690 int in_mouse_face = 0;
22691
22692 if (WINDOWP (dpyinfo->mouse_face_window)
22693 && XWINDOW (dpyinfo->mouse_face_window) == w)
22694 {
22695 int hpos = w->phys_cursor.hpos;
22696 int vpos = w->phys_cursor.vpos;
22697
22698 if (vpos >= dpyinfo->mouse_face_beg_row
22699 && vpos <= dpyinfo->mouse_face_end_row
22700 && (vpos > dpyinfo->mouse_face_beg_row
22701 || hpos >= dpyinfo->mouse_face_beg_col)
22702 && (vpos < dpyinfo->mouse_face_end_row
22703 || hpos < dpyinfo->mouse_face_end_col
22704 || dpyinfo->mouse_face_past_end))
22705 in_mouse_face = 1;
22706 }
22707
22708 return in_mouse_face;
22709 }
22710
22711
22712
22713 \f
22714 /* Find the glyph matrix position of buffer position CHARPOS in window
22715 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
22716 current glyphs must be up to date. If CHARPOS is above window
22717 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
22718 of last line in W. In the row containing CHARPOS, stop before glyphs
22719 having STOP as object. */
22720
22721 #if 1 /* This is a version of fast_find_position that's more correct
22722 in the presence of hscrolling, for example. I didn't install
22723 it right away because the problem fixed is minor, it failed
22724 in 20.x as well, and I think it's too risky to install
22725 so near the release of 21.1. 2001-09-25 gerd. */
22726
22727 #ifndef HAVE_CARBON
22728 static
22729 #endif
22730 int
22731 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
22732 struct window *w;
22733 EMACS_INT charpos;
22734 int *hpos, *vpos, *x, *y;
22735 Lisp_Object stop;
22736 {
22737 struct glyph_row *row, *first;
22738 struct glyph *glyph, *end;
22739 int past_end = 0;
22740
22741 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22742 if (charpos < MATRIX_ROW_START_CHARPOS (first))
22743 {
22744 *x = first->x;
22745 *y = first->y;
22746 *hpos = 0;
22747 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
22748 return 1;
22749 }
22750
22751 row = row_containing_pos (w, charpos, first, NULL, 0);
22752 if (row == NULL)
22753 {
22754 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
22755 past_end = 1;
22756 }
22757
22758 /* If whole rows or last part of a row came from a display overlay,
22759 row_containing_pos will skip over such rows because their end pos
22760 equals the start pos of the overlay or interval.
22761
22762 Move back if we have a STOP object and previous row's
22763 end glyph came from STOP. */
22764 if (!NILP (stop))
22765 {
22766 struct glyph_row *prev;
22767 while ((prev = row - 1, prev >= first)
22768 && MATRIX_ROW_END_CHARPOS (prev) == charpos
22769 && prev->used[TEXT_AREA] > 0)
22770 {
22771 struct glyph *beg = prev->glyphs[TEXT_AREA];
22772 glyph = beg + prev->used[TEXT_AREA];
22773 while (--glyph >= beg
22774 && INTEGERP (glyph->object));
22775 if (glyph < beg
22776 || !EQ (stop, glyph->object))
22777 break;
22778 row = prev;
22779 }
22780 }
22781
22782 *x = row->x;
22783 *y = row->y;
22784 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22785
22786 glyph = row->glyphs[TEXT_AREA];
22787 end = glyph + row->used[TEXT_AREA];
22788
22789 /* Skip over glyphs not having an object at the start of the row.
22790 These are special glyphs like truncation marks on terminal
22791 frames. */
22792 if (row->displays_text_p)
22793 while (glyph < end
22794 && INTEGERP (glyph->object)
22795 && !EQ (stop, glyph->object)
22796 && glyph->charpos < 0)
22797 {
22798 *x += glyph->pixel_width;
22799 ++glyph;
22800 }
22801
22802 while (glyph < end
22803 && !INTEGERP (glyph->object)
22804 && !EQ (stop, glyph->object)
22805 && (!BUFFERP (glyph->object)
22806 || glyph->charpos < charpos))
22807 {
22808 *x += glyph->pixel_width;
22809 ++glyph;
22810 }
22811
22812 *hpos = glyph - row->glyphs[TEXT_AREA];
22813 return !past_end;
22814 }
22815
22816 #else /* not 1 */
22817
22818 static int
22819 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22820 struct window *w;
22821 EMACS_INT pos;
22822 int *hpos, *vpos, *x, *y;
22823 Lisp_Object stop;
22824 {
22825 int i;
22826 int lastcol;
22827 int maybe_next_line_p = 0;
22828 int line_start_position;
22829 int yb = window_text_bottom_y (w);
22830 struct glyph_row *row, *best_row;
22831 int row_vpos, best_row_vpos;
22832 int current_x;
22833
22834 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22835 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22836
22837 while (row->y < yb)
22838 {
22839 if (row->used[TEXT_AREA])
22840 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22841 else
22842 line_start_position = 0;
22843
22844 if (line_start_position > pos)
22845 break;
22846 /* If the position sought is the end of the buffer,
22847 don't include the blank lines at the bottom of the window. */
22848 else if (line_start_position == pos
22849 && pos == BUF_ZV (XBUFFER (w->buffer)))
22850 {
22851 maybe_next_line_p = 1;
22852 break;
22853 }
22854 else if (line_start_position > 0)
22855 {
22856 best_row = row;
22857 best_row_vpos = row_vpos;
22858 }
22859
22860 if (row->y + row->height >= yb)
22861 break;
22862
22863 ++row;
22864 ++row_vpos;
22865 }
22866
22867 /* Find the right column within BEST_ROW. */
22868 lastcol = 0;
22869 current_x = best_row->x;
22870 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22871 {
22872 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22873 int charpos = glyph->charpos;
22874
22875 if (BUFFERP (glyph->object))
22876 {
22877 if (charpos == pos)
22878 {
22879 *hpos = i;
22880 *vpos = best_row_vpos;
22881 *x = current_x;
22882 *y = best_row->y;
22883 return 1;
22884 }
22885 else if (charpos > pos)
22886 break;
22887 }
22888 else if (EQ (glyph->object, stop))
22889 break;
22890
22891 if (charpos > 0)
22892 lastcol = i;
22893 current_x += glyph->pixel_width;
22894 }
22895
22896 /* If we're looking for the end of the buffer,
22897 and we didn't find it in the line we scanned,
22898 use the start of the following line. */
22899 if (maybe_next_line_p)
22900 {
22901 ++best_row;
22902 ++best_row_vpos;
22903 lastcol = 0;
22904 current_x = best_row->x;
22905 }
22906
22907 *vpos = best_row_vpos;
22908 *hpos = lastcol + 1;
22909 *x = current_x;
22910 *y = best_row->y;
22911 return 0;
22912 }
22913
22914 #endif /* not 1 */
22915
22916
22917 /* Find the position of the glyph for position POS in OBJECT in
22918 window W's current matrix, and return in *X, *Y the pixel
22919 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22920
22921 RIGHT_P non-zero means return the position of the right edge of the
22922 glyph, RIGHT_P zero means return the left edge position.
22923
22924 If no glyph for POS exists in the matrix, return the position of
22925 the glyph with the next smaller position that is in the matrix, if
22926 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22927 exists in the matrix, return the position of the glyph with the
22928 next larger position in OBJECT.
22929
22930 Value is non-zero if a glyph was found. */
22931
22932 static int
22933 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22934 struct window *w;
22935 EMACS_INT pos;
22936 Lisp_Object object;
22937 int *hpos, *vpos, *x, *y;
22938 int right_p;
22939 {
22940 int yb = window_text_bottom_y (w);
22941 struct glyph_row *r;
22942 struct glyph *best_glyph = NULL;
22943 struct glyph_row *best_row = NULL;
22944 int best_x = 0;
22945
22946 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22947 r->enabled_p && r->y < yb;
22948 ++r)
22949 {
22950 struct glyph *g = r->glyphs[TEXT_AREA];
22951 struct glyph *e = g + r->used[TEXT_AREA];
22952 int gx;
22953
22954 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22955 if (EQ (g->object, object))
22956 {
22957 if (g->charpos == pos)
22958 {
22959 best_glyph = g;
22960 best_x = gx;
22961 best_row = r;
22962 goto found;
22963 }
22964 else if (best_glyph == NULL
22965 || ((eabs (g->charpos - pos)
22966 < eabs (best_glyph->charpos - pos))
22967 && (right_p
22968 ? g->charpos < pos
22969 : g->charpos > pos)))
22970 {
22971 best_glyph = g;
22972 best_x = gx;
22973 best_row = r;
22974 }
22975 }
22976 }
22977
22978 found:
22979
22980 if (best_glyph)
22981 {
22982 *x = best_x;
22983 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22984
22985 if (right_p)
22986 {
22987 *x += best_glyph->pixel_width;
22988 ++*hpos;
22989 }
22990
22991 *y = best_row->y;
22992 *vpos = best_row - w->current_matrix->rows;
22993 }
22994
22995 return best_glyph != NULL;
22996 }
22997
22998
22999 /* See if position X, Y is within a hot-spot of an image. */
23000
23001 static int
23002 on_hot_spot_p (hot_spot, x, y)
23003 Lisp_Object hot_spot;
23004 int x, y;
23005 {
23006 if (!CONSP (hot_spot))
23007 return 0;
23008
23009 if (EQ (XCAR (hot_spot), Qrect))
23010 {
23011 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
23012 Lisp_Object rect = XCDR (hot_spot);
23013 Lisp_Object tem;
23014 if (!CONSP (rect))
23015 return 0;
23016 if (!CONSP (XCAR (rect)))
23017 return 0;
23018 if (!CONSP (XCDR (rect)))
23019 return 0;
23020 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
23021 return 0;
23022 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
23023 return 0;
23024 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
23025 return 0;
23026 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
23027 return 0;
23028 return 1;
23029 }
23030 else if (EQ (XCAR (hot_spot), Qcircle))
23031 {
23032 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
23033 Lisp_Object circ = XCDR (hot_spot);
23034 Lisp_Object lr, lx0, ly0;
23035 if (CONSP (circ)
23036 && CONSP (XCAR (circ))
23037 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
23038 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
23039 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
23040 {
23041 double r = XFLOATINT (lr);
23042 double dx = XINT (lx0) - x;
23043 double dy = XINT (ly0) - y;
23044 return (dx * dx + dy * dy <= r * r);
23045 }
23046 }
23047 else if (EQ (XCAR (hot_spot), Qpoly))
23048 {
23049 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
23050 if (VECTORP (XCDR (hot_spot)))
23051 {
23052 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
23053 Lisp_Object *poly = v->contents;
23054 int n = v->size;
23055 int i;
23056 int inside = 0;
23057 Lisp_Object lx, ly;
23058 int x0, y0;
23059
23060 /* Need an even number of coordinates, and at least 3 edges. */
23061 if (n < 6 || n & 1)
23062 return 0;
23063
23064 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
23065 If count is odd, we are inside polygon. Pixels on edges
23066 may or may not be included depending on actual geometry of the
23067 polygon. */
23068 if ((lx = poly[n-2], !INTEGERP (lx))
23069 || (ly = poly[n-1], !INTEGERP (lx)))
23070 return 0;
23071 x0 = XINT (lx), y0 = XINT (ly);
23072 for (i = 0; i < n; i += 2)
23073 {
23074 int x1 = x0, y1 = y0;
23075 if ((lx = poly[i], !INTEGERP (lx))
23076 || (ly = poly[i+1], !INTEGERP (ly)))
23077 return 0;
23078 x0 = XINT (lx), y0 = XINT (ly);
23079
23080 /* Does this segment cross the X line? */
23081 if (x0 >= x)
23082 {
23083 if (x1 >= x)
23084 continue;
23085 }
23086 else if (x1 < x)
23087 continue;
23088 if (y > y0 && y > y1)
23089 continue;
23090 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
23091 inside = !inside;
23092 }
23093 return inside;
23094 }
23095 }
23096 return 0;
23097 }
23098
23099 Lisp_Object
23100 find_hot_spot (map, x, y)
23101 Lisp_Object map;
23102 int x, y;
23103 {
23104 while (CONSP (map))
23105 {
23106 if (CONSP (XCAR (map))
23107 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
23108 return XCAR (map);
23109 map = XCDR (map);
23110 }
23111
23112 return Qnil;
23113 }
23114
23115 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
23116 3, 3, 0,
23117 doc: /* Lookup in image map MAP coordinates X and Y.
23118 An image map is an alist where each element has the format (AREA ID PLIST).
23119 An AREA is specified as either a rectangle, a circle, or a polygon:
23120 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
23121 pixel coordinates of the upper left and bottom right corners.
23122 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
23123 and the radius of the circle; r may be a float or integer.
23124 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
23125 vector describes one corner in the polygon.
23126 Returns the alist element for the first matching AREA in MAP. */)
23127 (map, x, y)
23128 Lisp_Object map;
23129 Lisp_Object x, y;
23130 {
23131 if (NILP (map))
23132 return Qnil;
23133
23134 CHECK_NUMBER (x);
23135 CHECK_NUMBER (y);
23136
23137 return find_hot_spot (map, XINT (x), XINT (y));
23138 }
23139
23140
23141 /* Display frame CURSOR, optionally using shape defined by POINTER. */
23142 static void
23143 define_frame_cursor1 (f, cursor, pointer)
23144 struct frame *f;
23145 Cursor cursor;
23146 Lisp_Object pointer;
23147 {
23148 /* Do not change cursor shape while dragging mouse. */
23149 if (!NILP (do_mouse_tracking))
23150 return;
23151
23152 if (!NILP (pointer))
23153 {
23154 if (EQ (pointer, Qarrow))
23155 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23156 else if (EQ (pointer, Qhand))
23157 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
23158 else if (EQ (pointer, Qtext))
23159 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23160 else if (EQ (pointer, intern ("hdrag")))
23161 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23162 #ifdef HAVE_X_WINDOWS
23163 else if (EQ (pointer, intern ("vdrag")))
23164 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
23165 #endif
23166 else if (EQ (pointer, intern ("hourglass")))
23167 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
23168 else if (EQ (pointer, Qmodeline))
23169 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
23170 else
23171 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23172 }
23173
23174 if (cursor != No_Cursor)
23175 FRAME_RIF (f)->define_frame_cursor (f, cursor);
23176 }
23177
23178 /* Take proper action when mouse has moved to the mode or header line
23179 or marginal area AREA of window W, x-position X and y-position Y.
23180 X is relative to the start of the text display area of W, so the
23181 width of bitmap areas and scroll bars must be subtracted to get a
23182 position relative to the start of the mode line. */
23183
23184 static void
23185 note_mode_line_or_margin_highlight (window, x, y, area)
23186 Lisp_Object window;
23187 int x, y;
23188 enum window_part area;
23189 {
23190 struct window *w = XWINDOW (window);
23191 struct frame *f = XFRAME (w->frame);
23192 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23193 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23194 Lisp_Object pointer = Qnil;
23195 int charpos, dx, dy, width, height;
23196 Lisp_Object string, object = Qnil;
23197 Lisp_Object pos, help;
23198
23199 Lisp_Object mouse_face;
23200 int original_x_pixel = x;
23201 struct glyph * glyph = NULL, * row_start_glyph = NULL;
23202 struct glyph_row *row;
23203
23204 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
23205 {
23206 int x0;
23207 struct glyph *end;
23208
23209 string = mode_line_string (w, area, &x, &y, &charpos,
23210 &object, &dx, &dy, &width, &height);
23211
23212 row = (area == ON_MODE_LINE
23213 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
23214 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
23215
23216 /* Find glyph */
23217 if (row->mode_line_p && row->enabled_p)
23218 {
23219 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
23220 end = glyph + row->used[TEXT_AREA];
23221
23222 for (x0 = original_x_pixel;
23223 glyph < end && x0 >= glyph->pixel_width;
23224 ++glyph)
23225 x0 -= glyph->pixel_width;
23226
23227 if (glyph >= end)
23228 glyph = NULL;
23229 }
23230 }
23231 else
23232 {
23233 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
23234 string = marginal_area_string (w, area, &x, &y, &charpos,
23235 &object, &dx, &dy, &width, &height);
23236 }
23237
23238 help = Qnil;
23239
23240 if (IMAGEP (object))
23241 {
23242 Lisp_Object image_map, hotspot;
23243 if ((image_map = Fplist_get (XCDR (object), QCmap),
23244 !NILP (image_map))
23245 && (hotspot = find_hot_spot (image_map, dx, dy),
23246 CONSP (hotspot))
23247 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23248 {
23249 Lisp_Object area_id, plist;
23250
23251 area_id = XCAR (hotspot);
23252 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23253 If so, we could look for mouse-enter, mouse-leave
23254 properties in PLIST (and do something...). */
23255 hotspot = XCDR (hotspot);
23256 if (CONSP (hotspot)
23257 && (plist = XCAR (hotspot), CONSP (plist)))
23258 {
23259 pointer = Fplist_get (plist, Qpointer);
23260 if (NILP (pointer))
23261 pointer = Qhand;
23262 help = Fplist_get (plist, Qhelp_echo);
23263 if (!NILP (help))
23264 {
23265 help_echo_string = help;
23266 /* Is this correct? ++kfs */
23267 XSETWINDOW (help_echo_window, w);
23268 help_echo_object = w->buffer;
23269 help_echo_pos = charpos;
23270 }
23271 }
23272 }
23273 if (NILP (pointer))
23274 pointer = Fplist_get (XCDR (object), QCpointer);
23275 }
23276
23277 if (STRINGP (string))
23278 {
23279 pos = make_number (charpos);
23280 /* If we're on a string with `help-echo' text property, arrange
23281 for the help to be displayed. This is done by setting the
23282 global variable help_echo_string to the help string. */
23283 if (NILP (help))
23284 {
23285 help = Fget_text_property (pos, Qhelp_echo, string);
23286 if (!NILP (help))
23287 {
23288 help_echo_string = help;
23289 XSETWINDOW (help_echo_window, w);
23290 help_echo_object = string;
23291 help_echo_pos = charpos;
23292 }
23293 }
23294
23295 if (NILP (pointer))
23296 pointer = Fget_text_property (pos, Qpointer, string);
23297
23298 /* Change the mouse pointer according to what is under X/Y. */
23299 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
23300 {
23301 Lisp_Object map;
23302 map = Fget_text_property (pos, Qlocal_map, string);
23303 if (!KEYMAPP (map))
23304 map = Fget_text_property (pos, Qkeymap, string);
23305 if (!KEYMAPP (map))
23306 cursor = dpyinfo->vertical_scroll_bar_cursor;
23307 }
23308
23309 /* Change the mouse face according to what is under X/Y. */
23310 mouse_face = Fget_text_property (pos, Qmouse_face, string);
23311 if (!NILP (mouse_face)
23312 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23313 && glyph)
23314 {
23315 Lisp_Object b, e;
23316
23317 struct glyph * tmp_glyph;
23318
23319 int gpos;
23320 int gseq_length;
23321 int total_pixel_width;
23322 EMACS_INT ignore;
23323
23324 int vpos, hpos;
23325
23326 b = Fprevious_single_property_change (make_number (charpos + 1),
23327 Qmouse_face, string, Qnil);
23328 if (NILP (b))
23329 b = make_number (0);
23330
23331 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
23332 if (NILP (e))
23333 e = make_number (SCHARS (string));
23334
23335 /* Calculate the position(glyph position: GPOS) of GLYPH in
23336 displayed string. GPOS is different from CHARPOS.
23337
23338 CHARPOS is the position of glyph in internal string
23339 object. A mode line string format has structures which
23340 is converted to a flatten by emacs lisp interpreter.
23341 The internal string is an element of the structures.
23342 The displayed string is the flatten string. */
23343 gpos = 0;
23344 if (glyph > row_start_glyph)
23345 {
23346 tmp_glyph = glyph - 1;
23347 while (tmp_glyph >= row_start_glyph
23348 && tmp_glyph->charpos >= XINT (b)
23349 && EQ (tmp_glyph->object, glyph->object))
23350 {
23351 tmp_glyph--;
23352 gpos++;
23353 }
23354 }
23355
23356 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
23357 displayed string holding GLYPH.
23358
23359 GSEQ_LENGTH is different from SCHARS (STRING).
23360 SCHARS (STRING) returns the length of the internal string. */
23361 for (tmp_glyph = glyph, gseq_length = gpos;
23362 tmp_glyph->charpos < XINT (e);
23363 tmp_glyph++, gseq_length++)
23364 {
23365 if (!EQ (tmp_glyph->object, glyph->object))
23366 break;
23367 }
23368
23369 total_pixel_width = 0;
23370 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
23371 total_pixel_width += tmp_glyph->pixel_width;
23372
23373 /* Pre calculation of re-rendering position */
23374 vpos = (x - gpos);
23375 hpos = (area == ON_MODE_LINE
23376 ? (w->current_matrix)->nrows - 1
23377 : 0);
23378
23379 /* If the re-rendering position is included in the last
23380 re-rendering area, we should do nothing. */
23381 if ( EQ (window, dpyinfo->mouse_face_window)
23382 && dpyinfo->mouse_face_beg_col <= vpos
23383 && vpos < dpyinfo->mouse_face_end_col
23384 && dpyinfo->mouse_face_beg_row == hpos )
23385 return;
23386
23387 if (clear_mouse_face (dpyinfo))
23388 cursor = No_Cursor;
23389
23390 dpyinfo->mouse_face_beg_col = vpos;
23391 dpyinfo->mouse_face_beg_row = hpos;
23392
23393 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
23394 dpyinfo->mouse_face_beg_y = 0;
23395
23396 dpyinfo->mouse_face_end_col = vpos + gseq_length;
23397 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
23398
23399 dpyinfo->mouse_face_end_x = 0;
23400 dpyinfo->mouse_face_end_y = 0;
23401
23402 dpyinfo->mouse_face_past_end = 0;
23403 dpyinfo->mouse_face_window = window;
23404
23405 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
23406 charpos,
23407 0, 0, 0, &ignore,
23408 glyph->face_id, 1);
23409 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23410
23411 if (NILP (pointer))
23412 pointer = Qhand;
23413 }
23414 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
23415 clear_mouse_face (dpyinfo);
23416 }
23417 define_frame_cursor1 (f, cursor, pointer);
23418 }
23419
23420
23421 /* EXPORT:
23422 Take proper action when the mouse has moved to position X, Y on
23423 frame F as regards highlighting characters that have mouse-face
23424 properties. Also de-highlighting chars where the mouse was before.
23425 X and Y can be negative or out of range. */
23426
23427 void
23428 note_mouse_highlight (f, x, y)
23429 struct frame *f;
23430 int x, y;
23431 {
23432 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23433 enum window_part part;
23434 Lisp_Object window;
23435 struct window *w;
23436 Cursor cursor = No_Cursor;
23437 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
23438 struct buffer *b;
23439
23440 /* When a menu is active, don't highlight because this looks odd. */
23441 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (MAC_OS)
23442 if (popup_activated ())
23443 return;
23444 #endif
23445
23446 if (NILP (Vmouse_highlight)
23447 || !f->glyphs_initialized_p)
23448 return;
23449
23450 dpyinfo->mouse_face_mouse_x = x;
23451 dpyinfo->mouse_face_mouse_y = y;
23452 dpyinfo->mouse_face_mouse_frame = f;
23453
23454 if (dpyinfo->mouse_face_defer)
23455 return;
23456
23457 if (gc_in_progress)
23458 {
23459 dpyinfo->mouse_face_deferred_gc = 1;
23460 return;
23461 }
23462
23463 /* Which window is that in? */
23464 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
23465
23466 /* If we were displaying active text in another window, clear that.
23467 Also clear if we move out of text area in same window. */
23468 if (! EQ (window, dpyinfo->mouse_face_window)
23469 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
23470 && !NILP (dpyinfo->mouse_face_window)))
23471 clear_mouse_face (dpyinfo);
23472
23473 /* Not on a window -> return. */
23474 if (!WINDOWP (window))
23475 return;
23476
23477 /* Reset help_echo_string. It will get recomputed below. */
23478 help_echo_string = Qnil;
23479
23480 /* Convert to window-relative pixel coordinates. */
23481 w = XWINDOW (window);
23482 frame_to_window_pixel_xy (w, &x, &y);
23483
23484 /* Handle tool-bar window differently since it doesn't display a
23485 buffer. */
23486 if (EQ (window, f->tool_bar_window))
23487 {
23488 note_tool_bar_highlight (f, x, y);
23489 return;
23490 }
23491
23492 /* Mouse is on the mode, header line or margin? */
23493 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
23494 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
23495 {
23496 note_mode_line_or_margin_highlight (window, x, y, part);
23497 return;
23498 }
23499
23500 if (part == ON_VERTICAL_BORDER)
23501 {
23502 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
23503 help_echo_string = build_string ("drag-mouse-1: resize");
23504 }
23505 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
23506 || part == ON_SCROLL_BAR)
23507 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23508 else
23509 cursor = FRAME_X_OUTPUT (f)->text_cursor;
23510
23511 /* Are we in a window whose display is up to date?
23512 And verify the buffer's text has not changed. */
23513 b = XBUFFER (w->buffer);
23514 if (part == ON_TEXT
23515 && EQ (w->window_end_valid, w->buffer)
23516 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
23517 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
23518 {
23519 int hpos, vpos, pos, i, dx, dy, area;
23520 struct glyph *glyph;
23521 Lisp_Object object;
23522 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
23523 Lisp_Object *overlay_vec = NULL;
23524 int noverlays;
23525 struct buffer *obuf;
23526 int obegv, ozv, same_region;
23527
23528 /* Find the glyph under X/Y. */
23529 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
23530
23531 /* Look for :pointer property on image. */
23532 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
23533 {
23534 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
23535 if (img != NULL && IMAGEP (img->spec))
23536 {
23537 Lisp_Object image_map, hotspot;
23538 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
23539 !NILP (image_map))
23540 && (hotspot = find_hot_spot (image_map,
23541 glyph->slice.x + dx,
23542 glyph->slice.y + dy),
23543 CONSP (hotspot))
23544 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
23545 {
23546 Lisp_Object area_id, plist;
23547
23548 area_id = XCAR (hotspot);
23549 /* Could check AREA_ID to see if we enter/leave this hot-spot.
23550 If so, we could look for mouse-enter, mouse-leave
23551 properties in PLIST (and do something...). */
23552 hotspot = XCDR (hotspot);
23553 if (CONSP (hotspot)
23554 && (plist = XCAR (hotspot), CONSP (plist)))
23555 {
23556 pointer = Fplist_get (plist, Qpointer);
23557 if (NILP (pointer))
23558 pointer = Qhand;
23559 help_echo_string = Fplist_get (plist, Qhelp_echo);
23560 if (!NILP (help_echo_string))
23561 {
23562 help_echo_window = window;
23563 help_echo_object = glyph->object;
23564 help_echo_pos = glyph->charpos;
23565 }
23566 }
23567 }
23568 if (NILP (pointer))
23569 pointer = Fplist_get (XCDR (img->spec), QCpointer);
23570 }
23571 }
23572
23573 /* Clear mouse face if X/Y not over text. */
23574 if (glyph == NULL
23575 || area != TEXT_AREA
23576 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
23577 {
23578 if (clear_mouse_face (dpyinfo))
23579 cursor = No_Cursor;
23580 if (NILP (pointer))
23581 {
23582 if (area != TEXT_AREA)
23583 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
23584 else
23585 pointer = Vvoid_text_area_pointer;
23586 }
23587 goto set_cursor;
23588 }
23589
23590 pos = glyph->charpos;
23591 object = glyph->object;
23592 if (!STRINGP (object) && !BUFFERP (object))
23593 goto set_cursor;
23594
23595 /* If we get an out-of-range value, return now; avoid an error. */
23596 if (BUFFERP (object) && pos > BUF_Z (b))
23597 goto set_cursor;
23598
23599 /* Make the window's buffer temporarily current for
23600 overlays_at and compute_char_face. */
23601 obuf = current_buffer;
23602 current_buffer = b;
23603 obegv = BEGV;
23604 ozv = ZV;
23605 BEGV = BEG;
23606 ZV = Z;
23607
23608 /* Is this char mouse-active or does it have help-echo? */
23609 position = make_number (pos);
23610
23611 if (BUFFERP (object))
23612 {
23613 /* Put all the overlays we want in a vector in overlay_vec. */
23614 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
23615 /* Sort overlays into increasing priority order. */
23616 noverlays = sort_overlays (overlay_vec, noverlays, w);
23617 }
23618 else
23619 noverlays = 0;
23620
23621 same_region = (EQ (window, dpyinfo->mouse_face_window)
23622 && vpos >= dpyinfo->mouse_face_beg_row
23623 && vpos <= dpyinfo->mouse_face_end_row
23624 && (vpos > dpyinfo->mouse_face_beg_row
23625 || hpos >= dpyinfo->mouse_face_beg_col)
23626 && (vpos < dpyinfo->mouse_face_end_row
23627 || hpos < dpyinfo->mouse_face_end_col
23628 || dpyinfo->mouse_face_past_end));
23629
23630 if (same_region)
23631 cursor = No_Cursor;
23632
23633 /* Check mouse-face highlighting. */
23634 if (! same_region
23635 /* If there exists an overlay with mouse-face overlapping
23636 the one we are currently highlighting, we have to
23637 check if we enter the overlapping overlay, and then
23638 highlight only that. */
23639 || (OVERLAYP (dpyinfo->mouse_face_overlay)
23640 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
23641 {
23642 /* Find the highest priority overlay that has a mouse-face
23643 property. */
23644 overlay = Qnil;
23645 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
23646 {
23647 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
23648 if (!NILP (mouse_face))
23649 overlay = overlay_vec[i];
23650 }
23651
23652 /* If we're actually highlighting the same overlay as
23653 before, there's no need to do that again. */
23654 if (!NILP (overlay)
23655 && EQ (overlay, dpyinfo->mouse_face_overlay))
23656 goto check_help_echo;
23657
23658 dpyinfo->mouse_face_overlay = overlay;
23659
23660 /* Clear the display of the old active region, if any. */
23661 if (clear_mouse_face (dpyinfo))
23662 cursor = No_Cursor;
23663
23664 /* If no overlay applies, get a text property. */
23665 if (NILP (overlay))
23666 mouse_face = Fget_text_property (position, Qmouse_face, object);
23667
23668 /* Handle the overlay case. */
23669 if (!NILP (overlay))
23670 {
23671 /* Find the range of text around this char that
23672 should be active. */
23673 Lisp_Object before, after;
23674 EMACS_INT ignore;
23675
23676 before = Foverlay_start (overlay);
23677 after = Foverlay_end (overlay);
23678 /* Record this as the current active region. */
23679 fast_find_position (w, XFASTINT (before),
23680 &dpyinfo->mouse_face_beg_col,
23681 &dpyinfo->mouse_face_beg_row,
23682 &dpyinfo->mouse_face_beg_x,
23683 &dpyinfo->mouse_face_beg_y, Qnil);
23684
23685 dpyinfo->mouse_face_past_end
23686 = !fast_find_position (w, XFASTINT (after),
23687 &dpyinfo->mouse_face_end_col,
23688 &dpyinfo->mouse_face_end_row,
23689 &dpyinfo->mouse_face_end_x,
23690 &dpyinfo->mouse_face_end_y, Qnil);
23691 dpyinfo->mouse_face_window = window;
23692
23693 dpyinfo->mouse_face_face_id
23694 = face_at_buffer_position (w, pos, 0, 0,
23695 &ignore, pos + 1,
23696 !dpyinfo->mouse_face_hidden);
23697
23698 /* Display it as active. */
23699 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23700 cursor = No_Cursor;
23701 }
23702 /* Handle the text property case. */
23703 else if (!NILP (mouse_face) && BUFFERP (object))
23704 {
23705 /* Find the range of text around this char that
23706 should be active. */
23707 Lisp_Object before, after, beginning, end;
23708 EMACS_INT ignore;
23709
23710 beginning = Fmarker_position (w->start);
23711 end = make_number (BUF_Z (XBUFFER (object))
23712 - XFASTINT (w->window_end_pos));
23713 before
23714 = Fprevious_single_property_change (make_number (pos + 1),
23715 Qmouse_face,
23716 object, beginning);
23717 after
23718 = Fnext_single_property_change (position, Qmouse_face,
23719 object, end);
23720
23721 /* Record this as the current active region. */
23722 fast_find_position (w, XFASTINT (before),
23723 &dpyinfo->mouse_face_beg_col,
23724 &dpyinfo->mouse_face_beg_row,
23725 &dpyinfo->mouse_face_beg_x,
23726 &dpyinfo->mouse_face_beg_y, Qnil);
23727 dpyinfo->mouse_face_past_end
23728 = !fast_find_position (w, XFASTINT (after),
23729 &dpyinfo->mouse_face_end_col,
23730 &dpyinfo->mouse_face_end_row,
23731 &dpyinfo->mouse_face_end_x,
23732 &dpyinfo->mouse_face_end_y, Qnil);
23733 dpyinfo->mouse_face_window = window;
23734
23735 if (BUFFERP (object))
23736 dpyinfo->mouse_face_face_id
23737 = face_at_buffer_position (w, pos, 0, 0,
23738 &ignore, pos + 1,
23739 !dpyinfo->mouse_face_hidden);
23740
23741 /* Display it as active. */
23742 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23743 cursor = No_Cursor;
23744 }
23745 else if (!NILP (mouse_face) && STRINGP (object))
23746 {
23747 Lisp_Object b, e;
23748 EMACS_INT ignore;
23749
23750 b = Fprevious_single_property_change (make_number (pos + 1),
23751 Qmouse_face,
23752 object, Qnil);
23753 e = Fnext_single_property_change (position, Qmouse_face,
23754 object, Qnil);
23755 if (NILP (b))
23756 b = make_number (0);
23757 if (NILP (e))
23758 e = make_number (SCHARS (object) - 1);
23759
23760 fast_find_string_pos (w, XINT (b), object,
23761 &dpyinfo->mouse_face_beg_col,
23762 &dpyinfo->mouse_face_beg_row,
23763 &dpyinfo->mouse_face_beg_x,
23764 &dpyinfo->mouse_face_beg_y, 0);
23765 fast_find_string_pos (w, XINT (e), object,
23766 &dpyinfo->mouse_face_end_col,
23767 &dpyinfo->mouse_face_end_row,
23768 &dpyinfo->mouse_face_end_x,
23769 &dpyinfo->mouse_face_end_y, 1);
23770 dpyinfo->mouse_face_past_end = 0;
23771 dpyinfo->mouse_face_window = window;
23772 dpyinfo->mouse_face_face_id
23773 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
23774 glyph->face_id, 1);
23775 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23776 cursor = No_Cursor;
23777 }
23778 else if (STRINGP (object) && NILP (mouse_face))
23779 {
23780 /* A string which doesn't have mouse-face, but
23781 the text ``under'' it might have. */
23782 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
23783 int start = MATRIX_ROW_START_CHARPOS (r);
23784
23785 pos = string_buffer_position (w, object, start);
23786 if (pos > 0)
23787 mouse_face = get_char_property_and_overlay (make_number (pos),
23788 Qmouse_face,
23789 w->buffer,
23790 &overlay);
23791 if (!NILP (mouse_face) && !NILP (overlay))
23792 {
23793 Lisp_Object before = Foverlay_start (overlay);
23794 Lisp_Object after = Foverlay_end (overlay);
23795 EMACS_INT ignore;
23796
23797 /* Note that we might not be able to find position
23798 BEFORE in the glyph matrix if the overlay is
23799 entirely covered by a `display' property. In
23800 this case, we overshoot. So let's stop in
23801 the glyph matrix before glyphs for OBJECT. */
23802 fast_find_position (w, XFASTINT (before),
23803 &dpyinfo->mouse_face_beg_col,
23804 &dpyinfo->mouse_face_beg_row,
23805 &dpyinfo->mouse_face_beg_x,
23806 &dpyinfo->mouse_face_beg_y,
23807 object);
23808
23809 dpyinfo->mouse_face_past_end
23810 = !fast_find_position (w, XFASTINT (after),
23811 &dpyinfo->mouse_face_end_col,
23812 &dpyinfo->mouse_face_end_row,
23813 &dpyinfo->mouse_face_end_x,
23814 &dpyinfo->mouse_face_end_y,
23815 Qnil);
23816 dpyinfo->mouse_face_window = window;
23817 dpyinfo->mouse_face_face_id
23818 = face_at_buffer_position (w, pos, 0, 0,
23819 &ignore, pos + 1,
23820 !dpyinfo->mouse_face_hidden);
23821
23822 /* Display it as active. */
23823 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23824 cursor = No_Cursor;
23825 }
23826 }
23827 }
23828
23829 check_help_echo:
23830
23831 /* Look for a `help-echo' property. */
23832 if (NILP (help_echo_string)) {
23833 Lisp_Object help, overlay;
23834
23835 /* Check overlays first. */
23836 help = overlay = Qnil;
23837 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23838 {
23839 overlay = overlay_vec[i];
23840 help = Foverlay_get (overlay, Qhelp_echo);
23841 }
23842
23843 if (!NILP (help))
23844 {
23845 help_echo_string = help;
23846 help_echo_window = window;
23847 help_echo_object = overlay;
23848 help_echo_pos = pos;
23849 }
23850 else
23851 {
23852 Lisp_Object object = glyph->object;
23853 int charpos = glyph->charpos;
23854
23855 /* Try text properties. */
23856 if (STRINGP (object)
23857 && charpos >= 0
23858 && charpos < SCHARS (object))
23859 {
23860 help = Fget_text_property (make_number (charpos),
23861 Qhelp_echo, object);
23862 if (NILP (help))
23863 {
23864 /* If the string itself doesn't specify a help-echo,
23865 see if the buffer text ``under'' it does. */
23866 struct glyph_row *r
23867 = MATRIX_ROW (w->current_matrix, vpos);
23868 int start = MATRIX_ROW_START_CHARPOS (r);
23869 int pos = string_buffer_position (w, object, start);
23870 if (pos > 0)
23871 {
23872 help = Fget_char_property (make_number (pos),
23873 Qhelp_echo, w->buffer);
23874 if (!NILP (help))
23875 {
23876 charpos = pos;
23877 object = w->buffer;
23878 }
23879 }
23880 }
23881 }
23882 else if (BUFFERP (object)
23883 && charpos >= BEGV
23884 && charpos < ZV)
23885 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23886 object);
23887
23888 if (!NILP (help))
23889 {
23890 help_echo_string = help;
23891 help_echo_window = window;
23892 help_echo_object = object;
23893 help_echo_pos = charpos;
23894 }
23895 }
23896 }
23897
23898 /* Look for a `pointer' property. */
23899 if (NILP (pointer))
23900 {
23901 /* Check overlays first. */
23902 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23903 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23904
23905 if (NILP (pointer))
23906 {
23907 Lisp_Object object = glyph->object;
23908 int charpos = glyph->charpos;
23909
23910 /* Try text properties. */
23911 if (STRINGP (object)
23912 && charpos >= 0
23913 && charpos < SCHARS (object))
23914 {
23915 pointer = Fget_text_property (make_number (charpos),
23916 Qpointer, object);
23917 if (NILP (pointer))
23918 {
23919 /* If the string itself doesn't specify a pointer,
23920 see if the buffer text ``under'' it does. */
23921 struct glyph_row *r
23922 = MATRIX_ROW (w->current_matrix, vpos);
23923 int start = MATRIX_ROW_START_CHARPOS (r);
23924 int pos = string_buffer_position (w, object, start);
23925 if (pos > 0)
23926 pointer = Fget_char_property (make_number (pos),
23927 Qpointer, w->buffer);
23928 }
23929 }
23930 else if (BUFFERP (object)
23931 && charpos >= BEGV
23932 && charpos < ZV)
23933 pointer = Fget_text_property (make_number (charpos),
23934 Qpointer, object);
23935 }
23936 }
23937
23938 BEGV = obegv;
23939 ZV = ozv;
23940 current_buffer = obuf;
23941 }
23942
23943 set_cursor:
23944
23945 define_frame_cursor1 (f, cursor, pointer);
23946 }
23947
23948
23949 /* EXPORT for RIF:
23950 Clear any mouse-face on window W. This function is part of the
23951 redisplay interface, and is called from try_window_id and similar
23952 functions to ensure the mouse-highlight is off. */
23953
23954 void
23955 x_clear_window_mouse_face (w)
23956 struct window *w;
23957 {
23958 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23959 Lisp_Object window;
23960
23961 BLOCK_INPUT;
23962 XSETWINDOW (window, w);
23963 if (EQ (window, dpyinfo->mouse_face_window))
23964 clear_mouse_face (dpyinfo);
23965 UNBLOCK_INPUT;
23966 }
23967
23968
23969 /* EXPORT:
23970 Just discard the mouse face information for frame F, if any.
23971 This is used when the size of F is changed. */
23972
23973 void
23974 cancel_mouse_face (f)
23975 struct frame *f;
23976 {
23977 Lisp_Object window;
23978 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23979
23980 window = dpyinfo->mouse_face_window;
23981 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23982 {
23983 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23984 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23985 dpyinfo->mouse_face_window = Qnil;
23986 }
23987 }
23988
23989
23990 #endif /* HAVE_WINDOW_SYSTEM */
23991
23992 \f
23993 /***********************************************************************
23994 Exposure Events
23995 ***********************************************************************/
23996
23997 #ifdef HAVE_WINDOW_SYSTEM
23998
23999 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
24000 which intersects rectangle R. R is in window-relative coordinates. */
24001
24002 static void
24003 expose_area (w, row, r, area)
24004 struct window *w;
24005 struct glyph_row *row;
24006 XRectangle *r;
24007 enum glyph_row_area area;
24008 {
24009 struct glyph *first = row->glyphs[area];
24010 struct glyph *end = row->glyphs[area] + row->used[area];
24011 struct glyph *last;
24012 int first_x, start_x, x;
24013
24014 if (area == TEXT_AREA && row->fill_line_p)
24015 /* If row extends face to end of line write the whole line. */
24016 draw_glyphs (w, 0, row, area,
24017 0, row->used[area],
24018 DRAW_NORMAL_TEXT, 0);
24019 else
24020 {
24021 /* Set START_X to the window-relative start position for drawing glyphs of
24022 AREA. The first glyph of the text area can be partially visible.
24023 The first glyphs of other areas cannot. */
24024 start_x = window_box_left_offset (w, area);
24025 x = start_x;
24026 if (area == TEXT_AREA)
24027 x += row->x;
24028
24029 /* Find the first glyph that must be redrawn. */
24030 while (first < end
24031 && x + first->pixel_width < r->x)
24032 {
24033 x += first->pixel_width;
24034 ++first;
24035 }
24036
24037 /* Find the last one. */
24038 last = first;
24039 first_x = x;
24040 while (last < end
24041 && x < r->x + r->width)
24042 {
24043 x += last->pixel_width;
24044 ++last;
24045 }
24046
24047 /* Repaint. */
24048 if (last > first)
24049 draw_glyphs (w, first_x - start_x, row, area,
24050 first - row->glyphs[area], last - row->glyphs[area],
24051 DRAW_NORMAL_TEXT, 0);
24052 }
24053 }
24054
24055
24056 /* Redraw the parts of the glyph row ROW on window W intersecting
24057 rectangle R. R is in window-relative coordinates. Value is
24058 non-zero if mouse-face was overwritten. */
24059
24060 static int
24061 expose_line (w, row, r)
24062 struct window *w;
24063 struct glyph_row *row;
24064 XRectangle *r;
24065 {
24066 xassert (row->enabled_p);
24067
24068 if (row->mode_line_p || w->pseudo_window_p)
24069 draw_glyphs (w, 0, row, TEXT_AREA,
24070 0, row->used[TEXT_AREA],
24071 DRAW_NORMAL_TEXT, 0);
24072 else
24073 {
24074 if (row->used[LEFT_MARGIN_AREA])
24075 expose_area (w, row, r, LEFT_MARGIN_AREA);
24076 if (row->used[TEXT_AREA])
24077 expose_area (w, row, r, TEXT_AREA);
24078 if (row->used[RIGHT_MARGIN_AREA])
24079 expose_area (w, row, r, RIGHT_MARGIN_AREA);
24080 draw_row_fringe_bitmaps (w, row);
24081 }
24082
24083 return row->mouse_face_p;
24084 }
24085
24086
24087 /* Redraw those parts of glyphs rows during expose event handling that
24088 overlap other rows. Redrawing of an exposed line writes over parts
24089 of lines overlapping that exposed line; this function fixes that.
24090
24091 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
24092 row in W's current matrix that is exposed and overlaps other rows.
24093 LAST_OVERLAPPING_ROW is the last such row. */
24094
24095 static void
24096 expose_overlaps (w, first_overlapping_row, last_overlapping_row, r)
24097 struct window *w;
24098 struct glyph_row *first_overlapping_row;
24099 struct glyph_row *last_overlapping_row;
24100 XRectangle *r;
24101 {
24102 struct glyph_row *row;
24103
24104 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
24105 if (row->overlapping_p)
24106 {
24107 xassert (row->enabled_p && !row->mode_line_p);
24108
24109 row->clip = r;
24110 if (row->used[LEFT_MARGIN_AREA])
24111 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
24112
24113 if (row->used[TEXT_AREA])
24114 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
24115
24116 if (row->used[RIGHT_MARGIN_AREA])
24117 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
24118 row->clip = NULL;
24119 }
24120 }
24121
24122
24123 /* Return non-zero if W's cursor intersects rectangle R. */
24124
24125 static int
24126 phys_cursor_in_rect_p (w, r)
24127 struct window *w;
24128 XRectangle *r;
24129 {
24130 XRectangle cr, result;
24131 struct glyph *cursor_glyph;
24132 struct glyph_row *row;
24133
24134 if (w->phys_cursor.vpos >= 0
24135 && w->phys_cursor.vpos < w->current_matrix->nrows
24136 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
24137 row->enabled_p)
24138 && row->cursor_in_fringe_p)
24139 {
24140 /* Cursor is in the fringe. */
24141 cr.x = window_box_right_offset (w,
24142 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
24143 ? RIGHT_MARGIN_AREA
24144 : TEXT_AREA));
24145 cr.y = row->y;
24146 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
24147 cr.height = row->height;
24148 return x_intersect_rectangles (&cr, r, &result);
24149 }
24150
24151 cursor_glyph = get_phys_cursor_glyph (w);
24152 if (cursor_glyph)
24153 {
24154 /* r is relative to W's box, but w->phys_cursor.x is relative
24155 to left edge of W's TEXT area. Adjust it. */
24156 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
24157 cr.y = w->phys_cursor.y;
24158 cr.width = cursor_glyph->pixel_width;
24159 cr.height = w->phys_cursor_height;
24160 /* ++KFS: W32 version used W32-specific IntersectRect here, but
24161 I assume the effect is the same -- and this is portable. */
24162 return x_intersect_rectangles (&cr, r, &result);
24163 }
24164 /* If we don't understand the format, pretend we're not in the hot-spot. */
24165 return 0;
24166 }
24167
24168
24169 /* EXPORT:
24170 Draw a vertical window border to the right of window W if W doesn't
24171 have vertical scroll bars. */
24172
24173 void
24174 x_draw_vertical_border (w)
24175 struct window *w;
24176 {
24177 struct frame *f = XFRAME (WINDOW_FRAME (w));
24178
24179 /* We could do better, if we knew what type of scroll-bar the adjacent
24180 windows (on either side) have... But we don't :-(
24181 However, I think this works ok. ++KFS 2003-04-25 */
24182
24183 /* Redraw borders between horizontally adjacent windows. Don't
24184 do it for frames with vertical scroll bars because either the
24185 right scroll bar of a window, or the left scroll bar of its
24186 neighbor will suffice as a border. */
24187 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
24188 return;
24189
24190 if (!WINDOW_RIGHTMOST_P (w)
24191 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
24192 {
24193 int x0, x1, y0, y1;
24194
24195 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24196 y1 -= 1;
24197
24198 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24199 x1 -= 1;
24200
24201 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
24202 }
24203 else if (!WINDOW_LEFTMOST_P (w)
24204 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
24205 {
24206 int x0, x1, y0, y1;
24207
24208 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
24209 y1 -= 1;
24210
24211 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
24212 x0 -= 1;
24213
24214 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
24215 }
24216 }
24217
24218
24219 /* Redraw the part of window W intersection rectangle FR. Pixel
24220 coordinates in FR are frame-relative. Call this function with
24221 input blocked. Value is non-zero if the exposure overwrites
24222 mouse-face. */
24223
24224 static int
24225 expose_window (w, fr)
24226 struct window *w;
24227 XRectangle *fr;
24228 {
24229 struct frame *f = XFRAME (w->frame);
24230 XRectangle wr, r;
24231 int mouse_face_overwritten_p = 0;
24232
24233 /* If window is not yet fully initialized, do nothing. This can
24234 happen when toolkit scroll bars are used and a window is split.
24235 Reconfiguring the scroll bar will generate an expose for a newly
24236 created window. */
24237 if (w->current_matrix == NULL)
24238 return 0;
24239
24240 /* When we're currently updating the window, display and current
24241 matrix usually don't agree. Arrange for a thorough display
24242 later. */
24243 if (w == updated_window)
24244 {
24245 SET_FRAME_GARBAGED (f);
24246 return 0;
24247 }
24248
24249 /* Frame-relative pixel rectangle of W. */
24250 wr.x = WINDOW_LEFT_EDGE_X (w);
24251 wr.y = WINDOW_TOP_EDGE_Y (w);
24252 wr.width = WINDOW_TOTAL_WIDTH (w);
24253 wr.height = WINDOW_TOTAL_HEIGHT (w);
24254
24255 if (x_intersect_rectangles (fr, &wr, &r))
24256 {
24257 int yb = window_text_bottom_y (w);
24258 struct glyph_row *row;
24259 int cursor_cleared_p;
24260 struct glyph_row *first_overlapping_row, *last_overlapping_row;
24261
24262 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
24263 r.x, r.y, r.width, r.height));
24264
24265 /* Convert to window coordinates. */
24266 r.x -= WINDOW_LEFT_EDGE_X (w);
24267 r.y -= WINDOW_TOP_EDGE_Y (w);
24268
24269 /* Turn off the cursor. */
24270 if (!w->pseudo_window_p
24271 && phys_cursor_in_rect_p (w, &r))
24272 {
24273 x_clear_cursor (w);
24274 cursor_cleared_p = 1;
24275 }
24276 else
24277 cursor_cleared_p = 0;
24278
24279 /* Update lines intersecting rectangle R. */
24280 first_overlapping_row = last_overlapping_row = NULL;
24281 for (row = w->current_matrix->rows;
24282 row->enabled_p;
24283 ++row)
24284 {
24285 int y0 = row->y;
24286 int y1 = MATRIX_ROW_BOTTOM_Y (row);
24287
24288 if ((y0 >= r.y && y0 < r.y + r.height)
24289 || (y1 > r.y && y1 < r.y + r.height)
24290 || (r.y >= y0 && r.y < y1)
24291 || (r.y + r.height > y0 && r.y + r.height < y1))
24292 {
24293 /* A header line may be overlapping, but there is no need
24294 to fix overlapping areas for them. KFS 2005-02-12 */
24295 if (row->overlapping_p && !row->mode_line_p)
24296 {
24297 if (first_overlapping_row == NULL)
24298 first_overlapping_row = row;
24299 last_overlapping_row = row;
24300 }
24301
24302 row->clip = fr;
24303 if (expose_line (w, row, &r))
24304 mouse_face_overwritten_p = 1;
24305 row->clip = NULL;
24306 }
24307 else if (row->overlapping_p)
24308 {
24309 /* We must redraw a row overlapping the exposed area. */
24310 if (y0 < r.y
24311 ? y0 + row->phys_height > r.y
24312 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
24313 {
24314 if (first_overlapping_row == NULL)
24315 first_overlapping_row = row;
24316 last_overlapping_row = row;
24317 }
24318 }
24319
24320 if (y1 >= yb)
24321 break;
24322 }
24323
24324 /* Display the mode line if there is one. */
24325 if (WINDOW_WANTS_MODELINE_P (w)
24326 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
24327 row->enabled_p)
24328 && row->y < r.y + r.height)
24329 {
24330 if (expose_line (w, row, &r))
24331 mouse_face_overwritten_p = 1;
24332 }
24333
24334 if (!w->pseudo_window_p)
24335 {
24336 /* Fix the display of overlapping rows. */
24337 if (first_overlapping_row)
24338 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
24339 fr);
24340
24341 /* Draw border between windows. */
24342 x_draw_vertical_border (w);
24343
24344 /* Turn the cursor on again. */
24345 if (cursor_cleared_p)
24346 update_window_cursor (w, 1);
24347 }
24348 }
24349
24350 return mouse_face_overwritten_p;
24351 }
24352
24353
24354
24355 /* Redraw (parts) of all windows in the window tree rooted at W that
24356 intersect R. R contains frame pixel coordinates. Value is
24357 non-zero if the exposure overwrites mouse-face. */
24358
24359 static int
24360 expose_window_tree (w, r)
24361 struct window *w;
24362 XRectangle *r;
24363 {
24364 struct frame *f = XFRAME (w->frame);
24365 int mouse_face_overwritten_p = 0;
24366
24367 while (w && !FRAME_GARBAGED_P (f))
24368 {
24369 if (!NILP (w->hchild))
24370 mouse_face_overwritten_p
24371 |= expose_window_tree (XWINDOW (w->hchild), r);
24372 else if (!NILP (w->vchild))
24373 mouse_face_overwritten_p
24374 |= expose_window_tree (XWINDOW (w->vchild), r);
24375 else
24376 mouse_face_overwritten_p |= expose_window (w, r);
24377
24378 w = NILP (w->next) ? NULL : XWINDOW (w->next);
24379 }
24380
24381 return mouse_face_overwritten_p;
24382 }
24383
24384
24385 /* EXPORT:
24386 Redisplay an exposed area of frame F. X and Y are the upper-left
24387 corner of the exposed rectangle. W and H are width and height of
24388 the exposed area. All are pixel values. W or H zero means redraw
24389 the entire frame. */
24390
24391 void
24392 expose_frame (f, x, y, w, h)
24393 struct frame *f;
24394 int x, y, w, h;
24395 {
24396 XRectangle r;
24397 int mouse_face_overwritten_p = 0;
24398
24399 TRACE ((stderr, "expose_frame "));
24400
24401 /* No need to redraw if frame will be redrawn soon. */
24402 if (FRAME_GARBAGED_P (f))
24403 {
24404 TRACE ((stderr, " garbaged\n"));
24405 return;
24406 }
24407
24408 /* If basic faces haven't been realized yet, there is no point in
24409 trying to redraw anything. This can happen when we get an expose
24410 event while Emacs is starting, e.g. by moving another window. */
24411 if (FRAME_FACE_CACHE (f) == NULL
24412 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
24413 {
24414 TRACE ((stderr, " no faces\n"));
24415 return;
24416 }
24417
24418 if (w == 0 || h == 0)
24419 {
24420 r.x = r.y = 0;
24421 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
24422 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
24423 }
24424 else
24425 {
24426 r.x = x;
24427 r.y = y;
24428 r.width = w;
24429 r.height = h;
24430 }
24431
24432 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
24433 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
24434
24435 if (WINDOWP (f->tool_bar_window))
24436 mouse_face_overwritten_p
24437 |= expose_window (XWINDOW (f->tool_bar_window), &r);
24438
24439 #ifdef HAVE_X_WINDOWS
24440 #ifndef MSDOS
24441 #ifndef USE_X_TOOLKIT
24442 if (WINDOWP (f->menu_bar_window))
24443 mouse_face_overwritten_p
24444 |= expose_window (XWINDOW (f->menu_bar_window), &r);
24445 #endif /* not USE_X_TOOLKIT */
24446 #endif
24447 #endif
24448
24449 /* Some window managers support a focus-follows-mouse style with
24450 delayed raising of frames. Imagine a partially obscured frame,
24451 and moving the mouse into partially obscured mouse-face on that
24452 frame. The visible part of the mouse-face will be highlighted,
24453 then the WM raises the obscured frame. With at least one WM, KDE
24454 2.1, Emacs is not getting any event for the raising of the frame
24455 (even tried with SubstructureRedirectMask), only Expose events.
24456 These expose events will draw text normally, i.e. not
24457 highlighted. Which means we must redo the highlight here.
24458 Subsume it under ``we love X''. --gerd 2001-08-15 */
24459 /* Included in Windows version because Windows most likely does not
24460 do the right thing if any third party tool offers
24461 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
24462 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
24463 {
24464 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
24465 if (f == dpyinfo->mouse_face_mouse_frame)
24466 {
24467 int x = dpyinfo->mouse_face_mouse_x;
24468 int y = dpyinfo->mouse_face_mouse_y;
24469 clear_mouse_face (dpyinfo);
24470 note_mouse_highlight (f, x, y);
24471 }
24472 }
24473 }
24474
24475
24476 /* EXPORT:
24477 Determine the intersection of two rectangles R1 and R2. Return
24478 the intersection in *RESULT. Value is non-zero if RESULT is not
24479 empty. */
24480
24481 int
24482 x_intersect_rectangles (r1, r2, result)
24483 XRectangle *r1, *r2, *result;
24484 {
24485 XRectangle *left, *right;
24486 XRectangle *upper, *lower;
24487 int intersection_p = 0;
24488
24489 /* Rearrange so that R1 is the left-most rectangle. */
24490 if (r1->x < r2->x)
24491 left = r1, right = r2;
24492 else
24493 left = r2, right = r1;
24494
24495 /* X0 of the intersection is right.x0, if this is inside R1,
24496 otherwise there is no intersection. */
24497 if (right->x <= left->x + left->width)
24498 {
24499 result->x = right->x;
24500
24501 /* The right end of the intersection is the minimum of the
24502 the right ends of left and right. */
24503 result->width = (min (left->x + left->width, right->x + right->width)
24504 - result->x);
24505
24506 /* Same game for Y. */
24507 if (r1->y < r2->y)
24508 upper = r1, lower = r2;
24509 else
24510 upper = r2, lower = r1;
24511
24512 /* The upper end of the intersection is lower.y0, if this is inside
24513 of upper. Otherwise, there is no intersection. */
24514 if (lower->y <= upper->y + upper->height)
24515 {
24516 result->y = lower->y;
24517
24518 /* The lower end of the intersection is the minimum of the lower
24519 ends of upper and lower. */
24520 result->height = (min (lower->y + lower->height,
24521 upper->y + upper->height)
24522 - result->y);
24523 intersection_p = 1;
24524 }
24525 }
24526
24527 return intersection_p;
24528 }
24529
24530 #endif /* HAVE_WINDOW_SYSTEM */
24531
24532 \f
24533 /***********************************************************************
24534 Initialization
24535 ***********************************************************************/
24536
24537 void
24538 syms_of_xdisp ()
24539 {
24540 Vwith_echo_area_save_vector = Qnil;
24541 staticpro (&Vwith_echo_area_save_vector);
24542
24543 Vmessage_stack = Qnil;
24544 staticpro (&Vmessage_stack);
24545
24546 Qinhibit_redisplay = intern ("inhibit-redisplay");
24547 staticpro (&Qinhibit_redisplay);
24548
24549 message_dolog_marker1 = Fmake_marker ();
24550 staticpro (&message_dolog_marker1);
24551 message_dolog_marker2 = Fmake_marker ();
24552 staticpro (&message_dolog_marker2);
24553 message_dolog_marker3 = Fmake_marker ();
24554 staticpro (&message_dolog_marker3);
24555
24556 #if GLYPH_DEBUG
24557 defsubr (&Sdump_frame_glyph_matrix);
24558 defsubr (&Sdump_glyph_matrix);
24559 defsubr (&Sdump_glyph_row);
24560 defsubr (&Sdump_tool_bar_row);
24561 defsubr (&Strace_redisplay);
24562 defsubr (&Strace_to_stderr);
24563 #endif
24564 #ifdef HAVE_WINDOW_SYSTEM
24565 defsubr (&Stool_bar_lines_needed);
24566 defsubr (&Slookup_image_map);
24567 #endif
24568 defsubr (&Sformat_mode_line);
24569 defsubr (&Sinvisible_p);
24570
24571 staticpro (&Qmenu_bar_update_hook);
24572 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
24573
24574 staticpro (&Qoverriding_terminal_local_map);
24575 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
24576
24577 staticpro (&Qoverriding_local_map);
24578 Qoverriding_local_map = intern ("overriding-local-map");
24579
24580 staticpro (&Qwindow_scroll_functions);
24581 Qwindow_scroll_functions = intern ("window-scroll-functions");
24582
24583 staticpro (&Qwindow_text_change_functions);
24584 Qwindow_text_change_functions = intern ("window-text-change-functions");
24585
24586 staticpro (&Qredisplay_end_trigger_functions);
24587 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
24588
24589 staticpro (&Qinhibit_point_motion_hooks);
24590 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
24591
24592 Qeval = intern ("eval");
24593 staticpro (&Qeval);
24594
24595 QCdata = intern (":data");
24596 staticpro (&QCdata);
24597 Qdisplay = intern ("display");
24598 staticpro (&Qdisplay);
24599 Qspace_width = intern ("space-width");
24600 staticpro (&Qspace_width);
24601 Qraise = intern ("raise");
24602 staticpro (&Qraise);
24603 Qslice = intern ("slice");
24604 staticpro (&Qslice);
24605 Qspace = intern ("space");
24606 staticpro (&Qspace);
24607 Qmargin = intern ("margin");
24608 staticpro (&Qmargin);
24609 Qpointer = intern ("pointer");
24610 staticpro (&Qpointer);
24611 Qleft_margin = intern ("left-margin");
24612 staticpro (&Qleft_margin);
24613 Qright_margin = intern ("right-margin");
24614 staticpro (&Qright_margin);
24615 Qcenter = intern ("center");
24616 staticpro (&Qcenter);
24617 Qline_height = intern ("line-height");
24618 staticpro (&Qline_height);
24619 QCalign_to = intern (":align-to");
24620 staticpro (&QCalign_to);
24621 QCrelative_width = intern (":relative-width");
24622 staticpro (&QCrelative_width);
24623 QCrelative_height = intern (":relative-height");
24624 staticpro (&QCrelative_height);
24625 QCeval = intern (":eval");
24626 staticpro (&QCeval);
24627 QCpropertize = intern (":propertize");
24628 staticpro (&QCpropertize);
24629 QCfile = intern (":file");
24630 staticpro (&QCfile);
24631 Qfontified = intern ("fontified");
24632 staticpro (&Qfontified);
24633 Qfontification_functions = intern ("fontification-functions");
24634 staticpro (&Qfontification_functions);
24635 Qtrailing_whitespace = intern ("trailing-whitespace");
24636 staticpro (&Qtrailing_whitespace);
24637 Qescape_glyph = intern ("escape-glyph");
24638 staticpro (&Qescape_glyph);
24639 Qnobreak_space = intern ("nobreak-space");
24640 staticpro (&Qnobreak_space);
24641 Qimage = intern ("image");
24642 staticpro (&Qimage);
24643 QCmap = intern (":map");
24644 staticpro (&QCmap);
24645 QCpointer = intern (":pointer");
24646 staticpro (&QCpointer);
24647 Qrect = intern ("rect");
24648 staticpro (&Qrect);
24649 Qcircle = intern ("circle");
24650 staticpro (&Qcircle);
24651 Qpoly = intern ("poly");
24652 staticpro (&Qpoly);
24653 Qmessage_truncate_lines = intern ("message-truncate-lines");
24654 staticpro (&Qmessage_truncate_lines);
24655 Qgrow_only = intern ("grow-only");
24656 staticpro (&Qgrow_only);
24657 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
24658 staticpro (&Qinhibit_menubar_update);
24659 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
24660 staticpro (&Qinhibit_eval_during_redisplay);
24661 Qposition = intern ("position");
24662 staticpro (&Qposition);
24663 Qbuffer_position = intern ("buffer-position");
24664 staticpro (&Qbuffer_position);
24665 Qobject = intern ("object");
24666 staticpro (&Qobject);
24667 Qbar = intern ("bar");
24668 staticpro (&Qbar);
24669 Qhbar = intern ("hbar");
24670 staticpro (&Qhbar);
24671 Qbox = intern ("box");
24672 staticpro (&Qbox);
24673 Qhollow = intern ("hollow");
24674 staticpro (&Qhollow);
24675 Qhand = intern ("hand");
24676 staticpro (&Qhand);
24677 Qarrow = intern ("arrow");
24678 staticpro (&Qarrow);
24679 Qtext = intern ("text");
24680 staticpro (&Qtext);
24681 Qrisky_local_variable = intern ("risky-local-variable");
24682 staticpro (&Qrisky_local_variable);
24683 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
24684 staticpro (&Qinhibit_free_realized_faces);
24685
24686 list_of_error = Fcons (Fcons (intern ("error"),
24687 Fcons (intern ("void-variable"), Qnil)),
24688 Qnil);
24689 staticpro (&list_of_error);
24690
24691 Qlast_arrow_position = intern ("last-arrow-position");
24692 staticpro (&Qlast_arrow_position);
24693 Qlast_arrow_string = intern ("last-arrow-string");
24694 staticpro (&Qlast_arrow_string);
24695
24696 Qoverlay_arrow_string = intern ("overlay-arrow-string");
24697 staticpro (&Qoverlay_arrow_string);
24698 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
24699 staticpro (&Qoverlay_arrow_bitmap);
24700
24701 echo_buffer[0] = echo_buffer[1] = Qnil;
24702 staticpro (&echo_buffer[0]);
24703 staticpro (&echo_buffer[1]);
24704
24705 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
24706 staticpro (&echo_area_buffer[0]);
24707 staticpro (&echo_area_buffer[1]);
24708
24709 Vmessages_buffer_name = build_string ("*Messages*");
24710 staticpro (&Vmessages_buffer_name);
24711
24712 mode_line_proptrans_alist = Qnil;
24713 staticpro (&mode_line_proptrans_alist);
24714 mode_line_string_list = Qnil;
24715 staticpro (&mode_line_string_list);
24716 mode_line_string_face = Qnil;
24717 staticpro (&mode_line_string_face);
24718 mode_line_string_face_prop = Qnil;
24719 staticpro (&mode_line_string_face_prop);
24720 Vmode_line_unwind_vector = Qnil;
24721 staticpro (&Vmode_line_unwind_vector);
24722
24723 help_echo_string = Qnil;
24724 staticpro (&help_echo_string);
24725 help_echo_object = Qnil;
24726 staticpro (&help_echo_object);
24727 help_echo_window = Qnil;
24728 staticpro (&help_echo_window);
24729 previous_help_echo_string = Qnil;
24730 staticpro (&previous_help_echo_string);
24731 help_echo_pos = -1;
24732
24733 #ifdef HAVE_WINDOW_SYSTEM
24734 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
24735 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
24736 For example, if a block cursor is over a tab, it will be drawn as
24737 wide as that tab on the display. */);
24738 x_stretch_cursor_p = 0;
24739 #endif
24740
24741 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
24742 doc: /* *Non-nil means highlight trailing whitespace.
24743 The face used for trailing whitespace is `trailing-whitespace'. */);
24744 Vshow_trailing_whitespace = Qnil;
24745
24746 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
24747 doc: /* *Control highlighting of nobreak space and soft hyphen.
24748 A value of t means highlight the character itself (for nobreak space,
24749 use face `nobreak-space').
24750 A value of nil means no highlighting.
24751 Other values mean display the escape glyph followed by an ordinary
24752 space or ordinary hyphen. */);
24753 Vnobreak_char_display = Qt;
24754
24755 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
24756 doc: /* *The pointer shape to show in void text areas.
24757 A value of nil means to show the text pointer. Other options are `arrow',
24758 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
24759 Vvoid_text_area_pointer = Qarrow;
24760
24761 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
24762 doc: /* Non-nil means don't actually do any redisplay.
24763 This is used for internal purposes. */);
24764 Vinhibit_redisplay = Qnil;
24765
24766 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
24767 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
24768 Vglobal_mode_string = Qnil;
24769
24770 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
24771 doc: /* Marker for where to display an arrow on top of the buffer text.
24772 This must be the beginning of a line in order to work.
24773 See also `overlay-arrow-string'. */);
24774 Voverlay_arrow_position = Qnil;
24775
24776 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
24777 doc: /* String to display as an arrow in non-window frames.
24778 See also `overlay-arrow-position'. */);
24779 Voverlay_arrow_string = build_string ("=>");
24780
24781 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
24782 doc: /* List of variables (symbols) which hold markers for overlay arrows.
24783 The symbols on this list are examined during redisplay to determine
24784 where to display overlay arrows. */);
24785 Voverlay_arrow_variable_list
24786 = Fcons (intern ("overlay-arrow-position"), Qnil);
24787
24788 DEFVAR_INT ("scroll-step", &scroll_step,
24789 doc: /* *The number of lines to try scrolling a window by when point moves out.
24790 If that fails to bring point back on frame, point is centered instead.
24791 If this is zero, point is always centered after it moves off frame.
24792 If you want scrolling to always be a line at a time, you should set
24793 `scroll-conservatively' to a large value rather than set this to 1. */);
24794
24795 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
24796 doc: /* *Scroll up to this many lines, to bring point back on screen.
24797 If point moves off-screen, redisplay will scroll by up to
24798 `scroll-conservatively' lines in order to bring point just barely
24799 onto the screen again. If that cannot be done, then redisplay
24800 recenters point as usual.
24801
24802 A value of zero means always recenter point if it moves off screen. */);
24803 scroll_conservatively = 0;
24804
24805 DEFVAR_INT ("scroll-margin", &scroll_margin,
24806 doc: /* *Number of lines of margin at the top and bottom of a window.
24807 Recenter the window whenever point gets within this many lines
24808 of the top or bottom of the window. */);
24809 scroll_margin = 0;
24810
24811 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
24812 doc: /* Pixels per inch value for non-window system displays.
24813 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
24814 Vdisplay_pixels_per_inch = make_float (72.0);
24815
24816 #if GLYPH_DEBUG
24817 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
24818 #endif
24819
24820 DEFVAR_LISP ("truncate-partial-width-windows",
24821 &Vtruncate_partial_width_windows,
24822 doc: /* Non-nil means truncate lines in windows with less than the frame width.
24823 For an integer value, truncate lines in each window with less than the
24824 full frame width, provided the window width is less than that integer;
24825 otherwise, respect the value of `truncate-lines'.
24826
24827 For any other non-nil value, truncate lines in all windows with
24828 less than the full frame width.
24829
24830 A value of nil means to respect the value of `truncate-lines'. */);
24831 Vtruncate_partial_width_windows = make_number (30);
24832
24833 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
24834 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
24835 Any other value means to use the appropriate face, `mode-line',
24836 `header-line', or `menu' respectively. */);
24837 mode_line_inverse_video = 1;
24838
24839 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
24840 doc: /* *Maximum buffer size for which line number should be displayed.
24841 If the buffer is bigger than this, the line number does not appear
24842 in the mode line. A value of nil means no limit. */);
24843 Vline_number_display_limit = Qnil;
24844
24845 DEFVAR_INT ("line-number-display-limit-width",
24846 &line_number_display_limit_width,
24847 doc: /* *Maximum line width (in characters) for line number display.
24848 If the average length of the lines near point is bigger than this, then the
24849 line number may be omitted from the mode line. */);
24850 line_number_display_limit_width = 200;
24851
24852 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
24853 doc: /* *Non-nil means highlight region even in nonselected windows. */);
24854 highlight_nonselected_windows = 0;
24855
24856 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
24857 doc: /* Non-nil if more than one frame is visible on this display.
24858 Minibuffer-only frames don't count, but iconified frames do.
24859 This variable is not guaranteed to be accurate except while processing
24860 `frame-title-format' and `icon-title-format'. */);
24861
24862 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
24863 doc: /* Template for displaying the title bar of visible frames.
24864 \(Assuming the window manager supports this feature.)
24865
24866 This variable has the same structure as `mode-line-format', except that
24867 the %c and %l constructs are ignored. It is used only on frames for
24868 which no explicit name has been set \(see `modify-frame-parameters'). */);
24869
24870 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
24871 doc: /* Template for displaying the title bar of an iconified frame.
24872 \(Assuming the window manager supports this feature.)
24873 This variable has the same structure as `mode-line-format' (which see),
24874 and is used only on frames for which no explicit name has been set
24875 \(see `modify-frame-parameters'). */);
24876 Vicon_title_format
24877 = Vframe_title_format
24878 = Fcons (intern ("multiple-frames"),
24879 Fcons (build_string ("%b"),
24880 Fcons (Fcons (empty_unibyte_string,
24881 Fcons (intern ("invocation-name"),
24882 Fcons (build_string ("@"),
24883 Fcons (intern ("system-name"),
24884 Qnil)))),
24885 Qnil)));
24886
24887 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24888 doc: /* Maximum number of lines to keep in the message log buffer.
24889 If nil, disable message logging. If t, log messages but don't truncate
24890 the buffer when it becomes large. */);
24891 Vmessage_log_max = make_number (100);
24892
24893 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24894 doc: /* Functions called before redisplay, if window sizes have changed.
24895 The value should be a list of functions that take one argument.
24896 Just before redisplay, for each frame, if any of its windows have changed
24897 size since the last redisplay, or have been split or deleted,
24898 all the functions in the list are called, with the frame as argument. */);
24899 Vwindow_size_change_functions = Qnil;
24900
24901 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24902 doc: /* List of functions to call before redisplaying a window with scrolling.
24903 Each function is called with two arguments, the window
24904 and its new display-start position. Note that the value of `window-end'
24905 is not valid when these functions are called. */);
24906 Vwindow_scroll_functions = Qnil;
24907
24908 DEFVAR_LISP ("window-text-change-functions",
24909 &Vwindow_text_change_functions,
24910 doc: /* Functions to call in redisplay when text in the window might change. */);
24911 Vwindow_text_change_functions = Qnil;
24912
24913 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24914 doc: /* Functions called when redisplay of a window reaches the end trigger.
24915 Each function is called with two arguments, the window and the end trigger value.
24916 See `set-window-redisplay-end-trigger'. */);
24917 Vredisplay_end_trigger_functions = Qnil;
24918
24919 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24920 doc: /* *Non-nil means autoselect window with mouse pointer.
24921 If nil, do not autoselect windows.
24922 A positive number means delay autoselection by that many seconds: a
24923 window is autoselected only after the mouse has remained in that
24924 window for the duration of the delay.
24925 A negative number has a similar effect, but causes windows to be
24926 autoselected only after the mouse has stopped moving. \(Because of
24927 the way Emacs compares mouse events, you will occasionally wait twice
24928 that time before the window gets selected.\)
24929 Any other value means to autoselect window instantaneously when the
24930 mouse pointer enters it.
24931
24932 Autoselection selects the minibuffer only if it is active, and never
24933 unselects the minibuffer if it is active.
24934
24935 When customizing this variable make sure that the actual value of
24936 `focus-follows-mouse' matches the behavior of your window manager. */);
24937 Vmouse_autoselect_window = Qnil;
24938
24939 DEFVAR_LISP ("auto-resize-tool-bars", &Vauto_resize_tool_bars,
24940 doc: /* *Non-nil means automatically resize tool-bars.
24941 This dynamically changes the tool-bar's height to the minimum height
24942 that is needed to make all tool-bar items visible.
24943 If value is `grow-only', the tool-bar's height is only increased
24944 automatically; to decrease the tool-bar height, use \\[recenter]. */);
24945 Vauto_resize_tool_bars = Qt;
24946
24947 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24948 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24949 auto_raise_tool_bar_buttons_p = 1;
24950
24951 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24952 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24953 make_cursor_line_fully_visible_p = 1;
24954
24955 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24956 doc: /* *Border below tool-bar in pixels.
24957 If an integer, use it as the height of the border.
24958 If it is one of `internal-border-width' or `border-width', use the
24959 value of the corresponding frame parameter.
24960 Otherwise, no border is added below the tool-bar. */);
24961 Vtool_bar_border = Qinternal_border_width;
24962
24963 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24964 doc: /* *Margin around tool-bar buttons in pixels.
24965 If an integer, use that for both horizontal and vertical margins.
24966 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24967 HORZ specifying the horizontal margin, and VERT specifying the
24968 vertical margin. */);
24969 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24970
24971 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24972 doc: /* *Relief thickness of tool-bar buttons. */);
24973 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24974
24975 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24976 doc: /* List of functions to call to fontify regions of text.
24977 Each function is called with one argument POS. Functions must
24978 fontify a region starting at POS in the current buffer, and give
24979 fontified regions the property `fontified'. */);
24980 Vfontification_functions = Qnil;
24981 Fmake_variable_buffer_local (Qfontification_functions);
24982
24983 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24984 &unibyte_display_via_language_environment,
24985 doc: /* *Non-nil means display unibyte text according to language environment.
24986 Specifically this means that unibyte non-ASCII characters
24987 are displayed by converting them to the equivalent multibyte characters
24988 according to the current language environment. As a result, they are
24989 displayed according to the current fontset. */);
24990 unibyte_display_via_language_environment = 0;
24991
24992 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24993 doc: /* *Maximum height for resizing mini-windows.
24994 If a float, it specifies a fraction of the mini-window frame's height.
24995 If an integer, it specifies a number of lines. */);
24996 Vmax_mini_window_height = make_float (0.25);
24997
24998 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24999 doc: /* *How to resize mini-windows.
25000 A value of nil means don't automatically resize mini-windows.
25001 A value of t means resize them to fit the text displayed in them.
25002 A value of `grow-only', the default, means let mini-windows grow
25003 only, until their display becomes empty, at which point the windows
25004 go back to their normal size. */);
25005 Vresize_mini_windows = Qgrow_only;
25006
25007 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
25008 doc: /* Alist specifying how to blink the cursor off.
25009 Each element has the form (ON-STATE . OFF-STATE). Whenever the
25010 `cursor-type' frame-parameter or variable equals ON-STATE,
25011 comparing using `equal', Emacs uses OFF-STATE to specify
25012 how to blink it off. ON-STATE and OFF-STATE are values for
25013 the `cursor-type' frame parameter.
25014
25015 If a frame's ON-STATE has no entry in this list,
25016 the frame's other specifications determine how to blink the cursor off. */);
25017 Vblink_cursor_alist = Qnil;
25018
25019 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
25020 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
25021 automatic_hscrolling_p = 1;
25022 Qauto_hscroll_mode = intern ("auto-hscroll-mode");
25023 staticpro (&Qauto_hscroll_mode);
25024
25025 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
25026 doc: /* *How many columns away from the window edge point is allowed to get
25027 before automatic hscrolling will horizontally scroll the window. */);
25028 hscroll_margin = 5;
25029
25030 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
25031 doc: /* *How many columns to scroll the window when point gets too close to the edge.
25032 When point is less than `hscroll-margin' columns from the window
25033 edge, automatic hscrolling will scroll the window by the amount of columns
25034 determined by this variable. If its value is a positive integer, scroll that
25035 many columns. If it's a positive floating-point number, it specifies the
25036 fraction of the window's width to scroll. If it's nil or zero, point will be
25037 centered horizontally after the scroll. Any other value, including negative
25038 numbers, are treated as if the value were zero.
25039
25040 Automatic hscrolling always moves point outside the scroll margin, so if
25041 point was more than scroll step columns inside the margin, the window will
25042 scroll more than the value given by the scroll step.
25043
25044 Note that the lower bound for automatic hscrolling specified by `scroll-left'
25045 and `scroll-right' overrides this variable's effect. */);
25046 Vhscroll_step = make_number (0);
25047
25048 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
25049 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
25050 Bind this around calls to `message' to let it take effect. */);
25051 message_truncate_lines = 0;
25052
25053 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
25054 doc: /* Normal hook run to update the menu bar definitions.
25055 Redisplay runs this hook before it redisplays the menu bar.
25056 This is used to update submenus such as Buffers,
25057 whose contents depend on various data. */);
25058 Vmenu_bar_update_hook = Qnil;
25059
25060 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
25061 doc: /* Frame for which we are updating a menu.
25062 The enable predicate for a menu binding should check this variable. */);
25063 Vmenu_updating_frame = Qnil;
25064
25065 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
25066 doc: /* Non-nil means don't update menu bars. Internal use only. */);
25067 inhibit_menubar_update = 0;
25068
25069 DEFVAR_LISP ("wrap-prefix", &Vwrap_prefix,
25070 doc: /* Prefix added to the beginning of all continuation lines at display-time.
25071 May be a string, an image, or a stretch-glyph such as used by the
25072 `display' text-property.
25073
25074 This variable is overridden by any `wrap-prefix' text-property.
25075
25076 To add a prefix to non-continuation lines, use the `line-prefix' variable. */);
25077 Vwrap_prefix = Qnil;
25078 staticpro (&Qwrap_prefix);
25079 Qwrap_prefix = intern ("wrap-prefix");
25080 Fmake_variable_buffer_local (Qwrap_prefix);
25081
25082 DEFVAR_LISP ("line-prefix", &Vline_prefix,
25083 doc: /* Prefix added to the beginning of all non-continuation lines at display-time.
25084 May be a string, an image, or a stretch-glyph such as used by the
25085 `display' text-property.
25086
25087 This variable is overridden by any `line-prefix' text-property.
25088
25089 To add a prefix to continuation lines, use the `wrap-prefix' variable. */);
25090 Vline_prefix = Qnil;
25091 staticpro (&Qline_prefix);
25092 Qline_prefix = intern ("line-prefix");
25093 Fmake_variable_buffer_local (Qline_prefix);
25094
25095 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
25096 doc: /* Non-nil means don't eval Lisp during redisplay. */);
25097 inhibit_eval_during_redisplay = 0;
25098
25099 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
25100 doc: /* Non-nil means don't free realized faces. Internal use only. */);
25101 inhibit_free_realized_faces = 0;
25102
25103 #if GLYPH_DEBUG
25104 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
25105 doc: /* Inhibit try_window_id display optimization. */);
25106 inhibit_try_window_id = 0;
25107
25108 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
25109 doc: /* Inhibit try_window_reusing display optimization. */);
25110 inhibit_try_window_reusing = 0;
25111
25112 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
25113 doc: /* Inhibit try_cursor_movement display optimization. */);
25114 inhibit_try_cursor_movement = 0;
25115 #endif /* GLYPH_DEBUG */
25116
25117 DEFVAR_INT ("overline-margin", &overline_margin,
25118 doc: /* *Space between overline and text, in pixels.
25119 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
25120 margin to the caracter height. */);
25121 overline_margin = 2;
25122
25123 DEFVAR_INT ("underline-minimum-offset",
25124 &underline_minimum_offset,
25125 doc: /* Minimum distance between baseline and underline.
25126 This can improve legibility of underlined text at small font sizes,
25127 particularly when using variable `x-use-underline-position-properties'
25128 with fonts that specify an UNDERLINE_POSITION relatively close to the
25129 baseline. The default value is 1. */);
25130 underline_minimum_offset = 1;
25131 }
25132
25133
25134 /* Initialize this module when Emacs starts. */
25135
25136 void
25137 init_xdisp ()
25138 {
25139 Lisp_Object root_window;
25140 struct window *mini_w;
25141
25142 current_header_line_height = current_mode_line_height = -1;
25143
25144 CHARPOS (this_line_start_pos) = 0;
25145
25146 mini_w = XWINDOW (minibuf_window);
25147 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
25148
25149 if (!noninteractive)
25150 {
25151 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
25152 int i;
25153
25154 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
25155 set_window_height (root_window,
25156 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
25157 0);
25158 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
25159 set_window_height (minibuf_window, 1, 0);
25160
25161 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
25162 mini_w->total_cols = make_number (FRAME_COLS (f));
25163
25164 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
25165 scratch_glyph_row.glyphs[TEXT_AREA + 1]
25166 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
25167
25168 /* The default ellipsis glyphs `...'. */
25169 for (i = 0; i < 3; ++i)
25170 default_invis_vector[i] = make_number ('.');
25171 }
25172
25173 {
25174 /* Allocate the buffer for frame titles.
25175 Also used for `format-mode-line'. */
25176 int size = 100;
25177 mode_line_noprop_buf = (char *) xmalloc (size);
25178 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
25179 mode_line_noprop_ptr = mode_line_noprop_buf;
25180 mode_line_target = MODE_LINE_DISPLAY;
25181 }
25182
25183 help_echo_showing_p = 0;
25184 }
25185
25186
25187 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
25188 (do not change this comment) */