]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(Fbury_buffer): Doc fix.
[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
8090 /* Prevent redisplay optimization in redisplay_internal by resetting
8091 this_line_start_pos. This is done because the mini-buffer now
8092 displays the message instead of its buffer text. */
8093 if (EQ (mini_window, selected_window))
8094 CHARPOS (this_line_start_pos) = 0;
8095
8096 return window_height_changed_p;
8097 }
8098
8099
8100 \f
8101 /***********************************************************************
8102 Frame Titles
8103 ***********************************************************************/
8104
8105
8106 /* The frame title buffering code is also used by Fformat_mode_line.
8107 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8108
8109 /* A buffer for constructing frame titles in it; allocated from the
8110 heap in init_xdisp and resized as needed in store_frame_title_char. */
8111
8112 static char *frame_title_buf;
8113
8114 /* The buffer's end, and a current output position in it. */
8115
8116 static char *frame_title_buf_end;
8117 static char *frame_title_ptr;
8118
8119
8120 /* Store a single character C for the frame title in frame_title_buf.
8121 Re-allocate frame_title_buf if necessary. */
8122
8123 static void
8124 #ifdef PROTOTYPES
8125 store_frame_title_char (char c)
8126 #else
8127 store_frame_title_char (c)
8128 char c;
8129 #endif
8130 {
8131 /* If output position has reached the end of the allocated buffer,
8132 double the buffer's size. */
8133 if (frame_title_ptr == frame_title_buf_end)
8134 {
8135 int len = frame_title_ptr - frame_title_buf;
8136 int new_size = 2 * len * sizeof *frame_title_buf;
8137 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8138 frame_title_buf_end = frame_title_buf + new_size;
8139 frame_title_ptr = frame_title_buf + len;
8140 }
8141
8142 *frame_title_ptr++ = c;
8143 }
8144
8145
8146 /* Store part of a frame title in frame_title_buf, beginning at
8147 frame_title_ptr. STR is the string to store. Do not copy
8148 characters that yield more columns than PRECISION; PRECISION <= 0
8149 means copy the whole string. Pad with spaces until FIELD_WIDTH
8150 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8151 pad. Called from display_mode_element when it is used to build a
8152 frame title. */
8153
8154 static int
8155 store_frame_title (str, field_width, precision)
8156 const unsigned char *str;
8157 int field_width, precision;
8158 {
8159 int n = 0;
8160 int dummy, nbytes;
8161
8162 /* Copy at most PRECISION chars from STR. */
8163 nbytes = strlen (str);
8164 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8165 while (nbytes--)
8166 store_frame_title_char (*str++);
8167
8168 /* Fill up with spaces until FIELD_WIDTH reached. */
8169 while (field_width > 0
8170 && n < field_width)
8171 {
8172 store_frame_title_char (' ');
8173 ++n;
8174 }
8175
8176 return n;
8177 }
8178
8179 #ifdef HAVE_WINDOW_SYSTEM
8180
8181 /* Set the title of FRAME, if it has changed. The title format is
8182 Vicon_title_format if FRAME is iconified, otherwise it is
8183 frame_title_format. */
8184
8185 static void
8186 x_consider_frame_title (frame)
8187 Lisp_Object frame;
8188 {
8189 struct frame *f = XFRAME (frame);
8190
8191 if (FRAME_WINDOW_P (f)
8192 || FRAME_MINIBUF_ONLY_P (f)
8193 || f->explicit_name)
8194 {
8195 /* Do we have more than one visible frame on this X display? */
8196 Lisp_Object tail;
8197 Lisp_Object fmt;
8198 struct buffer *obuf;
8199 int len;
8200 struct it it;
8201
8202 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8203 {
8204 Lisp_Object other_frame = XCAR (tail);
8205 struct frame *tf = XFRAME (other_frame);
8206
8207 if (tf != f
8208 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8209 && !FRAME_MINIBUF_ONLY_P (tf)
8210 && !EQ (other_frame, tip_frame)
8211 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8212 break;
8213 }
8214
8215 /* Set global variable indicating that multiple frames exist. */
8216 multiple_frames = CONSP (tail);
8217
8218 /* Switch to the buffer of selected window of the frame. Set up
8219 frame_title_ptr so that display_mode_element will output into it;
8220 then display the title. */
8221 obuf = current_buffer;
8222 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8223 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8224 frame_title_ptr = frame_title_buf;
8225 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8226 NULL, DEFAULT_FACE_ID);
8227 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8228 len = frame_title_ptr - frame_title_buf;
8229 frame_title_ptr = NULL;
8230 set_buffer_internal_1 (obuf);
8231
8232 /* Set the title only if it's changed. This avoids consing in
8233 the common case where it hasn't. (If it turns out that we've
8234 already wasted too much time by walking through the list with
8235 display_mode_element, then we might need to optimize at a
8236 higher level than this.) */
8237 if (! STRINGP (f->name)
8238 || SBYTES (f->name) != len
8239 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8240 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8241 }
8242 }
8243
8244 #endif /* not HAVE_WINDOW_SYSTEM */
8245
8246
8247
8248 \f
8249 /***********************************************************************
8250 Menu Bars
8251 ***********************************************************************/
8252
8253
8254 /* Prepare for redisplay by updating menu-bar item lists when
8255 appropriate. This can call eval. */
8256
8257 void
8258 prepare_menu_bars ()
8259 {
8260 int all_windows;
8261 struct gcpro gcpro1, gcpro2;
8262 struct frame *f;
8263 Lisp_Object tooltip_frame;
8264
8265 #ifdef HAVE_WINDOW_SYSTEM
8266 tooltip_frame = tip_frame;
8267 #else
8268 tooltip_frame = Qnil;
8269 #endif
8270
8271 /* Update all frame titles based on their buffer names, etc. We do
8272 this before the menu bars so that the buffer-menu will show the
8273 up-to-date frame titles. */
8274 #ifdef HAVE_WINDOW_SYSTEM
8275 if (windows_or_buffers_changed || update_mode_lines)
8276 {
8277 Lisp_Object tail, frame;
8278
8279 FOR_EACH_FRAME (tail, frame)
8280 {
8281 f = XFRAME (frame);
8282 if (!EQ (frame, tooltip_frame)
8283 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8284 x_consider_frame_title (frame);
8285 }
8286 }
8287 #endif /* HAVE_WINDOW_SYSTEM */
8288
8289 /* Update the menu bar item lists, if appropriate. This has to be
8290 done before any actual redisplay or generation of display lines. */
8291 all_windows = (update_mode_lines
8292 || buffer_shared > 1
8293 || windows_or_buffers_changed);
8294 if (all_windows)
8295 {
8296 Lisp_Object tail, frame;
8297 int count = SPECPDL_INDEX ();
8298
8299 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8300
8301 FOR_EACH_FRAME (tail, frame)
8302 {
8303 f = XFRAME (frame);
8304
8305 /* Ignore tooltip frame. */
8306 if (EQ (frame, tooltip_frame))
8307 continue;
8308
8309 /* If a window on this frame changed size, report that to
8310 the user and clear the size-change flag. */
8311 if (FRAME_WINDOW_SIZES_CHANGED (f))
8312 {
8313 Lisp_Object functions;
8314
8315 /* Clear flag first in case we get an error below. */
8316 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8317 functions = Vwindow_size_change_functions;
8318 GCPRO2 (tail, functions);
8319
8320 while (CONSP (functions))
8321 {
8322 call1 (XCAR (functions), frame);
8323 functions = XCDR (functions);
8324 }
8325 UNGCPRO;
8326 }
8327
8328 GCPRO1 (tail);
8329 update_menu_bar (f, 0);
8330 #ifdef HAVE_WINDOW_SYSTEM
8331 update_tool_bar (f, 0);
8332 #endif
8333 UNGCPRO;
8334 }
8335
8336 unbind_to (count, Qnil);
8337 }
8338 else
8339 {
8340 struct frame *sf = SELECTED_FRAME ();
8341 update_menu_bar (sf, 1);
8342 #ifdef HAVE_WINDOW_SYSTEM
8343 update_tool_bar (sf, 1);
8344 #endif
8345 }
8346
8347 /* Motif needs this. See comment in xmenu.c. Turn it off when
8348 pending_menu_activation is not defined. */
8349 #ifdef USE_X_TOOLKIT
8350 pending_menu_activation = 0;
8351 #endif
8352 }
8353
8354
8355 /* Update the menu bar item list for frame F. This has to be done
8356 before we start to fill in any display lines, because it can call
8357 eval.
8358
8359 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8360
8361 static void
8362 update_menu_bar (f, save_match_data)
8363 struct frame *f;
8364 int save_match_data;
8365 {
8366 Lisp_Object window;
8367 register struct window *w;
8368
8369 /* If called recursively during a menu update, do nothing. This can
8370 happen when, for instance, an activate-menubar-hook causes a
8371 redisplay. */
8372 if (inhibit_menubar_update)
8373 return;
8374
8375 window = FRAME_SELECTED_WINDOW (f);
8376 w = XWINDOW (window);
8377
8378 #if 0 /* The if statement below this if statement used to include the
8379 condition !NILP (w->update_mode_line), rather than using
8380 update_mode_lines directly, and this if statement may have
8381 been added to make that condition work. Now the if
8382 statement below matches its comment, this isn't needed. */
8383 if (update_mode_lines)
8384 w->update_mode_line = Qt;
8385 #endif
8386
8387 if (FRAME_WINDOW_P (f)
8388 ?
8389 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8390 || defined (USE_GTK)
8391 FRAME_EXTERNAL_MENU_BAR (f)
8392 #else
8393 FRAME_MENU_BAR_LINES (f) > 0
8394 #endif
8395 : FRAME_MENU_BAR_LINES (f) > 0)
8396 {
8397 /* If the user has switched buffers or windows, we need to
8398 recompute to reflect the new bindings. But we'll
8399 recompute when update_mode_lines is set too; that means
8400 that people can use force-mode-line-update to request
8401 that the menu bar be recomputed. The adverse effect on
8402 the rest of the redisplay algorithm is about the same as
8403 windows_or_buffers_changed anyway. */
8404 if (windows_or_buffers_changed
8405 /* This used to test w->update_mode_line, but we believe
8406 there is no need to recompute the menu in that case. */
8407 || update_mode_lines
8408 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8409 < BUF_MODIFF (XBUFFER (w->buffer)))
8410 != !NILP (w->last_had_star))
8411 || ((!NILP (Vtransient_mark_mode)
8412 && !NILP (XBUFFER (w->buffer)->mark_active))
8413 != !NILP (w->region_showing)))
8414 {
8415 struct buffer *prev = current_buffer;
8416 int count = SPECPDL_INDEX ();
8417
8418 specbind (Qinhibit_menubar_update, Qt);
8419
8420 set_buffer_internal_1 (XBUFFER (w->buffer));
8421 if (save_match_data)
8422 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8423 if (NILP (Voverriding_local_map_menu_flag))
8424 {
8425 specbind (Qoverriding_terminal_local_map, Qnil);
8426 specbind (Qoverriding_local_map, Qnil);
8427 }
8428
8429 /* Run the Lucid hook. */
8430 safe_run_hooks (Qactivate_menubar_hook);
8431
8432 /* If it has changed current-menubar from previous value,
8433 really recompute the menu-bar from the value. */
8434 if (! NILP (Vlucid_menu_bar_dirty_flag))
8435 call0 (Qrecompute_lucid_menubar);
8436
8437 safe_run_hooks (Qmenu_bar_update_hook);
8438 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8439
8440 /* Redisplay the menu bar in case we changed it. */
8441 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8442 || defined (USE_GTK)
8443 if (FRAME_WINDOW_P (f)
8444 #if defined (MAC_OS)
8445 /* All frames on Mac OS share the same menubar. So only the
8446 selected frame should be allowed to set it. */
8447 && f == SELECTED_FRAME ()
8448 #endif
8449 )
8450 set_frame_menubar (f, 0, 0);
8451 else
8452 /* On a terminal screen, the menu bar is an ordinary screen
8453 line, and this makes it get updated. */
8454 w->update_mode_line = Qt;
8455 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8456 /* In the non-toolkit version, the menu bar is an ordinary screen
8457 line, and this makes it get updated. */
8458 w->update_mode_line = Qt;
8459 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8460
8461 unbind_to (count, Qnil);
8462 set_buffer_internal_1 (prev);
8463 }
8464 }
8465 }
8466
8467
8468 \f
8469 /***********************************************************************
8470 Output Cursor
8471 ***********************************************************************/
8472
8473 #ifdef HAVE_WINDOW_SYSTEM
8474
8475 /* EXPORT:
8476 Nominal cursor position -- where to draw output.
8477 HPOS and VPOS are window relative glyph matrix coordinates.
8478 X and Y are window relative pixel coordinates. */
8479
8480 struct cursor_pos output_cursor;
8481
8482
8483 /* EXPORT:
8484 Set the global variable output_cursor to CURSOR. All cursor
8485 positions are relative to updated_window. */
8486
8487 void
8488 set_output_cursor (cursor)
8489 struct cursor_pos *cursor;
8490 {
8491 output_cursor.hpos = cursor->hpos;
8492 output_cursor.vpos = cursor->vpos;
8493 output_cursor.x = cursor->x;
8494 output_cursor.y = cursor->y;
8495 }
8496
8497
8498 /* EXPORT for RIF:
8499 Set a nominal cursor position.
8500
8501 HPOS and VPOS are column/row positions in a window glyph matrix. X
8502 and Y are window text area relative pixel positions.
8503
8504 If this is done during an update, updated_window will contain the
8505 window that is being updated and the position is the future output
8506 cursor position for that window. If updated_window is null, use
8507 selected_window and display the cursor at the given position. */
8508
8509 void
8510 x_cursor_to (vpos, hpos, y, x)
8511 int vpos, hpos, y, x;
8512 {
8513 struct window *w;
8514
8515 /* If updated_window is not set, work on selected_window. */
8516 if (updated_window)
8517 w = updated_window;
8518 else
8519 w = XWINDOW (selected_window);
8520
8521 /* Set the output cursor. */
8522 output_cursor.hpos = hpos;
8523 output_cursor.vpos = vpos;
8524 output_cursor.x = x;
8525 output_cursor.y = y;
8526
8527 /* If not called as part of an update, really display the cursor.
8528 This will also set the cursor position of W. */
8529 if (updated_window == NULL)
8530 {
8531 BLOCK_INPUT;
8532 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8533 if (rif->flush_display_optional)
8534 rif->flush_display_optional (SELECTED_FRAME ());
8535 UNBLOCK_INPUT;
8536 }
8537 }
8538
8539 #endif /* HAVE_WINDOW_SYSTEM */
8540
8541 \f
8542 /***********************************************************************
8543 Tool-bars
8544 ***********************************************************************/
8545
8546 #ifdef HAVE_WINDOW_SYSTEM
8547
8548 /* Where the mouse was last time we reported a mouse event. */
8549
8550 FRAME_PTR last_mouse_frame;
8551
8552 /* Tool-bar item index of the item on which a mouse button was pressed
8553 or -1. */
8554
8555 int last_tool_bar_item;
8556
8557
8558 /* Update the tool-bar item list for frame F. This has to be done
8559 before we start to fill in any display lines. Called from
8560 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8561 and restore it here. */
8562
8563 static void
8564 update_tool_bar (f, save_match_data)
8565 struct frame *f;
8566 int save_match_data;
8567 {
8568 #ifdef USE_GTK
8569 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8570 #else
8571 int do_update = WINDOWP (f->tool_bar_window)
8572 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8573 #endif
8574
8575 if (do_update)
8576 {
8577 Lisp_Object window;
8578 struct window *w;
8579
8580 window = FRAME_SELECTED_WINDOW (f);
8581 w = XWINDOW (window);
8582
8583 /* If the user has switched buffers or windows, we need to
8584 recompute to reflect the new bindings. But we'll
8585 recompute when update_mode_lines is set too; that means
8586 that people can use force-mode-line-update to request
8587 that the menu bar be recomputed. The adverse effect on
8588 the rest of the redisplay algorithm is about the same as
8589 windows_or_buffers_changed anyway. */
8590 if (windows_or_buffers_changed
8591 || !NILP (w->update_mode_line)
8592 || update_mode_lines
8593 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8594 < BUF_MODIFF (XBUFFER (w->buffer)))
8595 != !NILP (w->last_had_star))
8596 || ((!NILP (Vtransient_mark_mode)
8597 && !NILP (XBUFFER (w->buffer)->mark_active))
8598 != !NILP (w->region_showing)))
8599 {
8600 struct buffer *prev = current_buffer;
8601 int count = SPECPDL_INDEX ();
8602 Lisp_Object new_tool_bar;
8603 int new_n_tool_bar;
8604 struct gcpro gcpro1;
8605
8606 /* Set current_buffer to the buffer of the selected
8607 window of the frame, so that we get the right local
8608 keymaps. */
8609 set_buffer_internal_1 (XBUFFER (w->buffer));
8610
8611 /* Save match data, if we must. */
8612 if (save_match_data)
8613 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8614
8615 /* Make sure that we don't accidentally use bogus keymaps. */
8616 if (NILP (Voverriding_local_map_menu_flag))
8617 {
8618 specbind (Qoverriding_terminal_local_map, Qnil);
8619 specbind (Qoverriding_local_map, Qnil);
8620 }
8621
8622 GCPRO1 (new_tool_bar);
8623
8624 /* Build desired tool-bar items from keymaps. */
8625 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8626 &new_n_tool_bar);
8627
8628 /* Redisplay the tool-bar if we changed it. */
8629 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8630 {
8631 /* Redisplay that happens asynchronously due to an expose event
8632 may access f->tool_bar_items. Make sure we update both
8633 variables within BLOCK_INPUT so no such event interrupts. */
8634 BLOCK_INPUT;
8635 f->tool_bar_items = new_tool_bar;
8636 f->n_tool_bar_items = new_n_tool_bar;
8637 w->update_mode_line = Qt;
8638 UNBLOCK_INPUT;
8639 }
8640
8641 UNGCPRO;
8642
8643 unbind_to (count, Qnil);
8644 set_buffer_internal_1 (prev);
8645 }
8646 }
8647 }
8648
8649
8650 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8651 F's desired tool-bar contents. F->tool_bar_items must have
8652 been set up previously by calling prepare_menu_bars. */
8653
8654 static void
8655 build_desired_tool_bar_string (f)
8656 struct frame *f;
8657 {
8658 int i, size, size_needed;
8659 struct gcpro gcpro1, gcpro2, gcpro3;
8660 Lisp_Object image, plist, props;
8661
8662 image = plist = props = Qnil;
8663 GCPRO3 (image, plist, props);
8664
8665 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8666 Otherwise, make a new string. */
8667
8668 /* The size of the string we might be able to reuse. */
8669 size = (STRINGP (f->desired_tool_bar_string)
8670 ? SCHARS (f->desired_tool_bar_string)
8671 : 0);
8672
8673 /* We need one space in the string for each image. */
8674 size_needed = f->n_tool_bar_items;
8675
8676 /* Reuse f->desired_tool_bar_string, if possible. */
8677 if (size < size_needed || NILP (f->desired_tool_bar_string))
8678 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8679 make_number (' '));
8680 else
8681 {
8682 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8683 Fremove_text_properties (make_number (0), make_number (size),
8684 props, f->desired_tool_bar_string);
8685 }
8686
8687 /* Put a `display' property on the string for the images to display,
8688 put a `menu_item' property on tool-bar items with a value that
8689 is the index of the item in F's tool-bar item vector. */
8690 for (i = 0; i < f->n_tool_bar_items; ++i)
8691 {
8692 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8693
8694 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8695 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8696 int hmargin, vmargin, relief, idx, end;
8697 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8698
8699 /* If image is a vector, choose the image according to the
8700 button state. */
8701 image = PROP (TOOL_BAR_ITEM_IMAGES);
8702 if (VECTORP (image))
8703 {
8704 if (enabled_p)
8705 idx = (selected_p
8706 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8707 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8708 else
8709 idx = (selected_p
8710 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8711 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8712
8713 xassert (ASIZE (image) >= idx);
8714 image = AREF (image, idx);
8715 }
8716 else
8717 idx = -1;
8718
8719 /* Ignore invalid image specifications. */
8720 if (!valid_image_p (image))
8721 continue;
8722
8723 /* Display the tool-bar button pressed, or depressed. */
8724 plist = Fcopy_sequence (XCDR (image));
8725
8726 /* Compute margin and relief to draw. */
8727 relief = (tool_bar_button_relief >= 0
8728 ? tool_bar_button_relief
8729 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8730 hmargin = vmargin = relief;
8731
8732 if (INTEGERP (Vtool_bar_button_margin)
8733 && XINT (Vtool_bar_button_margin) > 0)
8734 {
8735 hmargin += XFASTINT (Vtool_bar_button_margin);
8736 vmargin += XFASTINT (Vtool_bar_button_margin);
8737 }
8738 else if (CONSP (Vtool_bar_button_margin))
8739 {
8740 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8741 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8742 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8743
8744 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8745 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8746 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8747 }
8748
8749 if (auto_raise_tool_bar_buttons_p)
8750 {
8751 /* Add a `:relief' property to the image spec if the item is
8752 selected. */
8753 if (selected_p)
8754 {
8755 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8756 hmargin -= relief;
8757 vmargin -= relief;
8758 }
8759 }
8760 else
8761 {
8762 /* If image is selected, display it pressed, i.e. with a
8763 negative relief. If it's not selected, display it with a
8764 raised relief. */
8765 plist = Fplist_put (plist, QCrelief,
8766 (selected_p
8767 ? make_number (-relief)
8768 : make_number (relief)));
8769 hmargin -= relief;
8770 vmargin -= relief;
8771 }
8772
8773 /* Put a margin around the image. */
8774 if (hmargin || vmargin)
8775 {
8776 if (hmargin == vmargin)
8777 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8778 else
8779 plist = Fplist_put (plist, QCmargin,
8780 Fcons (make_number (hmargin),
8781 make_number (vmargin)));
8782 }
8783
8784 /* If button is not enabled, and we don't have special images
8785 for the disabled state, make the image appear disabled by
8786 applying an appropriate algorithm to it. */
8787 if (!enabled_p && idx < 0)
8788 plist = Fplist_put (plist, QCconversion, Qdisabled);
8789
8790 /* Put a `display' text property on the string for the image to
8791 display. Put a `menu-item' property on the string that gives
8792 the start of this item's properties in the tool-bar items
8793 vector. */
8794 image = Fcons (Qimage, plist);
8795 props = list4 (Qdisplay, image,
8796 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8797
8798 /* Let the last image hide all remaining spaces in the tool bar
8799 string. The string can be longer than needed when we reuse a
8800 previous string. */
8801 if (i + 1 == f->n_tool_bar_items)
8802 end = SCHARS (f->desired_tool_bar_string);
8803 else
8804 end = i + 1;
8805 Fadd_text_properties (make_number (i), make_number (end),
8806 props, f->desired_tool_bar_string);
8807 #undef PROP
8808 }
8809
8810 UNGCPRO;
8811 }
8812
8813
8814 /* Display one line of the tool-bar of frame IT->f. */
8815
8816 static void
8817 display_tool_bar_line (it)
8818 struct it *it;
8819 {
8820 struct glyph_row *row = it->glyph_row;
8821 int max_x = it->last_visible_x;
8822 struct glyph *last;
8823
8824 prepare_desired_row (row);
8825 row->y = it->current_y;
8826
8827 /* Note that this isn't made use of if the face hasn't a box,
8828 so there's no need to check the face here. */
8829 it->start_of_box_run_p = 1;
8830
8831 while (it->current_x < max_x)
8832 {
8833 int x_before, x, n_glyphs_before, i, nglyphs;
8834
8835 /* Get the next display element. */
8836 if (!get_next_display_element (it))
8837 break;
8838
8839 /* Produce glyphs. */
8840 x_before = it->current_x;
8841 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8842 PRODUCE_GLYPHS (it);
8843
8844 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8845 i = 0;
8846 x = x_before;
8847 while (i < nglyphs)
8848 {
8849 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8850
8851 if (x + glyph->pixel_width > max_x)
8852 {
8853 /* Glyph doesn't fit on line. */
8854 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8855 it->current_x = x;
8856 goto out;
8857 }
8858
8859 ++it->hpos;
8860 x += glyph->pixel_width;
8861 ++i;
8862 }
8863
8864 /* Stop at line ends. */
8865 if (ITERATOR_AT_END_OF_LINE_P (it))
8866 break;
8867
8868 set_iterator_to_next (it, 1);
8869 }
8870
8871 out:;
8872
8873 row->displays_text_p = row->used[TEXT_AREA] != 0;
8874 extend_face_to_end_of_line (it);
8875 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8876 last->right_box_line_p = 1;
8877 if (last == row->glyphs[TEXT_AREA])
8878 last->left_box_line_p = 1;
8879 compute_line_metrics (it);
8880
8881 /* If line is empty, make it occupy the rest of the tool-bar. */
8882 if (!row->displays_text_p)
8883 {
8884 row->height = row->phys_height = it->last_visible_y - row->y;
8885 row->ascent = row->phys_ascent = 0;
8886 row->extra_line_spacing = 0;
8887 }
8888
8889 row->full_width_p = 1;
8890 row->continued_p = 0;
8891 row->truncated_on_left_p = 0;
8892 row->truncated_on_right_p = 0;
8893
8894 it->current_x = it->hpos = 0;
8895 it->current_y += row->height;
8896 ++it->vpos;
8897 ++it->glyph_row;
8898 }
8899
8900
8901 /* Value is the number of screen lines needed to make all tool-bar
8902 items of frame F visible. */
8903
8904 static int
8905 tool_bar_lines_needed (f)
8906 struct frame *f;
8907 {
8908 struct window *w = XWINDOW (f->tool_bar_window);
8909 struct it it;
8910
8911 /* Initialize an iterator for iteration over
8912 F->desired_tool_bar_string in the tool-bar window of frame F. */
8913 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8914 it.first_visible_x = 0;
8915 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8916 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8917
8918 while (!ITERATOR_AT_END_P (&it))
8919 {
8920 it.glyph_row = w->desired_matrix->rows;
8921 clear_glyph_row (it.glyph_row);
8922 display_tool_bar_line (&it);
8923 }
8924
8925 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8926 }
8927
8928
8929 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8930 0, 1, 0,
8931 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8932 (frame)
8933 Lisp_Object frame;
8934 {
8935 struct frame *f;
8936 struct window *w;
8937 int nlines = 0;
8938
8939 if (NILP (frame))
8940 frame = selected_frame;
8941 else
8942 CHECK_FRAME (frame);
8943 f = XFRAME (frame);
8944
8945 if (WINDOWP (f->tool_bar_window)
8946 || (w = XWINDOW (f->tool_bar_window),
8947 WINDOW_TOTAL_LINES (w) > 0))
8948 {
8949 update_tool_bar (f, 1);
8950 if (f->n_tool_bar_items)
8951 {
8952 build_desired_tool_bar_string (f);
8953 nlines = tool_bar_lines_needed (f);
8954 }
8955 }
8956
8957 return make_number (nlines);
8958 }
8959
8960
8961 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
8962 height should be changed. */
8963
8964 static int
8965 redisplay_tool_bar (f)
8966 struct frame *f;
8967 {
8968 struct window *w;
8969 struct it it;
8970 struct glyph_row *row;
8971 int change_height_p = 0;
8972
8973 #ifdef USE_GTK
8974 if (FRAME_EXTERNAL_TOOL_BAR (f))
8975 update_frame_tool_bar (f);
8976 return 0;
8977 #endif
8978
8979 /* If frame hasn't a tool-bar window or if it is zero-height, don't
8980 do anything. This means you must start with tool-bar-lines
8981 non-zero to get the auto-sizing effect. Or in other words, you
8982 can turn off tool-bars by specifying tool-bar-lines zero. */
8983 if (!WINDOWP (f->tool_bar_window)
8984 || (w = XWINDOW (f->tool_bar_window),
8985 WINDOW_TOTAL_LINES (w) == 0))
8986 return 0;
8987
8988 /* Set up an iterator for the tool-bar window. */
8989 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8990 it.first_visible_x = 0;
8991 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8992 row = it.glyph_row;
8993
8994 /* Build a string that represents the contents of the tool-bar. */
8995 build_desired_tool_bar_string (f);
8996 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8997
8998 /* Display as many lines as needed to display all tool-bar items. */
8999 while (it.current_y < it.last_visible_y)
9000 display_tool_bar_line (&it);
9001
9002 /* It doesn't make much sense to try scrolling in the tool-bar
9003 window, so don't do it. */
9004 w->desired_matrix->no_scrolling_p = 1;
9005 w->must_be_updated_p = 1;
9006
9007 if (auto_resize_tool_bars_p)
9008 {
9009 int nlines;
9010
9011 /* If we couldn't display everything, change the tool-bar's
9012 height. */
9013 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9014 change_height_p = 1;
9015
9016 /* If there are blank lines at the end, except for a partially
9017 visible blank line at the end that is smaller than
9018 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9019 row = it.glyph_row - 1;
9020 if (!row->displays_text_p
9021 && row->height >= FRAME_LINE_HEIGHT (f))
9022 change_height_p = 1;
9023
9024 /* If row displays tool-bar items, but is partially visible,
9025 change the tool-bar's height. */
9026 if (row->displays_text_p
9027 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9028 change_height_p = 1;
9029
9030 /* Resize windows as needed by changing the `tool-bar-lines'
9031 frame parameter. */
9032 if (change_height_p
9033 && (nlines = tool_bar_lines_needed (f),
9034 nlines != WINDOW_TOTAL_LINES (w)))
9035 {
9036 extern Lisp_Object Qtool_bar_lines;
9037 Lisp_Object frame;
9038 int old_height = WINDOW_TOTAL_LINES (w);
9039
9040 XSETFRAME (frame, f);
9041 clear_glyph_matrix (w->desired_matrix);
9042 Fmodify_frame_parameters (frame,
9043 Fcons (Fcons (Qtool_bar_lines,
9044 make_number (nlines)),
9045 Qnil));
9046 if (WINDOW_TOTAL_LINES (w) != old_height)
9047 fonts_changed_p = 1;
9048 }
9049 }
9050
9051 return change_height_p;
9052 }
9053
9054
9055 /* Get information about the tool-bar item which is displayed in GLYPH
9056 on frame F. Return in *PROP_IDX the index where tool-bar item
9057 properties start in F->tool_bar_items. Value is zero if
9058 GLYPH doesn't display a tool-bar item. */
9059
9060 static int
9061 tool_bar_item_info (f, glyph, prop_idx)
9062 struct frame *f;
9063 struct glyph *glyph;
9064 int *prop_idx;
9065 {
9066 Lisp_Object prop;
9067 int success_p;
9068 int charpos;
9069
9070 /* This function can be called asynchronously, which means we must
9071 exclude any possibility that Fget_text_property signals an
9072 error. */
9073 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9074 charpos = max (0, charpos);
9075
9076 /* Get the text property `menu-item' at pos. The value of that
9077 property is the start index of this item's properties in
9078 F->tool_bar_items. */
9079 prop = Fget_text_property (make_number (charpos),
9080 Qmenu_item, f->current_tool_bar_string);
9081 if (INTEGERP (prop))
9082 {
9083 *prop_idx = XINT (prop);
9084 success_p = 1;
9085 }
9086 else
9087 success_p = 0;
9088
9089 return success_p;
9090 }
9091
9092 \f
9093 /* Get information about the tool-bar item at position X/Y on frame F.
9094 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9095 the current matrix of the tool-bar window of F, or NULL if not
9096 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9097 item in F->tool_bar_items. Value is
9098
9099 -1 if X/Y is not on a tool-bar item
9100 0 if X/Y is on the same item that was highlighted before.
9101 1 otherwise. */
9102
9103 static int
9104 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9105 struct frame *f;
9106 int x, y;
9107 struct glyph **glyph;
9108 int *hpos, *vpos, *prop_idx;
9109 {
9110 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9111 struct window *w = XWINDOW (f->tool_bar_window);
9112 int area;
9113
9114 /* Find the glyph under X/Y. */
9115 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9116 if (*glyph == NULL)
9117 return -1;
9118
9119 /* Get the start of this tool-bar item's properties in
9120 f->tool_bar_items. */
9121 if (!tool_bar_item_info (f, *glyph, prop_idx))
9122 return -1;
9123
9124 /* Is mouse on the highlighted item? */
9125 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9126 && *vpos >= dpyinfo->mouse_face_beg_row
9127 && *vpos <= dpyinfo->mouse_face_end_row
9128 && (*vpos > dpyinfo->mouse_face_beg_row
9129 || *hpos >= dpyinfo->mouse_face_beg_col)
9130 && (*vpos < dpyinfo->mouse_face_end_row
9131 || *hpos < dpyinfo->mouse_face_end_col
9132 || dpyinfo->mouse_face_past_end))
9133 return 0;
9134
9135 return 1;
9136 }
9137
9138
9139 /* EXPORT:
9140 Handle mouse button event on the tool-bar of frame F, at
9141 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9142 0 for button release. MODIFIERS is event modifiers for button
9143 release. */
9144
9145 void
9146 handle_tool_bar_click (f, x, y, down_p, modifiers)
9147 struct frame *f;
9148 int x, y, down_p;
9149 unsigned int modifiers;
9150 {
9151 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9152 struct window *w = XWINDOW (f->tool_bar_window);
9153 int hpos, vpos, prop_idx;
9154 struct glyph *glyph;
9155 Lisp_Object enabled_p;
9156
9157 /* If not on the highlighted tool-bar item, return. */
9158 frame_to_window_pixel_xy (w, &x, &y);
9159 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9160 return;
9161
9162 /* If item is disabled, do nothing. */
9163 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9164 if (NILP (enabled_p))
9165 return;
9166
9167 if (down_p)
9168 {
9169 /* Show item in pressed state. */
9170 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9171 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9172 last_tool_bar_item = prop_idx;
9173 }
9174 else
9175 {
9176 Lisp_Object key, frame;
9177 struct input_event event;
9178 EVENT_INIT (event);
9179
9180 /* Show item in released state. */
9181 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9182 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9183
9184 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9185
9186 XSETFRAME (frame, f);
9187 event.kind = TOOL_BAR_EVENT;
9188 event.frame_or_window = frame;
9189 event.arg = frame;
9190 kbd_buffer_store_event (&event);
9191
9192 event.kind = TOOL_BAR_EVENT;
9193 event.frame_or_window = frame;
9194 event.arg = key;
9195 event.modifiers = modifiers;
9196 kbd_buffer_store_event (&event);
9197 last_tool_bar_item = -1;
9198 }
9199 }
9200
9201
9202 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9203 tool-bar window-relative coordinates X/Y. Called from
9204 note_mouse_highlight. */
9205
9206 static void
9207 note_tool_bar_highlight (f, x, y)
9208 struct frame *f;
9209 int x, y;
9210 {
9211 Lisp_Object window = f->tool_bar_window;
9212 struct window *w = XWINDOW (window);
9213 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9214 int hpos, vpos;
9215 struct glyph *glyph;
9216 struct glyph_row *row;
9217 int i;
9218 Lisp_Object enabled_p;
9219 int prop_idx;
9220 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9221 int mouse_down_p, rc;
9222
9223 /* Function note_mouse_highlight is called with negative x(y
9224 values when mouse moves outside of the frame. */
9225 if (x <= 0 || y <= 0)
9226 {
9227 clear_mouse_face (dpyinfo);
9228 return;
9229 }
9230
9231 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9232 if (rc < 0)
9233 {
9234 /* Not on tool-bar item. */
9235 clear_mouse_face (dpyinfo);
9236 return;
9237 }
9238 else if (rc == 0)
9239 /* On same tool-bar item as before. */
9240 goto set_help_echo;
9241
9242 clear_mouse_face (dpyinfo);
9243
9244 /* Mouse is down, but on different tool-bar item? */
9245 mouse_down_p = (dpyinfo->grabbed
9246 && f == last_mouse_frame
9247 && FRAME_LIVE_P (f));
9248 if (mouse_down_p
9249 && last_tool_bar_item != prop_idx)
9250 return;
9251
9252 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9253 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9254
9255 /* If tool-bar item is not enabled, don't highlight it. */
9256 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9257 if (!NILP (enabled_p))
9258 {
9259 /* Compute the x-position of the glyph. In front and past the
9260 image is a space. We include this in the highlighted area. */
9261 row = MATRIX_ROW (w->current_matrix, vpos);
9262 for (i = x = 0; i < hpos; ++i)
9263 x += row->glyphs[TEXT_AREA][i].pixel_width;
9264
9265 /* Record this as the current active region. */
9266 dpyinfo->mouse_face_beg_col = hpos;
9267 dpyinfo->mouse_face_beg_row = vpos;
9268 dpyinfo->mouse_face_beg_x = x;
9269 dpyinfo->mouse_face_beg_y = row->y;
9270 dpyinfo->mouse_face_past_end = 0;
9271
9272 dpyinfo->mouse_face_end_col = hpos + 1;
9273 dpyinfo->mouse_face_end_row = vpos;
9274 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9275 dpyinfo->mouse_face_end_y = row->y;
9276 dpyinfo->mouse_face_window = window;
9277 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9278
9279 /* Display it as active. */
9280 show_mouse_face (dpyinfo, draw);
9281 dpyinfo->mouse_face_image_state = draw;
9282 }
9283
9284 set_help_echo:
9285
9286 /* Set help_echo_string to a help string to display for this tool-bar item.
9287 XTread_socket does the rest. */
9288 help_echo_object = help_echo_window = Qnil;
9289 help_echo_pos = -1;
9290 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9291 if (NILP (help_echo_string))
9292 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9293 }
9294
9295 #endif /* HAVE_WINDOW_SYSTEM */
9296
9297
9298 \f
9299 /************************************************************************
9300 Horizontal scrolling
9301 ************************************************************************/
9302
9303 static int hscroll_window_tree P_ ((Lisp_Object));
9304 static int hscroll_windows P_ ((Lisp_Object));
9305
9306 /* For all leaf windows in the window tree rooted at WINDOW, set their
9307 hscroll value so that PT is (i) visible in the window, and (ii) so
9308 that it is not within a certain margin at the window's left and
9309 right border. Value is non-zero if any window's hscroll has been
9310 changed. */
9311
9312 static int
9313 hscroll_window_tree (window)
9314 Lisp_Object window;
9315 {
9316 int hscrolled_p = 0;
9317 int hscroll_relative_p = FLOATP (Vhscroll_step);
9318 int hscroll_step_abs = 0;
9319 double hscroll_step_rel = 0;
9320
9321 if (hscroll_relative_p)
9322 {
9323 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9324 if (hscroll_step_rel < 0)
9325 {
9326 hscroll_relative_p = 0;
9327 hscroll_step_abs = 0;
9328 }
9329 }
9330 else if (INTEGERP (Vhscroll_step))
9331 {
9332 hscroll_step_abs = XINT (Vhscroll_step);
9333 if (hscroll_step_abs < 0)
9334 hscroll_step_abs = 0;
9335 }
9336 else
9337 hscroll_step_abs = 0;
9338
9339 while (WINDOWP (window))
9340 {
9341 struct window *w = XWINDOW (window);
9342
9343 if (WINDOWP (w->hchild))
9344 hscrolled_p |= hscroll_window_tree (w->hchild);
9345 else if (WINDOWP (w->vchild))
9346 hscrolled_p |= hscroll_window_tree (w->vchild);
9347 else if (w->cursor.vpos >= 0)
9348 {
9349 int h_margin;
9350 int text_area_width;
9351 struct glyph_row *current_cursor_row
9352 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9353 struct glyph_row *desired_cursor_row
9354 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9355 struct glyph_row *cursor_row
9356 = (desired_cursor_row->enabled_p
9357 ? desired_cursor_row
9358 : current_cursor_row);
9359
9360 text_area_width = window_box_width (w, TEXT_AREA);
9361
9362 /* Scroll when cursor is inside this scroll margin. */
9363 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9364
9365 if ((XFASTINT (w->hscroll)
9366 && w->cursor.x <= h_margin)
9367 || (cursor_row->enabled_p
9368 && cursor_row->truncated_on_right_p
9369 && (w->cursor.x >= text_area_width - h_margin)))
9370 {
9371 struct it it;
9372 int hscroll;
9373 struct buffer *saved_current_buffer;
9374 int pt;
9375 int wanted_x;
9376
9377 /* Find point in a display of infinite width. */
9378 saved_current_buffer = current_buffer;
9379 current_buffer = XBUFFER (w->buffer);
9380
9381 if (w == XWINDOW (selected_window))
9382 pt = BUF_PT (current_buffer);
9383 else
9384 {
9385 pt = marker_position (w->pointm);
9386 pt = max (BEGV, pt);
9387 pt = min (ZV, pt);
9388 }
9389
9390 /* Move iterator to pt starting at cursor_row->start in
9391 a line with infinite width. */
9392 init_to_row_start (&it, w, cursor_row);
9393 it.last_visible_x = INFINITY;
9394 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9395 current_buffer = saved_current_buffer;
9396
9397 /* Position cursor in window. */
9398 if (!hscroll_relative_p && hscroll_step_abs == 0)
9399 hscroll = max (0, (it.current_x
9400 - (ITERATOR_AT_END_OF_LINE_P (&it)
9401 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9402 : (text_area_width / 2))))
9403 / FRAME_COLUMN_WIDTH (it.f);
9404 else if (w->cursor.x >= text_area_width - h_margin)
9405 {
9406 if (hscroll_relative_p)
9407 wanted_x = text_area_width * (1 - hscroll_step_rel)
9408 - h_margin;
9409 else
9410 wanted_x = text_area_width
9411 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9412 - h_margin;
9413 hscroll
9414 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9415 }
9416 else
9417 {
9418 if (hscroll_relative_p)
9419 wanted_x = text_area_width * hscroll_step_rel
9420 + h_margin;
9421 else
9422 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9423 + h_margin;
9424 hscroll
9425 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9426 }
9427 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9428
9429 /* Don't call Fset_window_hscroll if value hasn't
9430 changed because it will prevent redisplay
9431 optimizations. */
9432 if (XFASTINT (w->hscroll) != hscroll)
9433 {
9434 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9435 w->hscroll = make_number (hscroll);
9436 hscrolled_p = 1;
9437 }
9438 }
9439 }
9440
9441 window = w->next;
9442 }
9443
9444 /* Value is non-zero if hscroll of any leaf window has been changed. */
9445 return hscrolled_p;
9446 }
9447
9448
9449 /* Set hscroll so that cursor is visible and not inside horizontal
9450 scroll margins for all windows in the tree rooted at WINDOW. See
9451 also hscroll_window_tree above. Value is non-zero if any window's
9452 hscroll has been changed. If it has, desired matrices on the frame
9453 of WINDOW are cleared. */
9454
9455 static int
9456 hscroll_windows (window)
9457 Lisp_Object window;
9458 {
9459 int hscrolled_p;
9460
9461 if (automatic_hscrolling_p)
9462 {
9463 hscrolled_p = hscroll_window_tree (window);
9464 if (hscrolled_p)
9465 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9466 }
9467 else
9468 hscrolled_p = 0;
9469 return hscrolled_p;
9470 }
9471
9472
9473 \f
9474 /************************************************************************
9475 Redisplay
9476 ************************************************************************/
9477
9478 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9479 to a non-zero value. This is sometimes handy to have in a debugger
9480 session. */
9481
9482 #if GLYPH_DEBUG
9483
9484 /* First and last unchanged row for try_window_id. */
9485
9486 int debug_first_unchanged_at_end_vpos;
9487 int debug_last_unchanged_at_beg_vpos;
9488
9489 /* Delta vpos and y. */
9490
9491 int debug_dvpos, debug_dy;
9492
9493 /* Delta in characters and bytes for try_window_id. */
9494
9495 int debug_delta, debug_delta_bytes;
9496
9497 /* Values of window_end_pos and window_end_vpos at the end of
9498 try_window_id. */
9499
9500 EMACS_INT debug_end_pos, debug_end_vpos;
9501
9502 /* Append a string to W->desired_matrix->method. FMT is a printf
9503 format string. A1...A9 are a supplement for a variable-length
9504 argument list. If trace_redisplay_p is non-zero also printf the
9505 resulting string to stderr. */
9506
9507 static void
9508 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9509 struct window *w;
9510 char *fmt;
9511 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9512 {
9513 char buffer[512];
9514 char *method = w->desired_matrix->method;
9515 int len = strlen (method);
9516 int size = sizeof w->desired_matrix->method;
9517 int remaining = size - len - 1;
9518
9519 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9520 if (len && remaining)
9521 {
9522 method[len] = '|';
9523 --remaining, ++len;
9524 }
9525
9526 strncpy (method + len, buffer, remaining);
9527
9528 if (trace_redisplay_p)
9529 fprintf (stderr, "%p (%s): %s\n",
9530 w,
9531 ((BUFFERP (w->buffer)
9532 && STRINGP (XBUFFER (w->buffer)->name))
9533 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9534 : "no buffer"),
9535 buffer);
9536 }
9537
9538 #endif /* GLYPH_DEBUG */
9539
9540
9541 /* Value is non-zero if all changes in window W, which displays
9542 current_buffer, are in the text between START and END. START is a
9543 buffer position, END is given as a distance from Z. Used in
9544 redisplay_internal for display optimization. */
9545
9546 static INLINE int
9547 text_outside_line_unchanged_p (w, start, end)
9548 struct window *w;
9549 int start, end;
9550 {
9551 int unchanged_p = 1;
9552
9553 /* If text or overlays have changed, see where. */
9554 if (XFASTINT (w->last_modified) < MODIFF
9555 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9556 {
9557 /* Gap in the line? */
9558 if (GPT < start || Z - GPT < end)
9559 unchanged_p = 0;
9560
9561 /* Changes start in front of the line, or end after it? */
9562 if (unchanged_p
9563 && (BEG_UNCHANGED < start - 1
9564 || END_UNCHANGED < end))
9565 unchanged_p = 0;
9566
9567 /* If selective display, can't optimize if changes start at the
9568 beginning of the line. */
9569 if (unchanged_p
9570 && INTEGERP (current_buffer->selective_display)
9571 && XINT (current_buffer->selective_display) > 0
9572 && (BEG_UNCHANGED < start || GPT <= start))
9573 unchanged_p = 0;
9574
9575 /* If there are overlays at the start or end of the line, these
9576 may have overlay strings with newlines in them. A change at
9577 START, for instance, may actually concern the display of such
9578 overlay strings as well, and they are displayed on different
9579 lines. So, quickly rule out this case. (For the future, it
9580 might be desirable to implement something more telling than
9581 just BEG/END_UNCHANGED.) */
9582 if (unchanged_p)
9583 {
9584 if (BEG + BEG_UNCHANGED == start
9585 && overlay_touches_p (start))
9586 unchanged_p = 0;
9587 if (END_UNCHANGED == end
9588 && overlay_touches_p (Z - end))
9589 unchanged_p = 0;
9590 }
9591 }
9592
9593 return unchanged_p;
9594 }
9595
9596
9597 /* Do a frame update, taking possible shortcuts into account. This is
9598 the main external entry point for redisplay.
9599
9600 If the last redisplay displayed an echo area message and that message
9601 is no longer requested, we clear the echo area or bring back the
9602 mini-buffer if that is in use. */
9603
9604 void
9605 redisplay ()
9606 {
9607 redisplay_internal (0);
9608 }
9609
9610
9611 static Lisp_Object
9612 overlay_arrow_string_or_property (var, pbitmap)
9613 Lisp_Object var;
9614 int *pbitmap;
9615 {
9616 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9617 Lisp_Object bitmap;
9618
9619 if (pbitmap)
9620 {
9621 *pbitmap = 0;
9622 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9623 *pbitmap = XINT (bitmap);
9624 }
9625
9626 if (!NILP (pstr))
9627 return pstr;
9628 return Voverlay_arrow_string;
9629 }
9630
9631 /* Return 1 if there are any overlay-arrows in current_buffer. */
9632 static int
9633 overlay_arrow_in_current_buffer_p ()
9634 {
9635 Lisp_Object vlist;
9636
9637 for (vlist = Voverlay_arrow_variable_list;
9638 CONSP (vlist);
9639 vlist = XCDR (vlist))
9640 {
9641 Lisp_Object var = XCAR (vlist);
9642 Lisp_Object val;
9643
9644 if (!SYMBOLP (var))
9645 continue;
9646 val = find_symbol_value (var);
9647 if (MARKERP (val)
9648 && current_buffer == XMARKER (val)->buffer)
9649 return 1;
9650 }
9651 return 0;
9652 }
9653
9654
9655 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9656 has changed. */
9657
9658 static int
9659 overlay_arrows_changed_p ()
9660 {
9661 Lisp_Object vlist;
9662
9663 for (vlist = Voverlay_arrow_variable_list;
9664 CONSP (vlist);
9665 vlist = XCDR (vlist))
9666 {
9667 Lisp_Object var = XCAR (vlist);
9668 Lisp_Object val, pstr;
9669
9670 if (!SYMBOLP (var))
9671 continue;
9672 val = find_symbol_value (var);
9673 if (!MARKERP (val))
9674 continue;
9675 if (! EQ (COERCE_MARKER (val),
9676 Fget (var, Qlast_arrow_position))
9677 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9678 EQ (pstr, Fget (var, Qlast_arrow_string))))
9679 return 1;
9680 }
9681 return 0;
9682 }
9683
9684 /* Mark overlay arrows to be updated on next redisplay. */
9685
9686 static void
9687 update_overlay_arrows (up_to_date)
9688 int up_to_date;
9689 {
9690 Lisp_Object vlist;
9691
9692 for (vlist = Voverlay_arrow_variable_list;
9693 CONSP (vlist);
9694 vlist = XCDR (vlist))
9695 {
9696 Lisp_Object var = XCAR (vlist);
9697
9698 if (!SYMBOLP (var))
9699 continue;
9700
9701 if (up_to_date > 0)
9702 {
9703 Lisp_Object val = find_symbol_value (var);
9704 Fput (var, Qlast_arrow_position,
9705 COERCE_MARKER (val));
9706 Fput (var, Qlast_arrow_string,
9707 overlay_arrow_string_or_property (var, 0));
9708 }
9709 else if (up_to_date < 0
9710 || !NILP (Fget (var, Qlast_arrow_position)))
9711 {
9712 Fput (var, Qlast_arrow_position, Qt);
9713 Fput (var, Qlast_arrow_string, Qt);
9714 }
9715 }
9716 }
9717
9718
9719 /* Return overlay arrow string to display at row.
9720 Return t if display as bitmap in left fringe.
9721 Return nil if no overlay arrow. */
9722
9723 static Lisp_Object
9724 overlay_arrow_at_row (it, row, pbitmap)
9725 struct it *it;
9726 struct glyph_row *row;
9727 int *pbitmap;
9728 {
9729 Lisp_Object vlist;
9730
9731 for (vlist = Voverlay_arrow_variable_list;
9732 CONSP (vlist);
9733 vlist = XCDR (vlist))
9734 {
9735 Lisp_Object var = XCAR (vlist);
9736 Lisp_Object val;
9737
9738 if (!SYMBOLP (var))
9739 continue;
9740
9741 val = find_symbol_value (var);
9742
9743 if (MARKERP (val)
9744 && current_buffer == XMARKER (val)->buffer
9745 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9746 {
9747 val = overlay_arrow_string_or_property (var, pbitmap);
9748 if (FRAME_WINDOW_P (it->f)
9749 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9750 return Qt;
9751 if (STRINGP (val))
9752 return val;
9753 break;
9754 }
9755 }
9756
9757 *pbitmap = 0;
9758 return Qnil;
9759 }
9760
9761 /* Return 1 if point moved out of or into a composition. Otherwise
9762 return 0. PREV_BUF and PREV_PT are the last point buffer and
9763 position. BUF and PT are the current point buffer and position. */
9764
9765 int
9766 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9767 struct buffer *prev_buf, *buf;
9768 int prev_pt, pt;
9769 {
9770 int start, end;
9771 Lisp_Object prop;
9772 Lisp_Object buffer;
9773
9774 XSETBUFFER (buffer, buf);
9775 /* Check a composition at the last point if point moved within the
9776 same buffer. */
9777 if (prev_buf == buf)
9778 {
9779 if (prev_pt == pt)
9780 /* Point didn't move. */
9781 return 0;
9782
9783 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9784 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9785 && COMPOSITION_VALID_P (start, end, prop)
9786 && start < prev_pt && end > prev_pt)
9787 /* The last point was within the composition. Return 1 iff
9788 point moved out of the composition. */
9789 return (pt <= start || pt >= end);
9790 }
9791
9792 /* Check a composition at the current point. */
9793 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9794 && find_composition (pt, -1, &start, &end, &prop, buffer)
9795 && COMPOSITION_VALID_P (start, end, prop)
9796 && start < pt && end > pt);
9797 }
9798
9799
9800 /* Reconsider the setting of B->clip_changed which is displayed
9801 in window W. */
9802
9803 static INLINE void
9804 reconsider_clip_changes (w, b)
9805 struct window *w;
9806 struct buffer *b;
9807 {
9808 if (b->clip_changed
9809 && !NILP (w->window_end_valid)
9810 && w->current_matrix->buffer == b
9811 && w->current_matrix->zv == BUF_ZV (b)
9812 && w->current_matrix->begv == BUF_BEGV (b))
9813 b->clip_changed = 0;
9814
9815 /* If display wasn't paused, and W is not a tool bar window, see if
9816 point has been moved into or out of a composition. In that case,
9817 we set b->clip_changed to 1 to force updating the screen. If
9818 b->clip_changed has already been set to 1, we can skip this
9819 check. */
9820 if (!b->clip_changed
9821 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9822 {
9823 int pt;
9824
9825 if (w == XWINDOW (selected_window))
9826 pt = BUF_PT (current_buffer);
9827 else
9828 pt = marker_position (w->pointm);
9829
9830 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9831 || pt != XINT (w->last_point))
9832 && check_point_in_composition (w->current_matrix->buffer,
9833 XINT (w->last_point),
9834 XBUFFER (w->buffer), pt))
9835 b->clip_changed = 1;
9836 }
9837 }
9838 \f
9839
9840 /* Select FRAME to forward the values of frame-local variables into C
9841 variables so that the redisplay routines can access those values
9842 directly. */
9843
9844 static void
9845 select_frame_for_redisplay (frame)
9846 Lisp_Object frame;
9847 {
9848 Lisp_Object tail, sym, val;
9849 Lisp_Object old = selected_frame;
9850
9851 selected_frame = frame;
9852
9853 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9854 if (CONSP (XCAR (tail))
9855 && (sym = XCAR (XCAR (tail)),
9856 SYMBOLP (sym))
9857 && (sym = indirect_variable (sym),
9858 val = SYMBOL_VALUE (sym),
9859 (BUFFER_LOCAL_VALUEP (val)
9860 || SOME_BUFFER_LOCAL_VALUEP (val)))
9861 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9862 Fsymbol_value (sym);
9863
9864 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9865 if (CONSP (XCAR (tail))
9866 && (sym = XCAR (XCAR (tail)),
9867 SYMBOLP (sym))
9868 && (sym = indirect_variable (sym),
9869 val = SYMBOL_VALUE (sym),
9870 (BUFFER_LOCAL_VALUEP (val)
9871 || SOME_BUFFER_LOCAL_VALUEP (val)))
9872 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9873 Fsymbol_value (sym);
9874 }
9875
9876
9877 #define STOP_POLLING \
9878 do { if (! polling_stopped_here) stop_polling (); \
9879 polling_stopped_here = 1; } while (0)
9880
9881 #define RESUME_POLLING \
9882 do { if (polling_stopped_here) start_polling (); \
9883 polling_stopped_here = 0; } while (0)
9884
9885
9886 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9887 response to any user action; therefore, we should preserve the echo
9888 area. (Actually, our caller does that job.) Perhaps in the future
9889 avoid recentering windows if it is not necessary; currently that
9890 causes some problems. */
9891
9892 static void
9893 redisplay_internal (preserve_echo_area)
9894 int preserve_echo_area;
9895 {
9896 struct window *w = XWINDOW (selected_window);
9897 struct frame *f = XFRAME (w->frame);
9898 int pause;
9899 int must_finish = 0;
9900 struct text_pos tlbufpos, tlendpos;
9901 int number_of_visible_frames;
9902 int count;
9903 struct frame *sf = SELECTED_FRAME ();
9904 int polling_stopped_here = 0;
9905
9906 /* Non-zero means redisplay has to consider all windows on all
9907 frames. Zero means, only selected_window is considered. */
9908 int consider_all_windows_p;
9909
9910 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9911
9912 /* No redisplay if running in batch mode or frame is not yet fully
9913 initialized, or redisplay is explicitly turned off by setting
9914 Vinhibit_redisplay. */
9915 if (noninteractive
9916 || !NILP (Vinhibit_redisplay)
9917 || !f->glyphs_initialized_p)
9918 return;
9919
9920 /* The flag redisplay_performed_directly_p is set by
9921 direct_output_for_insert when it already did the whole screen
9922 update necessary. */
9923 if (redisplay_performed_directly_p)
9924 {
9925 redisplay_performed_directly_p = 0;
9926 if (!hscroll_windows (selected_window))
9927 return;
9928 }
9929
9930 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9931 if (popup_activated ())
9932 return;
9933 #endif
9934
9935 /* I don't think this happens but let's be paranoid. */
9936 if (redisplaying_p)
9937 return;
9938
9939 /* Record a function that resets redisplaying_p to its old value
9940 when we leave this function. */
9941 count = SPECPDL_INDEX ();
9942 record_unwind_protect (unwind_redisplay,
9943 Fcons (make_number (redisplaying_p), selected_frame));
9944 ++redisplaying_p;
9945 specbind (Qinhibit_free_realized_faces, Qnil);
9946
9947 retry:
9948 pause = 0;
9949 reconsider_clip_changes (w, current_buffer);
9950
9951 /* If new fonts have been loaded that make a glyph matrix adjustment
9952 necessary, do it. */
9953 if (fonts_changed_p)
9954 {
9955 adjust_glyphs (NULL);
9956 ++windows_or_buffers_changed;
9957 fonts_changed_p = 0;
9958 }
9959
9960 /* If face_change_count is non-zero, init_iterator will free all
9961 realized faces, which includes the faces referenced from current
9962 matrices. So, we can't reuse current matrices in this case. */
9963 if (face_change_count)
9964 ++windows_or_buffers_changed;
9965
9966 if (! FRAME_WINDOW_P (sf)
9967 && previous_terminal_frame != sf)
9968 {
9969 /* Since frames on an ASCII terminal share the same display
9970 area, displaying a different frame means redisplay the whole
9971 thing. */
9972 windows_or_buffers_changed++;
9973 SET_FRAME_GARBAGED (sf);
9974 XSETFRAME (Vterminal_frame, sf);
9975 }
9976 previous_terminal_frame = sf;
9977
9978 /* Set the visible flags for all frames. Do this before checking
9979 for resized or garbaged frames; they want to know if their frames
9980 are visible. See the comment in frame.h for
9981 FRAME_SAMPLE_VISIBILITY. */
9982 {
9983 Lisp_Object tail, frame;
9984
9985 number_of_visible_frames = 0;
9986
9987 FOR_EACH_FRAME (tail, frame)
9988 {
9989 struct frame *f = XFRAME (frame);
9990
9991 FRAME_SAMPLE_VISIBILITY (f);
9992 if (FRAME_VISIBLE_P (f))
9993 ++number_of_visible_frames;
9994 clear_desired_matrices (f);
9995 }
9996 }
9997
9998 /* Notice any pending interrupt request to change frame size. */
9999 do_pending_window_change (1);
10000
10001 /* Clear frames marked as garbaged. */
10002 if (frame_garbaged)
10003 clear_garbaged_frames ();
10004
10005 /* Build menubar and tool-bar items. */
10006 prepare_menu_bars ();
10007
10008 if (windows_or_buffers_changed)
10009 update_mode_lines++;
10010
10011 /* Detect case that we need to write or remove a star in the mode line. */
10012 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10013 {
10014 w->update_mode_line = Qt;
10015 if (buffer_shared > 1)
10016 update_mode_lines++;
10017 }
10018
10019 /* If %c is in the mode line, update it if needed. */
10020 if (!NILP (w->column_number_displayed)
10021 /* This alternative quickly identifies a common case
10022 where no change is needed. */
10023 && !(PT == XFASTINT (w->last_point)
10024 && XFASTINT (w->last_modified) >= MODIFF
10025 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10026 && (XFASTINT (w->column_number_displayed)
10027 != (int) current_column ())) /* iftc */
10028 w->update_mode_line = Qt;
10029
10030 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10031
10032 /* The variable buffer_shared is set in redisplay_window and
10033 indicates that we redisplay a buffer in different windows. See
10034 there. */
10035 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10036 || cursor_type_changed);
10037
10038 /* If specs for an arrow have changed, do thorough redisplay
10039 to ensure we remove any arrow that should no longer exist. */
10040 if (overlay_arrows_changed_p ())
10041 consider_all_windows_p = windows_or_buffers_changed = 1;
10042
10043 /* Normally the message* functions will have already displayed and
10044 updated the echo area, but the frame may have been trashed, or
10045 the update may have been preempted, so display the echo area
10046 again here. Checking message_cleared_p captures the case that
10047 the echo area should be cleared. */
10048 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10049 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10050 || (message_cleared_p
10051 && minibuf_level == 0
10052 /* If the mini-window is currently selected, this means the
10053 echo-area doesn't show through. */
10054 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10055 {
10056 int window_height_changed_p = echo_area_display (0);
10057 must_finish = 1;
10058
10059 /* If we don't display the current message, don't clear the
10060 message_cleared_p flag, because, if we did, we wouldn't clear
10061 the echo area in the next redisplay which doesn't preserve
10062 the echo area. */
10063 if (!display_last_displayed_message_p)
10064 message_cleared_p = 0;
10065
10066 if (fonts_changed_p)
10067 goto retry;
10068 else if (window_height_changed_p)
10069 {
10070 consider_all_windows_p = 1;
10071 ++update_mode_lines;
10072 ++windows_or_buffers_changed;
10073
10074 /* If window configuration was changed, frames may have been
10075 marked garbaged. Clear them or we will experience
10076 surprises wrt scrolling. */
10077 if (frame_garbaged)
10078 clear_garbaged_frames ();
10079 }
10080 }
10081 else if (EQ (selected_window, minibuf_window)
10082 && (current_buffer->clip_changed
10083 || XFASTINT (w->last_modified) < MODIFF
10084 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10085 && resize_mini_window (w, 0))
10086 {
10087 /* Resized active mini-window to fit the size of what it is
10088 showing if its contents might have changed. */
10089 must_finish = 1;
10090 consider_all_windows_p = 1;
10091 ++windows_or_buffers_changed;
10092 ++update_mode_lines;
10093
10094 /* If window configuration was changed, frames may have been
10095 marked garbaged. Clear them or we will experience
10096 surprises wrt scrolling. */
10097 if (frame_garbaged)
10098 clear_garbaged_frames ();
10099 }
10100
10101
10102 /* If showing the region, and mark has changed, we must redisplay
10103 the whole window. The assignment to this_line_start_pos prevents
10104 the optimization directly below this if-statement. */
10105 if (((!NILP (Vtransient_mark_mode)
10106 && !NILP (XBUFFER (w->buffer)->mark_active))
10107 != !NILP (w->region_showing))
10108 || (!NILP (w->region_showing)
10109 && !EQ (w->region_showing,
10110 Fmarker_position (XBUFFER (w->buffer)->mark))))
10111 CHARPOS (this_line_start_pos) = 0;
10112
10113 /* Optimize the case that only the line containing the cursor in the
10114 selected window has changed. Variables starting with this_ are
10115 set in display_line and record information about the line
10116 containing the cursor. */
10117 tlbufpos = this_line_start_pos;
10118 tlendpos = this_line_end_pos;
10119 if (!consider_all_windows_p
10120 && CHARPOS (tlbufpos) > 0
10121 && NILP (w->update_mode_line)
10122 && !current_buffer->clip_changed
10123 && !current_buffer->prevent_redisplay_optimizations_p
10124 && FRAME_VISIBLE_P (XFRAME (w->frame))
10125 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10126 /* Make sure recorded data applies to current buffer, etc. */
10127 && this_line_buffer == current_buffer
10128 && current_buffer == XBUFFER (w->buffer)
10129 && NILP (w->force_start)
10130 && NILP (w->optional_new_start)
10131 /* Point must be on the line that we have info recorded about. */
10132 && PT >= CHARPOS (tlbufpos)
10133 && PT <= Z - CHARPOS (tlendpos)
10134 /* All text outside that line, including its final newline,
10135 must be unchanged */
10136 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10137 CHARPOS (tlendpos)))
10138 {
10139 if (CHARPOS (tlbufpos) > BEGV
10140 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10141 && (CHARPOS (tlbufpos) == ZV
10142 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10143 /* Former continuation line has disappeared by becoming empty */
10144 goto cancel;
10145 else if (XFASTINT (w->last_modified) < MODIFF
10146 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10147 || MINI_WINDOW_P (w))
10148 {
10149 /* We have to handle the case of continuation around a
10150 wide-column character (See the comment in indent.c around
10151 line 885).
10152
10153 For instance, in the following case:
10154
10155 -------- Insert --------
10156 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10157 J_I_ ==> J_I_ `^^' are cursors.
10158 ^^ ^^
10159 -------- --------
10160
10161 As we have to redraw the line above, we should goto cancel. */
10162
10163 struct it it;
10164 int line_height_before = this_line_pixel_height;
10165
10166 /* Note that start_display will handle the case that the
10167 line starting at tlbufpos is a continuation lines. */
10168 start_display (&it, w, tlbufpos);
10169
10170 /* Implementation note: It this still necessary? */
10171 if (it.current_x != this_line_start_x)
10172 goto cancel;
10173
10174 TRACE ((stderr, "trying display optimization 1\n"));
10175 w->cursor.vpos = -1;
10176 overlay_arrow_seen = 0;
10177 it.vpos = this_line_vpos;
10178 it.current_y = this_line_y;
10179 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10180 display_line (&it);
10181
10182 /* If line contains point, is not continued,
10183 and ends at same distance from eob as before, we win */
10184 if (w->cursor.vpos >= 0
10185 /* Line is not continued, otherwise this_line_start_pos
10186 would have been set to 0 in display_line. */
10187 && CHARPOS (this_line_start_pos)
10188 /* Line ends as before. */
10189 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10190 /* Line has same height as before. Otherwise other lines
10191 would have to be shifted up or down. */
10192 && this_line_pixel_height == line_height_before)
10193 {
10194 /* If this is not the window's last line, we must adjust
10195 the charstarts of the lines below. */
10196 if (it.current_y < it.last_visible_y)
10197 {
10198 struct glyph_row *row
10199 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10200 int delta, delta_bytes;
10201
10202 if (Z - CHARPOS (tlendpos) == ZV)
10203 {
10204 /* This line ends at end of (accessible part of)
10205 buffer. There is no newline to count. */
10206 delta = (Z
10207 - CHARPOS (tlendpos)
10208 - MATRIX_ROW_START_CHARPOS (row));
10209 delta_bytes = (Z_BYTE
10210 - BYTEPOS (tlendpos)
10211 - MATRIX_ROW_START_BYTEPOS (row));
10212 }
10213 else
10214 {
10215 /* This line ends in a newline. Must take
10216 account of the newline and the rest of the
10217 text that follows. */
10218 delta = (Z
10219 - CHARPOS (tlendpos)
10220 - MATRIX_ROW_START_CHARPOS (row));
10221 delta_bytes = (Z_BYTE
10222 - BYTEPOS (tlendpos)
10223 - MATRIX_ROW_START_BYTEPOS (row));
10224 }
10225
10226 increment_matrix_positions (w->current_matrix,
10227 this_line_vpos + 1,
10228 w->current_matrix->nrows,
10229 delta, delta_bytes);
10230 }
10231
10232 /* If this row displays text now but previously didn't,
10233 or vice versa, w->window_end_vpos may have to be
10234 adjusted. */
10235 if ((it.glyph_row - 1)->displays_text_p)
10236 {
10237 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10238 XSETINT (w->window_end_vpos, this_line_vpos);
10239 }
10240 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10241 && this_line_vpos > 0)
10242 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10243 w->window_end_valid = Qnil;
10244
10245 /* Update hint: No need to try to scroll in update_window. */
10246 w->desired_matrix->no_scrolling_p = 1;
10247
10248 #if GLYPH_DEBUG
10249 *w->desired_matrix->method = 0;
10250 debug_method_add (w, "optimization 1");
10251 #endif
10252 #ifdef HAVE_WINDOW_SYSTEM
10253 update_window_fringes (w, 0);
10254 #endif
10255 goto update;
10256 }
10257 else
10258 goto cancel;
10259 }
10260 else if (/* Cursor position hasn't changed. */
10261 PT == XFASTINT (w->last_point)
10262 /* Make sure the cursor was last displayed
10263 in this window. Otherwise we have to reposition it. */
10264 && 0 <= w->cursor.vpos
10265 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10266 {
10267 if (!must_finish)
10268 {
10269 do_pending_window_change (1);
10270
10271 /* We used to always goto end_of_redisplay here, but this
10272 isn't enough if we have a blinking cursor. */
10273 if (w->cursor_off_p == w->last_cursor_off_p)
10274 goto end_of_redisplay;
10275 }
10276 goto update;
10277 }
10278 /* If highlighting the region, or if the cursor is in the echo area,
10279 then we can't just move the cursor. */
10280 else if (! (!NILP (Vtransient_mark_mode)
10281 && !NILP (current_buffer->mark_active))
10282 && (EQ (selected_window, current_buffer->last_selected_window)
10283 || highlight_nonselected_windows)
10284 && NILP (w->region_showing)
10285 && NILP (Vshow_trailing_whitespace)
10286 && !cursor_in_echo_area)
10287 {
10288 struct it it;
10289 struct glyph_row *row;
10290
10291 /* Skip from tlbufpos to PT and see where it is. Note that
10292 PT may be in invisible text. If so, we will end at the
10293 next visible position. */
10294 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10295 NULL, DEFAULT_FACE_ID);
10296 it.current_x = this_line_start_x;
10297 it.current_y = this_line_y;
10298 it.vpos = this_line_vpos;
10299
10300 /* The call to move_it_to stops in front of PT, but
10301 moves over before-strings. */
10302 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10303
10304 if (it.vpos == this_line_vpos
10305 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10306 row->enabled_p))
10307 {
10308 xassert (this_line_vpos == it.vpos);
10309 xassert (this_line_y == it.current_y);
10310 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10311 #if GLYPH_DEBUG
10312 *w->desired_matrix->method = 0;
10313 debug_method_add (w, "optimization 3");
10314 #endif
10315 goto update;
10316 }
10317 else
10318 goto cancel;
10319 }
10320
10321 cancel:
10322 /* Text changed drastically or point moved off of line. */
10323 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10324 }
10325
10326 CHARPOS (this_line_start_pos) = 0;
10327 consider_all_windows_p |= buffer_shared > 1;
10328 ++clear_face_cache_count;
10329
10330
10331 /* Build desired matrices, and update the display. If
10332 consider_all_windows_p is non-zero, do it for all windows on all
10333 frames. Otherwise do it for selected_window, only. */
10334
10335 if (consider_all_windows_p)
10336 {
10337 Lisp_Object tail, frame;
10338 int i, n = 0, size = 50;
10339 struct frame **updated
10340 = (struct frame **) alloca (size * sizeof *updated);
10341
10342 /* Clear the face cache eventually. */
10343 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10344 {
10345 clear_face_cache (0);
10346 clear_face_cache_count = 0;
10347 }
10348
10349 /* Recompute # windows showing selected buffer. This will be
10350 incremented each time such a window is displayed. */
10351 buffer_shared = 0;
10352
10353 FOR_EACH_FRAME (tail, frame)
10354 {
10355 struct frame *f = XFRAME (frame);
10356
10357 if (FRAME_WINDOW_P (f) || f == sf)
10358 {
10359 if (! EQ (frame, selected_frame))
10360 /* Select the frame, for the sake of frame-local
10361 variables. */
10362 select_frame_for_redisplay (frame);
10363
10364 #ifdef HAVE_WINDOW_SYSTEM
10365 if (clear_face_cache_count % 50 == 0
10366 && FRAME_WINDOW_P (f))
10367 clear_image_cache (f, 0);
10368 #endif /* HAVE_WINDOW_SYSTEM */
10369
10370 /* Mark all the scroll bars to be removed; we'll redeem
10371 the ones we want when we redisplay their windows. */
10372 if (condemn_scroll_bars_hook)
10373 condemn_scroll_bars_hook (f);
10374
10375 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10376 redisplay_windows (FRAME_ROOT_WINDOW (f));
10377
10378 /* Any scroll bars which redisplay_windows should have
10379 nuked should now go away. */
10380 if (judge_scroll_bars_hook)
10381 judge_scroll_bars_hook (f);
10382
10383 /* If fonts changed, display again. */
10384 /* ??? rms: I suspect it is a mistake to jump all the way
10385 back to retry here. It should just retry this frame. */
10386 if (fonts_changed_p)
10387 goto retry;
10388
10389 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10390 {
10391 /* See if we have to hscroll. */
10392 if (hscroll_windows (f->root_window))
10393 goto retry;
10394
10395 /* Prevent various kinds of signals during display
10396 update. stdio is not robust about handling
10397 signals, which can cause an apparent I/O
10398 error. */
10399 if (interrupt_input)
10400 unrequest_sigio ();
10401 STOP_POLLING;
10402
10403 /* Update the display. */
10404 set_window_update_flags (XWINDOW (f->root_window), 1);
10405 pause |= update_frame (f, 0, 0);
10406 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10407 if (pause)
10408 break;
10409 #endif
10410
10411 if (n == size)
10412 {
10413 int nbytes = size * sizeof *updated;
10414 struct frame **p = (struct frame **) alloca (2 * nbytes);
10415 bcopy (updated, p, nbytes);
10416 size *= 2;
10417 }
10418
10419 updated[n++] = f;
10420 }
10421 }
10422 }
10423
10424 if (!pause)
10425 {
10426 /* Do the mark_window_display_accurate after all windows have
10427 been redisplayed because this call resets flags in buffers
10428 which are needed for proper redisplay. */
10429 for (i = 0; i < n; ++i)
10430 {
10431 struct frame *f = updated[i];
10432 mark_window_display_accurate (f->root_window, 1);
10433 if (frame_up_to_date_hook)
10434 frame_up_to_date_hook (f);
10435 }
10436 }
10437 }
10438 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10439 {
10440 Lisp_Object mini_window;
10441 struct frame *mini_frame;
10442
10443 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10444 /* Use list_of_error, not Qerror, so that
10445 we catch only errors and don't run the debugger. */
10446 internal_condition_case_1 (redisplay_window_1, selected_window,
10447 list_of_error,
10448 redisplay_window_error);
10449
10450 /* Compare desired and current matrices, perform output. */
10451
10452 update:
10453 /* If fonts changed, display again. */
10454 if (fonts_changed_p)
10455 goto retry;
10456
10457 /* Prevent various kinds of signals during display update.
10458 stdio is not robust about handling signals,
10459 which can cause an apparent I/O error. */
10460 if (interrupt_input)
10461 unrequest_sigio ();
10462 STOP_POLLING;
10463
10464 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10465 {
10466 if (hscroll_windows (selected_window))
10467 goto retry;
10468
10469 XWINDOW (selected_window)->must_be_updated_p = 1;
10470 pause = update_frame (sf, 0, 0);
10471 }
10472
10473 /* We may have called echo_area_display at the top of this
10474 function. If the echo area is on another frame, that may
10475 have put text on a frame other than the selected one, so the
10476 above call to update_frame would not have caught it. Catch
10477 it here. */
10478 mini_window = FRAME_MINIBUF_WINDOW (sf);
10479 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10480
10481 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10482 {
10483 XWINDOW (mini_window)->must_be_updated_p = 1;
10484 pause |= update_frame (mini_frame, 0, 0);
10485 if (!pause && hscroll_windows (mini_window))
10486 goto retry;
10487 }
10488 }
10489
10490 /* If display was paused because of pending input, make sure we do a
10491 thorough update the next time. */
10492 if (pause)
10493 {
10494 /* Prevent the optimization at the beginning of
10495 redisplay_internal that tries a single-line update of the
10496 line containing the cursor in the selected window. */
10497 CHARPOS (this_line_start_pos) = 0;
10498
10499 /* Let the overlay arrow be updated the next time. */
10500 update_overlay_arrows (0);
10501
10502 /* If we pause after scrolling, some rows in the current
10503 matrices of some windows are not valid. */
10504 if (!WINDOW_FULL_WIDTH_P (w)
10505 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10506 update_mode_lines = 1;
10507 }
10508 else
10509 {
10510 if (!consider_all_windows_p)
10511 {
10512 /* This has already been done above if
10513 consider_all_windows_p is set. */
10514 mark_window_display_accurate_1 (w, 1);
10515
10516 /* Say overlay arrows are up to date. */
10517 update_overlay_arrows (1);
10518
10519 if (frame_up_to_date_hook != 0)
10520 frame_up_to_date_hook (sf);
10521 }
10522
10523 update_mode_lines = 0;
10524 windows_or_buffers_changed = 0;
10525 cursor_type_changed = 0;
10526 }
10527
10528 /* Start SIGIO interrupts coming again. Having them off during the
10529 code above makes it less likely one will discard output, but not
10530 impossible, since there might be stuff in the system buffer here.
10531 But it is much hairier to try to do anything about that. */
10532 if (interrupt_input)
10533 request_sigio ();
10534 RESUME_POLLING;
10535
10536 /* If a frame has become visible which was not before, redisplay
10537 again, so that we display it. Expose events for such a frame
10538 (which it gets when becoming visible) don't call the parts of
10539 redisplay constructing glyphs, so simply exposing a frame won't
10540 display anything in this case. So, we have to display these
10541 frames here explicitly. */
10542 if (!pause)
10543 {
10544 Lisp_Object tail, frame;
10545 int new_count = 0;
10546
10547 FOR_EACH_FRAME (tail, frame)
10548 {
10549 int this_is_visible = 0;
10550
10551 if (XFRAME (frame)->visible)
10552 this_is_visible = 1;
10553 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10554 if (XFRAME (frame)->visible)
10555 this_is_visible = 1;
10556
10557 if (this_is_visible)
10558 new_count++;
10559 }
10560
10561 if (new_count != number_of_visible_frames)
10562 windows_or_buffers_changed++;
10563 }
10564
10565 /* Change frame size now if a change is pending. */
10566 do_pending_window_change (1);
10567
10568 /* If we just did a pending size change, or have additional
10569 visible frames, redisplay again. */
10570 if (windows_or_buffers_changed && !pause)
10571 goto retry;
10572
10573 end_of_redisplay:
10574 unbind_to (count, Qnil);
10575 RESUME_POLLING;
10576 }
10577
10578
10579 /* Redisplay, but leave alone any recent echo area message unless
10580 another message has been requested in its place.
10581
10582 This is useful in situations where you need to redisplay but no
10583 user action has occurred, making it inappropriate for the message
10584 area to be cleared. See tracking_off and
10585 wait_reading_process_output for examples of these situations.
10586
10587 FROM_WHERE is an integer saying from where this function was
10588 called. This is useful for debugging. */
10589
10590 void
10591 redisplay_preserve_echo_area (from_where)
10592 int from_where;
10593 {
10594 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10595
10596 if (!NILP (echo_area_buffer[1]))
10597 {
10598 /* We have a previously displayed message, but no current
10599 message. Redisplay the previous message. */
10600 display_last_displayed_message_p = 1;
10601 redisplay_internal (1);
10602 display_last_displayed_message_p = 0;
10603 }
10604 else
10605 redisplay_internal (1);
10606
10607 if (rif != NULL && rif->flush_display_optional)
10608 rif->flush_display_optional (NULL);
10609 }
10610
10611
10612 /* Function registered with record_unwind_protect in
10613 redisplay_internal. Reset redisplaying_p to the value it had
10614 before redisplay_internal was called, and clear
10615 prevent_freeing_realized_faces_p. It also selects the previously
10616 selected frame. */
10617
10618 static Lisp_Object
10619 unwind_redisplay (val)
10620 Lisp_Object val;
10621 {
10622 Lisp_Object old_redisplaying_p, old_frame;
10623
10624 old_redisplaying_p = XCAR (val);
10625 redisplaying_p = XFASTINT (old_redisplaying_p);
10626 old_frame = XCDR (val);
10627 if (! EQ (old_frame, selected_frame))
10628 select_frame_for_redisplay (old_frame);
10629 return Qnil;
10630 }
10631
10632
10633 /* Mark the display of window W as accurate or inaccurate. If
10634 ACCURATE_P is non-zero mark display of W as accurate. If
10635 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10636 redisplay_internal is called. */
10637
10638 static void
10639 mark_window_display_accurate_1 (w, accurate_p)
10640 struct window *w;
10641 int accurate_p;
10642 {
10643 if (BUFFERP (w->buffer))
10644 {
10645 struct buffer *b = XBUFFER (w->buffer);
10646
10647 w->last_modified
10648 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10649 w->last_overlay_modified
10650 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10651 w->last_had_star
10652 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10653
10654 if (accurate_p)
10655 {
10656 b->clip_changed = 0;
10657 b->prevent_redisplay_optimizations_p = 0;
10658
10659 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10660 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10661 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10662 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10663
10664 w->current_matrix->buffer = b;
10665 w->current_matrix->begv = BUF_BEGV (b);
10666 w->current_matrix->zv = BUF_ZV (b);
10667
10668 w->last_cursor = w->cursor;
10669 w->last_cursor_off_p = w->cursor_off_p;
10670
10671 if (w == XWINDOW (selected_window))
10672 w->last_point = make_number (BUF_PT (b));
10673 else
10674 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10675 }
10676 }
10677
10678 if (accurate_p)
10679 {
10680 w->window_end_valid = w->buffer;
10681 #if 0 /* This is incorrect with variable-height lines. */
10682 xassert (XINT (w->window_end_vpos)
10683 < (WINDOW_TOTAL_LINES (w)
10684 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10685 #endif
10686 w->update_mode_line = Qnil;
10687 }
10688 }
10689
10690
10691 /* Mark the display of windows in the window tree rooted at WINDOW as
10692 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10693 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10694 be redisplayed the next time redisplay_internal is called. */
10695
10696 void
10697 mark_window_display_accurate (window, accurate_p)
10698 Lisp_Object window;
10699 int accurate_p;
10700 {
10701 struct window *w;
10702
10703 for (; !NILP (window); window = w->next)
10704 {
10705 w = XWINDOW (window);
10706 mark_window_display_accurate_1 (w, accurate_p);
10707
10708 if (!NILP (w->vchild))
10709 mark_window_display_accurate (w->vchild, accurate_p);
10710 if (!NILP (w->hchild))
10711 mark_window_display_accurate (w->hchild, accurate_p);
10712 }
10713
10714 if (accurate_p)
10715 {
10716 update_overlay_arrows (1);
10717 }
10718 else
10719 {
10720 /* Force a thorough redisplay the next time by setting
10721 last_arrow_position and last_arrow_string to t, which is
10722 unequal to any useful value of Voverlay_arrow_... */
10723 update_overlay_arrows (-1);
10724 }
10725 }
10726
10727
10728 /* Return value in display table DP (Lisp_Char_Table *) for character
10729 C. Since a display table doesn't have any parent, we don't have to
10730 follow parent. Do not call this function directly but use the
10731 macro DISP_CHAR_VECTOR. */
10732
10733 Lisp_Object
10734 disp_char_vector (dp, c)
10735 struct Lisp_Char_Table *dp;
10736 int c;
10737 {
10738 int code[4], i;
10739 Lisp_Object val;
10740
10741 if (SINGLE_BYTE_CHAR_P (c))
10742 return (dp->contents[c]);
10743
10744 SPLIT_CHAR (c, code[0], code[1], code[2]);
10745 if (code[1] < 32)
10746 code[1] = -1;
10747 else if (code[2] < 32)
10748 code[2] = -1;
10749
10750 /* Here, the possible range of code[0] (== charset ID) is
10751 128..max_charset. Since the top level char table contains data
10752 for multibyte characters after 256th element, we must increment
10753 code[0] by 128 to get a correct index. */
10754 code[0] += 128;
10755 code[3] = -1; /* anchor */
10756
10757 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10758 {
10759 val = dp->contents[code[i]];
10760 if (!SUB_CHAR_TABLE_P (val))
10761 return (NILP (val) ? dp->defalt : val);
10762 }
10763
10764 /* Here, val is a sub char table. We return the default value of
10765 it. */
10766 return (dp->defalt);
10767 }
10768
10769
10770 \f
10771 /***********************************************************************
10772 Window Redisplay
10773 ***********************************************************************/
10774
10775 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10776
10777 static void
10778 redisplay_windows (window)
10779 Lisp_Object window;
10780 {
10781 while (!NILP (window))
10782 {
10783 struct window *w = XWINDOW (window);
10784
10785 if (!NILP (w->hchild))
10786 redisplay_windows (w->hchild);
10787 else if (!NILP (w->vchild))
10788 redisplay_windows (w->vchild);
10789 else
10790 {
10791 displayed_buffer = XBUFFER (w->buffer);
10792 /* Use list_of_error, not Qerror, so that
10793 we catch only errors and don't run the debugger. */
10794 internal_condition_case_1 (redisplay_window_0, window,
10795 list_of_error,
10796 redisplay_window_error);
10797 }
10798
10799 window = w->next;
10800 }
10801 }
10802
10803 static Lisp_Object
10804 redisplay_window_error ()
10805 {
10806 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10807 return Qnil;
10808 }
10809
10810 static Lisp_Object
10811 redisplay_window_0 (window)
10812 Lisp_Object window;
10813 {
10814 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10815 redisplay_window (window, 0);
10816 return Qnil;
10817 }
10818
10819 static Lisp_Object
10820 redisplay_window_1 (window)
10821 Lisp_Object window;
10822 {
10823 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10824 redisplay_window (window, 1);
10825 return Qnil;
10826 }
10827 \f
10828
10829 /* Increment GLYPH until it reaches END or CONDITION fails while
10830 adding (GLYPH)->pixel_width to X. */
10831
10832 #define SKIP_GLYPHS(glyph, end, x, condition) \
10833 do \
10834 { \
10835 (x) += (glyph)->pixel_width; \
10836 ++(glyph); \
10837 } \
10838 while ((glyph) < (end) && (condition))
10839
10840
10841 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10842 DELTA is the number of bytes by which positions recorded in ROW
10843 differ from current buffer positions. */
10844
10845 void
10846 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10847 struct window *w;
10848 struct glyph_row *row;
10849 struct glyph_matrix *matrix;
10850 int delta, delta_bytes, dy, dvpos;
10851 {
10852 struct glyph *glyph = row->glyphs[TEXT_AREA];
10853 struct glyph *end = glyph + row->used[TEXT_AREA];
10854 struct glyph *cursor = NULL;
10855 /* The first glyph that starts a sequence of glyphs from string. */
10856 struct glyph *string_start;
10857 /* The X coordinate of string_start. */
10858 int string_start_x;
10859 /* The last known character position. */
10860 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10861 /* The last known character position before string_start. */
10862 int string_before_pos;
10863 int x = row->x;
10864 int cursor_x = x;
10865 int cursor_from_overlay_pos = 0;
10866 int pt_old = PT - delta;
10867
10868 /* Skip over glyphs not having an object at the start of the row.
10869 These are special glyphs like truncation marks on terminal
10870 frames. */
10871 if (row->displays_text_p)
10872 while (glyph < end
10873 && INTEGERP (glyph->object)
10874 && glyph->charpos < 0)
10875 {
10876 x += glyph->pixel_width;
10877 ++glyph;
10878 }
10879
10880 string_start = NULL;
10881 while (glyph < end
10882 && !INTEGERP (glyph->object)
10883 && (!BUFFERP (glyph->object)
10884 || (last_pos = glyph->charpos) < pt_old))
10885 {
10886 if (! STRINGP (glyph->object))
10887 {
10888 string_start = NULL;
10889 x += glyph->pixel_width;
10890 ++glyph;
10891 if (cursor_from_overlay_pos
10892 && last_pos > cursor_from_overlay_pos)
10893 {
10894 cursor_from_overlay_pos = 0;
10895 cursor = 0;
10896 }
10897 }
10898 else
10899 {
10900 string_before_pos = last_pos;
10901 string_start = glyph;
10902 string_start_x = x;
10903 /* Skip all glyphs from string. */
10904 do
10905 {
10906 int pos;
10907 if ((cursor == NULL || glyph > cursor)
10908 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10909 Qcursor, (glyph)->object))
10910 && (pos = string_buffer_position (w, glyph->object,
10911 string_before_pos),
10912 (pos == 0 /* From overlay */
10913 || pos == pt_old)))
10914 {
10915 /* Estimate overlay buffer position from the buffer
10916 positions of the glyphs before and after the overlay.
10917 Add 1 to last_pos so that if point corresponds to the
10918 glyph right after the overlay, we still use a 'cursor'
10919 property found in that overlay. */
10920 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10921 cursor = glyph;
10922 cursor_x = x;
10923 }
10924 x += glyph->pixel_width;
10925 ++glyph;
10926 }
10927 while (glyph < end && STRINGP (glyph->object));
10928 }
10929 }
10930
10931 if (cursor != NULL)
10932 {
10933 glyph = cursor;
10934 x = cursor_x;
10935 }
10936 else if (row->ends_in_ellipsis_p && glyph == end)
10937 {
10938 /* Scan back over the ellipsis glyphs, decrementing positions. */
10939 while (glyph > row->glyphs[TEXT_AREA]
10940 && (glyph - 1)->charpos == last_pos)
10941 glyph--, x -= glyph->pixel_width;
10942 /* That loop always goes one position too far,
10943 including the glyph before the ellipsis.
10944 So scan forward over that one. */
10945 x += glyph->pixel_width;
10946 glyph++;
10947 }
10948 else if (string_start
10949 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10950 {
10951 /* We may have skipped over point because the previous glyphs
10952 are from string. As there's no easy way to know the
10953 character position of the current glyph, find the correct
10954 glyph on point by scanning from string_start again. */
10955 Lisp_Object limit;
10956 Lisp_Object string;
10957 int pos;
10958
10959 limit = make_number (pt_old + 1);
10960 end = glyph;
10961 glyph = string_start;
10962 x = string_start_x;
10963 string = glyph->object;
10964 pos = string_buffer_position (w, string, string_before_pos);
10965 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
10966 because we always put cursor after overlay strings. */
10967 while (pos == 0 && glyph < end)
10968 {
10969 string = glyph->object;
10970 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10971 if (glyph < end)
10972 pos = string_buffer_position (w, glyph->object, string_before_pos);
10973 }
10974
10975 while (glyph < end)
10976 {
10977 pos = XINT (Fnext_single_char_property_change
10978 (make_number (pos), Qdisplay, Qnil, limit));
10979 if (pos > pt_old)
10980 break;
10981 /* Skip glyphs from the same string. */
10982 string = glyph->object;
10983 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10984 /* Skip glyphs from an overlay. */
10985 while (glyph < end
10986 && ! string_buffer_position (w, glyph->object, pos))
10987 {
10988 string = glyph->object;
10989 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
10990 }
10991 }
10992 }
10993
10994 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
10995 w->cursor.x = x;
10996 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
10997 w->cursor.y = row->y + dy;
10998
10999 if (w == XWINDOW (selected_window))
11000 {
11001 if (!row->continued_p
11002 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11003 && row->x == 0)
11004 {
11005 this_line_buffer = XBUFFER (w->buffer);
11006
11007 CHARPOS (this_line_start_pos)
11008 = MATRIX_ROW_START_CHARPOS (row) + delta;
11009 BYTEPOS (this_line_start_pos)
11010 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11011
11012 CHARPOS (this_line_end_pos)
11013 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11014 BYTEPOS (this_line_end_pos)
11015 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11016
11017 this_line_y = w->cursor.y;
11018 this_line_pixel_height = row->height;
11019 this_line_vpos = w->cursor.vpos;
11020 this_line_start_x = row->x;
11021 }
11022 else
11023 CHARPOS (this_line_start_pos) = 0;
11024 }
11025 }
11026
11027
11028 /* Run window scroll functions, if any, for WINDOW with new window
11029 start STARTP. Sets the window start of WINDOW to that position.
11030
11031 We assume that the window's buffer is really current. */
11032
11033 static INLINE struct text_pos
11034 run_window_scroll_functions (window, startp)
11035 Lisp_Object window;
11036 struct text_pos startp;
11037 {
11038 struct window *w = XWINDOW (window);
11039 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11040
11041 if (current_buffer != XBUFFER (w->buffer))
11042 abort ();
11043
11044 if (!NILP (Vwindow_scroll_functions))
11045 {
11046 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11047 make_number (CHARPOS (startp)));
11048 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11049 /* In case the hook functions switch buffers. */
11050 if (current_buffer != XBUFFER (w->buffer))
11051 set_buffer_internal_1 (XBUFFER (w->buffer));
11052 }
11053
11054 return startp;
11055 }
11056
11057
11058 /* Make sure the line containing the cursor is fully visible.
11059 A value of 1 means there is nothing to be done.
11060 (Either the line is fully visible, or it cannot be made so,
11061 or we cannot tell.)
11062
11063 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11064 is higher than window.
11065
11066 A value of 0 means the caller should do scrolling
11067 as if point had gone off the screen. */
11068
11069 static int
11070 make_cursor_line_fully_visible (w, force_p)
11071 struct window *w;
11072 int force_p;
11073 {
11074 struct glyph_matrix *matrix;
11075 struct glyph_row *row;
11076 int window_height;
11077
11078 if (!make_cursor_line_fully_visible_p)
11079 return 1;
11080
11081 /* It's not always possible to find the cursor, e.g, when a window
11082 is full of overlay strings. Don't do anything in that case. */
11083 if (w->cursor.vpos < 0)
11084 return 1;
11085
11086 matrix = w->desired_matrix;
11087 row = MATRIX_ROW (matrix, w->cursor.vpos);
11088
11089 /* If the cursor row is not partially visible, there's nothing to do. */
11090 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11091 return 1;
11092
11093 /* If the row the cursor is in is taller than the window's height,
11094 it's not clear what to do, so do nothing. */
11095 window_height = window_box_height (w);
11096 if (row->height >= window_height)
11097 {
11098 if (!force_p || w->vscroll)
11099 return 1;
11100 }
11101 return 0;
11102
11103 #if 0
11104 /* This code used to try to scroll the window just enough to make
11105 the line visible. It returned 0 to say that the caller should
11106 allocate larger glyph matrices. */
11107
11108 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11109 {
11110 int dy = row->height - row->visible_height;
11111 w->vscroll = 0;
11112 w->cursor.y += dy;
11113 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11114 }
11115 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11116 {
11117 int dy = - (row->height - row->visible_height);
11118 w->vscroll = dy;
11119 w->cursor.y += dy;
11120 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11121 }
11122
11123 /* When we change the cursor y-position of the selected window,
11124 change this_line_y as well so that the display optimization for
11125 the cursor line of the selected window in redisplay_internal uses
11126 the correct y-position. */
11127 if (w == XWINDOW (selected_window))
11128 this_line_y = w->cursor.y;
11129
11130 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11131 redisplay with larger matrices. */
11132 if (matrix->nrows < required_matrix_height (w))
11133 {
11134 fonts_changed_p = 1;
11135 return 0;
11136 }
11137
11138 return 1;
11139 #endif /* 0 */
11140 }
11141
11142
11143 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11144 non-zero means only WINDOW is redisplayed in redisplay_internal.
11145 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11146 in redisplay_window to bring a partially visible line into view in
11147 the case that only the cursor has moved.
11148
11149 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11150 last screen line's vertical height extends past the end of the screen.
11151
11152 Value is
11153
11154 1 if scrolling succeeded
11155
11156 0 if scrolling didn't find point.
11157
11158 -1 if new fonts have been loaded so that we must interrupt
11159 redisplay, adjust glyph matrices, and try again. */
11160
11161 enum
11162 {
11163 SCROLLING_SUCCESS,
11164 SCROLLING_FAILED,
11165 SCROLLING_NEED_LARGER_MATRICES
11166 };
11167
11168 static int
11169 try_scrolling (window, just_this_one_p, scroll_conservatively,
11170 scroll_step, temp_scroll_step, last_line_misfit)
11171 Lisp_Object window;
11172 int just_this_one_p;
11173 EMACS_INT scroll_conservatively, scroll_step;
11174 int temp_scroll_step;
11175 int last_line_misfit;
11176 {
11177 struct window *w = XWINDOW (window);
11178 struct frame *f = XFRAME (w->frame);
11179 struct text_pos scroll_margin_pos;
11180 struct text_pos pos;
11181 struct text_pos startp;
11182 struct it it;
11183 Lisp_Object window_end;
11184 int this_scroll_margin;
11185 int dy = 0;
11186 int scroll_max;
11187 int rc;
11188 int amount_to_scroll = 0;
11189 Lisp_Object aggressive;
11190 int height;
11191 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11192
11193 #if GLYPH_DEBUG
11194 debug_method_add (w, "try_scrolling");
11195 #endif
11196
11197 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11198
11199 /* Compute scroll margin height in pixels. We scroll when point is
11200 within this distance from the top or bottom of the window. */
11201 if (scroll_margin > 0)
11202 {
11203 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11204 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11205 }
11206 else
11207 this_scroll_margin = 0;
11208
11209 /* Force scroll_conservatively to have a reasonable value so it doesn't
11210 cause an overflow while computing how much to scroll. */
11211 if (scroll_conservatively)
11212 scroll_conservatively = min (scroll_conservatively,
11213 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11214
11215 /* Compute how much we should try to scroll maximally to bring point
11216 into view. */
11217 if (scroll_step || scroll_conservatively || temp_scroll_step)
11218 scroll_max = max (scroll_step,
11219 max (scroll_conservatively, temp_scroll_step));
11220 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11221 || NUMBERP (current_buffer->scroll_up_aggressively))
11222 /* We're trying to scroll because of aggressive scrolling
11223 but no scroll_step is set. Choose an arbitrary one. Maybe
11224 there should be a variable for this. */
11225 scroll_max = 10;
11226 else
11227 scroll_max = 0;
11228 scroll_max *= FRAME_LINE_HEIGHT (f);
11229
11230 /* Decide whether we have to scroll down. Start at the window end
11231 and move this_scroll_margin up to find the position of the scroll
11232 margin. */
11233 window_end = Fwindow_end (window, Qt);
11234
11235 too_near_end:
11236
11237 CHARPOS (scroll_margin_pos) = XINT (window_end);
11238 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11239
11240 if (this_scroll_margin || extra_scroll_margin_lines)
11241 {
11242 start_display (&it, w, scroll_margin_pos);
11243 if (this_scroll_margin)
11244 move_it_vertically_backward (&it, this_scroll_margin);
11245 if (extra_scroll_margin_lines)
11246 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11247 scroll_margin_pos = it.current.pos;
11248 }
11249
11250 if (PT >= CHARPOS (scroll_margin_pos))
11251 {
11252 int y0;
11253
11254 /* Point is in the scroll margin at the bottom of the window, or
11255 below. Compute a new window start that makes point visible. */
11256
11257 /* Compute the distance from the scroll margin to PT.
11258 Give up if the distance is greater than scroll_max. */
11259 start_display (&it, w, scroll_margin_pos);
11260 y0 = it.current_y;
11261 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11262 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11263
11264 /* To make point visible, we have to move the window start
11265 down so that the line the cursor is in is visible, which
11266 means we have to add in the height of the cursor line. */
11267 dy = line_bottom_y (&it) - y0;
11268
11269 if (dy > scroll_max)
11270 return SCROLLING_FAILED;
11271
11272 /* Move the window start down. If scrolling conservatively,
11273 move it just enough down to make point visible. If
11274 scroll_step is set, move it down by scroll_step. */
11275 start_display (&it, w, startp);
11276
11277 if (scroll_conservatively)
11278 /* Set AMOUNT_TO_SCROLL to at least one line,
11279 and at most scroll_conservatively lines. */
11280 amount_to_scroll
11281 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11282 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11283 else if (scroll_step || temp_scroll_step)
11284 amount_to_scroll = scroll_max;
11285 else
11286 {
11287 aggressive = current_buffer->scroll_up_aggressively;
11288 height = WINDOW_BOX_TEXT_HEIGHT (w);
11289 if (NUMBERP (aggressive))
11290 {
11291 double float_amount = XFLOATINT (aggressive) * height;
11292 amount_to_scroll = float_amount;
11293 if (amount_to_scroll == 0 && float_amount > 0)
11294 amount_to_scroll = 1;
11295 }
11296 }
11297
11298 if (amount_to_scroll <= 0)
11299 return SCROLLING_FAILED;
11300
11301 /* If moving by amount_to_scroll leaves STARTP unchanged,
11302 move it down one screen line. */
11303
11304 move_it_vertically (&it, amount_to_scroll);
11305 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11306 move_it_by_lines (&it, 1, 1);
11307 startp = it.current.pos;
11308 }
11309 else
11310 {
11311 /* See if point is inside the scroll margin at the top of the
11312 window. */
11313 scroll_margin_pos = startp;
11314 if (this_scroll_margin)
11315 {
11316 start_display (&it, w, startp);
11317 move_it_vertically (&it, this_scroll_margin);
11318 scroll_margin_pos = it.current.pos;
11319 }
11320
11321 if (PT < CHARPOS (scroll_margin_pos))
11322 {
11323 /* Point is in the scroll margin at the top of the window or
11324 above what is displayed in the window. */
11325 int y0;
11326
11327 /* Compute the vertical distance from PT to the scroll
11328 margin position. Give up if distance is greater than
11329 scroll_max. */
11330 SET_TEXT_POS (pos, PT, PT_BYTE);
11331 start_display (&it, w, pos);
11332 y0 = it.current_y;
11333 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11334 it.last_visible_y, -1,
11335 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11336 dy = it.current_y - y0;
11337 if (dy > scroll_max)
11338 return SCROLLING_FAILED;
11339
11340 /* Compute new window start. */
11341 start_display (&it, w, startp);
11342
11343 if (scroll_conservatively)
11344 amount_to_scroll
11345 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11346 else if (scroll_step || temp_scroll_step)
11347 amount_to_scroll = scroll_max;
11348 else
11349 {
11350 aggressive = current_buffer->scroll_down_aggressively;
11351 height = WINDOW_BOX_TEXT_HEIGHT (w);
11352 if (NUMBERP (aggressive))
11353 {
11354 double float_amount = XFLOATINT (aggressive) * height;
11355 amount_to_scroll = float_amount;
11356 if (amount_to_scroll == 0 && float_amount > 0)
11357 amount_to_scroll = 1;
11358 }
11359 }
11360
11361 if (amount_to_scroll <= 0)
11362 return SCROLLING_FAILED;
11363
11364 move_it_vertically_backward (&it, amount_to_scroll);
11365 startp = it.current.pos;
11366 }
11367 }
11368
11369 /* Run window scroll functions. */
11370 startp = run_window_scroll_functions (window, startp);
11371
11372 /* Display the window. Give up if new fonts are loaded, or if point
11373 doesn't appear. */
11374 if (!try_window (window, startp))
11375 rc = SCROLLING_NEED_LARGER_MATRICES;
11376 else if (w->cursor.vpos < 0)
11377 {
11378 clear_glyph_matrix (w->desired_matrix);
11379 rc = SCROLLING_FAILED;
11380 }
11381 else
11382 {
11383 /* Maybe forget recorded base line for line number display. */
11384 if (!just_this_one_p
11385 || current_buffer->clip_changed
11386 || BEG_UNCHANGED < CHARPOS (startp))
11387 w->base_line_number = Qnil;
11388
11389 /* If cursor ends up on a partially visible line,
11390 treat that as being off the bottom of the screen. */
11391 if (! make_cursor_line_fully_visible (w, extra_scroll_margin_lines <= 1))
11392 {
11393 clear_glyph_matrix (w->desired_matrix);
11394 ++extra_scroll_margin_lines;
11395 goto too_near_end;
11396 }
11397 rc = SCROLLING_SUCCESS;
11398 }
11399
11400 return rc;
11401 }
11402
11403
11404 /* Compute a suitable window start for window W if display of W starts
11405 on a continuation line. Value is non-zero if a new window start
11406 was computed.
11407
11408 The new window start will be computed, based on W's width, starting
11409 from the start of the continued line. It is the start of the
11410 screen line with the minimum distance from the old start W->start. */
11411
11412 static int
11413 compute_window_start_on_continuation_line (w)
11414 struct window *w;
11415 {
11416 struct text_pos pos, start_pos;
11417 int window_start_changed_p = 0;
11418
11419 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11420
11421 /* If window start is on a continuation line... Window start may be
11422 < BEGV in case there's invisible text at the start of the
11423 buffer (M-x rmail, for example). */
11424 if (CHARPOS (start_pos) > BEGV
11425 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11426 {
11427 struct it it;
11428 struct glyph_row *row;
11429
11430 /* Handle the case that the window start is out of range. */
11431 if (CHARPOS (start_pos) < BEGV)
11432 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11433 else if (CHARPOS (start_pos) > ZV)
11434 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11435
11436 /* Find the start of the continued line. This should be fast
11437 because scan_buffer is fast (newline cache). */
11438 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11439 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11440 row, DEFAULT_FACE_ID);
11441 reseat_at_previous_visible_line_start (&it);
11442
11443 /* If the line start is "too far" away from the window start,
11444 say it takes too much time to compute a new window start. */
11445 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11446 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11447 {
11448 int min_distance, distance;
11449
11450 /* Move forward by display lines to find the new window
11451 start. If window width was enlarged, the new start can
11452 be expected to be > the old start. If window width was
11453 decreased, the new window start will be < the old start.
11454 So, we're looking for the display line start with the
11455 minimum distance from the old window start. */
11456 pos = it.current.pos;
11457 min_distance = INFINITY;
11458 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11459 distance < min_distance)
11460 {
11461 min_distance = distance;
11462 pos = it.current.pos;
11463 move_it_by_lines (&it, 1, 0);
11464 }
11465
11466 /* Set the window start there. */
11467 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11468 window_start_changed_p = 1;
11469 }
11470 }
11471
11472 return window_start_changed_p;
11473 }
11474
11475
11476 /* Try cursor movement in case text has not changed in window WINDOW,
11477 with window start STARTP. Value is
11478
11479 CURSOR_MOVEMENT_SUCCESS if successful
11480
11481 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11482
11483 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11484 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11485 we want to scroll as if scroll-step were set to 1. See the code.
11486
11487 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11488 which case we have to abort this redisplay, and adjust matrices
11489 first. */
11490
11491 enum
11492 {
11493 CURSOR_MOVEMENT_SUCCESS,
11494 CURSOR_MOVEMENT_CANNOT_BE_USED,
11495 CURSOR_MOVEMENT_MUST_SCROLL,
11496 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11497 };
11498
11499 static int
11500 try_cursor_movement (window, startp, scroll_step)
11501 Lisp_Object window;
11502 struct text_pos startp;
11503 int *scroll_step;
11504 {
11505 struct window *w = XWINDOW (window);
11506 struct frame *f = XFRAME (w->frame);
11507 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11508
11509 #if GLYPH_DEBUG
11510 if (inhibit_try_cursor_movement)
11511 return rc;
11512 #endif
11513
11514 /* Handle case where text has not changed, only point, and it has
11515 not moved off the frame. */
11516 if (/* Point may be in this window. */
11517 PT >= CHARPOS (startp)
11518 /* Selective display hasn't changed. */
11519 && !current_buffer->clip_changed
11520 /* Function force-mode-line-update is used to force a thorough
11521 redisplay. It sets either windows_or_buffers_changed or
11522 update_mode_lines. So don't take a shortcut here for these
11523 cases. */
11524 && !update_mode_lines
11525 && !windows_or_buffers_changed
11526 && !cursor_type_changed
11527 /* Can't use this case if highlighting a region. When a
11528 region exists, cursor movement has to do more than just
11529 set the cursor. */
11530 && !(!NILP (Vtransient_mark_mode)
11531 && !NILP (current_buffer->mark_active))
11532 && NILP (w->region_showing)
11533 && NILP (Vshow_trailing_whitespace)
11534 /* Right after splitting windows, last_point may be nil. */
11535 && INTEGERP (w->last_point)
11536 /* This code is not used for mini-buffer for the sake of the case
11537 of redisplaying to replace an echo area message; since in
11538 that case the mini-buffer contents per se are usually
11539 unchanged. This code is of no real use in the mini-buffer
11540 since the handling of this_line_start_pos, etc., in redisplay
11541 handles the same cases. */
11542 && !EQ (window, minibuf_window)
11543 /* When splitting windows or for new windows, it happens that
11544 redisplay is called with a nil window_end_vpos or one being
11545 larger than the window. This should really be fixed in
11546 window.c. I don't have this on my list, now, so we do
11547 approximately the same as the old redisplay code. --gerd. */
11548 && INTEGERP (w->window_end_vpos)
11549 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11550 && (FRAME_WINDOW_P (f)
11551 || !overlay_arrow_in_current_buffer_p ()))
11552 {
11553 int this_scroll_margin, top_scroll_margin;
11554 struct glyph_row *row = NULL;
11555
11556 #if GLYPH_DEBUG
11557 debug_method_add (w, "cursor movement");
11558 #endif
11559
11560 /* Scroll if point within this distance from the top or bottom
11561 of the window. This is a pixel value. */
11562 this_scroll_margin = max (0, scroll_margin);
11563 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11564 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11565
11566 top_scroll_margin = this_scroll_margin;
11567 if (WINDOW_WANTS_HEADER_LINE_P (w))
11568 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11569
11570 /* Start with the row the cursor was displayed during the last
11571 not paused redisplay. Give up if that row is not valid. */
11572 if (w->last_cursor.vpos < 0
11573 || w->last_cursor.vpos >= w->current_matrix->nrows)
11574 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11575 else
11576 {
11577 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11578 if (row->mode_line_p)
11579 ++row;
11580 if (!row->enabled_p)
11581 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11582 }
11583
11584 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11585 {
11586 int scroll_p = 0;
11587 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11588
11589 if (PT > XFASTINT (w->last_point))
11590 {
11591 /* Point has moved forward. */
11592 while (MATRIX_ROW_END_CHARPOS (row) < PT
11593 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11594 {
11595 xassert (row->enabled_p);
11596 ++row;
11597 }
11598
11599 /* The end position of a row equals the start position
11600 of the next row. If PT is there, we would rather
11601 display it in the next line. */
11602 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11603 && MATRIX_ROW_END_CHARPOS (row) == PT
11604 && !cursor_row_p (w, row))
11605 ++row;
11606
11607 /* If within the scroll margin, scroll. Note that
11608 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11609 the next line would be drawn, and that
11610 this_scroll_margin can be zero. */
11611 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11612 || PT > MATRIX_ROW_END_CHARPOS (row)
11613 /* Line is completely visible last line in window
11614 and PT is to be set in the next line. */
11615 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11616 && PT == MATRIX_ROW_END_CHARPOS (row)
11617 && !row->ends_at_zv_p
11618 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11619 scroll_p = 1;
11620 }
11621 else if (PT < XFASTINT (w->last_point))
11622 {
11623 /* Cursor has to be moved backward. Note that PT >=
11624 CHARPOS (startp) because of the outer if-statement. */
11625 while (!row->mode_line_p
11626 && (MATRIX_ROW_START_CHARPOS (row) > PT
11627 || (MATRIX_ROW_START_CHARPOS (row) == PT
11628 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11629 && (row->y > top_scroll_margin
11630 || CHARPOS (startp) == BEGV))
11631 {
11632 xassert (row->enabled_p);
11633 --row;
11634 }
11635
11636 /* Consider the following case: Window starts at BEGV,
11637 there is invisible, intangible text at BEGV, so that
11638 display starts at some point START > BEGV. It can
11639 happen that we are called with PT somewhere between
11640 BEGV and START. Try to handle that case. */
11641 if (row < w->current_matrix->rows
11642 || row->mode_line_p)
11643 {
11644 row = w->current_matrix->rows;
11645 if (row->mode_line_p)
11646 ++row;
11647 }
11648
11649 /* Due to newlines in overlay strings, we may have to
11650 skip forward over overlay strings. */
11651 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11652 && MATRIX_ROW_END_CHARPOS (row) == PT
11653 && !cursor_row_p (w, row))
11654 ++row;
11655
11656 /* If within the scroll margin, scroll. */
11657 if (row->y < top_scroll_margin
11658 && CHARPOS (startp) != BEGV)
11659 scroll_p = 1;
11660 }
11661
11662 if (PT < MATRIX_ROW_START_CHARPOS (row)
11663 || PT > MATRIX_ROW_END_CHARPOS (row))
11664 {
11665 /* if PT is not in the glyph row, give up. */
11666 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11667 }
11668 else if (MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11669 && make_cursor_line_fully_visible_p)
11670 {
11671 if (PT == MATRIX_ROW_END_CHARPOS (row)
11672 && !row->ends_at_zv_p
11673 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11674 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11675 else if (row->height > window_box_height (w))
11676 {
11677 /* If we end up in a partially visible line, let's
11678 make it fully visible, except when it's taller
11679 than the window, in which case we can't do much
11680 about it. */
11681 *scroll_step = 1;
11682 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11683 }
11684 else
11685 {
11686 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11687 if (!make_cursor_line_fully_visible (w, 0))
11688 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11689 else
11690 rc = CURSOR_MOVEMENT_SUCCESS;
11691 }
11692 }
11693 else if (scroll_p)
11694 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11695 else
11696 {
11697 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11698 rc = CURSOR_MOVEMENT_SUCCESS;
11699 }
11700 }
11701 }
11702
11703 return rc;
11704 }
11705
11706 void
11707 set_vertical_scroll_bar (w)
11708 struct window *w;
11709 {
11710 int start, end, whole;
11711
11712 /* Calculate the start and end positions for the current window.
11713 At some point, it would be nice to choose between scrollbars
11714 which reflect the whole buffer size, with special markers
11715 indicating narrowing, and scrollbars which reflect only the
11716 visible region.
11717
11718 Note that mini-buffers sometimes aren't displaying any text. */
11719 if (!MINI_WINDOW_P (w)
11720 || (w == XWINDOW (minibuf_window)
11721 && NILP (echo_area_buffer[0])))
11722 {
11723 struct buffer *buf = XBUFFER (w->buffer);
11724 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11725 start = marker_position (w->start) - BUF_BEGV (buf);
11726 /* I don't think this is guaranteed to be right. For the
11727 moment, we'll pretend it is. */
11728 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11729
11730 if (end < start)
11731 end = start;
11732 if (whole < (end - start))
11733 whole = end - start;
11734 }
11735 else
11736 start = end = whole = 0;
11737
11738 /* Indicate what this scroll bar ought to be displaying now. */
11739 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11740 }
11741
11742
11743 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11744 selected_window is redisplayed.
11745
11746 We can return without actually redisplaying the window if
11747 fonts_changed_p is nonzero. In that case, redisplay_internal will
11748 retry. */
11749
11750 static void
11751 redisplay_window (window, just_this_one_p)
11752 Lisp_Object window;
11753 int just_this_one_p;
11754 {
11755 struct window *w = XWINDOW (window);
11756 struct frame *f = XFRAME (w->frame);
11757 struct buffer *buffer = XBUFFER (w->buffer);
11758 struct buffer *old = current_buffer;
11759 struct text_pos lpoint, opoint, startp;
11760 int update_mode_line;
11761 int tem;
11762 struct it it;
11763 /* Record it now because it's overwritten. */
11764 int current_matrix_up_to_date_p = 0;
11765 int used_current_matrix_p = 0;
11766 /* This is less strict than current_matrix_up_to_date_p.
11767 It indictes that the buffer contents and narrowing are unchanged. */
11768 int buffer_unchanged_p = 0;
11769 int temp_scroll_step = 0;
11770 int count = SPECPDL_INDEX ();
11771 int rc;
11772 int centering_position;
11773 int last_line_misfit = 0;
11774
11775 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11776 opoint = lpoint;
11777
11778 /* W must be a leaf window here. */
11779 xassert (!NILP (w->buffer));
11780 #if GLYPH_DEBUG
11781 *w->desired_matrix->method = 0;
11782 #endif
11783
11784 specbind (Qinhibit_point_motion_hooks, Qt);
11785
11786 reconsider_clip_changes (w, buffer);
11787
11788 /* Has the mode line to be updated? */
11789 update_mode_line = (!NILP (w->update_mode_line)
11790 || update_mode_lines
11791 || buffer->clip_changed
11792 || buffer->prevent_redisplay_optimizations_p);
11793
11794 if (MINI_WINDOW_P (w))
11795 {
11796 if (w == XWINDOW (echo_area_window)
11797 && !NILP (echo_area_buffer[0]))
11798 {
11799 if (update_mode_line)
11800 /* We may have to update a tty frame's menu bar or a
11801 tool-bar. Example `M-x C-h C-h C-g'. */
11802 goto finish_menu_bars;
11803 else
11804 /* We've already displayed the echo area glyphs in this window. */
11805 goto finish_scroll_bars;
11806 }
11807 else if ((w != XWINDOW (minibuf_window)
11808 || minibuf_level == 0)
11809 /* When buffer is nonempty, redisplay window normally. */
11810 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11811 /* Quail displays non-mini buffers in minibuffer window.
11812 In that case, redisplay the window normally. */
11813 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11814 {
11815 /* W is a mini-buffer window, but it's not active, so clear
11816 it. */
11817 int yb = window_text_bottom_y (w);
11818 struct glyph_row *row;
11819 int y;
11820
11821 for (y = 0, row = w->desired_matrix->rows;
11822 y < yb;
11823 y += row->height, ++row)
11824 blank_row (w, row, y);
11825 goto finish_scroll_bars;
11826 }
11827
11828 clear_glyph_matrix (w->desired_matrix);
11829 }
11830
11831 /* Otherwise set up data on this window; select its buffer and point
11832 value. */
11833 /* Really select the buffer, for the sake of buffer-local
11834 variables. */
11835 set_buffer_internal_1 (XBUFFER (w->buffer));
11836 SET_TEXT_POS (opoint, PT, PT_BYTE);
11837
11838 current_matrix_up_to_date_p
11839 = (!NILP (w->window_end_valid)
11840 && !current_buffer->clip_changed
11841 && !current_buffer->prevent_redisplay_optimizations_p
11842 && XFASTINT (w->last_modified) >= MODIFF
11843 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11844
11845 buffer_unchanged_p
11846 = (!NILP (w->window_end_valid)
11847 && !current_buffer->clip_changed
11848 && XFASTINT (w->last_modified) >= MODIFF
11849 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11850
11851 /* When windows_or_buffers_changed is non-zero, we can't rely on
11852 the window end being valid, so set it to nil there. */
11853 if (windows_or_buffers_changed)
11854 {
11855 /* If window starts on a continuation line, maybe adjust the
11856 window start in case the window's width changed. */
11857 if (XMARKER (w->start)->buffer == current_buffer)
11858 compute_window_start_on_continuation_line (w);
11859
11860 w->window_end_valid = Qnil;
11861 }
11862
11863 /* Some sanity checks. */
11864 CHECK_WINDOW_END (w);
11865 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11866 abort ();
11867 if (BYTEPOS (opoint) < CHARPOS (opoint))
11868 abort ();
11869
11870 /* If %c is in mode line, update it if needed. */
11871 if (!NILP (w->column_number_displayed)
11872 /* This alternative quickly identifies a common case
11873 where no change is needed. */
11874 && !(PT == XFASTINT (w->last_point)
11875 && XFASTINT (w->last_modified) >= MODIFF
11876 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11877 && (XFASTINT (w->column_number_displayed)
11878 != (int) current_column ())) /* iftc */
11879 update_mode_line = 1;
11880
11881 /* Count number of windows showing the selected buffer. An indirect
11882 buffer counts as its base buffer. */
11883 if (!just_this_one_p)
11884 {
11885 struct buffer *current_base, *window_base;
11886 current_base = current_buffer;
11887 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11888 if (current_base->base_buffer)
11889 current_base = current_base->base_buffer;
11890 if (window_base->base_buffer)
11891 window_base = window_base->base_buffer;
11892 if (current_base == window_base)
11893 buffer_shared++;
11894 }
11895
11896 /* Point refers normally to the selected window. For any other
11897 window, set up appropriate value. */
11898 if (!EQ (window, selected_window))
11899 {
11900 int new_pt = XMARKER (w->pointm)->charpos;
11901 int new_pt_byte = marker_byte_position (w->pointm);
11902 if (new_pt < BEGV)
11903 {
11904 new_pt = BEGV;
11905 new_pt_byte = BEGV_BYTE;
11906 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11907 }
11908 else if (new_pt > (ZV - 1))
11909 {
11910 new_pt = ZV;
11911 new_pt_byte = ZV_BYTE;
11912 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11913 }
11914
11915 /* We don't use SET_PT so that the point-motion hooks don't run. */
11916 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11917 }
11918
11919 /* If any of the character widths specified in the display table
11920 have changed, invalidate the width run cache. It's true that
11921 this may be a bit late to catch such changes, but the rest of
11922 redisplay goes (non-fatally) haywire when the display table is
11923 changed, so why should we worry about doing any better? */
11924 if (current_buffer->width_run_cache)
11925 {
11926 struct Lisp_Char_Table *disptab = buffer_display_table ();
11927
11928 if (! disptab_matches_widthtab (disptab,
11929 XVECTOR (current_buffer->width_table)))
11930 {
11931 invalidate_region_cache (current_buffer,
11932 current_buffer->width_run_cache,
11933 BEG, Z);
11934 recompute_width_table (current_buffer, disptab);
11935 }
11936 }
11937
11938 /* If window-start is screwed up, choose a new one. */
11939 if (XMARKER (w->start)->buffer != current_buffer)
11940 goto recenter;
11941
11942 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11943
11944 /* If someone specified a new starting point but did not insist,
11945 check whether it can be used. */
11946 if (!NILP (w->optional_new_start)
11947 && CHARPOS (startp) >= BEGV
11948 && CHARPOS (startp) <= ZV)
11949 {
11950 w->optional_new_start = Qnil;
11951 start_display (&it, w, startp);
11952 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11953 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11954 if (IT_CHARPOS (it) == PT)
11955 w->force_start = Qt;
11956 /* IT may overshoot PT if text at PT is invisible. */
11957 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
11958 w->force_start = Qt;
11959
11960
11961 }
11962
11963 /* Handle case where place to start displaying has been specified,
11964 unless the specified location is outside the accessible range. */
11965 if (!NILP (w->force_start)
11966 || w->frozen_window_start_p)
11967 {
11968 /* We set this later on if we have to adjust point. */
11969 int new_vpos = -1;
11970
11971 w->force_start = Qnil;
11972 w->vscroll = 0;
11973 w->window_end_valid = Qnil;
11974
11975 /* Forget any recorded base line for line number display. */
11976 if (!buffer_unchanged_p)
11977 w->base_line_number = Qnil;
11978
11979 /* Redisplay the mode line. Select the buffer properly for that.
11980 Also, run the hook window-scroll-functions
11981 because we have scrolled. */
11982 /* Note, we do this after clearing force_start because
11983 if there's an error, it is better to forget about force_start
11984 than to get into an infinite loop calling the hook functions
11985 and having them get more errors. */
11986 if (!update_mode_line
11987 || ! NILP (Vwindow_scroll_functions))
11988 {
11989 update_mode_line = 1;
11990 w->update_mode_line = Qt;
11991 startp = run_window_scroll_functions (window, startp);
11992 }
11993
11994 w->last_modified = make_number (0);
11995 w->last_overlay_modified = make_number (0);
11996 if (CHARPOS (startp) < BEGV)
11997 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
11998 else if (CHARPOS (startp) > ZV)
11999 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12000
12001 /* Redisplay, then check if cursor has been set during the
12002 redisplay. Give up if new fonts were loaded. */
12003 if (!try_window (window, startp))
12004 {
12005 w->force_start = Qt;
12006 clear_glyph_matrix (w->desired_matrix);
12007 goto need_larger_matrices;
12008 }
12009
12010 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12011 {
12012 /* If point does not appear, try to move point so it does
12013 appear. The desired matrix has been built above, so we
12014 can use it here. */
12015 new_vpos = window_box_height (w) / 2;
12016 }
12017
12018 if (!make_cursor_line_fully_visible (w, 0))
12019 {
12020 /* Point does appear, but on a line partly visible at end of window.
12021 Move it back to a fully-visible line. */
12022 new_vpos = window_box_height (w);
12023 }
12024
12025 /* If we need to move point for either of the above reasons,
12026 now actually do it. */
12027 if (new_vpos >= 0)
12028 {
12029 struct glyph_row *row;
12030
12031 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12032 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12033 ++row;
12034
12035 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12036 MATRIX_ROW_START_BYTEPOS (row));
12037
12038 if (w != XWINDOW (selected_window))
12039 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12040 else if (current_buffer == old)
12041 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12042
12043 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12044
12045 /* If we are highlighting the region, then we just changed
12046 the region, so redisplay to show it. */
12047 if (!NILP (Vtransient_mark_mode)
12048 && !NILP (current_buffer->mark_active))
12049 {
12050 clear_glyph_matrix (w->desired_matrix);
12051 if (!try_window (window, startp))
12052 goto need_larger_matrices;
12053 }
12054 }
12055
12056 #if GLYPH_DEBUG
12057 debug_method_add (w, "forced window start");
12058 #endif
12059 goto done;
12060 }
12061
12062 /* Handle case where text has not changed, only point, and it has
12063 not moved off the frame, and we are not retrying after hscroll.
12064 (current_matrix_up_to_date_p is nonzero when retrying.) */
12065 if (current_matrix_up_to_date_p
12066 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12067 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12068 {
12069 switch (rc)
12070 {
12071 case CURSOR_MOVEMENT_SUCCESS:
12072 used_current_matrix_p = 1;
12073 goto done;
12074
12075 #if 0 /* try_cursor_movement never returns this value. */
12076 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12077 goto need_larger_matrices;
12078 #endif
12079
12080 case CURSOR_MOVEMENT_MUST_SCROLL:
12081 goto try_to_scroll;
12082
12083 default:
12084 abort ();
12085 }
12086 }
12087 /* If current starting point was originally the beginning of a line
12088 but no longer is, find a new starting point. */
12089 else if (!NILP (w->start_at_line_beg)
12090 && !(CHARPOS (startp) <= BEGV
12091 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12092 {
12093 #if GLYPH_DEBUG
12094 debug_method_add (w, "recenter 1");
12095 #endif
12096 goto recenter;
12097 }
12098
12099 /* Try scrolling with try_window_id. Value is > 0 if update has
12100 been done, it is -1 if we know that the same window start will
12101 not work. It is 0 if unsuccessful for some other reason. */
12102 else if ((tem = try_window_id (w)) != 0)
12103 {
12104 #if GLYPH_DEBUG
12105 debug_method_add (w, "try_window_id %d", tem);
12106 #endif
12107
12108 if (fonts_changed_p)
12109 goto need_larger_matrices;
12110 if (tem > 0)
12111 goto done;
12112
12113 /* Otherwise try_window_id has returned -1 which means that we
12114 don't want the alternative below this comment to execute. */
12115 }
12116 else if (CHARPOS (startp) >= BEGV
12117 && CHARPOS (startp) <= ZV
12118 && PT >= CHARPOS (startp)
12119 && (CHARPOS (startp) < ZV
12120 /* Avoid starting at end of buffer. */
12121 || CHARPOS (startp) == BEGV
12122 || (XFASTINT (w->last_modified) >= MODIFF
12123 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12124 {
12125 #if GLYPH_DEBUG
12126 debug_method_add (w, "same window start");
12127 #endif
12128
12129 /* Try to redisplay starting at same place as before.
12130 If point has not moved off frame, accept the results. */
12131 if (!current_matrix_up_to_date_p
12132 /* Don't use try_window_reusing_current_matrix in this case
12133 because a window scroll function can have changed the
12134 buffer. */
12135 || !NILP (Vwindow_scroll_functions)
12136 || MINI_WINDOW_P (w)
12137 || !(used_current_matrix_p
12138 = try_window_reusing_current_matrix (w)))
12139 {
12140 IF_DEBUG (debug_method_add (w, "1"));
12141 try_window (window, startp);
12142 }
12143
12144 if (fonts_changed_p)
12145 goto need_larger_matrices;
12146
12147 if (w->cursor.vpos >= 0)
12148 {
12149 if (!just_this_one_p
12150 || current_buffer->clip_changed
12151 || BEG_UNCHANGED < CHARPOS (startp))
12152 /* Forget any recorded base line for line number display. */
12153 w->base_line_number = Qnil;
12154
12155 if (!make_cursor_line_fully_visible (w, 1))
12156 {
12157 clear_glyph_matrix (w->desired_matrix);
12158 last_line_misfit = 1;
12159 }
12160 /* Drop through and scroll. */
12161 else
12162 goto done;
12163 }
12164 else
12165 clear_glyph_matrix (w->desired_matrix);
12166 }
12167
12168 try_to_scroll:
12169
12170 w->last_modified = make_number (0);
12171 w->last_overlay_modified = make_number (0);
12172
12173 /* Redisplay the mode line. Select the buffer properly for that. */
12174 if (!update_mode_line)
12175 {
12176 update_mode_line = 1;
12177 w->update_mode_line = Qt;
12178 }
12179
12180 /* Try to scroll by specified few lines. */
12181 if ((scroll_conservatively
12182 || scroll_step
12183 || temp_scroll_step
12184 || NUMBERP (current_buffer->scroll_up_aggressively)
12185 || NUMBERP (current_buffer->scroll_down_aggressively))
12186 && !current_buffer->clip_changed
12187 && CHARPOS (startp) >= BEGV
12188 && CHARPOS (startp) <= ZV)
12189 {
12190 /* The function returns -1 if new fonts were loaded, 1 if
12191 successful, 0 if not successful. */
12192 int rc = try_scrolling (window, just_this_one_p,
12193 scroll_conservatively,
12194 scroll_step,
12195 temp_scroll_step, last_line_misfit);
12196 switch (rc)
12197 {
12198 case SCROLLING_SUCCESS:
12199 goto done;
12200
12201 case SCROLLING_NEED_LARGER_MATRICES:
12202 goto need_larger_matrices;
12203
12204 case SCROLLING_FAILED:
12205 break;
12206
12207 default:
12208 abort ();
12209 }
12210 }
12211
12212 /* Finally, just choose place to start which centers point */
12213
12214 recenter:
12215 centering_position = window_box_height (w) / 2;
12216
12217 point_at_top:
12218 /* Jump here with centering_position already set to 0. */
12219
12220 #if GLYPH_DEBUG
12221 debug_method_add (w, "recenter");
12222 #endif
12223
12224 /* w->vscroll = 0; */
12225
12226 /* Forget any previously recorded base line for line number display. */
12227 if (!buffer_unchanged_p)
12228 w->base_line_number = Qnil;
12229
12230 /* Move backward half the height of the window. */
12231 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12232 it.current_y = it.last_visible_y;
12233 move_it_vertically_backward (&it, centering_position);
12234 xassert (IT_CHARPOS (it) >= BEGV);
12235
12236 /* The function move_it_vertically_backward may move over more
12237 than the specified y-distance. If it->w is small, e.g. a
12238 mini-buffer window, we may end up in front of the window's
12239 display area. Start displaying at the start of the line
12240 containing PT in this case. */
12241 if (it.current_y <= 0)
12242 {
12243 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12244 move_it_vertically_backward (&it, 0);
12245 xassert (IT_CHARPOS (it) <= PT);
12246 it.current_y = 0;
12247 }
12248
12249 it.current_x = it.hpos = 0;
12250
12251 /* Set startp here explicitly in case that helps avoid an infinite loop
12252 in case the window-scroll-functions functions get errors. */
12253 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12254
12255 /* Run scroll hooks. */
12256 startp = run_window_scroll_functions (window, it.current.pos);
12257
12258 /* Redisplay the window. */
12259 if (!current_matrix_up_to_date_p
12260 || windows_or_buffers_changed
12261 || cursor_type_changed
12262 /* Don't use try_window_reusing_current_matrix in this case
12263 because it can have changed the buffer. */
12264 || !NILP (Vwindow_scroll_functions)
12265 || !just_this_one_p
12266 || MINI_WINDOW_P (w)
12267 || !(used_current_matrix_p
12268 = try_window_reusing_current_matrix (w)))
12269 try_window (window, startp);
12270
12271 /* If new fonts have been loaded (due to fontsets), give up. We
12272 have to start a new redisplay since we need to re-adjust glyph
12273 matrices. */
12274 if (fonts_changed_p)
12275 goto need_larger_matrices;
12276
12277 /* If cursor did not appear assume that the middle of the window is
12278 in the first line of the window. Do it again with the next line.
12279 (Imagine a window of height 100, displaying two lines of height
12280 60. Moving back 50 from it->last_visible_y will end in the first
12281 line.) */
12282 if (w->cursor.vpos < 0)
12283 {
12284 if (!NILP (w->window_end_valid)
12285 && PT >= Z - XFASTINT (w->window_end_pos))
12286 {
12287 clear_glyph_matrix (w->desired_matrix);
12288 move_it_by_lines (&it, 1, 0);
12289 try_window (window, it.current.pos);
12290 }
12291 else if (PT < IT_CHARPOS (it))
12292 {
12293 clear_glyph_matrix (w->desired_matrix);
12294 move_it_by_lines (&it, -1, 0);
12295 try_window (window, it.current.pos);
12296 }
12297 else
12298 {
12299 /* Not much we can do about it. */
12300 }
12301 }
12302
12303 /* Consider the following case: Window starts at BEGV, there is
12304 invisible, intangible text at BEGV, so that display starts at
12305 some point START > BEGV. It can happen that we are called with
12306 PT somewhere between BEGV and START. Try to handle that case. */
12307 if (w->cursor.vpos < 0)
12308 {
12309 struct glyph_row *row = w->current_matrix->rows;
12310 if (row->mode_line_p)
12311 ++row;
12312 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12313 }
12314
12315 if (!make_cursor_line_fully_visible (w, centering_position > 0))
12316 {
12317 /* If vscroll is enabled, disable it and try again. */
12318 if (w->vscroll)
12319 {
12320 w->vscroll = 0;
12321 clear_glyph_matrix (w->desired_matrix);
12322 goto recenter;
12323 }
12324
12325 /* If centering point failed to make the whole line visible,
12326 put point at the top instead. That has to make the whole line
12327 visible, if it can be done. */
12328 if (centering_position == 0)
12329 goto done;
12330 clear_glyph_matrix (w->desired_matrix);
12331 centering_position = 0;
12332 goto point_at_top;
12333 }
12334
12335 done:
12336
12337 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12338 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12339 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12340 ? Qt : Qnil);
12341
12342 /* Display the mode line, if we must. */
12343 if ((update_mode_line
12344 /* If window not full width, must redo its mode line
12345 if (a) the window to its side is being redone and
12346 (b) we do a frame-based redisplay. This is a consequence
12347 of how inverted lines are drawn in frame-based redisplay. */
12348 || (!just_this_one_p
12349 && !FRAME_WINDOW_P (f)
12350 && !WINDOW_FULL_WIDTH_P (w))
12351 /* Line number to display. */
12352 || INTEGERP (w->base_line_pos)
12353 /* Column number is displayed and different from the one displayed. */
12354 || (!NILP (w->column_number_displayed)
12355 && (XFASTINT (w->column_number_displayed)
12356 != (int) current_column ()))) /* iftc */
12357 /* This means that the window has a mode line. */
12358 && (WINDOW_WANTS_MODELINE_P (w)
12359 || WINDOW_WANTS_HEADER_LINE_P (w)))
12360 {
12361 display_mode_lines (w);
12362
12363 /* If mode line height has changed, arrange for a thorough
12364 immediate redisplay using the correct mode line height. */
12365 if (WINDOW_WANTS_MODELINE_P (w)
12366 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12367 {
12368 fonts_changed_p = 1;
12369 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12370 = DESIRED_MODE_LINE_HEIGHT (w);
12371 }
12372
12373 /* If top line height has changed, arrange for a thorough
12374 immediate redisplay using the correct mode line height. */
12375 if (WINDOW_WANTS_HEADER_LINE_P (w)
12376 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12377 {
12378 fonts_changed_p = 1;
12379 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12380 = DESIRED_HEADER_LINE_HEIGHT (w);
12381 }
12382
12383 if (fonts_changed_p)
12384 goto need_larger_matrices;
12385 }
12386
12387 if (!line_number_displayed
12388 && !BUFFERP (w->base_line_pos))
12389 {
12390 w->base_line_pos = Qnil;
12391 w->base_line_number = Qnil;
12392 }
12393
12394 finish_menu_bars:
12395
12396 /* When we reach a frame's selected window, redo the frame's menu bar. */
12397 if (update_mode_line
12398 && EQ (FRAME_SELECTED_WINDOW (f), window))
12399 {
12400 int redisplay_menu_p = 0;
12401 int redisplay_tool_bar_p = 0;
12402
12403 if (FRAME_WINDOW_P (f))
12404 {
12405 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12406 || defined (USE_GTK)
12407 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12408 #else
12409 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12410 #endif
12411 }
12412 else
12413 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12414
12415 if (redisplay_menu_p)
12416 display_menu_bar (w);
12417
12418 #ifdef HAVE_WINDOW_SYSTEM
12419 #ifdef USE_GTK
12420 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12421 #else
12422 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12423 && (FRAME_TOOL_BAR_LINES (f) > 0
12424 || auto_resize_tool_bars_p);
12425
12426 #endif
12427
12428 if (redisplay_tool_bar_p)
12429 redisplay_tool_bar (f);
12430 #endif
12431 }
12432
12433 #ifdef HAVE_WINDOW_SYSTEM
12434 if (FRAME_WINDOW_P (f)
12435 && update_window_fringes (w, 0)
12436 && !just_this_one_p
12437 && (used_current_matrix_p || overlay_arrow_seen)
12438 && !w->pseudo_window_p)
12439 {
12440 update_begin (f);
12441 BLOCK_INPUT;
12442 if (draw_window_fringes (w, 1))
12443 x_draw_vertical_border (w);
12444 UNBLOCK_INPUT;
12445 update_end (f);
12446 }
12447 #endif /* HAVE_WINDOW_SYSTEM */
12448
12449 /* We go to this label, with fonts_changed_p nonzero,
12450 if it is necessary to try again using larger glyph matrices.
12451 We have to redeem the scroll bar even in this case,
12452 because the loop in redisplay_internal expects that. */
12453 need_larger_matrices:
12454 ;
12455 finish_scroll_bars:
12456
12457 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12458 {
12459 /* Set the thumb's position and size. */
12460 set_vertical_scroll_bar (w);
12461
12462 /* Note that we actually used the scroll bar attached to this
12463 window, so it shouldn't be deleted at the end of redisplay. */
12464 redeem_scroll_bar_hook (w);
12465 }
12466
12467 /* Restore current_buffer and value of point in it. */
12468 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12469 set_buffer_internal_1 (old);
12470 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12471
12472 unbind_to (count, Qnil);
12473 }
12474
12475
12476 /* Build the complete desired matrix of WINDOW with a window start
12477 buffer position POS. Value is non-zero if successful. It is zero
12478 if fonts were loaded during redisplay which makes re-adjusting
12479 glyph matrices necessary. */
12480
12481 int
12482 try_window (window, pos)
12483 Lisp_Object window;
12484 struct text_pos pos;
12485 {
12486 struct window *w = XWINDOW (window);
12487 struct it it;
12488 struct glyph_row *last_text_row = NULL;
12489
12490 /* Make POS the new window start. */
12491 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12492
12493 /* Mark cursor position as unknown. No overlay arrow seen. */
12494 w->cursor.vpos = -1;
12495 overlay_arrow_seen = 0;
12496
12497 /* Initialize iterator and info to start at POS. */
12498 start_display (&it, w, pos);
12499
12500 /* Display all lines of W. */
12501 while (it.current_y < it.last_visible_y)
12502 {
12503 if (display_line (&it))
12504 last_text_row = it.glyph_row - 1;
12505 if (fonts_changed_p)
12506 return 0;
12507 }
12508
12509 /* If bottom moved off end of frame, change mode line percentage. */
12510 if (XFASTINT (w->window_end_pos) <= 0
12511 && Z != IT_CHARPOS (it))
12512 w->update_mode_line = Qt;
12513
12514 /* Set window_end_pos to the offset of the last character displayed
12515 on the window from the end of current_buffer. Set
12516 window_end_vpos to its row number. */
12517 if (last_text_row)
12518 {
12519 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12520 w->window_end_bytepos
12521 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12522 w->window_end_pos
12523 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12524 w->window_end_vpos
12525 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12526 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12527 ->displays_text_p);
12528 }
12529 else
12530 {
12531 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12532 w->window_end_pos = make_number (Z - ZV);
12533 w->window_end_vpos = make_number (0);
12534 }
12535
12536 /* But that is not valid info until redisplay finishes. */
12537 w->window_end_valid = Qnil;
12538 return 1;
12539 }
12540
12541
12542 \f
12543 /************************************************************************
12544 Window redisplay reusing current matrix when buffer has not changed
12545 ************************************************************************/
12546
12547 /* Try redisplay of window W showing an unchanged buffer with a
12548 different window start than the last time it was displayed by
12549 reusing its current matrix. Value is non-zero if successful.
12550 W->start is the new window start. */
12551
12552 static int
12553 try_window_reusing_current_matrix (w)
12554 struct window *w;
12555 {
12556 struct frame *f = XFRAME (w->frame);
12557 struct glyph_row *row, *bottom_row;
12558 struct it it;
12559 struct run run;
12560 struct text_pos start, new_start;
12561 int nrows_scrolled, i;
12562 struct glyph_row *last_text_row;
12563 struct glyph_row *last_reused_text_row;
12564 struct glyph_row *start_row;
12565 int start_vpos, min_y, max_y;
12566
12567 #if GLYPH_DEBUG
12568 if (inhibit_try_window_reusing)
12569 return 0;
12570 #endif
12571
12572 if (/* This function doesn't handle terminal frames. */
12573 !FRAME_WINDOW_P (f)
12574 /* Don't try to reuse the display if windows have been split
12575 or such. */
12576 || windows_or_buffers_changed
12577 || cursor_type_changed)
12578 return 0;
12579
12580 /* Can't do this if region may have changed. */
12581 if ((!NILP (Vtransient_mark_mode)
12582 && !NILP (current_buffer->mark_active))
12583 || !NILP (w->region_showing)
12584 || !NILP (Vshow_trailing_whitespace))
12585 return 0;
12586
12587 /* If top-line visibility has changed, give up. */
12588 if (WINDOW_WANTS_HEADER_LINE_P (w)
12589 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12590 return 0;
12591
12592 /* Give up if old or new display is scrolled vertically. We could
12593 make this function handle this, but right now it doesn't. */
12594 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12595 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12596 return 0;
12597
12598 /* The variable new_start now holds the new window start. The old
12599 start `start' can be determined from the current matrix. */
12600 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12601 start = start_row->start.pos;
12602 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12603
12604 /* Clear the desired matrix for the display below. */
12605 clear_glyph_matrix (w->desired_matrix);
12606
12607 if (CHARPOS (new_start) <= CHARPOS (start))
12608 {
12609 int first_row_y;
12610
12611 /* Don't use this method if the display starts with an ellipsis
12612 displayed for invisible text. It's not easy to handle that case
12613 below, and it's certainly not worth the effort since this is
12614 not a frequent case. */
12615 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12616 return 0;
12617
12618 IF_DEBUG (debug_method_add (w, "twu1"));
12619
12620 /* Display up to a row that can be reused. The variable
12621 last_text_row is set to the last row displayed that displays
12622 text. Note that it.vpos == 0 if or if not there is a
12623 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12624 start_display (&it, w, new_start);
12625 first_row_y = it.current_y;
12626 w->cursor.vpos = -1;
12627 last_text_row = last_reused_text_row = NULL;
12628
12629 while (it.current_y < it.last_visible_y
12630 && !fonts_changed_p)
12631 {
12632 /* If we have reached into the characters in the START row,
12633 that means the line boundaries have changed. So we
12634 can't start copying with the row START. Maybe it will
12635 work to start copying with the following row. */
12636 while (IT_CHARPOS (it) > CHARPOS (start))
12637 {
12638 /* Advance to the next row as the "start". */
12639 start_row++;
12640 start = start_row->start.pos;
12641 /* If there are no more rows to try, or just one, give up. */
12642 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12643 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12644 || CHARPOS (start) == ZV)
12645 {
12646 clear_glyph_matrix (w->desired_matrix);
12647 return 0;
12648 }
12649
12650 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12651 }
12652 /* If we have reached alignment,
12653 we can copy the rest of the rows. */
12654 if (IT_CHARPOS (it) == CHARPOS (start))
12655 break;
12656
12657 if (display_line (&it))
12658 last_text_row = it.glyph_row - 1;
12659 }
12660
12661 /* A value of current_y < last_visible_y means that we stopped
12662 at the previous window start, which in turn means that we
12663 have at least one reusable row. */
12664 if (it.current_y < it.last_visible_y)
12665 {
12666 /* IT.vpos always starts from 0; it counts text lines. */
12667 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12668
12669 /* Find PT if not already found in the lines displayed. */
12670 if (w->cursor.vpos < 0)
12671 {
12672 int dy = it.current_y - start_row->y;
12673
12674 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12675 row = row_containing_pos (w, PT, row, NULL, dy);
12676 if (row)
12677 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12678 dy, nrows_scrolled);
12679 else
12680 {
12681 clear_glyph_matrix (w->desired_matrix);
12682 return 0;
12683 }
12684 }
12685
12686 /* Scroll the display. Do it before the current matrix is
12687 changed. The problem here is that update has not yet
12688 run, i.e. part of the current matrix is not up to date.
12689 scroll_run_hook will clear the cursor, and use the
12690 current matrix to get the height of the row the cursor is
12691 in. */
12692 run.current_y = start_row->y;
12693 run.desired_y = it.current_y;
12694 run.height = it.last_visible_y - it.current_y;
12695
12696 if (run.height > 0 && run.current_y != run.desired_y)
12697 {
12698 update_begin (f);
12699 rif->update_window_begin_hook (w);
12700 rif->clear_window_mouse_face (w);
12701 rif->scroll_run_hook (w, &run);
12702 rif->update_window_end_hook (w, 0, 0);
12703 update_end (f);
12704 }
12705
12706 /* Shift current matrix down by nrows_scrolled lines. */
12707 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12708 rotate_matrix (w->current_matrix,
12709 start_vpos,
12710 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12711 nrows_scrolled);
12712
12713 /* Disable lines that must be updated. */
12714 for (i = 0; i < it.vpos; ++i)
12715 (start_row + i)->enabled_p = 0;
12716
12717 /* Re-compute Y positions. */
12718 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12719 max_y = it.last_visible_y;
12720 for (row = start_row + nrows_scrolled;
12721 row < bottom_row;
12722 ++row)
12723 {
12724 row->y = it.current_y;
12725 row->visible_height = row->height;
12726
12727 if (row->y < min_y)
12728 row->visible_height -= min_y - row->y;
12729 if (row->y + row->height > max_y)
12730 row->visible_height -= row->y + row->height - max_y;
12731 row->redraw_fringe_bitmaps_p = 1;
12732
12733 it.current_y += row->height;
12734
12735 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12736 last_reused_text_row = row;
12737 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12738 break;
12739 }
12740
12741 /* Disable lines in the current matrix which are now
12742 below the window. */
12743 for (++row; row < bottom_row; ++row)
12744 row->enabled_p = 0;
12745 }
12746
12747 /* Update window_end_pos etc.; last_reused_text_row is the last
12748 reused row from the current matrix containing text, if any.
12749 The value of last_text_row is the last displayed line
12750 containing text. */
12751 if (last_reused_text_row)
12752 {
12753 w->window_end_bytepos
12754 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12755 w->window_end_pos
12756 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12757 w->window_end_vpos
12758 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12759 w->current_matrix));
12760 }
12761 else if (last_text_row)
12762 {
12763 w->window_end_bytepos
12764 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12765 w->window_end_pos
12766 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12767 w->window_end_vpos
12768 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12769 }
12770 else
12771 {
12772 /* This window must be completely empty. */
12773 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12774 w->window_end_pos = make_number (Z - ZV);
12775 w->window_end_vpos = make_number (0);
12776 }
12777 w->window_end_valid = Qnil;
12778
12779 /* Update hint: don't try scrolling again in update_window. */
12780 w->desired_matrix->no_scrolling_p = 1;
12781
12782 #if GLYPH_DEBUG
12783 debug_method_add (w, "try_window_reusing_current_matrix 1");
12784 #endif
12785 return 1;
12786 }
12787 else if (CHARPOS (new_start) > CHARPOS (start))
12788 {
12789 struct glyph_row *pt_row, *row;
12790 struct glyph_row *first_reusable_row;
12791 struct glyph_row *first_row_to_display;
12792 int dy;
12793 int yb = window_text_bottom_y (w);
12794
12795 /* Find the row starting at new_start, if there is one. Don't
12796 reuse a partially visible line at the end. */
12797 first_reusable_row = start_row;
12798 while (first_reusable_row->enabled_p
12799 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12800 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12801 < CHARPOS (new_start)))
12802 ++first_reusable_row;
12803
12804 /* Give up if there is no row to reuse. */
12805 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12806 || !first_reusable_row->enabled_p
12807 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12808 != CHARPOS (new_start)))
12809 return 0;
12810
12811 /* We can reuse fully visible rows beginning with
12812 first_reusable_row to the end of the window. Set
12813 first_row_to_display to the first row that cannot be reused.
12814 Set pt_row to the row containing point, if there is any. */
12815 pt_row = NULL;
12816 for (first_row_to_display = first_reusable_row;
12817 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12818 ++first_row_to_display)
12819 {
12820 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12821 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12822 pt_row = first_row_to_display;
12823 }
12824
12825 /* Start displaying at the start of first_row_to_display. */
12826 xassert (first_row_to_display->y < yb);
12827 init_to_row_start (&it, w, first_row_to_display);
12828
12829 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12830 - start_vpos);
12831 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12832 - nrows_scrolled);
12833 it.current_y = (first_row_to_display->y - first_reusable_row->y
12834 + WINDOW_HEADER_LINE_HEIGHT (w));
12835
12836 /* Display lines beginning with first_row_to_display in the
12837 desired matrix. Set last_text_row to the last row displayed
12838 that displays text. */
12839 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12840 if (pt_row == NULL)
12841 w->cursor.vpos = -1;
12842 last_text_row = NULL;
12843 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12844 if (display_line (&it))
12845 last_text_row = it.glyph_row - 1;
12846
12847 /* Give up If point isn't in a row displayed or reused. */
12848 if (w->cursor.vpos < 0)
12849 {
12850 clear_glyph_matrix (w->desired_matrix);
12851 return 0;
12852 }
12853
12854 /* If point is in a reused row, adjust y and vpos of the cursor
12855 position. */
12856 if (pt_row)
12857 {
12858 w->cursor.vpos -= nrows_scrolled;
12859 w->cursor.y -= first_reusable_row->y - start_row->y;
12860 }
12861
12862 /* Scroll the display. */
12863 run.current_y = first_reusable_row->y;
12864 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12865 run.height = it.last_visible_y - run.current_y;
12866 dy = run.current_y - run.desired_y;
12867
12868 if (run.height)
12869 {
12870 update_begin (f);
12871 rif->update_window_begin_hook (w);
12872 rif->clear_window_mouse_face (w);
12873 rif->scroll_run_hook (w, &run);
12874 rif->update_window_end_hook (w, 0, 0);
12875 update_end (f);
12876 }
12877
12878 /* Adjust Y positions of reused rows. */
12879 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12880 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12881 max_y = it.last_visible_y;
12882 for (row = first_reusable_row; row < first_row_to_display; ++row)
12883 {
12884 row->y -= dy;
12885 row->visible_height = row->height;
12886 if (row->y < min_y)
12887 row->visible_height -= min_y - row->y;
12888 if (row->y + row->height > max_y)
12889 row->visible_height -= row->y + row->height - max_y;
12890 row->redraw_fringe_bitmaps_p = 1;
12891 }
12892
12893 /* Scroll the current matrix. */
12894 xassert (nrows_scrolled > 0);
12895 rotate_matrix (w->current_matrix,
12896 start_vpos,
12897 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12898 -nrows_scrolled);
12899
12900 /* Disable rows not reused. */
12901 for (row -= nrows_scrolled; row < bottom_row; ++row)
12902 row->enabled_p = 0;
12903
12904 /* Point may have moved to a different line, so we cannot assume that
12905 the previous cursor position is valid; locate the correct row. */
12906 if (pt_row)
12907 {
12908 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12909 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12910 row++)
12911 {
12912 w->cursor.vpos++;
12913 w->cursor.y = row->y;
12914 }
12915 if (row < bottom_row)
12916 {
12917 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12918 while (glyph->charpos < PT)
12919 {
12920 w->cursor.hpos++;
12921 w->cursor.x += glyph->pixel_width;
12922 glyph++;
12923 }
12924 }
12925 }
12926
12927 /* Adjust window end. A null value of last_text_row means that
12928 the window end is in reused rows which in turn means that
12929 only its vpos can have changed. */
12930 if (last_text_row)
12931 {
12932 w->window_end_bytepos
12933 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12934 w->window_end_pos
12935 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12936 w->window_end_vpos
12937 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12938 }
12939 else
12940 {
12941 w->window_end_vpos
12942 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12943 }
12944
12945 w->window_end_valid = Qnil;
12946 w->desired_matrix->no_scrolling_p = 1;
12947
12948 #if GLYPH_DEBUG
12949 debug_method_add (w, "try_window_reusing_current_matrix 2");
12950 #endif
12951 return 1;
12952 }
12953
12954 return 0;
12955 }
12956
12957
12958 \f
12959 /************************************************************************
12960 Window redisplay reusing current matrix when buffer has changed
12961 ************************************************************************/
12962
12963 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
12964 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
12965 int *, int *));
12966 static struct glyph_row *
12967 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
12968 struct glyph_row *));
12969
12970
12971 /* Return the last row in MATRIX displaying text. If row START is
12972 non-null, start searching with that row. IT gives the dimensions
12973 of the display. Value is null if matrix is empty; otherwise it is
12974 a pointer to the row found. */
12975
12976 static struct glyph_row *
12977 find_last_row_displaying_text (matrix, it, start)
12978 struct glyph_matrix *matrix;
12979 struct it *it;
12980 struct glyph_row *start;
12981 {
12982 struct glyph_row *row, *row_found;
12983
12984 /* Set row_found to the last row in IT->w's current matrix
12985 displaying text. The loop looks funny but think of partially
12986 visible lines. */
12987 row_found = NULL;
12988 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
12989 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12990 {
12991 xassert (row->enabled_p);
12992 row_found = row;
12993 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
12994 break;
12995 ++row;
12996 }
12997
12998 return row_found;
12999 }
13000
13001
13002 /* Return the last row in the current matrix of W that is not affected
13003 by changes at the start of current_buffer that occurred since W's
13004 current matrix was built. Value is null if no such row exists.
13005
13006 BEG_UNCHANGED us the number of characters unchanged at the start of
13007 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13008 first changed character in current_buffer. Characters at positions <
13009 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13010 when the current matrix was built. */
13011
13012 static struct glyph_row *
13013 find_last_unchanged_at_beg_row (w)
13014 struct window *w;
13015 {
13016 int first_changed_pos = BEG + BEG_UNCHANGED;
13017 struct glyph_row *row;
13018 struct glyph_row *row_found = NULL;
13019 int yb = window_text_bottom_y (w);
13020
13021 /* Find the last row displaying unchanged text. */
13022 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13023 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13024 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13025 {
13026 if (/* If row ends before first_changed_pos, it is unchanged,
13027 except in some case. */
13028 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13029 /* When row ends in ZV and we write at ZV it is not
13030 unchanged. */
13031 && !row->ends_at_zv_p
13032 /* When first_changed_pos is the end of a continued line,
13033 row is not unchanged because it may be no longer
13034 continued. */
13035 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13036 && (row->continued_p
13037 || row->exact_window_width_line_p)))
13038 row_found = row;
13039
13040 /* Stop if last visible row. */
13041 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13042 break;
13043
13044 ++row;
13045 }
13046
13047 return row_found;
13048 }
13049
13050
13051 /* Find the first glyph row in the current matrix of W that is not
13052 affected by changes at the end of current_buffer since the
13053 time W's current matrix was built.
13054
13055 Return in *DELTA the number of chars by which buffer positions in
13056 unchanged text at the end of current_buffer must be adjusted.
13057
13058 Return in *DELTA_BYTES the corresponding number of bytes.
13059
13060 Value is null if no such row exists, i.e. all rows are affected by
13061 changes. */
13062
13063 static struct glyph_row *
13064 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13065 struct window *w;
13066 int *delta, *delta_bytes;
13067 {
13068 struct glyph_row *row;
13069 struct glyph_row *row_found = NULL;
13070
13071 *delta = *delta_bytes = 0;
13072
13073 /* Display must not have been paused, otherwise the current matrix
13074 is not up to date. */
13075 if (NILP (w->window_end_valid))
13076 abort ();
13077
13078 /* A value of window_end_pos >= END_UNCHANGED means that the window
13079 end is in the range of changed text. If so, there is no
13080 unchanged row at the end of W's current matrix. */
13081 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13082 return NULL;
13083
13084 /* Set row to the last row in W's current matrix displaying text. */
13085 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13086
13087 /* If matrix is entirely empty, no unchanged row exists. */
13088 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13089 {
13090 /* The value of row is the last glyph row in the matrix having a
13091 meaningful buffer position in it. The end position of row
13092 corresponds to window_end_pos. This allows us to translate
13093 buffer positions in the current matrix to current buffer
13094 positions for characters not in changed text. */
13095 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13096 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13097 int last_unchanged_pos, last_unchanged_pos_old;
13098 struct glyph_row *first_text_row
13099 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13100
13101 *delta = Z - Z_old;
13102 *delta_bytes = Z_BYTE - Z_BYTE_old;
13103
13104 /* Set last_unchanged_pos to the buffer position of the last
13105 character in the buffer that has not been changed. Z is the
13106 index + 1 of the last character in current_buffer, i.e. by
13107 subtracting END_UNCHANGED we get the index of the last
13108 unchanged character, and we have to add BEG to get its buffer
13109 position. */
13110 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13111 last_unchanged_pos_old = last_unchanged_pos - *delta;
13112
13113 /* Search backward from ROW for a row displaying a line that
13114 starts at a minimum position >= last_unchanged_pos_old. */
13115 for (; row > first_text_row; --row)
13116 {
13117 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13118 abort ();
13119
13120 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13121 row_found = row;
13122 }
13123 }
13124
13125 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13126 abort ();
13127
13128 return row_found;
13129 }
13130
13131
13132 /* Make sure that glyph rows in the current matrix of window W
13133 reference the same glyph memory as corresponding rows in the
13134 frame's frame matrix. This function is called after scrolling W's
13135 current matrix on a terminal frame in try_window_id and
13136 try_window_reusing_current_matrix. */
13137
13138 static void
13139 sync_frame_with_window_matrix_rows (w)
13140 struct window *w;
13141 {
13142 struct frame *f = XFRAME (w->frame);
13143 struct glyph_row *window_row, *window_row_end, *frame_row;
13144
13145 /* Preconditions: W must be a leaf window and full-width. Its frame
13146 must have a frame matrix. */
13147 xassert (NILP (w->hchild) && NILP (w->vchild));
13148 xassert (WINDOW_FULL_WIDTH_P (w));
13149 xassert (!FRAME_WINDOW_P (f));
13150
13151 /* If W is a full-width window, glyph pointers in W's current matrix
13152 have, by definition, to be the same as glyph pointers in the
13153 corresponding frame matrix. Note that frame matrices have no
13154 marginal areas (see build_frame_matrix). */
13155 window_row = w->current_matrix->rows;
13156 window_row_end = window_row + w->current_matrix->nrows;
13157 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13158 while (window_row < window_row_end)
13159 {
13160 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13161 struct glyph *end = window_row->glyphs[LAST_AREA];
13162
13163 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13164 frame_row->glyphs[TEXT_AREA] = start;
13165 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13166 frame_row->glyphs[LAST_AREA] = end;
13167
13168 /* Disable frame rows whose corresponding window rows have
13169 been disabled in try_window_id. */
13170 if (!window_row->enabled_p)
13171 frame_row->enabled_p = 0;
13172
13173 ++window_row, ++frame_row;
13174 }
13175 }
13176
13177
13178 /* Find the glyph row in window W containing CHARPOS. Consider all
13179 rows between START and END (not inclusive). END null means search
13180 all rows to the end of the display area of W. Value is the row
13181 containing CHARPOS or null. */
13182
13183 struct glyph_row *
13184 row_containing_pos (w, charpos, start, end, dy)
13185 struct window *w;
13186 int charpos;
13187 struct glyph_row *start, *end;
13188 int dy;
13189 {
13190 struct glyph_row *row = start;
13191 int last_y;
13192
13193 /* If we happen to start on a header-line, skip that. */
13194 if (row->mode_line_p)
13195 ++row;
13196
13197 if ((end && row >= end) || !row->enabled_p)
13198 return NULL;
13199
13200 last_y = window_text_bottom_y (w) - dy;
13201
13202 while (1)
13203 {
13204 /* Give up if we have gone too far. */
13205 if (end && row >= end)
13206 return NULL;
13207 /* This formerly returned if they were equal.
13208 I think that both quantities are of a "last plus one" type;
13209 if so, when they are equal, the row is within the screen. -- rms. */
13210 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13211 return NULL;
13212
13213 /* If it is in this row, return this row. */
13214 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13215 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13216 /* The end position of a row equals the start
13217 position of the next row. If CHARPOS is there, we
13218 would rather display it in the next line, except
13219 when this line ends in ZV. */
13220 && !row->ends_at_zv_p
13221 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13222 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13223 return row;
13224 ++row;
13225 }
13226 }
13227
13228
13229 /* Try to redisplay window W by reusing its existing display. W's
13230 current matrix must be up to date when this function is called,
13231 i.e. window_end_valid must not be nil.
13232
13233 Value is
13234
13235 1 if display has been updated
13236 0 if otherwise unsuccessful
13237 -1 if redisplay with same window start is known not to succeed
13238
13239 The following steps are performed:
13240
13241 1. Find the last row in the current matrix of W that is not
13242 affected by changes at the start of current_buffer. If no such row
13243 is found, give up.
13244
13245 2. Find the first row in W's current matrix that is not affected by
13246 changes at the end of current_buffer. Maybe there is no such row.
13247
13248 3. Display lines beginning with the row + 1 found in step 1 to the
13249 row found in step 2 or, if step 2 didn't find a row, to the end of
13250 the window.
13251
13252 4. If cursor is not known to appear on the window, give up.
13253
13254 5. If display stopped at the row found in step 2, scroll the
13255 display and current matrix as needed.
13256
13257 6. Maybe display some lines at the end of W, if we must. This can
13258 happen under various circumstances, like a partially visible line
13259 becoming fully visible, or because newly displayed lines are displayed
13260 in smaller font sizes.
13261
13262 7. Update W's window end information. */
13263
13264 static int
13265 try_window_id (w)
13266 struct window *w;
13267 {
13268 struct frame *f = XFRAME (w->frame);
13269 struct glyph_matrix *current_matrix = w->current_matrix;
13270 struct glyph_matrix *desired_matrix = w->desired_matrix;
13271 struct glyph_row *last_unchanged_at_beg_row;
13272 struct glyph_row *first_unchanged_at_end_row;
13273 struct glyph_row *row;
13274 struct glyph_row *bottom_row;
13275 int bottom_vpos;
13276 struct it it;
13277 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13278 struct text_pos start_pos;
13279 struct run run;
13280 int first_unchanged_at_end_vpos = 0;
13281 struct glyph_row *last_text_row, *last_text_row_at_end;
13282 struct text_pos start;
13283 int first_changed_charpos, last_changed_charpos;
13284
13285 #if GLYPH_DEBUG
13286 if (inhibit_try_window_id)
13287 return 0;
13288 #endif
13289
13290 /* This is handy for debugging. */
13291 #if 0
13292 #define GIVE_UP(X) \
13293 do { \
13294 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13295 return 0; \
13296 } while (0)
13297 #else
13298 #define GIVE_UP(X) return 0
13299 #endif
13300
13301 SET_TEXT_POS_FROM_MARKER (start, w->start);
13302
13303 /* Don't use this for mini-windows because these can show
13304 messages and mini-buffers, and we don't handle that here. */
13305 if (MINI_WINDOW_P (w))
13306 GIVE_UP (1);
13307
13308 /* This flag is used to prevent redisplay optimizations. */
13309 if (windows_or_buffers_changed || cursor_type_changed)
13310 GIVE_UP (2);
13311
13312 /* Verify that narrowing has not changed.
13313 Also verify that we were not told to prevent redisplay optimizations.
13314 It would be nice to further
13315 reduce the number of cases where this prevents try_window_id. */
13316 if (current_buffer->clip_changed
13317 || current_buffer->prevent_redisplay_optimizations_p)
13318 GIVE_UP (3);
13319
13320 /* Window must either use window-based redisplay or be full width. */
13321 if (!FRAME_WINDOW_P (f)
13322 && (!line_ins_del_ok
13323 || !WINDOW_FULL_WIDTH_P (w)))
13324 GIVE_UP (4);
13325
13326 /* Give up if point is not known NOT to appear in W. */
13327 if (PT < CHARPOS (start))
13328 GIVE_UP (5);
13329
13330 /* Another way to prevent redisplay optimizations. */
13331 if (XFASTINT (w->last_modified) == 0)
13332 GIVE_UP (6);
13333
13334 /* Verify that window is not hscrolled. */
13335 if (XFASTINT (w->hscroll) != 0)
13336 GIVE_UP (7);
13337
13338 /* Verify that display wasn't paused. */
13339 if (NILP (w->window_end_valid))
13340 GIVE_UP (8);
13341
13342 /* Can't use this if highlighting a region because a cursor movement
13343 will do more than just set the cursor. */
13344 if (!NILP (Vtransient_mark_mode)
13345 && !NILP (current_buffer->mark_active))
13346 GIVE_UP (9);
13347
13348 /* Likewise if highlighting trailing whitespace. */
13349 if (!NILP (Vshow_trailing_whitespace))
13350 GIVE_UP (11);
13351
13352 /* Likewise if showing a region. */
13353 if (!NILP (w->region_showing))
13354 GIVE_UP (10);
13355
13356 /* Can use this if overlay arrow position and or string have changed. */
13357 if (overlay_arrows_changed_p ())
13358 GIVE_UP (12);
13359
13360
13361 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13362 only if buffer has really changed. The reason is that the gap is
13363 initially at Z for freshly visited files. The code below would
13364 set end_unchanged to 0 in that case. */
13365 if (MODIFF > SAVE_MODIFF
13366 /* This seems to happen sometimes after saving a buffer. */
13367 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13368 {
13369 if (GPT - BEG < BEG_UNCHANGED)
13370 BEG_UNCHANGED = GPT - BEG;
13371 if (Z - GPT < END_UNCHANGED)
13372 END_UNCHANGED = Z - GPT;
13373 }
13374
13375 /* The position of the first and last character that has been changed. */
13376 first_changed_charpos = BEG + BEG_UNCHANGED;
13377 last_changed_charpos = Z - END_UNCHANGED;
13378
13379 /* If window starts after a line end, and the last change is in
13380 front of that newline, then changes don't affect the display.
13381 This case happens with stealth-fontification. Note that although
13382 the display is unchanged, glyph positions in the matrix have to
13383 be adjusted, of course. */
13384 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13385 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13386 && ((last_changed_charpos < CHARPOS (start)
13387 && CHARPOS (start) == BEGV)
13388 || (last_changed_charpos < CHARPOS (start) - 1
13389 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13390 {
13391 int Z_old, delta, Z_BYTE_old, delta_bytes;
13392 struct glyph_row *r0;
13393
13394 /* Compute how many chars/bytes have been added to or removed
13395 from the buffer. */
13396 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13397 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13398 delta = Z - Z_old;
13399 delta_bytes = Z_BYTE - Z_BYTE_old;
13400
13401 /* Give up if PT is not in the window. Note that it already has
13402 been checked at the start of try_window_id that PT is not in
13403 front of the window start. */
13404 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13405 GIVE_UP (13);
13406
13407 /* If window start is unchanged, we can reuse the whole matrix
13408 as is, after adjusting glyph positions. No need to compute
13409 the window end again, since its offset from Z hasn't changed. */
13410 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13411 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13412 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13413 /* PT must not be in a partially visible line. */
13414 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13415 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13416 {
13417 /* Adjust positions in the glyph matrix. */
13418 if (delta || delta_bytes)
13419 {
13420 struct glyph_row *r1
13421 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13422 increment_matrix_positions (w->current_matrix,
13423 MATRIX_ROW_VPOS (r0, current_matrix),
13424 MATRIX_ROW_VPOS (r1, current_matrix),
13425 delta, delta_bytes);
13426 }
13427
13428 /* Set the cursor. */
13429 row = row_containing_pos (w, PT, r0, NULL, 0);
13430 if (row)
13431 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13432 else
13433 abort ();
13434 return 1;
13435 }
13436 }
13437
13438 /* Handle the case that changes are all below what is displayed in
13439 the window, and that PT is in the window. This shortcut cannot
13440 be taken if ZV is visible in the window, and text has been added
13441 there that is visible in the window. */
13442 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13443 /* ZV is not visible in the window, or there are no
13444 changes at ZV, actually. */
13445 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13446 || first_changed_charpos == last_changed_charpos))
13447 {
13448 struct glyph_row *r0;
13449
13450 /* Give up if PT is not in the window. Note that it already has
13451 been checked at the start of try_window_id that PT is not in
13452 front of the window start. */
13453 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13454 GIVE_UP (14);
13455
13456 /* If window start is unchanged, we can reuse the whole matrix
13457 as is, without changing glyph positions since no text has
13458 been added/removed in front of the window end. */
13459 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13460 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13461 /* PT must not be in a partially visible line. */
13462 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13463 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13464 {
13465 /* We have to compute the window end anew since text
13466 can have been added/removed after it. */
13467 w->window_end_pos
13468 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13469 w->window_end_bytepos
13470 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13471
13472 /* Set the cursor. */
13473 row = row_containing_pos (w, PT, r0, NULL, 0);
13474 if (row)
13475 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13476 else
13477 abort ();
13478 return 2;
13479 }
13480 }
13481
13482 /* Give up if window start is in the changed area.
13483
13484 The condition used to read
13485
13486 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13487
13488 but why that was tested escapes me at the moment. */
13489 if (CHARPOS (start) >= first_changed_charpos
13490 && CHARPOS (start) <= last_changed_charpos)
13491 GIVE_UP (15);
13492
13493 /* Check that window start agrees with the start of the first glyph
13494 row in its current matrix. Check this after we know the window
13495 start is not in changed text, otherwise positions would not be
13496 comparable. */
13497 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13498 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13499 GIVE_UP (16);
13500
13501 /* Give up if the window ends in strings. Overlay strings
13502 at the end are difficult to handle, so don't try. */
13503 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13504 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13505 GIVE_UP (20);
13506
13507 /* Compute the position at which we have to start displaying new
13508 lines. Some of the lines at the top of the window might be
13509 reusable because they are not displaying changed text. Find the
13510 last row in W's current matrix not affected by changes at the
13511 start of current_buffer. Value is null if changes start in the
13512 first line of window. */
13513 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13514 if (last_unchanged_at_beg_row)
13515 {
13516 /* Avoid starting to display in the moddle of a character, a TAB
13517 for instance. This is easier than to set up the iterator
13518 exactly, and it's not a frequent case, so the additional
13519 effort wouldn't really pay off. */
13520 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13521 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13522 && last_unchanged_at_beg_row > w->current_matrix->rows)
13523 --last_unchanged_at_beg_row;
13524
13525 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13526 GIVE_UP (17);
13527
13528 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13529 GIVE_UP (18);
13530 start_pos = it.current.pos;
13531
13532 /* Start displaying new lines in the desired matrix at the same
13533 vpos we would use in the current matrix, i.e. below
13534 last_unchanged_at_beg_row. */
13535 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13536 current_matrix);
13537 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13538 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13539
13540 xassert (it.hpos == 0 && it.current_x == 0);
13541 }
13542 else
13543 {
13544 /* There are no reusable lines at the start of the window.
13545 Start displaying in the first text line. */
13546 start_display (&it, w, start);
13547 it.vpos = it.first_vpos;
13548 start_pos = it.current.pos;
13549 }
13550
13551 /* Find the first row that is not affected by changes at the end of
13552 the buffer. Value will be null if there is no unchanged row, in
13553 which case we must redisplay to the end of the window. delta
13554 will be set to the value by which buffer positions beginning with
13555 first_unchanged_at_end_row have to be adjusted due to text
13556 changes. */
13557 first_unchanged_at_end_row
13558 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13559 IF_DEBUG (debug_delta = delta);
13560 IF_DEBUG (debug_delta_bytes = delta_bytes);
13561
13562 /* Set stop_pos to the buffer position up to which we will have to
13563 display new lines. If first_unchanged_at_end_row != NULL, this
13564 is the buffer position of the start of the line displayed in that
13565 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13566 that we don't stop at a buffer position. */
13567 stop_pos = 0;
13568 if (first_unchanged_at_end_row)
13569 {
13570 xassert (last_unchanged_at_beg_row == NULL
13571 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13572
13573 /* If this is a continuation line, move forward to the next one
13574 that isn't. Changes in lines above affect this line.
13575 Caution: this may move first_unchanged_at_end_row to a row
13576 not displaying text. */
13577 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13578 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13579 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13580 < it.last_visible_y))
13581 ++first_unchanged_at_end_row;
13582
13583 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13584 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13585 >= it.last_visible_y))
13586 first_unchanged_at_end_row = NULL;
13587 else
13588 {
13589 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13590 + delta);
13591 first_unchanged_at_end_vpos
13592 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13593 xassert (stop_pos >= Z - END_UNCHANGED);
13594 }
13595 }
13596 else if (last_unchanged_at_beg_row == NULL)
13597 GIVE_UP (19);
13598
13599
13600 #if GLYPH_DEBUG
13601
13602 /* Either there is no unchanged row at the end, or the one we have
13603 now displays text. This is a necessary condition for the window
13604 end pos calculation at the end of this function. */
13605 xassert (first_unchanged_at_end_row == NULL
13606 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13607
13608 debug_last_unchanged_at_beg_vpos
13609 = (last_unchanged_at_beg_row
13610 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13611 : -1);
13612 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13613
13614 #endif /* GLYPH_DEBUG != 0 */
13615
13616
13617 /* Display new lines. Set last_text_row to the last new line
13618 displayed which has text on it, i.e. might end up as being the
13619 line where the window_end_vpos is. */
13620 w->cursor.vpos = -1;
13621 last_text_row = NULL;
13622 overlay_arrow_seen = 0;
13623 while (it.current_y < it.last_visible_y
13624 && !fonts_changed_p
13625 && (first_unchanged_at_end_row == NULL
13626 || IT_CHARPOS (it) < stop_pos))
13627 {
13628 if (display_line (&it))
13629 last_text_row = it.glyph_row - 1;
13630 }
13631
13632 if (fonts_changed_p)
13633 return -1;
13634
13635
13636 /* Compute differences in buffer positions, y-positions etc. for
13637 lines reused at the bottom of the window. Compute what we can
13638 scroll. */
13639 if (first_unchanged_at_end_row
13640 /* No lines reused because we displayed everything up to the
13641 bottom of the window. */
13642 && it.current_y < it.last_visible_y)
13643 {
13644 dvpos = (it.vpos
13645 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13646 current_matrix));
13647 dy = it.current_y - first_unchanged_at_end_row->y;
13648 run.current_y = first_unchanged_at_end_row->y;
13649 run.desired_y = run.current_y + dy;
13650 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13651 }
13652 else
13653 {
13654 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13655 first_unchanged_at_end_row = NULL;
13656 }
13657 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13658
13659
13660 /* Find the cursor if not already found. We have to decide whether
13661 PT will appear on this window (it sometimes doesn't, but this is
13662 not a very frequent case.) This decision has to be made before
13663 the current matrix is altered. A value of cursor.vpos < 0 means
13664 that PT is either in one of the lines beginning at
13665 first_unchanged_at_end_row or below the window. Don't care for
13666 lines that might be displayed later at the window end; as
13667 mentioned, this is not a frequent case. */
13668 if (w->cursor.vpos < 0)
13669 {
13670 /* Cursor in unchanged rows at the top? */
13671 if (PT < CHARPOS (start_pos)
13672 && last_unchanged_at_beg_row)
13673 {
13674 row = row_containing_pos (w, PT,
13675 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13676 last_unchanged_at_beg_row + 1, 0);
13677 if (row)
13678 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13679 }
13680
13681 /* Start from first_unchanged_at_end_row looking for PT. */
13682 else if (first_unchanged_at_end_row)
13683 {
13684 row = row_containing_pos (w, PT - delta,
13685 first_unchanged_at_end_row, NULL, 0);
13686 if (row)
13687 set_cursor_from_row (w, row, w->current_matrix, delta,
13688 delta_bytes, dy, dvpos);
13689 }
13690
13691 /* Give up if cursor was not found. */
13692 if (w->cursor.vpos < 0)
13693 {
13694 clear_glyph_matrix (w->desired_matrix);
13695 return -1;
13696 }
13697 }
13698
13699 /* Don't let the cursor end in the scroll margins. */
13700 {
13701 int this_scroll_margin, cursor_height;
13702
13703 this_scroll_margin = max (0, scroll_margin);
13704 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13705 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13706 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13707
13708 if ((w->cursor.y < this_scroll_margin
13709 && CHARPOS (start) > BEGV)
13710 /* Old redisplay didn't take scroll margin into account at the bottom,
13711 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13712 || (w->cursor.y + (make_cursor_line_fully_visible_p
13713 ? cursor_height + this_scroll_margin
13714 : 1)) > it.last_visible_y)
13715 {
13716 w->cursor.vpos = -1;
13717 clear_glyph_matrix (w->desired_matrix);
13718 return -1;
13719 }
13720 }
13721
13722 /* Scroll the display. Do it before changing the current matrix so
13723 that xterm.c doesn't get confused about where the cursor glyph is
13724 found. */
13725 if (dy && run.height)
13726 {
13727 update_begin (f);
13728
13729 if (FRAME_WINDOW_P (f))
13730 {
13731 rif->update_window_begin_hook (w);
13732 rif->clear_window_mouse_face (w);
13733 rif->scroll_run_hook (w, &run);
13734 rif->update_window_end_hook (w, 0, 0);
13735 }
13736 else
13737 {
13738 /* Terminal frame. In this case, dvpos gives the number of
13739 lines to scroll by; dvpos < 0 means scroll up. */
13740 int first_unchanged_at_end_vpos
13741 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13742 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13743 int end = (WINDOW_TOP_EDGE_LINE (w)
13744 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13745 + window_internal_height (w));
13746
13747 /* Perform the operation on the screen. */
13748 if (dvpos > 0)
13749 {
13750 /* Scroll last_unchanged_at_beg_row to the end of the
13751 window down dvpos lines. */
13752 set_terminal_window (end);
13753
13754 /* On dumb terminals delete dvpos lines at the end
13755 before inserting dvpos empty lines. */
13756 if (!scroll_region_ok)
13757 ins_del_lines (end - dvpos, -dvpos);
13758
13759 /* Insert dvpos empty lines in front of
13760 last_unchanged_at_beg_row. */
13761 ins_del_lines (from, dvpos);
13762 }
13763 else if (dvpos < 0)
13764 {
13765 /* Scroll up last_unchanged_at_beg_vpos to the end of
13766 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13767 set_terminal_window (end);
13768
13769 /* Delete dvpos lines in front of
13770 last_unchanged_at_beg_vpos. ins_del_lines will set
13771 the cursor to the given vpos and emit |dvpos| delete
13772 line sequences. */
13773 ins_del_lines (from + dvpos, dvpos);
13774
13775 /* On a dumb terminal insert dvpos empty lines at the
13776 end. */
13777 if (!scroll_region_ok)
13778 ins_del_lines (end + dvpos, -dvpos);
13779 }
13780
13781 set_terminal_window (0);
13782 }
13783
13784 update_end (f);
13785 }
13786
13787 /* Shift reused rows of the current matrix to the right position.
13788 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13789 text. */
13790 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13791 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13792 if (dvpos < 0)
13793 {
13794 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13795 bottom_vpos, dvpos);
13796 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13797 bottom_vpos, 0);
13798 }
13799 else if (dvpos > 0)
13800 {
13801 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13802 bottom_vpos, dvpos);
13803 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13804 first_unchanged_at_end_vpos + dvpos, 0);
13805 }
13806
13807 /* For frame-based redisplay, make sure that current frame and window
13808 matrix are in sync with respect to glyph memory. */
13809 if (!FRAME_WINDOW_P (f))
13810 sync_frame_with_window_matrix_rows (w);
13811
13812 /* Adjust buffer positions in reused rows. */
13813 if (delta)
13814 increment_matrix_positions (current_matrix,
13815 first_unchanged_at_end_vpos + dvpos,
13816 bottom_vpos, delta, delta_bytes);
13817
13818 /* Adjust Y positions. */
13819 if (dy)
13820 shift_glyph_matrix (w, current_matrix,
13821 first_unchanged_at_end_vpos + dvpos,
13822 bottom_vpos, dy);
13823
13824 if (first_unchanged_at_end_row)
13825 first_unchanged_at_end_row += dvpos;
13826
13827 /* If scrolling up, there may be some lines to display at the end of
13828 the window. */
13829 last_text_row_at_end = NULL;
13830 if (dy < 0)
13831 {
13832 /* Scrolling up can leave for example a partially visible line
13833 at the end of the window to be redisplayed. */
13834 /* Set last_row to the glyph row in the current matrix where the
13835 window end line is found. It has been moved up or down in
13836 the matrix by dvpos. */
13837 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13838 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13839
13840 /* If last_row is the window end line, it should display text. */
13841 xassert (last_row->displays_text_p);
13842
13843 /* If window end line was partially visible before, begin
13844 displaying at that line. Otherwise begin displaying with the
13845 line following it. */
13846 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13847 {
13848 init_to_row_start (&it, w, last_row);
13849 it.vpos = last_vpos;
13850 it.current_y = last_row->y;
13851 }
13852 else
13853 {
13854 init_to_row_end (&it, w, last_row);
13855 it.vpos = 1 + last_vpos;
13856 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13857 ++last_row;
13858 }
13859
13860 /* We may start in a continuation line. If so, we have to
13861 get the right continuation_lines_width and current_x. */
13862 it.continuation_lines_width = last_row->continuation_lines_width;
13863 it.hpos = it.current_x = 0;
13864
13865 /* Display the rest of the lines at the window end. */
13866 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13867 while (it.current_y < it.last_visible_y
13868 && !fonts_changed_p)
13869 {
13870 /* Is it always sure that the display agrees with lines in
13871 the current matrix? I don't think so, so we mark rows
13872 displayed invalid in the current matrix by setting their
13873 enabled_p flag to zero. */
13874 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13875 if (display_line (&it))
13876 last_text_row_at_end = it.glyph_row - 1;
13877 }
13878 }
13879
13880 /* Update window_end_pos and window_end_vpos. */
13881 if (first_unchanged_at_end_row
13882 && first_unchanged_at_end_row->y < it.last_visible_y
13883 && !last_text_row_at_end)
13884 {
13885 /* Window end line if one of the preserved rows from the current
13886 matrix. Set row to the last row displaying text in current
13887 matrix starting at first_unchanged_at_end_row, after
13888 scrolling. */
13889 xassert (first_unchanged_at_end_row->displays_text_p);
13890 row = find_last_row_displaying_text (w->current_matrix, &it,
13891 first_unchanged_at_end_row);
13892 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13893
13894 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13895 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13896 w->window_end_vpos
13897 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13898 xassert (w->window_end_bytepos >= 0);
13899 IF_DEBUG (debug_method_add (w, "A"));
13900 }
13901 else if (last_text_row_at_end)
13902 {
13903 w->window_end_pos
13904 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13905 w->window_end_bytepos
13906 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13907 w->window_end_vpos
13908 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13909 xassert (w->window_end_bytepos >= 0);
13910 IF_DEBUG (debug_method_add (w, "B"));
13911 }
13912 else if (last_text_row)
13913 {
13914 /* We have displayed either to the end of the window or at the
13915 end of the window, i.e. the last row with text is to be found
13916 in the desired matrix. */
13917 w->window_end_pos
13918 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13919 w->window_end_bytepos
13920 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13921 w->window_end_vpos
13922 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13923 xassert (w->window_end_bytepos >= 0);
13924 }
13925 else if (first_unchanged_at_end_row == NULL
13926 && last_text_row == NULL
13927 && last_text_row_at_end == NULL)
13928 {
13929 /* Displayed to end of window, but no line containing text was
13930 displayed. Lines were deleted at the end of the window. */
13931 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13932 int vpos = XFASTINT (w->window_end_vpos);
13933 struct glyph_row *current_row = current_matrix->rows + vpos;
13934 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13935
13936 for (row = NULL;
13937 row == NULL && vpos >= first_vpos;
13938 --vpos, --current_row, --desired_row)
13939 {
13940 if (desired_row->enabled_p)
13941 {
13942 if (desired_row->displays_text_p)
13943 row = desired_row;
13944 }
13945 else if (current_row->displays_text_p)
13946 row = current_row;
13947 }
13948
13949 xassert (row != NULL);
13950 w->window_end_vpos = make_number (vpos + 1);
13951 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13952 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13953 xassert (w->window_end_bytepos >= 0);
13954 IF_DEBUG (debug_method_add (w, "C"));
13955 }
13956 else
13957 abort ();
13958
13959 #if 0 /* This leads to problems, for instance when the cursor is
13960 at ZV, and the cursor line displays no text. */
13961 /* Disable rows below what's displayed in the window. This makes
13962 debugging easier. */
13963 enable_glyph_matrix_rows (current_matrix,
13964 XFASTINT (w->window_end_vpos) + 1,
13965 bottom_vpos, 0);
13966 #endif
13967
13968 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
13969 debug_end_vpos = XFASTINT (w->window_end_vpos));
13970
13971 /* Record that display has not been completed. */
13972 w->window_end_valid = Qnil;
13973 w->desired_matrix->no_scrolling_p = 1;
13974 return 3;
13975
13976 #undef GIVE_UP
13977 }
13978
13979
13980 \f
13981 /***********************************************************************
13982 More debugging support
13983 ***********************************************************************/
13984
13985 #if GLYPH_DEBUG
13986
13987 void dump_glyph_row P_ ((struct glyph_row *, int, int));
13988 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
13989 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
13990
13991
13992 /* Dump the contents of glyph matrix MATRIX on stderr.
13993
13994 GLYPHS 0 means don't show glyph contents.
13995 GLYPHS 1 means show glyphs in short form
13996 GLYPHS > 1 means show glyphs in long form. */
13997
13998 void
13999 dump_glyph_matrix (matrix, glyphs)
14000 struct glyph_matrix *matrix;
14001 int glyphs;
14002 {
14003 int i;
14004 for (i = 0; i < matrix->nrows; ++i)
14005 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14006 }
14007
14008
14009 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14010 the glyph row and area where the glyph comes from. */
14011
14012 void
14013 dump_glyph (row, glyph, area)
14014 struct glyph_row *row;
14015 struct glyph *glyph;
14016 int area;
14017 {
14018 if (glyph->type == CHAR_GLYPH)
14019 {
14020 fprintf (stderr,
14021 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14022 glyph - row->glyphs[TEXT_AREA],
14023 'C',
14024 glyph->charpos,
14025 (BUFFERP (glyph->object)
14026 ? 'B'
14027 : (STRINGP (glyph->object)
14028 ? 'S'
14029 : '-')),
14030 glyph->pixel_width,
14031 glyph->u.ch,
14032 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14033 ? glyph->u.ch
14034 : '.'),
14035 glyph->face_id,
14036 glyph->left_box_line_p,
14037 glyph->right_box_line_p);
14038 }
14039 else if (glyph->type == STRETCH_GLYPH)
14040 {
14041 fprintf (stderr,
14042 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14043 glyph - row->glyphs[TEXT_AREA],
14044 'S',
14045 glyph->charpos,
14046 (BUFFERP (glyph->object)
14047 ? 'B'
14048 : (STRINGP (glyph->object)
14049 ? 'S'
14050 : '-')),
14051 glyph->pixel_width,
14052 0,
14053 '.',
14054 glyph->face_id,
14055 glyph->left_box_line_p,
14056 glyph->right_box_line_p);
14057 }
14058 else if (glyph->type == IMAGE_GLYPH)
14059 {
14060 fprintf (stderr,
14061 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14062 glyph - row->glyphs[TEXT_AREA],
14063 'I',
14064 glyph->charpos,
14065 (BUFFERP (glyph->object)
14066 ? 'B'
14067 : (STRINGP (glyph->object)
14068 ? 'S'
14069 : '-')),
14070 glyph->pixel_width,
14071 glyph->u.img_id,
14072 '.',
14073 glyph->face_id,
14074 glyph->left_box_line_p,
14075 glyph->right_box_line_p);
14076 }
14077 }
14078
14079
14080 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14081 GLYPHS 0 means don't show glyph contents.
14082 GLYPHS 1 means show glyphs in short form
14083 GLYPHS > 1 means show glyphs in long form. */
14084
14085 void
14086 dump_glyph_row (row, vpos, glyphs)
14087 struct glyph_row *row;
14088 int vpos, glyphs;
14089 {
14090 if (glyphs != 1)
14091 {
14092 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
14093 fprintf (stderr, "=======================================================================\n");
14094
14095 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
14096 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14097 vpos,
14098 MATRIX_ROW_START_CHARPOS (row),
14099 MATRIX_ROW_END_CHARPOS (row),
14100 row->used[TEXT_AREA],
14101 row->contains_overlapping_glyphs_p,
14102 row->enabled_p,
14103 row->truncated_on_left_p,
14104 row->truncated_on_right_p,
14105 row->overlay_arrow_p,
14106 row->continued_p,
14107 MATRIX_ROW_CONTINUATION_LINE_P (row),
14108 row->displays_text_p,
14109 row->ends_at_zv_p,
14110 row->fill_line_p,
14111 row->ends_in_middle_of_char_p,
14112 row->starts_in_middle_of_char_p,
14113 row->mouse_face_p,
14114 row->x,
14115 row->y,
14116 row->pixel_width,
14117 row->height,
14118 row->visible_height,
14119 row->ascent,
14120 row->phys_ascent);
14121 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14122 row->end.overlay_string_index,
14123 row->continuation_lines_width);
14124 fprintf (stderr, "%9d %5d\n",
14125 CHARPOS (row->start.string_pos),
14126 CHARPOS (row->end.string_pos));
14127 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14128 row->end.dpvec_index);
14129 }
14130
14131 if (glyphs > 1)
14132 {
14133 int area;
14134
14135 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14136 {
14137 struct glyph *glyph = row->glyphs[area];
14138 struct glyph *glyph_end = glyph + row->used[area];
14139
14140 /* Glyph for a line end in text. */
14141 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14142 ++glyph_end;
14143
14144 if (glyph < glyph_end)
14145 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14146
14147 for (; glyph < glyph_end; ++glyph)
14148 dump_glyph (row, glyph, area);
14149 }
14150 }
14151 else if (glyphs == 1)
14152 {
14153 int area;
14154
14155 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14156 {
14157 char *s = (char *) alloca (row->used[area] + 1);
14158 int i;
14159
14160 for (i = 0; i < row->used[area]; ++i)
14161 {
14162 struct glyph *glyph = row->glyphs[area] + i;
14163 if (glyph->type == CHAR_GLYPH
14164 && glyph->u.ch < 0x80
14165 && glyph->u.ch >= ' ')
14166 s[i] = glyph->u.ch;
14167 else
14168 s[i] = '.';
14169 }
14170
14171 s[i] = '\0';
14172 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14173 }
14174 }
14175 }
14176
14177
14178 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14179 Sdump_glyph_matrix, 0, 1, "p",
14180 doc: /* Dump the current matrix of the selected window to stderr.
14181 Shows contents of glyph row structures. With non-nil
14182 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14183 glyphs in short form, otherwise show glyphs in long form. */)
14184 (glyphs)
14185 Lisp_Object glyphs;
14186 {
14187 struct window *w = XWINDOW (selected_window);
14188 struct buffer *buffer = XBUFFER (w->buffer);
14189
14190 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14191 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14192 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14193 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14194 fprintf (stderr, "=============================================\n");
14195 dump_glyph_matrix (w->current_matrix,
14196 NILP (glyphs) ? 0 : XINT (glyphs));
14197 return Qnil;
14198 }
14199
14200
14201 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14202 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14203 ()
14204 {
14205 struct frame *f = XFRAME (selected_frame);
14206 dump_glyph_matrix (f->current_matrix, 1);
14207 return Qnil;
14208 }
14209
14210
14211 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14212 doc: /* Dump glyph row ROW to stderr.
14213 GLYPH 0 means don't dump glyphs.
14214 GLYPH 1 means dump glyphs in short form.
14215 GLYPH > 1 or omitted means dump glyphs in long form. */)
14216 (row, glyphs)
14217 Lisp_Object row, glyphs;
14218 {
14219 struct glyph_matrix *matrix;
14220 int vpos;
14221
14222 CHECK_NUMBER (row);
14223 matrix = XWINDOW (selected_window)->current_matrix;
14224 vpos = XINT (row);
14225 if (vpos >= 0 && vpos < matrix->nrows)
14226 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14227 vpos,
14228 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14229 return Qnil;
14230 }
14231
14232
14233 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14234 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14235 GLYPH 0 means don't dump glyphs.
14236 GLYPH 1 means dump glyphs in short form.
14237 GLYPH > 1 or omitted means dump glyphs in long form. */)
14238 (row, glyphs)
14239 Lisp_Object row, glyphs;
14240 {
14241 struct frame *sf = SELECTED_FRAME ();
14242 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14243 int vpos;
14244
14245 CHECK_NUMBER (row);
14246 vpos = XINT (row);
14247 if (vpos >= 0 && vpos < m->nrows)
14248 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14249 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14250 return Qnil;
14251 }
14252
14253
14254 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14255 doc: /* Toggle tracing of redisplay.
14256 With ARG, turn tracing on if and only if ARG is positive. */)
14257 (arg)
14258 Lisp_Object arg;
14259 {
14260 if (NILP (arg))
14261 trace_redisplay_p = !trace_redisplay_p;
14262 else
14263 {
14264 arg = Fprefix_numeric_value (arg);
14265 trace_redisplay_p = XINT (arg) > 0;
14266 }
14267
14268 return Qnil;
14269 }
14270
14271
14272 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14273 doc: /* Like `format', but print result to stderr.
14274 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14275 (nargs, args)
14276 int nargs;
14277 Lisp_Object *args;
14278 {
14279 Lisp_Object s = Fformat (nargs, args);
14280 fprintf (stderr, "%s", SDATA (s));
14281 return Qnil;
14282 }
14283
14284 #endif /* GLYPH_DEBUG */
14285
14286
14287 \f
14288 /***********************************************************************
14289 Building Desired Matrix Rows
14290 ***********************************************************************/
14291
14292 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14293 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14294
14295 static struct glyph_row *
14296 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14297 struct window *w;
14298 Lisp_Object overlay_arrow_string;
14299 {
14300 struct frame *f = XFRAME (WINDOW_FRAME (w));
14301 struct buffer *buffer = XBUFFER (w->buffer);
14302 struct buffer *old = current_buffer;
14303 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14304 int arrow_len = SCHARS (overlay_arrow_string);
14305 const unsigned char *arrow_end = arrow_string + arrow_len;
14306 const unsigned char *p;
14307 struct it it;
14308 int multibyte_p;
14309 int n_glyphs_before;
14310
14311 set_buffer_temp (buffer);
14312 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14313 it.glyph_row->used[TEXT_AREA] = 0;
14314 SET_TEXT_POS (it.position, 0, 0);
14315
14316 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14317 p = arrow_string;
14318 while (p < arrow_end)
14319 {
14320 Lisp_Object face, ilisp;
14321
14322 /* Get the next character. */
14323 if (multibyte_p)
14324 it.c = string_char_and_length (p, arrow_len, &it.len);
14325 else
14326 it.c = *p, it.len = 1;
14327 p += it.len;
14328
14329 /* Get its face. */
14330 ilisp = make_number (p - arrow_string);
14331 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14332 it.face_id = compute_char_face (f, it.c, face);
14333
14334 /* Compute its width, get its glyphs. */
14335 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14336 SET_TEXT_POS (it.position, -1, -1);
14337 PRODUCE_GLYPHS (&it);
14338
14339 /* If this character doesn't fit any more in the line, we have
14340 to remove some glyphs. */
14341 if (it.current_x > it.last_visible_x)
14342 {
14343 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14344 break;
14345 }
14346 }
14347
14348 set_buffer_temp (old);
14349 return it.glyph_row;
14350 }
14351
14352
14353 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14354 glyphs are only inserted for terminal frames since we can't really
14355 win with truncation glyphs when partially visible glyphs are
14356 involved. Which glyphs to insert is determined by
14357 produce_special_glyphs. */
14358
14359 static void
14360 insert_left_trunc_glyphs (it)
14361 struct it *it;
14362 {
14363 struct it truncate_it;
14364 struct glyph *from, *end, *to, *toend;
14365
14366 xassert (!FRAME_WINDOW_P (it->f));
14367
14368 /* Get the truncation glyphs. */
14369 truncate_it = *it;
14370 truncate_it.current_x = 0;
14371 truncate_it.face_id = DEFAULT_FACE_ID;
14372 truncate_it.glyph_row = &scratch_glyph_row;
14373 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14374 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14375 truncate_it.object = make_number (0);
14376 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14377
14378 /* Overwrite glyphs from IT with truncation glyphs. */
14379 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14380 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14381 to = it->glyph_row->glyphs[TEXT_AREA];
14382 toend = to + it->glyph_row->used[TEXT_AREA];
14383
14384 while (from < end)
14385 *to++ = *from++;
14386
14387 /* There may be padding glyphs left over. Overwrite them too. */
14388 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14389 {
14390 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14391 while (from < end)
14392 *to++ = *from++;
14393 }
14394
14395 if (to > toend)
14396 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14397 }
14398
14399
14400 /* Compute the pixel height and width of IT->glyph_row.
14401
14402 Most of the time, ascent and height of a display line will be equal
14403 to the max_ascent and max_height values of the display iterator
14404 structure. This is not the case if
14405
14406 1. We hit ZV without displaying anything. In this case, max_ascent
14407 and max_height will be zero.
14408
14409 2. We have some glyphs that don't contribute to the line height.
14410 (The glyph row flag contributes_to_line_height_p is for future
14411 pixmap extensions).
14412
14413 The first case is easily covered by using default values because in
14414 these cases, the line height does not really matter, except that it
14415 must not be zero. */
14416
14417 static void
14418 compute_line_metrics (it)
14419 struct it *it;
14420 {
14421 struct glyph_row *row = it->glyph_row;
14422 int area, i;
14423
14424 if (FRAME_WINDOW_P (it->f))
14425 {
14426 int i, min_y, max_y;
14427
14428 /* The line may consist of one space only, that was added to
14429 place the cursor on it. If so, the row's height hasn't been
14430 computed yet. */
14431 if (row->height == 0)
14432 {
14433 if (it->max_ascent + it->max_descent == 0)
14434 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14435 row->ascent = it->max_ascent;
14436 row->height = it->max_ascent + it->max_descent;
14437 row->phys_ascent = it->max_phys_ascent;
14438 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14439 row->extra_line_spacing = it->max_extra_line_spacing;
14440 }
14441
14442 /* Compute the width of this line. */
14443 row->pixel_width = row->x;
14444 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14445 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14446
14447 xassert (row->pixel_width >= 0);
14448 xassert (row->ascent >= 0 && row->height > 0);
14449
14450 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14451 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14452
14453 /* If first line's physical ascent is larger than its logical
14454 ascent, use the physical ascent, and make the row taller.
14455 This makes accented characters fully visible. */
14456 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14457 && row->phys_ascent > row->ascent)
14458 {
14459 row->height += row->phys_ascent - row->ascent;
14460 row->ascent = row->phys_ascent;
14461 }
14462
14463 /* Compute how much of the line is visible. */
14464 row->visible_height = row->height;
14465
14466 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14467 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14468
14469 if (row->y < min_y)
14470 row->visible_height -= min_y - row->y;
14471 if (row->y + row->height > max_y)
14472 row->visible_height -= row->y + row->height - max_y;
14473 }
14474 else
14475 {
14476 row->pixel_width = row->used[TEXT_AREA];
14477 if (row->continued_p)
14478 row->pixel_width -= it->continuation_pixel_width;
14479 else if (row->truncated_on_right_p)
14480 row->pixel_width -= it->truncation_pixel_width;
14481 row->ascent = row->phys_ascent = 0;
14482 row->height = row->phys_height = row->visible_height = 1;
14483 row->extra_line_spacing = 0;
14484 }
14485
14486 /* Compute a hash code for this row. */
14487 row->hash = 0;
14488 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14489 for (i = 0; i < row->used[area]; ++i)
14490 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14491 + row->glyphs[area][i].u.val
14492 + row->glyphs[area][i].face_id
14493 + row->glyphs[area][i].padding_p
14494 + (row->glyphs[area][i].type << 2));
14495
14496 it->max_ascent = it->max_descent = 0;
14497 it->max_phys_ascent = it->max_phys_descent = 0;
14498 }
14499
14500
14501 /* Append one space to the glyph row of iterator IT if doing a
14502 window-based redisplay. The space has the same face as
14503 IT->face_id. Value is non-zero if a space was added.
14504
14505 This function is called to make sure that there is always one glyph
14506 at the end of a glyph row that the cursor can be set on under
14507 window-systems. (If there weren't such a glyph we would not know
14508 how wide and tall a box cursor should be displayed).
14509
14510 At the same time this space let's a nicely handle clearing to the
14511 end of the line if the row ends in italic text. */
14512
14513 static int
14514 append_space_for_newline (it, default_face_p)
14515 struct it *it;
14516 int default_face_p;
14517 {
14518 if (FRAME_WINDOW_P (it->f))
14519 {
14520 int n = it->glyph_row->used[TEXT_AREA];
14521
14522 if (it->glyph_row->glyphs[TEXT_AREA] + n
14523 < it->glyph_row->glyphs[1 + TEXT_AREA])
14524 {
14525 /* Save some values that must not be changed.
14526 Must save IT->c and IT->len because otherwise
14527 ITERATOR_AT_END_P wouldn't work anymore after
14528 append_space_for_newline has been called. */
14529 enum display_element_type saved_what = it->what;
14530 int saved_c = it->c, saved_len = it->len;
14531 int saved_x = it->current_x;
14532 int saved_face_id = it->face_id;
14533 struct text_pos saved_pos;
14534 Lisp_Object saved_object;
14535 struct face *face;
14536
14537 saved_object = it->object;
14538 saved_pos = it->position;
14539
14540 it->what = IT_CHARACTER;
14541 bzero (&it->position, sizeof it->position);
14542 it->object = make_number (0);
14543 it->c = ' ';
14544 it->len = 1;
14545
14546 if (default_face_p)
14547 it->face_id = DEFAULT_FACE_ID;
14548 else if (it->face_before_selective_p)
14549 it->face_id = it->saved_face_id;
14550 face = FACE_FROM_ID (it->f, it->face_id);
14551 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14552
14553 PRODUCE_GLYPHS (it);
14554
14555 it->override_ascent = -1;
14556 it->constrain_row_ascent_descent_p = 0;
14557 it->current_x = saved_x;
14558 it->object = saved_object;
14559 it->position = saved_pos;
14560 it->what = saved_what;
14561 it->face_id = saved_face_id;
14562 it->len = saved_len;
14563 it->c = saved_c;
14564 return 1;
14565 }
14566 }
14567
14568 return 0;
14569 }
14570
14571
14572 /* Extend the face of the last glyph in the text area of IT->glyph_row
14573 to the end of the display line. Called from display_line.
14574 If the glyph row is empty, add a space glyph to it so that we
14575 know the face to draw. Set the glyph row flag fill_line_p. */
14576
14577 static void
14578 extend_face_to_end_of_line (it)
14579 struct it *it;
14580 {
14581 struct face *face;
14582 struct frame *f = it->f;
14583
14584 /* If line is already filled, do nothing. */
14585 if (it->current_x >= it->last_visible_x)
14586 return;
14587
14588 /* Face extension extends the background and box of IT->face_id
14589 to the end of the line. If the background equals the background
14590 of the frame, we don't have to do anything. */
14591 if (it->face_before_selective_p)
14592 face = FACE_FROM_ID (it->f, it->saved_face_id);
14593 else
14594 face = FACE_FROM_ID (f, it->face_id);
14595
14596 if (FRAME_WINDOW_P (f)
14597 && face->box == FACE_NO_BOX
14598 && face->background == FRAME_BACKGROUND_PIXEL (f)
14599 && !face->stipple)
14600 return;
14601
14602 /* Set the glyph row flag indicating that the face of the last glyph
14603 in the text area has to be drawn to the end of the text area. */
14604 it->glyph_row->fill_line_p = 1;
14605
14606 /* If current character of IT is not ASCII, make sure we have the
14607 ASCII face. This will be automatically undone the next time
14608 get_next_display_element returns a multibyte character. Note
14609 that the character will always be single byte in unibyte text. */
14610 if (!SINGLE_BYTE_CHAR_P (it->c))
14611 {
14612 it->face_id = FACE_FOR_CHAR (f, face, 0);
14613 }
14614
14615 if (FRAME_WINDOW_P (f))
14616 {
14617 /* If the row is empty, add a space with the current face of IT,
14618 so that we know which face to draw. */
14619 if (it->glyph_row->used[TEXT_AREA] == 0)
14620 {
14621 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14622 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14623 it->glyph_row->used[TEXT_AREA] = 1;
14624 }
14625 }
14626 else
14627 {
14628 /* Save some values that must not be changed. */
14629 int saved_x = it->current_x;
14630 struct text_pos saved_pos;
14631 Lisp_Object saved_object;
14632 enum display_element_type saved_what = it->what;
14633 int saved_face_id = it->face_id;
14634
14635 saved_object = it->object;
14636 saved_pos = it->position;
14637
14638 it->what = IT_CHARACTER;
14639 bzero (&it->position, sizeof it->position);
14640 it->object = make_number (0);
14641 it->c = ' ';
14642 it->len = 1;
14643 it->face_id = face->id;
14644
14645 PRODUCE_GLYPHS (it);
14646
14647 while (it->current_x <= it->last_visible_x)
14648 PRODUCE_GLYPHS (it);
14649
14650 /* Don't count these blanks really. It would let us insert a left
14651 truncation glyph below and make us set the cursor on them, maybe. */
14652 it->current_x = saved_x;
14653 it->object = saved_object;
14654 it->position = saved_pos;
14655 it->what = saved_what;
14656 it->face_id = saved_face_id;
14657 }
14658 }
14659
14660
14661 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14662 trailing whitespace. */
14663
14664 static int
14665 trailing_whitespace_p (charpos)
14666 int charpos;
14667 {
14668 int bytepos = CHAR_TO_BYTE (charpos);
14669 int c = 0;
14670
14671 while (bytepos < ZV_BYTE
14672 && (c = FETCH_CHAR (bytepos),
14673 c == ' ' || c == '\t'))
14674 ++bytepos;
14675
14676 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14677 {
14678 if (bytepos != PT_BYTE)
14679 return 1;
14680 }
14681 return 0;
14682 }
14683
14684
14685 /* Highlight trailing whitespace, if any, in ROW. */
14686
14687 void
14688 highlight_trailing_whitespace (f, row)
14689 struct frame *f;
14690 struct glyph_row *row;
14691 {
14692 int used = row->used[TEXT_AREA];
14693
14694 if (used)
14695 {
14696 struct glyph *start = row->glyphs[TEXT_AREA];
14697 struct glyph *glyph = start + used - 1;
14698
14699 /* Skip over glyphs inserted to display the cursor at the
14700 end of a line, for extending the face of the last glyph
14701 to the end of the line on terminals, and for truncation
14702 and continuation glyphs. */
14703 while (glyph >= start
14704 && glyph->type == CHAR_GLYPH
14705 && INTEGERP (glyph->object))
14706 --glyph;
14707
14708 /* If last glyph is a space or stretch, and it's trailing
14709 whitespace, set the face of all trailing whitespace glyphs in
14710 IT->glyph_row to `trailing-whitespace'. */
14711 if (glyph >= start
14712 && BUFFERP (glyph->object)
14713 && (glyph->type == STRETCH_GLYPH
14714 || (glyph->type == CHAR_GLYPH
14715 && glyph->u.ch == ' '))
14716 && trailing_whitespace_p (glyph->charpos))
14717 {
14718 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14719 if (face_id < 0)
14720 return;
14721
14722 while (glyph >= start
14723 && BUFFERP (glyph->object)
14724 && (glyph->type == STRETCH_GLYPH
14725 || (glyph->type == CHAR_GLYPH
14726 && glyph->u.ch == ' ')))
14727 (glyph--)->face_id = face_id;
14728 }
14729 }
14730 }
14731
14732
14733 /* Value is non-zero if glyph row ROW in window W should be
14734 used to hold the cursor. */
14735
14736 static int
14737 cursor_row_p (w, row)
14738 struct window *w;
14739 struct glyph_row *row;
14740 {
14741 int cursor_row_p = 1;
14742
14743 if (PT == MATRIX_ROW_END_CHARPOS (row))
14744 {
14745 /* If the row ends with a newline from a string, we don't want
14746 the cursor there (if the row is continued it doesn't end in a
14747 newline). */
14748 if (CHARPOS (row->end.string_pos) >= 0)
14749 cursor_row_p = row->continued_p;
14750 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14751 {
14752 /* If the row ends in middle of a real character,
14753 and the line is continued, we want the cursor here.
14754 That's because MATRIX_ROW_END_CHARPOS would equal
14755 PT if PT is before the character. */
14756 if (!row->ends_in_ellipsis_p)
14757 cursor_row_p = row->continued_p;
14758 else
14759 /* If the row ends in an ellipsis, then
14760 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14761 We want that position to be displayed after the ellipsis. */
14762 cursor_row_p = 0;
14763 }
14764 /* If the row ends at ZV, display the cursor at the end of that
14765 row instead of at the start of the row below. */
14766 else if (row->ends_at_zv_p)
14767 cursor_row_p = 1;
14768 else
14769 cursor_row_p = 0;
14770 }
14771
14772 return cursor_row_p;
14773 }
14774
14775
14776 /* Construct the glyph row IT->glyph_row in the desired matrix of
14777 IT->w from text at the current position of IT. See dispextern.h
14778 for an overview of struct it. Value is non-zero if
14779 IT->glyph_row displays text, as opposed to a line displaying ZV
14780 only. */
14781
14782 static int
14783 display_line (it)
14784 struct it *it;
14785 {
14786 struct glyph_row *row = it->glyph_row;
14787 int overlay_arrow_bitmap;
14788 Lisp_Object overlay_arrow_string;
14789
14790 /* We always start displaying at hpos zero even if hscrolled. */
14791 xassert (it->hpos == 0 && it->current_x == 0);
14792
14793 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14794 >= it->w->desired_matrix->nrows)
14795 {
14796 it->w->nrows_scale_factor++;
14797 fonts_changed_p = 1;
14798 return 0;
14799 }
14800
14801 /* Is IT->w showing the region? */
14802 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14803
14804 /* Clear the result glyph row and enable it. */
14805 prepare_desired_row (row);
14806
14807 row->y = it->current_y;
14808 row->start = it->start;
14809 row->continuation_lines_width = it->continuation_lines_width;
14810 row->displays_text_p = 1;
14811 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14812 it->starts_in_middle_of_char_p = 0;
14813
14814 /* Arrange the overlays nicely for our purposes. Usually, we call
14815 display_line on only one line at a time, in which case this
14816 can't really hurt too much, or we call it on lines which appear
14817 one after another in the buffer, in which case all calls to
14818 recenter_overlay_lists but the first will be pretty cheap. */
14819 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14820
14821 /* Move over display elements that are not visible because we are
14822 hscrolled. This may stop at an x-position < IT->first_visible_x
14823 if the first glyph is partially visible or if we hit a line end. */
14824 if (it->current_x < it->first_visible_x)
14825 {
14826 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14827 MOVE_TO_POS | MOVE_TO_X);
14828 }
14829
14830 /* Get the initial row height. This is either the height of the
14831 text hscrolled, if there is any, or zero. */
14832 row->ascent = it->max_ascent;
14833 row->height = it->max_ascent + it->max_descent;
14834 row->phys_ascent = it->max_phys_ascent;
14835 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14836 row->extra_line_spacing = it->max_extra_line_spacing;
14837
14838 /* Loop generating characters. The loop is left with IT on the next
14839 character to display. */
14840 while (1)
14841 {
14842 int n_glyphs_before, hpos_before, x_before;
14843 int x, i, nglyphs;
14844 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14845
14846 /* Retrieve the next thing to display. Value is zero if end of
14847 buffer reached. */
14848 if (!get_next_display_element (it))
14849 {
14850 /* Maybe add a space at the end of this line that is used to
14851 display the cursor there under X. Set the charpos of the
14852 first glyph of blank lines not corresponding to any text
14853 to -1. */
14854 #ifdef HAVE_WINDOW_SYSTEM
14855 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14856 row->exact_window_width_line_p = 1;
14857 else
14858 #endif /* HAVE_WINDOW_SYSTEM */
14859 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14860 || row->used[TEXT_AREA] == 0)
14861 {
14862 row->glyphs[TEXT_AREA]->charpos = -1;
14863 row->displays_text_p = 0;
14864
14865 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14866 && (!MINI_WINDOW_P (it->w)
14867 || (minibuf_level && EQ (it->window, minibuf_window))))
14868 row->indicate_empty_line_p = 1;
14869 }
14870
14871 it->continuation_lines_width = 0;
14872 row->ends_at_zv_p = 1;
14873 break;
14874 }
14875
14876 /* Now, get the metrics of what we want to display. This also
14877 generates glyphs in `row' (which is IT->glyph_row). */
14878 n_glyphs_before = row->used[TEXT_AREA];
14879 x = it->current_x;
14880
14881 /* Remember the line height so far in case the next element doesn't
14882 fit on the line. */
14883 if (!it->truncate_lines_p)
14884 {
14885 ascent = it->max_ascent;
14886 descent = it->max_descent;
14887 phys_ascent = it->max_phys_ascent;
14888 phys_descent = it->max_phys_descent;
14889 }
14890
14891 PRODUCE_GLYPHS (it);
14892
14893 /* If this display element was in marginal areas, continue with
14894 the next one. */
14895 if (it->area != TEXT_AREA)
14896 {
14897 row->ascent = max (row->ascent, it->max_ascent);
14898 row->height = max (row->height, it->max_ascent + it->max_descent);
14899 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14900 row->phys_height = max (row->phys_height,
14901 it->max_phys_ascent + it->max_phys_descent);
14902 row->extra_line_spacing = max (row->extra_line_spacing,
14903 it->max_extra_line_spacing);
14904 set_iterator_to_next (it, 1);
14905 continue;
14906 }
14907
14908 /* Does the display element fit on the line? If we truncate
14909 lines, we should draw past the right edge of the window. If
14910 we don't truncate, we want to stop so that we can display the
14911 continuation glyph before the right margin. If lines are
14912 continued, there are two possible strategies for characters
14913 resulting in more than 1 glyph (e.g. tabs): Display as many
14914 glyphs as possible in this line and leave the rest for the
14915 continuation line, or display the whole element in the next
14916 line. Original redisplay did the former, so we do it also. */
14917 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14918 hpos_before = it->hpos;
14919 x_before = x;
14920
14921 if (/* Not a newline. */
14922 nglyphs > 0
14923 /* Glyphs produced fit entirely in the line. */
14924 && it->current_x < it->last_visible_x)
14925 {
14926 it->hpos += nglyphs;
14927 row->ascent = max (row->ascent, it->max_ascent);
14928 row->height = max (row->height, it->max_ascent + it->max_descent);
14929 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14930 row->phys_height = max (row->phys_height,
14931 it->max_phys_ascent + it->max_phys_descent);
14932 row->extra_line_spacing = max (row->extra_line_spacing,
14933 it->max_extra_line_spacing);
14934 if (it->current_x - it->pixel_width < it->first_visible_x)
14935 row->x = x - it->first_visible_x;
14936 }
14937 else
14938 {
14939 int new_x;
14940 struct glyph *glyph;
14941
14942 for (i = 0; i < nglyphs; ++i, x = new_x)
14943 {
14944 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
14945 new_x = x + glyph->pixel_width;
14946
14947 if (/* Lines are continued. */
14948 !it->truncate_lines_p
14949 && (/* Glyph doesn't fit on the line. */
14950 new_x > it->last_visible_x
14951 /* Or it fits exactly on a window system frame. */
14952 || (new_x == it->last_visible_x
14953 && FRAME_WINDOW_P (it->f))))
14954 {
14955 /* End of a continued line. */
14956
14957 if (it->hpos == 0
14958 || (new_x == it->last_visible_x
14959 && FRAME_WINDOW_P (it->f)))
14960 {
14961 /* Current glyph is the only one on the line or
14962 fits exactly on the line. We must continue
14963 the line because we can't draw the cursor
14964 after the glyph. */
14965 row->continued_p = 1;
14966 it->current_x = new_x;
14967 it->continuation_lines_width += new_x;
14968 ++it->hpos;
14969 if (i == nglyphs - 1)
14970 {
14971 set_iterator_to_next (it, 1);
14972 #ifdef HAVE_WINDOW_SYSTEM
14973 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14974 {
14975 if (!get_next_display_element (it))
14976 {
14977 row->exact_window_width_line_p = 1;
14978 it->continuation_lines_width = 0;
14979 row->continued_p = 0;
14980 row->ends_at_zv_p = 1;
14981 }
14982 else if (ITERATOR_AT_END_OF_LINE_P (it))
14983 {
14984 row->continued_p = 0;
14985 row->exact_window_width_line_p = 1;
14986 }
14987 }
14988 #endif /* HAVE_WINDOW_SYSTEM */
14989 }
14990 }
14991 else if (CHAR_GLYPH_PADDING_P (*glyph)
14992 && !FRAME_WINDOW_P (it->f))
14993 {
14994 /* A padding glyph that doesn't fit on this line.
14995 This means the whole character doesn't fit
14996 on the line. */
14997 row->used[TEXT_AREA] = n_glyphs_before;
14998
14999 /* Fill the rest of the row with continuation
15000 glyphs like in 20.x. */
15001 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15002 < row->glyphs[1 + TEXT_AREA])
15003 produce_special_glyphs (it, IT_CONTINUATION);
15004
15005 row->continued_p = 1;
15006 it->current_x = x_before;
15007 it->continuation_lines_width += x_before;
15008
15009 /* Restore the height to what it was before the
15010 element not fitting on the line. */
15011 it->max_ascent = ascent;
15012 it->max_descent = descent;
15013 it->max_phys_ascent = phys_ascent;
15014 it->max_phys_descent = phys_descent;
15015 }
15016 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15017 {
15018 /* A TAB that extends past the right edge of the
15019 window. This produces a single glyph on
15020 window system frames. We leave the glyph in
15021 this row and let it fill the row, but don't
15022 consume the TAB. */
15023 it->continuation_lines_width += it->last_visible_x;
15024 row->ends_in_middle_of_char_p = 1;
15025 row->continued_p = 1;
15026 glyph->pixel_width = it->last_visible_x - x;
15027 it->starts_in_middle_of_char_p = 1;
15028 }
15029 else
15030 {
15031 /* Something other than a TAB that draws past
15032 the right edge of the window. Restore
15033 positions to values before the element. */
15034 row->used[TEXT_AREA] = n_glyphs_before + i;
15035
15036 /* Display continuation glyphs. */
15037 if (!FRAME_WINDOW_P (it->f))
15038 produce_special_glyphs (it, IT_CONTINUATION);
15039 row->continued_p = 1;
15040
15041 it->continuation_lines_width += x;
15042
15043 if (nglyphs > 1 && i > 0)
15044 {
15045 row->ends_in_middle_of_char_p = 1;
15046 it->starts_in_middle_of_char_p = 1;
15047 }
15048
15049 /* Restore the height to what it was before the
15050 element not fitting on the line. */
15051 it->max_ascent = ascent;
15052 it->max_descent = descent;
15053 it->max_phys_ascent = phys_ascent;
15054 it->max_phys_descent = phys_descent;
15055 }
15056
15057 break;
15058 }
15059 else if (new_x > it->first_visible_x)
15060 {
15061 /* Increment number of glyphs actually displayed. */
15062 ++it->hpos;
15063
15064 if (x < it->first_visible_x)
15065 /* Glyph is partially visible, i.e. row starts at
15066 negative X position. */
15067 row->x = x - it->first_visible_x;
15068 }
15069 else
15070 {
15071 /* Glyph is completely off the left margin of the
15072 window. This should not happen because of the
15073 move_it_in_display_line at the start of this
15074 function, unless the text display area of the
15075 window is empty. */
15076 xassert (it->first_visible_x <= it->last_visible_x);
15077 }
15078 }
15079
15080 row->ascent = max (row->ascent, it->max_ascent);
15081 row->height = max (row->height, it->max_ascent + it->max_descent);
15082 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15083 row->phys_height = max (row->phys_height,
15084 it->max_phys_ascent + it->max_phys_descent);
15085 row->extra_line_spacing = max (row->extra_line_spacing,
15086 it->max_extra_line_spacing);
15087
15088 /* End of this display line if row is continued. */
15089 if (row->continued_p || row->ends_at_zv_p)
15090 break;
15091 }
15092
15093 at_end_of_line:
15094 /* Is this a line end? If yes, we're also done, after making
15095 sure that a non-default face is extended up to the right
15096 margin of the window. */
15097 if (ITERATOR_AT_END_OF_LINE_P (it))
15098 {
15099 int used_before = row->used[TEXT_AREA];
15100
15101 row->ends_in_newline_from_string_p = STRINGP (it->object);
15102
15103 #ifdef HAVE_WINDOW_SYSTEM
15104 /* Add a space at the end of the line that is used to
15105 display the cursor there. */
15106 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15107 append_space_for_newline (it, 0);
15108 #endif /* HAVE_WINDOW_SYSTEM */
15109
15110 /* Extend the face to the end of the line. */
15111 extend_face_to_end_of_line (it);
15112
15113 /* Make sure we have the position. */
15114 if (used_before == 0)
15115 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15116
15117 /* Consume the line end. This skips over invisible lines. */
15118 set_iterator_to_next (it, 1);
15119 it->continuation_lines_width = 0;
15120 break;
15121 }
15122
15123 /* Proceed with next display element. Note that this skips
15124 over lines invisible because of selective display. */
15125 set_iterator_to_next (it, 1);
15126
15127 /* If we truncate lines, we are done when the last displayed
15128 glyphs reach past the right margin of the window. */
15129 if (it->truncate_lines_p
15130 && (FRAME_WINDOW_P (it->f)
15131 ? (it->current_x >= it->last_visible_x)
15132 : (it->current_x > it->last_visible_x)))
15133 {
15134 /* Maybe add truncation glyphs. */
15135 if (!FRAME_WINDOW_P (it->f))
15136 {
15137 int i, n;
15138
15139 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15140 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15141 break;
15142
15143 for (n = row->used[TEXT_AREA]; i < n; ++i)
15144 {
15145 row->used[TEXT_AREA] = i;
15146 produce_special_glyphs (it, IT_TRUNCATION);
15147 }
15148 }
15149 #ifdef HAVE_WINDOW_SYSTEM
15150 else
15151 {
15152 /* Don't truncate if we can overflow newline into fringe. */
15153 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15154 {
15155 if (!get_next_display_element (it))
15156 {
15157 it->continuation_lines_width = 0;
15158 row->ends_at_zv_p = 1;
15159 row->exact_window_width_line_p = 1;
15160 break;
15161 }
15162 if (ITERATOR_AT_END_OF_LINE_P (it))
15163 {
15164 row->exact_window_width_line_p = 1;
15165 goto at_end_of_line;
15166 }
15167 }
15168 }
15169 #endif /* HAVE_WINDOW_SYSTEM */
15170
15171 row->truncated_on_right_p = 1;
15172 it->continuation_lines_width = 0;
15173 reseat_at_next_visible_line_start (it, 0);
15174 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15175 it->hpos = hpos_before;
15176 it->current_x = x_before;
15177 break;
15178 }
15179 }
15180
15181 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15182 at the left window margin. */
15183 if (it->first_visible_x
15184 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15185 {
15186 if (!FRAME_WINDOW_P (it->f))
15187 insert_left_trunc_glyphs (it);
15188 row->truncated_on_left_p = 1;
15189 }
15190
15191 /* If the start of this line is the overlay arrow-position, then
15192 mark this glyph row as the one containing the overlay arrow.
15193 This is clearly a mess with variable size fonts. It would be
15194 better to let it be displayed like cursors under X. */
15195 if (! overlay_arrow_seen
15196 && (overlay_arrow_string
15197 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15198 !NILP (overlay_arrow_string)))
15199 {
15200 /* Overlay arrow in window redisplay is a fringe bitmap. */
15201 if (STRINGP (overlay_arrow_string))
15202 {
15203 struct glyph_row *arrow_row
15204 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15205 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15206 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15207 struct glyph *p = row->glyphs[TEXT_AREA];
15208 struct glyph *p2, *end;
15209
15210 /* Copy the arrow glyphs. */
15211 while (glyph < arrow_end)
15212 *p++ = *glyph++;
15213
15214 /* Throw away padding glyphs. */
15215 p2 = p;
15216 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15217 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15218 ++p2;
15219 if (p2 > p)
15220 {
15221 while (p2 < end)
15222 *p++ = *p2++;
15223 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15224 }
15225 }
15226 else
15227 {
15228 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15229 row->overlay_arrow_p = 1;
15230 }
15231 overlay_arrow_seen = 1;
15232 }
15233
15234 /* Compute pixel dimensions of this line. */
15235 compute_line_metrics (it);
15236
15237 /* Remember the position at which this line ends. */
15238 row->end = it->current;
15239
15240 /* Record whether this row ends inside an ellipsis. */
15241 row->ends_in_ellipsis_p
15242 = (it->method == next_element_from_display_vector
15243 && it->ellipsis_p);
15244
15245 /* Save fringe bitmaps in this row. */
15246 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15247 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15248 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15249 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15250
15251 it->left_user_fringe_bitmap = 0;
15252 it->left_user_fringe_face_id = 0;
15253 it->right_user_fringe_bitmap = 0;
15254 it->right_user_fringe_face_id = 0;
15255
15256 /* Maybe set the cursor. */
15257 if (it->w->cursor.vpos < 0
15258 && PT >= MATRIX_ROW_START_CHARPOS (row)
15259 && PT <= MATRIX_ROW_END_CHARPOS (row)
15260 && cursor_row_p (it->w, row))
15261 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15262
15263 /* Highlight trailing whitespace. */
15264 if (!NILP (Vshow_trailing_whitespace))
15265 highlight_trailing_whitespace (it->f, it->glyph_row);
15266
15267 /* Prepare for the next line. This line starts horizontally at (X
15268 HPOS) = (0 0). Vertical positions are incremented. As a
15269 convenience for the caller, IT->glyph_row is set to the next
15270 row to be used. */
15271 it->current_x = it->hpos = 0;
15272 it->current_y += row->height;
15273 ++it->vpos;
15274 ++it->glyph_row;
15275 it->start = it->current;
15276 return row->displays_text_p;
15277 }
15278
15279
15280 \f
15281 /***********************************************************************
15282 Menu Bar
15283 ***********************************************************************/
15284
15285 /* Redisplay the menu bar in the frame for window W.
15286
15287 The menu bar of X frames that don't have X toolkit support is
15288 displayed in a special window W->frame->menu_bar_window.
15289
15290 The menu bar of terminal frames is treated specially as far as
15291 glyph matrices are concerned. Menu bar lines are not part of
15292 windows, so the update is done directly on the frame matrix rows
15293 for the menu bar. */
15294
15295 static void
15296 display_menu_bar (w)
15297 struct window *w;
15298 {
15299 struct frame *f = XFRAME (WINDOW_FRAME (w));
15300 struct it it;
15301 Lisp_Object items;
15302 int i;
15303
15304 /* Don't do all this for graphical frames. */
15305 #ifdef HAVE_NTGUI
15306 if (!NILP (Vwindow_system))
15307 return;
15308 #endif
15309 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15310 if (FRAME_X_P (f))
15311 return;
15312 #endif
15313 #ifdef MAC_OS
15314 if (FRAME_MAC_P (f))
15315 return;
15316 #endif
15317
15318 #ifdef USE_X_TOOLKIT
15319 xassert (!FRAME_WINDOW_P (f));
15320 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15321 it.first_visible_x = 0;
15322 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15323 #else /* not USE_X_TOOLKIT */
15324 if (FRAME_WINDOW_P (f))
15325 {
15326 /* Menu bar lines are displayed in the desired matrix of the
15327 dummy window menu_bar_window. */
15328 struct window *menu_w;
15329 xassert (WINDOWP (f->menu_bar_window));
15330 menu_w = XWINDOW (f->menu_bar_window);
15331 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15332 MENU_FACE_ID);
15333 it.first_visible_x = 0;
15334 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15335 }
15336 else
15337 {
15338 /* This is a TTY frame, i.e. character hpos/vpos are used as
15339 pixel x/y. */
15340 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15341 MENU_FACE_ID);
15342 it.first_visible_x = 0;
15343 it.last_visible_x = FRAME_COLS (f);
15344 }
15345 #endif /* not USE_X_TOOLKIT */
15346
15347 if (! mode_line_inverse_video)
15348 /* Force the menu-bar to be displayed in the default face. */
15349 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15350
15351 /* Clear all rows of the menu bar. */
15352 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15353 {
15354 struct glyph_row *row = it.glyph_row + i;
15355 clear_glyph_row (row);
15356 row->enabled_p = 1;
15357 row->full_width_p = 1;
15358 }
15359
15360 /* Display all items of the menu bar. */
15361 items = FRAME_MENU_BAR_ITEMS (it.f);
15362 for (i = 0; i < XVECTOR (items)->size; i += 4)
15363 {
15364 Lisp_Object string;
15365
15366 /* Stop at nil string. */
15367 string = AREF (items, i + 1);
15368 if (NILP (string))
15369 break;
15370
15371 /* Remember where item was displayed. */
15372 AREF (items, i + 3) = make_number (it.hpos);
15373
15374 /* Display the item, pad with one space. */
15375 if (it.current_x < it.last_visible_x)
15376 display_string (NULL, string, Qnil, 0, 0, &it,
15377 SCHARS (string) + 1, 0, 0, -1);
15378 }
15379
15380 /* Fill out the line with spaces. */
15381 if (it.current_x < it.last_visible_x)
15382 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15383
15384 /* Compute the total height of the lines. */
15385 compute_line_metrics (&it);
15386 }
15387
15388
15389 \f
15390 /***********************************************************************
15391 Mode Line
15392 ***********************************************************************/
15393
15394 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15395 FORCE is non-zero, redisplay mode lines unconditionally.
15396 Otherwise, redisplay only mode lines that are garbaged. Value is
15397 the number of windows whose mode lines were redisplayed. */
15398
15399 static int
15400 redisplay_mode_lines (window, force)
15401 Lisp_Object window;
15402 int force;
15403 {
15404 int nwindows = 0;
15405
15406 while (!NILP (window))
15407 {
15408 struct window *w = XWINDOW (window);
15409
15410 if (WINDOWP (w->hchild))
15411 nwindows += redisplay_mode_lines (w->hchild, force);
15412 else if (WINDOWP (w->vchild))
15413 nwindows += redisplay_mode_lines (w->vchild, force);
15414 else if (force
15415 || FRAME_GARBAGED_P (XFRAME (w->frame))
15416 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15417 {
15418 struct text_pos lpoint;
15419 struct buffer *old = current_buffer;
15420
15421 /* Set the window's buffer for the mode line display. */
15422 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15423 set_buffer_internal_1 (XBUFFER (w->buffer));
15424
15425 /* Point refers normally to the selected window. For any
15426 other window, set up appropriate value. */
15427 if (!EQ (window, selected_window))
15428 {
15429 struct text_pos pt;
15430
15431 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15432 if (CHARPOS (pt) < BEGV)
15433 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15434 else if (CHARPOS (pt) > (ZV - 1))
15435 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15436 else
15437 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15438 }
15439
15440 /* Display mode lines. */
15441 clear_glyph_matrix (w->desired_matrix);
15442 if (display_mode_lines (w))
15443 {
15444 ++nwindows;
15445 w->must_be_updated_p = 1;
15446 }
15447
15448 /* Restore old settings. */
15449 set_buffer_internal_1 (old);
15450 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15451 }
15452
15453 window = w->next;
15454 }
15455
15456 return nwindows;
15457 }
15458
15459
15460 /* Display the mode and/or top line of window W. Value is the number
15461 of mode lines displayed. */
15462
15463 static int
15464 display_mode_lines (w)
15465 struct window *w;
15466 {
15467 Lisp_Object old_selected_window, old_selected_frame;
15468 int n = 0;
15469
15470 old_selected_frame = selected_frame;
15471 selected_frame = w->frame;
15472 old_selected_window = selected_window;
15473 XSETWINDOW (selected_window, w);
15474
15475 /* These will be set while the mode line specs are processed. */
15476 line_number_displayed = 0;
15477 w->column_number_displayed = Qnil;
15478
15479 if (WINDOW_WANTS_MODELINE_P (w))
15480 {
15481 struct window *sel_w = XWINDOW (old_selected_window);
15482
15483 /* Select mode line face based on the real selected window. */
15484 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15485 current_buffer->mode_line_format);
15486 ++n;
15487 }
15488
15489 if (WINDOW_WANTS_HEADER_LINE_P (w))
15490 {
15491 display_mode_line (w, HEADER_LINE_FACE_ID,
15492 current_buffer->header_line_format);
15493 ++n;
15494 }
15495
15496 selected_frame = old_selected_frame;
15497 selected_window = old_selected_window;
15498 return n;
15499 }
15500
15501
15502 /* Display mode or top line of window W. FACE_ID specifies which line
15503 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15504 FORMAT is the mode line format to display. Value is the pixel
15505 height of the mode line displayed. */
15506
15507 static int
15508 display_mode_line (w, face_id, format)
15509 struct window *w;
15510 enum face_id face_id;
15511 Lisp_Object format;
15512 {
15513 struct it it;
15514 struct face *face;
15515
15516 init_iterator (&it, w, -1, -1, NULL, face_id);
15517 prepare_desired_row (it.glyph_row);
15518
15519 it.glyph_row->mode_line_p = 1;
15520
15521 if (! mode_line_inverse_video)
15522 /* Force the mode-line to be displayed in the default face. */
15523 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15524
15525 /* Temporarily make frame's keyboard the current kboard so that
15526 kboard-local variables in the mode_line_format will get the right
15527 values. */
15528 push_frame_kboard (it.f);
15529 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15530 pop_frame_kboard ();
15531
15532 /* Fill up with spaces. */
15533 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15534
15535 compute_line_metrics (&it);
15536 it.glyph_row->full_width_p = 1;
15537 it.glyph_row->continued_p = 0;
15538 it.glyph_row->truncated_on_left_p = 0;
15539 it.glyph_row->truncated_on_right_p = 0;
15540
15541 /* Make a 3D mode-line have a shadow at its right end. */
15542 face = FACE_FROM_ID (it.f, face_id);
15543 extend_face_to_end_of_line (&it);
15544 if (face->box != FACE_NO_BOX)
15545 {
15546 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15547 + it.glyph_row->used[TEXT_AREA] - 1);
15548 last->right_box_line_p = 1;
15549 }
15550
15551 return it.glyph_row->height;
15552 }
15553
15554 /* Alist that caches the results of :propertize.
15555 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15556 Lisp_Object mode_line_proptrans_alist;
15557
15558 /* List of strings making up the mode-line. */
15559 Lisp_Object mode_line_string_list;
15560
15561 /* Base face property when building propertized mode line string. */
15562 static Lisp_Object mode_line_string_face;
15563 static Lisp_Object mode_line_string_face_prop;
15564
15565
15566 /* Contribute ELT to the mode line for window IT->w. How it
15567 translates into text depends on its data type.
15568
15569 IT describes the display environment in which we display, as usual.
15570
15571 DEPTH is the depth in recursion. It is used to prevent
15572 infinite recursion here.
15573
15574 FIELD_WIDTH is the number of characters the display of ELT should
15575 occupy in the mode line, and PRECISION is the maximum number of
15576 characters to display from ELT's representation. See
15577 display_string for details.
15578
15579 Returns the hpos of the end of the text generated by ELT.
15580
15581 PROPS is a property list to add to any string we encounter.
15582
15583 If RISKY is nonzero, remove (disregard) any properties in any string
15584 we encounter, and ignore :eval and :propertize.
15585
15586 If the global variable `frame_title_ptr' is non-NULL, then the output
15587 is passed to `store_frame_title' instead of `display_string'. */
15588
15589 static int
15590 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15591 struct it *it;
15592 int depth;
15593 int field_width, precision;
15594 Lisp_Object elt, props;
15595 int risky;
15596 {
15597 int n = 0, field, prec;
15598 int literal = 0;
15599
15600 tail_recurse:
15601 if (depth > 100)
15602 elt = build_string ("*too-deep*");
15603
15604 depth++;
15605
15606 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15607 {
15608 case Lisp_String:
15609 {
15610 /* A string: output it and check for %-constructs within it. */
15611 unsigned char c;
15612 const unsigned char *this, *lisp_string;
15613
15614 if (!NILP (props) || risky)
15615 {
15616 Lisp_Object oprops, aelt;
15617 oprops = Ftext_properties_at (make_number (0), elt);
15618
15619 /* If the starting string's properties are not what
15620 we want, translate the string. Also, if the string
15621 is risky, do that anyway. */
15622
15623 if (NILP (Fequal (props, oprops)) || risky)
15624 {
15625 /* If the starting string has properties,
15626 merge the specified ones onto the existing ones. */
15627 if (! NILP (oprops) && !risky)
15628 {
15629 Lisp_Object tem;
15630
15631 oprops = Fcopy_sequence (oprops);
15632 tem = props;
15633 while (CONSP (tem))
15634 {
15635 oprops = Fplist_put (oprops, XCAR (tem),
15636 XCAR (XCDR (tem)));
15637 tem = XCDR (XCDR (tem));
15638 }
15639 props = oprops;
15640 }
15641
15642 aelt = Fassoc (elt, mode_line_proptrans_alist);
15643 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15644 {
15645 mode_line_proptrans_alist
15646 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15647 elt = XCAR (aelt);
15648 }
15649 else
15650 {
15651 Lisp_Object tem;
15652
15653 elt = Fcopy_sequence (elt);
15654 Fset_text_properties (make_number (0), Flength (elt),
15655 props, elt);
15656 /* Add this item to mode_line_proptrans_alist. */
15657 mode_line_proptrans_alist
15658 = Fcons (Fcons (elt, props),
15659 mode_line_proptrans_alist);
15660 /* Truncate mode_line_proptrans_alist
15661 to at most 50 elements. */
15662 tem = Fnthcdr (make_number (50),
15663 mode_line_proptrans_alist);
15664 if (! NILP (tem))
15665 XSETCDR (tem, Qnil);
15666 }
15667 }
15668 }
15669
15670 this = SDATA (elt);
15671 lisp_string = this;
15672
15673 if (literal)
15674 {
15675 prec = precision - n;
15676 if (frame_title_ptr)
15677 n += store_frame_title (SDATA (elt), -1, prec);
15678 else if (!NILP (mode_line_string_list))
15679 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15680 else
15681 n += display_string (NULL, elt, Qnil, 0, 0, it,
15682 0, prec, 0, STRING_MULTIBYTE (elt));
15683
15684 break;
15685 }
15686
15687 while ((precision <= 0 || n < precision)
15688 && *this
15689 && (frame_title_ptr
15690 || !NILP (mode_line_string_list)
15691 || it->current_x < it->last_visible_x))
15692 {
15693 const unsigned char *last = this;
15694
15695 /* Advance to end of string or next format specifier. */
15696 while ((c = *this++) != '\0' && c != '%')
15697 ;
15698
15699 if (this - 1 != last)
15700 {
15701 int nchars, nbytes;
15702
15703 /* Output to end of string or up to '%'. Field width
15704 is length of string. Don't output more than
15705 PRECISION allows us. */
15706 --this;
15707
15708 prec = c_string_width (last, this - last, precision - n,
15709 &nchars, &nbytes);
15710
15711 if (frame_title_ptr)
15712 n += store_frame_title (last, 0, prec);
15713 else if (!NILP (mode_line_string_list))
15714 {
15715 int bytepos = last - lisp_string;
15716 int charpos = string_byte_to_char (elt, bytepos);
15717 int endpos = (precision <= 0
15718 ? string_byte_to_char (elt,
15719 this - lisp_string)
15720 : charpos + nchars);
15721
15722 n += store_mode_line_string (NULL,
15723 Fsubstring (elt, make_number (charpos),
15724 make_number (endpos)),
15725 0, 0, 0, Qnil);
15726 }
15727 else
15728 {
15729 int bytepos = last - lisp_string;
15730 int charpos = string_byte_to_char (elt, bytepos);
15731 n += display_string (NULL, elt, Qnil, 0, charpos,
15732 it, 0, prec, 0,
15733 STRING_MULTIBYTE (elt));
15734 }
15735 }
15736 else /* c == '%' */
15737 {
15738 const unsigned char *percent_position = this;
15739
15740 /* Get the specified minimum width. Zero means
15741 don't pad. */
15742 field = 0;
15743 while ((c = *this++) >= '0' && c <= '9')
15744 field = field * 10 + c - '0';
15745
15746 /* Don't pad beyond the total padding allowed. */
15747 if (field_width - n > 0 && field > field_width - n)
15748 field = field_width - n;
15749
15750 /* Note that either PRECISION <= 0 or N < PRECISION. */
15751 prec = precision - n;
15752
15753 if (c == 'M')
15754 n += display_mode_element (it, depth, field, prec,
15755 Vglobal_mode_string, props,
15756 risky);
15757 else if (c != 0)
15758 {
15759 int multibyte;
15760 int bytepos, charpos;
15761 unsigned char *spec;
15762
15763 bytepos = percent_position - lisp_string;
15764 charpos = (STRING_MULTIBYTE (elt)
15765 ? string_byte_to_char (elt, bytepos)
15766 : bytepos);
15767
15768 spec
15769 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15770
15771 if (frame_title_ptr)
15772 n += store_frame_title (spec, field, prec);
15773 else if (!NILP (mode_line_string_list))
15774 {
15775 int len = strlen (spec);
15776 Lisp_Object tem = make_string (spec, len);
15777 props = Ftext_properties_at (make_number (charpos), elt);
15778 /* Should only keep face property in props */
15779 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15780 }
15781 else
15782 {
15783 int nglyphs_before, nwritten;
15784
15785 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15786 nwritten = display_string (spec, Qnil, elt,
15787 charpos, 0, it,
15788 field, prec, 0,
15789 multibyte);
15790
15791 /* Assign to the glyphs written above the
15792 string where the `%x' came from, position
15793 of the `%'. */
15794 if (nwritten > 0)
15795 {
15796 struct glyph *glyph
15797 = (it->glyph_row->glyphs[TEXT_AREA]
15798 + nglyphs_before);
15799 int i;
15800
15801 for (i = 0; i < nwritten; ++i)
15802 {
15803 glyph[i].object = elt;
15804 glyph[i].charpos = charpos;
15805 }
15806
15807 n += nwritten;
15808 }
15809 }
15810 }
15811 else /* c == 0 */
15812 break;
15813 }
15814 }
15815 }
15816 break;
15817
15818 case Lisp_Symbol:
15819 /* A symbol: process the value of the symbol recursively
15820 as if it appeared here directly. Avoid error if symbol void.
15821 Special case: if value of symbol is a string, output the string
15822 literally. */
15823 {
15824 register Lisp_Object tem;
15825
15826 /* If the variable is not marked as risky to set
15827 then its contents are risky to use. */
15828 if (NILP (Fget (elt, Qrisky_local_variable)))
15829 risky = 1;
15830
15831 tem = Fboundp (elt);
15832 if (!NILP (tem))
15833 {
15834 tem = Fsymbol_value (elt);
15835 /* If value is a string, output that string literally:
15836 don't check for % within it. */
15837 if (STRINGP (tem))
15838 literal = 1;
15839
15840 if (!EQ (tem, elt))
15841 {
15842 /* Give up right away for nil or t. */
15843 elt = tem;
15844 goto tail_recurse;
15845 }
15846 }
15847 }
15848 break;
15849
15850 case Lisp_Cons:
15851 {
15852 register Lisp_Object car, tem;
15853
15854 /* A cons cell: five distinct cases.
15855 If first element is :eval or :propertize, do something special.
15856 If first element is a string or a cons, process all the elements
15857 and effectively concatenate them.
15858 If first element is a negative number, truncate displaying cdr to
15859 at most that many characters. If positive, pad (with spaces)
15860 to at least that many characters.
15861 If first element is a symbol, process the cadr or caddr recursively
15862 according to whether the symbol's value is non-nil or nil. */
15863 car = XCAR (elt);
15864 if (EQ (car, QCeval))
15865 {
15866 /* An element of the form (:eval FORM) means evaluate FORM
15867 and use the result as mode line elements. */
15868
15869 if (risky)
15870 break;
15871
15872 if (CONSP (XCDR (elt)))
15873 {
15874 Lisp_Object spec;
15875 spec = safe_eval (XCAR (XCDR (elt)));
15876 n += display_mode_element (it, depth, field_width - n,
15877 precision - n, spec, props,
15878 risky);
15879 }
15880 }
15881 else if (EQ (car, QCpropertize))
15882 {
15883 /* An element of the form (:propertize ELT PROPS...)
15884 means display ELT but applying properties PROPS. */
15885
15886 if (risky)
15887 break;
15888
15889 if (CONSP (XCDR (elt)))
15890 n += display_mode_element (it, depth, field_width - n,
15891 precision - n, XCAR (XCDR (elt)),
15892 XCDR (XCDR (elt)), risky);
15893 }
15894 else if (SYMBOLP (car))
15895 {
15896 tem = Fboundp (car);
15897 elt = XCDR (elt);
15898 if (!CONSP (elt))
15899 goto invalid;
15900 /* elt is now the cdr, and we know it is a cons cell.
15901 Use its car if CAR has a non-nil value. */
15902 if (!NILP (tem))
15903 {
15904 tem = Fsymbol_value (car);
15905 if (!NILP (tem))
15906 {
15907 elt = XCAR (elt);
15908 goto tail_recurse;
15909 }
15910 }
15911 /* Symbol's value is nil (or symbol is unbound)
15912 Get the cddr of the original list
15913 and if possible find the caddr and use that. */
15914 elt = XCDR (elt);
15915 if (NILP (elt))
15916 break;
15917 else if (!CONSP (elt))
15918 goto invalid;
15919 elt = XCAR (elt);
15920 goto tail_recurse;
15921 }
15922 else if (INTEGERP (car))
15923 {
15924 register int lim = XINT (car);
15925 elt = XCDR (elt);
15926 if (lim < 0)
15927 {
15928 /* Negative int means reduce maximum width. */
15929 if (precision <= 0)
15930 precision = -lim;
15931 else
15932 precision = min (precision, -lim);
15933 }
15934 else if (lim > 0)
15935 {
15936 /* Padding specified. Don't let it be more than
15937 current maximum. */
15938 if (precision > 0)
15939 lim = min (precision, lim);
15940
15941 /* If that's more padding than already wanted, queue it.
15942 But don't reduce padding already specified even if
15943 that is beyond the current truncation point. */
15944 field_width = max (lim, field_width);
15945 }
15946 goto tail_recurse;
15947 }
15948 else if (STRINGP (car) || CONSP (car))
15949 {
15950 register int limit = 50;
15951 /* Limit is to protect against circular lists. */
15952 while (CONSP (elt)
15953 && --limit > 0
15954 && (precision <= 0 || n < precision))
15955 {
15956 n += display_mode_element (it, depth, field_width - n,
15957 precision - n, XCAR (elt),
15958 props, risky);
15959 elt = XCDR (elt);
15960 }
15961 }
15962 }
15963 break;
15964
15965 default:
15966 invalid:
15967 elt = build_string ("*invalid*");
15968 goto tail_recurse;
15969 }
15970
15971 /* Pad to FIELD_WIDTH. */
15972 if (field_width > 0 && n < field_width)
15973 {
15974 if (frame_title_ptr)
15975 n += store_frame_title ("", field_width - n, 0);
15976 else if (!NILP (mode_line_string_list))
15977 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
15978 else
15979 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
15980 0, 0, 0);
15981 }
15982
15983 return n;
15984 }
15985
15986 /* Store a mode-line string element in mode_line_string_list.
15987
15988 If STRING is non-null, display that C string. Otherwise, the Lisp
15989 string LISP_STRING is displayed.
15990
15991 FIELD_WIDTH is the minimum number of output glyphs to produce.
15992 If STRING has fewer characters than FIELD_WIDTH, pad to the right
15993 with spaces. FIELD_WIDTH <= 0 means don't pad.
15994
15995 PRECISION is the maximum number of characters to output from
15996 STRING. PRECISION <= 0 means don't truncate the string.
15997
15998 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
15999 properties to the string.
16000
16001 PROPS are the properties to add to the string.
16002 The mode_line_string_face face property is always added to the string.
16003 */
16004
16005 static int
16006 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16007 char *string;
16008 Lisp_Object lisp_string;
16009 int copy_string;
16010 int field_width;
16011 int precision;
16012 Lisp_Object props;
16013 {
16014 int len;
16015 int n = 0;
16016
16017 if (string != NULL)
16018 {
16019 len = strlen (string);
16020 if (precision > 0 && len > precision)
16021 len = precision;
16022 lisp_string = make_string (string, len);
16023 if (NILP (props))
16024 props = mode_line_string_face_prop;
16025 else if (!NILP (mode_line_string_face))
16026 {
16027 Lisp_Object face = Fsafe_plist_get (props, Qface);
16028 props = Fcopy_sequence (props);
16029 if (NILP (face))
16030 face = mode_line_string_face;
16031 else
16032 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16033 props = Fplist_put (props, Qface, face);
16034 }
16035 Fadd_text_properties (make_number (0), make_number (len),
16036 props, lisp_string);
16037 }
16038 else
16039 {
16040 len = XFASTINT (Flength (lisp_string));
16041 if (precision > 0 && len > precision)
16042 {
16043 len = precision;
16044 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16045 precision = -1;
16046 }
16047 if (!NILP (mode_line_string_face))
16048 {
16049 Lisp_Object face;
16050 if (NILP (props))
16051 props = Ftext_properties_at (make_number (0), lisp_string);
16052 face = Fsafe_plist_get (props, Qface);
16053 if (NILP (face))
16054 face = mode_line_string_face;
16055 else
16056 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16057 props = Fcons (Qface, Fcons (face, Qnil));
16058 if (copy_string)
16059 lisp_string = Fcopy_sequence (lisp_string);
16060 }
16061 if (!NILP (props))
16062 Fadd_text_properties (make_number (0), make_number (len),
16063 props, lisp_string);
16064 }
16065
16066 if (len > 0)
16067 {
16068 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16069 n += len;
16070 }
16071
16072 if (field_width > len)
16073 {
16074 field_width -= len;
16075 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16076 if (!NILP (props))
16077 Fadd_text_properties (make_number (0), make_number (field_width),
16078 props, lisp_string);
16079 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16080 n += field_width;
16081 }
16082
16083 return n;
16084 }
16085
16086
16087 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16088 1, 4, 0,
16089 doc: /* Format a string out of a mode line format specification.
16090 First arg FORMAT specifies the mode line format (see `mode-line-format'
16091 for details) to use.
16092
16093 Optional second arg FACE specifies the face property to put
16094 on all characters for which no face is specified.
16095 t means whatever face the window's mode line currently uses
16096 \(either `mode-line' or `mode-line-inactive', depending).
16097 nil means the default is no face property.
16098 If FACE is an integer, the value string has no text properties.
16099
16100 Optional third and fourth args WINDOW and BUFFER specify the window
16101 and buffer to use as the context for the formatting (defaults
16102 are the selected window and the window's buffer). */)
16103 (format, face, window, buffer)
16104 Lisp_Object format, face, window, buffer;
16105 {
16106 struct it it;
16107 int len;
16108 struct window *w;
16109 struct buffer *old_buffer = NULL;
16110 int face_id = -1;
16111 int no_props = INTEGERP (face);
16112
16113 if (NILP (window))
16114 window = selected_window;
16115 CHECK_WINDOW (window);
16116 w = XWINDOW (window);
16117
16118 if (NILP (buffer))
16119 buffer = w->buffer;
16120 CHECK_BUFFER (buffer);
16121
16122 if (NILP (format))
16123 return build_string ("");
16124
16125 if (no_props)
16126 face = Qnil;
16127
16128 if (!NILP (face))
16129 {
16130 if (EQ (face, Qt))
16131 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16132 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16133 }
16134
16135 if (face_id < 0)
16136 face_id = DEFAULT_FACE_ID;
16137
16138 if (XBUFFER (buffer) != current_buffer)
16139 {
16140 old_buffer = current_buffer;
16141 set_buffer_internal_1 (XBUFFER (buffer));
16142 }
16143
16144 init_iterator (&it, w, -1, -1, NULL, face_id);
16145
16146 if (!no_props)
16147 {
16148 mode_line_string_face = face;
16149 mode_line_string_face_prop
16150 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16151
16152 /* We need a dummy last element in mode_line_string_list to
16153 indicate we are building the propertized mode-line string.
16154 Using mode_line_string_face_prop here GC protects it. */
16155 mode_line_string_list
16156 = Fcons (mode_line_string_face_prop, Qnil);
16157 frame_title_ptr = NULL;
16158 }
16159 else
16160 {
16161 mode_line_string_face_prop = Qnil;
16162 mode_line_string_list = Qnil;
16163 frame_title_ptr = frame_title_buf;
16164 }
16165
16166 push_frame_kboard (it.f);
16167 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16168 pop_frame_kboard ();
16169
16170 if (old_buffer)
16171 set_buffer_internal_1 (old_buffer);
16172
16173 if (!no_props)
16174 {
16175 Lisp_Object str;
16176 mode_line_string_list = Fnreverse (mode_line_string_list);
16177 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16178 make_string ("", 0));
16179 mode_line_string_face_prop = Qnil;
16180 mode_line_string_list = Qnil;
16181 return str;
16182 }
16183
16184 len = frame_title_ptr - frame_title_buf;
16185 if (len > 0 && frame_title_ptr[-1] == '-')
16186 {
16187 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16188 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16189 ;
16190 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16191 if (len > frame_title_ptr - frame_title_buf)
16192 len = frame_title_ptr - frame_title_buf;
16193 }
16194
16195 frame_title_ptr = NULL;
16196 return make_string (frame_title_buf, len);
16197 }
16198
16199 /* Write a null-terminated, right justified decimal representation of
16200 the positive integer D to BUF using a minimal field width WIDTH. */
16201
16202 static void
16203 pint2str (buf, width, d)
16204 register char *buf;
16205 register int width;
16206 register int d;
16207 {
16208 register char *p = buf;
16209
16210 if (d <= 0)
16211 *p++ = '0';
16212 else
16213 {
16214 while (d > 0)
16215 {
16216 *p++ = d % 10 + '0';
16217 d /= 10;
16218 }
16219 }
16220
16221 for (width -= (int) (p - buf); width > 0; --width)
16222 *p++ = ' ';
16223 *p-- = '\0';
16224 while (p > buf)
16225 {
16226 d = *buf;
16227 *buf++ = *p;
16228 *p-- = d;
16229 }
16230 }
16231
16232 /* Write a null-terminated, right justified decimal and "human
16233 readable" representation of the nonnegative integer D to BUF using
16234 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16235
16236 static const char power_letter[] =
16237 {
16238 0, /* not used */
16239 'k', /* kilo */
16240 'M', /* mega */
16241 'G', /* giga */
16242 'T', /* tera */
16243 'P', /* peta */
16244 'E', /* exa */
16245 'Z', /* zetta */
16246 'Y' /* yotta */
16247 };
16248
16249 static void
16250 pint2hrstr (buf, width, d)
16251 char *buf;
16252 int width;
16253 int d;
16254 {
16255 /* We aim to represent the nonnegative integer D as
16256 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16257 int quotient = d;
16258 int remainder = 0;
16259 /* -1 means: do not use TENTHS. */
16260 int tenths = -1;
16261 int exponent = 0;
16262
16263 /* Length of QUOTIENT.TENTHS as a string. */
16264 int length;
16265
16266 char * psuffix;
16267 char * p;
16268
16269 if (1000 <= quotient)
16270 {
16271 /* Scale to the appropriate EXPONENT. */
16272 do
16273 {
16274 remainder = quotient % 1000;
16275 quotient /= 1000;
16276 exponent++;
16277 }
16278 while (1000 <= quotient);
16279
16280 /* Round to nearest and decide whether to use TENTHS or not. */
16281 if (quotient <= 9)
16282 {
16283 tenths = remainder / 100;
16284 if (50 <= remainder % 100)
16285 {
16286 if (tenths < 9)
16287 tenths++;
16288 else
16289 {
16290 quotient++;
16291 if (quotient == 10)
16292 tenths = -1;
16293 else
16294 tenths = 0;
16295 }
16296 }
16297 }
16298 else
16299 if (500 <= remainder)
16300 {
16301 if (quotient < 999)
16302 quotient++;
16303 else
16304 {
16305 quotient = 1;
16306 exponent++;
16307 tenths = 0;
16308 }
16309 }
16310 }
16311
16312 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16313 if (tenths == -1 && quotient <= 99)
16314 if (quotient <= 9)
16315 length = 1;
16316 else
16317 length = 2;
16318 else
16319 length = 3;
16320 p = psuffix = buf + max (width, length);
16321
16322 /* Print EXPONENT. */
16323 if (exponent)
16324 *psuffix++ = power_letter[exponent];
16325 *psuffix = '\0';
16326
16327 /* Print TENTHS. */
16328 if (tenths >= 0)
16329 {
16330 *--p = '0' + tenths;
16331 *--p = '.';
16332 }
16333
16334 /* Print QUOTIENT. */
16335 do
16336 {
16337 int digit = quotient % 10;
16338 *--p = '0' + digit;
16339 }
16340 while ((quotient /= 10) != 0);
16341
16342 /* Print leading spaces. */
16343 while (buf < p)
16344 *--p = ' ';
16345 }
16346
16347 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16348 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16349 type of CODING_SYSTEM. Return updated pointer into BUF. */
16350
16351 static unsigned char invalid_eol_type[] = "(*invalid*)";
16352
16353 static char *
16354 decode_mode_spec_coding (coding_system, buf, eol_flag)
16355 Lisp_Object coding_system;
16356 register char *buf;
16357 int eol_flag;
16358 {
16359 Lisp_Object val;
16360 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16361 const unsigned char *eol_str;
16362 int eol_str_len;
16363 /* The EOL conversion we are using. */
16364 Lisp_Object eoltype;
16365
16366 val = Fget (coding_system, Qcoding_system);
16367 eoltype = Qnil;
16368
16369 if (!VECTORP (val)) /* Not yet decided. */
16370 {
16371 if (multibyte)
16372 *buf++ = '-';
16373 if (eol_flag)
16374 eoltype = eol_mnemonic_undecided;
16375 /* Don't mention EOL conversion if it isn't decided. */
16376 }
16377 else
16378 {
16379 Lisp_Object eolvalue;
16380
16381 eolvalue = Fget (coding_system, Qeol_type);
16382
16383 if (multibyte)
16384 *buf++ = XFASTINT (AREF (val, 1));
16385
16386 if (eol_flag)
16387 {
16388 /* The EOL conversion that is normal on this system. */
16389
16390 if (NILP (eolvalue)) /* Not yet decided. */
16391 eoltype = eol_mnemonic_undecided;
16392 else if (VECTORP (eolvalue)) /* Not yet decided. */
16393 eoltype = eol_mnemonic_undecided;
16394 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16395 eoltype = (XFASTINT (eolvalue) == 0
16396 ? eol_mnemonic_unix
16397 : (XFASTINT (eolvalue) == 1
16398 ? eol_mnemonic_dos : eol_mnemonic_mac));
16399 }
16400 }
16401
16402 if (eol_flag)
16403 {
16404 /* Mention the EOL conversion if it is not the usual one. */
16405 if (STRINGP (eoltype))
16406 {
16407 eol_str = SDATA (eoltype);
16408 eol_str_len = SBYTES (eoltype);
16409 }
16410 else if (INTEGERP (eoltype)
16411 && CHAR_VALID_P (XINT (eoltype), 0))
16412 {
16413 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16414 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16415 eol_str = tmp;
16416 }
16417 else
16418 {
16419 eol_str = invalid_eol_type;
16420 eol_str_len = sizeof (invalid_eol_type) - 1;
16421 }
16422 bcopy (eol_str, buf, eol_str_len);
16423 buf += eol_str_len;
16424 }
16425
16426 return buf;
16427 }
16428
16429 /* Return a string for the output of a mode line %-spec for window W,
16430 generated by character C. PRECISION >= 0 means don't return a
16431 string longer than that value. FIELD_WIDTH > 0 means pad the
16432 string returned with spaces to that value. Return 1 in *MULTIBYTE
16433 if the result is multibyte text.
16434
16435 Note we operate on the current buffer for most purposes,
16436 the exception being w->base_line_pos. */
16437
16438 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16439
16440 static char *
16441 decode_mode_spec (w, c, field_width, precision, multibyte)
16442 struct window *w;
16443 register int c;
16444 int field_width, precision;
16445 int *multibyte;
16446 {
16447 Lisp_Object obj;
16448 struct frame *f = XFRAME (WINDOW_FRAME (w));
16449 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16450 struct buffer *b = current_buffer;
16451
16452 obj = Qnil;
16453 *multibyte = 0;
16454
16455 switch (c)
16456 {
16457 case '*':
16458 if (!NILP (b->read_only))
16459 return "%";
16460 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16461 return "*";
16462 return "-";
16463
16464 case '+':
16465 /* This differs from %* only for a modified read-only buffer. */
16466 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16467 return "*";
16468 if (!NILP (b->read_only))
16469 return "%";
16470 return "-";
16471
16472 case '&':
16473 /* This differs from %* in ignoring read-only-ness. */
16474 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16475 return "*";
16476 return "-";
16477
16478 case '%':
16479 return "%";
16480
16481 case '[':
16482 {
16483 int i;
16484 char *p;
16485
16486 if (command_loop_level > 5)
16487 return "[[[... ";
16488 p = decode_mode_spec_buf;
16489 for (i = 0; i < command_loop_level; i++)
16490 *p++ = '[';
16491 *p = 0;
16492 return decode_mode_spec_buf;
16493 }
16494
16495 case ']':
16496 {
16497 int i;
16498 char *p;
16499
16500 if (command_loop_level > 5)
16501 return " ...]]]";
16502 p = decode_mode_spec_buf;
16503 for (i = 0; i < command_loop_level; i++)
16504 *p++ = ']';
16505 *p = 0;
16506 return decode_mode_spec_buf;
16507 }
16508
16509 case '-':
16510 {
16511 register int i;
16512
16513 /* Let lots_of_dashes be a string of infinite length. */
16514 if (!NILP (mode_line_string_list))
16515 return "--";
16516 if (field_width <= 0
16517 || field_width > sizeof (lots_of_dashes))
16518 {
16519 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16520 decode_mode_spec_buf[i] = '-';
16521 decode_mode_spec_buf[i] = '\0';
16522 return decode_mode_spec_buf;
16523 }
16524 else
16525 return lots_of_dashes;
16526 }
16527
16528 case 'b':
16529 obj = b->name;
16530 break;
16531
16532 case 'c':
16533 {
16534 int col = (int) current_column (); /* iftc */
16535 w->column_number_displayed = make_number (col);
16536 pint2str (decode_mode_spec_buf, field_width, col);
16537 return decode_mode_spec_buf;
16538 }
16539
16540 case 'F':
16541 /* %F displays the frame name. */
16542 if (!NILP (f->title))
16543 return (char *) SDATA (f->title);
16544 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16545 return (char *) SDATA (f->name);
16546 return "Emacs";
16547
16548 case 'f':
16549 obj = b->filename;
16550 break;
16551
16552 case 'i':
16553 {
16554 int size = ZV - BEGV;
16555 pint2str (decode_mode_spec_buf, field_width, size);
16556 return decode_mode_spec_buf;
16557 }
16558
16559 case 'I':
16560 {
16561 int size = ZV - BEGV;
16562 pint2hrstr (decode_mode_spec_buf, field_width, size);
16563 return decode_mode_spec_buf;
16564 }
16565
16566 case 'l':
16567 {
16568 int startpos = XMARKER (w->start)->charpos;
16569 int startpos_byte = marker_byte_position (w->start);
16570 int line, linepos, linepos_byte, topline;
16571 int nlines, junk;
16572 int height = WINDOW_TOTAL_LINES (w);
16573
16574 /* If we decided that this buffer isn't suitable for line numbers,
16575 don't forget that too fast. */
16576 if (EQ (w->base_line_pos, w->buffer))
16577 goto no_value;
16578 /* But do forget it, if the window shows a different buffer now. */
16579 else if (BUFFERP (w->base_line_pos))
16580 w->base_line_pos = Qnil;
16581
16582 /* If the buffer is very big, don't waste time. */
16583 if (INTEGERP (Vline_number_display_limit)
16584 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16585 {
16586 w->base_line_pos = Qnil;
16587 w->base_line_number = Qnil;
16588 goto no_value;
16589 }
16590
16591 if (!NILP (w->base_line_number)
16592 && !NILP (w->base_line_pos)
16593 && XFASTINT (w->base_line_pos) <= startpos)
16594 {
16595 line = XFASTINT (w->base_line_number);
16596 linepos = XFASTINT (w->base_line_pos);
16597 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16598 }
16599 else
16600 {
16601 line = 1;
16602 linepos = BUF_BEGV (b);
16603 linepos_byte = BUF_BEGV_BYTE (b);
16604 }
16605
16606 /* Count lines from base line to window start position. */
16607 nlines = display_count_lines (linepos, linepos_byte,
16608 startpos_byte,
16609 startpos, &junk);
16610
16611 topline = nlines + line;
16612
16613 /* Determine a new base line, if the old one is too close
16614 or too far away, or if we did not have one.
16615 "Too close" means it's plausible a scroll-down would
16616 go back past it. */
16617 if (startpos == BUF_BEGV (b))
16618 {
16619 w->base_line_number = make_number (topline);
16620 w->base_line_pos = make_number (BUF_BEGV (b));
16621 }
16622 else if (nlines < height + 25 || nlines > height * 3 + 50
16623 || linepos == BUF_BEGV (b))
16624 {
16625 int limit = BUF_BEGV (b);
16626 int limit_byte = BUF_BEGV_BYTE (b);
16627 int position;
16628 int distance = (height * 2 + 30) * line_number_display_limit_width;
16629
16630 if (startpos - distance > limit)
16631 {
16632 limit = startpos - distance;
16633 limit_byte = CHAR_TO_BYTE (limit);
16634 }
16635
16636 nlines = display_count_lines (startpos, startpos_byte,
16637 limit_byte,
16638 - (height * 2 + 30),
16639 &position);
16640 /* If we couldn't find the lines we wanted within
16641 line_number_display_limit_width chars per line,
16642 give up on line numbers for this window. */
16643 if (position == limit_byte && limit == startpos - distance)
16644 {
16645 w->base_line_pos = w->buffer;
16646 w->base_line_number = Qnil;
16647 goto no_value;
16648 }
16649
16650 w->base_line_number = make_number (topline - nlines);
16651 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16652 }
16653
16654 /* Now count lines from the start pos to point. */
16655 nlines = display_count_lines (startpos, startpos_byte,
16656 PT_BYTE, PT, &junk);
16657
16658 /* Record that we did display the line number. */
16659 line_number_displayed = 1;
16660
16661 /* Make the string to show. */
16662 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16663 return decode_mode_spec_buf;
16664 no_value:
16665 {
16666 char* p = decode_mode_spec_buf;
16667 int pad = field_width - 2;
16668 while (pad-- > 0)
16669 *p++ = ' ';
16670 *p++ = '?';
16671 *p++ = '?';
16672 *p = '\0';
16673 return decode_mode_spec_buf;
16674 }
16675 }
16676 break;
16677
16678 case 'm':
16679 obj = b->mode_name;
16680 break;
16681
16682 case 'n':
16683 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16684 return " Narrow";
16685 break;
16686
16687 case 'p':
16688 {
16689 int pos = marker_position (w->start);
16690 int total = BUF_ZV (b) - BUF_BEGV (b);
16691
16692 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16693 {
16694 if (pos <= BUF_BEGV (b))
16695 return "All";
16696 else
16697 return "Bottom";
16698 }
16699 else if (pos <= BUF_BEGV (b))
16700 return "Top";
16701 else
16702 {
16703 if (total > 1000000)
16704 /* Do it differently for a large value, to avoid overflow. */
16705 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16706 else
16707 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16708 /* We can't normally display a 3-digit number,
16709 so get us a 2-digit number that is close. */
16710 if (total == 100)
16711 total = 99;
16712 sprintf (decode_mode_spec_buf, "%2d%%", total);
16713 return decode_mode_spec_buf;
16714 }
16715 }
16716
16717 /* Display percentage of size above the bottom of the screen. */
16718 case 'P':
16719 {
16720 int toppos = marker_position (w->start);
16721 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16722 int total = BUF_ZV (b) - BUF_BEGV (b);
16723
16724 if (botpos >= BUF_ZV (b))
16725 {
16726 if (toppos <= BUF_BEGV (b))
16727 return "All";
16728 else
16729 return "Bottom";
16730 }
16731 else
16732 {
16733 if (total > 1000000)
16734 /* Do it differently for a large value, to avoid overflow. */
16735 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16736 else
16737 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16738 /* We can't normally display a 3-digit number,
16739 so get us a 2-digit number that is close. */
16740 if (total == 100)
16741 total = 99;
16742 if (toppos <= BUF_BEGV (b))
16743 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16744 else
16745 sprintf (decode_mode_spec_buf, "%2d%%", total);
16746 return decode_mode_spec_buf;
16747 }
16748 }
16749
16750 case 's':
16751 /* status of process */
16752 obj = Fget_buffer_process (Fcurrent_buffer ());
16753 if (NILP (obj))
16754 return "no process";
16755 #ifdef subprocesses
16756 obj = Fsymbol_name (Fprocess_status (obj));
16757 #endif
16758 break;
16759
16760 case 't': /* indicate TEXT or BINARY */
16761 #ifdef MODE_LINE_BINARY_TEXT
16762 return MODE_LINE_BINARY_TEXT (b);
16763 #else
16764 return "T";
16765 #endif
16766
16767 case 'z':
16768 /* coding-system (not including end-of-line format) */
16769 case 'Z':
16770 /* coding-system (including end-of-line type) */
16771 {
16772 int eol_flag = (c == 'Z');
16773 char *p = decode_mode_spec_buf;
16774
16775 if (! FRAME_WINDOW_P (f))
16776 {
16777 /* No need to mention EOL here--the terminal never needs
16778 to do EOL conversion. */
16779 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16780 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16781 }
16782 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16783 p, eol_flag);
16784
16785 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16786 #ifdef subprocesses
16787 obj = Fget_buffer_process (Fcurrent_buffer ());
16788 if (PROCESSP (obj))
16789 {
16790 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16791 p, eol_flag);
16792 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16793 p, eol_flag);
16794 }
16795 #endif /* subprocesses */
16796 #endif /* 0 */
16797 *p = 0;
16798 return decode_mode_spec_buf;
16799 }
16800 }
16801
16802 if (STRINGP (obj))
16803 {
16804 *multibyte = STRING_MULTIBYTE (obj);
16805 return (char *) SDATA (obj);
16806 }
16807 else
16808 return "";
16809 }
16810
16811
16812 /* Count up to COUNT lines starting from START / START_BYTE.
16813 But don't go beyond LIMIT_BYTE.
16814 Return the number of lines thus found (always nonnegative).
16815
16816 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16817
16818 static int
16819 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16820 int start, start_byte, limit_byte, count;
16821 int *byte_pos_ptr;
16822 {
16823 register unsigned char *cursor;
16824 unsigned char *base;
16825
16826 register int ceiling;
16827 register unsigned char *ceiling_addr;
16828 int orig_count = count;
16829
16830 /* If we are not in selective display mode,
16831 check only for newlines. */
16832 int selective_display = (!NILP (current_buffer->selective_display)
16833 && !INTEGERP (current_buffer->selective_display));
16834
16835 if (count > 0)
16836 {
16837 while (start_byte < limit_byte)
16838 {
16839 ceiling = BUFFER_CEILING_OF (start_byte);
16840 ceiling = min (limit_byte - 1, ceiling);
16841 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16842 base = (cursor = BYTE_POS_ADDR (start_byte));
16843 while (1)
16844 {
16845 if (selective_display)
16846 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16847 ;
16848 else
16849 while (*cursor != '\n' && ++cursor != ceiling_addr)
16850 ;
16851
16852 if (cursor != ceiling_addr)
16853 {
16854 if (--count == 0)
16855 {
16856 start_byte += cursor - base + 1;
16857 *byte_pos_ptr = start_byte;
16858 return orig_count;
16859 }
16860 else
16861 if (++cursor == ceiling_addr)
16862 break;
16863 }
16864 else
16865 break;
16866 }
16867 start_byte += cursor - base;
16868 }
16869 }
16870 else
16871 {
16872 while (start_byte > limit_byte)
16873 {
16874 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16875 ceiling = max (limit_byte, ceiling);
16876 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16877 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16878 while (1)
16879 {
16880 if (selective_display)
16881 while (--cursor != ceiling_addr
16882 && *cursor != '\n' && *cursor != 015)
16883 ;
16884 else
16885 while (--cursor != ceiling_addr && *cursor != '\n')
16886 ;
16887
16888 if (cursor != ceiling_addr)
16889 {
16890 if (++count == 0)
16891 {
16892 start_byte += cursor - base + 1;
16893 *byte_pos_ptr = start_byte;
16894 /* When scanning backwards, we should
16895 not count the newline posterior to which we stop. */
16896 return - orig_count - 1;
16897 }
16898 }
16899 else
16900 break;
16901 }
16902 /* Here we add 1 to compensate for the last decrement
16903 of CURSOR, which took it past the valid range. */
16904 start_byte += cursor - base + 1;
16905 }
16906 }
16907
16908 *byte_pos_ptr = limit_byte;
16909
16910 if (count < 0)
16911 return - orig_count + count;
16912 return orig_count - count;
16913
16914 }
16915
16916
16917 \f
16918 /***********************************************************************
16919 Displaying strings
16920 ***********************************************************************/
16921
16922 /* Display a NUL-terminated string, starting with index START.
16923
16924 If STRING is non-null, display that C string. Otherwise, the Lisp
16925 string LISP_STRING is displayed.
16926
16927 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16928 FACE_STRING. Display STRING or LISP_STRING with the face at
16929 FACE_STRING_POS in FACE_STRING:
16930
16931 Display the string in the environment given by IT, but use the
16932 standard display table, temporarily.
16933
16934 FIELD_WIDTH is the minimum number of output glyphs to produce.
16935 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16936 with spaces. If STRING has more characters, more than FIELD_WIDTH
16937 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16938
16939 PRECISION is the maximum number of characters to output from
16940 STRING. PRECISION < 0 means don't truncate the string.
16941
16942 This is roughly equivalent to printf format specifiers:
16943
16944 FIELD_WIDTH PRECISION PRINTF
16945 ----------------------------------------
16946 -1 -1 %s
16947 -1 10 %.10s
16948 10 -1 %10s
16949 20 10 %20.10s
16950
16951 MULTIBYTE zero means do not display multibyte chars, > 0 means do
16952 display them, and < 0 means obey the current buffer's value of
16953 enable_multibyte_characters.
16954
16955 Value is the number of glyphs produced. */
16956
16957 static int
16958 display_string (string, lisp_string, face_string, face_string_pos,
16959 start, it, field_width, precision, max_x, multibyte)
16960 unsigned char *string;
16961 Lisp_Object lisp_string;
16962 Lisp_Object face_string;
16963 int face_string_pos;
16964 int start;
16965 struct it *it;
16966 int field_width, precision, max_x;
16967 int multibyte;
16968 {
16969 int hpos_at_start = it->hpos;
16970 int saved_face_id = it->face_id;
16971 struct glyph_row *row = it->glyph_row;
16972
16973 /* Initialize the iterator IT for iteration over STRING beginning
16974 with index START. */
16975 reseat_to_string (it, string, lisp_string, start,
16976 precision, field_width, multibyte);
16977
16978 /* If displaying STRING, set up the face of the iterator
16979 from LISP_STRING, if that's given. */
16980 if (STRINGP (face_string))
16981 {
16982 int endptr;
16983 struct face *face;
16984
16985 it->face_id
16986 = face_at_string_position (it->w, face_string, face_string_pos,
16987 0, it->region_beg_charpos,
16988 it->region_end_charpos,
16989 &endptr, it->base_face_id, 0);
16990 face = FACE_FROM_ID (it->f, it->face_id);
16991 it->face_box_p = face->box != FACE_NO_BOX;
16992 }
16993
16994 /* Set max_x to the maximum allowed X position. Don't let it go
16995 beyond the right edge of the window. */
16996 if (max_x <= 0)
16997 max_x = it->last_visible_x;
16998 else
16999 max_x = min (max_x, it->last_visible_x);
17000
17001 /* Skip over display elements that are not visible. because IT->w is
17002 hscrolled. */
17003 if (it->current_x < it->first_visible_x)
17004 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17005 MOVE_TO_POS | MOVE_TO_X);
17006
17007 row->ascent = it->max_ascent;
17008 row->height = it->max_ascent + it->max_descent;
17009 row->phys_ascent = it->max_phys_ascent;
17010 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17011 row->extra_line_spacing = it->max_extra_line_spacing;
17012
17013 /* This condition is for the case that we are called with current_x
17014 past last_visible_x. */
17015 while (it->current_x < max_x)
17016 {
17017 int x_before, x, n_glyphs_before, i, nglyphs;
17018
17019 /* Get the next display element. */
17020 if (!get_next_display_element (it))
17021 break;
17022
17023 /* Produce glyphs. */
17024 x_before = it->current_x;
17025 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17026 PRODUCE_GLYPHS (it);
17027
17028 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17029 i = 0;
17030 x = x_before;
17031 while (i < nglyphs)
17032 {
17033 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17034
17035 if (!it->truncate_lines_p
17036 && x + glyph->pixel_width > max_x)
17037 {
17038 /* End of continued line or max_x reached. */
17039 if (CHAR_GLYPH_PADDING_P (*glyph))
17040 {
17041 /* A wide character is unbreakable. */
17042 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17043 it->current_x = x_before;
17044 }
17045 else
17046 {
17047 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17048 it->current_x = x;
17049 }
17050 break;
17051 }
17052 else if (x + glyph->pixel_width > it->first_visible_x)
17053 {
17054 /* Glyph is at least partially visible. */
17055 ++it->hpos;
17056 if (x < it->first_visible_x)
17057 it->glyph_row->x = x - it->first_visible_x;
17058 }
17059 else
17060 {
17061 /* Glyph is off the left margin of the display area.
17062 Should not happen. */
17063 abort ();
17064 }
17065
17066 row->ascent = max (row->ascent, it->max_ascent);
17067 row->height = max (row->height, it->max_ascent + it->max_descent);
17068 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17069 row->phys_height = max (row->phys_height,
17070 it->max_phys_ascent + it->max_phys_descent);
17071 row->extra_line_spacing = max (row->extra_line_spacing,
17072 it->max_extra_line_spacing);
17073 x += glyph->pixel_width;
17074 ++i;
17075 }
17076
17077 /* Stop if max_x reached. */
17078 if (i < nglyphs)
17079 break;
17080
17081 /* Stop at line ends. */
17082 if (ITERATOR_AT_END_OF_LINE_P (it))
17083 {
17084 it->continuation_lines_width = 0;
17085 break;
17086 }
17087
17088 set_iterator_to_next (it, 1);
17089
17090 /* Stop if truncating at the right edge. */
17091 if (it->truncate_lines_p
17092 && it->current_x >= it->last_visible_x)
17093 {
17094 /* Add truncation mark, but don't do it if the line is
17095 truncated at a padding space. */
17096 if (IT_CHARPOS (*it) < it->string_nchars)
17097 {
17098 if (!FRAME_WINDOW_P (it->f))
17099 {
17100 int i, n;
17101
17102 if (it->current_x > it->last_visible_x)
17103 {
17104 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17105 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17106 break;
17107 for (n = row->used[TEXT_AREA]; i < n; ++i)
17108 {
17109 row->used[TEXT_AREA] = i;
17110 produce_special_glyphs (it, IT_TRUNCATION);
17111 }
17112 }
17113 produce_special_glyphs (it, IT_TRUNCATION);
17114 }
17115 it->glyph_row->truncated_on_right_p = 1;
17116 }
17117 break;
17118 }
17119 }
17120
17121 /* Maybe insert a truncation at the left. */
17122 if (it->first_visible_x
17123 && IT_CHARPOS (*it) > 0)
17124 {
17125 if (!FRAME_WINDOW_P (it->f))
17126 insert_left_trunc_glyphs (it);
17127 it->glyph_row->truncated_on_left_p = 1;
17128 }
17129
17130 it->face_id = saved_face_id;
17131
17132 /* Value is number of columns displayed. */
17133 return it->hpos - hpos_at_start;
17134 }
17135
17136
17137 \f
17138 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17139 appears as an element of LIST or as the car of an element of LIST.
17140 If PROPVAL is a list, compare each element against LIST in that
17141 way, and return 1/2 if any element of PROPVAL is found in LIST.
17142 Otherwise return 0. This function cannot quit.
17143 The return value is 2 if the text is invisible but with an ellipsis
17144 and 1 if it's invisible and without an ellipsis. */
17145
17146 int
17147 invisible_p (propval, list)
17148 register Lisp_Object propval;
17149 Lisp_Object list;
17150 {
17151 register Lisp_Object tail, proptail;
17152
17153 for (tail = list; CONSP (tail); tail = XCDR (tail))
17154 {
17155 register Lisp_Object tem;
17156 tem = XCAR (tail);
17157 if (EQ (propval, tem))
17158 return 1;
17159 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17160 return NILP (XCDR (tem)) ? 1 : 2;
17161 }
17162
17163 if (CONSP (propval))
17164 {
17165 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17166 {
17167 Lisp_Object propelt;
17168 propelt = XCAR (proptail);
17169 for (tail = list; CONSP (tail); tail = XCDR (tail))
17170 {
17171 register Lisp_Object tem;
17172 tem = XCAR (tail);
17173 if (EQ (propelt, tem))
17174 return 1;
17175 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17176 return NILP (XCDR (tem)) ? 1 : 2;
17177 }
17178 }
17179 }
17180
17181 return 0;
17182 }
17183
17184 /* Calculate a width or height in pixels from a specification using
17185 the following elements:
17186
17187 SPEC ::=
17188 NUM - a (fractional) multiple of the default font width/height
17189 (NUM) - specifies exactly NUM pixels
17190 UNIT - a fixed number of pixels, see below.
17191 ELEMENT - size of a display element in pixels, see below.
17192 (NUM . SPEC) - equals NUM * SPEC
17193 (+ SPEC SPEC ...) - add pixel values
17194 (- SPEC SPEC ...) - subtract pixel values
17195 (- SPEC) - negate pixel value
17196
17197 NUM ::=
17198 INT or FLOAT - a number constant
17199 SYMBOL - use symbol's (buffer local) variable binding.
17200
17201 UNIT ::=
17202 in - pixels per inch *)
17203 mm - pixels per 1/1000 meter *)
17204 cm - pixels per 1/100 meter *)
17205 width - width of current font in pixels.
17206 height - height of current font in pixels.
17207
17208 *) using the ratio(s) defined in display-pixels-per-inch.
17209
17210 ELEMENT ::=
17211
17212 left-fringe - left fringe width in pixels
17213 right-fringe - right fringe width in pixels
17214
17215 left-margin - left margin width in pixels
17216 right-margin - right margin width in pixels
17217
17218 scroll-bar - scroll-bar area width in pixels
17219
17220 Examples:
17221
17222 Pixels corresponding to 5 inches:
17223 (5 . in)
17224
17225 Total width of non-text areas on left side of window (if scroll-bar is on left):
17226 '(space :width (+ left-fringe left-margin scroll-bar))
17227
17228 Align to first text column (in header line):
17229 '(space :align-to 0)
17230
17231 Align to middle of text area minus half the width of variable `my-image'
17232 containing a loaded image:
17233 '(space :align-to (0.5 . (- text my-image)))
17234
17235 Width of left margin minus width of 1 character in the default font:
17236 '(space :width (- left-margin 1))
17237
17238 Width of left margin minus width of 2 characters in the current font:
17239 '(space :width (- left-margin (2 . width)))
17240
17241 Center 1 character over left-margin (in header line):
17242 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17243
17244 Different ways to express width of left fringe plus left margin minus one pixel:
17245 '(space :width (- (+ left-fringe left-margin) (1)))
17246 '(space :width (+ left-fringe left-margin (- (1))))
17247 '(space :width (+ left-fringe left-margin (-1)))
17248
17249 */
17250
17251 #define NUMVAL(X) \
17252 ((INTEGERP (X) || FLOATP (X)) \
17253 ? XFLOATINT (X) \
17254 : - 1)
17255
17256 int
17257 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17258 double *res;
17259 struct it *it;
17260 Lisp_Object prop;
17261 void *font;
17262 int width_p, *align_to;
17263 {
17264 double pixels;
17265
17266 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17267 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17268
17269 if (NILP (prop))
17270 return OK_PIXELS (0);
17271
17272 if (SYMBOLP (prop))
17273 {
17274 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17275 {
17276 char *unit = SDATA (SYMBOL_NAME (prop));
17277
17278 if (unit[0] == 'i' && unit[1] == 'n')
17279 pixels = 1.0;
17280 else if (unit[0] == 'm' && unit[1] == 'm')
17281 pixels = 25.4;
17282 else if (unit[0] == 'c' && unit[1] == 'm')
17283 pixels = 2.54;
17284 else
17285 pixels = 0;
17286 if (pixels > 0)
17287 {
17288 double ppi;
17289 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17290 || (CONSP (Vdisplay_pixels_per_inch)
17291 && (ppi = (width_p
17292 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17293 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17294 ppi > 0)))
17295 return OK_PIXELS (ppi / pixels);
17296
17297 return 0;
17298 }
17299 }
17300
17301 #ifdef HAVE_WINDOW_SYSTEM
17302 if (EQ (prop, Qheight))
17303 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17304 if (EQ (prop, Qwidth))
17305 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17306 #else
17307 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17308 return OK_PIXELS (1);
17309 #endif
17310
17311 if (EQ (prop, Qtext))
17312 return OK_PIXELS (width_p
17313 ? window_box_width (it->w, TEXT_AREA)
17314 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17315
17316 if (align_to && *align_to < 0)
17317 {
17318 *res = 0;
17319 if (EQ (prop, Qleft))
17320 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17321 if (EQ (prop, Qright))
17322 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17323 if (EQ (prop, Qcenter))
17324 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17325 + window_box_width (it->w, TEXT_AREA) / 2);
17326 if (EQ (prop, Qleft_fringe))
17327 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17328 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17329 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17330 if (EQ (prop, Qright_fringe))
17331 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17332 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17333 : window_box_right_offset (it->w, TEXT_AREA));
17334 if (EQ (prop, Qleft_margin))
17335 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17336 if (EQ (prop, Qright_margin))
17337 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17338 if (EQ (prop, Qscroll_bar))
17339 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17340 ? 0
17341 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17342 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17343 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17344 : 0)));
17345 }
17346 else
17347 {
17348 if (EQ (prop, Qleft_fringe))
17349 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17350 if (EQ (prop, Qright_fringe))
17351 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17352 if (EQ (prop, Qleft_margin))
17353 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17354 if (EQ (prop, Qright_margin))
17355 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17356 if (EQ (prop, Qscroll_bar))
17357 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17358 }
17359
17360 prop = Fbuffer_local_value (prop, it->w->buffer);
17361 }
17362
17363 if (INTEGERP (prop) || FLOATP (prop))
17364 {
17365 int base_unit = (width_p
17366 ? FRAME_COLUMN_WIDTH (it->f)
17367 : FRAME_LINE_HEIGHT (it->f));
17368 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17369 }
17370
17371 if (CONSP (prop))
17372 {
17373 Lisp_Object car = XCAR (prop);
17374 Lisp_Object cdr = XCDR (prop);
17375
17376 if (SYMBOLP (car))
17377 {
17378 #ifdef HAVE_WINDOW_SYSTEM
17379 if (valid_image_p (prop))
17380 {
17381 int id = lookup_image (it->f, prop);
17382 struct image *img = IMAGE_FROM_ID (it->f, id);
17383
17384 return OK_PIXELS (width_p ? img->width : img->height);
17385 }
17386 #endif
17387 if (EQ (car, Qplus) || EQ (car, Qminus))
17388 {
17389 int first = 1;
17390 double px;
17391
17392 pixels = 0;
17393 while (CONSP (cdr))
17394 {
17395 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17396 font, width_p, align_to))
17397 return 0;
17398 if (first)
17399 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17400 else
17401 pixels += px;
17402 cdr = XCDR (cdr);
17403 }
17404 if (EQ (car, Qminus))
17405 pixels = -pixels;
17406 return OK_PIXELS (pixels);
17407 }
17408
17409 car = Fbuffer_local_value (car, it->w->buffer);
17410 }
17411
17412 if (INTEGERP (car) || FLOATP (car))
17413 {
17414 double fact;
17415 pixels = XFLOATINT (car);
17416 if (NILP (cdr))
17417 return OK_PIXELS (pixels);
17418 if (calc_pixel_width_or_height (&fact, it, cdr,
17419 font, width_p, align_to))
17420 return OK_PIXELS (pixels * fact);
17421 return 0;
17422 }
17423
17424 return 0;
17425 }
17426
17427 return 0;
17428 }
17429
17430 \f
17431 /***********************************************************************
17432 Glyph Display
17433 ***********************************************************************/
17434
17435 #ifdef HAVE_WINDOW_SYSTEM
17436
17437 #if GLYPH_DEBUG
17438
17439 void
17440 dump_glyph_string (s)
17441 struct glyph_string *s;
17442 {
17443 fprintf (stderr, "glyph string\n");
17444 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17445 s->x, s->y, s->width, s->height);
17446 fprintf (stderr, " ybase = %d\n", s->ybase);
17447 fprintf (stderr, " hl = %d\n", s->hl);
17448 fprintf (stderr, " left overhang = %d, right = %d\n",
17449 s->left_overhang, s->right_overhang);
17450 fprintf (stderr, " nchars = %d\n", s->nchars);
17451 fprintf (stderr, " extends to end of line = %d\n",
17452 s->extends_to_end_of_line_p);
17453 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17454 fprintf (stderr, " bg width = %d\n", s->background_width);
17455 }
17456
17457 #endif /* GLYPH_DEBUG */
17458
17459 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17460 of XChar2b structures for S; it can't be allocated in
17461 init_glyph_string because it must be allocated via `alloca'. W
17462 is the window on which S is drawn. ROW and AREA are the glyph row
17463 and area within the row from which S is constructed. START is the
17464 index of the first glyph structure covered by S. HL is a
17465 face-override for drawing S. */
17466
17467 #ifdef HAVE_NTGUI
17468 #define OPTIONAL_HDC(hdc) hdc,
17469 #define DECLARE_HDC(hdc) HDC hdc;
17470 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17471 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17472 #endif
17473
17474 #ifndef OPTIONAL_HDC
17475 #define OPTIONAL_HDC(hdc)
17476 #define DECLARE_HDC(hdc)
17477 #define ALLOCATE_HDC(hdc, f)
17478 #define RELEASE_HDC(hdc, f)
17479 #endif
17480
17481 static void
17482 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17483 struct glyph_string *s;
17484 DECLARE_HDC (hdc)
17485 XChar2b *char2b;
17486 struct window *w;
17487 struct glyph_row *row;
17488 enum glyph_row_area area;
17489 int start;
17490 enum draw_glyphs_face hl;
17491 {
17492 bzero (s, sizeof *s);
17493 s->w = w;
17494 s->f = XFRAME (w->frame);
17495 #ifdef HAVE_NTGUI
17496 s->hdc = hdc;
17497 #endif
17498 s->display = FRAME_X_DISPLAY (s->f);
17499 s->window = FRAME_X_WINDOW (s->f);
17500 s->char2b = char2b;
17501 s->hl = hl;
17502 s->row = row;
17503 s->area = area;
17504 s->first_glyph = row->glyphs[area] + start;
17505 s->height = row->height;
17506 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17507
17508 /* Display the internal border below the tool-bar window. */
17509 if (WINDOWP (s->f->tool_bar_window)
17510 && s->w == XWINDOW (s->f->tool_bar_window))
17511 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17512
17513 s->ybase = s->y + row->ascent;
17514 }
17515
17516
17517 /* Append the list of glyph strings with head H and tail T to the list
17518 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17519
17520 static INLINE void
17521 append_glyph_string_lists (head, tail, h, t)
17522 struct glyph_string **head, **tail;
17523 struct glyph_string *h, *t;
17524 {
17525 if (h)
17526 {
17527 if (*head)
17528 (*tail)->next = h;
17529 else
17530 *head = h;
17531 h->prev = *tail;
17532 *tail = t;
17533 }
17534 }
17535
17536
17537 /* Prepend the list of glyph strings with head H and tail T to the
17538 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17539 result. */
17540
17541 static INLINE void
17542 prepend_glyph_string_lists (head, tail, h, t)
17543 struct glyph_string **head, **tail;
17544 struct glyph_string *h, *t;
17545 {
17546 if (h)
17547 {
17548 if (*head)
17549 (*head)->prev = t;
17550 else
17551 *tail = t;
17552 t->next = *head;
17553 *head = h;
17554 }
17555 }
17556
17557
17558 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17559 Set *HEAD and *TAIL to the resulting list. */
17560
17561 static INLINE void
17562 append_glyph_string (head, tail, s)
17563 struct glyph_string **head, **tail;
17564 struct glyph_string *s;
17565 {
17566 s->next = s->prev = NULL;
17567 append_glyph_string_lists (head, tail, s, s);
17568 }
17569
17570
17571 /* Get face and two-byte form of character glyph GLYPH on frame F.
17572 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17573 a pointer to a realized face that is ready for display. */
17574
17575 static INLINE struct face *
17576 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17577 struct frame *f;
17578 struct glyph *glyph;
17579 XChar2b *char2b;
17580 int *two_byte_p;
17581 {
17582 struct face *face;
17583
17584 xassert (glyph->type == CHAR_GLYPH);
17585 face = FACE_FROM_ID (f, glyph->face_id);
17586
17587 if (two_byte_p)
17588 *two_byte_p = 0;
17589
17590 if (!glyph->multibyte_p)
17591 {
17592 /* Unibyte case. We don't have to encode, but we have to make
17593 sure to use a face suitable for unibyte. */
17594 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17595 }
17596 else if (glyph->u.ch < 128
17597 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17598 {
17599 /* Case of ASCII in a face known to fit ASCII. */
17600 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17601 }
17602 else
17603 {
17604 int c1, c2, charset;
17605
17606 /* Split characters into bytes. If c2 is -1 afterwards, C is
17607 really a one-byte character so that byte1 is zero. */
17608 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17609 if (c2 > 0)
17610 STORE_XCHAR2B (char2b, c1, c2);
17611 else
17612 STORE_XCHAR2B (char2b, 0, c1);
17613
17614 /* Maybe encode the character in *CHAR2B. */
17615 if (charset != CHARSET_ASCII)
17616 {
17617 struct font_info *font_info
17618 = FONT_INFO_FROM_ID (f, face->font_info_id);
17619 if (font_info)
17620 glyph->font_type
17621 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17622 }
17623 }
17624
17625 /* Make sure X resources of the face are allocated. */
17626 xassert (face != NULL);
17627 PREPARE_FACE_FOR_DISPLAY (f, face);
17628 return face;
17629 }
17630
17631
17632 /* Fill glyph string S with composition components specified by S->cmp.
17633
17634 FACES is an array of faces for all components of this composition.
17635 S->gidx is the index of the first component for S.
17636 OVERLAPS_P non-zero means S should draw the foreground only, and
17637 use its physical height for clipping.
17638
17639 Value is the index of a component not in S. */
17640
17641 static int
17642 fill_composite_glyph_string (s, faces, overlaps_p)
17643 struct glyph_string *s;
17644 struct face **faces;
17645 int overlaps_p;
17646 {
17647 int i;
17648
17649 xassert (s);
17650
17651 s->for_overlaps_p = overlaps_p;
17652
17653 s->face = faces[s->gidx];
17654 s->font = s->face->font;
17655 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17656
17657 /* For all glyphs of this composition, starting at the offset
17658 S->gidx, until we reach the end of the definition or encounter a
17659 glyph that requires the different face, add it to S. */
17660 ++s->nchars;
17661 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17662 ++s->nchars;
17663
17664 /* All glyph strings for the same composition has the same width,
17665 i.e. the width set for the first component of the composition. */
17666
17667 s->width = s->first_glyph->pixel_width;
17668
17669 /* If the specified font could not be loaded, use the frame's
17670 default font, but record the fact that we couldn't load it in
17671 the glyph string so that we can draw rectangles for the
17672 characters of the glyph string. */
17673 if (s->font == NULL)
17674 {
17675 s->font_not_found_p = 1;
17676 s->font = FRAME_FONT (s->f);
17677 }
17678
17679 /* Adjust base line for subscript/superscript text. */
17680 s->ybase += s->first_glyph->voffset;
17681
17682 xassert (s->face && s->face->gc);
17683
17684 /* This glyph string must always be drawn with 16-bit functions. */
17685 s->two_byte_p = 1;
17686
17687 return s->gidx + s->nchars;
17688 }
17689
17690
17691 /* Fill glyph string S from a sequence of character glyphs.
17692
17693 FACE_ID is the face id of the string. START is the index of the
17694 first glyph to consider, END is the index of the last + 1.
17695 OVERLAPS_P non-zero means S should draw the foreground only, and
17696 use its physical height for clipping.
17697
17698 Value is the index of the first glyph not in S. */
17699
17700 static int
17701 fill_glyph_string (s, face_id, start, end, overlaps_p)
17702 struct glyph_string *s;
17703 int face_id;
17704 int start, end, overlaps_p;
17705 {
17706 struct glyph *glyph, *last;
17707 int voffset;
17708 int glyph_not_available_p;
17709
17710 xassert (s->f == XFRAME (s->w->frame));
17711 xassert (s->nchars == 0);
17712 xassert (start >= 0 && end > start);
17713
17714 s->for_overlaps_p = overlaps_p,
17715 glyph = s->row->glyphs[s->area] + start;
17716 last = s->row->glyphs[s->area] + end;
17717 voffset = glyph->voffset;
17718
17719 glyph_not_available_p = glyph->glyph_not_available_p;
17720
17721 while (glyph < last
17722 && glyph->type == CHAR_GLYPH
17723 && glyph->voffset == voffset
17724 /* Same face id implies same font, nowadays. */
17725 && glyph->face_id == face_id
17726 && glyph->glyph_not_available_p == glyph_not_available_p)
17727 {
17728 int two_byte_p;
17729
17730 s->face = get_glyph_face_and_encoding (s->f, glyph,
17731 s->char2b + s->nchars,
17732 &two_byte_p);
17733 s->two_byte_p = two_byte_p;
17734 ++s->nchars;
17735 xassert (s->nchars <= end - start);
17736 s->width += glyph->pixel_width;
17737 ++glyph;
17738 }
17739
17740 s->font = s->face->font;
17741 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17742
17743 /* If the specified font could not be loaded, use the frame's font,
17744 but record the fact that we couldn't load it in
17745 S->font_not_found_p so that we can draw rectangles for the
17746 characters of the glyph string. */
17747 if (s->font == NULL || glyph_not_available_p)
17748 {
17749 s->font_not_found_p = 1;
17750 s->font = FRAME_FONT (s->f);
17751 }
17752
17753 /* Adjust base line for subscript/superscript text. */
17754 s->ybase += voffset;
17755
17756 xassert (s->face && s->face->gc);
17757 return glyph - s->row->glyphs[s->area];
17758 }
17759
17760
17761 /* Fill glyph string S from image glyph S->first_glyph. */
17762
17763 static void
17764 fill_image_glyph_string (s)
17765 struct glyph_string *s;
17766 {
17767 xassert (s->first_glyph->type == IMAGE_GLYPH);
17768 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17769 xassert (s->img);
17770 s->slice = s->first_glyph->slice;
17771 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17772 s->font = s->face->font;
17773 s->width = s->first_glyph->pixel_width;
17774
17775 /* Adjust base line for subscript/superscript text. */
17776 s->ybase += s->first_glyph->voffset;
17777 }
17778
17779
17780 /* Fill glyph string S from a sequence of stretch glyphs.
17781
17782 ROW is the glyph row in which the glyphs are found, AREA is the
17783 area within the row. START is the index of the first glyph to
17784 consider, END is the index of the last + 1.
17785
17786 Value is the index of the first glyph not in S. */
17787
17788 static int
17789 fill_stretch_glyph_string (s, row, area, start, end)
17790 struct glyph_string *s;
17791 struct glyph_row *row;
17792 enum glyph_row_area area;
17793 int start, end;
17794 {
17795 struct glyph *glyph, *last;
17796 int voffset, face_id;
17797
17798 xassert (s->first_glyph->type == STRETCH_GLYPH);
17799
17800 glyph = s->row->glyphs[s->area] + start;
17801 last = s->row->glyphs[s->area] + end;
17802 face_id = glyph->face_id;
17803 s->face = FACE_FROM_ID (s->f, face_id);
17804 s->font = s->face->font;
17805 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17806 s->width = glyph->pixel_width;
17807 voffset = glyph->voffset;
17808
17809 for (++glyph;
17810 (glyph < last
17811 && glyph->type == STRETCH_GLYPH
17812 && glyph->voffset == voffset
17813 && glyph->face_id == face_id);
17814 ++glyph)
17815 s->width += glyph->pixel_width;
17816
17817 /* Adjust base line for subscript/superscript text. */
17818 s->ybase += voffset;
17819
17820 /* The case that face->gc == 0 is handled when drawing the glyph
17821 string by calling PREPARE_FACE_FOR_DISPLAY. */
17822 xassert (s->face);
17823 return glyph - s->row->glyphs[s->area];
17824 }
17825
17826
17827 /* EXPORT for RIF:
17828 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17829 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17830 assumed to be zero. */
17831
17832 void
17833 x_get_glyph_overhangs (glyph, f, left, right)
17834 struct glyph *glyph;
17835 struct frame *f;
17836 int *left, *right;
17837 {
17838 *left = *right = 0;
17839
17840 if (glyph->type == CHAR_GLYPH)
17841 {
17842 XFontStruct *font;
17843 struct face *face;
17844 struct font_info *font_info;
17845 XChar2b char2b;
17846 XCharStruct *pcm;
17847
17848 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17849 font = face->font;
17850 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17851 if (font /* ++KFS: Should this be font_info ? */
17852 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17853 {
17854 if (pcm->rbearing > pcm->width)
17855 *right = pcm->rbearing - pcm->width;
17856 if (pcm->lbearing < 0)
17857 *left = -pcm->lbearing;
17858 }
17859 }
17860 }
17861
17862
17863 /* Return the index of the first glyph preceding glyph string S that
17864 is overwritten by S because of S's left overhang. Value is -1
17865 if no glyphs are overwritten. */
17866
17867 static int
17868 left_overwritten (s)
17869 struct glyph_string *s;
17870 {
17871 int k;
17872
17873 if (s->left_overhang)
17874 {
17875 int x = 0, i;
17876 struct glyph *glyphs = s->row->glyphs[s->area];
17877 int first = s->first_glyph - glyphs;
17878
17879 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17880 x -= glyphs[i].pixel_width;
17881
17882 k = i + 1;
17883 }
17884 else
17885 k = -1;
17886
17887 return k;
17888 }
17889
17890
17891 /* Return the index of the first glyph preceding glyph string S that
17892 is overwriting S because of its right overhang. Value is -1 if no
17893 glyph in front of S overwrites S. */
17894
17895 static int
17896 left_overwriting (s)
17897 struct glyph_string *s;
17898 {
17899 int i, k, x;
17900 struct glyph *glyphs = s->row->glyphs[s->area];
17901 int first = s->first_glyph - glyphs;
17902
17903 k = -1;
17904 x = 0;
17905 for (i = first - 1; i >= 0; --i)
17906 {
17907 int left, right;
17908 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17909 if (x + right > 0)
17910 k = i;
17911 x -= glyphs[i].pixel_width;
17912 }
17913
17914 return k;
17915 }
17916
17917
17918 /* Return the index of the last glyph following glyph string S that is
17919 not overwritten by S because of S's right overhang. Value is -1 if
17920 no such glyph is found. */
17921
17922 static int
17923 right_overwritten (s)
17924 struct glyph_string *s;
17925 {
17926 int k = -1;
17927
17928 if (s->right_overhang)
17929 {
17930 int x = 0, i;
17931 struct glyph *glyphs = s->row->glyphs[s->area];
17932 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17933 int end = s->row->used[s->area];
17934
17935 for (i = first; i < end && s->right_overhang > x; ++i)
17936 x += glyphs[i].pixel_width;
17937
17938 k = i;
17939 }
17940
17941 return k;
17942 }
17943
17944
17945 /* Return the index of the last glyph following glyph string S that
17946 overwrites S because of its left overhang. Value is negative
17947 if no such glyph is found. */
17948
17949 static int
17950 right_overwriting (s)
17951 struct glyph_string *s;
17952 {
17953 int i, k, x;
17954 int end = s->row->used[s->area];
17955 struct glyph *glyphs = s->row->glyphs[s->area];
17956 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17957
17958 k = -1;
17959 x = 0;
17960 for (i = first; i < end; ++i)
17961 {
17962 int left, right;
17963 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17964 if (x - left < 0)
17965 k = i;
17966 x += glyphs[i].pixel_width;
17967 }
17968
17969 return k;
17970 }
17971
17972
17973 /* Get face and two-byte form of character C in face FACE_ID on frame
17974 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
17975 means we want to display multibyte text. DISPLAY_P non-zero means
17976 make sure that X resources for the face returned are allocated.
17977 Value is a pointer to a realized face that is ready for display if
17978 DISPLAY_P is non-zero. */
17979
17980 static INLINE struct face *
17981 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
17982 struct frame *f;
17983 int c, face_id;
17984 XChar2b *char2b;
17985 int multibyte_p, display_p;
17986 {
17987 struct face *face = FACE_FROM_ID (f, face_id);
17988
17989 if (!multibyte_p)
17990 {
17991 /* Unibyte case. We don't have to encode, but we have to make
17992 sure to use a face suitable for unibyte. */
17993 STORE_XCHAR2B (char2b, 0, c);
17994 face_id = FACE_FOR_CHAR (f, face, c);
17995 face = FACE_FROM_ID (f, face_id);
17996 }
17997 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
17998 {
17999 /* Case of ASCII in a face known to fit ASCII. */
18000 STORE_XCHAR2B (char2b, 0, c);
18001 }
18002 else
18003 {
18004 int c1, c2, charset;
18005
18006 /* Split characters into bytes. If c2 is -1 afterwards, C is
18007 really a one-byte character so that byte1 is zero. */
18008 SPLIT_CHAR (c, charset, c1, c2);
18009 if (c2 > 0)
18010 STORE_XCHAR2B (char2b, c1, c2);
18011 else
18012 STORE_XCHAR2B (char2b, 0, c1);
18013
18014 /* Maybe encode the character in *CHAR2B. */
18015 if (face->font != NULL)
18016 {
18017 struct font_info *font_info
18018 = FONT_INFO_FROM_ID (f, face->font_info_id);
18019 if (font_info)
18020 rif->encode_char (c, char2b, font_info, 0);
18021 }
18022 }
18023
18024 /* Make sure X resources of the face are allocated. */
18025 #ifdef HAVE_X_WINDOWS
18026 if (display_p)
18027 #endif
18028 {
18029 xassert (face != NULL);
18030 PREPARE_FACE_FOR_DISPLAY (f, face);
18031 }
18032
18033 return face;
18034 }
18035
18036
18037 /* Set background width of glyph string S. START is the index of the
18038 first glyph following S. LAST_X is the right-most x-position + 1
18039 in the drawing area. */
18040
18041 static INLINE void
18042 set_glyph_string_background_width (s, start, last_x)
18043 struct glyph_string *s;
18044 int start;
18045 int last_x;
18046 {
18047 /* If the face of this glyph string has to be drawn to the end of
18048 the drawing area, set S->extends_to_end_of_line_p. */
18049 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18050
18051 if (start == s->row->used[s->area]
18052 && s->area == TEXT_AREA
18053 && ((s->hl == DRAW_NORMAL_TEXT
18054 && (s->row->fill_line_p
18055 || s->face->background != default_face->background
18056 || s->face->stipple != default_face->stipple
18057 || s->row->mouse_face_p))
18058 || s->hl == DRAW_MOUSE_FACE
18059 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18060 && s->row->fill_line_p)))
18061 s->extends_to_end_of_line_p = 1;
18062
18063 /* If S extends its face to the end of the line, set its
18064 background_width to the distance to the right edge of the drawing
18065 area. */
18066 if (s->extends_to_end_of_line_p)
18067 s->background_width = last_x - s->x + 1;
18068 else
18069 s->background_width = s->width;
18070 }
18071
18072
18073 /* Compute overhangs and x-positions for glyph string S and its
18074 predecessors, or successors. X is the starting x-position for S.
18075 BACKWARD_P non-zero means process predecessors. */
18076
18077 static void
18078 compute_overhangs_and_x (s, x, backward_p)
18079 struct glyph_string *s;
18080 int x;
18081 int backward_p;
18082 {
18083 if (backward_p)
18084 {
18085 while (s)
18086 {
18087 if (rif->compute_glyph_string_overhangs)
18088 rif->compute_glyph_string_overhangs (s);
18089 x -= s->width;
18090 s->x = x;
18091 s = s->prev;
18092 }
18093 }
18094 else
18095 {
18096 while (s)
18097 {
18098 if (rif->compute_glyph_string_overhangs)
18099 rif->compute_glyph_string_overhangs (s);
18100 s->x = x;
18101 x += s->width;
18102 s = s->next;
18103 }
18104 }
18105 }
18106
18107
18108
18109 /* The following macros are only called from draw_glyphs below.
18110 They reference the following parameters of that function directly:
18111 `w', `row', `area', and `overlap_p'
18112 as well as the following local variables:
18113 `s', `f', and `hdc' (in W32) */
18114
18115 #ifdef HAVE_NTGUI
18116 /* On W32, silently add local `hdc' variable to argument list of
18117 init_glyph_string. */
18118 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18119 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18120 #else
18121 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18122 init_glyph_string (s, char2b, w, row, area, start, hl)
18123 #endif
18124
18125 /* Add a glyph string for a stretch glyph to the list of strings
18126 between HEAD and TAIL. START is the index of the stretch glyph in
18127 row area AREA of glyph row ROW. END is the index of the last glyph
18128 in that glyph row area. X is the current output position assigned
18129 to the new glyph string constructed. HL overrides that face of the
18130 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18131 is the right-most x-position of the drawing area. */
18132
18133 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18134 and below -- keep them on one line. */
18135 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18136 do \
18137 { \
18138 s = (struct glyph_string *) alloca (sizeof *s); \
18139 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18140 START = fill_stretch_glyph_string (s, row, area, START, END); \
18141 append_glyph_string (&HEAD, &TAIL, s); \
18142 s->x = (X); \
18143 } \
18144 while (0)
18145
18146
18147 /* Add a glyph string for an image glyph to the list of strings
18148 between HEAD and TAIL. START is the index of the image glyph in
18149 row area AREA of glyph row ROW. END is the index of the last glyph
18150 in that glyph row area. X is the current output position assigned
18151 to the new glyph string constructed. HL overrides that face of the
18152 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18153 is the right-most x-position of the drawing area. */
18154
18155 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18156 do \
18157 { \
18158 s = (struct glyph_string *) alloca (sizeof *s); \
18159 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18160 fill_image_glyph_string (s); \
18161 append_glyph_string (&HEAD, &TAIL, s); \
18162 ++START; \
18163 s->x = (X); \
18164 } \
18165 while (0)
18166
18167
18168 /* Add a glyph string for a sequence of character glyphs to the list
18169 of strings between HEAD and TAIL. START is the index of the first
18170 glyph in row area AREA of glyph row ROW that is part of the new
18171 glyph string. END is the index of the last glyph in that glyph row
18172 area. X is the current output position assigned to the new glyph
18173 string constructed. HL overrides that face of the glyph; e.g. it
18174 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18175 right-most x-position of the drawing area. */
18176
18177 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18178 do \
18179 { \
18180 int c, face_id; \
18181 XChar2b *char2b; \
18182 \
18183 c = (row)->glyphs[area][START].u.ch; \
18184 face_id = (row)->glyphs[area][START].face_id; \
18185 \
18186 s = (struct glyph_string *) alloca (sizeof *s); \
18187 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18188 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18189 append_glyph_string (&HEAD, &TAIL, s); \
18190 s->x = (X); \
18191 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18192 } \
18193 while (0)
18194
18195
18196 /* Add a glyph string for a composite sequence to the list of strings
18197 between HEAD and TAIL. START is the index of the first glyph in
18198 row area AREA of glyph row ROW that is part of the new glyph
18199 string. END is the index of the last glyph in that glyph row area.
18200 X is the current output position assigned to the new glyph string
18201 constructed. HL overrides that face of the glyph; e.g. it is
18202 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18203 x-position of the drawing area. */
18204
18205 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18206 do { \
18207 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18208 int face_id = (row)->glyphs[area][START].face_id; \
18209 struct face *base_face = FACE_FROM_ID (f, face_id); \
18210 struct composition *cmp = composition_table[cmp_id]; \
18211 int glyph_len = cmp->glyph_len; \
18212 XChar2b *char2b; \
18213 struct face **faces; \
18214 struct glyph_string *first_s = NULL; \
18215 int n; \
18216 \
18217 base_face = base_face->ascii_face; \
18218 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18219 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18220 /* At first, fill in `char2b' and `faces'. */ \
18221 for (n = 0; n < glyph_len; n++) \
18222 { \
18223 int c = COMPOSITION_GLYPH (cmp, n); \
18224 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18225 faces[n] = FACE_FROM_ID (f, this_face_id); \
18226 get_char_face_and_encoding (f, c, this_face_id, \
18227 char2b + n, 1, 1); \
18228 } \
18229 \
18230 /* Make glyph_strings for each glyph sequence that is drawable by \
18231 the same face, and append them to HEAD/TAIL. */ \
18232 for (n = 0; n < cmp->glyph_len;) \
18233 { \
18234 s = (struct glyph_string *) alloca (sizeof *s); \
18235 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18236 append_glyph_string (&(HEAD), &(TAIL), s); \
18237 s->cmp = cmp; \
18238 s->gidx = n; \
18239 s->x = (X); \
18240 \
18241 if (n == 0) \
18242 first_s = s; \
18243 \
18244 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18245 } \
18246 \
18247 ++START; \
18248 s = first_s; \
18249 } while (0)
18250
18251
18252 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18253 of AREA of glyph row ROW on window W between indices START and END.
18254 HL overrides the face for drawing glyph strings, e.g. it is
18255 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18256 x-positions of the drawing area.
18257
18258 This is an ugly monster macro construct because we must use alloca
18259 to allocate glyph strings (because draw_glyphs can be called
18260 asynchronously). */
18261
18262 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18263 do \
18264 { \
18265 HEAD = TAIL = NULL; \
18266 while (START < END) \
18267 { \
18268 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18269 switch (first_glyph->type) \
18270 { \
18271 case CHAR_GLYPH: \
18272 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18273 HL, X, LAST_X); \
18274 break; \
18275 \
18276 case COMPOSITE_GLYPH: \
18277 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18278 HL, X, LAST_X); \
18279 break; \
18280 \
18281 case STRETCH_GLYPH: \
18282 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18283 HL, X, LAST_X); \
18284 break; \
18285 \
18286 case IMAGE_GLYPH: \
18287 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18288 HL, X, LAST_X); \
18289 break; \
18290 \
18291 default: \
18292 abort (); \
18293 } \
18294 \
18295 set_glyph_string_background_width (s, START, LAST_X); \
18296 (X) += s->width; \
18297 } \
18298 } \
18299 while (0)
18300
18301
18302 /* Draw glyphs between START and END in AREA of ROW on window W,
18303 starting at x-position X. X is relative to AREA in W. HL is a
18304 face-override with the following meaning:
18305
18306 DRAW_NORMAL_TEXT draw normally
18307 DRAW_CURSOR draw in cursor face
18308 DRAW_MOUSE_FACE draw in mouse face.
18309 DRAW_INVERSE_VIDEO draw in mode line face
18310 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18311 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18312
18313 If OVERLAPS_P is non-zero, draw only the foreground of characters
18314 and clip to the physical height of ROW.
18315
18316 Value is the x-position reached, relative to AREA of W. */
18317
18318 static int
18319 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18320 struct window *w;
18321 int x;
18322 struct glyph_row *row;
18323 enum glyph_row_area area;
18324 int start, end;
18325 enum draw_glyphs_face hl;
18326 int overlaps_p;
18327 {
18328 struct glyph_string *head, *tail;
18329 struct glyph_string *s;
18330 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18331 int last_x, area_width;
18332 int x_reached;
18333 int i, j;
18334 struct frame *f = XFRAME (WINDOW_FRAME (w));
18335 DECLARE_HDC (hdc);
18336
18337 ALLOCATE_HDC (hdc, f);
18338
18339 /* Let's rather be paranoid than getting a SEGV. */
18340 end = min (end, row->used[area]);
18341 start = max (0, start);
18342 start = min (end, start);
18343
18344 /* Translate X to frame coordinates. Set last_x to the right
18345 end of the drawing area. */
18346 if (row->full_width_p)
18347 {
18348 /* X is relative to the left edge of W, without scroll bars
18349 or fringes. */
18350 x += WINDOW_LEFT_EDGE_X (w);
18351 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18352 }
18353 else
18354 {
18355 int area_left = window_box_left (w, area);
18356 x += area_left;
18357 area_width = window_box_width (w, area);
18358 last_x = area_left + area_width;
18359 }
18360
18361 /* Build a doubly-linked list of glyph_string structures between
18362 head and tail from what we have to draw. Note that the macro
18363 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18364 the reason we use a separate variable `i'. */
18365 i = start;
18366 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18367 if (tail)
18368 x_reached = tail->x + tail->background_width;
18369 else
18370 x_reached = x;
18371
18372 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18373 the row, redraw some glyphs in front or following the glyph
18374 strings built above. */
18375 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18376 {
18377 int dummy_x = 0;
18378 struct glyph_string *h, *t;
18379
18380 /* Compute overhangs for all glyph strings. */
18381 if (rif->compute_glyph_string_overhangs)
18382 for (s = head; s; s = s->next)
18383 rif->compute_glyph_string_overhangs (s);
18384
18385 /* Prepend glyph strings for glyphs in front of the first glyph
18386 string that are overwritten because of the first glyph
18387 string's left overhang. The background of all strings
18388 prepended must be drawn because the first glyph string
18389 draws over it. */
18390 i = left_overwritten (head);
18391 if (i >= 0)
18392 {
18393 j = i;
18394 BUILD_GLYPH_STRINGS (j, start, h, t,
18395 DRAW_NORMAL_TEXT, dummy_x, last_x);
18396 start = i;
18397 compute_overhangs_and_x (t, head->x, 1);
18398 prepend_glyph_string_lists (&head, &tail, h, t);
18399 clip_head = head;
18400 }
18401
18402 /* Prepend glyph strings for glyphs in front of the first glyph
18403 string that overwrite that glyph string because of their
18404 right overhang. For these strings, only the foreground must
18405 be drawn, because it draws over the glyph string at `head'.
18406 The background must not be drawn because this would overwrite
18407 right overhangs of preceding glyphs for which no glyph
18408 strings exist. */
18409 i = left_overwriting (head);
18410 if (i >= 0)
18411 {
18412 clip_head = head;
18413 BUILD_GLYPH_STRINGS (i, start, h, t,
18414 DRAW_NORMAL_TEXT, dummy_x, last_x);
18415 for (s = h; s; s = s->next)
18416 s->background_filled_p = 1;
18417 compute_overhangs_and_x (t, head->x, 1);
18418 prepend_glyph_string_lists (&head, &tail, h, t);
18419 }
18420
18421 /* Append glyphs strings for glyphs following the last glyph
18422 string tail that are overwritten by tail. The background of
18423 these strings has to be drawn because tail's foreground draws
18424 over it. */
18425 i = right_overwritten (tail);
18426 if (i >= 0)
18427 {
18428 BUILD_GLYPH_STRINGS (end, i, h, t,
18429 DRAW_NORMAL_TEXT, x, last_x);
18430 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18431 append_glyph_string_lists (&head, &tail, h, t);
18432 clip_tail = tail;
18433 }
18434
18435 /* Append glyph strings for glyphs following the last glyph
18436 string tail that overwrite tail. The foreground of such
18437 glyphs has to be drawn because it writes into the background
18438 of tail. The background must not be drawn because it could
18439 paint over the foreground of following glyphs. */
18440 i = right_overwriting (tail);
18441 if (i >= 0)
18442 {
18443 clip_tail = tail;
18444 BUILD_GLYPH_STRINGS (end, i, h, t,
18445 DRAW_NORMAL_TEXT, x, last_x);
18446 for (s = h; s; s = s->next)
18447 s->background_filled_p = 1;
18448 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18449 append_glyph_string_lists (&head, &tail, h, t);
18450 }
18451 if (clip_head || clip_tail)
18452 for (s = head; s; s = s->next)
18453 {
18454 s->clip_head = clip_head;
18455 s->clip_tail = clip_tail;
18456 }
18457 }
18458
18459 /* Draw all strings. */
18460 for (s = head; s; s = s->next)
18461 rif->draw_glyph_string (s);
18462
18463 if (area == TEXT_AREA
18464 && !row->full_width_p
18465 /* When drawing overlapping rows, only the glyph strings'
18466 foreground is drawn, which doesn't erase a cursor
18467 completely. */
18468 && !overlaps_p)
18469 {
18470 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18471 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18472 : (tail ? tail->x + tail->background_width : x));
18473
18474 int text_left = window_box_left (w, TEXT_AREA);
18475 x0 -= text_left;
18476 x1 -= text_left;
18477
18478 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18479 row->y, MATRIX_ROW_BOTTOM_Y (row));
18480 }
18481
18482 /* Value is the x-position up to which drawn, relative to AREA of W.
18483 This doesn't include parts drawn because of overhangs. */
18484 if (row->full_width_p)
18485 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18486 else
18487 x_reached -= window_box_left (w, area);
18488
18489 RELEASE_HDC (hdc, f);
18490
18491 return x_reached;
18492 }
18493
18494 /* Expand row matrix if too narrow. Don't expand if area
18495 is not present. */
18496
18497 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18498 { \
18499 if (!fonts_changed_p \
18500 && (it->glyph_row->glyphs[area] \
18501 < it->glyph_row->glyphs[area + 1])) \
18502 { \
18503 it->w->ncols_scale_factor++; \
18504 fonts_changed_p = 1; \
18505 } \
18506 }
18507
18508 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18509 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18510
18511 static INLINE void
18512 append_glyph (it)
18513 struct it *it;
18514 {
18515 struct glyph *glyph;
18516 enum glyph_row_area area = it->area;
18517
18518 xassert (it->glyph_row);
18519 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18520
18521 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18522 if (glyph < it->glyph_row->glyphs[area + 1])
18523 {
18524 glyph->charpos = CHARPOS (it->position);
18525 glyph->object = it->object;
18526 glyph->pixel_width = it->pixel_width;
18527 glyph->ascent = it->ascent;
18528 glyph->descent = it->descent;
18529 glyph->voffset = it->voffset;
18530 glyph->type = CHAR_GLYPH;
18531 glyph->multibyte_p = it->multibyte_p;
18532 glyph->left_box_line_p = it->start_of_box_run_p;
18533 glyph->right_box_line_p = it->end_of_box_run_p;
18534 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18535 || it->phys_descent > it->descent);
18536 glyph->padding_p = 0;
18537 glyph->glyph_not_available_p = it->glyph_not_available_p;
18538 glyph->face_id = it->face_id;
18539 glyph->u.ch = it->char_to_display;
18540 glyph->slice = null_glyph_slice;
18541 glyph->font_type = FONT_TYPE_UNKNOWN;
18542 ++it->glyph_row->used[area];
18543 }
18544 else
18545 IT_EXPAND_MATRIX_WIDTH (it, area);
18546 }
18547
18548 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18549 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18550
18551 static INLINE void
18552 append_composite_glyph (it)
18553 struct it *it;
18554 {
18555 struct glyph *glyph;
18556 enum glyph_row_area area = it->area;
18557
18558 xassert (it->glyph_row);
18559
18560 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18561 if (glyph < it->glyph_row->glyphs[area + 1])
18562 {
18563 glyph->charpos = CHARPOS (it->position);
18564 glyph->object = it->object;
18565 glyph->pixel_width = it->pixel_width;
18566 glyph->ascent = it->ascent;
18567 glyph->descent = it->descent;
18568 glyph->voffset = it->voffset;
18569 glyph->type = COMPOSITE_GLYPH;
18570 glyph->multibyte_p = it->multibyte_p;
18571 glyph->left_box_line_p = it->start_of_box_run_p;
18572 glyph->right_box_line_p = it->end_of_box_run_p;
18573 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18574 || it->phys_descent > it->descent);
18575 glyph->padding_p = 0;
18576 glyph->glyph_not_available_p = 0;
18577 glyph->face_id = it->face_id;
18578 glyph->u.cmp_id = it->cmp_id;
18579 glyph->slice = null_glyph_slice;
18580 glyph->font_type = FONT_TYPE_UNKNOWN;
18581 ++it->glyph_row->used[area];
18582 }
18583 else
18584 IT_EXPAND_MATRIX_WIDTH (it, area);
18585 }
18586
18587
18588 /* Change IT->ascent and IT->height according to the setting of
18589 IT->voffset. */
18590
18591 static INLINE void
18592 take_vertical_position_into_account (it)
18593 struct it *it;
18594 {
18595 if (it->voffset)
18596 {
18597 if (it->voffset < 0)
18598 /* Increase the ascent so that we can display the text higher
18599 in the line. */
18600 it->ascent -= it->voffset;
18601 else
18602 /* Increase the descent so that we can display the text lower
18603 in the line. */
18604 it->descent += it->voffset;
18605 }
18606 }
18607
18608
18609 /* Produce glyphs/get display metrics for the image IT is loaded with.
18610 See the description of struct display_iterator in dispextern.h for
18611 an overview of struct display_iterator. */
18612
18613 static void
18614 produce_image_glyph (it)
18615 struct it *it;
18616 {
18617 struct image *img;
18618 struct face *face;
18619 int glyph_ascent;
18620 struct glyph_slice slice;
18621
18622 xassert (it->what == IT_IMAGE);
18623
18624 face = FACE_FROM_ID (it->f, it->face_id);
18625 xassert (face);
18626 /* Make sure X resources of the face is loaded. */
18627 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18628
18629 if (it->image_id < 0)
18630 {
18631 /* Fringe bitmap. */
18632 it->ascent = it->phys_ascent = 0;
18633 it->descent = it->phys_descent = 0;
18634 it->pixel_width = 0;
18635 it->nglyphs = 0;
18636 return;
18637 }
18638
18639 img = IMAGE_FROM_ID (it->f, it->image_id);
18640 xassert (img);
18641 /* Make sure X resources of the image is loaded. */
18642 prepare_image_for_display (it->f, img);
18643
18644 slice.x = slice.y = 0;
18645 slice.width = img->width;
18646 slice.height = img->height;
18647
18648 if (INTEGERP (it->slice.x))
18649 slice.x = XINT (it->slice.x);
18650 else if (FLOATP (it->slice.x))
18651 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18652
18653 if (INTEGERP (it->slice.y))
18654 slice.y = XINT (it->slice.y);
18655 else if (FLOATP (it->slice.y))
18656 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18657
18658 if (INTEGERP (it->slice.width))
18659 slice.width = XINT (it->slice.width);
18660 else if (FLOATP (it->slice.width))
18661 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18662
18663 if (INTEGERP (it->slice.height))
18664 slice.height = XINT (it->slice.height);
18665 else if (FLOATP (it->slice.height))
18666 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18667
18668 if (slice.x >= img->width)
18669 slice.x = img->width;
18670 if (slice.y >= img->height)
18671 slice.y = img->height;
18672 if (slice.x + slice.width >= img->width)
18673 slice.width = img->width - slice.x;
18674 if (slice.y + slice.height > img->height)
18675 slice.height = img->height - slice.y;
18676
18677 if (slice.width == 0 || slice.height == 0)
18678 return;
18679
18680 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18681
18682 it->descent = slice.height - glyph_ascent;
18683 if (slice.y == 0)
18684 it->descent += img->vmargin;
18685 if (slice.y + slice.height == img->height)
18686 it->descent += img->vmargin;
18687 it->phys_descent = it->descent;
18688
18689 it->pixel_width = slice.width;
18690 if (slice.x == 0)
18691 it->pixel_width += img->hmargin;
18692 if (slice.x + slice.width == img->width)
18693 it->pixel_width += img->hmargin;
18694
18695 /* It's quite possible for images to have an ascent greater than
18696 their height, so don't get confused in that case. */
18697 if (it->descent < 0)
18698 it->descent = 0;
18699
18700 #if 0 /* this breaks image tiling */
18701 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18702 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18703 if (face_ascent > it->ascent)
18704 it->ascent = it->phys_ascent = face_ascent;
18705 #endif
18706
18707 it->nglyphs = 1;
18708
18709 if (face->box != FACE_NO_BOX)
18710 {
18711 if (face->box_line_width > 0)
18712 {
18713 if (slice.y == 0)
18714 it->ascent += face->box_line_width;
18715 if (slice.y + slice.height == img->height)
18716 it->descent += face->box_line_width;
18717 }
18718
18719 if (it->start_of_box_run_p && slice.x == 0)
18720 it->pixel_width += abs (face->box_line_width);
18721 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18722 it->pixel_width += abs (face->box_line_width);
18723 }
18724
18725 take_vertical_position_into_account (it);
18726
18727 if (it->glyph_row)
18728 {
18729 struct glyph *glyph;
18730 enum glyph_row_area area = it->area;
18731
18732 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18733 if (glyph < it->glyph_row->glyphs[area + 1])
18734 {
18735 glyph->charpos = CHARPOS (it->position);
18736 glyph->object = it->object;
18737 glyph->pixel_width = it->pixel_width;
18738 glyph->ascent = glyph_ascent;
18739 glyph->descent = it->descent;
18740 glyph->voffset = it->voffset;
18741 glyph->type = IMAGE_GLYPH;
18742 glyph->multibyte_p = it->multibyte_p;
18743 glyph->left_box_line_p = it->start_of_box_run_p;
18744 glyph->right_box_line_p = it->end_of_box_run_p;
18745 glyph->overlaps_vertically_p = 0;
18746 glyph->padding_p = 0;
18747 glyph->glyph_not_available_p = 0;
18748 glyph->face_id = it->face_id;
18749 glyph->u.img_id = img->id;
18750 glyph->slice = slice;
18751 glyph->font_type = FONT_TYPE_UNKNOWN;
18752 ++it->glyph_row->used[area];
18753 }
18754 else
18755 IT_EXPAND_MATRIX_WIDTH (it, area);
18756 }
18757 }
18758
18759
18760 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18761 of the glyph, WIDTH and HEIGHT are the width and height of the
18762 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18763
18764 static void
18765 append_stretch_glyph (it, object, width, height, ascent)
18766 struct it *it;
18767 Lisp_Object object;
18768 int width, height;
18769 int ascent;
18770 {
18771 struct glyph *glyph;
18772 enum glyph_row_area area = it->area;
18773
18774 xassert (ascent >= 0 && ascent <= height);
18775
18776 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18777 if (glyph < it->glyph_row->glyphs[area + 1])
18778 {
18779 glyph->charpos = CHARPOS (it->position);
18780 glyph->object = object;
18781 glyph->pixel_width = width;
18782 glyph->ascent = ascent;
18783 glyph->descent = height - ascent;
18784 glyph->voffset = it->voffset;
18785 glyph->type = STRETCH_GLYPH;
18786 glyph->multibyte_p = it->multibyte_p;
18787 glyph->left_box_line_p = it->start_of_box_run_p;
18788 glyph->right_box_line_p = it->end_of_box_run_p;
18789 glyph->overlaps_vertically_p = 0;
18790 glyph->padding_p = 0;
18791 glyph->glyph_not_available_p = 0;
18792 glyph->face_id = it->face_id;
18793 glyph->u.stretch.ascent = ascent;
18794 glyph->u.stretch.height = height;
18795 glyph->slice = null_glyph_slice;
18796 glyph->font_type = FONT_TYPE_UNKNOWN;
18797 ++it->glyph_row->used[area];
18798 }
18799 else
18800 IT_EXPAND_MATRIX_WIDTH (it, area);
18801 }
18802
18803
18804 /* Produce a stretch glyph for iterator IT. IT->object is the value
18805 of the glyph property displayed. The value must be a list
18806 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18807 being recognized:
18808
18809 1. `:width WIDTH' specifies that the space should be WIDTH *
18810 canonical char width wide. WIDTH may be an integer or floating
18811 point number.
18812
18813 2. `:relative-width FACTOR' specifies that the width of the stretch
18814 should be computed from the width of the first character having the
18815 `glyph' property, and should be FACTOR times that width.
18816
18817 3. `:align-to HPOS' specifies that the space should be wide enough
18818 to reach HPOS, a value in canonical character units.
18819
18820 Exactly one of the above pairs must be present.
18821
18822 4. `:height HEIGHT' specifies that the height of the stretch produced
18823 should be HEIGHT, measured in canonical character units.
18824
18825 5. `:relative-height FACTOR' specifies that the height of the
18826 stretch should be FACTOR times the height of the characters having
18827 the glyph property.
18828
18829 Either none or exactly one of 4 or 5 must be present.
18830
18831 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18832 of the stretch should be used for the ascent of the stretch.
18833 ASCENT must be in the range 0 <= ASCENT <= 100. */
18834
18835 static void
18836 produce_stretch_glyph (it)
18837 struct it *it;
18838 {
18839 /* (space :width WIDTH :height HEIGHT ...) */
18840 Lisp_Object prop, plist;
18841 int width = 0, height = 0, align_to = -1;
18842 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18843 int ascent = 0;
18844 double tem;
18845 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18846 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18847
18848 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18849
18850 /* List should start with `space'. */
18851 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18852 plist = XCDR (it->object);
18853
18854 /* Compute the width of the stretch. */
18855 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18856 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18857 {
18858 /* Absolute width `:width WIDTH' specified and valid. */
18859 zero_width_ok_p = 1;
18860 width = (int)tem;
18861 }
18862 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18863 NUMVAL (prop) > 0)
18864 {
18865 /* Relative width `:relative-width FACTOR' specified and valid.
18866 Compute the width of the characters having the `glyph'
18867 property. */
18868 struct it it2;
18869 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18870
18871 it2 = *it;
18872 if (it->multibyte_p)
18873 {
18874 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18875 - IT_BYTEPOS (*it));
18876 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18877 }
18878 else
18879 it2.c = *p, it2.len = 1;
18880
18881 it2.glyph_row = NULL;
18882 it2.what = IT_CHARACTER;
18883 x_produce_glyphs (&it2);
18884 width = NUMVAL (prop) * it2.pixel_width;
18885 }
18886 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18887 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18888 {
18889 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18890 align_to = (align_to < 0
18891 ? 0
18892 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18893 else if (align_to < 0)
18894 align_to = window_box_left_offset (it->w, TEXT_AREA);
18895 width = max (0, (int)tem + align_to - it->current_x);
18896 zero_width_ok_p = 1;
18897 }
18898 else
18899 /* Nothing specified -> width defaults to canonical char width. */
18900 width = FRAME_COLUMN_WIDTH (it->f);
18901
18902 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18903 width = 1;
18904
18905 /* Compute height. */
18906 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18907 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18908 {
18909 height = (int)tem;
18910 zero_height_ok_p = 1;
18911 }
18912 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18913 NUMVAL (prop) > 0)
18914 height = FONT_HEIGHT (font) * NUMVAL (prop);
18915 else
18916 height = FONT_HEIGHT (font);
18917
18918 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18919 height = 1;
18920
18921 /* Compute percentage of height used for ascent. If
18922 `:ascent ASCENT' is present and valid, use that. Otherwise,
18923 derive the ascent from the font in use. */
18924 if (prop = Fsafe_plist_get (plist, QCascent),
18925 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18926 ascent = height * NUMVAL (prop) / 100.0;
18927 else if (!NILP (prop)
18928 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18929 ascent = min (max (0, (int)tem), height);
18930 else
18931 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18932
18933 if (width > 0 && height > 0 && it->glyph_row)
18934 {
18935 Lisp_Object object = it->stack[it->sp - 1].string;
18936 if (!STRINGP (object))
18937 object = it->w->buffer;
18938 append_stretch_glyph (it, object, width, height, ascent);
18939 }
18940
18941 it->pixel_width = width;
18942 it->ascent = it->phys_ascent = ascent;
18943 it->descent = it->phys_descent = height - it->ascent;
18944 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
18945
18946 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
18947 {
18948 if (face->box_line_width > 0)
18949 {
18950 it->ascent += face->box_line_width;
18951 it->descent += face->box_line_width;
18952 }
18953
18954 if (it->start_of_box_run_p)
18955 it->pixel_width += abs (face->box_line_width);
18956 if (it->end_of_box_run_p)
18957 it->pixel_width += abs (face->box_line_width);
18958 }
18959
18960 take_vertical_position_into_account (it);
18961 }
18962
18963 /* Get line-height and line-spacing property at point.
18964 If line-height has format (HEIGHT TOTAL), return TOTAL
18965 in TOTAL_HEIGHT. */
18966
18967 static Lisp_Object
18968 get_line_height_property (it, prop)
18969 struct it *it;
18970 Lisp_Object prop;
18971 {
18972 Lisp_Object position, val;
18973
18974 if (STRINGP (it->object))
18975 position = make_number (IT_STRING_CHARPOS (*it));
18976 else if (BUFFERP (it->object))
18977 position = make_number (IT_CHARPOS (*it));
18978 else
18979 return Qnil;
18980
18981 return Fget_char_property (position, prop, it->object);
18982 }
18983
18984 /* Calculate line-height and line-spacing properties.
18985 An integer value specifies explicit pixel value.
18986 A float value specifies relative value to current face height.
18987 A cons (float . face-name) specifies relative value to
18988 height of specified face font.
18989
18990 Returns height in pixels, or nil. */
18991
18992
18993 static Lisp_Object
18994 calc_line_height_property (it, val, font, boff, override)
18995 struct it *it;
18996 Lisp_Object val;
18997 XFontStruct *font;
18998 int boff, override;
18999 {
19000 Lisp_Object face_name = Qnil;
19001 int ascent, descent, height;
19002
19003 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19004 return val;
19005
19006 if (CONSP (val))
19007 {
19008 face_name = XCAR (val);
19009 val = XCDR (val);
19010 if (!NUMBERP (val))
19011 val = make_number (1);
19012 if (NILP (face_name))
19013 {
19014 height = it->ascent + it->descent;
19015 goto scale;
19016 }
19017 }
19018
19019 if (NILP (face_name))
19020 {
19021 font = FRAME_FONT (it->f);
19022 boff = FRAME_BASELINE_OFFSET (it->f);
19023 }
19024 else if (EQ (face_name, Qt))
19025 {
19026 override = 0;
19027 }
19028 else
19029 {
19030 int face_id;
19031 struct face *face;
19032 struct font_info *font_info;
19033
19034 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19035 if (face_id < 0)
19036 return make_number (-1);
19037
19038 face = FACE_FROM_ID (it->f, face_id);
19039 font = face->font;
19040 if (font == NULL)
19041 return make_number (-1);
19042
19043 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19044 boff = font_info->baseline_offset;
19045 if (font_info->vertical_centering)
19046 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19047 }
19048
19049 ascent = FONT_BASE (font) + boff;
19050 descent = FONT_DESCENT (font) - boff;
19051
19052 if (override)
19053 {
19054 it->override_ascent = ascent;
19055 it->override_descent = descent;
19056 it->override_boff = boff;
19057 }
19058
19059 height = ascent + descent;
19060
19061 scale:
19062 if (FLOATP (val))
19063 height = (int)(XFLOAT_DATA (val) * height);
19064 else if (INTEGERP (val))
19065 height *= XINT (val);
19066
19067 return make_number (height);
19068 }
19069
19070
19071 /* RIF:
19072 Produce glyphs/get display metrics for the display element IT is
19073 loaded with. See the description of struct display_iterator in
19074 dispextern.h for an overview of struct display_iterator. */
19075
19076 void
19077 x_produce_glyphs (it)
19078 struct it *it;
19079 {
19080 int extra_line_spacing = it->extra_line_spacing;
19081
19082 it->glyph_not_available_p = 0;
19083
19084 if (it->what == IT_CHARACTER)
19085 {
19086 XChar2b char2b;
19087 XFontStruct *font;
19088 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19089 XCharStruct *pcm;
19090 int font_not_found_p;
19091 struct font_info *font_info;
19092 int boff; /* baseline offset */
19093 /* We may change it->multibyte_p upon unibyte<->multibyte
19094 conversion. So, save the current value now and restore it
19095 later.
19096
19097 Note: It seems that we don't have to record multibyte_p in
19098 struct glyph because the character code itself tells if or
19099 not the character is multibyte. Thus, in the future, we must
19100 consider eliminating the field `multibyte_p' in the struct
19101 glyph. */
19102 int saved_multibyte_p = it->multibyte_p;
19103
19104 /* Maybe translate single-byte characters to multibyte, or the
19105 other way. */
19106 it->char_to_display = it->c;
19107 if (!ASCII_BYTE_P (it->c))
19108 {
19109 if (unibyte_display_via_language_environment
19110 && SINGLE_BYTE_CHAR_P (it->c)
19111 && (it->c >= 0240
19112 || !NILP (Vnonascii_translation_table)))
19113 {
19114 it->char_to_display = unibyte_char_to_multibyte (it->c);
19115 it->multibyte_p = 1;
19116 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19117 face = FACE_FROM_ID (it->f, it->face_id);
19118 }
19119 else if (!SINGLE_BYTE_CHAR_P (it->c)
19120 && !it->multibyte_p)
19121 {
19122 it->multibyte_p = 1;
19123 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19124 face = FACE_FROM_ID (it->f, it->face_id);
19125 }
19126 }
19127
19128 /* Get font to use. Encode IT->char_to_display. */
19129 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19130 &char2b, it->multibyte_p, 0);
19131 font = face->font;
19132
19133 /* When no suitable font found, use the default font. */
19134 font_not_found_p = font == NULL;
19135 if (font_not_found_p)
19136 {
19137 font = FRAME_FONT (it->f);
19138 boff = FRAME_BASELINE_OFFSET (it->f);
19139 font_info = NULL;
19140 }
19141 else
19142 {
19143 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19144 boff = font_info->baseline_offset;
19145 if (font_info->vertical_centering)
19146 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19147 }
19148
19149 if (it->char_to_display >= ' '
19150 && (!it->multibyte_p || it->char_to_display < 128))
19151 {
19152 /* Either unibyte or ASCII. */
19153 int stretched_p;
19154
19155 it->nglyphs = 1;
19156
19157 pcm = rif->per_char_metric (font, &char2b,
19158 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19159
19160 if (it->override_ascent >= 0)
19161 {
19162 it->ascent = it->override_ascent;
19163 it->descent = it->override_descent;
19164 boff = it->override_boff;
19165 }
19166 else
19167 {
19168 it->ascent = FONT_BASE (font) + boff;
19169 it->descent = FONT_DESCENT (font) - boff;
19170 }
19171
19172 if (pcm)
19173 {
19174 it->phys_ascent = pcm->ascent + boff;
19175 it->phys_descent = pcm->descent - boff;
19176 it->pixel_width = pcm->width;
19177 }
19178 else
19179 {
19180 it->glyph_not_available_p = 1;
19181 it->phys_ascent = it->ascent;
19182 it->phys_descent = it->descent;
19183 it->pixel_width = FONT_WIDTH (font);
19184 }
19185
19186 if (it->constrain_row_ascent_descent_p)
19187 {
19188 if (it->descent > it->max_descent)
19189 {
19190 it->ascent += it->descent - it->max_descent;
19191 it->descent = it->max_descent;
19192 }
19193 if (it->ascent > it->max_ascent)
19194 {
19195 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19196 it->ascent = it->max_ascent;
19197 }
19198 it->phys_ascent = min (it->phys_ascent, it->ascent);
19199 it->phys_descent = min (it->phys_descent, it->descent);
19200 extra_line_spacing = 0;
19201 }
19202
19203 /* If this is a space inside a region of text with
19204 `space-width' property, change its width. */
19205 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19206 if (stretched_p)
19207 it->pixel_width *= XFLOATINT (it->space_width);
19208
19209 /* If face has a box, add the box thickness to the character
19210 height. If character has a box line to the left and/or
19211 right, add the box line width to the character's width. */
19212 if (face->box != FACE_NO_BOX)
19213 {
19214 int thick = face->box_line_width;
19215
19216 if (thick > 0)
19217 {
19218 it->ascent += thick;
19219 it->descent += thick;
19220 }
19221 else
19222 thick = -thick;
19223
19224 if (it->start_of_box_run_p)
19225 it->pixel_width += thick;
19226 if (it->end_of_box_run_p)
19227 it->pixel_width += thick;
19228 }
19229
19230 /* If face has an overline, add the height of the overline
19231 (1 pixel) and a 1 pixel margin to the character height. */
19232 if (face->overline_p)
19233 it->ascent += 2;
19234
19235 if (it->constrain_row_ascent_descent_p)
19236 {
19237 if (it->ascent > it->max_ascent)
19238 it->ascent = it->max_ascent;
19239 if (it->descent > it->max_descent)
19240 it->descent = it->max_descent;
19241 }
19242
19243 take_vertical_position_into_account (it);
19244
19245 /* If we have to actually produce glyphs, do it. */
19246 if (it->glyph_row)
19247 {
19248 if (stretched_p)
19249 {
19250 /* Translate a space with a `space-width' property
19251 into a stretch glyph. */
19252 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19253 / FONT_HEIGHT (font));
19254 append_stretch_glyph (it, it->object, it->pixel_width,
19255 it->ascent + it->descent, ascent);
19256 }
19257 else
19258 append_glyph (it);
19259
19260 /* If characters with lbearing or rbearing are displayed
19261 in this line, record that fact in a flag of the
19262 glyph row. This is used to optimize X output code. */
19263 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19264 it->glyph_row->contains_overlapping_glyphs_p = 1;
19265 }
19266 }
19267 else if (it->char_to_display == '\n')
19268 {
19269 /* A newline has no width but we need the height of the line.
19270 But if previous part of the line set a height, don't
19271 increase that height */
19272
19273 Lisp_Object height;
19274 Lisp_Object total_height = Qnil;
19275
19276 it->override_ascent = -1;
19277 it->pixel_width = 0;
19278 it->nglyphs = 0;
19279
19280 height = get_line_height_property(it, Qline_height);
19281 /* Split (line-height total-height) list */
19282 if (CONSP (height)
19283 && CONSP (XCDR (height))
19284 && NILP (XCDR (XCDR (height))))
19285 {
19286 total_height = XCAR (XCDR (height));
19287 height = XCAR (height);
19288 }
19289 height = calc_line_height_property(it, height, font, boff, 1);
19290
19291 if (it->override_ascent >= 0)
19292 {
19293 it->ascent = it->override_ascent;
19294 it->descent = it->override_descent;
19295 boff = it->override_boff;
19296 }
19297 else
19298 {
19299 it->ascent = FONT_BASE (font) + boff;
19300 it->descent = FONT_DESCENT (font) - boff;
19301 }
19302
19303 if (EQ (height, Qt))
19304 {
19305 if (it->descent > it->max_descent)
19306 {
19307 it->ascent += it->descent - it->max_descent;
19308 it->descent = it->max_descent;
19309 }
19310 if (it->ascent > it->max_ascent)
19311 {
19312 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19313 it->ascent = it->max_ascent;
19314 }
19315 it->phys_ascent = min (it->phys_ascent, it->ascent);
19316 it->phys_descent = min (it->phys_descent, it->descent);
19317 it->constrain_row_ascent_descent_p = 1;
19318 extra_line_spacing = 0;
19319 }
19320 else
19321 {
19322 Lisp_Object spacing;
19323 int total = 0;
19324
19325 it->phys_ascent = it->ascent;
19326 it->phys_descent = it->descent;
19327
19328 if ((it->max_ascent > 0 || it->max_descent > 0)
19329 && face->box != FACE_NO_BOX
19330 && face->box_line_width > 0)
19331 {
19332 it->ascent += face->box_line_width;
19333 it->descent += face->box_line_width;
19334 }
19335 if (!NILP (height)
19336 && XINT (height) > it->ascent + it->descent)
19337 it->ascent = XINT (height) - it->descent;
19338
19339 if (!NILP (total_height))
19340 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19341 else
19342 {
19343 spacing = get_line_height_property(it, Qline_spacing);
19344 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19345 }
19346 if (INTEGERP (spacing))
19347 {
19348 extra_line_spacing = XINT (spacing);
19349 if (!NILP (total_height))
19350 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19351 }
19352 }
19353 }
19354 else if (it->char_to_display == '\t')
19355 {
19356 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19357 int x = it->current_x + it->continuation_lines_width;
19358 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19359
19360 /* If the distance from the current position to the next tab
19361 stop is less than a space character width, use the
19362 tab stop after that. */
19363 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19364 next_tab_x += tab_width;
19365
19366 it->pixel_width = next_tab_x - x;
19367 it->nglyphs = 1;
19368 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19369 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19370
19371 if (it->glyph_row)
19372 {
19373 append_stretch_glyph (it, it->object, it->pixel_width,
19374 it->ascent + it->descent, it->ascent);
19375 }
19376 }
19377 else
19378 {
19379 /* A multi-byte character. Assume that the display width of the
19380 character is the width of the character multiplied by the
19381 width of the font. */
19382
19383 /* If we found a font, this font should give us the right
19384 metrics. If we didn't find a font, use the frame's
19385 default font and calculate the width of the character
19386 from the charset width; this is what old redisplay code
19387 did. */
19388
19389 pcm = rif->per_char_metric (font, &char2b,
19390 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19391
19392 if (font_not_found_p || !pcm)
19393 {
19394 int charset = CHAR_CHARSET (it->char_to_display);
19395
19396 it->glyph_not_available_p = 1;
19397 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19398 * CHARSET_WIDTH (charset));
19399 it->phys_ascent = FONT_BASE (font) + boff;
19400 it->phys_descent = FONT_DESCENT (font) - boff;
19401 }
19402 else
19403 {
19404 it->pixel_width = pcm->width;
19405 it->phys_ascent = pcm->ascent + boff;
19406 it->phys_descent = pcm->descent - boff;
19407 if (it->glyph_row
19408 && (pcm->lbearing < 0
19409 || pcm->rbearing > pcm->width))
19410 it->glyph_row->contains_overlapping_glyphs_p = 1;
19411 }
19412 it->nglyphs = 1;
19413 it->ascent = FONT_BASE (font) + boff;
19414 it->descent = FONT_DESCENT (font) - boff;
19415 if (face->box != FACE_NO_BOX)
19416 {
19417 int thick = face->box_line_width;
19418
19419 if (thick > 0)
19420 {
19421 it->ascent += thick;
19422 it->descent += thick;
19423 }
19424 else
19425 thick = - thick;
19426
19427 if (it->start_of_box_run_p)
19428 it->pixel_width += thick;
19429 if (it->end_of_box_run_p)
19430 it->pixel_width += thick;
19431 }
19432
19433 /* If face has an overline, add the height of the overline
19434 (1 pixel) and a 1 pixel margin to the character height. */
19435 if (face->overline_p)
19436 it->ascent += 2;
19437
19438 take_vertical_position_into_account (it);
19439
19440 if (it->glyph_row)
19441 append_glyph (it);
19442 }
19443 it->multibyte_p = saved_multibyte_p;
19444 }
19445 else if (it->what == IT_COMPOSITION)
19446 {
19447 /* Note: A composition is represented as one glyph in the
19448 glyph matrix. There are no padding glyphs. */
19449 XChar2b char2b;
19450 XFontStruct *font;
19451 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19452 XCharStruct *pcm;
19453 int font_not_found_p;
19454 struct font_info *font_info;
19455 int boff; /* baseline offset */
19456 struct composition *cmp = composition_table[it->cmp_id];
19457
19458 /* Maybe translate single-byte characters to multibyte. */
19459 it->char_to_display = it->c;
19460 if (unibyte_display_via_language_environment
19461 && SINGLE_BYTE_CHAR_P (it->c)
19462 && (it->c >= 0240
19463 || (it->c >= 0200
19464 && !NILP (Vnonascii_translation_table))))
19465 {
19466 it->char_to_display = unibyte_char_to_multibyte (it->c);
19467 }
19468
19469 /* Get face and font to use. Encode IT->char_to_display. */
19470 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19471 face = FACE_FROM_ID (it->f, it->face_id);
19472 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19473 &char2b, it->multibyte_p, 0);
19474 font = face->font;
19475
19476 /* When no suitable font found, use the default font. */
19477 font_not_found_p = font == NULL;
19478 if (font_not_found_p)
19479 {
19480 font = FRAME_FONT (it->f);
19481 boff = FRAME_BASELINE_OFFSET (it->f);
19482 font_info = NULL;
19483 }
19484 else
19485 {
19486 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19487 boff = font_info->baseline_offset;
19488 if (font_info->vertical_centering)
19489 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19490 }
19491
19492 /* There are no padding glyphs, so there is only one glyph to
19493 produce for the composition. Important is that pixel_width,
19494 ascent and descent are the values of what is drawn by
19495 draw_glyphs (i.e. the values of the overall glyphs composed). */
19496 it->nglyphs = 1;
19497
19498 /* If we have not yet calculated pixel size data of glyphs of
19499 the composition for the current face font, calculate them
19500 now. Theoretically, we have to check all fonts for the
19501 glyphs, but that requires much time and memory space. So,
19502 here we check only the font of the first glyph. This leads
19503 to incorrect display very rarely, and C-l (recenter) can
19504 correct the display anyway. */
19505 if (cmp->font != (void *) font)
19506 {
19507 /* Ascent and descent of the font of the first character of
19508 this composition (adjusted by baseline offset). Ascent
19509 and descent of overall glyphs should not be less than
19510 them respectively. */
19511 int font_ascent = FONT_BASE (font) + boff;
19512 int font_descent = FONT_DESCENT (font) - boff;
19513 /* Bounding box of the overall glyphs. */
19514 int leftmost, rightmost, lowest, highest;
19515 int i, width, ascent, descent;
19516
19517 cmp->font = (void *) font;
19518
19519 /* Initialize the bounding box. */
19520 if (font_info
19521 && (pcm = rif->per_char_metric (font, &char2b,
19522 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19523 {
19524 width = pcm->width;
19525 ascent = pcm->ascent;
19526 descent = pcm->descent;
19527 }
19528 else
19529 {
19530 width = FONT_WIDTH (font);
19531 ascent = FONT_BASE (font);
19532 descent = FONT_DESCENT (font);
19533 }
19534
19535 rightmost = width;
19536 lowest = - descent + boff;
19537 highest = ascent + boff;
19538 leftmost = 0;
19539
19540 if (font_info
19541 && font_info->default_ascent
19542 && CHAR_TABLE_P (Vuse_default_ascent)
19543 && !NILP (Faref (Vuse_default_ascent,
19544 make_number (it->char_to_display))))
19545 highest = font_info->default_ascent + boff;
19546
19547 /* Draw the first glyph at the normal position. It may be
19548 shifted to right later if some other glyphs are drawn at
19549 the left. */
19550 cmp->offsets[0] = 0;
19551 cmp->offsets[1] = boff;
19552
19553 /* Set cmp->offsets for the remaining glyphs. */
19554 for (i = 1; i < cmp->glyph_len; i++)
19555 {
19556 int left, right, btm, top;
19557 int ch = COMPOSITION_GLYPH (cmp, i);
19558 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19559
19560 face = FACE_FROM_ID (it->f, face_id);
19561 get_char_face_and_encoding (it->f, ch, face->id,
19562 &char2b, it->multibyte_p, 0);
19563 font = face->font;
19564 if (font == NULL)
19565 {
19566 font = FRAME_FONT (it->f);
19567 boff = FRAME_BASELINE_OFFSET (it->f);
19568 font_info = NULL;
19569 }
19570 else
19571 {
19572 font_info
19573 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19574 boff = font_info->baseline_offset;
19575 if (font_info->vertical_centering)
19576 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19577 }
19578
19579 if (font_info
19580 && (pcm = rif->per_char_metric (font, &char2b,
19581 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19582 {
19583 width = pcm->width;
19584 ascent = pcm->ascent;
19585 descent = pcm->descent;
19586 }
19587 else
19588 {
19589 width = FONT_WIDTH (font);
19590 ascent = 1;
19591 descent = 0;
19592 }
19593
19594 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19595 {
19596 /* Relative composition with or without
19597 alternate chars. */
19598 left = (leftmost + rightmost - width) / 2;
19599 btm = - descent + boff;
19600 if (font_info && font_info->relative_compose
19601 && (! CHAR_TABLE_P (Vignore_relative_composition)
19602 || NILP (Faref (Vignore_relative_composition,
19603 make_number (ch)))))
19604 {
19605
19606 if (- descent >= font_info->relative_compose)
19607 /* One extra pixel between two glyphs. */
19608 btm = highest + 1;
19609 else if (ascent <= 0)
19610 /* One extra pixel between two glyphs. */
19611 btm = lowest - 1 - ascent - descent;
19612 }
19613 }
19614 else
19615 {
19616 /* A composition rule is specified by an integer
19617 value that encodes global and new reference
19618 points (GREF and NREF). GREF and NREF are
19619 specified by numbers as below:
19620
19621 0---1---2 -- ascent
19622 | |
19623 | |
19624 | |
19625 9--10--11 -- center
19626 | |
19627 ---3---4---5--- baseline
19628 | |
19629 6---7---8 -- descent
19630 */
19631 int rule = COMPOSITION_RULE (cmp, i);
19632 int gref, nref, grefx, grefy, nrefx, nrefy;
19633
19634 COMPOSITION_DECODE_RULE (rule, gref, nref);
19635 grefx = gref % 3, nrefx = nref % 3;
19636 grefy = gref / 3, nrefy = nref / 3;
19637
19638 left = (leftmost
19639 + grefx * (rightmost - leftmost) / 2
19640 - nrefx * width / 2);
19641 btm = ((grefy == 0 ? highest
19642 : grefy == 1 ? 0
19643 : grefy == 2 ? lowest
19644 : (highest + lowest) / 2)
19645 - (nrefy == 0 ? ascent + descent
19646 : nrefy == 1 ? descent - boff
19647 : nrefy == 2 ? 0
19648 : (ascent + descent) / 2));
19649 }
19650
19651 cmp->offsets[i * 2] = left;
19652 cmp->offsets[i * 2 + 1] = btm + descent;
19653
19654 /* Update the bounding box of the overall glyphs. */
19655 right = left + width;
19656 top = btm + descent + ascent;
19657 if (left < leftmost)
19658 leftmost = left;
19659 if (right > rightmost)
19660 rightmost = right;
19661 if (top > highest)
19662 highest = top;
19663 if (btm < lowest)
19664 lowest = btm;
19665 }
19666
19667 /* If there are glyphs whose x-offsets are negative,
19668 shift all glyphs to the right and make all x-offsets
19669 non-negative. */
19670 if (leftmost < 0)
19671 {
19672 for (i = 0; i < cmp->glyph_len; i++)
19673 cmp->offsets[i * 2] -= leftmost;
19674 rightmost -= leftmost;
19675 }
19676
19677 cmp->pixel_width = rightmost;
19678 cmp->ascent = highest;
19679 cmp->descent = - lowest;
19680 if (cmp->ascent < font_ascent)
19681 cmp->ascent = font_ascent;
19682 if (cmp->descent < font_descent)
19683 cmp->descent = font_descent;
19684 }
19685
19686 it->pixel_width = cmp->pixel_width;
19687 it->ascent = it->phys_ascent = cmp->ascent;
19688 it->descent = it->phys_descent = cmp->descent;
19689
19690 if (face->box != FACE_NO_BOX)
19691 {
19692 int thick = face->box_line_width;
19693
19694 if (thick > 0)
19695 {
19696 it->ascent += thick;
19697 it->descent += thick;
19698 }
19699 else
19700 thick = - thick;
19701
19702 if (it->start_of_box_run_p)
19703 it->pixel_width += thick;
19704 if (it->end_of_box_run_p)
19705 it->pixel_width += thick;
19706 }
19707
19708 /* If face has an overline, add the height of the overline
19709 (1 pixel) and a 1 pixel margin to the character height. */
19710 if (face->overline_p)
19711 it->ascent += 2;
19712
19713 take_vertical_position_into_account (it);
19714
19715 if (it->glyph_row)
19716 append_composite_glyph (it);
19717 }
19718 else if (it->what == IT_IMAGE)
19719 produce_image_glyph (it);
19720 else if (it->what == IT_STRETCH)
19721 produce_stretch_glyph (it);
19722
19723 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19724 because this isn't true for images with `:ascent 100'. */
19725 xassert (it->ascent >= 0 && it->descent >= 0);
19726 if (it->area == TEXT_AREA)
19727 it->current_x += it->pixel_width;
19728
19729 if (extra_line_spacing > 0)
19730 {
19731 it->descent += extra_line_spacing;
19732 if (extra_line_spacing > it->max_extra_line_spacing)
19733 it->max_extra_line_spacing = extra_line_spacing;
19734 }
19735
19736 it->max_ascent = max (it->max_ascent, it->ascent);
19737 it->max_descent = max (it->max_descent, it->descent);
19738 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19739 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19740 }
19741
19742 /* EXPORT for RIF:
19743 Output LEN glyphs starting at START at the nominal cursor position.
19744 Advance the nominal cursor over the text. The global variable
19745 updated_window contains the window being updated, updated_row is
19746 the glyph row being updated, and updated_area is the area of that
19747 row being updated. */
19748
19749 void
19750 x_write_glyphs (start, len)
19751 struct glyph *start;
19752 int len;
19753 {
19754 int x, hpos;
19755
19756 xassert (updated_window && updated_row);
19757 BLOCK_INPUT;
19758
19759 /* Write glyphs. */
19760
19761 hpos = start - updated_row->glyphs[updated_area];
19762 x = draw_glyphs (updated_window, output_cursor.x,
19763 updated_row, updated_area,
19764 hpos, hpos + len,
19765 DRAW_NORMAL_TEXT, 0);
19766
19767 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19768 if (updated_area == TEXT_AREA
19769 && updated_window->phys_cursor_on_p
19770 && updated_window->phys_cursor.vpos == output_cursor.vpos
19771 && updated_window->phys_cursor.hpos >= hpos
19772 && updated_window->phys_cursor.hpos < hpos + len)
19773 updated_window->phys_cursor_on_p = 0;
19774
19775 UNBLOCK_INPUT;
19776
19777 /* Advance the output cursor. */
19778 output_cursor.hpos += len;
19779 output_cursor.x = x;
19780 }
19781
19782
19783 /* EXPORT for RIF:
19784 Insert LEN glyphs from START at the nominal cursor position. */
19785
19786 void
19787 x_insert_glyphs (start, len)
19788 struct glyph *start;
19789 int len;
19790 {
19791 struct frame *f;
19792 struct window *w;
19793 int line_height, shift_by_width, shifted_region_width;
19794 struct glyph_row *row;
19795 struct glyph *glyph;
19796 int frame_x, frame_y, hpos;
19797
19798 xassert (updated_window && updated_row);
19799 BLOCK_INPUT;
19800 w = updated_window;
19801 f = XFRAME (WINDOW_FRAME (w));
19802
19803 /* Get the height of the line we are in. */
19804 row = updated_row;
19805 line_height = row->height;
19806
19807 /* Get the width of the glyphs to insert. */
19808 shift_by_width = 0;
19809 for (glyph = start; glyph < start + len; ++glyph)
19810 shift_by_width += glyph->pixel_width;
19811
19812 /* Get the width of the region to shift right. */
19813 shifted_region_width = (window_box_width (w, updated_area)
19814 - output_cursor.x
19815 - shift_by_width);
19816
19817 /* Shift right. */
19818 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19819 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19820
19821 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19822 line_height, shift_by_width);
19823
19824 /* Write the glyphs. */
19825 hpos = start - row->glyphs[updated_area];
19826 draw_glyphs (w, output_cursor.x, row, updated_area,
19827 hpos, hpos + len,
19828 DRAW_NORMAL_TEXT, 0);
19829
19830 /* Advance the output cursor. */
19831 output_cursor.hpos += len;
19832 output_cursor.x += shift_by_width;
19833 UNBLOCK_INPUT;
19834 }
19835
19836
19837 /* EXPORT for RIF:
19838 Erase the current text line from the nominal cursor position
19839 (inclusive) to pixel column TO_X (exclusive). The idea is that
19840 everything from TO_X onward is already erased.
19841
19842 TO_X is a pixel position relative to updated_area of
19843 updated_window. TO_X == -1 means clear to the end of this area. */
19844
19845 void
19846 x_clear_end_of_line (to_x)
19847 int to_x;
19848 {
19849 struct frame *f;
19850 struct window *w = updated_window;
19851 int max_x, min_y, max_y;
19852 int from_x, from_y, to_y;
19853
19854 xassert (updated_window && updated_row);
19855 f = XFRAME (w->frame);
19856
19857 if (updated_row->full_width_p)
19858 max_x = WINDOW_TOTAL_WIDTH (w);
19859 else
19860 max_x = window_box_width (w, updated_area);
19861 max_y = window_text_bottom_y (w);
19862
19863 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19864 of window. For TO_X > 0, truncate to end of drawing area. */
19865 if (to_x == 0)
19866 return;
19867 else if (to_x < 0)
19868 to_x = max_x;
19869 else
19870 to_x = min (to_x, max_x);
19871
19872 to_y = min (max_y, output_cursor.y + updated_row->height);
19873
19874 /* Notice if the cursor will be cleared by this operation. */
19875 if (!updated_row->full_width_p)
19876 notice_overwritten_cursor (w, updated_area,
19877 output_cursor.x, -1,
19878 updated_row->y,
19879 MATRIX_ROW_BOTTOM_Y (updated_row));
19880
19881 from_x = output_cursor.x;
19882
19883 /* Translate to frame coordinates. */
19884 if (updated_row->full_width_p)
19885 {
19886 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19887 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19888 }
19889 else
19890 {
19891 int area_left = window_box_left (w, updated_area);
19892 from_x += area_left;
19893 to_x += area_left;
19894 }
19895
19896 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19897 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19898 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19899
19900 /* Prevent inadvertently clearing to end of the X window. */
19901 if (to_x > from_x && to_y > from_y)
19902 {
19903 BLOCK_INPUT;
19904 rif->clear_frame_area (f, from_x, from_y,
19905 to_x - from_x, to_y - from_y);
19906 UNBLOCK_INPUT;
19907 }
19908 }
19909
19910 #endif /* HAVE_WINDOW_SYSTEM */
19911
19912
19913 \f
19914 /***********************************************************************
19915 Cursor types
19916 ***********************************************************************/
19917
19918 /* Value is the internal representation of the specified cursor type
19919 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19920 of the bar cursor. */
19921
19922 static enum text_cursor_kinds
19923 get_specified_cursor_type (arg, width)
19924 Lisp_Object arg;
19925 int *width;
19926 {
19927 enum text_cursor_kinds type;
19928
19929 if (NILP (arg))
19930 return NO_CURSOR;
19931
19932 if (EQ (arg, Qbox))
19933 return FILLED_BOX_CURSOR;
19934
19935 if (EQ (arg, Qhollow))
19936 return HOLLOW_BOX_CURSOR;
19937
19938 if (EQ (arg, Qbar))
19939 {
19940 *width = 2;
19941 return BAR_CURSOR;
19942 }
19943
19944 if (CONSP (arg)
19945 && EQ (XCAR (arg), Qbar)
19946 && INTEGERP (XCDR (arg))
19947 && XINT (XCDR (arg)) >= 0)
19948 {
19949 *width = XINT (XCDR (arg));
19950 return BAR_CURSOR;
19951 }
19952
19953 if (EQ (arg, Qhbar))
19954 {
19955 *width = 2;
19956 return HBAR_CURSOR;
19957 }
19958
19959 if (CONSP (arg)
19960 && EQ (XCAR (arg), Qhbar)
19961 && INTEGERP (XCDR (arg))
19962 && XINT (XCDR (arg)) >= 0)
19963 {
19964 *width = XINT (XCDR (arg));
19965 return HBAR_CURSOR;
19966 }
19967
19968 /* Treat anything unknown as "hollow box cursor".
19969 It was bad to signal an error; people have trouble fixing
19970 .Xdefaults with Emacs, when it has something bad in it. */
19971 type = HOLLOW_BOX_CURSOR;
19972
19973 return type;
19974 }
19975
19976 /* Set the default cursor types for specified frame. */
19977 void
19978 set_frame_cursor_types (f, arg)
19979 struct frame *f;
19980 Lisp_Object arg;
19981 {
19982 int width;
19983 Lisp_Object tem;
19984
19985 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
19986 FRAME_CURSOR_WIDTH (f) = width;
19987
19988 /* By default, set up the blink-off state depending on the on-state. */
19989
19990 tem = Fassoc (arg, Vblink_cursor_alist);
19991 if (!NILP (tem))
19992 {
19993 FRAME_BLINK_OFF_CURSOR (f)
19994 = get_specified_cursor_type (XCDR (tem), &width);
19995 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
19996 }
19997 else
19998 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
19999 }
20000
20001
20002 /* Return the cursor we want to be displayed in window W. Return
20003 width of bar/hbar cursor through WIDTH arg. Return with
20004 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20005 (i.e. if the `system caret' should track this cursor).
20006
20007 In a mini-buffer window, we want the cursor only to appear if we
20008 are reading input from this window. For the selected window, we
20009 want the cursor type given by the frame parameter or buffer local
20010 setting of cursor-type. If explicitly marked off, draw no cursor.
20011 In all other cases, we want a hollow box cursor. */
20012
20013 static enum text_cursor_kinds
20014 get_window_cursor_type (w, glyph, width, active_cursor)
20015 struct window *w;
20016 struct glyph *glyph;
20017 int *width;
20018 int *active_cursor;
20019 {
20020 struct frame *f = XFRAME (w->frame);
20021 struct buffer *b = XBUFFER (w->buffer);
20022 int cursor_type = DEFAULT_CURSOR;
20023 Lisp_Object alt_cursor;
20024 int non_selected = 0;
20025
20026 *active_cursor = 1;
20027
20028 /* Echo area */
20029 if (cursor_in_echo_area
20030 && FRAME_HAS_MINIBUF_P (f)
20031 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20032 {
20033 if (w == XWINDOW (echo_area_window))
20034 {
20035 *width = FRAME_CURSOR_WIDTH (f);
20036 return FRAME_DESIRED_CURSOR (f);
20037 }
20038
20039 *active_cursor = 0;
20040 non_selected = 1;
20041 }
20042
20043 /* Nonselected window or nonselected frame. */
20044 else if (w != XWINDOW (f->selected_window)
20045 #ifdef HAVE_WINDOW_SYSTEM
20046 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20047 #endif
20048 )
20049 {
20050 *active_cursor = 0;
20051
20052 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20053 return NO_CURSOR;
20054
20055 non_selected = 1;
20056 }
20057
20058 /* Never display a cursor in a window in which cursor-type is nil. */
20059 if (NILP (b->cursor_type))
20060 return NO_CURSOR;
20061
20062 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20063 if (non_selected)
20064 {
20065 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
20066 return get_specified_cursor_type (alt_cursor, width);
20067 }
20068
20069 /* Get the normal cursor type for this window. */
20070 if (EQ (b->cursor_type, Qt))
20071 {
20072 cursor_type = FRAME_DESIRED_CURSOR (f);
20073 *width = FRAME_CURSOR_WIDTH (f);
20074 }
20075 else
20076 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20077
20078 /* Use normal cursor if not blinked off. */
20079 if (!w->cursor_off_p)
20080 {
20081 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20082 if (cursor_type == FILLED_BOX_CURSOR)
20083 cursor_type = HOLLOW_BOX_CURSOR;
20084 }
20085 return cursor_type;
20086 }
20087
20088 /* Cursor is blinked off, so determine how to "toggle" it. */
20089
20090 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20091 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20092 return get_specified_cursor_type (XCDR (alt_cursor), width);
20093
20094 /* Then see if frame has specified a specific blink off cursor type. */
20095 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20096 {
20097 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20098 return FRAME_BLINK_OFF_CURSOR (f);
20099 }
20100
20101 #if 0
20102 /* Some people liked having a permanently visible blinking cursor,
20103 while others had very strong opinions against it. So it was
20104 decided to remove it. KFS 2003-09-03 */
20105
20106 /* Finally perform built-in cursor blinking:
20107 filled box <-> hollow box
20108 wide [h]bar <-> narrow [h]bar
20109 narrow [h]bar <-> no cursor
20110 other type <-> no cursor */
20111
20112 if (cursor_type == FILLED_BOX_CURSOR)
20113 return HOLLOW_BOX_CURSOR;
20114
20115 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20116 {
20117 *width = 1;
20118 return cursor_type;
20119 }
20120 #endif
20121
20122 return NO_CURSOR;
20123 }
20124
20125
20126 #ifdef HAVE_WINDOW_SYSTEM
20127
20128 /* Notice when the text cursor of window W has been completely
20129 overwritten by a drawing operation that outputs glyphs in AREA
20130 starting at X0 and ending at X1 in the line starting at Y0 and
20131 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20132 the rest of the line after X0 has been written. Y coordinates
20133 are window-relative. */
20134
20135 static void
20136 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20137 struct window *w;
20138 enum glyph_row_area area;
20139 int x0, y0, x1, y1;
20140 {
20141 int cx0, cx1, cy0, cy1;
20142 struct glyph_row *row;
20143
20144 if (!w->phys_cursor_on_p)
20145 return;
20146 if (area != TEXT_AREA)
20147 return;
20148
20149 row = w->current_matrix->rows + w->phys_cursor.vpos;
20150 if (!row->displays_text_p)
20151 return;
20152
20153 if (row->cursor_in_fringe_p)
20154 {
20155 row->cursor_in_fringe_p = 0;
20156 draw_fringe_bitmap (w, row, 0);
20157 w->phys_cursor_on_p = 0;
20158 return;
20159 }
20160
20161 cx0 = w->phys_cursor.x;
20162 cx1 = cx0 + w->phys_cursor_width;
20163 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20164 return;
20165
20166 /* The cursor image will be completely removed from the
20167 screen if the output area intersects the cursor area in
20168 y-direction. When we draw in [y0 y1[, and some part of
20169 the cursor is at y < y0, that part must have been drawn
20170 before. When scrolling, the cursor is erased before
20171 actually scrolling, so we don't come here. When not
20172 scrolling, the rows above the old cursor row must have
20173 changed, and in this case these rows must have written
20174 over the cursor image.
20175
20176 Likewise if part of the cursor is below y1, with the
20177 exception of the cursor being in the first blank row at
20178 the buffer and window end because update_text_area
20179 doesn't draw that row. (Except when it does, but
20180 that's handled in update_text_area.) */
20181
20182 cy0 = w->phys_cursor.y;
20183 cy1 = cy0 + w->phys_cursor_height;
20184 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20185 return;
20186
20187 w->phys_cursor_on_p = 0;
20188 }
20189
20190 #endif /* HAVE_WINDOW_SYSTEM */
20191
20192 \f
20193 /************************************************************************
20194 Mouse Face
20195 ************************************************************************/
20196
20197 #ifdef HAVE_WINDOW_SYSTEM
20198
20199 /* EXPORT for RIF:
20200 Fix the display of area AREA of overlapping row ROW in window W. */
20201
20202 void
20203 x_fix_overlapping_area (w, row, area)
20204 struct window *w;
20205 struct glyph_row *row;
20206 enum glyph_row_area area;
20207 {
20208 int i, x;
20209
20210 BLOCK_INPUT;
20211
20212 x = 0;
20213 for (i = 0; i < row->used[area];)
20214 {
20215 if (row->glyphs[area][i].overlaps_vertically_p)
20216 {
20217 int start = i, start_x = x;
20218
20219 do
20220 {
20221 x += row->glyphs[area][i].pixel_width;
20222 ++i;
20223 }
20224 while (i < row->used[area]
20225 && row->glyphs[area][i].overlaps_vertically_p);
20226
20227 draw_glyphs (w, start_x, row, area,
20228 start, i,
20229 DRAW_NORMAL_TEXT, 1);
20230 }
20231 else
20232 {
20233 x += row->glyphs[area][i].pixel_width;
20234 ++i;
20235 }
20236 }
20237
20238 UNBLOCK_INPUT;
20239 }
20240
20241
20242 /* EXPORT:
20243 Draw the cursor glyph of window W in glyph row ROW. See the
20244 comment of draw_glyphs for the meaning of HL. */
20245
20246 void
20247 draw_phys_cursor_glyph (w, row, hl)
20248 struct window *w;
20249 struct glyph_row *row;
20250 enum draw_glyphs_face hl;
20251 {
20252 /* If cursor hpos is out of bounds, don't draw garbage. This can
20253 happen in mini-buffer windows when switching between echo area
20254 glyphs and mini-buffer. */
20255 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20256 {
20257 int on_p = w->phys_cursor_on_p;
20258 int x1;
20259 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20260 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20261 hl, 0);
20262 w->phys_cursor_on_p = on_p;
20263
20264 if (hl == DRAW_CURSOR)
20265 w->phys_cursor_width = x1 - w->phys_cursor.x;
20266 /* When we erase the cursor, and ROW is overlapped by other
20267 rows, make sure that these overlapping parts of other rows
20268 are redrawn. */
20269 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20270 {
20271 if (row > w->current_matrix->rows
20272 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20273 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20274
20275 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20276 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20277 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20278 }
20279 }
20280 }
20281
20282
20283 /* EXPORT:
20284 Erase the image of a cursor of window W from the screen. */
20285
20286 void
20287 erase_phys_cursor (w)
20288 struct window *w;
20289 {
20290 struct frame *f = XFRAME (w->frame);
20291 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20292 int hpos = w->phys_cursor.hpos;
20293 int vpos = w->phys_cursor.vpos;
20294 int mouse_face_here_p = 0;
20295 struct glyph_matrix *active_glyphs = w->current_matrix;
20296 struct glyph_row *cursor_row;
20297 struct glyph *cursor_glyph;
20298 enum draw_glyphs_face hl;
20299
20300 /* No cursor displayed or row invalidated => nothing to do on the
20301 screen. */
20302 if (w->phys_cursor_type == NO_CURSOR)
20303 goto mark_cursor_off;
20304
20305 /* VPOS >= active_glyphs->nrows means that window has been resized.
20306 Don't bother to erase the cursor. */
20307 if (vpos >= active_glyphs->nrows)
20308 goto mark_cursor_off;
20309
20310 /* If row containing cursor is marked invalid, there is nothing we
20311 can do. */
20312 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20313 if (!cursor_row->enabled_p)
20314 goto mark_cursor_off;
20315
20316 /* If line spacing is > 0, old cursor may only be partially visible in
20317 window after split-window. So adjust visible height. */
20318 cursor_row->visible_height = min (cursor_row->visible_height,
20319 window_text_bottom_y (w) - cursor_row->y);
20320
20321 /* If row is completely invisible, don't attempt to delete a cursor which
20322 isn't there. This can happen if cursor is at top of a window, and
20323 we switch to a buffer with a header line in that window. */
20324 if (cursor_row->visible_height <= 0)
20325 goto mark_cursor_off;
20326
20327 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20328 if (cursor_row->cursor_in_fringe_p)
20329 {
20330 cursor_row->cursor_in_fringe_p = 0;
20331 draw_fringe_bitmap (w, cursor_row, 0);
20332 goto mark_cursor_off;
20333 }
20334
20335 /* This can happen when the new row is shorter than the old one.
20336 In this case, either draw_glyphs or clear_end_of_line
20337 should have cleared the cursor. Note that we wouldn't be
20338 able to erase the cursor in this case because we don't have a
20339 cursor glyph at hand. */
20340 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20341 goto mark_cursor_off;
20342
20343 /* If the cursor is in the mouse face area, redisplay that when
20344 we clear the cursor. */
20345 if (! NILP (dpyinfo->mouse_face_window)
20346 && w == XWINDOW (dpyinfo->mouse_face_window)
20347 && (vpos > dpyinfo->mouse_face_beg_row
20348 || (vpos == dpyinfo->mouse_face_beg_row
20349 && hpos >= dpyinfo->mouse_face_beg_col))
20350 && (vpos < dpyinfo->mouse_face_end_row
20351 || (vpos == dpyinfo->mouse_face_end_row
20352 && hpos < dpyinfo->mouse_face_end_col))
20353 /* Don't redraw the cursor's spot in mouse face if it is at the
20354 end of a line (on a newline). The cursor appears there, but
20355 mouse highlighting does not. */
20356 && cursor_row->used[TEXT_AREA] > hpos)
20357 mouse_face_here_p = 1;
20358
20359 /* Maybe clear the display under the cursor. */
20360 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20361 {
20362 int x, y;
20363 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20364 int width;
20365
20366 cursor_glyph = get_phys_cursor_glyph (w);
20367 if (cursor_glyph == NULL)
20368 goto mark_cursor_off;
20369
20370 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20371 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20372 width = min (cursor_glyph->pixel_width,
20373 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20374
20375 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20376 }
20377
20378 /* Erase the cursor by redrawing the character underneath it. */
20379 if (mouse_face_here_p)
20380 hl = DRAW_MOUSE_FACE;
20381 else
20382 hl = DRAW_NORMAL_TEXT;
20383 draw_phys_cursor_glyph (w, cursor_row, hl);
20384
20385 mark_cursor_off:
20386 w->phys_cursor_on_p = 0;
20387 w->phys_cursor_type = NO_CURSOR;
20388 }
20389
20390
20391 /* EXPORT:
20392 Display or clear cursor of window W. If ON is zero, clear the
20393 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20394 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20395
20396 void
20397 display_and_set_cursor (w, on, hpos, vpos, x, y)
20398 struct window *w;
20399 int on, hpos, vpos, x, y;
20400 {
20401 struct frame *f = XFRAME (w->frame);
20402 int new_cursor_type;
20403 int new_cursor_width;
20404 int active_cursor;
20405 struct glyph_row *glyph_row;
20406 struct glyph *glyph;
20407
20408 /* This is pointless on invisible frames, and dangerous on garbaged
20409 windows and frames; in the latter case, the frame or window may
20410 be in the midst of changing its size, and x and y may be off the
20411 window. */
20412 if (! FRAME_VISIBLE_P (f)
20413 || FRAME_GARBAGED_P (f)
20414 || vpos >= w->current_matrix->nrows
20415 || hpos >= w->current_matrix->matrix_w)
20416 return;
20417
20418 /* If cursor is off and we want it off, return quickly. */
20419 if (!on && !w->phys_cursor_on_p)
20420 return;
20421
20422 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20423 /* If cursor row is not enabled, we don't really know where to
20424 display the cursor. */
20425 if (!glyph_row->enabled_p)
20426 {
20427 w->phys_cursor_on_p = 0;
20428 return;
20429 }
20430
20431 glyph = NULL;
20432 if (!glyph_row->exact_window_width_line_p
20433 || hpos < glyph_row->used[TEXT_AREA])
20434 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20435
20436 xassert (interrupt_input_blocked);
20437
20438 /* Set new_cursor_type to the cursor we want to be displayed. */
20439 new_cursor_type = get_window_cursor_type (w, glyph,
20440 &new_cursor_width, &active_cursor);
20441
20442 /* If cursor is currently being shown and we don't want it to be or
20443 it is in the wrong place, or the cursor type is not what we want,
20444 erase it. */
20445 if (w->phys_cursor_on_p
20446 && (!on
20447 || w->phys_cursor.x != x
20448 || w->phys_cursor.y != y
20449 || new_cursor_type != w->phys_cursor_type
20450 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20451 && new_cursor_width != w->phys_cursor_width)))
20452 erase_phys_cursor (w);
20453
20454 /* Don't check phys_cursor_on_p here because that flag is only set
20455 to zero in some cases where we know that the cursor has been
20456 completely erased, to avoid the extra work of erasing the cursor
20457 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20458 still not be visible, or it has only been partly erased. */
20459 if (on)
20460 {
20461 w->phys_cursor_ascent = glyph_row->ascent;
20462 w->phys_cursor_height = glyph_row->height;
20463
20464 /* Set phys_cursor_.* before x_draw_.* is called because some
20465 of them may need the information. */
20466 w->phys_cursor.x = x;
20467 w->phys_cursor.y = glyph_row->y;
20468 w->phys_cursor.hpos = hpos;
20469 w->phys_cursor.vpos = vpos;
20470 }
20471
20472 rif->draw_window_cursor (w, glyph_row, x, y,
20473 new_cursor_type, new_cursor_width,
20474 on, active_cursor);
20475 }
20476
20477
20478 /* Switch the display of W's cursor on or off, according to the value
20479 of ON. */
20480
20481 static void
20482 update_window_cursor (w, on)
20483 struct window *w;
20484 int on;
20485 {
20486 /* Don't update cursor in windows whose frame is in the process
20487 of being deleted. */
20488 if (w->current_matrix)
20489 {
20490 BLOCK_INPUT;
20491 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20492 w->phys_cursor.x, w->phys_cursor.y);
20493 UNBLOCK_INPUT;
20494 }
20495 }
20496
20497
20498 /* Call update_window_cursor with parameter ON_P on all leaf windows
20499 in the window tree rooted at W. */
20500
20501 static void
20502 update_cursor_in_window_tree (w, on_p)
20503 struct window *w;
20504 int on_p;
20505 {
20506 while (w)
20507 {
20508 if (!NILP (w->hchild))
20509 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20510 else if (!NILP (w->vchild))
20511 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20512 else
20513 update_window_cursor (w, on_p);
20514
20515 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20516 }
20517 }
20518
20519
20520 /* EXPORT:
20521 Display the cursor on window W, or clear it, according to ON_P.
20522 Don't change the cursor's position. */
20523
20524 void
20525 x_update_cursor (f, on_p)
20526 struct frame *f;
20527 int on_p;
20528 {
20529 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20530 }
20531
20532
20533 /* EXPORT:
20534 Clear the cursor of window W to background color, and mark the
20535 cursor as not shown. This is used when the text where the cursor
20536 is is about to be rewritten. */
20537
20538 void
20539 x_clear_cursor (w)
20540 struct window *w;
20541 {
20542 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20543 update_window_cursor (w, 0);
20544 }
20545
20546
20547 /* EXPORT:
20548 Display the active region described by mouse_face_* according to DRAW. */
20549
20550 void
20551 show_mouse_face (dpyinfo, draw)
20552 Display_Info *dpyinfo;
20553 enum draw_glyphs_face draw;
20554 {
20555 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20556 struct frame *f = XFRAME (WINDOW_FRAME (w));
20557
20558 if (/* If window is in the process of being destroyed, don't bother
20559 to do anything. */
20560 w->current_matrix != NULL
20561 /* Don't update mouse highlight if hidden */
20562 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20563 /* Recognize when we are called to operate on rows that don't exist
20564 anymore. This can happen when a window is split. */
20565 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20566 {
20567 int phys_cursor_on_p = w->phys_cursor_on_p;
20568 struct glyph_row *row, *first, *last;
20569
20570 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20571 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20572
20573 for (row = first; row <= last && row->enabled_p; ++row)
20574 {
20575 int start_hpos, end_hpos, start_x;
20576
20577 /* For all but the first row, the highlight starts at column 0. */
20578 if (row == first)
20579 {
20580 start_hpos = dpyinfo->mouse_face_beg_col;
20581 start_x = dpyinfo->mouse_face_beg_x;
20582 }
20583 else
20584 {
20585 start_hpos = 0;
20586 start_x = 0;
20587 }
20588
20589 if (row == last)
20590 end_hpos = dpyinfo->mouse_face_end_col;
20591 else
20592 end_hpos = row->used[TEXT_AREA];
20593
20594 if (end_hpos > start_hpos)
20595 {
20596 draw_glyphs (w, start_x, row, TEXT_AREA,
20597 start_hpos, end_hpos,
20598 draw, 0);
20599
20600 row->mouse_face_p
20601 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20602 }
20603 }
20604
20605 /* When we've written over the cursor, arrange for it to
20606 be displayed again. */
20607 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20608 {
20609 BLOCK_INPUT;
20610 display_and_set_cursor (w, 1,
20611 w->phys_cursor.hpos, w->phys_cursor.vpos,
20612 w->phys_cursor.x, w->phys_cursor.y);
20613 UNBLOCK_INPUT;
20614 }
20615 }
20616
20617 /* Change the mouse cursor. */
20618 if (draw == DRAW_NORMAL_TEXT)
20619 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20620 else if (draw == DRAW_MOUSE_FACE)
20621 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20622 else
20623 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20624 }
20625
20626 /* EXPORT:
20627 Clear out the mouse-highlighted active region.
20628 Redraw it un-highlighted first. Value is non-zero if mouse
20629 face was actually drawn unhighlighted. */
20630
20631 int
20632 clear_mouse_face (dpyinfo)
20633 Display_Info *dpyinfo;
20634 {
20635 int cleared = 0;
20636
20637 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20638 {
20639 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20640 cleared = 1;
20641 }
20642
20643 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20644 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20645 dpyinfo->mouse_face_window = Qnil;
20646 dpyinfo->mouse_face_overlay = Qnil;
20647 return cleared;
20648 }
20649
20650
20651 /* EXPORT:
20652 Non-zero if physical cursor of window W is within mouse face. */
20653
20654 int
20655 cursor_in_mouse_face_p (w)
20656 struct window *w;
20657 {
20658 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20659 int in_mouse_face = 0;
20660
20661 if (WINDOWP (dpyinfo->mouse_face_window)
20662 && XWINDOW (dpyinfo->mouse_face_window) == w)
20663 {
20664 int hpos = w->phys_cursor.hpos;
20665 int vpos = w->phys_cursor.vpos;
20666
20667 if (vpos >= dpyinfo->mouse_face_beg_row
20668 && vpos <= dpyinfo->mouse_face_end_row
20669 && (vpos > dpyinfo->mouse_face_beg_row
20670 || hpos >= dpyinfo->mouse_face_beg_col)
20671 && (vpos < dpyinfo->mouse_face_end_row
20672 || hpos < dpyinfo->mouse_face_end_col
20673 || dpyinfo->mouse_face_past_end))
20674 in_mouse_face = 1;
20675 }
20676
20677 return in_mouse_face;
20678 }
20679
20680
20681
20682 \f
20683 /* Find the glyph matrix position of buffer position CHARPOS in window
20684 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20685 current glyphs must be up to date. If CHARPOS is above window
20686 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20687 of last line in W. In the row containing CHARPOS, stop before glyphs
20688 having STOP as object. */
20689
20690 #if 1 /* This is a version of fast_find_position that's more correct
20691 in the presence of hscrolling, for example. I didn't install
20692 it right away because the problem fixed is minor, it failed
20693 in 20.x as well, and I think it's too risky to install
20694 so near the release of 21.1. 2001-09-25 gerd. */
20695
20696 static int
20697 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20698 struct window *w;
20699 int charpos;
20700 int *hpos, *vpos, *x, *y;
20701 Lisp_Object stop;
20702 {
20703 struct glyph_row *row, *first;
20704 struct glyph *glyph, *end;
20705 int past_end = 0;
20706
20707 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20708 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20709 {
20710 *x = first->x;
20711 *y = first->y;
20712 *hpos = 0;
20713 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20714 return 1;
20715 }
20716
20717 row = row_containing_pos (w, charpos, first, NULL, 0);
20718 if (row == NULL)
20719 {
20720 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20721 past_end = 1;
20722 }
20723
20724 /* If whole rows or last part of a row came from a display overlay,
20725 row_containing_pos will skip over such rows because their end pos
20726 equals the start pos of the overlay or interval. Backtrack if we
20727 have a STOP object and previous row's end glyph came from STOP. */
20728 if (!NILP (stop))
20729 {
20730 struct glyph_row *prev = row-1;
20731 while ((prev = row - 1, prev >= first)
20732 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20733 && prev->used[TEXT_AREA] > 0)
20734 {
20735 end = prev->glyphs[TEXT_AREA];
20736 glyph = end + prev->used[TEXT_AREA];
20737 while (--glyph >= end
20738 && INTEGERP (glyph->object));
20739 if (glyph >= end
20740 && !EQ (stop, glyph->object))
20741 break;
20742 row = prev;
20743 }
20744 }
20745
20746 *x = row->x;
20747 *y = row->y;
20748 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20749
20750 glyph = row->glyphs[TEXT_AREA];
20751 end = glyph + row->used[TEXT_AREA];
20752
20753 /* Skip over glyphs not having an object at the start of the row.
20754 These are special glyphs like truncation marks on terminal
20755 frames. */
20756 if (row->displays_text_p)
20757 while (glyph < end
20758 && INTEGERP (glyph->object)
20759 && !EQ (stop, glyph->object)
20760 && glyph->charpos < 0)
20761 {
20762 *x += glyph->pixel_width;
20763 ++glyph;
20764 }
20765
20766 while (glyph < end
20767 && !INTEGERP (glyph->object)
20768 && !EQ (stop, glyph->object)
20769 && (!BUFFERP (glyph->object)
20770 || glyph->charpos < charpos))
20771 {
20772 *x += glyph->pixel_width;
20773 ++glyph;
20774 }
20775
20776 *hpos = glyph - row->glyphs[TEXT_AREA];
20777 return !past_end;
20778 }
20779
20780 #else /* not 1 */
20781
20782 static int
20783 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20784 struct window *w;
20785 int pos;
20786 int *hpos, *vpos, *x, *y;
20787 Lisp_Object stop;
20788 {
20789 int i;
20790 int lastcol;
20791 int maybe_next_line_p = 0;
20792 int line_start_position;
20793 int yb = window_text_bottom_y (w);
20794 struct glyph_row *row, *best_row;
20795 int row_vpos, best_row_vpos;
20796 int current_x;
20797
20798 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20799 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20800
20801 while (row->y < yb)
20802 {
20803 if (row->used[TEXT_AREA])
20804 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20805 else
20806 line_start_position = 0;
20807
20808 if (line_start_position > pos)
20809 break;
20810 /* If the position sought is the end of the buffer,
20811 don't include the blank lines at the bottom of the window. */
20812 else if (line_start_position == pos
20813 && pos == BUF_ZV (XBUFFER (w->buffer)))
20814 {
20815 maybe_next_line_p = 1;
20816 break;
20817 }
20818 else if (line_start_position > 0)
20819 {
20820 best_row = row;
20821 best_row_vpos = row_vpos;
20822 }
20823
20824 if (row->y + row->height >= yb)
20825 break;
20826
20827 ++row;
20828 ++row_vpos;
20829 }
20830
20831 /* Find the right column within BEST_ROW. */
20832 lastcol = 0;
20833 current_x = best_row->x;
20834 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20835 {
20836 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20837 int charpos = glyph->charpos;
20838
20839 if (BUFFERP (glyph->object))
20840 {
20841 if (charpos == pos)
20842 {
20843 *hpos = i;
20844 *vpos = best_row_vpos;
20845 *x = current_x;
20846 *y = best_row->y;
20847 return 1;
20848 }
20849 else if (charpos > pos)
20850 break;
20851 }
20852 else if (EQ (glyph->object, stop))
20853 break;
20854
20855 if (charpos > 0)
20856 lastcol = i;
20857 current_x += glyph->pixel_width;
20858 }
20859
20860 /* If we're looking for the end of the buffer,
20861 and we didn't find it in the line we scanned,
20862 use the start of the following line. */
20863 if (maybe_next_line_p)
20864 {
20865 ++best_row;
20866 ++best_row_vpos;
20867 lastcol = 0;
20868 current_x = best_row->x;
20869 }
20870
20871 *vpos = best_row_vpos;
20872 *hpos = lastcol + 1;
20873 *x = current_x;
20874 *y = best_row->y;
20875 return 0;
20876 }
20877
20878 #endif /* not 1 */
20879
20880
20881 /* Find the position of the glyph for position POS in OBJECT in
20882 window W's current matrix, and return in *X, *Y the pixel
20883 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20884
20885 RIGHT_P non-zero means return the position of the right edge of the
20886 glyph, RIGHT_P zero means return the left edge position.
20887
20888 If no glyph for POS exists in the matrix, return the position of
20889 the glyph with the next smaller position that is in the matrix, if
20890 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20891 exists in the matrix, return the position of the glyph with the
20892 next larger position in OBJECT.
20893
20894 Value is non-zero if a glyph was found. */
20895
20896 static int
20897 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20898 struct window *w;
20899 int pos;
20900 Lisp_Object object;
20901 int *hpos, *vpos, *x, *y;
20902 int right_p;
20903 {
20904 int yb = window_text_bottom_y (w);
20905 struct glyph_row *r;
20906 struct glyph *best_glyph = NULL;
20907 struct glyph_row *best_row = NULL;
20908 int best_x = 0;
20909
20910 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20911 r->enabled_p && r->y < yb;
20912 ++r)
20913 {
20914 struct glyph *g = r->glyphs[TEXT_AREA];
20915 struct glyph *e = g + r->used[TEXT_AREA];
20916 int gx;
20917
20918 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20919 if (EQ (g->object, object))
20920 {
20921 if (g->charpos == pos)
20922 {
20923 best_glyph = g;
20924 best_x = gx;
20925 best_row = r;
20926 goto found;
20927 }
20928 else if (best_glyph == NULL
20929 || ((abs (g->charpos - pos)
20930 < abs (best_glyph->charpos - pos))
20931 && (right_p
20932 ? g->charpos < pos
20933 : g->charpos > pos)))
20934 {
20935 best_glyph = g;
20936 best_x = gx;
20937 best_row = r;
20938 }
20939 }
20940 }
20941
20942 found:
20943
20944 if (best_glyph)
20945 {
20946 *x = best_x;
20947 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
20948
20949 if (right_p)
20950 {
20951 *x += best_glyph->pixel_width;
20952 ++*hpos;
20953 }
20954
20955 *y = best_row->y;
20956 *vpos = best_row - w->current_matrix->rows;
20957 }
20958
20959 return best_glyph != NULL;
20960 }
20961
20962
20963 /* See if position X, Y is within a hot-spot of an image. */
20964
20965 static int
20966 on_hot_spot_p (hot_spot, x, y)
20967 Lisp_Object hot_spot;
20968 int x, y;
20969 {
20970 if (!CONSP (hot_spot))
20971 return 0;
20972
20973 if (EQ (XCAR (hot_spot), Qrect))
20974 {
20975 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
20976 Lisp_Object rect = XCDR (hot_spot);
20977 Lisp_Object tem;
20978 if (!CONSP (rect))
20979 return 0;
20980 if (!CONSP (XCAR (rect)))
20981 return 0;
20982 if (!CONSP (XCDR (rect)))
20983 return 0;
20984 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
20985 return 0;
20986 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
20987 return 0;
20988 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
20989 return 0;
20990 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
20991 return 0;
20992 return 1;
20993 }
20994 else if (EQ (XCAR (hot_spot), Qcircle))
20995 {
20996 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
20997 Lisp_Object circ = XCDR (hot_spot);
20998 Lisp_Object lr, lx0, ly0;
20999 if (CONSP (circ)
21000 && CONSP (XCAR (circ))
21001 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21002 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21003 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21004 {
21005 double r = XFLOATINT (lr);
21006 double dx = XINT (lx0) - x;
21007 double dy = XINT (ly0) - y;
21008 return (dx * dx + dy * dy <= r * r);
21009 }
21010 }
21011 else if (EQ (XCAR (hot_spot), Qpoly))
21012 {
21013 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21014 if (VECTORP (XCDR (hot_spot)))
21015 {
21016 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21017 Lisp_Object *poly = v->contents;
21018 int n = v->size;
21019 int i;
21020 int inside = 0;
21021 Lisp_Object lx, ly;
21022 int x0, y0;
21023
21024 /* Need an even number of coordinates, and at least 3 edges. */
21025 if (n < 6 || n & 1)
21026 return 0;
21027
21028 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21029 If count is odd, we are inside polygon. Pixels on edges
21030 may or may not be included depending on actual geometry of the
21031 polygon. */
21032 if ((lx = poly[n-2], !INTEGERP (lx))
21033 || (ly = poly[n-1], !INTEGERP (lx)))
21034 return 0;
21035 x0 = XINT (lx), y0 = XINT (ly);
21036 for (i = 0; i < n; i += 2)
21037 {
21038 int x1 = x0, y1 = y0;
21039 if ((lx = poly[i], !INTEGERP (lx))
21040 || (ly = poly[i+1], !INTEGERP (ly)))
21041 return 0;
21042 x0 = XINT (lx), y0 = XINT (ly);
21043
21044 /* Does this segment cross the X line? */
21045 if (x0 >= x)
21046 {
21047 if (x1 >= x)
21048 continue;
21049 }
21050 else if (x1 < x)
21051 continue;
21052 if (y > y0 && y > y1)
21053 continue;
21054 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21055 inside = !inside;
21056 }
21057 return inside;
21058 }
21059 }
21060 /* If we don't understand the format, pretend we're not in the hot-spot. */
21061 return 0;
21062 }
21063
21064 Lisp_Object
21065 find_hot_spot (map, x, y)
21066 Lisp_Object map;
21067 int x, y;
21068 {
21069 while (CONSP (map))
21070 {
21071 if (CONSP (XCAR (map))
21072 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21073 return XCAR (map);
21074 map = XCDR (map);
21075 }
21076
21077 return Qnil;
21078 }
21079
21080 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21081 3, 3, 0,
21082 doc: /* Lookup in image map MAP coordinates X and Y.
21083 An image map is an alist where each element has the format (AREA ID PLIST).
21084 An AREA is specified as either a rectangle, a circle, or a polygon:
21085 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21086 pixel coordinates of the upper left and bottom right corners.
21087 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21088 and the radius of the circle; r may be a float or integer.
21089 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21090 vector describes one corner in the polygon.
21091 Returns the alist element for the first matching AREA in MAP. */)
21092 (map, x, y)
21093 Lisp_Object map;
21094 Lisp_Object x, y;
21095 {
21096 if (NILP (map))
21097 return Qnil;
21098
21099 CHECK_NUMBER (x);
21100 CHECK_NUMBER (y);
21101
21102 return find_hot_spot (map, XINT (x), XINT (y));
21103 }
21104
21105
21106 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21107 static void
21108 define_frame_cursor1 (f, cursor, pointer)
21109 struct frame *f;
21110 Cursor cursor;
21111 Lisp_Object pointer;
21112 {
21113 /* Do not change cursor shape while dragging mouse. */
21114 if (!NILP (do_mouse_tracking))
21115 return;
21116
21117 if (!NILP (pointer))
21118 {
21119 if (EQ (pointer, Qarrow))
21120 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21121 else if (EQ (pointer, Qhand))
21122 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21123 else if (EQ (pointer, Qtext))
21124 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21125 else if (EQ (pointer, intern ("hdrag")))
21126 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21127 #ifdef HAVE_X_WINDOWS
21128 else if (EQ (pointer, intern ("vdrag")))
21129 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21130 #endif
21131 else if (EQ (pointer, intern ("hourglass")))
21132 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21133 else if (EQ (pointer, Qmodeline))
21134 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21135 else
21136 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21137 }
21138
21139 if (cursor != No_Cursor)
21140 rif->define_frame_cursor (f, cursor);
21141 }
21142
21143 /* Take proper action when mouse has moved to the mode or header line
21144 or marginal area AREA of window W, x-position X and y-position Y.
21145 X is relative to the start of the text display area of W, so the
21146 width of bitmap areas and scroll bars must be subtracted to get a
21147 position relative to the start of the mode line. */
21148
21149 static void
21150 note_mode_line_or_margin_highlight (w, x, y, area)
21151 struct window *w;
21152 int x, y;
21153 enum window_part area;
21154 {
21155 struct frame *f = XFRAME (w->frame);
21156 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21157 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21158 Lisp_Object pointer = Qnil;
21159 int charpos, dx, dy, width, height;
21160 Lisp_Object string, object = Qnil;
21161 Lisp_Object pos, help;
21162
21163 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21164 string = mode_line_string (w, area, &x, &y, &charpos,
21165 &object, &dx, &dy, &width, &height);
21166 else
21167 {
21168 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21169 string = marginal_area_string (w, area, &x, &y, &charpos,
21170 &object, &dx, &dy, &width, &height);
21171 }
21172
21173 help = Qnil;
21174
21175 if (IMAGEP (object))
21176 {
21177 Lisp_Object image_map, hotspot;
21178 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
21179 !NILP (image_map))
21180 && (hotspot = find_hot_spot (image_map, dx, dy),
21181 CONSP (hotspot))
21182 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21183 {
21184 Lisp_Object area_id, plist;
21185
21186 area_id = XCAR (hotspot);
21187 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21188 If so, we could look for mouse-enter, mouse-leave
21189 properties in PLIST (and do something...). */
21190 hotspot = XCDR (hotspot);
21191 if (CONSP (hotspot)
21192 && (plist = XCAR (hotspot), CONSP (plist)))
21193 {
21194 pointer = Fsafe_plist_get (plist, Qpointer);
21195 if (NILP (pointer))
21196 pointer = Qhand;
21197 help = Fsafe_plist_get (plist, Qhelp_echo);
21198 if (!NILP (help))
21199 {
21200 help_echo_string = help;
21201 /* Is this correct? ++kfs */
21202 XSETWINDOW (help_echo_window, w);
21203 help_echo_object = w->buffer;
21204 help_echo_pos = charpos;
21205 }
21206 }
21207 }
21208 if (NILP (pointer))
21209 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21210 }
21211
21212 if (STRINGP (string))
21213 {
21214 pos = make_number (charpos);
21215 /* If we're on a string with `help-echo' text property, arrange
21216 for the help to be displayed. This is done by setting the
21217 global variable help_echo_string to the help string. */
21218 if (NILP (help))
21219 {
21220 help = Fget_text_property (pos, Qhelp_echo, string);
21221 if (!NILP (help))
21222 {
21223 help_echo_string = help;
21224 XSETWINDOW (help_echo_window, w);
21225 help_echo_object = string;
21226 help_echo_pos = charpos;
21227 }
21228 }
21229
21230 if (NILP (pointer))
21231 pointer = Fget_text_property (pos, Qpointer, string);
21232
21233 /* Change the mouse pointer according to what is under X/Y. */
21234 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21235 {
21236 Lisp_Object map;
21237 map = Fget_text_property (pos, Qlocal_map, string);
21238 if (!KEYMAPP (map))
21239 map = Fget_text_property (pos, Qkeymap, string);
21240 if (!KEYMAPP (map))
21241 cursor = dpyinfo->vertical_scroll_bar_cursor;
21242 }
21243 }
21244
21245 define_frame_cursor1 (f, cursor, pointer);
21246 }
21247
21248
21249 /* EXPORT:
21250 Take proper action when the mouse has moved to position X, Y on
21251 frame F as regards highlighting characters that have mouse-face
21252 properties. Also de-highlighting chars where the mouse was before.
21253 X and Y can be negative or out of range. */
21254
21255 void
21256 note_mouse_highlight (f, x, y)
21257 struct frame *f;
21258 int x, y;
21259 {
21260 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21261 enum window_part part;
21262 Lisp_Object window;
21263 struct window *w;
21264 Cursor cursor = No_Cursor;
21265 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21266 struct buffer *b;
21267
21268 /* When a menu is active, don't highlight because this looks odd. */
21269 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21270 if (popup_activated ())
21271 return;
21272 #endif
21273
21274 if (NILP (Vmouse_highlight)
21275 || !f->glyphs_initialized_p)
21276 return;
21277
21278 dpyinfo->mouse_face_mouse_x = x;
21279 dpyinfo->mouse_face_mouse_y = y;
21280 dpyinfo->mouse_face_mouse_frame = f;
21281
21282 if (dpyinfo->mouse_face_defer)
21283 return;
21284
21285 if (gc_in_progress)
21286 {
21287 dpyinfo->mouse_face_deferred_gc = 1;
21288 return;
21289 }
21290
21291 /* Which window is that in? */
21292 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21293
21294 /* If we were displaying active text in another window, clear that.
21295 Also clear if we move out of text area in same window. */
21296 if (! EQ (window, dpyinfo->mouse_face_window)
21297 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21298 clear_mouse_face (dpyinfo);
21299
21300 /* Not on a window -> return. */
21301 if (!WINDOWP (window))
21302 return;
21303
21304 /* Reset help_echo_string. It will get recomputed below. */
21305 help_echo_string = Qnil;
21306
21307 /* Convert to window-relative pixel coordinates. */
21308 w = XWINDOW (window);
21309 frame_to_window_pixel_xy (w, &x, &y);
21310
21311 /* Handle tool-bar window differently since it doesn't display a
21312 buffer. */
21313 if (EQ (window, f->tool_bar_window))
21314 {
21315 note_tool_bar_highlight (f, x, y);
21316 return;
21317 }
21318
21319 /* Mouse is on the mode, header line or margin? */
21320 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21321 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21322 {
21323 note_mode_line_or_margin_highlight (w, x, y, part);
21324 return;
21325 }
21326
21327 if (part == ON_VERTICAL_BORDER)
21328 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21329 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21330 || part == ON_SCROLL_BAR)
21331 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21332 else
21333 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21334
21335 /* Are we in a window whose display is up to date?
21336 And verify the buffer's text has not changed. */
21337 b = XBUFFER (w->buffer);
21338 if (part == ON_TEXT
21339 && EQ (w->window_end_valid, w->buffer)
21340 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21341 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21342 {
21343 int hpos, vpos, pos, i, dx, dy, area;
21344 struct glyph *glyph;
21345 Lisp_Object object;
21346 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21347 Lisp_Object *overlay_vec = NULL;
21348 int noverlays;
21349 struct buffer *obuf;
21350 int obegv, ozv, same_region;
21351
21352 /* Find the glyph under X/Y. */
21353 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21354
21355 /* Look for :pointer property on image. */
21356 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21357 {
21358 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21359 if (img != NULL && IMAGEP (img->spec))
21360 {
21361 Lisp_Object image_map, hotspot;
21362 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21363 !NILP (image_map))
21364 && (hotspot = find_hot_spot (image_map,
21365 glyph->slice.x + dx,
21366 glyph->slice.y + dy),
21367 CONSP (hotspot))
21368 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21369 {
21370 Lisp_Object area_id, plist;
21371
21372 area_id = XCAR (hotspot);
21373 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21374 If so, we could look for mouse-enter, mouse-leave
21375 properties in PLIST (and do something...). */
21376 hotspot = XCDR (hotspot);
21377 if (CONSP (hotspot)
21378 && (plist = XCAR (hotspot), CONSP (plist)))
21379 {
21380 pointer = Fsafe_plist_get (plist, Qpointer);
21381 if (NILP (pointer))
21382 pointer = Qhand;
21383 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21384 if (!NILP (help_echo_string))
21385 {
21386 help_echo_window = window;
21387 help_echo_object = glyph->object;
21388 help_echo_pos = glyph->charpos;
21389 }
21390 }
21391 }
21392 if (NILP (pointer))
21393 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21394 }
21395 }
21396
21397 /* Clear mouse face if X/Y not over text. */
21398 if (glyph == NULL
21399 || area != TEXT_AREA
21400 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21401 {
21402 if (clear_mouse_face (dpyinfo))
21403 cursor = No_Cursor;
21404 if (NILP (pointer))
21405 {
21406 if (area != TEXT_AREA)
21407 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21408 else
21409 pointer = Vvoid_text_area_pointer;
21410 }
21411 goto set_cursor;
21412 }
21413
21414 pos = glyph->charpos;
21415 object = glyph->object;
21416 if (!STRINGP (object) && !BUFFERP (object))
21417 goto set_cursor;
21418
21419 /* If we get an out-of-range value, return now; avoid an error. */
21420 if (BUFFERP (object) && pos > BUF_Z (b))
21421 goto set_cursor;
21422
21423 /* Make the window's buffer temporarily current for
21424 overlays_at and compute_char_face. */
21425 obuf = current_buffer;
21426 current_buffer = b;
21427 obegv = BEGV;
21428 ozv = ZV;
21429 BEGV = BEG;
21430 ZV = Z;
21431
21432 /* Is this char mouse-active or does it have help-echo? */
21433 position = make_number (pos);
21434
21435 if (BUFFERP (object))
21436 {
21437 /* Put all the overlays we want in a vector in overlay_vec. */
21438 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21439 /* Sort overlays into increasing priority order. */
21440 noverlays = sort_overlays (overlay_vec, noverlays, w);
21441 }
21442 else
21443 noverlays = 0;
21444
21445 same_region = (EQ (window, dpyinfo->mouse_face_window)
21446 && vpos >= dpyinfo->mouse_face_beg_row
21447 && vpos <= dpyinfo->mouse_face_end_row
21448 && (vpos > dpyinfo->mouse_face_beg_row
21449 || hpos >= dpyinfo->mouse_face_beg_col)
21450 && (vpos < dpyinfo->mouse_face_end_row
21451 || hpos < dpyinfo->mouse_face_end_col
21452 || dpyinfo->mouse_face_past_end));
21453
21454 if (same_region)
21455 cursor = No_Cursor;
21456
21457 /* Check mouse-face highlighting. */
21458 if (! same_region
21459 /* If there exists an overlay with mouse-face overlapping
21460 the one we are currently highlighting, we have to
21461 check if we enter the overlapping overlay, and then
21462 highlight only that. */
21463 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21464 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21465 {
21466 /* Find the highest priority overlay that has a mouse-face
21467 property. */
21468 overlay = Qnil;
21469 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21470 {
21471 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21472 if (!NILP (mouse_face))
21473 overlay = overlay_vec[i];
21474 }
21475
21476 /* If we're actually highlighting the same overlay as
21477 before, there's no need to do that again. */
21478 if (!NILP (overlay)
21479 && EQ (overlay, dpyinfo->mouse_face_overlay))
21480 goto check_help_echo;
21481
21482 dpyinfo->mouse_face_overlay = overlay;
21483
21484 /* Clear the display of the old active region, if any. */
21485 if (clear_mouse_face (dpyinfo))
21486 cursor = No_Cursor;
21487
21488 /* If no overlay applies, get a text property. */
21489 if (NILP (overlay))
21490 mouse_face = Fget_text_property (position, Qmouse_face, object);
21491
21492 /* Handle the overlay case. */
21493 if (!NILP (overlay))
21494 {
21495 /* Find the range of text around this char that
21496 should be active. */
21497 Lisp_Object before, after;
21498 int ignore;
21499
21500 before = Foverlay_start (overlay);
21501 after = Foverlay_end (overlay);
21502 /* Record this as the current active region. */
21503 fast_find_position (w, XFASTINT (before),
21504 &dpyinfo->mouse_face_beg_col,
21505 &dpyinfo->mouse_face_beg_row,
21506 &dpyinfo->mouse_face_beg_x,
21507 &dpyinfo->mouse_face_beg_y, Qnil);
21508
21509 dpyinfo->mouse_face_past_end
21510 = !fast_find_position (w, XFASTINT (after),
21511 &dpyinfo->mouse_face_end_col,
21512 &dpyinfo->mouse_face_end_row,
21513 &dpyinfo->mouse_face_end_x,
21514 &dpyinfo->mouse_face_end_y, Qnil);
21515 dpyinfo->mouse_face_window = window;
21516
21517 dpyinfo->mouse_face_face_id
21518 = face_at_buffer_position (w, pos, 0, 0,
21519 &ignore, pos + 1,
21520 !dpyinfo->mouse_face_hidden);
21521
21522 /* Display it as active. */
21523 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21524 cursor = No_Cursor;
21525 }
21526 /* Handle the text property case. */
21527 else if (!NILP (mouse_face) && BUFFERP (object))
21528 {
21529 /* Find the range of text around this char that
21530 should be active. */
21531 Lisp_Object before, after, beginning, end;
21532 int ignore;
21533
21534 beginning = Fmarker_position (w->start);
21535 end = make_number (BUF_Z (XBUFFER (object))
21536 - XFASTINT (w->window_end_pos));
21537 before
21538 = Fprevious_single_property_change (make_number (pos + 1),
21539 Qmouse_face,
21540 object, beginning);
21541 after
21542 = Fnext_single_property_change (position, Qmouse_face,
21543 object, end);
21544
21545 /* Record this as the current active region. */
21546 fast_find_position (w, XFASTINT (before),
21547 &dpyinfo->mouse_face_beg_col,
21548 &dpyinfo->mouse_face_beg_row,
21549 &dpyinfo->mouse_face_beg_x,
21550 &dpyinfo->mouse_face_beg_y, Qnil);
21551 dpyinfo->mouse_face_past_end
21552 = !fast_find_position (w, XFASTINT (after),
21553 &dpyinfo->mouse_face_end_col,
21554 &dpyinfo->mouse_face_end_row,
21555 &dpyinfo->mouse_face_end_x,
21556 &dpyinfo->mouse_face_end_y, Qnil);
21557 dpyinfo->mouse_face_window = window;
21558
21559 if (BUFFERP (object))
21560 dpyinfo->mouse_face_face_id
21561 = face_at_buffer_position (w, pos, 0, 0,
21562 &ignore, pos + 1,
21563 !dpyinfo->mouse_face_hidden);
21564
21565 /* Display it as active. */
21566 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21567 cursor = No_Cursor;
21568 }
21569 else if (!NILP (mouse_face) && STRINGP (object))
21570 {
21571 Lisp_Object b, e;
21572 int ignore;
21573
21574 b = Fprevious_single_property_change (make_number (pos + 1),
21575 Qmouse_face,
21576 object, Qnil);
21577 e = Fnext_single_property_change (position, Qmouse_face,
21578 object, Qnil);
21579 if (NILP (b))
21580 b = make_number (0);
21581 if (NILP (e))
21582 e = make_number (SCHARS (object) - 1);
21583 fast_find_string_pos (w, XINT (b), object,
21584 &dpyinfo->mouse_face_beg_col,
21585 &dpyinfo->mouse_face_beg_row,
21586 &dpyinfo->mouse_face_beg_x,
21587 &dpyinfo->mouse_face_beg_y, 0);
21588 fast_find_string_pos (w, XINT (e), object,
21589 &dpyinfo->mouse_face_end_col,
21590 &dpyinfo->mouse_face_end_row,
21591 &dpyinfo->mouse_face_end_x,
21592 &dpyinfo->mouse_face_end_y, 1);
21593 dpyinfo->mouse_face_past_end = 0;
21594 dpyinfo->mouse_face_window = window;
21595 dpyinfo->mouse_face_face_id
21596 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21597 glyph->face_id, 1);
21598 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21599 cursor = No_Cursor;
21600 }
21601 else if (STRINGP (object) && NILP (mouse_face))
21602 {
21603 /* A string which doesn't have mouse-face, but
21604 the text ``under'' it might have. */
21605 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21606 int start = MATRIX_ROW_START_CHARPOS (r);
21607
21608 pos = string_buffer_position (w, object, start);
21609 if (pos > 0)
21610 mouse_face = get_char_property_and_overlay (make_number (pos),
21611 Qmouse_face,
21612 w->buffer,
21613 &overlay);
21614 if (!NILP (mouse_face) && !NILP (overlay))
21615 {
21616 Lisp_Object before = Foverlay_start (overlay);
21617 Lisp_Object after = Foverlay_end (overlay);
21618 int ignore;
21619
21620 /* Note that we might not be able to find position
21621 BEFORE in the glyph matrix if the overlay is
21622 entirely covered by a `display' property. In
21623 this case, we overshoot. So let's stop in
21624 the glyph matrix before glyphs for OBJECT. */
21625 fast_find_position (w, XFASTINT (before),
21626 &dpyinfo->mouse_face_beg_col,
21627 &dpyinfo->mouse_face_beg_row,
21628 &dpyinfo->mouse_face_beg_x,
21629 &dpyinfo->mouse_face_beg_y,
21630 object);
21631
21632 dpyinfo->mouse_face_past_end
21633 = !fast_find_position (w, XFASTINT (after),
21634 &dpyinfo->mouse_face_end_col,
21635 &dpyinfo->mouse_face_end_row,
21636 &dpyinfo->mouse_face_end_x,
21637 &dpyinfo->mouse_face_end_y,
21638 Qnil);
21639 dpyinfo->mouse_face_window = window;
21640 dpyinfo->mouse_face_face_id
21641 = face_at_buffer_position (w, pos, 0, 0,
21642 &ignore, pos + 1,
21643 !dpyinfo->mouse_face_hidden);
21644
21645 /* Display it as active. */
21646 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21647 cursor = No_Cursor;
21648 }
21649 }
21650 }
21651
21652 check_help_echo:
21653
21654 /* Look for a `help-echo' property. */
21655 if (NILP (help_echo_string)) {
21656 Lisp_Object help, overlay;
21657
21658 /* Check overlays first. */
21659 help = overlay = Qnil;
21660 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21661 {
21662 overlay = overlay_vec[i];
21663 help = Foverlay_get (overlay, Qhelp_echo);
21664 }
21665
21666 if (!NILP (help))
21667 {
21668 help_echo_string = help;
21669 help_echo_window = window;
21670 help_echo_object = overlay;
21671 help_echo_pos = pos;
21672 }
21673 else
21674 {
21675 Lisp_Object object = glyph->object;
21676 int charpos = glyph->charpos;
21677
21678 /* Try text properties. */
21679 if (STRINGP (object)
21680 && charpos >= 0
21681 && charpos < SCHARS (object))
21682 {
21683 help = Fget_text_property (make_number (charpos),
21684 Qhelp_echo, object);
21685 if (NILP (help))
21686 {
21687 /* If the string itself doesn't specify a help-echo,
21688 see if the buffer text ``under'' it does. */
21689 struct glyph_row *r
21690 = MATRIX_ROW (w->current_matrix, vpos);
21691 int start = MATRIX_ROW_START_CHARPOS (r);
21692 int pos = string_buffer_position (w, object, start);
21693 if (pos > 0)
21694 {
21695 help = Fget_char_property (make_number (pos),
21696 Qhelp_echo, w->buffer);
21697 if (!NILP (help))
21698 {
21699 charpos = pos;
21700 object = w->buffer;
21701 }
21702 }
21703 }
21704 }
21705 else if (BUFFERP (object)
21706 && charpos >= BEGV
21707 && charpos < ZV)
21708 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21709 object);
21710
21711 if (!NILP (help))
21712 {
21713 help_echo_string = help;
21714 help_echo_window = window;
21715 help_echo_object = object;
21716 help_echo_pos = charpos;
21717 }
21718 }
21719 }
21720
21721 /* Look for a `pointer' property. */
21722 if (NILP (pointer))
21723 {
21724 /* Check overlays first. */
21725 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21726 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21727
21728 if (NILP (pointer))
21729 {
21730 Lisp_Object object = glyph->object;
21731 int charpos = glyph->charpos;
21732
21733 /* Try text properties. */
21734 if (STRINGP (object)
21735 && charpos >= 0
21736 && charpos < SCHARS (object))
21737 {
21738 pointer = Fget_text_property (make_number (charpos),
21739 Qpointer, object);
21740 if (NILP (pointer))
21741 {
21742 /* If the string itself doesn't specify a pointer,
21743 see if the buffer text ``under'' it does. */
21744 struct glyph_row *r
21745 = MATRIX_ROW (w->current_matrix, vpos);
21746 int start = MATRIX_ROW_START_CHARPOS (r);
21747 int pos = string_buffer_position (w, object, start);
21748 if (pos > 0)
21749 pointer = Fget_char_property (make_number (pos),
21750 Qpointer, w->buffer);
21751 }
21752 }
21753 else if (BUFFERP (object)
21754 && charpos >= BEGV
21755 && charpos < ZV)
21756 pointer = Fget_text_property (make_number (charpos),
21757 Qpointer, object);
21758 }
21759 }
21760
21761 BEGV = obegv;
21762 ZV = ozv;
21763 current_buffer = obuf;
21764 }
21765
21766 set_cursor:
21767
21768 define_frame_cursor1 (f, cursor, pointer);
21769 }
21770
21771
21772 /* EXPORT for RIF:
21773 Clear any mouse-face on window W. This function is part of the
21774 redisplay interface, and is called from try_window_id and similar
21775 functions to ensure the mouse-highlight is off. */
21776
21777 void
21778 x_clear_window_mouse_face (w)
21779 struct window *w;
21780 {
21781 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21782 Lisp_Object window;
21783
21784 BLOCK_INPUT;
21785 XSETWINDOW (window, w);
21786 if (EQ (window, dpyinfo->mouse_face_window))
21787 clear_mouse_face (dpyinfo);
21788 UNBLOCK_INPUT;
21789 }
21790
21791
21792 /* EXPORT:
21793 Just discard the mouse face information for frame F, if any.
21794 This is used when the size of F is changed. */
21795
21796 void
21797 cancel_mouse_face (f)
21798 struct frame *f;
21799 {
21800 Lisp_Object window;
21801 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21802
21803 window = dpyinfo->mouse_face_window;
21804 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21805 {
21806 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21807 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21808 dpyinfo->mouse_face_window = Qnil;
21809 }
21810 }
21811
21812
21813 #endif /* HAVE_WINDOW_SYSTEM */
21814
21815 \f
21816 /***********************************************************************
21817 Exposure Events
21818 ***********************************************************************/
21819
21820 #ifdef HAVE_WINDOW_SYSTEM
21821
21822 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21823 which intersects rectangle R. R is in window-relative coordinates. */
21824
21825 static void
21826 expose_area (w, row, r, area)
21827 struct window *w;
21828 struct glyph_row *row;
21829 XRectangle *r;
21830 enum glyph_row_area area;
21831 {
21832 struct glyph *first = row->glyphs[area];
21833 struct glyph *end = row->glyphs[area] + row->used[area];
21834 struct glyph *last;
21835 int first_x, start_x, x;
21836
21837 if (area == TEXT_AREA && row->fill_line_p)
21838 /* If row extends face to end of line write the whole line. */
21839 draw_glyphs (w, 0, row, area,
21840 0, row->used[area],
21841 DRAW_NORMAL_TEXT, 0);
21842 else
21843 {
21844 /* Set START_X to the window-relative start position for drawing glyphs of
21845 AREA. The first glyph of the text area can be partially visible.
21846 The first glyphs of other areas cannot. */
21847 start_x = window_box_left_offset (w, area);
21848 x = start_x;
21849 if (area == TEXT_AREA)
21850 x += row->x;
21851
21852 /* Find the first glyph that must be redrawn. */
21853 while (first < end
21854 && x + first->pixel_width < r->x)
21855 {
21856 x += first->pixel_width;
21857 ++first;
21858 }
21859
21860 /* Find the last one. */
21861 last = first;
21862 first_x = x;
21863 while (last < end
21864 && x < r->x + r->width)
21865 {
21866 x += last->pixel_width;
21867 ++last;
21868 }
21869
21870 /* Repaint. */
21871 if (last > first)
21872 draw_glyphs (w, first_x - start_x, row, area,
21873 first - row->glyphs[area], last - row->glyphs[area],
21874 DRAW_NORMAL_TEXT, 0);
21875 }
21876 }
21877
21878
21879 /* Redraw the parts of the glyph row ROW on window W intersecting
21880 rectangle R. R is in window-relative coordinates. Value is
21881 non-zero if mouse-face was overwritten. */
21882
21883 static int
21884 expose_line (w, row, r)
21885 struct window *w;
21886 struct glyph_row *row;
21887 XRectangle *r;
21888 {
21889 xassert (row->enabled_p);
21890
21891 if (row->mode_line_p || w->pseudo_window_p)
21892 draw_glyphs (w, 0, row, TEXT_AREA,
21893 0, row->used[TEXT_AREA],
21894 DRAW_NORMAL_TEXT, 0);
21895 else
21896 {
21897 if (row->used[LEFT_MARGIN_AREA])
21898 expose_area (w, row, r, LEFT_MARGIN_AREA);
21899 if (row->used[TEXT_AREA])
21900 expose_area (w, row, r, TEXT_AREA);
21901 if (row->used[RIGHT_MARGIN_AREA])
21902 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21903 draw_row_fringe_bitmaps (w, row);
21904 }
21905
21906 return row->mouse_face_p;
21907 }
21908
21909
21910 /* Redraw those parts of glyphs rows during expose event handling that
21911 overlap other rows. Redrawing of an exposed line writes over parts
21912 of lines overlapping that exposed line; this function fixes that.
21913
21914 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21915 row in W's current matrix that is exposed and overlaps other rows.
21916 LAST_OVERLAPPING_ROW is the last such row. */
21917
21918 static void
21919 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21920 struct window *w;
21921 struct glyph_row *first_overlapping_row;
21922 struct glyph_row *last_overlapping_row;
21923 {
21924 struct glyph_row *row;
21925
21926 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21927 if (row->overlapping_p)
21928 {
21929 xassert (row->enabled_p && !row->mode_line_p);
21930
21931 if (row->used[LEFT_MARGIN_AREA])
21932 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21933
21934 if (row->used[TEXT_AREA])
21935 x_fix_overlapping_area (w, row, TEXT_AREA);
21936
21937 if (row->used[RIGHT_MARGIN_AREA])
21938 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21939 }
21940 }
21941
21942
21943 /* Return non-zero if W's cursor intersects rectangle R. */
21944
21945 static int
21946 phys_cursor_in_rect_p (w, r)
21947 struct window *w;
21948 XRectangle *r;
21949 {
21950 XRectangle cr, result;
21951 struct glyph *cursor_glyph;
21952
21953 cursor_glyph = get_phys_cursor_glyph (w);
21954 if (cursor_glyph)
21955 {
21956 /* r is relative to W's box, but w->phys_cursor.x is relative
21957 to left edge of W's TEXT area. Adjust it. */
21958 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
21959 cr.y = w->phys_cursor.y;
21960 cr.width = cursor_glyph->pixel_width;
21961 cr.height = w->phys_cursor_height;
21962 /* ++KFS: W32 version used W32-specific IntersectRect here, but
21963 I assume the effect is the same -- and this is portable. */
21964 return x_intersect_rectangles (&cr, r, &result);
21965 }
21966 else
21967 return 0;
21968 }
21969
21970
21971 /* EXPORT:
21972 Draw a vertical window border to the right of window W if W doesn't
21973 have vertical scroll bars. */
21974
21975 void
21976 x_draw_vertical_border (w)
21977 struct window *w;
21978 {
21979 /* We could do better, if we knew what type of scroll-bar the adjacent
21980 windows (on either side) have... But we don't :-(
21981 However, I think this works ok. ++KFS 2003-04-25 */
21982
21983 /* Redraw borders between horizontally adjacent windows. Don't
21984 do it for frames with vertical scroll bars because either the
21985 right scroll bar of a window, or the left scroll bar of its
21986 neighbor will suffice as a border. */
21987 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
21988 return;
21989
21990 if (!WINDOW_RIGHTMOST_P (w)
21991 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
21992 {
21993 int x0, x1, y0, y1;
21994
21995 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
21996 y1 -= 1;
21997
21998 rif->draw_vertical_window_border (w, x1, y0, y1);
21999 }
22000 else if (!WINDOW_LEFTMOST_P (w)
22001 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22002 {
22003 int x0, x1, y0, y1;
22004
22005 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22006 y1 -= 1;
22007
22008 rif->draw_vertical_window_border (w, x0, y0, y1);
22009 }
22010 }
22011
22012
22013 /* Redraw the part of window W intersection rectangle FR. Pixel
22014 coordinates in FR are frame-relative. Call this function with
22015 input blocked. Value is non-zero if the exposure overwrites
22016 mouse-face. */
22017
22018 static int
22019 expose_window (w, fr)
22020 struct window *w;
22021 XRectangle *fr;
22022 {
22023 struct frame *f = XFRAME (w->frame);
22024 XRectangle wr, r;
22025 int mouse_face_overwritten_p = 0;
22026
22027 /* If window is not yet fully initialized, do nothing. This can
22028 happen when toolkit scroll bars are used and a window is split.
22029 Reconfiguring the scroll bar will generate an expose for a newly
22030 created window. */
22031 if (w->current_matrix == NULL)
22032 return 0;
22033
22034 /* When we're currently updating the window, display and current
22035 matrix usually don't agree. Arrange for a thorough display
22036 later. */
22037 if (w == updated_window)
22038 {
22039 SET_FRAME_GARBAGED (f);
22040 return 0;
22041 }
22042
22043 /* Frame-relative pixel rectangle of W. */
22044 wr.x = WINDOW_LEFT_EDGE_X (w);
22045 wr.y = WINDOW_TOP_EDGE_Y (w);
22046 wr.width = WINDOW_TOTAL_WIDTH (w);
22047 wr.height = WINDOW_TOTAL_HEIGHT (w);
22048
22049 if (x_intersect_rectangles (fr, &wr, &r))
22050 {
22051 int yb = window_text_bottom_y (w);
22052 struct glyph_row *row;
22053 int cursor_cleared_p;
22054 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22055
22056 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22057 r.x, r.y, r.width, r.height));
22058
22059 /* Convert to window coordinates. */
22060 r.x -= WINDOW_LEFT_EDGE_X (w);
22061 r.y -= WINDOW_TOP_EDGE_Y (w);
22062
22063 /* Turn off the cursor. */
22064 if (!w->pseudo_window_p
22065 && phys_cursor_in_rect_p (w, &r))
22066 {
22067 x_clear_cursor (w);
22068 cursor_cleared_p = 1;
22069 }
22070 else
22071 cursor_cleared_p = 0;
22072
22073 /* Update lines intersecting rectangle R. */
22074 first_overlapping_row = last_overlapping_row = NULL;
22075 for (row = w->current_matrix->rows;
22076 row->enabled_p;
22077 ++row)
22078 {
22079 int y0 = row->y;
22080 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22081
22082 if ((y0 >= r.y && y0 < r.y + r.height)
22083 || (y1 > r.y && y1 < r.y + r.height)
22084 || (r.y >= y0 && r.y < y1)
22085 || (r.y + r.height > y0 && r.y + r.height < y1))
22086 {
22087 if (row->overlapping_p)
22088 {
22089 if (first_overlapping_row == NULL)
22090 first_overlapping_row = row;
22091 last_overlapping_row = row;
22092 }
22093
22094 if (expose_line (w, row, &r))
22095 mouse_face_overwritten_p = 1;
22096 }
22097
22098 if (y1 >= yb)
22099 break;
22100 }
22101
22102 /* Display the mode line if there is one. */
22103 if (WINDOW_WANTS_MODELINE_P (w)
22104 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22105 row->enabled_p)
22106 && row->y < r.y + r.height)
22107 {
22108 if (expose_line (w, row, &r))
22109 mouse_face_overwritten_p = 1;
22110 }
22111
22112 if (!w->pseudo_window_p)
22113 {
22114 /* Fix the display of overlapping rows. */
22115 if (first_overlapping_row)
22116 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22117
22118 /* Draw border between windows. */
22119 x_draw_vertical_border (w);
22120
22121 /* Turn the cursor on again. */
22122 if (cursor_cleared_p)
22123 update_window_cursor (w, 1);
22124 }
22125 }
22126
22127 return mouse_face_overwritten_p;
22128 }
22129
22130
22131
22132 /* Redraw (parts) of all windows in the window tree rooted at W that
22133 intersect R. R contains frame pixel coordinates. Value is
22134 non-zero if the exposure overwrites mouse-face. */
22135
22136 static int
22137 expose_window_tree (w, r)
22138 struct window *w;
22139 XRectangle *r;
22140 {
22141 struct frame *f = XFRAME (w->frame);
22142 int mouse_face_overwritten_p = 0;
22143
22144 while (w && !FRAME_GARBAGED_P (f))
22145 {
22146 if (!NILP (w->hchild))
22147 mouse_face_overwritten_p
22148 |= expose_window_tree (XWINDOW (w->hchild), r);
22149 else if (!NILP (w->vchild))
22150 mouse_face_overwritten_p
22151 |= expose_window_tree (XWINDOW (w->vchild), r);
22152 else
22153 mouse_face_overwritten_p |= expose_window (w, r);
22154
22155 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22156 }
22157
22158 return mouse_face_overwritten_p;
22159 }
22160
22161
22162 /* EXPORT:
22163 Redisplay an exposed area of frame F. X and Y are the upper-left
22164 corner of the exposed rectangle. W and H are width and height of
22165 the exposed area. All are pixel values. W or H zero means redraw
22166 the entire frame. */
22167
22168 void
22169 expose_frame (f, x, y, w, h)
22170 struct frame *f;
22171 int x, y, w, h;
22172 {
22173 XRectangle r;
22174 int mouse_face_overwritten_p = 0;
22175
22176 TRACE ((stderr, "expose_frame "));
22177
22178 /* No need to redraw if frame will be redrawn soon. */
22179 if (FRAME_GARBAGED_P (f))
22180 {
22181 TRACE ((stderr, " garbaged\n"));
22182 return;
22183 }
22184
22185 /* If basic faces haven't been realized yet, there is no point in
22186 trying to redraw anything. This can happen when we get an expose
22187 event while Emacs is starting, e.g. by moving another window. */
22188 if (FRAME_FACE_CACHE (f) == NULL
22189 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22190 {
22191 TRACE ((stderr, " no faces\n"));
22192 return;
22193 }
22194
22195 if (w == 0 || h == 0)
22196 {
22197 r.x = r.y = 0;
22198 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22199 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22200 }
22201 else
22202 {
22203 r.x = x;
22204 r.y = y;
22205 r.width = w;
22206 r.height = h;
22207 }
22208
22209 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22210 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22211
22212 if (WINDOWP (f->tool_bar_window))
22213 mouse_face_overwritten_p
22214 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22215
22216 #ifdef HAVE_X_WINDOWS
22217 #ifndef MSDOS
22218 #ifndef USE_X_TOOLKIT
22219 if (WINDOWP (f->menu_bar_window))
22220 mouse_face_overwritten_p
22221 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22222 #endif /* not USE_X_TOOLKIT */
22223 #endif
22224 #endif
22225
22226 /* Some window managers support a focus-follows-mouse style with
22227 delayed raising of frames. Imagine a partially obscured frame,
22228 and moving the mouse into partially obscured mouse-face on that
22229 frame. The visible part of the mouse-face will be highlighted,
22230 then the WM raises the obscured frame. With at least one WM, KDE
22231 2.1, Emacs is not getting any event for the raising of the frame
22232 (even tried with SubstructureRedirectMask), only Expose events.
22233 These expose events will draw text normally, i.e. not
22234 highlighted. Which means we must redo the highlight here.
22235 Subsume it under ``we love X''. --gerd 2001-08-15 */
22236 /* Included in Windows version because Windows most likely does not
22237 do the right thing if any third party tool offers
22238 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22239 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22240 {
22241 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22242 if (f == dpyinfo->mouse_face_mouse_frame)
22243 {
22244 int x = dpyinfo->mouse_face_mouse_x;
22245 int y = dpyinfo->mouse_face_mouse_y;
22246 clear_mouse_face (dpyinfo);
22247 note_mouse_highlight (f, x, y);
22248 }
22249 }
22250 }
22251
22252
22253 /* EXPORT:
22254 Determine the intersection of two rectangles R1 and R2. Return
22255 the intersection in *RESULT. Value is non-zero if RESULT is not
22256 empty. */
22257
22258 int
22259 x_intersect_rectangles (r1, r2, result)
22260 XRectangle *r1, *r2, *result;
22261 {
22262 XRectangle *left, *right;
22263 XRectangle *upper, *lower;
22264 int intersection_p = 0;
22265
22266 /* Rearrange so that R1 is the left-most rectangle. */
22267 if (r1->x < r2->x)
22268 left = r1, right = r2;
22269 else
22270 left = r2, right = r1;
22271
22272 /* X0 of the intersection is right.x0, if this is inside R1,
22273 otherwise there is no intersection. */
22274 if (right->x <= left->x + left->width)
22275 {
22276 result->x = right->x;
22277
22278 /* The right end of the intersection is the minimum of the
22279 the right ends of left and right. */
22280 result->width = (min (left->x + left->width, right->x + right->width)
22281 - result->x);
22282
22283 /* Same game for Y. */
22284 if (r1->y < r2->y)
22285 upper = r1, lower = r2;
22286 else
22287 upper = r2, lower = r1;
22288
22289 /* The upper end of the intersection is lower.y0, if this is inside
22290 of upper. Otherwise, there is no intersection. */
22291 if (lower->y <= upper->y + upper->height)
22292 {
22293 result->y = lower->y;
22294
22295 /* The lower end of the intersection is the minimum of the lower
22296 ends of upper and lower. */
22297 result->height = (min (lower->y + lower->height,
22298 upper->y + upper->height)
22299 - result->y);
22300 intersection_p = 1;
22301 }
22302 }
22303
22304 return intersection_p;
22305 }
22306
22307 #endif /* HAVE_WINDOW_SYSTEM */
22308
22309 \f
22310 /***********************************************************************
22311 Initialization
22312 ***********************************************************************/
22313
22314 void
22315 syms_of_xdisp ()
22316 {
22317 Vwith_echo_area_save_vector = Qnil;
22318 staticpro (&Vwith_echo_area_save_vector);
22319
22320 Vmessage_stack = Qnil;
22321 staticpro (&Vmessage_stack);
22322
22323 Qinhibit_redisplay = intern ("inhibit-redisplay");
22324 staticpro (&Qinhibit_redisplay);
22325
22326 message_dolog_marker1 = Fmake_marker ();
22327 staticpro (&message_dolog_marker1);
22328 message_dolog_marker2 = Fmake_marker ();
22329 staticpro (&message_dolog_marker2);
22330 message_dolog_marker3 = Fmake_marker ();
22331 staticpro (&message_dolog_marker3);
22332
22333 #if GLYPH_DEBUG
22334 defsubr (&Sdump_frame_glyph_matrix);
22335 defsubr (&Sdump_glyph_matrix);
22336 defsubr (&Sdump_glyph_row);
22337 defsubr (&Sdump_tool_bar_row);
22338 defsubr (&Strace_redisplay);
22339 defsubr (&Strace_to_stderr);
22340 #endif
22341 #ifdef HAVE_WINDOW_SYSTEM
22342 defsubr (&Stool_bar_lines_needed);
22343 defsubr (&Slookup_image_map);
22344 #endif
22345 defsubr (&Sformat_mode_line);
22346
22347 staticpro (&Qmenu_bar_update_hook);
22348 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22349
22350 staticpro (&Qoverriding_terminal_local_map);
22351 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22352
22353 staticpro (&Qoverriding_local_map);
22354 Qoverriding_local_map = intern ("overriding-local-map");
22355
22356 staticpro (&Qwindow_scroll_functions);
22357 Qwindow_scroll_functions = intern ("window-scroll-functions");
22358
22359 staticpro (&Qredisplay_end_trigger_functions);
22360 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22361
22362 staticpro (&Qinhibit_point_motion_hooks);
22363 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22364
22365 QCdata = intern (":data");
22366 staticpro (&QCdata);
22367 Qdisplay = intern ("display");
22368 staticpro (&Qdisplay);
22369 Qspace_width = intern ("space-width");
22370 staticpro (&Qspace_width);
22371 Qraise = intern ("raise");
22372 staticpro (&Qraise);
22373 Qslice = intern ("slice");
22374 staticpro (&Qslice);
22375 Qspace = intern ("space");
22376 staticpro (&Qspace);
22377 Qmargin = intern ("margin");
22378 staticpro (&Qmargin);
22379 Qpointer = intern ("pointer");
22380 staticpro (&Qpointer);
22381 Qleft_margin = intern ("left-margin");
22382 staticpro (&Qleft_margin);
22383 Qright_margin = intern ("right-margin");
22384 staticpro (&Qright_margin);
22385 Qcenter = intern ("center");
22386 staticpro (&Qcenter);
22387 Qline_height = intern ("line-height");
22388 staticpro (&Qline_height);
22389 QCalign_to = intern (":align-to");
22390 staticpro (&QCalign_to);
22391 QCrelative_width = intern (":relative-width");
22392 staticpro (&QCrelative_width);
22393 QCrelative_height = intern (":relative-height");
22394 staticpro (&QCrelative_height);
22395 QCeval = intern (":eval");
22396 staticpro (&QCeval);
22397 QCpropertize = intern (":propertize");
22398 staticpro (&QCpropertize);
22399 QCfile = intern (":file");
22400 staticpro (&QCfile);
22401 Qfontified = intern ("fontified");
22402 staticpro (&Qfontified);
22403 Qfontification_functions = intern ("fontification-functions");
22404 staticpro (&Qfontification_functions);
22405 Qtrailing_whitespace = intern ("trailing-whitespace");
22406 staticpro (&Qtrailing_whitespace);
22407 Qescape_glyph = intern ("escape-glyph");
22408 staticpro (&Qescape_glyph);
22409 Qimage = intern ("image");
22410 staticpro (&Qimage);
22411 QCmap = intern (":map");
22412 staticpro (&QCmap);
22413 QCpointer = intern (":pointer");
22414 staticpro (&QCpointer);
22415 Qrect = intern ("rect");
22416 staticpro (&Qrect);
22417 Qcircle = intern ("circle");
22418 staticpro (&Qcircle);
22419 Qpoly = intern ("poly");
22420 staticpro (&Qpoly);
22421 Qmessage_truncate_lines = intern ("message-truncate-lines");
22422 staticpro (&Qmessage_truncate_lines);
22423 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22424 staticpro (&Qcursor_in_non_selected_windows);
22425 Qgrow_only = intern ("grow-only");
22426 staticpro (&Qgrow_only);
22427 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22428 staticpro (&Qinhibit_menubar_update);
22429 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22430 staticpro (&Qinhibit_eval_during_redisplay);
22431 Qposition = intern ("position");
22432 staticpro (&Qposition);
22433 Qbuffer_position = intern ("buffer-position");
22434 staticpro (&Qbuffer_position);
22435 Qobject = intern ("object");
22436 staticpro (&Qobject);
22437 Qbar = intern ("bar");
22438 staticpro (&Qbar);
22439 Qhbar = intern ("hbar");
22440 staticpro (&Qhbar);
22441 Qbox = intern ("box");
22442 staticpro (&Qbox);
22443 Qhollow = intern ("hollow");
22444 staticpro (&Qhollow);
22445 Qhand = intern ("hand");
22446 staticpro (&Qhand);
22447 Qarrow = intern ("arrow");
22448 staticpro (&Qarrow);
22449 Qtext = intern ("text");
22450 staticpro (&Qtext);
22451 Qrisky_local_variable = intern ("risky-local-variable");
22452 staticpro (&Qrisky_local_variable);
22453 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22454 staticpro (&Qinhibit_free_realized_faces);
22455
22456 list_of_error = Fcons (Fcons (intern ("error"),
22457 Fcons (intern ("void-variable"), Qnil)),
22458 Qnil);
22459 staticpro (&list_of_error);
22460
22461 Qlast_arrow_position = intern ("last-arrow-position");
22462 staticpro (&Qlast_arrow_position);
22463 Qlast_arrow_string = intern ("last-arrow-string");
22464 staticpro (&Qlast_arrow_string);
22465
22466 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22467 staticpro (&Qoverlay_arrow_string);
22468 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22469 staticpro (&Qoverlay_arrow_bitmap);
22470
22471 echo_buffer[0] = echo_buffer[1] = Qnil;
22472 staticpro (&echo_buffer[0]);
22473 staticpro (&echo_buffer[1]);
22474
22475 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22476 staticpro (&echo_area_buffer[0]);
22477 staticpro (&echo_area_buffer[1]);
22478
22479 Vmessages_buffer_name = build_string ("*Messages*");
22480 staticpro (&Vmessages_buffer_name);
22481
22482 mode_line_proptrans_alist = Qnil;
22483 staticpro (&mode_line_proptrans_alist);
22484
22485 mode_line_string_list = Qnil;
22486 staticpro (&mode_line_string_list);
22487
22488 help_echo_string = Qnil;
22489 staticpro (&help_echo_string);
22490 help_echo_object = Qnil;
22491 staticpro (&help_echo_object);
22492 help_echo_window = Qnil;
22493 staticpro (&help_echo_window);
22494 previous_help_echo_string = Qnil;
22495 staticpro (&previous_help_echo_string);
22496 help_echo_pos = -1;
22497
22498 #ifdef HAVE_WINDOW_SYSTEM
22499 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22500 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22501 For example, if a block cursor is over a tab, it will be drawn as
22502 wide as that tab on the display. */);
22503 x_stretch_cursor_p = 0;
22504 #endif
22505
22506 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22507 doc: /* *Non-nil means highlight trailing whitespace.
22508 The face used for trailing whitespace is `trailing-whitespace'. */);
22509 Vshow_trailing_whitespace = Qnil;
22510
22511 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22512 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22513 Vshow_nonbreak_escape = Qt;
22514
22515 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22516 doc: /* *The pointer shape to show in void text areas.
22517 Nil means to show the text pointer. Other options are `arrow', `text',
22518 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22519 Vvoid_text_area_pointer = Qarrow;
22520
22521 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22522 doc: /* Non-nil means don't actually do any redisplay.
22523 This is used for internal purposes. */);
22524 Vinhibit_redisplay = Qnil;
22525
22526 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22527 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22528 Vglobal_mode_string = Qnil;
22529
22530 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22531 doc: /* Marker for where to display an arrow on top of the buffer text.
22532 This must be the beginning of a line in order to work.
22533 See also `overlay-arrow-string'. */);
22534 Voverlay_arrow_position = Qnil;
22535
22536 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22537 doc: /* String to display as an arrow in non-window frames.
22538 See also `overlay-arrow-position'. */);
22539 Voverlay_arrow_string = Qnil;
22540
22541 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22542 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22543 The symbols on this list are examined during redisplay to determine
22544 where to display overlay arrows. */);
22545 Voverlay_arrow_variable_list
22546 = Fcons (intern ("overlay-arrow-position"), Qnil);
22547
22548 DEFVAR_INT ("scroll-step", &scroll_step,
22549 doc: /* *The number of lines to try scrolling a window by when point moves out.
22550 If that fails to bring point back on frame, point is centered instead.
22551 If this is zero, point is always centered after it moves off frame.
22552 If you want scrolling to always be a line at a time, you should set
22553 `scroll-conservatively' to a large value rather than set this to 1. */);
22554
22555 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22556 doc: /* *Scroll up to this many lines, to bring point back on screen.
22557 A value of zero means to scroll the text to center point vertically
22558 in the window. */);
22559 scroll_conservatively = 0;
22560
22561 DEFVAR_INT ("scroll-margin", &scroll_margin,
22562 doc: /* *Number of lines of margin at the top and bottom of a window.
22563 Recenter the window whenever point gets within this many lines
22564 of the top or bottom of the window. */);
22565 scroll_margin = 0;
22566
22567 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22568 doc: /* Pixels per inch on current display.
22569 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22570 Vdisplay_pixels_per_inch = make_float (72.0);
22571
22572 #if GLYPH_DEBUG
22573 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22574 #endif
22575
22576 DEFVAR_BOOL ("truncate-partial-width-windows",
22577 &truncate_partial_width_windows,
22578 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22579 truncate_partial_width_windows = 1;
22580
22581 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22582 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22583 Any other value means to use the appropriate face, `mode-line',
22584 `header-line', or `menu' respectively. */);
22585 mode_line_inverse_video = 1;
22586
22587 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22588 doc: /* *Maximum buffer size for which line number should be displayed.
22589 If the buffer is bigger than this, the line number does not appear
22590 in the mode line. A value of nil means no limit. */);
22591 Vline_number_display_limit = Qnil;
22592
22593 DEFVAR_INT ("line-number-display-limit-width",
22594 &line_number_display_limit_width,
22595 doc: /* *Maximum line width (in characters) for line number display.
22596 If the average length of the lines near point is bigger than this, then the
22597 line number may be omitted from the mode line. */);
22598 line_number_display_limit_width = 200;
22599
22600 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22601 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22602 highlight_nonselected_windows = 0;
22603
22604 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22605 doc: /* Non-nil if more than one frame is visible on this display.
22606 Minibuffer-only frames don't count, but iconified frames do.
22607 This variable is not guaranteed to be accurate except while processing
22608 `frame-title-format' and `icon-title-format'. */);
22609
22610 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22611 doc: /* Template for displaying the title bar of visible frames.
22612 \(Assuming the window manager supports this feature.)
22613 This variable has the same structure as `mode-line-format' (which see),
22614 and is used only on frames for which no explicit name has been set
22615 \(see `modify-frame-parameters'). */);
22616
22617 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22618 doc: /* Template for displaying the title bar of an iconified frame.
22619 \(Assuming the window manager supports this feature.)
22620 This variable has the same structure as `mode-line-format' (which see),
22621 and is used only on frames for which no explicit name has been set
22622 \(see `modify-frame-parameters'). */);
22623 Vicon_title_format
22624 = Vframe_title_format
22625 = Fcons (intern ("multiple-frames"),
22626 Fcons (build_string ("%b"),
22627 Fcons (Fcons (empty_string,
22628 Fcons (intern ("invocation-name"),
22629 Fcons (build_string ("@"),
22630 Fcons (intern ("system-name"),
22631 Qnil)))),
22632 Qnil)));
22633
22634 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22635 doc: /* Maximum number of lines to keep in the message log buffer.
22636 If nil, disable message logging. If t, log messages but don't truncate
22637 the buffer when it becomes large. */);
22638 Vmessage_log_max = make_number (50);
22639
22640 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22641 doc: /* Functions called before redisplay, if window sizes have changed.
22642 The value should be a list of functions that take one argument.
22643 Just before redisplay, for each frame, if any of its windows have changed
22644 size since the last redisplay, or have been split or deleted,
22645 all the functions in the list are called, with the frame as argument. */);
22646 Vwindow_size_change_functions = Qnil;
22647
22648 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22649 doc: /* List of functions to call before redisplaying a window with scrolling.
22650 Each function is called with two arguments, the window
22651 and its new display-start position. Note that the value of `window-end'
22652 is not valid when these functions are called. */);
22653 Vwindow_scroll_functions = Qnil;
22654
22655 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22656 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22657 mouse_autoselect_window = 0;
22658
22659 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22660 doc: /* *Non-nil means automatically resize tool-bars.
22661 This increases a tool-bar's height if not all tool-bar items are visible.
22662 It decreases a tool-bar's height when it would display blank lines
22663 otherwise. */);
22664 auto_resize_tool_bars_p = 1;
22665
22666 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22667 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22668 auto_raise_tool_bar_buttons_p = 1;
22669
22670 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22671 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22672 make_cursor_line_fully_visible_p = 1;
22673
22674 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22675 doc: /* *Margin around tool-bar buttons in pixels.
22676 If an integer, use that for both horizontal and vertical margins.
22677 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22678 HORZ specifying the horizontal margin, and VERT specifying the
22679 vertical margin. */);
22680 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22681
22682 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22683 doc: /* *Relief thickness of tool-bar buttons. */);
22684 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22685
22686 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22687 doc: /* List of functions to call to fontify regions of text.
22688 Each function is called with one argument POS. Functions must
22689 fontify a region starting at POS in the current buffer, and give
22690 fontified regions the property `fontified'. */);
22691 Vfontification_functions = Qnil;
22692 Fmake_variable_buffer_local (Qfontification_functions);
22693
22694 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22695 &unibyte_display_via_language_environment,
22696 doc: /* *Non-nil means display unibyte text according to language environment.
22697 Specifically this means that unibyte non-ASCII characters
22698 are displayed by converting them to the equivalent multibyte characters
22699 according to the current language environment. As a result, they are
22700 displayed according to the current fontset. */);
22701 unibyte_display_via_language_environment = 0;
22702
22703 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22704 doc: /* *Maximum height for resizing mini-windows.
22705 If a float, it specifies a fraction of the mini-window frame's height.
22706 If an integer, it specifies a number of lines. */);
22707 Vmax_mini_window_height = make_float (0.25);
22708
22709 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22710 doc: /* *How to resize mini-windows.
22711 A value of nil means don't automatically resize mini-windows.
22712 A value of t means resize them to fit the text displayed in them.
22713 A value of `grow-only', the default, means let mini-windows grow
22714 only, until their display becomes empty, at which point the windows
22715 go back to their normal size. */);
22716 Vresize_mini_windows = Qgrow_only;
22717
22718 DEFVAR_LISP ("cursor-in-non-selected-windows",
22719 &Vcursor_in_non_selected_windows,
22720 doc: /* *Cursor type to display in non-selected windows.
22721 t means to use hollow box cursor. See `cursor-type' for other values. */);
22722 Vcursor_in_non_selected_windows = Qt;
22723
22724 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22725 doc: /* Alist specifying how to blink the cursor off.
22726 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22727 `cursor-type' frame-parameter or variable equals ON-STATE,
22728 comparing using `equal', Emacs uses OFF-STATE to specify
22729 how to blink it off. */);
22730 Vblink_cursor_alist = Qnil;
22731
22732 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22733 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22734 automatic_hscrolling_p = 1;
22735
22736 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22737 doc: /* *How many columns away from the window edge point is allowed to get
22738 before automatic hscrolling will horizontally scroll the window. */);
22739 hscroll_margin = 5;
22740
22741 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22742 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22743 When point is less than `automatic-hscroll-margin' columns from the window
22744 edge, automatic hscrolling will scroll the window by the amount of columns
22745 determined by this variable. If its value is a positive integer, scroll that
22746 many columns. If it's a positive floating-point number, it specifies the
22747 fraction of the window's width to scroll. If it's nil or zero, point will be
22748 centered horizontally after the scroll. Any other value, including negative
22749 numbers, are treated as if the value were zero.
22750
22751 Automatic hscrolling always moves point outside the scroll margin, so if
22752 point was more than scroll step columns inside the margin, the window will
22753 scroll more than the value given by the scroll step.
22754
22755 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22756 and `scroll-right' overrides this variable's effect. */);
22757 Vhscroll_step = make_number (0);
22758
22759 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22760 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22761 Bind this around calls to `message' to let it take effect. */);
22762 message_truncate_lines = 0;
22763
22764 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22765 doc: /* Normal hook run for clicks on menu bar, before displaying a submenu.
22766 Can be used to update submenus whose contents should vary. */);
22767 Vmenu_bar_update_hook = Qnil;
22768
22769 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22770 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22771 inhibit_menubar_update = 0;
22772
22773 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22774 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22775 inhibit_eval_during_redisplay = 0;
22776
22777 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22778 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22779 inhibit_free_realized_faces = 0;
22780
22781 #if GLYPH_DEBUG
22782 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22783 doc: /* Inhibit try_window_id display optimization. */);
22784 inhibit_try_window_id = 0;
22785
22786 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22787 doc: /* Inhibit try_window_reusing display optimization. */);
22788 inhibit_try_window_reusing = 0;
22789
22790 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22791 doc: /* Inhibit try_cursor_movement display optimization. */);
22792 inhibit_try_cursor_movement = 0;
22793 #endif /* GLYPH_DEBUG */
22794 }
22795
22796
22797 /* Initialize this module when Emacs starts. */
22798
22799 void
22800 init_xdisp ()
22801 {
22802 Lisp_Object root_window;
22803 struct window *mini_w;
22804
22805 current_header_line_height = current_mode_line_height = -1;
22806
22807 CHARPOS (this_line_start_pos) = 0;
22808
22809 mini_w = XWINDOW (minibuf_window);
22810 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22811
22812 if (!noninteractive)
22813 {
22814 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22815 int i;
22816
22817 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22818 set_window_height (root_window,
22819 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22820 0);
22821 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22822 set_window_height (minibuf_window, 1, 0);
22823
22824 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22825 mini_w->total_cols = make_number (FRAME_COLS (f));
22826
22827 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22828 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22829 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22830
22831 /* The default ellipsis glyphs `...'. */
22832 for (i = 0; i < 3; ++i)
22833 default_invis_vector[i] = make_number ('.');
22834 }
22835
22836 {
22837 /* Allocate the buffer for frame titles.
22838 Also used for `format-mode-line'. */
22839 int size = 100;
22840 frame_title_buf = (char *) xmalloc (size);
22841 frame_title_buf_end = frame_title_buf + size;
22842 frame_title_ptr = NULL;
22843 }
22844
22845 help_echo_showing_p = 0;
22846 }
22847
22848
22849 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22850 (do not change this comment) */