]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(echo_area_display): Clear echo_message_buffer.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23
24 Redisplay.
25
26 Emacs separates the task of updating the display from code
27 modifying global state, e.g. buffer text. This way functions
28 operating on buffers don't also have to be concerned with updating
29 the display.
30
31 Updating the display is triggered by the Lisp interpreter when it
32 decides it's time to do it. This is done either automatically for
33 you as part of the interpreter's command loop or as the result of
34 calling Lisp functions like `sit-for'. The C function `redisplay'
35 in xdisp.c is the only entry into the inner redisplay code. (Or,
36 let's say almost---see the description of direct update
37 operations, below.)
38
39 The following diagram shows how redisplay code is invoked. As you
40 can see, Lisp calls redisplay and vice versa. Under window systems
41 like X, some portions of the redisplay code are also called
42 asynchronously during mouse movement or expose events. It is very
43 important that these code parts do NOT use the C library (malloc,
44 free) because many C libraries under Unix are not reentrant. They
45 may also NOT call functions of the Lisp interpreter which could
46 change the interpreter's state. If you don't follow these rules,
47 you will encounter bugs which are very hard to explain.
48
49 (Direct functions, see below)
50 direct_output_for_insert,
51 direct_forward_char (dispnew.c)
52 +---------------------------------+
53 | |
54 | V
55 +--------------+ redisplay +----------------+
56 | Lisp machine |---------------->| Redisplay code |<--+
57 +--------------+ (xdisp.c) +----------------+ |
58 ^ | |
59 +----------------------------------+ |
60 Don't use this path when called |
61 asynchronously! |
62 |
63 expose_window (asynchronous) |
64 |
65 X expose events -----+
66
67 What does redisplay do? Obviously, it has to figure out somehow what
68 has been changed since the last time the display has been updated,
69 and to make these changes visible. Preferably it would do that in
70 a moderately intelligent way, i.e. fast.
71
72 Changes in buffer text can be deduced from window and buffer
73 structures, and from some global variables like `beg_unchanged' and
74 `end_unchanged'. The contents of the display are additionally
75 recorded in a `glyph matrix', a two-dimensional matrix of glyph
76 structures. Each row in such a matrix corresponds to a line on the
77 display, and each glyph in a row corresponds to a column displaying
78 a character, an image, or what else. This matrix is called the
79 `current glyph matrix' or `current matrix' in redisplay
80 terminology.
81
82 For buffer parts that have been changed since the last update, a
83 second glyph matrix is constructed, the so called `desired glyph
84 matrix' or short `desired matrix'. Current and desired matrix are
85 then compared to find a cheap way to update the display, e.g. by
86 reusing part of the display by scrolling lines.
87
88
89 Direct operations.
90
91 You will find a lot of redisplay optimizations when you start
92 looking at the innards of redisplay. The overall goal of all these
93 optimizations is to make redisplay fast because it is done
94 frequently.
95
96 Two optimizations are not found in xdisp.c. These are the direct
97 operations mentioned above. As the name suggests they follow a
98 different principle than the rest of redisplay. Instead of
99 building a desired matrix and then comparing it with the current
100 display, they perform their actions directly on the display and on
101 the current matrix.
102
103 One direct operation updates the display after one character has
104 been entered. The other one moves the cursor by one position
105 forward or backward. You find these functions under the names
106 `direct_output_for_insert' and `direct_output_forward_char' in
107 dispnew.c.
108
109
110 Desired matrices.
111
112 Desired matrices are always built per Emacs window. The function
113 `display_line' is the central function to look at if you are
114 interested. It constructs one row in a desired matrix given an
115 iterator structure containing both a buffer position and a
116 description of the environment in which the text is to be
117 displayed. But this is too early, read on.
118
119 Characters and pixmaps displayed for a range of buffer text depend
120 on various settings of buffers and windows, on overlays and text
121 properties, on display tables, on selective display. The good news
122 is that all this hairy stuff is hidden behind a small set of
123 interface functions taking an iterator structure (struct it)
124 argument.
125
126 Iteration over things to be displayed is then simple. It is
127 started by initializing an iterator with a call to init_iterator.
128 Calls to get_next_display_element fill the iterator structure with
129 relevant information about the next thing to display. Calls to
130 set_iterator_to_next move the iterator to the next thing.
131
132 Besides this, an iterator also contains information about the
133 display environment in which glyphs for display elements are to be
134 produced. It has fields for the width and height of the display,
135 the information whether long lines are truncated or continued, a
136 current X and Y position, and lots of other stuff you can better
137 see in dispextern.h.
138
139 Glyphs in a desired matrix are normally constructed in a loop
140 calling get_next_display_element and then produce_glyphs. The call
141 to produce_glyphs will fill the iterator structure with pixel
142 information about the element being displayed and at the same time
143 produce glyphs for it. If the display element fits on the line
144 being displayed, set_iterator_to_next is called next, otherwise the
145 glyphs produced are discarded.
146
147
148 Frame matrices.
149
150 That just couldn't be all, could it? What about terminal types not
151 supporting operations on sub-windows of the screen? To update the
152 display on such a terminal, window-based glyph matrices are not
153 well suited. To be able to reuse part of the display (scrolling
154 lines up and down), we must instead have a view of the whole
155 screen. This is what `frame matrices' are for. They are a trick.
156
157 Frames on terminals like above have a glyph pool. Windows on such
158 a frame sub-allocate their glyph memory from their frame's glyph
159 pool. The frame itself is given its own glyph matrices. By
160 coincidence---or maybe something else---rows in window glyph
161 matrices are slices of corresponding rows in frame matrices. Thus
162 writing to window matrices implicitly updates a frame matrix which
163 provides us with the view of the whole screen that we originally
164 wanted to have without having to move many bytes around. To be
165 honest, there is a little bit more done, but not much more. If you
166 plan to extend that code, take a look at dispnew.c. The function
167 build_frame_matrix is a good starting point. */
168
169 #include <config.h>
170 #include <stdio.h>
171
172 #include "lisp.h"
173 #include "keyboard.h"
174 #include "frame.h"
175 #include "window.h"
176 #include "termchar.h"
177 #include "dispextern.h"
178 #include "buffer.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 #ifndef FRAME_X_OUTPUT
204 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
205 #endif
206
207 #define INFINITY 10000000
208
209 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
210 || defined (USE_GTK)
211 extern void set_frame_menubar P_ ((struct frame *f, int, int));
212 extern int pending_menu_activation;
213 #endif
214
215 extern int interrupt_input;
216 extern int command_loop_level;
217
218 extern Lisp_Object do_mouse_tracking;
219
220 extern int minibuffer_auto_raise;
221 extern Lisp_Object Vminibuffer_list;
222
223 extern Lisp_Object Qface;
224 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
225
226 extern Lisp_Object Voverriding_local_map;
227 extern Lisp_Object Voverriding_local_map_menu_flag;
228 extern Lisp_Object Qmenu_item;
229 extern Lisp_Object Qwhen;
230 extern Lisp_Object Qhelp_echo;
231
232 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
233 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
234 Lisp_Object Qredisplay_end_trigger_functions;
235 Lisp_Object Qinhibit_point_motion_hooks;
236 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
237 Lisp_Object Qfontified;
238 Lisp_Object Qgrow_only;
239 Lisp_Object Qinhibit_eval_during_redisplay;
240 Lisp_Object Qbuffer_position, Qposition, Qobject;
241
242 /* Cursor shapes */
243 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
244
245 /* Pointer shapes */
246 Lisp_Object Qarrow, Qhand, Qtext;
247
248 Lisp_Object Qrisky_local_variable;
249
250 /* Holds the list (error). */
251 Lisp_Object list_of_error;
252
253 /* Functions called to fontify regions of text. */
254
255 Lisp_Object Vfontification_functions;
256 Lisp_Object Qfontification_functions;
257
258 /* Non-zero means automatically select any window when the mouse
259 cursor moves into it. */
260 int mouse_autoselect_window;
261
262 /* Non-zero means draw tool bar buttons raised when the mouse moves
263 over them. */
264
265 int auto_raise_tool_bar_buttons_p;
266
267 /* Non-zero means to reposition window if cursor line is only partially visible. */
268
269 int make_cursor_line_fully_visible_p;
270
271 /* Margin around tool bar buttons in pixels. */
272
273 Lisp_Object Vtool_bar_button_margin;
274
275 /* Thickness of shadow to draw around tool bar buttons. */
276
277 EMACS_INT tool_bar_button_relief;
278
279 /* Non-zero means automatically resize tool-bars so that all tool-bar
280 items are visible, and no blank lines remain. */
281
282 int auto_resize_tool_bars_p;
283
284 /* Non-zero means draw block and hollow cursor as wide as the glyph
285 under it. For example, if a block cursor is over a tab, it will be
286 drawn as wide as that tab on the display. */
287
288 int x_stretch_cursor_p;
289
290 /* Non-nil means don't actually do any redisplay. */
291
292 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
293
294 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
295
296 int inhibit_eval_during_redisplay;
297
298 /* Names of text properties relevant for redisplay. */
299
300 Lisp_Object Qdisplay;
301 extern Lisp_Object Qface, Qinvisible, Qwidth;
302
303 /* Symbols used in text property values. */
304
305 Lisp_Object Vdisplay_pixels_per_inch;
306 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
307 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
308 Lisp_Object Qslice;
309 Lisp_Object Qcenter;
310 Lisp_Object Qmargin, Qpointer;
311 Lisp_Object Qline_height;
312 extern Lisp_Object Qheight;
313 extern Lisp_Object QCwidth, QCheight, QCascent;
314 extern Lisp_Object Qscroll_bar;
315 extern Lisp_Object Qcursor;
316
317 /* Non-nil means highlight trailing whitespace. */
318
319 Lisp_Object Vshow_trailing_whitespace;
320
321 /* Non-nil means escape non-break space and hyphens. */
322
323 Lisp_Object Vshow_nonbreak_escape;
324
325 #ifdef HAVE_WINDOW_SYSTEM
326 extern Lisp_Object Voverflow_newline_into_fringe;
327
328 /* Test if overflow newline into fringe. Called with iterator IT
329 at or past right window margin, and with IT->current_x set. */
330
331 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
332 (!NILP (Voverflow_newline_into_fringe) \
333 && FRAME_WINDOW_P (it->f) \
334 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
335 && it->current_x == it->last_visible_x)
336
337 #endif /* HAVE_WINDOW_SYSTEM */
338
339 /* Non-nil means show the text cursor in void text areas
340 i.e. in blank areas after eol and eob. This used to be
341 the default in 21.3. */
342
343 Lisp_Object Vvoid_text_area_pointer;
344
345 /* Name of the face used to highlight trailing whitespace. */
346
347 Lisp_Object Qtrailing_whitespace;
348
349 /* Name and number of the face used to highlight escape glyphs. */
350
351 Lisp_Object Qescape_glyph;
352
353 /* The symbol `image' which is the car of the lists used to represent
354 images in Lisp. */
355
356 Lisp_Object Qimage;
357
358 /* The image map types. */
359 Lisp_Object QCmap, QCpointer;
360 Lisp_Object Qrect, Qcircle, Qpoly;
361
362 /* Non-zero means print newline to stdout before next mini-buffer
363 message. */
364
365 int noninteractive_need_newline;
366
367 /* Non-zero means print newline to message log before next message. */
368
369 static int message_log_need_newline;
370
371 /* Three markers that message_dolog uses.
372 It could allocate them itself, but that causes trouble
373 in handling memory-full errors. */
374 static Lisp_Object message_dolog_marker1;
375 static Lisp_Object message_dolog_marker2;
376 static Lisp_Object message_dolog_marker3;
377 \f
378 /* The buffer position of the first character appearing entirely or
379 partially on the line of the selected window which contains the
380 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
381 redisplay optimization in redisplay_internal. */
382
383 static struct text_pos this_line_start_pos;
384
385 /* Number of characters past the end of the line above, including the
386 terminating newline. */
387
388 static struct text_pos this_line_end_pos;
389
390 /* The vertical positions and the height of this line. */
391
392 static int this_line_vpos;
393 static int this_line_y;
394 static int this_line_pixel_height;
395
396 /* X position at which this display line starts. Usually zero;
397 negative if first character is partially visible. */
398
399 static int this_line_start_x;
400
401 /* Buffer that this_line_.* variables are referring to. */
402
403 static struct buffer *this_line_buffer;
404
405 /* Nonzero means truncate lines in all windows less wide than the
406 frame. */
407
408 int truncate_partial_width_windows;
409
410 /* A flag to control how to display unibyte 8-bit character. */
411
412 int unibyte_display_via_language_environment;
413
414 /* Nonzero means we have more than one non-mini-buffer-only frame.
415 Not guaranteed to be accurate except while parsing
416 frame-title-format. */
417
418 int multiple_frames;
419
420 Lisp_Object Vglobal_mode_string;
421
422
423 /* List of variables (symbols) which hold markers for overlay arrows.
424 The symbols on this list are examined during redisplay to determine
425 where to display overlay arrows. */
426
427 Lisp_Object Voverlay_arrow_variable_list;
428
429 /* Marker for where to display an arrow on top of the buffer text. */
430
431 Lisp_Object Voverlay_arrow_position;
432
433 /* String to display for the arrow. Only used on terminal frames. */
434
435 Lisp_Object Voverlay_arrow_string;
436
437 /* Values of those variables at last redisplay are stored as
438 properties on `overlay-arrow-position' symbol. However, if
439 Voverlay_arrow_position is a marker, last-arrow-position is its
440 numerical position. */
441
442 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
443
444 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
445 properties on a symbol in overlay-arrow-variable-list. */
446
447 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
448
449 /* Like mode-line-format, but for the title bar on a visible frame. */
450
451 Lisp_Object Vframe_title_format;
452
453 /* Like mode-line-format, but for the title bar on an iconified frame. */
454
455 Lisp_Object Vicon_title_format;
456
457 /* List of functions to call when a window's size changes. These
458 functions get one arg, a frame on which one or more windows' sizes
459 have changed. */
460
461 static Lisp_Object Vwindow_size_change_functions;
462
463 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
464
465 /* Nonzero if overlay arrow has been displayed once in this window. */
466
467 static int overlay_arrow_seen;
468
469 /* Nonzero means highlight the region even in nonselected windows. */
470
471 int highlight_nonselected_windows;
472
473 /* If cursor motion alone moves point off frame, try scrolling this
474 many lines up or down if that will bring it back. */
475
476 static EMACS_INT scroll_step;
477
478 /* Nonzero means scroll just far enough to bring point back on the
479 screen, when appropriate. */
480
481 static EMACS_INT scroll_conservatively;
482
483 /* Recenter the window whenever point gets within this many lines of
484 the top or bottom of the window. This value is translated into a
485 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
486 that there is really a fixed pixel height scroll margin. */
487
488 EMACS_INT scroll_margin;
489
490 /* Number of windows showing the buffer of the selected window (or
491 another buffer with the same base buffer). keyboard.c refers to
492 this. */
493
494 int buffer_shared;
495
496 /* Vector containing glyphs for an ellipsis `...'. */
497
498 static Lisp_Object default_invis_vector[3];
499
500 /* Zero means display the mode-line/header-line/menu-bar in the default face
501 (this slightly odd definition is for compatibility with previous versions
502 of emacs), non-zero means display them using their respective faces.
503
504 This variable is deprecated. */
505
506 int mode_line_inverse_video;
507
508 /* Prompt to display in front of the mini-buffer contents. */
509
510 Lisp_Object minibuf_prompt;
511
512 /* Width of current mini-buffer prompt. Only set after display_line
513 of the line that contains the prompt. */
514
515 int minibuf_prompt_width;
516
517 /* This is the window where the echo area message was displayed. It
518 is always a mini-buffer window, but it may not be the same window
519 currently active as a mini-buffer. */
520
521 Lisp_Object echo_area_window;
522
523 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
524 pushes the current message and the value of
525 message_enable_multibyte on the stack, the function restore_message
526 pops the stack and displays MESSAGE again. */
527
528 Lisp_Object Vmessage_stack;
529
530 /* Nonzero means multibyte characters were enabled when the echo area
531 message was specified. */
532
533 int message_enable_multibyte;
534
535 /* Nonzero if we should redraw the mode lines on the next redisplay. */
536
537 int update_mode_lines;
538
539 /* Nonzero if window sizes or contents have changed since last
540 redisplay that finished. */
541
542 int windows_or_buffers_changed;
543
544 /* Nonzero means a frame's cursor type has been changed. */
545
546 int cursor_type_changed;
547
548 /* Nonzero after display_mode_line if %l was used and it displayed a
549 line number. */
550
551 int line_number_displayed;
552
553 /* Maximum buffer size for which to display line numbers. */
554
555 Lisp_Object Vline_number_display_limit;
556
557 /* Line width to consider when repositioning for line number display. */
558
559 static EMACS_INT line_number_display_limit_width;
560
561 /* Number of lines to keep in the message log buffer. t means
562 infinite. nil means don't log at all. */
563
564 Lisp_Object Vmessage_log_max;
565
566 /* The name of the *Messages* buffer, a string. */
567
568 static Lisp_Object Vmessages_buffer_name;
569
570 /* Current, index 0, and last displayed echo area message. Either
571 buffers from echo_buffers, or nil to indicate no message. */
572
573 Lisp_Object echo_area_buffer[2];
574
575 /* The buffers referenced from echo_area_buffer. */
576
577 static Lisp_Object echo_buffer[2];
578
579 /* A vector saved used in with_area_buffer to reduce consing. */
580
581 static Lisp_Object Vwith_echo_area_save_vector;
582
583 /* Non-zero means display_echo_area should display the last echo area
584 message again. Set by redisplay_preserve_echo_area. */
585
586 static int display_last_displayed_message_p;
587
588 /* Nonzero if echo area is being used by print; zero if being used by
589 message. */
590
591 int message_buf_print;
592
593 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
594
595 Lisp_Object Qinhibit_menubar_update;
596 int inhibit_menubar_update;
597
598 /* Maximum height for resizing mini-windows. Either a float
599 specifying a fraction of the available height, or an integer
600 specifying a number of lines. */
601
602 Lisp_Object Vmax_mini_window_height;
603
604 /* Non-zero means messages should be displayed with truncated
605 lines instead of being continued. */
606
607 int message_truncate_lines;
608 Lisp_Object Qmessage_truncate_lines;
609
610 /* Set to 1 in clear_message to make redisplay_internal aware
611 of an emptied echo area. */
612
613 static int message_cleared_p;
614
615 /* Non-zero means we want a hollow cursor in windows that are not
616 selected. Zero means there's no cursor in such windows. */
617
618 Lisp_Object Vcursor_in_non_selected_windows;
619 Lisp_Object Qcursor_in_non_selected_windows;
620
621 /* How to blink the default frame cursor off. */
622 Lisp_Object Vblink_cursor_alist;
623
624 /* A scratch glyph row with contents used for generating truncation
625 glyphs. Also used in direct_output_for_insert. */
626
627 #define MAX_SCRATCH_GLYPHS 100
628 struct glyph_row scratch_glyph_row;
629 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
630
631 /* Ascent and height of the last line processed by move_it_to. */
632
633 static int last_max_ascent, last_height;
634
635 /* Non-zero if there's a help-echo in the echo area. */
636
637 int help_echo_showing_p;
638
639 /* If >= 0, computed, exact values of mode-line and header-line height
640 to use in the macros CURRENT_MODE_LINE_HEIGHT and
641 CURRENT_HEADER_LINE_HEIGHT. */
642
643 int current_mode_line_height, current_header_line_height;
644
645 /* The maximum distance to look ahead for text properties. Values
646 that are too small let us call compute_char_face and similar
647 functions too often which is expensive. Values that are too large
648 let us call compute_char_face and alike too often because we
649 might not be interested in text properties that far away. */
650
651 #define TEXT_PROP_DISTANCE_LIMIT 100
652
653 #if GLYPH_DEBUG
654
655 /* Variables to turn off display optimizations from Lisp. */
656
657 int inhibit_try_window_id, inhibit_try_window_reusing;
658 int inhibit_try_cursor_movement;
659
660 /* Non-zero means print traces of redisplay if compiled with
661 GLYPH_DEBUG != 0. */
662
663 int trace_redisplay_p;
664
665 #endif /* GLYPH_DEBUG */
666
667 #ifdef DEBUG_TRACE_MOVE
668 /* Non-zero means trace with TRACE_MOVE to stderr. */
669 int trace_move;
670
671 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
672 #else
673 #define TRACE_MOVE(x) (void) 0
674 #endif
675
676 /* Non-zero means automatically scroll windows horizontally to make
677 point visible. */
678
679 int automatic_hscrolling_p;
680
681 /* How close to the margin can point get before the window is scrolled
682 horizontally. */
683 EMACS_INT hscroll_margin;
684
685 /* How much to scroll horizontally when point is inside the above margin. */
686 Lisp_Object Vhscroll_step;
687
688 /* The variable `resize-mini-windows'. If nil, don't resize
689 mini-windows. If t, always resize them to fit the text they
690 display. If `grow-only', let mini-windows grow only until they
691 become empty. */
692
693 Lisp_Object Vresize_mini_windows;
694
695 /* Buffer being redisplayed -- for redisplay_window_error. */
696
697 struct buffer *displayed_buffer;
698
699 /* Value returned from text property handlers (see below). */
700
701 enum prop_handled
702 {
703 HANDLED_NORMALLY,
704 HANDLED_RECOMPUTE_PROPS,
705 HANDLED_OVERLAY_STRING_CONSUMED,
706 HANDLED_RETURN
707 };
708
709 /* A description of text properties that redisplay is interested
710 in. */
711
712 struct props
713 {
714 /* The name of the property. */
715 Lisp_Object *name;
716
717 /* A unique index for the property. */
718 enum prop_idx idx;
719
720 /* A handler function called to set up iterator IT from the property
721 at IT's current position. Value is used to steer handle_stop. */
722 enum prop_handled (*handler) P_ ((struct it *it));
723 };
724
725 static enum prop_handled handle_face_prop P_ ((struct it *));
726 static enum prop_handled handle_invisible_prop P_ ((struct it *));
727 static enum prop_handled handle_display_prop P_ ((struct it *));
728 static enum prop_handled handle_composition_prop P_ ((struct it *));
729 static enum prop_handled handle_overlay_change P_ ((struct it *));
730 static enum prop_handled handle_fontified_prop P_ ((struct it *));
731
732 /* Properties handled by iterators. */
733
734 static struct props it_props[] =
735 {
736 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
737 /* Handle `face' before `display' because some sub-properties of
738 `display' need to know the face. */
739 {&Qface, FACE_PROP_IDX, handle_face_prop},
740 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
741 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
742 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
743 {NULL, 0, NULL}
744 };
745
746 /* Value is the position described by X. If X is a marker, value is
747 the marker_position of X. Otherwise, value is X. */
748
749 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
750
751 /* Enumeration returned by some move_it_.* functions internally. */
752
753 enum move_it_result
754 {
755 /* Not used. Undefined value. */
756 MOVE_UNDEFINED,
757
758 /* Move ended at the requested buffer position or ZV. */
759 MOVE_POS_MATCH_OR_ZV,
760
761 /* Move ended at the requested X pixel position. */
762 MOVE_X_REACHED,
763
764 /* Move within a line ended at the end of a line that must be
765 continued. */
766 MOVE_LINE_CONTINUED,
767
768 /* Move within a line ended at the end of a line that would
769 be displayed truncated. */
770 MOVE_LINE_TRUNCATED,
771
772 /* Move within a line ended at a line end. */
773 MOVE_NEWLINE_OR_CR
774 };
775
776 /* This counter is used to clear the face cache every once in a while
777 in redisplay_internal. It is incremented for each redisplay.
778 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
779 cleared. */
780
781 #define CLEAR_FACE_CACHE_COUNT 500
782 static int clear_face_cache_count;
783
784 /* Record the previous terminal frame we displayed. */
785
786 static struct frame *previous_terminal_frame;
787
788 /* Non-zero while redisplay_internal is in progress. */
789
790 int redisplaying_p;
791
792 /* Non-zero means don't free realized faces. Bound while freeing
793 realized faces is dangerous because glyph matrices might still
794 reference them. */
795
796 int inhibit_free_realized_faces;
797 Lisp_Object Qinhibit_free_realized_faces;
798
799 /* If a string, XTread_socket generates an event to display that string.
800 (The display is done in read_char.) */
801
802 Lisp_Object help_echo_string;
803 Lisp_Object help_echo_window;
804 Lisp_Object help_echo_object;
805 int help_echo_pos;
806
807 /* Temporary variable for XTread_socket. */
808
809 Lisp_Object previous_help_echo_string;
810
811 /* Null glyph slice */
812
813 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
814
815 \f
816 /* Function prototypes. */
817
818 static void setup_for_ellipsis P_ ((struct it *, int));
819 static void mark_window_display_accurate_1 P_ ((struct window *, int));
820 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
821 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
822 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
823 static int redisplay_mode_lines P_ ((Lisp_Object, int));
824 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
825
826 #if 0
827 static int invisible_text_between_p P_ ((struct it *, int, int));
828 #endif
829
830 static int next_element_from_ellipsis P_ ((struct it *));
831 static void pint2str P_ ((char *, int, int));
832 static void pint2hrstr P_ ((char *, int, int));
833 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
834 struct text_pos));
835 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
836 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
837 static void store_frame_title_char P_ ((char));
838 static int store_frame_title P_ ((const unsigned char *, int, int));
839 static void x_consider_frame_title P_ ((Lisp_Object));
840 static void handle_stop P_ ((struct it *));
841 static int tool_bar_lines_needed P_ ((struct frame *));
842 static int single_display_spec_intangible_p P_ ((Lisp_Object));
843 static void ensure_echo_area_buffers P_ ((void));
844 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
845 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
846 static int with_echo_area_buffer P_ ((struct window *, int,
847 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
848 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
849 static void clear_garbaged_frames P_ ((void));
850 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
851 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
853 static int display_echo_area P_ ((struct window *));
854 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
855 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
856 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
857 static int string_char_and_length P_ ((const unsigned char *, int, int *));
858 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
859 struct text_pos));
860 static int compute_window_start_on_continuation_line P_ ((struct window *));
861 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
862 static void insert_left_trunc_glyphs P_ ((struct it *));
863 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
864 Lisp_Object));
865 static void extend_face_to_end_of_line P_ ((struct it *));
866 static int append_space_for_newline P_ ((struct it *, int));
867 static int make_cursor_line_fully_visible P_ ((struct window *, int));
868 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
869 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
870 static int trailing_whitespace_p P_ ((int));
871 static int message_log_check_duplicate P_ ((int, int, int, int));
872 static void push_it P_ ((struct it *));
873 static void pop_it P_ ((struct it *));
874 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
875 static void select_frame_for_redisplay P_ ((Lisp_Object));
876 static void redisplay_internal P_ ((int));
877 static int echo_area_display P_ ((int));
878 static void redisplay_windows P_ ((Lisp_Object));
879 static void redisplay_window P_ ((Lisp_Object, int));
880 static Lisp_Object redisplay_window_error ();
881 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
882 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
883 static void update_menu_bar P_ ((struct frame *, int));
884 static int try_window_reusing_current_matrix P_ ((struct window *));
885 static int try_window_id P_ ((struct window *));
886 static int display_line P_ ((struct it *));
887 static int display_mode_lines P_ ((struct window *));
888 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
889 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
890 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
891 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
892 static void display_menu_bar P_ ((struct window *));
893 static int display_count_lines P_ ((int, int, int, int, int *));
894 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
895 int, int, struct it *, int, int, int, int));
896 static void compute_line_metrics P_ ((struct it *));
897 static void run_redisplay_end_trigger_hook P_ ((struct it *));
898 static int get_overlay_strings P_ ((struct it *, int));
899 static void next_overlay_string P_ ((struct it *));
900 static void reseat P_ ((struct it *, struct text_pos, int));
901 static void reseat_1 P_ ((struct it *, struct text_pos, int));
902 static void back_to_previous_visible_line_start P_ ((struct it *));
903 void reseat_at_previous_visible_line_start P_ ((struct it *));
904 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
905 static int next_element_from_display_vector P_ ((struct it *));
906 static int next_element_from_string P_ ((struct it *));
907 static int next_element_from_c_string P_ ((struct it *));
908 static int next_element_from_buffer P_ ((struct it *));
909 static int next_element_from_composition P_ ((struct it *));
910 static int next_element_from_image P_ ((struct it *));
911 static int next_element_from_stretch P_ ((struct it *));
912 static void load_overlay_strings P_ ((struct it *, int));
913 static int init_from_display_pos P_ ((struct it *, struct window *,
914 struct display_pos *));
915 static void reseat_to_string P_ ((struct it *, unsigned char *,
916 Lisp_Object, int, int, int, int));
917 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
918 int, int, int));
919 void move_it_vertically_backward P_ ((struct it *, int));
920 static void init_to_row_start P_ ((struct it *, struct window *,
921 struct glyph_row *));
922 static int init_to_row_end P_ ((struct it *, struct window *,
923 struct glyph_row *));
924 static void back_to_previous_line_start P_ ((struct it *));
925 static int forward_to_next_line_start P_ ((struct it *, int *));
926 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
927 Lisp_Object, int));
928 static struct text_pos string_pos P_ ((int, Lisp_Object));
929 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
930 static int number_of_chars P_ ((unsigned char *, int));
931 static void compute_stop_pos P_ ((struct it *));
932 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
933 Lisp_Object));
934 static int face_before_or_after_it_pos P_ ((struct it *, int));
935 static int next_overlay_change P_ ((int));
936 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
937 Lisp_Object, struct text_pos *,
938 int));
939 static int underlying_face_id P_ ((struct it *));
940 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
941 struct window *));
942
943 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
944 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
945
946 #ifdef HAVE_WINDOW_SYSTEM
947
948 static void update_tool_bar P_ ((struct frame *, int));
949 static void build_desired_tool_bar_string P_ ((struct frame *f));
950 static int redisplay_tool_bar P_ ((struct frame *));
951 static void display_tool_bar_line P_ ((struct it *));
952 static void notice_overwritten_cursor P_ ((struct window *,
953 enum glyph_row_area,
954 int, int, int, int));
955
956
957
958 #endif /* HAVE_WINDOW_SYSTEM */
959
960 \f
961 /***********************************************************************
962 Window display dimensions
963 ***********************************************************************/
964
965 /* Return the bottom boundary y-position for text lines in window W.
966 This is the first y position at which a line cannot start.
967 It is relative to the top of the window.
968
969 This is the height of W minus the height of a mode line, if any. */
970
971 INLINE int
972 window_text_bottom_y (w)
973 struct window *w;
974 {
975 int height = WINDOW_TOTAL_HEIGHT (w);
976
977 if (WINDOW_WANTS_MODELINE_P (w))
978 height -= CURRENT_MODE_LINE_HEIGHT (w);
979 return height;
980 }
981
982 /* Return the pixel width of display area AREA of window W. AREA < 0
983 means return the total width of W, not including fringes to
984 the left and right of the window. */
985
986 INLINE int
987 window_box_width (w, area)
988 struct window *w;
989 int area;
990 {
991 int cols = XFASTINT (w->total_cols);
992 int pixels = 0;
993
994 if (!w->pseudo_window_p)
995 {
996 cols -= WINDOW_SCROLL_BAR_COLS (w);
997
998 if (area == TEXT_AREA)
999 {
1000 if (INTEGERP (w->left_margin_cols))
1001 cols -= XFASTINT (w->left_margin_cols);
1002 if (INTEGERP (w->right_margin_cols))
1003 cols -= XFASTINT (w->right_margin_cols);
1004 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1005 }
1006 else if (area == LEFT_MARGIN_AREA)
1007 {
1008 cols = (INTEGERP (w->left_margin_cols)
1009 ? XFASTINT (w->left_margin_cols) : 0);
1010 pixels = 0;
1011 }
1012 else if (area == RIGHT_MARGIN_AREA)
1013 {
1014 cols = (INTEGERP (w->right_margin_cols)
1015 ? XFASTINT (w->right_margin_cols) : 0);
1016 pixels = 0;
1017 }
1018 }
1019
1020 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1021 }
1022
1023
1024 /* Return the pixel height of the display area of window W, not
1025 including mode lines of W, if any. */
1026
1027 INLINE int
1028 window_box_height (w)
1029 struct window *w;
1030 {
1031 struct frame *f = XFRAME (w->frame);
1032 int height = WINDOW_TOTAL_HEIGHT (w);
1033
1034 xassert (height >= 0);
1035
1036 /* Note: the code below that determines the mode-line/header-line
1037 height is essentially the same as that contained in the macro
1038 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1039 the appropriate glyph row has its `mode_line_p' flag set,
1040 and if it doesn't, uses estimate_mode_line_height instead. */
1041
1042 if (WINDOW_WANTS_MODELINE_P (w))
1043 {
1044 struct glyph_row *ml_row
1045 = (w->current_matrix && w->current_matrix->rows
1046 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1047 : 0);
1048 if (ml_row && ml_row->mode_line_p)
1049 height -= ml_row->height;
1050 else
1051 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1052 }
1053
1054 if (WINDOW_WANTS_HEADER_LINE_P (w))
1055 {
1056 struct glyph_row *hl_row
1057 = (w->current_matrix && w->current_matrix->rows
1058 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1059 : 0);
1060 if (hl_row && hl_row->mode_line_p)
1061 height -= hl_row->height;
1062 else
1063 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1064 }
1065
1066 /* With a very small font and a mode-line that's taller than
1067 default, we might end up with a negative height. */
1068 return max (0, height);
1069 }
1070
1071 /* Return the window-relative coordinate of the left edge of display
1072 area AREA of window W. AREA < 0 means return the left edge of the
1073 whole window, to the right of the left fringe of W. */
1074
1075 INLINE int
1076 window_box_left_offset (w, area)
1077 struct window *w;
1078 int area;
1079 {
1080 int x;
1081
1082 if (w->pseudo_window_p)
1083 return 0;
1084
1085 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1086
1087 if (area == TEXT_AREA)
1088 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1089 + window_box_width (w, LEFT_MARGIN_AREA));
1090 else if (area == RIGHT_MARGIN_AREA)
1091 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1092 + window_box_width (w, LEFT_MARGIN_AREA)
1093 + window_box_width (w, TEXT_AREA)
1094 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1095 ? 0
1096 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1097 else if (area == LEFT_MARGIN_AREA
1098 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1099 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1100
1101 return x;
1102 }
1103
1104
1105 /* Return the window-relative coordinate of the right edge of display
1106 area AREA of window W. AREA < 0 means return the left edge of the
1107 whole window, to the left of the right fringe of W. */
1108
1109 INLINE int
1110 window_box_right_offset (w, area)
1111 struct window *w;
1112 int area;
1113 {
1114 return window_box_left_offset (w, area) + window_box_width (w, area);
1115 }
1116
1117 /* Return the frame-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 (w, area)
1123 struct window *w;
1124 int area;
1125 {
1126 struct frame *f = XFRAME (w->frame);
1127 int x;
1128
1129 if (w->pseudo_window_p)
1130 return FRAME_INTERNAL_BORDER_WIDTH (f);
1131
1132 x = (WINDOW_LEFT_EDGE_X (w)
1133 + window_box_left_offset (w, area));
1134
1135 return x;
1136 }
1137
1138
1139 /* Return the frame-relative coordinate of the right edge of display
1140 area AREA of window W. AREA < 0 means return the left edge of the
1141 whole window, to the left of the right fringe of W. */
1142
1143 INLINE int
1144 window_box_right (w, area)
1145 struct window *w;
1146 int area;
1147 {
1148 return window_box_left (w, area) + window_box_width (w, area);
1149 }
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines, in frame-relative coordinates. AREA < 0 means the
1153 whole window, not including the left and right fringes of
1154 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1155 coordinates of the upper-left corner of the box. Return in
1156 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1157
1158 INLINE void
1159 window_box (w, area, box_x, box_y, box_width, box_height)
1160 struct window *w;
1161 int area;
1162 int *box_x, *box_y, *box_width, *box_height;
1163 {
1164 if (box_width)
1165 *box_width = window_box_width (w, area);
1166 if (box_height)
1167 *box_height = window_box_height (w);
1168 if (box_x)
1169 *box_x = window_box_left (w, area);
1170 if (box_y)
1171 {
1172 *box_y = WINDOW_TOP_EDGE_Y (w);
1173 if (WINDOW_WANTS_HEADER_LINE_P (w))
1174 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1175 }
1176 }
1177
1178
1179 /* Get the bounding box of the display area AREA of window W, without
1180 mode lines. AREA < 0 means the whole window, not including the
1181 left and right fringe of the window. Return in *TOP_LEFT_X
1182 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1183 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1184 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1185 box. */
1186
1187 INLINE void
1188 window_box_edges (w, area, top_left_x, top_left_y,
1189 bottom_right_x, bottom_right_y)
1190 struct window *w;
1191 int area;
1192 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1193 {
1194 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1195 bottom_right_y);
1196 *bottom_right_x += *top_left_x;
1197 *bottom_right_y += *top_left_y;
1198 }
1199
1200
1201 \f
1202 /***********************************************************************
1203 Utilities
1204 ***********************************************************************/
1205
1206 /* Return the bottom y-position of the line the iterator IT is in.
1207 This can modify IT's settings. */
1208
1209 int
1210 line_bottom_y (it)
1211 struct it *it;
1212 {
1213 int line_height = it->max_ascent + it->max_descent;
1214 int line_top_y = it->current_y;
1215
1216 if (line_height == 0)
1217 {
1218 if (last_height)
1219 line_height = last_height;
1220 else if (IT_CHARPOS (*it) < ZV)
1221 {
1222 move_it_by_lines (it, 1, 1);
1223 line_height = (it->max_ascent || it->max_descent
1224 ? it->max_ascent + it->max_descent
1225 : last_height);
1226 }
1227 else
1228 {
1229 struct glyph_row *row = it->glyph_row;
1230
1231 /* Use the default character height. */
1232 it->glyph_row = NULL;
1233 it->what = IT_CHARACTER;
1234 it->c = ' ';
1235 it->len = 1;
1236 PRODUCE_GLYPHS (it);
1237 line_height = it->ascent + it->descent;
1238 it->glyph_row = row;
1239 }
1240 }
1241
1242 return line_top_y + line_height;
1243 }
1244
1245
1246 /* Return 1 if position CHARPOS is visible in window W.
1247 If visible, set *X and *Y to pixel coordinates of top left corner.
1248 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1249 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1250 and header-lines heights. */
1251
1252 int
1253 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1254 struct window *w;
1255 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1256 {
1257 struct it it;
1258 struct text_pos top;
1259 int visible_p = 0;
1260 struct buffer *old_buffer = NULL;
1261
1262 if (noninteractive)
1263 return visible_p;
1264
1265 if (XBUFFER (w->buffer) != current_buffer)
1266 {
1267 old_buffer = current_buffer;
1268 set_buffer_internal_1 (XBUFFER (w->buffer));
1269 }
1270
1271 SET_TEXT_POS_FROM_MARKER (top, w->start);
1272
1273 /* Compute exact mode line heights, if requested. */
1274 if (exact_mode_line_heights_p)
1275 {
1276 if (WINDOW_WANTS_MODELINE_P (w))
1277 current_mode_line_height
1278 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1279 current_buffer->mode_line_format);
1280
1281 if (WINDOW_WANTS_HEADER_LINE_P (w))
1282 current_header_line_height
1283 = display_mode_line (w, HEADER_LINE_FACE_ID,
1284 current_buffer->header_line_format);
1285 }
1286
1287 start_display (&it, w, top);
1288 move_it_to (&it, charpos, 0, it.last_visible_y, -1,
1289 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
1290
1291 /* Note that we may overshoot because of invisible text. */
1292 if (IT_CHARPOS (it) >= charpos)
1293 {
1294 int top_y = it.current_y;
1295 int bottom_y = (last_height = 0, line_bottom_y (&it));
1296 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1297
1298 if (top_y < window_top_y)
1299 visible_p = bottom_y > window_top_y;
1300 else if (top_y < it.last_visible_y)
1301 visible_p = 1;
1302 if (visible_p && x)
1303 {
1304 *x = it.current_x;
1305 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1306 if (rtop)
1307 {
1308 *rtop = max (0, window_top_y - top_y);
1309 *rbot = max (0, bottom_y - it.last_visible_y);
1310 }
1311 }
1312 }
1313 else if (it.current_y + it.max_ascent + it.max_descent > it.last_visible_y)
1314 {
1315 struct it it2;
1316
1317 it2 = it;
1318 move_it_by_lines (&it, 1, 0);
1319 if (charpos < IT_CHARPOS (it))
1320 {
1321 visible_p = 1;
1322 if (x)
1323 {
1324 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1325 *x = it2.current_x;
1326 *y = it2.current_y + it2.max_ascent - it2.ascent;
1327 if (rtop)
1328 {
1329 *rtop = 0;
1330 *rbot = max (0, (it2.current_y + it2.max_ascent + it2.max_descent) - it.last_visible_y);
1331 }
1332 }
1333 }
1334 }
1335
1336 if (old_buffer)
1337 set_buffer_internal_1 (old_buffer);
1338
1339 current_header_line_height = current_mode_line_height = -1;
1340
1341 return visible_p;
1342 }
1343
1344
1345 /* Return the next character from STR which is MAXLEN bytes long.
1346 Return in *LEN the length of the character. This is like
1347 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1348 we find one, we return a `?', but with the length of the invalid
1349 character. */
1350
1351 static INLINE int
1352 string_char_and_length (str, maxlen, len)
1353 const unsigned char *str;
1354 int maxlen, *len;
1355 {
1356 int c;
1357
1358 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1359 if (!CHAR_VALID_P (c, 1))
1360 /* We may not change the length here because other places in Emacs
1361 don't use this function, i.e. they silently accept invalid
1362 characters. */
1363 c = '?';
1364
1365 return c;
1366 }
1367
1368
1369
1370 /* Given a position POS containing a valid character and byte position
1371 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1372
1373 static struct text_pos
1374 string_pos_nchars_ahead (pos, string, nchars)
1375 struct text_pos pos;
1376 Lisp_Object string;
1377 int nchars;
1378 {
1379 xassert (STRINGP (string) && nchars >= 0);
1380
1381 if (STRING_MULTIBYTE (string))
1382 {
1383 int rest = SBYTES (string) - BYTEPOS (pos);
1384 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1385 int len;
1386
1387 while (nchars--)
1388 {
1389 string_char_and_length (p, rest, &len);
1390 p += len, rest -= len;
1391 xassert (rest >= 0);
1392 CHARPOS (pos) += 1;
1393 BYTEPOS (pos) += len;
1394 }
1395 }
1396 else
1397 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1398
1399 return pos;
1400 }
1401
1402
1403 /* Value is the text position, i.e. character and byte position,
1404 for character position CHARPOS in STRING. */
1405
1406 static INLINE struct text_pos
1407 string_pos (charpos, string)
1408 int charpos;
1409 Lisp_Object string;
1410 {
1411 struct text_pos pos;
1412 xassert (STRINGP (string));
1413 xassert (charpos >= 0);
1414 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1415 return pos;
1416 }
1417
1418
1419 /* Value is a text position, i.e. character and byte position, for
1420 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1421 means recognize multibyte characters. */
1422
1423 static struct text_pos
1424 c_string_pos (charpos, s, multibyte_p)
1425 int charpos;
1426 unsigned char *s;
1427 int multibyte_p;
1428 {
1429 struct text_pos pos;
1430
1431 xassert (s != NULL);
1432 xassert (charpos >= 0);
1433
1434 if (multibyte_p)
1435 {
1436 int rest = strlen (s), len;
1437
1438 SET_TEXT_POS (pos, 0, 0);
1439 while (charpos--)
1440 {
1441 string_char_and_length (s, rest, &len);
1442 s += len, rest -= len;
1443 xassert (rest >= 0);
1444 CHARPOS (pos) += 1;
1445 BYTEPOS (pos) += len;
1446 }
1447 }
1448 else
1449 SET_TEXT_POS (pos, charpos, charpos);
1450
1451 return pos;
1452 }
1453
1454
1455 /* Value is the number of characters in C string S. MULTIBYTE_P
1456 non-zero means recognize multibyte characters. */
1457
1458 static int
1459 number_of_chars (s, multibyte_p)
1460 unsigned char *s;
1461 int multibyte_p;
1462 {
1463 int nchars;
1464
1465 if (multibyte_p)
1466 {
1467 int rest = strlen (s), len;
1468 unsigned char *p = (unsigned char *) s;
1469
1470 for (nchars = 0; rest > 0; ++nchars)
1471 {
1472 string_char_and_length (p, rest, &len);
1473 rest -= len, p += len;
1474 }
1475 }
1476 else
1477 nchars = strlen (s);
1478
1479 return nchars;
1480 }
1481
1482
1483 /* Compute byte position NEWPOS->bytepos corresponding to
1484 NEWPOS->charpos. POS is a known position in string STRING.
1485 NEWPOS->charpos must be >= POS.charpos. */
1486
1487 static void
1488 compute_string_pos (newpos, pos, string)
1489 struct text_pos *newpos, pos;
1490 Lisp_Object string;
1491 {
1492 xassert (STRINGP (string));
1493 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1494
1495 if (STRING_MULTIBYTE (string))
1496 *newpos = string_pos_nchars_ahead (pos, string,
1497 CHARPOS (*newpos) - CHARPOS (pos));
1498 else
1499 BYTEPOS (*newpos) = CHARPOS (*newpos);
1500 }
1501
1502 /* EXPORT:
1503 Return an estimation of the pixel height of mode or top lines on
1504 frame F. FACE_ID specifies what line's height to estimate. */
1505
1506 int
1507 estimate_mode_line_height (f, face_id)
1508 struct frame *f;
1509 enum face_id face_id;
1510 {
1511 #ifdef HAVE_WINDOW_SYSTEM
1512 if (FRAME_WINDOW_P (f))
1513 {
1514 int height = FONT_HEIGHT (FRAME_FONT (f));
1515
1516 /* This function is called so early when Emacs starts that the face
1517 cache and mode line face are not yet initialized. */
1518 if (FRAME_FACE_CACHE (f))
1519 {
1520 struct face *face = FACE_FROM_ID (f, face_id);
1521 if (face)
1522 {
1523 if (face->font)
1524 height = FONT_HEIGHT (face->font);
1525 if (face->box_line_width > 0)
1526 height += 2 * face->box_line_width;
1527 }
1528 }
1529
1530 return height;
1531 }
1532 #endif
1533
1534 return 1;
1535 }
1536
1537 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1538 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1539 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1540 not force the value into range. */
1541
1542 void
1543 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1544 FRAME_PTR f;
1545 register int pix_x, pix_y;
1546 int *x, *y;
1547 NativeRectangle *bounds;
1548 int noclip;
1549 {
1550
1551 #ifdef HAVE_WINDOW_SYSTEM
1552 if (FRAME_WINDOW_P (f))
1553 {
1554 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1555 even for negative values. */
1556 if (pix_x < 0)
1557 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1558 if (pix_y < 0)
1559 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1560
1561 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1562 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1563
1564 if (bounds)
1565 STORE_NATIVE_RECT (*bounds,
1566 FRAME_COL_TO_PIXEL_X (f, pix_x),
1567 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1568 FRAME_COLUMN_WIDTH (f) - 1,
1569 FRAME_LINE_HEIGHT (f) - 1);
1570
1571 if (!noclip)
1572 {
1573 if (pix_x < 0)
1574 pix_x = 0;
1575 else if (pix_x > FRAME_TOTAL_COLS (f))
1576 pix_x = FRAME_TOTAL_COLS (f);
1577
1578 if (pix_y < 0)
1579 pix_y = 0;
1580 else if (pix_y > FRAME_LINES (f))
1581 pix_y = FRAME_LINES (f);
1582 }
1583 }
1584 #endif
1585
1586 *x = pix_x;
1587 *y = pix_y;
1588 }
1589
1590
1591 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1592 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1593 can't tell the positions because W's display is not up to date,
1594 return 0. */
1595
1596 int
1597 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1598 struct window *w;
1599 int hpos, vpos;
1600 int *frame_x, *frame_y;
1601 {
1602 #ifdef HAVE_WINDOW_SYSTEM
1603 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1604 {
1605 int success_p;
1606
1607 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1608 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1609
1610 if (display_completed)
1611 {
1612 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1613 struct glyph *glyph = row->glyphs[TEXT_AREA];
1614 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1615
1616 hpos = row->x;
1617 vpos = row->y;
1618 while (glyph < end)
1619 {
1620 hpos += glyph->pixel_width;
1621 ++glyph;
1622 }
1623
1624 /* If first glyph is partially visible, its first visible position is still 0. */
1625 if (hpos < 0)
1626 hpos = 0;
1627
1628 success_p = 1;
1629 }
1630 else
1631 {
1632 hpos = vpos = 0;
1633 success_p = 0;
1634 }
1635
1636 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1637 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1638 return success_p;
1639 }
1640 #endif
1641
1642 *frame_x = hpos;
1643 *frame_y = vpos;
1644 return 1;
1645 }
1646
1647
1648 #ifdef HAVE_WINDOW_SYSTEM
1649
1650 /* Find the glyph under window-relative coordinates X/Y in window W.
1651 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1652 strings. Return in *HPOS and *VPOS the row and column number of
1653 the glyph found. Return in *AREA the glyph area containing X.
1654 Value is a pointer to the glyph found or null if X/Y is not on
1655 text, or we can't tell because W's current matrix is not up to
1656 date. */
1657
1658 static struct glyph *
1659 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1660 struct window *w;
1661 int x, y;
1662 int *hpos, *vpos, *dx, *dy, *area;
1663 {
1664 struct glyph *glyph, *end;
1665 struct glyph_row *row = NULL;
1666 int x0, i;
1667
1668 /* Find row containing Y. Give up if some row is not enabled. */
1669 for (i = 0; i < w->current_matrix->nrows; ++i)
1670 {
1671 row = MATRIX_ROW (w->current_matrix, i);
1672 if (!row->enabled_p)
1673 return NULL;
1674 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1675 break;
1676 }
1677
1678 *vpos = i;
1679 *hpos = 0;
1680
1681 /* Give up if Y is not in the window. */
1682 if (i == w->current_matrix->nrows)
1683 return NULL;
1684
1685 /* Get the glyph area containing X. */
1686 if (w->pseudo_window_p)
1687 {
1688 *area = TEXT_AREA;
1689 x0 = 0;
1690 }
1691 else
1692 {
1693 if (x < window_box_left_offset (w, TEXT_AREA))
1694 {
1695 *area = LEFT_MARGIN_AREA;
1696 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1697 }
1698 else if (x < window_box_right_offset (w, TEXT_AREA))
1699 {
1700 *area = TEXT_AREA;
1701 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1702 }
1703 else
1704 {
1705 *area = RIGHT_MARGIN_AREA;
1706 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1707 }
1708 }
1709
1710 /* Find glyph containing X. */
1711 glyph = row->glyphs[*area];
1712 end = glyph + row->used[*area];
1713 x -= x0;
1714 while (glyph < end && x >= glyph->pixel_width)
1715 {
1716 x -= glyph->pixel_width;
1717 ++glyph;
1718 }
1719
1720 if (glyph == end)
1721 return NULL;
1722
1723 if (dx)
1724 {
1725 *dx = x;
1726 *dy = y - (row->y + row->ascent - glyph->ascent);
1727 }
1728
1729 *hpos = glyph - row->glyphs[*area];
1730 return glyph;
1731 }
1732
1733
1734 /* EXPORT:
1735 Convert frame-relative x/y to coordinates relative to window W.
1736 Takes pseudo-windows into account. */
1737
1738 void
1739 frame_to_window_pixel_xy (w, x, y)
1740 struct window *w;
1741 int *x, *y;
1742 {
1743 if (w->pseudo_window_p)
1744 {
1745 /* A pseudo-window is always full-width, and starts at the
1746 left edge of the frame, plus a frame border. */
1747 struct frame *f = XFRAME (w->frame);
1748 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1749 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1750 }
1751 else
1752 {
1753 *x -= WINDOW_LEFT_EDGE_X (w);
1754 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1755 }
1756 }
1757
1758 /* EXPORT:
1759 Return in *R the clipping rectangle for glyph string S. */
1760
1761 void
1762 get_glyph_string_clip_rect (s, nr)
1763 struct glyph_string *s;
1764 NativeRectangle *nr;
1765 {
1766 XRectangle r;
1767
1768 if (s->row->full_width_p)
1769 {
1770 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1771 r.x = WINDOW_LEFT_EDGE_X (s->w);
1772 r.width = WINDOW_TOTAL_WIDTH (s->w);
1773
1774 /* Unless displaying a mode or menu bar line, which are always
1775 fully visible, clip to the visible part of the row. */
1776 if (s->w->pseudo_window_p)
1777 r.height = s->row->visible_height;
1778 else
1779 r.height = s->height;
1780 }
1781 else
1782 {
1783 /* This is a text line that may be partially visible. */
1784 r.x = window_box_left (s->w, s->area);
1785 r.width = window_box_width (s->w, s->area);
1786 r.height = s->row->visible_height;
1787 }
1788
1789 if (s->clip_head)
1790 if (r.x < s->clip_head->x)
1791 {
1792 if (r.width >= s->clip_head->x - r.x)
1793 r.width -= s->clip_head->x - r.x;
1794 else
1795 r.width = 0;
1796 r.x = s->clip_head->x;
1797 }
1798 if (s->clip_tail)
1799 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1800 {
1801 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1802 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1803 else
1804 r.width = 0;
1805 }
1806
1807 /* If S draws overlapping rows, it's sufficient to use the top and
1808 bottom of the window for clipping because this glyph string
1809 intentionally draws over other lines. */
1810 if (s->for_overlaps_p)
1811 {
1812 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1813 r.height = window_text_bottom_y (s->w) - r.y;
1814 }
1815 else
1816 {
1817 /* Don't use S->y for clipping because it doesn't take partially
1818 visible lines into account. For example, it can be negative for
1819 partially visible lines at the top of a window. */
1820 if (!s->row->full_width_p
1821 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1822 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1823 else
1824 r.y = max (0, s->row->y);
1825
1826 /* If drawing a tool-bar window, draw it over the internal border
1827 at the top of the window. */
1828 if (WINDOWP (s->f->tool_bar_window)
1829 && s->w == XWINDOW (s->f->tool_bar_window))
1830 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1831 }
1832
1833 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1834
1835 /* If drawing the cursor, don't let glyph draw outside its
1836 advertised boundaries. Cleartype does this under some circumstances. */
1837 if (s->hl == DRAW_CURSOR)
1838 {
1839 struct glyph *glyph = s->first_glyph;
1840 int height, max_y;
1841
1842 if (s->x > r.x)
1843 {
1844 r.width -= s->x - r.x;
1845 r.x = s->x;
1846 }
1847 r.width = min (r.width, glyph->pixel_width);
1848
1849 /* If r.y is below window bottom, ensure that we still see a cursor. */
1850 height = min (glyph->ascent + glyph->descent,
1851 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1852 max_y = window_text_bottom_y (s->w) - height;
1853 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1854 if (s->ybase - glyph->ascent > max_y)
1855 {
1856 r.y = max_y;
1857 r.height = height;
1858 }
1859 else
1860 {
1861 /* Don't draw cursor glyph taller than our actual glyph. */
1862 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1863 if (height < r.height)
1864 {
1865 max_y = r.y + r.height;
1866 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1867 r.height = min (max_y - r.y, height);
1868 }
1869 }
1870 }
1871
1872 #ifdef CONVERT_FROM_XRECT
1873 CONVERT_FROM_XRECT (r, *nr);
1874 #else
1875 *nr = r;
1876 #endif
1877 }
1878
1879
1880 /* EXPORT:
1881 Return the position and height of the phys cursor in window W.
1882 Set w->phys_cursor_width to width of phys cursor.
1883 */
1884
1885 int
1886 get_phys_cursor_geometry (w, row, glyph, heightp)
1887 struct window *w;
1888 struct glyph_row *row;
1889 struct glyph *glyph;
1890 int *heightp;
1891 {
1892 struct frame *f = XFRAME (WINDOW_FRAME (w));
1893 int x, y, wd, h, h0, y0;
1894
1895 /* Compute the width of the rectangle to draw. If on a stretch
1896 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1897 rectangle as wide as the glyph, but use a canonical character
1898 width instead. */
1899 wd = glyph->pixel_width - 1;
1900 #ifdef HAVE_NTGUI
1901 wd++; /* Why? */
1902 #endif
1903 if (glyph->type == STRETCH_GLYPH
1904 && !x_stretch_cursor_p)
1905 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1906 w->phys_cursor_width = wd;
1907
1908 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1909
1910 /* If y is below window bottom, ensure that we still see a cursor. */
1911 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1912
1913 h = max (h0, glyph->ascent + glyph->descent);
1914 h0 = min (h0, glyph->ascent + glyph->descent);
1915
1916 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1917 if (y < y0)
1918 {
1919 h = max (h - (y0 - y) + 1, h0);
1920 y = y0 - 1;
1921 }
1922 else
1923 {
1924 y0 = window_text_bottom_y (w) - h0;
1925 if (y > y0)
1926 {
1927 h += y - y0;
1928 y = y0;
1929 }
1930 }
1931
1932 *heightp = h - 1;
1933 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1934 }
1935
1936
1937 #endif /* HAVE_WINDOW_SYSTEM */
1938
1939 \f
1940 /***********************************************************************
1941 Lisp form evaluation
1942 ***********************************************************************/
1943
1944 /* Error handler for safe_eval and safe_call. */
1945
1946 static Lisp_Object
1947 safe_eval_handler (arg)
1948 Lisp_Object arg;
1949 {
1950 add_to_log ("Error during redisplay: %s", arg, Qnil);
1951 return Qnil;
1952 }
1953
1954
1955 /* Evaluate SEXPR and return the result, or nil if something went
1956 wrong. Prevent redisplay during the evaluation. */
1957
1958 Lisp_Object
1959 safe_eval (sexpr)
1960 Lisp_Object sexpr;
1961 {
1962 Lisp_Object val;
1963
1964 if (inhibit_eval_during_redisplay)
1965 val = Qnil;
1966 else
1967 {
1968 int count = SPECPDL_INDEX ();
1969 struct gcpro gcpro1;
1970
1971 GCPRO1 (sexpr);
1972 specbind (Qinhibit_redisplay, Qt);
1973 /* Use Qt to ensure debugger does not run,
1974 so there is no possibility of wanting to redisplay. */
1975 val = internal_condition_case_1 (Feval, sexpr, Qt,
1976 safe_eval_handler);
1977 UNGCPRO;
1978 val = unbind_to (count, val);
1979 }
1980
1981 return val;
1982 }
1983
1984
1985 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1986 Return the result, or nil if something went wrong. Prevent
1987 redisplay during the evaluation. */
1988
1989 Lisp_Object
1990 safe_call (nargs, args)
1991 int nargs;
1992 Lisp_Object *args;
1993 {
1994 Lisp_Object val;
1995
1996 if (inhibit_eval_during_redisplay)
1997 val = Qnil;
1998 else
1999 {
2000 int count = SPECPDL_INDEX ();
2001 struct gcpro gcpro1;
2002
2003 GCPRO1 (args[0]);
2004 gcpro1.nvars = nargs;
2005 specbind (Qinhibit_redisplay, Qt);
2006 /* Use Qt to ensure debugger does not run,
2007 so there is no possibility of wanting to redisplay. */
2008 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2009 safe_eval_handler);
2010 UNGCPRO;
2011 val = unbind_to (count, val);
2012 }
2013
2014 return val;
2015 }
2016
2017
2018 /* Call function FN with one argument ARG.
2019 Return the result, or nil if something went wrong. */
2020
2021 Lisp_Object
2022 safe_call1 (fn, arg)
2023 Lisp_Object fn, arg;
2024 {
2025 Lisp_Object args[2];
2026 args[0] = fn;
2027 args[1] = arg;
2028 return safe_call (2, args);
2029 }
2030
2031
2032 \f
2033 /***********************************************************************
2034 Debugging
2035 ***********************************************************************/
2036
2037 #if 0
2038
2039 /* Define CHECK_IT to perform sanity checks on iterators.
2040 This is for debugging. It is too slow to do unconditionally. */
2041
2042 static void
2043 check_it (it)
2044 struct it *it;
2045 {
2046 if (it->method == next_element_from_string)
2047 {
2048 xassert (STRINGP (it->string));
2049 xassert (IT_STRING_CHARPOS (*it) >= 0);
2050 }
2051 else
2052 {
2053 xassert (IT_STRING_CHARPOS (*it) < 0);
2054 if (it->method == next_element_from_buffer)
2055 {
2056 /* Check that character and byte positions agree. */
2057 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2058 }
2059 }
2060
2061 if (it->dpvec)
2062 xassert (it->current.dpvec_index >= 0);
2063 else
2064 xassert (it->current.dpvec_index < 0);
2065 }
2066
2067 #define CHECK_IT(IT) check_it ((IT))
2068
2069 #else /* not 0 */
2070
2071 #define CHECK_IT(IT) (void) 0
2072
2073 #endif /* not 0 */
2074
2075
2076 #if GLYPH_DEBUG
2077
2078 /* Check that the window end of window W is what we expect it
2079 to be---the last row in the current matrix displaying text. */
2080
2081 static void
2082 check_window_end (w)
2083 struct window *w;
2084 {
2085 if (!MINI_WINDOW_P (w)
2086 && !NILP (w->window_end_valid))
2087 {
2088 struct glyph_row *row;
2089 xassert ((row = MATRIX_ROW (w->current_matrix,
2090 XFASTINT (w->window_end_vpos)),
2091 !row->enabled_p
2092 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2093 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2094 }
2095 }
2096
2097 #define CHECK_WINDOW_END(W) check_window_end ((W))
2098
2099 #else /* not GLYPH_DEBUG */
2100
2101 #define CHECK_WINDOW_END(W) (void) 0
2102
2103 #endif /* not GLYPH_DEBUG */
2104
2105
2106 \f
2107 /***********************************************************************
2108 Iterator initialization
2109 ***********************************************************************/
2110
2111 /* Initialize IT for displaying current_buffer in window W, starting
2112 at character position CHARPOS. CHARPOS < 0 means that no buffer
2113 position is specified which is useful when the iterator is assigned
2114 a position later. BYTEPOS is the byte position corresponding to
2115 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2116
2117 If ROW is not null, calls to produce_glyphs with IT as parameter
2118 will produce glyphs in that row.
2119
2120 BASE_FACE_ID is the id of a base face to use. It must be one of
2121 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2122 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2123 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2124
2125 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2126 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2127 will be initialized to use the corresponding mode line glyph row of
2128 the desired matrix of W. */
2129
2130 void
2131 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2132 struct it *it;
2133 struct window *w;
2134 int charpos, bytepos;
2135 struct glyph_row *row;
2136 enum face_id base_face_id;
2137 {
2138 int highlight_region_p;
2139
2140 /* Some precondition checks. */
2141 xassert (w != NULL && it != NULL);
2142 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2143 && charpos <= ZV));
2144
2145 /* If face attributes have been changed since the last redisplay,
2146 free realized faces now because they depend on face definitions
2147 that might have changed. Don't free faces while there might be
2148 desired matrices pending which reference these faces. */
2149 if (face_change_count && !inhibit_free_realized_faces)
2150 {
2151 face_change_count = 0;
2152 free_all_realized_faces (Qnil);
2153 }
2154
2155 /* Use one of the mode line rows of W's desired matrix if
2156 appropriate. */
2157 if (row == NULL)
2158 {
2159 if (base_face_id == MODE_LINE_FACE_ID
2160 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2161 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2162 else if (base_face_id == HEADER_LINE_FACE_ID)
2163 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2164 }
2165
2166 /* Clear IT. */
2167 bzero (it, sizeof *it);
2168 it->current.overlay_string_index = -1;
2169 it->current.dpvec_index = -1;
2170 it->base_face_id = base_face_id;
2171 it->string = Qnil;
2172 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2173
2174 /* The window in which we iterate over current_buffer: */
2175 XSETWINDOW (it->window, w);
2176 it->w = w;
2177 it->f = XFRAME (w->frame);
2178
2179 /* Extra space between lines (on window systems only). */
2180 if (base_face_id == DEFAULT_FACE_ID
2181 && FRAME_WINDOW_P (it->f))
2182 {
2183 if (NATNUMP (current_buffer->extra_line_spacing))
2184 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2185 else if (FLOATP (current_buffer->extra_line_spacing))
2186 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2187 * FRAME_LINE_HEIGHT (it->f));
2188 else if (it->f->extra_line_spacing > 0)
2189 it->extra_line_spacing = it->f->extra_line_spacing;
2190 it->max_extra_line_spacing = 0;
2191 }
2192
2193 /* If realized faces have been removed, e.g. because of face
2194 attribute changes of named faces, recompute them. When running
2195 in batch mode, the face cache of Vterminal_frame is null. If
2196 we happen to get called, make a dummy face cache. */
2197 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2198 init_frame_faces (it->f);
2199 if (FRAME_FACE_CACHE (it->f)->used == 0)
2200 recompute_basic_faces (it->f);
2201
2202 /* Current value of the `slice', `space-width', and 'height' properties. */
2203 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2204 it->space_width = Qnil;
2205 it->font_height = Qnil;
2206 it->override_ascent = -1;
2207
2208 /* Are control characters displayed as `^C'? */
2209 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2210
2211 /* -1 means everything between a CR and the following line end
2212 is invisible. >0 means lines indented more than this value are
2213 invisible. */
2214 it->selective = (INTEGERP (current_buffer->selective_display)
2215 ? XFASTINT (current_buffer->selective_display)
2216 : (!NILP (current_buffer->selective_display)
2217 ? -1 : 0));
2218 it->selective_display_ellipsis_p
2219 = !NILP (current_buffer->selective_display_ellipses);
2220
2221 /* Display table to use. */
2222 it->dp = window_display_table (w);
2223
2224 /* Are multibyte characters enabled in current_buffer? */
2225 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2226
2227 /* Non-zero if we should highlight the region. */
2228 highlight_region_p
2229 = (!NILP (Vtransient_mark_mode)
2230 && !NILP (current_buffer->mark_active)
2231 && XMARKER (current_buffer->mark)->buffer != 0);
2232
2233 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2234 start and end of a visible region in window IT->w. Set both to
2235 -1 to indicate no region. */
2236 if (highlight_region_p
2237 /* Maybe highlight only in selected window. */
2238 && (/* Either show region everywhere. */
2239 highlight_nonselected_windows
2240 /* Or show region in the selected window. */
2241 || w == XWINDOW (selected_window)
2242 /* Or show the region if we are in the mini-buffer and W is
2243 the window the mini-buffer refers to. */
2244 || (MINI_WINDOW_P (XWINDOW (selected_window))
2245 && WINDOWP (minibuf_selected_window)
2246 && w == XWINDOW (minibuf_selected_window))))
2247 {
2248 int charpos = marker_position (current_buffer->mark);
2249 it->region_beg_charpos = min (PT, charpos);
2250 it->region_end_charpos = max (PT, charpos);
2251 }
2252 else
2253 it->region_beg_charpos = it->region_end_charpos = -1;
2254
2255 /* Get the position at which the redisplay_end_trigger hook should
2256 be run, if it is to be run at all. */
2257 if (MARKERP (w->redisplay_end_trigger)
2258 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2259 it->redisplay_end_trigger_charpos
2260 = marker_position (w->redisplay_end_trigger);
2261 else if (INTEGERP (w->redisplay_end_trigger))
2262 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2263
2264 /* Correct bogus values of tab_width. */
2265 it->tab_width = XINT (current_buffer->tab_width);
2266 if (it->tab_width <= 0 || it->tab_width > 1000)
2267 it->tab_width = 8;
2268
2269 /* Are lines in the display truncated? */
2270 it->truncate_lines_p
2271 = (base_face_id != DEFAULT_FACE_ID
2272 || XINT (it->w->hscroll)
2273 || (truncate_partial_width_windows
2274 && !WINDOW_FULL_WIDTH_P (it->w))
2275 || !NILP (current_buffer->truncate_lines));
2276
2277 /* Get dimensions of truncation and continuation glyphs. These are
2278 displayed as fringe bitmaps under X, so we don't need them for such
2279 frames. */
2280 if (!FRAME_WINDOW_P (it->f))
2281 {
2282 if (it->truncate_lines_p)
2283 {
2284 /* We will need the truncation glyph. */
2285 xassert (it->glyph_row == NULL);
2286 produce_special_glyphs (it, IT_TRUNCATION);
2287 it->truncation_pixel_width = it->pixel_width;
2288 }
2289 else
2290 {
2291 /* We will need the continuation glyph. */
2292 xassert (it->glyph_row == NULL);
2293 produce_special_glyphs (it, IT_CONTINUATION);
2294 it->continuation_pixel_width = it->pixel_width;
2295 }
2296
2297 /* Reset these values to zero because the produce_special_glyphs
2298 above has changed them. */
2299 it->pixel_width = it->ascent = it->descent = 0;
2300 it->phys_ascent = it->phys_descent = 0;
2301 }
2302
2303 /* Set this after getting the dimensions of truncation and
2304 continuation glyphs, so that we don't produce glyphs when calling
2305 produce_special_glyphs, above. */
2306 it->glyph_row = row;
2307 it->area = TEXT_AREA;
2308
2309 /* Get the dimensions of the display area. The display area
2310 consists of the visible window area plus a horizontally scrolled
2311 part to the left of the window. All x-values are relative to the
2312 start of this total display area. */
2313 if (base_face_id != DEFAULT_FACE_ID)
2314 {
2315 /* Mode lines, menu bar in terminal frames. */
2316 it->first_visible_x = 0;
2317 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2318 }
2319 else
2320 {
2321 it->first_visible_x
2322 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2323 it->last_visible_x = (it->first_visible_x
2324 + window_box_width (w, TEXT_AREA));
2325
2326 /* If we truncate lines, leave room for the truncator glyph(s) at
2327 the right margin. Otherwise, leave room for the continuation
2328 glyph(s). Truncation and continuation glyphs are not inserted
2329 for window-based redisplay. */
2330 if (!FRAME_WINDOW_P (it->f))
2331 {
2332 if (it->truncate_lines_p)
2333 it->last_visible_x -= it->truncation_pixel_width;
2334 else
2335 it->last_visible_x -= it->continuation_pixel_width;
2336 }
2337
2338 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2339 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2340 }
2341
2342 /* Leave room for a border glyph. */
2343 if (!FRAME_WINDOW_P (it->f)
2344 && !WINDOW_RIGHTMOST_P (it->w))
2345 it->last_visible_x -= 1;
2346
2347 it->last_visible_y = window_text_bottom_y (w);
2348
2349 /* For mode lines and alike, arrange for the first glyph having a
2350 left box line if the face specifies a box. */
2351 if (base_face_id != DEFAULT_FACE_ID)
2352 {
2353 struct face *face;
2354
2355 it->face_id = base_face_id;
2356
2357 /* If we have a boxed mode line, make the first character appear
2358 with a left box line. */
2359 face = FACE_FROM_ID (it->f, base_face_id);
2360 if (face->box != FACE_NO_BOX)
2361 it->start_of_box_run_p = 1;
2362 }
2363
2364 /* If a buffer position was specified, set the iterator there,
2365 getting overlays and face properties from that position. */
2366 if (charpos >= BUF_BEG (current_buffer))
2367 {
2368 it->end_charpos = ZV;
2369 it->face_id = -1;
2370 IT_CHARPOS (*it) = charpos;
2371
2372 /* Compute byte position if not specified. */
2373 if (bytepos < charpos)
2374 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2375 else
2376 IT_BYTEPOS (*it) = bytepos;
2377
2378 it->start = it->current;
2379
2380 /* Compute faces etc. */
2381 reseat (it, it->current.pos, 1);
2382 }
2383
2384 CHECK_IT (it);
2385 }
2386
2387
2388 /* Initialize IT for the display of window W with window start POS. */
2389
2390 void
2391 start_display (it, w, pos)
2392 struct it *it;
2393 struct window *w;
2394 struct text_pos pos;
2395 {
2396 struct glyph_row *row;
2397 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2398
2399 row = w->desired_matrix->rows + first_vpos;
2400 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2401 it->first_vpos = first_vpos;
2402
2403 if (!it->truncate_lines_p)
2404 {
2405 int start_at_line_beg_p;
2406 int first_y = it->current_y;
2407
2408 /* If window start is not at a line start, skip forward to POS to
2409 get the correct continuation lines width. */
2410 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2411 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2412 if (!start_at_line_beg_p)
2413 {
2414 int new_x;
2415
2416 reseat_at_previous_visible_line_start (it);
2417 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2418
2419 new_x = it->current_x + it->pixel_width;
2420
2421 /* If lines are continued, this line may end in the middle
2422 of a multi-glyph character (e.g. a control character
2423 displayed as \003, or in the middle of an overlay
2424 string). In this case move_it_to above will not have
2425 taken us to the start of the continuation line but to the
2426 end of the continued line. */
2427 if (it->current_x > 0
2428 && !it->truncate_lines_p /* Lines are continued. */
2429 && (/* And glyph doesn't fit on the line. */
2430 new_x > it->last_visible_x
2431 /* Or it fits exactly and we're on a window
2432 system frame. */
2433 || (new_x == it->last_visible_x
2434 && FRAME_WINDOW_P (it->f))))
2435 {
2436 if (it->current.dpvec_index >= 0
2437 || it->current.overlay_string_index >= 0)
2438 {
2439 set_iterator_to_next (it, 1);
2440 move_it_in_display_line_to (it, -1, -1, 0);
2441 }
2442
2443 it->continuation_lines_width += it->current_x;
2444 }
2445
2446 /* We're starting a new display line, not affected by the
2447 height of the continued line, so clear the appropriate
2448 fields in the iterator structure. */
2449 it->max_ascent = it->max_descent = 0;
2450 it->max_phys_ascent = it->max_phys_descent = 0;
2451
2452 it->current_y = first_y;
2453 it->vpos = 0;
2454 it->current_x = it->hpos = 0;
2455 }
2456 }
2457
2458 #if 0 /* Don't assert the following because start_display is sometimes
2459 called intentionally with a window start that is not at a
2460 line start. Please leave this code in as a comment. */
2461
2462 /* Window start should be on a line start, now. */
2463 xassert (it->continuation_lines_width
2464 || IT_CHARPOS (it) == BEGV
2465 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2466 #endif /* 0 */
2467 }
2468
2469
2470 /* Return 1 if POS is a position in ellipses displayed for invisible
2471 text. W is the window we display, for text property lookup. */
2472
2473 static int
2474 in_ellipses_for_invisible_text_p (pos, w)
2475 struct display_pos *pos;
2476 struct window *w;
2477 {
2478 Lisp_Object prop, window;
2479 int ellipses_p = 0;
2480 int charpos = CHARPOS (pos->pos);
2481
2482 /* If POS specifies a position in a display vector, this might
2483 be for an ellipsis displayed for invisible text. We won't
2484 get the iterator set up for delivering that ellipsis unless
2485 we make sure that it gets aware of the invisible text. */
2486 if (pos->dpvec_index >= 0
2487 && pos->overlay_string_index < 0
2488 && CHARPOS (pos->string_pos) < 0
2489 && charpos > BEGV
2490 && (XSETWINDOW (window, w),
2491 prop = Fget_char_property (make_number (charpos),
2492 Qinvisible, window),
2493 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2494 {
2495 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2496 window);
2497 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2498 }
2499
2500 return ellipses_p;
2501 }
2502
2503
2504 /* Initialize IT for stepping through current_buffer in window W,
2505 starting at position POS that includes overlay string and display
2506 vector/ control character translation position information. Value
2507 is zero if there are overlay strings with newlines at POS. */
2508
2509 static int
2510 init_from_display_pos (it, w, pos)
2511 struct it *it;
2512 struct window *w;
2513 struct display_pos *pos;
2514 {
2515 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2516 int i, overlay_strings_with_newlines = 0;
2517
2518 /* If POS specifies a position in a display vector, this might
2519 be for an ellipsis displayed for invisible text. We won't
2520 get the iterator set up for delivering that ellipsis unless
2521 we make sure that it gets aware of the invisible text. */
2522 if (in_ellipses_for_invisible_text_p (pos, w))
2523 {
2524 --charpos;
2525 bytepos = 0;
2526 }
2527
2528 /* Keep in mind: the call to reseat in init_iterator skips invisible
2529 text, so we might end up at a position different from POS. This
2530 is only a problem when POS is a row start after a newline and an
2531 overlay starts there with an after-string, and the overlay has an
2532 invisible property. Since we don't skip invisible text in
2533 display_line and elsewhere immediately after consuming the
2534 newline before the row start, such a POS will not be in a string,
2535 but the call to init_iterator below will move us to the
2536 after-string. */
2537 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2538
2539 for (i = 0; i < it->n_overlay_strings; ++i)
2540 {
2541 const char *s = SDATA (it->overlay_strings[i]);
2542 const char *e = s + SBYTES (it->overlay_strings[i]);
2543
2544 while (s < e && *s != '\n')
2545 ++s;
2546
2547 if (s < e)
2548 {
2549 overlay_strings_with_newlines = 1;
2550 break;
2551 }
2552 }
2553
2554 /* If position is within an overlay string, set up IT to the right
2555 overlay string. */
2556 if (pos->overlay_string_index >= 0)
2557 {
2558 int relative_index;
2559
2560 /* If the first overlay string happens to have a `display'
2561 property for an image, the iterator will be set up for that
2562 image, and we have to undo that setup first before we can
2563 correct the overlay string index. */
2564 if (it->method == next_element_from_image)
2565 pop_it (it);
2566
2567 /* We already have the first chunk of overlay strings in
2568 IT->overlay_strings. Load more until the one for
2569 pos->overlay_string_index is in IT->overlay_strings. */
2570 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2571 {
2572 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2573 it->current.overlay_string_index = 0;
2574 while (n--)
2575 {
2576 load_overlay_strings (it, 0);
2577 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2578 }
2579 }
2580
2581 it->current.overlay_string_index = pos->overlay_string_index;
2582 relative_index = (it->current.overlay_string_index
2583 % OVERLAY_STRING_CHUNK_SIZE);
2584 it->string = it->overlay_strings[relative_index];
2585 xassert (STRINGP (it->string));
2586 it->current.string_pos = pos->string_pos;
2587 it->method = next_element_from_string;
2588 }
2589
2590 #if 0 /* This is bogus because POS not having an overlay string
2591 position does not mean it's after the string. Example: A
2592 line starting with a before-string and initialization of IT
2593 to the previous row's end position. */
2594 else if (it->current.overlay_string_index >= 0)
2595 {
2596 /* If POS says we're already after an overlay string ending at
2597 POS, make sure to pop the iterator because it will be in
2598 front of that overlay string. When POS is ZV, we've thereby
2599 also ``processed'' overlay strings at ZV. */
2600 while (it->sp)
2601 pop_it (it);
2602 it->current.overlay_string_index = -1;
2603 it->method = next_element_from_buffer;
2604 if (CHARPOS (pos->pos) == ZV)
2605 it->overlay_strings_at_end_processed_p = 1;
2606 }
2607 #endif /* 0 */
2608
2609 if (CHARPOS (pos->string_pos) >= 0)
2610 {
2611 /* Recorded position is not in an overlay string, but in another
2612 string. This can only be a string from a `display' property.
2613 IT should already be filled with that string. */
2614 it->current.string_pos = pos->string_pos;
2615 xassert (STRINGP (it->string));
2616 }
2617
2618 /* Restore position in display vector translations, control
2619 character translations or ellipses. */
2620 if (pos->dpvec_index >= 0)
2621 {
2622 if (it->dpvec == NULL)
2623 get_next_display_element (it);
2624 xassert (it->dpvec && it->current.dpvec_index == 0);
2625 it->current.dpvec_index = pos->dpvec_index;
2626 }
2627
2628 CHECK_IT (it);
2629 return !overlay_strings_with_newlines;
2630 }
2631
2632
2633 /* Initialize IT for stepping through current_buffer in window W
2634 starting at ROW->start. */
2635
2636 static void
2637 init_to_row_start (it, w, row)
2638 struct it *it;
2639 struct window *w;
2640 struct glyph_row *row;
2641 {
2642 init_from_display_pos (it, w, &row->start);
2643 it->start = row->start;
2644 it->continuation_lines_width = row->continuation_lines_width;
2645 CHECK_IT (it);
2646 }
2647
2648
2649 /* Initialize IT for stepping through current_buffer in window W
2650 starting in the line following ROW, i.e. starting at ROW->end.
2651 Value is zero if there are overlay strings with newlines at ROW's
2652 end position. */
2653
2654 static int
2655 init_to_row_end (it, w, row)
2656 struct it *it;
2657 struct window *w;
2658 struct glyph_row *row;
2659 {
2660 int success = 0;
2661
2662 if (init_from_display_pos (it, w, &row->end))
2663 {
2664 if (row->continued_p)
2665 it->continuation_lines_width
2666 = row->continuation_lines_width + row->pixel_width;
2667 CHECK_IT (it);
2668 success = 1;
2669 }
2670
2671 return success;
2672 }
2673
2674
2675
2676 \f
2677 /***********************************************************************
2678 Text properties
2679 ***********************************************************************/
2680
2681 /* Called when IT reaches IT->stop_charpos. Handle text property and
2682 overlay changes. Set IT->stop_charpos to the next position where
2683 to stop. */
2684
2685 static void
2686 handle_stop (it)
2687 struct it *it;
2688 {
2689 enum prop_handled handled;
2690 int handle_overlay_change_p = 1;
2691 struct props *p;
2692
2693 it->dpvec = NULL;
2694 it->current.dpvec_index = -1;
2695
2696 do
2697 {
2698 handled = HANDLED_NORMALLY;
2699
2700 /* Call text property handlers. */
2701 for (p = it_props; p->handler; ++p)
2702 {
2703 handled = p->handler (it);
2704
2705 if (handled == HANDLED_RECOMPUTE_PROPS)
2706 break;
2707 else if (handled == HANDLED_RETURN)
2708 return;
2709 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2710 handle_overlay_change_p = 0;
2711 }
2712
2713 if (handled != HANDLED_RECOMPUTE_PROPS)
2714 {
2715 /* Don't check for overlay strings below when set to deliver
2716 characters from a display vector. */
2717 if (it->method == next_element_from_display_vector)
2718 handle_overlay_change_p = 0;
2719
2720 /* Handle overlay changes. */
2721 if (handle_overlay_change_p)
2722 handled = handle_overlay_change (it);
2723
2724 /* Determine where to stop next. */
2725 if (handled == HANDLED_NORMALLY)
2726 compute_stop_pos (it);
2727 }
2728 }
2729 while (handled == HANDLED_RECOMPUTE_PROPS);
2730 }
2731
2732
2733 /* Compute IT->stop_charpos from text property and overlay change
2734 information for IT's current position. */
2735
2736 static void
2737 compute_stop_pos (it)
2738 struct it *it;
2739 {
2740 register INTERVAL iv, next_iv;
2741 Lisp_Object object, limit, position;
2742
2743 /* If nowhere else, stop at the end. */
2744 it->stop_charpos = it->end_charpos;
2745
2746 if (STRINGP (it->string))
2747 {
2748 /* Strings are usually short, so don't limit the search for
2749 properties. */
2750 object = it->string;
2751 limit = Qnil;
2752 position = make_number (IT_STRING_CHARPOS (*it));
2753 }
2754 else
2755 {
2756 int charpos;
2757
2758 /* If next overlay change is in front of the current stop pos
2759 (which is IT->end_charpos), stop there. Note: value of
2760 next_overlay_change is point-max if no overlay change
2761 follows. */
2762 charpos = next_overlay_change (IT_CHARPOS (*it));
2763 if (charpos < it->stop_charpos)
2764 it->stop_charpos = charpos;
2765
2766 /* If showing the region, we have to stop at the region
2767 start or end because the face might change there. */
2768 if (it->region_beg_charpos > 0)
2769 {
2770 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2771 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2772 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2773 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2774 }
2775
2776 /* Set up variables for computing the stop position from text
2777 property changes. */
2778 XSETBUFFER (object, current_buffer);
2779 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2780 position = make_number (IT_CHARPOS (*it));
2781
2782 }
2783
2784 /* Get the interval containing IT's position. Value is a null
2785 interval if there isn't such an interval. */
2786 iv = validate_interval_range (object, &position, &position, 0);
2787 if (!NULL_INTERVAL_P (iv))
2788 {
2789 Lisp_Object values_here[LAST_PROP_IDX];
2790 struct props *p;
2791
2792 /* Get properties here. */
2793 for (p = it_props; p->handler; ++p)
2794 values_here[p->idx] = textget (iv->plist, *p->name);
2795
2796 /* Look for an interval following iv that has different
2797 properties. */
2798 for (next_iv = next_interval (iv);
2799 (!NULL_INTERVAL_P (next_iv)
2800 && (NILP (limit)
2801 || XFASTINT (limit) > next_iv->position));
2802 next_iv = next_interval (next_iv))
2803 {
2804 for (p = it_props; p->handler; ++p)
2805 {
2806 Lisp_Object new_value;
2807
2808 new_value = textget (next_iv->plist, *p->name);
2809 if (!EQ (values_here[p->idx], new_value))
2810 break;
2811 }
2812
2813 if (p->handler)
2814 break;
2815 }
2816
2817 if (!NULL_INTERVAL_P (next_iv))
2818 {
2819 if (INTEGERP (limit)
2820 && next_iv->position >= XFASTINT (limit))
2821 /* No text property change up to limit. */
2822 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2823 else
2824 /* Text properties change in next_iv. */
2825 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2826 }
2827 }
2828
2829 xassert (STRINGP (it->string)
2830 || (it->stop_charpos >= BEGV
2831 && it->stop_charpos >= IT_CHARPOS (*it)));
2832 }
2833
2834
2835 /* Return the position of the next overlay change after POS in
2836 current_buffer. Value is point-max if no overlay change
2837 follows. This is like `next-overlay-change' but doesn't use
2838 xmalloc. */
2839
2840 static int
2841 next_overlay_change (pos)
2842 int pos;
2843 {
2844 int noverlays;
2845 int endpos;
2846 Lisp_Object *overlays;
2847 int i;
2848
2849 /* Get all overlays at the given position. */
2850 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2851
2852 /* If any of these overlays ends before endpos,
2853 use its ending point instead. */
2854 for (i = 0; i < noverlays; ++i)
2855 {
2856 Lisp_Object oend;
2857 int oendpos;
2858
2859 oend = OVERLAY_END (overlays[i]);
2860 oendpos = OVERLAY_POSITION (oend);
2861 endpos = min (endpos, oendpos);
2862 }
2863
2864 return endpos;
2865 }
2866
2867
2868 \f
2869 /***********************************************************************
2870 Fontification
2871 ***********************************************************************/
2872
2873 /* Handle changes in the `fontified' property of the current buffer by
2874 calling hook functions from Qfontification_functions to fontify
2875 regions of text. */
2876
2877 static enum prop_handled
2878 handle_fontified_prop (it)
2879 struct it *it;
2880 {
2881 Lisp_Object prop, pos;
2882 enum prop_handled handled = HANDLED_NORMALLY;
2883
2884 /* Get the value of the `fontified' property at IT's current buffer
2885 position. (The `fontified' property doesn't have a special
2886 meaning in strings.) If the value is nil, call functions from
2887 Qfontification_functions. */
2888 if (!STRINGP (it->string)
2889 && it->s == NULL
2890 && !NILP (Vfontification_functions)
2891 && !NILP (Vrun_hooks)
2892 && (pos = make_number (IT_CHARPOS (*it)),
2893 prop = Fget_char_property (pos, Qfontified, Qnil),
2894 NILP (prop)))
2895 {
2896 int count = SPECPDL_INDEX ();
2897 Lisp_Object val;
2898
2899 val = Vfontification_functions;
2900 specbind (Qfontification_functions, Qnil);
2901
2902 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2903 safe_call1 (val, pos);
2904 else
2905 {
2906 Lisp_Object globals, fn;
2907 struct gcpro gcpro1, gcpro2;
2908
2909 globals = Qnil;
2910 GCPRO2 (val, globals);
2911
2912 for (; CONSP (val); val = XCDR (val))
2913 {
2914 fn = XCAR (val);
2915
2916 if (EQ (fn, Qt))
2917 {
2918 /* A value of t indicates this hook has a local
2919 binding; it means to run the global binding too.
2920 In a global value, t should not occur. If it
2921 does, we must ignore it to avoid an endless
2922 loop. */
2923 for (globals = Fdefault_value (Qfontification_functions);
2924 CONSP (globals);
2925 globals = XCDR (globals))
2926 {
2927 fn = XCAR (globals);
2928 if (!EQ (fn, Qt))
2929 safe_call1 (fn, pos);
2930 }
2931 }
2932 else
2933 safe_call1 (fn, pos);
2934 }
2935
2936 UNGCPRO;
2937 }
2938
2939 unbind_to (count, Qnil);
2940
2941 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2942 something. This avoids an endless loop if they failed to
2943 fontify the text for which reason ever. */
2944 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2945 handled = HANDLED_RECOMPUTE_PROPS;
2946 }
2947
2948 return handled;
2949 }
2950
2951
2952 \f
2953 /***********************************************************************
2954 Faces
2955 ***********************************************************************/
2956
2957 /* Set up iterator IT from face properties at its current position.
2958 Called from handle_stop. */
2959
2960 static enum prop_handled
2961 handle_face_prop (it)
2962 struct it *it;
2963 {
2964 int new_face_id, next_stop;
2965
2966 if (!STRINGP (it->string))
2967 {
2968 new_face_id
2969 = face_at_buffer_position (it->w,
2970 IT_CHARPOS (*it),
2971 it->region_beg_charpos,
2972 it->region_end_charpos,
2973 &next_stop,
2974 (IT_CHARPOS (*it)
2975 + TEXT_PROP_DISTANCE_LIMIT),
2976 0);
2977
2978 /* Is this a start of a run of characters with box face?
2979 Caveat: this can be called for a freshly initialized
2980 iterator; face_id is -1 in this case. We know that the new
2981 face will not change until limit, i.e. if the new face has a
2982 box, all characters up to limit will have one. But, as
2983 usual, we don't know whether limit is really the end. */
2984 if (new_face_id != it->face_id)
2985 {
2986 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2987
2988 /* If new face has a box but old face has not, this is
2989 the start of a run of characters with box, i.e. it has
2990 a shadow on the left side. The value of face_id of the
2991 iterator will be -1 if this is the initial call that gets
2992 the face. In this case, we have to look in front of IT's
2993 position and see whether there is a face != new_face_id. */
2994 it->start_of_box_run_p
2995 = (new_face->box != FACE_NO_BOX
2996 && (it->face_id >= 0
2997 || IT_CHARPOS (*it) == BEG
2998 || new_face_id != face_before_it_pos (it)));
2999 it->face_box_p = new_face->box != FACE_NO_BOX;
3000 }
3001 }
3002 else
3003 {
3004 int base_face_id, bufpos;
3005
3006 if (it->current.overlay_string_index >= 0)
3007 bufpos = IT_CHARPOS (*it);
3008 else
3009 bufpos = 0;
3010
3011 /* For strings from a buffer, i.e. overlay strings or strings
3012 from a `display' property, use the face at IT's current
3013 buffer position as the base face to merge with, so that
3014 overlay strings appear in the same face as surrounding
3015 text, unless they specify their own faces. */
3016 base_face_id = underlying_face_id (it);
3017
3018 new_face_id = face_at_string_position (it->w,
3019 it->string,
3020 IT_STRING_CHARPOS (*it),
3021 bufpos,
3022 it->region_beg_charpos,
3023 it->region_end_charpos,
3024 &next_stop,
3025 base_face_id, 0);
3026
3027 #if 0 /* This shouldn't be neccessary. Let's check it. */
3028 /* If IT is used to display a mode line we would really like to
3029 use the mode line face instead of the frame's default face. */
3030 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3031 && new_face_id == DEFAULT_FACE_ID)
3032 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3033 #endif
3034
3035 /* Is this a start of a run of characters with box? Caveat:
3036 this can be called for a freshly allocated iterator; face_id
3037 is -1 is this case. We know that the new face will not
3038 change until the next check pos, i.e. if the new face has a
3039 box, all characters up to that position will have a
3040 box. But, as usual, we don't know whether that position
3041 is really the end. */
3042 if (new_face_id != it->face_id)
3043 {
3044 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3045 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3046
3047 /* If new face has a box but old face hasn't, this is the
3048 start of a run of characters with box, i.e. it has a
3049 shadow on the left side. */
3050 it->start_of_box_run_p
3051 = new_face->box && (old_face == NULL || !old_face->box);
3052 it->face_box_p = new_face->box != FACE_NO_BOX;
3053 }
3054 }
3055
3056 it->face_id = new_face_id;
3057 return HANDLED_NORMALLY;
3058 }
3059
3060
3061 /* Return the ID of the face ``underlying'' IT's current position,
3062 which is in a string. If the iterator is associated with a
3063 buffer, return the face at IT's current buffer position.
3064 Otherwise, use the iterator's base_face_id. */
3065
3066 static int
3067 underlying_face_id (it)
3068 struct it *it;
3069 {
3070 int face_id = it->base_face_id, i;
3071
3072 xassert (STRINGP (it->string));
3073
3074 for (i = it->sp - 1; i >= 0; --i)
3075 if (NILP (it->stack[i].string))
3076 face_id = it->stack[i].face_id;
3077
3078 return face_id;
3079 }
3080
3081
3082 /* Compute the face one character before or after the current position
3083 of IT. BEFORE_P non-zero means get the face in front of IT's
3084 position. Value is the id of the face. */
3085
3086 static int
3087 face_before_or_after_it_pos (it, before_p)
3088 struct it *it;
3089 int before_p;
3090 {
3091 int face_id, limit;
3092 int next_check_charpos;
3093 struct text_pos pos;
3094
3095 xassert (it->s == NULL);
3096
3097 if (STRINGP (it->string))
3098 {
3099 int bufpos, base_face_id;
3100
3101 /* No face change past the end of the string (for the case
3102 we are padding with spaces). No face change before the
3103 string start. */
3104 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3105 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3106 return it->face_id;
3107
3108 /* Set pos to the position before or after IT's current position. */
3109 if (before_p)
3110 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3111 else
3112 /* For composition, we must check the character after the
3113 composition. */
3114 pos = (it->what == IT_COMPOSITION
3115 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3116 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3117
3118 if (it->current.overlay_string_index >= 0)
3119 bufpos = IT_CHARPOS (*it);
3120 else
3121 bufpos = 0;
3122
3123 base_face_id = underlying_face_id (it);
3124
3125 /* Get the face for ASCII, or unibyte. */
3126 face_id = face_at_string_position (it->w,
3127 it->string,
3128 CHARPOS (pos),
3129 bufpos,
3130 it->region_beg_charpos,
3131 it->region_end_charpos,
3132 &next_check_charpos,
3133 base_face_id, 0);
3134
3135 /* Correct the face for charsets different from ASCII. Do it
3136 for the multibyte case only. The face returned above is
3137 suitable for unibyte text if IT->string is unibyte. */
3138 if (STRING_MULTIBYTE (it->string))
3139 {
3140 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3141 int rest = SBYTES (it->string) - BYTEPOS (pos);
3142 int c, len;
3143 struct face *face = FACE_FROM_ID (it->f, face_id);
3144
3145 c = string_char_and_length (p, rest, &len);
3146 face_id = FACE_FOR_CHAR (it->f, face, c);
3147 }
3148 }
3149 else
3150 {
3151 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3152 || (IT_CHARPOS (*it) <= BEGV && before_p))
3153 return it->face_id;
3154
3155 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3156 pos = it->current.pos;
3157
3158 if (before_p)
3159 DEC_TEXT_POS (pos, it->multibyte_p);
3160 else
3161 {
3162 if (it->what == IT_COMPOSITION)
3163 /* For composition, we must check the position after the
3164 composition. */
3165 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3166 else
3167 INC_TEXT_POS (pos, it->multibyte_p);
3168 }
3169
3170 /* Determine face for CHARSET_ASCII, or unibyte. */
3171 face_id = face_at_buffer_position (it->w,
3172 CHARPOS (pos),
3173 it->region_beg_charpos,
3174 it->region_end_charpos,
3175 &next_check_charpos,
3176 limit, 0);
3177
3178 /* Correct the face for charsets different from ASCII. Do it
3179 for the multibyte case only. The face returned above is
3180 suitable for unibyte text if current_buffer is unibyte. */
3181 if (it->multibyte_p)
3182 {
3183 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3184 struct face *face = FACE_FROM_ID (it->f, face_id);
3185 face_id = FACE_FOR_CHAR (it->f, face, c);
3186 }
3187 }
3188
3189 return face_id;
3190 }
3191
3192
3193 \f
3194 /***********************************************************************
3195 Invisible text
3196 ***********************************************************************/
3197
3198 /* Set up iterator IT from invisible properties at its current
3199 position. Called from handle_stop. */
3200
3201 static enum prop_handled
3202 handle_invisible_prop (it)
3203 struct it *it;
3204 {
3205 enum prop_handled handled = HANDLED_NORMALLY;
3206
3207 if (STRINGP (it->string))
3208 {
3209 extern Lisp_Object Qinvisible;
3210 Lisp_Object prop, end_charpos, limit, charpos;
3211
3212 /* Get the value of the invisible text property at the
3213 current position. Value will be nil if there is no such
3214 property. */
3215 charpos = make_number (IT_STRING_CHARPOS (*it));
3216 prop = Fget_text_property (charpos, Qinvisible, it->string);
3217
3218 if (!NILP (prop)
3219 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3220 {
3221 handled = HANDLED_RECOMPUTE_PROPS;
3222
3223 /* Get the position at which the next change of the
3224 invisible text property can be found in IT->string.
3225 Value will be nil if the property value is the same for
3226 all the rest of IT->string. */
3227 XSETINT (limit, SCHARS (it->string));
3228 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3229 it->string, limit);
3230
3231 /* Text at current position is invisible. The next
3232 change in the property is at position end_charpos.
3233 Move IT's current position to that position. */
3234 if (INTEGERP (end_charpos)
3235 && XFASTINT (end_charpos) < XFASTINT (limit))
3236 {
3237 struct text_pos old;
3238 old = it->current.string_pos;
3239 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3240 compute_string_pos (&it->current.string_pos, old, it->string);
3241 }
3242 else
3243 {
3244 /* The rest of the string is invisible. If this is an
3245 overlay string, proceed with the next overlay string
3246 or whatever comes and return a character from there. */
3247 if (it->current.overlay_string_index >= 0)
3248 {
3249 next_overlay_string (it);
3250 /* Don't check for overlay strings when we just
3251 finished processing them. */
3252 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3253 }
3254 else
3255 {
3256 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3257 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3258 }
3259 }
3260 }
3261 }
3262 else
3263 {
3264 int invis_p, newpos, next_stop, start_charpos;
3265 Lisp_Object pos, prop, overlay;
3266
3267 /* First of all, is there invisible text at this position? */
3268 start_charpos = IT_CHARPOS (*it);
3269 pos = make_number (IT_CHARPOS (*it));
3270 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3271 &overlay);
3272 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3273
3274 /* If we are on invisible text, skip over it. */
3275 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3276 {
3277 /* Record whether we have to display an ellipsis for the
3278 invisible text. */
3279 int display_ellipsis_p = invis_p == 2;
3280
3281 handled = HANDLED_RECOMPUTE_PROPS;
3282
3283 /* Loop skipping over invisible text. The loop is left at
3284 ZV or with IT on the first char being visible again. */
3285 do
3286 {
3287 /* Try to skip some invisible text. Return value is the
3288 position reached which can be equal to IT's position
3289 if there is nothing invisible here. This skips both
3290 over invisible text properties and overlays with
3291 invisible property. */
3292 newpos = skip_invisible (IT_CHARPOS (*it),
3293 &next_stop, ZV, it->window);
3294
3295 /* If we skipped nothing at all we weren't at invisible
3296 text in the first place. If everything to the end of
3297 the buffer was skipped, end the loop. */
3298 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3299 invis_p = 0;
3300 else
3301 {
3302 /* We skipped some characters but not necessarily
3303 all there are. Check if we ended up on visible
3304 text. Fget_char_property returns the property of
3305 the char before the given position, i.e. if we
3306 get invis_p = 0, this means that the char at
3307 newpos is visible. */
3308 pos = make_number (newpos);
3309 prop = Fget_char_property (pos, Qinvisible, it->window);
3310 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3311 }
3312
3313 /* If we ended up on invisible text, proceed to
3314 skip starting with next_stop. */
3315 if (invis_p)
3316 IT_CHARPOS (*it) = next_stop;
3317 }
3318 while (invis_p);
3319
3320 /* The position newpos is now either ZV or on visible text. */
3321 IT_CHARPOS (*it) = newpos;
3322 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3323
3324 /* If there are before-strings at the start of invisible
3325 text, and the text is invisible because of a text
3326 property, arrange to show before-strings because 20.x did
3327 it that way. (If the text is invisible because of an
3328 overlay property instead of a text property, this is
3329 already handled in the overlay code.) */
3330 if (NILP (overlay)
3331 && get_overlay_strings (it, start_charpos))
3332 {
3333 handled = HANDLED_RECOMPUTE_PROPS;
3334 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3335 }
3336 else if (display_ellipsis_p)
3337 setup_for_ellipsis (it, 0);
3338 }
3339 }
3340
3341 return handled;
3342 }
3343
3344
3345 /* Make iterator IT return `...' next.
3346 Replaces LEN characters from buffer. */
3347
3348 static void
3349 setup_for_ellipsis (it, len)
3350 struct it *it;
3351 int len;
3352 {
3353 /* Use the display table definition for `...'. Invalid glyphs
3354 will be handled by the method returning elements from dpvec. */
3355 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3356 {
3357 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3358 it->dpvec = v->contents;
3359 it->dpend = v->contents + v->size;
3360 }
3361 else
3362 {
3363 /* Default `...'. */
3364 it->dpvec = default_invis_vector;
3365 it->dpend = default_invis_vector + 3;
3366 }
3367
3368 it->dpvec_char_len = len;
3369 it->current.dpvec_index = 0;
3370 it->dpvec_face_id = -1;
3371
3372 /* Remember the current face id in case glyphs specify faces.
3373 IT's face is restored in set_iterator_to_next. */
3374 it->saved_face_id = it->face_id;
3375 it->method = next_element_from_display_vector;
3376 it->ellipsis_p = 1;
3377 }
3378
3379
3380 \f
3381 /***********************************************************************
3382 'display' property
3383 ***********************************************************************/
3384
3385 /* Set up iterator IT from `display' property at its current position.
3386 Called from handle_stop.
3387 We return HANDLED_RETURN if some part of the display property
3388 overrides the display of the buffer text itself.
3389 Otherwise we return HANDLED_NORMALLY. */
3390
3391 static enum prop_handled
3392 handle_display_prop (it)
3393 struct it *it;
3394 {
3395 Lisp_Object prop, object;
3396 struct text_pos *position;
3397 /* Nonzero if some property replaces the display of the text itself. */
3398 int display_replaced_p = 0;
3399
3400 if (STRINGP (it->string))
3401 {
3402 object = it->string;
3403 position = &it->current.string_pos;
3404 }
3405 else
3406 {
3407 object = it->w->buffer;
3408 position = &it->current.pos;
3409 }
3410
3411 /* Reset those iterator values set from display property values. */
3412 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3413 it->space_width = Qnil;
3414 it->font_height = Qnil;
3415 it->voffset = 0;
3416
3417 /* We don't support recursive `display' properties, i.e. string
3418 values that have a string `display' property, that have a string
3419 `display' property etc. */
3420 if (!it->string_from_display_prop_p)
3421 it->area = TEXT_AREA;
3422
3423 prop = Fget_char_property (make_number (position->charpos),
3424 Qdisplay, object);
3425 if (NILP (prop))
3426 return HANDLED_NORMALLY;
3427
3428 if (CONSP (prop)
3429 /* Simple properties. */
3430 && !EQ (XCAR (prop), Qimage)
3431 && !EQ (XCAR (prop), Qspace)
3432 && !EQ (XCAR (prop), Qwhen)
3433 && !EQ (XCAR (prop), Qslice)
3434 && !EQ (XCAR (prop), Qspace_width)
3435 && !EQ (XCAR (prop), Qheight)
3436 && !EQ (XCAR (prop), Qraise)
3437 /* Marginal area specifications. */
3438 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3439 && !EQ (XCAR (prop), Qleft_fringe)
3440 && !EQ (XCAR (prop), Qright_fringe)
3441 && !NILP (XCAR (prop)))
3442 {
3443 for (; CONSP (prop); prop = XCDR (prop))
3444 {
3445 if (handle_single_display_spec (it, XCAR (prop), object,
3446 position, display_replaced_p))
3447 display_replaced_p = 1;
3448 }
3449 }
3450 else if (VECTORP (prop))
3451 {
3452 int i;
3453 for (i = 0; i < ASIZE (prop); ++i)
3454 if (handle_single_display_spec (it, AREF (prop, i), object,
3455 position, display_replaced_p))
3456 display_replaced_p = 1;
3457 }
3458 else
3459 {
3460 if (handle_single_display_spec (it, prop, object, position, 0))
3461 display_replaced_p = 1;
3462 }
3463
3464 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3465 }
3466
3467
3468 /* Value is the position of the end of the `display' property starting
3469 at START_POS in OBJECT. */
3470
3471 static struct text_pos
3472 display_prop_end (it, object, start_pos)
3473 struct it *it;
3474 Lisp_Object object;
3475 struct text_pos start_pos;
3476 {
3477 Lisp_Object end;
3478 struct text_pos end_pos;
3479
3480 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3481 Qdisplay, object, Qnil);
3482 CHARPOS (end_pos) = XFASTINT (end);
3483 if (STRINGP (object))
3484 compute_string_pos (&end_pos, start_pos, it->string);
3485 else
3486 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3487
3488 return end_pos;
3489 }
3490
3491
3492 /* Set up IT from a single `display' specification PROP. OBJECT
3493 is the object in which the `display' property was found. *POSITION
3494 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3495 means that we previously saw a display specification which already
3496 replaced text display with something else, for example an image;
3497 we ignore such properties after the first one has been processed.
3498
3499 If PROP is a `space' or `image' specification, and in some other
3500 cases too, set *POSITION to the position where the `display'
3501 property ends.
3502
3503 Value is non-zero if something was found which replaces the display
3504 of buffer or string text. */
3505
3506 static int
3507 handle_single_display_spec (it, spec, object, position,
3508 display_replaced_before_p)
3509 struct it *it;
3510 Lisp_Object spec;
3511 Lisp_Object object;
3512 struct text_pos *position;
3513 int display_replaced_before_p;
3514 {
3515 Lisp_Object form;
3516 Lisp_Object location, value;
3517 struct text_pos start_pos;
3518 int valid_p;
3519
3520 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3521 If the result is non-nil, use VALUE instead of SPEC. */
3522 form = Qt;
3523 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3524 {
3525 spec = XCDR (spec);
3526 if (!CONSP (spec))
3527 return 0;
3528 form = XCAR (spec);
3529 spec = XCDR (spec);
3530 }
3531
3532 if (!NILP (form) && !EQ (form, Qt))
3533 {
3534 int count = SPECPDL_INDEX ();
3535 struct gcpro gcpro1;
3536
3537 /* Bind `object' to the object having the `display' property, a
3538 buffer or string. Bind `position' to the position in the
3539 object where the property was found, and `buffer-position'
3540 to the current position in the buffer. */
3541 specbind (Qobject, object);
3542 specbind (Qposition, make_number (CHARPOS (*position)));
3543 specbind (Qbuffer_position,
3544 make_number (STRINGP (object)
3545 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3546 GCPRO1 (form);
3547 form = safe_eval (form);
3548 UNGCPRO;
3549 unbind_to (count, Qnil);
3550 }
3551
3552 if (NILP (form))
3553 return 0;
3554
3555 /* Handle `(height HEIGHT)' specifications. */
3556 if (CONSP (spec)
3557 && EQ (XCAR (spec), Qheight)
3558 && CONSP (XCDR (spec)))
3559 {
3560 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3561 return 0;
3562
3563 it->font_height = XCAR (XCDR (spec));
3564 if (!NILP (it->font_height))
3565 {
3566 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3567 int new_height = -1;
3568
3569 if (CONSP (it->font_height)
3570 && (EQ (XCAR (it->font_height), Qplus)
3571 || EQ (XCAR (it->font_height), Qminus))
3572 && CONSP (XCDR (it->font_height))
3573 && INTEGERP (XCAR (XCDR (it->font_height))))
3574 {
3575 /* `(+ N)' or `(- N)' where N is an integer. */
3576 int steps = XINT (XCAR (XCDR (it->font_height)));
3577 if (EQ (XCAR (it->font_height), Qplus))
3578 steps = - steps;
3579 it->face_id = smaller_face (it->f, it->face_id, steps);
3580 }
3581 else if (FUNCTIONP (it->font_height))
3582 {
3583 /* Call function with current height as argument.
3584 Value is the new height. */
3585 Lisp_Object height;
3586 height = safe_call1 (it->font_height,
3587 face->lface[LFACE_HEIGHT_INDEX]);
3588 if (NUMBERP (height))
3589 new_height = XFLOATINT (height);
3590 }
3591 else if (NUMBERP (it->font_height))
3592 {
3593 /* Value is a multiple of the canonical char height. */
3594 struct face *face;
3595
3596 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3597 new_height = (XFLOATINT (it->font_height)
3598 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3599 }
3600 else
3601 {
3602 /* Evaluate IT->font_height with `height' bound to the
3603 current specified height to get the new height. */
3604 int count = SPECPDL_INDEX ();
3605
3606 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3607 value = safe_eval (it->font_height);
3608 unbind_to (count, Qnil);
3609
3610 if (NUMBERP (value))
3611 new_height = XFLOATINT (value);
3612 }
3613
3614 if (new_height > 0)
3615 it->face_id = face_with_height (it->f, it->face_id, new_height);
3616 }
3617
3618 return 0;
3619 }
3620
3621 /* Handle `(space_width WIDTH)'. */
3622 if (CONSP (spec)
3623 && EQ (XCAR (spec), Qspace_width)
3624 && CONSP (XCDR (spec)))
3625 {
3626 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3627 return 0;
3628
3629 value = XCAR (XCDR (spec));
3630 if (NUMBERP (value) && XFLOATINT (value) > 0)
3631 it->space_width = value;
3632
3633 return 0;
3634 }
3635
3636 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3637 if (CONSP (spec)
3638 && EQ (XCAR (spec), Qslice))
3639 {
3640 Lisp_Object tem;
3641
3642 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3643 return 0;
3644
3645 if (tem = XCDR (spec), CONSP (tem))
3646 {
3647 it->slice.x = XCAR (tem);
3648 if (tem = XCDR (tem), CONSP (tem))
3649 {
3650 it->slice.y = XCAR (tem);
3651 if (tem = XCDR (tem), CONSP (tem))
3652 {
3653 it->slice.width = XCAR (tem);
3654 if (tem = XCDR (tem), CONSP (tem))
3655 it->slice.height = XCAR (tem);
3656 }
3657 }
3658 }
3659
3660 return 0;
3661 }
3662
3663 /* Handle `(raise FACTOR)'. */
3664 if (CONSP (spec)
3665 && EQ (XCAR (spec), Qraise)
3666 && CONSP (XCDR (spec)))
3667 {
3668 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3669 return 0;
3670
3671 #ifdef HAVE_WINDOW_SYSTEM
3672 value = XCAR (XCDR (spec));
3673 if (NUMBERP (value))
3674 {
3675 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3676 it->voffset = - (XFLOATINT (value)
3677 * (FONT_HEIGHT (face->font)));
3678 }
3679 #endif /* HAVE_WINDOW_SYSTEM */
3680
3681 return 0;
3682 }
3683
3684 /* Don't handle the other kinds of display specifications
3685 inside a string that we got from a `display' property. */
3686 if (it->string_from_display_prop_p)
3687 return 0;
3688
3689 /* Characters having this form of property are not displayed, so
3690 we have to find the end of the property. */
3691 start_pos = *position;
3692 *position = display_prop_end (it, object, start_pos);
3693 value = Qnil;
3694
3695 /* Stop the scan at that end position--we assume that all
3696 text properties change there. */
3697 it->stop_charpos = position->charpos;
3698
3699 /* Handle `(left-fringe BITMAP [FACE])'
3700 and `(right-fringe BITMAP [FACE])'. */
3701 if (CONSP (spec)
3702 && (EQ (XCAR (spec), Qleft_fringe)
3703 || EQ (XCAR (spec), Qright_fringe))
3704 && CONSP (XCDR (spec)))
3705 {
3706 int face_id = DEFAULT_FACE_ID;
3707 int fringe_bitmap;
3708
3709 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3710 /* If we return here, POSITION has been advanced
3711 across the text with this property. */
3712 return 0;
3713
3714 #ifdef HAVE_WINDOW_SYSTEM
3715 value = XCAR (XCDR (spec));
3716 if (!SYMBOLP (value)
3717 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3718 /* If we return here, POSITION has been advanced
3719 across the text with this property. */
3720 return 0;
3721
3722 if (CONSP (XCDR (XCDR (spec))))
3723 {
3724 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3725 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3726 if (face_id2 >= 0)
3727 face_id = face_id2;
3728 }
3729
3730 /* Save current settings of IT so that we can restore them
3731 when we are finished with the glyph property value. */
3732
3733 push_it (it);
3734
3735 it->area = TEXT_AREA;
3736 it->what = IT_IMAGE;
3737 it->image_id = -1; /* no image */
3738 it->position = start_pos;
3739 it->object = NILP (object) ? it->w->buffer : object;
3740 it->method = next_element_from_image;
3741 it->face_id = face_id;
3742
3743 /* Say that we haven't consumed the characters with
3744 `display' property yet. The call to pop_it in
3745 set_iterator_to_next will clean this up. */
3746 *position = start_pos;
3747
3748 if (EQ (XCAR (spec), Qleft_fringe))
3749 {
3750 it->left_user_fringe_bitmap = fringe_bitmap;
3751 it->left_user_fringe_face_id = face_id;
3752 }
3753 else
3754 {
3755 it->right_user_fringe_bitmap = fringe_bitmap;
3756 it->right_user_fringe_face_id = face_id;
3757 }
3758 #endif /* HAVE_WINDOW_SYSTEM */
3759 return 1;
3760 }
3761
3762 /* Prepare to handle `((margin left-margin) ...)',
3763 `((margin right-margin) ...)' and `((margin nil) ...)'
3764 prefixes for display specifications. */
3765 location = Qunbound;
3766 if (CONSP (spec) && CONSP (XCAR (spec)))
3767 {
3768 Lisp_Object tem;
3769
3770 value = XCDR (spec);
3771 if (CONSP (value))
3772 value = XCAR (value);
3773
3774 tem = XCAR (spec);
3775 if (EQ (XCAR (tem), Qmargin)
3776 && (tem = XCDR (tem),
3777 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3778 (NILP (tem)
3779 || EQ (tem, Qleft_margin)
3780 || EQ (tem, Qright_margin))))
3781 location = tem;
3782 }
3783
3784 if (EQ (location, Qunbound))
3785 {
3786 location = Qnil;
3787 value = spec;
3788 }
3789
3790 /* After this point, VALUE is the property after any
3791 margin prefix has been stripped. It must be a string,
3792 an image specification, or `(space ...)'.
3793
3794 LOCATION specifies where to display: `left-margin',
3795 `right-margin' or nil. */
3796
3797 valid_p = (STRINGP (value)
3798 #ifdef HAVE_WINDOW_SYSTEM
3799 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3800 #endif /* not HAVE_WINDOW_SYSTEM */
3801 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3802
3803 if (valid_p && !display_replaced_before_p)
3804 {
3805 /* Save current settings of IT so that we can restore them
3806 when we are finished with the glyph property value. */
3807 push_it (it);
3808
3809 if (NILP (location))
3810 it->area = TEXT_AREA;
3811 else if (EQ (location, Qleft_margin))
3812 it->area = LEFT_MARGIN_AREA;
3813 else
3814 it->area = RIGHT_MARGIN_AREA;
3815
3816 if (STRINGP (value))
3817 {
3818 it->string = value;
3819 it->multibyte_p = STRING_MULTIBYTE (it->string);
3820 it->current.overlay_string_index = -1;
3821 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3822 it->end_charpos = it->string_nchars = SCHARS (it->string);
3823 it->method = next_element_from_string;
3824 it->stop_charpos = 0;
3825 it->string_from_display_prop_p = 1;
3826 /* Say that we haven't consumed the characters with
3827 `display' property yet. The call to pop_it in
3828 set_iterator_to_next will clean this up. */
3829 *position = start_pos;
3830 }
3831 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3832 {
3833 it->method = next_element_from_stretch;
3834 it->object = value;
3835 it->current.pos = it->position = start_pos;
3836 }
3837 #ifdef HAVE_WINDOW_SYSTEM
3838 else
3839 {
3840 it->what = IT_IMAGE;
3841 it->image_id = lookup_image (it->f, value);
3842 it->position = start_pos;
3843 it->object = NILP (object) ? it->w->buffer : object;
3844 it->method = next_element_from_image;
3845
3846 /* Say that we haven't consumed the characters with
3847 `display' property yet. The call to pop_it in
3848 set_iterator_to_next will clean this up. */
3849 *position = start_pos;
3850 }
3851 #endif /* HAVE_WINDOW_SYSTEM */
3852
3853 return 1;
3854 }
3855
3856 /* Invalid property or property not supported. Restore
3857 POSITION to what it was before. */
3858 *position = start_pos;
3859 return 0;
3860 }
3861
3862
3863 /* Check if SPEC is a display specification value whose text should be
3864 treated as intangible. */
3865
3866 static int
3867 single_display_spec_intangible_p (prop)
3868 Lisp_Object prop;
3869 {
3870 /* Skip over `when FORM'. */
3871 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3872 {
3873 prop = XCDR (prop);
3874 if (!CONSP (prop))
3875 return 0;
3876 prop = XCDR (prop);
3877 }
3878
3879 if (STRINGP (prop))
3880 return 1;
3881
3882 if (!CONSP (prop))
3883 return 0;
3884
3885 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3886 we don't need to treat text as intangible. */
3887 if (EQ (XCAR (prop), Qmargin))
3888 {
3889 prop = XCDR (prop);
3890 if (!CONSP (prop))
3891 return 0;
3892
3893 prop = XCDR (prop);
3894 if (!CONSP (prop)
3895 || EQ (XCAR (prop), Qleft_margin)
3896 || EQ (XCAR (prop), Qright_margin))
3897 return 0;
3898 }
3899
3900 return (CONSP (prop)
3901 && (EQ (XCAR (prop), Qimage)
3902 || EQ (XCAR (prop), Qspace)));
3903 }
3904
3905
3906 /* Check if PROP is a display property value whose text should be
3907 treated as intangible. */
3908
3909 int
3910 display_prop_intangible_p (prop)
3911 Lisp_Object prop;
3912 {
3913 if (CONSP (prop)
3914 && CONSP (XCAR (prop))
3915 && !EQ (Qmargin, XCAR (XCAR (prop))))
3916 {
3917 /* A list of sub-properties. */
3918 while (CONSP (prop))
3919 {
3920 if (single_display_spec_intangible_p (XCAR (prop)))
3921 return 1;
3922 prop = XCDR (prop);
3923 }
3924 }
3925 else if (VECTORP (prop))
3926 {
3927 /* A vector of sub-properties. */
3928 int i;
3929 for (i = 0; i < ASIZE (prop); ++i)
3930 if (single_display_spec_intangible_p (AREF (prop, i)))
3931 return 1;
3932 }
3933 else
3934 return single_display_spec_intangible_p (prop);
3935
3936 return 0;
3937 }
3938
3939
3940 /* Return 1 if PROP is a display sub-property value containing STRING. */
3941
3942 static int
3943 single_display_spec_string_p (prop, string)
3944 Lisp_Object prop, string;
3945 {
3946 if (EQ (string, prop))
3947 return 1;
3948
3949 /* Skip over `when FORM'. */
3950 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3951 {
3952 prop = XCDR (prop);
3953 if (!CONSP (prop))
3954 return 0;
3955 prop = XCDR (prop);
3956 }
3957
3958 if (CONSP (prop))
3959 /* Skip over `margin LOCATION'. */
3960 if (EQ (XCAR (prop), Qmargin))
3961 {
3962 prop = XCDR (prop);
3963 if (!CONSP (prop))
3964 return 0;
3965
3966 prop = XCDR (prop);
3967 if (!CONSP (prop))
3968 return 0;
3969 }
3970
3971 return CONSP (prop) && EQ (XCAR (prop), string);
3972 }
3973
3974
3975 /* Return 1 if STRING appears in the `display' property PROP. */
3976
3977 static int
3978 display_prop_string_p (prop, string)
3979 Lisp_Object prop, string;
3980 {
3981 if (CONSP (prop)
3982 && CONSP (XCAR (prop))
3983 && !EQ (Qmargin, XCAR (XCAR (prop))))
3984 {
3985 /* A list of sub-properties. */
3986 while (CONSP (prop))
3987 {
3988 if (single_display_spec_string_p (XCAR (prop), string))
3989 return 1;
3990 prop = XCDR (prop);
3991 }
3992 }
3993 else if (VECTORP (prop))
3994 {
3995 /* A vector of sub-properties. */
3996 int i;
3997 for (i = 0; i < ASIZE (prop); ++i)
3998 if (single_display_spec_string_p (AREF (prop, i), string))
3999 return 1;
4000 }
4001 else
4002 return single_display_spec_string_p (prop, string);
4003
4004 return 0;
4005 }
4006
4007
4008 /* Determine from which buffer position in W's buffer STRING comes
4009 from. AROUND_CHARPOS is an approximate position where it could
4010 be from. Value is the buffer position or 0 if it couldn't be
4011 determined.
4012
4013 W's buffer must be current.
4014
4015 This function is necessary because we don't record buffer positions
4016 in glyphs generated from strings (to keep struct glyph small).
4017 This function may only use code that doesn't eval because it is
4018 called asynchronously from note_mouse_highlight. */
4019
4020 int
4021 string_buffer_position (w, string, around_charpos)
4022 struct window *w;
4023 Lisp_Object string;
4024 int around_charpos;
4025 {
4026 Lisp_Object limit, prop, pos;
4027 const int MAX_DISTANCE = 1000;
4028 int found = 0;
4029
4030 pos = make_number (around_charpos);
4031 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4032 while (!found && !EQ (pos, limit))
4033 {
4034 prop = Fget_char_property (pos, Qdisplay, Qnil);
4035 if (!NILP (prop) && display_prop_string_p (prop, string))
4036 found = 1;
4037 else
4038 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4039 }
4040
4041 if (!found)
4042 {
4043 pos = make_number (around_charpos);
4044 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4045 while (!found && !EQ (pos, limit))
4046 {
4047 prop = Fget_char_property (pos, Qdisplay, Qnil);
4048 if (!NILP (prop) && display_prop_string_p (prop, string))
4049 found = 1;
4050 else
4051 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4052 limit);
4053 }
4054 }
4055
4056 return found ? XINT (pos) : 0;
4057 }
4058
4059
4060 \f
4061 /***********************************************************************
4062 `composition' property
4063 ***********************************************************************/
4064
4065 /* Set up iterator IT from `composition' property at its current
4066 position. Called from handle_stop. */
4067
4068 static enum prop_handled
4069 handle_composition_prop (it)
4070 struct it *it;
4071 {
4072 Lisp_Object prop, string;
4073 int pos, pos_byte, end;
4074 enum prop_handled handled = HANDLED_NORMALLY;
4075
4076 if (STRINGP (it->string))
4077 {
4078 pos = IT_STRING_CHARPOS (*it);
4079 pos_byte = IT_STRING_BYTEPOS (*it);
4080 string = it->string;
4081 }
4082 else
4083 {
4084 pos = IT_CHARPOS (*it);
4085 pos_byte = IT_BYTEPOS (*it);
4086 string = Qnil;
4087 }
4088
4089 /* If there's a valid composition and point is not inside of the
4090 composition (in the case that the composition is from the current
4091 buffer), draw a glyph composed from the composition components. */
4092 if (find_composition (pos, -1, &pos, &end, &prop, string)
4093 && COMPOSITION_VALID_P (pos, end, prop)
4094 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4095 {
4096 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4097
4098 if (id >= 0)
4099 {
4100 it->method = next_element_from_composition;
4101 it->cmp_id = id;
4102 it->cmp_len = COMPOSITION_LENGTH (prop);
4103 /* For a terminal, draw only the first character of the
4104 components. */
4105 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4106 it->len = (STRINGP (it->string)
4107 ? string_char_to_byte (it->string, end)
4108 : CHAR_TO_BYTE (end)) - pos_byte;
4109 it->stop_charpos = end;
4110 handled = HANDLED_RETURN;
4111 }
4112 }
4113
4114 return handled;
4115 }
4116
4117
4118 \f
4119 /***********************************************************************
4120 Overlay strings
4121 ***********************************************************************/
4122
4123 /* The following structure is used to record overlay strings for
4124 later sorting in load_overlay_strings. */
4125
4126 struct overlay_entry
4127 {
4128 Lisp_Object overlay;
4129 Lisp_Object string;
4130 int priority;
4131 int after_string_p;
4132 };
4133
4134
4135 /* Set up iterator IT from overlay strings at its current position.
4136 Called from handle_stop. */
4137
4138 static enum prop_handled
4139 handle_overlay_change (it)
4140 struct it *it;
4141 {
4142 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4143 return HANDLED_RECOMPUTE_PROPS;
4144 else
4145 return HANDLED_NORMALLY;
4146 }
4147
4148
4149 /* Set up the next overlay string for delivery by IT, if there is an
4150 overlay string to deliver. Called by set_iterator_to_next when the
4151 end of the current overlay string is reached. If there are more
4152 overlay strings to display, IT->string and
4153 IT->current.overlay_string_index are set appropriately here.
4154 Otherwise IT->string is set to nil. */
4155
4156 static void
4157 next_overlay_string (it)
4158 struct it *it;
4159 {
4160 ++it->current.overlay_string_index;
4161 if (it->current.overlay_string_index == it->n_overlay_strings)
4162 {
4163 /* No more overlay strings. Restore IT's settings to what
4164 they were before overlay strings were processed, and
4165 continue to deliver from current_buffer. */
4166 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4167
4168 pop_it (it);
4169 xassert (it->stop_charpos >= BEGV
4170 && it->stop_charpos <= it->end_charpos);
4171 it->string = Qnil;
4172 it->current.overlay_string_index = -1;
4173 SET_TEXT_POS (it->current.string_pos, -1, -1);
4174 it->n_overlay_strings = 0;
4175 it->method = next_element_from_buffer;
4176
4177 /* If we're at the end of the buffer, record that we have
4178 processed the overlay strings there already, so that
4179 next_element_from_buffer doesn't try it again. */
4180 if (IT_CHARPOS (*it) >= it->end_charpos)
4181 it->overlay_strings_at_end_processed_p = 1;
4182
4183 /* If we have to display `...' for invisible text, set
4184 the iterator up for that. */
4185 if (display_ellipsis_p)
4186 setup_for_ellipsis (it, 0);
4187 }
4188 else
4189 {
4190 /* There are more overlay strings to process. If
4191 IT->current.overlay_string_index has advanced to a position
4192 where we must load IT->overlay_strings with more strings, do
4193 it. */
4194 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4195
4196 if (it->current.overlay_string_index && i == 0)
4197 load_overlay_strings (it, 0);
4198
4199 /* Initialize IT to deliver display elements from the overlay
4200 string. */
4201 it->string = it->overlay_strings[i];
4202 it->multibyte_p = STRING_MULTIBYTE (it->string);
4203 SET_TEXT_POS (it->current.string_pos, 0, 0);
4204 it->method = next_element_from_string;
4205 it->stop_charpos = 0;
4206 }
4207
4208 CHECK_IT (it);
4209 }
4210
4211
4212 /* Compare two overlay_entry structures E1 and E2. Used as a
4213 comparison function for qsort in load_overlay_strings. Overlay
4214 strings for the same position are sorted so that
4215
4216 1. All after-strings come in front of before-strings, except
4217 when they come from the same overlay.
4218
4219 2. Within after-strings, strings are sorted so that overlay strings
4220 from overlays with higher priorities come first.
4221
4222 2. Within before-strings, strings are sorted so that overlay
4223 strings from overlays with higher priorities come last.
4224
4225 Value is analogous to strcmp. */
4226
4227
4228 static int
4229 compare_overlay_entries (e1, e2)
4230 void *e1, *e2;
4231 {
4232 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4233 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4234 int result;
4235
4236 if (entry1->after_string_p != entry2->after_string_p)
4237 {
4238 /* Let after-strings appear in front of before-strings if
4239 they come from different overlays. */
4240 if (EQ (entry1->overlay, entry2->overlay))
4241 result = entry1->after_string_p ? 1 : -1;
4242 else
4243 result = entry1->after_string_p ? -1 : 1;
4244 }
4245 else if (entry1->after_string_p)
4246 /* After-strings sorted in order of decreasing priority. */
4247 result = entry2->priority - entry1->priority;
4248 else
4249 /* Before-strings sorted in order of increasing priority. */
4250 result = entry1->priority - entry2->priority;
4251
4252 return result;
4253 }
4254
4255
4256 /* Load the vector IT->overlay_strings with overlay strings from IT's
4257 current buffer position, or from CHARPOS if that is > 0. Set
4258 IT->n_overlays to the total number of overlay strings found.
4259
4260 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4261 a time. On entry into load_overlay_strings,
4262 IT->current.overlay_string_index gives the number of overlay
4263 strings that have already been loaded by previous calls to this
4264 function.
4265
4266 IT->add_overlay_start contains an additional overlay start
4267 position to consider for taking overlay strings from, if non-zero.
4268 This position comes into play when the overlay has an `invisible'
4269 property, and both before and after-strings. When we've skipped to
4270 the end of the overlay, because of its `invisible' property, we
4271 nevertheless want its before-string to appear.
4272 IT->add_overlay_start will contain the overlay start position
4273 in this case.
4274
4275 Overlay strings are sorted so that after-string strings come in
4276 front of before-string strings. Within before and after-strings,
4277 strings are sorted by overlay priority. See also function
4278 compare_overlay_entries. */
4279
4280 static void
4281 load_overlay_strings (it, charpos)
4282 struct it *it;
4283 int charpos;
4284 {
4285 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4286 Lisp_Object overlay, window, str, invisible;
4287 struct Lisp_Overlay *ov;
4288 int start, end;
4289 int size = 20;
4290 int n = 0, i, j, invis_p;
4291 struct overlay_entry *entries
4292 = (struct overlay_entry *) alloca (size * sizeof *entries);
4293
4294 if (charpos <= 0)
4295 charpos = IT_CHARPOS (*it);
4296
4297 /* Append the overlay string STRING of overlay OVERLAY to vector
4298 `entries' which has size `size' and currently contains `n'
4299 elements. AFTER_P non-zero means STRING is an after-string of
4300 OVERLAY. */
4301 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4302 do \
4303 { \
4304 Lisp_Object priority; \
4305 \
4306 if (n == size) \
4307 { \
4308 int new_size = 2 * size; \
4309 struct overlay_entry *old = entries; \
4310 entries = \
4311 (struct overlay_entry *) alloca (new_size \
4312 * sizeof *entries); \
4313 bcopy (old, entries, size * sizeof *entries); \
4314 size = new_size; \
4315 } \
4316 \
4317 entries[n].string = (STRING); \
4318 entries[n].overlay = (OVERLAY); \
4319 priority = Foverlay_get ((OVERLAY), Qpriority); \
4320 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4321 entries[n].after_string_p = (AFTER_P); \
4322 ++n; \
4323 } \
4324 while (0)
4325
4326 /* Process overlay before the overlay center. */
4327 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4328 {
4329 XSETMISC (overlay, ov);
4330 xassert (OVERLAYP (overlay));
4331 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4332 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4333
4334 if (end < charpos)
4335 break;
4336
4337 /* Skip this overlay if it doesn't start or end at IT's current
4338 position. */
4339 if (end != charpos && start != charpos)
4340 continue;
4341
4342 /* Skip this overlay if it doesn't apply to IT->w. */
4343 window = Foverlay_get (overlay, Qwindow);
4344 if (WINDOWP (window) && XWINDOW (window) != it->w)
4345 continue;
4346
4347 /* If the text ``under'' the overlay is invisible, both before-
4348 and after-strings from this overlay are visible; start and
4349 end position are indistinguishable. */
4350 invisible = Foverlay_get (overlay, Qinvisible);
4351 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4352
4353 /* If overlay has a non-empty before-string, record it. */
4354 if ((start == charpos || (end == charpos && invis_p))
4355 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4356 && SCHARS (str))
4357 RECORD_OVERLAY_STRING (overlay, str, 0);
4358
4359 /* If overlay has a non-empty after-string, record it. */
4360 if ((end == charpos || (start == charpos && invis_p))
4361 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4362 && SCHARS (str))
4363 RECORD_OVERLAY_STRING (overlay, str, 1);
4364 }
4365
4366 /* Process overlays after the overlay center. */
4367 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4368 {
4369 XSETMISC (overlay, ov);
4370 xassert (OVERLAYP (overlay));
4371 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4372 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4373
4374 if (start > charpos)
4375 break;
4376
4377 /* Skip this overlay if it doesn't start or end at IT's current
4378 position. */
4379 if (end != charpos && start != charpos)
4380 continue;
4381
4382 /* Skip this overlay if it doesn't apply to IT->w. */
4383 window = Foverlay_get (overlay, Qwindow);
4384 if (WINDOWP (window) && XWINDOW (window) != it->w)
4385 continue;
4386
4387 /* If the text ``under'' the overlay is invisible, it has a zero
4388 dimension, and both before- and after-strings apply. */
4389 invisible = Foverlay_get (overlay, Qinvisible);
4390 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4391
4392 /* If overlay has a non-empty before-string, record it. */
4393 if ((start == charpos || (end == charpos && invis_p))
4394 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4395 && SCHARS (str))
4396 RECORD_OVERLAY_STRING (overlay, str, 0);
4397
4398 /* If overlay has a non-empty after-string, record it. */
4399 if ((end == charpos || (start == charpos && invis_p))
4400 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4401 && SCHARS (str))
4402 RECORD_OVERLAY_STRING (overlay, str, 1);
4403 }
4404
4405 #undef RECORD_OVERLAY_STRING
4406
4407 /* Sort entries. */
4408 if (n > 1)
4409 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4410
4411 /* Record the total number of strings to process. */
4412 it->n_overlay_strings = n;
4413
4414 /* IT->current.overlay_string_index is the number of overlay strings
4415 that have already been consumed by IT. Copy some of the
4416 remaining overlay strings to IT->overlay_strings. */
4417 i = 0;
4418 j = it->current.overlay_string_index;
4419 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4420 it->overlay_strings[i++] = entries[j++].string;
4421
4422 CHECK_IT (it);
4423 }
4424
4425
4426 /* Get the first chunk of overlay strings at IT's current buffer
4427 position, or at CHARPOS if that is > 0. Value is non-zero if at
4428 least one overlay string was found. */
4429
4430 static int
4431 get_overlay_strings (it, charpos)
4432 struct it *it;
4433 int charpos;
4434 {
4435 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4436 process. This fills IT->overlay_strings with strings, and sets
4437 IT->n_overlay_strings to the total number of strings to process.
4438 IT->pos.overlay_string_index has to be set temporarily to zero
4439 because load_overlay_strings needs this; it must be set to -1
4440 when no overlay strings are found because a zero value would
4441 indicate a position in the first overlay string. */
4442 it->current.overlay_string_index = 0;
4443 load_overlay_strings (it, charpos);
4444
4445 /* If we found overlay strings, set up IT to deliver display
4446 elements from the first one. Otherwise set up IT to deliver
4447 from current_buffer. */
4448 if (it->n_overlay_strings)
4449 {
4450 /* Make sure we know settings in current_buffer, so that we can
4451 restore meaningful values when we're done with the overlay
4452 strings. */
4453 compute_stop_pos (it);
4454 xassert (it->face_id >= 0);
4455
4456 /* Save IT's settings. They are restored after all overlay
4457 strings have been processed. */
4458 xassert (it->sp == 0);
4459 push_it (it);
4460
4461 /* Set up IT to deliver display elements from the first overlay
4462 string. */
4463 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4464 it->string = it->overlay_strings[0];
4465 it->stop_charpos = 0;
4466 xassert (STRINGP (it->string));
4467 it->end_charpos = SCHARS (it->string);
4468 it->multibyte_p = STRING_MULTIBYTE (it->string);
4469 it->method = next_element_from_string;
4470 }
4471 else
4472 {
4473 it->string = Qnil;
4474 it->current.overlay_string_index = -1;
4475 it->method = next_element_from_buffer;
4476 }
4477
4478 CHECK_IT (it);
4479
4480 /* Value is non-zero if we found at least one overlay string. */
4481 return STRINGP (it->string);
4482 }
4483
4484
4485 \f
4486 /***********************************************************************
4487 Saving and restoring state
4488 ***********************************************************************/
4489
4490 /* Save current settings of IT on IT->stack. Called, for example,
4491 before setting up IT for an overlay string, to be able to restore
4492 IT's settings to what they were after the overlay string has been
4493 processed. */
4494
4495 static void
4496 push_it (it)
4497 struct it *it;
4498 {
4499 struct iterator_stack_entry *p;
4500
4501 xassert (it->sp < 2);
4502 p = it->stack + it->sp;
4503
4504 p->stop_charpos = it->stop_charpos;
4505 xassert (it->face_id >= 0);
4506 p->face_id = it->face_id;
4507 p->string = it->string;
4508 p->pos = it->current;
4509 p->end_charpos = it->end_charpos;
4510 p->string_nchars = it->string_nchars;
4511 p->area = it->area;
4512 p->multibyte_p = it->multibyte_p;
4513 p->slice = it->slice;
4514 p->space_width = it->space_width;
4515 p->font_height = it->font_height;
4516 p->voffset = it->voffset;
4517 p->string_from_display_prop_p = it->string_from_display_prop_p;
4518 p->display_ellipsis_p = 0;
4519 ++it->sp;
4520 }
4521
4522
4523 /* Restore IT's settings from IT->stack. Called, for example, when no
4524 more overlay strings must be processed, and we return to delivering
4525 display elements from a buffer, or when the end of a string from a
4526 `display' property is reached and we return to delivering display
4527 elements from an overlay string, or from a buffer. */
4528
4529 static void
4530 pop_it (it)
4531 struct it *it;
4532 {
4533 struct iterator_stack_entry *p;
4534
4535 xassert (it->sp > 0);
4536 --it->sp;
4537 p = it->stack + it->sp;
4538 it->stop_charpos = p->stop_charpos;
4539 it->face_id = p->face_id;
4540 it->string = p->string;
4541 it->current = p->pos;
4542 it->end_charpos = p->end_charpos;
4543 it->string_nchars = p->string_nchars;
4544 it->area = p->area;
4545 it->multibyte_p = p->multibyte_p;
4546 it->slice = p->slice;
4547 it->space_width = p->space_width;
4548 it->font_height = p->font_height;
4549 it->voffset = p->voffset;
4550 it->string_from_display_prop_p = p->string_from_display_prop_p;
4551 }
4552
4553
4554 \f
4555 /***********************************************************************
4556 Moving over lines
4557 ***********************************************************************/
4558
4559 /* Set IT's current position to the previous line start. */
4560
4561 static void
4562 back_to_previous_line_start (it)
4563 struct it *it;
4564 {
4565 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4566 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4567 }
4568
4569
4570 /* Move IT to the next line start.
4571
4572 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4573 we skipped over part of the text (as opposed to moving the iterator
4574 continuously over the text). Otherwise, don't change the value
4575 of *SKIPPED_P.
4576
4577 Newlines may come from buffer text, overlay strings, or strings
4578 displayed via the `display' property. That's the reason we can't
4579 simply use find_next_newline_no_quit.
4580
4581 Note that this function may not skip over invisible text that is so
4582 because of text properties and immediately follows a newline. If
4583 it would, function reseat_at_next_visible_line_start, when called
4584 from set_iterator_to_next, would effectively make invisible
4585 characters following a newline part of the wrong glyph row, which
4586 leads to wrong cursor motion. */
4587
4588 static int
4589 forward_to_next_line_start (it, skipped_p)
4590 struct it *it;
4591 int *skipped_p;
4592 {
4593 int old_selective, newline_found_p, n;
4594 const int MAX_NEWLINE_DISTANCE = 500;
4595
4596 /* If already on a newline, just consume it to avoid unintended
4597 skipping over invisible text below. */
4598 if (it->what == IT_CHARACTER
4599 && it->c == '\n'
4600 && CHARPOS (it->position) == IT_CHARPOS (*it))
4601 {
4602 set_iterator_to_next (it, 0);
4603 it->c = 0;
4604 return 1;
4605 }
4606
4607 /* Don't handle selective display in the following. It's (a)
4608 unnecessary because it's done by the caller, and (b) leads to an
4609 infinite recursion because next_element_from_ellipsis indirectly
4610 calls this function. */
4611 old_selective = it->selective;
4612 it->selective = 0;
4613
4614 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4615 from buffer text. */
4616 for (n = newline_found_p = 0;
4617 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4618 n += STRINGP (it->string) ? 0 : 1)
4619 {
4620 if (!get_next_display_element (it))
4621 return 0;
4622 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4623 set_iterator_to_next (it, 0);
4624 }
4625
4626 /* If we didn't find a newline near enough, see if we can use a
4627 short-cut. */
4628 if (!newline_found_p)
4629 {
4630 int start = IT_CHARPOS (*it);
4631 int limit = find_next_newline_no_quit (start, 1);
4632 Lisp_Object pos;
4633
4634 xassert (!STRINGP (it->string));
4635
4636 /* If there isn't any `display' property in sight, and no
4637 overlays, we can just use the position of the newline in
4638 buffer text. */
4639 if (it->stop_charpos >= limit
4640 || ((pos = Fnext_single_property_change (make_number (start),
4641 Qdisplay,
4642 Qnil, make_number (limit)),
4643 NILP (pos))
4644 && next_overlay_change (start) == ZV))
4645 {
4646 IT_CHARPOS (*it) = limit;
4647 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4648 *skipped_p = newline_found_p = 1;
4649 }
4650 else
4651 {
4652 while (get_next_display_element (it)
4653 && !newline_found_p)
4654 {
4655 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4656 set_iterator_to_next (it, 0);
4657 }
4658 }
4659 }
4660
4661 it->selective = old_selective;
4662 return newline_found_p;
4663 }
4664
4665
4666 /* Set IT's current position to the previous visible line start. Skip
4667 invisible text that is so either due to text properties or due to
4668 selective display. Caution: this does not change IT->current_x and
4669 IT->hpos. */
4670
4671 static void
4672 back_to_previous_visible_line_start (it)
4673 struct it *it;
4674 {
4675 while (IT_CHARPOS (*it) > BEGV)
4676 {
4677 back_to_previous_line_start (it);
4678 if (IT_CHARPOS (*it) <= BEGV)
4679 break;
4680
4681 /* If selective > 0, then lines indented more than that values
4682 are invisible. */
4683 if (it->selective > 0
4684 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4685 (double) it->selective)) /* iftc */
4686 continue;
4687
4688 /* Check the newline before point for invisibility. */
4689 {
4690 Lisp_Object prop;
4691 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4692 Qinvisible, it->window);
4693 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4694 continue;
4695 }
4696
4697 /* If newline has a display property that replaces the newline with something
4698 else (image or text), find start of overlay or interval and continue search
4699 from that point. */
4700 {
4701 struct it it2 = *it;
4702 int pos = IT_CHARPOS (*it);
4703 int beg, end;
4704 Lisp_Object val, overlay;
4705
4706 it2.sp = 0;
4707 if (handle_display_prop (&it2) == HANDLED_RETURN
4708 && !NILP (val = get_char_property_and_overlay
4709 (make_number (pos), Qdisplay, Qnil, &overlay))
4710 && (OVERLAYP (overlay)
4711 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4712 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4713 {
4714 if (beg < BEGV)
4715 beg = BEGV;
4716 IT_CHARPOS (*it) = beg;
4717 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4718 continue;
4719 }
4720 }
4721 break;
4722 }
4723
4724 xassert (IT_CHARPOS (*it) >= BEGV);
4725 xassert (IT_CHARPOS (*it) == BEGV
4726 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4727 CHECK_IT (it);
4728 }
4729
4730
4731 /* Reseat iterator IT at the previous visible line start. Skip
4732 invisible text that is so either due to text properties or due to
4733 selective display. At the end, update IT's overlay information,
4734 face information etc. */
4735
4736 void
4737 reseat_at_previous_visible_line_start (it)
4738 struct it *it;
4739 {
4740 back_to_previous_visible_line_start (it);
4741 reseat (it, it->current.pos, 1);
4742 CHECK_IT (it);
4743 }
4744
4745
4746 /* Reseat iterator IT on the next visible line start in the current
4747 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4748 preceding the line start. Skip over invisible text that is so
4749 because of selective display. Compute faces, overlays etc at the
4750 new position. Note that this function does not skip over text that
4751 is invisible because of text properties. */
4752
4753 static void
4754 reseat_at_next_visible_line_start (it, on_newline_p)
4755 struct it *it;
4756 int on_newline_p;
4757 {
4758 int newline_found_p, skipped_p = 0;
4759
4760 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4761
4762 /* Skip over lines that are invisible because they are indented
4763 more than the value of IT->selective. */
4764 if (it->selective > 0)
4765 while (IT_CHARPOS (*it) < ZV
4766 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4767 (double) it->selective)) /* iftc */
4768 {
4769 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4770 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4771 }
4772
4773 /* Position on the newline if that's what's requested. */
4774 if (on_newline_p && newline_found_p)
4775 {
4776 if (STRINGP (it->string))
4777 {
4778 if (IT_STRING_CHARPOS (*it) > 0)
4779 {
4780 --IT_STRING_CHARPOS (*it);
4781 --IT_STRING_BYTEPOS (*it);
4782 }
4783 }
4784 else if (IT_CHARPOS (*it) > BEGV)
4785 {
4786 --IT_CHARPOS (*it);
4787 --IT_BYTEPOS (*it);
4788 reseat (it, it->current.pos, 0);
4789 }
4790 }
4791 else if (skipped_p)
4792 reseat (it, it->current.pos, 0);
4793
4794 CHECK_IT (it);
4795 }
4796
4797
4798 \f
4799 /***********************************************************************
4800 Changing an iterator's position
4801 ***********************************************************************/
4802
4803 /* Change IT's current position to POS in current_buffer. If FORCE_P
4804 is non-zero, always check for text properties at the new position.
4805 Otherwise, text properties are only looked up if POS >=
4806 IT->check_charpos of a property. */
4807
4808 static void
4809 reseat (it, pos, force_p)
4810 struct it *it;
4811 struct text_pos pos;
4812 int force_p;
4813 {
4814 int original_pos = IT_CHARPOS (*it);
4815
4816 reseat_1 (it, pos, 0);
4817
4818 /* Determine where to check text properties. Avoid doing it
4819 where possible because text property lookup is very expensive. */
4820 if (force_p
4821 || CHARPOS (pos) > it->stop_charpos
4822 || CHARPOS (pos) < original_pos)
4823 handle_stop (it);
4824
4825 CHECK_IT (it);
4826 }
4827
4828
4829 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4830 IT->stop_pos to POS, also. */
4831
4832 static void
4833 reseat_1 (it, pos, set_stop_p)
4834 struct it *it;
4835 struct text_pos pos;
4836 int set_stop_p;
4837 {
4838 /* Don't call this function when scanning a C string. */
4839 xassert (it->s == NULL);
4840
4841 /* POS must be a reasonable value. */
4842 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4843
4844 it->current.pos = it->position = pos;
4845 XSETBUFFER (it->object, current_buffer);
4846 it->end_charpos = ZV;
4847 it->dpvec = NULL;
4848 it->current.dpvec_index = -1;
4849 it->current.overlay_string_index = -1;
4850 IT_STRING_CHARPOS (*it) = -1;
4851 IT_STRING_BYTEPOS (*it) = -1;
4852 it->string = Qnil;
4853 it->method = next_element_from_buffer;
4854 /* RMS: I added this to fix a bug in move_it_vertically_backward
4855 where it->area continued to relate to the starting point
4856 for the backward motion. Bug report from
4857 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4858 However, I am not sure whether reseat still does the right thing
4859 in general after this change. */
4860 it->area = TEXT_AREA;
4861 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4862 it->sp = 0;
4863 it->face_before_selective_p = 0;
4864
4865 if (set_stop_p)
4866 it->stop_charpos = CHARPOS (pos);
4867 }
4868
4869
4870 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4871 If S is non-null, it is a C string to iterate over. Otherwise,
4872 STRING gives a Lisp string to iterate over.
4873
4874 If PRECISION > 0, don't return more then PRECISION number of
4875 characters from the string.
4876
4877 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4878 characters have been returned. FIELD_WIDTH < 0 means an infinite
4879 field width.
4880
4881 MULTIBYTE = 0 means disable processing of multibyte characters,
4882 MULTIBYTE > 0 means enable it,
4883 MULTIBYTE < 0 means use IT->multibyte_p.
4884
4885 IT must be initialized via a prior call to init_iterator before
4886 calling this function. */
4887
4888 static void
4889 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4890 struct it *it;
4891 unsigned char *s;
4892 Lisp_Object string;
4893 int charpos;
4894 int precision, field_width, multibyte;
4895 {
4896 /* No region in strings. */
4897 it->region_beg_charpos = it->region_end_charpos = -1;
4898
4899 /* No text property checks performed by default, but see below. */
4900 it->stop_charpos = -1;
4901
4902 /* Set iterator position and end position. */
4903 bzero (&it->current, sizeof it->current);
4904 it->current.overlay_string_index = -1;
4905 it->current.dpvec_index = -1;
4906 xassert (charpos >= 0);
4907
4908 /* If STRING is specified, use its multibyteness, otherwise use the
4909 setting of MULTIBYTE, if specified. */
4910 if (multibyte >= 0)
4911 it->multibyte_p = multibyte > 0;
4912
4913 if (s == NULL)
4914 {
4915 xassert (STRINGP (string));
4916 it->string = string;
4917 it->s = NULL;
4918 it->end_charpos = it->string_nchars = SCHARS (string);
4919 it->method = next_element_from_string;
4920 it->current.string_pos = string_pos (charpos, string);
4921 }
4922 else
4923 {
4924 it->s = s;
4925 it->string = Qnil;
4926
4927 /* Note that we use IT->current.pos, not it->current.string_pos,
4928 for displaying C strings. */
4929 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4930 if (it->multibyte_p)
4931 {
4932 it->current.pos = c_string_pos (charpos, s, 1);
4933 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4934 }
4935 else
4936 {
4937 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4938 it->end_charpos = it->string_nchars = strlen (s);
4939 }
4940
4941 it->method = next_element_from_c_string;
4942 }
4943
4944 /* PRECISION > 0 means don't return more than PRECISION characters
4945 from the string. */
4946 if (precision > 0 && it->end_charpos - charpos > precision)
4947 it->end_charpos = it->string_nchars = charpos + precision;
4948
4949 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4950 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4951 FIELD_WIDTH < 0 means infinite field width. This is useful for
4952 padding with `-' at the end of a mode line. */
4953 if (field_width < 0)
4954 field_width = INFINITY;
4955 if (field_width > it->end_charpos - charpos)
4956 it->end_charpos = charpos + field_width;
4957
4958 /* Use the standard display table for displaying strings. */
4959 if (DISP_TABLE_P (Vstandard_display_table))
4960 it->dp = XCHAR_TABLE (Vstandard_display_table);
4961
4962 it->stop_charpos = charpos;
4963 CHECK_IT (it);
4964 }
4965
4966
4967 \f
4968 /***********************************************************************
4969 Iteration
4970 ***********************************************************************/
4971
4972 /* Load IT's display element fields with information about the next
4973 display element from the current position of IT. Value is zero if
4974 end of buffer (or C string) is reached. */
4975
4976 int
4977 get_next_display_element (it)
4978 struct it *it;
4979 {
4980 /* Non-zero means that we found a display element. Zero means that
4981 we hit the end of what we iterate over. Performance note: the
4982 function pointer `method' used here turns out to be faster than
4983 using a sequence of if-statements. */
4984 int success_p;
4985
4986 get_next:
4987 success_p = (*it->method) (it);
4988
4989 if (it->what == IT_CHARACTER)
4990 {
4991 /* Map via display table or translate control characters.
4992 IT->c, IT->len etc. have been set to the next character by
4993 the function call above. If we have a display table, and it
4994 contains an entry for IT->c, translate it. Don't do this if
4995 IT->c itself comes from a display table, otherwise we could
4996 end up in an infinite recursion. (An alternative could be to
4997 count the recursion depth of this function and signal an
4998 error when a certain maximum depth is reached.) Is it worth
4999 it? */
5000 if (success_p && it->dpvec == NULL)
5001 {
5002 Lisp_Object dv;
5003
5004 if (it->dp
5005 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5006 VECTORP (dv)))
5007 {
5008 struct Lisp_Vector *v = XVECTOR (dv);
5009
5010 /* Return the first character from the display table
5011 entry, if not empty. If empty, don't display the
5012 current character. */
5013 if (v->size)
5014 {
5015 it->dpvec_char_len = it->len;
5016 it->dpvec = v->contents;
5017 it->dpend = v->contents + v->size;
5018 it->current.dpvec_index = 0;
5019 it->dpvec_face_id = -1;
5020 it->saved_face_id = it->face_id;
5021 it->method = next_element_from_display_vector;
5022 it->ellipsis_p = 0;
5023 }
5024 else
5025 {
5026 set_iterator_to_next (it, 0);
5027 }
5028 goto get_next;
5029 }
5030
5031 /* Translate control characters into `\003' or `^C' form.
5032 Control characters coming from a display table entry are
5033 currently not translated because we use IT->dpvec to hold
5034 the translation. This could easily be changed but I
5035 don't believe that it is worth doing.
5036
5037 If it->multibyte_p is nonzero, eight-bit characters and
5038 non-printable multibyte characters are also translated to
5039 octal form.
5040
5041 If it->multibyte_p is zero, eight-bit characters that
5042 don't have corresponding multibyte char code are also
5043 translated to octal form. */
5044 else if ((it->c < ' '
5045 && (it->area != TEXT_AREA
5046 /* In mode line, treat \n like other crl chars. */
5047 || (it->c != '\n'
5048 && it->glyph_row && it->glyph_row->mode_line_p)
5049 || (it->c != '\n' && it->c != '\t')))
5050 || (it->multibyte_p
5051 ? ((it->c >= 127
5052 && it->len == 1)
5053 || !CHAR_PRINTABLE_P (it->c)
5054 || (!NILP (Vshow_nonbreak_escape)
5055 && (it->c == 0x8ad || it->c == 0x8a0)))
5056 : (it->c >= 127
5057 && (!unibyte_display_via_language_environment
5058 || it->c == unibyte_char_to_multibyte (it->c)))))
5059 {
5060 /* IT->c is a control character which must be displayed
5061 either as '\003' or as `^C' where the '\\' and '^'
5062 can be defined in the display table. Fill
5063 IT->ctl_chars with glyphs for what we have to
5064 display. Then, set IT->dpvec to these glyphs. */
5065 GLYPH g;
5066 int ctl_len;
5067 int face_id, lface_id;
5068 GLYPH escape_glyph;
5069
5070 if (it->c < 128 && it->ctl_arrow_p)
5071 {
5072 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5073 if (it->dp
5074 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5075 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5076 {
5077 g = XINT (DISP_CTRL_GLYPH (it->dp));
5078 lface_id = FAST_GLYPH_FACE (g);
5079 if (lface_id)
5080 {
5081 g = FAST_GLYPH_CHAR (g);
5082 face_id = merge_faces (it->f, Qt, lface_id,
5083 it->face_id);
5084 }
5085 }
5086 else
5087 {
5088 /* Merge the escape-glyph face into the current face. */
5089 face_id = merge_faces (it->f, Qescape_glyph, 0,
5090 it->face_id);
5091 g = '^';
5092 }
5093
5094 XSETINT (it->ctl_chars[0], g);
5095 g = it->c ^ 0100;
5096 XSETINT (it->ctl_chars[1], g);
5097 ctl_len = 2;
5098 goto display_control;
5099 }
5100
5101 if (it->dp
5102 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5103 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5104 {
5105 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5106 lface_id = FAST_GLYPH_FACE (escape_glyph);
5107 if (lface_id)
5108 {
5109 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5110 face_id = merge_faces (it->f, Qt, lface_id,
5111 it->face_id);
5112 }
5113 }
5114 else
5115 {
5116 /* Merge the escape-glyph face into the current face. */
5117 face_id = merge_faces (it->f, Qescape_glyph, 0,
5118 it->face_id);
5119 escape_glyph = '\\';
5120 }
5121
5122 if (it->c == 0x8a0 || it->c == 0x8ad)
5123 {
5124 XSETINT (it->ctl_chars[0], escape_glyph);
5125 g = it->c == 0x8ad ? '-' : ' ';
5126 XSETINT (it->ctl_chars[1], g);
5127 ctl_len = 2;
5128 goto display_control;
5129 }
5130
5131 {
5132 unsigned char str[MAX_MULTIBYTE_LENGTH];
5133 int len;
5134 int i;
5135
5136 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5137 if (SINGLE_BYTE_CHAR_P (it->c))
5138 str[0] = it->c, len = 1;
5139 else
5140 {
5141 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5142 if (len < 0)
5143 {
5144 /* It's an invalid character, which shouldn't
5145 happen actually, but due to bugs it may
5146 happen. Let's print the char as is, there's
5147 not much meaningful we can do with it. */
5148 str[0] = it->c;
5149 str[1] = it->c >> 8;
5150 str[2] = it->c >> 16;
5151 str[3] = it->c >> 24;
5152 len = 4;
5153 }
5154 }
5155
5156 for (i = 0; i < len; i++)
5157 {
5158 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5159 /* Insert three more glyphs into IT->ctl_chars for
5160 the octal display of the character. */
5161 g = ((str[i] >> 6) & 7) + '0';
5162 XSETINT (it->ctl_chars[i * 4 + 1], g);
5163 g = ((str[i] >> 3) & 7) + '0';
5164 XSETINT (it->ctl_chars[i * 4 + 2], g);
5165 g = (str[i] & 7) + '0';
5166 XSETINT (it->ctl_chars[i * 4 + 3], g);
5167 }
5168 ctl_len = len * 4;
5169 }
5170
5171 display_control:
5172 /* Set up IT->dpvec and return first character from it. */
5173 it->dpvec_char_len = it->len;
5174 it->dpvec = it->ctl_chars;
5175 it->dpend = it->dpvec + ctl_len;
5176 it->current.dpvec_index = 0;
5177 it->dpvec_face_id = face_id;
5178 it->saved_face_id = it->face_id;
5179 it->method = next_element_from_display_vector;
5180 it->ellipsis_p = 0;
5181 goto get_next;
5182 }
5183 }
5184
5185 /* Adjust face id for a multibyte character. There are no
5186 multibyte character in unibyte text. */
5187 if (it->multibyte_p
5188 && success_p
5189 && FRAME_WINDOW_P (it->f))
5190 {
5191 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5192 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5193 }
5194 }
5195
5196 /* Is this character the last one of a run of characters with
5197 box? If yes, set IT->end_of_box_run_p to 1. */
5198 if (it->face_box_p
5199 && it->s == NULL)
5200 {
5201 int face_id;
5202 struct face *face;
5203
5204 it->end_of_box_run_p
5205 = ((face_id = face_after_it_pos (it),
5206 face_id != it->face_id)
5207 && (face = FACE_FROM_ID (it->f, face_id),
5208 face->box == FACE_NO_BOX));
5209 }
5210
5211 /* Value is 0 if end of buffer or string reached. */
5212 return success_p;
5213 }
5214
5215
5216 /* Move IT to the next display element.
5217
5218 RESEAT_P non-zero means if called on a newline in buffer text,
5219 skip to the next visible line start.
5220
5221 Functions get_next_display_element and set_iterator_to_next are
5222 separate because I find this arrangement easier to handle than a
5223 get_next_display_element function that also increments IT's
5224 position. The way it is we can first look at an iterator's current
5225 display element, decide whether it fits on a line, and if it does,
5226 increment the iterator position. The other way around we probably
5227 would either need a flag indicating whether the iterator has to be
5228 incremented the next time, or we would have to implement a
5229 decrement position function which would not be easy to write. */
5230
5231 void
5232 set_iterator_to_next (it, reseat_p)
5233 struct it *it;
5234 int reseat_p;
5235 {
5236 /* Reset flags indicating start and end of a sequence of characters
5237 with box. Reset them at the start of this function because
5238 moving the iterator to a new position might set them. */
5239 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5240
5241 if (it->method == next_element_from_buffer)
5242 {
5243 /* The current display element of IT is a character from
5244 current_buffer. Advance in the buffer, and maybe skip over
5245 invisible lines that are so because of selective display. */
5246 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5247 reseat_at_next_visible_line_start (it, 0);
5248 else
5249 {
5250 xassert (it->len != 0);
5251 IT_BYTEPOS (*it) += it->len;
5252 IT_CHARPOS (*it) += 1;
5253 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5254 }
5255 }
5256 else if (it->method == next_element_from_composition)
5257 {
5258 xassert (it->cmp_id >= 0 && it ->cmp_id < n_compositions);
5259 if (STRINGP (it->string))
5260 {
5261 IT_STRING_BYTEPOS (*it) += it->len;
5262 IT_STRING_CHARPOS (*it) += it->cmp_len;
5263 it->method = next_element_from_string;
5264 goto consider_string_end;
5265 }
5266 else
5267 {
5268 IT_BYTEPOS (*it) += it->len;
5269 IT_CHARPOS (*it) += it->cmp_len;
5270 it->method = next_element_from_buffer;
5271 }
5272 }
5273 else if (it->method == next_element_from_c_string)
5274 {
5275 /* Current display element of IT is from a C string. */
5276 IT_BYTEPOS (*it) += it->len;
5277 IT_CHARPOS (*it) += 1;
5278 }
5279 else if (it->method == next_element_from_display_vector)
5280 {
5281 /* Current display element of IT is from a display table entry.
5282 Advance in the display table definition. Reset it to null if
5283 end reached, and continue with characters from buffers/
5284 strings. */
5285 ++it->current.dpvec_index;
5286
5287 /* Restore face of the iterator to what they were before the
5288 display vector entry (these entries may contain faces). */
5289 it->face_id = it->saved_face_id;
5290
5291 if (it->dpvec + it->current.dpvec_index == it->dpend)
5292 {
5293 if (it->s)
5294 it->method = next_element_from_c_string;
5295 else if (STRINGP (it->string))
5296 it->method = next_element_from_string;
5297 else
5298 it->method = next_element_from_buffer;
5299
5300 it->dpvec = NULL;
5301 it->current.dpvec_index = -1;
5302
5303 /* Skip over characters which were displayed via IT->dpvec. */
5304 if (it->dpvec_char_len < 0)
5305 reseat_at_next_visible_line_start (it, 1);
5306 else if (it->dpvec_char_len > 0)
5307 {
5308 it->len = it->dpvec_char_len;
5309 set_iterator_to_next (it, reseat_p);
5310 }
5311
5312 /* Recheck faces after display vector */
5313 it->stop_charpos = IT_CHARPOS (*it);
5314 }
5315 }
5316 else if (it->method == next_element_from_string)
5317 {
5318 /* Current display element is a character from a Lisp string. */
5319 xassert (it->s == NULL && STRINGP (it->string));
5320 IT_STRING_BYTEPOS (*it) += it->len;
5321 IT_STRING_CHARPOS (*it) += 1;
5322
5323 consider_string_end:
5324
5325 if (it->current.overlay_string_index >= 0)
5326 {
5327 /* IT->string is an overlay string. Advance to the
5328 next, if there is one. */
5329 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5330 next_overlay_string (it);
5331 }
5332 else
5333 {
5334 /* IT->string is not an overlay string. If we reached
5335 its end, and there is something on IT->stack, proceed
5336 with what is on the stack. This can be either another
5337 string, this time an overlay string, or a buffer. */
5338 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5339 && it->sp > 0)
5340 {
5341 pop_it (it);
5342 if (!STRINGP (it->string))
5343 it->method = next_element_from_buffer;
5344 else
5345 goto consider_string_end;
5346 }
5347 }
5348 }
5349 else if (it->method == next_element_from_image
5350 || it->method == next_element_from_stretch)
5351 {
5352 /* The position etc with which we have to proceed are on
5353 the stack. The position may be at the end of a string,
5354 if the `display' property takes up the whole string. */
5355 pop_it (it);
5356 it->image_id = 0;
5357 if (STRINGP (it->string))
5358 {
5359 it->method = next_element_from_string;
5360 goto consider_string_end;
5361 }
5362 else
5363 it->method = next_element_from_buffer;
5364 }
5365 else
5366 /* There are no other methods defined, so this should be a bug. */
5367 abort ();
5368
5369 xassert (it->method != next_element_from_string
5370 || (STRINGP (it->string)
5371 && IT_STRING_CHARPOS (*it) >= 0));
5372 }
5373
5374 /* Load IT's display element fields with information about the next
5375 display element which comes from a display table entry or from the
5376 result of translating a control character to one of the forms `^C'
5377 or `\003'.
5378
5379 IT->dpvec holds the glyphs to return as characters.
5380 IT->saved_face_id holds the face id before the display vector--
5381 it is restored into IT->face_idin set_iterator_to_next. */
5382
5383 static int
5384 next_element_from_display_vector (it)
5385 struct it *it;
5386 {
5387 /* Precondition. */
5388 xassert (it->dpvec && it->current.dpvec_index >= 0);
5389
5390 if (INTEGERP (*it->dpvec)
5391 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5392 {
5393 GLYPH g;
5394
5395 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5396 it->c = FAST_GLYPH_CHAR (g);
5397 it->len = CHAR_BYTES (it->c);
5398
5399 /* The entry may contain a face id to use. Such a face id is
5400 the id of a Lisp face, not a realized face. A face id of
5401 zero means no face is specified. */
5402 if (it->dpvec_face_id >= 0)
5403 it->face_id = it->dpvec_face_id;
5404 else
5405 {
5406 int lface_id = FAST_GLYPH_FACE (g);
5407 if (lface_id > 0)
5408 it->face_id = merge_faces (it->f, Qt, lface_id,
5409 it->saved_face_id);
5410 }
5411 }
5412 else
5413 /* Display table entry is invalid. Return a space. */
5414 it->c = ' ', it->len = 1;
5415
5416 /* Don't change position and object of the iterator here. They are
5417 still the values of the character that had this display table
5418 entry or was translated, and that's what we want. */
5419 it->what = IT_CHARACTER;
5420 return 1;
5421 }
5422
5423
5424 /* Load IT with the next display element from Lisp string IT->string.
5425 IT->current.string_pos is the current position within the string.
5426 If IT->current.overlay_string_index >= 0, the Lisp string is an
5427 overlay string. */
5428
5429 static int
5430 next_element_from_string (it)
5431 struct it *it;
5432 {
5433 struct text_pos position;
5434
5435 xassert (STRINGP (it->string));
5436 xassert (IT_STRING_CHARPOS (*it) >= 0);
5437 position = it->current.string_pos;
5438
5439 /* Time to check for invisible text? */
5440 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5441 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5442 {
5443 handle_stop (it);
5444
5445 /* Since a handler may have changed IT->method, we must
5446 recurse here. */
5447 return get_next_display_element (it);
5448 }
5449
5450 if (it->current.overlay_string_index >= 0)
5451 {
5452 /* Get the next character from an overlay string. In overlay
5453 strings, There is no field width or padding with spaces to
5454 do. */
5455 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5456 {
5457 it->what = IT_EOB;
5458 return 0;
5459 }
5460 else if (STRING_MULTIBYTE (it->string))
5461 {
5462 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5463 const unsigned char *s = (SDATA (it->string)
5464 + IT_STRING_BYTEPOS (*it));
5465 it->c = string_char_and_length (s, remaining, &it->len);
5466 }
5467 else
5468 {
5469 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5470 it->len = 1;
5471 }
5472 }
5473 else
5474 {
5475 /* Get the next character from a Lisp string that is not an
5476 overlay string. Such strings come from the mode line, for
5477 example. We may have to pad with spaces, or truncate the
5478 string. See also next_element_from_c_string. */
5479 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5480 {
5481 it->what = IT_EOB;
5482 return 0;
5483 }
5484 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5485 {
5486 /* Pad with spaces. */
5487 it->c = ' ', it->len = 1;
5488 CHARPOS (position) = BYTEPOS (position) = -1;
5489 }
5490 else if (STRING_MULTIBYTE (it->string))
5491 {
5492 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5493 const unsigned char *s = (SDATA (it->string)
5494 + IT_STRING_BYTEPOS (*it));
5495 it->c = string_char_and_length (s, maxlen, &it->len);
5496 }
5497 else
5498 {
5499 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5500 it->len = 1;
5501 }
5502 }
5503
5504 /* Record what we have and where it came from. Note that we store a
5505 buffer position in IT->position although it could arguably be a
5506 string position. */
5507 it->what = IT_CHARACTER;
5508 it->object = it->string;
5509 it->position = position;
5510 return 1;
5511 }
5512
5513
5514 /* Load IT with next display element from C string IT->s.
5515 IT->string_nchars is the maximum number of characters to return
5516 from the string. IT->end_charpos may be greater than
5517 IT->string_nchars when this function is called, in which case we
5518 may have to return padding spaces. Value is zero if end of string
5519 reached, including padding spaces. */
5520
5521 static int
5522 next_element_from_c_string (it)
5523 struct it *it;
5524 {
5525 int success_p = 1;
5526
5527 xassert (it->s);
5528 it->what = IT_CHARACTER;
5529 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5530 it->object = Qnil;
5531
5532 /* IT's position can be greater IT->string_nchars in case a field
5533 width or precision has been specified when the iterator was
5534 initialized. */
5535 if (IT_CHARPOS (*it) >= it->end_charpos)
5536 {
5537 /* End of the game. */
5538 it->what = IT_EOB;
5539 success_p = 0;
5540 }
5541 else if (IT_CHARPOS (*it) >= it->string_nchars)
5542 {
5543 /* Pad with spaces. */
5544 it->c = ' ', it->len = 1;
5545 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5546 }
5547 else if (it->multibyte_p)
5548 {
5549 /* Implementation note: The calls to strlen apparently aren't a
5550 performance problem because there is no noticeable performance
5551 difference between Emacs running in unibyte or multibyte mode. */
5552 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5553 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5554 maxlen, &it->len);
5555 }
5556 else
5557 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5558
5559 return success_p;
5560 }
5561
5562
5563 /* Set up IT to return characters from an ellipsis, if appropriate.
5564 The definition of the ellipsis glyphs may come from a display table
5565 entry. This function Fills IT with the first glyph from the
5566 ellipsis if an ellipsis is to be displayed. */
5567
5568 static int
5569 next_element_from_ellipsis (it)
5570 struct it *it;
5571 {
5572 if (it->selective_display_ellipsis_p)
5573 setup_for_ellipsis (it, it->len);
5574 else
5575 {
5576 /* The face at the current position may be different from the
5577 face we find after the invisible text. Remember what it
5578 was in IT->saved_face_id, and signal that it's there by
5579 setting face_before_selective_p. */
5580 it->saved_face_id = it->face_id;
5581 it->method = next_element_from_buffer;
5582 reseat_at_next_visible_line_start (it, 1);
5583 it->face_before_selective_p = 1;
5584 }
5585
5586 return get_next_display_element (it);
5587 }
5588
5589
5590 /* Deliver an image display element. The iterator IT is already
5591 filled with image information (done in handle_display_prop). Value
5592 is always 1. */
5593
5594
5595 static int
5596 next_element_from_image (it)
5597 struct it *it;
5598 {
5599 it->what = IT_IMAGE;
5600 return 1;
5601 }
5602
5603
5604 /* Fill iterator IT with next display element from a stretch glyph
5605 property. IT->object is the value of the text property. Value is
5606 always 1. */
5607
5608 static int
5609 next_element_from_stretch (it)
5610 struct it *it;
5611 {
5612 it->what = IT_STRETCH;
5613 return 1;
5614 }
5615
5616
5617 /* Load IT with the next display element from current_buffer. Value
5618 is zero if end of buffer reached. IT->stop_charpos is the next
5619 position at which to stop and check for text properties or buffer
5620 end. */
5621
5622 static int
5623 next_element_from_buffer (it)
5624 struct it *it;
5625 {
5626 int success_p = 1;
5627
5628 /* Check this assumption, otherwise, we would never enter the
5629 if-statement, below. */
5630 xassert (IT_CHARPOS (*it) >= BEGV
5631 && IT_CHARPOS (*it) <= it->stop_charpos);
5632
5633 if (IT_CHARPOS (*it) >= it->stop_charpos)
5634 {
5635 if (IT_CHARPOS (*it) >= it->end_charpos)
5636 {
5637 int overlay_strings_follow_p;
5638
5639 /* End of the game, except when overlay strings follow that
5640 haven't been returned yet. */
5641 if (it->overlay_strings_at_end_processed_p)
5642 overlay_strings_follow_p = 0;
5643 else
5644 {
5645 it->overlay_strings_at_end_processed_p = 1;
5646 overlay_strings_follow_p = get_overlay_strings (it, 0);
5647 }
5648
5649 if (overlay_strings_follow_p)
5650 success_p = get_next_display_element (it);
5651 else
5652 {
5653 it->what = IT_EOB;
5654 it->position = it->current.pos;
5655 success_p = 0;
5656 }
5657 }
5658 else
5659 {
5660 handle_stop (it);
5661 return get_next_display_element (it);
5662 }
5663 }
5664 else
5665 {
5666 /* No face changes, overlays etc. in sight, so just return a
5667 character from current_buffer. */
5668 unsigned char *p;
5669
5670 /* Maybe run the redisplay end trigger hook. Performance note:
5671 This doesn't seem to cost measurable time. */
5672 if (it->redisplay_end_trigger_charpos
5673 && it->glyph_row
5674 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5675 run_redisplay_end_trigger_hook (it);
5676
5677 /* Get the next character, maybe multibyte. */
5678 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5679 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5680 {
5681 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5682 - IT_BYTEPOS (*it));
5683 it->c = string_char_and_length (p, maxlen, &it->len);
5684 }
5685 else
5686 it->c = *p, it->len = 1;
5687
5688 /* Record what we have and where it came from. */
5689 it->what = IT_CHARACTER;;
5690 it->object = it->w->buffer;
5691 it->position = it->current.pos;
5692
5693 /* Normally we return the character found above, except when we
5694 really want to return an ellipsis for selective display. */
5695 if (it->selective)
5696 {
5697 if (it->c == '\n')
5698 {
5699 /* A value of selective > 0 means hide lines indented more
5700 than that number of columns. */
5701 if (it->selective > 0
5702 && IT_CHARPOS (*it) + 1 < ZV
5703 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5704 IT_BYTEPOS (*it) + 1,
5705 (double) it->selective)) /* iftc */
5706 {
5707 success_p = next_element_from_ellipsis (it);
5708 it->dpvec_char_len = -1;
5709 }
5710 }
5711 else if (it->c == '\r' && it->selective == -1)
5712 {
5713 /* A value of selective == -1 means that everything from the
5714 CR to the end of the line is invisible, with maybe an
5715 ellipsis displayed for it. */
5716 success_p = next_element_from_ellipsis (it);
5717 it->dpvec_char_len = -1;
5718 }
5719 }
5720 }
5721
5722 /* Value is zero if end of buffer reached. */
5723 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5724 return success_p;
5725 }
5726
5727
5728 /* Run the redisplay end trigger hook for IT. */
5729
5730 static void
5731 run_redisplay_end_trigger_hook (it)
5732 struct it *it;
5733 {
5734 Lisp_Object args[3];
5735
5736 /* IT->glyph_row should be non-null, i.e. we should be actually
5737 displaying something, or otherwise we should not run the hook. */
5738 xassert (it->glyph_row);
5739
5740 /* Set up hook arguments. */
5741 args[0] = Qredisplay_end_trigger_functions;
5742 args[1] = it->window;
5743 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5744 it->redisplay_end_trigger_charpos = 0;
5745
5746 /* Since we are *trying* to run these functions, don't try to run
5747 them again, even if they get an error. */
5748 it->w->redisplay_end_trigger = Qnil;
5749 Frun_hook_with_args (3, args);
5750
5751 /* Notice if it changed the face of the character we are on. */
5752 handle_face_prop (it);
5753 }
5754
5755
5756 /* Deliver a composition display element. The iterator IT is already
5757 filled with composition information (done in
5758 handle_composition_prop). Value is always 1. */
5759
5760 static int
5761 next_element_from_composition (it)
5762 struct it *it;
5763 {
5764 it->what = IT_COMPOSITION;
5765 it->position = (STRINGP (it->string)
5766 ? it->current.string_pos
5767 : it->current.pos);
5768 return 1;
5769 }
5770
5771
5772 \f
5773 /***********************************************************************
5774 Moving an iterator without producing glyphs
5775 ***********************************************************************/
5776
5777 /* Move iterator IT to a specified buffer or X position within one
5778 line on the display without producing glyphs.
5779
5780 OP should be a bit mask including some or all of these bits:
5781 MOVE_TO_X: Stop on reaching x-position TO_X.
5782 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5783 Regardless of OP's value, stop in reaching the end of the display line.
5784
5785 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5786 This means, in particular, that TO_X includes window's horizontal
5787 scroll amount.
5788
5789 The return value has several possible values that
5790 say what condition caused the scan to stop:
5791
5792 MOVE_POS_MATCH_OR_ZV
5793 - when TO_POS or ZV was reached.
5794
5795 MOVE_X_REACHED
5796 -when TO_X was reached before TO_POS or ZV were reached.
5797
5798 MOVE_LINE_CONTINUED
5799 - when we reached the end of the display area and the line must
5800 be continued.
5801
5802 MOVE_LINE_TRUNCATED
5803 - when we reached the end of the display area and the line is
5804 truncated.
5805
5806 MOVE_NEWLINE_OR_CR
5807 - when we stopped at a line end, i.e. a newline or a CR and selective
5808 display is on. */
5809
5810 static enum move_it_result
5811 move_it_in_display_line_to (it, to_charpos, to_x, op)
5812 struct it *it;
5813 int to_charpos, to_x, op;
5814 {
5815 enum move_it_result result = MOVE_UNDEFINED;
5816 struct glyph_row *saved_glyph_row;
5817
5818 /* Don't produce glyphs in produce_glyphs. */
5819 saved_glyph_row = it->glyph_row;
5820 it->glyph_row = NULL;
5821
5822 #define BUFFER_POS_REACHED_P() \
5823 ((op & MOVE_TO_POS) != 0 \
5824 && BUFFERP (it->object) \
5825 && IT_CHARPOS (*it) >= to_charpos \
5826 && it->method == next_element_from_buffer)
5827
5828 while (1)
5829 {
5830 int x, i, ascent = 0, descent = 0;
5831
5832 /* Stop when ZV reached.
5833 We used to stop here when TO_CHARPOS reached as well, but that is
5834 too soon if this glyph does not fit on this line. So we handle it
5835 explicitly below. */
5836 if (!get_next_display_element (it)
5837 || (it->truncate_lines_p
5838 && BUFFER_POS_REACHED_P ()))
5839 {
5840 result = MOVE_POS_MATCH_OR_ZV;
5841 break;
5842 }
5843
5844 /* The call to produce_glyphs will get the metrics of the
5845 display element IT is loaded with. We record in x the
5846 x-position before this display element in case it does not
5847 fit on the line. */
5848 x = it->current_x;
5849
5850 /* Remember the line height so far in case the next element doesn't
5851 fit on the line. */
5852 if (!it->truncate_lines_p)
5853 {
5854 ascent = it->max_ascent;
5855 descent = it->max_descent;
5856 }
5857
5858 PRODUCE_GLYPHS (it);
5859
5860 if (it->area != TEXT_AREA)
5861 {
5862 set_iterator_to_next (it, 1);
5863 continue;
5864 }
5865
5866 /* The number of glyphs we get back in IT->nglyphs will normally
5867 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5868 character on a terminal frame, or (iii) a line end. For the
5869 second case, IT->nglyphs - 1 padding glyphs will be present
5870 (on X frames, there is only one glyph produced for a
5871 composite character.
5872
5873 The behavior implemented below means, for continuation lines,
5874 that as many spaces of a TAB as fit on the current line are
5875 displayed there. For terminal frames, as many glyphs of a
5876 multi-glyph character are displayed in the current line, too.
5877 This is what the old redisplay code did, and we keep it that
5878 way. Under X, the whole shape of a complex character must
5879 fit on the line or it will be completely displayed in the
5880 next line.
5881
5882 Note that both for tabs and padding glyphs, all glyphs have
5883 the same width. */
5884 if (it->nglyphs)
5885 {
5886 /* More than one glyph or glyph doesn't fit on line. All
5887 glyphs have the same width. */
5888 int single_glyph_width = it->pixel_width / it->nglyphs;
5889 int new_x;
5890
5891 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5892 {
5893 new_x = x + single_glyph_width;
5894
5895 /* We want to leave anything reaching TO_X to the caller. */
5896 if ((op & MOVE_TO_X) && new_x > to_x)
5897 {
5898 if (BUFFER_POS_REACHED_P ())
5899 goto buffer_pos_reached;
5900 it->current_x = x;
5901 result = MOVE_X_REACHED;
5902 break;
5903 }
5904 else if (/* Lines are continued. */
5905 !it->truncate_lines_p
5906 && (/* And glyph doesn't fit on the line. */
5907 new_x > it->last_visible_x
5908 /* Or it fits exactly and we're on a window
5909 system frame. */
5910 || (new_x == it->last_visible_x
5911 && FRAME_WINDOW_P (it->f))))
5912 {
5913 if (/* IT->hpos == 0 means the very first glyph
5914 doesn't fit on the line, e.g. a wide image. */
5915 it->hpos == 0
5916 || (new_x == it->last_visible_x
5917 && FRAME_WINDOW_P (it->f)))
5918 {
5919 ++it->hpos;
5920 it->current_x = new_x;
5921 if (i == it->nglyphs - 1)
5922 {
5923 set_iterator_to_next (it, 1);
5924 #ifdef HAVE_WINDOW_SYSTEM
5925 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5926 {
5927 if (!get_next_display_element (it))
5928 {
5929 result = MOVE_POS_MATCH_OR_ZV;
5930 break;
5931 }
5932 if (BUFFER_POS_REACHED_P ())
5933 {
5934 if (ITERATOR_AT_END_OF_LINE_P (it))
5935 result = MOVE_POS_MATCH_OR_ZV;
5936 else
5937 result = MOVE_LINE_CONTINUED;
5938 break;
5939 }
5940 if (ITERATOR_AT_END_OF_LINE_P (it))
5941 {
5942 result = MOVE_NEWLINE_OR_CR;
5943 break;
5944 }
5945 }
5946 #endif /* HAVE_WINDOW_SYSTEM */
5947 }
5948 }
5949 else
5950 {
5951 it->current_x = x;
5952 it->max_ascent = ascent;
5953 it->max_descent = descent;
5954 }
5955
5956 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5957 IT_CHARPOS (*it)));
5958 result = MOVE_LINE_CONTINUED;
5959 break;
5960 }
5961 else if (BUFFER_POS_REACHED_P ())
5962 goto buffer_pos_reached;
5963 else if (new_x > it->first_visible_x)
5964 {
5965 /* Glyph is visible. Increment number of glyphs that
5966 would be displayed. */
5967 ++it->hpos;
5968 }
5969 else
5970 {
5971 /* Glyph is completely off the left margin of the display
5972 area. Nothing to do. */
5973 }
5974 }
5975
5976 if (result != MOVE_UNDEFINED)
5977 break;
5978 }
5979 else if (BUFFER_POS_REACHED_P ())
5980 {
5981 buffer_pos_reached:
5982 it->current_x = x;
5983 it->max_ascent = ascent;
5984 it->max_descent = descent;
5985 result = MOVE_POS_MATCH_OR_ZV;
5986 break;
5987 }
5988 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
5989 {
5990 /* Stop when TO_X specified and reached. This check is
5991 necessary here because of lines consisting of a line end,
5992 only. The line end will not produce any glyphs and we
5993 would never get MOVE_X_REACHED. */
5994 xassert (it->nglyphs == 0);
5995 result = MOVE_X_REACHED;
5996 break;
5997 }
5998
5999 /* Is this a line end? If yes, we're done. */
6000 if (ITERATOR_AT_END_OF_LINE_P (it))
6001 {
6002 result = MOVE_NEWLINE_OR_CR;
6003 break;
6004 }
6005
6006 /* The current display element has been consumed. Advance
6007 to the next. */
6008 set_iterator_to_next (it, 1);
6009
6010 /* Stop if lines are truncated and IT's current x-position is
6011 past the right edge of the window now. */
6012 if (it->truncate_lines_p
6013 && it->current_x >= it->last_visible_x)
6014 {
6015 #ifdef HAVE_WINDOW_SYSTEM
6016 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6017 {
6018 if (!get_next_display_element (it)
6019 || BUFFER_POS_REACHED_P ())
6020 {
6021 result = MOVE_POS_MATCH_OR_ZV;
6022 break;
6023 }
6024 if (ITERATOR_AT_END_OF_LINE_P (it))
6025 {
6026 result = MOVE_NEWLINE_OR_CR;
6027 break;
6028 }
6029 }
6030 #endif /* HAVE_WINDOW_SYSTEM */
6031 result = MOVE_LINE_TRUNCATED;
6032 break;
6033 }
6034 }
6035
6036 #undef BUFFER_POS_REACHED_P
6037
6038 /* Restore the iterator settings altered at the beginning of this
6039 function. */
6040 it->glyph_row = saved_glyph_row;
6041 return result;
6042 }
6043
6044
6045 /* Move IT forward until it satisfies one or more of the criteria in
6046 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6047
6048 OP is a bit-mask that specifies where to stop, and in particular,
6049 which of those four position arguments makes a difference. See the
6050 description of enum move_operation_enum.
6051
6052 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6053 screen line, this function will set IT to the next position >
6054 TO_CHARPOS. */
6055
6056 void
6057 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6058 struct it *it;
6059 int to_charpos, to_x, to_y, to_vpos;
6060 int op;
6061 {
6062 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6063 int line_height;
6064 int reached = 0;
6065
6066 for (;;)
6067 {
6068 if (op & MOVE_TO_VPOS)
6069 {
6070 /* If no TO_CHARPOS and no TO_X specified, stop at the
6071 start of the line TO_VPOS. */
6072 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6073 {
6074 if (it->vpos == to_vpos)
6075 {
6076 reached = 1;
6077 break;
6078 }
6079 else
6080 skip = move_it_in_display_line_to (it, -1, -1, 0);
6081 }
6082 else
6083 {
6084 /* TO_VPOS >= 0 means stop at TO_X in the line at
6085 TO_VPOS, or at TO_POS, whichever comes first. */
6086 if (it->vpos == to_vpos)
6087 {
6088 reached = 2;
6089 break;
6090 }
6091
6092 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6093
6094 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6095 {
6096 reached = 3;
6097 break;
6098 }
6099 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6100 {
6101 /* We have reached TO_X but not in the line we want. */
6102 skip = move_it_in_display_line_to (it, to_charpos,
6103 -1, MOVE_TO_POS);
6104 if (skip == MOVE_POS_MATCH_OR_ZV)
6105 {
6106 reached = 4;
6107 break;
6108 }
6109 }
6110 }
6111 }
6112 else if (op & MOVE_TO_Y)
6113 {
6114 struct it it_backup;
6115
6116 /* TO_Y specified means stop at TO_X in the line containing
6117 TO_Y---or at TO_CHARPOS if this is reached first. The
6118 problem is that we can't really tell whether the line
6119 contains TO_Y before we have completely scanned it, and
6120 this may skip past TO_X. What we do is to first scan to
6121 TO_X.
6122
6123 If TO_X is not specified, use a TO_X of zero. The reason
6124 is to make the outcome of this function more predictable.
6125 If we didn't use TO_X == 0, we would stop at the end of
6126 the line which is probably not what a caller would expect
6127 to happen. */
6128 skip = move_it_in_display_line_to (it, to_charpos,
6129 ((op & MOVE_TO_X)
6130 ? to_x : 0),
6131 (MOVE_TO_X
6132 | (op & MOVE_TO_POS)));
6133
6134 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6135 if (skip == MOVE_POS_MATCH_OR_ZV)
6136 {
6137 reached = 5;
6138 break;
6139 }
6140
6141 /* If TO_X was reached, we would like to know whether TO_Y
6142 is in the line. This can only be said if we know the
6143 total line height which requires us to scan the rest of
6144 the line. */
6145 if (skip == MOVE_X_REACHED)
6146 {
6147 it_backup = *it;
6148 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6149 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6150 op & MOVE_TO_POS);
6151 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6152 }
6153
6154 /* Now, decide whether TO_Y is in this line. */
6155 line_height = it->max_ascent + it->max_descent;
6156 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6157
6158 if (to_y >= it->current_y
6159 && to_y < it->current_y + line_height)
6160 {
6161 if (skip == MOVE_X_REACHED)
6162 /* If TO_Y is in this line and TO_X was reached above,
6163 we scanned too far. We have to restore IT's settings
6164 to the ones before skipping. */
6165 *it = it_backup;
6166 reached = 6;
6167 }
6168 else if (skip == MOVE_X_REACHED)
6169 {
6170 skip = skip2;
6171 if (skip == MOVE_POS_MATCH_OR_ZV)
6172 reached = 7;
6173 }
6174
6175 if (reached)
6176 break;
6177 }
6178 else
6179 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6180
6181 switch (skip)
6182 {
6183 case MOVE_POS_MATCH_OR_ZV:
6184 reached = 8;
6185 goto out;
6186
6187 case MOVE_NEWLINE_OR_CR:
6188 set_iterator_to_next (it, 1);
6189 it->continuation_lines_width = 0;
6190 break;
6191
6192 case MOVE_LINE_TRUNCATED:
6193 it->continuation_lines_width = 0;
6194 reseat_at_next_visible_line_start (it, 0);
6195 if ((op & MOVE_TO_POS) != 0
6196 && IT_CHARPOS (*it) > to_charpos)
6197 {
6198 reached = 9;
6199 goto out;
6200 }
6201 break;
6202
6203 case MOVE_LINE_CONTINUED:
6204 it->continuation_lines_width += it->current_x;
6205 break;
6206
6207 default:
6208 abort ();
6209 }
6210
6211 /* Reset/increment for the next run. */
6212 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6213 it->current_x = it->hpos = 0;
6214 it->current_y += it->max_ascent + it->max_descent;
6215 ++it->vpos;
6216 last_height = it->max_ascent + it->max_descent;
6217 last_max_ascent = it->max_ascent;
6218 it->max_ascent = it->max_descent = 0;
6219 }
6220
6221 out:
6222
6223 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6224 }
6225
6226
6227 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6228
6229 If DY > 0, move IT backward at least that many pixels. DY = 0
6230 means move IT backward to the preceding line start or BEGV. This
6231 function may move over more than DY pixels if IT->current_y - DY
6232 ends up in the middle of a line; in this case IT->current_y will be
6233 set to the top of the line moved to. */
6234
6235 void
6236 move_it_vertically_backward (it, dy)
6237 struct it *it;
6238 int dy;
6239 {
6240 int nlines, h;
6241 struct it it2, it3;
6242 int start_pos;
6243
6244 move_further_back:
6245 xassert (dy >= 0);
6246
6247 start_pos = IT_CHARPOS (*it);
6248
6249 /* Estimate how many newlines we must move back. */
6250 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6251
6252 /* Set the iterator's position that many lines back. */
6253 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6254 back_to_previous_visible_line_start (it);
6255
6256 /* Reseat the iterator here. When moving backward, we don't want
6257 reseat to skip forward over invisible text, set up the iterator
6258 to deliver from overlay strings at the new position etc. So,
6259 use reseat_1 here. */
6260 reseat_1 (it, it->current.pos, 1);
6261
6262 /* We are now surely at a line start. */
6263 it->current_x = it->hpos = 0;
6264 it->continuation_lines_width = 0;
6265
6266 /* Move forward and see what y-distance we moved. First move to the
6267 start of the next line so that we get its height. We need this
6268 height to be able to tell whether we reached the specified
6269 y-distance. */
6270 it2 = *it;
6271 it2.max_ascent = it2.max_descent = 0;
6272 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6273 MOVE_TO_POS | MOVE_TO_VPOS);
6274 xassert (IT_CHARPOS (*it) >= BEGV);
6275 it3 = it2;
6276
6277 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6278 xassert (IT_CHARPOS (*it) >= BEGV);
6279 /* H is the actual vertical distance from the position in *IT
6280 and the starting position. */
6281 h = it2.current_y - it->current_y;
6282 /* NLINES is the distance in number of lines. */
6283 nlines = it2.vpos - it->vpos;
6284
6285 /* Correct IT's y and vpos position
6286 so that they are relative to the starting point. */
6287 it->vpos -= nlines;
6288 it->current_y -= h;
6289
6290 if (dy == 0)
6291 {
6292 /* DY == 0 means move to the start of the screen line. The
6293 value of nlines is > 0 if continuation lines were involved. */
6294 if (nlines > 0)
6295 move_it_by_lines (it, nlines, 1);
6296 xassert (IT_CHARPOS (*it) <= start_pos);
6297 }
6298 else
6299 {
6300 /* The y-position we try to reach, relative to *IT.
6301 Note that H has been subtracted in front of the if-statement. */
6302 int target_y = it->current_y + h - dy;
6303 int y0 = it3.current_y;
6304 int y1 = line_bottom_y (&it3);
6305 int line_height = y1 - y0;
6306
6307 /* If we did not reach target_y, try to move further backward if
6308 we can. If we moved too far backward, try to move forward. */
6309 if (target_y < it->current_y
6310 /* This is heuristic. In a window that's 3 lines high, with
6311 a line height of 13 pixels each, recentering with point
6312 on the bottom line will try to move -39/2 = 19 pixels
6313 backward. Try to avoid moving into the first line. */
6314 && it->current_y - target_y > line_height * 2 / 3
6315 && IT_CHARPOS (*it) > BEGV)
6316 {
6317 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6318 target_y - it->current_y));
6319 dy = it->current_y - target_y;
6320 goto move_further_back;
6321 }
6322 else if (target_y >= it->current_y + line_height
6323 && IT_CHARPOS (*it) < ZV)
6324 {
6325 /* Should move forward by at least one line, maybe more.
6326
6327 Note: Calling move_it_by_lines can be expensive on
6328 terminal frames, where compute_motion is used (via
6329 vmotion) to do the job, when there are very long lines
6330 and truncate-lines is nil. That's the reason for
6331 treating terminal frames specially here. */
6332
6333 if (!FRAME_WINDOW_P (it->f))
6334 move_it_vertically (it, target_y - (it->current_y + line_height));
6335 else
6336 {
6337 do
6338 {
6339 move_it_by_lines (it, 1, 1);
6340 }
6341 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6342 }
6343
6344 xassert (IT_CHARPOS (*it) >= BEGV);
6345 }
6346 }
6347 }
6348
6349
6350 /* Move IT by a specified amount of pixel lines DY. DY negative means
6351 move backwards. DY = 0 means move to start of screen line. At the
6352 end, IT will be on the start of a screen line. */
6353
6354 void
6355 move_it_vertically (it, dy)
6356 struct it *it;
6357 int dy;
6358 {
6359 if (dy <= 0)
6360 move_it_vertically_backward (it, -dy);
6361 else
6362 {
6363 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6364 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6365 MOVE_TO_POS | MOVE_TO_Y);
6366 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6367
6368 /* If buffer ends in ZV without a newline, move to the start of
6369 the line to satisfy the post-condition. */
6370 if (IT_CHARPOS (*it) == ZV
6371 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6372 move_it_by_lines (it, 0, 0);
6373 }
6374 }
6375
6376
6377 /* Move iterator IT past the end of the text line it is in. */
6378
6379 void
6380 move_it_past_eol (it)
6381 struct it *it;
6382 {
6383 enum move_it_result rc;
6384
6385 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6386 if (rc == MOVE_NEWLINE_OR_CR)
6387 set_iterator_to_next (it, 0);
6388 }
6389
6390
6391 #if 0 /* Currently not used. */
6392
6393 /* Return non-zero if some text between buffer positions START_CHARPOS
6394 and END_CHARPOS is invisible. IT->window is the window for text
6395 property lookup. */
6396
6397 static int
6398 invisible_text_between_p (it, start_charpos, end_charpos)
6399 struct it *it;
6400 int start_charpos, end_charpos;
6401 {
6402 Lisp_Object prop, limit;
6403 int invisible_found_p;
6404
6405 xassert (it != NULL && start_charpos <= end_charpos);
6406
6407 /* Is text at START invisible? */
6408 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6409 it->window);
6410 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6411 invisible_found_p = 1;
6412 else
6413 {
6414 limit = Fnext_single_char_property_change (make_number (start_charpos),
6415 Qinvisible, Qnil,
6416 make_number (end_charpos));
6417 invisible_found_p = XFASTINT (limit) < end_charpos;
6418 }
6419
6420 return invisible_found_p;
6421 }
6422
6423 #endif /* 0 */
6424
6425
6426 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6427 negative means move up. DVPOS == 0 means move to the start of the
6428 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6429 NEED_Y_P is zero, IT->current_y will be left unchanged.
6430
6431 Further optimization ideas: If we would know that IT->f doesn't use
6432 a face with proportional font, we could be faster for
6433 truncate-lines nil. */
6434
6435 void
6436 move_it_by_lines (it, dvpos, need_y_p)
6437 struct it *it;
6438 int dvpos, need_y_p;
6439 {
6440 struct position pos;
6441
6442 if (!FRAME_WINDOW_P (it->f))
6443 {
6444 struct text_pos textpos;
6445
6446 /* We can use vmotion on frames without proportional fonts. */
6447 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6448 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6449 reseat (it, textpos, 1);
6450 it->vpos += pos.vpos;
6451 it->current_y += pos.vpos;
6452 }
6453 else if (dvpos == 0)
6454 {
6455 /* DVPOS == 0 means move to the start of the screen line. */
6456 move_it_vertically_backward (it, 0);
6457 xassert (it->current_x == 0 && it->hpos == 0);
6458 /* Let next call to line_bottom_y calculate real line height */
6459 last_height = 0;
6460 }
6461 else if (dvpos > 0)
6462 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6463 else
6464 {
6465 struct it it2;
6466 int start_charpos, i;
6467
6468 /* Start at the beginning of the screen line containing IT's
6469 position. */
6470 move_it_vertically_backward (it, 0);
6471
6472 /* Go back -DVPOS visible lines and reseat the iterator there. */
6473 start_charpos = IT_CHARPOS (*it);
6474 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6475 back_to_previous_visible_line_start (it);
6476 reseat (it, it->current.pos, 1);
6477 it->current_x = it->hpos = 0;
6478
6479 /* Above call may have moved too far if continuation lines
6480 are involved. Scan forward and see if it did. */
6481 it2 = *it;
6482 it2.vpos = it2.current_y = 0;
6483 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6484 it->vpos -= it2.vpos;
6485 it->current_y -= it2.current_y;
6486 it->current_x = it->hpos = 0;
6487
6488 /* If we moved too far back, move IT some lines forward. */
6489 if (it2.vpos > -dvpos)
6490 {
6491 int delta = it2.vpos + dvpos;
6492 it2 = *it;
6493 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6494 /* Move back again if we got too far ahead. */
6495 if (IT_CHARPOS (*it) >= start_charpos)
6496 *it = it2;
6497 }
6498 }
6499 }
6500
6501 /* Return 1 if IT points into the middle of a display vector. */
6502
6503 int
6504 in_display_vector_p (it)
6505 struct it *it;
6506 {
6507 return (it->method == next_element_from_display_vector
6508 && it->current.dpvec_index > 0
6509 && it->dpvec + it->current.dpvec_index != it->dpend);
6510 }
6511
6512 \f
6513 /***********************************************************************
6514 Messages
6515 ***********************************************************************/
6516
6517
6518 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6519 to *Messages*. */
6520
6521 void
6522 add_to_log (format, arg1, arg2)
6523 char *format;
6524 Lisp_Object arg1, arg2;
6525 {
6526 Lisp_Object args[3];
6527 Lisp_Object msg, fmt;
6528 char *buffer;
6529 int len;
6530 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6531 USE_SAFE_ALLOCA;
6532
6533 /* Do nothing if called asynchronously. Inserting text into
6534 a buffer may call after-change-functions and alike and
6535 that would means running Lisp asynchronously. */
6536 if (handling_signal)
6537 return;
6538
6539 fmt = msg = Qnil;
6540 GCPRO4 (fmt, msg, arg1, arg2);
6541
6542 args[0] = fmt = build_string (format);
6543 args[1] = arg1;
6544 args[2] = arg2;
6545 msg = Fformat (3, args);
6546
6547 len = SBYTES (msg) + 1;
6548 SAFE_ALLOCA (buffer, char *, len);
6549 bcopy (SDATA (msg), buffer, len);
6550
6551 message_dolog (buffer, len - 1, 1, 0);
6552 SAFE_FREE ();
6553
6554 UNGCPRO;
6555 }
6556
6557
6558 /* Output a newline in the *Messages* buffer if "needs" one. */
6559
6560 void
6561 message_log_maybe_newline ()
6562 {
6563 if (message_log_need_newline)
6564 message_dolog ("", 0, 1, 0);
6565 }
6566
6567
6568 /* Add a string M of length NBYTES to the message log, optionally
6569 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6570 nonzero, means interpret the contents of M as multibyte. This
6571 function calls low-level routines in order to bypass text property
6572 hooks, etc. which might not be safe to run. */
6573
6574 void
6575 message_dolog (m, nbytes, nlflag, multibyte)
6576 const char *m;
6577 int nbytes, nlflag, multibyte;
6578 {
6579 if (!NILP (Vmemory_full))
6580 return;
6581
6582 if (!NILP (Vmessage_log_max))
6583 {
6584 struct buffer *oldbuf;
6585 Lisp_Object oldpoint, oldbegv, oldzv;
6586 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6587 int point_at_end = 0;
6588 int zv_at_end = 0;
6589 Lisp_Object old_deactivate_mark, tem;
6590 struct gcpro gcpro1;
6591
6592 old_deactivate_mark = Vdeactivate_mark;
6593 oldbuf = current_buffer;
6594 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6595 current_buffer->undo_list = Qt;
6596
6597 oldpoint = message_dolog_marker1;
6598 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6599 oldbegv = message_dolog_marker2;
6600 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6601 oldzv = message_dolog_marker3;
6602 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6603 GCPRO1 (old_deactivate_mark);
6604
6605 if (PT == Z)
6606 point_at_end = 1;
6607 if (ZV == Z)
6608 zv_at_end = 1;
6609
6610 BEGV = BEG;
6611 BEGV_BYTE = BEG_BYTE;
6612 ZV = Z;
6613 ZV_BYTE = Z_BYTE;
6614 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6615
6616 /* Insert the string--maybe converting multibyte to single byte
6617 or vice versa, so that all the text fits the buffer. */
6618 if (multibyte
6619 && NILP (current_buffer->enable_multibyte_characters))
6620 {
6621 int i, c, char_bytes;
6622 unsigned char work[1];
6623
6624 /* Convert a multibyte string to single-byte
6625 for the *Message* buffer. */
6626 for (i = 0; i < nbytes; i += char_bytes)
6627 {
6628 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6629 work[0] = (SINGLE_BYTE_CHAR_P (c)
6630 ? c
6631 : multibyte_char_to_unibyte (c, Qnil));
6632 insert_1_both (work, 1, 1, 1, 0, 0);
6633 }
6634 }
6635 else if (! multibyte
6636 && ! NILP (current_buffer->enable_multibyte_characters))
6637 {
6638 int i, c, char_bytes;
6639 unsigned char *msg = (unsigned char *) m;
6640 unsigned char str[MAX_MULTIBYTE_LENGTH];
6641 /* Convert a single-byte string to multibyte
6642 for the *Message* buffer. */
6643 for (i = 0; i < nbytes; i++)
6644 {
6645 c = unibyte_char_to_multibyte (msg[i]);
6646 char_bytes = CHAR_STRING (c, str);
6647 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6648 }
6649 }
6650 else if (nbytes)
6651 insert_1 (m, nbytes, 1, 0, 0);
6652
6653 if (nlflag)
6654 {
6655 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6656 insert_1 ("\n", 1, 1, 0, 0);
6657
6658 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6659 this_bol = PT;
6660 this_bol_byte = PT_BYTE;
6661
6662 /* See if this line duplicates the previous one.
6663 If so, combine duplicates. */
6664 if (this_bol > BEG)
6665 {
6666 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6667 prev_bol = PT;
6668 prev_bol_byte = PT_BYTE;
6669
6670 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6671 this_bol, this_bol_byte);
6672 if (dup)
6673 {
6674 del_range_both (prev_bol, prev_bol_byte,
6675 this_bol, this_bol_byte, 0);
6676 if (dup > 1)
6677 {
6678 char dupstr[40];
6679 int duplen;
6680
6681 /* If you change this format, don't forget to also
6682 change message_log_check_duplicate. */
6683 sprintf (dupstr, " [%d times]", dup);
6684 duplen = strlen (dupstr);
6685 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6686 insert_1 (dupstr, duplen, 1, 0, 1);
6687 }
6688 }
6689 }
6690
6691 /* If we have more than the desired maximum number of lines
6692 in the *Messages* buffer now, delete the oldest ones.
6693 This is safe because we don't have undo in this buffer. */
6694
6695 if (NATNUMP (Vmessage_log_max))
6696 {
6697 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6698 -XFASTINT (Vmessage_log_max) - 1, 0);
6699 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6700 }
6701 }
6702 BEGV = XMARKER (oldbegv)->charpos;
6703 BEGV_BYTE = marker_byte_position (oldbegv);
6704
6705 if (zv_at_end)
6706 {
6707 ZV = Z;
6708 ZV_BYTE = Z_BYTE;
6709 }
6710 else
6711 {
6712 ZV = XMARKER (oldzv)->charpos;
6713 ZV_BYTE = marker_byte_position (oldzv);
6714 }
6715
6716 if (point_at_end)
6717 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6718 else
6719 /* We can't do Fgoto_char (oldpoint) because it will run some
6720 Lisp code. */
6721 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6722 XMARKER (oldpoint)->bytepos);
6723
6724 UNGCPRO;
6725 unchain_marker (XMARKER (oldpoint));
6726 unchain_marker (XMARKER (oldbegv));
6727 unchain_marker (XMARKER (oldzv));
6728
6729 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6730 set_buffer_internal (oldbuf);
6731 if (NILP (tem))
6732 windows_or_buffers_changed = old_windows_or_buffers_changed;
6733 message_log_need_newline = !nlflag;
6734 Vdeactivate_mark = old_deactivate_mark;
6735 }
6736 }
6737
6738
6739 /* We are at the end of the buffer after just having inserted a newline.
6740 (Note: We depend on the fact we won't be crossing the gap.)
6741 Check to see if the most recent message looks a lot like the previous one.
6742 Return 0 if different, 1 if the new one should just replace it, or a
6743 value N > 1 if we should also append " [N times]". */
6744
6745 static int
6746 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6747 int prev_bol, this_bol;
6748 int prev_bol_byte, this_bol_byte;
6749 {
6750 int i;
6751 int len = Z_BYTE - 1 - this_bol_byte;
6752 int seen_dots = 0;
6753 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6754 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6755
6756 for (i = 0; i < len; i++)
6757 {
6758 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6759 seen_dots = 1;
6760 if (p1[i] != p2[i])
6761 return seen_dots;
6762 }
6763 p1 += len;
6764 if (*p1 == '\n')
6765 return 2;
6766 if (*p1++ == ' ' && *p1++ == '[')
6767 {
6768 int n = 0;
6769 while (*p1 >= '0' && *p1 <= '9')
6770 n = n * 10 + *p1++ - '0';
6771 if (strncmp (p1, " times]\n", 8) == 0)
6772 return n+1;
6773 }
6774 return 0;
6775 }
6776 \f
6777
6778 /* Display an echo area message M with a specified length of NBYTES
6779 bytes. The string may include null characters. If M is 0, clear
6780 out any existing message, and let the mini-buffer text show
6781 through.
6782
6783 The buffer M must continue to exist until after the echo area gets
6784 cleared or some other message gets displayed there. This means do
6785 not pass text that is stored in a Lisp string; do not pass text in
6786 a buffer that was alloca'd. */
6787
6788 void
6789 message2 (m, nbytes, multibyte)
6790 const char *m;
6791 int nbytes;
6792 int multibyte;
6793 {
6794 /* First flush out any partial line written with print. */
6795 message_log_maybe_newline ();
6796 if (m)
6797 message_dolog (m, nbytes, 1, multibyte);
6798 message2_nolog (m, nbytes, multibyte);
6799 }
6800
6801
6802 /* The non-logging counterpart of message2. */
6803
6804 void
6805 message2_nolog (m, nbytes, multibyte)
6806 const char *m;
6807 int nbytes, multibyte;
6808 {
6809 struct frame *sf = SELECTED_FRAME ();
6810 message_enable_multibyte = multibyte;
6811
6812 if (noninteractive)
6813 {
6814 if (noninteractive_need_newline)
6815 putc ('\n', stderr);
6816 noninteractive_need_newline = 0;
6817 if (m)
6818 fwrite (m, nbytes, 1, stderr);
6819 if (cursor_in_echo_area == 0)
6820 fprintf (stderr, "\n");
6821 fflush (stderr);
6822 }
6823 /* A null message buffer means that the frame hasn't really been
6824 initialized yet. Error messages get reported properly by
6825 cmd_error, so this must be just an informative message; toss it. */
6826 else if (INTERACTIVE
6827 && sf->glyphs_initialized_p
6828 && FRAME_MESSAGE_BUF (sf))
6829 {
6830 Lisp_Object mini_window;
6831 struct frame *f;
6832
6833 /* Get the frame containing the mini-buffer
6834 that the selected frame is using. */
6835 mini_window = FRAME_MINIBUF_WINDOW (sf);
6836 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6837
6838 FRAME_SAMPLE_VISIBILITY (f);
6839 if (FRAME_VISIBLE_P (sf)
6840 && ! FRAME_VISIBLE_P (f))
6841 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6842
6843 if (m)
6844 {
6845 set_message (m, Qnil, nbytes, multibyte);
6846 if (minibuffer_auto_raise)
6847 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6848 }
6849 else
6850 clear_message (1, 1);
6851
6852 do_pending_window_change (0);
6853 echo_area_display (1);
6854 do_pending_window_change (0);
6855 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6856 (*frame_up_to_date_hook) (f);
6857 }
6858 }
6859
6860
6861 /* Display an echo area message M with a specified length of NBYTES
6862 bytes. The string may include null characters. If M is not a
6863 string, clear out any existing message, and let the mini-buffer
6864 text show through. */
6865
6866 void
6867 message3 (m, nbytes, multibyte)
6868 Lisp_Object m;
6869 int nbytes;
6870 int multibyte;
6871 {
6872 struct gcpro gcpro1;
6873
6874 GCPRO1 (m);
6875 clear_message (1,1);
6876
6877 /* First flush out any partial line written with print. */
6878 message_log_maybe_newline ();
6879 if (STRINGP (m))
6880 message_dolog (SDATA (m), nbytes, 1, multibyte);
6881 message3_nolog (m, nbytes, multibyte);
6882
6883 UNGCPRO;
6884 }
6885
6886
6887 /* The non-logging version of message3. */
6888
6889 void
6890 message3_nolog (m, nbytes, multibyte)
6891 Lisp_Object m;
6892 int nbytes, multibyte;
6893 {
6894 struct frame *sf = SELECTED_FRAME ();
6895 message_enable_multibyte = multibyte;
6896
6897 if (noninteractive)
6898 {
6899 if (noninteractive_need_newline)
6900 putc ('\n', stderr);
6901 noninteractive_need_newline = 0;
6902 if (STRINGP (m))
6903 fwrite (SDATA (m), nbytes, 1, stderr);
6904 if (cursor_in_echo_area == 0)
6905 fprintf (stderr, "\n");
6906 fflush (stderr);
6907 }
6908 /* A null message buffer means that the frame hasn't really been
6909 initialized yet. Error messages get reported properly by
6910 cmd_error, so this must be just an informative message; toss it. */
6911 else if (INTERACTIVE
6912 && sf->glyphs_initialized_p
6913 && FRAME_MESSAGE_BUF (sf))
6914 {
6915 Lisp_Object mini_window;
6916 Lisp_Object frame;
6917 struct frame *f;
6918
6919 /* Get the frame containing the mini-buffer
6920 that the selected frame is using. */
6921 mini_window = FRAME_MINIBUF_WINDOW (sf);
6922 frame = XWINDOW (mini_window)->frame;
6923 f = XFRAME (frame);
6924
6925 FRAME_SAMPLE_VISIBILITY (f);
6926 if (FRAME_VISIBLE_P (sf)
6927 && !FRAME_VISIBLE_P (f))
6928 Fmake_frame_visible (frame);
6929
6930 if (STRINGP (m) && SCHARS (m) > 0)
6931 {
6932 set_message (NULL, m, nbytes, multibyte);
6933 if (minibuffer_auto_raise)
6934 Fraise_frame (frame);
6935 }
6936 else
6937 clear_message (1, 1);
6938
6939 do_pending_window_change (0);
6940 echo_area_display (1);
6941 do_pending_window_change (0);
6942 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6943 (*frame_up_to_date_hook) (f);
6944 }
6945 }
6946
6947
6948 /* Display a null-terminated echo area message M. If M is 0, clear
6949 out any existing message, and let the mini-buffer text show through.
6950
6951 The buffer M must continue to exist until after the echo area gets
6952 cleared or some other message gets displayed there. Do not pass
6953 text that is stored in a Lisp string. Do not pass text in a buffer
6954 that was alloca'd. */
6955
6956 void
6957 message1 (m)
6958 char *m;
6959 {
6960 message2 (m, (m ? strlen (m) : 0), 0);
6961 }
6962
6963
6964 /* The non-logging counterpart of message1. */
6965
6966 void
6967 message1_nolog (m)
6968 char *m;
6969 {
6970 message2_nolog (m, (m ? strlen (m) : 0), 0);
6971 }
6972
6973 /* Display a message M which contains a single %s
6974 which gets replaced with STRING. */
6975
6976 void
6977 message_with_string (m, string, log)
6978 char *m;
6979 Lisp_Object string;
6980 int log;
6981 {
6982 CHECK_STRING (string);
6983
6984 if (noninteractive)
6985 {
6986 if (m)
6987 {
6988 if (noninteractive_need_newline)
6989 putc ('\n', stderr);
6990 noninteractive_need_newline = 0;
6991 fprintf (stderr, m, SDATA (string));
6992 if (cursor_in_echo_area == 0)
6993 fprintf (stderr, "\n");
6994 fflush (stderr);
6995 }
6996 }
6997 else if (INTERACTIVE)
6998 {
6999 /* The frame whose minibuffer we're going to display the message on.
7000 It may be larger than the selected frame, so we need
7001 to use its buffer, not the selected frame's buffer. */
7002 Lisp_Object mini_window;
7003 struct frame *f, *sf = SELECTED_FRAME ();
7004
7005 /* Get the frame containing the minibuffer
7006 that the selected frame is using. */
7007 mini_window = FRAME_MINIBUF_WINDOW (sf);
7008 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7009
7010 /* A null message buffer means that the frame hasn't really been
7011 initialized yet. Error messages get reported properly by
7012 cmd_error, so this must be just an informative message; toss it. */
7013 if (FRAME_MESSAGE_BUF (f))
7014 {
7015 Lisp_Object args[2], message;
7016 struct gcpro gcpro1, gcpro2;
7017
7018 args[0] = build_string (m);
7019 args[1] = message = string;
7020 GCPRO2 (args[0], message);
7021 gcpro1.nvars = 2;
7022
7023 message = Fformat (2, args);
7024
7025 if (log)
7026 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7027 else
7028 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7029
7030 UNGCPRO;
7031
7032 /* Print should start at the beginning of the message
7033 buffer next time. */
7034 message_buf_print = 0;
7035 }
7036 }
7037 }
7038
7039
7040 /* Dump an informative message to the minibuf. If M is 0, clear out
7041 any existing message, and let the mini-buffer text show through. */
7042
7043 /* VARARGS 1 */
7044 void
7045 message (m, a1, a2, a3)
7046 char *m;
7047 EMACS_INT a1, a2, a3;
7048 {
7049 if (noninteractive)
7050 {
7051 if (m)
7052 {
7053 if (noninteractive_need_newline)
7054 putc ('\n', stderr);
7055 noninteractive_need_newline = 0;
7056 fprintf (stderr, m, a1, a2, a3);
7057 if (cursor_in_echo_area == 0)
7058 fprintf (stderr, "\n");
7059 fflush (stderr);
7060 }
7061 }
7062 else if (INTERACTIVE)
7063 {
7064 /* The frame whose mini-buffer we're going to display the message
7065 on. It may be larger than the selected frame, so we need to
7066 use its buffer, not the selected frame's buffer. */
7067 Lisp_Object mini_window;
7068 struct frame *f, *sf = SELECTED_FRAME ();
7069
7070 /* Get the frame containing the mini-buffer
7071 that the selected frame is using. */
7072 mini_window = FRAME_MINIBUF_WINDOW (sf);
7073 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7074
7075 /* A null message buffer means that the frame hasn't really been
7076 initialized yet. Error messages get reported properly by
7077 cmd_error, so this must be just an informative message; toss
7078 it. */
7079 if (FRAME_MESSAGE_BUF (f))
7080 {
7081 if (m)
7082 {
7083 int len;
7084 #ifdef NO_ARG_ARRAY
7085 char *a[3];
7086 a[0] = (char *) a1;
7087 a[1] = (char *) a2;
7088 a[2] = (char *) a3;
7089
7090 len = doprnt (FRAME_MESSAGE_BUF (f),
7091 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7092 #else
7093 len = doprnt (FRAME_MESSAGE_BUF (f),
7094 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7095 (char **) &a1);
7096 #endif /* NO_ARG_ARRAY */
7097
7098 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7099 }
7100 else
7101 message1 (0);
7102
7103 /* Print should start at the beginning of the message
7104 buffer next time. */
7105 message_buf_print = 0;
7106 }
7107 }
7108 }
7109
7110
7111 /* The non-logging version of message. */
7112
7113 void
7114 message_nolog (m, a1, a2, a3)
7115 char *m;
7116 EMACS_INT a1, a2, a3;
7117 {
7118 Lisp_Object old_log_max;
7119 old_log_max = Vmessage_log_max;
7120 Vmessage_log_max = Qnil;
7121 message (m, a1, a2, a3);
7122 Vmessage_log_max = old_log_max;
7123 }
7124
7125
7126 /* Display the current message in the current mini-buffer. This is
7127 only called from error handlers in process.c, and is not time
7128 critical. */
7129
7130 void
7131 update_echo_area ()
7132 {
7133 if (!NILP (echo_area_buffer[0]))
7134 {
7135 Lisp_Object string;
7136 string = Fcurrent_message ();
7137 message3 (string, SBYTES (string),
7138 !NILP (current_buffer->enable_multibyte_characters));
7139 }
7140 }
7141
7142
7143 /* Make sure echo area buffers in `echo_buffers' are live.
7144 If they aren't, make new ones. */
7145
7146 static void
7147 ensure_echo_area_buffers ()
7148 {
7149 int i;
7150
7151 for (i = 0; i < 2; ++i)
7152 if (!BUFFERP (echo_buffer[i])
7153 || NILP (XBUFFER (echo_buffer[i])->name))
7154 {
7155 char name[30];
7156 Lisp_Object old_buffer;
7157 int j;
7158
7159 old_buffer = echo_buffer[i];
7160 sprintf (name, " *Echo Area %d*", i);
7161 echo_buffer[i] = Fget_buffer_create (build_string (name));
7162 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7163
7164 for (j = 0; j < 2; ++j)
7165 if (EQ (old_buffer, echo_area_buffer[j]))
7166 echo_area_buffer[j] = echo_buffer[i];
7167 }
7168 }
7169
7170
7171 /* Call FN with args A1..A4 with either the current or last displayed
7172 echo_area_buffer as current buffer.
7173
7174 WHICH zero means use the current message buffer
7175 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7176 from echo_buffer[] and clear it.
7177
7178 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7179 suitable buffer from echo_buffer[] and clear it.
7180
7181 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7182 that the current message becomes the last displayed one, make
7183 choose a suitable buffer for echo_area_buffer[0], and clear it.
7184
7185 Value is what FN returns. */
7186
7187 static int
7188 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7189 struct window *w;
7190 int which;
7191 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7192 EMACS_INT a1;
7193 Lisp_Object a2;
7194 EMACS_INT a3, a4;
7195 {
7196 Lisp_Object buffer;
7197 int this_one, the_other, clear_buffer_p, rc;
7198 int count = SPECPDL_INDEX ();
7199
7200 /* If buffers aren't live, make new ones. */
7201 ensure_echo_area_buffers ();
7202
7203 clear_buffer_p = 0;
7204
7205 if (which == 0)
7206 this_one = 0, the_other = 1;
7207 else if (which > 0)
7208 this_one = 1, the_other = 0;
7209 else
7210 {
7211 this_one = 0, the_other = 1;
7212 clear_buffer_p = 1;
7213
7214 /* We need a fresh one in case the current echo buffer equals
7215 the one containing the last displayed echo area message. */
7216 if (!NILP (echo_area_buffer[this_one])
7217 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7218 echo_area_buffer[this_one] = Qnil;
7219 }
7220
7221 /* Choose a suitable buffer from echo_buffer[] is we don't
7222 have one. */
7223 if (NILP (echo_area_buffer[this_one]))
7224 {
7225 echo_area_buffer[this_one]
7226 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7227 ? echo_buffer[the_other]
7228 : echo_buffer[this_one]);
7229 clear_buffer_p = 1;
7230 }
7231
7232 buffer = echo_area_buffer[this_one];
7233
7234 /* Don't get confused by reusing the buffer used for echoing
7235 for a different purpose. */
7236 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7237 cancel_echoing ();
7238
7239 record_unwind_protect (unwind_with_echo_area_buffer,
7240 with_echo_area_buffer_unwind_data (w));
7241
7242 /* Make the echo area buffer current. Note that for display
7243 purposes, it is not necessary that the displayed window's buffer
7244 == current_buffer, except for text property lookup. So, let's
7245 only set that buffer temporarily here without doing a full
7246 Fset_window_buffer. We must also change w->pointm, though,
7247 because otherwise an assertions in unshow_buffer fails, and Emacs
7248 aborts. */
7249 set_buffer_internal_1 (XBUFFER (buffer));
7250 if (w)
7251 {
7252 w->buffer = buffer;
7253 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7254 }
7255
7256 current_buffer->undo_list = Qt;
7257 current_buffer->read_only = Qnil;
7258 specbind (Qinhibit_read_only, Qt);
7259 specbind (Qinhibit_modification_hooks, Qt);
7260
7261 if (clear_buffer_p && Z > BEG)
7262 del_range (BEG, Z);
7263
7264 xassert (BEGV >= BEG);
7265 xassert (ZV <= Z && ZV >= BEGV);
7266
7267 rc = fn (a1, a2, a3, a4);
7268
7269 xassert (BEGV >= BEG);
7270 xassert (ZV <= Z && ZV >= BEGV);
7271
7272 unbind_to (count, Qnil);
7273 return rc;
7274 }
7275
7276
7277 /* Save state that should be preserved around the call to the function
7278 FN called in with_echo_area_buffer. */
7279
7280 static Lisp_Object
7281 with_echo_area_buffer_unwind_data (w)
7282 struct window *w;
7283 {
7284 int i = 0;
7285 Lisp_Object vector;
7286
7287 /* Reduce consing by keeping one vector in
7288 Vwith_echo_area_save_vector. */
7289 vector = Vwith_echo_area_save_vector;
7290 Vwith_echo_area_save_vector = Qnil;
7291
7292 if (NILP (vector))
7293 vector = Fmake_vector (make_number (7), Qnil);
7294
7295 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7296 AREF (vector, i) = Vdeactivate_mark, ++i;
7297 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7298
7299 if (w)
7300 {
7301 XSETWINDOW (AREF (vector, i), w); ++i;
7302 AREF (vector, i) = w->buffer; ++i;
7303 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7304 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7305 }
7306 else
7307 {
7308 int end = i + 4;
7309 for (; i < end; ++i)
7310 AREF (vector, i) = Qnil;
7311 }
7312
7313 xassert (i == ASIZE (vector));
7314 return vector;
7315 }
7316
7317
7318 /* Restore global state from VECTOR which was created by
7319 with_echo_area_buffer_unwind_data. */
7320
7321 static Lisp_Object
7322 unwind_with_echo_area_buffer (vector)
7323 Lisp_Object vector;
7324 {
7325 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7326 Vdeactivate_mark = AREF (vector, 1);
7327 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7328
7329 if (WINDOWP (AREF (vector, 3)))
7330 {
7331 struct window *w;
7332 Lisp_Object buffer, charpos, bytepos;
7333
7334 w = XWINDOW (AREF (vector, 3));
7335 buffer = AREF (vector, 4);
7336 charpos = AREF (vector, 5);
7337 bytepos = AREF (vector, 6);
7338
7339 w->buffer = buffer;
7340 set_marker_both (w->pointm, buffer,
7341 XFASTINT (charpos), XFASTINT (bytepos));
7342 }
7343
7344 Vwith_echo_area_save_vector = vector;
7345 return Qnil;
7346 }
7347
7348
7349 /* Set up the echo area for use by print functions. MULTIBYTE_P
7350 non-zero means we will print multibyte. */
7351
7352 void
7353 setup_echo_area_for_printing (multibyte_p)
7354 int multibyte_p;
7355 {
7356 /* If we can't find an echo area any more, exit. */
7357 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7358 Fkill_emacs (Qnil);
7359
7360 ensure_echo_area_buffers ();
7361
7362 if (!message_buf_print)
7363 {
7364 /* A message has been output since the last time we printed.
7365 Choose a fresh echo area buffer. */
7366 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7367 echo_area_buffer[0] = echo_buffer[1];
7368 else
7369 echo_area_buffer[0] = echo_buffer[0];
7370
7371 /* Switch to that buffer and clear it. */
7372 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7373 current_buffer->truncate_lines = Qnil;
7374
7375 if (Z > BEG)
7376 {
7377 int count = SPECPDL_INDEX ();
7378 specbind (Qinhibit_read_only, Qt);
7379 /* Note that undo recording is always disabled. */
7380 del_range (BEG, Z);
7381 unbind_to (count, Qnil);
7382 }
7383 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7384
7385 /* Set up the buffer for the multibyteness we need. */
7386 if (multibyte_p
7387 != !NILP (current_buffer->enable_multibyte_characters))
7388 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7389
7390 /* Raise the frame containing the echo area. */
7391 if (minibuffer_auto_raise)
7392 {
7393 struct frame *sf = SELECTED_FRAME ();
7394 Lisp_Object mini_window;
7395 mini_window = FRAME_MINIBUF_WINDOW (sf);
7396 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7397 }
7398
7399 message_log_maybe_newline ();
7400 message_buf_print = 1;
7401 }
7402 else
7403 {
7404 if (NILP (echo_area_buffer[0]))
7405 {
7406 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7407 echo_area_buffer[0] = echo_buffer[1];
7408 else
7409 echo_area_buffer[0] = echo_buffer[0];
7410 }
7411
7412 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7413 {
7414 /* Someone switched buffers between print requests. */
7415 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7416 current_buffer->truncate_lines = Qnil;
7417 }
7418 }
7419 }
7420
7421
7422 /* Display an echo area message in window W. Value is non-zero if W's
7423 height is changed. If display_last_displayed_message_p is
7424 non-zero, display the message that was last displayed, otherwise
7425 display the current message. */
7426
7427 static int
7428 display_echo_area (w)
7429 struct window *w;
7430 {
7431 int i, no_message_p, window_height_changed_p, count;
7432
7433 /* Temporarily disable garbage collections while displaying the echo
7434 area. This is done because a GC can print a message itself.
7435 That message would modify the echo area buffer's contents while a
7436 redisplay of the buffer is going on, and seriously confuse
7437 redisplay. */
7438 count = inhibit_garbage_collection ();
7439
7440 /* If there is no message, we must call display_echo_area_1
7441 nevertheless because it resizes the window. But we will have to
7442 reset the echo_area_buffer in question to nil at the end because
7443 with_echo_area_buffer will sets it to an empty buffer. */
7444 i = display_last_displayed_message_p ? 1 : 0;
7445 no_message_p = NILP (echo_area_buffer[i]);
7446
7447 window_height_changed_p
7448 = with_echo_area_buffer (w, display_last_displayed_message_p,
7449 display_echo_area_1,
7450 (EMACS_INT) w, Qnil, 0, 0);
7451
7452 if (no_message_p)
7453 echo_area_buffer[i] = Qnil;
7454
7455 unbind_to (count, Qnil);
7456 return window_height_changed_p;
7457 }
7458
7459
7460 /* Helper for display_echo_area. Display the current buffer which
7461 contains the current echo area message in window W, a mini-window,
7462 a pointer to which is passed in A1. A2..A4 are currently not used.
7463 Change the height of W so that all of the message is displayed.
7464 Value is non-zero if height of W was changed. */
7465
7466 static int
7467 display_echo_area_1 (a1, a2, a3, a4)
7468 EMACS_INT a1;
7469 Lisp_Object a2;
7470 EMACS_INT a3, a4;
7471 {
7472 struct window *w = (struct window *) a1;
7473 Lisp_Object window;
7474 struct text_pos start;
7475 int window_height_changed_p = 0;
7476
7477 /* Do this before displaying, so that we have a large enough glyph
7478 matrix for the display. */
7479 window_height_changed_p = resize_mini_window (w, 0);
7480
7481 /* Display. */
7482 clear_glyph_matrix (w->desired_matrix);
7483 XSETWINDOW (window, w);
7484 SET_TEXT_POS (start, BEG, BEG_BYTE);
7485 try_window (window, start);
7486
7487 return window_height_changed_p;
7488 }
7489
7490
7491 /* Resize the echo area window to exactly the size needed for the
7492 currently displayed message, if there is one. If a mini-buffer
7493 is active, don't shrink it. */
7494
7495 void
7496 resize_echo_area_exactly ()
7497 {
7498 if (BUFFERP (echo_area_buffer[0])
7499 && WINDOWP (echo_area_window))
7500 {
7501 struct window *w = XWINDOW (echo_area_window);
7502 int resized_p;
7503 Lisp_Object resize_exactly;
7504
7505 if (minibuf_level == 0)
7506 resize_exactly = Qt;
7507 else
7508 resize_exactly = Qnil;
7509
7510 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7511 (EMACS_INT) w, resize_exactly, 0, 0);
7512 if (resized_p)
7513 {
7514 ++windows_or_buffers_changed;
7515 ++update_mode_lines;
7516 redisplay_internal (0);
7517 }
7518 }
7519 }
7520
7521
7522 /* Callback function for with_echo_area_buffer, when used from
7523 resize_echo_area_exactly. A1 contains a pointer to the window to
7524 resize, EXACTLY non-nil means resize the mini-window exactly to the
7525 size of the text displayed. A3 and A4 are not used. Value is what
7526 resize_mini_window returns. */
7527
7528 static int
7529 resize_mini_window_1 (a1, exactly, a3, a4)
7530 EMACS_INT a1;
7531 Lisp_Object exactly;
7532 EMACS_INT a3, a4;
7533 {
7534 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7535 }
7536
7537
7538 /* Resize mini-window W to fit the size of its contents. EXACT:P
7539 means size the window exactly to the size needed. Otherwise, it's
7540 only enlarged until W's buffer is empty. Value is non-zero if
7541 the window height has been changed. */
7542
7543 int
7544 resize_mini_window (w, exact_p)
7545 struct window *w;
7546 int exact_p;
7547 {
7548 struct frame *f = XFRAME (w->frame);
7549 int window_height_changed_p = 0;
7550
7551 xassert (MINI_WINDOW_P (w));
7552
7553 /* Don't resize windows while redisplaying a window; it would
7554 confuse redisplay functions when the size of the window they are
7555 displaying changes from under them. Such a resizing can happen,
7556 for instance, when which-func prints a long message while
7557 we are running fontification-functions. We're running these
7558 functions with safe_call which binds inhibit-redisplay to t. */
7559 if (!NILP (Vinhibit_redisplay))
7560 return 0;
7561
7562 /* Nil means don't try to resize. */
7563 if (NILP (Vresize_mini_windows)
7564 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7565 return 0;
7566
7567 if (!FRAME_MINIBUF_ONLY_P (f))
7568 {
7569 struct it it;
7570 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7571 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7572 int height, max_height;
7573 int unit = FRAME_LINE_HEIGHT (f);
7574 struct text_pos start;
7575 struct buffer *old_current_buffer = NULL;
7576
7577 if (current_buffer != XBUFFER (w->buffer))
7578 {
7579 old_current_buffer = current_buffer;
7580 set_buffer_internal (XBUFFER (w->buffer));
7581 }
7582
7583 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7584
7585 /* Compute the max. number of lines specified by the user. */
7586 if (FLOATP (Vmax_mini_window_height))
7587 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7588 else if (INTEGERP (Vmax_mini_window_height))
7589 max_height = XINT (Vmax_mini_window_height);
7590 else
7591 max_height = total_height / 4;
7592
7593 /* Correct that max. height if it's bogus. */
7594 max_height = max (1, max_height);
7595 max_height = min (total_height, max_height);
7596
7597 /* Find out the height of the text in the window. */
7598 if (it.truncate_lines_p)
7599 height = 1;
7600 else
7601 {
7602 last_height = 0;
7603 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7604 if (it.max_ascent == 0 && it.max_descent == 0)
7605 height = it.current_y + last_height;
7606 else
7607 height = it.current_y + it.max_ascent + it.max_descent;
7608 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7609 height = (height + unit - 1) / unit;
7610 }
7611
7612 /* Compute a suitable window start. */
7613 if (height > max_height)
7614 {
7615 height = max_height;
7616 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7617 move_it_vertically_backward (&it, (height - 1) * unit);
7618 start = it.current.pos;
7619 }
7620 else
7621 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7622 SET_MARKER_FROM_TEXT_POS (w->start, start);
7623
7624 if (EQ (Vresize_mini_windows, Qgrow_only))
7625 {
7626 /* Let it grow only, until we display an empty message, in which
7627 case the window shrinks again. */
7628 if (height > WINDOW_TOTAL_LINES (w))
7629 {
7630 int old_height = WINDOW_TOTAL_LINES (w);
7631 freeze_window_starts (f, 1);
7632 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7633 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7634 }
7635 else if (height < WINDOW_TOTAL_LINES (w)
7636 && (exact_p || BEGV == ZV))
7637 {
7638 int old_height = WINDOW_TOTAL_LINES (w);
7639 freeze_window_starts (f, 0);
7640 shrink_mini_window (w);
7641 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7642 }
7643 }
7644 else
7645 {
7646 /* Always resize to exact size needed. */
7647 if (height > WINDOW_TOTAL_LINES (w))
7648 {
7649 int old_height = WINDOW_TOTAL_LINES (w);
7650 freeze_window_starts (f, 1);
7651 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7652 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7653 }
7654 else if (height < WINDOW_TOTAL_LINES (w))
7655 {
7656 int old_height = WINDOW_TOTAL_LINES (w);
7657 freeze_window_starts (f, 0);
7658 shrink_mini_window (w);
7659
7660 if (height)
7661 {
7662 freeze_window_starts (f, 1);
7663 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7664 }
7665
7666 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7667 }
7668 }
7669
7670 if (old_current_buffer)
7671 set_buffer_internal (old_current_buffer);
7672 }
7673
7674 return window_height_changed_p;
7675 }
7676
7677
7678 /* Value is the current message, a string, or nil if there is no
7679 current message. */
7680
7681 Lisp_Object
7682 current_message ()
7683 {
7684 Lisp_Object msg;
7685
7686 if (NILP (echo_area_buffer[0]))
7687 msg = Qnil;
7688 else
7689 {
7690 with_echo_area_buffer (0, 0, current_message_1,
7691 (EMACS_INT) &msg, Qnil, 0, 0);
7692 if (NILP (msg))
7693 echo_area_buffer[0] = Qnil;
7694 }
7695
7696 return msg;
7697 }
7698
7699
7700 static int
7701 current_message_1 (a1, a2, a3, a4)
7702 EMACS_INT a1;
7703 Lisp_Object a2;
7704 EMACS_INT a3, a4;
7705 {
7706 Lisp_Object *msg = (Lisp_Object *) a1;
7707
7708 if (Z > BEG)
7709 *msg = make_buffer_string (BEG, Z, 1);
7710 else
7711 *msg = Qnil;
7712 return 0;
7713 }
7714
7715
7716 /* Push the current message on Vmessage_stack for later restauration
7717 by restore_message. Value is non-zero if the current message isn't
7718 empty. This is a relatively infrequent operation, so it's not
7719 worth optimizing. */
7720
7721 int
7722 push_message ()
7723 {
7724 Lisp_Object msg;
7725 msg = current_message ();
7726 Vmessage_stack = Fcons (msg, Vmessage_stack);
7727 return STRINGP (msg);
7728 }
7729
7730
7731 /* Restore message display from the top of Vmessage_stack. */
7732
7733 void
7734 restore_message ()
7735 {
7736 Lisp_Object msg;
7737
7738 xassert (CONSP (Vmessage_stack));
7739 msg = XCAR (Vmessage_stack);
7740 if (STRINGP (msg))
7741 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7742 else
7743 message3_nolog (msg, 0, 0);
7744 }
7745
7746
7747 /* Handler for record_unwind_protect calling pop_message. */
7748
7749 Lisp_Object
7750 pop_message_unwind (dummy)
7751 Lisp_Object dummy;
7752 {
7753 pop_message ();
7754 return Qnil;
7755 }
7756
7757 /* Pop the top-most entry off Vmessage_stack. */
7758
7759 void
7760 pop_message ()
7761 {
7762 xassert (CONSP (Vmessage_stack));
7763 Vmessage_stack = XCDR (Vmessage_stack);
7764 }
7765
7766
7767 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7768 exits. If the stack is not empty, we have a missing pop_message
7769 somewhere. */
7770
7771 void
7772 check_message_stack ()
7773 {
7774 if (!NILP (Vmessage_stack))
7775 abort ();
7776 }
7777
7778
7779 /* Truncate to NCHARS what will be displayed in the echo area the next
7780 time we display it---but don't redisplay it now. */
7781
7782 void
7783 truncate_echo_area (nchars)
7784 int nchars;
7785 {
7786 if (nchars == 0)
7787 echo_area_buffer[0] = Qnil;
7788 /* A null message buffer means that the frame hasn't really been
7789 initialized yet. Error messages get reported properly by
7790 cmd_error, so this must be just an informative message; toss it. */
7791 else if (!noninteractive
7792 && INTERACTIVE
7793 && !NILP (echo_area_buffer[0]))
7794 {
7795 struct frame *sf = SELECTED_FRAME ();
7796 if (FRAME_MESSAGE_BUF (sf))
7797 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7798 }
7799 }
7800
7801
7802 /* Helper function for truncate_echo_area. Truncate the current
7803 message to at most NCHARS characters. */
7804
7805 static int
7806 truncate_message_1 (nchars, a2, a3, a4)
7807 EMACS_INT nchars;
7808 Lisp_Object a2;
7809 EMACS_INT a3, a4;
7810 {
7811 if (BEG + nchars < Z)
7812 del_range (BEG + nchars, Z);
7813 if (Z == BEG)
7814 echo_area_buffer[0] = Qnil;
7815 return 0;
7816 }
7817
7818
7819 /* Set the current message to a substring of S or STRING.
7820
7821 If STRING is a Lisp string, set the message to the first NBYTES
7822 bytes from STRING. NBYTES zero means use the whole string. If
7823 STRING is multibyte, the message will be displayed multibyte.
7824
7825 If S is not null, set the message to the first LEN bytes of S. LEN
7826 zero means use the whole string. MULTIBYTE_P non-zero means S is
7827 multibyte. Display the message multibyte in that case. */
7828
7829 void
7830 set_message (s, string, nbytes, multibyte_p)
7831 const char *s;
7832 Lisp_Object string;
7833 int nbytes, multibyte_p;
7834 {
7835 message_enable_multibyte
7836 = ((s && multibyte_p)
7837 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7838
7839 with_echo_area_buffer (0, -1, set_message_1,
7840 (EMACS_INT) s, string, nbytes, multibyte_p);
7841 message_buf_print = 0;
7842 help_echo_showing_p = 0;
7843 }
7844
7845
7846 /* Helper function for set_message. Arguments have the same meaning
7847 as there, with A1 corresponding to S and A2 corresponding to STRING
7848 This function is called with the echo area buffer being
7849 current. */
7850
7851 static int
7852 set_message_1 (a1, a2, nbytes, multibyte_p)
7853 EMACS_INT a1;
7854 Lisp_Object a2;
7855 EMACS_INT nbytes, multibyte_p;
7856 {
7857 const char *s = (const char *) a1;
7858 Lisp_Object string = a2;
7859
7860 xassert (BEG == Z);
7861
7862 /* Change multibyteness of the echo buffer appropriately. */
7863 if (message_enable_multibyte
7864 != !NILP (current_buffer->enable_multibyte_characters))
7865 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7866
7867 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7868
7869 /* Insert new message at BEG. */
7870 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7871
7872 if (STRINGP (string))
7873 {
7874 int nchars;
7875
7876 if (nbytes == 0)
7877 nbytes = SBYTES (string);
7878 nchars = string_byte_to_char (string, nbytes);
7879
7880 /* This function takes care of single/multibyte conversion. We
7881 just have to ensure that the echo area buffer has the right
7882 setting of enable_multibyte_characters. */
7883 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7884 }
7885 else if (s)
7886 {
7887 if (nbytes == 0)
7888 nbytes = strlen (s);
7889
7890 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7891 {
7892 /* Convert from multi-byte to single-byte. */
7893 int i, c, n;
7894 unsigned char work[1];
7895
7896 /* Convert a multibyte string to single-byte. */
7897 for (i = 0; i < nbytes; i += n)
7898 {
7899 c = string_char_and_length (s + i, nbytes - i, &n);
7900 work[0] = (SINGLE_BYTE_CHAR_P (c)
7901 ? c
7902 : multibyte_char_to_unibyte (c, Qnil));
7903 insert_1_both (work, 1, 1, 1, 0, 0);
7904 }
7905 }
7906 else if (!multibyte_p
7907 && !NILP (current_buffer->enable_multibyte_characters))
7908 {
7909 /* Convert from single-byte to multi-byte. */
7910 int i, c, n;
7911 const unsigned char *msg = (const unsigned char *) s;
7912 unsigned char str[MAX_MULTIBYTE_LENGTH];
7913
7914 /* Convert a single-byte string to multibyte. */
7915 for (i = 0; i < nbytes; i++)
7916 {
7917 c = unibyte_char_to_multibyte (msg[i]);
7918 n = CHAR_STRING (c, str);
7919 insert_1_both (str, 1, n, 1, 0, 0);
7920 }
7921 }
7922 else
7923 insert_1 (s, nbytes, 1, 0, 0);
7924 }
7925
7926 return 0;
7927 }
7928
7929
7930 /* Clear messages. CURRENT_P non-zero means clear the current
7931 message. LAST_DISPLAYED_P non-zero means clear the message
7932 last displayed. */
7933
7934 void
7935 clear_message (current_p, last_displayed_p)
7936 int current_p, last_displayed_p;
7937 {
7938 if (current_p)
7939 {
7940 echo_area_buffer[0] = Qnil;
7941 message_cleared_p = 1;
7942 }
7943
7944 if (last_displayed_p)
7945 echo_area_buffer[1] = Qnil;
7946
7947 message_buf_print = 0;
7948 }
7949
7950 /* Clear garbaged frames.
7951
7952 This function is used where the old redisplay called
7953 redraw_garbaged_frames which in turn called redraw_frame which in
7954 turn called clear_frame. The call to clear_frame was a source of
7955 flickering. I believe a clear_frame is not necessary. It should
7956 suffice in the new redisplay to invalidate all current matrices,
7957 and ensure a complete redisplay of all windows. */
7958
7959 static void
7960 clear_garbaged_frames ()
7961 {
7962 if (frame_garbaged)
7963 {
7964 Lisp_Object tail, frame;
7965 int changed_count = 0;
7966
7967 FOR_EACH_FRAME (tail, frame)
7968 {
7969 struct frame *f = XFRAME (frame);
7970
7971 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
7972 {
7973 if (f->resized_p)
7974 {
7975 Fredraw_frame (frame);
7976 f->force_flush_display_p = 1;
7977 }
7978 clear_current_matrices (f);
7979 changed_count++;
7980 f->garbaged = 0;
7981 f->resized_p = 0;
7982 }
7983 }
7984
7985 frame_garbaged = 0;
7986 if (changed_count)
7987 ++windows_or_buffers_changed;
7988 }
7989 }
7990
7991
7992 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
7993 is non-zero update selected_frame. Value is non-zero if the
7994 mini-windows height has been changed. */
7995
7996 static int
7997 echo_area_display (update_frame_p)
7998 int update_frame_p;
7999 {
8000 Lisp_Object mini_window;
8001 struct window *w;
8002 struct frame *f;
8003 int window_height_changed_p = 0;
8004 struct frame *sf = SELECTED_FRAME ();
8005
8006 mini_window = FRAME_MINIBUF_WINDOW (sf);
8007 w = XWINDOW (mini_window);
8008 f = XFRAME (WINDOW_FRAME (w));
8009
8010 /* Don't display if frame is invisible or not yet initialized. */
8011 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8012 return 0;
8013
8014 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8015 #ifndef MAC_OS8
8016 #ifdef HAVE_WINDOW_SYSTEM
8017 /* When Emacs starts, selected_frame may be a visible terminal
8018 frame, even if we run under a window system. If we let this
8019 through, a message would be displayed on the terminal. */
8020 if (EQ (selected_frame, Vterminal_frame)
8021 && !NILP (Vwindow_system))
8022 return 0;
8023 #endif /* HAVE_WINDOW_SYSTEM */
8024 #endif
8025
8026 /* Redraw garbaged frames. */
8027 if (frame_garbaged)
8028 clear_garbaged_frames ();
8029
8030 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8031 {
8032 echo_area_window = mini_window;
8033 window_height_changed_p = display_echo_area (w);
8034 w->must_be_updated_p = 1;
8035
8036 /* Update the display, unless called from redisplay_internal.
8037 Also don't update the screen during redisplay itself. The
8038 update will happen at the end of redisplay, and an update
8039 here could cause confusion. */
8040 if (update_frame_p && !redisplaying_p)
8041 {
8042 int n = 0;
8043
8044 /* If the display update has been interrupted by pending
8045 input, update mode lines in the frame. Due to the
8046 pending input, it might have been that redisplay hasn't
8047 been called, so that mode lines above the echo area are
8048 garbaged. This looks odd, so we prevent it here. */
8049 if (!display_completed)
8050 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8051
8052 if (window_height_changed_p
8053 /* Don't do this if Emacs is shutting down. Redisplay
8054 needs to run hooks. */
8055 && !NILP (Vrun_hooks))
8056 {
8057 /* Must update other windows. Likewise as in other
8058 cases, don't let this update be interrupted by
8059 pending input. */
8060 int count = SPECPDL_INDEX ();
8061 specbind (Qredisplay_dont_pause, Qt);
8062 windows_or_buffers_changed = 1;
8063 redisplay_internal (0);
8064 unbind_to (count, Qnil);
8065 }
8066 else if (FRAME_WINDOW_P (f) && n == 0)
8067 {
8068 /* Window configuration is the same as before.
8069 Can do with a display update of the echo area,
8070 unless we displayed some mode lines. */
8071 update_single_window (w, 1);
8072 rif->flush_display (f);
8073 }
8074 else
8075 update_frame (f, 1, 1);
8076
8077 /* If cursor is in the echo area, make sure that the next
8078 redisplay displays the minibuffer, so that the cursor will
8079 be replaced with what the minibuffer wants. */
8080 if (cursor_in_echo_area)
8081 ++windows_or_buffers_changed;
8082 }
8083 }
8084 else if (!EQ (mini_window, selected_window))
8085 windows_or_buffers_changed++;
8086
8087 /* Last displayed message is now the current message. */
8088 echo_area_buffer[1] = echo_area_buffer[0];
8089 /* Inform read_char that we're not echoing. */
8090 echo_message_buffer = Qnil;
8091
8092 /* Prevent redisplay optimization in redisplay_internal by resetting
8093 this_line_start_pos. This is done because the mini-buffer now
8094 displays the message instead of its buffer text. */
8095 if (EQ (mini_window, selected_window))
8096 CHARPOS (this_line_start_pos) = 0;
8097
8098 return window_height_changed_p;
8099 }
8100
8101
8102 \f
8103 /***********************************************************************
8104 Frame Titles
8105 ***********************************************************************/
8106
8107
8108 /* The frame title buffering code is also used by Fformat_mode_line.
8109 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8110
8111 /* A buffer for constructing frame titles in it; allocated from the
8112 heap in init_xdisp and resized as needed in store_frame_title_char. */
8113
8114 static char *frame_title_buf;
8115
8116 /* The buffer's end, and a current output position in it. */
8117
8118 static char *frame_title_buf_end;
8119 static char *frame_title_ptr;
8120
8121
8122 /* Store a single character C for the frame title in frame_title_buf.
8123 Re-allocate frame_title_buf if necessary. */
8124
8125 static void
8126 #ifdef PROTOTYPES
8127 store_frame_title_char (char c)
8128 #else
8129 store_frame_title_char (c)
8130 char c;
8131 #endif
8132 {
8133 /* If output position has reached the end of the allocated buffer,
8134 double the buffer's size. */
8135 if (frame_title_ptr == frame_title_buf_end)
8136 {
8137 int len = frame_title_ptr - frame_title_buf;
8138 int new_size = 2 * len * sizeof *frame_title_buf;
8139 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8140 frame_title_buf_end = frame_title_buf + new_size;
8141 frame_title_ptr = frame_title_buf + len;
8142 }
8143
8144 *frame_title_ptr++ = c;
8145 }
8146
8147
8148 /* Store part of a frame title in frame_title_buf, beginning at
8149 frame_title_ptr. STR is the string to store. Do not copy
8150 characters that yield more columns than PRECISION; PRECISION <= 0
8151 means copy the whole string. Pad with spaces until FIELD_WIDTH
8152 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8153 pad. Called from display_mode_element when it is used to build a
8154 frame title. */
8155
8156 static int
8157 store_frame_title (str, field_width, precision)
8158 const unsigned char *str;
8159 int field_width, precision;
8160 {
8161 int n = 0;
8162 int dummy, nbytes;
8163
8164 /* Copy at most PRECISION chars from STR. */
8165 nbytes = strlen (str);
8166 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8167 while (nbytes--)
8168 store_frame_title_char (*str++);
8169
8170 /* Fill up with spaces until FIELD_WIDTH reached. */
8171 while (field_width > 0
8172 && n < field_width)
8173 {
8174 store_frame_title_char (' ');
8175 ++n;
8176 }
8177
8178 return n;
8179 }
8180
8181 #ifdef HAVE_WINDOW_SYSTEM
8182
8183 /* Set the title of FRAME, if it has changed. The title format is
8184 Vicon_title_format if FRAME is iconified, otherwise it is
8185 frame_title_format. */
8186
8187 static void
8188 x_consider_frame_title (frame)
8189 Lisp_Object frame;
8190 {
8191 struct frame *f = XFRAME (frame);
8192
8193 if (FRAME_WINDOW_P (f)
8194 || FRAME_MINIBUF_ONLY_P (f)
8195 || f->explicit_name)
8196 {
8197 /* Do we have more than one visible frame on this X display? */
8198 Lisp_Object tail;
8199 Lisp_Object fmt;
8200 struct buffer *obuf;
8201 int len;
8202 struct it it;
8203
8204 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8205 {
8206 Lisp_Object other_frame = XCAR (tail);
8207 struct frame *tf = XFRAME (other_frame);
8208
8209 if (tf != f
8210 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8211 && !FRAME_MINIBUF_ONLY_P (tf)
8212 && !EQ (other_frame, tip_frame)
8213 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8214 break;
8215 }
8216
8217 /* Set global variable indicating that multiple frames exist. */
8218 multiple_frames = CONSP (tail);
8219
8220 /* Switch to the buffer of selected window of the frame. Set up
8221 frame_title_ptr so that display_mode_element will output into it;
8222 then display the title. */
8223 obuf = current_buffer;
8224 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8225 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8226 frame_title_ptr = frame_title_buf;
8227 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8228 NULL, DEFAULT_FACE_ID);
8229 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8230 len = frame_title_ptr - frame_title_buf;
8231 frame_title_ptr = NULL;
8232 set_buffer_internal_1 (obuf);
8233
8234 /* Set the title only if it's changed. This avoids consing in
8235 the common case where it hasn't. (If it turns out that we've
8236 already wasted too much time by walking through the list with
8237 display_mode_element, then we might need to optimize at a
8238 higher level than this.) */
8239 if (! STRINGP (f->name)
8240 || SBYTES (f->name) != len
8241 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8242 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8243 }
8244 }
8245
8246 #endif /* not HAVE_WINDOW_SYSTEM */
8247
8248
8249
8250 \f
8251 /***********************************************************************
8252 Menu Bars
8253 ***********************************************************************/
8254
8255
8256 /* Prepare for redisplay by updating menu-bar item lists when
8257 appropriate. This can call eval. */
8258
8259 void
8260 prepare_menu_bars ()
8261 {
8262 int all_windows;
8263 struct gcpro gcpro1, gcpro2;
8264 struct frame *f;
8265 Lisp_Object tooltip_frame;
8266
8267 #ifdef HAVE_WINDOW_SYSTEM
8268 tooltip_frame = tip_frame;
8269 #else
8270 tooltip_frame = Qnil;
8271 #endif
8272
8273 /* Update all frame titles based on their buffer names, etc. We do
8274 this before the menu bars so that the buffer-menu will show the
8275 up-to-date frame titles. */
8276 #ifdef HAVE_WINDOW_SYSTEM
8277 if (windows_or_buffers_changed || update_mode_lines)
8278 {
8279 Lisp_Object tail, frame;
8280
8281 FOR_EACH_FRAME (tail, frame)
8282 {
8283 f = XFRAME (frame);
8284 if (!EQ (frame, tooltip_frame)
8285 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8286 x_consider_frame_title (frame);
8287 }
8288 }
8289 #endif /* HAVE_WINDOW_SYSTEM */
8290
8291 /* Update the menu bar item lists, if appropriate. This has to be
8292 done before any actual redisplay or generation of display lines. */
8293 all_windows = (update_mode_lines
8294 || buffer_shared > 1
8295 || windows_or_buffers_changed);
8296 if (all_windows)
8297 {
8298 Lisp_Object tail, frame;
8299 int count = SPECPDL_INDEX ();
8300
8301 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8302
8303 FOR_EACH_FRAME (tail, frame)
8304 {
8305 f = XFRAME (frame);
8306
8307 /* Ignore tooltip frame. */
8308 if (EQ (frame, tooltip_frame))
8309 continue;
8310
8311 /* If a window on this frame changed size, report that to
8312 the user and clear the size-change flag. */
8313 if (FRAME_WINDOW_SIZES_CHANGED (f))
8314 {
8315 Lisp_Object functions;
8316
8317 /* Clear flag first in case we get an error below. */
8318 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8319 functions = Vwindow_size_change_functions;
8320 GCPRO2 (tail, functions);
8321
8322 while (CONSP (functions))
8323 {
8324 call1 (XCAR (functions), frame);
8325 functions = XCDR (functions);
8326 }
8327 UNGCPRO;
8328 }
8329
8330 GCPRO1 (tail);
8331 update_menu_bar (f, 0);
8332 #ifdef HAVE_WINDOW_SYSTEM
8333 update_tool_bar (f, 0);
8334 #endif
8335 UNGCPRO;
8336 }
8337
8338 unbind_to (count, Qnil);
8339 }
8340 else
8341 {
8342 struct frame *sf = SELECTED_FRAME ();
8343 update_menu_bar (sf, 1);
8344 #ifdef HAVE_WINDOW_SYSTEM
8345 update_tool_bar (sf, 1);
8346 #endif
8347 }
8348
8349 /* Motif needs this. See comment in xmenu.c. Turn it off when
8350 pending_menu_activation is not defined. */
8351 #ifdef USE_X_TOOLKIT
8352 pending_menu_activation = 0;
8353 #endif
8354 }
8355
8356
8357 /* Update the menu bar item list for frame F. This has to be done
8358 before we start to fill in any display lines, because it can call
8359 eval.
8360
8361 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8362
8363 static void
8364 update_menu_bar (f, save_match_data)
8365 struct frame *f;
8366 int save_match_data;
8367 {
8368 Lisp_Object window;
8369 register struct window *w;
8370
8371 /* If called recursively during a menu update, do nothing. This can
8372 happen when, for instance, an activate-menubar-hook causes a
8373 redisplay. */
8374 if (inhibit_menubar_update)
8375 return;
8376
8377 window = FRAME_SELECTED_WINDOW (f);
8378 w = XWINDOW (window);
8379
8380 #if 0 /* The if statement below this if statement used to include the
8381 condition !NILP (w->update_mode_line), rather than using
8382 update_mode_lines directly, and this if statement may have
8383 been added to make that condition work. Now the if
8384 statement below matches its comment, this isn't needed. */
8385 if (update_mode_lines)
8386 w->update_mode_line = Qt;
8387 #endif
8388
8389 if (FRAME_WINDOW_P (f)
8390 ?
8391 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8392 || defined (USE_GTK)
8393 FRAME_EXTERNAL_MENU_BAR (f)
8394 #else
8395 FRAME_MENU_BAR_LINES (f) > 0
8396 #endif
8397 : FRAME_MENU_BAR_LINES (f) > 0)
8398 {
8399 /* If the user has switched buffers or windows, we need to
8400 recompute to reflect the new bindings. But we'll
8401 recompute when update_mode_lines is set too; that means
8402 that people can use force-mode-line-update to request
8403 that the menu bar be recomputed. The adverse effect on
8404 the rest of the redisplay algorithm is about the same as
8405 windows_or_buffers_changed anyway. */
8406 if (windows_or_buffers_changed
8407 /* This used to test w->update_mode_line, but we believe
8408 there is no need to recompute the menu in that case. */
8409 || update_mode_lines
8410 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8411 < BUF_MODIFF (XBUFFER (w->buffer)))
8412 != !NILP (w->last_had_star))
8413 || ((!NILP (Vtransient_mark_mode)
8414 && !NILP (XBUFFER (w->buffer)->mark_active))
8415 != !NILP (w->region_showing)))
8416 {
8417 struct buffer *prev = current_buffer;
8418 int count = SPECPDL_INDEX ();
8419
8420 specbind (Qinhibit_menubar_update, Qt);
8421
8422 set_buffer_internal_1 (XBUFFER (w->buffer));
8423 if (save_match_data)
8424 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8425 if (NILP (Voverriding_local_map_menu_flag))
8426 {
8427 specbind (Qoverriding_terminal_local_map, Qnil);
8428 specbind (Qoverriding_local_map, Qnil);
8429 }
8430
8431 /* Run the Lucid hook. */
8432 safe_run_hooks (Qactivate_menubar_hook);
8433
8434 /* If it has changed current-menubar from previous value,
8435 really recompute the menu-bar from the value. */
8436 if (! NILP (Vlucid_menu_bar_dirty_flag))
8437 call0 (Qrecompute_lucid_menubar);
8438
8439 safe_run_hooks (Qmenu_bar_update_hook);
8440 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8441
8442 /* Redisplay the menu bar in case we changed it. */
8443 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8444 || defined (USE_GTK)
8445 if (FRAME_WINDOW_P (f)
8446 #if defined (MAC_OS)
8447 /* All frames on Mac OS share the same menubar. So only the
8448 selected frame should be allowed to set it. */
8449 && f == SELECTED_FRAME ()
8450 #endif
8451 )
8452 set_frame_menubar (f, 0, 0);
8453 else
8454 /* On a terminal screen, the menu bar is an ordinary screen
8455 line, and this makes it get updated. */
8456 w->update_mode_line = Qt;
8457 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8458 /* In the non-toolkit version, the menu bar is an ordinary screen
8459 line, and this makes it get updated. */
8460 w->update_mode_line = Qt;
8461 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8462
8463 unbind_to (count, Qnil);
8464 set_buffer_internal_1 (prev);
8465 }
8466 }
8467 }
8468
8469
8470 \f
8471 /***********************************************************************
8472 Output Cursor
8473 ***********************************************************************/
8474
8475 #ifdef HAVE_WINDOW_SYSTEM
8476
8477 /* EXPORT:
8478 Nominal cursor position -- where to draw output.
8479 HPOS and VPOS are window relative glyph matrix coordinates.
8480 X and Y are window relative pixel coordinates. */
8481
8482 struct cursor_pos output_cursor;
8483
8484
8485 /* EXPORT:
8486 Set the global variable output_cursor to CURSOR. All cursor
8487 positions are relative to updated_window. */
8488
8489 void
8490 set_output_cursor (cursor)
8491 struct cursor_pos *cursor;
8492 {
8493 output_cursor.hpos = cursor->hpos;
8494 output_cursor.vpos = cursor->vpos;
8495 output_cursor.x = cursor->x;
8496 output_cursor.y = cursor->y;
8497 }
8498
8499
8500 /* EXPORT for RIF:
8501 Set a nominal cursor position.
8502
8503 HPOS and VPOS are column/row positions in a window glyph matrix. X
8504 and Y are window text area relative pixel positions.
8505
8506 If this is done during an update, updated_window will contain the
8507 window that is being updated and the position is the future output
8508 cursor position for that window. If updated_window is null, use
8509 selected_window and display the cursor at the given position. */
8510
8511 void
8512 x_cursor_to (vpos, hpos, y, x)
8513 int vpos, hpos, y, x;
8514 {
8515 struct window *w;
8516
8517 /* If updated_window is not set, work on selected_window. */
8518 if (updated_window)
8519 w = updated_window;
8520 else
8521 w = XWINDOW (selected_window);
8522
8523 /* Set the output cursor. */
8524 output_cursor.hpos = hpos;
8525 output_cursor.vpos = vpos;
8526 output_cursor.x = x;
8527 output_cursor.y = y;
8528
8529 /* If not called as part of an update, really display the cursor.
8530 This will also set the cursor position of W. */
8531 if (updated_window == NULL)
8532 {
8533 BLOCK_INPUT;
8534 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8535 if (rif->flush_display_optional)
8536 rif->flush_display_optional (SELECTED_FRAME ());
8537 UNBLOCK_INPUT;
8538 }
8539 }
8540
8541 #endif /* HAVE_WINDOW_SYSTEM */
8542
8543 \f
8544 /***********************************************************************
8545 Tool-bars
8546 ***********************************************************************/
8547
8548 #ifdef HAVE_WINDOW_SYSTEM
8549
8550 /* Where the mouse was last time we reported a mouse event. */
8551
8552 FRAME_PTR last_mouse_frame;
8553
8554 /* Tool-bar item index of the item on which a mouse button was pressed
8555 or -1. */
8556
8557 int last_tool_bar_item;
8558
8559
8560 /* Update the tool-bar item list for frame F. This has to be done
8561 before we start to fill in any display lines. Called from
8562 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8563 and restore it here. */
8564
8565 static void
8566 update_tool_bar (f, save_match_data)
8567 struct frame *f;
8568 int save_match_data;
8569 {
8570 #ifdef USE_GTK
8571 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8572 #else
8573 int do_update = WINDOWP (f->tool_bar_window)
8574 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8575 #endif
8576
8577 if (do_update)
8578 {
8579 Lisp_Object window;
8580 struct window *w;
8581
8582 window = FRAME_SELECTED_WINDOW (f);
8583 w = XWINDOW (window);
8584
8585 /* If the user has switched buffers or windows, we need to
8586 recompute to reflect the new bindings. But we'll
8587 recompute when update_mode_lines is set too; that means
8588 that people can use force-mode-line-update to request
8589 that the menu bar be recomputed. The adverse effect on
8590 the rest of the redisplay algorithm is about the same as
8591 windows_or_buffers_changed anyway. */
8592 if (windows_or_buffers_changed
8593 || !NILP (w->update_mode_line)
8594 || update_mode_lines
8595 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8596 < BUF_MODIFF (XBUFFER (w->buffer)))
8597 != !NILP (w->last_had_star))
8598 || ((!NILP (Vtransient_mark_mode)
8599 && !NILP (XBUFFER (w->buffer)->mark_active))
8600 != !NILP (w->region_showing)))
8601 {
8602 struct buffer *prev = current_buffer;
8603 int count = SPECPDL_INDEX ();
8604 Lisp_Object new_tool_bar;
8605 int new_n_tool_bar;
8606 struct gcpro gcpro1;
8607
8608 /* Set current_buffer to the buffer of the selected
8609 window of the frame, so that we get the right local
8610 keymaps. */
8611 set_buffer_internal_1 (XBUFFER (w->buffer));
8612
8613 /* Save match data, if we must. */
8614 if (save_match_data)
8615 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8616
8617 /* Make sure that we don't accidentally use bogus keymaps. */
8618 if (NILP (Voverriding_local_map_menu_flag))
8619 {
8620 specbind (Qoverriding_terminal_local_map, Qnil);
8621 specbind (Qoverriding_local_map, Qnil);
8622 }
8623
8624 GCPRO1 (new_tool_bar);
8625
8626 /* Build desired tool-bar items from keymaps. */
8627 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8628 &new_n_tool_bar);
8629
8630 /* Redisplay the tool-bar if we changed it. */
8631 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8632 {
8633 /* Redisplay that happens asynchronously due to an expose event
8634 may access f->tool_bar_items. Make sure we update both
8635 variables within BLOCK_INPUT so no such event interrupts. */
8636 BLOCK_INPUT;
8637 f->tool_bar_items = new_tool_bar;
8638 f->n_tool_bar_items = new_n_tool_bar;
8639 w->update_mode_line = Qt;
8640 UNBLOCK_INPUT;
8641 }
8642
8643 UNGCPRO;
8644
8645 unbind_to (count, Qnil);
8646 set_buffer_internal_1 (prev);
8647 }
8648 }
8649 }
8650
8651
8652 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8653 F's desired tool-bar contents. F->tool_bar_items must have
8654 been set up previously by calling prepare_menu_bars. */
8655
8656 static void
8657 build_desired_tool_bar_string (f)
8658 struct frame *f;
8659 {
8660 int i, size, size_needed;
8661 struct gcpro gcpro1, gcpro2, gcpro3;
8662 Lisp_Object image, plist, props;
8663
8664 image = plist = props = Qnil;
8665 GCPRO3 (image, plist, props);
8666
8667 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8668 Otherwise, make a new string. */
8669
8670 /* The size of the string we might be able to reuse. */
8671 size = (STRINGP (f->desired_tool_bar_string)
8672 ? SCHARS (f->desired_tool_bar_string)
8673 : 0);
8674
8675 /* We need one space in the string for each image. */
8676 size_needed = f->n_tool_bar_items;
8677
8678 /* Reuse f->desired_tool_bar_string, if possible. */
8679 if (size < size_needed || NILP (f->desired_tool_bar_string))
8680 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8681 make_number (' '));
8682 else
8683 {
8684 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8685 Fremove_text_properties (make_number (0), make_number (size),
8686 props, f->desired_tool_bar_string);
8687 }
8688
8689 /* Put a `display' property on the string for the images to display,
8690 put a `menu_item' property on tool-bar items with a value that
8691 is the index of the item in F's tool-bar item vector. */
8692 for (i = 0; i < f->n_tool_bar_items; ++i)
8693 {
8694 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8695
8696 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8697 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8698 int hmargin, vmargin, relief, idx, end;
8699 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8700
8701 /* If image is a vector, choose the image according to the
8702 button state. */
8703 image = PROP (TOOL_BAR_ITEM_IMAGES);
8704 if (VECTORP (image))
8705 {
8706 if (enabled_p)
8707 idx = (selected_p
8708 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8709 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8710 else
8711 idx = (selected_p
8712 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8713 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8714
8715 xassert (ASIZE (image) >= idx);
8716 image = AREF (image, idx);
8717 }
8718 else
8719 idx = -1;
8720
8721 /* Ignore invalid image specifications. */
8722 if (!valid_image_p (image))
8723 continue;
8724
8725 /* Display the tool-bar button pressed, or depressed. */
8726 plist = Fcopy_sequence (XCDR (image));
8727
8728 /* Compute margin and relief to draw. */
8729 relief = (tool_bar_button_relief >= 0
8730 ? tool_bar_button_relief
8731 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8732 hmargin = vmargin = relief;
8733
8734 if (INTEGERP (Vtool_bar_button_margin)
8735 && XINT (Vtool_bar_button_margin) > 0)
8736 {
8737 hmargin += XFASTINT (Vtool_bar_button_margin);
8738 vmargin += XFASTINT (Vtool_bar_button_margin);
8739 }
8740 else if (CONSP (Vtool_bar_button_margin))
8741 {
8742 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8743 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8744 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8745
8746 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8747 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8748 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8749 }
8750
8751 if (auto_raise_tool_bar_buttons_p)
8752 {
8753 /* Add a `:relief' property to the image spec if the item is
8754 selected. */
8755 if (selected_p)
8756 {
8757 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8758 hmargin -= relief;
8759 vmargin -= relief;
8760 }
8761 }
8762 else
8763 {
8764 /* If image is selected, display it pressed, i.e. with a
8765 negative relief. If it's not selected, display it with a
8766 raised relief. */
8767 plist = Fplist_put (plist, QCrelief,
8768 (selected_p
8769 ? make_number (-relief)
8770 : make_number (relief)));
8771 hmargin -= relief;
8772 vmargin -= relief;
8773 }
8774
8775 /* Put a margin around the image. */
8776 if (hmargin || vmargin)
8777 {
8778 if (hmargin == vmargin)
8779 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8780 else
8781 plist = Fplist_put (plist, QCmargin,
8782 Fcons (make_number (hmargin),
8783 make_number (vmargin)));
8784 }
8785
8786 /* If button is not enabled, and we don't have special images
8787 for the disabled state, make the image appear disabled by
8788 applying an appropriate algorithm to it. */
8789 if (!enabled_p && idx < 0)
8790 plist = Fplist_put (plist, QCconversion, Qdisabled);
8791
8792 /* Put a `display' text property on the string for the image to
8793 display. Put a `menu-item' property on the string that gives
8794 the start of this item's properties in the tool-bar items
8795 vector. */
8796 image = Fcons (Qimage, plist);
8797 props = list4 (Qdisplay, image,
8798 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8799
8800 /* Let the last image hide all remaining spaces in the tool bar
8801 string. The string can be longer than needed when we reuse a
8802 previous string. */
8803 if (i + 1 == f->n_tool_bar_items)
8804 end = SCHARS (f->desired_tool_bar_string);
8805 else
8806 end = i + 1;
8807 Fadd_text_properties (make_number (i), make_number (end),
8808 props, f->desired_tool_bar_string);
8809 #undef PROP
8810 }
8811
8812 UNGCPRO;
8813 }
8814
8815
8816 /* Display one line of the tool-bar of frame IT->f. */
8817
8818 static void
8819 display_tool_bar_line (it)
8820 struct it *it;
8821 {
8822 struct glyph_row *row = it->glyph_row;
8823 int max_x = it->last_visible_x;
8824 struct glyph *last;
8825
8826 prepare_desired_row (row);
8827 row->y = it->current_y;
8828
8829 /* Note that this isn't made use of if the face hasn't a box,
8830 so there's no need to check the face here. */
8831 it->start_of_box_run_p = 1;
8832
8833 while (it->current_x < max_x)
8834 {
8835 int x_before, x, n_glyphs_before, i, nglyphs;
8836
8837 /* Get the next display element. */
8838 if (!get_next_display_element (it))
8839 break;
8840
8841 /* Produce glyphs. */
8842 x_before = it->current_x;
8843 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8844 PRODUCE_GLYPHS (it);
8845
8846 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8847 i = 0;
8848 x = x_before;
8849 while (i < nglyphs)
8850 {
8851 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8852
8853 if (x + glyph->pixel_width > max_x)
8854 {
8855 /* Glyph doesn't fit on line. */
8856 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8857 it->current_x = x;
8858 goto out;
8859 }
8860
8861 ++it->hpos;
8862 x += glyph->pixel_width;
8863 ++i;
8864 }
8865
8866 /* Stop at line ends. */
8867 if (ITERATOR_AT_END_OF_LINE_P (it))
8868 break;
8869
8870 set_iterator_to_next (it, 1);
8871 }
8872
8873 out:;
8874
8875 row->displays_text_p = row->used[TEXT_AREA] != 0;
8876 extend_face_to_end_of_line (it);
8877 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8878 last->right_box_line_p = 1;
8879 if (last == row->glyphs[TEXT_AREA])
8880 last->left_box_line_p = 1;
8881 compute_line_metrics (it);
8882
8883 /* If line is empty, make it occupy the rest of the tool-bar. */
8884 if (!row->displays_text_p)
8885 {
8886 row->height = row->phys_height = it->last_visible_y - row->y;
8887 row->ascent = row->phys_ascent = 0;
8888 row->extra_line_spacing = 0;
8889 }
8890
8891 row->full_width_p = 1;
8892 row->continued_p = 0;
8893 row->truncated_on_left_p = 0;
8894 row->truncated_on_right_p = 0;
8895
8896 it->current_x = it->hpos = 0;
8897 it->current_y += row->height;
8898 ++it->vpos;
8899 ++it->glyph_row;
8900 }
8901
8902
8903 /* Value is the number of screen lines needed to make all tool-bar
8904 items of frame F visible. */
8905
8906 static int
8907 tool_bar_lines_needed (f)
8908 struct frame *f;
8909 {
8910 struct window *w = XWINDOW (f->tool_bar_window);
8911 struct it it;
8912
8913 /* Initialize an iterator for iteration over
8914 F->desired_tool_bar_string in the tool-bar window of frame F. */
8915 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8916 it.first_visible_x = 0;
8917 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8918 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8919
8920 while (!ITERATOR_AT_END_P (&it))
8921 {
8922 it.glyph_row = w->desired_matrix->rows;
8923 clear_glyph_row (it.glyph_row);
8924 display_tool_bar_line (&it);
8925 }
8926
8927 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8928 }
8929
8930
8931 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8932 0, 1, 0,
8933 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8934 (frame)
8935 Lisp_Object frame;
8936 {
8937 struct frame *f;
8938 struct window *w;
8939 int nlines = 0;
8940
8941 if (NILP (frame))
8942 frame = selected_frame;
8943 else
8944 CHECK_FRAME (frame);
8945 f = XFRAME (frame);
8946
8947 if (WINDOWP (f->tool_bar_window)
8948 || (w = XWINDOW (f->tool_bar_window),
8949 WINDOW_TOTAL_LINES (w) > 0))
8950 {
8951 update_tool_bar (f, 1);
8952 if (f->n_tool_bar_items)
8953 {
8954 build_desired_tool_bar_string (f);
8955 nlines = tool_bar_lines_needed (f);
8956 }
8957 }
8958
8959 return make_number (nlines);
8960 }
8961
8962
8963 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8964 height should be changed. */
8965
8966 static int
8967 redisplay_tool_bar (f)
8968 struct frame *f;
8969 {
8970 struct window *w;
8971 struct it it;
8972 struct glyph_row *row;
8973 int change_height_p = 0;
8974
8975 #ifdef USE_GTK
8976 if (FRAME_EXTERNAL_TOOL_BAR (f))
8977 update_frame_tool_bar (f);
8978 return 0;
8979 #endif
8980
8981 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8982 do anything. This means you must start with tool-bar-lines
8983 non-zero to get the auto-sizing effect. Or in other words, you
8984 can turn off tool-bars by specifying tool-bar-lines zero. */
8985 if (!WINDOWP (f->tool_bar_window)
8986 || (w = XWINDOW (f->tool_bar_window),
8987 WINDOW_TOTAL_LINES (w) == 0))
8988 return 0;
8989
8990 /* Set up an iterator for the tool-bar window. */
8991 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8992 it.first_visible_x = 0;
8993 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8994 row = it.glyph_row;
8995
8996 /* Build a string that represents the contents of the tool-bar. */
8997 build_desired_tool_bar_string (f);
8998 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8999
9000 /* Display as many lines as needed to display all tool-bar items. */
9001 while (it.current_y < it.last_visible_y)
9002 display_tool_bar_line (&it);
9003
9004 /* It doesn't make much sense to try scrolling in the tool-bar
9005 window, so don't do it. */
9006 w->desired_matrix->no_scrolling_p = 1;
9007 w->must_be_updated_p = 1;
9008
9009 if (auto_resize_tool_bars_p)
9010 {
9011 int nlines;
9012
9013 /* If we couldn't display everything, change the tool-bar's
9014 height. */
9015 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9016 change_height_p = 1;
9017
9018 /* If there are blank lines at the end, except for a partially
9019 visible blank line at the end that is smaller than
9020 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9021 row = it.glyph_row - 1;
9022 if (!row->displays_text_p
9023 && row->height >= FRAME_LINE_HEIGHT (f))
9024 change_height_p = 1;
9025
9026 /* If row displays tool-bar items, but is partially visible,
9027 change the tool-bar's height. */
9028 if (row->displays_text_p
9029 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9030 change_height_p = 1;
9031
9032 /* Resize windows as needed by changing the `tool-bar-lines'
9033 frame parameter. */
9034 if (change_height_p
9035 && (nlines = tool_bar_lines_needed (f),
9036 nlines != WINDOW_TOTAL_LINES (w)))
9037 {
9038 extern Lisp_Object Qtool_bar_lines;
9039 Lisp_Object frame;
9040 int old_height = WINDOW_TOTAL_LINES (w);
9041
9042 XSETFRAME (frame, f);
9043 clear_glyph_matrix (w->desired_matrix);
9044 Fmodify_frame_parameters (frame,
9045 Fcons (Fcons (Qtool_bar_lines,
9046 make_number (nlines)),
9047 Qnil));
9048 if (WINDOW_TOTAL_LINES (w) != old_height)
9049 fonts_changed_p = 1;
9050 }
9051 }
9052
9053 return change_height_p;
9054 }
9055
9056
9057 /* Get information about the tool-bar item which is displayed in GLYPH
9058 on frame F. Return in *PROP_IDX the index where tool-bar item
9059 properties start in F->tool_bar_items. Value is zero if
9060 GLYPH doesn't display a tool-bar item. */
9061
9062 static int
9063 tool_bar_item_info (f, glyph, prop_idx)
9064 struct frame *f;
9065 struct glyph *glyph;
9066 int *prop_idx;
9067 {
9068 Lisp_Object prop;
9069 int success_p;
9070 int charpos;
9071
9072 /* This function can be called asynchronously, which means we must
9073 exclude any possibility that Fget_text_property signals an
9074 error. */
9075 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9076 charpos = max (0, charpos);
9077
9078 /* Get the text property `menu-item' at pos. The value of that
9079 property is the start index of this item's properties in
9080 F->tool_bar_items. */
9081 prop = Fget_text_property (make_number (charpos),
9082 Qmenu_item, f->current_tool_bar_string);
9083 if (INTEGERP (prop))
9084 {
9085 *prop_idx = XINT (prop);
9086 success_p = 1;
9087 }
9088 else
9089 success_p = 0;
9090
9091 return success_p;
9092 }
9093
9094 \f
9095 /* Get information about the tool-bar item at position X/Y on frame F.
9096 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9097 the current matrix of the tool-bar window of F, or NULL if not
9098 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9099 item in F->tool_bar_items. Value is
9100
9101 -1 if X/Y is not on a tool-bar item
9102 0 if X/Y is on the same item that was highlighted before.
9103 1 otherwise. */
9104
9105 static int
9106 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9107 struct frame *f;
9108 int x, y;
9109 struct glyph **glyph;
9110 int *hpos, *vpos, *prop_idx;
9111 {
9112 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9113 struct window *w = XWINDOW (f->tool_bar_window);
9114 int area;
9115
9116 /* Find the glyph under X/Y. */
9117 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9118 if (*glyph == NULL)
9119 return -1;
9120
9121 /* Get the start of this tool-bar item's properties in
9122 f->tool_bar_items. */
9123 if (!tool_bar_item_info (f, *glyph, prop_idx))
9124 return -1;
9125
9126 /* Is mouse on the highlighted item? */
9127 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9128 && *vpos >= dpyinfo->mouse_face_beg_row
9129 && *vpos <= dpyinfo->mouse_face_end_row
9130 && (*vpos > dpyinfo->mouse_face_beg_row
9131 || *hpos >= dpyinfo->mouse_face_beg_col)
9132 && (*vpos < dpyinfo->mouse_face_end_row
9133 || *hpos < dpyinfo->mouse_face_end_col
9134 || dpyinfo->mouse_face_past_end))
9135 return 0;
9136
9137 return 1;
9138 }
9139
9140
9141 /* EXPORT:
9142 Handle mouse button event on the tool-bar of frame F, at
9143 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9144 0 for button release. MODIFIERS is event modifiers for button
9145 release. */
9146
9147 void
9148 handle_tool_bar_click (f, x, y, down_p, modifiers)
9149 struct frame *f;
9150 int x, y, down_p;
9151 unsigned int modifiers;
9152 {
9153 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9154 struct window *w = XWINDOW (f->tool_bar_window);
9155 int hpos, vpos, prop_idx;
9156 struct glyph *glyph;
9157 Lisp_Object enabled_p;
9158
9159 /* If not on the highlighted tool-bar item, return. */
9160 frame_to_window_pixel_xy (w, &x, &y);
9161 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9162 return;
9163
9164 /* If item is disabled, do nothing. */
9165 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9166 if (NILP (enabled_p))
9167 return;
9168
9169 if (down_p)
9170 {
9171 /* Show item in pressed state. */
9172 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9173 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9174 last_tool_bar_item = prop_idx;
9175 }
9176 else
9177 {
9178 Lisp_Object key, frame;
9179 struct input_event event;
9180 EVENT_INIT (event);
9181
9182 /* Show item in released state. */
9183 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9184 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9185
9186 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9187
9188 XSETFRAME (frame, f);
9189 event.kind = TOOL_BAR_EVENT;
9190 event.frame_or_window = frame;
9191 event.arg = frame;
9192 kbd_buffer_store_event (&event);
9193
9194 event.kind = TOOL_BAR_EVENT;
9195 event.frame_or_window = frame;
9196 event.arg = key;
9197 event.modifiers = modifiers;
9198 kbd_buffer_store_event (&event);
9199 last_tool_bar_item = -1;
9200 }
9201 }
9202
9203
9204 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9205 tool-bar window-relative coordinates X/Y. Called from
9206 note_mouse_highlight. */
9207
9208 static void
9209 note_tool_bar_highlight (f, x, y)
9210 struct frame *f;
9211 int x, y;
9212 {
9213 Lisp_Object window = f->tool_bar_window;
9214 struct window *w = XWINDOW (window);
9215 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9216 int hpos, vpos;
9217 struct glyph *glyph;
9218 struct glyph_row *row;
9219 int i;
9220 Lisp_Object enabled_p;
9221 int prop_idx;
9222 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9223 int mouse_down_p, rc;
9224
9225 /* Function note_mouse_highlight is called with negative x(y
9226 values when mouse moves outside of the frame. */
9227 if (x <= 0 || y <= 0)
9228 {
9229 clear_mouse_face (dpyinfo);
9230 return;
9231 }
9232
9233 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9234 if (rc < 0)
9235 {
9236 /* Not on tool-bar item. */
9237 clear_mouse_face (dpyinfo);
9238 return;
9239 }
9240 else if (rc == 0)
9241 /* On same tool-bar item as before. */
9242 goto set_help_echo;
9243
9244 clear_mouse_face (dpyinfo);
9245
9246 /* Mouse is down, but on different tool-bar item? */
9247 mouse_down_p = (dpyinfo->grabbed
9248 && f == last_mouse_frame
9249 && FRAME_LIVE_P (f));
9250 if (mouse_down_p
9251 && last_tool_bar_item != prop_idx)
9252 return;
9253
9254 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9255 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9256
9257 /* If tool-bar item is not enabled, don't highlight it. */
9258 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9259 if (!NILP (enabled_p))
9260 {
9261 /* Compute the x-position of the glyph. In front and past the
9262 image is a space. We include this in the highlighted area. */
9263 row = MATRIX_ROW (w->current_matrix, vpos);
9264 for (i = x = 0; i < hpos; ++i)
9265 x += row->glyphs[TEXT_AREA][i].pixel_width;
9266
9267 /* Record this as the current active region. */
9268 dpyinfo->mouse_face_beg_col = hpos;
9269 dpyinfo->mouse_face_beg_row = vpos;
9270 dpyinfo->mouse_face_beg_x = x;
9271 dpyinfo->mouse_face_beg_y = row->y;
9272 dpyinfo->mouse_face_past_end = 0;
9273
9274 dpyinfo->mouse_face_end_col = hpos + 1;
9275 dpyinfo->mouse_face_end_row = vpos;
9276 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9277 dpyinfo->mouse_face_end_y = row->y;
9278 dpyinfo->mouse_face_window = window;
9279 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9280
9281 /* Display it as active. */
9282 show_mouse_face (dpyinfo, draw);
9283 dpyinfo->mouse_face_image_state = draw;
9284 }
9285
9286 set_help_echo:
9287
9288 /* Set help_echo_string to a help string to display for this tool-bar item.
9289 XTread_socket does the rest. */
9290 help_echo_object = help_echo_window = Qnil;
9291 help_echo_pos = -1;
9292 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9293 if (NILP (help_echo_string))
9294 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9295 }
9296
9297 #endif /* HAVE_WINDOW_SYSTEM */
9298
9299
9300 \f
9301 /************************************************************************
9302 Horizontal scrolling
9303 ************************************************************************/
9304
9305 static int hscroll_window_tree P_ ((Lisp_Object));
9306 static int hscroll_windows P_ ((Lisp_Object));
9307
9308 /* For all leaf windows in the window tree rooted at WINDOW, set their
9309 hscroll value so that PT is (i) visible in the window, and (ii) so
9310 that it is not within a certain margin at the window's left and
9311 right border. Value is non-zero if any window's hscroll has been
9312 changed. */
9313
9314 static int
9315 hscroll_window_tree (window)
9316 Lisp_Object window;
9317 {
9318 int hscrolled_p = 0;
9319 int hscroll_relative_p = FLOATP (Vhscroll_step);
9320 int hscroll_step_abs = 0;
9321 double hscroll_step_rel = 0;
9322
9323 if (hscroll_relative_p)
9324 {
9325 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9326 if (hscroll_step_rel < 0)
9327 {
9328 hscroll_relative_p = 0;
9329 hscroll_step_abs = 0;
9330 }
9331 }
9332 else if (INTEGERP (Vhscroll_step))
9333 {
9334 hscroll_step_abs = XINT (Vhscroll_step);
9335 if (hscroll_step_abs < 0)
9336 hscroll_step_abs = 0;
9337 }
9338 else
9339 hscroll_step_abs = 0;
9340
9341 while (WINDOWP (window))
9342 {
9343 struct window *w = XWINDOW (window);
9344
9345 if (WINDOWP (w->hchild))
9346 hscrolled_p |= hscroll_window_tree (w->hchild);
9347 else if (WINDOWP (w->vchild))
9348 hscrolled_p |= hscroll_window_tree (w->vchild);
9349 else if (w->cursor.vpos >= 0)
9350 {
9351 int h_margin;
9352 int text_area_width;
9353 struct glyph_row *current_cursor_row
9354 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9355 struct glyph_row *desired_cursor_row
9356 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9357 struct glyph_row *cursor_row
9358 = (desired_cursor_row->enabled_p
9359 ? desired_cursor_row
9360 : current_cursor_row);
9361
9362 text_area_width = window_box_width (w, TEXT_AREA);
9363
9364 /* Scroll when cursor is inside this scroll margin. */
9365 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9366
9367 if ((XFASTINT (w->hscroll)
9368 && w->cursor.x <= h_margin)
9369 || (cursor_row->enabled_p
9370 && cursor_row->truncated_on_right_p
9371 && (w->cursor.x >= text_area_width - h_margin)))
9372 {
9373 struct it it;
9374 int hscroll;
9375 struct buffer *saved_current_buffer;
9376 int pt;
9377 int wanted_x;
9378
9379 /* Find point in a display of infinite width. */
9380 saved_current_buffer = current_buffer;
9381 current_buffer = XBUFFER (w->buffer);
9382
9383 if (w == XWINDOW (selected_window))
9384 pt = BUF_PT (current_buffer);
9385 else
9386 {
9387 pt = marker_position (w->pointm);
9388 pt = max (BEGV, pt);
9389 pt = min (ZV, pt);
9390 }
9391
9392 /* Move iterator to pt starting at cursor_row->start in
9393 a line with infinite width. */
9394 init_to_row_start (&it, w, cursor_row);
9395 it.last_visible_x = INFINITY;
9396 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9397 current_buffer = saved_current_buffer;
9398
9399 /* Position cursor in window. */
9400 if (!hscroll_relative_p && hscroll_step_abs == 0)
9401 hscroll = max (0, (it.current_x
9402 - (ITERATOR_AT_END_OF_LINE_P (&it)
9403 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9404 : (text_area_width / 2))))
9405 / FRAME_COLUMN_WIDTH (it.f);
9406 else if (w->cursor.x >= text_area_width - h_margin)
9407 {
9408 if (hscroll_relative_p)
9409 wanted_x = text_area_width * (1 - hscroll_step_rel)
9410 - h_margin;
9411 else
9412 wanted_x = text_area_width
9413 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9414 - h_margin;
9415 hscroll
9416 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9417 }
9418 else
9419 {
9420 if (hscroll_relative_p)
9421 wanted_x = text_area_width * hscroll_step_rel
9422 + h_margin;
9423 else
9424 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9425 + h_margin;
9426 hscroll
9427 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9428 }
9429 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9430
9431 /* Don't call Fset_window_hscroll if value hasn't
9432 changed because it will prevent redisplay
9433 optimizations. */
9434 if (XFASTINT (w->hscroll) != hscroll)
9435 {
9436 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9437 w->hscroll = make_number (hscroll);
9438 hscrolled_p = 1;
9439 }
9440 }
9441 }
9442
9443 window = w->next;
9444 }
9445
9446 /* Value is non-zero if hscroll of any leaf window has been changed. */
9447 return hscrolled_p;
9448 }
9449
9450
9451 /* Set hscroll so that cursor is visible and not inside horizontal
9452 scroll margins for all windows in the tree rooted at WINDOW. See
9453 also hscroll_window_tree above. Value is non-zero if any window's
9454 hscroll has been changed. If it has, desired matrices on the frame
9455 of WINDOW are cleared. */
9456
9457 static int
9458 hscroll_windows (window)
9459 Lisp_Object window;
9460 {
9461 int hscrolled_p;
9462
9463 if (automatic_hscrolling_p)
9464 {
9465 hscrolled_p = hscroll_window_tree (window);
9466 if (hscrolled_p)
9467 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9468 }
9469 else
9470 hscrolled_p = 0;
9471 return hscrolled_p;
9472 }
9473
9474
9475 \f
9476 /************************************************************************
9477 Redisplay
9478 ************************************************************************/
9479
9480 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9481 to a non-zero value. This is sometimes handy to have in a debugger
9482 session. */
9483
9484 #if GLYPH_DEBUG
9485
9486 /* First and last unchanged row for try_window_id. */
9487
9488 int debug_first_unchanged_at_end_vpos;
9489 int debug_last_unchanged_at_beg_vpos;
9490
9491 /* Delta vpos and y. */
9492
9493 int debug_dvpos, debug_dy;
9494
9495 /* Delta in characters and bytes for try_window_id. */
9496
9497 int debug_delta, debug_delta_bytes;
9498
9499 /* Values of window_end_pos and window_end_vpos at the end of
9500 try_window_id. */
9501
9502 EMACS_INT debug_end_pos, debug_end_vpos;
9503
9504 /* Append a string to W->desired_matrix->method. FMT is a printf
9505 format string. A1...A9 are a supplement for a variable-length
9506 argument list. If trace_redisplay_p is non-zero also printf the
9507 resulting string to stderr. */
9508
9509 static void
9510 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9511 struct window *w;
9512 char *fmt;
9513 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9514 {
9515 char buffer[512];
9516 char *method = w->desired_matrix->method;
9517 int len = strlen (method);
9518 int size = sizeof w->desired_matrix->method;
9519 int remaining = size - len - 1;
9520
9521 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9522 if (len && remaining)
9523 {
9524 method[len] = '|';
9525 --remaining, ++len;
9526 }
9527
9528 strncpy (method + len, buffer, remaining);
9529
9530 if (trace_redisplay_p)
9531 fprintf (stderr, "%p (%s): %s\n",
9532 w,
9533 ((BUFFERP (w->buffer)
9534 && STRINGP (XBUFFER (w->buffer)->name))
9535 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9536 : "no buffer"),
9537 buffer);
9538 }
9539
9540 #endif /* GLYPH_DEBUG */
9541
9542
9543 /* Value is non-zero if all changes in window W, which displays
9544 current_buffer, are in the text between START and END. START is a
9545 buffer position, END is given as a distance from Z. Used in
9546 redisplay_internal for display optimization. */
9547
9548 static INLINE int
9549 text_outside_line_unchanged_p (w, start, end)
9550 struct window *w;
9551 int start, end;
9552 {
9553 int unchanged_p = 1;
9554
9555 /* If text or overlays have changed, see where. */
9556 if (XFASTINT (w->last_modified) < MODIFF
9557 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9558 {
9559 /* Gap in the line? */
9560 if (GPT < start || Z - GPT < end)
9561 unchanged_p = 0;
9562
9563 /* Changes start in front of the line, or end after it? */
9564 if (unchanged_p
9565 && (BEG_UNCHANGED < start - 1
9566 || END_UNCHANGED < end))
9567 unchanged_p = 0;
9568
9569 /* If selective display, can't optimize if changes start at the
9570 beginning of the line. */
9571 if (unchanged_p
9572 && INTEGERP (current_buffer->selective_display)
9573 && XINT (current_buffer->selective_display) > 0
9574 && (BEG_UNCHANGED < start || GPT <= start))
9575 unchanged_p = 0;
9576
9577 /* If there are overlays at the start or end of the line, these
9578 may have overlay strings with newlines in them. A change at
9579 START, for instance, may actually concern the display of such
9580 overlay strings as well, and they are displayed on different
9581 lines. So, quickly rule out this case. (For the future, it
9582 might be desirable to implement something more telling than
9583 just BEG/END_UNCHANGED.) */
9584 if (unchanged_p)
9585 {
9586 if (BEG + BEG_UNCHANGED == start
9587 && overlay_touches_p (start))
9588 unchanged_p = 0;
9589 if (END_UNCHANGED == end
9590 && overlay_touches_p (Z - end))
9591 unchanged_p = 0;
9592 }
9593 }
9594
9595 return unchanged_p;
9596 }
9597
9598
9599 /* Do a frame update, taking possible shortcuts into account. This is
9600 the main external entry point for redisplay.
9601
9602 If the last redisplay displayed an echo area message and that message
9603 is no longer requested, we clear the echo area or bring back the
9604 mini-buffer if that is in use. */
9605
9606 void
9607 redisplay ()
9608 {
9609 redisplay_internal (0);
9610 }
9611
9612
9613 static Lisp_Object
9614 overlay_arrow_string_or_property (var, pbitmap)
9615 Lisp_Object var;
9616 int *pbitmap;
9617 {
9618 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9619 Lisp_Object bitmap;
9620
9621 if (pbitmap)
9622 {
9623 *pbitmap = 0;
9624 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9625 *pbitmap = XINT (bitmap);
9626 }
9627
9628 if (!NILP (pstr))
9629 return pstr;
9630 return Voverlay_arrow_string;
9631 }
9632
9633 /* Return 1 if there are any overlay-arrows in current_buffer. */
9634 static int
9635 overlay_arrow_in_current_buffer_p ()
9636 {
9637 Lisp_Object vlist;
9638
9639 for (vlist = Voverlay_arrow_variable_list;
9640 CONSP (vlist);
9641 vlist = XCDR (vlist))
9642 {
9643 Lisp_Object var = XCAR (vlist);
9644 Lisp_Object val;
9645
9646 if (!SYMBOLP (var))
9647 continue;
9648 val = find_symbol_value (var);
9649 if (MARKERP (val)
9650 && current_buffer == XMARKER (val)->buffer)
9651 return 1;
9652 }
9653 return 0;
9654 }
9655
9656
9657 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9658 has changed. */
9659
9660 static int
9661 overlay_arrows_changed_p ()
9662 {
9663 Lisp_Object vlist;
9664
9665 for (vlist = Voverlay_arrow_variable_list;
9666 CONSP (vlist);
9667 vlist = XCDR (vlist))
9668 {
9669 Lisp_Object var = XCAR (vlist);
9670 Lisp_Object val, pstr;
9671
9672 if (!SYMBOLP (var))
9673 continue;
9674 val = find_symbol_value (var);
9675 if (!MARKERP (val))
9676 continue;
9677 if (! EQ (COERCE_MARKER (val),
9678 Fget (var, Qlast_arrow_position))
9679 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9680 EQ (pstr, Fget (var, Qlast_arrow_string))))
9681 return 1;
9682 }
9683 return 0;
9684 }
9685
9686 /* Mark overlay arrows to be updated on next redisplay. */
9687
9688 static void
9689 update_overlay_arrows (up_to_date)
9690 int up_to_date;
9691 {
9692 Lisp_Object vlist;
9693
9694 for (vlist = Voverlay_arrow_variable_list;
9695 CONSP (vlist);
9696 vlist = XCDR (vlist))
9697 {
9698 Lisp_Object var = XCAR (vlist);
9699
9700 if (!SYMBOLP (var))
9701 continue;
9702
9703 if (up_to_date > 0)
9704 {
9705 Lisp_Object val = find_symbol_value (var);
9706 Fput (var, Qlast_arrow_position,
9707 COERCE_MARKER (val));
9708 Fput (var, Qlast_arrow_string,
9709 overlay_arrow_string_or_property (var, 0));
9710 }
9711 else if (up_to_date < 0
9712 || !NILP (Fget (var, Qlast_arrow_position)))
9713 {
9714 Fput (var, Qlast_arrow_position, Qt);
9715 Fput (var, Qlast_arrow_string, Qt);
9716 }
9717 }
9718 }
9719
9720
9721 /* Return overlay arrow string to display at row.
9722 Return t if display as bitmap in left fringe.
9723 Return nil if no overlay arrow. */
9724
9725 static Lisp_Object
9726 overlay_arrow_at_row (it, row, pbitmap)
9727 struct it *it;
9728 struct glyph_row *row;
9729 int *pbitmap;
9730 {
9731 Lisp_Object vlist;
9732
9733 for (vlist = Voverlay_arrow_variable_list;
9734 CONSP (vlist);
9735 vlist = XCDR (vlist))
9736 {
9737 Lisp_Object var = XCAR (vlist);
9738 Lisp_Object val;
9739
9740 if (!SYMBOLP (var))
9741 continue;
9742
9743 val = find_symbol_value (var);
9744
9745 if (MARKERP (val)
9746 && current_buffer == XMARKER (val)->buffer
9747 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9748 {
9749 val = overlay_arrow_string_or_property (var, pbitmap);
9750 if (FRAME_WINDOW_P (it->f)
9751 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9752 return Qt;
9753 if (STRINGP (val))
9754 return val;
9755 break;
9756 }
9757 }
9758
9759 *pbitmap = 0;
9760 return Qnil;
9761 }
9762
9763 /* Return 1 if point moved out of or into a composition. Otherwise
9764 return 0. PREV_BUF and PREV_PT are the last point buffer and
9765 position. BUF and PT are the current point buffer and position. */
9766
9767 int
9768 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9769 struct buffer *prev_buf, *buf;
9770 int prev_pt, pt;
9771 {
9772 int start, end;
9773 Lisp_Object prop;
9774 Lisp_Object buffer;
9775
9776 XSETBUFFER (buffer, buf);
9777 /* Check a composition at the last point if point moved within the
9778 same buffer. */
9779 if (prev_buf == buf)
9780 {
9781 if (prev_pt == pt)
9782 /* Point didn't move. */
9783 return 0;
9784
9785 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9786 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9787 && COMPOSITION_VALID_P (start, end, prop)
9788 && start < prev_pt && end > prev_pt)
9789 /* The last point was within the composition. Return 1 iff
9790 point moved out of the composition. */
9791 return (pt <= start || pt >= end);
9792 }
9793
9794 /* Check a composition at the current point. */
9795 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9796 && find_composition (pt, -1, &start, &end, &prop, buffer)
9797 && COMPOSITION_VALID_P (start, end, prop)
9798 && start < pt && end > pt);
9799 }
9800
9801
9802 /* Reconsider the setting of B->clip_changed which is displayed
9803 in window W. */
9804
9805 static INLINE void
9806 reconsider_clip_changes (w, b)
9807 struct window *w;
9808 struct buffer *b;
9809 {
9810 if (b->clip_changed
9811 && !NILP (w->window_end_valid)
9812 && w->current_matrix->buffer == b
9813 && w->current_matrix->zv == BUF_ZV (b)
9814 && w->current_matrix->begv == BUF_BEGV (b))
9815 b->clip_changed = 0;
9816
9817 /* If display wasn't paused, and W is not a tool bar window, see if
9818 point has been moved into or out of a composition. In that case,
9819 we set b->clip_changed to 1 to force updating the screen. If
9820 b->clip_changed has already been set to 1, we can skip this
9821 check. */
9822 if (!b->clip_changed
9823 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9824 {
9825 int pt;
9826
9827 if (w == XWINDOW (selected_window))
9828 pt = BUF_PT (current_buffer);
9829 else
9830 pt = marker_position (w->pointm);
9831
9832 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9833 || pt != XINT (w->last_point))
9834 && check_point_in_composition (w->current_matrix->buffer,
9835 XINT (w->last_point),
9836 XBUFFER (w->buffer), pt))
9837 b->clip_changed = 1;
9838 }
9839 }
9840 \f
9841
9842 /* Select FRAME to forward the values of frame-local variables into C
9843 variables so that the redisplay routines can access those values
9844 directly. */
9845
9846 static void
9847 select_frame_for_redisplay (frame)
9848 Lisp_Object frame;
9849 {
9850 Lisp_Object tail, sym, val;
9851 Lisp_Object old = selected_frame;
9852
9853 selected_frame = frame;
9854
9855 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9856 if (CONSP (XCAR (tail))
9857 && (sym = XCAR (XCAR (tail)),
9858 SYMBOLP (sym))
9859 && (sym = indirect_variable (sym),
9860 val = SYMBOL_VALUE (sym),
9861 (BUFFER_LOCAL_VALUEP (val)
9862 || SOME_BUFFER_LOCAL_VALUEP (val)))
9863 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9864 Fsymbol_value (sym);
9865
9866 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9867 if (CONSP (XCAR (tail))
9868 && (sym = XCAR (XCAR (tail)),
9869 SYMBOLP (sym))
9870 && (sym = indirect_variable (sym),
9871 val = SYMBOL_VALUE (sym),
9872 (BUFFER_LOCAL_VALUEP (val)
9873 || SOME_BUFFER_LOCAL_VALUEP (val)))
9874 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9875 Fsymbol_value (sym);
9876 }
9877
9878
9879 #define STOP_POLLING \
9880 do { if (! polling_stopped_here) stop_polling (); \
9881 polling_stopped_here = 1; } while (0)
9882
9883 #define RESUME_POLLING \
9884 do { if (polling_stopped_here) start_polling (); \
9885 polling_stopped_here = 0; } while (0)
9886
9887
9888 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9889 response to any user action; therefore, we should preserve the echo
9890 area. (Actually, our caller does that job.) Perhaps in the future
9891 avoid recentering windows if it is not necessary; currently that
9892 causes some problems. */
9893
9894 static void
9895 redisplay_internal (preserve_echo_area)
9896 int preserve_echo_area;
9897 {
9898 struct window *w = XWINDOW (selected_window);
9899 struct frame *f = XFRAME (w->frame);
9900 int pause;
9901 int must_finish = 0;
9902 struct text_pos tlbufpos, tlendpos;
9903 int number_of_visible_frames;
9904 int count;
9905 struct frame *sf = SELECTED_FRAME ();
9906 int polling_stopped_here = 0;
9907
9908 /* Non-zero means redisplay has to consider all windows on all
9909 frames. Zero means, only selected_window is considered. */
9910 int consider_all_windows_p;
9911
9912 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9913
9914 /* No redisplay if running in batch mode or frame is not yet fully
9915 initialized, or redisplay is explicitly turned off by setting
9916 Vinhibit_redisplay. */
9917 if (noninteractive
9918 || !NILP (Vinhibit_redisplay)
9919 || !f->glyphs_initialized_p)
9920 return;
9921
9922 /* The flag redisplay_performed_directly_p is set by
9923 direct_output_for_insert when it already did the whole screen
9924 update necessary. */
9925 if (redisplay_performed_directly_p)
9926 {
9927 redisplay_performed_directly_p = 0;
9928 if (!hscroll_windows (selected_window))
9929 return;
9930 }
9931
9932 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9933 if (popup_activated ())
9934 return;
9935 #endif
9936
9937 /* I don't think this happens but let's be paranoid. */
9938 if (redisplaying_p)
9939 return;
9940
9941 /* Record a function that resets redisplaying_p to its old value
9942 when we leave this function. */
9943 count = SPECPDL_INDEX ();
9944 record_unwind_protect (unwind_redisplay,
9945 Fcons (make_number (redisplaying_p), selected_frame));
9946 ++redisplaying_p;
9947 specbind (Qinhibit_free_realized_faces, Qnil);
9948
9949 retry:
9950 pause = 0;
9951 reconsider_clip_changes (w, current_buffer);
9952
9953 /* If new fonts have been loaded that make a glyph matrix adjustment
9954 necessary, do it. */
9955 if (fonts_changed_p)
9956 {
9957 adjust_glyphs (NULL);
9958 ++windows_or_buffers_changed;
9959 fonts_changed_p = 0;
9960 }
9961
9962 /* If face_change_count is non-zero, init_iterator will free all
9963 realized faces, which includes the faces referenced from current
9964 matrices. So, we can't reuse current matrices in this case. */
9965 if (face_change_count)
9966 ++windows_or_buffers_changed;
9967
9968 if (! FRAME_WINDOW_P (sf)
9969 && previous_terminal_frame != sf)
9970 {
9971 /* Since frames on an ASCII terminal share the same display
9972 area, displaying a different frame means redisplay the whole
9973 thing. */
9974 windows_or_buffers_changed++;
9975 SET_FRAME_GARBAGED (sf);
9976 XSETFRAME (Vterminal_frame, sf);
9977 }
9978 previous_terminal_frame = sf;
9979
9980 /* Set the visible flags for all frames. Do this before checking
9981 for resized or garbaged frames; they want to know if their frames
9982 are visible. See the comment in frame.h for
9983 FRAME_SAMPLE_VISIBILITY. */
9984 {
9985 Lisp_Object tail, frame;
9986
9987 number_of_visible_frames = 0;
9988
9989 FOR_EACH_FRAME (tail, frame)
9990 {
9991 struct frame *f = XFRAME (frame);
9992
9993 FRAME_SAMPLE_VISIBILITY (f);
9994 if (FRAME_VISIBLE_P (f))
9995 ++number_of_visible_frames;
9996 clear_desired_matrices (f);
9997 }
9998 }
9999
10000 /* Notice any pending interrupt request to change frame size. */
10001 do_pending_window_change (1);
10002
10003 /* Clear frames marked as garbaged. */
10004 if (frame_garbaged)
10005 clear_garbaged_frames ();
10006
10007 /* Build menubar and tool-bar items. */
10008 prepare_menu_bars ();
10009
10010 if (windows_or_buffers_changed)
10011 update_mode_lines++;
10012
10013 /* Detect case that we need to write or remove a star in the mode line. */
10014 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10015 {
10016 w->update_mode_line = Qt;
10017 if (buffer_shared > 1)
10018 update_mode_lines++;
10019 }
10020
10021 /* If %c is in the mode line, update it if needed. */
10022 if (!NILP (w->column_number_displayed)
10023 /* This alternative quickly identifies a common case
10024 where no change is needed. */
10025 && !(PT == XFASTINT (w->last_point)
10026 && XFASTINT (w->last_modified) >= MODIFF
10027 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10028 && (XFASTINT (w->column_number_displayed)
10029 != (int) current_column ())) /* iftc */
10030 w->update_mode_line = Qt;
10031
10032 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10033
10034 /* The variable buffer_shared is set in redisplay_window and
10035 indicates that we redisplay a buffer in different windows. See
10036 there. */
10037 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10038 || cursor_type_changed);
10039
10040 /* If specs for an arrow have changed, do thorough redisplay
10041 to ensure we remove any arrow that should no longer exist. */
10042 if (overlay_arrows_changed_p ())
10043 consider_all_windows_p = windows_or_buffers_changed = 1;
10044
10045 /* Normally the message* functions will have already displayed and
10046 updated the echo area, but the frame may have been trashed, or
10047 the update may have been preempted, so display the echo area
10048 again here. Checking message_cleared_p captures the case that
10049 the echo area should be cleared. */
10050 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10051 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10052 || (message_cleared_p
10053 && minibuf_level == 0
10054 /* If the mini-window is currently selected, this means the
10055 echo-area doesn't show through. */
10056 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10057 {
10058 int window_height_changed_p = echo_area_display (0);
10059 must_finish = 1;
10060
10061 /* If we don't display the current message, don't clear the
10062 message_cleared_p flag, because, if we did, we wouldn't clear
10063 the echo area in the next redisplay which doesn't preserve
10064 the echo area. */
10065 if (!display_last_displayed_message_p)
10066 message_cleared_p = 0;
10067
10068 if (fonts_changed_p)
10069 goto retry;
10070 else if (window_height_changed_p)
10071 {
10072 consider_all_windows_p = 1;
10073 ++update_mode_lines;
10074 ++windows_or_buffers_changed;
10075
10076 /* If window configuration was changed, frames may have been
10077 marked garbaged. Clear them or we will experience
10078 surprises wrt scrolling. */
10079 if (frame_garbaged)
10080 clear_garbaged_frames ();
10081 }
10082 }
10083 else if (EQ (selected_window, minibuf_window)
10084 && (current_buffer->clip_changed
10085 || XFASTINT (w->last_modified) < MODIFF
10086 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10087 && resize_mini_window (w, 0))
10088 {
10089 /* Resized active mini-window to fit the size of what it is
10090 showing if its contents might have changed. */
10091 must_finish = 1;
10092 consider_all_windows_p = 1;
10093 ++windows_or_buffers_changed;
10094 ++update_mode_lines;
10095
10096 /* If window configuration was changed, frames may have been
10097 marked garbaged. Clear them or we will experience
10098 surprises wrt scrolling. */
10099 if (frame_garbaged)
10100 clear_garbaged_frames ();
10101 }
10102
10103
10104 /* If showing the region, and mark has changed, we must redisplay
10105 the whole window. The assignment to this_line_start_pos prevents
10106 the optimization directly below this if-statement. */
10107 if (((!NILP (Vtransient_mark_mode)
10108 && !NILP (XBUFFER (w->buffer)->mark_active))
10109 != !NILP (w->region_showing))
10110 || (!NILP (w->region_showing)
10111 && !EQ (w->region_showing,
10112 Fmarker_position (XBUFFER (w->buffer)->mark))))
10113 CHARPOS (this_line_start_pos) = 0;
10114
10115 /* Optimize the case that only the line containing the cursor in the
10116 selected window has changed. Variables starting with this_ are
10117 set in display_line and record information about the line
10118 containing the cursor. */
10119 tlbufpos = this_line_start_pos;
10120 tlendpos = this_line_end_pos;
10121 if (!consider_all_windows_p
10122 && CHARPOS (tlbufpos) > 0
10123 && NILP (w->update_mode_line)
10124 && !current_buffer->clip_changed
10125 && !current_buffer->prevent_redisplay_optimizations_p
10126 && FRAME_VISIBLE_P (XFRAME (w->frame))
10127 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10128 /* Make sure recorded data applies to current buffer, etc. */
10129 && this_line_buffer == current_buffer
10130 && current_buffer == XBUFFER (w->buffer)
10131 && NILP (w->force_start)
10132 && NILP (w->optional_new_start)
10133 /* Point must be on the line that we have info recorded about. */
10134 && PT >= CHARPOS (tlbufpos)
10135 && PT <= Z - CHARPOS (tlendpos)
10136 /* All text outside that line, including its final newline,
10137 must be unchanged */
10138 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10139 CHARPOS (tlendpos)))
10140 {
10141 if (CHARPOS (tlbufpos) > BEGV
10142 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10143 && (CHARPOS (tlbufpos) == ZV
10144 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10145 /* Former continuation line has disappeared by becoming empty */
10146 goto cancel;
10147 else if (XFASTINT (w->last_modified) < MODIFF
10148 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10149 || MINI_WINDOW_P (w))
10150 {
10151 /* We have to handle the case of continuation around a
10152 wide-column character (See the comment in indent.c around
10153 line 885).
10154
10155 For instance, in the following case:
10156
10157 -------- Insert --------
10158 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10159 J_I_ ==> J_I_ `^^' are cursors.
10160 ^^ ^^
10161 -------- --------
10162
10163 As we have to redraw the line above, we should goto cancel. */
10164
10165 struct it it;
10166 int line_height_before = this_line_pixel_height;
10167
10168 /* Note that start_display will handle the case that the
10169 line starting at tlbufpos is a continuation lines. */
10170 start_display (&it, w, tlbufpos);
10171
10172 /* Implementation note: It this still necessary? */
10173 if (it.current_x != this_line_start_x)
10174 goto cancel;
10175
10176 TRACE ((stderr, "trying display optimization 1\n"));
10177 w->cursor.vpos = -1;
10178 overlay_arrow_seen = 0;
10179 it.vpos = this_line_vpos;
10180 it.current_y = this_line_y;
10181 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10182 display_line (&it);
10183
10184 /* If line contains point, is not continued,
10185 and ends at same distance from eob as before, we win */
10186 if (w->cursor.vpos >= 0
10187 /* Line is not continued, otherwise this_line_start_pos
10188 would have been set to 0 in display_line. */
10189 && CHARPOS (this_line_start_pos)
10190 /* Line ends as before. */
10191 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10192 /* Line has same height as before. Otherwise other lines
10193 would have to be shifted up or down. */
10194 && this_line_pixel_height == line_height_before)
10195 {
10196 /* If this is not the window's last line, we must adjust
10197 the charstarts of the lines below. */
10198 if (it.current_y < it.last_visible_y)
10199 {
10200 struct glyph_row *row
10201 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10202 int delta, delta_bytes;
10203
10204 if (Z - CHARPOS (tlendpos) == ZV)
10205 {
10206 /* This line ends at end of (accessible part of)
10207 buffer. There is no newline to count. */
10208 delta = (Z
10209 - CHARPOS (tlendpos)
10210 - MATRIX_ROW_START_CHARPOS (row));
10211 delta_bytes = (Z_BYTE
10212 - BYTEPOS (tlendpos)
10213 - MATRIX_ROW_START_BYTEPOS (row));
10214 }
10215 else
10216 {
10217 /* This line ends in a newline. Must take
10218 account of the newline and the rest of the
10219 text that follows. */
10220 delta = (Z
10221 - CHARPOS (tlendpos)
10222 - MATRIX_ROW_START_CHARPOS (row));
10223 delta_bytes = (Z_BYTE
10224 - BYTEPOS (tlendpos)
10225 - MATRIX_ROW_START_BYTEPOS (row));
10226 }
10227
10228 increment_matrix_positions (w->current_matrix,
10229 this_line_vpos + 1,
10230 w->current_matrix->nrows,
10231 delta, delta_bytes);
10232 }
10233
10234 /* If this row displays text now but previously didn't,
10235 or vice versa, w->window_end_vpos may have to be
10236 adjusted. */
10237 if ((it.glyph_row - 1)->displays_text_p)
10238 {
10239 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10240 XSETINT (w->window_end_vpos, this_line_vpos);
10241 }
10242 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10243 && this_line_vpos > 0)
10244 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10245 w->window_end_valid = Qnil;
10246
10247 /* Update hint: No need to try to scroll in update_window. */
10248 w->desired_matrix->no_scrolling_p = 1;
10249
10250 #if GLYPH_DEBUG
10251 *w->desired_matrix->method = 0;
10252 debug_method_add (w, "optimization 1");
10253 #endif
10254 #ifdef HAVE_WINDOW_SYSTEM
10255 update_window_fringes (w, 0);
10256 #endif
10257 goto update;
10258 }
10259 else
10260 goto cancel;
10261 }
10262 else if (/* Cursor position hasn't changed. */
10263 PT == XFASTINT (w->last_point)
10264 /* Make sure the cursor was last displayed
10265 in this window. Otherwise we have to reposition it. */
10266 && 0 <= w->cursor.vpos
10267 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10268 {
10269 if (!must_finish)
10270 {
10271 do_pending_window_change (1);
10272
10273 /* We used to always goto end_of_redisplay here, but this
10274 isn't enough if we have a blinking cursor. */
10275 if (w->cursor_off_p == w->last_cursor_off_p)
10276 goto end_of_redisplay;
10277 }
10278 goto update;
10279 }
10280 /* If highlighting the region, or if the cursor is in the echo area,
10281 then we can't just move the cursor. */
10282 else if (! (!NILP (Vtransient_mark_mode)
10283 && !NILP (current_buffer->mark_active))
10284 && (EQ (selected_window, current_buffer->last_selected_window)
10285 || highlight_nonselected_windows)
10286 && NILP (w->region_showing)
10287 && NILP (Vshow_trailing_whitespace)
10288 && !cursor_in_echo_area)
10289 {
10290 struct it it;
10291 struct glyph_row *row;
10292
10293 /* Skip from tlbufpos to PT and see where it is. Note that
10294 PT may be in invisible text. If so, we will end at the
10295 next visible position. */
10296 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10297 NULL, DEFAULT_FACE_ID);
10298 it.current_x = this_line_start_x;
10299 it.current_y = this_line_y;
10300 it.vpos = this_line_vpos;
10301
10302 /* The call to move_it_to stops in front of PT, but
10303 moves over before-strings. */
10304 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10305
10306 if (it.vpos == this_line_vpos
10307 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10308 row->enabled_p))
10309 {
10310 xassert (this_line_vpos == it.vpos);
10311 xassert (this_line_y == it.current_y);
10312 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10313 #if GLYPH_DEBUG
10314 *w->desired_matrix->method = 0;
10315 debug_method_add (w, "optimization 3");
10316 #endif
10317 goto update;
10318 }
10319 else
10320 goto cancel;
10321 }
10322
10323 cancel:
10324 /* Text changed drastically or point moved off of line. */
10325 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10326 }
10327
10328 CHARPOS (this_line_start_pos) = 0;
10329 consider_all_windows_p |= buffer_shared > 1;
10330 ++clear_face_cache_count;
10331
10332
10333 /* Build desired matrices, and update the display. If
10334 consider_all_windows_p is non-zero, do it for all windows on all
10335 frames. Otherwise do it for selected_window, only. */
10336
10337 if (consider_all_windows_p)
10338 {
10339 Lisp_Object tail, frame;
10340 int i, n = 0, size = 50;
10341 struct frame **updated
10342 = (struct frame **) alloca (size * sizeof *updated);
10343
10344 /* Clear the face cache eventually. */
10345 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10346 {
10347 clear_face_cache (0);
10348 clear_face_cache_count = 0;
10349 }
10350
10351 /* Recompute # windows showing selected buffer. This will be
10352 incremented each time such a window is displayed. */
10353 buffer_shared = 0;
10354
10355 FOR_EACH_FRAME (tail, frame)
10356 {
10357 struct frame *f = XFRAME (frame);
10358
10359 if (FRAME_WINDOW_P (f) || f == sf)
10360 {
10361 if (! EQ (frame, selected_frame))
10362 /* Select the frame, for the sake of frame-local
10363 variables. */
10364 select_frame_for_redisplay (frame);
10365
10366 #ifdef HAVE_WINDOW_SYSTEM
10367 if (clear_face_cache_count % 50 == 0
10368 && FRAME_WINDOW_P (f))
10369 clear_image_cache (f, 0);
10370 #endif /* HAVE_WINDOW_SYSTEM */
10371
10372 /* Mark all the scroll bars to be removed; we'll redeem
10373 the ones we want when we redisplay their windows. */
10374 if (condemn_scroll_bars_hook)
10375 condemn_scroll_bars_hook (f);
10376
10377 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10378 redisplay_windows (FRAME_ROOT_WINDOW (f));
10379
10380 /* Any scroll bars which redisplay_windows should have
10381 nuked should now go away. */
10382 if (judge_scroll_bars_hook)
10383 judge_scroll_bars_hook (f);
10384
10385 /* If fonts changed, display again. */
10386 /* ??? rms: I suspect it is a mistake to jump all the way
10387 back to retry here. It should just retry this frame. */
10388 if (fonts_changed_p)
10389 goto retry;
10390
10391 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10392 {
10393 /* See if we have to hscroll. */
10394 if (hscroll_windows (f->root_window))
10395 goto retry;
10396
10397 /* Prevent various kinds of signals during display
10398 update. stdio is not robust about handling
10399 signals, which can cause an apparent I/O
10400 error. */
10401 if (interrupt_input)
10402 unrequest_sigio ();
10403 STOP_POLLING;
10404
10405 /* Update the display. */
10406 set_window_update_flags (XWINDOW (f->root_window), 1);
10407 pause |= update_frame (f, 0, 0);
10408 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10409 if (pause)
10410 break;
10411 #endif
10412
10413 if (n == size)
10414 {
10415 int nbytes = size * sizeof *updated;
10416 struct frame **p = (struct frame **) alloca (2 * nbytes);
10417 bcopy (updated, p, nbytes);
10418 size *= 2;
10419 }
10420
10421 updated[n++] = f;
10422 }
10423 }
10424 }
10425
10426 if (!pause)
10427 {
10428 /* Do the mark_window_display_accurate after all windows have
10429 been redisplayed because this call resets flags in buffers
10430 which are needed for proper redisplay. */
10431 for (i = 0; i < n; ++i)
10432 {
10433 struct frame *f = updated[i];
10434 mark_window_display_accurate (f->root_window, 1);
10435 if (frame_up_to_date_hook)
10436 frame_up_to_date_hook (f);
10437 }
10438 }
10439 }
10440 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10441 {
10442 Lisp_Object mini_window;
10443 struct frame *mini_frame;
10444
10445 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10446 /* Use list_of_error, not Qerror, so that
10447 we catch only errors and don't run the debugger. */
10448 internal_condition_case_1 (redisplay_window_1, selected_window,
10449 list_of_error,
10450 redisplay_window_error);
10451
10452 /* Compare desired and current matrices, perform output. */
10453
10454 update:
10455 /* If fonts changed, display again. */
10456 if (fonts_changed_p)
10457 goto retry;
10458
10459 /* Prevent various kinds of signals during display update.
10460 stdio is not robust about handling signals,
10461 which can cause an apparent I/O error. */
10462 if (interrupt_input)
10463 unrequest_sigio ();
10464 STOP_POLLING;
10465
10466 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10467 {
10468 if (hscroll_windows (selected_window))
10469 goto retry;
10470
10471 XWINDOW (selected_window)->must_be_updated_p = 1;
10472 pause = update_frame (sf, 0, 0);
10473 }
10474
10475 /* We may have called echo_area_display at the top of this
10476 function. If the echo area is on another frame, that may
10477 have put text on a frame other than the selected one, so the
10478 above call to update_frame would not have caught it. Catch
10479 it here. */
10480 mini_window = FRAME_MINIBUF_WINDOW (sf);
10481 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10482
10483 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10484 {
10485 XWINDOW (mini_window)->must_be_updated_p = 1;
10486 pause |= update_frame (mini_frame, 0, 0);
10487 if (!pause && hscroll_windows (mini_window))
10488 goto retry;
10489 }
10490 }
10491
10492 /* If display was paused because of pending input, make sure we do a
10493 thorough update the next time. */
10494 if (pause)
10495 {
10496 /* Prevent the optimization at the beginning of
10497 redisplay_internal that tries a single-line update of the
10498 line containing the cursor in the selected window. */
10499 CHARPOS (this_line_start_pos) = 0;
10500
10501 /* Let the overlay arrow be updated the next time. */
10502 update_overlay_arrows (0);
10503
10504 /* If we pause after scrolling, some rows in the current
10505 matrices of some windows are not valid. */
10506 if (!WINDOW_FULL_WIDTH_P (w)
10507 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10508 update_mode_lines = 1;
10509 }
10510 else
10511 {
10512 if (!consider_all_windows_p)
10513 {
10514 /* This has already been done above if
10515 consider_all_windows_p is set. */
10516 mark_window_display_accurate_1 (w, 1);
10517
10518 /* Say overlay arrows are up to date. */
10519 update_overlay_arrows (1);
10520
10521 if (frame_up_to_date_hook != 0)
10522 frame_up_to_date_hook (sf);
10523 }
10524
10525 update_mode_lines = 0;
10526 windows_or_buffers_changed = 0;
10527 cursor_type_changed = 0;
10528 }
10529
10530 /* Start SIGIO interrupts coming again. Having them off during the
10531 code above makes it less likely one will discard output, but not
10532 impossible, since there might be stuff in the system buffer here.
10533 But it is much hairier to try to do anything about that. */
10534 if (interrupt_input)
10535 request_sigio ();
10536 RESUME_POLLING;
10537
10538 /* If a frame has become visible which was not before, redisplay
10539 again, so that we display it. Expose events for such a frame
10540 (which it gets when becoming visible) don't call the parts of
10541 redisplay constructing glyphs, so simply exposing a frame won't
10542 display anything in this case. So, we have to display these
10543 frames here explicitly. */
10544 if (!pause)
10545 {
10546 Lisp_Object tail, frame;
10547 int new_count = 0;
10548
10549 FOR_EACH_FRAME (tail, frame)
10550 {
10551 int this_is_visible = 0;
10552
10553 if (XFRAME (frame)->visible)
10554 this_is_visible = 1;
10555 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10556 if (XFRAME (frame)->visible)
10557 this_is_visible = 1;
10558
10559 if (this_is_visible)
10560 new_count++;
10561 }
10562
10563 if (new_count != number_of_visible_frames)
10564 windows_or_buffers_changed++;
10565 }
10566
10567 /* Change frame size now if a change is pending. */
10568 do_pending_window_change (1);
10569
10570 /* If we just did a pending size change, or have additional
10571 visible frames, redisplay again. */
10572 if (windows_or_buffers_changed && !pause)
10573 goto retry;
10574
10575 end_of_redisplay:
10576 unbind_to (count, Qnil);
10577 RESUME_POLLING;
10578 }
10579
10580
10581 /* Redisplay, but leave alone any recent echo area message unless
10582 another message has been requested in its place.
10583
10584 This is useful in situations where you need to redisplay but no
10585 user action has occurred, making it inappropriate for the message
10586 area to be cleared. See tracking_off and
10587 wait_reading_process_output for examples of these situations.
10588
10589 FROM_WHERE is an integer saying from where this function was
10590 called. This is useful for debugging. */
10591
10592 void
10593 redisplay_preserve_echo_area (from_where)
10594 int from_where;
10595 {
10596 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10597
10598 if (!NILP (echo_area_buffer[1]))
10599 {
10600 /* We have a previously displayed message, but no current
10601 message. Redisplay the previous message. */
10602 display_last_displayed_message_p = 1;
10603 redisplay_internal (1);
10604 display_last_displayed_message_p = 0;
10605 }
10606 else
10607 redisplay_internal (1);
10608
10609 if (rif != NULL && rif->flush_display_optional)
10610 rif->flush_display_optional (NULL);
10611 }
10612
10613
10614 /* Function registered with record_unwind_protect in
10615 redisplay_internal. Reset redisplaying_p to the value it had
10616 before redisplay_internal was called, and clear
10617 prevent_freeing_realized_faces_p. It also selects the previously
10618 selected frame. */
10619
10620 static Lisp_Object
10621 unwind_redisplay (val)
10622 Lisp_Object val;
10623 {
10624 Lisp_Object old_redisplaying_p, old_frame;
10625
10626 old_redisplaying_p = XCAR (val);
10627 redisplaying_p = XFASTINT (old_redisplaying_p);
10628 old_frame = XCDR (val);
10629 if (! EQ (old_frame, selected_frame))
10630 select_frame_for_redisplay (old_frame);
10631 return Qnil;
10632 }
10633
10634
10635 /* Mark the display of window W as accurate or inaccurate. If
10636 ACCURATE_P is non-zero mark display of W as accurate. If
10637 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10638 redisplay_internal is called. */
10639
10640 static void
10641 mark_window_display_accurate_1 (w, accurate_p)
10642 struct window *w;
10643 int accurate_p;
10644 {
10645 if (BUFFERP (w->buffer))
10646 {
10647 struct buffer *b = XBUFFER (w->buffer);
10648
10649 w->last_modified
10650 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10651 w->last_overlay_modified
10652 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10653 w->last_had_star
10654 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10655
10656 if (accurate_p)
10657 {
10658 b->clip_changed = 0;
10659 b->prevent_redisplay_optimizations_p = 0;
10660
10661 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10662 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10663 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10664 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10665
10666 w->current_matrix->buffer = b;
10667 w->current_matrix->begv = BUF_BEGV (b);
10668 w->current_matrix->zv = BUF_ZV (b);
10669
10670 w->last_cursor = w->cursor;
10671 w->last_cursor_off_p = w->cursor_off_p;
10672
10673 if (w == XWINDOW (selected_window))
10674 w->last_point = make_number (BUF_PT (b));
10675 else
10676 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10677 }
10678 }
10679
10680 if (accurate_p)
10681 {
10682 w->window_end_valid = w->buffer;
10683 #if 0 /* This is incorrect with variable-height lines. */
10684 xassert (XINT (w->window_end_vpos)
10685 < (WINDOW_TOTAL_LINES (w)
10686 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10687 #endif
10688 w->update_mode_line = Qnil;
10689 }
10690 }
10691
10692
10693 /* Mark the display of windows in the window tree rooted at WINDOW as
10694 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10695 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10696 be redisplayed the next time redisplay_internal is called. */
10697
10698 void
10699 mark_window_display_accurate (window, accurate_p)
10700 Lisp_Object window;
10701 int accurate_p;
10702 {
10703 struct window *w;
10704
10705 for (; !NILP (window); window = w->next)
10706 {
10707 w = XWINDOW (window);
10708 mark_window_display_accurate_1 (w, accurate_p);
10709
10710 if (!NILP (w->vchild))
10711 mark_window_display_accurate (w->vchild, accurate_p);
10712 if (!NILP (w->hchild))
10713 mark_window_display_accurate (w->hchild, accurate_p);
10714 }
10715
10716 if (accurate_p)
10717 {
10718 update_overlay_arrows (1);
10719 }
10720 else
10721 {
10722 /* Force a thorough redisplay the next time by setting
10723 last_arrow_position and last_arrow_string to t, which is
10724 unequal to any useful value of Voverlay_arrow_... */
10725 update_overlay_arrows (-1);
10726 }
10727 }
10728
10729
10730 /* Return value in display table DP (Lisp_Char_Table *) for character
10731 C. Since a display table doesn't have any parent, we don't have to
10732 follow parent. Do not call this function directly but use the
10733 macro DISP_CHAR_VECTOR. */
10734
10735 Lisp_Object
10736 disp_char_vector (dp, c)
10737 struct Lisp_Char_Table *dp;
10738 int c;
10739 {
10740 int code[4], i;
10741 Lisp_Object val;
10742
10743 if (SINGLE_BYTE_CHAR_P (c))
10744 return (dp->contents[c]);
10745
10746 SPLIT_CHAR (c, code[0], code[1], code[2]);
10747 if (code[1] < 32)
10748 code[1] = -1;
10749 else if (code[2] < 32)
10750 code[2] = -1;
10751
10752 /* Here, the possible range of code[0] (== charset ID) is
10753 128..max_charset. Since the top level char table contains data
10754 for multibyte characters after 256th element, we must increment
10755 code[0] by 128 to get a correct index. */
10756 code[0] += 128;
10757 code[3] = -1; /* anchor */
10758
10759 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10760 {
10761 val = dp->contents[code[i]];
10762 if (!SUB_CHAR_TABLE_P (val))
10763 return (NILP (val) ? dp->defalt : val);
10764 }
10765
10766 /* Here, val is a sub char table. We return the default value of
10767 it. */
10768 return (dp->defalt);
10769 }
10770
10771
10772 \f
10773 /***********************************************************************
10774 Window Redisplay
10775 ***********************************************************************/
10776
10777 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10778
10779 static void
10780 redisplay_windows (window)
10781 Lisp_Object window;
10782 {
10783 while (!NILP (window))
10784 {
10785 struct window *w = XWINDOW (window);
10786
10787 if (!NILP (w->hchild))
10788 redisplay_windows (w->hchild);
10789 else if (!NILP (w->vchild))
10790 redisplay_windows (w->vchild);
10791 else
10792 {
10793 displayed_buffer = XBUFFER (w->buffer);
10794 /* Use list_of_error, not Qerror, so that
10795 we catch only errors and don't run the debugger. */
10796 internal_condition_case_1 (redisplay_window_0, window,
10797 list_of_error,
10798 redisplay_window_error);
10799 }
10800
10801 window = w->next;
10802 }
10803 }
10804
10805 static Lisp_Object
10806 redisplay_window_error ()
10807 {
10808 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10809 return Qnil;
10810 }
10811
10812 static Lisp_Object
10813 redisplay_window_0 (window)
10814 Lisp_Object window;
10815 {
10816 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10817 redisplay_window (window, 0);
10818 return Qnil;
10819 }
10820
10821 static Lisp_Object
10822 redisplay_window_1 (window)
10823 Lisp_Object window;
10824 {
10825 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10826 redisplay_window (window, 1);
10827 return Qnil;
10828 }
10829 \f
10830
10831 /* Increment GLYPH until it reaches END or CONDITION fails while
10832 adding (GLYPH)->pixel_width to X. */
10833
10834 #define SKIP_GLYPHS(glyph, end, x, condition) \
10835 do \
10836 { \
10837 (x) += (glyph)->pixel_width; \
10838 ++(glyph); \
10839 } \
10840 while ((glyph) < (end) && (condition))
10841
10842
10843 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10844 DELTA is the number of bytes by which positions recorded in ROW
10845 differ from current buffer positions. */
10846
10847 void
10848 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10849 struct window *w;
10850 struct glyph_row *row;
10851 struct glyph_matrix *matrix;
10852 int delta, delta_bytes, dy, dvpos;
10853 {
10854 struct glyph *glyph = row->glyphs[TEXT_AREA];
10855 struct glyph *end = glyph + row->used[TEXT_AREA];
10856 struct glyph *cursor = NULL;
10857 /* The first glyph that starts a sequence of glyphs from string. */
10858 struct glyph *string_start;
10859 /* The X coordinate of string_start. */
10860 int string_start_x;
10861 /* The last known character position. */
10862 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10863 /* The last known character position before string_start. */
10864 int string_before_pos;
10865 int x = row->x;
10866 int cursor_x = x;
10867 int cursor_from_overlay_pos = 0;
10868 int pt_old = PT - delta;
10869
10870 /* Skip over glyphs not having an object at the start of the row.
10871 These are special glyphs like truncation marks on terminal
10872 frames. */
10873 if (row->displays_text_p)
10874 while (glyph < end
10875 && INTEGERP (glyph->object)
10876 && glyph->charpos < 0)
10877 {
10878 x += glyph->pixel_width;
10879 ++glyph;
10880 }
10881
10882 string_start = NULL;
10883 while (glyph < end
10884 && !INTEGERP (glyph->object)
10885 && (!BUFFERP (glyph->object)
10886 || (last_pos = glyph->charpos) < pt_old))
10887 {
10888 if (! STRINGP (glyph->object))
10889 {
10890 string_start = NULL;
10891 x += glyph->pixel_width;
10892 ++glyph;
10893 if (cursor_from_overlay_pos
10894 && last_pos > cursor_from_overlay_pos)
10895 {
10896 cursor_from_overlay_pos = 0;
10897 cursor = 0;
10898 }
10899 }
10900 else
10901 {
10902 string_before_pos = last_pos;
10903 string_start = glyph;
10904 string_start_x = x;
10905 /* Skip all glyphs from string. */
10906 do
10907 {
10908 int pos;
10909 if ((cursor == NULL || glyph > cursor)
10910 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10911 Qcursor, (glyph)->object))
10912 && (pos = string_buffer_position (w, glyph->object,
10913 string_before_pos),
10914 (pos == 0 /* From overlay */
10915 || pos == pt_old)))
10916 {
10917 /* Estimate overlay buffer position from the buffer
10918 positions of the glyphs before and after the overlay.
10919 Add 1 to last_pos so that if point corresponds to the
10920 glyph right after the overlay, we still use a 'cursor'
10921 property found in that overlay. */
10922 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10923 cursor = glyph;
10924 cursor_x = x;
10925 }
10926 x += glyph->pixel_width;
10927 ++glyph;
10928 }
10929 while (glyph < end && STRINGP (glyph->object));
10930 }
10931 }
10932
10933 if (cursor != NULL)
10934 {
10935 glyph = cursor;
10936 x = cursor_x;
10937 }
10938 else if (row->ends_in_ellipsis_p && glyph == end)
10939 {
10940 /* Scan back over the ellipsis glyphs, decrementing positions. */
10941 while (glyph > row->glyphs[TEXT_AREA]
10942 && (glyph - 1)->charpos == last_pos)
10943 glyph--, x -= glyph->pixel_width;
10944 /* That loop always goes one position too far,
10945 including the glyph before the ellipsis.
10946 So scan forward over that one. */
10947 x += glyph->pixel_width;
10948 glyph++;
10949 }
10950 else if (string_start
10951 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10952 {
10953 /* We may have skipped over point because the previous glyphs
10954 are from string. As there's no easy way to know the
10955 character position of the current glyph, find the correct
10956 glyph on point by scanning from string_start again. */
10957 Lisp_Object limit;
10958 Lisp_Object string;
10959 int pos;
10960
10961 limit = make_number (pt_old + 1);
10962 end = glyph;
10963 glyph = string_start;
10964 x = string_start_x;
10965 string = glyph->object;
10966 pos = string_buffer_position (w, string, string_before_pos);
10967 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10968 because we always put cursor after overlay strings. */
10969 while (pos == 0 && glyph < end)
10970 {
10971 string = glyph->object;
10972 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10973 if (glyph < end)
10974 pos = string_buffer_position (w, glyph->object, string_before_pos);
10975 }
10976
10977 while (glyph < end)
10978 {
10979 pos = XINT (Fnext_single_char_property_change
10980 (make_number (pos), Qdisplay, Qnil, limit));
10981 if (pos > pt_old)
10982 break;
10983 /* Skip glyphs from the same string. */
10984 string = glyph->object;
10985 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10986 /* Skip glyphs from an overlay. */
10987 while (glyph < end
10988 && ! string_buffer_position (w, glyph->object, pos))
10989 {
10990 string = glyph->object;
10991 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10992 }
10993 }
10994 }
10995
10996 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10997 w->cursor.x = x;
10998 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10999 w->cursor.y = row->y + dy;
11000
11001 if (w == XWINDOW (selected_window))
11002 {
11003 if (!row->continued_p
11004 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11005 && row->x == 0)
11006 {
11007 this_line_buffer = XBUFFER (w->buffer);
11008
11009 CHARPOS (this_line_start_pos)
11010 = MATRIX_ROW_START_CHARPOS (row) + delta;
11011 BYTEPOS (this_line_start_pos)
11012 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11013
11014 CHARPOS (this_line_end_pos)
11015 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11016 BYTEPOS (this_line_end_pos)
11017 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11018
11019 this_line_y = w->cursor.y;
11020 this_line_pixel_height = row->height;
11021 this_line_vpos = w->cursor.vpos;
11022 this_line_start_x = row->x;
11023 }
11024 else
11025 CHARPOS (this_line_start_pos) = 0;
11026 }
11027 }
11028
11029
11030 /* Run window scroll functions, if any, for WINDOW with new window
11031 start STARTP. Sets the window start of WINDOW to that position.
11032
11033 We assume that the window's buffer is really current. */
11034
11035 static INLINE struct text_pos
11036 run_window_scroll_functions (window, startp)
11037 Lisp_Object window;
11038 struct text_pos startp;
11039 {
11040 struct window *w = XWINDOW (window);
11041 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11042
11043 if (current_buffer != XBUFFER (w->buffer))
11044 abort ();
11045
11046 if (!NILP (Vwindow_scroll_functions))
11047 {
11048 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11049 make_number (CHARPOS (startp)));
11050 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11051 /* In case the hook functions switch buffers. */
11052 if (current_buffer != XBUFFER (w->buffer))
11053 set_buffer_internal_1 (XBUFFER (w->buffer));
11054 }
11055
11056 return startp;
11057 }
11058
11059
11060 /* Make sure the line containing the cursor is fully visible.
11061 A value of 1 means there is nothing to be done.
11062 (Either the line is fully visible, or it cannot be made so,
11063 or we cannot tell.)
11064
11065 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11066 is higher than window.
11067
11068 A value of 0 means the caller should do scrolling
11069 as if point had gone off the screen. */
11070
11071 static int
11072 make_cursor_line_fully_visible (w, force_p)
11073 struct window *w;
11074 int force_p;
11075 {
11076 struct glyph_matrix *matrix;
11077 struct glyph_row *row;
11078 int window_height;
11079
11080 if (!make_cursor_line_fully_visible_p)
11081 return 1;
11082
11083 /* It's not always possible to find the cursor, e.g, when a window
11084 is full of overlay strings. Don't do anything in that case. */
11085 if (w->cursor.vpos < 0)
11086 return 1;
11087
11088 matrix = w->desired_matrix;
11089 row = MATRIX_ROW (matrix, w->cursor.vpos);
11090
11091 /* If the cursor row is not partially visible, there's nothing to do. */
11092 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11093 return 1;
11094
11095 /* If the row the cursor is in is taller than the window's height,
11096 it's not clear what to do, so do nothing. */
11097 window_height = window_box_height (w);
11098 if (row->height >= window_height)
11099 {
11100 if (!force_p || w->vscroll)
11101 return 1;
11102 }
11103 return 0;
11104
11105 #if 0
11106 /* This code used to try to scroll the window just enough to make
11107 the line visible. It returned 0 to say that the caller should
11108 allocate larger glyph matrices. */
11109
11110 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11111 {
11112 int dy = row->height - row->visible_height;
11113 w->vscroll = 0;
11114 w->cursor.y += dy;
11115 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11116 }
11117 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11118 {
11119 int dy = - (row->height - row->visible_height);
11120 w->vscroll = dy;
11121 w->cursor.y += dy;
11122 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11123 }
11124
11125 /* When we change the cursor y-position of the selected window,
11126 change this_line_y as well so that the display optimization for
11127 the cursor line of the selected window in redisplay_internal uses
11128 the correct y-position. */
11129 if (w == XWINDOW (selected_window))
11130 this_line_y = w->cursor.y;
11131
11132 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11133 redisplay with larger matrices. */
11134 if (matrix->nrows < required_matrix_height (w))
11135 {
11136 fonts_changed_p = 1;
11137 return 0;
11138 }
11139
11140 return 1;
11141 #endif /* 0 */
11142 }
11143
11144
11145 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11146 non-zero means only WINDOW is redisplayed in redisplay_internal.
11147 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11148 in redisplay_window to bring a partially visible line into view in
11149 the case that only the cursor has moved.
11150
11151 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11152 last screen line's vertical height extends past the end of the screen.
11153
11154 Value is
11155
11156 1 if scrolling succeeded
11157
11158 0 if scrolling didn't find point.
11159
11160 -1 if new fonts have been loaded so that we must interrupt
11161 redisplay, adjust glyph matrices, and try again. */
11162
11163 enum
11164 {
11165 SCROLLING_SUCCESS,
11166 SCROLLING_FAILED,
11167 SCROLLING_NEED_LARGER_MATRICES
11168 };
11169
11170 static int
11171 try_scrolling (window, just_this_one_p, scroll_conservatively,
11172 scroll_step, temp_scroll_step, last_line_misfit)
11173 Lisp_Object window;
11174 int just_this_one_p;
11175 EMACS_INT scroll_conservatively, scroll_step;
11176 int temp_scroll_step;
11177 int last_line_misfit;
11178 {
11179 struct window *w = XWINDOW (window);
11180 struct frame *f = XFRAME (w->frame);
11181 struct text_pos scroll_margin_pos;
11182 struct text_pos pos;
11183 struct text_pos startp;
11184 struct it it;
11185 Lisp_Object window_end;
11186 int this_scroll_margin;
11187 int dy = 0;
11188 int scroll_max;
11189 int rc;
11190 int amount_to_scroll = 0;
11191 Lisp_Object aggressive;
11192 int height;
11193 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11194
11195 #if GLYPH_DEBUG
11196 debug_method_add (w, "try_scrolling");
11197 #endif
11198
11199 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11200
11201 /* Compute scroll margin height in pixels. We scroll when point is
11202 within this distance from the top or bottom of the window. */
11203 if (scroll_margin > 0)
11204 {
11205 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11206 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11207 }
11208 else
11209 this_scroll_margin = 0;
11210
11211 /* Force scroll_conservatively to have a reasonable value so it doesn't
11212 cause an overflow while computing how much to scroll. */
11213 if (scroll_conservatively)
11214 scroll_conservatively = min (scroll_conservatively,
11215 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11216
11217 /* Compute how much we should try to scroll maximally to bring point
11218 into view. */
11219 if (scroll_step || scroll_conservatively || temp_scroll_step)
11220 scroll_max = max (scroll_step,
11221 max (scroll_conservatively, temp_scroll_step));
11222 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11223 || NUMBERP (current_buffer->scroll_up_aggressively))
11224 /* We're trying to scroll because of aggressive scrolling
11225 but no scroll_step is set. Choose an arbitrary one. Maybe
11226 there should be a variable for this. */
11227 scroll_max = 10;
11228 else
11229 scroll_max = 0;
11230 scroll_max *= FRAME_LINE_HEIGHT (f);
11231
11232 /* Decide whether we have to scroll down. Start at the window end
11233 and move this_scroll_margin up to find the position of the scroll
11234 margin. */
11235 window_end = Fwindow_end (window, Qt);
11236
11237 too_near_end:
11238
11239 CHARPOS (scroll_margin_pos) = XINT (window_end);
11240 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11241
11242 if (this_scroll_margin || extra_scroll_margin_lines)
11243 {
11244 start_display (&it, w, scroll_margin_pos);
11245 if (this_scroll_margin)
11246 move_it_vertically_backward (&it, this_scroll_margin);
11247 if (extra_scroll_margin_lines)
11248 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11249 scroll_margin_pos = it.current.pos;
11250 }
11251
11252 if (PT >= CHARPOS (scroll_margin_pos))
11253 {
11254 int y0;
11255
11256 /* Point is in the scroll margin at the bottom of the window, or
11257 below. Compute a new window start that makes point visible. */
11258
11259 /* Compute the distance from the scroll margin to PT.
11260 Give up if the distance is greater than scroll_max. */
11261 start_display (&it, w, scroll_margin_pos);
11262 y0 = it.current_y;
11263 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11264 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11265
11266 /* To make point visible, we have to move the window start
11267 down so that the line the cursor is in is visible, which
11268 means we have to add in the height of the cursor line. */
11269 dy = line_bottom_y (&it) - y0;
11270
11271 if (dy > scroll_max)
11272 return SCROLLING_FAILED;
11273
11274 /* Move the window start down. If scrolling conservatively,
11275 move it just enough down to make point visible. If
11276 scroll_step is set, move it down by scroll_step. */
11277 start_display (&it, w, startp);
11278
11279 if (scroll_conservatively)
11280 /* Set AMOUNT_TO_SCROLL to at least one line,
11281 and at most scroll_conservatively lines. */
11282 amount_to_scroll
11283 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11284 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11285 else if (scroll_step || temp_scroll_step)
11286 amount_to_scroll = scroll_max;
11287 else
11288 {
11289 aggressive = current_buffer->scroll_up_aggressively;
11290 height = WINDOW_BOX_TEXT_HEIGHT (w);
11291 if (NUMBERP (aggressive))
11292 {
11293 double float_amount = XFLOATINT (aggressive) * height;
11294 amount_to_scroll = float_amount;
11295 if (amount_to_scroll == 0 && float_amount > 0)
11296 amount_to_scroll = 1;
11297 }
11298 }
11299
11300 if (amount_to_scroll <= 0)
11301 return SCROLLING_FAILED;
11302
11303 /* If moving by amount_to_scroll leaves STARTP unchanged,
11304 move it down one screen line. */
11305
11306 move_it_vertically (&it, amount_to_scroll);
11307 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11308 move_it_by_lines (&it, 1, 1);
11309 startp = it.current.pos;
11310 }
11311 else
11312 {
11313 /* See if point is inside the scroll margin at the top of the
11314 window. */
11315 scroll_margin_pos = startp;
11316 if (this_scroll_margin)
11317 {
11318 start_display (&it, w, startp);
11319 move_it_vertically (&it, this_scroll_margin);
11320 scroll_margin_pos = it.current.pos;
11321 }
11322
11323 if (PT < CHARPOS (scroll_margin_pos))
11324 {
11325 /* Point is in the scroll margin at the top of the window or
11326 above what is displayed in the window. */
11327 int y0;
11328
11329 /* Compute the vertical distance from PT to the scroll
11330 margin position. Give up if distance is greater than
11331 scroll_max. */
11332 SET_TEXT_POS (pos, PT, PT_BYTE);
11333 start_display (&it, w, pos);
11334 y0 = it.current_y;
11335 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11336 it.last_visible_y, -1,
11337 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11338 dy = it.current_y - y0;
11339 if (dy > scroll_max)
11340 return SCROLLING_FAILED;
11341
11342 /* Compute new window start. */
11343 start_display (&it, w, startp);
11344
11345 if (scroll_conservatively)
11346 amount_to_scroll
11347 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11348 else if (scroll_step || temp_scroll_step)
11349 amount_to_scroll = scroll_max;
11350 else
11351 {
11352 aggressive = current_buffer->scroll_down_aggressively;
11353 height = WINDOW_BOX_TEXT_HEIGHT (w);
11354 if (NUMBERP (aggressive))
11355 {
11356 double float_amount = XFLOATINT (aggressive) * height;
11357 amount_to_scroll = float_amount;
11358 if (amount_to_scroll == 0 && float_amount > 0)
11359 amount_to_scroll = 1;
11360 }
11361 }
11362
11363 if (amount_to_scroll <= 0)
11364 return SCROLLING_FAILED;
11365
11366 move_it_vertically_backward (&it, amount_to_scroll);
11367 startp = it.current.pos;
11368 }
11369 }
11370
11371 /* Run window scroll functions. */
11372 startp = run_window_scroll_functions (window, startp);
11373
11374 /* Display the window. Give up if new fonts are loaded, or if point
11375 doesn't appear. */
11376 if (!try_window (window, startp))
11377 rc = SCROLLING_NEED_LARGER_MATRICES;
11378 else if (w->cursor.vpos < 0)
11379 {
11380 clear_glyph_matrix (w->desired_matrix);
11381 rc = SCROLLING_FAILED;
11382 }
11383 else
11384 {
11385 /* Maybe forget recorded base line for line number display. */
11386 if (!just_this_one_p
11387 || current_buffer->clip_changed
11388 || BEG_UNCHANGED < CHARPOS (startp))
11389 w->base_line_number = Qnil;
11390
11391 /* If cursor ends up on a partially visible line,
11392 treat that as being off the bottom of the screen. */
11393 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11394 {
11395 clear_glyph_matrix (w->desired_matrix);
11396 ++extra_scroll_margin_lines;
11397 goto too_near_end;
11398 }
11399 rc = SCROLLING_SUCCESS;
11400 }
11401
11402 return rc;
11403 }
11404
11405
11406 /* Compute a suitable window start for window W if display of W starts
11407 on a continuation line. Value is non-zero if a new window start
11408 was computed.
11409
11410 The new window start will be computed, based on W's width, starting
11411 from the start of the continued line. It is the start of the
11412 screen line with the minimum distance from the old start W->start. */
11413
11414 static int
11415 compute_window_start_on_continuation_line (w)
11416 struct window *w;
11417 {
11418 struct text_pos pos, start_pos;
11419 int window_start_changed_p = 0;
11420
11421 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11422
11423 /* If window start is on a continuation line... Window start may be
11424 < BEGV in case there's invisible text at the start of the
11425 buffer (M-x rmail, for example). */
11426 if (CHARPOS (start_pos) > BEGV
11427 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11428 {
11429 struct it it;
11430 struct glyph_row *row;
11431
11432 /* Handle the case that the window start is out of range. */
11433 if (CHARPOS (start_pos) < BEGV)
11434 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11435 else if (CHARPOS (start_pos) > ZV)
11436 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11437
11438 /* Find the start of the continued line. This should be fast
11439 because scan_buffer is fast (newline cache). */
11440 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11441 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11442 row, DEFAULT_FACE_ID);
11443 reseat_at_previous_visible_line_start (&it);
11444
11445 /* If the line start is "too far" away from the window start,
11446 say it takes too much time to compute a new window start. */
11447 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11448 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11449 {
11450 int min_distance, distance;
11451
11452 /* Move forward by display lines to find the new window
11453 start. If window width was enlarged, the new start can
11454 be expected to be > the old start. If window width was
11455 decreased, the new window start will be < the old start.
11456 So, we're looking for the display line start with the
11457 minimum distance from the old window start. */
11458 pos = it.current.pos;
11459 min_distance = INFINITY;
11460 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11461 distance < min_distance)
11462 {
11463 min_distance = distance;
11464 pos = it.current.pos;
11465 move_it_by_lines (&it, 1, 0);
11466 }
11467
11468 /* Set the window start there. */
11469 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11470 window_start_changed_p = 1;
11471 }
11472 }
11473
11474 return window_start_changed_p;
11475 }
11476
11477
11478 /* Try cursor movement in case text has not changed in window WINDOW,
11479 with window start STARTP. Value is
11480
11481 CURSOR_MOVEMENT_SUCCESS if successful
11482
11483 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11484
11485 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11486 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11487 we want to scroll as if scroll-step were set to 1. See the code.
11488
11489 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11490 which case we have to abort this redisplay, and adjust matrices
11491 first. */
11492
11493 enum
11494 {
11495 CURSOR_MOVEMENT_SUCCESS,
11496 CURSOR_MOVEMENT_CANNOT_BE_USED,
11497 CURSOR_MOVEMENT_MUST_SCROLL,
11498 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11499 };
11500
11501 static int
11502 try_cursor_movement (window, startp, scroll_step)
11503 Lisp_Object window;
11504 struct text_pos startp;
11505 int *scroll_step;
11506 {
11507 struct window *w = XWINDOW (window);
11508 struct frame *f = XFRAME (w->frame);
11509 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11510
11511 #if GLYPH_DEBUG
11512 if (inhibit_try_cursor_movement)
11513 return rc;
11514 #endif
11515
11516 /* Handle case where text has not changed, only point, and it has
11517 not moved off the frame. */
11518 if (/* Point may be in this window. */
11519 PT >= CHARPOS (startp)
11520 /* Selective display hasn't changed. */
11521 && !current_buffer->clip_changed
11522 /* Function force-mode-line-update is used to force a thorough
11523 redisplay. It sets either windows_or_buffers_changed or
11524 update_mode_lines. So don't take a shortcut here for these
11525 cases. */
11526 && !update_mode_lines
11527 && !windows_or_buffers_changed
11528 && !cursor_type_changed
11529 /* Can't use this case if highlighting a region. When a
11530 region exists, cursor movement has to do more than just
11531 set the cursor. */
11532 && !(!NILP (Vtransient_mark_mode)
11533 && !NILP (current_buffer->mark_active))
11534 && NILP (w->region_showing)
11535 && NILP (Vshow_trailing_whitespace)
11536 /* Right after splitting windows, last_point may be nil. */
11537 && INTEGERP (w->last_point)
11538 /* This code is not used for mini-buffer for the sake of the case
11539 of redisplaying to replace an echo area message; since in
11540 that case the mini-buffer contents per se are usually
11541 unchanged. This code is of no real use in the mini-buffer
11542 since the handling of this_line_start_pos, etc., in redisplay
11543 handles the same cases. */
11544 && !EQ (window, minibuf_window)
11545 /* When splitting windows or for new windows, it happens that
11546 redisplay is called with a nil window_end_vpos or one being
11547 larger than the window. This should really be fixed in
11548 window.c. I don't have this on my list, now, so we do
11549 approximately the same as the old redisplay code. --gerd. */
11550 && INTEGERP (w->window_end_vpos)
11551 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11552 && (FRAME_WINDOW_P (f)
11553 || !overlay_arrow_in_current_buffer_p ()))
11554 {
11555 int this_scroll_margin, top_scroll_margin;
11556 struct glyph_row *row = NULL;
11557
11558 #if GLYPH_DEBUG
11559 debug_method_add (w, "cursor movement");
11560 #endif
11561
11562 /* Scroll if point within this distance from the top or bottom
11563 of the window. This is a pixel value. */
11564 this_scroll_margin = max (0, scroll_margin);
11565 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11566 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11567
11568 top_scroll_margin = this_scroll_margin;
11569 if (WINDOW_WANTS_HEADER_LINE_P (w))
11570 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11571
11572 /* Start with the row the cursor was displayed during the last
11573 not paused redisplay. Give up if that row is not valid. */
11574 if (w->last_cursor.vpos < 0
11575 || w->last_cursor.vpos >= w->current_matrix->nrows)
11576 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11577 else
11578 {
11579 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11580 if (row->mode_line_p)
11581 ++row;
11582 if (!row->enabled_p)
11583 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11584 }
11585
11586 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11587 {
11588 int scroll_p = 0;
11589 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11590
11591 if (PT > XFASTINT (w->last_point))
11592 {
11593 /* Point has moved forward. */
11594 while (MATRIX_ROW_END_CHARPOS (row) < PT
11595 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11596 {
11597 xassert (row->enabled_p);
11598 ++row;
11599 }
11600
11601 /* The end position of a row equals the start position
11602 of the next row. If PT is there, we would rather
11603 display it in the next line. */
11604 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11605 && MATRIX_ROW_END_CHARPOS (row) == PT
11606 && !cursor_row_p (w, row))
11607 ++row;
11608
11609 /* If within the scroll margin, scroll. Note that
11610 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11611 the next line would be drawn, and that
11612 this_scroll_margin can be zero. */
11613 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11614 || PT > MATRIX_ROW_END_CHARPOS (row)
11615 /* Line is completely visible last line in window
11616 and PT is to be set in the next line. */
11617 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11618 && PT == MATRIX_ROW_END_CHARPOS (row)
11619 && !row->ends_at_zv_p
11620 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11621 scroll_p = 1;
11622 }
11623 else if (PT < XFASTINT (w->last_point))
11624 {
11625 /* Cursor has to be moved backward. Note that PT >=
11626 CHARPOS (startp) because of the outer if-statement. */
11627 while (!row->mode_line_p
11628 && (MATRIX_ROW_START_CHARPOS (row) > PT
11629 || (MATRIX_ROW_START_CHARPOS (row) == PT
11630 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11631 && (row->y > top_scroll_margin
11632 || CHARPOS (startp) == BEGV))
11633 {
11634 xassert (row->enabled_p);
11635 --row;
11636 }
11637
11638 /* Consider the following case: Window starts at BEGV,
11639 there is invisible, intangible text at BEGV, so that
11640 display starts at some point START > BEGV. It can
11641 happen that we are called with PT somewhere between
11642 BEGV and START. Try to handle that case. */
11643 if (row < w->current_matrix->rows
11644 || row->mode_line_p)
11645 {
11646 row = w->current_matrix->rows;
11647 if (row->mode_line_p)
11648 ++row;
11649 }
11650
11651 /* Due to newlines in overlay strings, we may have to
11652 skip forward over overlay strings. */
11653 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11654 && MATRIX_ROW_END_CHARPOS (row) == PT
11655 && !cursor_row_p (w, row))
11656 ++row;
11657
11658 /* If within the scroll margin, scroll. */
11659 if (row->y < top_scroll_margin
11660 && CHARPOS (startp) != BEGV)
11661 scroll_p = 1;
11662 }
11663
11664 if (PT < MATRIX_ROW_START_CHARPOS (row)
11665 || PT > MATRIX_ROW_END_CHARPOS (row))
11666 {
11667 /* if PT is not in the glyph row, give up. */
11668 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11669 }
11670 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11671 && make_cursor_line_fully_visible_p)
11672 {
11673 if (PT == MATRIX_ROW_END_CHARPOS (row)
11674 && !row->ends_at_zv_p
11675 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11676 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11677 else if (row->height > window_box_height (w))
11678 {
11679 /* If we end up in a partially visible line, let's
11680 make it fully visible, except when it's taller
11681 than the window, in which case we can't do much
11682 about it. */
11683 *scroll_step = 1;
11684 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11685 }
11686 else
11687 {
11688 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11689 if (!make_cursor_line_fully_visible (w, 0))
11690 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11691 else
11692 rc = CURSOR_MOVEMENT_SUCCESS;
11693 }
11694 }
11695 else if (scroll_p)
11696 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11697 else
11698 {
11699 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11700 rc = CURSOR_MOVEMENT_SUCCESS;
11701 }
11702 }
11703 }
11704
11705 return rc;
11706 }
11707
11708 void
11709 set_vertical_scroll_bar (w)
11710 struct window *w;
11711 {
11712 int start, end, whole;
11713
11714 /* Calculate the start and end positions for the current window.
11715 At some point, it would be nice to choose between scrollbars
11716 which reflect the whole buffer size, with special markers
11717 indicating narrowing, and scrollbars which reflect only the
11718 visible region.
11719
11720 Note that mini-buffers sometimes aren't displaying any text. */
11721 if (!MINI_WINDOW_P (w)
11722 || (w == XWINDOW (minibuf_window)
11723 && NILP (echo_area_buffer[0])))
11724 {
11725 struct buffer *buf = XBUFFER (w->buffer);
11726 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11727 start = marker_position (w->start) - BUF_BEGV (buf);
11728 /* I don't think this is guaranteed to be right. For the
11729 moment, we'll pretend it is. */
11730 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11731
11732 if (end < start)
11733 end = start;
11734 if (whole < (end - start))
11735 whole = end - start;
11736 }
11737 else
11738 start = end = whole = 0;
11739
11740 /* Indicate what this scroll bar ought to be displaying now. */
11741 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11742 }
11743
11744
11745 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11746 selected_window is redisplayed.
11747
11748 We can return without actually redisplaying the window if
11749 fonts_changed_p is nonzero. In that case, redisplay_internal will
11750 retry. */
11751
11752 static void
11753 redisplay_window (window, just_this_one_p)
11754 Lisp_Object window;
11755 int just_this_one_p;
11756 {
11757 struct window *w = XWINDOW (window);
11758 struct frame *f = XFRAME (w->frame);
11759 struct buffer *buffer = XBUFFER (w->buffer);
11760 struct buffer *old = current_buffer;
11761 struct text_pos lpoint, opoint, startp;
11762 int update_mode_line;
11763 int tem;
11764 struct it it;
11765 /* Record it now because it's overwritten. */
11766 int current_matrix_up_to_date_p = 0;
11767 int used_current_matrix_p = 0;
11768 /* This is less strict than current_matrix_up_to_date_p.
11769 It indictes that the buffer contents and narrowing are unchanged. */
11770 int buffer_unchanged_p = 0;
11771 int temp_scroll_step = 0;
11772 int count = SPECPDL_INDEX ();
11773 int rc;
11774 int centering_position;
11775 int last_line_misfit = 0;
11776
11777 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11778 opoint = lpoint;
11779
11780 /* W must be a leaf window here. */
11781 xassert (!NILP (w->buffer));
11782 #if GLYPH_DEBUG
11783 *w->desired_matrix->method = 0;
11784 #endif
11785
11786 specbind (Qinhibit_point_motion_hooks, Qt);
11787
11788 reconsider_clip_changes (w, buffer);
11789
11790 /* Has the mode line to be updated? */
11791 update_mode_line = (!NILP (w->update_mode_line)
11792 || update_mode_lines
11793 || buffer->clip_changed
11794 || buffer->prevent_redisplay_optimizations_p);
11795
11796 if (MINI_WINDOW_P (w))
11797 {
11798 if (w == XWINDOW (echo_area_window)
11799 && !NILP (echo_area_buffer[0]))
11800 {
11801 if (update_mode_line)
11802 /* We may have to update a tty frame's menu bar or a
11803 tool-bar. Example `M-x C-h C-h C-g'. */
11804 goto finish_menu_bars;
11805 else
11806 /* We've already displayed the echo area glyphs in this window. */
11807 goto finish_scroll_bars;
11808 }
11809 else if ((w != XWINDOW (minibuf_window)
11810 || minibuf_level == 0)
11811 /* When buffer is nonempty, redisplay window normally. */
11812 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11813 /* Quail displays non-mini buffers in minibuffer window.
11814 In that case, redisplay the window normally. */
11815 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11816 {
11817 /* W is a mini-buffer window, but it's not active, so clear
11818 it. */
11819 int yb = window_text_bottom_y (w);
11820 struct glyph_row *row;
11821 int y;
11822
11823 for (y = 0, row = w->desired_matrix->rows;
11824 y < yb;
11825 y += row->height, ++row)
11826 blank_row (w, row, y);
11827 goto finish_scroll_bars;
11828 }
11829
11830 clear_glyph_matrix (w->desired_matrix);
11831 }
11832
11833 /* Otherwise set up data on this window; select its buffer and point
11834 value. */
11835 /* Really select the buffer, for the sake of buffer-local
11836 variables. */
11837 set_buffer_internal_1 (XBUFFER (w->buffer));
11838 SET_TEXT_POS (opoint, PT, PT_BYTE);
11839
11840 current_matrix_up_to_date_p
11841 = (!NILP (w->window_end_valid)
11842 && !current_buffer->clip_changed
11843 && !current_buffer->prevent_redisplay_optimizations_p
11844 && XFASTINT (w->last_modified) >= MODIFF
11845 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11846
11847 buffer_unchanged_p
11848 = (!NILP (w->window_end_valid)
11849 && !current_buffer->clip_changed
11850 && XFASTINT (w->last_modified) >= MODIFF
11851 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11852
11853 /* When windows_or_buffers_changed is non-zero, we can't rely on
11854 the window end being valid, so set it to nil there. */
11855 if (windows_or_buffers_changed)
11856 {
11857 /* If window starts on a continuation line, maybe adjust the
11858 window start in case the window's width changed. */
11859 if (XMARKER (w->start)->buffer == current_buffer)
11860 compute_window_start_on_continuation_line (w);
11861
11862 w->window_end_valid = Qnil;
11863 }
11864
11865 /* Some sanity checks. */
11866 CHECK_WINDOW_END (w);
11867 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11868 abort ();
11869 if (BYTEPOS (opoint) < CHARPOS (opoint))
11870 abort ();
11871
11872 /* If %c is in mode line, update it if needed. */
11873 if (!NILP (w->column_number_displayed)
11874 /* This alternative quickly identifies a common case
11875 where no change is needed. */
11876 && !(PT == XFASTINT (w->last_point)
11877 && XFASTINT (w->last_modified) >= MODIFF
11878 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11879 && (XFASTINT (w->column_number_displayed)
11880 != (int) current_column ())) /* iftc */
11881 update_mode_line = 1;
11882
11883 /* Count number of windows showing the selected buffer. An indirect
11884 buffer counts as its base buffer. */
11885 if (!just_this_one_p)
11886 {
11887 struct buffer *current_base, *window_base;
11888 current_base = current_buffer;
11889 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11890 if (current_base->base_buffer)
11891 current_base = current_base->base_buffer;
11892 if (window_base->base_buffer)
11893 window_base = window_base->base_buffer;
11894 if (current_base == window_base)
11895 buffer_shared++;
11896 }
11897
11898 /* Point refers normally to the selected window. For any other
11899 window, set up appropriate value. */
11900 if (!EQ (window, selected_window))
11901 {
11902 int new_pt = XMARKER (w->pointm)->charpos;
11903 int new_pt_byte = marker_byte_position (w->pointm);
11904 if (new_pt < BEGV)
11905 {
11906 new_pt = BEGV;
11907 new_pt_byte = BEGV_BYTE;
11908 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11909 }
11910 else if (new_pt > (ZV - 1))
11911 {
11912 new_pt = ZV;
11913 new_pt_byte = ZV_BYTE;
11914 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11915 }
11916
11917 /* We don't use SET_PT so that the point-motion hooks don't run. */
11918 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11919 }
11920
11921 /* If any of the character widths specified in the display table
11922 have changed, invalidate the width run cache. It's true that
11923 this may be a bit late to catch such changes, but the rest of
11924 redisplay goes (non-fatally) haywire when the display table is
11925 changed, so why should we worry about doing any better? */
11926 if (current_buffer->width_run_cache)
11927 {
11928 struct Lisp_Char_Table *disptab = buffer_display_table ();
11929
11930 if (! disptab_matches_widthtab (disptab,
11931 XVECTOR (current_buffer->width_table)))
11932 {
11933 invalidate_region_cache (current_buffer,
11934 current_buffer->width_run_cache,
11935 BEG, Z);
11936 recompute_width_table (current_buffer, disptab);
11937 }
11938 }
11939
11940 /* If window-start is screwed up, choose a new one. */
11941 if (XMARKER (w->start)->buffer != current_buffer)
11942 goto recenter;
11943
11944 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11945
11946 /* If someone specified a new starting point but did not insist,
11947 check whether it can be used. */
11948 if (!NILP (w->optional_new_start)
11949 && CHARPOS (startp) >= BEGV
11950 && CHARPOS (startp) <= ZV)
11951 {
11952 w->optional_new_start = Qnil;
11953 start_display (&it, w, startp);
11954 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11955 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11956 if (IT_CHARPOS (it) == PT)
11957 w->force_start = Qt;
11958 /* IT may overshoot PT if text at PT is invisible. */
11959 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11960 w->force_start = Qt;
11961
11962
11963 }
11964
11965 /* Handle case where place to start displaying has been specified,
11966 unless the specified location is outside the accessible range. */
11967 if (!NILP (w->force_start)
11968 || w->frozen_window_start_p)
11969 {
11970 /* We set this later on if we have to adjust point. */
11971 int new_vpos = -1;
11972
11973 w->force_start = Qnil;
11974 w->vscroll = 0;
11975 w->window_end_valid = Qnil;
11976
11977 /* Forget any recorded base line for line number display. */
11978 if (!buffer_unchanged_p)
11979 w->base_line_number = Qnil;
11980
11981 /* Redisplay the mode line. Select the buffer properly for that.
11982 Also, run the hook window-scroll-functions
11983 because we have scrolled. */
11984 /* Note, we do this after clearing force_start because
11985 if there's an error, it is better to forget about force_start
11986 than to get into an infinite loop calling the hook functions
11987 and having them get more errors. */
11988 if (!update_mode_line
11989 || ! NILP (Vwindow_scroll_functions))
11990 {
11991 update_mode_line = 1;
11992 w->update_mode_line = Qt;
11993 startp = run_window_scroll_functions (window, startp);
11994 }
11995
11996 w->last_modified = make_number (0);
11997 w->last_overlay_modified = make_number (0);
11998 if (CHARPOS (startp) < BEGV)
11999 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12000 else if (CHARPOS (startp) > ZV)
12001 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12002
12003 /* Redisplay, then check if cursor has been set during the
12004 redisplay. Give up if new fonts were loaded. */
12005 if (!try_window (window, startp))
12006 {
12007 w->force_start = Qt;
12008 clear_glyph_matrix (w->desired_matrix);
12009 goto need_larger_matrices;
12010 }
12011
12012 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12013 {
12014 /* If point does not appear, try to move point so it does
12015 appear. The desired matrix has been built above, so we
12016 can use it here. */
12017 new_vpos = window_box_height (w) / 2;
12018 }
12019
12020 if (!make_cursor_line_fully_visible (w, 0))
12021 {
12022 /* Point does appear, but on a line partly visible at end of window.
12023 Move it back to a fully-visible line. */
12024 new_vpos = window_box_height (w);
12025 }
12026
12027 /* If we need to move point for either of the above reasons,
12028 now actually do it. */
12029 if (new_vpos >= 0)
12030 {
12031 struct glyph_row *row;
12032
12033 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12034 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12035 ++row;
12036
12037 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12038 MATRIX_ROW_START_BYTEPOS (row));
12039
12040 if (w != XWINDOW (selected_window))
12041 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12042 else if (current_buffer == old)
12043 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12044
12045 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12046
12047 /* If we are highlighting the region, then we just changed
12048 the region, so redisplay to show it. */
12049 if (!NILP (Vtransient_mark_mode)
12050 && !NILP (current_buffer->mark_active))
12051 {
12052 clear_glyph_matrix (w->desired_matrix);
12053 if (!try_window (window, startp))
12054 goto need_larger_matrices;
12055 }
12056 }
12057
12058 #if GLYPH_DEBUG
12059 debug_method_add (w, "forced window start");
12060 #endif
12061 goto done;
12062 }
12063
12064 /* Handle case where text has not changed, only point, and it has
12065 not moved off the frame, and we are not retrying after hscroll.
12066 (current_matrix_up_to_date_p is nonzero when retrying.) */
12067 if (current_matrix_up_to_date_p
12068 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12069 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12070 {
12071 switch (rc)
12072 {
12073 case CURSOR_MOVEMENT_SUCCESS:
12074 used_current_matrix_p = 1;
12075 goto done;
12076
12077 #if 0 /* try_cursor_movement never returns this value. */
12078 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12079 goto need_larger_matrices;
12080 #endif
12081
12082 case CURSOR_MOVEMENT_MUST_SCROLL:
12083 goto try_to_scroll;
12084
12085 default:
12086 abort ();
12087 }
12088 }
12089 /* If current starting point was originally the beginning of a line
12090 but no longer is, find a new starting point. */
12091 else if (!NILP (w->start_at_line_beg)
12092 && !(CHARPOS (startp) <= BEGV
12093 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12094 {
12095 #if GLYPH_DEBUG
12096 debug_method_add (w, "recenter 1");
12097 #endif
12098 goto recenter;
12099 }
12100
12101 /* Try scrolling with try_window_id. Value is > 0 if update has
12102 been done, it is -1 if we know that the same window start will
12103 not work. It is 0 if unsuccessful for some other reason. */
12104 else if ((tem = try_window_id (w)) != 0)
12105 {
12106 #if GLYPH_DEBUG
12107 debug_method_add (w, "try_window_id %d", tem);
12108 #endif
12109
12110 if (fonts_changed_p)
12111 goto need_larger_matrices;
12112 if (tem > 0)
12113 goto done;
12114
12115 /* Otherwise try_window_id has returned -1 which means that we
12116 don't want the alternative below this comment to execute. */
12117 }
12118 else if (CHARPOS (startp) >= BEGV
12119 && CHARPOS (startp) <= ZV
12120 && PT >= CHARPOS (startp)
12121 && (CHARPOS (startp) < ZV
12122 /* Avoid starting at end of buffer. */
12123 || CHARPOS (startp) == BEGV
12124 || (XFASTINT (w->last_modified) >= MODIFF
12125 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12126 {
12127 #if GLYPH_DEBUG
12128 debug_method_add (w, "same window start");
12129 #endif
12130
12131 /* Try to redisplay starting at same place as before.
12132 If point has not moved off frame, accept the results. */
12133 if (!current_matrix_up_to_date_p
12134 /* Don't use try_window_reusing_current_matrix in this case
12135 because a window scroll function can have changed the
12136 buffer. */
12137 || !NILP (Vwindow_scroll_functions)
12138 || MINI_WINDOW_P (w)
12139 || !(used_current_matrix_p
12140 = try_window_reusing_current_matrix (w)))
12141 {
12142 IF_DEBUG (debug_method_add (w, "1"));
12143 try_window (window, startp);
12144 }
12145
12146 if (fonts_changed_p)
12147 goto need_larger_matrices;
12148
12149 if (w->cursor.vpos >= 0)
12150 {
12151 if (!just_this_one_p
12152 || current_buffer->clip_changed
12153 || BEG_UNCHANGED < CHARPOS (startp))
12154 /* Forget any recorded base line for line number display. */
12155 w->base_line_number = Qnil;
12156
12157 if (!make_cursor_line_fully_visible (w, 1))
12158 {
12159 clear_glyph_matrix (w->desired_matrix);
12160 last_line_misfit = 1;
12161 }
12162 /* Drop through and scroll. */
12163 else
12164 goto done;
12165 }
12166 else
12167 clear_glyph_matrix (w->desired_matrix);
12168 }
12169
12170 try_to_scroll:
12171
12172 w->last_modified = make_number (0);
12173 w->last_overlay_modified = make_number (0);
12174
12175 /* Redisplay the mode line. Select the buffer properly for that. */
12176 if (!update_mode_line)
12177 {
12178 update_mode_line = 1;
12179 w->update_mode_line = Qt;
12180 }
12181
12182 /* Try to scroll by specified few lines. */
12183 if ((scroll_conservatively
12184 || scroll_step
12185 || temp_scroll_step
12186 || NUMBERP (current_buffer->scroll_up_aggressively)
12187 || NUMBERP (current_buffer->scroll_down_aggressively))
12188 && !current_buffer->clip_changed
12189 && CHARPOS (startp) >= BEGV
12190 && CHARPOS (startp) <= ZV)
12191 {
12192 /* The function returns -1 if new fonts were loaded, 1 if
12193 successful, 0 if not successful. */
12194 int rc = try_scrolling (window, just_this_one_p,
12195 scroll_conservatively,
12196 scroll_step,
12197 temp_scroll_step, last_line_misfit);
12198 switch (rc)
12199 {
12200 case SCROLLING_SUCCESS:
12201 goto done;
12202
12203 case SCROLLING_NEED_LARGER_MATRICES:
12204 goto need_larger_matrices;
12205
12206 case SCROLLING_FAILED:
12207 break;
12208
12209 default:
12210 abort ();
12211 }
12212 }
12213
12214 /* Finally, just choose place to start which centers point */
12215
12216 recenter:
12217 centering_position = window_box_height (w) / 2;
12218
12219 point_at_top:
12220 /* Jump here with centering_position already set to 0. */
12221
12222 #if GLYPH_DEBUG
12223 debug_method_add (w, "recenter");
12224 #endif
12225
12226 /* w->vscroll = 0; */
12227
12228 /* Forget any previously recorded base line for line number display. */
12229 if (!buffer_unchanged_p)
12230 w->base_line_number = Qnil;
12231
12232 /* Move backward half the height of the window. */
12233 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12234 it.current_y = it.last_visible_y;
12235 move_it_vertically_backward (&it, centering_position);
12236 xassert (IT_CHARPOS (it) >= BEGV);
12237
12238 /* The function move_it_vertically_backward may move over more
12239 than the specified y-distance. If it->w is small, e.g. a
12240 mini-buffer window, we may end up in front of the window's
12241 display area. Start displaying at the start of the line
12242 containing PT in this case. */
12243 if (it.current_y <= 0)
12244 {
12245 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12246 move_it_vertically_backward (&it, 0);
12247 xassert (IT_CHARPOS (it) <= PT);
12248 it.current_y = 0;
12249 }
12250
12251 it.current_x = it.hpos = 0;
12252
12253 /* Set startp here explicitly in case that helps avoid an infinite loop
12254 in case the window-scroll-functions functions get errors. */
12255 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12256
12257 /* Run scroll hooks. */
12258 startp = run_window_scroll_functions (window, it.current.pos);
12259
12260 /* Redisplay the window. */
12261 if (!current_matrix_up_to_date_p
12262 || windows_or_buffers_changed
12263 || cursor_type_changed
12264 /* Don't use try_window_reusing_current_matrix in this case
12265 because it can have changed the buffer. */
12266 || !NILP (Vwindow_scroll_functions)
12267 || !just_this_one_p
12268 || MINI_WINDOW_P (w)
12269 || !(used_current_matrix_p
12270 = try_window_reusing_current_matrix (w)))
12271 try_window (window, startp);
12272
12273 /* If new fonts have been loaded (due to fontsets), give up. We
12274 have to start a new redisplay since we need to re-adjust glyph
12275 matrices. */
12276 if (fonts_changed_p)
12277 goto need_larger_matrices;
12278
12279 /* If cursor did not appear assume that the middle of the window is
12280 in the first line of the window. Do it again with the next line.
12281 (Imagine a window of height 100, displaying two lines of height
12282 60. Moving back 50 from it->last_visible_y will end in the first
12283 line.) */
12284 if (w->cursor.vpos < 0)
12285 {
12286 if (!NILP (w->window_end_valid)
12287 && PT >= Z - XFASTINT (w->window_end_pos))
12288 {
12289 clear_glyph_matrix (w->desired_matrix);
12290 move_it_by_lines (&it, 1, 0);
12291 try_window (window, it.current.pos);
12292 }
12293 else if (PT < IT_CHARPOS (it))
12294 {
12295 clear_glyph_matrix (w->desired_matrix);
12296 move_it_by_lines (&it, -1, 0);
12297 try_window (window, it.current.pos);
12298 }
12299 else
12300 {
12301 /* Not much we can do about it. */
12302 }
12303 }
12304
12305 /* Consider the following case: Window starts at BEGV, there is
12306 invisible, intangible text at BEGV, so that display starts at
12307 some point START > BEGV. It can happen that we are called with
12308 PT somewhere between BEGV and START. Try to handle that case. */
12309 if (w->cursor.vpos < 0)
12310 {
12311 struct glyph_row *row = w->current_matrix->rows;
12312 if (row->mode_line_p)
12313 ++row;
12314 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12315 }
12316
12317 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12318 {
12319 /* If vscroll is enabled, disable it and try again. */
12320 if (w->vscroll)
12321 {
12322 w->vscroll = 0;
12323 clear_glyph_matrix (w->desired_matrix);
12324 goto recenter;
12325 }
12326
12327 /* If centering point failed to make the whole line visible,
12328 put point at the top instead. That has to make the whole line
12329 visible, if it can be done. */
12330 if (centering_position == 0)
12331 goto done;
12332 clear_glyph_matrix (w->desired_matrix);
12333 centering_position = 0;
12334 goto point_at_top;
12335 }
12336
12337 done:
12338
12339 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12340 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12341 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12342 ? Qt : Qnil);
12343
12344 /* Display the mode line, if we must. */
12345 if ((update_mode_line
12346 /* If window not full width, must redo its mode line
12347 if (a) the window to its side is being redone and
12348 (b) we do a frame-based redisplay. This is a consequence
12349 of how inverted lines are drawn in frame-based redisplay. */
12350 || (!just_this_one_p
12351 && !FRAME_WINDOW_P (f)
12352 && !WINDOW_FULL_WIDTH_P (w))
12353 /* Line number to display. */
12354 || INTEGERP (w->base_line_pos)
12355 /* Column number is displayed and different from the one displayed. */
12356 || (!NILP (w->column_number_displayed)
12357 && (XFASTINT (w->column_number_displayed)
12358 != (int) current_column ()))) /* iftc */
12359 /* This means that the window has a mode line. */
12360 && (WINDOW_WANTS_MODELINE_P (w)
12361 || WINDOW_WANTS_HEADER_LINE_P (w)))
12362 {
12363 display_mode_lines (w);
12364
12365 /* If mode line height has changed, arrange for a thorough
12366 immediate redisplay using the correct mode line height. */
12367 if (WINDOW_WANTS_MODELINE_P (w)
12368 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12369 {
12370 fonts_changed_p = 1;
12371 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12372 = DESIRED_MODE_LINE_HEIGHT (w);
12373 }
12374
12375 /* If top line height has changed, arrange for a thorough
12376 immediate redisplay using the correct mode line height. */
12377 if (WINDOW_WANTS_HEADER_LINE_P (w)
12378 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12379 {
12380 fonts_changed_p = 1;
12381 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12382 = DESIRED_HEADER_LINE_HEIGHT (w);
12383 }
12384
12385 if (fonts_changed_p)
12386 goto need_larger_matrices;
12387 }
12388
12389 if (!line_number_displayed
12390 && !BUFFERP (w->base_line_pos))
12391 {
12392 w->base_line_pos = Qnil;
12393 w->base_line_number = Qnil;
12394 }
12395
12396 finish_menu_bars:
12397
12398 /* When we reach a frame's selected window, redo the frame's menu bar. */
12399 if (update_mode_line
12400 && EQ (FRAME_SELECTED_WINDOW (f), window))
12401 {
12402 int redisplay_menu_p = 0;
12403 int redisplay_tool_bar_p = 0;
12404
12405 if (FRAME_WINDOW_P (f))
12406 {
12407 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12408 || defined (USE_GTK)
12409 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12410 #else
12411 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12412 #endif
12413 }
12414 else
12415 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12416
12417 if (redisplay_menu_p)
12418 display_menu_bar (w);
12419
12420 #ifdef HAVE_WINDOW_SYSTEM
12421 #ifdef USE_GTK
12422 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12423 #else
12424 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12425 && (FRAME_TOOL_BAR_LINES (f) > 0
12426 || auto_resize_tool_bars_p);
12427
12428 #endif
12429
12430 if (redisplay_tool_bar_p)
12431 redisplay_tool_bar (f);
12432 #endif
12433 }
12434
12435 #ifdef HAVE_WINDOW_SYSTEM
12436 if (FRAME_WINDOW_P (f)
12437 && update_window_fringes (w, 0)
12438 && !just_this_one_p
12439 && (used_current_matrix_p || overlay_arrow_seen)
12440 && !w->pseudo_window_p)
12441 {
12442 update_begin (f);
12443 BLOCK_INPUT;
12444 if (draw_window_fringes (w, 1))
12445 x_draw_vertical_border (w);
12446 UNBLOCK_INPUT;
12447 update_end (f);
12448 }
12449 #endif /* HAVE_WINDOW_SYSTEM */
12450
12451 /* We go to this label, with fonts_changed_p nonzero,
12452 if it is necessary to try again using larger glyph matrices.
12453 We have to redeem the scroll bar even in this case,
12454 because the loop in redisplay_internal expects that. */
12455 need_larger_matrices:
12456 ;
12457 finish_scroll_bars:
12458
12459 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12460 {
12461 /* Set the thumb's position and size. */
12462 set_vertical_scroll_bar (w);
12463
12464 /* Note that we actually used the scroll bar attached to this
12465 window, so it shouldn't be deleted at the end of redisplay. */
12466 redeem_scroll_bar_hook (w);
12467 }
12468
12469 /* Restore current_buffer and value of point in it. */
12470 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12471 set_buffer_internal_1 (old);
12472 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12473
12474 unbind_to (count, Qnil);
12475 }
12476
12477
12478 /* Build the complete desired matrix of WINDOW with a window start
12479 buffer position POS. Value is non-zero if successful. It is zero
12480 if fonts were loaded during redisplay which makes re-adjusting
12481 glyph matrices necessary. */
12482
12483 int
12484 try_window (window, pos)
12485 Lisp_Object window;
12486 struct text_pos pos;
12487 {
12488 struct window *w = XWINDOW (window);
12489 struct it it;
12490 struct glyph_row *last_text_row = NULL;
12491
12492 /* Make POS the new window start. */
12493 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12494
12495 /* Mark cursor position as unknown. No overlay arrow seen. */
12496 w->cursor.vpos = -1;
12497 overlay_arrow_seen = 0;
12498
12499 /* Initialize iterator and info to start at POS. */
12500 start_display (&it, w, pos);
12501
12502 /* Display all lines of W. */
12503 while (it.current_y < it.last_visible_y)
12504 {
12505 if (display_line (&it))
12506 last_text_row = it.glyph_row - 1;
12507 if (fonts_changed_p)
12508 return 0;
12509 }
12510
12511 /* If bottom moved off end of frame, change mode line percentage. */
12512 if (XFASTINT (w->window_end_pos) <= 0
12513 && Z != IT_CHARPOS (it))
12514 w->update_mode_line = Qt;
12515
12516 /* Set window_end_pos to the offset of the last character displayed
12517 on the window from the end of current_buffer. Set
12518 window_end_vpos to its row number. */
12519 if (last_text_row)
12520 {
12521 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12522 w->window_end_bytepos
12523 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12524 w->window_end_pos
12525 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12526 w->window_end_vpos
12527 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12528 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12529 ->displays_text_p);
12530 }
12531 else
12532 {
12533 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12534 w->window_end_pos = make_number (Z - ZV);
12535 w->window_end_vpos = make_number (0);
12536 }
12537
12538 /* But that is not valid info until redisplay finishes. */
12539 w->window_end_valid = Qnil;
12540 return 1;
12541 }
12542
12543
12544 \f
12545 /************************************************************************
12546 Window redisplay reusing current matrix when buffer has not changed
12547 ************************************************************************/
12548
12549 /* Try redisplay of window W showing an unchanged buffer with a
12550 different window start than the last time it was displayed by
12551 reusing its current matrix. Value is non-zero if successful.
12552 W->start is the new window start. */
12553
12554 static int
12555 try_window_reusing_current_matrix (w)
12556 struct window *w;
12557 {
12558 struct frame *f = XFRAME (w->frame);
12559 struct glyph_row *row, *bottom_row;
12560 struct it it;
12561 struct run run;
12562 struct text_pos start, new_start;
12563 int nrows_scrolled, i;
12564 struct glyph_row *last_text_row;
12565 struct glyph_row *last_reused_text_row;
12566 struct glyph_row *start_row;
12567 int start_vpos, min_y, max_y;
12568
12569 #if GLYPH_DEBUG
12570 if (inhibit_try_window_reusing)
12571 return 0;
12572 #endif
12573
12574 if (/* This function doesn't handle terminal frames. */
12575 !FRAME_WINDOW_P (f)
12576 /* Don't try to reuse the display if windows have been split
12577 or such. */
12578 || windows_or_buffers_changed
12579 || cursor_type_changed)
12580 return 0;
12581
12582 /* Can't do this if region may have changed. */
12583 if ((!NILP (Vtransient_mark_mode)
12584 && !NILP (current_buffer->mark_active))
12585 || !NILP (w->region_showing)
12586 || !NILP (Vshow_trailing_whitespace))
12587 return 0;
12588
12589 /* If top-line visibility has changed, give up. */
12590 if (WINDOW_WANTS_HEADER_LINE_P (w)
12591 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12592 return 0;
12593
12594 /* Give up if old or new display is scrolled vertically. We could
12595 make this function handle this, but right now it doesn't. */
12596 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12597 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12598 return 0;
12599
12600 /* The variable new_start now holds the new window start. The old
12601 start `start' can be determined from the current matrix. */
12602 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12603 start = start_row->start.pos;
12604 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12605
12606 /* Clear the desired matrix for the display below. */
12607 clear_glyph_matrix (w->desired_matrix);
12608
12609 if (CHARPOS (new_start) <= CHARPOS (start))
12610 {
12611 int first_row_y;
12612
12613 /* Don't use this method if the display starts with an ellipsis
12614 displayed for invisible text. It's not easy to handle that case
12615 below, and it's certainly not worth the effort since this is
12616 not a frequent case. */
12617 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12618 return 0;
12619
12620 IF_DEBUG (debug_method_add (w, "twu1"));
12621
12622 /* Display up to a row that can be reused. The variable
12623 last_text_row is set to the last row displayed that displays
12624 text. Note that it.vpos == 0 if or if not there is a
12625 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12626 start_display (&it, w, new_start);
12627 first_row_y = it.current_y;
12628 w->cursor.vpos = -1;
12629 last_text_row = last_reused_text_row = NULL;
12630
12631 while (it.current_y < it.last_visible_y
12632 && !fonts_changed_p)
12633 {
12634 /* If we have reached into the characters in the START row,
12635 that means the line boundaries have changed. So we
12636 can't start copying with the row START. Maybe it will
12637 work to start copying with the following row. */
12638 while (IT_CHARPOS (it) > CHARPOS (start))
12639 {
12640 /* Advance to the next row as the "start". */
12641 start_row++;
12642 start = start_row->start.pos;
12643 /* If there are no more rows to try, or just one, give up. */
12644 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12645 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12646 || CHARPOS (start) == ZV)
12647 {
12648 clear_glyph_matrix (w->desired_matrix);
12649 return 0;
12650 }
12651
12652 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12653 }
12654 /* If we have reached alignment,
12655 we can copy the rest of the rows. */
12656 if (IT_CHARPOS (it) == CHARPOS (start))
12657 break;
12658
12659 if (display_line (&it))
12660 last_text_row = it.glyph_row - 1;
12661 }
12662
12663 /* A value of current_y < last_visible_y means that we stopped
12664 at the previous window start, which in turn means that we
12665 have at least one reusable row. */
12666 if (it.current_y < it.last_visible_y)
12667 {
12668 /* IT.vpos always starts from 0; it counts text lines. */
12669 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12670
12671 /* Find PT if not already found in the lines displayed. */
12672 if (w->cursor.vpos < 0)
12673 {
12674 int dy = it.current_y - start_row->y;
12675
12676 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12677 row = row_containing_pos (w, PT, row, NULL, dy);
12678 if (row)
12679 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12680 dy, nrows_scrolled);
12681 else
12682 {
12683 clear_glyph_matrix (w->desired_matrix);
12684 return 0;
12685 }
12686 }
12687
12688 /* Scroll the display. Do it before the current matrix is
12689 changed. The problem here is that update has not yet
12690 run, i.e. part of the current matrix is not up to date.
12691 scroll_run_hook will clear the cursor, and use the
12692 current matrix to get the height of the row the cursor is
12693 in. */
12694 run.current_y = start_row->y;
12695 run.desired_y = it.current_y;
12696 run.height = it.last_visible_y - it.current_y;
12697
12698 if (run.height > 0 && run.current_y != run.desired_y)
12699 {
12700 update_begin (f);
12701 rif->update_window_begin_hook (w);
12702 rif->clear_window_mouse_face (w);
12703 rif->scroll_run_hook (w, &run);
12704 rif->update_window_end_hook (w, 0, 0);
12705 update_end (f);
12706 }
12707
12708 /* Shift current matrix down by nrows_scrolled lines. */
12709 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12710 rotate_matrix (w->current_matrix,
12711 start_vpos,
12712 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12713 nrows_scrolled);
12714
12715 /* Disable lines that must be updated. */
12716 for (i = 0; i < it.vpos; ++i)
12717 (start_row + i)->enabled_p = 0;
12718
12719 /* Re-compute Y positions. */
12720 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12721 max_y = it.last_visible_y;
12722 for (row = start_row + nrows_scrolled;
12723 row < bottom_row;
12724 ++row)
12725 {
12726 row->y = it.current_y;
12727 row->visible_height = row->height;
12728
12729 if (row->y < min_y)
12730 row->visible_height -= min_y - row->y;
12731 if (row->y + row->height > max_y)
12732 row->visible_height -= row->y + row->height - max_y;
12733 row->redraw_fringe_bitmaps_p = 1;
12734
12735 it.current_y += row->height;
12736
12737 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12738 last_reused_text_row = row;
12739 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12740 break;
12741 }
12742
12743 /* Disable lines in the current matrix which are now
12744 below the window. */
12745 for (++row; row < bottom_row; ++row)
12746 row->enabled_p = 0;
12747 }
12748
12749 /* Update window_end_pos etc.; last_reused_text_row is the last
12750 reused row from the current matrix containing text, if any.
12751 The value of last_text_row is the last displayed line
12752 containing text. */
12753 if (last_reused_text_row)
12754 {
12755 w->window_end_bytepos
12756 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12757 w->window_end_pos
12758 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12759 w->window_end_vpos
12760 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12761 w->current_matrix));
12762 }
12763 else if (last_text_row)
12764 {
12765 w->window_end_bytepos
12766 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12767 w->window_end_pos
12768 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12769 w->window_end_vpos
12770 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12771 }
12772 else
12773 {
12774 /* This window must be completely empty. */
12775 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12776 w->window_end_pos = make_number (Z - ZV);
12777 w->window_end_vpos = make_number (0);
12778 }
12779 w->window_end_valid = Qnil;
12780
12781 /* Update hint: don't try scrolling again in update_window. */
12782 w->desired_matrix->no_scrolling_p = 1;
12783
12784 #if GLYPH_DEBUG
12785 debug_method_add (w, "try_window_reusing_current_matrix 1");
12786 #endif
12787 return 1;
12788 }
12789 else if (CHARPOS (new_start) > CHARPOS (start))
12790 {
12791 struct glyph_row *pt_row, *row;
12792 struct glyph_row *first_reusable_row;
12793 struct glyph_row *first_row_to_display;
12794 int dy;
12795 int yb = window_text_bottom_y (w);
12796
12797 /* Find the row starting at new_start, if there is one. Don't
12798 reuse a partially visible line at the end. */
12799 first_reusable_row = start_row;
12800 while (first_reusable_row->enabled_p
12801 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12802 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12803 < CHARPOS (new_start)))
12804 ++first_reusable_row;
12805
12806 /* Give up if there is no row to reuse. */
12807 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12808 || !first_reusable_row->enabled_p
12809 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12810 != CHARPOS (new_start)))
12811 return 0;
12812
12813 /* We can reuse fully visible rows beginning with
12814 first_reusable_row to the end of the window. Set
12815 first_row_to_display to the first row that cannot be reused.
12816 Set pt_row to the row containing point, if there is any. */
12817 pt_row = NULL;
12818 for (first_row_to_display = first_reusable_row;
12819 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12820 ++first_row_to_display)
12821 {
12822 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12823 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12824 pt_row = first_row_to_display;
12825 }
12826
12827 /* Start displaying at the start of first_row_to_display. */
12828 xassert (first_row_to_display->y < yb);
12829 init_to_row_start (&it, w, first_row_to_display);
12830
12831 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12832 - start_vpos);
12833 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12834 - nrows_scrolled);
12835 it.current_y = (first_row_to_display->y - first_reusable_row->y
12836 + WINDOW_HEADER_LINE_HEIGHT (w));
12837
12838 /* Display lines beginning with first_row_to_display in the
12839 desired matrix. Set last_text_row to the last row displayed
12840 that displays text. */
12841 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12842 if (pt_row == NULL)
12843 w->cursor.vpos = -1;
12844 last_text_row = NULL;
12845 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12846 if (display_line (&it))
12847 last_text_row = it.glyph_row - 1;
12848
12849 /* Give up If point isn't in a row displayed or reused. */
12850 if (w->cursor.vpos < 0)
12851 {
12852 clear_glyph_matrix (w->desired_matrix);
12853 return 0;
12854 }
12855
12856 /* If point is in a reused row, adjust y and vpos of the cursor
12857 position. */
12858 if (pt_row)
12859 {
12860 w->cursor.vpos -= nrows_scrolled;
12861 w->cursor.y -= first_reusable_row->y - start_row->y;
12862 }
12863
12864 /* Scroll the display. */
12865 run.current_y = first_reusable_row->y;
12866 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12867 run.height = it.last_visible_y - run.current_y;
12868 dy = run.current_y - run.desired_y;
12869
12870 if (run.height)
12871 {
12872 update_begin (f);
12873 rif->update_window_begin_hook (w);
12874 rif->clear_window_mouse_face (w);
12875 rif->scroll_run_hook (w, &run);
12876 rif->update_window_end_hook (w, 0, 0);
12877 update_end (f);
12878 }
12879
12880 /* Adjust Y positions of reused rows. */
12881 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12882 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12883 max_y = it.last_visible_y;
12884 for (row = first_reusable_row; row < first_row_to_display; ++row)
12885 {
12886 row->y -= dy;
12887 row->visible_height = row->height;
12888 if (row->y < min_y)
12889 row->visible_height -= min_y - row->y;
12890 if (row->y + row->height > max_y)
12891 row->visible_height -= row->y + row->height - max_y;
12892 row->redraw_fringe_bitmaps_p = 1;
12893 }
12894
12895 /* Scroll the current matrix. */
12896 xassert (nrows_scrolled > 0);
12897 rotate_matrix (w->current_matrix,
12898 start_vpos,
12899 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12900 -nrows_scrolled);
12901
12902 /* Disable rows not reused. */
12903 for (row -= nrows_scrolled; row < bottom_row; ++row)
12904 row->enabled_p = 0;
12905
12906 /* Point may have moved to a different line, so we cannot assume that
12907 the previous cursor position is valid; locate the correct row. */
12908 if (pt_row)
12909 {
12910 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12911 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12912 row++)
12913 {
12914 w->cursor.vpos++;
12915 w->cursor.y = row->y;
12916 }
12917 if (row < bottom_row)
12918 {
12919 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12920 while (glyph->charpos < PT)
12921 {
12922 w->cursor.hpos++;
12923 w->cursor.x += glyph->pixel_width;
12924 glyph++;
12925 }
12926 }
12927 }
12928
12929 /* Adjust window end. A null value of last_text_row means that
12930 the window end is in reused rows which in turn means that
12931 only its vpos can have changed. */
12932 if (last_text_row)
12933 {
12934 w->window_end_bytepos
12935 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12936 w->window_end_pos
12937 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12938 w->window_end_vpos
12939 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12940 }
12941 else
12942 {
12943 w->window_end_vpos
12944 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12945 }
12946
12947 w->window_end_valid = Qnil;
12948 w->desired_matrix->no_scrolling_p = 1;
12949
12950 #if GLYPH_DEBUG
12951 debug_method_add (w, "try_window_reusing_current_matrix 2");
12952 #endif
12953 return 1;
12954 }
12955
12956 return 0;
12957 }
12958
12959
12960 \f
12961 /************************************************************************
12962 Window redisplay reusing current matrix when buffer has changed
12963 ************************************************************************/
12964
12965 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12966 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12967 int *, int *));
12968 static struct glyph_row *
12969 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12970 struct glyph_row *));
12971
12972
12973 /* Return the last row in MATRIX displaying text. If row START is
12974 non-null, start searching with that row. IT gives the dimensions
12975 of the display. Value is null if matrix is empty; otherwise it is
12976 a pointer to the row found. */
12977
12978 static struct glyph_row *
12979 find_last_row_displaying_text (matrix, it, start)
12980 struct glyph_matrix *matrix;
12981 struct it *it;
12982 struct glyph_row *start;
12983 {
12984 struct glyph_row *row, *row_found;
12985
12986 /* Set row_found to the last row in IT->w's current matrix
12987 displaying text. The loop looks funny but think of partially
12988 visible lines. */
12989 row_found = NULL;
12990 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12991 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12992 {
12993 xassert (row->enabled_p);
12994 row_found = row;
12995 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12996 break;
12997 ++row;
12998 }
12999
13000 return row_found;
13001 }
13002
13003
13004 /* Return the last row in the current matrix of W that is not affected
13005 by changes at the start of current_buffer that occurred since W's
13006 current matrix was built. Value is null if no such row exists.
13007
13008 BEG_UNCHANGED us the number of characters unchanged at the start of
13009 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13010 first changed character in current_buffer. Characters at positions <
13011 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13012 when the current matrix was built. */
13013
13014 static struct glyph_row *
13015 find_last_unchanged_at_beg_row (w)
13016 struct window *w;
13017 {
13018 int first_changed_pos = BEG + BEG_UNCHANGED;
13019 struct glyph_row *row;
13020 struct glyph_row *row_found = NULL;
13021 int yb = window_text_bottom_y (w);
13022
13023 /* Find the last row displaying unchanged text. */
13024 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13025 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13026 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13027 {
13028 if (/* If row ends before first_changed_pos, it is unchanged,
13029 except in some case. */
13030 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13031 /* When row ends in ZV and we write at ZV it is not
13032 unchanged. */
13033 && !row->ends_at_zv_p
13034 /* When first_changed_pos is the end of a continued line,
13035 row is not unchanged because it may be no longer
13036 continued. */
13037 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13038 && (row->continued_p
13039 || row->exact_window_width_line_p)))
13040 row_found = row;
13041
13042 /* Stop if last visible row. */
13043 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13044 break;
13045
13046 ++row;
13047 }
13048
13049 return row_found;
13050 }
13051
13052
13053 /* Find the first glyph row in the current matrix of W that is not
13054 affected by changes at the end of current_buffer since the
13055 time W's current matrix was built.
13056
13057 Return in *DELTA the number of chars by which buffer positions in
13058 unchanged text at the end of current_buffer must be adjusted.
13059
13060 Return in *DELTA_BYTES the corresponding number of bytes.
13061
13062 Value is null if no such row exists, i.e. all rows are affected by
13063 changes. */
13064
13065 static struct glyph_row *
13066 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13067 struct window *w;
13068 int *delta, *delta_bytes;
13069 {
13070 struct glyph_row *row;
13071 struct glyph_row *row_found = NULL;
13072
13073 *delta = *delta_bytes = 0;
13074
13075 /* Display must not have been paused, otherwise the current matrix
13076 is not up to date. */
13077 if (NILP (w->window_end_valid))
13078 abort ();
13079
13080 /* A value of window_end_pos >= END_UNCHANGED means that the window
13081 end is in the range of changed text. If so, there is no
13082 unchanged row at the end of W's current matrix. */
13083 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13084 return NULL;
13085
13086 /* Set row to the last row in W's current matrix displaying text. */
13087 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13088
13089 /* If matrix is entirely empty, no unchanged row exists. */
13090 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13091 {
13092 /* The value of row is the last glyph row in the matrix having a
13093 meaningful buffer position in it. The end position of row
13094 corresponds to window_end_pos. This allows us to translate
13095 buffer positions in the current matrix to current buffer
13096 positions for characters not in changed text. */
13097 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13098 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13099 int last_unchanged_pos, last_unchanged_pos_old;
13100 struct glyph_row *first_text_row
13101 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13102
13103 *delta = Z - Z_old;
13104 *delta_bytes = Z_BYTE - Z_BYTE_old;
13105
13106 /* Set last_unchanged_pos to the buffer position of the last
13107 character in the buffer that has not been changed. Z is the
13108 index + 1 of the last character in current_buffer, i.e. by
13109 subtracting END_UNCHANGED we get the index of the last
13110 unchanged character, and we have to add BEG to get its buffer
13111 position. */
13112 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13113 last_unchanged_pos_old = last_unchanged_pos - *delta;
13114
13115 /* Search backward from ROW for a row displaying a line that
13116 starts at a minimum position >= last_unchanged_pos_old. */
13117 for (; row > first_text_row; --row)
13118 {
13119 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13120 abort ();
13121
13122 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13123 row_found = row;
13124 }
13125 }
13126
13127 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13128 abort ();
13129
13130 return row_found;
13131 }
13132
13133
13134 /* Make sure that glyph rows in the current matrix of window W
13135 reference the same glyph memory as corresponding rows in the
13136 frame's frame matrix. This function is called after scrolling W's
13137 current matrix on a terminal frame in try_window_id and
13138 try_window_reusing_current_matrix. */
13139
13140 static void
13141 sync_frame_with_window_matrix_rows (w)
13142 struct window *w;
13143 {
13144 struct frame *f = XFRAME (w->frame);
13145 struct glyph_row *window_row, *window_row_end, *frame_row;
13146
13147 /* Preconditions: W must be a leaf window and full-width. Its frame
13148 must have a frame matrix. */
13149 xassert (NILP (w->hchild) && NILP (w->vchild));
13150 xassert (WINDOW_FULL_WIDTH_P (w));
13151 xassert (!FRAME_WINDOW_P (f));
13152
13153 /* If W is a full-width window, glyph pointers in W's current matrix
13154 have, by definition, to be the same as glyph pointers in the
13155 corresponding frame matrix. Note that frame matrices have no
13156 marginal areas (see build_frame_matrix). */
13157 window_row = w->current_matrix->rows;
13158 window_row_end = window_row + w->current_matrix->nrows;
13159 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13160 while (window_row < window_row_end)
13161 {
13162 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13163 struct glyph *end = window_row->glyphs[LAST_AREA];
13164
13165 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13166 frame_row->glyphs[TEXT_AREA] = start;
13167 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13168 frame_row->glyphs[LAST_AREA] = end;
13169
13170 /* Disable frame rows whose corresponding window rows have
13171 been disabled in try_window_id. */
13172 if (!window_row->enabled_p)
13173 frame_row->enabled_p = 0;
13174
13175 ++window_row, ++frame_row;
13176 }
13177 }
13178
13179
13180 /* Find the glyph row in window W containing CHARPOS. Consider all
13181 rows between START and END (not inclusive). END null means search
13182 all rows to the end of the display area of W. Value is the row
13183 containing CHARPOS or null. */
13184
13185 struct glyph_row *
13186 row_containing_pos (w, charpos, start, end, dy)
13187 struct window *w;
13188 int charpos;
13189 struct glyph_row *start, *end;
13190 int dy;
13191 {
13192 struct glyph_row *row = start;
13193 int last_y;
13194
13195 /* If we happen to start on a header-line, skip that. */
13196 if (row->mode_line_p)
13197 ++row;
13198
13199 if ((end && row >= end) || !row->enabled_p)
13200 return NULL;
13201
13202 last_y = window_text_bottom_y (w) - dy;
13203
13204 while (1)
13205 {
13206 /* Give up if we have gone too far. */
13207 if (end && row >= end)
13208 return NULL;
13209 /* This formerly returned if they were equal.
13210 I think that both quantities are of a "last plus one" type;
13211 if so, when they are equal, the row is within the screen. -- rms. */
13212 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13213 return NULL;
13214
13215 /* If it is in this row, return this row. */
13216 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13217 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13218 /* The end position of a row equals the start
13219 position of the next row. If CHARPOS is there, we
13220 would rather display it in the next line, except
13221 when this line ends in ZV. */
13222 && !row->ends_at_zv_p
13223 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13224 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13225 return row;
13226 ++row;
13227 }
13228 }
13229
13230
13231 /* Try to redisplay window W by reusing its existing display. W's
13232 current matrix must be up to date when this function is called,
13233 i.e. window_end_valid must not be nil.
13234
13235 Value is
13236
13237 1 if display has been updated
13238 0 if otherwise unsuccessful
13239 -1 if redisplay with same window start is known not to succeed
13240
13241 The following steps are performed:
13242
13243 1. Find the last row in the current matrix of W that is not
13244 affected by changes at the start of current_buffer. If no such row
13245 is found, give up.
13246
13247 2. Find the first row in W's current matrix that is not affected by
13248 changes at the end of current_buffer. Maybe there is no such row.
13249
13250 3. Display lines beginning with the row + 1 found in step 1 to the
13251 row found in step 2 or, if step 2 didn't find a row, to the end of
13252 the window.
13253
13254 4. If cursor is not known to appear on the window, give up.
13255
13256 5. If display stopped at the row found in step 2, scroll the
13257 display and current matrix as needed.
13258
13259 6. Maybe display some lines at the end of W, if we must. This can
13260 happen under various circumstances, like a partially visible line
13261 becoming fully visible, or because newly displayed lines are displayed
13262 in smaller font sizes.
13263
13264 7. Update W's window end information. */
13265
13266 static int
13267 try_window_id (w)
13268 struct window *w;
13269 {
13270 struct frame *f = XFRAME (w->frame);
13271 struct glyph_matrix *current_matrix = w->current_matrix;
13272 struct glyph_matrix *desired_matrix = w->desired_matrix;
13273 struct glyph_row *last_unchanged_at_beg_row;
13274 struct glyph_row *first_unchanged_at_end_row;
13275 struct glyph_row *row;
13276 struct glyph_row *bottom_row;
13277 int bottom_vpos;
13278 struct it it;
13279 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13280 struct text_pos start_pos;
13281 struct run run;
13282 int first_unchanged_at_end_vpos = 0;
13283 struct glyph_row *last_text_row, *last_text_row_at_end;
13284 struct text_pos start;
13285 int first_changed_charpos, last_changed_charpos;
13286
13287 #if GLYPH_DEBUG
13288 if (inhibit_try_window_id)
13289 return 0;
13290 #endif
13291
13292 /* This is handy for debugging. */
13293 #if 0
13294 #define GIVE_UP(X) \
13295 do { \
13296 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13297 return 0; \
13298 } while (0)
13299 #else
13300 #define GIVE_UP(X) return 0
13301 #endif
13302
13303 SET_TEXT_POS_FROM_MARKER (start, w->start);
13304
13305 /* Don't use this for mini-windows because these can show
13306 messages and mini-buffers, and we don't handle that here. */
13307 if (MINI_WINDOW_P (w))
13308 GIVE_UP (1);
13309
13310 /* This flag is used to prevent redisplay optimizations. */
13311 if (windows_or_buffers_changed || cursor_type_changed)
13312 GIVE_UP (2);
13313
13314 /* Verify that narrowing has not changed.
13315 Also verify that we were not told to prevent redisplay optimizations.
13316 It would be nice to further
13317 reduce the number of cases where this prevents try_window_id. */
13318 if (current_buffer->clip_changed
13319 || current_buffer->prevent_redisplay_optimizations_p)
13320 GIVE_UP (3);
13321
13322 /* Window must either use window-based redisplay or be full width. */
13323 if (!FRAME_WINDOW_P (f)
13324 && (!line_ins_del_ok
13325 || !WINDOW_FULL_WIDTH_P (w)))
13326 GIVE_UP (4);
13327
13328 /* Give up if point is not known NOT to appear in W. */
13329 if (PT < CHARPOS (start))
13330 GIVE_UP (5);
13331
13332 /* Another way to prevent redisplay optimizations. */
13333 if (XFASTINT (w->last_modified) == 0)
13334 GIVE_UP (6);
13335
13336 /* Verify that window is not hscrolled. */
13337 if (XFASTINT (w->hscroll) != 0)
13338 GIVE_UP (7);
13339
13340 /* Verify that display wasn't paused. */
13341 if (NILP (w->window_end_valid))
13342 GIVE_UP (8);
13343
13344 /* Can't use this if highlighting a region because a cursor movement
13345 will do more than just set the cursor. */
13346 if (!NILP (Vtransient_mark_mode)
13347 && !NILP (current_buffer->mark_active))
13348 GIVE_UP (9);
13349
13350 /* Likewise if highlighting trailing whitespace. */
13351 if (!NILP (Vshow_trailing_whitespace))
13352 GIVE_UP (11);
13353
13354 /* Likewise if showing a region. */
13355 if (!NILP (w->region_showing))
13356 GIVE_UP (10);
13357
13358 /* Can use this if overlay arrow position and or string have changed. */
13359 if (overlay_arrows_changed_p ())
13360 GIVE_UP (12);
13361
13362
13363 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13364 only if buffer has really changed. The reason is that the gap is
13365 initially at Z for freshly visited files. The code below would
13366 set end_unchanged to 0 in that case. */
13367 if (MODIFF > SAVE_MODIFF
13368 /* This seems to happen sometimes after saving a buffer. */
13369 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13370 {
13371 if (GPT - BEG < BEG_UNCHANGED)
13372 BEG_UNCHANGED = GPT - BEG;
13373 if (Z - GPT < END_UNCHANGED)
13374 END_UNCHANGED = Z - GPT;
13375 }
13376
13377 /* The position of the first and last character that has been changed. */
13378 first_changed_charpos = BEG + BEG_UNCHANGED;
13379 last_changed_charpos = Z - END_UNCHANGED;
13380
13381 /* If window starts after a line end, and the last change is in
13382 front of that newline, then changes don't affect the display.
13383 This case happens with stealth-fontification. Note that although
13384 the display is unchanged, glyph positions in the matrix have to
13385 be adjusted, of course. */
13386 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13387 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13388 && ((last_changed_charpos < CHARPOS (start)
13389 && CHARPOS (start) == BEGV)
13390 || (last_changed_charpos < CHARPOS (start) - 1
13391 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13392 {
13393 int Z_old, delta, Z_BYTE_old, delta_bytes;
13394 struct glyph_row *r0;
13395
13396 /* Compute how many chars/bytes have been added to or removed
13397 from the buffer. */
13398 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13399 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13400 delta = Z - Z_old;
13401 delta_bytes = Z_BYTE - Z_BYTE_old;
13402
13403 /* Give up if PT is not in the window. Note that it already has
13404 been checked at the start of try_window_id that PT is not in
13405 front of the window start. */
13406 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13407 GIVE_UP (13);
13408
13409 /* If window start is unchanged, we can reuse the whole matrix
13410 as is, after adjusting glyph positions. No need to compute
13411 the window end again, since its offset from Z hasn't changed. */
13412 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13413 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13414 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13415 /* PT must not be in a partially visible line. */
13416 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13417 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13418 {
13419 /* Adjust positions in the glyph matrix. */
13420 if (delta || delta_bytes)
13421 {
13422 struct glyph_row *r1
13423 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13424 increment_matrix_positions (w->current_matrix,
13425 MATRIX_ROW_VPOS (r0, current_matrix),
13426 MATRIX_ROW_VPOS (r1, current_matrix),
13427 delta, delta_bytes);
13428 }
13429
13430 /* Set the cursor. */
13431 row = row_containing_pos (w, PT, r0, NULL, 0);
13432 if (row)
13433 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13434 else
13435 abort ();
13436 return 1;
13437 }
13438 }
13439
13440 /* Handle the case that changes are all below what is displayed in
13441 the window, and that PT is in the window. This shortcut cannot
13442 be taken if ZV is visible in the window, and text has been added
13443 there that is visible in the window. */
13444 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13445 /* ZV is not visible in the window, or there are no
13446 changes at ZV, actually. */
13447 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13448 || first_changed_charpos == last_changed_charpos))
13449 {
13450 struct glyph_row *r0;
13451
13452 /* Give up if PT is not in the window. Note that it already has
13453 been checked at the start of try_window_id that PT is not in
13454 front of the window start. */
13455 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13456 GIVE_UP (14);
13457
13458 /* If window start is unchanged, we can reuse the whole matrix
13459 as is, without changing glyph positions since no text has
13460 been added/removed in front of the window end. */
13461 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13462 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13463 /* PT must not be in a partially visible line. */
13464 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13465 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13466 {
13467 /* We have to compute the window end anew since text
13468 can have been added/removed after it. */
13469 w->window_end_pos
13470 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13471 w->window_end_bytepos
13472 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13473
13474 /* Set the cursor. */
13475 row = row_containing_pos (w, PT, r0, NULL, 0);
13476 if (row)
13477 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13478 else
13479 abort ();
13480 return 2;
13481 }
13482 }
13483
13484 /* Give up if window start is in the changed area.
13485
13486 The condition used to read
13487
13488 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13489
13490 but why that was tested escapes me at the moment. */
13491 if (CHARPOS (start) >= first_changed_charpos
13492 && CHARPOS (start) <= last_changed_charpos)
13493 GIVE_UP (15);
13494
13495 /* Check that window start agrees with the start of the first glyph
13496 row in its current matrix. Check this after we know the window
13497 start is not in changed text, otherwise positions would not be
13498 comparable. */
13499 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13500 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13501 GIVE_UP (16);
13502
13503 /* Give up if the window ends in strings. Overlay strings
13504 at the end are difficult to handle, so don't try. */
13505 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13506 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13507 GIVE_UP (20);
13508
13509 /* Compute the position at which we have to start displaying new
13510 lines. Some of the lines at the top of the window might be
13511 reusable because they are not displaying changed text. Find the
13512 last row in W's current matrix not affected by changes at the
13513 start of current_buffer. Value is null if changes start in the
13514 first line of window. */
13515 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13516 if (last_unchanged_at_beg_row)
13517 {
13518 /* Avoid starting to display in the moddle of a character, a TAB
13519 for instance. This is easier than to set up the iterator
13520 exactly, and it's not a frequent case, so the additional
13521 effort wouldn't really pay off. */
13522 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13523 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13524 && last_unchanged_at_beg_row > w->current_matrix->rows)
13525 --last_unchanged_at_beg_row;
13526
13527 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13528 GIVE_UP (17);
13529
13530 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13531 GIVE_UP (18);
13532 start_pos = it.current.pos;
13533
13534 /* Start displaying new lines in the desired matrix at the same
13535 vpos we would use in the current matrix, i.e. below
13536 last_unchanged_at_beg_row. */
13537 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13538 current_matrix);
13539 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13540 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13541
13542 xassert (it.hpos == 0 && it.current_x == 0);
13543 }
13544 else
13545 {
13546 /* There are no reusable lines at the start of the window.
13547 Start displaying in the first text line. */
13548 start_display (&it, w, start);
13549 it.vpos = it.first_vpos;
13550 start_pos = it.current.pos;
13551 }
13552
13553 /* Find the first row that is not affected by changes at the end of
13554 the buffer. Value will be null if there is no unchanged row, in
13555 which case we must redisplay to the end of the window. delta
13556 will be set to the value by which buffer positions beginning with
13557 first_unchanged_at_end_row have to be adjusted due to text
13558 changes. */
13559 first_unchanged_at_end_row
13560 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13561 IF_DEBUG (debug_delta = delta);
13562 IF_DEBUG (debug_delta_bytes = delta_bytes);
13563
13564 /* Set stop_pos to the buffer position up to which we will have to
13565 display new lines. If first_unchanged_at_end_row != NULL, this
13566 is the buffer position of the start of the line displayed in that
13567 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13568 that we don't stop at a buffer position. */
13569 stop_pos = 0;
13570 if (first_unchanged_at_end_row)
13571 {
13572 xassert (last_unchanged_at_beg_row == NULL
13573 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13574
13575 /* If this is a continuation line, move forward to the next one
13576 that isn't. Changes in lines above affect this line.
13577 Caution: this may move first_unchanged_at_end_row to a row
13578 not displaying text. */
13579 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13580 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13581 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13582 < it.last_visible_y))
13583 ++first_unchanged_at_end_row;
13584
13585 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13586 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13587 >= it.last_visible_y))
13588 first_unchanged_at_end_row = NULL;
13589 else
13590 {
13591 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13592 + delta);
13593 first_unchanged_at_end_vpos
13594 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13595 xassert (stop_pos >= Z - END_UNCHANGED);
13596 }
13597 }
13598 else if (last_unchanged_at_beg_row == NULL)
13599 GIVE_UP (19);
13600
13601
13602 #if GLYPH_DEBUG
13603
13604 /* Either there is no unchanged row at the end, or the one we have
13605 now displays text. This is a necessary condition for the window
13606 end pos calculation at the end of this function. */
13607 xassert (first_unchanged_at_end_row == NULL
13608 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13609
13610 debug_last_unchanged_at_beg_vpos
13611 = (last_unchanged_at_beg_row
13612 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13613 : -1);
13614 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13615
13616 #endif /* GLYPH_DEBUG != 0 */
13617
13618
13619 /* Display new lines. Set last_text_row to the last new line
13620 displayed which has text on it, i.e. might end up as being the
13621 line where the window_end_vpos is. */
13622 w->cursor.vpos = -1;
13623 last_text_row = NULL;
13624 overlay_arrow_seen = 0;
13625 while (it.current_y < it.last_visible_y
13626 && !fonts_changed_p
13627 && (first_unchanged_at_end_row == NULL
13628 || IT_CHARPOS (it) < stop_pos))
13629 {
13630 if (display_line (&it))
13631 last_text_row = it.glyph_row - 1;
13632 }
13633
13634 if (fonts_changed_p)
13635 return -1;
13636
13637
13638 /* Compute differences in buffer positions, y-positions etc. for
13639 lines reused at the bottom of the window. Compute what we can
13640 scroll. */
13641 if (first_unchanged_at_end_row
13642 /* No lines reused because we displayed everything up to the
13643 bottom of the window. */
13644 && it.current_y < it.last_visible_y)
13645 {
13646 dvpos = (it.vpos
13647 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13648 current_matrix));
13649 dy = it.current_y - first_unchanged_at_end_row->y;
13650 run.current_y = first_unchanged_at_end_row->y;
13651 run.desired_y = run.current_y + dy;
13652 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13653 }
13654 else
13655 {
13656 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13657 first_unchanged_at_end_row = NULL;
13658 }
13659 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13660
13661
13662 /* Find the cursor if not already found. We have to decide whether
13663 PT will appear on this window (it sometimes doesn't, but this is
13664 not a very frequent case.) This decision has to be made before
13665 the current matrix is altered. A value of cursor.vpos < 0 means
13666 that PT is either in one of the lines beginning at
13667 first_unchanged_at_end_row or below the window. Don't care for
13668 lines that might be displayed later at the window end; as
13669 mentioned, this is not a frequent case. */
13670 if (w->cursor.vpos < 0)
13671 {
13672 /* Cursor in unchanged rows at the top? */
13673 if (PT < CHARPOS (start_pos)
13674 && last_unchanged_at_beg_row)
13675 {
13676 row = row_containing_pos (w, PT,
13677 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13678 last_unchanged_at_beg_row + 1, 0);
13679 if (row)
13680 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13681 }
13682
13683 /* Start from first_unchanged_at_end_row looking for PT. */
13684 else if (first_unchanged_at_end_row)
13685 {
13686 row = row_containing_pos (w, PT - delta,
13687 first_unchanged_at_end_row, NULL, 0);
13688 if (row)
13689 set_cursor_from_row (w, row, w->current_matrix, delta,
13690 delta_bytes, dy, dvpos);
13691 }
13692
13693 /* Give up if cursor was not found. */
13694 if (w->cursor.vpos < 0)
13695 {
13696 clear_glyph_matrix (w->desired_matrix);
13697 return -1;
13698 }
13699 }
13700
13701 /* Don't let the cursor end in the scroll margins. */
13702 {
13703 int this_scroll_margin, cursor_height;
13704
13705 this_scroll_margin = max (0, scroll_margin);
13706 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13707 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13708 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13709
13710 if ((w->cursor.y < this_scroll_margin
13711 && CHARPOS (start) > BEGV)
13712 /* Old redisplay didn't take scroll margin into account at the bottom,
13713 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13714 || (w->cursor.y + (make_cursor_line_fully_visible_p
13715 ? cursor_height + this_scroll_margin
13716 : 1)) > it.last_visible_y)
13717 {
13718 w->cursor.vpos = -1;
13719 clear_glyph_matrix (w->desired_matrix);
13720 return -1;
13721 }
13722 }
13723
13724 /* Scroll the display. Do it before changing the current matrix so
13725 that xterm.c doesn't get confused about where the cursor glyph is
13726 found. */
13727 if (dy && run.height)
13728 {
13729 update_begin (f);
13730
13731 if (FRAME_WINDOW_P (f))
13732 {
13733 rif->update_window_begin_hook (w);
13734 rif->clear_window_mouse_face (w);
13735 rif->scroll_run_hook (w, &run);
13736 rif->update_window_end_hook (w, 0, 0);
13737 }
13738 else
13739 {
13740 /* Terminal frame. In this case, dvpos gives the number of
13741 lines to scroll by; dvpos < 0 means scroll up. */
13742 int first_unchanged_at_end_vpos
13743 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13744 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13745 int end = (WINDOW_TOP_EDGE_LINE (w)
13746 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13747 + window_internal_height (w));
13748
13749 /* Perform the operation on the screen. */
13750 if (dvpos > 0)
13751 {
13752 /* Scroll last_unchanged_at_beg_row to the end of the
13753 window down dvpos lines. */
13754 set_terminal_window (end);
13755
13756 /* On dumb terminals delete dvpos lines at the end
13757 before inserting dvpos empty lines. */
13758 if (!scroll_region_ok)
13759 ins_del_lines (end - dvpos, -dvpos);
13760
13761 /* Insert dvpos empty lines in front of
13762 last_unchanged_at_beg_row. */
13763 ins_del_lines (from, dvpos);
13764 }
13765 else if (dvpos < 0)
13766 {
13767 /* Scroll up last_unchanged_at_beg_vpos to the end of
13768 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13769 set_terminal_window (end);
13770
13771 /* Delete dvpos lines in front of
13772 last_unchanged_at_beg_vpos. ins_del_lines will set
13773 the cursor to the given vpos and emit |dvpos| delete
13774 line sequences. */
13775 ins_del_lines (from + dvpos, dvpos);
13776
13777 /* On a dumb terminal insert dvpos empty lines at the
13778 end. */
13779 if (!scroll_region_ok)
13780 ins_del_lines (end + dvpos, -dvpos);
13781 }
13782
13783 set_terminal_window (0);
13784 }
13785
13786 update_end (f);
13787 }
13788
13789 /* Shift reused rows of the current matrix to the right position.
13790 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13791 text. */
13792 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13793 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13794 if (dvpos < 0)
13795 {
13796 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13797 bottom_vpos, dvpos);
13798 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13799 bottom_vpos, 0);
13800 }
13801 else if (dvpos > 0)
13802 {
13803 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13804 bottom_vpos, dvpos);
13805 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13806 first_unchanged_at_end_vpos + dvpos, 0);
13807 }
13808
13809 /* For frame-based redisplay, make sure that current frame and window
13810 matrix are in sync with respect to glyph memory. */
13811 if (!FRAME_WINDOW_P (f))
13812 sync_frame_with_window_matrix_rows (w);
13813
13814 /* Adjust buffer positions in reused rows. */
13815 if (delta)
13816 increment_matrix_positions (current_matrix,
13817 first_unchanged_at_end_vpos + dvpos,
13818 bottom_vpos, delta, delta_bytes);
13819
13820 /* Adjust Y positions. */
13821 if (dy)
13822 shift_glyph_matrix (w, current_matrix,
13823 first_unchanged_at_end_vpos + dvpos,
13824 bottom_vpos, dy);
13825
13826 if (first_unchanged_at_end_row)
13827 first_unchanged_at_end_row += dvpos;
13828
13829 /* If scrolling up, there may be some lines to display at the end of
13830 the window. */
13831 last_text_row_at_end = NULL;
13832 if (dy < 0)
13833 {
13834 /* Scrolling up can leave for example a partially visible line
13835 at the end of the window to be redisplayed. */
13836 /* Set last_row to the glyph row in the current matrix where the
13837 window end line is found. It has been moved up or down in
13838 the matrix by dvpos. */
13839 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13840 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13841
13842 /* If last_row is the window end line, it should display text. */
13843 xassert (last_row->displays_text_p);
13844
13845 /* If window end line was partially visible before, begin
13846 displaying at that line. Otherwise begin displaying with the
13847 line following it. */
13848 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13849 {
13850 init_to_row_start (&it, w, last_row);
13851 it.vpos = last_vpos;
13852 it.current_y = last_row->y;
13853 }
13854 else
13855 {
13856 init_to_row_end (&it, w, last_row);
13857 it.vpos = 1 + last_vpos;
13858 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13859 ++last_row;
13860 }
13861
13862 /* We may start in a continuation line. If so, we have to
13863 get the right continuation_lines_width and current_x. */
13864 it.continuation_lines_width = last_row->continuation_lines_width;
13865 it.hpos = it.current_x = 0;
13866
13867 /* Display the rest of the lines at the window end. */
13868 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13869 while (it.current_y < it.last_visible_y
13870 && !fonts_changed_p)
13871 {
13872 /* Is it always sure that the display agrees with lines in
13873 the current matrix? I don't think so, so we mark rows
13874 displayed invalid in the current matrix by setting their
13875 enabled_p flag to zero. */
13876 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13877 if (display_line (&it))
13878 last_text_row_at_end = it.glyph_row - 1;
13879 }
13880 }
13881
13882 /* Update window_end_pos and window_end_vpos. */
13883 if (first_unchanged_at_end_row
13884 && first_unchanged_at_end_row->y < it.last_visible_y
13885 && !last_text_row_at_end)
13886 {
13887 /* Window end line if one of the preserved rows from the current
13888 matrix. Set row to the last row displaying text in current
13889 matrix starting at first_unchanged_at_end_row, after
13890 scrolling. */
13891 xassert (first_unchanged_at_end_row->displays_text_p);
13892 row = find_last_row_displaying_text (w->current_matrix, &it,
13893 first_unchanged_at_end_row);
13894 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13895
13896 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13897 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13898 w->window_end_vpos
13899 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13900 xassert (w->window_end_bytepos >= 0);
13901 IF_DEBUG (debug_method_add (w, "A"));
13902 }
13903 else if (last_text_row_at_end)
13904 {
13905 w->window_end_pos
13906 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13907 w->window_end_bytepos
13908 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13909 w->window_end_vpos
13910 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13911 xassert (w->window_end_bytepos >= 0);
13912 IF_DEBUG (debug_method_add (w, "B"));
13913 }
13914 else if (last_text_row)
13915 {
13916 /* We have displayed either to the end of the window or at the
13917 end of the window, i.e. the last row with text is to be found
13918 in the desired matrix. */
13919 w->window_end_pos
13920 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13921 w->window_end_bytepos
13922 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13923 w->window_end_vpos
13924 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13925 xassert (w->window_end_bytepos >= 0);
13926 }
13927 else if (first_unchanged_at_end_row == NULL
13928 && last_text_row == NULL
13929 && last_text_row_at_end == NULL)
13930 {
13931 /* Displayed to end of window, but no line containing text was
13932 displayed. Lines were deleted at the end of the window. */
13933 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13934 int vpos = XFASTINT (w->window_end_vpos);
13935 struct glyph_row *current_row = current_matrix->rows + vpos;
13936 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13937
13938 for (row = NULL;
13939 row == NULL && vpos >= first_vpos;
13940 --vpos, --current_row, --desired_row)
13941 {
13942 if (desired_row->enabled_p)
13943 {
13944 if (desired_row->displays_text_p)
13945 row = desired_row;
13946 }
13947 else if (current_row->displays_text_p)
13948 row = current_row;
13949 }
13950
13951 xassert (row != NULL);
13952 w->window_end_vpos = make_number (vpos + 1);
13953 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13954 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13955 xassert (w->window_end_bytepos >= 0);
13956 IF_DEBUG (debug_method_add (w, "C"));
13957 }
13958 else
13959 abort ();
13960
13961 #if 0 /* This leads to problems, for instance when the cursor is
13962 at ZV, and the cursor line displays no text. */
13963 /* Disable rows below what's displayed in the window. This makes
13964 debugging easier. */
13965 enable_glyph_matrix_rows (current_matrix,
13966 XFASTINT (w->window_end_vpos) + 1,
13967 bottom_vpos, 0);
13968 #endif
13969
13970 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13971 debug_end_vpos = XFASTINT (w->window_end_vpos));
13972
13973 /* Record that display has not been completed. */
13974 w->window_end_valid = Qnil;
13975 w->desired_matrix->no_scrolling_p = 1;
13976 return 3;
13977
13978 #undef GIVE_UP
13979 }
13980
13981
13982 \f
13983 /***********************************************************************
13984 More debugging support
13985 ***********************************************************************/
13986
13987 #if GLYPH_DEBUG
13988
13989 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13990 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13991 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13992
13993
13994 /* Dump the contents of glyph matrix MATRIX on stderr.
13995
13996 GLYPHS 0 means don't show glyph contents.
13997 GLYPHS 1 means show glyphs in short form
13998 GLYPHS > 1 means show glyphs in long form. */
13999
14000 void
14001 dump_glyph_matrix (matrix, glyphs)
14002 struct glyph_matrix *matrix;
14003 int glyphs;
14004 {
14005 int i;
14006 for (i = 0; i < matrix->nrows; ++i)
14007 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14008 }
14009
14010
14011 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14012 the glyph row and area where the glyph comes from. */
14013
14014 void
14015 dump_glyph (row, glyph, area)
14016 struct glyph_row *row;
14017 struct glyph *glyph;
14018 int area;
14019 {
14020 if (glyph->type == CHAR_GLYPH)
14021 {
14022 fprintf (stderr,
14023 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14024 glyph - row->glyphs[TEXT_AREA],
14025 'C',
14026 glyph->charpos,
14027 (BUFFERP (glyph->object)
14028 ? 'B'
14029 : (STRINGP (glyph->object)
14030 ? 'S'
14031 : '-')),
14032 glyph->pixel_width,
14033 glyph->u.ch,
14034 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14035 ? glyph->u.ch
14036 : '.'),
14037 glyph->face_id,
14038 glyph->left_box_line_p,
14039 glyph->right_box_line_p);
14040 }
14041 else if (glyph->type == STRETCH_GLYPH)
14042 {
14043 fprintf (stderr,
14044 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14045 glyph - row->glyphs[TEXT_AREA],
14046 'S',
14047 glyph->charpos,
14048 (BUFFERP (glyph->object)
14049 ? 'B'
14050 : (STRINGP (glyph->object)
14051 ? 'S'
14052 : '-')),
14053 glyph->pixel_width,
14054 0,
14055 '.',
14056 glyph->face_id,
14057 glyph->left_box_line_p,
14058 glyph->right_box_line_p);
14059 }
14060 else if (glyph->type == IMAGE_GLYPH)
14061 {
14062 fprintf (stderr,
14063 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14064 glyph - row->glyphs[TEXT_AREA],
14065 'I',
14066 glyph->charpos,
14067 (BUFFERP (glyph->object)
14068 ? 'B'
14069 : (STRINGP (glyph->object)
14070 ? 'S'
14071 : '-')),
14072 glyph->pixel_width,
14073 glyph->u.img_id,
14074 '.',
14075 glyph->face_id,
14076 glyph->left_box_line_p,
14077 glyph->right_box_line_p);
14078 }
14079 }
14080
14081
14082 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14083 GLYPHS 0 means don't show glyph contents.
14084 GLYPHS 1 means show glyphs in short form
14085 GLYPHS > 1 means show glyphs in long form. */
14086
14087 void
14088 dump_glyph_row (row, vpos, glyphs)
14089 struct glyph_row *row;
14090 int vpos, glyphs;
14091 {
14092 if (glyphs != 1)
14093 {
14094 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
14095 fprintf (stderr, "=======================================================================\n");
14096
14097 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
14098 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14099 vpos,
14100 MATRIX_ROW_START_CHARPOS (row),
14101 MATRIX_ROW_END_CHARPOS (row),
14102 row->used[TEXT_AREA],
14103 row->contains_overlapping_glyphs_p,
14104 row->enabled_p,
14105 row->truncated_on_left_p,
14106 row->truncated_on_right_p,
14107 row->overlay_arrow_p,
14108 row->continued_p,
14109 MATRIX_ROW_CONTINUATION_LINE_P (row),
14110 row->displays_text_p,
14111 row->ends_at_zv_p,
14112 row->fill_line_p,
14113 row->ends_in_middle_of_char_p,
14114 row->starts_in_middle_of_char_p,
14115 row->mouse_face_p,
14116 row->x,
14117 row->y,
14118 row->pixel_width,
14119 row->height,
14120 row->visible_height,
14121 row->ascent,
14122 row->phys_ascent);
14123 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14124 row->end.overlay_string_index,
14125 row->continuation_lines_width);
14126 fprintf (stderr, "%9d %5d\n",
14127 CHARPOS (row->start.string_pos),
14128 CHARPOS (row->end.string_pos));
14129 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14130 row->end.dpvec_index);
14131 }
14132
14133 if (glyphs > 1)
14134 {
14135 int area;
14136
14137 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14138 {
14139 struct glyph *glyph = row->glyphs[area];
14140 struct glyph *glyph_end = glyph + row->used[area];
14141
14142 /* Glyph for a line end in text. */
14143 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14144 ++glyph_end;
14145
14146 if (glyph < glyph_end)
14147 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14148
14149 for (; glyph < glyph_end; ++glyph)
14150 dump_glyph (row, glyph, area);
14151 }
14152 }
14153 else if (glyphs == 1)
14154 {
14155 int area;
14156
14157 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14158 {
14159 char *s = (char *) alloca (row->used[area] + 1);
14160 int i;
14161
14162 for (i = 0; i < row->used[area]; ++i)
14163 {
14164 struct glyph *glyph = row->glyphs[area] + i;
14165 if (glyph->type == CHAR_GLYPH
14166 && glyph->u.ch < 0x80
14167 && glyph->u.ch >= ' ')
14168 s[i] = glyph->u.ch;
14169 else
14170 s[i] = '.';
14171 }
14172
14173 s[i] = '\0';
14174 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14175 }
14176 }
14177 }
14178
14179
14180 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14181 Sdump_glyph_matrix, 0, 1, "p",
14182 doc: /* Dump the current matrix of the selected window to stderr.
14183 Shows contents of glyph row structures. With non-nil
14184 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14185 glyphs in short form, otherwise show glyphs in long form. */)
14186 (glyphs)
14187 Lisp_Object glyphs;
14188 {
14189 struct window *w = XWINDOW (selected_window);
14190 struct buffer *buffer = XBUFFER (w->buffer);
14191
14192 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14193 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14194 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14195 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14196 fprintf (stderr, "=============================================\n");
14197 dump_glyph_matrix (w->current_matrix,
14198 NILP (glyphs) ? 0 : XINT (glyphs));
14199 return Qnil;
14200 }
14201
14202
14203 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14204 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14205 ()
14206 {
14207 struct frame *f = XFRAME (selected_frame);
14208 dump_glyph_matrix (f->current_matrix, 1);
14209 return Qnil;
14210 }
14211
14212
14213 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14214 doc: /* Dump glyph row ROW to stderr.
14215 GLYPH 0 means don't dump glyphs.
14216 GLYPH 1 means dump glyphs in short form.
14217 GLYPH > 1 or omitted means dump glyphs in long form. */)
14218 (row, glyphs)
14219 Lisp_Object row, glyphs;
14220 {
14221 struct glyph_matrix *matrix;
14222 int vpos;
14223
14224 CHECK_NUMBER (row);
14225 matrix = XWINDOW (selected_window)->current_matrix;
14226 vpos = XINT (row);
14227 if (vpos >= 0 && vpos < matrix->nrows)
14228 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14229 vpos,
14230 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14231 return Qnil;
14232 }
14233
14234
14235 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14236 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14237 GLYPH 0 means don't dump glyphs.
14238 GLYPH 1 means dump glyphs in short form.
14239 GLYPH > 1 or omitted means dump glyphs in long form. */)
14240 (row, glyphs)
14241 Lisp_Object row, glyphs;
14242 {
14243 struct frame *sf = SELECTED_FRAME ();
14244 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14245 int vpos;
14246
14247 CHECK_NUMBER (row);
14248 vpos = XINT (row);
14249 if (vpos >= 0 && vpos < m->nrows)
14250 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14251 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14252 return Qnil;
14253 }
14254
14255
14256 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14257 doc: /* Toggle tracing of redisplay.
14258 With ARG, turn tracing on if and only if ARG is positive. */)
14259 (arg)
14260 Lisp_Object arg;
14261 {
14262 if (NILP (arg))
14263 trace_redisplay_p = !trace_redisplay_p;
14264 else
14265 {
14266 arg = Fprefix_numeric_value (arg);
14267 trace_redisplay_p = XINT (arg) > 0;
14268 }
14269
14270 return Qnil;
14271 }
14272
14273
14274 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14275 doc: /* Like `format', but print result to stderr.
14276 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14277 (nargs, args)
14278 int nargs;
14279 Lisp_Object *args;
14280 {
14281 Lisp_Object s = Fformat (nargs, args);
14282 fprintf (stderr, "%s", SDATA (s));
14283 return Qnil;
14284 }
14285
14286 #endif /* GLYPH_DEBUG */
14287
14288
14289 \f
14290 /***********************************************************************
14291 Building Desired Matrix Rows
14292 ***********************************************************************/
14293
14294 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14295 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14296
14297 static struct glyph_row *
14298 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14299 struct window *w;
14300 Lisp_Object overlay_arrow_string;
14301 {
14302 struct frame *f = XFRAME (WINDOW_FRAME (w));
14303 struct buffer *buffer = XBUFFER (w->buffer);
14304 struct buffer *old = current_buffer;
14305 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14306 int arrow_len = SCHARS (overlay_arrow_string);
14307 const unsigned char *arrow_end = arrow_string + arrow_len;
14308 const unsigned char *p;
14309 struct it it;
14310 int multibyte_p;
14311 int n_glyphs_before;
14312
14313 set_buffer_temp (buffer);
14314 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14315 it.glyph_row->used[TEXT_AREA] = 0;
14316 SET_TEXT_POS (it.position, 0, 0);
14317
14318 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14319 p = arrow_string;
14320 while (p < arrow_end)
14321 {
14322 Lisp_Object face, ilisp;
14323
14324 /* Get the next character. */
14325 if (multibyte_p)
14326 it.c = string_char_and_length (p, arrow_len, &it.len);
14327 else
14328 it.c = *p, it.len = 1;
14329 p += it.len;
14330
14331 /* Get its face. */
14332 ilisp = make_number (p - arrow_string);
14333 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14334 it.face_id = compute_char_face (f, it.c, face);
14335
14336 /* Compute its width, get its glyphs. */
14337 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14338 SET_TEXT_POS (it.position, -1, -1);
14339 PRODUCE_GLYPHS (&it);
14340
14341 /* If this character doesn't fit any more in the line, we have
14342 to remove some glyphs. */
14343 if (it.current_x > it.last_visible_x)
14344 {
14345 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14346 break;
14347 }
14348 }
14349
14350 set_buffer_temp (old);
14351 return it.glyph_row;
14352 }
14353
14354
14355 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14356 glyphs are only inserted for terminal frames since we can't really
14357 win with truncation glyphs when partially visible glyphs are
14358 involved. Which glyphs to insert is determined by
14359 produce_special_glyphs. */
14360
14361 static void
14362 insert_left_trunc_glyphs (it)
14363 struct it *it;
14364 {
14365 struct it truncate_it;
14366 struct glyph *from, *end, *to, *toend;
14367
14368 xassert (!FRAME_WINDOW_P (it->f));
14369
14370 /* Get the truncation glyphs. */
14371 truncate_it = *it;
14372 truncate_it.current_x = 0;
14373 truncate_it.face_id = DEFAULT_FACE_ID;
14374 truncate_it.glyph_row = &scratch_glyph_row;
14375 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14376 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14377 truncate_it.object = make_number (0);
14378 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14379
14380 /* Overwrite glyphs from IT with truncation glyphs. */
14381 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14382 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14383 to = it->glyph_row->glyphs[TEXT_AREA];
14384 toend = to + it->glyph_row->used[TEXT_AREA];
14385
14386 while (from < end)
14387 *to++ = *from++;
14388
14389 /* There may be padding glyphs left over. Overwrite them too. */
14390 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14391 {
14392 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14393 while (from < end)
14394 *to++ = *from++;
14395 }
14396
14397 if (to > toend)
14398 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14399 }
14400
14401
14402 /* Compute the pixel height and width of IT->glyph_row.
14403
14404 Most of the time, ascent and height of a display line will be equal
14405 to the max_ascent and max_height values of the display iterator
14406 structure. This is not the case if
14407
14408 1. We hit ZV without displaying anything. In this case, max_ascent
14409 and max_height will be zero.
14410
14411 2. We have some glyphs that don't contribute to the line height.
14412 (The glyph row flag contributes_to_line_height_p is for future
14413 pixmap extensions).
14414
14415 The first case is easily covered by using default values because in
14416 these cases, the line height does not really matter, except that it
14417 must not be zero. */
14418
14419 static void
14420 compute_line_metrics (it)
14421 struct it *it;
14422 {
14423 struct glyph_row *row = it->glyph_row;
14424 int area, i;
14425
14426 if (FRAME_WINDOW_P (it->f))
14427 {
14428 int i, min_y, max_y;
14429
14430 /* The line may consist of one space only, that was added to
14431 place the cursor on it. If so, the row's height hasn't been
14432 computed yet. */
14433 if (row->height == 0)
14434 {
14435 if (it->max_ascent + it->max_descent == 0)
14436 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14437 row->ascent = it->max_ascent;
14438 row->height = it->max_ascent + it->max_descent;
14439 row->phys_ascent = it->max_phys_ascent;
14440 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14441 row->extra_line_spacing = it->max_extra_line_spacing;
14442 }
14443
14444 /* Compute the width of this line. */
14445 row->pixel_width = row->x;
14446 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14447 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14448
14449 xassert (row->pixel_width >= 0);
14450 xassert (row->ascent >= 0 && row->height > 0);
14451
14452 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14453 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14454
14455 /* If first line's physical ascent is larger than its logical
14456 ascent, use the physical ascent, and make the row taller.
14457 This makes accented characters fully visible. */
14458 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14459 && row->phys_ascent > row->ascent)
14460 {
14461 row->height += row->phys_ascent - row->ascent;
14462 row->ascent = row->phys_ascent;
14463 }
14464
14465 /* Compute how much of the line is visible. */
14466 row->visible_height = row->height;
14467
14468 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14469 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14470
14471 if (row->y < min_y)
14472 row->visible_height -= min_y - row->y;
14473 if (row->y + row->height > max_y)
14474 row->visible_height -= row->y + row->height - max_y;
14475 }
14476 else
14477 {
14478 row->pixel_width = row->used[TEXT_AREA];
14479 if (row->continued_p)
14480 row->pixel_width -= it->continuation_pixel_width;
14481 else if (row->truncated_on_right_p)
14482 row->pixel_width -= it->truncation_pixel_width;
14483 row->ascent = row->phys_ascent = 0;
14484 row->height = row->phys_height = row->visible_height = 1;
14485 row->extra_line_spacing = 0;
14486 }
14487
14488 /* Compute a hash code for this row. */
14489 row->hash = 0;
14490 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14491 for (i = 0; i < row->used[area]; ++i)
14492 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14493 + row->glyphs[area][i].u.val
14494 + row->glyphs[area][i].face_id
14495 + row->glyphs[area][i].padding_p
14496 + (row->glyphs[area][i].type << 2));
14497
14498 it->max_ascent = it->max_descent = 0;
14499 it->max_phys_ascent = it->max_phys_descent = 0;
14500 }
14501
14502
14503 /* Append one space to the glyph row of iterator IT if doing a
14504 window-based redisplay. The space has the same face as
14505 IT->face_id. Value is non-zero if a space was added.
14506
14507 This function is called to make sure that there is always one glyph
14508 at the end of a glyph row that the cursor can be set on under
14509 window-systems. (If there weren't such a glyph we would not know
14510 how wide and tall a box cursor should be displayed).
14511
14512 At the same time this space let's a nicely handle clearing to the
14513 end of the line if the row ends in italic text. */
14514
14515 static int
14516 append_space_for_newline (it, default_face_p)
14517 struct it *it;
14518 int default_face_p;
14519 {
14520 if (FRAME_WINDOW_P (it->f))
14521 {
14522 int n = it->glyph_row->used[TEXT_AREA];
14523
14524 if (it->glyph_row->glyphs[TEXT_AREA] + n
14525 < it->glyph_row->glyphs[1 + TEXT_AREA])
14526 {
14527 /* Save some values that must not be changed.
14528 Must save IT->c and IT->len because otherwise
14529 ITERATOR_AT_END_P wouldn't work anymore after
14530 append_space_for_newline has been called. */
14531 enum display_element_type saved_what = it->what;
14532 int saved_c = it->c, saved_len = it->len;
14533 int saved_x = it->current_x;
14534 int saved_face_id = it->face_id;
14535 struct text_pos saved_pos;
14536 Lisp_Object saved_object;
14537 struct face *face;
14538
14539 saved_object = it->object;
14540 saved_pos = it->position;
14541
14542 it->what = IT_CHARACTER;
14543 bzero (&it->position, sizeof it->position);
14544 it->object = make_number (0);
14545 it->c = ' ';
14546 it->len = 1;
14547
14548 if (default_face_p)
14549 it->face_id = DEFAULT_FACE_ID;
14550 else if (it->face_before_selective_p)
14551 it->face_id = it->saved_face_id;
14552 face = FACE_FROM_ID (it->f, it->face_id);
14553 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14554
14555 PRODUCE_GLYPHS (it);
14556
14557 it->override_ascent = -1;
14558 it->constrain_row_ascent_descent_p = 0;
14559 it->current_x = saved_x;
14560 it->object = saved_object;
14561 it->position = saved_pos;
14562 it->what = saved_what;
14563 it->face_id = saved_face_id;
14564 it->len = saved_len;
14565 it->c = saved_c;
14566 return 1;
14567 }
14568 }
14569
14570 return 0;
14571 }
14572
14573
14574 /* Extend the face of the last glyph in the text area of IT->glyph_row
14575 to the end of the display line. Called from display_line.
14576 If the glyph row is empty, add a space glyph to it so that we
14577 know the face to draw. Set the glyph row flag fill_line_p. */
14578
14579 static void
14580 extend_face_to_end_of_line (it)
14581 struct it *it;
14582 {
14583 struct face *face;
14584 struct frame *f = it->f;
14585
14586 /* If line is already filled, do nothing. */
14587 if (it->current_x >= it->last_visible_x)
14588 return;
14589
14590 /* Face extension extends the background and box of IT->face_id
14591 to the end of the line. If the background equals the background
14592 of the frame, we don't have to do anything. */
14593 if (it->face_before_selective_p)
14594 face = FACE_FROM_ID (it->f, it->saved_face_id);
14595 else
14596 face = FACE_FROM_ID (f, it->face_id);
14597
14598 if (FRAME_WINDOW_P (f)
14599 && face->box == FACE_NO_BOX
14600 && face->background == FRAME_BACKGROUND_PIXEL (f)
14601 && !face->stipple)
14602 return;
14603
14604 /* Set the glyph row flag indicating that the face of the last glyph
14605 in the text area has to be drawn to the end of the text area. */
14606 it->glyph_row->fill_line_p = 1;
14607
14608 /* If current character of IT is not ASCII, make sure we have the
14609 ASCII face. This will be automatically undone the next time
14610 get_next_display_element returns a multibyte character. Note
14611 that the character will always be single byte in unibyte text. */
14612 if (!SINGLE_BYTE_CHAR_P (it->c))
14613 {
14614 it->face_id = FACE_FOR_CHAR (f, face, 0);
14615 }
14616
14617 if (FRAME_WINDOW_P (f))
14618 {
14619 /* If the row is empty, add a space with the current face of IT,
14620 so that we know which face to draw. */
14621 if (it->glyph_row->used[TEXT_AREA] == 0)
14622 {
14623 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14624 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14625 it->glyph_row->used[TEXT_AREA] = 1;
14626 }
14627 }
14628 else
14629 {
14630 /* Save some values that must not be changed. */
14631 int saved_x = it->current_x;
14632 struct text_pos saved_pos;
14633 Lisp_Object saved_object;
14634 enum display_element_type saved_what = it->what;
14635 int saved_face_id = it->face_id;
14636
14637 saved_object = it->object;
14638 saved_pos = it->position;
14639
14640 it->what = IT_CHARACTER;
14641 bzero (&it->position, sizeof it->position);
14642 it->object = make_number (0);
14643 it->c = ' ';
14644 it->len = 1;
14645 it->face_id = face->id;
14646
14647 PRODUCE_GLYPHS (it);
14648
14649 while (it->current_x <= it->last_visible_x)
14650 PRODUCE_GLYPHS (it);
14651
14652 /* Don't count these blanks really. It would let us insert a left
14653 truncation glyph below and make us set the cursor on them, maybe. */
14654 it->current_x = saved_x;
14655 it->object = saved_object;
14656 it->position = saved_pos;
14657 it->what = saved_what;
14658 it->face_id = saved_face_id;
14659 }
14660 }
14661
14662
14663 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14664 trailing whitespace. */
14665
14666 static int
14667 trailing_whitespace_p (charpos)
14668 int charpos;
14669 {
14670 int bytepos = CHAR_TO_BYTE (charpos);
14671 int c = 0;
14672
14673 while (bytepos < ZV_BYTE
14674 && (c = FETCH_CHAR (bytepos),
14675 c == ' ' || c == '\t'))
14676 ++bytepos;
14677
14678 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14679 {
14680 if (bytepos != PT_BYTE)
14681 return 1;
14682 }
14683 return 0;
14684 }
14685
14686
14687 /* Highlight trailing whitespace, if any, in ROW. */
14688
14689 void
14690 highlight_trailing_whitespace (f, row)
14691 struct frame *f;
14692 struct glyph_row *row;
14693 {
14694 int used = row->used[TEXT_AREA];
14695
14696 if (used)
14697 {
14698 struct glyph *start = row->glyphs[TEXT_AREA];
14699 struct glyph *glyph = start + used - 1;
14700
14701 /* Skip over glyphs inserted to display the cursor at the
14702 end of a line, for extending the face of the last glyph
14703 to the end of the line on terminals, and for truncation
14704 and continuation glyphs. */
14705 while (glyph >= start
14706 && glyph->type == CHAR_GLYPH
14707 && INTEGERP (glyph->object))
14708 --glyph;
14709
14710 /* If last glyph is a space or stretch, and it's trailing
14711 whitespace, set the face of all trailing whitespace glyphs in
14712 IT->glyph_row to `trailing-whitespace'. */
14713 if (glyph >= start
14714 && BUFFERP (glyph->object)
14715 && (glyph->type == STRETCH_GLYPH
14716 || (glyph->type == CHAR_GLYPH
14717 && glyph->u.ch == ' '))
14718 && trailing_whitespace_p (glyph->charpos))
14719 {
14720 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14721 if (face_id < 0)
14722 return;
14723
14724 while (glyph >= start
14725 && BUFFERP (glyph->object)
14726 && (glyph->type == STRETCH_GLYPH
14727 || (glyph->type == CHAR_GLYPH
14728 && glyph->u.ch == ' ')))
14729 (glyph--)->face_id = face_id;
14730 }
14731 }
14732 }
14733
14734
14735 /* Value is non-zero if glyph row ROW in window W should be
14736 used to hold the cursor. */
14737
14738 static int
14739 cursor_row_p (w, row)
14740 struct window *w;
14741 struct glyph_row *row;
14742 {
14743 int cursor_row_p = 1;
14744
14745 if (PT == MATRIX_ROW_END_CHARPOS (row))
14746 {
14747 /* If the row ends with a newline from a string, we don't want
14748 the cursor there (if the row is continued it doesn't end in a
14749 newline). */
14750 if (CHARPOS (row->end.string_pos) >= 0)
14751 cursor_row_p = row->continued_p;
14752 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14753 {
14754 /* If the row ends in middle of a real character,
14755 and the line is continued, we want the cursor here.
14756 That's because MATRIX_ROW_END_CHARPOS would equal
14757 PT if PT is before the character. */
14758 if (!row->ends_in_ellipsis_p)
14759 cursor_row_p = row->continued_p;
14760 else
14761 /* If the row ends in an ellipsis, then
14762 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14763 We want that position to be displayed after the ellipsis. */
14764 cursor_row_p = 0;
14765 }
14766 /* If the row ends at ZV, display the cursor at the end of that
14767 row instead of at the start of the row below. */
14768 else if (row->ends_at_zv_p)
14769 cursor_row_p = 1;
14770 else
14771 cursor_row_p = 0;
14772 }
14773
14774 return cursor_row_p;
14775 }
14776
14777
14778 /* Construct the glyph row IT->glyph_row in the desired matrix of
14779 IT->w from text at the current position of IT. See dispextern.h
14780 for an overview of struct it. Value is non-zero if
14781 IT->glyph_row displays text, as opposed to a line displaying ZV
14782 only. */
14783
14784 static int
14785 display_line (it)
14786 struct it *it;
14787 {
14788 struct glyph_row *row = it->glyph_row;
14789 int overlay_arrow_bitmap;
14790 Lisp_Object overlay_arrow_string;
14791
14792 /* We always start displaying at hpos zero even if hscrolled. */
14793 xassert (it->hpos == 0 && it->current_x == 0);
14794
14795 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14796 >= it->w->desired_matrix->nrows)
14797 {
14798 it->w->nrows_scale_factor++;
14799 fonts_changed_p = 1;
14800 return 0;
14801 }
14802
14803 /* Is IT->w showing the region? */
14804 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14805
14806 /* Clear the result glyph row and enable it. */
14807 prepare_desired_row (row);
14808
14809 row->y = it->current_y;
14810 row->start = it->start;
14811 row->continuation_lines_width = it->continuation_lines_width;
14812 row->displays_text_p = 1;
14813 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14814 it->starts_in_middle_of_char_p = 0;
14815
14816 /* Arrange the overlays nicely for our purposes. Usually, we call
14817 display_line on only one line at a time, in which case this
14818 can't really hurt too much, or we call it on lines which appear
14819 one after another in the buffer, in which case all calls to
14820 recenter_overlay_lists but the first will be pretty cheap. */
14821 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14822
14823 /* Move over display elements that are not visible because we are
14824 hscrolled. This may stop at an x-position < IT->first_visible_x
14825 if the first glyph is partially visible or if we hit a line end. */
14826 if (it->current_x < it->first_visible_x)
14827 {
14828 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14829 MOVE_TO_POS | MOVE_TO_X);
14830 }
14831
14832 /* Get the initial row height. This is either the height of the
14833 text hscrolled, if there is any, or zero. */
14834 row->ascent = it->max_ascent;
14835 row->height = it->max_ascent + it->max_descent;
14836 row->phys_ascent = it->max_phys_ascent;
14837 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14838 row->extra_line_spacing = it->max_extra_line_spacing;
14839
14840 /* Loop generating characters. The loop is left with IT on the next
14841 character to display. */
14842 while (1)
14843 {
14844 int n_glyphs_before, hpos_before, x_before;
14845 int x, i, nglyphs;
14846 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14847
14848 /* Retrieve the next thing to display. Value is zero if end of
14849 buffer reached. */
14850 if (!get_next_display_element (it))
14851 {
14852 /* Maybe add a space at the end of this line that is used to
14853 display the cursor there under X. Set the charpos of the
14854 first glyph of blank lines not corresponding to any text
14855 to -1. */
14856 #ifdef HAVE_WINDOW_SYSTEM
14857 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14858 row->exact_window_width_line_p = 1;
14859 else
14860 #endif /* HAVE_WINDOW_SYSTEM */
14861 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14862 || row->used[TEXT_AREA] == 0)
14863 {
14864 row->glyphs[TEXT_AREA]->charpos = -1;
14865 row->displays_text_p = 0;
14866
14867 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14868 && (!MINI_WINDOW_P (it->w)
14869 || (minibuf_level && EQ (it->window, minibuf_window))))
14870 row->indicate_empty_line_p = 1;
14871 }
14872
14873 it->continuation_lines_width = 0;
14874 row->ends_at_zv_p = 1;
14875 break;
14876 }
14877
14878 /* Now, get the metrics of what we want to display. This also
14879 generates glyphs in `row' (which is IT->glyph_row). */
14880 n_glyphs_before = row->used[TEXT_AREA];
14881 x = it->current_x;
14882
14883 /* Remember the line height so far in case the next element doesn't
14884 fit on the line. */
14885 if (!it->truncate_lines_p)
14886 {
14887 ascent = it->max_ascent;
14888 descent = it->max_descent;
14889 phys_ascent = it->max_phys_ascent;
14890 phys_descent = it->max_phys_descent;
14891 }
14892
14893 PRODUCE_GLYPHS (it);
14894
14895 /* If this display element was in marginal areas, continue with
14896 the next one. */
14897 if (it->area != TEXT_AREA)
14898 {
14899 row->ascent = max (row->ascent, it->max_ascent);
14900 row->height = max (row->height, it->max_ascent + it->max_descent);
14901 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14902 row->phys_height = max (row->phys_height,
14903 it->max_phys_ascent + it->max_phys_descent);
14904 row->extra_line_spacing = max (row->extra_line_spacing,
14905 it->max_extra_line_spacing);
14906 set_iterator_to_next (it, 1);
14907 continue;
14908 }
14909
14910 /* Does the display element fit on the line? If we truncate
14911 lines, we should draw past the right edge of the window. If
14912 we don't truncate, we want to stop so that we can display the
14913 continuation glyph before the right margin. If lines are
14914 continued, there are two possible strategies for characters
14915 resulting in more than 1 glyph (e.g. tabs): Display as many
14916 glyphs as possible in this line and leave the rest for the
14917 continuation line, or display the whole element in the next
14918 line. Original redisplay did the former, so we do it also. */
14919 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14920 hpos_before = it->hpos;
14921 x_before = x;
14922
14923 if (/* Not a newline. */
14924 nglyphs > 0
14925 /* Glyphs produced fit entirely in the line. */
14926 && it->current_x < it->last_visible_x)
14927 {
14928 it->hpos += nglyphs;
14929 row->ascent = max (row->ascent, it->max_ascent);
14930 row->height = max (row->height, it->max_ascent + it->max_descent);
14931 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14932 row->phys_height = max (row->phys_height,
14933 it->max_phys_ascent + it->max_phys_descent);
14934 row->extra_line_spacing = max (row->extra_line_spacing,
14935 it->max_extra_line_spacing);
14936 if (it->current_x - it->pixel_width < it->first_visible_x)
14937 row->x = x - it->first_visible_x;
14938 }
14939 else
14940 {
14941 int new_x;
14942 struct glyph *glyph;
14943
14944 for (i = 0; i < nglyphs; ++i, x = new_x)
14945 {
14946 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14947 new_x = x + glyph->pixel_width;
14948
14949 if (/* Lines are continued. */
14950 !it->truncate_lines_p
14951 && (/* Glyph doesn't fit on the line. */
14952 new_x > it->last_visible_x
14953 /* Or it fits exactly on a window system frame. */
14954 || (new_x == it->last_visible_x
14955 && FRAME_WINDOW_P (it->f))))
14956 {
14957 /* End of a continued line. */
14958
14959 if (it->hpos == 0
14960 || (new_x == it->last_visible_x
14961 && FRAME_WINDOW_P (it->f)))
14962 {
14963 /* Current glyph is the only one on the line or
14964 fits exactly on the line. We must continue
14965 the line because we can't draw the cursor
14966 after the glyph. */
14967 row->continued_p = 1;
14968 it->current_x = new_x;
14969 it->continuation_lines_width += new_x;
14970 ++it->hpos;
14971 if (i == nglyphs - 1)
14972 {
14973 set_iterator_to_next (it, 1);
14974 #ifdef HAVE_WINDOW_SYSTEM
14975 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14976 {
14977 if (!get_next_display_element (it))
14978 {
14979 row->exact_window_width_line_p = 1;
14980 it->continuation_lines_width = 0;
14981 row->continued_p = 0;
14982 row->ends_at_zv_p = 1;
14983 }
14984 else if (ITERATOR_AT_END_OF_LINE_P (it))
14985 {
14986 row->continued_p = 0;
14987 row->exact_window_width_line_p = 1;
14988 }
14989 }
14990 #endif /* HAVE_WINDOW_SYSTEM */
14991 }
14992 }
14993 else if (CHAR_GLYPH_PADDING_P (*glyph)
14994 && !FRAME_WINDOW_P (it->f))
14995 {
14996 /* A padding glyph that doesn't fit on this line.
14997 This means the whole character doesn't fit
14998 on the line. */
14999 row->used[TEXT_AREA] = n_glyphs_before;
15000
15001 /* Fill the rest of the row with continuation
15002 glyphs like in 20.x. */
15003 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15004 < row->glyphs[1 + TEXT_AREA])
15005 produce_special_glyphs (it, IT_CONTINUATION);
15006
15007 row->continued_p = 1;
15008 it->current_x = x_before;
15009 it->continuation_lines_width += x_before;
15010
15011 /* Restore the height to what it was before the
15012 element not fitting on the line. */
15013 it->max_ascent = ascent;
15014 it->max_descent = descent;
15015 it->max_phys_ascent = phys_ascent;
15016 it->max_phys_descent = phys_descent;
15017 }
15018 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15019 {
15020 /* A TAB that extends past the right edge of the
15021 window. This produces a single glyph on
15022 window system frames. We leave the glyph in
15023 this row and let it fill the row, but don't
15024 consume the TAB. */
15025 it->continuation_lines_width += it->last_visible_x;
15026 row->ends_in_middle_of_char_p = 1;
15027 row->continued_p = 1;
15028 glyph->pixel_width = it->last_visible_x - x;
15029 it->starts_in_middle_of_char_p = 1;
15030 }
15031 else
15032 {
15033 /* Something other than a TAB that draws past
15034 the right edge of the window. Restore
15035 positions to values before the element. */
15036 row->used[TEXT_AREA] = n_glyphs_before + i;
15037
15038 /* Display continuation glyphs. */
15039 if (!FRAME_WINDOW_P (it->f))
15040 produce_special_glyphs (it, IT_CONTINUATION);
15041 row->continued_p = 1;
15042
15043 it->continuation_lines_width += x;
15044
15045 if (nglyphs > 1 && i > 0)
15046 {
15047 row->ends_in_middle_of_char_p = 1;
15048 it->starts_in_middle_of_char_p = 1;
15049 }
15050
15051 /* Restore the height to what it was before the
15052 element not fitting on the line. */
15053 it->max_ascent = ascent;
15054 it->max_descent = descent;
15055 it->max_phys_ascent = phys_ascent;
15056 it->max_phys_descent = phys_descent;
15057 }
15058
15059 break;
15060 }
15061 else if (new_x > it->first_visible_x)
15062 {
15063 /* Increment number of glyphs actually displayed. */
15064 ++it->hpos;
15065
15066 if (x < it->first_visible_x)
15067 /* Glyph is partially visible, i.e. row starts at
15068 negative X position. */
15069 row->x = x - it->first_visible_x;
15070 }
15071 else
15072 {
15073 /* Glyph is completely off the left margin of the
15074 window. This should not happen because of the
15075 move_it_in_display_line at the start of this
15076 function, unless the text display area of the
15077 window is empty. */
15078 xassert (it->first_visible_x <= it->last_visible_x);
15079 }
15080 }
15081
15082 row->ascent = max (row->ascent, it->max_ascent);
15083 row->height = max (row->height, it->max_ascent + it->max_descent);
15084 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15085 row->phys_height = max (row->phys_height,
15086 it->max_phys_ascent + it->max_phys_descent);
15087 row->extra_line_spacing = max (row->extra_line_spacing,
15088 it->max_extra_line_spacing);
15089
15090 /* End of this display line if row is continued. */
15091 if (row->continued_p || row->ends_at_zv_p)
15092 break;
15093 }
15094
15095 at_end_of_line:
15096 /* Is this a line end? If yes, we're also done, after making
15097 sure that a non-default face is extended up to the right
15098 margin of the window. */
15099 if (ITERATOR_AT_END_OF_LINE_P (it))
15100 {
15101 int used_before = row->used[TEXT_AREA];
15102
15103 row->ends_in_newline_from_string_p = STRINGP (it->object);
15104
15105 #ifdef HAVE_WINDOW_SYSTEM
15106 /* Add a space at the end of the line that is used to
15107 display the cursor there. */
15108 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15109 append_space_for_newline (it, 0);
15110 #endif /* HAVE_WINDOW_SYSTEM */
15111
15112 /* Extend the face to the end of the line. */
15113 extend_face_to_end_of_line (it);
15114
15115 /* Make sure we have the position. */
15116 if (used_before == 0)
15117 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15118
15119 /* Consume the line end. This skips over invisible lines. */
15120 set_iterator_to_next (it, 1);
15121 it->continuation_lines_width = 0;
15122 break;
15123 }
15124
15125 /* Proceed with next display element. Note that this skips
15126 over lines invisible because of selective display. */
15127 set_iterator_to_next (it, 1);
15128
15129 /* If we truncate lines, we are done when the last displayed
15130 glyphs reach past the right margin of the window. */
15131 if (it->truncate_lines_p
15132 && (FRAME_WINDOW_P (it->f)
15133 ? (it->current_x >= it->last_visible_x)
15134 : (it->current_x > it->last_visible_x)))
15135 {
15136 /* Maybe add truncation glyphs. */
15137 if (!FRAME_WINDOW_P (it->f))
15138 {
15139 int i, n;
15140
15141 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15142 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15143 break;
15144
15145 for (n = row->used[TEXT_AREA]; i < n; ++i)
15146 {
15147 row->used[TEXT_AREA] = i;
15148 produce_special_glyphs (it, IT_TRUNCATION);
15149 }
15150 }
15151 #ifdef HAVE_WINDOW_SYSTEM
15152 else
15153 {
15154 /* Don't truncate if we can overflow newline into fringe. */
15155 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15156 {
15157 if (!get_next_display_element (it))
15158 {
15159 it->continuation_lines_width = 0;
15160 row->ends_at_zv_p = 1;
15161 row->exact_window_width_line_p = 1;
15162 break;
15163 }
15164 if (ITERATOR_AT_END_OF_LINE_P (it))
15165 {
15166 row->exact_window_width_line_p = 1;
15167 goto at_end_of_line;
15168 }
15169 }
15170 }
15171 #endif /* HAVE_WINDOW_SYSTEM */
15172
15173 row->truncated_on_right_p = 1;
15174 it->continuation_lines_width = 0;
15175 reseat_at_next_visible_line_start (it, 0);
15176 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15177 it->hpos = hpos_before;
15178 it->current_x = x_before;
15179 break;
15180 }
15181 }
15182
15183 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15184 at the left window margin. */
15185 if (it->first_visible_x
15186 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15187 {
15188 if (!FRAME_WINDOW_P (it->f))
15189 insert_left_trunc_glyphs (it);
15190 row->truncated_on_left_p = 1;
15191 }
15192
15193 /* If the start of this line is the overlay arrow-position, then
15194 mark this glyph row as the one containing the overlay arrow.
15195 This is clearly a mess with variable size fonts. It would be
15196 better to let it be displayed like cursors under X. */
15197 if (! overlay_arrow_seen
15198 && (overlay_arrow_string
15199 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15200 !NILP (overlay_arrow_string)))
15201 {
15202 /* Overlay arrow in window redisplay is a fringe bitmap. */
15203 if (STRINGP (overlay_arrow_string))
15204 {
15205 struct glyph_row *arrow_row
15206 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15207 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15208 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15209 struct glyph *p = row->glyphs[TEXT_AREA];
15210 struct glyph *p2, *end;
15211
15212 /* Copy the arrow glyphs. */
15213 while (glyph < arrow_end)
15214 *p++ = *glyph++;
15215
15216 /* Throw away padding glyphs. */
15217 p2 = p;
15218 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15219 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15220 ++p2;
15221 if (p2 > p)
15222 {
15223 while (p2 < end)
15224 *p++ = *p2++;
15225 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15226 }
15227 }
15228 else
15229 {
15230 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15231 row->overlay_arrow_p = 1;
15232 }
15233 overlay_arrow_seen = 1;
15234 }
15235
15236 /* Compute pixel dimensions of this line. */
15237 compute_line_metrics (it);
15238
15239 /* Remember the position at which this line ends. */
15240 row->end = it->current;
15241
15242 /* Record whether this row ends inside an ellipsis. */
15243 row->ends_in_ellipsis_p
15244 = (it->method == next_element_from_display_vector
15245 && it->ellipsis_p);
15246
15247 /* Save fringe bitmaps in this row. */
15248 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15249 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15250 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15251 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15252
15253 it->left_user_fringe_bitmap = 0;
15254 it->left_user_fringe_face_id = 0;
15255 it->right_user_fringe_bitmap = 0;
15256 it->right_user_fringe_face_id = 0;
15257
15258 /* Maybe set the cursor. */
15259 if (it->w->cursor.vpos < 0
15260 && PT >= MATRIX_ROW_START_CHARPOS (row)
15261 && PT <= MATRIX_ROW_END_CHARPOS (row)
15262 && cursor_row_p (it->w, row))
15263 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15264
15265 /* Highlight trailing whitespace. */
15266 if (!NILP (Vshow_trailing_whitespace))
15267 highlight_trailing_whitespace (it->f, it->glyph_row);
15268
15269 /* Prepare for the next line. This line starts horizontally at (X
15270 HPOS) = (0 0). Vertical positions are incremented. As a
15271 convenience for the caller, IT->glyph_row is set to the next
15272 row to be used. */
15273 it->current_x = it->hpos = 0;
15274 it->current_y += row->height;
15275 ++it->vpos;
15276 ++it->glyph_row;
15277 it->start = it->current;
15278 return row->displays_text_p;
15279 }
15280
15281
15282 \f
15283 /***********************************************************************
15284 Menu Bar
15285 ***********************************************************************/
15286
15287 /* Redisplay the menu bar in the frame for window W.
15288
15289 The menu bar of X frames that don't have X toolkit support is
15290 displayed in a special window W->frame->menu_bar_window.
15291
15292 The menu bar of terminal frames is treated specially as far as
15293 glyph matrices are concerned. Menu bar lines are not part of
15294 windows, so the update is done directly on the frame matrix rows
15295 for the menu bar. */
15296
15297 static void
15298 display_menu_bar (w)
15299 struct window *w;
15300 {
15301 struct frame *f = XFRAME (WINDOW_FRAME (w));
15302 struct it it;
15303 Lisp_Object items;
15304 int i;
15305
15306 /* Don't do all this for graphical frames. */
15307 #ifdef HAVE_NTGUI
15308 if (!NILP (Vwindow_system))
15309 return;
15310 #endif
15311 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15312 if (FRAME_X_P (f))
15313 return;
15314 #endif
15315 #ifdef MAC_OS
15316 if (FRAME_MAC_P (f))
15317 return;
15318 #endif
15319
15320 #ifdef USE_X_TOOLKIT
15321 xassert (!FRAME_WINDOW_P (f));
15322 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15323 it.first_visible_x = 0;
15324 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15325 #else /* not USE_X_TOOLKIT */
15326 if (FRAME_WINDOW_P (f))
15327 {
15328 /* Menu bar lines are displayed in the desired matrix of the
15329 dummy window menu_bar_window. */
15330 struct window *menu_w;
15331 xassert (WINDOWP (f->menu_bar_window));
15332 menu_w = XWINDOW (f->menu_bar_window);
15333 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15334 MENU_FACE_ID);
15335 it.first_visible_x = 0;
15336 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15337 }
15338 else
15339 {
15340 /* This is a TTY frame, i.e. character hpos/vpos are used as
15341 pixel x/y. */
15342 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15343 MENU_FACE_ID);
15344 it.first_visible_x = 0;
15345 it.last_visible_x = FRAME_COLS (f);
15346 }
15347 #endif /* not USE_X_TOOLKIT */
15348
15349 if (! mode_line_inverse_video)
15350 /* Force the menu-bar to be displayed in the default face. */
15351 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15352
15353 /* Clear all rows of the menu bar. */
15354 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15355 {
15356 struct glyph_row *row = it.glyph_row + i;
15357 clear_glyph_row (row);
15358 row->enabled_p = 1;
15359 row->full_width_p = 1;
15360 }
15361
15362 /* Display all items of the menu bar. */
15363 items = FRAME_MENU_BAR_ITEMS (it.f);
15364 for (i = 0; i < XVECTOR (items)->size; i += 4)
15365 {
15366 Lisp_Object string;
15367
15368 /* Stop at nil string. */
15369 string = AREF (items, i + 1);
15370 if (NILP (string))
15371 break;
15372
15373 /* Remember where item was displayed. */
15374 AREF (items, i + 3) = make_number (it.hpos);
15375
15376 /* Display the item, pad with one space. */
15377 if (it.current_x < it.last_visible_x)
15378 display_string (NULL, string, Qnil, 0, 0, &it,
15379 SCHARS (string) + 1, 0, 0, -1);
15380 }
15381
15382 /* Fill out the line with spaces. */
15383 if (it.current_x < it.last_visible_x)
15384 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15385
15386 /* Compute the total height of the lines. */
15387 compute_line_metrics (&it);
15388 }
15389
15390
15391 \f
15392 /***********************************************************************
15393 Mode Line
15394 ***********************************************************************/
15395
15396 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15397 FORCE is non-zero, redisplay mode lines unconditionally.
15398 Otherwise, redisplay only mode lines that are garbaged. Value is
15399 the number of windows whose mode lines were redisplayed. */
15400
15401 static int
15402 redisplay_mode_lines (window, force)
15403 Lisp_Object window;
15404 int force;
15405 {
15406 int nwindows = 0;
15407
15408 while (!NILP (window))
15409 {
15410 struct window *w = XWINDOW (window);
15411
15412 if (WINDOWP (w->hchild))
15413 nwindows += redisplay_mode_lines (w->hchild, force);
15414 else if (WINDOWP (w->vchild))
15415 nwindows += redisplay_mode_lines (w->vchild, force);
15416 else if (force
15417 || FRAME_GARBAGED_P (XFRAME (w->frame))
15418 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15419 {
15420 struct text_pos lpoint;
15421 struct buffer *old = current_buffer;
15422
15423 /* Set the window's buffer for the mode line display. */
15424 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15425 set_buffer_internal_1 (XBUFFER (w->buffer));
15426
15427 /* Point refers normally to the selected window. For any
15428 other window, set up appropriate value. */
15429 if (!EQ (window, selected_window))
15430 {
15431 struct text_pos pt;
15432
15433 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15434 if (CHARPOS (pt) < BEGV)
15435 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15436 else if (CHARPOS (pt) > (ZV - 1))
15437 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15438 else
15439 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15440 }
15441
15442 /* Display mode lines. */
15443 clear_glyph_matrix (w->desired_matrix);
15444 if (display_mode_lines (w))
15445 {
15446 ++nwindows;
15447 w->must_be_updated_p = 1;
15448 }
15449
15450 /* Restore old settings. */
15451 set_buffer_internal_1 (old);
15452 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15453 }
15454
15455 window = w->next;
15456 }
15457
15458 return nwindows;
15459 }
15460
15461
15462 /* Display the mode and/or top line of window W. Value is the number
15463 of mode lines displayed. */
15464
15465 static int
15466 display_mode_lines (w)
15467 struct window *w;
15468 {
15469 Lisp_Object old_selected_window, old_selected_frame;
15470 int n = 0;
15471
15472 old_selected_frame = selected_frame;
15473 selected_frame = w->frame;
15474 old_selected_window = selected_window;
15475 XSETWINDOW (selected_window, w);
15476
15477 /* These will be set while the mode line specs are processed. */
15478 line_number_displayed = 0;
15479 w->column_number_displayed = Qnil;
15480
15481 if (WINDOW_WANTS_MODELINE_P (w))
15482 {
15483 struct window *sel_w = XWINDOW (old_selected_window);
15484
15485 /* Select mode line face based on the real selected window. */
15486 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15487 current_buffer->mode_line_format);
15488 ++n;
15489 }
15490
15491 if (WINDOW_WANTS_HEADER_LINE_P (w))
15492 {
15493 display_mode_line (w, HEADER_LINE_FACE_ID,
15494 current_buffer->header_line_format);
15495 ++n;
15496 }
15497
15498 selected_frame = old_selected_frame;
15499 selected_window = old_selected_window;
15500 return n;
15501 }
15502
15503
15504 /* Display mode or top line of window W. FACE_ID specifies which line
15505 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15506 FORMAT is the mode line format to display. Value is the pixel
15507 height of the mode line displayed. */
15508
15509 static int
15510 display_mode_line (w, face_id, format)
15511 struct window *w;
15512 enum face_id face_id;
15513 Lisp_Object format;
15514 {
15515 struct it it;
15516 struct face *face;
15517
15518 init_iterator (&it, w, -1, -1, NULL, face_id);
15519 prepare_desired_row (it.glyph_row);
15520
15521 it.glyph_row->mode_line_p = 1;
15522
15523 if (! mode_line_inverse_video)
15524 /* Force the mode-line to be displayed in the default face. */
15525 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15526
15527 /* Temporarily make frame's keyboard the current kboard so that
15528 kboard-local variables in the mode_line_format will get the right
15529 values. */
15530 push_frame_kboard (it.f);
15531 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15532 pop_frame_kboard ();
15533
15534 /* Fill up with spaces. */
15535 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15536
15537 compute_line_metrics (&it);
15538 it.glyph_row->full_width_p = 1;
15539 it.glyph_row->continued_p = 0;
15540 it.glyph_row->truncated_on_left_p = 0;
15541 it.glyph_row->truncated_on_right_p = 0;
15542
15543 /* Make a 3D mode-line have a shadow at its right end. */
15544 face = FACE_FROM_ID (it.f, face_id);
15545 extend_face_to_end_of_line (&it);
15546 if (face->box != FACE_NO_BOX)
15547 {
15548 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15549 + it.glyph_row->used[TEXT_AREA] - 1);
15550 last->right_box_line_p = 1;
15551 }
15552
15553 return it.glyph_row->height;
15554 }
15555
15556 /* Alist that caches the results of :propertize.
15557 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15558 Lisp_Object mode_line_proptrans_alist;
15559
15560 /* List of strings making up the mode-line. */
15561 Lisp_Object mode_line_string_list;
15562
15563 /* Base face property when building propertized mode line string. */
15564 static Lisp_Object mode_line_string_face;
15565 static Lisp_Object mode_line_string_face_prop;
15566
15567
15568 /* Contribute ELT to the mode line for window IT->w. How it
15569 translates into text depends on its data type.
15570
15571 IT describes the display environment in which we display, as usual.
15572
15573 DEPTH is the depth in recursion. It is used to prevent
15574 infinite recursion here.
15575
15576 FIELD_WIDTH is the number of characters the display of ELT should
15577 occupy in the mode line, and PRECISION is the maximum number of
15578 characters to display from ELT's representation. See
15579 display_string for details.
15580
15581 Returns the hpos of the end of the text generated by ELT.
15582
15583 PROPS is a property list to add to any string we encounter.
15584
15585 If RISKY is nonzero, remove (disregard) any properties in any string
15586 we encounter, and ignore :eval and :propertize.
15587
15588 If the global variable `frame_title_ptr' is non-NULL, then the output
15589 is passed to `store_frame_title' instead of `display_string'. */
15590
15591 static int
15592 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15593 struct it *it;
15594 int depth;
15595 int field_width, precision;
15596 Lisp_Object elt, props;
15597 int risky;
15598 {
15599 int n = 0, field, prec;
15600 int literal = 0;
15601
15602 tail_recurse:
15603 if (depth > 100)
15604 elt = build_string ("*too-deep*");
15605
15606 depth++;
15607
15608 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15609 {
15610 case Lisp_String:
15611 {
15612 /* A string: output it and check for %-constructs within it. */
15613 unsigned char c;
15614 const unsigned char *this, *lisp_string;
15615
15616 if (!NILP (props) || risky)
15617 {
15618 Lisp_Object oprops, aelt;
15619 oprops = Ftext_properties_at (make_number (0), elt);
15620
15621 /* If the starting string's properties are not what
15622 we want, translate the string. Also, if the string
15623 is risky, do that anyway. */
15624
15625 if (NILP (Fequal (props, oprops)) || risky)
15626 {
15627 /* If the starting string has properties,
15628 merge the specified ones onto the existing ones. */
15629 if (! NILP (oprops) && !risky)
15630 {
15631 Lisp_Object tem;
15632
15633 oprops = Fcopy_sequence (oprops);
15634 tem = props;
15635 while (CONSP (tem))
15636 {
15637 oprops = Fplist_put (oprops, XCAR (tem),
15638 XCAR (XCDR (tem)));
15639 tem = XCDR (XCDR (tem));
15640 }
15641 props = oprops;
15642 }
15643
15644 aelt = Fassoc (elt, mode_line_proptrans_alist);
15645 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15646 {
15647 mode_line_proptrans_alist
15648 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15649 elt = XCAR (aelt);
15650 }
15651 else
15652 {
15653 Lisp_Object tem;
15654
15655 elt = Fcopy_sequence (elt);
15656 Fset_text_properties (make_number (0), Flength (elt),
15657 props, elt);
15658 /* Add this item to mode_line_proptrans_alist. */
15659 mode_line_proptrans_alist
15660 = Fcons (Fcons (elt, props),
15661 mode_line_proptrans_alist);
15662 /* Truncate mode_line_proptrans_alist
15663 to at most 50 elements. */
15664 tem = Fnthcdr (make_number (50),
15665 mode_line_proptrans_alist);
15666 if (! NILP (tem))
15667 XSETCDR (tem, Qnil);
15668 }
15669 }
15670 }
15671
15672 this = SDATA (elt);
15673 lisp_string = this;
15674
15675 if (literal)
15676 {
15677 prec = precision - n;
15678 if (frame_title_ptr)
15679 n += store_frame_title (SDATA (elt), -1, prec);
15680 else if (!NILP (mode_line_string_list))
15681 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15682 else
15683 n += display_string (NULL, elt, Qnil, 0, 0, it,
15684 0, prec, 0, STRING_MULTIBYTE (elt));
15685
15686 break;
15687 }
15688
15689 while ((precision <= 0 || n < precision)
15690 && *this
15691 && (frame_title_ptr
15692 || !NILP (mode_line_string_list)
15693 || it->current_x < it->last_visible_x))
15694 {
15695 const unsigned char *last = this;
15696
15697 /* Advance to end of string or next format specifier. */
15698 while ((c = *this++) != '\0' && c != '%')
15699 ;
15700
15701 if (this - 1 != last)
15702 {
15703 int nchars, nbytes;
15704
15705 /* Output to end of string or up to '%'. Field width
15706 is length of string. Don't output more than
15707 PRECISION allows us. */
15708 --this;
15709
15710 prec = c_string_width (last, this - last, precision - n,
15711 &nchars, &nbytes);
15712
15713 if (frame_title_ptr)
15714 n += store_frame_title (last, 0, prec);
15715 else if (!NILP (mode_line_string_list))
15716 {
15717 int bytepos = last - lisp_string;
15718 int charpos = string_byte_to_char (elt, bytepos);
15719 int endpos = (precision <= 0
15720 ? string_byte_to_char (elt,
15721 this - lisp_string)
15722 : charpos + nchars);
15723
15724 n += store_mode_line_string (NULL,
15725 Fsubstring (elt, make_number (charpos),
15726 make_number (endpos)),
15727 0, 0, 0, Qnil);
15728 }
15729 else
15730 {
15731 int bytepos = last - lisp_string;
15732 int charpos = string_byte_to_char (elt, bytepos);
15733 n += display_string (NULL, elt, Qnil, 0, charpos,
15734 it, 0, prec, 0,
15735 STRING_MULTIBYTE (elt));
15736 }
15737 }
15738 else /* c == '%' */
15739 {
15740 const unsigned char *percent_position = this;
15741
15742 /* Get the specified minimum width. Zero means
15743 don't pad. */
15744 field = 0;
15745 while ((c = *this++) >= '0' && c <= '9')
15746 field = field * 10 + c - '0';
15747
15748 /* Don't pad beyond the total padding allowed. */
15749 if (field_width - n > 0 && field > field_width - n)
15750 field = field_width - n;
15751
15752 /* Note that either PRECISION <= 0 or N < PRECISION. */
15753 prec = precision - n;
15754
15755 if (c == 'M')
15756 n += display_mode_element (it, depth, field, prec,
15757 Vglobal_mode_string, props,
15758 risky);
15759 else if (c != 0)
15760 {
15761 int multibyte;
15762 int bytepos, charpos;
15763 unsigned char *spec;
15764
15765 bytepos = percent_position - lisp_string;
15766 charpos = (STRING_MULTIBYTE (elt)
15767 ? string_byte_to_char (elt, bytepos)
15768 : bytepos);
15769
15770 spec
15771 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15772
15773 if (frame_title_ptr)
15774 n += store_frame_title (spec, field, prec);
15775 else if (!NILP (mode_line_string_list))
15776 {
15777 int len = strlen (spec);
15778 Lisp_Object tem = make_string (spec, len);
15779 props = Ftext_properties_at (make_number (charpos), elt);
15780 /* Should only keep face property in props */
15781 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15782 }
15783 else
15784 {
15785 int nglyphs_before, nwritten;
15786
15787 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15788 nwritten = display_string (spec, Qnil, elt,
15789 charpos, 0, it,
15790 field, prec, 0,
15791 multibyte);
15792
15793 /* Assign to the glyphs written above the
15794 string where the `%x' came from, position
15795 of the `%'. */
15796 if (nwritten > 0)
15797 {
15798 struct glyph *glyph
15799 = (it->glyph_row->glyphs[TEXT_AREA]
15800 + nglyphs_before);
15801 int i;
15802
15803 for (i = 0; i < nwritten; ++i)
15804 {
15805 glyph[i].object = elt;
15806 glyph[i].charpos = charpos;
15807 }
15808
15809 n += nwritten;
15810 }
15811 }
15812 }
15813 else /* c == 0 */
15814 break;
15815 }
15816 }
15817 }
15818 break;
15819
15820 case Lisp_Symbol:
15821 /* A symbol: process the value of the symbol recursively
15822 as if it appeared here directly. Avoid error if symbol void.
15823 Special case: if value of symbol is a string, output the string
15824 literally. */
15825 {
15826 register Lisp_Object tem;
15827
15828 /* If the variable is not marked as risky to set
15829 then its contents are risky to use. */
15830 if (NILP (Fget (elt, Qrisky_local_variable)))
15831 risky = 1;
15832
15833 tem = Fboundp (elt);
15834 if (!NILP (tem))
15835 {
15836 tem = Fsymbol_value (elt);
15837 /* If value is a string, output that string literally:
15838 don't check for % within it. */
15839 if (STRINGP (tem))
15840 literal = 1;
15841
15842 if (!EQ (tem, elt))
15843 {
15844 /* Give up right away for nil or t. */
15845 elt = tem;
15846 goto tail_recurse;
15847 }
15848 }
15849 }
15850 break;
15851
15852 case Lisp_Cons:
15853 {
15854 register Lisp_Object car, tem;
15855
15856 /* A cons cell: five distinct cases.
15857 If first element is :eval or :propertize, do something special.
15858 If first element is a string or a cons, process all the elements
15859 and effectively concatenate them.
15860 If first element is a negative number, truncate displaying cdr to
15861 at most that many characters. If positive, pad (with spaces)
15862 to at least that many characters.
15863 If first element is a symbol, process the cadr or caddr recursively
15864 according to whether the symbol's value is non-nil or nil. */
15865 car = XCAR (elt);
15866 if (EQ (car, QCeval))
15867 {
15868 /* An element of the form (:eval FORM) means evaluate FORM
15869 and use the result as mode line elements. */
15870
15871 if (risky)
15872 break;
15873
15874 if (CONSP (XCDR (elt)))
15875 {
15876 Lisp_Object spec;
15877 spec = safe_eval (XCAR (XCDR (elt)));
15878 n += display_mode_element (it, depth, field_width - n,
15879 precision - n, spec, props,
15880 risky);
15881 }
15882 }
15883 else if (EQ (car, QCpropertize))
15884 {
15885 /* An element of the form (:propertize ELT PROPS...)
15886 means display ELT but applying properties PROPS. */
15887
15888 if (risky)
15889 break;
15890
15891 if (CONSP (XCDR (elt)))
15892 n += display_mode_element (it, depth, field_width - n,
15893 precision - n, XCAR (XCDR (elt)),
15894 XCDR (XCDR (elt)), risky);
15895 }
15896 else if (SYMBOLP (car))
15897 {
15898 tem = Fboundp (car);
15899 elt = XCDR (elt);
15900 if (!CONSP (elt))
15901 goto invalid;
15902 /* elt is now the cdr, and we know it is a cons cell.
15903 Use its car if CAR has a non-nil value. */
15904 if (!NILP (tem))
15905 {
15906 tem = Fsymbol_value (car);
15907 if (!NILP (tem))
15908 {
15909 elt = XCAR (elt);
15910 goto tail_recurse;
15911 }
15912 }
15913 /* Symbol's value is nil (or symbol is unbound)
15914 Get the cddr of the original list
15915 and if possible find the caddr and use that. */
15916 elt = XCDR (elt);
15917 if (NILP (elt))
15918 break;
15919 else if (!CONSP (elt))
15920 goto invalid;
15921 elt = XCAR (elt);
15922 goto tail_recurse;
15923 }
15924 else if (INTEGERP (car))
15925 {
15926 register int lim = XINT (car);
15927 elt = XCDR (elt);
15928 if (lim < 0)
15929 {
15930 /* Negative int means reduce maximum width. */
15931 if (precision <= 0)
15932 precision = -lim;
15933 else
15934 precision = min (precision, -lim);
15935 }
15936 else if (lim > 0)
15937 {
15938 /* Padding specified. Don't let it be more than
15939 current maximum. */
15940 if (precision > 0)
15941 lim = min (precision, lim);
15942
15943 /* If that's more padding than already wanted, queue it.
15944 But don't reduce padding already specified even if
15945 that is beyond the current truncation point. */
15946 field_width = max (lim, field_width);
15947 }
15948 goto tail_recurse;
15949 }
15950 else if (STRINGP (car) || CONSP (car))
15951 {
15952 register int limit = 50;
15953 /* Limit is to protect against circular lists. */
15954 while (CONSP (elt)
15955 && --limit > 0
15956 && (precision <= 0 || n < precision))
15957 {
15958 n += display_mode_element (it, depth, field_width - n,
15959 precision - n, XCAR (elt),
15960 props, risky);
15961 elt = XCDR (elt);
15962 }
15963 }
15964 }
15965 break;
15966
15967 default:
15968 invalid:
15969 elt = build_string ("*invalid*");
15970 goto tail_recurse;
15971 }
15972
15973 /* Pad to FIELD_WIDTH. */
15974 if (field_width > 0 && n < field_width)
15975 {
15976 if (frame_title_ptr)
15977 n += store_frame_title ("", field_width - n, 0);
15978 else if (!NILP (mode_line_string_list))
15979 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15980 else
15981 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15982 0, 0, 0);
15983 }
15984
15985 return n;
15986 }
15987
15988 /* Store a mode-line string element in mode_line_string_list.
15989
15990 If STRING is non-null, display that C string. Otherwise, the Lisp
15991 string LISP_STRING is displayed.
15992
15993 FIELD_WIDTH is the minimum number of output glyphs to produce.
15994 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15995 with spaces. FIELD_WIDTH <= 0 means don't pad.
15996
15997 PRECISION is the maximum number of characters to output from
15998 STRING. PRECISION <= 0 means don't truncate the string.
15999
16000 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16001 properties to the string.
16002
16003 PROPS are the properties to add to the string.
16004 The mode_line_string_face face property is always added to the string.
16005 */
16006
16007 static int
16008 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16009 char *string;
16010 Lisp_Object lisp_string;
16011 int copy_string;
16012 int field_width;
16013 int precision;
16014 Lisp_Object props;
16015 {
16016 int len;
16017 int n = 0;
16018
16019 if (string != NULL)
16020 {
16021 len = strlen (string);
16022 if (precision > 0 && len > precision)
16023 len = precision;
16024 lisp_string = make_string (string, len);
16025 if (NILP (props))
16026 props = mode_line_string_face_prop;
16027 else if (!NILP (mode_line_string_face))
16028 {
16029 Lisp_Object face = Fsafe_plist_get (props, Qface);
16030 props = Fcopy_sequence (props);
16031 if (NILP (face))
16032 face = mode_line_string_face;
16033 else
16034 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16035 props = Fplist_put (props, Qface, face);
16036 }
16037 Fadd_text_properties (make_number (0), make_number (len),
16038 props, lisp_string);
16039 }
16040 else
16041 {
16042 len = XFASTINT (Flength (lisp_string));
16043 if (precision > 0 && len > precision)
16044 {
16045 len = precision;
16046 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16047 precision = -1;
16048 }
16049 if (!NILP (mode_line_string_face))
16050 {
16051 Lisp_Object face;
16052 if (NILP (props))
16053 props = Ftext_properties_at (make_number (0), lisp_string);
16054 face = Fsafe_plist_get (props, Qface);
16055 if (NILP (face))
16056 face = mode_line_string_face;
16057 else
16058 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16059 props = Fcons (Qface, Fcons (face, Qnil));
16060 if (copy_string)
16061 lisp_string = Fcopy_sequence (lisp_string);
16062 }
16063 if (!NILP (props))
16064 Fadd_text_properties (make_number (0), make_number (len),
16065 props, lisp_string);
16066 }
16067
16068 if (len > 0)
16069 {
16070 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16071 n += len;
16072 }
16073
16074 if (field_width > len)
16075 {
16076 field_width -= len;
16077 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16078 if (!NILP (props))
16079 Fadd_text_properties (make_number (0), make_number (field_width),
16080 props, lisp_string);
16081 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16082 n += field_width;
16083 }
16084
16085 return n;
16086 }
16087
16088
16089 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16090 1, 4, 0,
16091 doc: /* Format a string out of a mode line format specification.
16092 First arg FORMAT specifies the mode line format (see `mode-line-format'
16093 for details) to use.
16094
16095 Optional second arg FACE specifies the face property to put
16096 on all characters for which no face is specified.
16097 t means whatever face the window's mode line currently uses
16098 \(either `mode-line' or `mode-line-inactive', depending).
16099 nil means the default is no face property.
16100 If FACE is an integer, the value string has no text properties.
16101
16102 Optional third and fourth args WINDOW and BUFFER specify the window
16103 and buffer to use as the context for the formatting (defaults
16104 are the selected window and the window's buffer). */)
16105 (format, face, window, buffer)
16106 Lisp_Object format, face, window, buffer;
16107 {
16108 struct it it;
16109 int len;
16110 struct window *w;
16111 struct buffer *old_buffer = NULL;
16112 int face_id = -1;
16113 int no_props = INTEGERP (face);
16114
16115 if (NILP (window))
16116 window = selected_window;
16117 CHECK_WINDOW (window);
16118 w = XWINDOW (window);
16119
16120 if (NILP (buffer))
16121 buffer = w->buffer;
16122 CHECK_BUFFER (buffer);
16123
16124 if (NILP (format))
16125 return build_string ("");
16126
16127 if (no_props)
16128 face = Qnil;
16129
16130 if (!NILP (face))
16131 {
16132 if (EQ (face, Qt))
16133 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16134 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16135 }
16136
16137 if (face_id < 0)
16138 face_id = DEFAULT_FACE_ID;
16139
16140 if (XBUFFER (buffer) != current_buffer)
16141 {
16142 old_buffer = current_buffer;
16143 set_buffer_internal_1 (XBUFFER (buffer));
16144 }
16145
16146 init_iterator (&it, w, -1, -1, NULL, face_id);
16147
16148 if (!no_props)
16149 {
16150 mode_line_string_face = face;
16151 mode_line_string_face_prop
16152 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16153
16154 /* We need a dummy last element in mode_line_string_list to
16155 indicate we are building the propertized mode-line string.
16156 Using mode_line_string_face_prop here GC protects it. */
16157 mode_line_string_list
16158 = Fcons (mode_line_string_face_prop, Qnil);
16159 frame_title_ptr = NULL;
16160 }
16161 else
16162 {
16163 mode_line_string_face_prop = Qnil;
16164 mode_line_string_list = Qnil;
16165 frame_title_ptr = frame_title_buf;
16166 }
16167
16168 push_frame_kboard (it.f);
16169 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16170 pop_frame_kboard ();
16171
16172 if (old_buffer)
16173 set_buffer_internal_1 (old_buffer);
16174
16175 if (!no_props)
16176 {
16177 Lisp_Object str;
16178 mode_line_string_list = Fnreverse (mode_line_string_list);
16179 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16180 make_string ("", 0));
16181 mode_line_string_face_prop = Qnil;
16182 mode_line_string_list = Qnil;
16183 return str;
16184 }
16185
16186 len = frame_title_ptr - frame_title_buf;
16187 if (len > 0 && frame_title_ptr[-1] == '-')
16188 {
16189 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16190 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16191 ;
16192 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16193 if (len > frame_title_ptr - frame_title_buf)
16194 len = frame_title_ptr - frame_title_buf;
16195 }
16196
16197 frame_title_ptr = NULL;
16198 return make_string (frame_title_buf, len);
16199 }
16200
16201 /* Write a null-terminated, right justified decimal representation of
16202 the positive integer D to BUF using a minimal field width WIDTH. */
16203
16204 static void
16205 pint2str (buf, width, d)
16206 register char *buf;
16207 register int width;
16208 register int d;
16209 {
16210 register char *p = buf;
16211
16212 if (d <= 0)
16213 *p++ = '0';
16214 else
16215 {
16216 while (d > 0)
16217 {
16218 *p++ = d % 10 + '0';
16219 d /= 10;
16220 }
16221 }
16222
16223 for (width -= (int) (p - buf); width > 0; --width)
16224 *p++ = ' ';
16225 *p-- = '\0';
16226 while (p > buf)
16227 {
16228 d = *buf;
16229 *buf++ = *p;
16230 *p-- = d;
16231 }
16232 }
16233
16234 /* Write a null-terminated, right justified decimal and "human
16235 readable" representation of the nonnegative integer D to BUF using
16236 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16237
16238 static const char power_letter[] =
16239 {
16240 0, /* not used */
16241 'k', /* kilo */
16242 'M', /* mega */
16243 'G', /* giga */
16244 'T', /* tera */
16245 'P', /* peta */
16246 'E', /* exa */
16247 'Z', /* zetta */
16248 'Y' /* yotta */
16249 };
16250
16251 static void
16252 pint2hrstr (buf, width, d)
16253 char *buf;
16254 int width;
16255 int d;
16256 {
16257 /* We aim to represent the nonnegative integer D as
16258 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16259 int quotient = d;
16260 int remainder = 0;
16261 /* -1 means: do not use TENTHS. */
16262 int tenths = -1;
16263 int exponent = 0;
16264
16265 /* Length of QUOTIENT.TENTHS as a string. */
16266 int length;
16267
16268 char * psuffix;
16269 char * p;
16270
16271 if (1000 <= quotient)
16272 {
16273 /* Scale to the appropriate EXPONENT. */
16274 do
16275 {
16276 remainder = quotient % 1000;
16277 quotient /= 1000;
16278 exponent++;
16279 }
16280 while (1000 <= quotient);
16281
16282 /* Round to nearest and decide whether to use TENTHS or not. */
16283 if (quotient <= 9)
16284 {
16285 tenths = remainder / 100;
16286 if (50 <= remainder % 100)
16287 {
16288 if (tenths < 9)
16289 tenths++;
16290 else
16291 {
16292 quotient++;
16293 if (quotient == 10)
16294 tenths = -1;
16295 else
16296 tenths = 0;
16297 }
16298 }
16299 }
16300 else
16301 if (500 <= remainder)
16302 {
16303 if (quotient < 999)
16304 quotient++;
16305 else
16306 {
16307 quotient = 1;
16308 exponent++;
16309 tenths = 0;
16310 }
16311 }
16312 }
16313
16314 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16315 if (tenths == -1 && quotient <= 99)
16316 if (quotient <= 9)
16317 length = 1;
16318 else
16319 length = 2;
16320 else
16321 length = 3;
16322 p = psuffix = buf + max (width, length);
16323
16324 /* Print EXPONENT. */
16325 if (exponent)
16326 *psuffix++ = power_letter[exponent];
16327 *psuffix = '\0';
16328
16329 /* Print TENTHS. */
16330 if (tenths >= 0)
16331 {
16332 *--p = '0' + tenths;
16333 *--p = '.';
16334 }
16335
16336 /* Print QUOTIENT. */
16337 do
16338 {
16339 int digit = quotient % 10;
16340 *--p = '0' + digit;
16341 }
16342 while ((quotient /= 10) != 0);
16343
16344 /* Print leading spaces. */
16345 while (buf < p)
16346 *--p = ' ';
16347 }
16348
16349 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16350 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16351 type of CODING_SYSTEM. Return updated pointer into BUF. */
16352
16353 static unsigned char invalid_eol_type[] = "(*invalid*)";
16354
16355 static char *
16356 decode_mode_spec_coding (coding_system, buf, eol_flag)
16357 Lisp_Object coding_system;
16358 register char *buf;
16359 int eol_flag;
16360 {
16361 Lisp_Object val;
16362 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16363 const unsigned char *eol_str;
16364 int eol_str_len;
16365 /* The EOL conversion we are using. */
16366 Lisp_Object eoltype;
16367
16368 val = Fget (coding_system, Qcoding_system);
16369 eoltype = Qnil;
16370
16371 if (!VECTORP (val)) /* Not yet decided. */
16372 {
16373 if (multibyte)
16374 *buf++ = '-';
16375 if (eol_flag)
16376 eoltype = eol_mnemonic_undecided;
16377 /* Don't mention EOL conversion if it isn't decided. */
16378 }
16379 else
16380 {
16381 Lisp_Object eolvalue;
16382
16383 eolvalue = Fget (coding_system, Qeol_type);
16384
16385 if (multibyte)
16386 *buf++ = XFASTINT (AREF (val, 1));
16387
16388 if (eol_flag)
16389 {
16390 /* The EOL conversion that is normal on this system. */
16391
16392 if (NILP (eolvalue)) /* Not yet decided. */
16393 eoltype = eol_mnemonic_undecided;
16394 else if (VECTORP (eolvalue)) /* Not yet decided. */
16395 eoltype = eol_mnemonic_undecided;
16396 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16397 eoltype = (XFASTINT (eolvalue) == 0
16398 ? eol_mnemonic_unix
16399 : (XFASTINT (eolvalue) == 1
16400 ? eol_mnemonic_dos : eol_mnemonic_mac));
16401 }
16402 }
16403
16404 if (eol_flag)
16405 {
16406 /* Mention the EOL conversion if it is not the usual one. */
16407 if (STRINGP (eoltype))
16408 {
16409 eol_str = SDATA (eoltype);
16410 eol_str_len = SBYTES (eoltype);
16411 }
16412 else if (INTEGERP (eoltype)
16413 && CHAR_VALID_P (XINT (eoltype), 0))
16414 {
16415 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16416 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16417 eol_str = tmp;
16418 }
16419 else
16420 {
16421 eol_str = invalid_eol_type;
16422 eol_str_len = sizeof (invalid_eol_type) - 1;
16423 }
16424 bcopy (eol_str, buf, eol_str_len);
16425 buf += eol_str_len;
16426 }
16427
16428 return buf;
16429 }
16430
16431 /* Return a string for the output of a mode line %-spec for window W,
16432 generated by character C. PRECISION >= 0 means don't return a
16433 string longer than that value. FIELD_WIDTH > 0 means pad the
16434 string returned with spaces to that value. Return 1 in *MULTIBYTE
16435 if the result is multibyte text.
16436
16437 Note we operate on the current buffer for most purposes,
16438 the exception being w->base_line_pos. */
16439
16440 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16441
16442 static char *
16443 decode_mode_spec (w, c, field_width, precision, multibyte)
16444 struct window *w;
16445 register int c;
16446 int field_width, precision;
16447 int *multibyte;
16448 {
16449 Lisp_Object obj;
16450 struct frame *f = XFRAME (WINDOW_FRAME (w));
16451 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16452 struct buffer *b = current_buffer;
16453
16454 obj = Qnil;
16455 *multibyte = 0;
16456
16457 switch (c)
16458 {
16459 case '*':
16460 if (!NILP (b->read_only))
16461 return "%";
16462 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16463 return "*";
16464 return "-";
16465
16466 case '+':
16467 /* This differs from %* only for a modified read-only buffer. */
16468 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16469 return "*";
16470 if (!NILP (b->read_only))
16471 return "%";
16472 return "-";
16473
16474 case '&':
16475 /* This differs from %* in ignoring read-only-ness. */
16476 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16477 return "*";
16478 return "-";
16479
16480 case '%':
16481 return "%";
16482
16483 case '[':
16484 {
16485 int i;
16486 char *p;
16487
16488 if (command_loop_level > 5)
16489 return "[[[... ";
16490 p = decode_mode_spec_buf;
16491 for (i = 0; i < command_loop_level; i++)
16492 *p++ = '[';
16493 *p = 0;
16494 return decode_mode_spec_buf;
16495 }
16496
16497 case ']':
16498 {
16499 int i;
16500 char *p;
16501
16502 if (command_loop_level > 5)
16503 return " ...]]]";
16504 p = decode_mode_spec_buf;
16505 for (i = 0; i < command_loop_level; i++)
16506 *p++ = ']';
16507 *p = 0;
16508 return decode_mode_spec_buf;
16509 }
16510
16511 case '-':
16512 {
16513 register int i;
16514
16515 /* Let lots_of_dashes be a string of infinite length. */
16516 if (!NILP (mode_line_string_list))
16517 return "--";
16518 if (field_width <= 0
16519 || field_width > sizeof (lots_of_dashes))
16520 {
16521 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16522 decode_mode_spec_buf[i] = '-';
16523 decode_mode_spec_buf[i] = '\0';
16524 return decode_mode_spec_buf;
16525 }
16526 else
16527 return lots_of_dashes;
16528 }
16529
16530 case 'b':
16531 obj = b->name;
16532 break;
16533
16534 case 'c':
16535 {
16536 int col = (int) current_column (); /* iftc */
16537 w->column_number_displayed = make_number (col);
16538 pint2str (decode_mode_spec_buf, field_width, col);
16539 return decode_mode_spec_buf;
16540 }
16541
16542 case 'F':
16543 /* %F displays the frame name. */
16544 if (!NILP (f->title))
16545 return (char *) SDATA (f->title);
16546 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16547 return (char *) SDATA (f->name);
16548 return "Emacs";
16549
16550 case 'f':
16551 obj = b->filename;
16552 break;
16553
16554 case 'i':
16555 {
16556 int size = ZV - BEGV;
16557 pint2str (decode_mode_spec_buf, field_width, size);
16558 return decode_mode_spec_buf;
16559 }
16560
16561 case 'I':
16562 {
16563 int size = ZV - BEGV;
16564 pint2hrstr (decode_mode_spec_buf, field_width, size);
16565 return decode_mode_spec_buf;
16566 }
16567
16568 case 'l':
16569 {
16570 int startpos = XMARKER (w->start)->charpos;
16571 int startpos_byte = marker_byte_position (w->start);
16572 int line, linepos, linepos_byte, topline;
16573 int nlines, junk;
16574 int height = WINDOW_TOTAL_LINES (w);
16575
16576 /* If we decided that this buffer isn't suitable for line numbers,
16577 don't forget that too fast. */
16578 if (EQ (w->base_line_pos, w->buffer))
16579 goto no_value;
16580 /* But do forget it, if the window shows a different buffer now. */
16581 else if (BUFFERP (w->base_line_pos))
16582 w->base_line_pos = Qnil;
16583
16584 /* If the buffer is very big, don't waste time. */
16585 if (INTEGERP (Vline_number_display_limit)
16586 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16587 {
16588 w->base_line_pos = Qnil;
16589 w->base_line_number = Qnil;
16590 goto no_value;
16591 }
16592
16593 if (!NILP (w->base_line_number)
16594 && !NILP (w->base_line_pos)
16595 && XFASTINT (w->base_line_pos) <= startpos)
16596 {
16597 line = XFASTINT (w->base_line_number);
16598 linepos = XFASTINT (w->base_line_pos);
16599 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16600 }
16601 else
16602 {
16603 line = 1;
16604 linepos = BUF_BEGV (b);
16605 linepos_byte = BUF_BEGV_BYTE (b);
16606 }
16607
16608 /* Count lines from base line to window start position. */
16609 nlines = display_count_lines (linepos, linepos_byte,
16610 startpos_byte,
16611 startpos, &junk);
16612
16613 topline = nlines + line;
16614
16615 /* Determine a new base line, if the old one is too close
16616 or too far away, or if we did not have one.
16617 "Too close" means it's plausible a scroll-down would
16618 go back past it. */
16619 if (startpos == BUF_BEGV (b))
16620 {
16621 w->base_line_number = make_number (topline);
16622 w->base_line_pos = make_number (BUF_BEGV (b));
16623 }
16624 else if (nlines < height + 25 || nlines > height * 3 + 50
16625 || linepos == BUF_BEGV (b))
16626 {
16627 int limit = BUF_BEGV (b);
16628 int limit_byte = BUF_BEGV_BYTE (b);
16629 int position;
16630 int distance = (height * 2 + 30) * line_number_display_limit_width;
16631
16632 if (startpos - distance > limit)
16633 {
16634 limit = startpos - distance;
16635 limit_byte = CHAR_TO_BYTE (limit);
16636 }
16637
16638 nlines = display_count_lines (startpos, startpos_byte,
16639 limit_byte,
16640 - (height * 2 + 30),
16641 &position);
16642 /* If we couldn't find the lines we wanted within
16643 line_number_display_limit_width chars per line,
16644 give up on line numbers for this window. */
16645 if (position == limit_byte && limit == startpos - distance)
16646 {
16647 w->base_line_pos = w->buffer;
16648 w->base_line_number = Qnil;
16649 goto no_value;
16650 }
16651
16652 w->base_line_number = make_number (topline - nlines);
16653 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16654 }
16655
16656 /* Now count lines from the start pos to point. */
16657 nlines = display_count_lines (startpos, startpos_byte,
16658 PT_BYTE, PT, &junk);
16659
16660 /* Record that we did display the line number. */
16661 line_number_displayed = 1;
16662
16663 /* Make the string to show. */
16664 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16665 return decode_mode_spec_buf;
16666 no_value:
16667 {
16668 char* p = decode_mode_spec_buf;
16669 int pad = field_width - 2;
16670 while (pad-- > 0)
16671 *p++ = ' ';
16672 *p++ = '?';
16673 *p++ = '?';
16674 *p = '\0';
16675 return decode_mode_spec_buf;
16676 }
16677 }
16678 break;
16679
16680 case 'm':
16681 obj = b->mode_name;
16682 break;
16683
16684 case 'n':
16685 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16686 return " Narrow";
16687 break;
16688
16689 case 'p':
16690 {
16691 int pos = marker_position (w->start);
16692 int total = BUF_ZV (b) - BUF_BEGV (b);
16693
16694 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16695 {
16696 if (pos <= BUF_BEGV (b))
16697 return "All";
16698 else
16699 return "Bottom";
16700 }
16701 else if (pos <= BUF_BEGV (b))
16702 return "Top";
16703 else
16704 {
16705 if (total > 1000000)
16706 /* Do it differently for a large value, to avoid overflow. */
16707 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16708 else
16709 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16710 /* We can't normally display a 3-digit number,
16711 so get us a 2-digit number that is close. */
16712 if (total == 100)
16713 total = 99;
16714 sprintf (decode_mode_spec_buf, "%2d%%", total);
16715 return decode_mode_spec_buf;
16716 }
16717 }
16718
16719 /* Display percentage of size above the bottom of the screen. */
16720 case 'P':
16721 {
16722 int toppos = marker_position (w->start);
16723 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16724 int total = BUF_ZV (b) - BUF_BEGV (b);
16725
16726 if (botpos >= BUF_ZV (b))
16727 {
16728 if (toppos <= BUF_BEGV (b))
16729 return "All";
16730 else
16731 return "Bottom";
16732 }
16733 else
16734 {
16735 if (total > 1000000)
16736 /* Do it differently for a large value, to avoid overflow. */
16737 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16738 else
16739 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16740 /* We can't normally display a 3-digit number,
16741 so get us a 2-digit number that is close. */
16742 if (total == 100)
16743 total = 99;
16744 if (toppos <= BUF_BEGV (b))
16745 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16746 else
16747 sprintf (decode_mode_spec_buf, "%2d%%", total);
16748 return decode_mode_spec_buf;
16749 }
16750 }
16751
16752 case 's':
16753 /* status of process */
16754 obj = Fget_buffer_process (Fcurrent_buffer ());
16755 if (NILP (obj))
16756 return "no process";
16757 #ifdef subprocesses
16758 obj = Fsymbol_name (Fprocess_status (obj));
16759 #endif
16760 break;
16761
16762 case 't': /* indicate TEXT or BINARY */
16763 #ifdef MODE_LINE_BINARY_TEXT
16764 return MODE_LINE_BINARY_TEXT (b);
16765 #else
16766 return "T";
16767 #endif
16768
16769 case 'z':
16770 /* coding-system (not including end-of-line format) */
16771 case 'Z':
16772 /* coding-system (including end-of-line type) */
16773 {
16774 int eol_flag = (c == 'Z');
16775 char *p = decode_mode_spec_buf;
16776
16777 if (! FRAME_WINDOW_P (f))
16778 {
16779 /* No need to mention EOL here--the terminal never needs
16780 to do EOL conversion. */
16781 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16782 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16783 }
16784 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16785 p, eol_flag);
16786
16787 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16788 #ifdef subprocesses
16789 obj = Fget_buffer_process (Fcurrent_buffer ());
16790 if (PROCESSP (obj))
16791 {
16792 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16793 p, eol_flag);
16794 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16795 p, eol_flag);
16796 }
16797 #endif /* subprocesses */
16798 #endif /* 0 */
16799 *p = 0;
16800 return decode_mode_spec_buf;
16801 }
16802 }
16803
16804 if (STRINGP (obj))
16805 {
16806 *multibyte = STRING_MULTIBYTE (obj);
16807 return (char *) SDATA (obj);
16808 }
16809 else
16810 return "";
16811 }
16812
16813
16814 /* Count up to COUNT lines starting from START / START_BYTE.
16815 But don't go beyond LIMIT_BYTE.
16816 Return the number of lines thus found (always nonnegative).
16817
16818 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16819
16820 static int
16821 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16822 int start, start_byte, limit_byte, count;
16823 int *byte_pos_ptr;
16824 {
16825 register unsigned char *cursor;
16826 unsigned char *base;
16827
16828 register int ceiling;
16829 register unsigned char *ceiling_addr;
16830 int orig_count = count;
16831
16832 /* If we are not in selective display mode,
16833 check only for newlines. */
16834 int selective_display = (!NILP (current_buffer->selective_display)
16835 && !INTEGERP (current_buffer->selective_display));
16836
16837 if (count > 0)
16838 {
16839 while (start_byte < limit_byte)
16840 {
16841 ceiling = BUFFER_CEILING_OF (start_byte);
16842 ceiling = min (limit_byte - 1, ceiling);
16843 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16844 base = (cursor = BYTE_POS_ADDR (start_byte));
16845 while (1)
16846 {
16847 if (selective_display)
16848 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16849 ;
16850 else
16851 while (*cursor != '\n' && ++cursor != ceiling_addr)
16852 ;
16853
16854 if (cursor != ceiling_addr)
16855 {
16856 if (--count == 0)
16857 {
16858 start_byte += cursor - base + 1;
16859 *byte_pos_ptr = start_byte;
16860 return orig_count;
16861 }
16862 else
16863 if (++cursor == ceiling_addr)
16864 break;
16865 }
16866 else
16867 break;
16868 }
16869 start_byte += cursor - base;
16870 }
16871 }
16872 else
16873 {
16874 while (start_byte > limit_byte)
16875 {
16876 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16877 ceiling = max (limit_byte, ceiling);
16878 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16879 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16880 while (1)
16881 {
16882 if (selective_display)
16883 while (--cursor != ceiling_addr
16884 && *cursor != '\n' && *cursor != 015)
16885 ;
16886 else
16887 while (--cursor != ceiling_addr && *cursor != '\n')
16888 ;
16889
16890 if (cursor != ceiling_addr)
16891 {
16892 if (++count == 0)
16893 {
16894 start_byte += cursor - base + 1;
16895 *byte_pos_ptr = start_byte;
16896 /* When scanning backwards, we should
16897 not count the newline posterior to which we stop. */
16898 return - orig_count - 1;
16899 }
16900 }
16901 else
16902 break;
16903 }
16904 /* Here we add 1 to compensate for the last decrement
16905 of CURSOR, which took it past the valid range. */
16906 start_byte += cursor - base + 1;
16907 }
16908 }
16909
16910 *byte_pos_ptr = limit_byte;
16911
16912 if (count < 0)
16913 return - orig_count + count;
16914 return orig_count - count;
16915
16916 }
16917
16918
16919 \f
16920 /***********************************************************************
16921 Displaying strings
16922 ***********************************************************************/
16923
16924 /* Display a NUL-terminated string, starting with index START.
16925
16926 If STRING is non-null, display that C string. Otherwise, the Lisp
16927 string LISP_STRING is displayed.
16928
16929 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16930 FACE_STRING. Display STRING or LISP_STRING with the face at
16931 FACE_STRING_POS in FACE_STRING:
16932
16933 Display the string in the environment given by IT, but use the
16934 standard display table, temporarily.
16935
16936 FIELD_WIDTH is the minimum number of output glyphs to produce.
16937 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16938 with spaces. If STRING has more characters, more than FIELD_WIDTH
16939 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16940
16941 PRECISION is the maximum number of characters to output from
16942 STRING. PRECISION < 0 means don't truncate the string.
16943
16944 This is roughly equivalent to printf format specifiers:
16945
16946 FIELD_WIDTH PRECISION PRINTF
16947 ----------------------------------------
16948 -1 -1 %s
16949 -1 10 %.10s
16950 10 -1 %10s
16951 20 10 %20.10s
16952
16953 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16954 display them, and < 0 means obey the current buffer's value of
16955 enable_multibyte_characters.
16956
16957 Value is the number of glyphs produced. */
16958
16959 static int
16960 display_string (string, lisp_string, face_string, face_string_pos,
16961 start, it, field_width, precision, max_x, multibyte)
16962 unsigned char *string;
16963 Lisp_Object lisp_string;
16964 Lisp_Object face_string;
16965 int face_string_pos;
16966 int start;
16967 struct it *it;
16968 int field_width, precision, max_x;
16969 int multibyte;
16970 {
16971 int hpos_at_start = it->hpos;
16972 int saved_face_id = it->face_id;
16973 struct glyph_row *row = it->glyph_row;
16974
16975 /* Initialize the iterator IT for iteration over STRING beginning
16976 with index START. */
16977 reseat_to_string (it, string, lisp_string, start,
16978 precision, field_width, multibyte);
16979
16980 /* If displaying STRING, set up the face of the iterator
16981 from LISP_STRING, if that's given. */
16982 if (STRINGP (face_string))
16983 {
16984 int endptr;
16985 struct face *face;
16986
16987 it->face_id
16988 = face_at_string_position (it->w, face_string, face_string_pos,
16989 0, it->region_beg_charpos,
16990 it->region_end_charpos,
16991 &endptr, it->base_face_id, 0);
16992 face = FACE_FROM_ID (it->f, it->face_id);
16993 it->face_box_p = face->box != FACE_NO_BOX;
16994 }
16995
16996 /* Set max_x to the maximum allowed X position. Don't let it go
16997 beyond the right edge of the window. */
16998 if (max_x <= 0)
16999 max_x = it->last_visible_x;
17000 else
17001 max_x = min (max_x, it->last_visible_x);
17002
17003 /* Skip over display elements that are not visible. because IT->w is
17004 hscrolled. */
17005 if (it->current_x < it->first_visible_x)
17006 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17007 MOVE_TO_POS | MOVE_TO_X);
17008
17009 row->ascent = it->max_ascent;
17010 row->height = it->max_ascent + it->max_descent;
17011 row->phys_ascent = it->max_phys_ascent;
17012 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17013 row->extra_line_spacing = it->max_extra_line_spacing;
17014
17015 /* This condition is for the case that we are called with current_x
17016 past last_visible_x. */
17017 while (it->current_x < max_x)
17018 {
17019 int x_before, x, n_glyphs_before, i, nglyphs;
17020
17021 /* Get the next display element. */
17022 if (!get_next_display_element (it))
17023 break;
17024
17025 /* Produce glyphs. */
17026 x_before = it->current_x;
17027 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17028 PRODUCE_GLYPHS (it);
17029
17030 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17031 i = 0;
17032 x = x_before;
17033 while (i < nglyphs)
17034 {
17035 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17036
17037 if (!it->truncate_lines_p
17038 && x + glyph->pixel_width > max_x)
17039 {
17040 /* End of continued line or max_x reached. */
17041 if (CHAR_GLYPH_PADDING_P (*glyph))
17042 {
17043 /* A wide character is unbreakable. */
17044 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17045 it->current_x = x_before;
17046 }
17047 else
17048 {
17049 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17050 it->current_x = x;
17051 }
17052 break;
17053 }
17054 else if (x + glyph->pixel_width > it->first_visible_x)
17055 {
17056 /* Glyph is at least partially visible. */
17057 ++it->hpos;
17058 if (x < it->first_visible_x)
17059 it->glyph_row->x = x - it->first_visible_x;
17060 }
17061 else
17062 {
17063 /* Glyph is off the left margin of the display area.
17064 Should not happen. */
17065 abort ();
17066 }
17067
17068 row->ascent = max (row->ascent, it->max_ascent);
17069 row->height = max (row->height, it->max_ascent + it->max_descent);
17070 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17071 row->phys_height = max (row->phys_height,
17072 it->max_phys_ascent + it->max_phys_descent);
17073 row->extra_line_spacing = max (row->extra_line_spacing,
17074 it->max_extra_line_spacing);
17075 x += glyph->pixel_width;
17076 ++i;
17077 }
17078
17079 /* Stop if max_x reached. */
17080 if (i < nglyphs)
17081 break;
17082
17083 /* Stop at line ends. */
17084 if (ITERATOR_AT_END_OF_LINE_P (it))
17085 {
17086 it->continuation_lines_width = 0;
17087 break;
17088 }
17089
17090 set_iterator_to_next (it, 1);
17091
17092 /* Stop if truncating at the right edge. */
17093 if (it->truncate_lines_p
17094 && it->current_x >= it->last_visible_x)
17095 {
17096 /* Add truncation mark, but don't do it if the line is
17097 truncated at a padding space. */
17098 if (IT_CHARPOS (*it) < it->string_nchars)
17099 {
17100 if (!FRAME_WINDOW_P (it->f))
17101 {
17102 int i, n;
17103
17104 if (it->current_x > it->last_visible_x)
17105 {
17106 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17107 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17108 break;
17109 for (n = row->used[TEXT_AREA]; i < n; ++i)
17110 {
17111 row->used[TEXT_AREA] = i;
17112 produce_special_glyphs (it, IT_TRUNCATION);
17113 }
17114 }
17115 produce_special_glyphs (it, IT_TRUNCATION);
17116 }
17117 it->glyph_row->truncated_on_right_p = 1;
17118 }
17119 break;
17120 }
17121 }
17122
17123 /* Maybe insert a truncation at the left. */
17124 if (it->first_visible_x
17125 && IT_CHARPOS (*it) > 0)
17126 {
17127 if (!FRAME_WINDOW_P (it->f))
17128 insert_left_trunc_glyphs (it);
17129 it->glyph_row->truncated_on_left_p = 1;
17130 }
17131
17132 it->face_id = saved_face_id;
17133
17134 /* Value is number of columns displayed. */
17135 return it->hpos - hpos_at_start;
17136 }
17137
17138
17139 \f
17140 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17141 appears as an element of LIST or as the car of an element of LIST.
17142 If PROPVAL is a list, compare each element against LIST in that
17143 way, and return 1/2 if any element of PROPVAL is found in LIST.
17144 Otherwise return 0. This function cannot quit.
17145 The return value is 2 if the text is invisible but with an ellipsis
17146 and 1 if it's invisible and without an ellipsis. */
17147
17148 int
17149 invisible_p (propval, list)
17150 register Lisp_Object propval;
17151 Lisp_Object list;
17152 {
17153 register Lisp_Object tail, proptail;
17154
17155 for (tail = list; CONSP (tail); tail = XCDR (tail))
17156 {
17157 register Lisp_Object tem;
17158 tem = XCAR (tail);
17159 if (EQ (propval, tem))
17160 return 1;
17161 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17162 return NILP (XCDR (tem)) ? 1 : 2;
17163 }
17164
17165 if (CONSP (propval))
17166 {
17167 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17168 {
17169 Lisp_Object propelt;
17170 propelt = XCAR (proptail);
17171 for (tail = list; CONSP (tail); tail = XCDR (tail))
17172 {
17173 register Lisp_Object tem;
17174 tem = XCAR (tail);
17175 if (EQ (propelt, tem))
17176 return 1;
17177 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17178 return NILP (XCDR (tem)) ? 1 : 2;
17179 }
17180 }
17181 }
17182
17183 return 0;
17184 }
17185
17186 /* Calculate a width or height in pixels from a specification using
17187 the following elements:
17188
17189 SPEC ::=
17190 NUM - a (fractional) multiple of the default font width/height
17191 (NUM) - specifies exactly NUM pixels
17192 UNIT - a fixed number of pixels, see below.
17193 ELEMENT - size of a display element in pixels, see below.
17194 (NUM . SPEC) - equals NUM * SPEC
17195 (+ SPEC SPEC ...) - add pixel values
17196 (- SPEC SPEC ...) - subtract pixel values
17197 (- SPEC) - negate pixel value
17198
17199 NUM ::=
17200 INT or FLOAT - a number constant
17201 SYMBOL - use symbol's (buffer local) variable binding.
17202
17203 UNIT ::=
17204 in - pixels per inch *)
17205 mm - pixels per 1/1000 meter *)
17206 cm - pixels per 1/100 meter *)
17207 width - width of current font in pixels.
17208 height - height of current font in pixels.
17209
17210 *) using the ratio(s) defined in display-pixels-per-inch.
17211
17212 ELEMENT ::=
17213
17214 left-fringe - left fringe width in pixels
17215 right-fringe - right fringe width in pixels
17216
17217 left-margin - left margin width in pixels
17218 right-margin - right margin width in pixels
17219
17220 scroll-bar - scroll-bar area width in pixels
17221
17222 Examples:
17223
17224 Pixels corresponding to 5 inches:
17225 (5 . in)
17226
17227 Total width of non-text areas on left side of window (if scroll-bar is on left):
17228 '(space :width (+ left-fringe left-margin scroll-bar))
17229
17230 Align to first text column (in header line):
17231 '(space :align-to 0)
17232
17233 Align to middle of text area minus half the width of variable `my-image'
17234 containing a loaded image:
17235 '(space :align-to (0.5 . (- text my-image)))
17236
17237 Width of left margin minus width of 1 character in the default font:
17238 '(space :width (- left-margin 1))
17239
17240 Width of left margin minus width of 2 characters in the current font:
17241 '(space :width (- left-margin (2 . width)))
17242
17243 Center 1 character over left-margin (in header line):
17244 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17245
17246 Different ways to express width of left fringe plus left margin minus one pixel:
17247 '(space :width (- (+ left-fringe left-margin) (1)))
17248 '(space :width (+ left-fringe left-margin (- (1))))
17249 '(space :width (+ left-fringe left-margin (-1)))
17250
17251 */
17252
17253 #define NUMVAL(X) \
17254 ((INTEGERP (X) || FLOATP (X)) \
17255 ? XFLOATINT (X) \
17256 : - 1)
17257
17258 int
17259 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17260 double *res;
17261 struct it *it;
17262 Lisp_Object prop;
17263 void *font;
17264 int width_p, *align_to;
17265 {
17266 double pixels;
17267
17268 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17269 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17270
17271 if (NILP (prop))
17272 return OK_PIXELS (0);
17273
17274 if (SYMBOLP (prop))
17275 {
17276 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17277 {
17278 char *unit = SDATA (SYMBOL_NAME (prop));
17279
17280 if (unit[0] == 'i' && unit[1] == 'n')
17281 pixels = 1.0;
17282 else if (unit[0] == 'm' && unit[1] == 'm')
17283 pixels = 25.4;
17284 else if (unit[0] == 'c' && unit[1] == 'm')
17285 pixels = 2.54;
17286 else
17287 pixels = 0;
17288 if (pixels > 0)
17289 {
17290 double ppi;
17291 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17292 || (CONSP (Vdisplay_pixels_per_inch)
17293 && (ppi = (width_p
17294 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17295 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17296 ppi > 0)))
17297 return OK_PIXELS (ppi / pixels);
17298
17299 return 0;
17300 }
17301 }
17302
17303 #ifdef HAVE_WINDOW_SYSTEM
17304 if (EQ (prop, Qheight))
17305 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17306 if (EQ (prop, Qwidth))
17307 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17308 #else
17309 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17310 return OK_PIXELS (1);
17311 #endif
17312
17313 if (EQ (prop, Qtext))
17314 return OK_PIXELS (width_p
17315 ? window_box_width (it->w, TEXT_AREA)
17316 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17317
17318 if (align_to && *align_to < 0)
17319 {
17320 *res = 0;
17321 if (EQ (prop, Qleft))
17322 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17323 if (EQ (prop, Qright))
17324 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17325 if (EQ (prop, Qcenter))
17326 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17327 + window_box_width (it->w, TEXT_AREA) / 2);
17328 if (EQ (prop, Qleft_fringe))
17329 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17330 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17331 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17332 if (EQ (prop, Qright_fringe))
17333 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17334 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17335 : window_box_right_offset (it->w, TEXT_AREA));
17336 if (EQ (prop, Qleft_margin))
17337 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17338 if (EQ (prop, Qright_margin))
17339 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17340 if (EQ (prop, Qscroll_bar))
17341 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17342 ? 0
17343 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17344 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17345 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17346 : 0)));
17347 }
17348 else
17349 {
17350 if (EQ (prop, Qleft_fringe))
17351 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17352 if (EQ (prop, Qright_fringe))
17353 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17354 if (EQ (prop, Qleft_margin))
17355 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17356 if (EQ (prop, Qright_margin))
17357 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17358 if (EQ (prop, Qscroll_bar))
17359 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17360 }
17361
17362 prop = Fbuffer_local_value (prop, it->w->buffer);
17363 }
17364
17365 if (INTEGERP (prop) || FLOATP (prop))
17366 {
17367 int base_unit = (width_p
17368 ? FRAME_COLUMN_WIDTH (it->f)
17369 : FRAME_LINE_HEIGHT (it->f));
17370 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17371 }
17372
17373 if (CONSP (prop))
17374 {
17375 Lisp_Object car = XCAR (prop);
17376 Lisp_Object cdr = XCDR (prop);
17377
17378 if (SYMBOLP (car))
17379 {
17380 #ifdef HAVE_WINDOW_SYSTEM
17381 if (valid_image_p (prop))
17382 {
17383 int id = lookup_image (it->f, prop);
17384 struct image *img = IMAGE_FROM_ID (it->f, id);
17385
17386 return OK_PIXELS (width_p ? img->width : img->height);
17387 }
17388 #endif
17389 if (EQ (car, Qplus) || EQ (car, Qminus))
17390 {
17391 int first = 1;
17392 double px;
17393
17394 pixels = 0;
17395 while (CONSP (cdr))
17396 {
17397 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17398 font, width_p, align_to))
17399 return 0;
17400 if (first)
17401 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17402 else
17403 pixels += px;
17404 cdr = XCDR (cdr);
17405 }
17406 if (EQ (car, Qminus))
17407 pixels = -pixels;
17408 return OK_PIXELS (pixels);
17409 }
17410
17411 car = Fbuffer_local_value (car, it->w->buffer);
17412 }
17413
17414 if (INTEGERP (car) || FLOATP (car))
17415 {
17416 double fact;
17417 pixels = XFLOATINT (car);
17418 if (NILP (cdr))
17419 return OK_PIXELS (pixels);
17420 if (calc_pixel_width_or_height (&fact, it, cdr,
17421 font, width_p, align_to))
17422 return OK_PIXELS (pixels * fact);
17423 return 0;
17424 }
17425
17426 return 0;
17427 }
17428
17429 return 0;
17430 }
17431
17432 \f
17433 /***********************************************************************
17434 Glyph Display
17435 ***********************************************************************/
17436
17437 #ifdef HAVE_WINDOW_SYSTEM
17438
17439 #if GLYPH_DEBUG
17440
17441 void
17442 dump_glyph_string (s)
17443 struct glyph_string *s;
17444 {
17445 fprintf (stderr, "glyph string\n");
17446 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17447 s->x, s->y, s->width, s->height);
17448 fprintf (stderr, " ybase = %d\n", s->ybase);
17449 fprintf (stderr, " hl = %d\n", s->hl);
17450 fprintf (stderr, " left overhang = %d, right = %d\n",
17451 s->left_overhang, s->right_overhang);
17452 fprintf (stderr, " nchars = %d\n", s->nchars);
17453 fprintf (stderr, " extends to end of line = %d\n",
17454 s->extends_to_end_of_line_p);
17455 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17456 fprintf (stderr, " bg width = %d\n", s->background_width);
17457 }
17458
17459 #endif /* GLYPH_DEBUG */
17460
17461 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17462 of XChar2b structures for S; it can't be allocated in
17463 init_glyph_string because it must be allocated via `alloca'. W
17464 is the window on which S is drawn. ROW and AREA are the glyph row
17465 and area within the row from which S is constructed. START is the
17466 index of the first glyph structure covered by S. HL is a
17467 face-override for drawing S. */
17468
17469 #ifdef HAVE_NTGUI
17470 #define OPTIONAL_HDC(hdc) hdc,
17471 #define DECLARE_HDC(hdc) HDC hdc;
17472 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17473 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17474 #endif
17475
17476 #ifndef OPTIONAL_HDC
17477 #define OPTIONAL_HDC(hdc)
17478 #define DECLARE_HDC(hdc)
17479 #define ALLOCATE_HDC(hdc, f)
17480 #define RELEASE_HDC(hdc, f)
17481 #endif
17482
17483 static void
17484 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17485 struct glyph_string *s;
17486 DECLARE_HDC (hdc)
17487 XChar2b *char2b;
17488 struct window *w;
17489 struct glyph_row *row;
17490 enum glyph_row_area area;
17491 int start;
17492 enum draw_glyphs_face hl;
17493 {
17494 bzero (s, sizeof *s);
17495 s->w = w;
17496 s->f = XFRAME (w->frame);
17497 #ifdef HAVE_NTGUI
17498 s->hdc = hdc;
17499 #endif
17500 s->display = FRAME_X_DISPLAY (s->f);
17501 s->window = FRAME_X_WINDOW (s->f);
17502 s->char2b = char2b;
17503 s->hl = hl;
17504 s->row = row;
17505 s->area = area;
17506 s->first_glyph = row->glyphs[area] + start;
17507 s->height = row->height;
17508 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17509
17510 /* Display the internal border below the tool-bar window. */
17511 if (WINDOWP (s->f->tool_bar_window)
17512 && s->w == XWINDOW (s->f->tool_bar_window))
17513 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17514
17515 s->ybase = s->y + row->ascent;
17516 }
17517
17518
17519 /* Append the list of glyph strings with head H and tail T to the list
17520 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17521
17522 static INLINE void
17523 append_glyph_string_lists (head, tail, h, t)
17524 struct glyph_string **head, **tail;
17525 struct glyph_string *h, *t;
17526 {
17527 if (h)
17528 {
17529 if (*head)
17530 (*tail)->next = h;
17531 else
17532 *head = h;
17533 h->prev = *tail;
17534 *tail = t;
17535 }
17536 }
17537
17538
17539 /* Prepend the list of glyph strings with head H and tail T to the
17540 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17541 result. */
17542
17543 static INLINE void
17544 prepend_glyph_string_lists (head, tail, h, t)
17545 struct glyph_string **head, **tail;
17546 struct glyph_string *h, *t;
17547 {
17548 if (h)
17549 {
17550 if (*head)
17551 (*head)->prev = t;
17552 else
17553 *tail = t;
17554 t->next = *head;
17555 *head = h;
17556 }
17557 }
17558
17559
17560 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17561 Set *HEAD and *TAIL to the resulting list. */
17562
17563 static INLINE void
17564 append_glyph_string (head, tail, s)
17565 struct glyph_string **head, **tail;
17566 struct glyph_string *s;
17567 {
17568 s->next = s->prev = NULL;
17569 append_glyph_string_lists (head, tail, s, s);
17570 }
17571
17572
17573 /* Get face and two-byte form of character glyph GLYPH on frame F.
17574 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17575 a pointer to a realized face that is ready for display. */
17576
17577 static INLINE struct face *
17578 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17579 struct frame *f;
17580 struct glyph *glyph;
17581 XChar2b *char2b;
17582 int *two_byte_p;
17583 {
17584 struct face *face;
17585
17586 xassert (glyph->type == CHAR_GLYPH);
17587 face = FACE_FROM_ID (f, glyph->face_id);
17588
17589 if (two_byte_p)
17590 *two_byte_p = 0;
17591
17592 if (!glyph->multibyte_p)
17593 {
17594 /* Unibyte case. We don't have to encode, but we have to make
17595 sure to use a face suitable for unibyte. */
17596 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17597 }
17598 else if (glyph->u.ch < 128
17599 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17600 {
17601 /* Case of ASCII in a face known to fit ASCII. */
17602 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17603 }
17604 else
17605 {
17606 int c1, c2, charset;
17607
17608 /* Split characters into bytes. If c2 is -1 afterwards, C is
17609 really a one-byte character so that byte1 is zero. */
17610 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17611 if (c2 > 0)
17612 STORE_XCHAR2B (char2b, c1, c2);
17613 else
17614 STORE_XCHAR2B (char2b, 0, c1);
17615
17616 /* Maybe encode the character in *CHAR2B. */
17617 if (charset != CHARSET_ASCII)
17618 {
17619 struct font_info *font_info
17620 = FONT_INFO_FROM_ID (f, face->font_info_id);
17621 if (font_info)
17622 glyph->font_type
17623 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17624 }
17625 }
17626
17627 /* Make sure X resources of the face are allocated. */
17628 xassert (face != NULL);
17629 PREPARE_FACE_FOR_DISPLAY (f, face);
17630 return face;
17631 }
17632
17633
17634 /* Fill glyph string S with composition components specified by S->cmp.
17635
17636 FACES is an array of faces for all components of this composition.
17637 S->gidx is the index of the first component for S.
17638 OVERLAPS_P non-zero means S should draw the foreground only, and
17639 use its physical height for clipping.
17640
17641 Value is the index of a component not in S. */
17642
17643 static int
17644 fill_composite_glyph_string (s, faces, overlaps_p)
17645 struct glyph_string *s;
17646 struct face **faces;
17647 int overlaps_p;
17648 {
17649 int i;
17650
17651 xassert (s);
17652
17653 s->for_overlaps_p = overlaps_p;
17654
17655 s->face = faces[s->gidx];
17656 s->font = s->face->font;
17657 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17658
17659 /* For all glyphs of this composition, starting at the offset
17660 S->gidx, until we reach the end of the definition or encounter a
17661 glyph that requires the different face, add it to S. */
17662 ++s->nchars;
17663 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17664 ++s->nchars;
17665
17666 /* All glyph strings for the same composition has the same width,
17667 i.e. the width set for the first component of the composition. */
17668
17669 s->width = s->first_glyph->pixel_width;
17670
17671 /* If the specified font could not be loaded, use the frame's
17672 default font, but record the fact that we couldn't load it in
17673 the glyph string so that we can draw rectangles for the
17674 characters of the glyph string. */
17675 if (s->font == NULL)
17676 {
17677 s->font_not_found_p = 1;
17678 s->font = FRAME_FONT (s->f);
17679 }
17680
17681 /* Adjust base line for subscript/superscript text. */
17682 s->ybase += s->first_glyph->voffset;
17683
17684 xassert (s->face && s->face->gc);
17685
17686 /* This glyph string must always be drawn with 16-bit functions. */
17687 s->two_byte_p = 1;
17688
17689 return s->gidx + s->nchars;
17690 }
17691
17692
17693 /* Fill glyph string S from a sequence of character glyphs.
17694
17695 FACE_ID is the face id of the string. START is the index of the
17696 first glyph to consider, END is the index of the last + 1.
17697 OVERLAPS_P non-zero means S should draw the foreground only, and
17698 use its physical height for clipping.
17699
17700 Value is the index of the first glyph not in S. */
17701
17702 static int
17703 fill_glyph_string (s, face_id, start, end, overlaps_p)
17704 struct glyph_string *s;
17705 int face_id;
17706 int start, end, overlaps_p;
17707 {
17708 struct glyph *glyph, *last;
17709 int voffset;
17710 int glyph_not_available_p;
17711
17712 xassert (s->f == XFRAME (s->w->frame));
17713 xassert (s->nchars == 0);
17714 xassert (start >= 0 && end > start);
17715
17716 s->for_overlaps_p = overlaps_p,
17717 glyph = s->row->glyphs[s->area] + start;
17718 last = s->row->glyphs[s->area] + end;
17719 voffset = glyph->voffset;
17720
17721 glyph_not_available_p = glyph->glyph_not_available_p;
17722
17723 while (glyph < last
17724 && glyph->type == CHAR_GLYPH
17725 && glyph->voffset == voffset
17726 /* Same face id implies same font, nowadays. */
17727 && glyph->face_id == face_id
17728 && glyph->glyph_not_available_p == glyph_not_available_p)
17729 {
17730 int two_byte_p;
17731
17732 s->face = get_glyph_face_and_encoding (s->f, glyph,
17733 s->char2b + s->nchars,
17734 &two_byte_p);
17735 s->two_byte_p = two_byte_p;
17736 ++s->nchars;
17737 xassert (s->nchars <= end - start);
17738 s->width += glyph->pixel_width;
17739 ++glyph;
17740 }
17741
17742 s->font = s->face->font;
17743 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17744
17745 /* If the specified font could not be loaded, use the frame's font,
17746 but record the fact that we couldn't load it in
17747 S->font_not_found_p so that we can draw rectangles for the
17748 characters of the glyph string. */
17749 if (s->font == NULL || glyph_not_available_p)
17750 {
17751 s->font_not_found_p = 1;
17752 s->font = FRAME_FONT (s->f);
17753 }
17754
17755 /* Adjust base line for subscript/superscript text. */
17756 s->ybase += voffset;
17757
17758 xassert (s->face && s->face->gc);
17759 return glyph - s->row->glyphs[s->area];
17760 }
17761
17762
17763 /* Fill glyph string S from image glyph S->first_glyph. */
17764
17765 static void
17766 fill_image_glyph_string (s)
17767 struct glyph_string *s;
17768 {
17769 xassert (s->first_glyph->type == IMAGE_GLYPH);
17770 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17771 xassert (s->img);
17772 s->slice = s->first_glyph->slice;
17773 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17774 s->font = s->face->font;
17775 s->width = s->first_glyph->pixel_width;
17776
17777 /* Adjust base line for subscript/superscript text. */
17778 s->ybase += s->first_glyph->voffset;
17779 }
17780
17781
17782 /* Fill glyph string S from a sequence of stretch glyphs.
17783
17784 ROW is the glyph row in which the glyphs are found, AREA is the
17785 area within the row. START is the index of the first glyph to
17786 consider, END is the index of the last + 1.
17787
17788 Value is the index of the first glyph not in S. */
17789
17790 static int
17791 fill_stretch_glyph_string (s, row, area, start, end)
17792 struct glyph_string *s;
17793 struct glyph_row *row;
17794 enum glyph_row_area area;
17795 int start, end;
17796 {
17797 struct glyph *glyph, *last;
17798 int voffset, face_id;
17799
17800 xassert (s->first_glyph->type == STRETCH_GLYPH);
17801
17802 glyph = s->row->glyphs[s->area] + start;
17803 last = s->row->glyphs[s->area] + end;
17804 face_id = glyph->face_id;
17805 s->face = FACE_FROM_ID (s->f, face_id);
17806 s->font = s->face->font;
17807 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17808 s->width = glyph->pixel_width;
17809 voffset = glyph->voffset;
17810
17811 for (++glyph;
17812 (glyph < last
17813 && glyph->type == STRETCH_GLYPH
17814 && glyph->voffset == voffset
17815 && glyph->face_id == face_id);
17816 ++glyph)
17817 s->width += glyph->pixel_width;
17818
17819 /* Adjust base line for subscript/superscript text. */
17820 s->ybase += voffset;
17821
17822 /* The case that face->gc == 0 is handled when drawing the glyph
17823 string by calling PREPARE_FACE_FOR_DISPLAY. */
17824 xassert (s->face);
17825 return glyph - s->row->glyphs[s->area];
17826 }
17827
17828
17829 /* EXPORT for RIF:
17830 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17831 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17832 assumed to be zero. */
17833
17834 void
17835 x_get_glyph_overhangs (glyph, f, left, right)
17836 struct glyph *glyph;
17837 struct frame *f;
17838 int *left, *right;
17839 {
17840 *left = *right = 0;
17841
17842 if (glyph->type == CHAR_GLYPH)
17843 {
17844 XFontStruct *font;
17845 struct face *face;
17846 struct font_info *font_info;
17847 XChar2b char2b;
17848 XCharStruct *pcm;
17849
17850 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17851 font = face->font;
17852 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17853 if (font /* ++KFS: Should this be font_info ? */
17854 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17855 {
17856 if (pcm->rbearing > pcm->width)
17857 *right = pcm->rbearing - pcm->width;
17858 if (pcm->lbearing < 0)
17859 *left = -pcm->lbearing;
17860 }
17861 }
17862 }
17863
17864
17865 /* Return the index of the first glyph preceding glyph string S that
17866 is overwritten by S because of S's left overhang. Value is -1
17867 if no glyphs are overwritten. */
17868
17869 static int
17870 left_overwritten (s)
17871 struct glyph_string *s;
17872 {
17873 int k;
17874
17875 if (s->left_overhang)
17876 {
17877 int x = 0, i;
17878 struct glyph *glyphs = s->row->glyphs[s->area];
17879 int first = s->first_glyph - glyphs;
17880
17881 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17882 x -= glyphs[i].pixel_width;
17883
17884 k = i + 1;
17885 }
17886 else
17887 k = -1;
17888
17889 return k;
17890 }
17891
17892
17893 /* Return the index of the first glyph preceding glyph string S that
17894 is overwriting S because of its right overhang. Value is -1 if no
17895 glyph in front of S overwrites S. */
17896
17897 static int
17898 left_overwriting (s)
17899 struct glyph_string *s;
17900 {
17901 int i, k, x;
17902 struct glyph *glyphs = s->row->glyphs[s->area];
17903 int first = s->first_glyph - glyphs;
17904
17905 k = -1;
17906 x = 0;
17907 for (i = first - 1; i >= 0; --i)
17908 {
17909 int left, right;
17910 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17911 if (x + right > 0)
17912 k = i;
17913 x -= glyphs[i].pixel_width;
17914 }
17915
17916 return k;
17917 }
17918
17919
17920 /* Return the index of the last glyph following glyph string S that is
17921 not overwritten by S because of S's right overhang. Value is -1 if
17922 no such glyph is found. */
17923
17924 static int
17925 right_overwritten (s)
17926 struct glyph_string *s;
17927 {
17928 int k = -1;
17929
17930 if (s->right_overhang)
17931 {
17932 int x = 0, i;
17933 struct glyph *glyphs = s->row->glyphs[s->area];
17934 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17935 int end = s->row->used[s->area];
17936
17937 for (i = first; i < end && s->right_overhang > x; ++i)
17938 x += glyphs[i].pixel_width;
17939
17940 k = i;
17941 }
17942
17943 return k;
17944 }
17945
17946
17947 /* Return the index of the last glyph following glyph string S that
17948 overwrites S because of its left overhang. Value is negative
17949 if no such glyph is found. */
17950
17951 static int
17952 right_overwriting (s)
17953 struct glyph_string *s;
17954 {
17955 int i, k, x;
17956 int end = s->row->used[s->area];
17957 struct glyph *glyphs = s->row->glyphs[s->area];
17958 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17959
17960 k = -1;
17961 x = 0;
17962 for (i = first; i < end; ++i)
17963 {
17964 int left, right;
17965 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17966 if (x - left < 0)
17967 k = i;
17968 x += glyphs[i].pixel_width;
17969 }
17970
17971 return k;
17972 }
17973
17974
17975 /* Get face and two-byte form of character C in face FACE_ID on frame
17976 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17977 means we want to display multibyte text. DISPLAY_P non-zero means
17978 make sure that X resources for the face returned are allocated.
17979 Value is a pointer to a realized face that is ready for display if
17980 DISPLAY_P is non-zero. */
17981
17982 static INLINE struct face *
17983 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17984 struct frame *f;
17985 int c, face_id;
17986 XChar2b *char2b;
17987 int multibyte_p, display_p;
17988 {
17989 struct face *face = FACE_FROM_ID (f, face_id);
17990
17991 if (!multibyte_p)
17992 {
17993 /* Unibyte case. We don't have to encode, but we have to make
17994 sure to use a face suitable for unibyte. */
17995 STORE_XCHAR2B (char2b, 0, c);
17996 face_id = FACE_FOR_CHAR (f, face, c);
17997 face = FACE_FROM_ID (f, face_id);
17998 }
17999 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18000 {
18001 /* Case of ASCII in a face known to fit ASCII. */
18002 STORE_XCHAR2B (char2b, 0, c);
18003 }
18004 else
18005 {
18006 int c1, c2, charset;
18007
18008 /* Split characters into bytes. If c2 is -1 afterwards, C is
18009 really a one-byte character so that byte1 is zero. */
18010 SPLIT_CHAR (c, charset, c1, c2);
18011 if (c2 > 0)
18012 STORE_XCHAR2B (char2b, c1, c2);
18013 else
18014 STORE_XCHAR2B (char2b, 0, c1);
18015
18016 /* Maybe encode the character in *CHAR2B. */
18017 if (face->font != NULL)
18018 {
18019 struct font_info *font_info
18020 = FONT_INFO_FROM_ID (f, face->font_info_id);
18021 if (font_info)
18022 rif->encode_char (c, char2b, font_info, 0);
18023 }
18024 }
18025
18026 /* Make sure X resources of the face are allocated. */
18027 #ifdef HAVE_X_WINDOWS
18028 if (display_p)
18029 #endif
18030 {
18031 xassert (face != NULL);
18032 PREPARE_FACE_FOR_DISPLAY (f, face);
18033 }
18034
18035 return face;
18036 }
18037
18038
18039 /* Set background width of glyph string S. START is the index of the
18040 first glyph following S. LAST_X is the right-most x-position + 1
18041 in the drawing area. */
18042
18043 static INLINE void
18044 set_glyph_string_background_width (s, start, last_x)
18045 struct glyph_string *s;
18046 int start;
18047 int last_x;
18048 {
18049 /* If the face of this glyph string has to be drawn to the end of
18050 the drawing area, set S->extends_to_end_of_line_p. */
18051 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18052
18053 if (start == s->row->used[s->area]
18054 && s->area == TEXT_AREA
18055 && ((s->hl == DRAW_NORMAL_TEXT
18056 && (s->row->fill_line_p
18057 || s->face->background != default_face->background
18058 || s->face->stipple != default_face->stipple
18059 || s->row->mouse_face_p))
18060 || s->hl == DRAW_MOUSE_FACE
18061 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18062 && s->row->fill_line_p)))
18063 s->extends_to_end_of_line_p = 1;
18064
18065 /* If S extends its face to the end of the line, set its
18066 background_width to the distance to the right edge of the drawing
18067 area. */
18068 if (s->extends_to_end_of_line_p)
18069 s->background_width = last_x - s->x + 1;
18070 else
18071 s->background_width = s->width;
18072 }
18073
18074
18075 /* Compute overhangs and x-positions for glyph string S and its
18076 predecessors, or successors. X is the starting x-position for S.
18077 BACKWARD_P non-zero means process predecessors. */
18078
18079 static void
18080 compute_overhangs_and_x (s, x, backward_p)
18081 struct glyph_string *s;
18082 int x;
18083 int backward_p;
18084 {
18085 if (backward_p)
18086 {
18087 while (s)
18088 {
18089 if (rif->compute_glyph_string_overhangs)
18090 rif->compute_glyph_string_overhangs (s);
18091 x -= s->width;
18092 s->x = x;
18093 s = s->prev;
18094 }
18095 }
18096 else
18097 {
18098 while (s)
18099 {
18100 if (rif->compute_glyph_string_overhangs)
18101 rif->compute_glyph_string_overhangs (s);
18102 s->x = x;
18103 x += s->width;
18104 s = s->next;
18105 }
18106 }
18107 }
18108
18109
18110
18111 /* The following macros are only called from draw_glyphs below.
18112 They reference the following parameters of that function directly:
18113 `w', `row', `area', and `overlap_p'
18114 as well as the following local variables:
18115 `s', `f', and `hdc' (in W32) */
18116
18117 #ifdef HAVE_NTGUI
18118 /* On W32, silently add local `hdc' variable to argument list of
18119 init_glyph_string. */
18120 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18121 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18122 #else
18123 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18124 init_glyph_string (s, char2b, w, row, area, start, hl)
18125 #endif
18126
18127 /* Add a glyph string for a stretch glyph to the list of strings
18128 between HEAD and TAIL. START is the index of the stretch glyph in
18129 row area AREA of glyph row ROW. END is the index of the last glyph
18130 in that glyph row area. X is the current output position assigned
18131 to the new glyph string constructed. HL overrides that face of the
18132 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18133 is the right-most x-position of the drawing area. */
18134
18135 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18136 and below -- keep them on one line. */
18137 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18138 do \
18139 { \
18140 s = (struct glyph_string *) alloca (sizeof *s); \
18141 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18142 START = fill_stretch_glyph_string (s, row, area, START, END); \
18143 append_glyph_string (&HEAD, &TAIL, s); \
18144 s->x = (X); \
18145 } \
18146 while (0)
18147
18148
18149 /* Add a glyph string for an image glyph to the list of strings
18150 between HEAD and TAIL. START is the index of the image glyph in
18151 row area AREA of glyph row ROW. END is the index of the last glyph
18152 in that glyph row area. X is the current output position assigned
18153 to the new glyph string constructed. HL overrides that face of the
18154 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18155 is the right-most x-position of the drawing area. */
18156
18157 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18158 do \
18159 { \
18160 s = (struct glyph_string *) alloca (sizeof *s); \
18161 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18162 fill_image_glyph_string (s); \
18163 append_glyph_string (&HEAD, &TAIL, s); \
18164 ++START; \
18165 s->x = (X); \
18166 } \
18167 while (0)
18168
18169
18170 /* Add a glyph string for a sequence of character glyphs to the list
18171 of strings between HEAD and TAIL. START is the index of the first
18172 glyph in row area AREA of glyph row ROW that is part of the new
18173 glyph string. END is the index of the last glyph in that glyph row
18174 area. X is the current output position assigned to the new glyph
18175 string constructed. HL overrides that face of the glyph; e.g. it
18176 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18177 right-most x-position of the drawing area. */
18178
18179 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18180 do \
18181 { \
18182 int c, face_id; \
18183 XChar2b *char2b; \
18184 \
18185 c = (row)->glyphs[area][START].u.ch; \
18186 face_id = (row)->glyphs[area][START].face_id; \
18187 \
18188 s = (struct glyph_string *) alloca (sizeof *s); \
18189 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18190 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18191 append_glyph_string (&HEAD, &TAIL, s); \
18192 s->x = (X); \
18193 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18194 } \
18195 while (0)
18196
18197
18198 /* Add a glyph string for a composite sequence to the list of strings
18199 between HEAD and TAIL. START is the index of the first glyph in
18200 row area AREA of glyph row ROW that is part of the new glyph
18201 string. END is the index of the last glyph in that glyph row area.
18202 X is the current output position assigned to the new glyph string
18203 constructed. HL overrides that face of the glyph; e.g. it is
18204 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18205 x-position of the drawing area. */
18206
18207 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18208 do { \
18209 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18210 int face_id = (row)->glyphs[area][START].face_id; \
18211 struct face *base_face = FACE_FROM_ID (f, face_id); \
18212 struct composition *cmp = composition_table[cmp_id]; \
18213 int glyph_len = cmp->glyph_len; \
18214 XChar2b *char2b; \
18215 struct face **faces; \
18216 struct glyph_string *first_s = NULL; \
18217 int n; \
18218 \
18219 base_face = base_face->ascii_face; \
18220 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18221 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18222 /* At first, fill in `char2b' and `faces'. */ \
18223 for (n = 0; n < glyph_len; n++) \
18224 { \
18225 int c = COMPOSITION_GLYPH (cmp, n); \
18226 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18227 faces[n] = FACE_FROM_ID (f, this_face_id); \
18228 get_char_face_and_encoding (f, c, this_face_id, \
18229 char2b + n, 1, 1); \
18230 } \
18231 \
18232 /* Make glyph_strings for each glyph sequence that is drawable by \
18233 the same face, and append them to HEAD/TAIL. */ \
18234 for (n = 0; n < cmp->glyph_len;) \
18235 { \
18236 s = (struct glyph_string *) alloca (sizeof *s); \
18237 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18238 append_glyph_string (&(HEAD), &(TAIL), s); \
18239 s->cmp = cmp; \
18240 s->gidx = n; \
18241 s->x = (X); \
18242 \
18243 if (n == 0) \
18244 first_s = s; \
18245 \
18246 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18247 } \
18248 \
18249 ++START; \
18250 s = first_s; \
18251 } while (0)
18252
18253
18254 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18255 of AREA of glyph row ROW on window W between indices START and END.
18256 HL overrides the face for drawing glyph strings, e.g. it is
18257 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18258 x-positions of the drawing area.
18259
18260 This is an ugly monster macro construct because we must use alloca
18261 to allocate glyph strings (because draw_glyphs can be called
18262 asynchronously). */
18263
18264 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18265 do \
18266 { \
18267 HEAD = TAIL = NULL; \
18268 while (START < END) \
18269 { \
18270 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18271 switch (first_glyph->type) \
18272 { \
18273 case CHAR_GLYPH: \
18274 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18275 HL, X, LAST_X); \
18276 break; \
18277 \
18278 case COMPOSITE_GLYPH: \
18279 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18280 HL, X, LAST_X); \
18281 break; \
18282 \
18283 case STRETCH_GLYPH: \
18284 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18285 HL, X, LAST_X); \
18286 break; \
18287 \
18288 case IMAGE_GLYPH: \
18289 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18290 HL, X, LAST_X); \
18291 break; \
18292 \
18293 default: \
18294 abort (); \
18295 } \
18296 \
18297 set_glyph_string_background_width (s, START, LAST_X); \
18298 (X) += s->width; \
18299 } \
18300 } \
18301 while (0)
18302
18303
18304 /* Draw glyphs between START and END in AREA of ROW on window W,
18305 starting at x-position X. X is relative to AREA in W. HL is a
18306 face-override with the following meaning:
18307
18308 DRAW_NORMAL_TEXT draw normally
18309 DRAW_CURSOR draw in cursor face
18310 DRAW_MOUSE_FACE draw in mouse face.
18311 DRAW_INVERSE_VIDEO draw in mode line face
18312 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18313 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18314
18315 If OVERLAPS_P is non-zero, draw only the foreground of characters
18316 and clip to the physical height of ROW.
18317
18318 Value is the x-position reached, relative to AREA of W. */
18319
18320 static int
18321 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18322 struct window *w;
18323 int x;
18324 struct glyph_row *row;
18325 enum glyph_row_area area;
18326 int start, end;
18327 enum draw_glyphs_face hl;
18328 int overlaps_p;
18329 {
18330 struct glyph_string *head, *tail;
18331 struct glyph_string *s;
18332 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18333 int last_x, area_width;
18334 int x_reached;
18335 int i, j;
18336 struct frame *f = XFRAME (WINDOW_FRAME (w));
18337 DECLARE_HDC (hdc);
18338
18339 ALLOCATE_HDC (hdc, f);
18340
18341 /* Let's rather be paranoid than getting a SEGV. */
18342 end = min (end, row->used[area]);
18343 start = max (0, start);
18344 start = min (end, start);
18345
18346 /* Translate X to frame coordinates. Set last_x to the right
18347 end of the drawing area. */
18348 if (row->full_width_p)
18349 {
18350 /* X is relative to the left edge of W, without scroll bars
18351 or fringes. */
18352 x += WINDOW_LEFT_EDGE_X (w);
18353 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18354 }
18355 else
18356 {
18357 int area_left = window_box_left (w, area);
18358 x += area_left;
18359 area_width = window_box_width (w, area);
18360 last_x = area_left + area_width;
18361 }
18362
18363 /* Build a doubly-linked list of glyph_string structures between
18364 head and tail from what we have to draw. Note that the macro
18365 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18366 the reason we use a separate variable `i'. */
18367 i = start;
18368 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18369 if (tail)
18370 x_reached = tail->x + tail->background_width;
18371 else
18372 x_reached = x;
18373
18374 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18375 the row, redraw some glyphs in front or following the glyph
18376 strings built above. */
18377 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18378 {
18379 int dummy_x = 0;
18380 struct glyph_string *h, *t;
18381
18382 /* Compute overhangs for all glyph strings. */
18383 if (rif->compute_glyph_string_overhangs)
18384 for (s = head; s; s = s->next)
18385 rif->compute_glyph_string_overhangs (s);
18386
18387 /* Prepend glyph strings for glyphs in front of the first glyph
18388 string that are overwritten because of the first glyph
18389 string's left overhang. The background of all strings
18390 prepended must be drawn because the first glyph string
18391 draws over it. */
18392 i = left_overwritten (head);
18393 if (i >= 0)
18394 {
18395 j = i;
18396 BUILD_GLYPH_STRINGS (j, start, h, t,
18397 DRAW_NORMAL_TEXT, dummy_x, last_x);
18398 start = i;
18399 compute_overhangs_and_x (t, head->x, 1);
18400 prepend_glyph_string_lists (&head, &tail, h, t);
18401 clip_head = head;
18402 }
18403
18404 /* Prepend glyph strings for glyphs in front of the first glyph
18405 string that overwrite that glyph string because of their
18406 right overhang. For these strings, only the foreground must
18407 be drawn, because it draws over the glyph string at `head'.
18408 The background must not be drawn because this would overwrite
18409 right overhangs of preceding glyphs for which no glyph
18410 strings exist. */
18411 i = left_overwriting (head);
18412 if (i >= 0)
18413 {
18414 clip_head = head;
18415 BUILD_GLYPH_STRINGS (i, start, h, t,
18416 DRAW_NORMAL_TEXT, dummy_x, last_x);
18417 for (s = h; s; s = s->next)
18418 s->background_filled_p = 1;
18419 compute_overhangs_and_x (t, head->x, 1);
18420 prepend_glyph_string_lists (&head, &tail, h, t);
18421 }
18422
18423 /* Append glyphs strings for glyphs following the last glyph
18424 string tail that are overwritten by tail. The background of
18425 these strings has to be drawn because tail's foreground draws
18426 over it. */
18427 i = right_overwritten (tail);
18428 if (i >= 0)
18429 {
18430 BUILD_GLYPH_STRINGS (end, i, h, t,
18431 DRAW_NORMAL_TEXT, x, last_x);
18432 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18433 append_glyph_string_lists (&head, &tail, h, t);
18434 clip_tail = tail;
18435 }
18436
18437 /* Append glyph strings for glyphs following the last glyph
18438 string tail that overwrite tail. The foreground of such
18439 glyphs has to be drawn because it writes into the background
18440 of tail. The background must not be drawn because it could
18441 paint over the foreground of following glyphs. */
18442 i = right_overwriting (tail);
18443 if (i >= 0)
18444 {
18445 clip_tail = tail;
18446 BUILD_GLYPH_STRINGS (end, i, h, t,
18447 DRAW_NORMAL_TEXT, x, last_x);
18448 for (s = h; s; s = s->next)
18449 s->background_filled_p = 1;
18450 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18451 append_glyph_string_lists (&head, &tail, h, t);
18452 }
18453 if (clip_head || clip_tail)
18454 for (s = head; s; s = s->next)
18455 {
18456 s->clip_head = clip_head;
18457 s->clip_tail = clip_tail;
18458 }
18459 }
18460
18461 /* Draw all strings. */
18462 for (s = head; s; s = s->next)
18463 rif->draw_glyph_string (s);
18464
18465 if (area == TEXT_AREA
18466 && !row->full_width_p
18467 /* When drawing overlapping rows, only the glyph strings'
18468 foreground is drawn, which doesn't erase a cursor
18469 completely. */
18470 && !overlaps_p)
18471 {
18472 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18473 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18474 : (tail ? tail->x + tail->background_width : x));
18475
18476 int text_left = window_box_left (w, TEXT_AREA);
18477 x0 -= text_left;
18478 x1 -= text_left;
18479
18480 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18481 row->y, MATRIX_ROW_BOTTOM_Y (row));
18482 }
18483
18484 /* Value is the x-position up to which drawn, relative to AREA of W.
18485 This doesn't include parts drawn because of overhangs. */
18486 if (row->full_width_p)
18487 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18488 else
18489 x_reached -= window_box_left (w, area);
18490
18491 RELEASE_HDC (hdc, f);
18492
18493 return x_reached;
18494 }
18495
18496 /* Expand row matrix if too narrow. Don't expand if area
18497 is not present. */
18498
18499 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18500 { \
18501 if (!fonts_changed_p \
18502 && (it->glyph_row->glyphs[area] \
18503 < it->glyph_row->glyphs[area + 1])) \
18504 { \
18505 it->w->ncols_scale_factor++; \
18506 fonts_changed_p = 1; \
18507 } \
18508 }
18509
18510 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18511 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18512
18513 static INLINE void
18514 append_glyph (it)
18515 struct it *it;
18516 {
18517 struct glyph *glyph;
18518 enum glyph_row_area area = it->area;
18519
18520 xassert (it->glyph_row);
18521 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18522
18523 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18524 if (glyph < it->glyph_row->glyphs[area + 1])
18525 {
18526 glyph->charpos = CHARPOS (it->position);
18527 glyph->object = it->object;
18528 glyph->pixel_width = it->pixel_width;
18529 glyph->ascent = it->ascent;
18530 glyph->descent = it->descent;
18531 glyph->voffset = it->voffset;
18532 glyph->type = CHAR_GLYPH;
18533 glyph->multibyte_p = it->multibyte_p;
18534 glyph->left_box_line_p = it->start_of_box_run_p;
18535 glyph->right_box_line_p = it->end_of_box_run_p;
18536 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18537 || it->phys_descent > it->descent);
18538 glyph->padding_p = 0;
18539 glyph->glyph_not_available_p = it->glyph_not_available_p;
18540 glyph->face_id = it->face_id;
18541 glyph->u.ch = it->char_to_display;
18542 glyph->slice = null_glyph_slice;
18543 glyph->font_type = FONT_TYPE_UNKNOWN;
18544 ++it->glyph_row->used[area];
18545 }
18546 else
18547 IT_EXPAND_MATRIX_WIDTH (it, area);
18548 }
18549
18550 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18551 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18552
18553 static INLINE void
18554 append_composite_glyph (it)
18555 struct it *it;
18556 {
18557 struct glyph *glyph;
18558 enum glyph_row_area area = it->area;
18559
18560 xassert (it->glyph_row);
18561
18562 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18563 if (glyph < it->glyph_row->glyphs[area + 1])
18564 {
18565 glyph->charpos = CHARPOS (it->position);
18566 glyph->object = it->object;
18567 glyph->pixel_width = it->pixel_width;
18568 glyph->ascent = it->ascent;
18569 glyph->descent = it->descent;
18570 glyph->voffset = it->voffset;
18571 glyph->type = COMPOSITE_GLYPH;
18572 glyph->multibyte_p = it->multibyte_p;
18573 glyph->left_box_line_p = it->start_of_box_run_p;
18574 glyph->right_box_line_p = it->end_of_box_run_p;
18575 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18576 || it->phys_descent > it->descent);
18577 glyph->padding_p = 0;
18578 glyph->glyph_not_available_p = 0;
18579 glyph->face_id = it->face_id;
18580 glyph->u.cmp_id = it->cmp_id;
18581 glyph->slice = null_glyph_slice;
18582 glyph->font_type = FONT_TYPE_UNKNOWN;
18583 ++it->glyph_row->used[area];
18584 }
18585 else
18586 IT_EXPAND_MATRIX_WIDTH (it, area);
18587 }
18588
18589
18590 /* Change IT->ascent and IT->height according to the setting of
18591 IT->voffset. */
18592
18593 static INLINE void
18594 take_vertical_position_into_account (it)
18595 struct it *it;
18596 {
18597 if (it->voffset)
18598 {
18599 if (it->voffset < 0)
18600 /* Increase the ascent so that we can display the text higher
18601 in the line. */
18602 it->ascent -= it->voffset;
18603 else
18604 /* Increase the descent so that we can display the text lower
18605 in the line. */
18606 it->descent += it->voffset;
18607 }
18608 }
18609
18610
18611 /* Produce glyphs/get display metrics for the image IT is loaded with.
18612 See the description of struct display_iterator in dispextern.h for
18613 an overview of struct display_iterator. */
18614
18615 static void
18616 produce_image_glyph (it)
18617 struct it *it;
18618 {
18619 struct image *img;
18620 struct face *face;
18621 int glyph_ascent;
18622 struct glyph_slice slice;
18623
18624 xassert (it->what == IT_IMAGE);
18625
18626 face = FACE_FROM_ID (it->f, it->face_id);
18627 xassert (face);
18628 /* Make sure X resources of the face is loaded. */
18629 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18630
18631 if (it->image_id < 0)
18632 {
18633 /* Fringe bitmap. */
18634 it->ascent = it->phys_ascent = 0;
18635 it->descent = it->phys_descent = 0;
18636 it->pixel_width = 0;
18637 it->nglyphs = 0;
18638 return;
18639 }
18640
18641 img = IMAGE_FROM_ID (it->f, it->image_id);
18642 xassert (img);
18643 /* Make sure X resources of the image is loaded. */
18644 prepare_image_for_display (it->f, img);
18645
18646 slice.x = slice.y = 0;
18647 slice.width = img->width;
18648 slice.height = img->height;
18649
18650 if (INTEGERP (it->slice.x))
18651 slice.x = XINT (it->slice.x);
18652 else if (FLOATP (it->slice.x))
18653 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18654
18655 if (INTEGERP (it->slice.y))
18656 slice.y = XINT (it->slice.y);
18657 else if (FLOATP (it->slice.y))
18658 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18659
18660 if (INTEGERP (it->slice.width))
18661 slice.width = XINT (it->slice.width);
18662 else if (FLOATP (it->slice.width))
18663 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18664
18665 if (INTEGERP (it->slice.height))
18666 slice.height = XINT (it->slice.height);
18667 else if (FLOATP (it->slice.height))
18668 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18669
18670 if (slice.x >= img->width)
18671 slice.x = img->width;
18672 if (slice.y >= img->height)
18673 slice.y = img->height;
18674 if (slice.x + slice.width >= img->width)
18675 slice.width = img->width - slice.x;
18676 if (slice.y + slice.height > img->height)
18677 slice.height = img->height - slice.y;
18678
18679 if (slice.width == 0 || slice.height == 0)
18680 return;
18681
18682 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18683
18684 it->descent = slice.height - glyph_ascent;
18685 if (slice.y == 0)
18686 it->descent += img->vmargin;
18687 if (slice.y + slice.height == img->height)
18688 it->descent += img->vmargin;
18689 it->phys_descent = it->descent;
18690
18691 it->pixel_width = slice.width;
18692 if (slice.x == 0)
18693 it->pixel_width += img->hmargin;
18694 if (slice.x + slice.width == img->width)
18695 it->pixel_width += img->hmargin;
18696
18697 /* It's quite possible for images to have an ascent greater than
18698 their height, so don't get confused in that case. */
18699 if (it->descent < 0)
18700 it->descent = 0;
18701
18702 #if 0 /* this breaks image tiling */
18703 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18704 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18705 if (face_ascent > it->ascent)
18706 it->ascent = it->phys_ascent = face_ascent;
18707 #endif
18708
18709 it->nglyphs = 1;
18710
18711 if (face->box != FACE_NO_BOX)
18712 {
18713 if (face->box_line_width > 0)
18714 {
18715 if (slice.y == 0)
18716 it->ascent += face->box_line_width;
18717 if (slice.y + slice.height == img->height)
18718 it->descent += face->box_line_width;
18719 }
18720
18721 if (it->start_of_box_run_p && slice.x == 0)
18722 it->pixel_width += abs (face->box_line_width);
18723 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18724 it->pixel_width += abs (face->box_line_width);
18725 }
18726
18727 take_vertical_position_into_account (it);
18728
18729 if (it->glyph_row)
18730 {
18731 struct glyph *glyph;
18732 enum glyph_row_area area = it->area;
18733
18734 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18735 if (glyph < it->glyph_row->glyphs[area + 1])
18736 {
18737 glyph->charpos = CHARPOS (it->position);
18738 glyph->object = it->object;
18739 glyph->pixel_width = it->pixel_width;
18740 glyph->ascent = glyph_ascent;
18741 glyph->descent = it->descent;
18742 glyph->voffset = it->voffset;
18743 glyph->type = IMAGE_GLYPH;
18744 glyph->multibyte_p = it->multibyte_p;
18745 glyph->left_box_line_p = it->start_of_box_run_p;
18746 glyph->right_box_line_p = it->end_of_box_run_p;
18747 glyph->overlaps_vertically_p = 0;
18748 glyph->padding_p = 0;
18749 glyph->glyph_not_available_p = 0;
18750 glyph->face_id = it->face_id;
18751 glyph->u.img_id = img->id;
18752 glyph->slice = slice;
18753 glyph->font_type = FONT_TYPE_UNKNOWN;
18754 ++it->glyph_row->used[area];
18755 }
18756 else
18757 IT_EXPAND_MATRIX_WIDTH (it, area);
18758 }
18759 }
18760
18761
18762 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18763 of the glyph, WIDTH and HEIGHT are the width and height of the
18764 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18765
18766 static void
18767 append_stretch_glyph (it, object, width, height, ascent)
18768 struct it *it;
18769 Lisp_Object object;
18770 int width, height;
18771 int ascent;
18772 {
18773 struct glyph *glyph;
18774 enum glyph_row_area area = it->area;
18775
18776 xassert (ascent >= 0 && ascent <= height);
18777
18778 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18779 if (glyph < it->glyph_row->glyphs[area + 1])
18780 {
18781 glyph->charpos = CHARPOS (it->position);
18782 glyph->object = object;
18783 glyph->pixel_width = width;
18784 glyph->ascent = ascent;
18785 glyph->descent = height - ascent;
18786 glyph->voffset = it->voffset;
18787 glyph->type = STRETCH_GLYPH;
18788 glyph->multibyte_p = it->multibyte_p;
18789 glyph->left_box_line_p = it->start_of_box_run_p;
18790 glyph->right_box_line_p = it->end_of_box_run_p;
18791 glyph->overlaps_vertically_p = 0;
18792 glyph->padding_p = 0;
18793 glyph->glyph_not_available_p = 0;
18794 glyph->face_id = it->face_id;
18795 glyph->u.stretch.ascent = ascent;
18796 glyph->u.stretch.height = height;
18797 glyph->slice = null_glyph_slice;
18798 glyph->font_type = FONT_TYPE_UNKNOWN;
18799 ++it->glyph_row->used[area];
18800 }
18801 else
18802 IT_EXPAND_MATRIX_WIDTH (it, area);
18803 }
18804
18805
18806 /* Produce a stretch glyph for iterator IT. IT->object is the value
18807 of the glyph property displayed. The value must be a list
18808 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18809 being recognized:
18810
18811 1. `:width WIDTH' specifies that the space should be WIDTH *
18812 canonical char width wide. WIDTH may be an integer or floating
18813 point number.
18814
18815 2. `:relative-width FACTOR' specifies that the width of the stretch
18816 should be computed from the width of the first character having the
18817 `glyph' property, and should be FACTOR times that width.
18818
18819 3. `:align-to HPOS' specifies that the space should be wide enough
18820 to reach HPOS, a value in canonical character units.
18821
18822 Exactly one of the above pairs must be present.
18823
18824 4. `:height HEIGHT' specifies that the height of the stretch produced
18825 should be HEIGHT, measured in canonical character units.
18826
18827 5. `:relative-height FACTOR' specifies that the height of the
18828 stretch should be FACTOR times the height of the characters having
18829 the glyph property.
18830
18831 Either none or exactly one of 4 or 5 must be present.
18832
18833 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18834 of the stretch should be used for the ascent of the stretch.
18835 ASCENT must be in the range 0 <= ASCENT <= 100. */
18836
18837 static void
18838 produce_stretch_glyph (it)
18839 struct it *it;
18840 {
18841 /* (space :width WIDTH :height HEIGHT ...) */
18842 Lisp_Object prop, plist;
18843 int width = 0, height = 0, align_to = -1;
18844 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18845 int ascent = 0;
18846 double tem;
18847 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18848 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18849
18850 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18851
18852 /* List should start with `space'. */
18853 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18854 plist = XCDR (it->object);
18855
18856 /* Compute the width of the stretch. */
18857 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18858 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18859 {
18860 /* Absolute width `:width WIDTH' specified and valid. */
18861 zero_width_ok_p = 1;
18862 width = (int)tem;
18863 }
18864 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18865 NUMVAL (prop) > 0)
18866 {
18867 /* Relative width `:relative-width FACTOR' specified and valid.
18868 Compute the width of the characters having the `glyph'
18869 property. */
18870 struct it it2;
18871 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18872
18873 it2 = *it;
18874 if (it->multibyte_p)
18875 {
18876 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18877 - IT_BYTEPOS (*it));
18878 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18879 }
18880 else
18881 it2.c = *p, it2.len = 1;
18882
18883 it2.glyph_row = NULL;
18884 it2.what = IT_CHARACTER;
18885 x_produce_glyphs (&it2);
18886 width = NUMVAL (prop) * it2.pixel_width;
18887 }
18888 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18889 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18890 {
18891 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18892 align_to = (align_to < 0
18893 ? 0
18894 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18895 else if (align_to < 0)
18896 align_to = window_box_left_offset (it->w, TEXT_AREA);
18897 width = max (0, (int)tem + align_to - it->current_x);
18898 zero_width_ok_p = 1;
18899 }
18900 else
18901 /* Nothing specified -> width defaults to canonical char width. */
18902 width = FRAME_COLUMN_WIDTH (it->f);
18903
18904 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18905 width = 1;
18906
18907 /* Compute height. */
18908 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18909 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18910 {
18911 height = (int)tem;
18912 zero_height_ok_p = 1;
18913 }
18914 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18915 NUMVAL (prop) > 0)
18916 height = FONT_HEIGHT (font) * NUMVAL (prop);
18917 else
18918 height = FONT_HEIGHT (font);
18919
18920 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18921 height = 1;
18922
18923 /* Compute percentage of height used for ascent. If
18924 `:ascent ASCENT' is present and valid, use that. Otherwise,
18925 derive the ascent from the font in use. */
18926 if (prop = Fsafe_plist_get (plist, QCascent),
18927 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18928 ascent = height * NUMVAL (prop) / 100.0;
18929 else if (!NILP (prop)
18930 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18931 ascent = min (max (0, (int)tem), height);
18932 else
18933 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18934
18935 if (width > 0 && height > 0 && it->glyph_row)
18936 {
18937 Lisp_Object object = it->stack[it->sp - 1].string;
18938 if (!STRINGP (object))
18939 object = it->w->buffer;
18940 append_stretch_glyph (it, object, width, height, ascent);
18941 }
18942
18943 it->pixel_width = width;
18944 it->ascent = it->phys_ascent = ascent;
18945 it->descent = it->phys_descent = height - it->ascent;
18946 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18947
18948 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18949 {
18950 if (face->box_line_width > 0)
18951 {
18952 it->ascent += face->box_line_width;
18953 it->descent += face->box_line_width;
18954 }
18955
18956 if (it->start_of_box_run_p)
18957 it->pixel_width += abs (face->box_line_width);
18958 if (it->end_of_box_run_p)
18959 it->pixel_width += abs (face->box_line_width);
18960 }
18961
18962 take_vertical_position_into_account (it);
18963 }
18964
18965 /* Get line-height and line-spacing property at point.
18966 If line-height has format (HEIGHT TOTAL), return TOTAL
18967 in TOTAL_HEIGHT. */
18968
18969 static Lisp_Object
18970 get_line_height_property (it, prop)
18971 struct it *it;
18972 Lisp_Object prop;
18973 {
18974 Lisp_Object position, val;
18975
18976 if (STRINGP (it->object))
18977 position = make_number (IT_STRING_CHARPOS (*it));
18978 else if (BUFFERP (it->object))
18979 position = make_number (IT_CHARPOS (*it));
18980 else
18981 return Qnil;
18982
18983 return Fget_char_property (position, prop, it->object);
18984 }
18985
18986 /* Calculate line-height and line-spacing properties.
18987 An integer value specifies explicit pixel value.
18988 A float value specifies relative value to current face height.
18989 A cons (float . face-name) specifies relative value to
18990 height of specified face font.
18991
18992 Returns height in pixels, or nil. */
18993
18994
18995 static Lisp_Object
18996 calc_line_height_property (it, val, font, boff, override)
18997 struct it *it;
18998 Lisp_Object val;
18999 XFontStruct *font;
19000 int boff, override;
19001 {
19002 Lisp_Object face_name = Qnil;
19003 int ascent, descent, height;
19004
19005 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19006 return val;
19007
19008 if (CONSP (val))
19009 {
19010 face_name = XCAR (val);
19011 val = XCDR (val);
19012 if (!NUMBERP (val))
19013 val = make_number (1);
19014 if (NILP (face_name))
19015 {
19016 height = it->ascent + it->descent;
19017 goto scale;
19018 }
19019 }
19020
19021 if (NILP (face_name))
19022 {
19023 font = FRAME_FONT (it->f);
19024 boff = FRAME_BASELINE_OFFSET (it->f);
19025 }
19026 else if (EQ (face_name, Qt))
19027 {
19028 override = 0;
19029 }
19030 else
19031 {
19032 int face_id;
19033 struct face *face;
19034 struct font_info *font_info;
19035
19036 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19037 if (face_id < 0)
19038 return make_number (-1);
19039
19040 face = FACE_FROM_ID (it->f, face_id);
19041 font = face->font;
19042 if (font == NULL)
19043 return make_number (-1);
19044
19045 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19046 boff = font_info->baseline_offset;
19047 if (font_info->vertical_centering)
19048 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19049 }
19050
19051 ascent = FONT_BASE (font) + boff;
19052 descent = FONT_DESCENT (font) - boff;
19053
19054 if (override)
19055 {
19056 it->override_ascent = ascent;
19057 it->override_descent = descent;
19058 it->override_boff = boff;
19059 }
19060
19061 height = ascent + descent;
19062
19063 scale:
19064 if (FLOATP (val))
19065 height = (int)(XFLOAT_DATA (val) * height);
19066 else if (INTEGERP (val))
19067 height *= XINT (val);
19068
19069 return make_number (height);
19070 }
19071
19072
19073 /* RIF:
19074 Produce glyphs/get display metrics for the display element IT is
19075 loaded with. See the description of struct display_iterator in
19076 dispextern.h for an overview of struct display_iterator. */
19077
19078 void
19079 x_produce_glyphs (it)
19080 struct it *it;
19081 {
19082 int extra_line_spacing = it->extra_line_spacing;
19083
19084 it->glyph_not_available_p = 0;
19085
19086 if (it->what == IT_CHARACTER)
19087 {
19088 XChar2b char2b;
19089 XFontStruct *font;
19090 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19091 XCharStruct *pcm;
19092 int font_not_found_p;
19093 struct font_info *font_info;
19094 int boff; /* baseline offset */
19095 /* We may change it->multibyte_p upon unibyte<->multibyte
19096 conversion. So, save the current value now and restore it
19097 later.
19098
19099 Note: It seems that we don't have to record multibyte_p in
19100 struct glyph because the character code itself tells if or
19101 not the character is multibyte. Thus, in the future, we must
19102 consider eliminating the field `multibyte_p' in the struct
19103 glyph. */
19104 int saved_multibyte_p = it->multibyte_p;
19105
19106 /* Maybe translate single-byte characters to multibyte, or the
19107 other way. */
19108 it->char_to_display = it->c;
19109 if (!ASCII_BYTE_P (it->c))
19110 {
19111 if (unibyte_display_via_language_environment
19112 && SINGLE_BYTE_CHAR_P (it->c)
19113 && (it->c >= 0240
19114 || !NILP (Vnonascii_translation_table)))
19115 {
19116 it->char_to_display = unibyte_char_to_multibyte (it->c);
19117 it->multibyte_p = 1;
19118 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19119 face = FACE_FROM_ID (it->f, it->face_id);
19120 }
19121 else if (!SINGLE_BYTE_CHAR_P (it->c)
19122 && !it->multibyte_p)
19123 {
19124 it->multibyte_p = 1;
19125 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19126 face = FACE_FROM_ID (it->f, it->face_id);
19127 }
19128 }
19129
19130 /* Get font to use. Encode IT->char_to_display. */
19131 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19132 &char2b, it->multibyte_p, 0);
19133 font = face->font;
19134
19135 /* When no suitable font found, use the default font. */
19136 font_not_found_p = font == NULL;
19137 if (font_not_found_p)
19138 {
19139 font = FRAME_FONT (it->f);
19140 boff = FRAME_BASELINE_OFFSET (it->f);
19141 font_info = NULL;
19142 }
19143 else
19144 {
19145 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19146 boff = font_info->baseline_offset;
19147 if (font_info->vertical_centering)
19148 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19149 }
19150
19151 if (it->char_to_display >= ' '
19152 && (!it->multibyte_p || it->char_to_display < 128))
19153 {
19154 /* Either unibyte or ASCII. */
19155 int stretched_p;
19156
19157 it->nglyphs = 1;
19158
19159 pcm = rif->per_char_metric (font, &char2b,
19160 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19161
19162 if (it->override_ascent >= 0)
19163 {
19164 it->ascent = it->override_ascent;
19165 it->descent = it->override_descent;
19166 boff = it->override_boff;
19167 }
19168 else
19169 {
19170 it->ascent = FONT_BASE (font) + boff;
19171 it->descent = FONT_DESCENT (font) - boff;
19172 }
19173
19174 if (pcm)
19175 {
19176 it->phys_ascent = pcm->ascent + boff;
19177 it->phys_descent = pcm->descent - boff;
19178 it->pixel_width = pcm->width;
19179 }
19180 else
19181 {
19182 it->glyph_not_available_p = 1;
19183 it->phys_ascent = it->ascent;
19184 it->phys_descent = it->descent;
19185 it->pixel_width = FONT_WIDTH (font);
19186 }
19187
19188 if (it->constrain_row_ascent_descent_p)
19189 {
19190 if (it->descent > it->max_descent)
19191 {
19192 it->ascent += it->descent - it->max_descent;
19193 it->descent = it->max_descent;
19194 }
19195 if (it->ascent > it->max_ascent)
19196 {
19197 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19198 it->ascent = it->max_ascent;
19199 }
19200 it->phys_ascent = min (it->phys_ascent, it->ascent);
19201 it->phys_descent = min (it->phys_descent, it->descent);
19202 extra_line_spacing = 0;
19203 }
19204
19205 /* If this is a space inside a region of text with
19206 `space-width' property, change its width. */
19207 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19208 if (stretched_p)
19209 it->pixel_width *= XFLOATINT (it->space_width);
19210
19211 /* If face has a box, add the box thickness to the character
19212 height. If character has a box line to the left and/or
19213 right, add the box line width to the character's width. */
19214 if (face->box != FACE_NO_BOX)
19215 {
19216 int thick = face->box_line_width;
19217
19218 if (thick > 0)
19219 {
19220 it->ascent += thick;
19221 it->descent += thick;
19222 }
19223 else
19224 thick = -thick;
19225
19226 if (it->start_of_box_run_p)
19227 it->pixel_width += thick;
19228 if (it->end_of_box_run_p)
19229 it->pixel_width += thick;
19230 }
19231
19232 /* If face has an overline, add the height of the overline
19233 (1 pixel) and a 1 pixel margin to the character height. */
19234 if (face->overline_p)
19235 it->ascent += 2;
19236
19237 if (it->constrain_row_ascent_descent_p)
19238 {
19239 if (it->ascent > it->max_ascent)
19240 it->ascent = it->max_ascent;
19241 if (it->descent > it->max_descent)
19242 it->descent = it->max_descent;
19243 }
19244
19245 take_vertical_position_into_account (it);
19246
19247 /* If we have to actually produce glyphs, do it. */
19248 if (it->glyph_row)
19249 {
19250 if (stretched_p)
19251 {
19252 /* Translate a space with a `space-width' property
19253 into a stretch glyph. */
19254 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19255 / FONT_HEIGHT (font));
19256 append_stretch_glyph (it, it->object, it->pixel_width,
19257 it->ascent + it->descent, ascent);
19258 }
19259 else
19260 append_glyph (it);
19261
19262 /* If characters with lbearing or rbearing are displayed
19263 in this line, record that fact in a flag of the
19264 glyph row. This is used to optimize X output code. */
19265 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19266 it->glyph_row->contains_overlapping_glyphs_p = 1;
19267 }
19268 }
19269 else if (it->char_to_display == '\n')
19270 {
19271 /* A newline has no width but we need the height of the line.
19272 But if previous part of the line set a height, don't
19273 increase that height */
19274
19275 Lisp_Object height;
19276 Lisp_Object total_height = Qnil;
19277
19278 it->override_ascent = -1;
19279 it->pixel_width = 0;
19280 it->nglyphs = 0;
19281
19282 height = get_line_height_property(it, Qline_height);
19283 /* Split (line-height total-height) list */
19284 if (CONSP (height)
19285 && CONSP (XCDR (height))
19286 && NILP (XCDR (XCDR (height))))
19287 {
19288 total_height = XCAR (XCDR (height));
19289 height = XCAR (height);
19290 }
19291 height = calc_line_height_property(it, height, font, boff, 1);
19292
19293 if (it->override_ascent >= 0)
19294 {
19295 it->ascent = it->override_ascent;
19296 it->descent = it->override_descent;
19297 boff = it->override_boff;
19298 }
19299 else
19300 {
19301 it->ascent = FONT_BASE (font) + boff;
19302 it->descent = FONT_DESCENT (font) - boff;
19303 }
19304
19305 if (EQ (height, Qt))
19306 {
19307 if (it->descent > it->max_descent)
19308 {
19309 it->ascent += it->descent - it->max_descent;
19310 it->descent = it->max_descent;
19311 }
19312 if (it->ascent > it->max_ascent)
19313 {
19314 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19315 it->ascent = it->max_ascent;
19316 }
19317 it->phys_ascent = min (it->phys_ascent, it->ascent);
19318 it->phys_descent = min (it->phys_descent, it->descent);
19319 it->constrain_row_ascent_descent_p = 1;
19320 extra_line_spacing = 0;
19321 }
19322 else
19323 {
19324 Lisp_Object spacing;
19325 int total = 0;
19326
19327 it->phys_ascent = it->ascent;
19328 it->phys_descent = it->descent;
19329
19330 if ((it->max_ascent > 0 || it->max_descent > 0)
19331 && face->box != FACE_NO_BOX
19332 && face->box_line_width > 0)
19333 {
19334 it->ascent += face->box_line_width;
19335 it->descent += face->box_line_width;
19336 }
19337 if (!NILP (height)
19338 && XINT (height) > it->ascent + it->descent)
19339 it->ascent = XINT (height) - it->descent;
19340
19341 if (!NILP (total_height))
19342 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19343 else
19344 {
19345 spacing = get_line_height_property(it, Qline_spacing);
19346 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19347 }
19348 if (INTEGERP (spacing))
19349 {
19350 extra_line_spacing = XINT (spacing);
19351 if (!NILP (total_height))
19352 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19353 }
19354 }
19355 }
19356 else if (it->char_to_display == '\t')
19357 {
19358 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19359 int x = it->current_x + it->continuation_lines_width;
19360 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19361
19362 /* If the distance from the current position to the next tab
19363 stop is less than a space character width, use the
19364 tab stop after that. */
19365 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19366 next_tab_x += tab_width;
19367
19368 it->pixel_width = next_tab_x - x;
19369 it->nglyphs = 1;
19370 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19371 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19372
19373 if (it->glyph_row)
19374 {
19375 append_stretch_glyph (it, it->object, it->pixel_width,
19376 it->ascent + it->descent, it->ascent);
19377 }
19378 }
19379 else
19380 {
19381 /* A multi-byte character. Assume that the display width of the
19382 character is the width of the character multiplied by the
19383 width of the font. */
19384
19385 /* If we found a font, this font should give us the right
19386 metrics. If we didn't find a font, use the frame's
19387 default font and calculate the width of the character
19388 from the charset width; this is what old redisplay code
19389 did. */
19390
19391 pcm = rif->per_char_metric (font, &char2b,
19392 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19393
19394 if (font_not_found_p || !pcm)
19395 {
19396 int charset = CHAR_CHARSET (it->char_to_display);
19397
19398 it->glyph_not_available_p = 1;
19399 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19400 * CHARSET_WIDTH (charset));
19401 it->phys_ascent = FONT_BASE (font) + boff;
19402 it->phys_descent = FONT_DESCENT (font) - boff;
19403 }
19404 else
19405 {
19406 it->pixel_width = pcm->width;
19407 it->phys_ascent = pcm->ascent + boff;
19408 it->phys_descent = pcm->descent - boff;
19409 if (it->glyph_row
19410 && (pcm->lbearing < 0
19411 || pcm->rbearing > pcm->width))
19412 it->glyph_row->contains_overlapping_glyphs_p = 1;
19413 }
19414 it->nglyphs = 1;
19415 it->ascent = FONT_BASE (font) + boff;
19416 it->descent = FONT_DESCENT (font) - boff;
19417 if (face->box != FACE_NO_BOX)
19418 {
19419 int thick = face->box_line_width;
19420
19421 if (thick > 0)
19422 {
19423 it->ascent += thick;
19424 it->descent += thick;
19425 }
19426 else
19427 thick = - thick;
19428
19429 if (it->start_of_box_run_p)
19430 it->pixel_width += thick;
19431 if (it->end_of_box_run_p)
19432 it->pixel_width += thick;
19433 }
19434
19435 /* If face has an overline, add the height of the overline
19436 (1 pixel) and a 1 pixel margin to the character height. */
19437 if (face->overline_p)
19438 it->ascent += 2;
19439
19440 take_vertical_position_into_account (it);
19441
19442 if (it->glyph_row)
19443 append_glyph (it);
19444 }
19445 it->multibyte_p = saved_multibyte_p;
19446 }
19447 else if (it->what == IT_COMPOSITION)
19448 {
19449 /* Note: A composition is represented as one glyph in the
19450 glyph matrix. There are no padding glyphs. */
19451 XChar2b char2b;
19452 XFontStruct *font;
19453 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19454 XCharStruct *pcm;
19455 int font_not_found_p;
19456 struct font_info *font_info;
19457 int boff; /* baseline offset */
19458 struct composition *cmp = composition_table[it->cmp_id];
19459
19460 /* Maybe translate single-byte characters to multibyte. */
19461 it->char_to_display = it->c;
19462 if (unibyte_display_via_language_environment
19463 && SINGLE_BYTE_CHAR_P (it->c)
19464 && (it->c >= 0240
19465 || (it->c >= 0200
19466 && !NILP (Vnonascii_translation_table))))
19467 {
19468 it->char_to_display = unibyte_char_to_multibyte (it->c);
19469 }
19470
19471 /* Get face and font to use. Encode IT->char_to_display. */
19472 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19473 face = FACE_FROM_ID (it->f, it->face_id);
19474 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19475 &char2b, it->multibyte_p, 0);
19476 font = face->font;
19477
19478 /* When no suitable font found, use the default font. */
19479 font_not_found_p = font == NULL;
19480 if (font_not_found_p)
19481 {
19482 font = FRAME_FONT (it->f);
19483 boff = FRAME_BASELINE_OFFSET (it->f);
19484 font_info = NULL;
19485 }
19486 else
19487 {
19488 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19489 boff = font_info->baseline_offset;
19490 if (font_info->vertical_centering)
19491 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19492 }
19493
19494 /* There are no padding glyphs, so there is only one glyph to
19495 produce for the composition. Important is that pixel_width,
19496 ascent and descent are the values of what is drawn by
19497 draw_glyphs (i.e. the values of the overall glyphs composed). */
19498 it->nglyphs = 1;
19499
19500 /* If we have not yet calculated pixel size data of glyphs of
19501 the composition for the current face font, calculate them
19502 now. Theoretically, we have to check all fonts for the
19503 glyphs, but that requires much time and memory space. So,
19504 here we check only the font of the first glyph. This leads
19505 to incorrect display very rarely, and C-l (recenter) can
19506 correct the display anyway. */
19507 if (cmp->font != (void *) font)
19508 {
19509 /* Ascent and descent of the font of the first character of
19510 this composition (adjusted by baseline offset). Ascent
19511 and descent of overall glyphs should not be less than
19512 them respectively. */
19513 int font_ascent = FONT_BASE (font) + boff;
19514 int font_descent = FONT_DESCENT (font) - boff;
19515 /* Bounding box of the overall glyphs. */
19516 int leftmost, rightmost, lowest, highest;
19517 int i, width, ascent, descent;
19518
19519 cmp->font = (void *) font;
19520
19521 /* Initialize the bounding box. */
19522 if (font_info
19523 && (pcm = rif->per_char_metric (font, &char2b,
19524 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19525 {
19526 width = pcm->width;
19527 ascent = pcm->ascent;
19528 descent = pcm->descent;
19529 }
19530 else
19531 {
19532 width = FONT_WIDTH (font);
19533 ascent = FONT_BASE (font);
19534 descent = FONT_DESCENT (font);
19535 }
19536
19537 rightmost = width;
19538 lowest = - descent + boff;
19539 highest = ascent + boff;
19540 leftmost = 0;
19541
19542 if (font_info
19543 && font_info->default_ascent
19544 && CHAR_TABLE_P (Vuse_default_ascent)
19545 && !NILP (Faref (Vuse_default_ascent,
19546 make_number (it->char_to_display))))
19547 highest = font_info->default_ascent + boff;
19548
19549 /* Draw the first glyph at the normal position. It may be
19550 shifted to right later if some other glyphs are drawn at
19551 the left. */
19552 cmp->offsets[0] = 0;
19553 cmp->offsets[1] = boff;
19554
19555 /* Set cmp->offsets for the remaining glyphs. */
19556 for (i = 1; i < cmp->glyph_len; i++)
19557 {
19558 int left, right, btm, top;
19559 int ch = COMPOSITION_GLYPH (cmp, i);
19560 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19561
19562 face = FACE_FROM_ID (it->f, face_id);
19563 get_char_face_and_encoding (it->f, ch, face->id,
19564 &char2b, it->multibyte_p, 0);
19565 font = face->font;
19566 if (font == NULL)
19567 {
19568 font = FRAME_FONT (it->f);
19569 boff = FRAME_BASELINE_OFFSET (it->f);
19570 font_info = NULL;
19571 }
19572 else
19573 {
19574 font_info
19575 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19576 boff = font_info->baseline_offset;
19577 if (font_info->vertical_centering)
19578 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19579 }
19580
19581 if (font_info
19582 && (pcm = rif->per_char_metric (font, &char2b,
19583 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19584 {
19585 width = pcm->width;
19586 ascent = pcm->ascent;
19587 descent = pcm->descent;
19588 }
19589 else
19590 {
19591 width = FONT_WIDTH (font);
19592 ascent = 1;
19593 descent = 0;
19594 }
19595
19596 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19597 {
19598 /* Relative composition with or without
19599 alternate chars. */
19600 left = (leftmost + rightmost - width) / 2;
19601 btm = - descent + boff;
19602 if (font_info && font_info->relative_compose
19603 && (! CHAR_TABLE_P (Vignore_relative_composition)
19604 || NILP (Faref (Vignore_relative_composition,
19605 make_number (ch)))))
19606 {
19607
19608 if (- descent >= font_info->relative_compose)
19609 /* One extra pixel between two glyphs. */
19610 btm = highest + 1;
19611 else if (ascent <= 0)
19612 /* One extra pixel between two glyphs. */
19613 btm = lowest - 1 - ascent - descent;
19614 }
19615 }
19616 else
19617 {
19618 /* A composition rule is specified by an integer
19619 value that encodes global and new reference
19620 points (GREF and NREF). GREF and NREF are
19621 specified by numbers as below:
19622
19623 0---1---2 -- ascent
19624 | |
19625 | |
19626 | |
19627 9--10--11 -- center
19628 | |
19629 ---3---4---5--- baseline
19630 | |
19631 6---7---8 -- descent
19632 */
19633 int rule = COMPOSITION_RULE (cmp, i);
19634 int gref, nref, grefx, grefy, nrefx, nrefy;
19635
19636 COMPOSITION_DECODE_RULE (rule, gref, nref);
19637 grefx = gref % 3, nrefx = nref % 3;
19638 grefy = gref / 3, nrefy = nref / 3;
19639
19640 left = (leftmost
19641 + grefx * (rightmost - leftmost) / 2
19642 - nrefx * width / 2);
19643 btm = ((grefy == 0 ? highest
19644 : grefy == 1 ? 0
19645 : grefy == 2 ? lowest
19646 : (highest + lowest) / 2)
19647 - (nrefy == 0 ? ascent + descent
19648 : nrefy == 1 ? descent - boff
19649 : nrefy == 2 ? 0
19650 : (ascent + descent) / 2));
19651 }
19652
19653 cmp->offsets[i * 2] = left;
19654 cmp->offsets[i * 2 + 1] = btm + descent;
19655
19656 /* Update the bounding box of the overall glyphs. */
19657 right = left + width;
19658 top = btm + descent + ascent;
19659 if (left < leftmost)
19660 leftmost = left;
19661 if (right > rightmost)
19662 rightmost = right;
19663 if (top > highest)
19664 highest = top;
19665 if (btm < lowest)
19666 lowest = btm;
19667 }
19668
19669 /* If there are glyphs whose x-offsets are negative,
19670 shift all glyphs to the right and make all x-offsets
19671 non-negative. */
19672 if (leftmost < 0)
19673 {
19674 for (i = 0; i < cmp->glyph_len; i++)
19675 cmp->offsets[i * 2] -= leftmost;
19676 rightmost -= leftmost;
19677 }
19678
19679 cmp->pixel_width = rightmost;
19680 cmp->ascent = highest;
19681 cmp->descent = - lowest;
19682 if (cmp->ascent < font_ascent)
19683 cmp->ascent = font_ascent;
19684 if (cmp->descent < font_descent)
19685 cmp->descent = font_descent;
19686 }
19687
19688 it->pixel_width = cmp->pixel_width;
19689 it->ascent = it->phys_ascent = cmp->ascent;
19690 it->descent = it->phys_descent = cmp->descent;
19691
19692 if (face->box != FACE_NO_BOX)
19693 {
19694 int thick = face->box_line_width;
19695
19696 if (thick > 0)
19697 {
19698 it->ascent += thick;
19699 it->descent += thick;
19700 }
19701 else
19702 thick = - thick;
19703
19704 if (it->start_of_box_run_p)
19705 it->pixel_width += thick;
19706 if (it->end_of_box_run_p)
19707 it->pixel_width += thick;
19708 }
19709
19710 /* If face has an overline, add the height of the overline
19711 (1 pixel) and a 1 pixel margin to the character height. */
19712 if (face->overline_p)
19713 it->ascent += 2;
19714
19715 take_vertical_position_into_account (it);
19716
19717 if (it->glyph_row)
19718 append_composite_glyph (it);
19719 }
19720 else if (it->what == IT_IMAGE)
19721 produce_image_glyph (it);
19722 else if (it->what == IT_STRETCH)
19723 produce_stretch_glyph (it);
19724
19725 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19726 because this isn't true for images with `:ascent 100'. */
19727 xassert (it->ascent >= 0 && it->descent >= 0);
19728 if (it->area == TEXT_AREA)
19729 it->current_x += it->pixel_width;
19730
19731 if (extra_line_spacing > 0)
19732 {
19733 it->descent += extra_line_spacing;
19734 if (extra_line_spacing > it->max_extra_line_spacing)
19735 it->max_extra_line_spacing = extra_line_spacing;
19736 }
19737
19738 it->max_ascent = max (it->max_ascent, it->ascent);
19739 it->max_descent = max (it->max_descent, it->descent);
19740 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19741 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19742 }
19743
19744 /* EXPORT for RIF:
19745 Output LEN glyphs starting at START at the nominal cursor position.
19746 Advance the nominal cursor over the text. The global variable
19747 updated_window contains the window being updated, updated_row is
19748 the glyph row being updated, and updated_area is the area of that
19749 row being updated. */
19750
19751 void
19752 x_write_glyphs (start, len)
19753 struct glyph *start;
19754 int len;
19755 {
19756 int x, hpos;
19757
19758 xassert (updated_window && updated_row);
19759 BLOCK_INPUT;
19760
19761 /* Write glyphs. */
19762
19763 hpos = start - updated_row->glyphs[updated_area];
19764 x = draw_glyphs (updated_window, output_cursor.x,
19765 updated_row, updated_area,
19766 hpos, hpos + len,
19767 DRAW_NORMAL_TEXT, 0);
19768
19769 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19770 if (updated_area == TEXT_AREA
19771 && updated_window->phys_cursor_on_p
19772 && updated_window->phys_cursor.vpos == output_cursor.vpos
19773 && updated_window->phys_cursor.hpos >= hpos
19774 && updated_window->phys_cursor.hpos < hpos + len)
19775 updated_window->phys_cursor_on_p = 0;
19776
19777 UNBLOCK_INPUT;
19778
19779 /* Advance the output cursor. */
19780 output_cursor.hpos += len;
19781 output_cursor.x = x;
19782 }
19783
19784
19785 /* EXPORT for RIF:
19786 Insert LEN glyphs from START at the nominal cursor position. */
19787
19788 void
19789 x_insert_glyphs (start, len)
19790 struct glyph *start;
19791 int len;
19792 {
19793 struct frame *f;
19794 struct window *w;
19795 int line_height, shift_by_width, shifted_region_width;
19796 struct glyph_row *row;
19797 struct glyph *glyph;
19798 int frame_x, frame_y, hpos;
19799
19800 xassert (updated_window && updated_row);
19801 BLOCK_INPUT;
19802 w = updated_window;
19803 f = XFRAME (WINDOW_FRAME (w));
19804
19805 /* Get the height of the line we are in. */
19806 row = updated_row;
19807 line_height = row->height;
19808
19809 /* Get the width of the glyphs to insert. */
19810 shift_by_width = 0;
19811 for (glyph = start; glyph < start + len; ++glyph)
19812 shift_by_width += glyph->pixel_width;
19813
19814 /* Get the width of the region to shift right. */
19815 shifted_region_width = (window_box_width (w, updated_area)
19816 - output_cursor.x
19817 - shift_by_width);
19818
19819 /* Shift right. */
19820 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19821 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19822
19823 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19824 line_height, shift_by_width);
19825
19826 /* Write the glyphs. */
19827 hpos = start - row->glyphs[updated_area];
19828 draw_glyphs (w, output_cursor.x, row, updated_area,
19829 hpos, hpos + len,
19830 DRAW_NORMAL_TEXT, 0);
19831
19832 /* Advance the output cursor. */
19833 output_cursor.hpos += len;
19834 output_cursor.x += shift_by_width;
19835 UNBLOCK_INPUT;
19836 }
19837
19838
19839 /* EXPORT for RIF:
19840 Erase the current text line from the nominal cursor position
19841 (inclusive) to pixel column TO_X (exclusive). The idea is that
19842 everything from TO_X onward is already erased.
19843
19844 TO_X is a pixel position relative to updated_area of
19845 updated_window. TO_X == -1 means clear to the end of this area. */
19846
19847 void
19848 x_clear_end_of_line (to_x)
19849 int to_x;
19850 {
19851 struct frame *f;
19852 struct window *w = updated_window;
19853 int max_x, min_y, max_y;
19854 int from_x, from_y, to_y;
19855
19856 xassert (updated_window && updated_row);
19857 f = XFRAME (w->frame);
19858
19859 if (updated_row->full_width_p)
19860 max_x = WINDOW_TOTAL_WIDTH (w);
19861 else
19862 max_x = window_box_width (w, updated_area);
19863 max_y = window_text_bottom_y (w);
19864
19865 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19866 of window. For TO_X > 0, truncate to end of drawing area. */
19867 if (to_x == 0)
19868 return;
19869 else if (to_x < 0)
19870 to_x = max_x;
19871 else
19872 to_x = min (to_x, max_x);
19873
19874 to_y = min (max_y, output_cursor.y + updated_row->height);
19875
19876 /* Notice if the cursor will be cleared by this operation. */
19877 if (!updated_row->full_width_p)
19878 notice_overwritten_cursor (w, updated_area,
19879 output_cursor.x, -1,
19880 updated_row->y,
19881 MATRIX_ROW_BOTTOM_Y (updated_row));
19882
19883 from_x = output_cursor.x;
19884
19885 /* Translate to frame coordinates. */
19886 if (updated_row->full_width_p)
19887 {
19888 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19889 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19890 }
19891 else
19892 {
19893 int area_left = window_box_left (w, updated_area);
19894 from_x += area_left;
19895 to_x += area_left;
19896 }
19897
19898 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19899 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19900 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19901
19902 /* Prevent inadvertently clearing to end of the X window. */
19903 if (to_x > from_x && to_y > from_y)
19904 {
19905 BLOCK_INPUT;
19906 rif->clear_frame_area (f, from_x, from_y,
19907 to_x - from_x, to_y - from_y);
19908 UNBLOCK_INPUT;
19909 }
19910 }
19911
19912 #endif /* HAVE_WINDOW_SYSTEM */
19913
19914
19915 \f
19916 /***********************************************************************
19917 Cursor types
19918 ***********************************************************************/
19919
19920 /* Value is the internal representation of the specified cursor type
19921 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19922 of the bar cursor. */
19923
19924 static enum text_cursor_kinds
19925 get_specified_cursor_type (arg, width)
19926 Lisp_Object arg;
19927 int *width;
19928 {
19929 enum text_cursor_kinds type;
19930
19931 if (NILP (arg))
19932 return NO_CURSOR;
19933
19934 if (EQ (arg, Qbox))
19935 return FILLED_BOX_CURSOR;
19936
19937 if (EQ (arg, Qhollow))
19938 return HOLLOW_BOX_CURSOR;
19939
19940 if (EQ (arg, Qbar))
19941 {
19942 *width = 2;
19943 return BAR_CURSOR;
19944 }
19945
19946 if (CONSP (arg)
19947 && EQ (XCAR (arg), Qbar)
19948 && INTEGERP (XCDR (arg))
19949 && XINT (XCDR (arg)) >= 0)
19950 {
19951 *width = XINT (XCDR (arg));
19952 return BAR_CURSOR;
19953 }
19954
19955 if (EQ (arg, Qhbar))
19956 {
19957 *width = 2;
19958 return HBAR_CURSOR;
19959 }
19960
19961 if (CONSP (arg)
19962 && EQ (XCAR (arg), Qhbar)
19963 && INTEGERP (XCDR (arg))
19964 && XINT (XCDR (arg)) >= 0)
19965 {
19966 *width = XINT (XCDR (arg));
19967 return HBAR_CURSOR;
19968 }
19969
19970 /* Treat anything unknown as "hollow box cursor".
19971 It was bad to signal an error; people have trouble fixing
19972 .Xdefaults with Emacs, when it has something bad in it. */
19973 type = HOLLOW_BOX_CURSOR;
19974
19975 return type;
19976 }
19977
19978 /* Set the default cursor types for specified frame. */
19979 void
19980 set_frame_cursor_types (f, arg)
19981 struct frame *f;
19982 Lisp_Object arg;
19983 {
19984 int width;
19985 Lisp_Object tem;
19986
19987 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19988 FRAME_CURSOR_WIDTH (f) = width;
19989
19990 /* By default, set up the blink-off state depending on the on-state. */
19991
19992 tem = Fassoc (arg, Vblink_cursor_alist);
19993 if (!NILP (tem))
19994 {
19995 FRAME_BLINK_OFF_CURSOR (f)
19996 = get_specified_cursor_type (XCDR (tem), &width);
19997 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19998 }
19999 else
20000 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20001 }
20002
20003
20004 /* Return the cursor we want to be displayed in window W. Return
20005 width of bar/hbar cursor through WIDTH arg. Return with
20006 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20007 (i.e. if the `system caret' should track this cursor).
20008
20009 In a mini-buffer window, we want the cursor only to appear if we
20010 are reading input from this window. For the selected window, we
20011 want the cursor type given by the frame parameter or buffer local
20012 setting of cursor-type. If explicitly marked off, draw no cursor.
20013 In all other cases, we want a hollow box cursor. */
20014
20015 static enum text_cursor_kinds
20016 get_window_cursor_type (w, glyph, width, active_cursor)
20017 struct window *w;
20018 struct glyph *glyph;
20019 int *width;
20020 int *active_cursor;
20021 {
20022 struct frame *f = XFRAME (w->frame);
20023 struct buffer *b = XBUFFER (w->buffer);
20024 int cursor_type = DEFAULT_CURSOR;
20025 Lisp_Object alt_cursor;
20026 int non_selected = 0;
20027
20028 *active_cursor = 1;
20029
20030 /* Echo area */
20031 if (cursor_in_echo_area
20032 && FRAME_HAS_MINIBUF_P (f)
20033 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20034 {
20035 if (w == XWINDOW (echo_area_window))
20036 {
20037 *width = FRAME_CURSOR_WIDTH (f);
20038 return FRAME_DESIRED_CURSOR (f);
20039 }
20040
20041 *active_cursor = 0;
20042 non_selected = 1;
20043 }
20044
20045 /* Nonselected window or nonselected frame. */
20046 else if (w != XWINDOW (f->selected_window)
20047 #ifdef HAVE_WINDOW_SYSTEM
20048 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20049 #endif
20050 )
20051 {
20052 *active_cursor = 0;
20053
20054 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20055 return NO_CURSOR;
20056
20057 non_selected = 1;
20058 }
20059
20060 /* Never display a cursor in a window in which cursor-type is nil. */
20061 if (NILP (b->cursor_type))
20062 return NO_CURSOR;
20063
20064 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20065 if (non_selected)
20066 {
20067 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
20068 return get_specified_cursor_type (alt_cursor, width);
20069 }
20070
20071 /* Get the normal cursor type for this window. */
20072 if (EQ (b->cursor_type, Qt))
20073 {
20074 cursor_type = FRAME_DESIRED_CURSOR (f);
20075 *width = FRAME_CURSOR_WIDTH (f);
20076 }
20077 else
20078 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20079
20080 /* Use normal cursor if not blinked off. */
20081 if (!w->cursor_off_p)
20082 {
20083 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20084 if (cursor_type == FILLED_BOX_CURSOR)
20085 cursor_type = HOLLOW_BOX_CURSOR;
20086 }
20087 return cursor_type;
20088 }
20089
20090 /* Cursor is blinked off, so determine how to "toggle" it. */
20091
20092 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20093 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20094 return get_specified_cursor_type (XCDR (alt_cursor), width);
20095
20096 /* Then see if frame has specified a specific blink off cursor type. */
20097 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20098 {
20099 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20100 return FRAME_BLINK_OFF_CURSOR (f);
20101 }
20102
20103 #if 0
20104 /* Some people liked having a permanently visible blinking cursor,
20105 while others had very strong opinions against it. So it was
20106 decided to remove it. KFS 2003-09-03 */
20107
20108 /* Finally perform built-in cursor blinking:
20109 filled box <-> hollow box
20110 wide [h]bar <-> narrow [h]bar
20111 narrow [h]bar <-> no cursor
20112 other type <-> no cursor */
20113
20114 if (cursor_type == FILLED_BOX_CURSOR)
20115 return HOLLOW_BOX_CURSOR;
20116
20117 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20118 {
20119 *width = 1;
20120 return cursor_type;
20121 }
20122 #endif
20123
20124 return NO_CURSOR;
20125 }
20126
20127
20128 #ifdef HAVE_WINDOW_SYSTEM
20129
20130 /* Notice when the text cursor of window W has been completely
20131 overwritten by a drawing operation that outputs glyphs in AREA
20132 starting at X0 and ending at X1 in the line starting at Y0 and
20133 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20134 the rest of the line after X0 has been written. Y coordinates
20135 are window-relative. */
20136
20137 static void
20138 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20139 struct window *w;
20140 enum glyph_row_area area;
20141 int x0, y0, x1, y1;
20142 {
20143 int cx0, cx1, cy0, cy1;
20144 struct glyph_row *row;
20145
20146 if (!w->phys_cursor_on_p)
20147 return;
20148 if (area != TEXT_AREA)
20149 return;
20150
20151 row = w->current_matrix->rows + w->phys_cursor.vpos;
20152 if (!row->displays_text_p)
20153 return;
20154
20155 if (row->cursor_in_fringe_p)
20156 {
20157 row->cursor_in_fringe_p = 0;
20158 draw_fringe_bitmap (w, row, 0);
20159 w->phys_cursor_on_p = 0;
20160 return;
20161 }
20162
20163 cx0 = w->phys_cursor.x;
20164 cx1 = cx0 + w->phys_cursor_width;
20165 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20166 return;
20167
20168 /* The cursor image will be completely removed from the
20169 screen if the output area intersects the cursor area in
20170 y-direction. When we draw in [y0 y1[, and some part of
20171 the cursor is at y < y0, that part must have been drawn
20172 before. When scrolling, the cursor is erased before
20173 actually scrolling, so we don't come here. When not
20174 scrolling, the rows above the old cursor row must have
20175 changed, and in this case these rows must have written
20176 over the cursor image.
20177
20178 Likewise if part of the cursor is below y1, with the
20179 exception of the cursor being in the first blank row at
20180 the buffer and window end because update_text_area
20181 doesn't draw that row. (Except when it does, but
20182 that's handled in update_text_area.) */
20183
20184 cy0 = w->phys_cursor.y;
20185 cy1 = cy0 + w->phys_cursor_height;
20186 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20187 return;
20188
20189 w->phys_cursor_on_p = 0;
20190 }
20191
20192 #endif /* HAVE_WINDOW_SYSTEM */
20193
20194 \f
20195 /************************************************************************
20196 Mouse Face
20197 ************************************************************************/
20198
20199 #ifdef HAVE_WINDOW_SYSTEM
20200
20201 /* EXPORT for RIF:
20202 Fix the display of area AREA of overlapping row ROW in window W. */
20203
20204 void
20205 x_fix_overlapping_area (w, row, area)
20206 struct window *w;
20207 struct glyph_row *row;
20208 enum glyph_row_area area;
20209 {
20210 int i, x;
20211
20212 BLOCK_INPUT;
20213
20214 x = 0;
20215 for (i = 0; i < row->used[area];)
20216 {
20217 if (row->glyphs[area][i].overlaps_vertically_p)
20218 {
20219 int start = i, start_x = x;
20220
20221 do
20222 {
20223 x += row->glyphs[area][i].pixel_width;
20224 ++i;
20225 }
20226 while (i < row->used[area]
20227 && row->glyphs[area][i].overlaps_vertically_p);
20228
20229 draw_glyphs (w, start_x, row, area,
20230 start, i,
20231 DRAW_NORMAL_TEXT, 1);
20232 }
20233 else
20234 {
20235 x += row->glyphs[area][i].pixel_width;
20236 ++i;
20237 }
20238 }
20239
20240 UNBLOCK_INPUT;
20241 }
20242
20243
20244 /* EXPORT:
20245 Draw the cursor glyph of window W in glyph row ROW. See the
20246 comment of draw_glyphs for the meaning of HL. */
20247
20248 void
20249 draw_phys_cursor_glyph (w, row, hl)
20250 struct window *w;
20251 struct glyph_row *row;
20252 enum draw_glyphs_face hl;
20253 {
20254 /* If cursor hpos is out of bounds, don't draw garbage. This can
20255 happen in mini-buffer windows when switching between echo area
20256 glyphs and mini-buffer. */
20257 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20258 {
20259 int on_p = w->phys_cursor_on_p;
20260 int x1;
20261 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20262 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20263 hl, 0);
20264 w->phys_cursor_on_p = on_p;
20265
20266 if (hl == DRAW_CURSOR)
20267 w->phys_cursor_width = x1 - w->phys_cursor.x;
20268 /* When we erase the cursor, and ROW is overlapped by other
20269 rows, make sure that these overlapping parts of other rows
20270 are redrawn. */
20271 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20272 {
20273 if (row > w->current_matrix->rows
20274 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20275 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20276
20277 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20278 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20279 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20280 }
20281 }
20282 }
20283
20284
20285 /* EXPORT:
20286 Erase the image of a cursor of window W from the screen. */
20287
20288 void
20289 erase_phys_cursor (w)
20290 struct window *w;
20291 {
20292 struct frame *f = XFRAME (w->frame);
20293 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20294 int hpos = w->phys_cursor.hpos;
20295 int vpos = w->phys_cursor.vpos;
20296 int mouse_face_here_p = 0;
20297 struct glyph_matrix *active_glyphs = w->current_matrix;
20298 struct glyph_row *cursor_row;
20299 struct glyph *cursor_glyph;
20300 enum draw_glyphs_face hl;
20301
20302 /* No cursor displayed or row invalidated => nothing to do on the
20303 screen. */
20304 if (w->phys_cursor_type == NO_CURSOR)
20305 goto mark_cursor_off;
20306
20307 /* VPOS >= active_glyphs->nrows means that window has been resized.
20308 Don't bother to erase the cursor. */
20309 if (vpos >= active_glyphs->nrows)
20310 goto mark_cursor_off;
20311
20312 /* If row containing cursor is marked invalid, there is nothing we
20313 can do. */
20314 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20315 if (!cursor_row->enabled_p)
20316 goto mark_cursor_off;
20317
20318 /* If line spacing is > 0, old cursor may only be partially visible in
20319 window after split-window. So adjust visible height. */
20320 cursor_row->visible_height = min (cursor_row->visible_height,
20321 window_text_bottom_y (w) - cursor_row->y);
20322
20323 /* If row is completely invisible, don't attempt to delete a cursor which
20324 isn't there. This can happen if cursor is at top of a window, and
20325 we switch to a buffer with a header line in that window. */
20326 if (cursor_row->visible_height <= 0)
20327 goto mark_cursor_off;
20328
20329 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20330 if (cursor_row->cursor_in_fringe_p)
20331 {
20332 cursor_row->cursor_in_fringe_p = 0;
20333 draw_fringe_bitmap (w, cursor_row, 0);
20334 goto mark_cursor_off;
20335 }
20336
20337 /* This can happen when the new row is shorter than the old one.
20338 In this case, either draw_glyphs or clear_end_of_line
20339 should have cleared the cursor. Note that we wouldn't be
20340 able to erase the cursor in this case because we don't have a
20341 cursor glyph at hand. */
20342 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20343 goto mark_cursor_off;
20344
20345 /* If the cursor is in the mouse face area, redisplay that when
20346 we clear the cursor. */
20347 if (! NILP (dpyinfo->mouse_face_window)
20348 && w == XWINDOW (dpyinfo->mouse_face_window)
20349 && (vpos > dpyinfo->mouse_face_beg_row
20350 || (vpos == dpyinfo->mouse_face_beg_row
20351 && hpos >= dpyinfo->mouse_face_beg_col))
20352 && (vpos < dpyinfo->mouse_face_end_row
20353 || (vpos == dpyinfo->mouse_face_end_row
20354 && hpos < dpyinfo->mouse_face_end_col))
20355 /* Don't redraw the cursor's spot in mouse face if it is at the
20356 end of a line (on a newline). The cursor appears there, but
20357 mouse highlighting does not. */
20358 && cursor_row->used[TEXT_AREA] > hpos)
20359 mouse_face_here_p = 1;
20360
20361 /* Maybe clear the display under the cursor. */
20362 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20363 {
20364 int x, y;
20365 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20366 int width;
20367
20368 cursor_glyph = get_phys_cursor_glyph (w);
20369 if (cursor_glyph == NULL)
20370 goto mark_cursor_off;
20371
20372 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20373 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20374 width = min (cursor_glyph->pixel_width,
20375 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20376
20377 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20378 }
20379
20380 /* Erase the cursor by redrawing the character underneath it. */
20381 if (mouse_face_here_p)
20382 hl = DRAW_MOUSE_FACE;
20383 else
20384 hl = DRAW_NORMAL_TEXT;
20385 draw_phys_cursor_glyph (w, cursor_row, hl);
20386
20387 mark_cursor_off:
20388 w->phys_cursor_on_p = 0;
20389 w->phys_cursor_type = NO_CURSOR;
20390 }
20391
20392
20393 /* EXPORT:
20394 Display or clear cursor of window W. If ON is zero, clear the
20395 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20396 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20397
20398 void
20399 display_and_set_cursor (w, on, hpos, vpos, x, y)
20400 struct window *w;
20401 int on, hpos, vpos, x, y;
20402 {
20403 struct frame *f = XFRAME (w->frame);
20404 int new_cursor_type;
20405 int new_cursor_width;
20406 int active_cursor;
20407 struct glyph_row *glyph_row;
20408 struct glyph *glyph;
20409
20410 /* This is pointless on invisible frames, and dangerous on garbaged
20411 windows and frames; in the latter case, the frame or window may
20412 be in the midst of changing its size, and x and y may be off the
20413 window. */
20414 if (! FRAME_VISIBLE_P (f)
20415 || FRAME_GARBAGED_P (f)
20416 || vpos >= w->current_matrix->nrows
20417 || hpos >= w->current_matrix->matrix_w)
20418 return;
20419
20420 /* If cursor is off and we want it off, return quickly. */
20421 if (!on && !w->phys_cursor_on_p)
20422 return;
20423
20424 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20425 /* If cursor row is not enabled, we don't really know where to
20426 display the cursor. */
20427 if (!glyph_row->enabled_p)
20428 {
20429 w->phys_cursor_on_p = 0;
20430 return;
20431 }
20432
20433 glyph = NULL;
20434 if (!glyph_row->exact_window_width_line_p
20435 || hpos < glyph_row->used[TEXT_AREA])
20436 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20437
20438 xassert (interrupt_input_blocked);
20439
20440 /* Set new_cursor_type to the cursor we want to be displayed. */
20441 new_cursor_type = get_window_cursor_type (w, glyph,
20442 &new_cursor_width, &active_cursor);
20443
20444 /* If cursor is currently being shown and we don't want it to be or
20445 it is in the wrong place, or the cursor type is not what we want,
20446 erase it. */
20447 if (w->phys_cursor_on_p
20448 && (!on
20449 || w->phys_cursor.x != x
20450 || w->phys_cursor.y != y
20451 || new_cursor_type != w->phys_cursor_type
20452 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20453 && new_cursor_width != w->phys_cursor_width)))
20454 erase_phys_cursor (w);
20455
20456 /* Don't check phys_cursor_on_p here because that flag is only set
20457 to zero in some cases where we know that the cursor has been
20458 completely erased, to avoid the extra work of erasing the cursor
20459 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20460 still not be visible, or it has only been partly erased. */
20461 if (on)
20462 {
20463 w->phys_cursor_ascent = glyph_row->ascent;
20464 w->phys_cursor_height = glyph_row->height;
20465
20466 /* Set phys_cursor_.* before x_draw_.* is called because some
20467 of them may need the information. */
20468 w->phys_cursor.x = x;
20469 w->phys_cursor.y = glyph_row->y;
20470 w->phys_cursor.hpos = hpos;
20471 w->phys_cursor.vpos = vpos;
20472 }
20473
20474 rif->draw_window_cursor (w, glyph_row, x, y,
20475 new_cursor_type, new_cursor_width,
20476 on, active_cursor);
20477 }
20478
20479
20480 /* Switch the display of W's cursor on or off, according to the value
20481 of ON. */
20482
20483 static void
20484 update_window_cursor (w, on)
20485 struct window *w;
20486 int on;
20487 {
20488 /* Don't update cursor in windows whose frame is in the process
20489 of being deleted. */
20490 if (w->current_matrix)
20491 {
20492 BLOCK_INPUT;
20493 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20494 w->phys_cursor.x, w->phys_cursor.y);
20495 UNBLOCK_INPUT;
20496 }
20497 }
20498
20499
20500 /* Call update_window_cursor with parameter ON_P on all leaf windows
20501 in the window tree rooted at W. */
20502
20503 static void
20504 update_cursor_in_window_tree (w, on_p)
20505 struct window *w;
20506 int on_p;
20507 {
20508 while (w)
20509 {
20510 if (!NILP (w->hchild))
20511 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20512 else if (!NILP (w->vchild))
20513 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20514 else
20515 update_window_cursor (w, on_p);
20516
20517 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20518 }
20519 }
20520
20521
20522 /* EXPORT:
20523 Display the cursor on window W, or clear it, according to ON_P.
20524 Don't change the cursor's position. */
20525
20526 void
20527 x_update_cursor (f, on_p)
20528 struct frame *f;
20529 int on_p;
20530 {
20531 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20532 }
20533
20534
20535 /* EXPORT:
20536 Clear the cursor of window W to background color, and mark the
20537 cursor as not shown. This is used when the text where the cursor
20538 is is about to be rewritten. */
20539
20540 void
20541 x_clear_cursor (w)
20542 struct window *w;
20543 {
20544 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20545 update_window_cursor (w, 0);
20546 }
20547
20548
20549 /* EXPORT:
20550 Display the active region described by mouse_face_* according to DRAW. */
20551
20552 void
20553 show_mouse_face (dpyinfo, draw)
20554 Display_Info *dpyinfo;
20555 enum draw_glyphs_face draw;
20556 {
20557 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20558 struct frame *f = XFRAME (WINDOW_FRAME (w));
20559
20560 if (/* If window is in the process of being destroyed, don't bother
20561 to do anything. */
20562 w->current_matrix != NULL
20563 /* Don't update mouse highlight if hidden */
20564 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20565 /* Recognize when we are called to operate on rows that don't exist
20566 anymore. This can happen when a window is split. */
20567 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20568 {
20569 int phys_cursor_on_p = w->phys_cursor_on_p;
20570 struct glyph_row *row, *first, *last;
20571
20572 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20573 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20574
20575 for (row = first; row <= last && row->enabled_p; ++row)
20576 {
20577 int start_hpos, end_hpos, start_x;
20578
20579 /* For all but the first row, the highlight starts at column 0. */
20580 if (row == first)
20581 {
20582 start_hpos = dpyinfo->mouse_face_beg_col;
20583 start_x = dpyinfo->mouse_face_beg_x;
20584 }
20585 else
20586 {
20587 start_hpos = 0;
20588 start_x = 0;
20589 }
20590
20591 if (row == last)
20592 end_hpos = dpyinfo->mouse_face_end_col;
20593 else
20594 end_hpos = row->used[TEXT_AREA];
20595
20596 if (end_hpos > start_hpos)
20597 {
20598 draw_glyphs (w, start_x, row, TEXT_AREA,
20599 start_hpos, end_hpos,
20600 draw, 0);
20601
20602 row->mouse_face_p
20603 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20604 }
20605 }
20606
20607 /* When we've written over the cursor, arrange for it to
20608 be displayed again. */
20609 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20610 {
20611 BLOCK_INPUT;
20612 display_and_set_cursor (w, 1,
20613 w->phys_cursor.hpos, w->phys_cursor.vpos,
20614 w->phys_cursor.x, w->phys_cursor.y);
20615 UNBLOCK_INPUT;
20616 }
20617 }
20618
20619 /* Change the mouse cursor. */
20620 if (draw == DRAW_NORMAL_TEXT)
20621 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20622 else if (draw == DRAW_MOUSE_FACE)
20623 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20624 else
20625 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20626 }
20627
20628 /* EXPORT:
20629 Clear out the mouse-highlighted active region.
20630 Redraw it un-highlighted first. Value is non-zero if mouse
20631 face was actually drawn unhighlighted. */
20632
20633 int
20634 clear_mouse_face (dpyinfo)
20635 Display_Info *dpyinfo;
20636 {
20637 int cleared = 0;
20638
20639 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20640 {
20641 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20642 cleared = 1;
20643 }
20644
20645 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20646 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20647 dpyinfo->mouse_face_window = Qnil;
20648 dpyinfo->mouse_face_overlay = Qnil;
20649 return cleared;
20650 }
20651
20652
20653 /* EXPORT:
20654 Non-zero if physical cursor of window W is within mouse face. */
20655
20656 int
20657 cursor_in_mouse_face_p (w)
20658 struct window *w;
20659 {
20660 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20661 int in_mouse_face = 0;
20662
20663 if (WINDOWP (dpyinfo->mouse_face_window)
20664 && XWINDOW (dpyinfo->mouse_face_window) == w)
20665 {
20666 int hpos = w->phys_cursor.hpos;
20667 int vpos = w->phys_cursor.vpos;
20668
20669 if (vpos >= dpyinfo->mouse_face_beg_row
20670 && vpos <= dpyinfo->mouse_face_end_row
20671 && (vpos > dpyinfo->mouse_face_beg_row
20672 || hpos >= dpyinfo->mouse_face_beg_col)
20673 && (vpos < dpyinfo->mouse_face_end_row
20674 || hpos < dpyinfo->mouse_face_end_col
20675 || dpyinfo->mouse_face_past_end))
20676 in_mouse_face = 1;
20677 }
20678
20679 return in_mouse_face;
20680 }
20681
20682
20683
20684 \f
20685 /* Find the glyph matrix position of buffer position CHARPOS in window
20686 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20687 current glyphs must be up to date. If CHARPOS is above window
20688 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20689 of last line in W. In the row containing CHARPOS, stop before glyphs
20690 having STOP as object. */
20691
20692 #if 1 /* This is a version of fast_find_position that's more correct
20693 in the presence of hscrolling, for example. I didn't install
20694 it right away because the problem fixed is minor, it failed
20695 in 20.x as well, and I think it's too risky to install
20696 so near the release of 21.1. 2001-09-25 gerd. */
20697
20698 static int
20699 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20700 struct window *w;
20701 int charpos;
20702 int *hpos, *vpos, *x, *y;
20703 Lisp_Object stop;
20704 {
20705 struct glyph_row *row, *first;
20706 struct glyph *glyph, *end;
20707 int past_end = 0;
20708
20709 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20710 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20711 {
20712 *x = first->x;
20713 *y = first->y;
20714 *hpos = 0;
20715 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20716 return 1;
20717 }
20718
20719 row = row_containing_pos (w, charpos, first, NULL, 0);
20720 if (row == NULL)
20721 {
20722 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20723 past_end = 1;
20724 }
20725
20726 /* If whole rows or last part of a row came from a display overlay,
20727 row_containing_pos will skip over such rows because their end pos
20728 equals the start pos of the overlay or interval. Backtrack if we
20729 have a STOP object and previous row's end glyph came from STOP. */
20730 if (!NILP (stop))
20731 {
20732 struct glyph_row *prev = row-1;
20733 while ((prev = row - 1, prev >= first)
20734 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20735 && prev->used[TEXT_AREA] > 0)
20736 {
20737 end = prev->glyphs[TEXT_AREA];
20738 glyph = end + prev->used[TEXT_AREA];
20739 while (--glyph >= end
20740 && INTEGERP (glyph->object));
20741 if (glyph >= end
20742 && !EQ (stop, glyph->object))
20743 break;
20744 row = prev;
20745 }
20746 }
20747
20748 *x = row->x;
20749 *y = row->y;
20750 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20751
20752 glyph = row->glyphs[TEXT_AREA];
20753 end = glyph + row->used[TEXT_AREA];
20754
20755 /* Skip over glyphs not having an object at the start of the row.
20756 These are special glyphs like truncation marks on terminal
20757 frames. */
20758 if (row->displays_text_p)
20759 while (glyph < end
20760 && INTEGERP (glyph->object)
20761 && !EQ (stop, glyph->object)
20762 && glyph->charpos < 0)
20763 {
20764 *x += glyph->pixel_width;
20765 ++glyph;
20766 }
20767
20768 while (glyph < end
20769 && !INTEGERP (glyph->object)
20770 && !EQ (stop, glyph->object)
20771 && (!BUFFERP (glyph->object)
20772 || glyph->charpos < charpos))
20773 {
20774 *x += glyph->pixel_width;
20775 ++glyph;
20776 }
20777
20778 *hpos = glyph - row->glyphs[TEXT_AREA];
20779 return !past_end;
20780 }
20781
20782 #else /* not 1 */
20783
20784 static int
20785 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20786 struct window *w;
20787 int pos;
20788 int *hpos, *vpos, *x, *y;
20789 Lisp_Object stop;
20790 {
20791 int i;
20792 int lastcol;
20793 int maybe_next_line_p = 0;
20794 int line_start_position;
20795 int yb = window_text_bottom_y (w);
20796 struct glyph_row *row, *best_row;
20797 int row_vpos, best_row_vpos;
20798 int current_x;
20799
20800 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20801 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20802
20803 while (row->y < yb)
20804 {
20805 if (row->used[TEXT_AREA])
20806 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20807 else
20808 line_start_position = 0;
20809
20810 if (line_start_position > pos)
20811 break;
20812 /* If the position sought is the end of the buffer,
20813 don't include the blank lines at the bottom of the window. */
20814 else if (line_start_position == pos
20815 && pos == BUF_ZV (XBUFFER (w->buffer)))
20816 {
20817 maybe_next_line_p = 1;
20818 break;
20819 }
20820 else if (line_start_position > 0)
20821 {
20822 best_row = row;
20823 best_row_vpos = row_vpos;
20824 }
20825
20826 if (row->y + row->height >= yb)
20827 break;
20828
20829 ++row;
20830 ++row_vpos;
20831 }
20832
20833 /* Find the right column within BEST_ROW. */
20834 lastcol = 0;
20835 current_x = best_row->x;
20836 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20837 {
20838 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20839 int charpos = glyph->charpos;
20840
20841 if (BUFFERP (glyph->object))
20842 {
20843 if (charpos == pos)
20844 {
20845 *hpos = i;
20846 *vpos = best_row_vpos;
20847 *x = current_x;
20848 *y = best_row->y;
20849 return 1;
20850 }
20851 else if (charpos > pos)
20852 break;
20853 }
20854 else if (EQ (glyph->object, stop))
20855 break;
20856
20857 if (charpos > 0)
20858 lastcol = i;
20859 current_x += glyph->pixel_width;
20860 }
20861
20862 /* If we're looking for the end of the buffer,
20863 and we didn't find it in the line we scanned,
20864 use the start of the following line. */
20865 if (maybe_next_line_p)
20866 {
20867 ++best_row;
20868 ++best_row_vpos;
20869 lastcol = 0;
20870 current_x = best_row->x;
20871 }
20872
20873 *vpos = best_row_vpos;
20874 *hpos = lastcol + 1;
20875 *x = current_x;
20876 *y = best_row->y;
20877 return 0;
20878 }
20879
20880 #endif /* not 1 */
20881
20882
20883 /* Find the position of the glyph for position POS in OBJECT in
20884 window W's current matrix, and return in *X, *Y the pixel
20885 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20886
20887 RIGHT_P non-zero means return the position of the right edge of the
20888 glyph, RIGHT_P zero means return the left edge position.
20889
20890 If no glyph for POS exists in the matrix, return the position of
20891 the glyph with the next smaller position that is in the matrix, if
20892 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20893 exists in the matrix, return the position of the glyph with the
20894 next larger position in OBJECT.
20895
20896 Value is non-zero if a glyph was found. */
20897
20898 static int
20899 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20900 struct window *w;
20901 int pos;
20902 Lisp_Object object;
20903 int *hpos, *vpos, *x, *y;
20904 int right_p;
20905 {
20906 int yb = window_text_bottom_y (w);
20907 struct glyph_row *r;
20908 struct glyph *best_glyph = NULL;
20909 struct glyph_row *best_row = NULL;
20910 int best_x = 0;
20911
20912 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20913 r->enabled_p && r->y < yb;
20914 ++r)
20915 {
20916 struct glyph *g = r->glyphs[TEXT_AREA];
20917 struct glyph *e = g + r->used[TEXT_AREA];
20918 int gx;
20919
20920 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20921 if (EQ (g->object, object))
20922 {
20923 if (g->charpos == pos)
20924 {
20925 best_glyph = g;
20926 best_x = gx;
20927 best_row = r;
20928 goto found;
20929 }
20930 else if (best_glyph == NULL
20931 || ((abs (g->charpos - pos)
20932 < abs (best_glyph->charpos - pos))
20933 && (right_p
20934 ? g->charpos < pos
20935 : g->charpos > pos)))
20936 {
20937 best_glyph = g;
20938 best_x = gx;
20939 best_row = r;
20940 }
20941 }
20942 }
20943
20944 found:
20945
20946 if (best_glyph)
20947 {
20948 *x = best_x;
20949 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20950
20951 if (right_p)
20952 {
20953 *x += best_glyph->pixel_width;
20954 ++*hpos;
20955 }
20956
20957 *y = best_row->y;
20958 *vpos = best_row - w->current_matrix->rows;
20959 }
20960
20961 return best_glyph != NULL;
20962 }
20963
20964
20965 /* See if position X, Y is within a hot-spot of an image. */
20966
20967 static int
20968 on_hot_spot_p (hot_spot, x, y)
20969 Lisp_Object hot_spot;
20970 int x, y;
20971 {
20972 if (!CONSP (hot_spot))
20973 return 0;
20974
20975 if (EQ (XCAR (hot_spot), Qrect))
20976 {
20977 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20978 Lisp_Object rect = XCDR (hot_spot);
20979 Lisp_Object tem;
20980 if (!CONSP (rect))
20981 return 0;
20982 if (!CONSP (XCAR (rect)))
20983 return 0;
20984 if (!CONSP (XCDR (rect)))
20985 return 0;
20986 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20987 return 0;
20988 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20989 return 0;
20990 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20991 return 0;
20992 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20993 return 0;
20994 return 1;
20995 }
20996 else if (EQ (XCAR (hot_spot), Qcircle))
20997 {
20998 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20999 Lisp_Object circ = XCDR (hot_spot);
21000 Lisp_Object lr, lx0, ly0;
21001 if (CONSP (circ)
21002 && CONSP (XCAR (circ))
21003 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21004 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21005 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21006 {
21007 double r = XFLOATINT (lr);
21008 double dx = XINT (lx0) - x;
21009 double dy = XINT (ly0) - y;
21010 return (dx * dx + dy * dy <= r * r);
21011 }
21012 }
21013 else if (EQ (XCAR (hot_spot), Qpoly))
21014 {
21015 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21016 if (VECTORP (XCDR (hot_spot)))
21017 {
21018 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21019 Lisp_Object *poly = v->contents;
21020 int n = v->size;
21021 int i;
21022 int inside = 0;
21023 Lisp_Object lx, ly;
21024 int x0, y0;
21025
21026 /* Need an even number of coordinates, and at least 3 edges. */
21027 if (n < 6 || n & 1)
21028 return 0;
21029
21030 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21031 If count is odd, we are inside polygon. Pixels on edges
21032 may or may not be included depending on actual geometry of the
21033 polygon. */
21034 if ((lx = poly[n-2], !INTEGERP (lx))
21035 || (ly = poly[n-1], !INTEGERP (lx)))
21036 return 0;
21037 x0 = XINT (lx), y0 = XINT (ly);
21038 for (i = 0; i < n; i += 2)
21039 {
21040 int x1 = x0, y1 = y0;
21041 if ((lx = poly[i], !INTEGERP (lx))
21042 || (ly = poly[i+1], !INTEGERP (ly)))
21043 return 0;
21044 x0 = XINT (lx), y0 = XINT (ly);
21045
21046 /* Does this segment cross the X line? */
21047 if (x0 >= x)
21048 {
21049 if (x1 >= x)
21050 continue;
21051 }
21052 else if (x1 < x)
21053 continue;
21054 if (y > y0 && y > y1)
21055 continue;
21056 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21057 inside = !inside;
21058 }
21059 return inside;
21060 }
21061 }
21062 /* If we don't understand the format, pretend we're not in the hot-spot. */
21063 return 0;
21064 }
21065
21066 Lisp_Object
21067 find_hot_spot (map, x, y)
21068 Lisp_Object map;
21069 int x, y;
21070 {
21071 while (CONSP (map))
21072 {
21073 if (CONSP (XCAR (map))
21074 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21075 return XCAR (map);
21076 map = XCDR (map);
21077 }
21078
21079 return Qnil;
21080 }
21081
21082 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21083 3, 3, 0,
21084 doc: /* Lookup in image map MAP coordinates X and Y.
21085 An image map is an alist where each element has the format (AREA ID PLIST).
21086 An AREA is specified as either a rectangle, a circle, or a polygon:
21087 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21088 pixel coordinates of the upper left and bottom right corners.
21089 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21090 and the radius of the circle; r may be a float or integer.
21091 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21092 vector describes one corner in the polygon.
21093 Returns the alist element for the first matching AREA in MAP. */)
21094 (map, x, y)
21095 Lisp_Object map;
21096 Lisp_Object x, y;
21097 {
21098 if (NILP (map))
21099 return Qnil;
21100
21101 CHECK_NUMBER (x);
21102 CHECK_NUMBER (y);
21103
21104 return find_hot_spot (map, XINT (x), XINT (y));
21105 }
21106
21107
21108 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21109 static void
21110 define_frame_cursor1 (f, cursor, pointer)
21111 struct frame *f;
21112 Cursor cursor;
21113 Lisp_Object pointer;
21114 {
21115 /* Do not change cursor shape while dragging mouse. */
21116 if (!NILP (do_mouse_tracking))
21117 return;
21118
21119 if (!NILP (pointer))
21120 {
21121 if (EQ (pointer, Qarrow))
21122 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21123 else if (EQ (pointer, Qhand))
21124 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21125 else if (EQ (pointer, Qtext))
21126 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21127 else if (EQ (pointer, intern ("hdrag")))
21128 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21129 #ifdef HAVE_X_WINDOWS
21130 else if (EQ (pointer, intern ("vdrag")))
21131 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21132 #endif
21133 else if (EQ (pointer, intern ("hourglass")))
21134 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21135 else if (EQ (pointer, Qmodeline))
21136 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21137 else
21138 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21139 }
21140
21141 if (cursor != No_Cursor)
21142 rif->define_frame_cursor (f, cursor);
21143 }
21144
21145 /* Take proper action when mouse has moved to the mode or header line
21146 or marginal area AREA of window W, x-position X and y-position Y.
21147 X is relative to the start of the text display area of W, so the
21148 width of bitmap areas and scroll bars must be subtracted to get a
21149 position relative to the start of the mode line. */
21150
21151 static void
21152 note_mode_line_or_margin_highlight (w, x, y, area)
21153 struct window *w;
21154 int x, y;
21155 enum window_part area;
21156 {
21157 struct frame *f = XFRAME (w->frame);
21158 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21159 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21160 Lisp_Object pointer = Qnil;
21161 int charpos, dx, dy, width, height;
21162 Lisp_Object string, object = Qnil;
21163 Lisp_Object pos, help;
21164
21165 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21166 string = mode_line_string (w, area, &x, &y, &charpos,
21167 &object, &dx, &dy, &width, &height);
21168 else
21169 {
21170 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21171 string = marginal_area_string (w, area, &x, &y, &charpos,
21172 &object, &dx, &dy, &width, &height);
21173 }
21174
21175 help = Qnil;
21176
21177 if (IMAGEP (object))
21178 {
21179 Lisp_Object image_map, hotspot;
21180 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
21181 !NILP (image_map))
21182 && (hotspot = find_hot_spot (image_map, dx, dy),
21183 CONSP (hotspot))
21184 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21185 {
21186 Lisp_Object area_id, plist;
21187
21188 area_id = XCAR (hotspot);
21189 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21190 If so, we could look for mouse-enter, mouse-leave
21191 properties in PLIST (and do something...). */
21192 hotspot = XCDR (hotspot);
21193 if (CONSP (hotspot)
21194 && (plist = XCAR (hotspot), CONSP (plist)))
21195 {
21196 pointer = Fsafe_plist_get (plist, Qpointer);
21197 if (NILP (pointer))
21198 pointer = Qhand;
21199 help = Fsafe_plist_get (plist, Qhelp_echo);
21200 if (!NILP (help))
21201 {
21202 help_echo_string = help;
21203 /* Is this correct? ++kfs */
21204 XSETWINDOW (help_echo_window, w);
21205 help_echo_object = w->buffer;
21206 help_echo_pos = charpos;
21207 }
21208 }
21209 }
21210 if (NILP (pointer))
21211 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21212 }
21213
21214 if (STRINGP (string))
21215 {
21216 pos = make_number (charpos);
21217 /* If we're on a string with `help-echo' text property, arrange
21218 for the help to be displayed. This is done by setting the
21219 global variable help_echo_string to the help string. */
21220 if (NILP (help))
21221 {
21222 help = Fget_text_property (pos, Qhelp_echo, string);
21223 if (!NILP (help))
21224 {
21225 help_echo_string = help;
21226 XSETWINDOW (help_echo_window, w);
21227 help_echo_object = string;
21228 help_echo_pos = charpos;
21229 }
21230 }
21231
21232 if (NILP (pointer))
21233 pointer = Fget_text_property (pos, Qpointer, string);
21234
21235 /* Change the mouse pointer according to what is under X/Y. */
21236 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21237 {
21238 Lisp_Object map;
21239 map = Fget_text_property (pos, Qlocal_map, string);
21240 if (!KEYMAPP (map))
21241 map = Fget_text_property (pos, Qkeymap, string);
21242 if (!KEYMAPP (map))
21243 cursor = dpyinfo->vertical_scroll_bar_cursor;
21244 }
21245 }
21246
21247 define_frame_cursor1 (f, cursor, pointer);
21248 }
21249
21250
21251 /* EXPORT:
21252 Take proper action when the mouse has moved to position X, Y on
21253 frame F as regards highlighting characters that have mouse-face
21254 properties. Also de-highlighting chars where the mouse was before.
21255 X and Y can be negative or out of range. */
21256
21257 void
21258 note_mouse_highlight (f, x, y)
21259 struct frame *f;
21260 int x, y;
21261 {
21262 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21263 enum window_part part;
21264 Lisp_Object window;
21265 struct window *w;
21266 Cursor cursor = No_Cursor;
21267 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21268 struct buffer *b;
21269
21270 /* When a menu is active, don't highlight because this looks odd. */
21271 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21272 if (popup_activated ())
21273 return;
21274 #endif
21275
21276 if (NILP (Vmouse_highlight)
21277 || !f->glyphs_initialized_p)
21278 return;
21279
21280 dpyinfo->mouse_face_mouse_x = x;
21281 dpyinfo->mouse_face_mouse_y = y;
21282 dpyinfo->mouse_face_mouse_frame = f;
21283
21284 if (dpyinfo->mouse_face_defer)
21285 return;
21286
21287 if (gc_in_progress)
21288 {
21289 dpyinfo->mouse_face_deferred_gc = 1;
21290 return;
21291 }
21292
21293 /* Which window is that in? */
21294 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21295
21296 /* If we were displaying active text in another window, clear that.
21297 Also clear if we move out of text area in same window. */
21298 if (! EQ (window, dpyinfo->mouse_face_window)
21299 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21300 clear_mouse_face (dpyinfo);
21301
21302 /* Not on a window -> return. */
21303 if (!WINDOWP (window))
21304 return;
21305
21306 /* Reset help_echo_string. It will get recomputed below. */
21307 help_echo_string = Qnil;
21308
21309 /* Convert to window-relative pixel coordinates. */
21310 w = XWINDOW (window);
21311 frame_to_window_pixel_xy (w, &x, &y);
21312
21313 /* Handle tool-bar window differently since it doesn't display a
21314 buffer. */
21315 if (EQ (window, f->tool_bar_window))
21316 {
21317 note_tool_bar_highlight (f, x, y);
21318 return;
21319 }
21320
21321 /* Mouse is on the mode, header line or margin? */
21322 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21323 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21324 {
21325 note_mode_line_or_margin_highlight (w, x, y, part);
21326 return;
21327 }
21328
21329 if (part == ON_VERTICAL_BORDER)
21330 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21331 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21332 || part == ON_SCROLL_BAR)
21333 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21334 else
21335 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21336
21337 /* Are we in a window whose display is up to date?
21338 And verify the buffer's text has not changed. */
21339 b = XBUFFER (w->buffer);
21340 if (part == ON_TEXT
21341 && EQ (w->window_end_valid, w->buffer)
21342 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21343 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21344 {
21345 int hpos, vpos, pos, i, dx, dy, area;
21346 struct glyph *glyph;
21347 Lisp_Object object;
21348 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21349 Lisp_Object *overlay_vec = NULL;
21350 int noverlays;
21351 struct buffer *obuf;
21352 int obegv, ozv, same_region;
21353
21354 /* Find the glyph under X/Y. */
21355 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21356
21357 /* Look for :pointer property on image. */
21358 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21359 {
21360 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21361 if (img != NULL && IMAGEP (img->spec))
21362 {
21363 Lisp_Object image_map, hotspot;
21364 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21365 !NILP (image_map))
21366 && (hotspot = find_hot_spot (image_map,
21367 glyph->slice.x + dx,
21368 glyph->slice.y + dy),
21369 CONSP (hotspot))
21370 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21371 {
21372 Lisp_Object area_id, plist;
21373
21374 area_id = XCAR (hotspot);
21375 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21376 If so, we could look for mouse-enter, mouse-leave
21377 properties in PLIST (and do something...). */
21378 hotspot = XCDR (hotspot);
21379 if (CONSP (hotspot)
21380 && (plist = XCAR (hotspot), CONSP (plist)))
21381 {
21382 pointer = Fsafe_plist_get (plist, Qpointer);
21383 if (NILP (pointer))
21384 pointer = Qhand;
21385 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21386 if (!NILP (help_echo_string))
21387 {
21388 help_echo_window = window;
21389 help_echo_object = glyph->object;
21390 help_echo_pos = glyph->charpos;
21391 }
21392 }
21393 }
21394 if (NILP (pointer))
21395 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21396 }
21397 }
21398
21399 /* Clear mouse face if X/Y not over text. */
21400 if (glyph == NULL
21401 || area != TEXT_AREA
21402 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21403 {
21404 if (clear_mouse_face (dpyinfo))
21405 cursor = No_Cursor;
21406 if (NILP (pointer))
21407 {
21408 if (area != TEXT_AREA)
21409 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21410 else
21411 pointer = Vvoid_text_area_pointer;
21412 }
21413 goto set_cursor;
21414 }
21415
21416 pos = glyph->charpos;
21417 object = glyph->object;
21418 if (!STRINGP (object) && !BUFFERP (object))
21419 goto set_cursor;
21420
21421 /* If we get an out-of-range value, return now; avoid an error. */
21422 if (BUFFERP (object) && pos > BUF_Z (b))
21423 goto set_cursor;
21424
21425 /* Make the window's buffer temporarily current for
21426 overlays_at and compute_char_face. */
21427 obuf = current_buffer;
21428 current_buffer = b;
21429 obegv = BEGV;
21430 ozv = ZV;
21431 BEGV = BEG;
21432 ZV = Z;
21433
21434 /* Is this char mouse-active or does it have help-echo? */
21435 position = make_number (pos);
21436
21437 if (BUFFERP (object))
21438 {
21439 /* Put all the overlays we want in a vector in overlay_vec. */
21440 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21441 /* Sort overlays into increasing priority order. */
21442 noverlays = sort_overlays (overlay_vec, noverlays, w);
21443 }
21444 else
21445 noverlays = 0;
21446
21447 same_region = (EQ (window, dpyinfo->mouse_face_window)
21448 && vpos >= dpyinfo->mouse_face_beg_row
21449 && vpos <= dpyinfo->mouse_face_end_row
21450 && (vpos > dpyinfo->mouse_face_beg_row
21451 || hpos >= dpyinfo->mouse_face_beg_col)
21452 && (vpos < dpyinfo->mouse_face_end_row
21453 || hpos < dpyinfo->mouse_face_end_col
21454 || dpyinfo->mouse_face_past_end));
21455
21456 if (same_region)
21457 cursor = No_Cursor;
21458
21459 /* Check mouse-face highlighting. */
21460 if (! same_region
21461 /* If there exists an overlay with mouse-face overlapping
21462 the one we are currently highlighting, we have to
21463 check if we enter the overlapping overlay, and then
21464 highlight only that. */
21465 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21466 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21467 {
21468 /* Find the highest priority overlay that has a mouse-face
21469 property. */
21470 overlay = Qnil;
21471 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21472 {
21473 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21474 if (!NILP (mouse_face))
21475 overlay = overlay_vec[i];
21476 }
21477
21478 /* If we're actually highlighting the same overlay as
21479 before, there's no need to do that again. */
21480 if (!NILP (overlay)
21481 && EQ (overlay, dpyinfo->mouse_face_overlay))
21482 goto check_help_echo;
21483
21484 dpyinfo->mouse_face_overlay = overlay;
21485
21486 /* Clear the display of the old active region, if any. */
21487 if (clear_mouse_face (dpyinfo))
21488 cursor = No_Cursor;
21489
21490 /* If no overlay applies, get a text property. */
21491 if (NILP (overlay))
21492 mouse_face = Fget_text_property (position, Qmouse_face, object);
21493
21494 /* Handle the overlay case. */
21495 if (!NILP (overlay))
21496 {
21497 /* Find the range of text around this char that
21498 should be active. */
21499 Lisp_Object before, after;
21500 int ignore;
21501
21502 before = Foverlay_start (overlay);
21503 after = Foverlay_end (overlay);
21504 /* Record this as the current active region. */
21505 fast_find_position (w, XFASTINT (before),
21506 &dpyinfo->mouse_face_beg_col,
21507 &dpyinfo->mouse_face_beg_row,
21508 &dpyinfo->mouse_face_beg_x,
21509 &dpyinfo->mouse_face_beg_y, Qnil);
21510
21511 dpyinfo->mouse_face_past_end
21512 = !fast_find_position (w, XFASTINT (after),
21513 &dpyinfo->mouse_face_end_col,
21514 &dpyinfo->mouse_face_end_row,
21515 &dpyinfo->mouse_face_end_x,
21516 &dpyinfo->mouse_face_end_y, Qnil);
21517 dpyinfo->mouse_face_window = window;
21518
21519 dpyinfo->mouse_face_face_id
21520 = face_at_buffer_position (w, pos, 0, 0,
21521 &ignore, pos + 1,
21522 !dpyinfo->mouse_face_hidden);
21523
21524 /* Display it as active. */
21525 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21526 cursor = No_Cursor;
21527 }
21528 /* Handle the text property case. */
21529 else if (!NILP (mouse_face) && BUFFERP (object))
21530 {
21531 /* Find the range of text around this char that
21532 should be active. */
21533 Lisp_Object before, after, beginning, end;
21534 int ignore;
21535
21536 beginning = Fmarker_position (w->start);
21537 end = make_number (BUF_Z (XBUFFER (object))
21538 - XFASTINT (w->window_end_pos));
21539 before
21540 = Fprevious_single_property_change (make_number (pos + 1),
21541 Qmouse_face,
21542 object, beginning);
21543 after
21544 = Fnext_single_property_change (position, Qmouse_face,
21545 object, end);
21546
21547 /* Record this as the current active region. */
21548 fast_find_position (w, XFASTINT (before),
21549 &dpyinfo->mouse_face_beg_col,
21550 &dpyinfo->mouse_face_beg_row,
21551 &dpyinfo->mouse_face_beg_x,
21552 &dpyinfo->mouse_face_beg_y, Qnil);
21553 dpyinfo->mouse_face_past_end
21554 = !fast_find_position (w, XFASTINT (after),
21555 &dpyinfo->mouse_face_end_col,
21556 &dpyinfo->mouse_face_end_row,
21557 &dpyinfo->mouse_face_end_x,
21558 &dpyinfo->mouse_face_end_y, Qnil);
21559 dpyinfo->mouse_face_window = window;
21560
21561 if (BUFFERP (object))
21562 dpyinfo->mouse_face_face_id
21563 = face_at_buffer_position (w, pos, 0, 0,
21564 &ignore, pos + 1,
21565 !dpyinfo->mouse_face_hidden);
21566
21567 /* Display it as active. */
21568 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21569 cursor = No_Cursor;
21570 }
21571 else if (!NILP (mouse_face) && STRINGP (object))
21572 {
21573 Lisp_Object b, e;
21574 int ignore;
21575
21576 b = Fprevious_single_property_change (make_number (pos + 1),
21577 Qmouse_face,
21578 object, Qnil);
21579 e = Fnext_single_property_change (position, Qmouse_face,
21580 object, Qnil);
21581 if (NILP (b))
21582 b = make_number (0);
21583 if (NILP (e))
21584 e = make_number (SCHARS (object) - 1);
21585 fast_find_string_pos (w, XINT (b), object,
21586 &dpyinfo->mouse_face_beg_col,
21587 &dpyinfo->mouse_face_beg_row,
21588 &dpyinfo->mouse_face_beg_x,
21589 &dpyinfo->mouse_face_beg_y, 0);
21590 fast_find_string_pos (w, XINT (e), object,
21591 &dpyinfo->mouse_face_end_col,
21592 &dpyinfo->mouse_face_end_row,
21593 &dpyinfo->mouse_face_end_x,
21594 &dpyinfo->mouse_face_end_y, 1);
21595 dpyinfo->mouse_face_past_end = 0;
21596 dpyinfo->mouse_face_window = window;
21597 dpyinfo->mouse_face_face_id
21598 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21599 glyph->face_id, 1);
21600 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21601 cursor = No_Cursor;
21602 }
21603 else if (STRINGP (object) && NILP (mouse_face))
21604 {
21605 /* A string which doesn't have mouse-face, but
21606 the text ``under'' it might have. */
21607 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21608 int start = MATRIX_ROW_START_CHARPOS (r);
21609
21610 pos = string_buffer_position (w, object, start);
21611 if (pos > 0)
21612 mouse_face = get_char_property_and_overlay (make_number (pos),
21613 Qmouse_face,
21614 w->buffer,
21615 &overlay);
21616 if (!NILP (mouse_face) && !NILP (overlay))
21617 {
21618 Lisp_Object before = Foverlay_start (overlay);
21619 Lisp_Object after = Foverlay_end (overlay);
21620 int ignore;
21621
21622 /* Note that we might not be able to find position
21623 BEFORE in the glyph matrix if the overlay is
21624 entirely covered by a `display' property. In
21625 this case, we overshoot. So let's stop in
21626 the glyph matrix before glyphs for OBJECT. */
21627 fast_find_position (w, XFASTINT (before),
21628 &dpyinfo->mouse_face_beg_col,
21629 &dpyinfo->mouse_face_beg_row,
21630 &dpyinfo->mouse_face_beg_x,
21631 &dpyinfo->mouse_face_beg_y,
21632 object);
21633
21634 dpyinfo->mouse_face_past_end
21635 = !fast_find_position (w, XFASTINT (after),
21636 &dpyinfo->mouse_face_end_col,
21637 &dpyinfo->mouse_face_end_row,
21638 &dpyinfo->mouse_face_end_x,
21639 &dpyinfo->mouse_face_end_y,
21640 Qnil);
21641 dpyinfo->mouse_face_window = window;
21642 dpyinfo->mouse_face_face_id
21643 = face_at_buffer_position (w, pos, 0, 0,
21644 &ignore, pos + 1,
21645 !dpyinfo->mouse_face_hidden);
21646
21647 /* Display it as active. */
21648 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21649 cursor = No_Cursor;
21650 }
21651 }
21652 }
21653
21654 check_help_echo:
21655
21656 /* Look for a `help-echo' property. */
21657 if (NILP (help_echo_string)) {
21658 Lisp_Object help, overlay;
21659
21660 /* Check overlays first. */
21661 help = overlay = Qnil;
21662 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21663 {
21664 overlay = overlay_vec[i];
21665 help = Foverlay_get (overlay, Qhelp_echo);
21666 }
21667
21668 if (!NILP (help))
21669 {
21670 help_echo_string = help;
21671 help_echo_window = window;
21672 help_echo_object = overlay;
21673 help_echo_pos = pos;
21674 }
21675 else
21676 {
21677 Lisp_Object object = glyph->object;
21678 int charpos = glyph->charpos;
21679
21680 /* Try text properties. */
21681 if (STRINGP (object)
21682 && charpos >= 0
21683 && charpos < SCHARS (object))
21684 {
21685 help = Fget_text_property (make_number (charpos),
21686 Qhelp_echo, object);
21687 if (NILP (help))
21688 {
21689 /* If the string itself doesn't specify a help-echo,
21690 see if the buffer text ``under'' it does. */
21691 struct glyph_row *r
21692 = MATRIX_ROW (w->current_matrix, vpos);
21693 int start = MATRIX_ROW_START_CHARPOS (r);
21694 int pos = string_buffer_position (w, object, start);
21695 if (pos > 0)
21696 {
21697 help = Fget_char_property (make_number (pos),
21698 Qhelp_echo, w->buffer);
21699 if (!NILP (help))
21700 {
21701 charpos = pos;
21702 object = w->buffer;
21703 }
21704 }
21705 }
21706 }
21707 else if (BUFFERP (object)
21708 && charpos >= BEGV
21709 && charpos < ZV)
21710 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21711 object);
21712
21713 if (!NILP (help))
21714 {
21715 help_echo_string = help;
21716 help_echo_window = window;
21717 help_echo_object = object;
21718 help_echo_pos = charpos;
21719 }
21720 }
21721 }
21722
21723 /* Look for a `pointer' property. */
21724 if (NILP (pointer))
21725 {
21726 /* Check overlays first. */
21727 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21728 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21729
21730 if (NILP (pointer))
21731 {
21732 Lisp_Object object = glyph->object;
21733 int charpos = glyph->charpos;
21734
21735 /* Try text properties. */
21736 if (STRINGP (object)
21737 && charpos >= 0
21738 && charpos < SCHARS (object))
21739 {
21740 pointer = Fget_text_property (make_number (charpos),
21741 Qpointer, object);
21742 if (NILP (pointer))
21743 {
21744 /* If the string itself doesn't specify a pointer,
21745 see if the buffer text ``under'' it does. */
21746 struct glyph_row *r
21747 = MATRIX_ROW (w->current_matrix, vpos);
21748 int start = MATRIX_ROW_START_CHARPOS (r);
21749 int pos = string_buffer_position (w, object, start);
21750 if (pos > 0)
21751 pointer = Fget_char_property (make_number (pos),
21752 Qpointer, w->buffer);
21753 }
21754 }
21755 else if (BUFFERP (object)
21756 && charpos >= BEGV
21757 && charpos < ZV)
21758 pointer = Fget_text_property (make_number (charpos),
21759 Qpointer, object);
21760 }
21761 }
21762
21763 BEGV = obegv;
21764 ZV = ozv;
21765 current_buffer = obuf;
21766 }
21767
21768 set_cursor:
21769
21770 define_frame_cursor1 (f, cursor, pointer);
21771 }
21772
21773
21774 /* EXPORT for RIF:
21775 Clear any mouse-face on window W. This function is part of the
21776 redisplay interface, and is called from try_window_id and similar
21777 functions to ensure the mouse-highlight is off. */
21778
21779 void
21780 x_clear_window_mouse_face (w)
21781 struct window *w;
21782 {
21783 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21784 Lisp_Object window;
21785
21786 BLOCK_INPUT;
21787 XSETWINDOW (window, w);
21788 if (EQ (window, dpyinfo->mouse_face_window))
21789 clear_mouse_face (dpyinfo);
21790 UNBLOCK_INPUT;
21791 }
21792
21793
21794 /* EXPORT:
21795 Just discard the mouse face information for frame F, if any.
21796 This is used when the size of F is changed. */
21797
21798 void
21799 cancel_mouse_face (f)
21800 struct frame *f;
21801 {
21802 Lisp_Object window;
21803 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21804
21805 window = dpyinfo->mouse_face_window;
21806 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21807 {
21808 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21809 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21810 dpyinfo->mouse_face_window = Qnil;
21811 }
21812 }
21813
21814
21815 #endif /* HAVE_WINDOW_SYSTEM */
21816
21817 \f
21818 /***********************************************************************
21819 Exposure Events
21820 ***********************************************************************/
21821
21822 #ifdef HAVE_WINDOW_SYSTEM
21823
21824 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21825 which intersects rectangle R. R is in window-relative coordinates. */
21826
21827 static void
21828 expose_area (w, row, r, area)
21829 struct window *w;
21830 struct glyph_row *row;
21831 XRectangle *r;
21832 enum glyph_row_area area;
21833 {
21834 struct glyph *first = row->glyphs[area];
21835 struct glyph *end = row->glyphs[area] + row->used[area];
21836 struct glyph *last;
21837 int first_x, start_x, x;
21838
21839 if (area == TEXT_AREA && row->fill_line_p)
21840 /* If row extends face to end of line write the whole line. */
21841 draw_glyphs (w, 0, row, area,
21842 0, row->used[area],
21843 DRAW_NORMAL_TEXT, 0);
21844 else
21845 {
21846 /* Set START_X to the window-relative start position for drawing glyphs of
21847 AREA. The first glyph of the text area can be partially visible.
21848 The first glyphs of other areas cannot. */
21849 start_x = window_box_left_offset (w, area);
21850 x = start_x;
21851 if (area == TEXT_AREA)
21852 x += row->x;
21853
21854 /* Find the first glyph that must be redrawn. */
21855 while (first < end
21856 && x + first->pixel_width < r->x)
21857 {
21858 x += first->pixel_width;
21859 ++first;
21860 }
21861
21862 /* Find the last one. */
21863 last = first;
21864 first_x = x;
21865 while (last < end
21866 && x < r->x + r->width)
21867 {
21868 x += last->pixel_width;
21869 ++last;
21870 }
21871
21872 /* Repaint. */
21873 if (last > first)
21874 draw_glyphs (w, first_x - start_x, row, area,
21875 first - row->glyphs[area], last - row->glyphs[area],
21876 DRAW_NORMAL_TEXT, 0);
21877 }
21878 }
21879
21880
21881 /* Redraw the parts of the glyph row ROW on window W intersecting
21882 rectangle R. R is in window-relative coordinates. Value is
21883 non-zero if mouse-face was overwritten. */
21884
21885 static int
21886 expose_line (w, row, r)
21887 struct window *w;
21888 struct glyph_row *row;
21889 XRectangle *r;
21890 {
21891 xassert (row->enabled_p);
21892
21893 if (row->mode_line_p || w->pseudo_window_p)
21894 draw_glyphs (w, 0, row, TEXT_AREA,
21895 0, row->used[TEXT_AREA],
21896 DRAW_NORMAL_TEXT, 0);
21897 else
21898 {
21899 if (row->used[LEFT_MARGIN_AREA])
21900 expose_area (w, row, r, LEFT_MARGIN_AREA);
21901 if (row->used[TEXT_AREA])
21902 expose_area (w, row, r, TEXT_AREA);
21903 if (row->used[RIGHT_MARGIN_AREA])
21904 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21905 draw_row_fringe_bitmaps (w, row);
21906 }
21907
21908 return row->mouse_face_p;
21909 }
21910
21911
21912 /* Redraw those parts of glyphs rows during expose event handling that
21913 overlap other rows. Redrawing of an exposed line writes over parts
21914 of lines overlapping that exposed line; this function fixes that.
21915
21916 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21917 row in W's current matrix that is exposed and overlaps other rows.
21918 LAST_OVERLAPPING_ROW is the last such row. */
21919
21920 static void
21921 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21922 struct window *w;
21923 struct glyph_row *first_overlapping_row;
21924 struct glyph_row *last_overlapping_row;
21925 {
21926 struct glyph_row *row;
21927
21928 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21929 if (row->overlapping_p)
21930 {
21931 xassert (row->enabled_p && !row->mode_line_p);
21932
21933 if (row->used[LEFT_MARGIN_AREA])
21934 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21935
21936 if (row->used[TEXT_AREA])
21937 x_fix_overlapping_area (w, row, TEXT_AREA);
21938
21939 if (row->used[RIGHT_MARGIN_AREA])
21940 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21941 }
21942 }
21943
21944
21945 /* Return non-zero if W's cursor intersects rectangle R. */
21946
21947 static int
21948 phys_cursor_in_rect_p (w, r)
21949 struct window *w;
21950 XRectangle *r;
21951 {
21952 XRectangle cr, result;
21953 struct glyph *cursor_glyph;
21954
21955 cursor_glyph = get_phys_cursor_glyph (w);
21956 if (cursor_glyph)
21957 {
21958 /* r is relative to W's box, but w->phys_cursor.x is relative
21959 to left edge of W's TEXT area. Adjust it. */
21960 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21961 cr.y = w->phys_cursor.y;
21962 cr.width = cursor_glyph->pixel_width;
21963 cr.height = w->phys_cursor_height;
21964 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21965 I assume the effect is the same -- and this is portable. */
21966 return x_intersect_rectangles (&cr, r, &result);
21967 }
21968 else
21969 return 0;
21970 }
21971
21972
21973 /* EXPORT:
21974 Draw a vertical window border to the right of window W if W doesn't
21975 have vertical scroll bars. */
21976
21977 void
21978 x_draw_vertical_border (w)
21979 struct window *w;
21980 {
21981 /* We could do better, if we knew what type of scroll-bar the adjacent
21982 windows (on either side) have... But we don't :-(
21983 However, I think this works ok. ++KFS 2003-04-25 */
21984
21985 /* Redraw borders between horizontally adjacent windows. Don't
21986 do it for frames with vertical scroll bars because either the
21987 right scroll bar of a window, or the left scroll bar of its
21988 neighbor will suffice as a border. */
21989 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21990 return;
21991
21992 if (!WINDOW_RIGHTMOST_P (w)
21993 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21994 {
21995 int x0, x1, y0, y1;
21996
21997 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21998 y1 -= 1;
21999
22000 rif->draw_vertical_window_border (w, x1, y0, y1);
22001 }
22002 else if (!WINDOW_LEFTMOST_P (w)
22003 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22004 {
22005 int x0, x1, y0, y1;
22006
22007 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22008 y1 -= 1;
22009
22010 rif->draw_vertical_window_border (w, x0, y0, y1);
22011 }
22012 }
22013
22014
22015 /* Redraw the part of window W intersection rectangle FR. Pixel
22016 coordinates in FR are frame-relative. Call this function with
22017 input blocked. Value is non-zero if the exposure overwrites
22018 mouse-face. */
22019
22020 static int
22021 expose_window (w, fr)
22022 struct window *w;
22023 XRectangle *fr;
22024 {
22025 struct frame *f = XFRAME (w->frame);
22026 XRectangle wr, r;
22027 int mouse_face_overwritten_p = 0;
22028
22029 /* If window is not yet fully initialized, do nothing. This can
22030 happen when toolkit scroll bars are used and a window is split.
22031 Reconfiguring the scroll bar will generate an expose for a newly
22032 created window. */
22033 if (w->current_matrix == NULL)
22034 return 0;
22035
22036 /* When we're currently updating the window, display and current
22037 matrix usually don't agree. Arrange for a thorough display
22038 later. */
22039 if (w == updated_window)
22040 {
22041 SET_FRAME_GARBAGED (f);
22042 return 0;
22043 }
22044
22045 /* Frame-relative pixel rectangle of W. */
22046 wr.x = WINDOW_LEFT_EDGE_X (w);
22047 wr.y = WINDOW_TOP_EDGE_Y (w);
22048 wr.width = WINDOW_TOTAL_WIDTH (w);
22049 wr.height = WINDOW_TOTAL_HEIGHT (w);
22050
22051 if (x_intersect_rectangles (fr, &wr, &r))
22052 {
22053 int yb = window_text_bottom_y (w);
22054 struct glyph_row *row;
22055 int cursor_cleared_p;
22056 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22057
22058 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22059 r.x, r.y, r.width, r.height));
22060
22061 /* Convert to window coordinates. */
22062 r.x -= WINDOW_LEFT_EDGE_X (w);
22063 r.y -= WINDOW_TOP_EDGE_Y (w);
22064
22065 /* Turn off the cursor. */
22066 if (!w->pseudo_window_p
22067 && phys_cursor_in_rect_p (w, &r))
22068 {
22069 x_clear_cursor (w);
22070 cursor_cleared_p = 1;
22071 }
22072 else
22073 cursor_cleared_p = 0;
22074
22075 /* Update lines intersecting rectangle R. */
22076 first_overlapping_row = last_overlapping_row = NULL;
22077 for (row = w->current_matrix->rows;
22078 row->enabled_p;
22079 ++row)
22080 {
22081 int y0 = row->y;
22082 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22083
22084 if ((y0 >= r.y && y0 < r.y + r.height)
22085 || (y1 > r.y && y1 < r.y + r.height)
22086 || (r.y >= y0 && r.y < y1)
22087 || (r.y + r.height > y0 && r.y + r.height < y1))
22088 {
22089 if (row->overlapping_p)
22090 {
22091 if (first_overlapping_row == NULL)
22092 first_overlapping_row = row;
22093 last_overlapping_row = row;
22094 }
22095
22096 if (expose_line (w, row, &r))
22097 mouse_face_overwritten_p = 1;
22098 }
22099
22100 if (y1 >= yb)
22101 break;
22102 }
22103
22104 /* Display the mode line if there is one. */
22105 if (WINDOW_WANTS_MODELINE_P (w)
22106 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22107 row->enabled_p)
22108 && row->y < r.y + r.height)
22109 {
22110 if (expose_line (w, row, &r))
22111 mouse_face_overwritten_p = 1;
22112 }
22113
22114 if (!w->pseudo_window_p)
22115 {
22116 /* Fix the display of overlapping rows. */
22117 if (first_overlapping_row)
22118 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22119
22120 /* Draw border between windows. */
22121 x_draw_vertical_border (w);
22122
22123 /* Turn the cursor on again. */
22124 if (cursor_cleared_p)
22125 update_window_cursor (w, 1);
22126 }
22127 }
22128
22129 return mouse_face_overwritten_p;
22130 }
22131
22132
22133
22134 /* Redraw (parts) of all windows in the window tree rooted at W that
22135 intersect R. R contains frame pixel coordinates. Value is
22136 non-zero if the exposure overwrites mouse-face. */
22137
22138 static int
22139 expose_window_tree (w, r)
22140 struct window *w;
22141 XRectangle *r;
22142 {
22143 struct frame *f = XFRAME (w->frame);
22144 int mouse_face_overwritten_p = 0;
22145
22146 while (w && !FRAME_GARBAGED_P (f))
22147 {
22148 if (!NILP (w->hchild))
22149 mouse_face_overwritten_p
22150 |= expose_window_tree (XWINDOW (w->hchild), r);
22151 else if (!NILP (w->vchild))
22152 mouse_face_overwritten_p
22153 |= expose_window_tree (XWINDOW (w->vchild), r);
22154 else
22155 mouse_face_overwritten_p |= expose_window (w, r);
22156
22157 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22158 }
22159
22160 return mouse_face_overwritten_p;
22161 }
22162
22163
22164 /* EXPORT:
22165 Redisplay an exposed area of frame F. X and Y are the upper-left
22166 corner of the exposed rectangle. W and H are width and height of
22167 the exposed area. All are pixel values. W or H zero means redraw
22168 the entire frame. */
22169
22170 void
22171 expose_frame (f, x, y, w, h)
22172 struct frame *f;
22173 int x, y, w, h;
22174 {
22175 XRectangle r;
22176 int mouse_face_overwritten_p = 0;
22177
22178 TRACE ((stderr, "expose_frame "));
22179
22180 /* No need to redraw if frame will be redrawn soon. */
22181 if (FRAME_GARBAGED_P (f))
22182 {
22183 TRACE ((stderr, " garbaged\n"));
22184 return;
22185 }
22186
22187 /* If basic faces haven't been realized yet, there is no point in
22188 trying to redraw anything. This can happen when we get an expose
22189 event while Emacs is starting, e.g. by moving another window. */
22190 if (FRAME_FACE_CACHE (f) == NULL
22191 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22192 {
22193 TRACE ((stderr, " no faces\n"));
22194 return;
22195 }
22196
22197 if (w == 0 || h == 0)
22198 {
22199 r.x = r.y = 0;
22200 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22201 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22202 }
22203 else
22204 {
22205 r.x = x;
22206 r.y = y;
22207 r.width = w;
22208 r.height = h;
22209 }
22210
22211 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22212 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22213
22214 if (WINDOWP (f->tool_bar_window))
22215 mouse_face_overwritten_p
22216 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22217
22218 #ifdef HAVE_X_WINDOWS
22219 #ifndef MSDOS
22220 #ifndef USE_X_TOOLKIT
22221 if (WINDOWP (f->menu_bar_window))
22222 mouse_face_overwritten_p
22223 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22224 #endif /* not USE_X_TOOLKIT */
22225 #endif
22226 #endif
22227
22228 /* Some window managers support a focus-follows-mouse style with
22229 delayed raising of frames. Imagine a partially obscured frame,
22230 and moving the mouse into partially obscured mouse-face on that
22231 frame. The visible part of the mouse-face will be highlighted,
22232 then the WM raises the obscured frame. With at least one WM, KDE
22233 2.1, Emacs is not getting any event for the raising of the frame
22234 (even tried with SubstructureRedirectMask), only Expose events.
22235 These expose events will draw text normally, i.e. not
22236 highlighted. Which means we must redo the highlight here.
22237 Subsume it under ``we love X''. --gerd 2001-08-15 */
22238 /* Included in Windows version because Windows most likely does not
22239 do the right thing if any third party tool offers
22240 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22241 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22242 {
22243 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22244 if (f == dpyinfo->mouse_face_mouse_frame)
22245 {
22246 int x = dpyinfo->mouse_face_mouse_x;
22247 int y = dpyinfo->mouse_face_mouse_y;
22248 clear_mouse_face (dpyinfo);
22249 note_mouse_highlight (f, x, y);
22250 }
22251 }
22252 }
22253
22254
22255 /* EXPORT:
22256 Determine the intersection of two rectangles R1 and R2. Return
22257 the intersection in *RESULT. Value is non-zero if RESULT is not
22258 empty. */
22259
22260 int
22261 x_intersect_rectangles (r1, r2, result)
22262 XRectangle *r1, *r2, *result;
22263 {
22264 XRectangle *left, *right;
22265 XRectangle *upper, *lower;
22266 int intersection_p = 0;
22267
22268 /* Rearrange so that R1 is the left-most rectangle. */
22269 if (r1->x < r2->x)
22270 left = r1, right = r2;
22271 else
22272 left = r2, right = r1;
22273
22274 /* X0 of the intersection is right.x0, if this is inside R1,
22275 otherwise there is no intersection. */
22276 if (right->x <= left->x + left->width)
22277 {
22278 result->x = right->x;
22279
22280 /* The right end of the intersection is the minimum of the
22281 the right ends of left and right. */
22282 result->width = (min (left->x + left->width, right->x + right->width)
22283 - result->x);
22284
22285 /* Same game for Y. */
22286 if (r1->y < r2->y)
22287 upper = r1, lower = r2;
22288 else
22289 upper = r2, lower = r1;
22290
22291 /* The upper end of the intersection is lower.y0, if this is inside
22292 of upper. Otherwise, there is no intersection. */
22293 if (lower->y <= upper->y + upper->height)
22294 {
22295 result->y = lower->y;
22296
22297 /* The lower end of the intersection is the minimum of the lower
22298 ends of upper and lower. */
22299 result->height = (min (lower->y + lower->height,
22300 upper->y + upper->height)
22301 - result->y);
22302 intersection_p = 1;
22303 }
22304 }
22305
22306 return intersection_p;
22307 }
22308
22309 #endif /* HAVE_WINDOW_SYSTEM */
22310
22311 \f
22312 /***********************************************************************
22313 Initialization
22314 ***********************************************************************/
22315
22316 void
22317 syms_of_xdisp ()
22318 {
22319 Vwith_echo_area_save_vector = Qnil;
22320 staticpro (&Vwith_echo_area_save_vector);
22321
22322 Vmessage_stack = Qnil;
22323 staticpro (&Vmessage_stack);
22324
22325 Qinhibit_redisplay = intern ("inhibit-redisplay");
22326 staticpro (&Qinhibit_redisplay);
22327
22328 message_dolog_marker1 = Fmake_marker ();
22329 staticpro (&message_dolog_marker1);
22330 message_dolog_marker2 = Fmake_marker ();
22331 staticpro (&message_dolog_marker2);
22332 message_dolog_marker3 = Fmake_marker ();
22333 staticpro (&message_dolog_marker3);
22334
22335 #if GLYPH_DEBUG
22336 defsubr (&Sdump_frame_glyph_matrix);
22337 defsubr (&Sdump_glyph_matrix);
22338 defsubr (&Sdump_glyph_row);
22339 defsubr (&Sdump_tool_bar_row);
22340 defsubr (&Strace_redisplay);
22341 defsubr (&Strace_to_stderr);
22342 #endif
22343 #ifdef HAVE_WINDOW_SYSTEM
22344 defsubr (&Stool_bar_lines_needed);
22345 defsubr (&Slookup_image_map);
22346 #endif
22347 defsubr (&Sformat_mode_line);
22348
22349 staticpro (&Qmenu_bar_update_hook);
22350 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22351
22352 staticpro (&Qoverriding_terminal_local_map);
22353 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22354
22355 staticpro (&Qoverriding_local_map);
22356 Qoverriding_local_map = intern ("overriding-local-map");
22357
22358 staticpro (&Qwindow_scroll_functions);
22359 Qwindow_scroll_functions = intern ("window-scroll-functions");
22360
22361 staticpro (&Qredisplay_end_trigger_functions);
22362 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22363
22364 staticpro (&Qinhibit_point_motion_hooks);
22365 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22366
22367 QCdata = intern (":data");
22368 staticpro (&QCdata);
22369 Qdisplay = intern ("display");
22370 staticpro (&Qdisplay);
22371 Qspace_width = intern ("space-width");
22372 staticpro (&Qspace_width);
22373 Qraise = intern ("raise");
22374 staticpro (&Qraise);
22375 Qslice = intern ("slice");
22376 staticpro (&Qslice);
22377 Qspace = intern ("space");
22378 staticpro (&Qspace);
22379 Qmargin = intern ("margin");
22380 staticpro (&Qmargin);
22381 Qpointer = intern ("pointer");
22382 staticpro (&Qpointer);
22383 Qleft_margin = intern ("left-margin");
22384 staticpro (&Qleft_margin);
22385 Qright_margin = intern ("right-margin");
22386 staticpro (&Qright_margin);
22387 Qcenter = intern ("center");
22388 staticpro (&Qcenter);
22389 Qline_height = intern ("line-height");
22390 staticpro (&Qline_height);
22391 QCalign_to = intern (":align-to");
22392 staticpro (&QCalign_to);
22393 QCrelative_width = intern (":relative-width");
22394 staticpro (&QCrelative_width);
22395 QCrelative_height = intern (":relative-height");
22396 staticpro (&QCrelative_height);
22397 QCeval = intern (":eval");
22398 staticpro (&QCeval);
22399 QCpropertize = intern (":propertize");
22400 staticpro (&QCpropertize);
22401 QCfile = intern (":file");
22402 staticpro (&QCfile);
22403 Qfontified = intern ("fontified");
22404 staticpro (&Qfontified);
22405 Qfontification_functions = intern ("fontification-functions");
22406 staticpro (&Qfontification_functions);
22407 Qtrailing_whitespace = intern ("trailing-whitespace");
22408 staticpro (&Qtrailing_whitespace);
22409 Qescape_glyph = intern ("escape-glyph");
22410 staticpro (&Qescape_glyph);
22411 Qimage = intern ("image");
22412 staticpro (&Qimage);
22413 QCmap = intern (":map");
22414 staticpro (&QCmap);
22415 QCpointer = intern (":pointer");
22416 staticpro (&QCpointer);
22417 Qrect = intern ("rect");
22418 staticpro (&Qrect);
22419 Qcircle = intern ("circle");
22420 staticpro (&Qcircle);
22421 Qpoly = intern ("poly");
22422 staticpro (&Qpoly);
22423 Qmessage_truncate_lines = intern ("message-truncate-lines");
22424 staticpro (&Qmessage_truncate_lines);
22425 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22426 staticpro (&Qcursor_in_non_selected_windows);
22427 Qgrow_only = intern ("grow-only");
22428 staticpro (&Qgrow_only);
22429 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22430 staticpro (&Qinhibit_menubar_update);
22431 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22432 staticpro (&Qinhibit_eval_during_redisplay);
22433 Qposition = intern ("position");
22434 staticpro (&Qposition);
22435 Qbuffer_position = intern ("buffer-position");
22436 staticpro (&Qbuffer_position);
22437 Qobject = intern ("object");
22438 staticpro (&Qobject);
22439 Qbar = intern ("bar");
22440 staticpro (&Qbar);
22441 Qhbar = intern ("hbar");
22442 staticpro (&Qhbar);
22443 Qbox = intern ("box");
22444 staticpro (&Qbox);
22445 Qhollow = intern ("hollow");
22446 staticpro (&Qhollow);
22447 Qhand = intern ("hand");
22448 staticpro (&Qhand);
22449 Qarrow = intern ("arrow");
22450 staticpro (&Qarrow);
22451 Qtext = intern ("text");
22452 staticpro (&Qtext);
22453 Qrisky_local_variable = intern ("risky-local-variable");
22454 staticpro (&Qrisky_local_variable);
22455 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22456 staticpro (&Qinhibit_free_realized_faces);
22457
22458 list_of_error = Fcons (Fcons (intern ("error"),
22459 Fcons (intern ("void-variable"), Qnil)),
22460 Qnil);
22461 staticpro (&list_of_error);
22462
22463 Qlast_arrow_position = intern ("last-arrow-position");
22464 staticpro (&Qlast_arrow_position);
22465 Qlast_arrow_string = intern ("last-arrow-string");
22466 staticpro (&Qlast_arrow_string);
22467
22468 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22469 staticpro (&Qoverlay_arrow_string);
22470 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22471 staticpro (&Qoverlay_arrow_bitmap);
22472
22473 echo_buffer[0] = echo_buffer[1] = Qnil;
22474 staticpro (&echo_buffer[0]);
22475 staticpro (&echo_buffer[1]);
22476
22477 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22478 staticpro (&echo_area_buffer[0]);
22479 staticpro (&echo_area_buffer[1]);
22480
22481 Vmessages_buffer_name = build_string ("*Messages*");
22482 staticpro (&Vmessages_buffer_name);
22483
22484 mode_line_proptrans_alist = Qnil;
22485 staticpro (&mode_line_proptrans_alist);
22486
22487 mode_line_string_list = Qnil;
22488 staticpro (&mode_line_string_list);
22489
22490 help_echo_string = Qnil;
22491 staticpro (&help_echo_string);
22492 help_echo_object = Qnil;
22493 staticpro (&help_echo_object);
22494 help_echo_window = Qnil;
22495 staticpro (&help_echo_window);
22496 previous_help_echo_string = Qnil;
22497 staticpro (&previous_help_echo_string);
22498 help_echo_pos = -1;
22499
22500 #ifdef HAVE_WINDOW_SYSTEM
22501 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22502 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22503 For example, if a block cursor is over a tab, it will be drawn as
22504 wide as that tab on the display. */);
22505 x_stretch_cursor_p = 0;
22506 #endif
22507
22508 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22509 doc: /* *Non-nil means highlight trailing whitespace.
22510 The face used for trailing whitespace is `trailing-whitespace'. */);
22511 Vshow_trailing_whitespace = Qnil;
22512
22513 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22514 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22515 Vshow_nonbreak_escape = Qt;
22516
22517 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22518 doc: /* *The pointer shape to show in void text areas.
22519 Nil means to show the text pointer. Other options are `arrow', `text',
22520 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22521 Vvoid_text_area_pointer = Qarrow;
22522
22523 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22524 doc: /* Non-nil means don't actually do any redisplay.
22525 This is used for internal purposes. */);
22526 Vinhibit_redisplay = Qnil;
22527
22528 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22529 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22530 Vglobal_mode_string = Qnil;
22531
22532 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22533 doc: /* Marker for where to display an arrow on top of the buffer text.
22534 This must be the beginning of a line in order to work.
22535 See also `overlay-arrow-string'. */);
22536 Voverlay_arrow_position = Qnil;
22537
22538 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22539 doc: /* String to display as an arrow in non-window frames.
22540 See also `overlay-arrow-position'. */);
22541 Voverlay_arrow_string = Qnil;
22542
22543 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22544 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22545 The symbols on this list are examined during redisplay to determine
22546 where to display overlay arrows. */);
22547 Voverlay_arrow_variable_list
22548 = Fcons (intern ("overlay-arrow-position"), Qnil);
22549
22550 DEFVAR_INT ("scroll-step", &scroll_step,
22551 doc: /* *The number of lines to try scrolling a window by when point moves out.
22552 If that fails to bring point back on frame, point is centered instead.
22553 If this is zero, point is always centered after it moves off frame.
22554 If you want scrolling to always be a line at a time, you should set
22555 `scroll-conservatively' to a large value rather than set this to 1. */);
22556
22557 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22558 doc: /* *Scroll up to this many lines, to bring point back on screen.
22559 A value of zero means to scroll the text to center point vertically
22560 in the window. */);
22561 scroll_conservatively = 0;
22562
22563 DEFVAR_INT ("scroll-margin", &scroll_margin,
22564 doc: /* *Number of lines of margin at the top and bottom of a window.
22565 Recenter the window whenever point gets within this many lines
22566 of the top or bottom of the window. */);
22567 scroll_margin = 0;
22568
22569 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22570 doc: /* Pixels per inch on current display.
22571 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22572 Vdisplay_pixels_per_inch = make_float (72.0);
22573
22574 #if GLYPH_DEBUG
22575 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22576 #endif
22577
22578 DEFVAR_BOOL ("truncate-partial-width-windows",
22579 &truncate_partial_width_windows,
22580 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22581 truncate_partial_width_windows = 1;
22582
22583 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22584 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22585 Any other value means to use the appropriate face, `mode-line',
22586 `header-line', or `menu' respectively. */);
22587 mode_line_inverse_video = 1;
22588
22589 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22590 doc: /* *Maximum buffer size for which line number should be displayed.
22591 If the buffer is bigger than this, the line number does not appear
22592 in the mode line. A value of nil means no limit. */);
22593 Vline_number_display_limit = Qnil;
22594
22595 DEFVAR_INT ("line-number-display-limit-width",
22596 &line_number_display_limit_width,
22597 doc: /* *Maximum line width (in characters) for line number display.
22598 If the average length of the lines near point is bigger than this, then the
22599 line number may be omitted from the mode line. */);
22600 line_number_display_limit_width = 200;
22601
22602 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22603 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22604 highlight_nonselected_windows = 0;
22605
22606 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22607 doc: /* Non-nil if more than one frame is visible on this display.
22608 Minibuffer-only frames don't count, but iconified frames do.
22609 This variable is not guaranteed to be accurate except while processing
22610 `frame-title-format' and `icon-title-format'. */);
22611
22612 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22613 doc: /* Template for displaying the title bar of visible frames.
22614 \(Assuming the window manager supports this feature.)
22615 This variable has the same structure as `mode-line-format' (which see),
22616 and is used only on frames for which no explicit name has been set
22617 \(see `modify-frame-parameters'). */);
22618
22619 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22620 doc: /* Template for displaying the title bar of an iconified frame.
22621 \(Assuming the window manager supports this feature.)
22622 This variable has the same structure as `mode-line-format' (which see),
22623 and is used only on frames for which no explicit name has been set
22624 \(see `modify-frame-parameters'). */);
22625 Vicon_title_format
22626 = Vframe_title_format
22627 = Fcons (intern ("multiple-frames"),
22628 Fcons (build_string ("%b"),
22629 Fcons (Fcons (empty_string,
22630 Fcons (intern ("invocation-name"),
22631 Fcons (build_string ("@"),
22632 Fcons (intern ("system-name"),
22633 Qnil)))),
22634 Qnil)));
22635
22636 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22637 doc: /* Maximum number of lines to keep in the message log buffer.
22638 If nil, disable message logging. If t, log messages but don't truncate
22639 the buffer when it becomes large. */);
22640 Vmessage_log_max = make_number (50);
22641
22642 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22643 doc: /* Functions called before redisplay, if window sizes have changed.
22644 The value should be a list of functions that take one argument.
22645 Just before redisplay, for each frame, if any of its windows have changed
22646 size since the last redisplay, or have been split or deleted,
22647 all the functions in the list are called, with the frame as argument. */);
22648 Vwindow_size_change_functions = Qnil;
22649
22650 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22651 doc: /* List of functions to call before redisplaying a window with scrolling.
22652 Each function is called with two arguments, the window
22653 and its new display-start position. Note that the value of `window-end'
22654 is not valid when these functions are called. */);
22655 Vwindow_scroll_functions = Qnil;
22656
22657 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22658 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22659 mouse_autoselect_window = 0;
22660
22661 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22662 doc: /* *Non-nil means automatically resize tool-bars.
22663 This increases a tool-bar's height if not all tool-bar items are visible.
22664 It decreases a tool-bar's height when it would display blank lines
22665 otherwise. */);
22666 auto_resize_tool_bars_p = 1;
22667
22668 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22669 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22670 auto_raise_tool_bar_buttons_p = 1;
22671
22672 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22673 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22674 make_cursor_line_fully_visible_p = 1;
22675
22676 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22677 doc: /* *Margin around tool-bar buttons in pixels.
22678 If an integer, use that for both horizontal and vertical margins.
22679 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22680 HORZ specifying the horizontal margin, and VERT specifying the
22681 vertical margin. */);
22682 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22683
22684 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22685 doc: /* *Relief thickness of tool-bar buttons. */);
22686 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22687
22688 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22689 doc: /* List of functions to call to fontify regions of text.
22690 Each function is called with one argument POS. Functions must
22691 fontify a region starting at POS in the current buffer, and give
22692 fontified regions the property `fontified'. */);
22693 Vfontification_functions = Qnil;
22694 Fmake_variable_buffer_local (Qfontification_functions);
22695
22696 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22697 &unibyte_display_via_language_environment,
22698 doc: /* *Non-nil means display unibyte text according to language environment.
22699 Specifically this means that unibyte non-ASCII characters
22700 are displayed by converting them to the equivalent multibyte characters
22701 according to the current language environment. As a result, they are
22702 displayed according to the current fontset. */);
22703 unibyte_display_via_language_environment = 0;
22704
22705 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22706 doc: /* *Maximum height for resizing mini-windows.
22707 If a float, it specifies a fraction of the mini-window frame's height.
22708 If an integer, it specifies a number of lines. */);
22709 Vmax_mini_window_height = make_float (0.25);
22710
22711 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22712 doc: /* *How to resize mini-windows.
22713 A value of nil means don't automatically resize mini-windows.
22714 A value of t means resize them to fit the text displayed in them.
22715 A value of `grow-only', the default, means let mini-windows grow
22716 only, until their display becomes empty, at which point the windows
22717 go back to their normal size. */);
22718 Vresize_mini_windows = Qgrow_only;
22719
22720 DEFVAR_LISP ("cursor-in-non-selected-windows",
22721 &Vcursor_in_non_selected_windows,
22722 doc: /* *Cursor type to display in non-selected windows.
22723 t means to use hollow box cursor. See `cursor-type' for other values. */);
22724 Vcursor_in_non_selected_windows = Qt;
22725
22726 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22727 doc: /* Alist specifying how to blink the cursor off.
22728 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22729 `cursor-type' frame-parameter or variable equals ON-STATE,
22730 comparing using `equal', Emacs uses OFF-STATE to specify
22731 how to blink it off. */);
22732 Vblink_cursor_alist = Qnil;
22733
22734 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22735 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22736 automatic_hscrolling_p = 1;
22737
22738 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22739 doc: /* *How many columns away from the window edge point is allowed to get
22740 before automatic hscrolling will horizontally scroll the window. */);
22741 hscroll_margin = 5;
22742
22743 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22744 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22745 When point is less than `automatic-hscroll-margin' columns from the window
22746 edge, automatic hscrolling will scroll the window by the amount of columns
22747 determined by this variable. If its value is a positive integer, scroll that
22748 many columns. If it's a positive floating-point number, it specifies the
22749 fraction of the window's width to scroll. If it's nil or zero, point will be
22750 centered horizontally after the scroll. Any other value, including negative
22751 numbers, are treated as if the value were zero.
22752
22753 Automatic hscrolling always moves point outside the scroll margin, so if
22754 point was more than scroll step columns inside the margin, the window will
22755 scroll more than the value given by the scroll step.
22756
22757 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22758 and `scroll-right' overrides this variable's effect. */);
22759 Vhscroll_step = make_number (0);
22760
22761 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22762 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22763 Bind this around calls to `message' to let it take effect. */);
22764 message_truncate_lines = 0;
22765
22766 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22767 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22768 Can be used to update submenus whose contents should vary. */);
22769 Vmenu_bar_update_hook = Qnil;
22770
22771 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22772 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22773 inhibit_menubar_update = 0;
22774
22775 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22776 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22777 inhibit_eval_during_redisplay = 0;
22778
22779 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22780 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22781 inhibit_free_realized_faces = 0;
22782
22783 #if GLYPH_DEBUG
22784 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22785 doc: /* Inhibit try_window_id display optimization. */);
22786 inhibit_try_window_id = 0;
22787
22788 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22789 doc: /* Inhibit try_window_reusing display optimization. */);
22790 inhibit_try_window_reusing = 0;
22791
22792 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22793 doc: /* Inhibit try_cursor_movement display optimization. */);
22794 inhibit_try_cursor_movement = 0;
22795 #endif /* GLYPH_DEBUG */
22796 }
22797
22798
22799 /* Initialize this module when Emacs starts. */
22800
22801 void
22802 init_xdisp ()
22803 {
22804 Lisp_Object root_window;
22805 struct window *mini_w;
22806
22807 current_header_line_height = current_mode_line_height = -1;
22808
22809 CHARPOS (this_line_start_pos) = 0;
22810
22811 mini_w = XWINDOW (minibuf_window);
22812 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22813
22814 if (!noninteractive)
22815 {
22816 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22817 int i;
22818
22819 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22820 set_window_height (root_window,
22821 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22822 0);
22823 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22824 set_window_height (minibuf_window, 1, 0);
22825
22826 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22827 mini_w->total_cols = make_number (FRAME_COLS (f));
22828
22829 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22830 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22831 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22832
22833 /* The default ellipsis glyphs `...'. */
22834 for (i = 0; i < 3; ++i)
22835 default_invis_vector[i] = make_number ('.');
22836 }
22837
22838 {
22839 /* Allocate the buffer for frame titles.
22840 Also used for `format-mode-line'. */
22841 int size = 100;
22842 frame_title_buf = (char *) xmalloc (size);
22843 frame_title_buf_end = frame_title_buf + size;
22844 frame_title_ptr = NULL;
22845 }
22846
22847 help_echo_showing_p = 0;
22848 }
22849
22850
22851 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22852 (do not change this comment) */