]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(Fexpand_file_name) [VMS]: Don't upcase the name
[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 void pint2str P_ ((char *, int, int));
831 static void pint2hrstr P_ ((char *, int, int));
832 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
833 struct text_pos));
834 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
835 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
836 static void store_frame_title_char P_ ((char));
837 static int store_frame_title P_ ((const unsigned char *, int, int));
838 static void x_consider_frame_title P_ ((Lisp_Object));
839 static void handle_stop P_ ((struct it *));
840 static int tool_bar_lines_needed P_ ((struct frame *));
841 static int single_display_spec_intangible_p P_ ((Lisp_Object));
842 static void ensure_echo_area_buffers P_ ((void));
843 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
844 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
845 static int with_echo_area_buffer P_ ((struct window *, int,
846 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
847 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
848 static void clear_garbaged_frames P_ ((void));
849 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
850 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
851 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
852 static int display_echo_area P_ ((struct window *));
853 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
854 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
855 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
856 static int string_char_and_length P_ ((const unsigned char *, int, int *));
857 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
858 struct text_pos));
859 static int compute_window_start_on_continuation_line P_ ((struct window *));
860 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
861 static void insert_left_trunc_glyphs P_ ((struct it *));
862 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
863 Lisp_Object));
864 static void extend_face_to_end_of_line P_ ((struct it *));
865 static int append_space_for_newline P_ ((struct it *, int));
866 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
867 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
868 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
869 static int trailing_whitespace_p P_ ((int));
870 static int message_log_check_duplicate P_ ((int, int, int, int));
871 static void push_it P_ ((struct it *));
872 static void pop_it P_ ((struct it *));
873 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
874 static void select_frame_for_redisplay P_ ((Lisp_Object));
875 static void redisplay_internal P_ ((int));
876 static int echo_area_display P_ ((int));
877 static void redisplay_windows P_ ((Lisp_Object));
878 static void redisplay_window P_ ((Lisp_Object, int));
879 static Lisp_Object redisplay_window_error ();
880 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
881 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
882 static void update_menu_bar P_ ((struct frame *, int));
883 static int try_window_reusing_current_matrix P_ ((struct window *));
884 static int try_window_id P_ ((struct window *));
885 static int display_line P_ ((struct it *));
886 static int display_mode_lines P_ ((struct window *));
887 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
888 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
889 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
890 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
891 static void display_menu_bar P_ ((struct window *));
892 static int display_count_lines P_ ((int, int, int, int, int *));
893 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
894 int, int, struct it *, int, int, int, int));
895 static void compute_line_metrics P_ ((struct it *));
896 static void run_redisplay_end_trigger_hook P_ ((struct it *));
897 static int get_overlay_strings P_ ((struct it *, int));
898 static void next_overlay_string P_ ((struct it *));
899 static void reseat P_ ((struct it *, struct text_pos, int));
900 static void reseat_1 P_ ((struct it *, struct text_pos, int));
901 static void back_to_previous_visible_line_start P_ ((struct it *));
902 void reseat_at_previous_visible_line_start P_ ((struct it *));
903 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
904 static int next_element_from_ellipsis P_ ((struct it *));
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, -1, it.last_visible_y, -1,
1289 MOVE_TO_POS | 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
1314 {
1315 struct it it2;
1316
1317 it2 = it;
1318 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1319 move_it_by_lines (&it, 1, 0);
1320 if (charpos < IT_CHARPOS (it))
1321 {
1322 visible_p = 1;
1323 if (x)
1324 {
1325 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1326 *x = it2.current_x;
1327 *y = it2.current_y + it2.max_ascent - it2.ascent;
1328 if (rtop)
1329 {
1330 *rtop = max (0, -it2.current_y);
1331 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1332 - it.last_visible_y));
1333 }
1334 }
1335 }
1336 }
1337
1338 if (old_buffer)
1339 set_buffer_internal_1 (old_buffer);
1340
1341 current_header_line_height = current_mode_line_height = -1;
1342
1343 return visible_p;
1344 }
1345
1346
1347 /* Return the next character from STR which is MAXLEN bytes long.
1348 Return in *LEN the length of the character. This is like
1349 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1350 we find one, we return a `?', but with the length of the invalid
1351 character. */
1352
1353 static INLINE int
1354 string_char_and_length (str, maxlen, len)
1355 const unsigned char *str;
1356 int maxlen, *len;
1357 {
1358 int c;
1359
1360 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1361 if (!CHAR_VALID_P (c, 1))
1362 /* We may not change the length here because other places in Emacs
1363 don't use this function, i.e. they silently accept invalid
1364 characters. */
1365 c = '?';
1366
1367 return c;
1368 }
1369
1370
1371
1372 /* Given a position POS containing a valid character and byte position
1373 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1374
1375 static struct text_pos
1376 string_pos_nchars_ahead (pos, string, nchars)
1377 struct text_pos pos;
1378 Lisp_Object string;
1379 int nchars;
1380 {
1381 xassert (STRINGP (string) && nchars >= 0);
1382
1383 if (STRING_MULTIBYTE (string))
1384 {
1385 int rest = SBYTES (string) - BYTEPOS (pos);
1386 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1387 int len;
1388
1389 while (nchars--)
1390 {
1391 string_char_and_length (p, rest, &len);
1392 p += len, rest -= len;
1393 xassert (rest >= 0);
1394 CHARPOS (pos) += 1;
1395 BYTEPOS (pos) += len;
1396 }
1397 }
1398 else
1399 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1400
1401 return pos;
1402 }
1403
1404
1405 /* Value is the text position, i.e. character and byte position,
1406 for character position CHARPOS in STRING. */
1407
1408 static INLINE struct text_pos
1409 string_pos (charpos, string)
1410 int charpos;
1411 Lisp_Object string;
1412 {
1413 struct text_pos pos;
1414 xassert (STRINGP (string));
1415 xassert (charpos >= 0);
1416 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1417 return pos;
1418 }
1419
1420
1421 /* Value is a text position, i.e. character and byte position, for
1422 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1423 means recognize multibyte characters. */
1424
1425 static struct text_pos
1426 c_string_pos (charpos, s, multibyte_p)
1427 int charpos;
1428 unsigned char *s;
1429 int multibyte_p;
1430 {
1431 struct text_pos pos;
1432
1433 xassert (s != NULL);
1434 xassert (charpos >= 0);
1435
1436 if (multibyte_p)
1437 {
1438 int rest = strlen (s), len;
1439
1440 SET_TEXT_POS (pos, 0, 0);
1441 while (charpos--)
1442 {
1443 string_char_and_length (s, rest, &len);
1444 s += len, rest -= len;
1445 xassert (rest >= 0);
1446 CHARPOS (pos) += 1;
1447 BYTEPOS (pos) += len;
1448 }
1449 }
1450 else
1451 SET_TEXT_POS (pos, charpos, charpos);
1452
1453 return pos;
1454 }
1455
1456
1457 /* Value is the number of characters in C string S. MULTIBYTE_P
1458 non-zero means recognize multibyte characters. */
1459
1460 static int
1461 number_of_chars (s, multibyte_p)
1462 unsigned char *s;
1463 int multibyte_p;
1464 {
1465 int nchars;
1466
1467 if (multibyte_p)
1468 {
1469 int rest = strlen (s), len;
1470 unsigned char *p = (unsigned char *) s;
1471
1472 for (nchars = 0; rest > 0; ++nchars)
1473 {
1474 string_char_and_length (p, rest, &len);
1475 rest -= len, p += len;
1476 }
1477 }
1478 else
1479 nchars = strlen (s);
1480
1481 return nchars;
1482 }
1483
1484
1485 /* Compute byte position NEWPOS->bytepos corresponding to
1486 NEWPOS->charpos. POS is a known position in string STRING.
1487 NEWPOS->charpos must be >= POS.charpos. */
1488
1489 static void
1490 compute_string_pos (newpos, pos, string)
1491 struct text_pos *newpos, pos;
1492 Lisp_Object string;
1493 {
1494 xassert (STRINGP (string));
1495 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1496
1497 if (STRING_MULTIBYTE (string))
1498 *newpos = string_pos_nchars_ahead (pos, string,
1499 CHARPOS (*newpos) - CHARPOS (pos));
1500 else
1501 BYTEPOS (*newpos) = CHARPOS (*newpos);
1502 }
1503
1504 /* EXPORT:
1505 Return an estimation of the pixel height of mode or top lines on
1506 frame F. FACE_ID specifies what line's height to estimate. */
1507
1508 int
1509 estimate_mode_line_height (f, face_id)
1510 struct frame *f;
1511 enum face_id face_id;
1512 {
1513 #ifdef HAVE_WINDOW_SYSTEM
1514 if (FRAME_WINDOW_P (f))
1515 {
1516 int height = FONT_HEIGHT (FRAME_FONT (f));
1517
1518 /* This function is called so early when Emacs starts that the face
1519 cache and mode line face are not yet initialized. */
1520 if (FRAME_FACE_CACHE (f))
1521 {
1522 struct face *face = FACE_FROM_ID (f, face_id);
1523 if (face)
1524 {
1525 if (face->font)
1526 height = FONT_HEIGHT (face->font);
1527 if (face->box_line_width > 0)
1528 height += 2 * face->box_line_width;
1529 }
1530 }
1531
1532 return height;
1533 }
1534 #endif
1535
1536 return 1;
1537 }
1538
1539 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1540 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1541 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1542 not force the value into range. */
1543
1544 void
1545 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1546 FRAME_PTR f;
1547 register int pix_x, pix_y;
1548 int *x, *y;
1549 NativeRectangle *bounds;
1550 int noclip;
1551 {
1552
1553 #ifdef HAVE_WINDOW_SYSTEM
1554 if (FRAME_WINDOW_P (f))
1555 {
1556 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1557 even for negative values. */
1558 if (pix_x < 0)
1559 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1560 if (pix_y < 0)
1561 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1562
1563 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1564 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1565
1566 if (bounds)
1567 STORE_NATIVE_RECT (*bounds,
1568 FRAME_COL_TO_PIXEL_X (f, pix_x),
1569 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1570 FRAME_COLUMN_WIDTH (f) - 1,
1571 FRAME_LINE_HEIGHT (f) - 1);
1572
1573 if (!noclip)
1574 {
1575 if (pix_x < 0)
1576 pix_x = 0;
1577 else if (pix_x > FRAME_TOTAL_COLS (f))
1578 pix_x = FRAME_TOTAL_COLS (f);
1579
1580 if (pix_y < 0)
1581 pix_y = 0;
1582 else if (pix_y > FRAME_LINES (f))
1583 pix_y = FRAME_LINES (f);
1584 }
1585 }
1586 #endif
1587
1588 *x = pix_x;
1589 *y = pix_y;
1590 }
1591
1592
1593 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1594 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1595 can't tell the positions because W's display is not up to date,
1596 return 0. */
1597
1598 int
1599 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1600 struct window *w;
1601 int hpos, vpos;
1602 int *frame_x, *frame_y;
1603 {
1604 #ifdef HAVE_WINDOW_SYSTEM
1605 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1606 {
1607 int success_p;
1608
1609 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1610 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1611
1612 if (display_completed)
1613 {
1614 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1615 struct glyph *glyph = row->glyphs[TEXT_AREA];
1616 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1617
1618 hpos = row->x;
1619 vpos = row->y;
1620 while (glyph < end)
1621 {
1622 hpos += glyph->pixel_width;
1623 ++glyph;
1624 }
1625
1626 /* If first glyph is partially visible, its first visible position is still 0. */
1627 if (hpos < 0)
1628 hpos = 0;
1629
1630 success_p = 1;
1631 }
1632 else
1633 {
1634 hpos = vpos = 0;
1635 success_p = 0;
1636 }
1637
1638 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1639 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1640 return success_p;
1641 }
1642 #endif
1643
1644 *frame_x = hpos;
1645 *frame_y = vpos;
1646 return 1;
1647 }
1648
1649
1650 #ifdef HAVE_WINDOW_SYSTEM
1651
1652 /* Find the glyph under window-relative coordinates X/Y in window W.
1653 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1654 strings. Return in *HPOS and *VPOS the row and column number of
1655 the glyph found. Return in *AREA the glyph area containing X.
1656 Value is a pointer to the glyph found or null if X/Y is not on
1657 text, or we can't tell because W's current matrix is not up to
1658 date. */
1659
1660 static struct glyph *
1661 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1662 struct window *w;
1663 int x, y;
1664 int *hpos, *vpos, *dx, *dy, *area;
1665 {
1666 struct glyph *glyph, *end;
1667 struct glyph_row *row = NULL;
1668 int x0, i;
1669
1670 /* Find row containing Y. Give up if some row is not enabled. */
1671 for (i = 0; i < w->current_matrix->nrows; ++i)
1672 {
1673 row = MATRIX_ROW (w->current_matrix, i);
1674 if (!row->enabled_p)
1675 return NULL;
1676 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1677 break;
1678 }
1679
1680 *vpos = i;
1681 *hpos = 0;
1682
1683 /* Give up if Y is not in the window. */
1684 if (i == w->current_matrix->nrows)
1685 return NULL;
1686
1687 /* Get the glyph area containing X. */
1688 if (w->pseudo_window_p)
1689 {
1690 *area = TEXT_AREA;
1691 x0 = 0;
1692 }
1693 else
1694 {
1695 if (x < window_box_left_offset (w, TEXT_AREA))
1696 {
1697 *area = LEFT_MARGIN_AREA;
1698 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1699 }
1700 else if (x < window_box_right_offset (w, TEXT_AREA))
1701 {
1702 *area = TEXT_AREA;
1703 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1704 }
1705 else
1706 {
1707 *area = RIGHT_MARGIN_AREA;
1708 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1709 }
1710 }
1711
1712 /* Find glyph containing X. */
1713 glyph = row->glyphs[*area];
1714 end = glyph + row->used[*area];
1715 x -= x0;
1716 while (glyph < end && x >= glyph->pixel_width)
1717 {
1718 x -= glyph->pixel_width;
1719 ++glyph;
1720 }
1721
1722 if (glyph == end)
1723 return NULL;
1724
1725 if (dx)
1726 {
1727 *dx = x;
1728 *dy = y - (row->y + row->ascent - glyph->ascent);
1729 }
1730
1731 *hpos = glyph - row->glyphs[*area];
1732 return glyph;
1733 }
1734
1735
1736 /* EXPORT:
1737 Convert frame-relative x/y to coordinates relative to window W.
1738 Takes pseudo-windows into account. */
1739
1740 void
1741 frame_to_window_pixel_xy (w, x, y)
1742 struct window *w;
1743 int *x, *y;
1744 {
1745 if (w->pseudo_window_p)
1746 {
1747 /* A pseudo-window is always full-width, and starts at the
1748 left edge of the frame, plus a frame border. */
1749 struct frame *f = XFRAME (w->frame);
1750 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1751 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1752 }
1753 else
1754 {
1755 *x -= WINDOW_LEFT_EDGE_X (w);
1756 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1757 }
1758 }
1759
1760 /* EXPORT:
1761 Return in *R the clipping rectangle for glyph string S. */
1762
1763 void
1764 get_glyph_string_clip_rect (s, nr)
1765 struct glyph_string *s;
1766 NativeRectangle *nr;
1767 {
1768 XRectangle r;
1769
1770 if (s->row->full_width_p)
1771 {
1772 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1773 r.x = WINDOW_LEFT_EDGE_X (s->w);
1774 r.width = WINDOW_TOTAL_WIDTH (s->w);
1775
1776 /* Unless displaying a mode or menu bar line, which are always
1777 fully visible, clip to the visible part of the row. */
1778 if (s->w->pseudo_window_p)
1779 r.height = s->row->visible_height;
1780 else
1781 r.height = s->height;
1782 }
1783 else
1784 {
1785 /* This is a text line that may be partially visible. */
1786 r.x = window_box_left (s->w, s->area);
1787 r.width = window_box_width (s->w, s->area);
1788 r.height = s->row->visible_height;
1789 }
1790
1791 if (s->clip_head)
1792 if (r.x < s->clip_head->x)
1793 {
1794 if (r.width >= s->clip_head->x - r.x)
1795 r.width -= s->clip_head->x - r.x;
1796 else
1797 r.width = 0;
1798 r.x = s->clip_head->x;
1799 }
1800 if (s->clip_tail)
1801 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1802 {
1803 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1804 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1805 else
1806 r.width = 0;
1807 }
1808
1809 /* If S draws overlapping rows, it's sufficient to use the top and
1810 bottom of the window for clipping because this glyph string
1811 intentionally draws over other lines. */
1812 if (s->for_overlaps_p)
1813 {
1814 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1815 r.height = window_text_bottom_y (s->w) - r.y;
1816 }
1817 else
1818 {
1819 /* Don't use S->y for clipping because it doesn't take partially
1820 visible lines into account. For example, it can be negative for
1821 partially visible lines at the top of a window. */
1822 if (!s->row->full_width_p
1823 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1824 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1825 else
1826 r.y = max (0, s->row->y);
1827
1828 /* If drawing a tool-bar window, draw it over the internal border
1829 at the top of the window. */
1830 if (WINDOWP (s->f->tool_bar_window)
1831 && s->w == XWINDOW (s->f->tool_bar_window))
1832 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1833 }
1834
1835 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1836
1837 /* If drawing the cursor, don't let glyph draw outside its
1838 advertised boundaries. Cleartype does this under some circumstances. */
1839 if (s->hl == DRAW_CURSOR)
1840 {
1841 struct glyph *glyph = s->first_glyph;
1842 int height, max_y;
1843
1844 if (s->x > r.x)
1845 {
1846 r.width -= s->x - r.x;
1847 r.x = s->x;
1848 }
1849 r.width = min (r.width, glyph->pixel_width);
1850
1851 /* If r.y is below window bottom, ensure that we still see a cursor. */
1852 height = min (glyph->ascent + glyph->descent,
1853 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1854 max_y = window_text_bottom_y (s->w) - height;
1855 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1856 if (s->ybase - glyph->ascent > max_y)
1857 {
1858 r.y = max_y;
1859 r.height = height;
1860 }
1861 else
1862 {
1863 /* Don't draw cursor glyph taller than our actual glyph. */
1864 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1865 if (height < r.height)
1866 {
1867 max_y = r.y + r.height;
1868 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1869 r.height = min (max_y - r.y, height);
1870 }
1871 }
1872 }
1873
1874 #ifdef CONVERT_FROM_XRECT
1875 CONVERT_FROM_XRECT (r, *nr);
1876 #else
1877 *nr = r;
1878 #endif
1879 }
1880
1881
1882 /* EXPORT:
1883 Return the position and height of the phys cursor in window W.
1884 Set w->phys_cursor_width to width of phys cursor.
1885 */
1886
1887 int
1888 get_phys_cursor_geometry (w, row, glyph, heightp)
1889 struct window *w;
1890 struct glyph_row *row;
1891 struct glyph *glyph;
1892 int *heightp;
1893 {
1894 struct frame *f = XFRAME (WINDOW_FRAME (w));
1895 int x, y, wd, h, h0, y0;
1896
1897 /* Compute the width of the rectangle to draw. If on a stretch
1898 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1899 rectangle as wide as the glyph, but use a canonical character
1900 width instead. */
1901 wd = glyph->pixel_width - 1;
1902 #ifdef HAVE_NTGUI
1903 wd++; /* Why? */
1904 #endif
1905 if (glyph->type == STRETCH_GLYPH
1906 && !x_stretch_cursor_p)
1907 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1908 w->phys_cursor_width = wd;
1909
1910 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1911
1912 /* If y is below window bottom, ensure that we still see a cursor. */
1913 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1914
1915 h = max (h0, glyph->ascent + glyph->descent);
1916 h0 = min (h0, glyph->ascent + glyph->descent);
1917
1918 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1919 if (y < y0)
1920 {
1921 h = max (h - (y0 - y) + 1, h0);
1922 y = y0 - 1;
1923 }
1924 else
1925 {
1926 y0 = window_text_bottom_y (w) - h0;
1927 if (y > y0)
1928 {
1929 h += y - y0;
1930 y = y0;
1931 }
1932 }
1933
1934 *heightp = h - 1;
1935 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1936 }
1937
1938
1939 #endif /* HAVE_WINDOW_SYSTEM */
1940
1941 \f
1942 /***********************************************************************
1943 Lisp form evaluation
1944 ***********************************************************************/
1945
1946 /* Error handler for safe_eval and safe_call. */
1947
1948 static Lisp_Object
1949 safe_eval_handler (arg)
1950 Lisp_Object arg;
1951 {
1952 add_to_log ("Error during redisplay: %s", arg, Qnil);
1953 return Qnil;
1954 }
1955
1956
1957 /* Evaluate SEXPR and return the result, or nil if something went
1958 wrong. Prevent redisplay during the evaluation. */
1959
1960 Lisp_Object
1961 safe_eval (sexpr)
1962 Lisp_Object sexpr;
1963 {
1964 Lisp_Object val;
1965
1966 if (inhibit_eval_during_redisplay)
1967 val = Qnil;
1968 else
1969 {
1970 int count = SPECPDL_INDEX ();
1971 struct gcpro gcpro1;
1972
1973 GCPRO1 (sexpr);
1974 specbind (Qinhibit_redisplay, Qt);
1975 /* Use Qt to ensure debugger does not run,
1976 so there is no possibility of wanting to redisplay. */
1977 val = internal_condition_case_1 (Feval, sexpr, Qt,
1978 safe_eval_handler);
1979 UNGCPRO;
1980 val = unbind_to (count, val);
1981 }
1982
1983 return val;
1984 }
1985
1986
1987 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1988 Return the result, or nil if something went wrong. Prevent
1989 redisplay during the evaluation. */
1990
1991 Lisp_Object
1992 safe_call (nargs, args)
1993 int nargs;
1994 Lisp_Object *args;
1995 {
1996 Lisp_Object val;
1997
1998 if (inhibit_eval_during_redisplay)
1999 val = Qnil;
2000 else
2001 {
2002 int count = SPECPDL_INDEX ();
2003 struct gcpro gcpro1;
2004
2005 GCPRO1 (args[0]);
2006 gcpro1.nvars = nargs;
2007 specbind (Qinhibit_redisplay, Qt);
2008 /* Use Qt to ensure debugger does not run,
2009 so there is no possibility of wanting to redisplay. */
2010 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2011 safe_eval_handler);
2012 UNGCPRO;
2013 val = unbind_to (count, val);
2014 }
2015
2016 return val;
2017 }
2018
2019
2020 /* Call function FN with one argument ARG.
2021 Return the result, or nil if something went wrong. */
2022
2023 Lisp_Object
2024 safe_call1 (fn, arg)
2025 Lisp_Object fn, arg;
2026 {
2027 Lisp_Object args[2];
2028 args[0] = fn;
2029 args[1] = arg;
2030 return safe_call (2, args);
2031 }
2032
2033
2034 \f
2035 /***********************************************************************
2036 Debugging
2037 ***********************************************************************/
2038
2039 #if 0
2040
2041 /* Define CHECK_IT to perform sanity checks on iterators.
2042 This is for debugging. It is too slow to do unconditionally. */
2043
2044 static void
2045 check_it (it)
2046 struct it *it;
2047 {
2048 if (it->method == GET_FROM_STRING)
2049 {
2050 xassert (STRINGP (it->string));
2051 xassert (IT_STRING_CHARPOS (*it) >= 0);
2052 }
2053 else
2054 {
2055 xassert (IT_STRING_CHARPOS (*it) < 0);
2056 if (it->method == GET_FROM_BUFFER)
2057 {
2058 /* Check that character and byte positions agree. */
2059 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2060 }
2061 }
2062
2063 if (it->dpvec)
2064 xassert (it->current.dpvec_index >= 0);
2065 else
2066 xassert (it->current.dpvec_index < 0);
2067 }
2068
2069 #define CHECK_IT(IT) check_it ((IT))
2070
2071 #else /* not 0 */
2072
2073 #define CHECK_IT(IT) (void) 0
2074
2075 #endif /* not 0 */
2076
2077
2078 #if GLYPH_DEBUG
2079
2080 /* Check that the window end of window W is what we expect it
2081 to be---the last row in the current matrix displaying text. */
2082
2083 static void
2084 check_window_end (w)
2085 struct window *w;
2086 {
2087 if (!MINI_WINDOW_P (w)
2088 && !NILP (w->window_end_valid))
2089 {
2090 struct glyph_row *row;
2091 xassert ((row = MATRIX_ROW (w->current_matrix,
2092 XFASTINT (w->window_end_vpos)),
2093 !row->enabled_p
2094 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2095 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2096 }
2097 }
2098
2099 #define CHECK_WINDOW_END(W) check_window_end ((W))
2100
2101 #else /* not GLYPH_DEBUG */
2102
2103 #define CHECK_WINDOW_END(W) (void) 0
2104
2105 #endif /* not GLYPH_DEBUG */
2106
2107
2108 \f
2109 /***********************************************************************
2110 Iterator initialization
2111 ***********************************************************************/
2112
2113 /* Initialize IT for displaying current_buffer in window W, starting
2114 at character position CHARPOS. CHARPOS < 0 means that no buffer
2115 position is specified which is useful when the iterator is assigned
2116 a position later. BYTEPOS is the byte position corresponding to
2117 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2118
2119 If ROW is not null, calls to produce_glyphs with IT as parameter
2120 will produce glyphs in that row.
2121
2122 BASE_FACE_ID is the id of a base face to use. It must be one of
2123 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2124 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2125 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2126
2127 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2128 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2129 will be initialized to use the corresponding mode line glyph row of
2130 the desired matrix of W. */
2131
2132 void
2133 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2134 struct it *it;
2135 struct window *w;
2136 int charpos, bytepos;
2137 struct glyph_row *row;
2138 enum face_id base_face_id;
2139 {
2140 int highlight_region_p;
2141
2142 /* Some precondition checks. */
2143 xassert (w != NULL && it != NULL);
2144 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2145 && charpos <= ZV));
2146
2147 /* If face attributes have been changed since the last redisplay,
2148 free realized faces now because they depend on face definitions
2149 that might have changed. Don't free faces while there might be
2150 desired matrices pending which reference these faces. */
2151 if (face_change_count && !inhibit_free_realized_faces)
2152 {
2153 face_change_count = 0;
2154 free_all_realized_faces (Qnil);
2155 }
2156
2157 /* Use one of the mode line rows of W's desired matrix if
2158 appropriate. */
2159 if (row == NULL)
2160 {
2161 if (base_face_id == MODE_LINE_FACE_ID
2162 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2163 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2164 else if (base_face_id == HEADER_LINE_FACE_ID)
2165 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2166 }
2167
2168 /* Clear IT. */
2169 bzero (it, sizeof *it);
2170 it->current.overlay_string_index = -1;
2171 it->current.dpvec_index = -1;
2172 it->base_face_id = base_face_id;
2173 it->string = Qnil;
2174 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2175
2176 /* The window in which we iterate over current_buffer: */
2177 XSETWINDOW (it->window, w);
2178 it->w = w;
2179 it->f = XFRAME (w->frame);
2180
2181 /* Extra space between lines (on window systems only). */
2182 if (base_face_id == DEFAULT_FACE_ID
2183 && FRAME_WINDOW_P (it->f))
2184 {
2185 if (NATNUMP (current_buffer->extra_line_spacing))
2186 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2187 else if (FLOATP (current_buffer->extra_line_spacing))
2188 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2189 * FRAME_LINE_HEIGHT (it->f));
2190 else if (it->f->extra_line_spacing > 0)
2191 it->extra_line_spacing = it->f->extra_line_spacing;
2192 it->max_extra_line_spacing = 0;
2193 }
2194
2195 /* If realized faces have been removed, e.g. because of face
2196 attribute changes of named faces, recompute them. When running
2197 in batch mode, the face cache of Vterminal_frame is null. If
2198 we happen to get called, make a dummy face cache. */
2199 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2200 init_frame_faces (it->f);
2201 if (FRAME_FACE_CACHE (it->f)->used == 0)
2202 recompute_basic_faces (it->f);
2203
2204 /* Current value of the `slice', `space-width', and 'height' properties. */
2205 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2206 it->space_width = Qnil;
2207 it->font_height = Qnil;
2208 it->override_ascent = -1;
2209
2210 /* Are control characters displayed as `^C'? */
2211 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2212
2213 /* -1 means everything between a CR and the following line end
2214 is invisible. >0 means lines indented more than this value are
2215 invisible. */
2216 it->selective = (INTEGERP (current_buffer->selective_display)
2217 ? XFASTINT (current_buffer->selective_display)
2218 : (!NILP (current_buffer->selective_display)
2219 ? -1 : 0));
2220 it->selective_display_ellipsis_p
2221 = !NILP (current_buffer->selective_display_ellipses);
2222
2223 /* Display table to use. */
2224 it->dp = window_display_table (w);
2225
2226 /* Are multibyte characters enabled in current_buffer? */
2227 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2228
2229 /* Non-zero if we should highlight the region. */
2230 highlight_region_p
2231 = (!NILP (Vtransient_mark_mode)
2232 && !NILP (current_buffer->mark_active)
2233 && XMARKER (current_buffer->mark)->buffer != 0);
2234
2235 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2236 start and end of a visible region in window IT->w. Set both to
2237 -1 to indicate no region. */
2238 if (highlight_region_p
2239 /* Maybe highlight only in selected window. */
2240 && (/* Either show region everywhere. */
2241 highlight_nonselected_windows
2242 /* Or show region in the selected window. */
2243 || w == XWINDOW (selected_window)
2244 /* Or show the region if we are in the mini-buffer and W is
2245 the window the mini-buffer refers to. */
2246 || (MINI_WINDOW_P (XWINDOW (selected_window))
2247 && WINDOWP (minibuf_selected_window)
2248 && w == XWINDOW (minibuf_selected_window))))
2249 {
2250 int charpos = marker_position (current_buffer->mark);
2251 it->region_beg_charpos = min (PT, charpos);
2252 it->region_end_charpos = max (PT, charpos);
2253 }
2254 else
2255 it->region_beg_charpos = it->region_end_charpos = -1;
2256
2257 /* Get the position at which the redisplay_end_trigger hook should
2258 be run, if it is to be run at all. */
2259 if (MARKERP (w->redisplay_end_trigger)
2260 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2261 it->redisplay_end_trigger_charpos
2262 = marker_position (w->redisplay_end_trigger);
2263 else if (INTEGERP (w->redisplay_end_trigger))
2264 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2265
2266 /* Correct bogus values of tab_width. */
2267 it->tab_width = XINT (current_buffer->tab_width);
2268 if (it->tab_width <= 0 || it->tab_width > 1000)
2269 it->tab_width = 8;
2270
2271 /* Are lines in the display truncated? */
2272 it->truncate_lines_p
2273 = (base_face_id != DEFAULT_FACE_ID
2274 || XINT (it->w->hscroll)
2275 || (truncate_partial_width_windows
2276 && !WINDOW_FULL_WIDTH_P (it->w))
2277 || !NILP (current_buffer->truncate_lines));
2278
2279 /* Get dimensions of truncation and continuation glyphs. These are
2280 displayed as fringe bitmaps under X, so we don't need them for such
2281 frames. */
2282 if (!FRAME_WINDOW_P (it->f))
2283 {
2284 if (it->truncate_lines_p)
2285 {
2286 /* We will need the truncation glyph. */
2287 xassert (it->glyph_row == NULL);
2288 produce_special_glyphs (it, IT_TRUNCATION);
2289 it->truncation_pixel_width = it->pixel_width;
2290 }
2291 else
2292 {
2293 /* We will need the continuation glyph. */
2294 xassert (it->glyph_row == NULL);
2295 produce_special_glyphs (it, IT_CONTINUATION);
2296 it->continuation_pixel_width = it->pixel_width;
2297 }
2298
2299 /* Reset these values to zero because the produce_special_glyphs
2300 above has changed them. */
2301 it->pixel_width = it->ascent = it->descent = 0;
2302 it->phys_ascent = it->phys_descent = 0;
2303 }
2304
2305 /* Set this after getting the dimensions of truncation and
2306 continuation glyphs, so that we don't produce glyphs when calling
2307 produce_special_glyphs, above. */
2308 it->glyph_row = row;
2309 it->area = TEXT_AREA;
2310
2311 /* Get the dimensions of the display area. The display area
2312 consists of the visible window area plus a horizontally scrolled
2313 part to the left of the window. All x-values are relative to the
2314 start of this total display area. */
2315 if (base_face_id != DEFAULT_FACE_ID)
2316 {
2317 /* Mode lines, menu bar in terminal frames. */
2318 it->first_visible_x = 0;
2319 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2320 }
2321 else
2322 {
2323 it->first_visible_x
2324 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2325 it->last_visible_x = (it->first_visible_x
2326 + window_box_width (w, TEXT_AREA));
2327
2328 /* If we truncate lines, leave room for the truncator glyph(s) at
2329 the right margin. Otherwise, leave room for the continuation
2330 glyph(s). Truncation and continuation glyphs are not inserted
2331 for window-based redisplay. */
2332 if (!FRAME_WINDOW_P (it->f))
2333 {
2334 if (it->truncate_lines_p)
2335 it->last_visible_x -= it->truncation_pixel_width;
2336 else
2337 it->last_visible_x -= it->continuation_pixel_width;
2338 }
2339
2340 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2341 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2342 }
2343
2344 /* Leave room for a border glyph. */
2345 if (!FRAME_WINDOW_P (it->f)
2346 && !WINDOW_RIGHTMOST_P (it->w))
2347 it->last_visible_x -= 1;
2348
2349 it->last_visible_y = window_text_bottom_y (w);
2350
2351 /* For mode lines and alike, arrange for the first glyph having a
2352 left box line if the face specifies a box. */
2353 if (base_face_id != DEFAULT_FACE_ID)
2354 {
2355 struct face *face;
2356
2357 it->face_id = base_face_id;
2358
2359 /* If we have a boxed mode line, make the first character appear
2360 with a left box line. */
2361 face = FACE_FROM_ID (it->f, base_face_id);
2362 if (face->box != FACE_NO_BOX)
2363 it->start_of_box_run_p = 1;
2364 }
2365
2366 /* If a buffer position was specified, set the iterator there,
2367 getting overlays and face properties from that position. */
2368 if (charpos >= BUF_BEG (current_buffer))
2369 {
2370 it->end_charpos = ZV;
2371 it->face_id = -1;
2372 IT_CHARPOS (*it) = charpos;
2373
2374 /* Compute byte position if not specified. */
2375 if (bytepos < charpos)
2376 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2377 else
2378 IT_BYTEPOS (*it) = bytepos;
2379
2380 it->start = it->current;
2381
2382 /* Compute faces etc. */
2383 reseat (it, it->current.pos, 1);
2384 }
2385
2386 CHECK_IT (it);
2387 }
2388
2389
2390 /* Initialize IT for the display of window W with window start POS. */
2391
2392 void
2393 start_display (it, w, pos)
2394 struct it *it;
2395 struct window *w;
2396 struct text_pos pos;
2397 {
2398 struct glyph_row *row;
2399 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2400
2401 row = w->desired_matrix->rows + first_vpos;
2402 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2403 it->first_vpos = first_vpos;
2404
2405 if (!it->truncate_lines_p)
2406 {
2407 int start_at_line_beg_p;
2408 int first_y = it->current_y;
2409
2410 /* If window start is not at a line start, skip forward to POS to
2411 get the correct continuation lines width. */
2412 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2413 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2414 if (!start_at_line_beg_p)
2415 {
2416 int new_x;
2417
2418 reseat_at_previous_visible_line_start (it);
2419 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2420
2421 new_x = it->current_x + it->pixel_width;
2422
2423 /* If lines are continued, this line may end in the middle
2424 of a multi-glyph character (e.g. a control character
2425 displayed as \003, or in the middle of an overlay
2426 string). In this case move_it_to above will not have
2427 taken us to the start of the continuation line but to the
2428 end of the continued line. */
2429 if (it->current_x > 0
2430 && !it->truncate_lines_p /* Lines are continued. */
2431 && (/* And glyph doesn't fit on the line. */
2432 new_x > it->last_visible_x
2433 /* Or it fits exactly and we're on a window
2434 system frame. */
2435 || (new_x == it->last_visible_x
2436 && FRAME_WINDOW_P (it->f))))
2437 {
2438 if (it->current.dpvec_index >= 0
2439 || it->current.overlay_string_index >= 0)
2440 {
2441 set_iterator_to_next (it, 1);
2442 move_it_in_display_line_to (it, -1, -1, 0);
2443 }
2444
2445 it->continuation_lines_width += it->current_x;
2446 }
2447
2448 /* We're starting a new display line, not affected by the
2449 height of the continued line, so clear the appropriate
2450 fields in the iterator structure. */
2451 it->max_ascent = it->max_descent = 0;
2452 it->max_phys_ascent = it->max_phys_descent = 0;
2453
2454 it->current_y = first_y;
2455 it->vpos = 0;
2456 it->current_x = it->hpos = 0;
2457 }
2458 }
2459
2460 #if 0 /* Don't assert the following because start_display is sometimes
2461 called intentionally with a window start that is not at a
2462 line start. Please leave this code in as a comment. */
2463
2464 /* Window start should be on a line start, now. */
2465 xassert (it->continuation_lines_width
2466 || IT_CHARPOS (it) == BEGV
2467 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2468 #endif /* 0 */
2469 }
2470
2471
2472 /* Return 1 if POS is a position in ellipses displayed for invisible
2473 text. W is the window we display, for text property lookup. */
2474
2475 static int
2476 in_ellipses_for_invisible_text_p (pos, w)
2477 struct display_pos *pos;
2478 struct window *w;
2479 {
2480 Lisp_Object prop, window;
2481 int ellipses_p = 0;
2482 int charpos = CHARPOS (pos->pos);
2483
2484 /* If POS specifies a position in a display vector, this might
2485 be for an ellipsis displayed for invisible text. We won't
2486 get the iterator set up for delivering that ellipsis unless
2487 we make sure that it gets aware of the invisible text. */
2488 if (pos->dpvec_index >= 0
2489 && pos->overlay_string_index < 0
2490 && CHARPOS (pos->string_pos) < 0
2491 && charpos > BEGV
2492 && (XSETWINDOW (window, w),
2493 prop = Fget_char_property (make_number (charpos),
2494 Qinvisible, window),
2495 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2496 {
2497 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2498 window);
2499 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2500 }
2501
2502 return ellipses_p;
2503 }
2504
2505
2506 /* Initialize IT for stepping through current_buffer in window W,
2507 starting at position POS that includes overlay string and display
2508 vector/ control character translation position information. Value
2509 is zero if there are overlay strings with newlines at POS. */
2510
2511 static int
2512 init_from_display_pos (it, w, pos)
2513 struct it *it;
2514 struct window *w;
2515 struct display_pos *pos;
2516 {
2517 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2518 int i, overlay_strings_with_newlines = 0;
2519
2520 /* If POS specifies a position in a display vector, this might
2521 be for an ellipsis displayed for invisible text. We won't
2522 get the iterator set up for delivering that ellipsis unless
2523 we make sure that it gets aware of the invisible text. */
2524 if (in_ellipses_for_invisible_text_p (pos, w))
2525 {
2526 --charpos;
2527 bytepos = 0;
2528 }
2529
2530 /* Keep in mind: the call to reseat in init_iterator skips invisible
2531 text, so we might end up at a position different from POS. This
2532 is only a problem when POS is a row start after a newline and an
2533 overlay starts there with an after-string, and the overlay has an
2534 invisible property. Since we don't skip invisible text in
2535 display_line and elsewhere immediately after consuming the
2536 newline before the row start, such a POS will not be in a string,
2537 but the call to init_iterator below will move us to the
2538 after-string. */
2539 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2540
2541 /* This only scans the current chunk -- it should scan all chunks.
2542 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2543 to 16 in 22.1 to make this a lesser problem. */
2544 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2545 {
2546 const char *s = SDATA (it->overlay_strings[i]);
2547 const char *e = s + SBYTES (it->overlay_strings[i]);
2548
2549 while (s < e && *s != '\n')
2550 ++s;
2551
2552 if (s < e)
2553 {
2554 overlay_strings_with_newlines = 1;
2555 break;
2556 }
2557 }
2558
2559 /* If position is within an overlay string, set up IT to the right
2560 overlay string. */
2561 if (pos->overlay_string_index >= 0)
2562 {
2563 int relative_index;
2564
2565 /* If the first overlay string happens to have a `display'
2566 property for an image, the iterator will be set up for that
2567 image, and we have to undo that setup first before we can
2568 correct the overlay string index. */
2569 if (it->method == GET_FROM_IMAGE)
2570 pop_it (it);
2571
2572 /* We already have the first chunk of overlay strings in
2573 IT->overlay_strings. Load more until the one for
2574 pos->overlay_string_index is in IT->overlay_strings. */
2575 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2576 {
2577 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2578 it->current.overlay_string_index = 0;
2579 while (n--)
2580 {
2581 load_overlay_strings (it, 0);
2582 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2583 }
2584 }
2585
2586 it->current.overlay_string_index = pos->overlay_string_index;
2587 relative_index = (it->current.overlay_string_index
2588 % OVERLAY_STRING_CHUNK_SIZE);
2589 it->string = it->overlay_strings[relative_index];
2590 xassert (STRINGP (it->string));
2591 it->current.string_pos = pos->string_pos;
2592 it->method = GET_FROM_STRING;
2593 }
2594
2595 #if 0 /* This is bogus because POS not having an overlay string
2596 position does not mean it's after the string. Example: A
2597 line starting with a before-string and initialization of IT
2598 to the previous row's end position. */
2599 else if (it->current.overlay_string_index >= 0)
2600 {
2601 /* If POS says we're already after an overlay string ending at
2602 POS, make sure to pop the iterator because it will be in
2603 front of that overlay string. When POS is ZV, we've thereby
2604 also ``processed'' overlay strings at ZV. */
2605 while (it->sp)
2606 pop_it (it);
2607 it->current.overlay_string_index = -1;
2608 it->method = GET_FROM_BUFFER;
2609 if (CHARPOS (pos->pos) == ZV)
2610 it->overlay_strings_at_end_processed_p = 1;
2611 }
2612 #endif /* 0 */
2613
2614 if (CHARPOS (pos->string_pos) >= 0)
2615 {
2616 /* Recorded position is not in an overlay string, but in another
2617 string. This can only be a string from a `display' property.
2618 IT should already be filled with that string. */
2619 it->current.string_pos = pos->string_pos;
2620 xassert (STRINGP (it->string));
2621 }
2622
2623 /* Restore position in display vector translations, control
2624 character translations or ellipses. */
2625 if (pos->dpvec_index >= 0)
2626 {
2627 if (it->dpvec == NULL)
2628 get_next_display_element (it);
2629 xassert (it->dpvec && it->current.dpvec_index == 0);
2630 it->current.dpvec_index = pos->dpvec_index;
2631 }
2632
2633 CHECK_IT (it);
2634 return !overlay_strings_with_newlines;
2635 }
2636
2637
2638 /* Initialize IT for stepping through current_buffer in window W
2639 starting at ROW->start. */
2640
2641 static void
2642 init_to_row_start (it, w, row)
2643 struct it *it;
2644 struct window *w;
2645 struct glyph_row *row;
2646 {
2647 init_from_display_pos (it, w, &row->start);
2648 it->start = row->start;
2649 it->continuation_lines_width = row->continuation_lines_width;
2650 CHECK_IT (it);
2651 }
2652
2653
2654 /* Initialize IT for stepping through current_buffer in window W
2655 starting in the line following ROW, i.e. starting at ROW->end.
2656 Value is zero if there are overlay strings with newlines at ROW's
2657 end position. */
2658
2659 static int
2660 init_to_row_end (it, w, row)
2661 struct it *it;
2662 struct window *w;
2663 struct glyph_row *row;
2664 {
2665 int success = 0;
2666
2667 if (init_from_display_pos (it, w, &row->end))
2668 {
2669 if (row->continued_p)
2670 it->continuation_lines_width
2671 = row->continuation_lines_width + row->pixel_width;
2672 CHECK_IT (it);
2673 success = 1;
2674 }
2675
2676 return success;
2677 }
2678
2679
2680
2681 \f
2682 /***********************************************************************
2683 Text properties
2684 ***********************************************************************/
2685
2686 /* Called when IT reaches IT->stop_charpos. Handle text property and
2687 overlay changes. Set IT->stop_charpos to the next position where
2688 to stop. */
2689
2690 static void
2691 handle_stop (it)
2692 struct it *it;
2693 {
2694 enum prop_handled handled;
2695 int handle_overlay_change_p = 1;
2696 struct props *p;
2697
2698 it->dpvec = NULL;
2699 it->current.dpvec_index = -1;
2700
2701 do
2702 {
2703 handled = HANDLED_NORMALLY;
2704
2705 /* Call text property handlers. */
2706 for (p = it_props; p->handler; ++p)
2707 {
2708 handled = p->handler (it);
2709
2710 if (handled == HANDLED_RECOMPUTE_PROPS)
2711 break;
2712 else if (handled == HANDLED_RETURN)
2713 return;
2714 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2715 handle_overlay_change_p = 0;
2716 }
2717
2718 if (handled != HANDLED_RECOMPUTE_PROPS)
2719 {
2720 /* Don't check for overlay strings below when set to deliver
2721 characters from a display vector. */
2722 if (it->method == GET_FROM_DISPLAY_VECTOR)
2723 handle_overlay_change_p = 0;
2724
2725 /* Handle overlay changes. */
2726 if (handle_overlay_change_p)
2727 handled = handle_overlay_change (it);
2728
2729 /* Determine where to stop next. */
2730 if (handled == HANDLED_NORMALLY)
2731 compute_stop_pos (it);
2732 }
2733 }
2734 while (handled == HANDLED_RECOMPUTE_PROPS);
2735 }
2736
2737
2738 /* Compute IT->stop_charpos from text property and overlay change
2739 information for IT's current position. */
2740
2741 static void
2742 compute_stop_pos (it)
2743 struct it *it;
2744 {
2745 register INTERVAL iv, next_iv;
2746 Lisp_Object object, limit, position;
2747
2748 /* If nowhere else, stop at the end. */
2749 it->stop_charpos = it->end_charpos;
2750
2751 if (STRINGP (it->string))
2752 {
2753 /* Strings are usually short, so don't limit the search for
2754 properties. */
2755 object = it->string;
2756 limit = Qnil;
2757 position = make_number (IT_STRING_CHARPOS (*it));
2758 }
2759 else
2760 {
2761 int charpos;
2762
2763 /* If next overlay change is in front of the current stop pos
2764 (which is IT->end_charpos), stop there. Note: value of
2765 next_overlay_change is point-max if no overlay change
2766 follows. */
2767 charpos = next_overlay_change (IT_CHARPOS (*it));
2768 if (charpos < it->stop_charpos)
2769 it->stop_charpos = charpos;
2770
2771 /* If showing the region, we have to stop at the region
2772 start or end because the face might change there. */
2773 if (it->region_beg_charpos > 0)
2774 {
2775 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2776 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2777 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2778 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2779 }
2780
2781 /* Set up variables for computing the stop position from text
2782 property changes. */
2783 XSETBUFFER (object, current_buffer);
2784 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2785 position = make_number (IT_CHARPOS (*it));
2786
2787 }
2788
2789 /* Get the interval containing IT's position. Value is a null
2790 interval if there isn't such an interval. */
2791 iv = validate_interval_range (object, &position, &position, 0);
2792 if (!NULL_INTERVAL_P (iv))
2793 {
2794 Lisp_Object values_here[LAST_PROP_IDX];
2795 struct props *p;
2796
2797 /* Get properties here. */
2798 for (p = it_props; p->handler; ++p)
2799 values_here[p->idx] = textget (iv->plist, *p->name);
2800
2801 /* Look for an interval following iv that has different
2802 properties. */
2803 for (next_iv = next_interval (iv);
2804 (!NULL_INTERVAL_P (next_iv)
2805 && (NILP (limit)
2806 || XFASTINT (limit) > next_iv->position));
2807 next_iv = next_interval (next_iv))
2808 {
2809 for (p = it_props; p->handler; ++p)
2810 {
2811 Lisp_Object new_value;
2812
2813 new_value = textget (next_iv->plist, *p->name);
2814 if (!EQ (values_here[p->idx], new_value))
2815 break;
2816 }
2817
2818 if (p->handler)
2819 break;
2820 }
2821
2822 if (!NULL_INTERVAL_P (next_iv))
2823 {
2824 if (INTEGERP (limit)
2825 && next_iv->position >= XFASTINT (limit))
2826 /* No text property change up to limit. */
2827 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2828 else
2829 /* Text properties change in next_iv. */
2830 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2831 }
2832 }
2833
2834 xassert (STRINGP (it->string)
2835 || (it->stop_charpos >= BEGV
2836 && it->stop_charpos >= IT_CHARPOS (*it)));
2837 }
2838
2839
2840 /* Return the position of the next overlay change after POS in
2841 current_buffer. Value is point-max if no overlay change
2842 follows. This is like `next-overlay-change' but doesn't use
2843 xmalloc. */
2844
2845 static int
2846 next_overlay_change (pos)
2847 int pos;
2848 {
2849 int noverlays;
2850 int endpos;
2851 Lisp_Object *overlays;
2852 int i;
2853
2854 /* Get all overlays at the given position. */
2855 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2856
2857 /* If any of these overlays ends before endpos,
2858 use its ending point instead. */
2859 for (i = 0; i < noverlays; ++i)
2860 {
2861 Lisp_Object oend;
2862 int oendpos;
2863
2864 oend = OVERLAY_END (overlays[i]);
2865 oendpos = OVERLAY_POSITION (oend);
2866 endpos = min (endpos, oendpos);
2867 }
2868
2869 return endpos;
2870 }
2871
2872
2873 \f
2874 /***********************************************************************
2875 Fontification
2876 ***********************************************************************/
2877
2878 /* Handle changes in the `fontified' property of the current buffer by
2879 calling hook functions from Qfontification_functions to fontify
2880 regions of text. */
2881
2882 static enum prop_handled
2883 handle_fontified_prop (it)
2884 struct it *it;
2885 {
2886 Lisp_Object prop, pos;
2887 enum prop_handled handled = HANDLED_NORMALLY;
2888
2889 /* Get the value of the `fontified' property at IT's current buffer
2890 position. (The `fontified' property doesn't have a special
2891 meaning in strings.) If the value is nil, call functions from
2892 Qfontification_functions. */
2893 if (!STRINGP (it->string)
2894 && it->s == NULL
2895 && !NILP (Vfontification_functions)
2896 && !NILP (Vrun_hooks)
2897 && (pos = make_number (IT_CHARPOS (*it)),
2898 prop = Fget_char_property (pos, Qfontified, Qnil),
2899 NILP (prop)))
2900 {
2901 int count = SPECPDL_INDEX ();
2902 Lisp_Object val;
2903
2904 val = Vfontification_functions;
2905 specbind (Qfontification_functions, Qnil);
2906
2907 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2908 safe_call1 (val, pos);
2909 else
2910 {
2911 Lisp_Object globals, fn;
2912 struct gcpro gcpro1, gcpro2;
2913
2914 globals = Qnil;
2915 GCPRO2 (val, globals);
2916
2917 for (; CONSP (val); val = XCDR (val))
2918 {
2919 fn = XCAR (val);
2920
2921 if (EQ (fn, Qt))
2922 {
2923 /* A value of t indicates this hook has a local
2924 binding; it means to run the global binding too.
2925 In a global value, t should not occur. If it
2926 does, we must ignore it to avoid an endless
2927 loop. */
2928 for (globals = Fdefault_value (Qfontification_functions);
2929 CONSP (globals);
2930 globals = XCDR (globals))
2931 {
2932 fn = XCAR (globals);
2933 if (!EQ (fn, Qt))
2934 safe_call1 (fn, pos);
2935 }
2936 }
2937 else
2938 safe_call1 (fn, pos);
2939 }
2940
2941 UNGCPRO;
2942 }
2943
2944 unbind_to (count, Qnil);
2945
2946 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2947 something. This avoids an endless loop if they failed to
2948 fontify the text for which reason ever. */
2949 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2950 handled = HANDLED_RECOMPUTE_PROPS;
2951 }
2952
2953 return handled;
2954 }
2955
2956
2957 \f
2958 /***********************************************************************
2959 Faces
2960 ***********************************************************************/
2961
2962 /* Set up iterator IT from face properties at its current position.
2963 Called from handle_stop. */
2964
2965 static enum prop_handled
2966 handle_face_prop (it)
2967 struct it *it;
2968 {
2969 int new_face_id, next_stop;
2970
2971 if (!STRINGP (it->string))
2972 {
2973 new_face_id
2974 = face_at_buffer_position (it->w,
2975 IT_CHARPOS (*it),
2976 it->region_beg_charpos,
2977 it->region_end_charpos,
2978 &next_stop,
2979 (IT_CHARPOS (*it)
2980 + TEXT_PROP_DISTANCE_LIMIT),
2981 0);
2982
2983 /* Is this a start of a run of characters with box face?
2984 Caveat: this can be called for a freshly initialized
2985 iterator; face_id is -1 in this case. We know that the new
2986 face will not change until limit, i.e. if the new face has a
2987 box, all characters up to limit will have one. But, as
2988 usual, we don't know whether limit is really the end. */
2989 if (new_face_id != it->face_id)
2990 {
2991 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
2992
2993 /* If new face has a box but old face has not, this is
2994 the start of a run of characters with box, i.e. it has
2995 a shadow on the left side. The value of face_id of the
2996 iterator will be -1 if this is the initial call that gets
2997 the face. In this case, we have to look in front of IT's
2998 position and see whether there is a face != new_face_id. */
2999 it->start_of_box_run_p
3000 = (new_face->box != FACE_NO_BOX
3001 && (it->face_id >= 0
3002 || IT_CHARPOS (*it) == BEG
3003 || new_face_id != face_before_it_pos (it)));
3004 it->face_box_p = new_face->box != FACE_NO_BOX;
3005 }
3006 }
3007 else
3008 {
3009 int base_face_id, bufpos;
3010
3011 if (it->current.overlay_string_index >= 0)
3012 bufpos = IT_CHARPOS (*it);
3013 else
3014 bufpos = 0;
3015
3016 /* For strings from a buffer, i.e. overlay strings or strings
3017 from a `display' property, use the face at IT's current
3018 buffer position as the base face to merge with, so that
3019 overlay strings appear in the same face as surrounding
3020 text, unless they specify their own faces. */
3021 base_face_id = underlying_face_id (it);
3022
3023 new_face_id = face_at_string_position (it->w,
3024 it->string,
3025 IT_STRING_CHARPOS (*it),
3026 bufpos,
3027 it->region_beg_charpos,
3028 it->region_end_charpos,
3029 &next_stop,
3030 base_face_id, 0);
3031
3032 #if 0 /* This shouldn't be neccessary. Let's check it. */
3033 /* If IT is used to display a mode line we would really like to
3034 use the mode line face instead of the frame's default face. */
3035 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3036 && new_face_id == DEFAULT_FACE_ID)
3037 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3038 #endif
3039
3040 /* Is this a start of a run of characters with box? Caveat:
3041 this can be called for a freshly allocated iterator; face_id
3042 is -1 is this case. We know that the new face will not
3043 change until the next check pos, i.e. if the new face has a
3044 box, all characters up to that position will have a
3045 box. But, as usual, we don't know whether that position
3046 is really the end. */
3047 if (new_face_id != it->face_id)
3048 {
3049 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3050 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3051
3052 /* If new face has a box but old face hasn't, this is the
3053 start of a run of characters with box, i.e. it has a
3054 shadow on the left side. */
3055 it->start_of_box_run_p
3056 = new_face->box && (old_face == NULL || !old_face->box);
3057 it->face_box_p = new_face->box != FACE_NO_BOX;
3058 }
3059 }
3060
3061 it->face_id = new_face_id;
3062 return HANDLED_NORMALLY;
3063 }
3064
3065
3066 /* Return the ID of the face ``underlying'' IT's current position,
3067 which is in a string. If the iterator is associated with a
3068 buffer, return the face at IT's current buffer position.
3069 Otherwise, use the iterator's base_face_id. */
3070
3071 static int
3072 underlying_face_id (it)
3073 struct it *it;
3074 {
3075 int face_id = it->base_face_id, i;
3076
3077 xassert (STRINGP (it->string));
3078
3079 for (i = it->sp - 1; i >= 0; --i)
3080 if (NILP (it->stack[i].string))
3081 face_id = it->stack[i].face_id;
3082
3083 return face_id;
3084 }
3085
3086
3087 /* Compute the face one character before or after the current position
3088 of IT. BEFORE_P non-zero means get the face in front of IT's
3089 position. Value is the id of the face. */
3090
3091 static int
3092 face_before_or_after_it_pos (it, before_p)
3093 struct it *it;
3094 int before_p;
3095 {
3096 int face_id, limit;
3097 int next_check_charpos;
3098 struct text_pos pos;
3099
3100 xassert (it->s == NULL);
3101
3102 if (STRINGP (it->string))
3103 {
3104 int bufpos, base_face_id;
3105
3106 /* No face change past the end of the string (for the case
3107 we are padding with spaces). No face change before the
3108 string start. */
3109 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3110 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3111 return it->face_id;
3112
3113 /* Set pos to the position before or after IT's current position. */
3114 if (before_p)
3115 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3116 else
3117 /* For composition, we must check the character after the
3118 composition. */
3119 pos = (it->what == IT_COMPOSITION
3120 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3121 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3122
3123 if (it->current.overlay_string_index >= 0)
3124 bufpos = IT_CHARPOS (*it);
3125 else
3126 bufpos = 0;
3127
3128 base_face_id = underlying_face_id (it);
3129
3130 /* Get the face for ASCII, or unibyte. */
3131 face_id = face_at_string_position (it->w,
3132 it->string,
3133 CHARPOS (pos),
3134 bufpos,
3135 it->region_beg_charpos,
3136 it->region_end_charpos,
3137 &next_check_charpos,
3138 base_face_id, 0);
3139
3140 /* Correct the face for charsets different from ASCII. Do it
3141 for the multibyte case only. The face returned above is
3142 suitable for unibyte text if IT->string is unibyte. */
3143 if (STRING_MULTIBYTE (it->string))
3144 {
3145 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3146 int rest = SBYTES (it->string) - BYTEPOS (pos);
3147 int c, len;
3148 struct face *face = FACE_FROM_ID (it->f, face_id);
3149
3150 c = string_char_and_length (p, rest, &len);
3151 face_id = FACE_FOR_CHAR (it->f, face, c);
3152 }
3153 }
3154 else
3155 {
3156 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3157 || (IT_CHARPOS (*it) <= BEGV && before_p))
3158 return it->face_id;
3159
3160 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3161 pos = it->current.pos;
3162
3163 if (before_p)
3164 DEC_TEXT_POS (pos, it->multibyte_p);
3165 else
3166 {
3167 if (it->what == IT_COMPOSITION)
3168 /* For composition, we must check the position after the
3169 composition. */
3170 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3171 else
3172 INC_TEXT_POS (pos, it->multibyte_p);
3173 }
3174
3175 /* Determine face for CHARSET_ASCII, or unibyte. */
3176 face_id = face_at_buffer_position (it->w,
3177 CHARPOS (pos),
3178 it->region_beg_charpos,
3179 it->region_end_charpos,
3180 &next_check_charpos,
3181 limit, 0);
3182
3183 /* Correct the face for charsets different from ASCII. Do it
3184 for the multibyte case only. The face returned above is
3185 suitable for unibyte text if current_buffer is unibyte. */
3186 if (it->multibyte_p)
3187 {
3188 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3189 struct face *face = FACE_FROM_ID (it->f, face_id);
3190 face_id = FACE_FOR_CHAR (it->f, face, c);
3191 }
3192 }
3193
3194 return face_id;
3195 }
3196
3197
3198 \f
3199 /***********************************************************************
3200 Invisible text
3201 ***********************************************************************/
3202
3203 /* Set up iterator IT from invisible properties at its current
3204 position. Called from handle_stop. */
3205
3206 static enum prop_handled
3207 handle_invisible_prop (it)
3208 struct it *it;
3209 {
3210 enum prop_handled handled = HANDLED_NORMALLY;
3211
3212 if (STRINGP (it->string))
3213 {
3214 extern Lisp_Object Qinvisible;
3215 Lisp_Object prop, end_charpos, limit, charpos;
3216
3217 /* Get the value of the invisible text property at the
3218 current position. Value will be nil if there is no such
3219 property. */
3220 charpos = make_number (IT_STRING_CHARPOS (*it));
3221 prop = Fget_text_property (charpos, Qinvisible, it->string);
3222
3223 if (!NILP (prop)
3224 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3225 {
3226 handled = HANDLED_RECOMPUTE_PROPS;
3227
3228 /* Get the position at which the next change of the
3229 invisible text property can be found in IT->string.
3230 Value will be nil if the property value is the same for
3231 all the rest of IT->string. */
3232 XSETINT (limit, SCHARS (it->string));
3233 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3234 it->string, limit);
3235
3236 /* Text at current position is invisible. The next
3237 change in the property is at position end_charpos.
3238 Move IT's current position to that position. */
3239 if (INTEGERP (end_charpos)
3240 && XFASTINT (end_charpos) < XFASTINT (limit))
3241 {
3242 struct text_pos old;
3243 old = it->current.string_pos;
3244 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3245 compute_string_pos (&it->current.string_pos, old, it->string);
3246 }
3247 else
3248 {
3249 /* The rest of the string is invisible. If this is an
3250 overlay string, proceed with the next overlay string
3251 or whatever comes and return a character from there. */
3252 if (it->current.overlay_string_index >= 0)
3253 {
3254 next_overlay_string (it);
3255 /* Don't check for overlay strings when we just
3256 finished processing them. */
3257 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3258 }
3259 else
3260 {
3261 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3262 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3263 }
3264 }
3265 }
3266 }
3267 else
3268 {
3269 int invis_p, newpos, next_stop, start_charpos;
3270 Lisp_Object pos, prop, overlay;
3271
3272 /* First of all, is there invisible text at this position? */
3273 start_charpos = IT_CHARPOS (*it);
3274 pos = make_number (IT_CHARPOS (*it));
3275 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3276 &overlay);
3277 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3278
3279 /* If we are on invisible text, skip over it. */
3280 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3281 {
3282 /* Record whether we have to display an ellipsis for the
3283 invisible text. */
3284 int display_ellipsis_p = invis_p == 2;
3285
3286 handled = HANDLED_RECOMPUTE_PROPS;
3287
3288 /* Loop skipping over invisible text. The loop is left at
3289 ZV or with IT on the first char being visible again. */
3290 do
3291 {
3292 /* Try to skip some invisible text. Return value is the
3293 position reached which can be equal to IT's position
3294 if there is nothing invisible here. This skips both
3295 over invisible text properties and overlays with
3296 invisible property. */
3297 newpos = skip_invisible (IT_CHARPOS (*it),
3298 &next_stop, ZV, it->window);
3299
3300 /* If we skipped nothing at all we weren't at invisible
3301 text in the first place. If everything to the end of
3302 the buffer was skipped, end the loop. */
3303 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3304 invis_p = 0;
3305 else
3306 {
3307 /* We skipped some characters but not necessarily
3308 all there are. Check if we ended up on visible
3309 text. Fget_char_property returns the property of
3310 the char before the given position, i.e. if we
3311 get invis_p = 0, this means that the char at
3312 newpos is visible. */
3313 pos = make_number (newpos);
3314 prop = Fget_char_property (pos, Qinvisible, it->window);
3315 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3316 }
3317
3318 /* If we ended up on invisible text, proceed to
3319 skip starting with next_stop. */
3320 if (invis_p)
3321 IT_CHARPOS (*it) = next_stop;
3322 }
3323 while (invis_p);
3324
3325 /* The position newpos is now either ZV or on visible text. */
3326 IT_CHARPOS (*it) = newpos;
3327 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3328
3329 /* If there are before-strings at the start of invisible
3330 text, and the text is invisible because of a text
3331 property, arrange to show before-strings because 20.x did
3332 it that way. (If the text is invisible because of an
3333 overlay property instead of a text property, this is
3334 already handled in the overlay code.) */
3335 if (NILP (overlay)
3336 && get_overlay_strings (it, start_charpos))
3337 {
3338 handled = HANDLED_RECOMPUTE_PROPS;
3339 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3340 }
3341 else if (display_ellipsis_p)
3342 setup_for_ellipsis (it, 0);
3343 }
3344 }
3345
3346 return handled;
3347 }
3348
3349
3350 /* Make iterator IT return `...' next.
3351 Replaces LEN characters from buffer. */
3352
3353 static void
3354 setup_for_ellipsis (it, len)
3355 struct it *it;
3356 int len;
3357 {
3358 /* Use the display table definition for `...'. Invalid glyphs
3359 will be handled by the method returning elements from dpvec. */
3360 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3361 {
3362 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3363 it->dpvec = v->contents;
3364 it->dpend = v->contents + v->size;
3365 }
3366 else
3367 {
3368 /* Default `...'. */
3369 it->dpvec = default_invis_vector;
3370 it->dpend = default_invis_vector + 3;
3371 }
3372
3373 it->dpvec_char_len = len;
3374 it->current.dpvec_index = 0;
3375 it->dpvec_face_id = -1;
3376
3377 /* Remember the current face id in case glyphs specify faces.
3378 IT's face is restored in set_iterator_to_next. */
3379 it->saved_face_id = it->face_id;
3380 it->method = GET_FROM_DISPLAY_VECTOR;
3381 it->ellipsis_p = 1;
3382 }
3383
3384
3385 \f
3386 /***********************************************************************
3387 'display' property
3388 ***********************************************************************/
3389
3390 /* Set up iterator IT from `display' property at its current position.
3391 Called from handle_stop.
3392 We return HANDLED_RETURN if some part of the display property
3393 overrides the display of the buffer text itself.
3394 Otherwise we return HANDLED_NORMALLY. */
3395
3396 static enum prop_handled
3397 handle_display_prop (it)
3398 struct it *it;
3399 {
3400 Lisp_Object prop, object;
3401 struct text_pos *position;
3402 /* Nonzero if some property replaces the display of the text itself. */
3403 int display_replaced_p = 0;
3404
3405 if (STRINGP (it->string))
3406 {
3407 object = it->string;
3408 position = &it->current.string_pos;
3409 }
3410 else
3411 {
3412 object = it->w->buffer;
3413 position = &it->current.pos;
3414 }
3415
3416 /* Reset those iterator values set from display property values. */
3417 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3418 it->space_width = Qnil;
3419 it->font_height = Qnil;
3420 it->voffset = 0;
3421
3422 /* We don't support recursive `display' properties, i.e. string
3423 values that have a string `display' property, that have a string
3424 `display' property etc. */
3425 if (!it->string_from_display_prop_p)
3426 it->area = TEXT_AREA;
3427
3428 prop = Fget_char_property (make_number (position->charpos),
3429 Qdisplay, object);
3430 if (NILP (prop))
3431 return HANDLED_NORMALLY;
3432
3433 if (CONSP (prop)
3434 /* Simple properties. */
3435 && !EQ (XCAR (prop), Qimage)
3436 && !EQ (XCAR (prop), Qspace)
3437 && !EQ (XCAR (prop), Qwhen)
3438 && !EQ (XCAR (prop), Qslice)
3439 && !EQ (XCAR (prop), Qspace_width)
3440 && !EQ (XCAR (prop), Qheight)
3441 && !EQ (XCAR (prop), Qraise)
3442 /* Marginal area specifications. */
3443 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3444 && !EQ (XCAR (prop), Qleft_fringe)
3445 && !EQ (XCAR (prop), Qright_fringe)
3446 && !NILP (XCAR (prop)))
3447 {
3448 for (; CONSP (prop); prop = XCDR (prop))
3449 {
3450 if (handle_single_display_spec (it, XCAR (prop), object,
3451 position, display_replaced_p))
3452 display_replaced_p = 1;
3453 }
3454 }
3455 else if (VECTORP (prop))
3456 {
3457 int i;
3458 for (i = 0; i < ASIZE (prop); ++i)
3459 if (handle_single_display_spec (it, AREF (prop, i), object,
3460 position, display_replaced_p))
3461 display_replaced_p = 1;
3462 }
3463 else
3464 {
3465 if (handle_single_display_spec (it, prop, object, position, 0))
3466 display_replaced_p = 1;
3467 }
3468
3469 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3470 }
3471
3472
3473 /* Value is the position of the end of the `display' property starting
3474 at START_POS in OBJECT. */
3475
3476 static struct text_pos
3477 display_prop_end (it, object, start_pos)
3478 struct it *it;
3479 Lisp_Object object;
3480 struct text_pos start_pos;
3481 {
3482 Lisp_Object end;
3483 struct text_pos end_pos;
3484
3485 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3486 Qdisplay, object, Qnil);
3487 CHARPOS (end_pos) = XFASTINT (end);
3488 if (STRINGP (object))
3489 compute_string_pos (&end_pos, start_pos, it->string);
3490 else
3491 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3492
3493 return end_pos;
3494 }
3495
3496
3497 /* Set up IT from a single `display' specification PROP. OBJECT
3498 is the object in which the `display' property was found. *POSITION
3499 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3500 means that we previously saw a display specification which already
3501 replaced text display with something else, for example an image;
3502 we ignore such properties after the first one has been processed.
3503
3504 If PROP is a `space' or `image' specification, and in some other
3505 cases too, set *POSITION to the position where the `display'
3506 property ends.
3507
3508 Value is non-zero if something was found which replaces the display
3509 of buffer or string text. */
3510
3511 static int
3512 handle_single_display_spec (it, spec, object, position,
3513 display_replaced_before_p)
3514 struct it *it;
3515 Lisp_Object spec;
3516 Lisp_Object object;
3517 struct text_pos *position;
3518 int display_replaced_before_p;
3519 {
3520 Lisp_Object form;
3521 Lisp_Object location, value;
3522 struct text_pos start_pos;
3523 int valid_p;
3524
3525 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3526 If the result is non-nil, use VALUE instead of SPEC. */
3527 form = Qt;
3528 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3529 {
3530 spec = XCDR (spec);
3531 if (!CONSP (spec))
3532 return 0;
3533 form = XCAR (spec);
3534 spec = XCDR (spec);
3535 }
3536
3537 if (!NILP (form) && !EQ (form, Qt))
3538 {
3539 int count = SPECPDL_INDEX ();
3540 struct gcpro gcpro1;
3541
3542 /* Bind `object' to the object having the `display' property, a
3543 buffer or string. Bind `position' to the position in the
3544 object where the property was found, and `buffer-position'
3545 to the current position in the buffer. */
3546 specbind (Qobject, object);
3547 specbind (Qposition, make_number (CHARPOS (*position)));
3548 specbind (Qbuffer_position,
3549 make_number (STRINGP (object)
3550 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3551 GCPRO1 (form);
3552 form = safe_eval (form);
3553 UNGCPRO;
3554 unbind_to (count, Qnil);
3555 }
3556
3557 if (NILP (form))
3558 return 0;
3559
3560 /* Handle `(height HEIGHT)' specifications. */
3561 if (CONSP (spec)
3562 && EQ (XCAR (spec), Qheight)
3563 && CONSP (XCDR (spec)))
3564 {
3565 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3566 return 0;
3567
3568 it->font_height = XCAR (XCDR (spec));
3569 if (!NILP (it->font_height))
3570 {
3571 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3572 int new_height = -1;
3573
3574 if (CONSP (it->font_height)
3575 && (EQ (XCAR (it->font_height), Qplus)
3576 || EQ (XCAR (it->font_height), Qminus))
3577 && CONSP (XCDR (it->font_height))
3578 && INTEGERP (XCAR (XCDR (it->font_height))))
3579 {
3580 /* `(+ N)' or `(- N)' where N is an integer. */
3581 int steps = XINT (XCAR (XCDR (it->font_height)));
3582 if (EQ (XCAR (it->font_height), Qplus))
3583 steps = - steps;
3584 it->face_id = smaller_face (it->f, it->face_id, steps);
3585 }
3586 else if (FUNCTIONP (it->font_height))
3587 {
3588 /* Call function with current height as argument.
3589 Value is the new height. */
3590 Lisp_Object height;
3591 height = safe_call1 (it->font_height,
3592 face->lface[LFACE_HEIGHT_INDEX]);
3593 if (NUMBERP (height))
3594 new_height = XFLOATINT (height);
3595 }
3596 else if (NUMBERP (it->font_height))
3597 {
3598 /* Value is a multiple of the canonical char height. */
3599 struct face *face;
3600
3601 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3602 new_height = (XFLOATINT (it->font_height)
3603 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3604 }
3605 else
3606 {
3607 /* Evaluate IT->font_height with `height' bound to the
3608 current specified height to get the new height. */
3609 int count = SPECPDL_INDEX ();
3610
3611 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3612 value = safe_eval (it->font_height);
3613 unbind_to (count, Qnil);
3614
3615 if (NUMBERP (value))
3616 new_height = XFLOATINT (value);
3617 }
3618
3619 if (new_height > 0)
3620 it->face_id = face_with_height (it->f, it->face_id, new_height);
3621 }
3622
3623 return 0;
3624 }
3625
3626 /* Handle `(space_width WIDTH)'. */
3627 if (CONSP (spec)
3628 && EQ (XCAR (spec), Qspace_width)
3629 && CONSP (XCDR (spec)))
3630 {
3631 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3632 return 0;
3633
3634 value = XCAR (XCDR (spec));
3635 if (NUMBERP (value) && XFLOATINT (value) > 0)
3636 it->space_width = value;
3637
3638 return 0;
3639 }
3640
3641 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3642 if (CONSP (spec)
3643 && EQ (XCAR (spec), Qslice))
3644 {
3645 Lisp_Object tem;
3646
3647 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3648 return 0;
3649
3650 if (tem = XCDR (spec), CONSP (tem))
3651 {
3652 it->slice.x = XCAR (tem);
3653 if (tem = XCDR (tem), CONSP (tem))
3654 {
3655 it->slice.y = XCAR (tem);
3656 if (tem = XCDR (tem), CONSP (tem))
3657 {
3658 it->slice.width = XCAR (tem);
3659 if (tem = XCDR (tem), CONSP (tem))
3660 it->slice.height = XCAR (tem);
3661 }
3662 }
3663 }
3664
3665 return 0;
3666 }
3667
3668 /* Handle `(raise FACTOR)'. */
3669 if (CONSP (spec)
3670 && EQ (XCAR (spec), Qraise)
3671 && CONSP (XCDR (spec)))
3672 {
3673 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3674 return 0;
3675
3676 #ifdef HAVE_WINDOW_SYSTEM
3677 value = XCAR (XCDR (spec));
3678 if (NUMBERP (value))
3679 {
3680 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3681 it->voffset = - (XFLOATINT (value)
3682 * (FONT_HEIGHT (face->font)));
3683 }
3684 #endif /* HAVE_WINDOW_SYSTEM */
3685
3686 return 0;
3687 }
3688
3689 /* Don't handle the other kinds of display specifications
3690 inside a string that we got from a `display' property. */
3691 if (it->string_from_display_prop_p)
3692 return 0;
3693
3694 /* Characters having this form of property are not displayed, so
3695 we have to find the end of the property. */
3696 start_pos = *position;
3697 *position = display_prop_end (it, object, start_pos);
3698 value = Qnil;
3699
3700 /* Stop the scan at that end position--we assume that all
3701 text properties change there. */
3702 it->stop_charpos = position->charpos;
3703
3704 /* Handle `(left-fringe BITMAP [FACE])'
3705 and `(right-fringe BITMAP [FACE])'. */
3706 if (CONSP (spec)
3707 && (EQ (XCAR (spec), Qleft_fringe)
3708 || EQ (XCAR (spec), Qright_fringe))
3709 && CONSP (XCDR (spec)))
3710 {
3711 int face_id = DEFAULT_FACE_ID;
3712 int fringe_bitmap;
3713
3714 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3715 /* If we return here, POSITION has been advanced
3716 across the text with this property. */
3717 return 0;
3718
3719 #ifdef HAVE_WINDOW_SYSTEM
3720 value = XCAR (XCDR (spec));
3721 if (!SYMBOLP (value)
3722 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3723 /* If we return here, POSITION has been advanced
3724 across the text with this property. */
3725 return 0;
3726
3727 if (CONSP (XCDR (XCDR (spec))))
3728 {
3729 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3730 int face_id2 = lookup_named_face (it->f, face_name, 'A', 0);
3731 if (face_id2 >= 0)
3732 face_id = face_id2;
3733 }
3734
3735 /* Save current settings of IT so that we can restore them
3736 when we are finished with the glyph property value. */
3737
3738 push_it (it);
3739
3740 it->area = TEXT_AREA;
3741 it->what = IT_IMAGE;
3742 it->image_id = -1; /* no image */
3743 it->position = start_pos;
3744 it->object = NILP (object) ? it->w->buffer : object;
3745 it->method = GET_FROM_IMAGE;
3746 it->face_id = face_id;
3747
3748 /* Say that we haven't consumed the characters with
3749 `display' property yet. The call to pop_it in
3750 set_iterator_to_next will clean this up. */
3751 *position = start_pos;
3752
3753 if (EQ (XCAR (spec), Qleft_fringe))
3754 {
3755 it->left_user_fringe_bitmap = fringe_bitmap;
3756 it->left_user_fringe_face_id = face_id;
3757 }
3758 else
3759 {
3760 it->right_user_fringe_bitmap = fringe_bitmap;
3761 it->right_user_fringe_face_id = face_id;
3762 }
3763 #endif /* HAVE_WINDOW_SYSTEM */
3764 return 1;
3765 }
3766
3767 /* Prepare to handle `((margin left-margin) ...)',
3768 `((margin right-margin) ...)' and `((margin nil) ...)'
3769 prefixes for display specifications. */
3770 location = Qunbound;
3771 if (CONSP (spec) && CONSP (XCAR (spec)))
3772 {
3773 Lisp_Object tem;
3774
3775 value = XCDR (spec);
3776 if (CONSP (value))
3777 value = XCAR (value);
3778
3779 tem = XCAR (spec);
3780 if (EQ (XCAR (tem), Qmargin)
3781 && (tem = XCDR (tem),
3782 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3783 (NILP (tem)
3784 || EQ (tem, Qleft_margin)
3785 || EQ (tem, Qright_margin))))
3786 location = tem;
3787 }
3788
3789 if (EQ (location, Qunbound))
3790 {
3791 location = Qnil;
3792 value = spec;
3793 }
3794
3795 /* After this point, VALUE is the property after any
3796 margin prefix has been stripped. It must be a string,
3797 an image specification, or `(space ...)'.
3798
3799 LOCATION specifies where to display: `left-margin',
3800 `right-margin' or nil. */
3801
3802 valid_p = (STRINGP (value)
3803 #ifdef HAVE_WINDOW_SYSTEM
3804 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3805 #endif /* not HAVE_WINDOW_SYSTEM */
3806 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3807
3808 if (valid_p && !display_replaced_before_p)
3809 {
3810 /* Save current settings of IT so that we can restore them
3811 when we are finished with the glyph property value. */
3812 push_it (it);
3813
3814 if (NILP (location))
3815 it->area = TEXT_AREA;
3816 else if (EQ (location, Qleft_margin))
3817 it->area = LEFT_MARGIN_AREA;
3818 else
3819 it->area = RIGHT_MARGIN_AREA;
3820
3821 if (STRINGP (value))
3822 {
3823 it->string = value;
3824 it->multibyte_p = STRING_MULTIBYTE (it->string);
3825 it->current.overlay_string_index = -1;
3826 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3827 it->end_charpos = it->string_nchars = SCHARS (it->string);
3828 it->method = GET_FROM_STRING;
3829 it->stop_charpos = 0;
3830 it->string_from_display_prop_p = 1;
3831 /* Say that we haven't consumed the characters with
3832 `display' property yet. The call to pop_it in
3833 set_iterator_to_next will clean this up. */
3834 *position = start_pos;
3835 }
3836 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3837 {
3838 it->method = GET_FROM_STRETCH;
3839 it->object = value;
3840 it->current.pos = it->position = start_pos;
3841 }
3842 #ifdef HAVE_WINDOW_SYSTEM
3843 else
3844 {
3845 it->what = IT_IMAGE;
3846 it->image_id = lookup_image (it->f, value);
3847 it->position = start_pos;
3848 it->object = NILP (object) ? it->w->buffer : object;
3849 it->method = GET_FROM_IMAGE;
3850
3851 /* Say that we haven't consumed the characters with
3852 `display' property yet. The call to pop_it in
3853 set_iterator_to_next will clean this up. */
3854 *position = start_pos;
3855 }
3856 #endif /* HAVE_WINDOW_SYSTEM */
3857
3858 return 1;
3859 }
3860
3861 /* Invalid property or property not supported. Restore
3862 POSITION to what it was before. */
3863 *position = start_pos;
3864 return 0;
3865 }
3866
3867
3868 /* Check if SPEC is a display specification value whose text should be
3869 treated as intangible. */
3870
3871 static int
3872 single_display_spec_intangible_p (prop)
3873 Lisp_Object prop;
3874 {
3875 /* Skip over `when FORM'. */
3876 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3877 {
3878 prop = XCDR (prop);
3879 if (!CONSP (prop))
3880 return 0;
3881 prop = XCDR (prop);
3882 }
3883
3884 if (STRINGP (prop))
3885 return 1;
3886
3887 if (!CONSP (prop))
3888 return 0;
3889
3890 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3891 we don't need to treat text as intangible. */
3892 if (EQ (XCAR (prop), Qmargin))
3893 {
3894 prop = XCDR (prop);
3895 if (!CONSP (prop))
3896 return 0;
3897
3898 prop = XCDR (prop);
3899 if (!CONSP (prop)
3900 || EQ (XCAR (prop), Qleft_margin)
3901 || EQ (XCAR (prop), Qright_margin))
3902 return 0;
3903 }
3904
3905 return (CONSP (prop)
3906 && (EQ (XCAR (prop), Qimage)
3907 || EQ (XCAR (prop), Qspace)));
3908 }
3909
3910
3911 /* Check if PROP is a display property value whose text should be
3912 treated as intangible. */
3913
3914 int
3915 display_prop_intangible_p (prop)
3916 Lisp_Object prop;
3917 {
3918 if (CONSP (prop)
3919 && CONSP (XCAR (prop))
3920 && !EQ (Qmargin, XCAR (XCAR (prop))))
3921 {
3922 /* A list of sub-properties. */
3923 while (CONSP (prop))
3924 {
3925 if (single_display_spec_intangible_p (XCAR (prop)))
3926 return 1;
3927 prop = XCDR (prop);
3928 }
3929 }
3930 else if (VECTORP (prop))
3931 {
3932 /* A vector of sub-properties. */
3933 int i;
3934 for (i = 0; i < ASIZE (prop); ++i)
3935 if (single_display_spec_intangible_p (AREF (prop, i)))
3936 return 1;
3937 }
3938 else
3939 return single_display_spec_intangible_p (prop);
3940
3941 return 0;
3942 }
3943
3944
3945 /* Return 1 if PROP is a display sub-property value containing STRING. */
3946
3947 static int
3948 single_display_spec_string_p (prop, string)
3949 Lisp_Object prop, string;
3950 {
3951 if (EQ (string, prop))
3952 return 1;
3953
3954 /* Skip over `when FORM'. */
3955 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3956 {
3957 prop = XCDR (prop);
3958 if (!CONSP (prop))
3959 return 0;
3960 prop = XCDR (prop);
3961 }
3962
3963 if (CONSP (prop))
3964 /* Skip over `margin LOCATION'. */
3965 if (EQ (XCAR (prop), Qmargin))
3966 {
3967 prop = XCDR (prop);
3968 if (!CONSP (prop))
3969 return 0;
3970
3971 prop = XCDR (prop);
3972 if (!CONSP (prop))
3973 return 0;
3974 }
3975
3976 return CONSP (prop) && EQ (XCAR (prop), string);
3977 }
3978
3979
3980 /* Return 1 if STRING appears in the `display' property PROP. */
3981
3982 static int
3983 display_prop_string_p (prop, string)
3984 Lisp_Object prop, string;
3985 {
3986 if (CONSP (prop)
3987 && CONSP (XCAR (prop))
3988 && !EQ (Qmargin, XCAR (XCAR (prop))))
3989 {
3990 /* A list of sub-properties. */
3991 while (CONSP (prop))
3992 {
3993 if (single_display_spec_string_p (XCAR (prop), string))
3994 return 1;
3995 prop = XCDR (prop);
3996 }
3997 }
3998 else if (VECTORP (prop))
3999 {
4000 /* A vector of sub-properties. */
4001 int i;
4002 for (i = 0; i < ASIZE (prop); ++i)
4003 if (single_display_spec_string_p (AREF (prop, i), string))
4004 return 1;
4005 }
4006 else
4007 return single_display_spec_string_p (prop, string);
4008
4009 return 0;
4010 }
4011
4012
4013 /* Determine from which buffer position in W's buffer STRING comes
4014 from. AROUND_CHARPOS is an approximate position where it could
4015 be from. Value is the buffer position or 0 if it couldn't be
4016 determined.
4017
4018 W's buffer must be current.
4019
4020 This function is necessary because we don't record buffer positions
4021 in glyphs generated from strings (to keep struct glyph small).
4022 This function may only use code that doesn't eval because it is
4023 called asynchronously from note_mouse_highlight. */
4024
4025 int
4026 string_buffer_position (w, string, around_charpos)
4027 struct window *w;
4028 Lisp_Object string;
4029 int around_charpos;
4030 {
4031 Lisp_Object limit, prop, pos;
4032 const int MAX_DISTANCE = 1000;
4033 int found = 0;
4034
4035 pos = make_number (around_charpos);
4036 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4037 while (!found && !EQ (pos, limit))
4038 {
4039 prop = Fget_char_property (pos, Qdisplay, Qnil);
4040 if (!NILP (prop) && display_prop_string_p (prop, string))
4041 found = 1;
4042 else
4043 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4044 }
4045
4046 if (!found)
4047 {
4048 pos = make_number (around_charpos);
4049 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4050 while (!found && !EQ (pos, limit))
4051 {
4052 prop = Fget_char_property (pos, Qdisplay, Qnil);
4053 if (!NILP (prop) && display_prop_string_p (prop, string))
4054 found = 1;
4055 else
4056 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4057 limit);
4058 }
4059 }
4060
4061 return found ? XINT (pos) : 0;
4062 }
4063
4064
4065 \f
4066 /***********************************************************************
4067 `composition' property
4068 ***********************************************************************/
4069
4070 /* Set up iterator IT from `composition' property at its current
4071 position. Called from handle_stop. */
4072
4073 static enum prop_handled
4074 handle_composition_prop (it)
4075 struct it *it;
4076 {
4077 Lisp_Object prop, string;
4078 int pos, pos_byte, end;
4079 enum prop_handled handled = HANDLED_NORMALLY;
4080
4081 if (STRINGP (it->string))
4082 {
4083 pos = IT_STRING_CHARPOS (*it);
4084 pos_byte = IT_STRING_BYTEPOS (*it);
4085 string = it->string;
4086 }
4087 else
4088 {
4089 pos = IT_CHARPOS (*it);
4090 pos_byte = IT_BYTEPOS (*it);
4091 string = Qnil;
4092 }
4093
4094 /* If there's a valid composition and point is not inside of the
4095 composition (in the case that the composition is from the current
4096 buffer), draw a glyph composed from the composition components. */
4097 if (find_composition (pos, -1, &pos, &end, &prop, string)
4098 && COMPOSITION_VALID_P (pos, end, prop)
4099 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4100 {
4101 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4102
4103 if (id >= 0)
4104 {
4105 it->method = GET_FROM_COMPOSITION;
4106 it->cmp_id = id;
4107 it->cmp_len = COMPOSITION_LENGTH (prop);
4108 /* For a terminal, draw only the first character of the
4109 components. */
4110 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4111 it->len = (STRINGP (it->string)
4112 ? string_char_to_byte (it->string, end)
4113 : CHAR_TO_BYTE (end)) - pos_byte;
4114 it->stop_charpos = end;
4115 handled = HANDLED_RETURN;
4116 }
4117 }
4118
4119 return handled;
4120 }
4121
4122
4123 \f
4124 /***********************************************************************
4125 Overlay strings
4126 ***********************************************************************/
4127
4128 /* The following structure is used to record overlay strings for
4129 later sorting in load_overlay_strings. */
4130
4131 struct overlay_entry
4132 {
4133 Lisp_Object overlay;
4134 Lisp_Object string;
4135 int priority;
4136 int after_string_p;
4137 };
4138
4139
4140 /* Set up iterator IT from overlay strings at its current position.
4141 Called from handle_stop. */
4142
4143 static enum prop_handled
4144 handle_overlay_change (it)
4145 struct it *it;
4146 {
4147 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4148 return HANDLED_RECOMPUTE_PROPS;
4149 else
4150 return HANDLED_NORMALLY;
4151 }
4152
4153
4154 /* Set up the next overlay string for delivery by IT, if there is an
4155 overlay string to deliver. Called by set_iterator_to_next when the
4156 end of the current overlay string is reached. If there are more
4157 overlay strings to display, IT->string and
4158 IT->current.overlay_string_index are set appropriately here.
4159 Otherwise IT->string is set to nil. */
4160
4161 static void
4162 next_overlay_string (it)
4163 struct it *it;
4164 {
4165 ++it->current.overlay_string_index;
4166 if (it->current.overlay_string_index == it->n_overlay_strings)
4167 {
4168 /* No more overlay strings. Restore IT's settings to what
4169 they were before overlay strings were processed, and
4170 continue to deliver from current_buffer. */
4171 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4172
4173 pop_it (it);
4174 xassert (it->stop_charpos >= BEGV
4175 && it->stop_charpos <= it->end_charpos);
4176 it->string = Qnil;
4177 it->current.overlay_string_index = -1;
4178 SET_TEXT_POS (it->current.string_pos, -1, -1);
4179 it->n_overlay_strings = 0;
4180 it->method = GET_FROM_BUFFER;
4181
4182 /* If we're at the end of the buffer, record that we have
4183 processed the overlay strings there already, so that
4184 next_element_from_buffer doesn't try it again. */
4185 if (IT_CHARPOS (*it) >= it->end_charpos)
4186 it->overlay_strings_at_end_processed_p = 1;
4187
4188 /* If we have to display `...' for invisible text, set
4189 the iterator up for that. */
4190 if (display_ellipsis_p)
4191 setup_for_ellipsis (it, 0);
4192 }
4193 else
4194 {
4195 /* There are more overlay strings to process. If
4196 IT->current.overlay_string_index has advanced to a position
4197 where we must load IT->overlay_strings with more strings, do
4198 it. */
4199 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4200
4201 if (it->current.overlay_string_index && i == 0)
4202 load_overlay_strings (it, 0);
4203
4204 /* Initialize IT to deliver display elements from the overlay
4205 string. */
4206 it->string = it->overlay_strings[i];
4207 it->multibyte_p = STRING_MULTIBYTE (it->string);
4208 SET_TEXT_POS (it->current.string_pos, 0, 0);
4209 it->method = GET_FROM_STRING;
4210 it->stop_charpos = 0;
4211 }
4212
4213 CHECK_IT (it);
4214 }
4215
4216
4217 /* Compare two overlay_entry structures E1 and E2. Used as a
4218 comparison function for qsort in load_overlay_strings. Overlay
4219 strings for the same position are sorted so that
4220
4221 1. All after-strings come in front of before-strings, except
4222 when they come from the same overlay.
4223
4224 2. Within after-strings, strings are sorted so that overlay strings
4225 from overlays with higher priorities come first.
4226
4227 2. Within before-strings, strings are sorted so that overlay
4228 strings from overlays with higher priorities come last.
4229
4230 Value is analogous to strcmp. */
4231
4232
4233 static int
4234 compare_overlay_entries (e1, e2)
4235 void *e1, *e2;
4236 {
4237 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4238 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4239 int result;
4240
4241 if (entry1->after_string_p != entry2->after_string_p)
4242 {
4243 /* Let after-strings appear in front of before-strings if
4244 they come from different overlays. */
4245 if (EQ (entry1->overlay, entry2->overlay))
4246 result = entry1->after_string_p ? 1 : -1;
4247 else
4248 result = entry1->after_string_p ? -1 : 1;
4249 }
4250 else if (entry1->after_string_p)
4251 /* After-strings sorted in order of decreasing priority. */
4252 result = entry2->priority - entry1->priority;
4253 else
4254 /* Before-strings sorted in order of increasing priority. */
4255 result = entry1->priority - entry2->priority;
4256
4257 return result;
4258 }
4259
4260
4261 /* Load the vector IT->overlay_strings with overlay strings from IT's
4262 current buffer position, or from CHARPOS if that is > 0. Set
4263 IT->n_overlays to the total number of overlay strings found.
4264
4265 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4266 a time. On entry into load_overlay_strings,
4267 IT->current.overlay_string_index gives the number of overlay
4268 strings that have already been loaded by previous calls to this
4269 function.
4270
4271 IT->add_overlay_start contains an additional overlay start
4272 position to consider for taking overlay strings from, if non-zero.
4273 This position comes into play when the overlay has an `invisible'
4274 property, and both before and after-strings. When we've skipped to
4275 the end of the overlay, because of its `invisible' property, we
4276 nevertheless want its before-string to appear.
4277 IT->add_overlay_start will contain the overlay start position
4278 in this case.
4279
4280 Overlay strings are sorted so that after-string strings come in
4281 front of before-string strings. Within before and after-strings,
4282 strings are sorted by overlay priority. See also function
4283 compare_overlay_entries. */
4284
4285 static void
4286 load_overlay_strings (it, charpos)
4287 struct it *it;
4288 int charpos;
4289 {
4290 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4291 Lisp_Object overlay, window, str, invisible;
4292 struct Lisp_Overlay *ov;
4293 int start, end;
4294 int size = 20;
4295 int n = 0, i, j, invis_p;
4296 struct overlay_entry *entries
4297 = (struct overlay_entry *) alloca (size * sizeof *entries);
4298
4299 if (charpos <= 0)
4300 charpos = IT_CHARPOS (*it);
4301
4302 /* Append the overlay string STRING of overlay OVERLAY to vector
4303 `entries' which has size `size' and currently contains `n'
4304 elements. AFTER_P non-zero means STRING is an after-string of
4305 OVERLAY. */
4306 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4307 do \
4308 { \
4309 Lisp_Object priority; \
4310 \
4311 if (n == size) \
4312 { \
4313 int new_size = 2 * size; \
4314 struct overlay_entry *old = entries; \
4315 entries = \
4316 (struct overlay_entry *) alloca (new_size \
4317 * sizeof *entries); \
4318 bcopy (old, entries, size * sizeof *entries); \
4319 size = new_size; \
4320 } \
4321 \
4322 entries[n].string = (STRING); \
4323 entries[n].overlay = (OVERLAY); \
4324 priority = Foverlay_get ((OVERLAY), Qpriority); \
4325 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4326 entries[n].after_string_p = (AFTER_P); \
4327 ++n; \
4328 } \
4329 while (0)
4330
4331 /* Process overlay before the overlay center. */
4332 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4333 {
4334 XSETMISC (overlay, ov);
4335 xassert (OVERLAYP (overlay));
4336 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4337 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4338
4339 if (end < charpos)
4340 break;
4341
4342 /* Skip this overlay if it doesn't start or end at IT's current
4343 position. */
4344 if (end != charpos && start != charpos)
4345 continue;
4346
4347 /* Skip this overlay if it doesn't apply to IT->w. */
4348 window = Foverlay_get (overlay, Qwindow);
4349 if (WINDOWP (window) && XWINDOW (window) != it->w)
4350 continue;
4351
4352 /* If the text ``under'' the overlay is invisible, both before-
4353 and after-strings from this overlay are visible; start and
4354 end position are indistinguishable. */
4355 invisible = Foverlay_get (overlay, Qinvisible);
4356 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4357
4358 /* If overlay has a non-empty before-string, record it. */
4359 if ((start == charpos || (end == charpos && invis_p))
4360 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4361 && SCHARS (str))
4362 RECORD_OVERLAY_STRING (overlay, str, 0);
4363
4364 /* If overlay has a non-empty after-string, record it. */
4365 if ((end == charpos || (start == charpos && invis_p))
4366 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4367 && SCHARS (str))
4368 RECORD_OVERLAY_STRING (overlay, str, 1);
4369 }
4370
4371 /* Process overlays after the overlay center. */
4372 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4373 {
4374 XSETMISC (overlay, ov);
4375 xassert (OVERLAYP (overlay));
4376 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4377 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4378
4379 if (start > charpos)
4380 break;
4381
4382 /* Skip this overlay if it doesn't start or end at IT's current
4383 position. */
4384 if (end != charpos && start != charpos)
4385 continue;
4386
4387 /* Skip this overlay if it doesn't apply to IT->w. */
4388 window = Foverlay_get (overlay, Qwindow);
4389 if (WINDOWP (window) && XWINDOW (window) != it->w)
4390 continue;
4391
4392 /* If the text ``under'' the overlay is invisible, it has a zero
4393 dimension, and both before- and after-strings apply. */
4394 invisible = Foverlay_get (overlay, Qinvisible);
4395 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4396
4397 /* If overlay has a non-empty before-string, record it. */
4398 if ((start == charpos || (end == charpos && invis_p))
4399 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4400 && SCHARS (str))
4401 RECORD_OVERLAY_STRING (overlay, str, 0);
4402
4403 /* If overlay has a non-empty after-string, record it. */
4404 if ((end == charpos || (start == charpos && invis_p))
4405 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4406 && SCHARS (str))
4407 RECORD_OVERLAY_STRING (overlay, str, 1);
4408 }
4409
4410 #undef RECORD_OVERLAY_STRING
4411
4412 /* Sort entries. */
4413 if (n > 1)
4414 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4415
4416 /* Record the total number of strings to process. */
4417 it->n_overlay_strings = n;
4418
4419 /* IT->current.overlay_string_index is the number of overlay strings
4420 that have already been consumed by IT. Copy some of the
4421 remaining overlay strings to IT->overlay_strings. */
4422 i = 0;
4423 j = it->current.overlay_string_index;
4424 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4425 it->overlay_strings[i++] = entries[j++].string;
4426
4427 CHECK_IT (it);
4428 }
4429
4430
4431 /* Get the first chunk of overlay strings at IT's current buffer
4432 position, or at CHARPOS if that is > 0. Value is non-zero if at
4433 least one overlay string was found. */
4434
4435 static int
4436 get_overlay_strings (it, charpos)
4437 struct it *it;
4438 int charpos;
4439 {
4440 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4441 process. This fills IT->overlay_strings with strings, and sets
4442 IT->n_overlay_strings to the total number of strings to process.
4443 IT->pos.overlay_string_index has to be set temporarily to zero
4444 because load_overlay_strings needs this; it must be set to -1
4445 when no overlay strings are found because a zero value would
4446 indicate a position in the first overlay string. */
4447 it->current.overlay_string_index = 0;
4448 load_overlay_strings (it, charpos);
4449
4450 /* If we found overlay strings, set up IT to deliver display
4451 elements from the first one. Otherwise set up IT to deliver
4452 from current_buffer. */
4453 if (it->n_overlay_strings)
4454 {
4455 /* Make sure we know settings in current_buffer, so that we can
4456 restore meaningful values when we're done with the overlay
4457 strings. */
4458 compute_stop_pos (it);
4459 xassert (it->face_id >= 0);
4460
4461 /* Save IT's settings. They are restored after all overlay
4462 strings have been processed. */
4463 xassert (it->sp == 0);
4464 push_it (it);
4465
4466 /* Set up IT to deliver display elements from the first overlay
4467 string. */
4468 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4469 it->string = it->overlay_strings[0];
4470 it->stop_charpos = 0;
4471 xassert (STRINGP (it->string));
4472 it->end_charpos = SCHARS (it->string);
4473 it->multibyte_p = STRING_MULTIBYTE (it->string);
4474 it->method = GET_FROM_STRING;
4475 }
4476 else
4477 {
4478 it->string = Qnil;
4479 it->current.overlay_string_index = -1;
4480 it->method = GET_FROM_BUFFER;
4481 }
4482
4483 CHECK_IT (it);
4484
4485 /* Value is non-zero if we found at least one overlay string. */
4486 return STRINGP (it->string);
4487 }
4488
4489
4490 \f
4491 /***********************************************************************
4492 Saving and restoring state
4493 ***********************************************************************/
4494
4495 /* Save current settings of IT on IT->stack. Called, for example,
4496 before setting up IT for an overlay string, to be able to restore
4497 IT's settings to what they were after the overlay string has been
4498 processed. */
4499
4500 static void
4501 push_it (it)
4502 struct it *it;
4503 {
4504 struct iterator_stack_entry *p;
4505
4506 xassert (it->sp < 2);
4507 p = it->stack + it->sp;
4508
4509 p->stop_charpos = it->stop_charpos;
4510 xassert (it->face_id >= 0);
4511 p->face_id = it->face_id;
4512 p->string = it->string;
4513 p->pos = it->current;
4514 p->end_charpos = it->end_charpos;
4515 p->string_nchars = it->string_nchars;
4516 p->area = it->area;
4517 p->multibyte_p = it->multibyte_p;
4518 p->slice = it->slice;
4519 p->space_width = it->space_width;
4520 p->font_height = it->font_height;
4521 p->voffset = it->voffset;
4522 p->string_from_display_prop_p = it->string_from_display_prop_p;
4523 p->display_ellipsis_p = 0;
4524 ++it->sp;
4525 }
4526
4527
4528 /* Restore IT's settings from IT->stack. Called, for example, when no
4529 more overlay strings must be processed, and we return to delivering
4530 display elements from a buffer, or when the end of a string from a
4531 `display' property is reached and we return to delivering display
4532 elements from an overlay string, or from a buffer. */
4533
4534 static void
4535 pop_it (it)
4536 struct it *it;
4537 {
4538 struct iterator_stack_entry *p;
4539
4540 xassert (it->sp > 0);
4541 --it->sp;
4542 p = it->stack + it->sp;
4543 it->stop_charpos = p->stop_charpos;
4544 it->face_id = p->face_id;
4545 it->string = p->string;
4546 it->current = p->pos;
4547 it->end_charpos = p->end_charpos;
4548 it->string_nchars = p->string_nchars;
4549 it->area = p->area;
4550 it->multibyte_p = p->multibyte_p;
4551 it->slice = p->slice;
4552 it->space_width = p->space_width;
4553 it->font_height = p->font_height;
4554 it->voffset = p->voffset;
4555 it->string_from_display_prop_p = p->string_from_display_prop_p;
4556 }
4557
4558
4559 \f
4560 /***********************************************************************
4561 Moving over lines
4562 ***********************************************************************/
4563
4564 /* Set IT's current position to the previous line start. */
4565
4566 static void
4567 back_to_previous_line_start (it)
4568 struct it *it;
4569 {
4570 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4571 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4572 }
4573
4574
4575 /* Move IT to the next line start.
4576
4577 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4578 we skipped over part of the text (as opposed to moving the iterator
4579 continuously over the text). Otherwise, don't change the value
4580 of *SKIPPED_P.
4581
4582 Newlines may come from buffer text, overlay strings, or strings
4583 displayed via the `display' property. That's the reason we can't
4584 simply use find_next_newline_no_quit.
4585
4586 Note that this function may not skip over invisible text that is so
4587 because of text properties and immediately follows a newline. If
4588 it would, function reseat_at_next_visible_line_start, when called
4589 from set_iterator_to_next, would effectively make invisible
4590 characters following a newline part of the wrong glyph row, which
4591 leads to wrong cursor motion. */
4592
4593 static int
4594 forward_to_next_line_start (it, skipped_p)
4595 struct it *it;
4596 int *skipped_p;
4597 {
4598 int old_selective, newline_found_p, n;
4599 const int MAX_NEWLINE_DISTANCE = 500;
4600
4601 /* If already on a newline, just consume it to avoid unintended
4602 skipping over invisible text below. */
4603 if (it->what == IT_CHARACTER
4604 && it->c == '\n'
4605 && CHARPOS (it->position) == IT_CHARPOS (*it))
4606 {
4607 set_iterator_to_next (it, 0);
4608 it->c = 0;
4609 return 1;
4610 }
4611
4612 /* Don't handle selective display in the following. It's (a)
4613 unnecessary because it's done by the caller, and (b) leads to an
4614 infinite recursion because next_element_from_ellipsis indirectly
4615 calls this function. */
4616 old_selective = it->selective;
4617 it->selective = 0;
4618
4619 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4620 from buffer text. */
4621 for (n = newline_found_p = 0;
4622 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4623 n += STRINGP (it->string) ? 0 : 1)
4624 {
4625 if (!get_next_display_element (it))
4626 return 0;
4627 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4628 set_iterator_to_next (it, 0);
4629 }
4630
4631 /* If we didn't find a newline near enough, see if we can use a
4632 short-cut. */
4633 if (!newline_found_p)
4634 {
4635 int start = IT_CHARPOS (*it);
4636 int limit = find_next_newline_no_quit (start, 1);
4637 Lisp_Object pos;
4638
4639 xassert (!STRINGP (it->string));
4640
4641 /* If there isn't any `display' property in sight, and no
4642 overlays, we can just use the position of the newline in
4643 buffer text. */
4644 if (it->stop_charpos >= limit
4645 || ((pos = Fnext_single_property_change (make_number (start),
4646 Qdisplay,
4647 Qnil, make_number (limit)),
4648 NILP (pos))
4649 && next_overlay_change (start) == ZV))
4650 {
4651 IT_CHARPOS (*it) = limit;
4652 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4653 *skipped_p = newline_found_p = 1;
4654 }
4655 else
4656 {
4657 while (get_next_display_element (it)
4658 && !newline_found_p)
4659 {
4660 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4661 set_iterator_to_next (it, 0);
4662 }
4663 }
4664 }
4665
4666 it->selective = old_selective;
4667 return newline_found_p;
4668 }
4669
4670
4671 /* Set IT's current position to the previous visible line start. Skip
4672 invisible text that is so either due to text properties or due to
4673 selective display. Caution: this does not change IT->current_x and
4674 IT->hpos. */
4675
4676 static void
4677 back_to_previous_visible_line_start (it)
4678 struct it *it;
4679 {
4680 while (IT_CHARPOS (*it) > BEGV)
4681 {
4682 back_to_previous_line_start (it);
4683 if (IT_CHARPOS (*it) <= BEGV)
4684 break;
4685
4686 /* If selective > 0, then lines indented more than that values
4687 are invisible. */
4688 if (it->selective > 0
4689 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4690 (double) it->selective)) /* iftc */
4691 continue;
4692
4693 /* Check the newline before point for invisibility. */
4694 {
4695 Lisp_Object prop;
4696 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4697 Qinvisible, it->window);
4698 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4699 continue;
4700 }
4701
4702 /* If newline has a display property that replaces the newline with something
4703 else (image or text), find start of overlay or interval and continue search
4704 from that point. */
4705 if (IT_CHARPOS (*it) > BEGV)
4706 {
4707 struct it it2 = *it;
4708 int pos;
4709 int beg, end;
4710 Lisp_Object val, overlay;
4711
4712 pos = --IT_CHARPOS (it2);
4713 --IT_BYTEPOS (it2);
4714 it2.sp = 0;
4715 if (handle_display_prop (&it2) == HANDLED_RETURN
4716 && !NILP (val = get_char_property_and_overlay
4717 (make_number (pos), Qdisplay, Qnil, &overlay))
4718 && (OVERLAYP (overlay)
4719 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4720 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4721 {
4722 if (beg < BEGV)
4723 beg = BEGV;
4724 IT_CHARPOS (*it) = beg;
4725 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4726 continue;
4727 }
4728 }
4729
4730 break;
4731 }
4732
4733 xassert (IT_CHARPOS (*it) >= BEGV);
4734 xassert (IT_CHARPOS (*it) == BEGV
4735 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4736 CHECK_IT (it);
4737 }
4738
4739
4740 /* Reseat iterator IT at the previous visible line start. Skip
4741 invisible text that is so either due to text properties or due to
4742 selective display. At the end, update IT's overlay information,
4743 face information etc. */
4744
4745 void
4746 reseat_at_previous_visible_line_start (it)
4747 struct it *it;
4748 {
4749 back_to_previous_visible_line_start (it);
4750 reseat (it, it->current.pos, 1);
4751 CHECK_IT (it);
4752 }
4753
4754
4755 /* Reseat iterator IT on the next visible line start in the current
4756 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4757 preceding the line start. Skip over invisible text that is so
4758 because of selective display. Compute faces, overlays etc at the
4759 new position. Note that this function does not skip over text that
4760 is invisible because of text properties. */
4761
4762 static void
4763 reseat_at_next_visible_line_start (it, on_newline_p)
4764 struct it *it;
4765 int on_newline_p;
4766 {
4767 int newline_found_p, skipped_p = 0;
4768
4769 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4770
4771 /* Skip over lines that are invisible because they are indented
4772 more than the value of IT->selective. */
4773 if (it->selective > 0)
4774 while (IT_CHARPOS (*it) < ZV
4775 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4776 (double) it->selective)) /* iftc */
4777 {
4778 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4779 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4780 }
4781
4782 /* Position on the newline if that's what's requested. */
4783 if (on_newline_p && newline_found_p)
4784 {
4785 if (STRINGP (it->string))
4786 {
4787 if (IT_STRING_CHARPOS (*it) > 0)
4788 {
4789 --IT_STRING_CHARPOS (*it);
4790 --IT_STRING_BYTEPOS (*it);
4791 }
4792 }
4793 else if (IT_CHARPOS (*it) > BEGV)
4794 {
4795 --IT_CHARPOS (*it);
4796 --IT_BYTEPOS (*it);
4797 reseat (it, it->current.pos, 0);
4798 }
4799 }
4800 else if (skipped_p)
4801 reseat (it, it->current.pos, 0);
4802
4803 CHECK_IT (it);
4804 }
4805
4806
4807 \f
4808 /***********************************************************************
4809 Changing an iterator's position
4810 ***********************************************************************/
4811
4812 /* Change IT's current position to POS in current_buffer. If FORCE_P
4813 is non-zero, always check for text properties at the new position.
4814 Otherwise, text properties are only looked up if POS >=
4815 IT->check_charpos of a property. */
4816
4817 static void
4818 reseat (it, pos, force_p)
4819 struct it *it;
4820 struct text_pos pos;
4821 int force_p;
4822 {
4823 int original_pos = IT_CHARPOS (*it);
4824
4825 reseat_1 (it, pos, 0);
4826
4827 /* Determine where to check text properties. Avoid doing it
4828 where possible because text property lookup is very expensive. */
4829 if (force_p
4830 || CHARPOS (pos) > it->stop_charpos
4831 || CHARPOS (pos) < original_pos)
4832 handle_stop (it);
4833
4834 CHECK_IT (it);
4835 }
4836
4837
4838 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4839 IT->stop_pos to POS, also. */
4840
4841 static void
4842 reseat_1 (it, pos, set_stop_p)
4843 struct it *it;
4844 struct text_pos pos;
4845 int set_stop_p;
4846 {
4847 /* Don't call this function when scanning a C string. */
4848 xassert (it->s == NULL);
4849
4850 /* POS must be a reasonable value. */
4851 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4852
4853 it->current.pos = it->position = pos;
4854 XSETBUFFER (it->object, current_buffer);
4855 it->end_charpos = ZV;
4856 it->dpvec = NULL;
4857 it->current.dpvec_index = -1;
4858 it->current.overlay_string_index = -1;
4859 IT_STRING_CHARPOS (*it) = -1;
4860 IT_STRING_BYTEPOS (*it) = -1;
4861 it->string = Qnil;
4862 it->method = GET_FROM_BUFFER;
4863 /* RMS: I added this to fix a bug in move_it_vertically_backward
4864 where it->area continued to relate to the starting point
4865 for the backward motion. Bug report from
4866 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4867 However, I am not sure whether reseat still does the right thing
4868 in general after this change. */
4869 it->area = TEXT_AREA;
4870 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4871 it->sp = 0;
4872 it->face_before_selective_p = 0;
4873
4874 if (set_stop_p)
4875 it->stop_charpos = CHARPOS (pos);
4876 }
4877
4878
4879 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4880 If S is non-null, it is a C string to iterate over. Otherwise,
4881 STRING gives a Lisp string to iterate over.
4882
4883 If PRECISION > 0, don't return more then PRECISION number of
4884 characters from the string.
4885
4886 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4887 characters have been returned. FIELD_WIDTH < 0 means an infinite
4888 field width.
4889
4890 MULTIBYTE = 0 means disable processing of multibyte characters,
4891 MULTIBYTE > 0 means enable it,
4892 MULTIBYTE < 0 means use IT->multibyte_p.
4893
4894 IT must be initialized via a prior call to init_iterator before
4895 calling this function. */
4896
4897 static void
4898 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4899 struct it *it;
4900 unsigned char *s;
4901 Lisp_Object string;
4902 int charpos;
4903 int precision, field_width, multibyte;
4904 {
4905 /* No region in strings. */
4906 it->region_beg_charpos = it->region_end_charpos = -1;
4907
4908 /* No text property checks performed by default, but see below. */
4909 it->stop_charpos = -1;
4910
4911 /* Set iterator position and end position. */
4912 bzero (&it->current, sizeof it->current);
4913 it->current.overlay_string_index = -1;
4914 it->current.dpvec_index = -1;
4915 xassert (charpos >= 0);
4916
4917 /* If STRING is specified, use its multibyteness, otherwise use the
4918 setting of MULTIBYTE, if specified. */
4919 if (multibyte >= 0)
4920 it->multibyte_p = multibyte > 0;
4921
4922 if (s == NULL)
4923 {
4924 xassert (STRINGP (string));
4925 it->string = string;
4926 it->s = NULL;
4927 it->end_charpos = it->string_nchars = SCHARS (string);
4928 it->method = GET_FROM_STRING;
4929 it->current.string_pos = string_pos (charpos, string);
4930 }
4931 else
4932 {
4933 it->s = s;
4934 it->string = Qnil;
4935
4936 /* Note that we use IT->current.pos, not it->current.string_pos,
4937 for displaying C strings. */
4938 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4939 if (it->multibyte_p)
4940 {
4941 it->current.pos = c_string_pos (charpos, s, 1);
4942 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4943 }
4944 else
4945 {
4946 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4947 it->end_charpos = it->string_nchars = strlen (s);
4948 }
4949
4950 it->method = GET_FROM_C_STRING;
4951 }
4952
4953 /* PRECISION > 0 means don't return more than PRECISION characters
4954 from the string. */
4955 if (precision > 0 && it->end_charpos - charpos > precision)
4956 it->end_charpos = it->string_nchars = charpos + precision;
4957
4958 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4959 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4960 FIELD_WIDTH < 0 means infinite field width. This is useful for
4961 padding with `-' at the end of a mode line. */
4962 if (field_width < 0)
4963 field_width = INFINITY;
4964 if (field_width > it->end_charpos - charpos)
4965 it->end_charpos = charpos + field_width;
4966
4967 /* Use the standard display table for displaying strings. */
4968 if (DISP_TABLE_P (Vstandard_display_table))
4969 it->dp = XCHAR_TABLE (Vstandard_display_table);
4970
4971 it->stop_charpos = charpos;
4972 CHECK_IT (it);
4973 }
4974
4975
4976 \f
4977 /***********************************************************************
4978 Iteration
4979 ***********************************************************************/
4980
4981 /* Map enum it_method value to corresponding next_element_from_* function. */
4982
4983 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
4984 {
4985 next_element_from_buffer,
4986 next_element_from_display_vector,
4987 next_element_from_composition,
4988 next_element_from_string,
4989 next_element_from_c_string,
4990 next_element_from_image,
4991 next_element_from_stretch
4992 };
4993
4994
4995 /* Load IT's display element fields with information about the next
4996 display element from the current position of IT. Value is zero if
4997 end of buffer (or C string) is reached. */
4998
4999 int
5000 get_next_display_element (it)
5001 struct it *it;
5002 {
5003 /* Non-zero means that we found a display element. Zero means that
5004 we hit the end of what we iterate over. Performance note: the
5005 function pointer `method' used here turns out to be faster than
5006 using a sequence of if-statements. */
5007 int success_p;
5008
5009 get_next:
5010 success_p = (*get_next_element[it->method]) (it);
5011
5012 if (it->what == IT_CHARACTER)
5013 {
5014 /* Map via display table or translate control characters.
5015 IT->c, IT->len etc. have been set to the next character by
5016 the function call above. If we have a display table, and it
5017 contains an entry for IT->c, translate it. Don't do this if
5018 IT->c itself comes from a display table, otherwise we could
5019 end up in an infinite recursion. (An alternative could be to
5020 count the recursion depth of this function and signal an
5021 error when a certain maximum depth is reached.) Is it worth
5022 it? */
5023 if (success_p && it->dpvec == NULL)
5024 {
5025 Lisp_Object dv;
5026
5027 if (it->dp
5028 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5029 VECTORP (dv)))
5030 {
5031 struct Lisp_Vector *v = XVECTOR (dv);
5032
5033 /* Return the first character from the display table
5034 entry, if not empty. If empty, don't display the
5035 current character. */
5036 if (v->size)
5037 {
5038 it->dpvec_char_len = it->len;
5039 it->dpvec = v->contents;
5040 it->dpend = v->contents + v->size;
5041 it->current.dpvec_index = 0;
5042 it->dpvec_face_id = -1;
5043 it->saved_face_id = it->face_id;
5044 it->method = GET_FROM_DISPLAY_VECTOR;
5045 it->ellipsis_p = 0;
5046 }
5047 else
5048 {
5049 set_iterator_to_next (it, 0);
5050 }
5051 goto get_next;
5052 }
5053
5054 /* Translate control characters into `\003' or `^C' form.
5055 Control characters coming from a display table entry are
5056 currently not translated because we use IT->dpvec to hold
5057 the translation. This could easily be changed but I
5058 don't believe that it is worth doing.
5059
5060 If it->multibyte_p is nonzero, eight-bit characters and
5061 non-printable multibyte characters are also translated to
5062 octal form.
5063
5064 If it->multibyte_p is zero, eight-bit characters that
5065 don't have corresponding multibyte char code are also
5066 translated to octal form. */
5067 else if ((it->c < ' '
5068 && (it->area != TEXT_AREA
5069 /* In mode line, treat \n like other crl chars. */
5070 || (it->c != '\t'
5071 && it->glyph_row && it->glyph_row->mode_line_p)
5072 || (it->c != '\n' && it->c != '\t')))
5073 || (it->multibyte_p
5074 ? ((it->c >= 127
5075 && it->len == 1)
5076 || !CHAR_PRINTABLE_P (it->c)
5077 || (!NILP (Vshow_nonbreak_escape)
5078 && (it->c == 0x8ad || it->c == 0x8a0)))
5079 : (it->c >= 127
5080 && (!unibyte_display_via_language_environment
5081 || it->c == unibyte_char_to_multibyte (it->c)))))
5082 {
5083 /* IT->c is a control character which must be displayed
5084 either as '\003' or as `^C' where the '\\' and '^'
5085 can be defined in the display table. Fill
5086 IT->ctl_chars with glyphs for what we have to
5087 display. Then, set IT->dpvec to these glyphs. */
5088 GLYPH g;
5089 int ctl_len;
5090 int face_id, lface_id = 0 ;
5091 GLYPH escape_glyph;
5092
5093 if (it->c < 128 && it->ctl_arrow_p)
5094 {
5095 g = '^'; /* default glyph for Control */
5096 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5097 if (it->dp
5098 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5099 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5100 {
5101 g = XINT (DISP_CTRL_GLYPH (it->dp));
5102 lface_id = FAST_GLYPH_FACE (g);
5103 }
5104 if (lface_id)
5105 {
5106 g = FAST_GLYPH_CHAR (g);
5107 face_id = merge_faces (it->f, Qt, lface_id,
5108 it->face_id);
5109 }
5110 else
5111 {
5112 /* Merge the escape-glyph face into the current face. */
5113 face_id = merge_faces (it->f, Qescape_glyph, 0,
5114 it->face_id);
5115 }
5116
5117 XSETINT (it->ctl_chars[0], g);
5118 g = it->c ^ 0100;
5119 XSETINT (it->ctl_chars[1], g);
5120 ctl_len = 2;
5121 goto display_control;
5122 }
5123
5124 escape_glyph = '\\'; /* default for Octal display */
5125 if (it->dp
5126 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5127 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5128 {
5129 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5130 lface_id = FAST_GLYPH_FACE (escape_glyph);
5131 }
5132 if (lface_id)
5133 {
5134 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5135 face_id = merge_faces (it->f, Qt, lface_id,
5136 it->face_id);
5137 }
5138 else
5139 {
5140 /* Merge the escape-glyph face into the current face. */
5141 face_id = merge_faces (it->f, Qescape_glyph, 0,
5142 it->face_id);
5143 }
5144
5145 if (it->c == 0x8a0 || it->c == 0x8ad)
5146 {
5147 XSETINT (it->ctl_chars[0], escape_glyph);
5148 g = it->c == 0x8ad ? '-' : ' ';
5149 XSETINT (it->ctl_chars[1], g);
5150 ctl_len = 2;
5151 goto display_control;
5152 }
5153
5154 {
5155 unsigned char str[MAX_MULTIBYTE_LENGTH];
5156 int len;
5157 int i;
5158
5159 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5160 if (SINGLE_BYTE_CHAR_P (it->c))
5161 str[0] = it->c, len = 1;
5162 else
5163 {
5164 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5165 if (len < 0)
5166 {
5167 /* It's an invalid character, which shouldn't
5168 happen actually, but due to bugs it may
5169 happen. Let's print the char as is, there's
5170 not much meaningful we can do with it. */
5171 str[0] = it->c;
5172 str[1] = it->c >> 8;
5173 str[2] = it->c >> 16;
5174 str[3] = it->c >> 24;
5175 len = 4;
5176 }
5177 }
5178
5179 for (i = 0; i < len; i++)
5180 {
5181 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5182 /* Insert three more glyphs into IT->ctl_chars for
5183 the octal display of the character. */
5184 g = ((str[i] >> 6) & 7) + '0';
5185 XSETINT (it->ctl_chars[i * 4 + 1], g);
5186 g = ((str[i] >> 3) & 7) + '0';
5187 XSETINT (it->ctl_chars[i * 4 + 2], g);
5188 g = (str[i] & 7) + '0';
5189 XSETINT (it->ctl_chars[i * 4 + 3], g);
5190 }
5191 ctl_len = len * 4;
5192 }
5193
5194 display_control:
5195 /* Set up IT->dpvec and return first character from it. */
5196 it->dpvec_char_len = it->len;
5197 it->dpvec = it->ctl_chars;
5198 it->dpend = it->dpvec + ctl_len;
5199 it->current.dpvec_index = 0;
5200 it->dpvec_face_id = face_id;
5201 it->saved_face_id = it->face_id;
5202 it->method = GET_FROM_DISPLAY_VECTOR;
5203 it->ellipsis_p = 0;
5204 goto get_next;
5205 }
5206 }
5207
5208 /* Adjust face id for a multibyte character. There are no
5209 multibyte character in unibyte text. */
5210 if (it->multibyte_p
5211 && success_p
5212 && FRAME_WINDOW_P (it->f))
5213 {
5214 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5215 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5216 }
5217 }
5218
5219 /* Is this character the last one of a run of characters with
5220 box? If yes, set IT->end_of_box_run_p to 1. */
5221 if (it->face_box_p
5222 && it->s == NULL)
5223 {
5224 int face_id;
5225 struct face *face;
5226
5227 it->end_of_box_run_p
5228 = ((face_id = face_after_it_pos (it),
5229 face_id != it->face_id)
5230 && (face = FACE_FROM_ID (it->f, face_id),
5231 face->box == FACE_NO_BOX));
5232 }
5233
5234 /* Value is 0 if end of buffer or string reached. */
5235 return success_p;
5236 }
5237
5238
5239 /* Move IT to the next display element.
5240
5241 RESEAT_P non-zero means if called on a newline in buffer text,
5242 skip to the next visible line start.
5243
5244 Functions get_next_display_element and set_iterator_to_next are
5245 separate because I find this arrangement easier to handle than a
5246 get_next_display_element function that also increments IT's
5247 position. The way it is we can first look at an iterator's current
5248 display element, decide whether it fits on a line, and if it does,
5249 increment the iterator position. The other way around we probably
5250 would either need a flag indicating whether the iterator has to be
5251 incremented the next time, or we would have to implement a
5252 decrement position function which would not be easy to write. */
5253
5254 void
5255 set_iterator_to_next (it, reseat_p)
5256 struct it *it;
5257 int reseat_p;
5258 {
5259 /* Reset flags indicating start and end of a sequence of characters
5260 with box. Reset them at the start of this function because
5261 moving the iterator to a new position might set them. */
5262 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5263
5264 switch (it->method)
5265 {
5266 case GET_FROM_BUFFER:
5267 /* The current display element of IT is a character from
5268 current_buffer. Advance in the buffer, and maybe skip over
5269 invisible lines that are so because of selective display. */
5270 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5271 reseat_at_next_visible_line_start (it, 0);
5272 else
5273 {
5274 xassert (it->len != 0);
5275 IT_BYTEPOS (*it) += it->len;
5276 IT_CHARPOS (*it) += 1;
5277 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5278 }
5279 break;
5280
5281 case GET_FROM_COMPOSITION:
5282 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5283 if (STRINGP (it->string))
5284 {
5285 IT_STRING_BYTEPOS (*it) += it->len;
5286 IT_STRING_CHARPOS (*it) += it->cmp_len;
5287 it->method = GET_FROM_STRING;
5288 goto consider_string_end;
5289 }
5290 else
5291 {
5292 IT_BYTEPOS (*it) += it->len;
5293 IT_CHARPOS (*it) += it->cmp_len;
5294 it->method = GET_FROM_BUFFER;
5295 }
5296 break;
5297
5298 case GET_FROM_C_STRING:
5299 /* Current display element of IT is from a C string. */
5300 IT_BYTEPOS (*it) += it->len;
5301 IT_CHARPOS (*it) += 1;
5302 break;
5303
5304 case GET_FROM_DISPLAY_VECTOR:
5305 /* Current display element of IT is from a display table entry.
5306 Advance in the display table definition. Reset it to null if
5307 end reached, and continue with characters from buffers/
5308 strings. */
5309 ++it->current.dpvec_index;
5310
5311 /* Restore face of the iterator to what they were before the
5312 display vector entry (these entries may contain faces). */
5313 it->face_id = it->saved_face_id;
5314
5315 if (it->dpvec + it->current.dpvec_index == it->dpend)
5316 {
5317 if (it->s)
5318 it->method = GET_FROM_C_STRING;
5319 else if (STRINGP (it->string))
5320 it->method = GET_FROM_STRING;
5321 else
5322 it->method = GET_FROM_BUFFER;
5323
5324 it->dpvec = NULL;
5325 it->current.dpvec_index = -1;
5326
5327 /* Skip over characters which were displayed via IT->dpvec. */
5328 if (it->dpvec_char_len < 0)
5329 reseat_at_next_visible_line_start (it, 1);
5330 else if (it->dpvec_char_len > 0)
5331 {
5332 it->len = it->dpvec_char_len;
5333 set_iterator_to_next (it, reseat_p);
5334 }
5335
5336 /* Recheck faces after display vector */
5337 it->stop_charpos = IT_CHARPOS (*it);
5338 }
5339 break;
5340
5341 case GET_FROM_STRING:
5342 /* Current display element is a character from a Lisp string. */
5343 xassert (it->s == NULL && STRINGP (it->string));
5344 IT_STRING_BYTEPOS (*it) += it->len;
5345 IT_STRING_CHARPOS (*it) += 1;
5346
5347 consider_string_end:
5348
5349 if (it->current.overlay_string_index >= 0)
5350 {
5351 /* IT->string is an overlay string. Advance to the
5352 next, if there is one. */
5353 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5354 next_overlay_string (it);
5355 }
5356 else
5357 {
5358 /* IT->string is not an overlay string. If we reached
5359 its end, and there is something on IT->stack, proceed
5360 with what is on the stack. This can be either another
5361 string, this time an overlay string, or a buffer. */
5362 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5363 && it->sp > 0)
5364 {
5365 pop_it (it);
5366 if (STRINGP (it->string))
5367 goto consider_string_end;
5368 it->method = GET_FROM_BUFFER;
5369 }
5370 }
5371 break;
5372
5373 case GET_FROM_IMAGE:
5374 case GET_FROM_STRETCH:
5375 /* The position etc with which we have to proceed are on
5376 the stack. The position may be at the end of a string,
5377 if the `display' property takes up the whole string. */
5378 xassert (it->sp > 0);
5379 pop_it (it);
5380 it->image_id = 0;
5381 if (STRINGP (it->string))
5382 {
5383 it->method = GET_FROM_STRING;
5384 goto consider_string_end;
5385 }
5386 it->method = GET_FROM_BUFFER;
5387 break;
5388
5389 default:
5390 /* There are no other methods defined, so this should be a bug. */
5391 abort ();
5392 }
5393
5394 xassert (it->method != GET_FROM_STRING
5395 || (STRINGP (it->string)
5396 && IT_STRING_CHARPOS (*it) >= 0));
5397 }
5398
5399 /* Load IT's display element fields with information about the next
5400 display element which comes from a display table entry or from the
5401 result of translating a control character to one of the forms `^C'
5402 or `\003'.
5403
5404 IT->dpvec holds the glyphs to return as characters.
5405 IT->saved_face_id holds the face id before the display vector--
5406 it is restored into IT->face_idin set_iterator_to_next. */
5407
5408 static int
5409 next_element_from_display_vector (it)
5410 struct it *it;
5411 {
5412 /* Precondition. */
5413 xassert (it->dpvec && it->current.dpvec_index >= 0);
5414
5415 if (INTEGERP (*it->dpvec)
5416 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5417 {
5418 GLYPH g;
5419
5420 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5421 it->c = FAST_GLYPH_CHAR (g);
5422 it->len = CHAR_BYTES (it->c);
5423
5424 /* The entry may contain a face id to use. Such a face id is
5425 the id of a Lisp face, not a realized face. A face id of
5426 zero means no face is specified. */
5427 if (it->dpvec_face_id >= 0)
5428 it->face_id = it->dpvec_face_id;
5429 else
5430 {
5431 int lface_id = FAST_GLYPH_FACE (g);
5432 if (lface_id > 0)
5433 it->face_id = merge_faces (it->f, Qt, lface_id,
5434 it->saved_face_id);
5435 }
5436 }
5437 else
5438 /* Display table entry is invalid. Return a space. */
5439 it->c = ' ', it->len = 1;
5440
5441 /* Don't change position and object of the iterator here. They are
5442 still the values of the character that had this display table
5443 entry or was translated, and that's what we want. */
5444 it->what = IT_CHARACTER;
5445 return 1;
5446 }
5447
5448
5449 /* Load IT with the next display element from Lisp string IT->string.
5450 IT->current.string_pos is the current position within the string.
5451 If IT->current.overlay_string_index >= 0, the Lisp string is an
5452 overlay string. */
5453
5454 static int
5455 next_element_from_string (it)
5456 struct it *it;
5457 {
5458 struct text_pos position;
5459
5460 xassert (STRINGP (it->string));
5461 xassert (IT_STRING_CHARPOS (*it) >= 0);
5462 position = it->current.string_pos;
5463
5464 /* Time to check for invisible text? */
5465 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5466 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5467 {
5468 handle_stop (it);
5469
5470 /* Since a handler may have changed IT->method, we must
5471 recurse here. */
5472 return get_next_display_element (it);
5473 }
5474
5475 if (it->current.overlay_string_index >= 0)
5476 {
5477 /* Get the next character from an overlay string. In overlay
5478 strings, There is no field width or padding with spaces to
5479 do. */
5480 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5481 {
5482 it->what = IT_EOB;
5483 return 0;
5484 }
5485 else if (STRING_MULTIBYTE (it->string))
5486 {
5487 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5488 const unsigned char *s = (SDATA (it->string)
5489 + IT_STRING_BYTEPOS (*it));
5490 it->c = string_char_and_length (s, remaining, &it->len);
5491 }
5492 else
5493 {
5494 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5495 it->len = 1;
5496 }
5497 }
5498 else
5499 {
5500 /* Get the next character from a Lisp string that is not an
5501 overlay string. Such strings come from the mode line, for
5502 example. We may have to pad with spaces, or truncate the
5503 string. See also next_element_from_c_string. */
5504 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5505 {
5506 it->what = IT_EOB;
5507 return 0;
5508 }
5509 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5510 {
5511 /* Pad with spaces. */
5512 it->c = ' ', it->len = 1;
5513 CHARPOS (position) = BYTEPOS (position) = -1;
5514 }
5515 else if (STRING_MULTIBYTE (it->string))
5516 {
5517 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5518 const unsigned char *s = (SDATA (it->string)
5519 + IT_STRING_BYTEPOS (*it));
5520 it->c = string_char_and_length (s, maxlen, &it->len);
5521 }
5522 else
5523 {
5524 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5525 it->len = 1;
5526 }
5527 }
5528
5529 /* Record what we have and where it came from. Note that we store a
5530 buffer position in IT->position although it could arguably be a
5531 string position. */
5532 it->what = IT_CHARACTER;
5533 it->object = it->string;
5534 it->position = position;
5535 return 1;
5536 }
5537
5538
5539 /* Load IT with next display element from C string IT->s.
5540 IT->string_nchars is the maximum number of characters to return
5541 from the string. IT->end_charpos may be greater than
5542 IT->string_nchars when this function is called, in which case we
5543 may have to return padding spaces. Value is zero if end of string
5544 reached, including padding spaces. */
5545
5546 static int
5547 next_element_from_c_string (it)
5548 struct it *it;
5549 {
5550 int success_p = 1;
5551
5552 xassert (it->s);
5553 it->what = IT_CHARACTER;
5554 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5555 it->object = Qnil;
5556
5557 /* IT's position can be greater IT->string_nchars in case a field
5558 width or precision has been specified when the iterator was
5559 initialized. */
5560 if (IT_CHARPOS (*it) >= it->end_charpos)
5561 {
5562 /* End of the game. */
5563 it->what = IT_EOB;
5564 success_p = 0;
5565 }
5566 else if (IT_CHARPOS (*it) >= it->string_nchars)
5567 {
5568 /* Pad with spaces. */
5569 it->c = ' ', it->len = 1;
5570 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5571 }
5572 else if (it->multibyte_p)
5573 {
5574 /* Implementation note: The calls to strlen apparently aren't a
5575 performance problem because there is no noticeable performance
5576 difference between Emacs running in unibyte or multibyte mode. */
5577 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5578 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5579 maxlen, &it->len);
5580 }
5581 else
5582 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5583
5584 return success_p;
5585 }
5586
5587
5588 /* Set up IT to return characters from an ellipsis, if appropriate.
5589 The definition of the ellipsis glyphs may come from a display table
5590 entry. This function Fills IT with the first glyph from the
5591 ellipsis if an ellipsis is to be displayed. */
5592
5593 static int
5594 next_element_from_ellipsis (it)
5595 struct it *it;
5596 {
5597 if (it->selective_display_ellipsis_p)
5598 setup_for_ellipsis (it, it->len);
5599 else
5600 {
5601 /* The face at the current position may be different from the
5602 face we find after the invisible text. Remember what it
5603 was in IT->saved_face_id, and signal that it's there by
5604 setting face_before_selective_p. */
5605 it->saved_face_id = it->face_id;
5606 it->method = GET_FROM_BUFFER;
5607 reseat_at_next_visible_line_start (it, 1);
5608 it->face_before_selective_p = 1;
5609 }
5610
5611 return get_next_display_element (it);
5612 }
5613
5614
5615 /* Deliver an image display element. The iterator IT is already
5616 filled with image information (done in handle_display_prop). Value
5617 is always 1. */
5618
5619
5620 static int
5621 next_element_from_image (it)
5622 struct it *it;
5623 {
5624 it->what = IT_IMAGE;
5625 return 1;
5626 }
5627
5628
5629 /* Fill iterator IT with next display element from a stretch glyph
5630 property. IT->object is the value of the text property. Value is
5631 always 1. */
5632
5633 static int
5634 next_element_from_stretch (it)
5635 struct it *it;
5636 {
5637 it->what = IT_STRETCH;
5638 return 1;
5639 }
5640
5641
5642 /* Load IT with the next display element from current_buffer. Value
5643 is zero if end of buffer reached. IT->stop_charpos is the next
5644 position at which to stop and check for text properties or buffer
5645 end. */
5646
5647 static int
5648 next_element_from_buffer (it)
5649 struct it *it;
5650 {
5651 int success_p = 1;
5652
5653 /* Check this assumption, otherwise, we would never enter the
5654 if-statement, below. */
5655 xassert (IT_CHARPOS (*it) >= BEGV
5656 && IT_CHARPOS (*it) <= it->stop_charpos);
5657
5658 if (IT_CHARPOS (*it) >= it->stop_charpos)
5659 {
5660 if (IT_CHARPOS (*it) >= it->end_charpos)
5661 {
5662 int overlay_strings_follow_p;
5663
5664 /* End of the game, except when overlay strings follow that
5665 haven't been returned yet. */
5666 if (it->overlay_strings_at_end_processed_p)
5667 overlay_strings_follow_p = 0;
5668 else
5669 {
5670 it->overlay_strings_at_end_processed_p = 1;
5671 overlay_strings_follow_p = get_overlay_strings (it, 0);
5672 }
5673
5674 if (overlay_strings_follow_p)
5675 success_p = get_next_display_element (it);
5676 else
5677 {
5678 it->what = IT_EOB;
5679 it->position = it->current.pos;
5680 success_p = 0;
5681 }
5682 }
5683 else
5684 {
5685 handle_stop (it);
5686 return get_next_display_element (it);
5687 }
5688 }
5689 else
5690 {
5691 /* No face changes, overlays etc. in sight, so just return a
5692 character from current_buffer. */
5693 unsigned char *p;
5694
5695 /* Maybe run the redisplay end trigger hook. Performance note:
5696 This doesn't seem to cost measurable time. */
5697 if (it->redisplay_end_trigger_charpos
5698 && it->glyph_row
5699 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5700 run_redisplay_end_trigger_hook (it);
5701
5702 /* Get the next character, maybe multibyte. */
5703 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5704 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5705 {
5706 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5707 - IT_BYTEPOS (*it));
5708 it->c = string_char_and_length (p, maxlen, &it->len);
5709 }
5710 else
5711 it->c = *p, it->len = 1;
5712
5713 /* Record what we have and where it came from. */
5714 it->what = IT_CHARACTER;;
5715 it->object = it->w->buffer;
5716 it->position = it->current.pos;
5717
5718 /* Normally we return the character found above, except when we
5719 really want to return an ellipsis for selective display. */
5720 if (it->selective)
5721 {
5722 if (it->c == '\n')
5723 {
5724 /* A value of selective > 0 means hide lines indented more
5725 than that number of columns. */
5726 if (it->selective > 0
5727 && IT_CHARPOS (*it) + 1 < ZV
5728 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5729 IT_BYTEPOS (*it) + 1,
5730 (double) it->selective)) /* iftc */
5731 {
5732 success_p = next_element_from_ellipsis (it);
5733 it->dpvec_char_len = -1;
5734 }
5735 }
5736 else if (it->c == '\r' && it->selective == -1)
5737 {
5738 /* A value of selective == -1 means that everything from the
5739 CR to the end of the line is invisible, with maybe an
5740 ellipsis displayed for it. */
5741 success_p = next_element_from_ellipsis (it);
5742 it->dpvec_char_len = -1;
5743 }
5744 }
5745 }
5746
5747 /* Value is zero if end of buffer reached. */
5748 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5749 return success_p;
5750 }
5751
5752
5753 /* Run the redisplay end trigger hook for IT. */
5754
5755 static void
5756 run_redisplay_end_trigger_hook (it)
5757 struct it *it;
5758 {
5759 Lisp_Object args[3];
5760
5761 /* IT->glyph_row should be non-null, i.e. we should be actually
5762 displaying something, or otherwise we should not run the hook. */
5763 xassert (it->glyph_row);
5764
5765 /* Set up hook arguments. */
5766 args[0] = Qredisplay_end_trigger_functions;
5767 args[1] = it->window;
5768 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5769 it->redisplay_end_trigger_charpos = 0;
5770
5771 /* Since we are *trying* to run these functions, don't try to run
5772 them again, even if they get an error. */
5773 it->w->redisplay_end_trigger = Qnil;
5774 Frun_hook_with_args (3, args);
5775
5776 /* Notice if it changed the face of the character we are on. */
5777 handle_face_prop (it);
5778 }
5779
5780
5781 /* Deliver a composition display element. The iterator IT is already
5782 filled with composition information (done in
5783 handle_composition_prop). Value is always 1. */
5784
5785 static int
5786 next_element_from_composition (it)
5787 struct it *it;
5788 {
5789 it->what = IT_COMPOSITION;
5790 it->position = (STRINGP (it->string)
5791 ? it->current.string_pos
5792 : it->current.pos);
5793 return 1;
5794 }
5795
5796
5797 \f
5798 /***********************************************************************
5799 Moving an iterator without producing glyphs
5800 ***********************************************************************/
5801
5802 /* Move iterator IT to a specified buffer or X position within one
5803 line on the display without producing glyphs.
5804
5805 OP should be a bit mask including some or all of these bits:
5806 MOVE_TO_X: Stop on reaching x-position TO_X.
5807 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5808 Regardless of OP's value, stop in reaching the end of the display line.
5809
5810 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5811 This means, in particular, that TO_X includes window's horizontal
5812 scroll amount.
5813
5814 The return value has several possible values that
5815 say what condition caused the scan to stop:
5816
5817 MOVE_POS_MATCH_OR_ZV
5818 - when TO_POS or ZV was reached.
5819
5820 MOVE_X_REACHED
5821 -when TO_X was reached before TO_POS or ZV were reached.
5822
5823 MOVE_LINE_CONTINUED
5824 - when we reached the end of the display area and the line must
5825 be continued.
5826
5827 MOVE_LINE_TRUNCATED
5828 - when we reached the end of the display area and the line is
5829 truncated.
5830
5831 MOVE_NEWLINE_OR_CR
5832 - when we stopped at a line end, i.e. a newline or a CR and selective
5833 display is on. */
5834
5835 static enum move_it_result
5836 move_it_in_display_line_to (it, to_charpos, to_x, op)
5837 struct it *it;
5838 int to_charpos, to_x, op;
5839 {
5840 enum move_it_result result = MOVE_UNDEFINED;
5841 struct glyph_row *saved_glyph_row;
5842
5843 /* Don't produce glyphs in produce_glyphs. */
5844 saved_glyph_row = it->glyph_row;
5845 it->glyph_row = NULL;
5846
5847 #define BUFFER_POS_REACHED_P() \
5848 ((op & MOVE_TO_POS) != 0 \
5849 && BUFFERP (it->object) \
5850 && IT_CHARPOS (*it) >= to_charpos \
5851 && (it->method == GET_FROM_BUFFER \
5852 || (it->method == GET_FROM_DISPLAY_VECTOR \
5853 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5854
5855
5856 while (1)
5857 {
5858 int x, i, ascent = 0, descent = 0;
5859
5860 /* Stop when ZV reached.
5861 We used to stop here when TO_CHARPOS reached as well, but that is
5862 too soon if this glyph does not fit on this line. So we handle it
5863 explicitly below. */
5864 if (!get_next_display_element (it)
5865 || (it->truncate_lines_p
5866 && BUFFER_POS_REACHED_P ()))
5867 {
5868 result = MOVE_POS_MATCH_OR_ZV;
5869 break;
5870 }
5871
5872 /* The call to produce_glyphs will get the metrics of the
5873 display element IT is loaded with. We record in x the
5874 x-position before this display element in case it does not
5875 fit on the line. */
5876 x = it->current_x;
5877
5878 /* Remember the line height so far in case the next element doesn't
5879 fit on the line. */
5880 if (!it->truncate_lines_p)
5881 {
5882 ascent = it->max_ascent;
5883 descent = it->max_descent;
5884 }
5885
5886 PRODUCE_GLYPHS (it);
5887
5888 if (it->area != TEXT_AREA)
5889 {
5890 set_iterator_to_next (it, 1);
5891 continue;
5892 }
5893
5894 /* The number of glyphs we get back in IT->nglyphs will normally
5895 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5896 character on a terminal frame, or (iii) a line end. For the
5897 second case, IT->nglyphs - 1 padding glyphs will be present
5898 (on X frames, there is only one glyph produced for a
5899 composite character.
5900
5901 The behavior implemented below means, for continuation lines,
5902 that as many spaces of a TAB as fit on the current line are
5903 displayed there. For terminal frames, as many glyphs of a
5904 multi-glyph character are displayed in the current line, too.
5905 This is what the old redisplay code did, and we keep it that
5906 way. Under X, the whole shape of a complex character must
5907 fit on the line or it will be completely displayed in the
5908 next line.
5909
5910 Note that both for tabs and padding glyphs, all glyphs have
5911 the same width. */
5912 if (it->nglyphs)
5913 {
5914 /* More than one glyph or glyph doesn't fit on line. All
5915 glyphs have the same width. */
5916 int single_glyph_width = it->pixel_width / it->nglyphs;
5917 int new_x;
5918
5919 for (i = 0; i < it->nglyphs; ++i, x = new_x)
5920 {
5921 new_x = x + single_glyph_width;
5922
5923 /* We want to leave anything reaching TO_X to the caller. */
5924 if ((op & MOVE_TO_X) && new_x > to_x)
5925 {
5926 if (BUFFER_POS_REACHED_P ())
5927 goto buffer_pos_reached;
5928 it->current_x = x;
5929 result = MOVE_X_REACHED;
5930 break;
5931 }
5932 else if (/* Lines are continued. */
5933 !it->truncate_lines_p
5934 && (/* And glyph doesn't fit on the line. */
5935 new_x > it->last_visible_x
5936 /* Or it fits exactly and we're on a window
5937 system frame. */
5938 || (new_x == it->last_visible_x
5939 && FRAME_WINDOW_P (it->f))))
5940 {
5941 if (/* IT->hpos == 0 means the very first glyph
5942 doesn't fit on the line, e.g. a wide image. */
5943 it->hpos == 0
5944 || (new_x == it->last_visible_x
5945 && FRAME_WINDOW_P (it->f)))
5946 {
5947 ++it->hpos;
5948 it->current_x = new_x;
5949 if (i == it->nglyphs - 1)
5950 {
5951 set_iterator_to_next (it, 1);
5952 #ifdef HAVE_WINDOW_SYSTEM
5953 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
5954 {
5955 if (!get_next_display_element (it))
5956 {
5957 result = MOVE_POS_MATCH_OR_ZV;
5958 break;
5959 }
5960 if (BUFFER_POS_REACHED_P ())
5961 {
5962 if (ITERATOR_AT_END_OF_LINE_P (it))
5963 result = MOVE_POS_MATCH_OR_ZV;
5964 else
5965 result = MOVE_LINE_CONTINUED;
5966 break;
5967 }
5968 if (ITERATOR_AT_END_OF_LINE_P (it))
5969 {
5970 result = MOVE_NEWLINE_OR_CR;
5971 break;
5972 }
5973 }
5974 #endif /* HAVE_WINDOW_SYSTEM */
5975 }
5976 }
5977 else
5978 {
5979 it->current_x = x;
5980 it->max_ascent = ascent;
5981 it->max_descent = descent;
5982 }
5983
5984 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
5985 IT_CHARPOS (*it)));
5986 result = MOVE_LINE_CONTINUED;
5987 break;
5988 }
5989 else if (BUFFER_POS_REACHED_P ())
5990 goto buffer_pos_reached;
5991 else if (new_x > it->first_visible_x)
5992 {
5993 /* Glyph is visible. Increment number of glyphs that
5994 would be displayed. */
5995 ++it->hpos;
5996 }
5997 else
5998 {
5999 /* Glyph is completely off the left margin of the display
6000 area. Nothing to do. */
6001 }
6002 }
6003
6004 if (result != MOVE_UNDEFINED)
6005 break;
6006 }
6007 else if (BUFFER_POS_REACHED_P ())
6008 {
6009 buffer_pos_reached:
6010 it->current_x = x;
6011 it->max_ascent = ascent;
6012 it->max_descent = descent;
6013 result = MOVE_POS_MATCH_OR_ZV;
6014 break;
6015 }
6016 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6017 {
6018 /* Stop when TO_X specified and reached. This check is
6019 necessary here because of lines consisting of a line end,
6020 only. The line end will not produce any glyphs and we
6021 would never get MOVE_X_REACHED. */
6022 xassert (it->nglyphs == 0);
6023 result = MOVE_X_REACHED;
6024 break;
6025 }
6026
6027 /* Is this a line end? If yes, we're done. */
6028 if (ITERATOR_AT_END_OF_LINE_P (it))
6029 {
6030 result = MOVE_NEWLINE_OR_CR;
6031 break;
6032 }
6033
6034 /* The current display element has been consumed. Advance
6035 to the next. */
6036 set_iterator_to_next (it, 1);
6037
6038 /* Stop if lines are truncated and IT's current x-position is
6039 past the right edge of the window now. */
6040 if (it->truncate_lines_p
6041 && it->current_x >= it->last_visible_x)
6042 {
6043 #ifdef HAVE_WINDOW_SYSTEM
6044 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6045 {
6046 if (!get_next_display_element (it)
6047 || BUFFER_POS_REACHED_P ())
6048 {
6049 result = MOVE_POS_MATCH_OR_ZV;
6050 break;
6051 }
6052 if (ITERATOR_AT_END_OF_LINE_P (it))
6053 {
6054 result = MOVE_NEWLINE_OR_CR;
6055 break;
6056 }
6057 }
6058 #endif /* HAVE_WINDOW_SYSTEM */
6059 result = MOVE_LINE_TRUNCATED;
6060 break;
6061 }
6062 }
6063
6064 #undef BUFFER_POS_REACHED_P
6065
6066 /* Restore the iterator settings altered at the beginning of this
6067 function. */
6068 it->glyph_row = saved_glyph_row;
6069 return result;
6070 }
6071
6072
6073 /* Move IT forward until it satisfies one or more of the criteria in
6074 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6075
6076 OP is a bit-mask that specifies where to stop, and in particular,
6077 which of those four position arguments makes a difference. See the
6078 description of enum move_operation_enum.
6079
6080 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6081 screen line, this function will set IT to the next position >
6082 TO_CHARPOS. */
6083
6084 void
6085 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6086 struct it *it;
6087 int to_charpos, to_x, to_y, to_vpos;
6088 int op;
6089 {
6090 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6091 int line_height;
6092 int reached = 0;
6093
6094 for (;;)
6095 {
6096 if (op & MOVE_TO_VPOS)
6097 {
6098 /* If no TO_CHARPOS and no TO_X specified, stop at the
6099 start of the line TO_VPOS. */
6100 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6101 {
6102 if (it->vpos == to_vpos)
6103 {
6104 reached = 1;
6105 break;
6106 }
6107 else
6108 skip = move_it_in_display_line_to (it, -1, -1, 0);
6109 }
6110 else
6111 {
6112 /* TO_VPOS >= 0 means stop at TO_X in the line at
6113 TO_VPOS, or at TO_POS, whichever comes first. */
6114 if (it->vpos == to_vpos)
6115 {
6116 reached = 2;
6117 break;
6118 }
6119
6120 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6121
6122 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6123 {
6124 reached = 3;
6125 break;
6126 }
6127 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6128 {
6129 /* We have reached TO_X but not in the line we want. */
6130 skip = move_it_in_display_line_to (it, to_charpos,
6131 -1, MOVE_TO_POS);
6132 if (skip == MOVE_POS_MATCH_OR_ZV)
6133 {
6134 reached = 4;
6135 break;
6136 }
6137 }
6138 }
6139 }
6140 else if (op & MOVE_TO_Y)
6141 {
6142 struct it it_backup;
6143
6144 /* TO_Y specified means stop at TO_X in the line containing
6145 TO_Y---or at TO_CHARPOS if this is reached first. The
6146 problem is that we can't really tell whether the line
6147 contains TO_Y before we have completely scanned it, and
6148 this may skip past TO_X. What we do is to first scan to
6149 TO_X.
6150
6151 If TO_X is not specified, use a TO_X of zero. The reason
6152 is to make the outcome of this function more predictable.
6153 If we didn't use TO_X == 0, we would stop at the end of
6154 the line which is probably not what a caller would expect
6155 to happen. */
6156 skip = move_it_in_display_line_to (it, to_charpos,
6157 ((op & MOVE_TO_X)
6158 ? to_x : 0),
6159 (MOVE_TO_X
6160 | (op & MOVE_TO_POS)));
6161
6162 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6163 if (skip == MOVE_POS_MATCH_OR_ZV)
6164 {
6165 reached = 5;
6166 break;
6167 }
6168
6169 /* If TO_X was reached, we would like to know whether TO_Y
6170 is in the line. This can only be said if we know the
6171 total line height which requires us to scan the rest of
6172 the line. */
6173 if (skip == MOVE_X_REACHED)
6174 {
6175 it_backup = *it;
6176 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6177 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6178 op & MOVE_TO_POS);
6179 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6180 }
6181
6182 /* Now, decide whether TO_Y is in this line. */
6183 line_height = it->max_ascent + it->max_descent;
6184 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6185
6186 if (to_y >= it->current_y
6187 && to_y < it->current_y + line_height)
6188 {
6189 if (skip == MOVE_X_REACHED)
6190 /* If TO_Y is in this line and TO_X was reached above,
6191 we scanned too far. We have to restore IT's settings
6192 to the ones before skipping. */
6193 *it = it_backup;
6194 reached = 6;
6195 }
6196 else if (skip == MOVE_X_REACHED)
6197 {
6198 skip = skip2;
6199 if (skip == MOVE_POS_MATCH_OR_ZV)
6200 reached = 7;
6201 }
6202
6203 if (reached)
6204 break;
6205 }
6206 else
6207 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6208
6209 switch (skip)
6210 {
6211 case MOVE_POS_MATCH_OR_ZV:
6212 reached = 8;
6213 goto out;
6214
6215 case MOVE_NEWLINE_OR_CR:
6216 set_iterator_to_next (it, 1);
6217 it->continuation_lines_width = 0;
6218 break;
6219
6220 case MOVE_LINE_TRUNCATED:
6221 it->continuation_lines_width = 0;
6222 reseat_at_next_visible_line_start (it, 0);
6223 if ((op & MOVE_TO_POS) != 0
6224 && IT_CHARPOS (*it) > to_charpos)
6225 {
6226 reached = 9;
6227 goto out;
6228 }
6229 break;
6230
6231 case MOVE_LINE_CONTINUED:
6232 it->continuation_lines_width += it->current_x;
6233 break;
6234
6235 default:
6236 abort ();
6237 }
6238
6239 /* Reset/increment for the next run. */
6240 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6241 it->current_x = it->hpos = 0;
6242 it->current_y += it->max_ascent + it->max_descent;
6243 ++it->vpos;
6244 last_height = it->max_ascent + it->max_descent;
6245 last_max_ascent = it->max_ascent;
6246 it->max_ascent = it->max_descent = 0;
6247 }
6248
6249 out:
6250
6251 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6252 }
6253
6254
6255 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6256
6257 If DY > 0, move IT backward at least that many pixels. DY = 0
6258 means move IT backward to the preceding line start or BEGV. This
6259 function may move over more than DY pixels if IT->current_y - DY
6260 ends up in the middle of a line; in this case IT->current_y will be
6261 set to the top of the line moved to. */
6262
6263 void
6264 move_it_vertically_backward (it, dy)
6265 struct it *it;
6266 int dy;
6267 {
6268 int nlines, h;
6269 struct it it2, it3;
6270 int start_pos;
6271
6272 move_further_back:
6273 xassert (dy >= 0);
6274
6275 start_pos = IT_CHARPOS (*it);
6276
6277 /* Estimate how many newlines we must move back. */
6278 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6279
6280 /* Set the iterator's position that many lines back. */
6281 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6282 back_to_previous_visible_line_start (it);
6283
6284 /* Reseat the iterator here. When moving backward, we don't want
6285 reseat to skip forward over invisible text, set up the iterator
6286 to deliver from overlay strings at the new position etc. So,
6287 use reseat_1 here. */
6288 reseat_1 (it, it->current.pos, 1);
6289
6290 /* We are now surely at a line start. */
6291 it->current_x = it->hpos = 0;
6292 it->continuation_lines_width = 0;
6293
6294 /* Move forward and see what y-distance we moved. First move to the
6295 start of the next line so that we get its height. We need this
6296 height to be able to tell whether we reached the specified
6297 y-distance. */
6298 it2 = *it;
6299 it2.max_ascent = it2.max_descent = 0;
6300 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6301 MOVE_TO_POS | MOVE_TO_VPOS);
6302 xassert (IT_CHARPOS (*it) >= BEGV);
6303 it3 = it2;
6304
6305 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6306 xassert (IT_CHARPOS (*it) >= BEGV);
6307 /* H is the actual vertical distance from the position in *IT
6308 and the starting position. */
6309 h = it2.current_y - it->current_y;
6310 /* NLINES is the distance in number of lines. */
6311 nlines = it2.vpos - it->vpos;
6312
6313 /* Correct IT's y and vpos position
6314 so that they are relative to the starting point. */
6315 it->vpos -= nlines;
6316 it->current_y -= h;
6317
6318 if (dy == 0)
6319 {
6320 /* DY == 0 means move to the start of the screen line. The
6321 value of nlines is > 0 if continuation lines were involved. */
6322 if (nlines > 0)
6323 move_it_by_lines (it, nlines, 1);
6324 #if 0
6325 /* I think this assert is bogus if buffer contains
6326 invisible text or images. KFS. */
6327 xassert (IT_CHARPOS (*it) <= start_pos);
6328 #endif
6329 }
6330 else
6331 {
6332 /* The y-position we try to reach, relative to *IT.
6333 Note that H has been subtracted in front of the if-statement. */
6334 int target_y = it->current_y + h - dy;
6335 int y0 = it3.current_y;
6336 int y1 = line_bottom_y (&it3);
6337 int line_height = y1 - y0;
6338
6339 /* If we did not reach target_y, try to move further backward if
6340 we can. If we moved too far backward, try to move forward. */
6341 if (target_y < it->current_y
6342 /* This is heuristic. In a window that's 3 lines high, with
6343 a line height of 13 pixels each, recentering with point
6344 on the bottom line will try to move -39/2 = 19 pixels
6345 backward. Try to avoid moving into the first line. */
6346 && (it->current_y - target_y
6347 > min (window_box_height (it->w), line_height * 2 / 3))
6348 && IT_CHARPOS (*it) > BEGV)
6349 {
6350 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6351 target_y - it->current_y));
6352 dy = it->current_y - target_y;
6353 goto move_further_back;
6354 }
6355 else if (target_y >= it->current_y + line_height
6356 && IT_CHARPOS (*it) < ZV)
6357 {
6358 /* Should move forward by at least one line, maybe more.
6359
6360 Note: Calling move_it_by_lines can be expensive on
6361 terminal frames, where compute_motion is used (via
6362 vmotion) to do the job, when there are very long lines
6363 and truncate-lines is nil. That's the reason for
6364 treating terminal frames specially here. */
6365
6366 if (!FRAME_WINDOW_P (it->f))
6367 move_it_vertically (it, target_y - (it->current_y + line_height));
6368 else
6369 {
6370 do
6371 {
6372 move_it_by_lines (it, 1, 1);
6373 }
6374 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6375 }
6376
6377 #if 0
6378 /* I think this assert is bogus if buffer contains
6379 invisible text or images. KFS. */
6380 xassert (IT_CHARPOS (*it) >= BEGV);
6381 #endif
6382 }
6383 }
6384 }
6385
6386
6387 /* Move IT by a specified amount of pixel lines DY. DY negative means
6388 move backwards. DY = 0 means move to start of screen line. At the
6389 end, IT will be on the start of a screen line. */
6390
6391 void
6392 move_it_vertically (it, dy)
6393 struct it *it;
6394 int dy;
6395 {
6396 if (dy <= 0)
6397 move_it_vertically_backward (it, -dy);
6398 else
6399 {
6400 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6401 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6402 MOVE_TO_POS | MOVE_TO_Y);
6403 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6404
6405 /* If buffer ends in ZV without a newline, move to the start of
6406 the line to satisfy the post-condition. */
6407 if (IT_CHARPOS (*it) == ZV
6408 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6409 move_it_by_lines (it, 0, 0);
6410 }
6411 }
6412
6413
6414 /* Move iterator IT past the end of the text line it is in. */
6415
6416 void
6417 move_it_past_eol (it)
6418 struct it *it;
6419 {
6420 enum move_it_result rc;
6421
6422 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6423 if (rc == MOVE_NEWLINE_OR_CR)
6424 set_iterator_to_next (it, 0);
6425 }
6426
6427
6428 #if 0 /* Currently not used. */
6429
6430 /* Return non-zero if some text between buffer positions START_CHARPOS
6431 and END_CHARPOS is invisible. IT->window is the window for text
6432 property lookup. */
6433
6434 static int
6435 invisible_text_between_p (it, start_charpos, end_charpos)
6436 struct it *it;
6437 int start_charpos, end_charpos;
6438 {
6439 Lisp_Object prop, limit;
6440 int invisible_found_p;
6441
6442 xassert (it != NULL && start_charpos <= end_charpos);
6443
6444 /* Is text at START invisible? */
6445 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6446 it->window);
6447 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6448 invisible_found_p = 1;
6449 else
6450 {
6451 limit = Fnext_single_char_property_change (make_number (start_charpos),
6452 Qinvisible, Qnil,
6453 make_number (end_charpos));
6454 invisible_found_p = XFASTINT (limit) < end_charpos;
6455 }
6456
6457 return invisible_found_p;
6458 }
6459
6460 #endif /* 0 */
6461
6462
6463 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6464 negative means move up. DVPOS == 0 means move to the start of the
6465 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6466 NEED_Y_P is zero, IT->current_y will be left unchanged.
6467
6468 Further optimization ideas: If we would know that IT->f doesn't use
6469 a face with proportional font, we could be faster for
6470 truncate-lines nil. */
6471
6472 void
6473 move_it_by_lines (it, dvpos, need_y_p)
6474 struct it *it;
6475 int dvpos, need_y_p;
6476 {
6477 struct position pos;
6478
6479 if (!FRAME_WINDOW_P (it->f))
6480 {
6481 struct text_pos textpos;
6482
6483 /* We can use vmotion on frames without proportional fonts. */
6484 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6485 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6486 reseat (it, textpos, 1);
6487 it->vpos += pos.vpos;
6488 it->current_y += pos.vpos;
6489 }
6490 else if (dvpos == 0)
6491 {
6492 /* DVPOS == 0 means move to the start of the screen line. */
6493 move_it_vertically_backward (it, 0);
6494 xassert (it->current_x == 0 && it->hpos == 0);
6495 /* Let next call to line_bottom_y calculate real line height */
6496 last_height = 0;
6497 }
6498 else if (dvpos > 0)
6499 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6500 else
6501 {
6502 struct it it2;
6503 int start_charpos, i;
6504
6505 /* Start at the beginning of the screen line containing IT's
6506 position. */
6507 move_it_vertically_backward (it, 0);
6508
6509 /* Go back -DVPOS visible lines and reseat the iterator there. */
6510 start_charpos = IT_CHARPOS (*it);
6511 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6512 back_to_previous_visible_line_start (it);
6513 reseat (it, it->current.pos, 1);
6514 it->current_x = it->hpos = 0;
6515
6516 /* Above call may have moved too far if continuation lines
6517 are involved. Scan forward and see if it did. */
6518 it2 = *it;
6519 it2.vpos = it2.current_y = 0;
6520 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6521 it->vpos -= it2.vpos;
6522 it->current_y -= it2.current_y;
6523 it->current_x = it->hpos = 0;
6524
6525 /* If we moved too far back, move IT some lines forward. */
6526 if (it2.vpos > -dvpos)
6527 {
6528 int delta = it2.vpos + dvpos;
6529 it2 = *it;
6530 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6531 /* Move back again if we got too far ahead. */
6532 if (IT_CHARPOS (*it) >= start_charpos)
6533 *it = it2;
6534 }
6535 }
6536 }
6537
6538 /* Return 1 if IT points into the middle of a display vector. */
6539
6540 int
6541 in_display_vector_p (it)
6542 struct it *it;
6543 {
6544 return (it->method == GET_FROM_DISPLAY_VECTOR
6545 && it->current.dpvec_index > 0
6546 && it->dpvec + it->current.dpvec_index != it->dpend);
6547 }
6548
6549 \f
6550 /***********************************************************************
6551 Messages
6552 ***********************************************************************/
6553
6554
6555 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6556 to *Messages*. */
6557
6558 void
6559 add_to_log (format, arg1, arg2)
6560 char *format;
6561 Lisp_Object arg1, arg2;
6562 {
6563 Lisp_Object args[3];
6564 Lisp_Object msg, fmt;
6565 char *buffer;
6566 int len;
6567 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6568 USE_SAFE_ALLOCA;
6569
6570 /* Do nothing if called asynchronously. Inserting text into
6571 a buffer may call after-change-functions and alike and
6572 that would means running Lisp asynchronously. */
6573 if (handling_signal)
6574 return;
6575
6576 fmt = msg = Qnil;
6577 GCPRO4 (fmt, msg, arg1, arg2);
6578
6579 args[0] = fmt = build_string (format);
6580 args[1] = arg1;
6581 args[2] = arg2;
6582 msg = Fformat (3, args);
6583
6584 len = SBYTES (msg) + 1;
6585 SAFE_ALLOCA (buffer, char *, len);
6586 bcopy (SDATA (msg), buffer, len);
6587
6588 message_dolog (buffer, len - 1, 1, 0);
6589 SAFE_FREE ();
6590
6591 UNGCPRO;
6592 }
6593
6594
6595 /* Output a newline in the *Messages* buffer if "needs" one. */
6596
6597 void
6598 message_log_maybe_newline ()
6599 {
6600 if (message_log_need_newline)
6601 message_dolog ("", 0, 1, 0);
6602 }
6603
6604
6605 /* Add a string M of length NBYTES to the message log, optionally
6606 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6607 nonzero, means interpret the contents of M as multibyte. This
6608 function calls low-level routines in order to bypass text property
6609 hooks, etc. which might not be safe to run. */
6610
6611 void
6612 message_dolog (m, nbytes, nlflag, multibyte)
6613 const char *m;
6614 int nbytes, nlflag, multibyte;
6615 {
6616 if (!NILP (Vmemory_full))
6617 return;
6618
6619 if (!NILP (Vmessage_log_max))
6620 {
6621 struct buffer *oldbuf;
6622 Lisp_Object oldpoint, oldbegv, oldzv;
6623 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6624 int point_at_end = 0;
6625 int zv_at_end = 0;
6626 Lisp_Object old_deactivate_mark, tem;
6627 struct gcpro gcpro1;
6628
6629 old_deactivate_mark = Vdeactivate_mark;
6630 oldbuf = current_buffer;
6631 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6632 current_buffer->undo_list = Qt;
6633
6634 oldpoint = message_dolog_marker1;
6635 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6636 oldbegv = message_dolog_marker2;
6637 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6638 oldzv = message_dolog_marker3;
6639 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6640 GCPRO1 (old_deactivate_mark);
6641
6642 if (PT == Z)
6643 point_at_end = 1;
6644 if (ZV == Z)
6645 zv_at_end = 1;
6646
6647 BEGV = BEG;
6648 BEGV_BYTE = BEG_BYTE;
6649 ZV = Z;
6650 ZV_BYTE = Z_BYTE;
6651 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6652
6653 /* Insert the string--maybe converting multibyte to single byte
6654 or vice versa, so that all the text fits the buffer. */
6655 if (multibyte
6656 && NILP (current_buffer->enable_multibyte_characters))
6657 {
6658 int i, c, char_bytes;
6659 unsigned char work[1];
6660
6661 /* Convert a multibyte string to single-byte
6662 for the *Message* buffer. */
6663 for (i = 0; i < nbytes; i += char_bytes)
6664 {
6665 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6666 work[0] = (SINGLE_BYTE_CHAR_P (c)
6667 ? c
6668 : multibyte_char_to_unibyte (c, Qnil));
6669 insert_1_both (work, 1, 1, 1, 0, 0);
6670 }
6671 }
6672 else if (! multibyte
6673 && ! NILP (current_buffer->enable_multibyte_characters))
6674 {
6675 int i, c, char_bytes;
6676 unsigned char *msg = (unsigned char *) m;
6677 unsigned char str[MAX_MULTIBYTE_LENGTH];
6678 /* Convert a single-byte string to multibyte
6679 for the *Message* buffer. */
6680 for (i = 0; i < nbytes; i++)
6681 {
6682 c = unibyte_char_to_multibyte (msg[i]);
6683 char_bytes = CHAR_STRING (c, str);
6684 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6685 }
6686 }
6687 else if (nbytes)
6688 insert_1 (m, nbytes, 1, 0, 0);
6689
6690 if (nlflag)
6691 {
6692 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6693 insert_1 ("\n", 1, 1, 0, 0);
6694
6695 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6696 this_bol = PT;
6697 this_bol_byte = PT_BYTE;
6698
6699 /* See if this line duplicates the previous one.
6700 If so, combine duplicates. */
6701 if (this_bol > BEG)
6702 {
6703 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6704 prev_bol = PT;
6705 prev_bol_byte = PT_BYTE;
6706
6707 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6708 this_bol, this_bol_byte);
6709 if (dup)
6710 {
6711 del_range_both (prev_bol, prev_bol_byte,
6712 this_bol, this_bol_byte, 0);
6713 if (dup > 1)
6714 {
6715 char dupstr[40];
6716 int duplen;
6717
6718 /* If you change this format, don't forget to also
6719 change message_log_check_duplicate. */
6720 sprintf (dupstr, " [%d times]", dup);
6721 duplen = strlen (dupstr);
6722 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6723 insert_1 (dupstr, duplen, 1, 0, 1);
6724 }
6725 }
6726 }
6727
6728 /* If we have more than the desired maximum number of lines
6729 in the *Messages* buffer now, delete the oldest ones.
6730 This is safe because we don't have undo in this buffer. */
6731
6732 if (NATNUMP (Vmessage_log_max))
6733 {
6734 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6735 -XFASTINT (Vmessage_log_max) - 1, 0);
6736 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6737 }
6738 }
6739 BEGV = XMARKER (oldbegv)->charpos;
6740 BEGV_BYTE = marker_byte_position (oldbegv);
6741
6742 if (zv_at_end)
6743 {
6744 ZV = Z;
6745 ZV_BYTE = Z_BYTE;
6746 }
6747 else
6748 {
6749 ZV = XMARKER (oldzv)->charpos;
6750 ZV_BYTE = marker_byte_position (oldzv);
6751 }
6752
6753 if (point_at_end)
6754 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6755 else
6756 /* We can't do Fgoto_char (oldpoint) because it will run some
6757 Lisp code. */
6758 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6759 XMARKER (oldpoint)->bytepos);
6760
6761 UNGCPRO;
6762 unchain_marker (XMARKER (oldpoint));
6763 unchain_marker (XMARKER (oldbegv));
6764 unchain_marker (XMARKER (oldzv));
6765
6766 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6767 set_buffer_internal (oldbuf);
6768 if (NILP (tem))
6769 windows_or_buffers_changed = old_windows_or_buffers_changed;
6770 message_log_need_newline = !nlflag;
6771 Vdeactivate_mark = old_deactivate_mark;
6772 }
6773 }
6774
6775
6776 /* We are at the end of the buffer after just having inserted a newline.
6777 (Note: We depend on the fact we won't be crossing the gap.)
6778 Check to see if the most recent message looks a lot like the previous one.
6779 Return 0 if different, 1 if the new one should just replace it, or a
6780 value N > 1 if we should also append " [N times]". */
6781
6782 static int
6783 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6784 int prev_bol, this_bol;
6785 int prev_bol_byte, this_bol_byte;
6786 {
6787 int i;
6788 int len = Z_BYTE - 1 - this_bol_byte;
6789 int seen_dots = 0;
6790 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6791 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6792
6793 for (i = 0; i < len; i++)
6794 {
6795 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6796 seen_dots = 1;
6797 if (p1[i] != p2[i])
6798 return seen_dots;
6799 }
6800 p1 += len;
6801 if (*p1 == '\n')
6802 return 2;
6803 if (*p1++ == ' ' && *p1++ == '[')
6804 {
6805 int n = 0;
6806 while (*p1 >= '0' && *p1 <= '9')
6807 n = n * 10 + *p1++ - '0';
6808 if (strncmp (p1, " times]\n", 8) == 0)
6809 return n+1;
6810 }
6811 return 0;
6812 }
6813 \f
6814
6815 /* Display an echo area message M with a specified length of NBYTES
6816 bytes. The string may include null characters. If M is 0, clear
6817 out any existing message, and let the mini-buffer text show
6818 through.
6819
6820 The buffer M must continue to exist until after the echo area gets
6821 cleared or some other message gets displayed there. This means do
6822 not pass text that is stored in a Lisp string; do not pass text in
6823 a buffer that was alloca'd. */
6824
6825 void
6826 message2 (m, nbytes, multibyte)
6827 const char *m;
6828 int nbytes;
6829 int multibyte;
6830 {
6831 /* First flush out any partial line written with print. */
6832 message_log_maybe_newline ();
6833 if (m)
6834 message_dolog (m, nbytes, 1, multibyte);
6835 message2_nolog (m, nbytes, multibyte);
6836 }
6837
6838
6839 /* The non-logging counterpart of message2. */
6840
6841 void
6842 message2_nolog (m, nbytes, multibyte)
6843 const char *m;
6844 int nbytes, multibyte;
6845 {
6846 struct frame *sf = SELECTED_FRAME ();
6847 message_enable_multibyte = multibyte;
6848
6849 if (noninteractive)
6850 {
6851 if (noninteractive_need_newline)
6852 putc ('\n', stderr);
6853 noninteractive_need_newline = 0;
6854 if (m)
6855 fwrite (m, nbytes, 1, stderr);
6856 if (cursor_in_echo_area == 0)
6857 fprintf (stderr, "\n");
6858 fflush (stderr);
6859 }
6860 /* A null message buffer means that the frame hasn't really been
6861 initialized yet. Error messages get reported properly by
6862 cmd_error, so this must be just an informative message; toss it. */
6863 else if (INTERACTIVE
6864 && sf->glyphs_initialized_p
6865 && FRAME_MESSAGE_BUF (sf))
6866 {
6867 Lisp_Object mini_window;
6868 struct frame *f;
6869
6870 /* Get the frame containing the mini-buffer
6871 that the selected frame is using. */
6872 mini_window = FRAME_MINIBUF_WINDOW (sf);
6873 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6874
6875 FRAME_SAMPLE_VISIBILITY (f);
6876 if (FRAME_VISIBLE_P (sf)
6877 && ! FRAME_VISIBLE_P (f))
6878 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6879
6880 if (m)
6881 {
6882 set_message (m, Qnil, nbytes, multibyte);
6883 if (minibuffer_auto_raise)
6884 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6885 }
6886 else
6887 clear_message (1, 1);
6888
6889 do_pending_window_change (0);
6890 echo_area_display (1);
6891 do_pending_window_change (0);
6892 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6893 (*frame_up_to_date_hook) (f);
6894 }
6895 }
6896
6897
6898 /* Display an echo area message M with a specified length of NBYTES
6899 bytes. The string may include null characters. If M is not a
6900 string, clear out any existing message, and let the mini-buffer
6901 text show through. */
6902
6903 void
6904 message3 (m, nbytes, multibyte)
6905 Lisp_Object m;
6906 int nbytes;
6907 int multibyte;
6908 {
6909 struct gcpro gcpro1;
6910
6911 GCPRO1 (m);
6912 clear_message (1,1);
6913
6914 /* First flush out any partial line written with print. */
6915 message_log_maybe_newline ();
6916 if (STRINGP (m))
6917 message_dolog (SDATA (m), nbytes, 1, multibyte);
6918 message3_nolog (m, nbytes, multibyte);
6919
6920 UNGCPRO;
6921 }
6922
6923
6924 /* The non-logging version of message3. */
6925
6926 void
6927 message3_nolog (m, nbytes, multibyte)
6928 Lisp_Object m;
6929 int nbytes, multibyte;
6930 {
6931 struct frame *sf = SELECTED_FRAME ();
6932 message_enable_multibyte = multibyte;
6933
6934 if (noninteractive)
6935 {
6936 if (noninteractive_need_newline)
6937 putc ('\n', stderr);
6938 noninteractive_need_newline = 0;
6939 if (STRINGP (m))
6940 fwrite (SDATA (m), nbytes, 1, stderr);
6941 if (cursor_in_echo_area == 0)
6942 fprintf (stderr, "\n");
6943 fflush (stderr);
6944 }
6945 /* A null message buffer means that the frame hasn't really been
6946 initialized yet. Error messages get reported properly by
6947 cmd_error, so this must be just an informative message; toss it. */
6948 else if (INTERACTIVE
6949 && sf->glyphs_initialized_p
6950 && FRAME_MESSAGE_BUF (sf))
6951 {
6952 Lisp_Object mini_window;
6953 Lisp_Object frame;
6954 struct frame *f;
6955
6956 /* Get the frame containing the mini-buffer
6957 that the selected frame is using. */
6958 mini_window = FRAME_MINIBUF_WINDOW (sf);
6959 frame = XWINDOW (mini_window)->frame;
6960 f = XFRAME (frame);
6961
6962 FRAME_SAMPLE_VISIBILITY (f);
6963 if (FRAME_VISIBLE_P (sf)
6964 && !FRAME_VISIBLE_P (f))
6965 Fmake_frame_visible (frame);
6966
6967 if (STRINGP (m) && SCHARS (m) > 0)
6968 {
6969 set_message (NULL, m, nbytes, multibyte);
6970 if (minibuffer_auto_raise)
6971 Fraise_frame (frame);
6972 }
6973 else
6974 clear_message (1, 1);
6975
6976 do_pending_window_change (0);
6977 echo_area_display (1);
6978 do_pending_window_change (0);
6979 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6980 (*frame_up_to_date_hook) (f);
6981 }
6982 }
6983
6984
6985 /* Display a null-terminated echo area message M. If M is 0, clear
6986 out any existing message, and let the mini-buffer text show through.
6987
6988 The buffer M must continue to exist until after the echo area gets
6989 cleared or some other message gets displayed there. Do not pass
6990 text that is stored in a Lisp string. Do not pass text in a buffer
6991 that was alloca'd. */
6992
6993 void
6994 message1 (m)
6995 char *m;
6996 {
6997 message2 (m, (m ? strlen (m) : 0), 0);
6998 }
6999
7000
7001 /* The non-logging counterpart of message1. */
7002
7003 void
7004 message1_nolog (m)
7005 char *m;
7006 {
7007 message2_nolog (m, (m ? strlen (m) : 0), 0);
7008 }
7009
7010 /* Display a message M which contains a single %s
7011 which gets replaced with STRING. */
7012
7013 void
7014 message_with_string (m, string, log)
7015 char *m;
7016 Lisp_Object string;
7017 int log;
7018 {
7019 CHECK_STRING (string);
7020
7021 if (noninteractive)
7022 {
7023 if (m)
7024 {
7025 if (noninteractive_need_newline)
7026 putc ('\n', stderr);
7027 noninteractive_need_newline = 0;
7028 fprintf (stderr, m, SDATA (string));
7029 if (cursor_in_echo_area == 0)
7030 fprintf (stderr, "\n");
7031 fflush (stderr);
7032 }
7033 }
7034 else if (INTERACTIVE)
7035 {
7036 /* The frame whose minibuffer we're going to display the message on.
7037 It may be larger than the selected frame, so we need
7038 to use its buffer, not the selected frame's buffer. */
7039 Lisp_Object mini_window;
7040 struct frame *f, *sf = SELECTED_FRAME ();
7041
7042 /* Get the frame containing the minibuffer
7043 that the selected frame is using. */
7044 mini_window = FRAME_MINIBUF_WINDOW (sf);
7045 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7046
7047 /* A null message buffer means that the frame hasn't really been
7048 initialized yet. Error messages get reported properly by
7049 cmd_error, so this must be just an informative message; toss it. */
7050 if (FRAME_MESSAGE_BUF (f))
7051 {
7052 Lisp_Object args[2], message;
7053 struct gcpro gcpro1, gcpro2;
7054
7055 args[0] = build_string (m);
7056 args[1] = message = string;
7057 GCPRO2 (args[0], message);
7058 gcpro1.nvars = 2;
7059
7060 message = Fformat (2, args);
7061
7062 if (log)
7063 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7064 else
7065 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7066
7067 UNGCPRO;
7068
7069 /* Print should start at the beginning of the message
7070 buffer next time. */
7071 message_buf_print = 0;
7072 }
7073 }
7074 }
7075
7076
7077 /* Dump an informative message to the minibuf. If M is 0, clear out
7078 any existing message, and let the mini-buffer text show through. */
7079
7080 /* VARARGS 1 */
7081 void
7082 message (m, a1, a2, a3)
7083 char *m;
7084 EMACS_INT a1, a2, a3;
7085 {
7086 if (noninteractive)
7087 {
7088 if (m)
7089 {
7090 if (noninteractive_need_newline)
7091 putc ('\n', stderr);
7092 noninteractive_need_newline = 0;
7093 fprintf (stderr, m, a1, a2, a3);
7094 if (cursor_in_echo_area == 0)
7095 fprintf (stderr, "\n");
7096 fflush (stderr);
7097 }
7098 }
7099 else if (INTERACTIVE)
7100 {
7101 /* The frame whose mini-buffer we're going to display the message
7102 on. It may be larger than the selected frame, so we need to
7103 use its buffer, not the selected frame's buffer. */
7104 Lisp_Object mini_window;
7105 struct frame *f, *sf = SELECTED_FRAME ();
7106
7107 /* Get the frame containing the mini-buffer
7108 that the selected frame is using. */
7109 mini_window = FRAME_MINIBUF_WINDOW (sf);
7110 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7111
7112 /* A null message buffer means that the frame hasn't really been
7113 initialized yet. Error messages get reported properly by
7114 cmd_error, so this must be just an informative message; toss
7115 it. */
7116 if (FRAME_MESSAGE_BUF (f))
7117 {
7118 if (m)
7119 {
7120 int len;
7121 #ifdef NO_ARG_ARRAY
7122 char *a[3];
7123 a[0] = (char *) a1;
7124 a[1] = (char *) a2;
7125 a[2] = (char *) a3;
7126
7127 len = doprnt (FRAME_MESSAGE_BUF (f),
7128 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7129 #else
7130 len = doprnt (FRAME_MESSAGE_BUF (f),
7131 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7132 (char **) &a1);
7133 #endif /* NO_ARG_ARRAY */
7134
7135 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7136 }
7137 else
7138 message1 (0);
7139
7140 /* Print should start at the beginning of the message
7141 buffer next time. */
7142 message_buf_print = 0;
7143 }
7144 }
7145 }
7146
7147
7148 /* The non-logging version of message. */
7149
7150 void
7151 message_nolog (m, a1, a2, a3)
7152 char *m;
7153 EMACS_INT a1, a2, a3;
7154 {
7155 Lisp_Object old_log_max;
7156 old_log_max = Vmessage_log_max;
7157 Vmessage_log_max = Qnil;
7158 message (m, a1, a2, a3);
7159 Vmessage_log_max = old_log_max;
7160 }
7161
7162
7163 /* Display the current message in the current mini-buffer. This is
7164 only called from error handlers in process.c, and is not time
7165 critical. */
7166
7167 void
7168 update_echo_area ()
7169 {
7170 if (!NILP (echo_area_buffer[0]))
7171 {
7172 Lisp_Object string;
7173 string = Fcurrent_message ();
7174 message3 (string, SBYTES (string),
7175 !NILP (current_buffer->enable_multibyte_characters));
7176 }
7177 }
7178
7179
7180 /* Make sure echo area buffers in `echo_buffers' are live.
7181 If they aren't, make new ones. */
7182
7183 static void
7184 ensure_echo_area_buffers ()
7185 {
7186 int i;
7187
7188 for (i = 0; i < 2; ++i)
7189 if (!BUFFERP (echo_buffer[i])
7190 || NILP (XBUFFER (echo_buffer[i])->name))
7191 {
7192 char name[30];
7193 Lisp_Object old_buffer;
7194 int j;
7195
7196 old_buffer = echo_buffer[i];
7197 sprintf (name, " *Echo Area %d*", i);
7198 echo_buffer[i] = Fget_buffer_create (build_string (name));
7199 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7200
7201 for (j = 0; j < 2; ++j)
7202 if (EQ (old_buffer, echo_area_buffer[j]))
7203 echo_area_buffer[j] = echo_buffer[i];
7204 }
7205 }
7206
7207
7208 /* Call FN with args A1..A4 with either the current or last displayed
7209 echo_area_buffer as current buffer.
7210
7211 WHICH zero means use the current message buffer
7212 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7213 from echo_buffer[] and clear it.
7214
7215 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7216 suitable buffer from echo_buffer[] and clear it.
7217
7218 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
7219 that the current message becomes the last displayed one, make
7220 choose a suitable buffer for echo_area_buffer[0], and clear it.
7221
7222 Value is what FN returns. */
7223
7224 static int
7225 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7226 struct window *w;
7227 int which;
7228 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7229 EMACS_INT a1;
7230 Lisp_Object a2;
7231 EMACS_INT a3, a4;
7232 {
7233 Lisp_Object buffer;
7234 int this_one, the_other, clear_buffer_p, rc;
7235 int count = SPECPDL_INDEX ();
7236
7237 /* If buffers aren't live, make new ones. */
7238 ensure_echo_area_buffers ();
7239
7240 clear_buffer_p = 0;
7241
7242 if (which == 0)
7243 this_one = 0, the_other = 1;
7244 else if (which > 0)
7245 this_one = 1, the_other = 0;
7246 else
7247 {
7248 this_one = 0, the_other = 1;
7249 clear_buffer_p = 1;
7250
7251 /* We need a fresh one in case the current echo buffer equals
7252 the one containing the last displayed echo area message. */
7253 if (!NILP (echo_area_buffer[this_one])
7254 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
7255 echo_area_buffer[this_one] = Qnil;
7256 }
7257
7258 /* Choose a suitable buffer from echo_buffer[] is we don't
7259 have one. */
7260 if (NILP (echo_area_buffer[this_one]))
7261 {
7262 echo_area_buffer[this_one]
7263 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7264 ? echo_buffer[the_other]
7265 : echo_buffer[this_one]);
7266 clear_buffer_p = 1;
7267 }
7268
7269 buffer = echo_area_buffer[this_one];
7270
7271 /* Don't get confused by reusing the buffer used for echoing
7272 for a different purpose. */
7273 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7274 cancel_echoing ();
7275
7276 record_unwind_protect (unwind_with_echo_area_buffer,
7277 with_echo_area_buffer_unwind_data (w));
7278
7279 /* Make the echo area buffer current. Note that for display
7280 purposes, it is not necessary that the displayed window's buffer
7281 == current_buffer, except for text property lookup. So, let's
7282 only set that buffer temporarily here without doing a full
7283 Fset_window_buffer. We must also change w->pointm, though,
7284 because otherwise an assertions in unshow_buffer fails, and Emacs
7285 aborts. */
7286 set_buffer_internal_1 (XBUFFER (buffer));
7287 if (w)
7288 {
7289 w->buffer = buffer;
7290 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7291 }
7292
7293 current_buffer->undo_list = Qt;
7294 current_buffer->read_only = Qnil;
7295 specbind (Qinhibit_read_only, Qt);
7296 specbind (Qinhibit_modification_hooks, Qt);
7297
7298 if (clear_buffer_p && Z > BEG)
7299 del_range (BEG, Z);
7300
7301 xassert (BEGV >= BEG);
7302 xassert (ZV <= Z && ZV >= BEGV);
7303
7304 rc = fn (a1, a2, a3, a4);
7305
7306 xassert (BEGV >= BEG);
7307 xassert (ZV <= Z && ZV >= BEGV);
7308
7309 unbind_to (count, Qnil);
7310 return rc;
7311 }
7312
7313
7314 /* Save state that should be preserved around the call to the function
7315 FN called in with_echo_area_buffer. */
7316
7317 static Lisp_Object
7318 with_echo_area_buffer_unwind_data (w)
7319 struct window *w;
7320 {
7321 int i = 0;
7322 Lisp_Object vector;
7323
7324 /* Reduce consing by keeping one vector in
7325 Vwith_echo_area_save_vector. */
7326 vector = Vwith_echo_area_save_vector;
7327 Vwith_echo_area_save_vector = Qnil;
7328
7329 if (NILP (vector))
7330 vector = Fmake_vector (make_number (7), Qnil);
7331
7332 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7333 AREF (vector, i) = Vdeactivate_mark, ++i;
7334 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7335
7336 if (w)
7337 {
7338 XSETWINDOW (AREF (vector, i), w); ++i;
7339 AREF (vector, i) = w->buffer; ++i;
7340 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7341 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7342 }
7343 else
7344 {
7345 int end = i + 4;
7346 for (; i < end; ++i)
7347 AREF (vector, i) = Qnil;
7348 }
7349
7350 xassert (i == ASIZE (vector));
7351 return vector;
7352 }
7353
7354
7355 /* Restore global state from VECTOR which was created by
7356 with_echo_area_buffer_unwind_data. */
7357
7358 static Lisp_Object
7359 unwind_with_echo_area_buffer (vector)
7360 Lisp_Object vector;
7361 {
7362 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7363 Vdeactivate_mark = AREF (vector, 1);
7364 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7365
7366 if (WINDOWP (AREF (vector, 3)))
7367 {
7368 struct window *w;
7369 Lisp_Object buffer, charpos, bytepos;
7370
7371 w = XWINDOW (AREF (vector, 3));
7372 buffer = AREF (vector, 4);
7373 charpos = AREF (vector, 5);
7374 bytepos = AREF (vector, 6);
7375
7376 w->buffer = buffer;
7377 set_marker_both (w->pointm, buffer,
7378 XFASTINT (charpos), XFASTINT (bytepos));
7379 }
7380
7381 Vwith_echo_area_save_vector = vector;
7382 return Qnil;
7383 }
7384
7385
7386 /* Set up the echo area for use by print functions. MULTIBYTE_P
7387 non-zero means we will print multibyte. */
7388
7389 void
7390 setup_echo_area_for_printing (multibyte_p)
7391 int multibyte_p;
7392 {
7393 /* If we can't find an echo area any more, exit. */
7394 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7395 Fkill_emacs (Qnil);
7396
7397 ensure_echo_area_buffers ();
7398
7399 if (!message_buf_print)
7400 {
7401 /* A message has been output since the last time we printed.
7402 Choose a fresh echo area buffer. */
7403 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7404 echo_area_buffer[0] = echo_buffer[1];
7405 else
7406 echo_area_buffer[0] = echo_buffer[0];
7407
7408 /* Switch to that buffer and clear it. */
7409 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7410 current_buffer->truncate_lines = Qnil;
7411
7412 if (Z > BEG)
7413 {
7414 int count = SPECPDL_INDEX ();
7415 specbind (Qinhibit_read_only, Qt);
7416 /* Note that undo recording is always disabled. */
7417 del_range (BEG, Z);
7418 unbind_to (count, Qnil);
7419 }
7420 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7421
7422 /* Set up the buffer for the multibyteness we need. */
7423 if (multibyte_p
7424 != !NILP (current_buffer->enable_multibyte_characters))
7425 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7426
7427 /* Raise the frame containing the echo area. */
7428 if (minibuffer_auto_raise)
7429 {
7430 struct frame *sf = SELECTED_FRAME ();
7431 Lisp_Object mini_window;
7432 mini_window = FRAME_MINIBUF_WINDOW (sf);
7433 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7434 }
7435
7436 message_log_maybe_newline ();
7437 message_buf_print = 1;
7438 }
7439 else
7440 {
7441 if (NILP (echo_area_buffer[0]))
7442 {
7443 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7444 echo_area_buffer[0] = echo_buffer[1];
7445 else
7446 echo_area_buffer[0] = echo_buffer[0];
7447 }
7448
7449 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7450 {
7451 /* Someone switched buffers between print requests. */
7452 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7453 current_buffer->truncate_lines = Qnil;
7454 }
7455 }
7456 }
7457
7458
7459 /* Display an echo area message in window W. Value is non-zero if W's
7460 height is changed. If display_last_displayed_message_p is
7461 non-zero, display the message that was last displayed, otherwise
7462 display the current message. */
7463
7464 static int
7465 display_echo_area (w)
7466 struct window *w;
7467 {
7468 int i, no_message_p, window_height_changed_p, count;
7469
7470 /* Temporarily disable garbage collections while displaying the echo
7471 area. This is done because a GC can print a message itself.
7472 That message would modify the echo area buffer's contents while a
7473 redisplay of the buffer is going on, and seriously confuse
7474 redisplay. */
7475 count = inhibit_garbage_collection ();
7476
7477 /* If there is no message, we must call display_echo_area_1
7478 nevertheless because it resizes the window. But we will have to
7479 reset the echo_area_buffer in question to nil at the end because
7480 with_echo_area_buffer will sets it to an empty buffer. */
7481 i = display_last_displayed_message_p ? 1 : 0;
7482 no_message_p = NILP (echo_area_buffer[i]);
7483
7484 window_height_changed_p
7485 = with_echo_area_buffer (w, display_last_displayed_message_p,
7486 display_echo_area_1,
7487 (EMACS_INT) w, Qnil, 0, 0);
7488
7489 if (no_message_p)
7490 echo_area_buffer[i] = Qnil;
7491
7492 unbind_to (count, Qnil);
7493 return window_height_changed_p;
7494 }
7495
7496
7497 /* Helper for display_echo_area. Display the current buffer which
7498 contains the current echo area message in window W, a mini-window,
7499 a pointer to which is passed in A1. A2..A4 are currently not used.
7500 Change the height of W so that all of the message is displayed.
7501 Value is non-zero if height of W was changed. */
7502
7503 static int
7504 display_echo_area_1 (a1, a2, a3, a4)
7505 EMACS_INT a1;
7506 Lisp_Object a2;
7507 EMACS_INT a3, a4;
7508 {
7509 struct window *w = (struct window *) a1;
7510 Lisp_Object window;
7511 struct text_pos start;
7512 int window_height_changed_p = 0;
7513
7514 /* Do this before displaying, so that we have a large enough glyph
7515 matrix for the display. */
7516 window_height_changed_p = resize_mini_window (w, 0);
7517
7518 /* Display. */
7519 clear_glyph_matrix (w->desired_matrix);
7520 XSETWINDOW (window, w);
7521 SET_TEXT_POS (start, BEG, BEG_BYTE);
7522 try_window (window, start);
7523
7524 return window_height_changed_p;
7525 }
7526
7527
7528 /* Resize the echo area window to exactly the size needed for the
7529 currently displayed message, if there is one. If a mini-buffer
7530 is active, don't shrink it. */
7531
7532 void
7533 resize_echo_area_exactly ()
7534 {
7535 if (BUFFERP (echo_area_buffer[0])
7536 && WINDOWP (echo_area_window))
7537 {
7538 struct window *w = XWINDOW (echo_area_window);
7539 int resized_p;
7540 Lisp_Object resize_exactly;
7541
7542 if (minibuf_level == 0)
7543 resize_exactly = Qt;
7544 else
7545 resize_exactly = Qnil;
7546
7547 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7548 (EMACS_INT) w, resize_exactly, 0, 0);
7549 if (resized_p)
7550 {
7551 ++windows_or_buffers_changed;
7552 ++update_mode_lines;
7553 redisplay_internal (0);
7554 }
7555 }
7556 }
7557
7558
7559 /* Callback function for with_echo_area_buffer, when used from
7560 resize_echo_area_exactly. A1 contains a pointer to the window to
7561 resize, EXACTLY non-nil means resize the mini-window exactly to the
7562 size of the text displayed. A3 and A4 are not used. Value is what
7563 resize_mini_window returns. */
7564
7565 static int
7566 resize_mini_window_1 (a1, exactly, a3, a4)
7567 EMACS_INT a1;
7568 Lisp_Object exactly;
7569 EMACS_INT a3, a4;
7570 {
7571 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7572 }
7573
7574
7575 /* Resize mini-window W to fit the size of its contents. EXACT:P
7576 means size the window exactly to the size needed. Otherwise, it's
7577 only enlarged until W's buffer is empty. Value is non-zero if
7578 the window height has been changed. */
7579
7580 int
7581 resize_mini_window (w, exact_p)
7582 struct window *w;
7583 int exact_p;
7584 {
7585 struct frame *f = XFRAME (w->frame);
7586 int window_height_changed_p = 0;
7587
7588 xassert (MINI_WINDOW_P (w));
7589
7590 /* Don't resize windows while redisplaying a window; it would
7591 confuse redisplay functions when the size of the window they are
7592 displaying changes from under them. Such a resizing can happen,
7593 for instance, when which-func prints a long message while
7594 we are running fontification-functions. We're running these
7595 functions with safe_call which binds inhibit-redisplay to t. */
7596 if (!NILP (Vinhibit_redisplay))
7597 return 0;
7598
7599 /* Nil means don't try to resize. */
7600 if (NILP (Vresize_mini_windows)
7601 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7602 return 0;
7603
7604 if (!FRAME_MINIBUF_ONLY_P (f))
7605 {
7606 struct it it;
7607 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7608 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7609 int height, max_height;
7610 int unit = FRAME_LINE_HEIGHT (f);
7611 struct text_pos start;
7612 struct buffer *old_current_buffer = NULL;
7613
7614 if (current_buffer != XBUFFER (w->buffer))
7615 {
7616 old_current_buffer = current_buffer;
7617 set_buffer_internal (XBUFFER (w->buffer));
7618 }
7619
7620 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7621
7622 /* Compute the max. number of lines specified by the user. */
7623 if (FLOATP (Vmax_mini_window_height))
7624 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7625 else if (INTEGERP (Vmax_mini_window_height))
7626 max_height = XINT (Vmax_mini_window_height);
7627 else
7628 max_height = total_height / 4;
7629
7630 /* Correct that max. height if it's bogus. */
7631 max_height = max (1, max_height);
7632 max_height = min (total_height, max_height);
7633
7634 /* Find out the height of the text in the window. */
7635 if (it.truncate_lines_p)
7636 height = 1;
7637 else
7638 {
7639 last_height = 0;
7640 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7641 if (it.max_ascent == 0 && it.max_descent == 0)
7642 height = it.current_y + last_height;
7643 else
7644 height = it.current_y + it.max_ascent + it.max_descent;
7645 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7646 height = (height + unit - 1) / unit;
7647 }
7648
7649 /* Compute a suitable window start. */
7650 if (height > max_height)
7651 {
7652 height = max_height;
7653 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7654 move_it_vertically_backward (&it, (height - 1) * unit);
7655 start = it.current.pos;
7656 }
7657 else
7658 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7659 SET_MARKER_FROM_TEXT_POS (w->start, start);
7660
7661 if (EQ (Vresize_mini_windows, Qgrow_only))
7662 {
7663 /* Let it grow only, until we display an empty message, in which
7664 case the window shrinks again. */
7665 if (height > WINDOW_TOTAL_LINES (w))
7666 {
7667 int old_height = WINDOW_TOTAL_LINES (w);
7668 freeze_window_starts (f, 1);
7669 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7670 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7671 }
7672 else if (height < WINDOW_TOTAL_LINES (w)
7673 && (exact_p || BEGV == ZV))
7674 {
7675 int old_height = WINDOW_TOTAL_LINES (w);
7676 freeze_window_starts (f, 0);
7677 shrink_mini_window (w);
7678 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7679 }
7680 }
7681 else
7682 {
7683 /* Always resize to exact size needed. */
7684 if (height > WINDOW_TOTAL_LINES (w))
7685 {
7686 int old_height = WINDOW_TOTAL_LINES (w);
7687 freeze_window_starts (f, 1);
7688 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7689 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7690 }
7691 else if (height < WINDOW_TOTAL_LINES (w))
7692 {
7693 int old_height = WINDOW_TOTAL_LINES (w);
7694 freeze_window_starts (f, 0);
7695 shrink_mini_window (w);
7696
7697 if (height)
7698 {
7699 freeze_window_starts (f, 1);
7700 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7701 }
7702
7703 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7704 }
7705 }
7706
7707 if (old_current_buffer)
7708 set_buffer_internal (old_current_buffer);
7709 }
7710
7711 return window_height_changed_p;
7712 }
7713
7714
7715 /* Value is the current message, a string, or nil if there is no
7716 current message. */
7717
7718 Lisp_Object
7719 current_message ()
7720 {
7721 Lisp_Object msg;
7722
7723 if (NILP (echo_area_buffer[0]))
7724 msg = Qnil;
7725 else
7726 {
7727 with_echo_area_buffer (0, 0, current_message_1,
7728 (EMACS_INT) &msg, Qnil, 0, 0);
7729 if (NILP (msg))
7730 echo_area_buffer[0] = Qnil;
7731 }
7732
7733 return msg;
7734 }
7735
7736
7737 static int
7738 current_message_1 (a1, a2, a3, a4)
7739 EMACS_INT a1;
7740 Lisp_Object a2;
7741 EMACS_INT a3, a4;
7742 {
7743 Lisp_Object *msg = (Lisp_Object *) a1;
7744
7745 if (Z > BEG)
7746 *msg = make_buffer_string (BEG, Z, 1);
7747 else
7748 *msg = Qnil;
7749 return 0;
7750 }
7751
7752
7753 /* Push the current message on Vmessage_stack for later restauration
7754 by restore_message. Value is non-zero if the current message isn't
7755 empty. This is a relatively infrequent operation, so it's not
7756 worth optimizing. */
7757
7758 int
7759 push_message ()
7760 {
7761 Lisp_Object msg;
7762 msg = current_message ();
7763 Vmessage_stack = Fcons (msg, Vmessage_stack);
7764 return STRINGP (msg);
7765 }
7766
7767
7768 /* Restore message display from the top of Vmessage_stack. */
7769
7770 void
7771 restore_message ()
7772 {
7773 Lisp_Object msg;
7774
7775 xassert (CONSP (Vmessage_stack));
7776 msg = XCAR (Vmessage_stack);
7777 if (STRINGP (msg))
7778 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7779 else
7780 message3_nolog (msg, 0, 0);
7781 }
7782
7783
7784 /* Handler for record_unwind_protect calling pop_message. */
7785
7786 Lisp_Object
7787 pop_message_unwind (dummy)
7788 Lisp_Object dummy;
7789 {
7790 pop_message ();
7791 return Qnil;
7792 }
7793
7794 /* Pop the top-most entry off Vmessage_stack. */
7795
7796 void
7797 pop_message ()
7798 {
7799 xassert (CONSP (Vmessage_stack));
7800 Vmessage_stack = XCDR (Vmessage_stack);
7801 }
7802
7803
7804 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7805 exits. If the stack is not empty, we have a missing pop_message
7806 somewhere. */
7807
7808 void
7809 check_message_stack ()
7810 {
7811 if (!NILP (Vmessage_stack))
7812 abort ();
7813 }
7814
7815
7816 /* Truncate to NCHARS what will be displayed in the echo area the next
7817 time we display it---but don't redisplay it now. */
7818
7819 void
7820 truncate_echo_area (nchars)
7821 int nchars;
7822 {
7823 if (nchars == 0)
7824 echo_area_buffer[0] = Qnil;
7825 /* A null message buffer means that the frame hasn't really been
7826 initialized yet. Error messages get reported properly by
7827 cmd_error, so this must be just an informative message; toss it. */
7828 else if (!noninteractive
7829 && INTERACTIVE
7830 && !NILP (echo_area_buffer[0]))
7831 {
7832 struct frame *sf = SELECTED_FRAME ();
7833 if (FRAME_MESSAGE_BUF (sf))
7834 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7835 }
7836 }
7837
7838
7839 /* Helper function for truncate_echo_area. Truncate the current
7840 message to at most NCHARS characters. */
7841
7842 static int
7843 truncate_message_1 (nchars, a2, a3, a4)
7844 EMACS_INT nchars;
7845 Lisp_Object a2;
7846 EMACS_INT a3, a4;
7847 {
7848 if (BEG + nchars < Z)
7849 del_range (BEG + nchars, Z);
7850 if (Z == BEG)
7851 echo_area_buffer[0] = Qnil;
7852 return 0;
7853 }
7854
7855
7856 /* Set the current message to a substring of S or STRING.
7857
7858 If STRING is a Lisp string, set the message to the first NBYTES
7859 bytes from STRING. NBYTES zero means use the whole string. If
7860 STRING is multibyte, the message will be displayed multibyte.
7861
7862 If S is not null, set the message to the first LEN bytes of S. LEN
7863 zero means use the whole string. MULTIBYTE_P non-zero means S is
7864 multibyte. Display the message multibyte in that case. */
7865
7866 void
7867 set_message (s, string, nbytes, multibyte_p)
7868 const char *s;
7869 Lisp_Object string;
7870 int nbytes, multibyte_p;
7871 {
7872 message_enable_multibyte
7873 = ((s && multibyte_p)
7874 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7875
7876 with_echo_area_buffer (0, -1, set_message_1,
7877 (EMACS_INT) s, string, nbytes, multibyte_p);
7878 message_buf_print = 0;
7879 help_echo_showing_p = 0;
7880 }
7881
7882
7883 /* Helper function for set_message. Arguments have the same meaning
7884 as there, with A1 corresponding to S and A2 corresponding to STRING
7885 This function is called with the echo area buffer being
7886 current. */
7887
7888 static int
7889 set_message_1 (a1, a2, nbytes, multibyte_p)
7890 EMACS_INT a1;
7891 Lisp_Object a2;
7892 EMACS_INT nbytes, multibyte_p;
7893 {
7894 const char *s = (const char *) a1;
7895 Lisp_Object string = a2;
7896
7897 xassert (BEG == Z);
7898
7899 /* Change multibyteness of the echo buffer appropriately. */
7900 if (message_enable_multibyte
7901 != !NILP (current_buffer->enable_multibyte_characters))
7902 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7903
7904 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7905
7906 /* Insert new message at BEG. */
7907 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7908
7909 if (STRINGP (string))
7910 {
7911 int nchars;
7912
7913 if (nbytes == 0)
7914 nbytes = SBYTES (string);
7915 nchars = string_byte_to_char (string, nbytes);
7916
7917 /* This function takes care of single/multibyte conversion. We
7918 just have to ensure that the echo area buffer has the right
7919 setting of enable_multibyte_characters. */
7920 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7921 }
7922 else if (s)
7923 {
7924 if (nbytes == 0)
7925 nbytes = strlen (s);
7926
7927 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
7928 {
7929 /* Convert from multi-byte to single-byte. */
7930 int i, c, n;
7931 unsigned char work[1];
7932
7933 /* Convert a multibyte string to single-byte. */
7934 for (i = 0; i < nbytes; i += n)
7935 {
7936 c = string_char_and_length (s + i, nbytes - i, &n);
7937 work[0] = (SINGLE_BYTE_CHAR_P (c)
7938 ? c
7939 : multibyte_char_to_unibyte (c, Qnil));
7940 insert_1_both (work, 1, 1, 1, 0, 0);
7941 }
7942 }
7943 else if (!multibyte_p
7944 && !NILP (current_buffer->enable_multibyte_characters))
7945 {
7946 /* Convert from single-byte to multi-byte. */
7947 int i, c, n;
7948 const unsigned char *msg = (const unsigned char *) s;
7949 unsigned char str[MAX_MULTIBYTE_LENGTH];
7950
7951 /* Convert a single-byte string to multibyte. */
7952 for (i = 0; i < nbytes; i++)
7953 {
7954 c = unibyte_char_to_multibyte (msg[i]);
7955 n = CHAR_STRING (c, str);
7956 insert_1_both (str, 1, n, 1, 0, 0);
7957 }
7958 }
7959 else
7960 insert_1 (s, nbytes, 1, 0, 0);
7961 }
7962
7963 return 0;
7964 }
7965
7966
7967 /* Clear messages. CURRENT_P non-zero means clear the current
7968 message. LAST_DISPLAYED_P non-zero means clear the message
7969 last displayed. */
7970
7971 void
7972 clear_message (current_p, last_displayed_p)
7973 int current_p, last_displayed_p;
7974 {
7975 if (current_p)
7976 {
7977 echo_area_buffer[0] = Qnil;
7978 message_cleared_p = 1;
7979 }
7980
7981 if (last_displayed_p)
7982 echo_area_buffer[1] = Qnil;
7983
7984 message_buf_print = 0;
7985 }
7986
7987 /* Clear garbaged frames.
7988
7989 This function is used where the old redisplay called
7990 redraw_garbaged_frames which in turn called redraw_frame which in
7991 turn called clear_frame. The call to clear_frame was a source of
7992 flickering. I believe a clear_frame is not necessary. It should
7993 suffice in the new redisplay to invalidate all current matrices,
7994 and ensure a complete redisplay of all windows. */
7995
7996 static void
7997 clear_garbaged_frames ()
7998 {
7999 if (frame_garbaged)
8000 {
8001 Lisp_Object tail, frame;
8002 int changed_count = 0;
8003
8004 FOR_EACH_FRAME (tail, frame)
8005 {
8006 struct frame *f = XFRAME (frame);
8007
8008 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8009 {
8010 if (f->resized_p)
8011 {
8012 Fredraw_frame (frame);
8013 f->force_flush_display_p = 1;
8014 }
8015 clear_current_matrices (f);
8016 changed_count++;
8017 f->garbaged = 0;
8018 f->resized_p = 0;
8019 }
8020 }
8021
8022 frame_garbaged = 0;
8023 if (changed_count)
8024 ++windows_or_buffers_changed;
8025 }
8026 }
8027
8028
8029 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8030 is non-zero update selected_frame. Value is non-zero if the
8031 mini-windows height has been changed. */
8032
8033 static int
8034 echo_area_display (update_frame_p)
8035 int update_frame_p;
8036 {
8037 Lisp_Object mini_window;
8038 struct window *w;
8039 struct frame *f;
8040 int window_height_changed_p = 0;
8041 struct frame *sf = SELECTED_FRAME ();
8042
8043 mini_window = FRAME_MINIBUF_WINDOW (sf);
8044 w = XWINDOW (mini_window);
8045 f = XFRAME (WINDOW_FRAME (w));
8046
8047 /* Don't display if frame is invisible or not yet initialized. */
8048 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8049 return 0;
8050
8051 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8052 #ifndef MAC_OS8
8053 #ifdef HAVE_WINDOW_SYSTEM
8054 /* When Emacs starts, selected_frame may be a visible terminal
8055 frame, even if we run under a window system. If we let this
8056 through, a message would be displayed on the terminal. */
8057 if (EQ (selected_frame, Vterminal_frame)
8058 && !NILP (Vwindow_system))
8059 return 0;
8060 #endif /* HAVE_WINDOW_SYSTEM */
8061 #endif
8062
8063 /* Redraw garbaged frames. */
8064 if (frame_garbaged)
8065 clear_garbaged_frames ();
8066
8067 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8068 {
8069 echo_area_window = mini_window;
8070 window_height_changed_p = display_echo_area (w);
8071 w->must_be_updated_p = 1;
8072
8073 /* Update the display, unless called from redisplay_internal.
8074 Also don't update the screen during redisplay itself. The
8075 update will happen at the end of redisplay, and an update
8076 here could cause confusion. */
8077 if (update_frame_p && !redisplaying_p)
8078 {
8079 int n = 0;
8080
8081 /* If the display update has been interrupted by pending
8082 input, update mode lines in the frame. Due to the
8083 pending input, it might have been that redisplay hasn't
8084 been called, so that mode lines above the echo area are
8085 garbaged. This looks odd, so we prevent it here. */
8086 if (!display_completed)
8087 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8088
8089 if (window_height_changed_p
8090 /* Don't do this if Emacs is shutting down. Redisplay
8091 needs to run hooks. */
8092 && !NILP (Vrun_hooks))
8093 {
8094 /* Must update other windows. Likewise as in other
8095 cases, don't let this update be interrupted by
8096 pending input. */
8097 int count = SPECPDL_INDEX ();
8098 specbind (Qredisplay_dont_pause, Qt);
8099 windows_or_buffers_changed = 1;
8100 redisplay_internal (0);
8101 unbind_to (count, Qnil);
8102 }
8103 else if (FRAME_WINDOW_P (f) && n == 0)
8104 {
8105 /* Window configuration is the same as before.
8106 Can do with a display update of the echo area,
8107 unless we displayed some mode lines. */
8108 update_single_window (w, 1);
8109 rif->flush_display (f);
8110 }
8111 else
8112 update_frame (f, 1, 1);
8113
8114 /* If cursor is in the echo area, make sure that the next
8115 redisplay displays the minibuffer, so that the cursor will
8116 be replaced with what the minibuffer wants. */
8117 if (cursor_in_echo_area)
8118 ++windows_or_buffers_changed;
8119 }
8120 }
8121 else if (!EQ (mini_window, selected_window))
8122 windows_or_buffers_changed++;
8123
8124 /* Last displayed message is now the current message. */
8125 echo_area_buffer[1] = echo_area_buffer[0];
8126 /* Inform read_char that we're not echoing. */
8127 echo_message_buffer = Qnil;
8128
8129 /* Prevent redisplay optimization in redisplay_internal by resetting
8130 this_line_start_pos. This is done because the mini-buffer now
8131 displays the message instead of its buffer text. */
8132 if (EQ (mini_window, selected_window))
8133 CHARPOS (this_line_start_pos) = 0;
8134
8135 return window_height_changed_p;
8136 }
8137
8138
8139 \f
8140 /***********************************************************************
8141 Frame Titles
8142 ***********************************************************************/
8143
8144
8145 /* The frame title buffering code is also used by Fformat_mode_line.
8146 So it is not conditioned by HAVE_WINDOW_SYSTEM. */
8147
8148 /* A buffer for constructing frame titles in it; allocated from the
8149 heap in init_xdisp and resized as needed in store_frame_title_char. */
8150
8151 static char *frame_title_buf;
8152
8153 /* The buffer's end, and a current output position in it. */
8154
8155 static char *frame_title_buf_end;
8156 static char *frame_title_ptr;
8157
8158
8159 /* Store a single character C for the frame title in frame_title_buf.
8160 Re-allocate frame_title_buf if necessary. */
8161
8162 static void
8163 #ifdef PROTOTYPES
8164 store_frame_title_char (char c)
8165 #else
8166 store_frame_title_char (c)
8167 char c;
8168 #endif
8169 {
8170 /* If output position has reached the end of the allocated buffer,
8171 double the buffer's size. */
8172 if (frame_title_ptr == frame_title_buf_end)
8173 {
8174 int len = frame_title_ptr - frame_title_buf;
8175 int new_size = 2 * len * sizeof *frame_title_buf;
8176 frame_title_buf = (char *) xrealloc (frame_title_buf, new_size);
8177 frame_title_buf_end = frame_title_buf + new_size;
8178 frame_title_ptr = frame_title_buf + len;
8179 }
8180
8181 *frame_title_ptr++ = c;
8182 }
8183
8184
8185 /* Store part of a frame title in frame_title_buf, beginning at
8186 frame_title_ptr. STR is the string to store. Do not copy
8187 characters that yield more columns than PRECISION; PRECISION <= 0
8188 means copy the whole string. Pad with spaces until FIELD_WIDTH
8189 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8190 pad. Called from display_mode_element when it is used to build a
8191 frame title. */
8192
8193 static int
8194 store_frame_title (str, field_width, precision)
8195 const unsigned char *str;
8196 int field_width, precision;
8197 {
8198 int n = 0;
8199 int dummy, nbytes;
8200
8201 /* Copy at most PRECISION chars from STR. */
8202 nbytes = strlen (str);
8203 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8204 while (nbytes--)
8205 store_frame_title_char (*str++);
8206
8207 /* Fill up with spaces until FIELD_WIDTH reached. */
8208 while (field_width > 0
8209 && n < field_width)
8210 {
8211 store_frame_title_char (' ');
8212 ++n;
8213 }
8214
8215 return n;
8216 }
8217
8218 #ifdef HAVE_WINDOW_SYSTEM
8219
8220 /* Set the title of FRAME, if it has changed. The title format is
8221 Vicon_title_format if FRAME is iconified, otherwise it is
8222 frame_title_format. */
8223
8224 static void
8225 x_consider_frame_title (frame)
8226 Lisp_Object frame;
8227 {
8228 struct frame *f = XFRAME (frame);
8229
8230 if (FRAME_WINDOW_P (f)
8231 || FRAME_MINIBUF_ONLY_P (f)
8232 || f->explicit_name)
8233 {
8234 /* Do we have more than one visible frame on this X display? */
8235 Lisp_Object tail;
8236 Lisp_Object fmt;
8237 struct buffer *obuf;
8238 int len;
8239 struct it it;
8240
8241 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8242 {
8243 Lisp_Object other_frame = XCAR (tail);
8244 struct frame *tf = XFRAME (other_frame);
8245
8246 if (tf != f
8247 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8248 && !FRAME_MINIBUF_ONLY_P (tf)
8249 && !EQ (other_frame, tip_frame)
8250 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8251 break;
8252 }
8253
8254 /* Set global variable indicating that multiple frames exist. */
8255 multiple_frames = CONSP (tail);
8256
8257 /* Switch to the buffer of selected window of the frame. Set up
8258 frame_title_ptr so that display_mode_element will output into it;
8259 then display the title. */
8260 obuf = current_buffer;
8261 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8262 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8263 frame_title_ptr = frame_title_buf;
8264 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8265 NULL, DEFAULT_FACE_ID);
8266 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8267 len = frame_title_ptr - frame_title_buf;
8268 frame_title_ptr = NULL;
8269 set_buffer_internal_1 (obuf);
8270
8271 /* Set the title only if it's changed. This avoids consing in
8272 the common case where it hasn't. (If it turns out that we've
8273 already wasted too much time by walking through the list with
8274 display_mode_element, then we might need to optimize at a
8275 higher level than this.) */
8276 if (! STRINGP (f->name)
8277 || SBYTES (f->name) != len
8278 || bcmp (frame_title_buf, SDATA (f->name), len) != 0)
8279 x_implicitly_set_name (f, make_string (frame_title_buf, len), Qnil);
8280 }
8281 }
8282
8283 #endif /* not HAVE_WINDOW_SYSTEM */
8284
8285
8286
8287 \f
8288 /***********************************************************************
8289 Menu Bars
8290 ***********************************************************************/
8291
8292
8293 /* Prepare for redisplay by updating menu-bar item lists when
8294 appropriate. This can call eval. */
8295
8296 void
8297 prepare_menu_bars ()
8298 {
8299 int all_windows;
8300 struct gcpro gcpro1, gcpro2;
8301 struct frame *f;
8302 Lisp_Object tooltip_frame;
8303
8304 #ifdef HAVE_WINDOW_SYSTEM
8305 tooltip_frame = tip_frame;
8306 #else
8307 tooltip_frame = Qnil;
8308 #endif
8309
8310 /* Update all frame titles based on their buffer names, etc. We do
8311 this before the menu bars so that the buffer-menu will show the
8312 up-to-date frame titles. */
8313 #ifdef HAVE_WINDOW_SYSTEM
8314 if (windows_or_buffers_changed || update_mode_lines)
8315 {
8316 Lisp_Object tail, frame;
8317
8318 FOR_EACH_FRAME (tail, frame)
8319 {
8320 f = XFRAME (frame);
8321 if (!EQ (frame, tooltip_frame)
8322 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8323 x_consider_frame_title (frame);
8324 }
8325 }
8326 #endif /* HAVE_WINDOW_SYSTEM */
8327
8328 /* Update the menu bar item lists, if appropriate. This has to be
8329 done before any actual redisplay or generation of display lines. */
8330 all_windows = (update_mode_lines
8331 || buffer_shared > 1
8332 || windows_or_buffers_changed);
8333 if (all_windows)
8334 {
8335 Lisp_Object tail, frame;
8336 int count = SPECPDL_INDEX ();
8337
8338 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8339
8340 FOR_EACH_FRAME (tail, frame)
8341 {
8342 f = XFRAME (frame);
8343
8344 /* Ignore tooltip frame. */
8345 if (EQ (frame, tooltip_frame))
8346 continue;
8347
8348 /* If a window on this frame changed size, report that to
8349 the user and clear the size-change flag. */
8350 if (FRAME_WINDOW_SIZES_CHANGED (f))
8351 {
8352 Lisp_Object functions;
8353
8354 /* Clear flag first in case we get an error below. */
8355 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8356 functions = Vwindow_size_change_functions;
8357 GCPRO2 (tail, functions);
8358
8359 while (CONSP (functions))
8360 {
8361 call1 (XCAR (functions), frame);
8362 functions = XCDR (functions);
8363 }
8364 UNGCPRO;
8365 }
8366
8367 GCPRO1 (tail);
8368 update_menu_bar (f, 0);
8369 #ifdef HAVE_WINDOW_SYSTEM
8370 update_tool_bar (f, 0);
8371 #endif
8372 UNGCPRO;
8373 }
8374
8375 unbind_to (count, Qnil);
8376 }
8377 else
8378 {
8379 struct frame *sf = SELECTED_FRAME ();
8380 update_menu_bar (sf, 1);
8381 #ifdef HAVE_WINDOW_SYSTEM
8382 update_tool_bar (sf, 1);
8383 #endif
8384 }
8385
8386 /* Motif needs this. See comment in xmenu.c. Turn it off when
8387 pending_menu_activation is not defined. */
8388 #ifdef USE_X_TOOLKIT
8389 pending_menu_activation = 0;
8390 #endif
8391 }
8392
8393
8394 /* Update the menu bar item list for frame F. This has to be done
8395 before we start to fill in any display lines, because it can call
8396 eval.
8397
8398 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8399
8400 static void
8401 update_menu_bar (f, save_match_data)
8402 struct frame *f;
8403 int save_match_data;
8404 {
8405 Lisp_Object window;
8406 register struct window *w;
8407
8408 /* If called recursively during a menu update, do nothing. This can
8409 happen when, for instance, an activate-menubar-hook causes a
8410 redisplay. */
8411 if (inhibit_menubar_update)
8412 return;
8413
8414 window = FRAME_SELECTED_WINDOW (f);
8415 w = XWINDOW (window);
8416
8417 #if 0 /* The if statement below this if statement used to include the
8418 condition !NILP (w->update_mode_line), rather than using
8419 update_mode_lines directly, and this if statement may have
8420 been added to make that condition work. Now the if
8421 statement below matches its comment, this isn't needed. */
8422 if (update_mode_lines)
8423 w->update_mode_line = Qt;
8424 #endif
8425
8426 if (FRAME_WINDOW_P (f)
8427 ?
8428 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8429 || defined (USE_GTK)
8430 FRAME_EXTERNAL_MENU_BAR (f)
8431 #else
8432 FRAME_MENU_BAR_LINES (f) > 0
8433 #endif
8434 : FRAME_MENU_BAR_LINES (f) > 0)
8435 {
8436 /* If the user has switched buffers or windows, we need to
8437 recompute to reflect the new bindings. But we'll
8438 recompute when update_mode_lines is set too; that means
8439 that people can use force-mode-line-update to request
8440 that the menu bar be recomputed. The adverse effect on
8441 the rest of the redisplay algorithm is about the same as
8442 windows_or_buffers_changed anyway. */
8443 if (windows_or_buffers_changed
8444 /* This used to test w->update_mode_line, but we believe
8445 there is no need to recompute the menu in that case. */
8446 || update_mode_lines
8447 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8448 < BUF_MODIFF (XBUFFER (w->buffer)))
8449 != !NILP (w->last_had_star))
8450 || ((!NILP (Vtransient_mark_mode)
8451 && !NILP (XBUFFER (w->buffer)->mark_active))
8452 != !NILP (w->region_showing)))
8453 {
8454 struct buffer *prev = current_buffer;
8455 int count = SPECPDL_INDEX ();
8456
8457 specbind (Qinhibit_menubar_update, Qt);
8458
8459 set_buffer_internal_1 (XBUFFER (w->buffer));
8460 if (save_match_data)
8461 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8462 if (NILP (Voverriding_local_map_menu_flag))
8463 {
8464 specbind (Qoverriding_terminal_local_map, Qnil);
8465 specbind (Qoverriding_local_map, Qnil);
8466 }
8467
8468 /* Run the Lucid hook. */
8469 safe_run_hooks (Qactivate_menubar_hook);
8470
8471 /* If it has changed current-menubar from previous value,
8472 really recompute the menu-bar from the value. */
8473 if (! NILP (Vlucid_menu_bar_dirty_flag))
8474 call0 (Qrecompute_lucid_menubar);
8475
8476 safe_run_hooks (Qmenu_bar_update_hook);
8477 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8478
8479 /* Redisplay the menu bar in case we changed it. */
8480 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8481 || defined (USE_GTK)
8482 if (FRAME_WINDOW_P (f)
8483 #if defined (MAC_OS)
8484 /* All frames on Mac OS share the same menubar. So only the
8485 selected frame should be allowed to set it. */
8486 && f == SELECTED_FRAME ()
8487 #endif
8488 )
8489 set_frame_menubar (f, 0, 0);
8490 else
8491 /* On a terminal screen, the menu bar is an ordinary screen
8492 line, and this makes it get updated. */
8493 w->update_mode_line = Qt;
8494 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8495 /* In the non-toolkit version, the menu bar is an ordinary screen
8496 line, and this makes it get updated. */
8497 w->update_mode_line = Qt;
8498 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8499
8500 unbind_to (count, Qnil);
8501 set_buffer_internal_1 (prev);
8502 }
8503 }
8504 }
8505
8506
8507 \f
8508 /***********************************************************************
8509 Output Cursor
8510 ***********************************************************************/
8511
8512 #ifdef HAVE_WINDOW_SYSTEM
8513
8514 /* EXPORT:
8515 Nominal cursor position -- where to draw output.
8516 HPOS and VPOS are window relative glyph matrix coordinates.
8517 X and Y are window relative pixel coordinates. */
8518
8519 struct cursor_pos output_cursor;
8520
8521
8522 /* EXPORT:
8523 Set the global variable output_cursor to CURSOR. All cursor
8524 positions are relative to updated_window. */
8525
8526 void
8527 set_output_cursor (cursor)
8528 struct cursor_pos *cursor;
8529 {
8530 output_cursor.hpos = cursor->hpos;
8531 output_cursor.vpos = cursor->vpos;
8532 output_cursor.x = cursor->x;
8533 output_cursor.y = cursor->y;
8534 }
8535
8536
8537 /* EXPORT for RIF:
8538 Set a nominal cursor position.
8539
8540 HPOS and VPOS are column/row positions in a window glyph matrix. X
8541 and Y are window text area relative pixel positions.
8542
8543 If this is done during an update, updated_window will contain the
8544 window that is being updated and the position is the future output
8545 cursor position for that window. If updated_window is null, use
8546 selected_window and display the cursor at the given position. */
8547
8548 void
8549 x_cursor_to (vpos, hpos, y, x)
8550 int vpos, hpos, y, x;
8551 {
8552 struct window *w;
8553
8554 /* If updated_window is not set, work on selected_window. */
8555 if (updated_window)
8556 w = updated_window;
8557 else
8558 w = XWINDOW (selected_window);
8559
8560 /* Set the output cursor. */
8561 output_cursor.hpos = hpos;
8562 output_cursor.vpos = vpos;
8563 output_cursor.x = x;
8564 output_cursor.y = y;
8565
8566 /* If not called as part of an update, really display the cursor.
8567 This will also set the cursor position of W. */
8568 if (updated_window == NULL)
8569 {
8570 BLOCK_INPUT;
8571 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8572 if (rif->flush_display_optional)
8573 rif->flush_display_optional (SELECTED_FRAME ());
8574 UNBLOCK_INPUT;
8575 }
8576 }
8577
8578 #endif /* HAVE_WINDOW_SYSTEM */
8579
8580 \f
8581 /***********************************************************************
8582 Tool-bars
8583 ***********************************************************************/
8584
8585 #ifdef HAVE_WINDOW_SYSTEM
8586
8587 /* Where the mouse was last time we reported a mouse event. */
8588
8589 FRAME_PTR last_mouse_frame;
8590
8591 /* Tool-bar item index of the item on which a mouse button was pressed
8592 or -1. */
8593
8594 int last_tool_bar_item;
8595
8596
8597 /* Update the tool-bar item list for frame F. This has to be done
8598 before we start to fill in any display lines. Called from
8599 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8600 and restore it here. */
8601
8602 static void
8603 update_tool_bar (f, save_match_data)
8604 struct frame *f;
8605 int save_match_data;
8606 {
8607 #ifdef USE_GTK
8608 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8609 #else
8610 int do_update = WINDOWP (f->tool_bar_window)
8611 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8612 #endif
8613
8614 if (do_update)
8615 {
8616 Lisp_Object window;
8617 struct window *w;
8618
8619 window = FRAME_SELECTED_WINDOW (f);
8620 w = XWINDOW (window);
8621
8622 /* If the user has switched buffers or windows, we need to
8623 recompute to reflect the new bindings. But we'll
8624 recompute when update_mode_lines is set too; that means
8625 that people can use force-mode-line-update to request
8626 that the menu bar be recomputed. The adverse effect on
8627 the rest of the redisplay algorithm is about the same as
8628 windows_or_buffers_changed anyway. */
8629 if (windows_or_buffers_changed
8630 || !NILP (w->update_mode_line)
8631 || update_mode_lines
8632 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8633 < BUF_MODIFF (XBUFFER (w->buffer)))
8634 != !NILP (w->last_had_star))
8635 || ((!NILP (Vtransient_mark_mode)
8636 && !NILP (XBUFFER (w->buffer)->mark_active))
8637 != !NILP (w->region_showing)))
8638 {
8639 struct buffer *prev = current_buffer;
8640 int count = SPECPDL_INDEX ();
8641 Lisp_Object new_tool_bar;
8642 int new_n_tool_bar;
8643 struct gcpro gcpro1;
8644
8645 /* Set current_buffer to the buffer of the selected
8646 window of the frame, so that we get the right local
8647 keymaps. */
8648 set_buffer_internal_1 (XBUFFER (w->buffer));
8649
8650 /* Save match data, if we must. */
8651 if (save_match_data)
8652 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
8653
8654 /* Make sure that we don't accidentally use bogus keymaps. */
8655 if (NILP (Voverriding_local_map_menu_flag))
8656 {
8657 specbind (Qoverriding_terminal_local_map, Qnil);
8658 specbind (Qoverriding_local_map, Qnil);
8659 }
8660
8661 GCPRO1 (new_tool_bar);
8662
8663 /* Build desired tool-bar items from keymaps. */
8664 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8665 &new_n_tool_bar);
8666
8667 /* Redisplay the tool-bar if we changed it. */
8668 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8669 {
8670 /* Redisplay that happens asynchronously due to an expose event
8671 may access f->tool_bar_items. Make sure we update both
8672 variables within BLOCK_INPUT so no such event interrupts. */
8673 BLOCK_INPUT;
8674 f->tool_bar_items = new_tool_bar;
8675 f->n_tool_bar_items = new_n_tool_bar;
8676 w->update_mode_line = Qt;
8677 UNBLOCK_INPUT;
8678 }
8679
8680 UNGCPRO;
8681
8682 unbind_to (count, Qnil);
8683 set_buffer_internal_1 (prev);
8684 }
8685 }
8686 }
8687
8688
8689 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8690 F's desired tool-bar contents. F->tool_bar_items must have
8691 been set up previously by calling prepare_menu_bars. */
8692
8693 static void
8694 build_desired_tool_bar_string (f)
8695 struct frame *f;
8696 {
8697 int i, size, size_needed;
8698 struct gcpro gcpro1, gcpro2, gcpro3;
8699 Lisp_Object image, plist, props;
8700
8701 image = plist = props = Qnil;
8702 GCPRO3 (image, plist, props);
8703
8704 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8705 Otherwise, make a new string. */
8706
8707 /* The size of the string we might be able to reuse. */
8708 size = (STRINGP (f->desired_tool_bar_string)
8709 ? SCHARS (f->desired_tool_bar_string)
8710 : 0);
8711
8712 /* We need one space in the string for each image. */
8713 size_needed = f->n_tool_bar_items;
8714
8715 /* Reuse f->desired_tool_bar_string, if possible. */
8716 if (size < size_needed || NILP (f->desired_tool_bar_string))
8717 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8718 make_number (' '));
8719 else
8720 {
8721 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8722 Fremove_text_properties (make_number (0), make_number (size),
8723 props, f->desired_tool_bar_string);
8724 }
8725
8726 /* Put a `display' property on the string for the images to display,
8727 put a `menu_item' property on tool-bar items with a value that
8728 is the index of the item in F's tool-bar item vector. */
8729 for (i = 0; i < f->n_tool_bar_items; ++i)
8730 {
8731 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8732
8733 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8734 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8735 int hmargin, vmargin, relief, idx, end;
8736 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8737
8738 /* If image is a vector, choose the image according to the
8739 button state. */
8740 image = PROP (TOOL_BAR_ITEM_IMAGES);
8741 if (VECTORP (image))
8742 {
8743 if (enabled_p)
8744 idx = (selected_p
8745 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8746 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8747 else
8748 idx = (selected_p
8749 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8750 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8751
8752 xassert (ASIZE (image) >= idx);
8753 image = AREF (image, idx);
8754 }
8755 else
8756 idx = -1;
8757
8758 /* Ignore invalid image specifications. */
8759 if (!valid_image_p (image))
8760 continue;
8761
8762 /* Display the tool-bar button pressed, or depressed. */
8763 plist = Fcopy_sequence (XCDR (image));
8764
8765 /* Compute margin and relief to draw. */
8766 relief = (tool_bar_button_relief >= 0
8767 ? tool_bar_button_relief
8768 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8769 hmargin = vmargin = relief;
8770
8771 if (INTEGERP (Vtool_bar_button_margin)
8772 && XINT (Vtool_bar_button_margin) > 0)
8773 {
8774 hmargin += XFASTINT (Vtool_bar_button_margin);
8775 vmargin += XFASTINT (Vtool_bar_button_margin);
8776 }
8777 else if (CONSP (Vtool_bar_button_margin))
8778 {
8779 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8780 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8781 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8782
8783 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8784 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8785 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8786 }
8787
8788 if (auto_raise_tool_bar_buttons_p)
8789 {
8790 /* Add a `:relief' property to the image spec if the item is
8791 selected. */
8792 if (selected_p)
8793 {
8794 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8795 hmargin -= relief;
8796 vmargin -= relief;
8797 }
8798 }
8799 else
8800 {
8801 /* If image is selected, display it pressed, i.e. with a
8802 negative relief. If it's not selected, display it with a
8803 raised relief. */
8804 plist = Fplist_put (plist, QCrelief,
8805 (selected_p
8806 ? make_number (-relief)
8807 : make_number (relief)));
8808 hmargin -= relief;
8809 vmargin -= relief;
8810 }
8811
8812 /* Put a margin around the image. */
8813 if (hmargin || vmargin)
8814 {
8815 if (hmargin == vmargin)
8816 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8817 else
8818 plist = Fplist_put (plist, QCmargin,
8819 Fcons (make_number (hmargin),
8820 make_number (vmargin)));
8821 }
8822
8823 /* If button is not enabled, and we don't have special images
8824 for the disabled state, make the image appear disabled by
8825 applying an appropriate algorithm to it. */
8826 if (!enabled_p && idx < 0)
8827 plist = Fplist_put (plist, QCconversion, Qdisabled);
8828
8829 /* Put a `display' text property on the string for the image to
8830 display. Put a `menu-item' property on the string that gives
8831 the start of this item's properties in the tool-bar items
8832 vector. */
8833 image = Fcons (Qimage, plist);
8834 props = list4 (Qdisplay, image,
8835 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8836
8837 /* Let the last image hide all remaining spaces in the tool bar
8838 string. The string can be longer than needed when we reuse a
8839 previous string. */
8840 if (i + 1 == f->n_tool_bar_items)
8841 end = SCHARS (f->desired_tool_bar_string);
8842 else
8843 end = i + 1;
8844 Fadd_text_properties (make_number (i), make_number (end),
8845 props, f->desired_tool_bar_string);
8846 #undef PROP
8847 }
8848
8849 UNGCPRO;
8850 }
8851
8852
8853 /* Display one line of the tool-bar of frame IT->f. */
8854
8855 static void
8856 display_tool_bar_line (it)
8857 struct it *it;
8858 {
8859 struct glyph_row *row = it->glyph_row;
8860 int max_x = it->last_visible_x;
8861 struct glyph *last;
8862
8863 prepare_desired_row (row);
8864 row->y = it->current_y;
8865
8866 /* Note that this isn't made use of if the face hasn't a box,
8867 so there's no need to check the face here. */
8868 it->start_of_box_run_p = 1;
8869
8870 while (it->current_x < max_x)
8871 {
8872 int x_before, x, n_glyphs_before, i, nglyphs;
8873
8874 /* Get the next display element. */
8875 if (!get_next_display_element (it))
8876 break;
8877
8878 /* Produce glyphs. */
8879 x_before = it->current_x;
8880 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
8881 PRODUCE_GLYPHS (it);
8882
8883 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
8884 i = 0;
8885 x = x_before;
8886 while (i < nglyphs)
8887 {
8888 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
8889
8890 if (x + glyph->pixel_width > max_x)
8891 {
8892 /* Glyph doesn't fit on line. */
8893 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
8894 it->current_x = x;
8895 goto out;
8896 }
8897
8898 ++it->hpos;
8899 x += glyph->pixel_width;
8900 ++i;
8901 }
8902
8903 /* Stop at line ends. */
8904 if (ITERATOR_AT_END_OF_LINE_P (it))
8905 break;
8906
8907 set_iterator_to_next (it, 1);
8908 }
8909
8910 out:;
8911
8912 row->displays_text_p = row->used[TEXT_AREA] != 0;
8913 extend_face_to_end_of_line (it);
8914 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
8915 last->right_box_line_p = 1;
8916 if (last == row->glyphs[TEXT_AREA])
8917 last->left_box_line_p = 1;
8918 compute_line_metrics (it);
8919
8920 /* If line is empty, make it occupy the rest of the tool-bar. */
8921 if (!row->displays_text_p)
8922 {
8923 row->height = row->phys_height = it->last_visible_y - row->y;
8924 row->ascent = row->phys_ascent = 0;
8925 row->extra_line_spacing = 0;
8926 }
8927
8928 row->full_width_p = 1;
8929 row->continued_p = 0;
8930 row->truncated_on_left_p = 0;
8931 row->truncated_on_right_p = 0;
8932
8933 it->current_x = it->hpos = 0;
8934 it->current_y += row->height;
8935 ++it->vpos;
8936 ++it->glyph_row;
8937 }
8938
8939
8940 /* Value is the number of screen lines needed to make all tool-bar
8941 items of frame F visible. */
8942
8943 static int
8944 tool_bar_lines_needed (f)
8945 struct frame *f;
8946 {
8947 struct window *w = XWINDOW (f->tool_bar_window);
8948 struct it it;
8949
8950 /* Initialize an iterator for iteration over
8951 F->desired_tool_bar_string in the tool-bar window of frame F. */
8952 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
8953 it.first_visible_x = 0;
8954 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
8955 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
8956
8957 while (!ITERATOR_AT_END_P (&it))
8958 {
8959 it.glyph_row = w->desired_matrix->rows;
8960 clear_glyph_row (it.glyph_row);
8961 display_tool_bar_line (&it);
8962 }
8963
8964 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
8965 }
8966
8967
8968 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
8969 0, 1, 0,
8970 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
8971 (frame)
8972 Lisp_Object frame;
8973 {
8974 struct frame *f;
8975 struct window *w;
8976 int nlines = 0;
8977
8978 if (NILP (frame))
8979 frame = selected_frame;
8980 else
8981 CHECK_FRAME (frame);
8982 f = XFRAME (frame);
8983
8984 if (WINDOWP (f->tool_bar_window)
8985 || (w = XWINDOW (f->tool_bar_window),
8986 WINDOW_TOTAL_LINES (w) > 0))
8987 {
8988 update_tool_bar (f, 1);
8989 if (f->n_tool_bar_items)
8990 {
8991 build_desired_tool_bar_string (f);
8992 nlines = tool_bar_lines_needed (f);
8993 }
8994 }
8995
8996 return make_number (nlines);
8997 }
8998
8999
9000 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9001 height should be changed. */
9002
9003 static int
9004 redisplay_tool_bar (f)
9005 struct frame *f;
9006 {
9007 struct window *w;
9008 struct it it;
9009 struct glyph_row *row;
9010 int change_height_p = 0;
9011
9012 #ifdef USE_GTK
9013 if (FRAME_EXTERNAL_TOOL_BAR (f))
9014 update_frame_tool_bar (f);
9015 return 0;
9016 #endif
9017
9018 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9019 do anything. This means you must start with tool-bar-lines
9020 non-zero to get the auto-sizing effect. Or in other words, you
9021 can turn off tool-bars by specifying tool-bar-lines zero. */
9022 if (!WINDOWP (f->tool_bar_window)
9023 || (w = XWINDOW (f->tool_bar_window),
9024 WINDOW_TOTAL_LINES (w) == 0))
9025 return 0;
9026
9027 /* Set up an iterator for the tool-bar window. */
9028 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9029 it.first_visible_x = 0;
9030 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9031 row = it.glyph_row;
9032
9033 /* Build a string that represents the contents of the tool-bar. */
9034 build_desired_tool_bar_string (f);
9035 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9036
9037 /* Display as many lines as needed to display all tool-bar items. */
9038 while (it.current_y < it.last_visible_y)
9039 display_tool_bar_line (&it);
9040
9041 /* It doesn't make much sense to try scrolling in the tool-bar
9042 window, so don't do it. */
9043 w->desired_matrix->no_scrolling_p = 1;
9044 w->must_be_updated_p = 1;
9045
9046 if (auto_resize_tool_bars_p)
9047 {
9048 int nlines;
9049
9050 /* If we couldn't display everything, change the tool-bar's
9051 height. */
9052 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9053 change_height_p = 1;
9054
9055 /* If there are blank lines at the end, except for a partially
9056 visible blank line at the end that is smaller than
9057 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9058 row = it.glyph_row - 1;
9059 if (!row->displays_text_p
9060 && row->height >= FRAME_LINE_HEIGHT (f))
9061 change_height_p = 1;
9062
9063 /* If row displays tool-bar items, but is partially visible,
9064 change the tool-bar's height. */
9065 if (row->displays_text_p
9066 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9067 change_height_p = 1;
9068
9069 /* Resize windows as needed by changing the `tool-bar-lines'
9070 frame parameter. */
9071 if (change_height_p
9072 && (nlines = tool_bar_lines_needed (f),
9073 nlines != WINDOW_TOTAL_LINES (w)))
9074 {
9075 extern Lisp_Object Qtool_bar_lines;
9076 Lisp_Object frame;
9077 int old_height = WINDOW_TOTAL_LINES (w);
9078
9079 XSETFRAME (frame, f);
9080 clear_glyph_matrix (w->desired_matrix);
9081 Fmodify_frame_parameters (frame,
9082 Fcons (Fcons (Qtool_bar_lines,
9083 make_number (nlines)),
9084 Qnil));
9085 if (WINDOW_TOTAL_LINES (w) != old_height)
9086 fonts_changed_p = 1;
9087 }
9088 }
9089
9090 return change_height_p;
9091 }
9092
9093
9094 /* Get information about the tool-bar item which is displayed in GLYPH
9095 on frame F. Return in *PROP_IDX the index where tool-bar item
9096 properties start in F->tool_bar_items. Value is zero if
9097 GLYPH doesn't display a tool-bar item. */
9098
9099 static int
9100 tool_bar_item_info (f, glyph, prop_idx)
9101 struct frame *f;
9102 struct glyph *glyph;
9103 int *prop_idx;
9104 {
9105 Lisp_Object prop;
9106 int success_p;
9107 int charpos;
9108
9109 /* This function can be called asynchronously, which means we must
9110 exclude any possibility that Fget_text_property signals an
9111 error. */
9112 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9113 charpos = max (0, charpos);
9114
9115 /* Get the text property `menu-item' at pos. The value of that
9116 property is the start index of this item's properties in
9117 F->tool_bar_items. */
9118 prop = Fget_text_property (make_number (charpos),
9119 Qmenu_item, f->current_tool_bar_string);
9120 if (INTEGERP (prop))
9121 {
9122 *prop_idx = XINT (prop);
9123 success_p = 1;
9124 }
9125 else
9126 success_p = 0;
9127
9128 return success_p;
9129 }
9130
9131 \f
9132 /* Get information about the tool-bar item at position X/Y on frame F.
9133 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9134 the current matrix of the tool-bar window of F, or NULL if not
9135 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9136 item in F->tool_bar_items. Value is
9137
9138 -1 if X/Y is not on a tool-bar item
9139 0 if X/Y is on the same item that was highlighted before.
9140 1 otherwise. */
9141
9142 static int
9143 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9144 struct frame *f;
9145 int x, y;
9146 struct glyph **glyph;
9147 int *hpos, *vpos, *prop_idx;
9148 {
9149 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9150 struct window *w = XWINDOW (f->tool_bar_window);
9151 int area;
9152
9153 /* Find the glyph under X/Y. */
9154 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9155 if (*glyph == NULL)
9156 return -1;
9157
9158 /* Get the start of this tool-bar item's properties in
9159 f->tool_bar_items. */
9160 if (!tool_bar_item_info (f, *glyph, prop_idx))
9161 return -1;
9162
9163 /* Is mouse on the highlighted item? */
9164 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9165 && *vpos >= dpyinfo->mouse_face_beg_row
9166 && *vpos <= dpyinfo->mouse_face_end_row
9167 && (*vpos > dpyinfo->mouse_face_beg_row
9168 || *hpos >= dpyinfo->mouse_face_beg_col)
9169 && (*vpos < dpyinfo->mouse_face_end_row
9170 || *hpos < dpyinfo->mouse_face_end_col
9171 || dpyinfo->mouse_face_past_end))
9172 return 0;
9173
9174 return 1;
9175 }
9176
9177
9178 /* EXPORT:
9179 Handle mouse button event on the tool-bar of frame F, at
9180 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9181 0 for button release. MODIFIERS is event modifiers for button
9182 release. */
9183
9184 void
9185 handle_tool_bar_click (f, x, y, down_p, modifiers)
9186 struct frame *f;
9187 int x, y, down_p;
9188 unsigned int modifiers;
9189 {
9190 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9191 struct window *w = XWINDOW (f->tool_bar_window);
9192 int hpos, vpos, prop_idx;
9193 struct glyph *glyph;
9194 Lisp_Object enabled_p;
9195
9196 /* If not on the highlighted tool-bar item, return. */
9197 frame_to_window_pixel_xy (w, &x, &y);
9198 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9199 return;
9200
9201 /* If item is disabled, do nothing. */
9202 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9203 if (NILP (enabled_p))
9204 return;
9205
9206 if (down_p)
9207 {
9208 /* Show item in pressed state. */
9209 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9210 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9211 last_tool_bar_item = prop_idx;
9212 }
9213 else
9214 {
9215 Lisp_Object key, frame;
9216 struct input_event event;
9217 EVENT_INIT (event);
9218
9219 /* Show item in released state. */
9220 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9221 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9222
9223 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9224
9225 XSETFRAME (frame, f);
9226 event.kind = TOOL_BAR_EVENT;
9227 event.frame_or_window = frame;
9228 event.arg = frame;
9229 kbd_buffer_store_event (&event);
9230
9231 event.kind = TOOL_BAR_EVENT;
9232 event.frame_or_window = frame;
9233 event.arg = key;
9234 event.modifiers = modifiers;
9235 kbd_buffer_store_event (&event);
9236 last_tool_bar_item = -1;
9237 }
9238 }
9239
9240
9241 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9242 tool-bar window-relative coordinates X/Y. Called from
9243 note_mouse_highlight. */
9244
9245 static void
9246 note_tool_bar_highlight (f, x, y)
9247 struct frame *f;
9248 int x, y;
9249 {
9250 Lisp_Object window = f->tool_bar_window;
9251 struct window *w = XWINDOW (window);
9252 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9253 int hpos, vpos;
9254 struct glyph *glyph;
9255 struct glyph_row *row;
9256 int i;
9257 Lisp_Object enabled_p;
9258 int prop_idx;
9259 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9260 int mouse_down_p, rc;
9261
9262 /* Function note_mouse_highlight is called with negative x(y
9263 values when mouse moves outside of the frame. */
9264 if (x <= 0 || y <= 0)
9265 {
9266 clear_mouse_face (dpyinfo);
9267 return;
9268 }
9269
9270 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9271 if (rc < 0)
9272 {
9273 /* Not on tool-bar item. */
9274 clear_mouse_face (dpyinfo);
9275 return;
9276 }
9277 else if (rc == 0)
9278 /* On same tool-bar item as before. */
9279 goto set_help_echo;
9280
9281 clear_mouse_face (dpyinfo);
9282
9283 /* Mouse is down, but on different tool-bar item? */
9284 mouse_down_p = (dpyinfo->grabbed
9285 && f == last_mouse_frame
9286 && FRAME_LIVE_P (f));
9287 if (mouse_down_p
9288 && last_tool_bar_item != prop_idx)
9289 return;
9290
9291 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9292 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9293
9294 /* If tool-bar item is not enabled, don't highlight it. */
9295 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9296 if (!NILP (enabled_p))
9297 {
9298 /* Compute the x-position of the glyph. In front and past the
9299 image is a space. We include this in the highlighted area. */
9300 row = MATRIX_ROW (w->current_matrix, vpos);
9301 for (i = x = 0; i < hpos; ++i)
9302 x += row->glyphs[TEXT_AREA][i].pixel_width;
9303
9304 /* Record this as the current active region. */
9305 dpyinfo->mouse_face_beg_col = hpos;
9306 dpyinfo->mouse_face_beg_row = vpos;
9307 dpyinfo->mouse_face_beg_x = x;
9308 dpyinfo->mouse_face_beg_y = row->y;
9309 dpyinfo->mouse_face_past_end = 0;
9310
9311 dpyinfo->mouse_face_end_col = hpos + 1;
9312 dpyinfo->mouse_face_end_row = vpos;
9313 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9314 dpyinfo->mouse_face_end_y = row->y;
9315 dpyinfo->mouse_face_window = window;
9316 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9317
9318 /* Display it as active. */
9319 show_mouse_face (dpyinfo, draw);
9320 dpyinfo->mouse_face_image_state = draw;
9321 }
9322
9323 set_help_echo:
9324
9325 /* Set help_echo_string to a help string to display for this tool-bar item.
9326 XTread_socket does the rest. */
9327 help_echo_object = help_echo_window = Qnil;
9328 help_echo_pos = -1;
9329 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9330 if (NILP (help_echo_string))
9331 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9332 }
9333
9334 #endif /* HAVE_WINDOW_SYSTEM */
9335
9336
9337 \f
9338 /************************************************************************
9339 Horizontal scrolling
9340 ************************************************************************/
9341
9342 static int hscroll_window_tree P_ ((Lisp_Object));
9343 static int hscroll_windows P_ ((Lisp_Object));
9344
9345 /* For all leaf windows in the window tree rooted at WINDOW, set their
9346 hscroll value so that PT is (i) visible in the window, and (ii) so
9347 that it is not within a certain margin at the window's left and
9348 right border. Value is non-zero if any window's hscroll has been
9349 changed. */
9350
9351 static int
9352 hscroll_window_tree (window)
9353 Lisp_Object window;
9354 {
9355 int hscrolled_p = 0;
9356 int hscroll_relative_p = FLOATP (Vhscroll_step);
9357 int hscroll_step_abs = 0;
9358 double hscroll_step_rel = 0;
9359
9360 if (hscroll_relative_p)
9361 {
9362 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9363 if (hscroll_step_rel < 0)
9364 {
9365 hscroll_relative_p = 0;
9366 hscroll_step_abs = 0;
9367 }
9368 }
9369 else if (INTEGERP (Vhscroll_step))
9370 {
9371 hscroll_step_abs = XINT (Vhscroll_step);
9372 if (hscroll_step_abs < 0)
9373 hscroll_step_abs = 0;
9374 }
9375 else
9376 hscroll_step_abs = 0;
9377
9378 while (WINDOWP (window))
9379 {
9380 struct window *w = XWINDOW (window);
9381
9382 if (WINDOWP (w->hchild))
9383 hscrolled_p |= hscroll_window_tree (w->hchild);
9384 else if (WINDOWP (w->vchild))
9385 hscrolled_p |= hscroll_window_tree (w->vchild);
9386 else if (w->cursor.vpos >= 0)
9387 {
9388 int h_margin;
9389 int text_area_width;
9390 struct glyph_row *current_cursor_row
9391 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9392 struct glyph_row *desired_cursor_row
9393 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9394 struct glyph_row *cursor_row
9395 = (desired_cursor_row->enabled_p
9396 ? desired_cursor_row
9397 : current_cursor_row);
9398
9399 text_area_width = window_box_width (w, TEXT_AREA);
9400
9401 /* Scroll when cursor is inside this scroll margin. */
9402 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9403
9404 if ((XFASTINT (w->hscroll)
9405 && w->cursor.x <= h_margin)
9406 || (cursor_row->enabled_p
9407 && cursor_row->truncated_on_right_p
9408 && (w->cursor.x >= text_area_width - h_margin)))
9409 {
9410 struct it it;
9411 int hscroll;
9412 struct buffer *saved_current_buffer;
9413 int pt;
9414 int wanted_x;
9415
9416 /* Find point in a display of infinite width. */
9417 saved_current_buffer = current_buffer;
9418 current_buffer = XBUFFER (w->buffer);
9419
9420 if (w == XWINDOW (selected_window))
9421 pt = BUF_PT (current_buffer);
9422 else
9423 {
9424 pt = marker_position (w->pointm);
9425 pt = max (BEGV, pt);
9426 pt = min (ZV, pt);
9427 }
9428
9429 /* Move iterator to pt starting at cursor_row->start in
9430 a line with infinite width. */
9431 init_to_row_start (&it, w, cursor_row);
9432 it.last_visible_x = INFINITY;
9433 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9434 current_buffer = saved_current_buffer;
9435
9436 /* Position cursor in window. */
9437 if (!hscroll_relative_p && hscroll_step_abs == 0)
9438 hscroll = max (0, (it.current_x
9439 - (ITERATOR_AT_END_OF_LINE_P (&it)
9440 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9441 : (text_area_width / 2))))
9442 / FRAME_COLUMN_WIDTH (it.f);
9443 else if (w->cursor.x >= text_area_width - h_margin)
9444 {
9445 if (hscroll_relative_p)
9446 wanted_x = text_area_width * (1 - hscroll_step_rel)
9447 - h_margin;
9448 else
9449 wanted_x = text_area_width
9450 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9451 - h_margin;
9452 hscroll
9453 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9454 }
9455 else
9456 {
9457 if (hscroll_relative_p)
9458 wanted_x = text_area_width * hscroll_step_rel
9459 + h_margin;
9460 else
9461 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9462 + h_margin;
9463 hscroll
9464 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9465 }
9466 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9467
9468 /* Don't call Fset_window_hscroll if value hasn't
9469 changed because it will prevent redisplay
9470 optimizations. */
9471 if (XFASTINT (w->hscroll) != hscroll)
9472 {
9473 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9474 w->hscroll = make_number (hscroll);
9475 hscrolled_p = 1;
9476 }
9477 }
9478 }
9479
9480 window = w->next;
9481 }
9482
9483 /* Value is non-zero if hscroll of any leaf window has been changed. */
9484 return hscrolled_p;
9485 }
9486
9487
9488 /* Set hscroll so that cursor is visible and not inside horizontal
9489 scroll margins for all windows in the tree rooted at WINDOW. See
9490 also hscroll_window_tree above. Value is non-zero if any window's
9491 hscroll has been changed. If it has, desired matrices on the frame
9492 of WINDOW are cleared. */
9493
9494 static int
9495 hscroll_windows (window)
9496 Lisp_Object window;
9497 {
9498 int hscrolled_p;
9499
9500 if (automatic_hscrolling_p)
9501 {
9502 hscrolled_p = hscroll_window_tree (window);
9503 if (hscrolled_p)
9504 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9505 }
9506 else
9507 hscrolled_p = 0;
9508 return hscrolled_p;
9509 }
9510
9511
9512 \f
9513 /************************************************************************
9514 Redisplay
9515 ************************************************************************/
9516
9517 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9518 to a non-zero value. This is sometimes handy to have in a debugger
9519 session. */
9520
9521 #if GLYPH_DEBUG
9522
9523 /* First and last unchanged row for try_window_id. */
9524
9525 int debug_first_unchanged_at_end_vpos;
9526 int debug_last_unchanged_at_beg_vpos;
9527
9528 /* Delta vpos and y. */
9529
9530 int debug_dvpos, debug_dy;
9531
9532 /* Delta in characters and bytes for try_window_id. */
9533
9534 int debug_delta, debug_delta_bytes;
9535
9536 /* Values of window_end_pos and window_end_vpos at the end of
9537 try_window_id. */
9538
9539 EMACS_INT debug_end_pos, debug_end_vpos;
9540
9541 /* Append a string to W->desired_matrix->method. FMT is a printf
9542 format string. A1...A9 are a supplement for a variable-length
9543 argument list. If trace_redisplay_p is non-zero also printf the
9544 resulting string to stderr. */
9545
9546 static void
9547 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9548 struct window *w;
9549 char *fmt;
9550 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9551 {
9552 char buffer[512];
9553 char *method = w->desired_matrix->method;
9554 int len = strlen (method);
9555 int size = sizeof w->desired_matrix->method;
9556 int remaining = size - len - 1;
9557
9558 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9559 if (len && remaining)
9560 {
9561 method[len] = '|';
9562 --remaining, ++len;
9563 }
9564
9565 strncpy (method + len, buffer, remaining);
9566
9567 if (trace_redisplay_p)
9568 fprintf (stderr, "%p (%s): %s\n",
9569 w,
9570 ((BUFFERP (w->buffer)
9571 && STRINGP (XBUFFER (w->buffer)->name))
9572 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9573 : "no buffer"),
9574 buffer);
9575 }
9576
9577 #endif /* GLYPH_DEBUG */
9578
9579
9580 /* Value is non-zero if all changes in window W, which displays
9581 current_buffer, are in the text between START and END. START is a
9582 buffer position, END is given as a distance from Z. Used in
9583 redisplay_internal for display optimization. */
9584
9585 static INLINE int
9586 text_outside_line_unchanged_p (w, start, end)
9587 struct window *w;
9588 int start, end;
9589 {
9590 int unchanged_p = 1;
9591
9592 /* If text or overlays have changed, see where. */
9593 if (XFASTINT (w->last_modified) < MODIFF
9594 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9595 {
9596 /* Gap in the line? */
9597 if (GPT < start || Z - GPT < end)
9598 unchanged_p = 0;
9599
9600 /* Changes start in front of the line, or end after it? */
9601 if (unchanged_p
9602 && (BEG_UNCHANGED < start - 1
9603 || END_UNCHANGED < end))
9604 unchanged_p = 0;
9605
9606 /* If selective display, can't optimize if changes start at the
9607 beginning of the line. */
9608 if (unchanged_p
9609 && INTEGERP (current_buffer->selective_display)
9610 && XINT (current_buffer->selective_display) > 0
9611 && (BEG_UNCHANGED < start || GPT <= start))
9612 unchanged_p = 0;
9613
9614 /* If there are overlays at the start or end of the line, these
9615 may have overlay strings with newlines in them. A change at
9616 START, for instance, may actually concern the display of such
9617 overlay strings as well, and they are displayed on different
9618 lines. So, quickly rule out this case. (For the future, it
9619 might be desirable to implement something more telling than
9620 just BEG/END_UNCHANGED.) */
9621 if (unchanged_p)
9622 {
9623 if (BEG + BEG_UNCHANGED == start
9624 && overlay_touches_p (start))
9625 unchanged_p = 0;
9626 if (END_UNCHANGED == end
9627 && overlay_touches_p (Z - end))
9628 unchanged_p = 0;
9629 }
9630 }
9631
9632 return unchanged_p;
9633 }
9634
9635
9636 /* Do a frame update, taking possible shortcuts into account. This is
9637 the main external entry point for redisplay.
9638
9639 If the last redisplay displayed an echo area message and that message
9640 is no longer requested, we clear the echo area or bring back the
9641 mini-buffer if that is in use. */
9642
9643 void
9644 redisplay ()
9645 {
9646 redisplay_internal (0);
9647 }
9648
9649
9650 static Lisp_Object
9651 overlay_arrow_string_or_property (var, pbitmap)
9652 Lisp_Object var;
9653 int *pbitmap;
9654 {
9655 Lisp_Object pstr = Fget (var, Qoverlay_arrow_string);
9656 Lisp_Object bitmap;
9657
9658 if (pbitmap)
9659 {
9660 *pbitmap = 0;
9661 if (bitmap = Fget (var, Qoverlay_arrow_bitmap), INTEGERP (bitmap))
9662 *pbitmap = XINT (bitmap);
9663 }
9664
9665 if (!NILP (pstr))
9666 return pstr;
9667 return Voverlay_arrow_string;
9668 }
9669
9670 /* Return 1 if there are any overlay-arrows in current_buffer. */
9671 static int
9672 overlay_arrow_in_current_buffer_p ()
9673 {
9674 Lisp_Object vlist;
9675
9676 for (vlist = Voverlay_arrow_variable_list;
9677 CONSP (vlist);
9678 vlist = XCDR (vlist))
9679 {
9680 Lisp_Object var = XCAR (vlist);
9681 Lisp_Object val;
9682
9683 if (!SYMBOLP (var))
9684 continue;
9685 val = find_symbol_value (var);
9686 if (MARKERP (val)
9687 && current_buffer == XMARKER (val)->buffer)
9688 return 1;
9689 }
9690 return 0;
9691 }
9692
9693
9694 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9695 has changed. */
9696
9697 static int
9698 overlay_arrows_changed_p ()
9699 {
9700 Lisp_Object vlist;
9701
9702 for (vlist = Voverlay_arrow_variable_list;
9703 CONSP (vlist);
9704 vlist = XCDR (vlist))
9705 {
9706 Lisp_Object var = XCAR (vlist);
9707 Lisp_Object val, pstr;
9708
9709 if (!SYMBOLP (var))
9710 continue;
9711 val = find_symbol_value (var);
9712 if (!MARKERP (val))
9713 continue;
9714 if (! EQ (COERCE_MARKER (val),
9715 Fget (var, Qlast_arrow_position))
9716 || ! (pstr = overlay_arrow_string_or_property (var, 0),
9717 EQ (pstr, Fget (var, Qlast_arrow_string))))
9718 return 1;
9719 }
9720 return 0;
9721 }
9722
9723 /* Mark overlay arrows to be updated on next redisplay. */
9724
9725 static void
9726 update_overlay_arrows (up_to_date)
9727 int up_to_date;
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
9737 if (!SYMBOLP (var))
9738 continue;
9739
9740 if (up_to_date > 0)
9741 {
9742 Lisp_Object val = find_symbol_value (var);
9743 Fput (var, Qlast_arrow_position,
9744 COERCE_MARKER (val));
9745 Fput (var, Qlast_arrow_string,
9746 overlay_arrow_string_or_property (var, 0));
9747 }
9748 else if (up_to_date < 0
9749 || !NILP (Fget (var, Qlast_arrow_position)))
9750 {
9751 Fput (var, Qlast_arrow_position, Qt);
9752 Fput (var, Qlast_arrow_string, Qt);
9753 }
9754 }
9755 }
9756
9757
9758 /* Return overlay arrow string to display at row.
9759 Return t if display as bitmap in left fringe.
9760 Return nil if no overlay arrow. */
9761
9762 static Lisp_Object
9763 overlay_arrow_at_row (it, row, pbitmap)
9764 struct it *it;
9765 struct glyph_row *row;
9766 int *pbitmap;
9767 {
9768 Lisp_Object vlist;
9769
9770 for (vlist = Voverlay_arrow_variable_list;
9771 CONSP (vlist);
9772 vlist = XCDR (vlist))
9773 {
9774 Lisp_Object var = XCAR (vlist);
9775 Lisp_Object val;
9776
9777 if (!SYMBOLP (var))
9778 continue;
9779
9780 val = find_symbol_value (var);
9781
9782 if (MARKERP (val)
9783 && current_buffer == XMARKER (val)->buffer
9784 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9785 {
9786 val = overlay_arrow_string_or_property (var, pbitmap);
9787 if (FRAME_WINDOW_P (it->f)
9788 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9789 return Qt;
9790 if (STRINGP (val))
9791 return val;
9792 break;
9793 }
9794 }
9795
9796 *pbitmap = 0;
9797 return Qnil;
9798 }
9799
9800 /* Return 1 if point moved out of or into a composition. Otherwise
9801 return 0. PREV_BUF and PREV_PT are the last point buffer and
9802 position. BUF and PT are the current point buffer and position. */
9803
9804 int
9805 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9806 struct buffer *prev_buf, *buf;
9807 int prev_pt, pt;
9808 {
9809 int start, end;
9810 Lisp_Object prop;
9811 Lisp_Object buffer;
9812
9813 XSETBUFFER (buffer, buf);
9814 /* Check a composition at the last point if point moved within the
9815 same buffer. */
9816 if (prev_buf == buf)
9817 {
9818 if (prev_pt == pt)
9819 /* Point didn't move. */
9820 return 0;
9821
9822 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9823 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9824 && COMPOSITION_VALID_P (start, end, prop)
9825 && start < prev_pt && end > prev_pt)
9826 /* The last point was within the composition. Return 1 iff
9827 point moved out of the composition. */
9828 return (pt <= start || pt >= end);
9829 }
9830
9831 /* Check a composition at the current point. */
9832 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9833 && find_composition (pt, -1, &start, &end, &prop, buffer)
9834 && COMPOSITION_VALID_P (start, end, prop)
9835 && start < pt && end > pt);
9836 }
9837
9838
9839 /* Reconsider the setting of B->clip_changed which is displayed
9840 in window W. */
9841
9842 static INLINE void
9843 reconsider_clip_changes (w, b)
9844 struct window *w;
9845 struct buffer *b;
9846 {
9847 if (b->clip_changed
9848 && !NILP (w->window_end_valid)
9849 && w->current_matrix->buffer == b
9850 && w->current_matrix->zv == BUF_ZV (b)
9851 && w->current_matrix->begv == BUF_BEGV (b))
9852 b->clip_changed = 0;
9853
9854 /* If display wasn't paused, and W is not a tool bar window, see if
9855 point has been moved into or out of a composition. In that case,
9856 we set b->clip_changed to 1 to force updating the screen. If
9857 b->clip_changed has already been set to 1, we can skip this
9858 check. */
9859 if (!b->clip_changed
9860 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
9861 {
9862 int pt;
9863
9864 if (w == XWINDOW (selected_window))
9865 pt = BUF_PT (current_buffer);
9866 else
9867 pt = marker_position (w->pointm);
9868
9869 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
9870 || pt != XINT (w->last_point))
9871 && check_point_in_composition (w->current_matrix->buffer,
9872 XINT (w->last_point),
9873 XBUFFER (w->buffer), pt))
9874 b->clip_changed = 1;
9875 }
9876 }
9877 \f
9878
9879 /* Select FRAME to forward the values of frame-local variables into C
9880 variables so that the redisplay routines can access those values
9881 directly. */
9882
9883 static void
9884 select_frame_for_redisplay (frame)
9885 Lisp_Object frame;
9886 {
9887 Lisp_Object tail, sym, val;
9888 Lisp_Object old = selected_frame;
9889
9890 selected_frame = frame;
9891
9892 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
9893 if (CONSP (XCAR (tail))
9894 && (sym = XCAR (XCAR (tail)),
9895 SYMBOLP (sym))
9896 && (sym = indirect_variable (sym),
9897 val = SYMBOL_VALUE (sym),
9898 (BUFFER_LOCAL_VALUEP (val)
9899 || SOME_BUFFER_LOCAL_VALUEP (val)))
9900 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9901 Fsymbol_value (sym);
9902
9903 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
9904 if (CONSP (XCAR (tail))
9905 && (sym = XCAR (XCAR (tail)),
9906 SYMBOLP (sym))
9907 && (sym = indirect_variable (sym),
9908 val = SYMBOL_VALUE (sym),
9909 (BUFFER_LOCAL_VALUEP (val)
9910 || SOME_BUFFER_LOCAL_VALUEP (val)))
9911 && XBUFFER_LOCAL_VALUE (val)->check_frame)
9912 Fsymbol_value (sym);
9913 }
9914
9915
9916 #define STOP_POLLING \
9917 do { if (! polling_stopped_here) stop_polling (); \
9918 polling_stopped_here = 1; } while (0)
9919
9920 #define RESUME_POLLING \
9921 do { if (polling_stopped_here) start_polling (); \
9922 polling_stopped_here = 0; } while (0)
9923
9924
9925 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
9926 response to any user action; therefore, we should preserve the echo
9927 area. (Actually, our caller does that job.) Perhaps in the future
9928 avoid recentering windows if it is not necessary; currently that
9929 causes some problems. */
9930
9931 static void
9932 redisplay_internal (preserve_echo_area)
9933 int preserve_echo_area;
9934 {
9935 struct window *w = XWINDOW (selected_window);
9936 struct frame *f = XFRAME (w->frame);
9937 int pause;
9938 int must_finish = 0;
9939 struct text_pos tlbufpos, tlendpos;
9940 int number_of_visible_frames;
9941 int count;
9942 struct frame *sf = SELECTED_FRAME ();
9943 int polling_stopped_here = 0;
9944
9945 /* Non-zero means redisplay has to consider all windows on all
9946 frames. Zero means, only selected_window is considered. */
9947 int consider_all_windows_p;
9948
9949 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
9950
9951 /* No redisplay if running in batch mode or frame is not yet fully
9952 initialized, or redisplay is explicitly turned off by setting
9953 Vinhibit_redisplay. */
9954 if (noninteractive
9955 || !NILP (Vinhibit_redisplay)
9956 || !f->glyphs_initialized_p)
9957 return;
9958
9959 /* The flag redisplay_performed_directly_p is set by
9960 direct_output_for_insert when it already did the whole screen
9961 update necessary. */
9962 if (redisplay_performed_directly_p)
9963 {
9964 redisplay_performed_directly_p = 0;
9965 if (!hscroll_windows (selected_window))
9966 return;
9967 }
9968
9969 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9970 if (popup_activated ())
9971 return;
9972 #endif
9973
9974 /* I don't think this happens but let's be paranoid. */
9975 if (redisplaying_p)
9976 return;
9977
9978 /* Record a function that resets redisplaying_p to its old value
9979 when we leave this function. */
9980 count = SPECPDL_INDEX ();
9981 record_unwind_protect (unwind_redisplay,
9982 Fcons (make_number (redisplaying_p), selected_frame));
9983 ++redisplaying_p;
9984 specbind (Qinhibit_free_realized_faces, Qnil);
9985
9986 retry:
9987 pause = 0;
9988 reconsider_clip_changes (w, current_buffer);
9989
9990 /* If new fonts have been loaded that make a glyph matrix adjustment
9991 necessary, do it. */
9992 if (fonts_changed_p)
9993 {
9994 adjust_glyphs (NULL);
9995 ++windows_or_buffers_changed;
9996 fonts_changed_p = 0;
9997 }
9998
9999 /* If face_change_count is non-zero, init_iterator will free all
10000 realized faces, which includes the faces referenced from current
10001 matrices. So, we can't reuse current matrices in this case. */
10002 if (face_change_count)
10003 ++windows_or_buffers_changed;
10004
10005 if (! FRAME_WINDOW_P (sf)
10006 && previous_terminal_frame != sf)
10007 {
10008 /* Since frames on an ASCII terminal share the same display
10009 area, displaying a different frame means redisplay the whole
10010 thing. */
10011 windows_or_buffers_changed++;
10012 SET_FRAME_GARBAGED (sf);
10013 XSETFRAME (Vterminal_frame, sf);
10014 }
10015 previous_terminal_frame = sf;
10016
10017 /* Set the visible flags for all frames. Do this before checking
10018 for resized or garbaged frames; they want to know if their frames
10019 are visible. See the comment in frame.h for
10020 FRAME_SAMPLE_VISIBILITY. */
10021 {
10022 Lisp_Object tail, frame;
10023
10024 number_of_visible_frames = 0;
10025
10026 FOR_EACH_FRAME (tail, frame)
10027 {
10028 struct frame *f = XFRAME (frame);
10029
10030 FRAME_SAMPLE_VISIBILITY (f);
10031 if (FRAME_VISIBLE_P (f))
10032 ++number_of_visible_frames;
10033 clear_desired_matrices (f);
10034 }
10035 }
10036
10037 /* Notice any pending interrupt request to change frame size. */
10038 do_pending_window_change (1);
10039
10040 /* Clear frames marked as garbaged. */
10041 if (frame_garbaged)
10042 clear_garbaged_frames ();
10043
10044 /* Build menubar and tool-bar items. */
10045 prepare_menu_bars ();
10046
10047 if (windows_or_buffers_changed)
10048 update_mode_lines++;
10049
10050 /* Detect case that we need to write or remove a star in the mode line. */
10051 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10052 {
10053 w->update_mode_line = Qt;
10054 if (buffer_shared > 1)
10055 update_mode_lines++;
10056 }
10057
10058 /* If %c is in the mode line, update it if needed. */
10059 if (!NILP (w->column_number_displayed)
10060 /* This alternative quickly identifies a common case
10061 where no change is needed. */
10062 && !(PT == XFASTINT (w->last_point)
10063 && XFASTINT (w->last_modified) >= MODIFF
10064 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10065 && (XFASTINT (w->column_number_displayed)
10066 != (int) current_column ())) /* iftc */
10067 w->update_mode_line = Qt;
10068
10069 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10070
10071 /* The variable buffer_shared is set in redisplay_window and
10072 indicates that we redisplay a buffer in different windows. See
10073 there. */
10074 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10075 || cursor_type_changed);
10076
10077 /* If specs for an arrow have changed, do thorough redisplay
10078 to ensure we remove any arrow that should no longer exist. */
10079 if (overlay_arrows_changed_p ())
10080 consider_all_windows_p = windows_or_buffers_changed = 1;
10081
10082 /* Normally the message* functions will have already displayed and
10083 updated the echo area, but the frame may have been trashed, or
10084 the update may have been preempted, so display the echo area
10085 again here. Checking message_cleared_p captures the case that
10086 the echo area should be cleared. */
10087 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10088 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10089 || (message_cleared_p
10090 && minibuf_level == 0
10091 /* If the mini-window is currently selected, this means the
10092 echo-area doesn't show through. */
10093 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10094 {
10095 int window_height_changed_p = echo_area_display (0);
10096 must_finish = 1;
10097
10098 /* If we don't display the current message, don't clear the
10099 message_cleared_p flag, because, if we did, we wouldn't clear
10100 the echo area in the next redisplay which doesn't preserve
10101 the echo area. */
10102 if (!display_last_displayed_message_p)
10103 message_cleared_p = 0;
10104
10105 if (fonts_changed_p)
10106 goto retry;
10107 else if (window_height_changed_p)
10108 {
10109 consider_all_windows_p = 1;
10110 ++update_mode_lines;
10111 ++windows_or_buffers_changed;
10112
10113 /* If window configuration was changed, frames may have been
10114 marked garbaged. Clear them or we will experience
10115 surprises wrt scrolling. */
10116 if (frame_garbaged)
10117 clear_garbaged_frames ();
10118 }
10119 }
10120 else if (EQ (selected_window, minibuf_window)
10121 && (current_buffer->clip_changed
10122 || XFASTINT (w->last_modified) < MODIFF
10123 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10124 && resize_mini_window (w, 0))
10125 {
10126 /* Resized active mini-window to fit the size of what it is
10127 showing if its contents might have changed. */
10128 must_finish = 1;
10129 consider_all_windows_p = 1;
10130 ++windows_or_buffers_changed;
10131 ++update_mode_lines;
10132
10133 /* If window configuration was changed, frames may have been
10134 marked garbaged. Clear them or we will experience
10135 surprises wrt scrolling. */
10136 if (frame_garbaged)
10137 clear_garbaged_frames ();
10138 }
10139
10140
10141 /* If showing the region, and mark has changed, we must redisplay
10142 the whole window. The assignment to this_line_start_pos prevents
10143 the optimization directly below this if-statement. */
10144 if (((!NILP (Vtransient_mark_mode)
10145 && !NILP (XBUFFER (w->buffer)->mark_active))
10146 != !NILP (w->region_showing))
10147 || (!NILP (w->region_showing)
10148 && !EQ (w->region_showing,
10149 Fmarker_position (XBUFFER (w->buffer)->mark))))
10150 CHARPOS (this_line_start_pos) = 0;
10151
10152 /* Optimize the case that only the line containing the cursor in the
10153 selected window has changed. Variables starting with this_ are
10154 set in display_line and record information about the line
10155 containing the cursor. */
10156 tlbufpos = this_line_start_pos;
10157 tlendpos = this_line_end_pos;
10158 if (!consider_all_windows_p
10159 && CHARPOS (tlbufpos) > 0
10160 && NILP (w->update_mode_line)
10161 && !current_buffer->clip_changed
10162 && !current_buffer->prevent_redisplay_optimizations_p
10163 && FRAME_VISIBLE_P (XFRAME (w->frame))
10164 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10165 /* Make sure recorded data applies to current buffer, etc. */
10166 && this_line_buffer == current_buffer
10167 && current_buffer == XBUFFER (w->buffer)
10168 && NILP (w->force_start)
10169 && NILP (w->optional_new_start)
10170 /* Point must be on the line that we have info recorded about. */
10171 && PT >= CHARPOS (tlbufpos)
10172 && PT <= Z - CHARPOS (tlendpos)
10173 /* All text outside that line, including its final newline,
10174 must be unchanged */
10175 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10176 CHARPOS (tlendpos)))
10177 {
10178 if (CHARPOS (tlbufpos) > BEGV
10179 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10180 && (CHARPOS (tlbufpos) == ZV
10181 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10182 /* Former continuation line has disappeared by becoming empty */
10183 goto cancel;
10184 else if (XFASTINT (w->last_modified) < MODIFF
10185 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10186 || MINI_WINDOW_P (w))
10187 {
10188 /* We have to handle the case of continuation around a
10189 wide-column character (See the comment in indent.c around
10190 line 885).
10191
10192 For instance, in the following case:
10193
10194 -------- Insert --------
10195 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10196 J_I_ ==> J_I_ `^^' are cursors.
10197 ^^ ^^
10198 -------- --------
10199
10200 As we have to redraw the line above, we should goto cancel. */
10201
10202 struct it it;
10203 int line_height_before = this_line_pixel_height;
10204
10205 /* Note that start_display will handle the case that the
10206 line starting at tlbufpos is a continuation lines. */
10207 start_display (&it, w, tlbufpos);
10208
10209 /* Implementation note: It this still necessary? */
10210 if (it.current_x != this_line_start_x)
10211 goto cancel;
10212
10213 TRACE ((stderr, "trying display optimization 1\n"));
10214 w->cursor.vpos = -1;
10215 overlay_arrow_seen = 0;
10216 it.vpos = this_line_vpos;
10217 it.current_y = this_line_y;
10218 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10219 display_line (&it);
10220
10221 /* If line contains point, is not continued,
10222 and ends at same distance from eob as before, we win */
10223 if (w->cursor.vpos >= 0
10224 /* Line is not continued, otherwise this_line_start_pos
10225 would have been set to 0 in display_line. */
10226 && CHARPOS (this_line_start_pos)
10227 /* Line ends as before. */
10228 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10229 /* Line has same height as before. Otherwise other lines
10230 would have to be shifted up or down. */
10231 && this_line_pixel_height == line_height_before)
10232 {
10233 /* If this is not the window's last line, we must adjust
10234 the charstarts of the lines below. */
10235 if (it.current_y < it.last_visible_y)
10236 {
10237 struct glyph_row *row
10238 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10239 int delta, delta_bytes;
10240
10241 if (Z - CHARPOS (tlendpos) == ZV)
10242 {
10243 /* This line ends at end of (accessible part of)
10244 buffer. There is no newline to count. */
10245 delta = (Z
10246 - CHARPOS (tlendpos)
10247 - MATRIX_ROW_START_CHARPOS (row));
10248 delta_bytes = (Z_BYTE
10249 - BYTEPOS (tlendpos)
10250 - MATRIX_ROW_START_BYTEPOS (row));
10251 }
10252 else
10253 {
10254 /* This line ends in a newline. Must take
10255 account of the newline and the rest of the
10256 text that follows. */
10257 delta = (Z
10258 - CHARPOS (tlendpos)
10259 - MATRIX_ROW_START_CHARPOS (row));
10260 delta_bytes = (Z_BYTE
10261 - BYTEPOS (tlendpos)
10262 - MATRIX_ROW_START_BYTEPOS (row));
10263 }
10264
10265 increment_matrix_positions (w->current_matrix,
10266 this_line_vpos + 1,
10267 w->current_matrix->nrows,
10268 delta, delta_bytes);
10269 }
10270
10271 /* If this row displays text now but previously didn't,
10272 or vice versa, w->window_end_vpos may have to be
10273 adjusted. */
10274 if ((it.glyph_row - 1)->displays_text_p)
10275 {
10276 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10277 XSETINT (w->window_end_vpos, this_line_vpos);
10278 }
10279 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10280 && this_line_vpos > 0)
10281 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10282 w->window_end_valid = Qnil;
10283
10284 /* Update hint: No need to try to scroll in update_window. */
10285 w->desired_matrix->no_scrolling_p = 1;
10286
10287 #if GLYPH_DEBUG
10288 *w->desired_matrix->method = 0;
10289 debug_method_add (w, "optimization 1");
10290 #endif
10291 #ifdef HAVE_WINDOW_SYSTEM
10292 update_window_fringes (w, 0);
10293 #endif
10294 goto update;
10295 }
10296 else
10297 goto cancel;
10298 }
10299 else if (/* Cursor position hasn't changed. */
10300 PT == XFASTINT (w->last_point)
10301 /* Make sure the cursor was last displayed
10302 in this window. Otherwise we have to reposition it. */
10303 && 0 <= w->cursor.vpos
10304 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10305 {
10306 if (!must_finish)
10307 {
10308 do_pending_window_change (1);
10309
10310 /* We used to always goto end_of_redisplay here, but this
10311 isn't enough if we have a blinking cursor. */
10312 if (w->cursor_off_p == w->last_cursor_off_p)
10313 goto end_of_redisplay;
10314 }
10315 goto update;
10316 }
10317 /* If highlighting the region, or if the cursor is in the echo area,
10318 then we can't just move the cursor. */
10319 else if (! (!NILP (Vtransient_mark_mode)
10320 && !NILP (current_buffer->mark_active))
10321 && (EQ (selected_window, current_buffer->last_selected_window)
10322 || highlight_nonselected_windows)
10323 && NILP (w->region_showing)
10324 && NILP (Vshow_trailing_whitespace)
10325 && !cursor_in_echo_area)
10326 {
10327 struct it it;
10328 struct glyph_row *row;
10329
10330 /* Skip from tlbufpos to PT and see where it is. Note that
10331 PT may be in invisible text. If so, we will end at the
10332 next visible position. */
10333 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10334 NULL, DEFAULT_FACE_ID);
10335 it.current_x = this_line_start_x;
10336 it.current_y = this_line_y;
10337 it.vpos = this_line_vpos;
10338
10339 /* The call to move_it_to stops in front of PT, but
10340 moves over before-strings. */
10341 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10342
10343 if (it.vpos == this_line_vpos
10344 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10345 row->enabled_p))
10346 {
10347 xassert (this_line_vpos == it.vpos);
10348 xassert (this_line_y == it.current_y);
10349 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10350 #if GLYPH_DEBUG
10351 *w->desired_matrix->method = 0;
10352 debug_method_add (w, "optimization 3");
10353 #endif
10354 goto update;
10355 }
10356 else
10357 goto cancel;
10358 }
10359
10360 cancel:
10361 /* Text changed drastically or point moved off of line. */
10362 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10363 }
10364
10365 CHARPOS (this_line_start_pos) = 0;
10366 consider_all_windows_p |= buffer_shared > 1;
10367 ++clear_face_cache_count;
10368
10369
10370 /* Build desired matrices, and update the display. If
10371 consider_all_windows_p is non-zero, do it for all windows on all
10372 frames. Otherwise do it for selected_window, only. */
10373
10374 if (consider_all_windows_p)
10375 {
10376 Lisp_Object tail, frame;
10377 int i, n = 0, size = 50;
10378 struct frame **updated
10379 = (struct frame **) alloca (size * sizeof *updated);
10380
10381 /* Clear the face cache eventually. */
10382 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10383 {
10384 clear_face_cache (0);
10385 clear_face_cache_count = 0;
10386 }
10387
10388 /* Recompute # windows showing selected buffer. This will be
10389 incremented each time such a window is displayed. */
10390 buffer_shared = 0;
10391
10392 FOR_EACH_FRAME (tail, frame)
10393 {
10394 struct frame *f = XFRAME (frame);
10395
10396 if (FRAME_WINDOW_P (f) || f == sf)
10397 {
10398 if (! EQ (frame, selected_frame))
10399 /* Select the frame, for the sake of frame-local
10400 variables. */
10401 select_frame_for_redisplay (frame);
10402
10403 #ifdef HAVE_WINDOW_SYSTEM
10404 if (clear_face_cache_count % 50 == 0
10405 && FRAME_WINDOW_P (f))
10406 clear_image_cache (f, 0);
10407 #endif /* HAVE_WINDOW_SYSTEM */
10408
10409 /* Mark all the scroll bars to be removed; we'll redeem
10410 the ones we want when we redisplay their windows. */
10411 if (condemn_scroll_bars_hook)
10412 condemn_scroll_bars_hook (f);
10413
10414 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10415 redisplay_windows (FRAME_ROOT_WINDOW (f));
10416
10417 /* Any scroll bars which redisplay_windows should have
10418 nuked should now go away. */
10419 if (judge_scroll_bars_hook)
10420 judge_scroll_bars_hook (f);
10421
10422 /* If fonts changed, display again. */
10423 /* ??? rms: I suspect it is a mistake to jump all the way
10424 back to retry here. It should just retry this frame. */
10425 if (fonts_changed_p)
10426 goto retry;
10427
10428 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10429 {
10430 /* See if we have to hscroll. */
10431 if (hscroll_windows (f->root_window))
10432 goto retry;
10433
10434 /* Prevent various kinds of signals during display
10435 update. stdio is not robust about handling
10436 signals, which can cause an apparent I/O
10437 error. */
10438 if (interrupt_input)
10439 unrequest_sigio ();
10440 STOP_POLLING;
10441
10442 /* Update the display. */
10443 set_window_update_flags (XWINDOW (f->root_window), 1);
10444 pause |= update_frame (f, 0, 0);
10445 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10446 if (pause)
10447 break;
10448 #endif
10449
10450 if (n == size)
10451 {
10452 int nbytes = size * sizeof *updated;
10453 struct frame **p = (struct frame **) alloca (2 * nbytes);
10454 bcopy (updated, p, nbytes);
10455 size *= 2;
10456 }
10457
10458 updated[n++] = f;
10459 }
10460 }
10461 }
10462
10463 if (!pause)
10464 {
10465 /* Do the mark_window_display_accurate after all windows have
10466 been redisplayed because this call resets flags in buffers
10467 which are needed for proper redisplay. */
10468 for (i = 0; i < n; ++i)
10469 {
10470 struct frame *f = updated[i];
10471 mark_window_display_accurate (f->root_window, 1);
10472 if (frame_up_to_date_hook)
10473 frame_up_to_date_hook (f);
10474 }
10475 }
10476 }
10477 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10478 {
10479 Lisp_Object mini_window;
10480 struct frame *mini_frame;
10481
10482 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10483 /* Use list_of_error, not Qerror, so that
10484 we catch only errors and don't run the debugger. */
10485 internal_condition_case_1 (redisplay_window_1, selected_window,
10486 list_of_error,
10487 redisplay_window_error);
10488
10489 /* Compare desired and current matrices, perform output. */
10490
10491 update:
10492 /* If fonts changed, display again. */
10493 if (fonts_changed_p)
10494 goto retry;
10495
10496 /* Prevent various kinds of signals during display update.
10497 stdio is not robust about handling signals,
10498 which can cause an apparent I/O error. */
10499 if (interrupt_input)
10500 unrequest_sigio ();
10501 STOP_POLLING;
10502
10503 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10504 {
10505 if (hscroll_windows (selected_window))
10506 goto retry;
10507
10508 XWINDOW (selected_window)->must_be_updated_p = 1;
10509 pause = update_frame (sf, 0, 0);
10510 }
10511
10512 /* We may have called echo_area_display at the top of this
10513 function. If the echo area is on another frame, that may
10514 have put text on a frame other than the selected one, so the
10515 above call to update_frame would not have caught it. Catch
10516 it here. */
10517 mini_window = FRAME_MINIBUF_WINDOW (sf);
10518 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10519
10520 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10521 {
10522 XWINDOW (mini_window)->must_be_updated_p = 1;
10523 pause |= update_frame (mini_frame, 0, 0);
10524 if (!pause && hscroll_windows (mini_window))
10525 goto retry;
10526 }
10527 }
10528
10529 /* If display was paused because of pending input, make sure we do a
10530 thorough update the next time. */
10531 if (pause)
10532 {
10533 /* Prevent the optimization at the beginning of
10534 redisplay_internal that tries a single-line update of the
10535 line containing the cursor in the selected window. */
10536 CHARPOS (this_line_start_pos) = 0;
10537
10538 /* Let the overlay arrow be updated the next time. */
10539 update_overlay_arrows (0);
10540
10541 /* If we pause after scrolling, some rows in the current
10542 matrices of some windows are not valid. */
10543 if (!WINDOW_FULL_WIDTH_P (w)
10544 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10545 update_mode_lines = 1;
10546 }
10547 else
10548 {
10549 if (!consider_all_windows_p)
10550 {
10551 /* This has already been done above if
10552 consider_all_windows_p is set. */
10553 mark_window_display_accurate_1 (w, 1);
10554
10555 /* Say overlay arrows are up to date. */
10556 update_overlay_arrows (1);
10557
10558 if (frame_up_to_date_hook != 0)
10559 frame_up_to_date_hook (sf);
10560 }
10561
10562 update_mode_lines = 0;
10563 windows_or_buffers_changed = 0;
10564 cursor_type_changed = 0;
10565 }
10566
10567 /* Start SIGIO interrupts coming again. Having them off during the
10568 code above makes it less likely one will discard output, but not
10569 impossible, since there might be stuff in the system buffer here.
10570 But it is much hairier to try to do anything about that. */
10571 if (interrupt_input)
10572 request_sigio ();
10573 RESUME_POLLING;
10574
10575 /* If a frame has become visible which was not before, redisplay
10576 again, so that we display it. Expose events for such a frame
10577 (which it gets when becoming visible) don't call the parts of
10578 redisplay constructing glyphs, so simply exposing a frame won't
10579 display anything in this case. So, we have to display these
10580 frames here explicitly. */
10581 if (!pause)
10582 {
10583 Lisp_Object tail, frame;
10584 int new_count = 0;
10585
10586 FOR_EACH_FRAME (tail, frame)
10587 {
10588 int this_is_visible = 0;
10589
10590 if (XFRAME (frame)->visible)
10591 this_is_visible = 1;
10592 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10593 if (XFRAME (frame)->visible)
10594 this_is_visible = 1;
10595
10596 if (this_is_visible)
10597 new_count++;
10598 }
10599
10600 if (new_count != number_of_visible_frames)
10601 windows_or_buffers_changed++;
10602 }
10603
10604 /* Change frame size now if a change is pending. */
10605 do_pending_window_change (1);
10606
10607 /* If we just did a pending size change, or have additional
10608 visible frames, redisplay again. */
10609 if (windows_or_buffers_changed && !pause)
10610 goto retry;
10611
10612 end_of_redisplay:
10613 unbind_to (count, Qnil);
10614 RESUME_POLLING;
10615 }
10616
10617
10618 /* Redisplay, but leave alone any recent echo area message unless
10619 another message has been requested in its place.
10620
10621 This is useful in situations where you need to redisplay but no
10622 user action has occurred, making it inappropriate for the message
10623 area to be cleared. See tracking_off and
10624 wait_reading_process_output for examples of these situations.
10625
10626 FROM_WHERE is an integer saying from where this function was
10627 called. This is useful for debugging. */
10628
10629 void
10630 redisplay_preserve_echo_area (from_where)
10631 int from_where;
10632 {
10633 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10634
10635 if (!NILP (echo_area_buffer[1]))
10636 {
10637 /* We have a previously displayed message, but no current
10638 message. Redisplay the previous message. */
10639 display_last_displayed_message_p = 1;
10640 redisplay_internal (1);
10641 display_last_displayed_message_p = 0;
10642 }
10643 else
10644 redisplay_internal (1);
10645
10646 if (rif != NULL && rif->flush_display_optional)
10647 rif->flush_display_optional (NULL);
10648 }
10649
10650
10651 /* Function registered with record_unwind_protect in
10652 redisplay_internal. Reset redisplaying_p to the value it had
10653 before redisplay_internal was called, and clear
10654 prevent_freeing_realized_faces_p. It also selects the previously
10655 selected frame. */
10656
10657 static Lisp_Object
10658 unwind_redisplay (val)
10659 Lisp_Object val;
10660 {
10661 Lisp_Object old_redisplaying_p, old_frame;
10662
10663 old_redisplaying_p = XCAR (val);
10664 redisplaying_p = XFASTINT (old_redisplaying_p);
10665 old_frame = XCDR (val);
10666 if (! EQ (old_frame, selected_frame))
10667 select_frame_for_redisplay (old_frame);
10668 return Qnil;
10669 }
10670
10671
10672 /* Mark the display of window W as accurate or inaccurate. If
10673 ACCURATE_P is non-zero mark display of W as accurate. If
10674 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10675 redisplay_internal is called. */
10676
10677 static void
10678 mark_window_display_accurate_1 (w, accurate_p)
10679 struct window *w;
10680 int accurate_p;
10681 {
10682 if (BUFFERP (w->buffer))
10683 {
10684 struct buffer *b = XBUFFER (w->buffer);
10685
10686 w->last_modified
10687 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10688 w->last_overlay_modified
10689 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10690 w->last_had_star
10691 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10692
10693 if (accurate_p)
10694 {
10695 b->clip_changed = 0;
10696 b->prevent_redisplay_optimizations_p = 0;
10697
10698 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10699 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10700 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10701 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10702
10703 w->current_matrix->buffer = b;
10704 w->current_matrix->begv = BUF_BEGV (b);
10705 w->current_matrix->zv = BUF_ZV (b);
10706
10707 w->last_cursor = w->cursor;
10708 w->last_cursor_off_p = w->cursor_off_p;
10709
10710 if (w == XWINDOW (selected_window))
10711 w->last_point = make_number (BUF_PT (b));
10712 else
10713 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10714 }
10715 }
10716
10717 if (accurate_p)
10718 {
10719 w->window_end_valid = w->buffer;
10720 #if 0 /* This is incorrect with variable-height lines. */
10721 xassert (XINT (w->window_end_vpos)
10722 < (WINDOW_TOTAL_LINES (w)
10723 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10724 #endif
10725 w->update_mode_line = Qnil;
10726 }
10727 }
10728
10729
10730 /* Mark the display of windows in the window tree rooted at WINDOW as
10731 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10732 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10733 be redisplayed the next time redisplay_internal is called. */
10734
10735 void
10736 mark_window_display_accurate (window, accurate_p)
10737 Lisp_Object window;
10738 int accurate_p;
10739 {
10740 struct window *w;
10741
10742 for (; !NILP (window); window = w->next)
10743 {
10744 w = XWINDOW (window);
10745 mark_window_display_accurate_1 (w, accurate_p);
10746
10747 if (!NILP (w->vchild))
10748 mark_window_display_accurate (w->vchild, accurate_p);
10749 if (!NILP (w->hchild))
10750 mark_window_display_accurate (w->hchild, accurate_p);
10751 }
10752
10753 if (accurate_p)
10754 {
10755 update_overlay_arrows (1);
10756 }
10757 else
10758 {
10759 /* Force a thorough redisplay the next time by setting
10760 last_arrow_position and last_arrow_string to t, which is
10761 unequal to any useful value of Voverlay_arrow_... */
10762 update_overlay_arrows (-1);
10763 }
10764 }
10765
10766
10767 /* Return value in display table DP (Lisp_Char_Table *) for character
10768 C. Since a display table doesn't have any parent, we don't have to
10769 follow parent. Do not call this function directly but use the
10770 macro DISP_CHAR_VECTOR. */
10771
10772 Lisp_Object
10773 disp_char_vector (dp, c)
10774 struct Lisp_Char_Table *dp;
10775 int c;
10776 {
10777 int code[4], i;
10778 Lisp_Object val;
10779
10780 if (SINGLE_BYTE_CHAR_P (c))
10781 return (dp->contents[c]);
10782
10783 SPLIT_CHAR (c, code[0], code[1], code[2]);
10784 if (code[1] < 32)
10785 code[1] = -1;
10786 else if (code[2] < 32)
10787 code[2] = -1;
10788
10789 /* Here, the possible range of code[0] (== charset ID) is
10790 128..max_charset. Since the top level char table contains data
10791 for multibyte characters after 256th element, we must increment
10792 code[0] by 128 to get a correct index. */
10793 code[0] += 128;
10794 code[3] = -1; /* anchor */
10795
10796 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10797 {
10798 val = dp->contents[code[i]];
10799 if (!SUB_CHAR_TABLE_P (val))
10800 return (NILP (val) ? dp->defalt : val);
10801 }
10802
10803 /* Here, val is a sub char table. We return the default value of
10804 it. */
10805 return (dp->defalt);
10806 }
10807
10808
10809 \f
10810 /***********************************************************************
10811 Window Redisplay
10812 ***********************************************************************/
10813
10814 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10815
10816 static void
10817 redisplay_windows (window)
10818 Lisp_Object window;
10819 {
10820 while (!NILP (window))
10821 {
10822 struct window *w = XWINDOW (window);
10823
10824 if (!NILP (w->hchild))
10825 redisplay_windows (w->hchild);
10826 else if (!NILP (w->vchild))
10827 redisplay_windows (w->vchild);
10828 else
10829 {
10830 displayed_buffer = XBUFFER (w->buffer);
10831 /* Use list_of_error, not Qerror, so that
10832 we catch only errors and don't run the debugger. */
10833 internal_condition_case_1 (redisplay_window_0, window,
10834 list_of_error,
10835 redisplay_window_error);
10836 }
10837
10838 window = w->next;
10839 }
10840 }
10841
10842 static Lisp_Object
10843 redisplay_window_error ()
10844 {
10845 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
10846 return Qnil;
10847 }
10848
10849 static Lisp_Object
10850 redisplay_window_0 (window)
10851 Lisp_Object window;
10852 {
10853 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10854 redisplay_window (window, 0);
10855 return Qnil;
10856 }
10857
10858 static Lisp_Object
10859 redisplay_window_1 (window)
10860 Lisp_Object window;
10861 {
10862 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
10863 redisplay_window (window, 1);
10864 return Qnil;
10865 }
10866 \f
10867
10868 /* Increment GLYPH until it reaches END or CONDITION fails while
10869 adding (GLYPH)->pixel_width to X. */
10870
10871 #define SKIP_GLYPHS(glyph, end, x, condition) \
10872 do \
10873 { \
10874 (x) += (glyph)->pixel_width; \
10875 ++(glyph); \
10876 } \
10877 while ((glyph) < (end) && (condition))
10878
10879
10880 /* Set cursor position of W. PT is assumed to be displayed in ROW.
10881 DELTA is the number of bytes by which positions recorded in ROW
10882 differ from current buffer positions. */
10883
10884 void
10885 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
10886 struct window *w;
10887 struct glyph_row *row;
10888 struct glyph_matrix *matrix;
10889 int delta, delta_bytes, dy, dvpos;
10890 {
10891 struct glyph *glyph = row->glyphs[TEXT_AREA];
10892 struct glyph *end = glyph + row->used[TEXT_AREA];
10893 struct glyph *cursor = NULL;
10894 /* The first glyph that starts a sequence of glyphs from string. */
10895 struct glyph *string_start;
10896 /* The X coordinate of string_start. */
10897 int string_start_x;
10898 /* The last known character position. */
10899 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
10900 /* The last known character position before string_start. */
10901 int string_before_pos;
10902 int x = row->x;
10903 int cursor_x = x;
10904 int cursor_from_overlay_pos = 0;
10905 int pt_old = PT - delta;
10906
10907 /* Skip over glyphs not having an object at the start of the row.
10908 These are special glyphs like truncation marks on terminal
10909 frames. */
10910 if (row->displays_text_p)
10911 while (glyph < end
10912 && INTEGERP (glyph->object)
10913 && glyph->charpos < 0)
10914 {
10915 x += glyph->pixel_width;
10916 ++glyph;
10917 }
10918
10919 string_start = NULL;
10920 while (glyph < end
10921 && !INTEGERP (glyph->object)
10922 && (!BUFFERP (glyph->object)
10923 || (last_pos = glyph->charpos) < pt_old))
10924 {
10925 if (! STRINGP (glyph->object))
10926 {
10927 string_start = NULL;
10928 x += glyph->pixel_width;
10929 ++glyph;
10930 if (cursor_from_overlay_pos
10931 && last_pos > cursor_from_overlay_pos)
10932 {
10933 cursor_from_overlay_pos = 0;
10934 cursor = 0;
10935 }
10936 }
10937 else
10938 {
10939 string_before_pos = last_pos;
10940 string_start = glyph;
10941 string_start_x = x;
10942 /* Skip all glyphs from string. */
10943 do
10944 {
10945 int pos;
10946 if ((cursor == NULL || glyph > cursor)
10947 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
10948 Qcursor, (glyph)->object))
10949 && (pos = string_buffer_position (w, glyph->object,
10950 string_before_pos),
10951 (pos == 0 /* From overlay */
10952 || pos == pt_old)))
10953 {
10954 /* Estimate overlay buffer position from the buffer
10955 positions of the glyphs before and after the overlay.
10956 Add 1 to last_pos so that if point corresponds to the
10957 glyph right after the overlay, we still use a 'cursor'
10958 property found in that overlay. */
10959 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
10960 cursor = glyph;
10961 cursor_x = x;
10962 }
10963 x += glyph->pixel_width;
10964 ++glyph;
10965 }
10966 while (glyph < end && STRINGP (glyph->object));
10967 }
10968 }
10969
10970 if (cursor != NULL)
10971 {
10972 glyph = cursor;
10973 x = cursor_x;
10974 }
10975 else if (row->ends_in_ellipsis_p && glyph == end)
10976 {
10977 /* Scan back over the ellipsis glyphs, decrementing positions. */
10978 while (glyph > row->glyphs[TEXT_AREA]
10979 && (glyph - 1)->charpos == last_pos)
10980 glyph--, x -= glyph->pixel_width;
10981 /* That loop always goes one position too far,
10982 including the glyph before the ellipsis.
10983 So scan forward over that one. */
10984 x += glyph->pixel_width;
10985 glyph++;
10986 }
10987 else if (string_start
10988 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
10989 {
10990 /* We may have skipped over point because the previous glyphs
10991 are from string. As there's no easy way to know the
10992 character position of the current glyph, find the correct
10993 glyph on point by scanning from string_start again. */
10994 Lisp_Object limit;
10995 Lisp_Object string;
10996 int pos;
10997
10998 limit = make_number (pt_old + 1);
10999 end = glyph;
11000 glyph = string_start;
11001 x = string_start_x;
11002 string = glyph->object;
11003 pos = string_buffer_position (w, string, string_before_pos);
11004 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11005 because we always put cursor after overlay strings. */
11006 while (pos == 0 && glyph < end)
11007 {
11008 string = glyph->object;
11009 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11010 if (glyph < end)
11011 pos = string_buffer_position (w, glyph->object, string_before_pos);
11012 }
11013
11014 while (glyph < end)
11015 {
11016 pos = XINT (Fnext_single_char_property_change
11017 (make_number (pos), Qdisplay, Qnil, limit));
11018 if (pos > pt_old)
11019 break;
11020 /* Skip glyphs from the same string. */
11021 string = glyph->object;
11022 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11023 /* Skip glyphs from an overlay. */
11024 while (glyph < end
11025 && ! string_buffer_position (w, glyph->object, pos))
11026 {
11027 string = glyph->object;
11028 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11029 }
11030 }
11031 }
11032
11033 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11034 w->cursor.x = x;
11035 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11036 w->cursor.y = row->y + dy;
11037
11038 if (w == XWINDOW (selected_window))
11039 {
11040 if (!row->continued_p
11041 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11042 && row->x == 0)
11043 {
11044 this_line_buffer = XBUFFER (w->buffer);
11045
11046 CHARPOS (this_line_start_pos)
11047 = MATRIX_ROW_START_CHARPOS (row) + delta;
11048 BYTEPOS (this_line_start_pos)
11049 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11050
11051 CHARPOS (this_line_end_pos)
11052 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11053 BYTEPOS (this_line_end_pos)
11054 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11055
11056 this_line_y = w->cursor.y;
11057 this_line_pixel_height = row->height;
11058 this_line_vpos = w->cursor.vpos;
11059 this_line_start_x = row->x;
11060 }
11061 else
11062 CHARPOS (this_line_start_pos) = 0;
11063 }
11064 }
11065
11066
11067 /* Run window scroll functions, if any, for WINDOW with new window
11068 start STARTP. Sets the window start of WINDOW to that position.
11069
11070 We assume that the window's buffer is really current. */
11071
11072 static INLINE struct text_pos
11073 run_window_scroll_functions (window, startp)
11074 Lisp_Object window;
11075 struct text_pos startp;
11076 {
11077 struct window *w = XWINDOW (window);
11078 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11079
11080 if (current_buffer != XBUFFER (w->buffer))
11081 abort ();
11082
11083 if (!NILP (Vwindow_scroll_functions))
11084 {
11085 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11086 make_number (CHARPOS (startp)));
11087 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11088 /* In case the hook functions switch buffers. */
11089 if (current_buffer != XBUFFER (w->buffer))
11090 set_buffer_internal_1 (XBUFFER (w->buffer));
11091 }
11092
11093 return startp;
11094 }
11095
11096
11097 /* Make sure the line containing the cursor is fully visible.
11098 A value of 1 means there is nothing to be done.
11099 (Either the line is fully visible, or it cannot be made so,
11100 or we cannot tell.)
11101
11102 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11103 is higher than window.
11104
11105 A value of 0 means the caller should do scrolling
11106 as if point had gone off the screen. */
11107
11108 static int
11109 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11110 struct window *w;
11111 int force_p;
11112 {
11113 struct glyph_matrix *matrix;
11114 struct glyph_row *row;
11115 int window_height;
11116
11117 if (!make_cursor_line_fully_visible_p)
11118 return 1;
11119
11120 /* It's not always possible to find the cursor, e.g, when a window
11121 is full of overlay strings. Don't do anything in that case. */
11122 if (w->cursor.vpos < 0)
11123 return 1;
11124
11125 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11126 row = MATRIX_ROW (matrix, w->cursor.vpos);
11127
11128 /* If the cursor row is not partially visible, there's nothing to do. */
11129 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11130 return 1;
11131
11132 /* If the row the cursor is in is taller than the window's height,
11133 it's not clear what to do, so do nothing. */
11134 window_height = window_box_height (w);
11135 if (row->height >= window_height)
11136 {
11137 if (!force_p || w->vscroll)
11138 return 1;
11139 }
11140 return 0;
11141
11142 #if 0
11143 /* This code used to try to scroll the window just enough to make
11144 the line visible. It returned 0 to say that the caller should
11145 allocate larger glyph matrices. */
11146
11147 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11148 {
11149 int dy = row->height - row->visible_height;
11150 w->vscroll = 0;
11151 w->cursor.y += dy;
11152 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11153 }
11154 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11155 {
11156 int dy = - (row->height - row->visible_height);
11157 w->vscroll = dy;
11158 w->cursor.y += dy;
11159 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11160 }
11161
11162 /* When we change the cursor y-position of the selected window,
11163 change this_line_y as well so that the display optimization for
11164 the cursor line of the selected window in redisplay_internal uses
11165 the correct y-position. */
11166 if (w == XWINDOW (selected_window))
11167 this_line_y = w->cursor.y;
11168
11169 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11170 redisplay with larger matrices. */
11171 if (matrix->nrows < required_matrix_height (w))
11172 {
11173 fonts_changed_p = 1;
11174 return 0;
11175 }
11176
11177 return 1;
11178 #endif /* 0 */
11179 }
11180
11181
11182 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11183 non-zero means only WINDOW is redisplayed in redisplay_internal.
11184 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11185 in redisplay_window to bring a partially visible line into view in
11186 the case that only the cursor has moved.
11187
11188 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11189 last screen line's vertical height extends past the end of the screen.
11190
11191 Value is
11192
11193 1 if scrolling succeeded
11194
11195 0 if scrolling didn't find point.
11196
11197 -1 if new fonts have been loaded so that we must interrupt
11198 redisplay, adjust glyph matrices, and try again. */
11199
11200 enum
11201 {
11202 SCROLLING_SUCCESS,
11203 SCROLLING_FAILED,
11204 SCROLLING_NEED_LARGER_MATRICES
11205 };
11206
11207 static int
11208 try_scrolling (window, just_this_one_p, scroll_conservatively,
11209 scroll_step, temp_scroll_step, last_line_misfit)
11210 Lisp_Object window;
11211 int just_this_one_p;
11212 EMACS_INT scroll_conservatively, scroll_step;
11213 int temp_scroll_step;
11214 int last_line_misfit;
11215 {
11216 struct window *w = XWINDOW (window);
11217 struct frame *f = XFRAME (w->frame);
11218 struct text_pos scroll_margin_pos;
11219 struct text_pos pos;
11220 struct text_pos startp;
11221 struct it it;
11222 Lisp_Object window_end;
11223 int this_scroll_margin;
11224 int dy = 0;
11225 int scroll_max;
11226 int rc;
11227 int amount_to_scroll = 0;
11228 Lisp_Object aggressive;
11229 int height;
11230 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11231
11232 #if GLYPH_DEBUG
11233 debug_method_add (w, "try_scrolling");
11234 #endif
11235
11236 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11237
11238 /* Compute scroll margin height in pixels. We scroll when point is
11239 within this distance from the top or bottom of the window. */
11240 if (scroll_margin > 0)
11241 {
11242 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11243 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11244 }
11245 else
11246 this_scroll_margin = 0;
11247
11248 /* Force scroll_conservatively to have a reasonable value so it doesn't
11249 cause an overflow while computing how much to scroll. */
11250 if (scroll_conservatively)
11251 scroll_conservatively = min (scroll_conservatively,
11252 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11253
11254 /* Compute how much we should try to scroll maximally to bring point
11255 into view. */
11256 if (scroll_step || scroll_conservatively || temp_scroll_step)
11257 scroll_max = max (scroll_step,
11258 max (scroll_conservatively, temp_scroll_step));
11259 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11260 || NUMBERP (current_buffer->scroll_up_aggressively))
11261 /* We're trying to scroll because of aggressive scrolling
11262 but no scroll_step is set. Choose an arbitrary one. Maybe
11263 there should be a variable for this. */
11264 scroll_max = 10;
11265 else
11266 scroll_max = 0;
11267 scroll_max *= FRAME_LINE_HEIGHT (f);
11268
11269 /* Decide whether we have to scroll down. Start at the window end
11270 and move this_scroll_margin up to find the position of the scroll
11271 margin. */
11272 window_end = Fwindow_end (window, Qt);
11273
11274 too_near_end:
11275
11276 CHARPOS (scroll_margin_pos) = XINT (window_end);
11277 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11278
11279 if (this_scroll_margin || extra_scroll_margin_lines)
11280 {
11281 start_display (&it, w, scroll_margin_pos);
11282 if (this_scroll_margin)
11283 move_it_vertically_backward (&it, this_scroll_margin);
11284 if (extra_scroll_margin_lines)
11285 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11286 scroll_margin_pos = it.current.pos;
11287 }
11288
11289 if (PT >= CHARPOS (scroll_margin_pos))
11290 {
11291 int y0;
11292
11293 /* Point is in the scroll margin at the bottom of the window, or
11294 below. Compute a new window start that makes point visible. */
11295
11296 /* Compute the distance from the scroll margin to PT.
11297 Give up if the distance is greater than scroll_max. */
11298 start_display (&it, w, scroll_margin_pos);
11299 y0 = it.current_y;
11300 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11301 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11302
11303 /* To make point visible, we have to move the window start
11304 down so that the line the cursor is in is visible, which
11305 means we have to add in the height of the cursor line. */
11306 dy = line_bottom_y (&it) - y0;
11307
11308 if (dy > scroll_max)
11309 return SCROLLING_FAILED;
11310
11311 /* Move the window start down. If scrolling conservatively,
11312 move it just enough down to make point visible. If
11313 scroll_step is set, move it down by scroll_step. */
11314 start_display (&it, w, startp);
11315
11316 if (scroll_conservatively)
11317 /* Set AMOUNT_TO_SCROLL to at least one line,
11318 and at most scroll_conservatively lines. */
11319 amount_to_scroll
11320 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11321 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11322 else if (scroll_step || temp_scroll_step)
11323 amount_to_scroll = scroll_max;
11324 else
11325 {
11326 aggressive = current_buffer->scroll_up_aggressively;
11327 height = WINDOW_BOX_TEXT_HEIGHT (w);
11328 if (NUMBERP (aggressive))
11329 {
11330 double float_amount = XFLOATINT (aggressive) * height;
11331 amount_to_scroll = float_amount;
11332 if (amount_to_scroll == 0 && float_amount > 0)
11333 amount_to_scroll = 1;
11334 }
11335 }
11336
11337 if (amount_to_scroll <= 0)
11338 return SCROLLING_FAILED;
11339
11340 /* If moving by amount_to_scroll leaves STARTP unchanged,
11341 move it down one screen line. */
11342
11343 move_it_vertically (&it, amount_to_scroll);
11344 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11345 move_it_by_lines (&it, 1, 1);
11346 startp = it.current.pos;
11347 }
11348 else
11349 {
11350 /* See if point is inside the scroll margin at the top of the
11351 window. */
11352 scroll_margin_pos = startp;
11353 if (this_scroll_margin)
11354 {
11355 start_display (&it, w, startp);
11356 move_it_vertically (&it, this_scroll_margin);
11357 scroll_margin_pos = it.current.pos;
11358 }
11359
11360 if (PT < CHARPOS (scroll_margin_pos))
11361 {
11362 /* Point is in the scroll margin at the top of the window or
11363 above what is displayed in the window. */
11364 int y0;
11365
11366 /* Compute the vertical distance from PT to the scroll
11367 margin position. Give up if distance is greater than
11368 scroll_max. */
11369 SET_TEXT_POS (pos, PT, PT_BYTE);
11370 start_display (&it, w, pos);
11371 y0 = it.current_y;
11372 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11373 it.last_visible_y, -1,
11374 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11375 dy = it.current_y - y0;
11376 if (dy > scroll_max)
11377 return SCROLLING_FAILED;
11378
11379 /* Compute new window start. */
11380 start_display (&it, w, startp);
11381
11382 if (scroll_conservatively)
11383 amount_to_scroll
11384 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11385 else if (scroll_step || temp_scroll_step)
11386 amount_to_scroll = scroll_max;
11387 else
11388 {
11389 aggressive = current_buffer->scroll_down_aggressively;
11390 height = WINDOW_BOX_TEXT_HEIGHT (w);
11391 if (NUMBERP (aggressive))
11392 {
11393 double float_amount = XFLOATINT (aggressive) * height;
11394 amount_to_scroll = float_amount;
11395 if (amount_to_scroll == 0 && float_amount > 0)
11396 amount_to_scroll = 1;
11397 }
11398 }
11399
11400 if (amount_to_scroll <= 0)
11401 return SCROLLING_FAILED;
11402
11403 move_it_vertically_backward (&it, amount_to_scroll);
11404 startp = it.current.pos;
11405 }
11406 }
11407
11408 /* Run window scroll functions. */
11409 startp = run_window_scroll_functions (window, startp);
11410
11411 /* Display the window. Give up if new fonts are loaded, or if point
11412 doesn't appear. */
11413 if (!try_window (window, startp))
11414 rc = SCROLLING_NEED_LARGER_MATRICES;
11415 else if (w->cursor.vpos < 0)
11416 {
11417 clear_glyph_matrix (w->desired_matrix);
11418 rc = SCROLLING_FAILED;
11419 }
11420 else
11421 {
11422 /* Maybe forget recorded base line for line number display. */
11423 if (!just_this_one_p
11424 || current_buffer->clip_changed
11425 || BEG_UNCHANGED < CHARPOS (startp))
11426 w->base_line_number = Qnil;
11427
11428 /* If cursor ends up on a partially visible line,
11429 treat that as being off the bottom of the screen. */
11430 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11431 {
11432 clear_glyph_matrix (w->desired_matrix);
11433 ++extra_scroll_margin_lines;
11434 goto too_near_end;
11435 }
11436 rc = SCROLLING_SUCCESS;
11437 }
11438
11439 return rc;
11440 }
11441
11442
11443 /* Compute a suitable window start for window W if display of W starts
11444 on a continuation line. Value is non-zero if a new window start
11445 was computed.
11446
11447 The new window start will be computed, based on W's width, starting
11448 from the start of the continued line. It is the start of the
11449 screen line with the minimum distance from the old start W->start. */
11450
11451 static int
11452 compute_window_start_on_continuation_line (w)
11453 struct window *w;
11454 {
11455 struct text_pos pos, start_pos;
11456 int window_start_changed_p = 0;
11457
11458 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11459
11460 /* If window start is on a continuation line... Window start may be
11461 < BEGV in case there's invisible text at the start of the
11462 buffer (M-x rmail, for example). */
11463 if (CHARPOS (start_pos) > BEGV
11464 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11465 {
11466 struct it it;
11467 struct glyph_row *row;
11468
11469 /* Handle the case that the window start is out of range. */
11470 if (CHARPOS (start_pos) < BEGV)
11471 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11472 else if (CHARPOS (start_pos) > ZV)
11473 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11474
11475 /* Find the start of the continued line. This should be fast
11476 because scan_buffer is fast (newline cache). */
11477 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11478 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11479 row, DEFAULT_FACE_ID);
11480 reseat_at_previous_visible_line_start (&it);
11481
11482 /* If the line start is "too far" away from the window start,
11483 say it takes too much time to compute a new window start. */
11484 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11485 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11486 {
11487 int min_distance, distance;
11488
11489 /* Move forward by display lines to find the new window
11490 start. If window width was enlarged, the new start can
11491 be expected to be > the old start. If window width was
11492 decreased, the new window start will be < the old start.
11493 So, we're looking for the display line start with the
11494 minimum distance from the old window start. */
11495 pos = it.current.pos;
11496 min_distance = INFINITY;
11497 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11498 distance < min_distance)
11499 {
11500 min_distance = distance;
11501 pos = it.current.pos;
11502 move_it_by_lines (&it, 1, 0);
11503 }
11504
11505 /* Set the window start there. */
11506 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11507 window_start_changed_p = 1;
11508 }
11509 }
11510
11511 return window_start_changed_p;
11512 }
11513
11514
11515 /* Try cursor movement in case text has not changed in window WINDOW,
11516 with window start STARTP. Value is
11517
11518 CURSOR_MOVEMENT_SUCCESS if successful
11519
11520 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11521
11522 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11523 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11524 we want to scroll as if scroll-step were set to 1. See the code.
11525
11526 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11527 which case we have to abort this redisplay, and adjust matrices
11528 first. */
11529
11530 enum
11531 {
11532 CURSOR_MOVEMENT_SUCCESS,
11533 CURSOR_MOVEMENT_CANNOT_BE_USED,
11534 CURSOR_MOVEMENT_MUST_SCROLL,
11535 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11536 };
11537
11538 static int
11539 try_cursor_movement (window, startp, scroll_step)
11540 Lisp_Object window;
11541 struct text_pos startp;
11542 int *scroll_step;
11543 {
11544 struct window *w = XWINDOW (window);
11545 struct frame *f = XFRAME (w->frame);
11546 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11547
11548 #if GLYPH_DEBUG
11549 if (inhibit_try_cursor_movement)
11550 return rc;
11551 #endif
11552
11553 /* Handle case where text has not changed, only point, and it has
11554 not moved off the frame. */
11555 if (/* Point may be in this window. */
11556 PT >= CHARPOS (startp)
11557 /* Selective display hasn't changed. */
11558 && !current_buffer->clip_changed
11559 /* Function force-mode-line-update is used to force a thorough
11560 redisplay. It sets either windows_or_buffers_changed or
11561 update_mode_lines. So don't take a shortcut here for these
11562 cases. */
11563 && !update_mode_lines
11564 && !windows_or_buffers_changed
11565 && !cursor_type_changed
11566 /* Can't use this case if highlighting a region. When a
11567 region exists, cursor movement has to do more than just
11568 set the cursor. */
11569 && !(!NILP (Vtransient_mark_mode)
11570 && !NILP (current_buffer->mark_active))
11571 && NILP (w->region_showing)
11572 && NILP (Vshow_trailing_whitespace)
11573 /* Right after splitting windows, last_point may be nil. */
11574 && INTEGERP (w->last_point)
11575 /* This code is not used for mini-buffer for the sake of the case
11576 of redisplaying to replace an echo area message; since in
11577 that case the mini-buffer contents per se are usually
11578 unchanged. This code is of no real use in the mini-buffer
11579 since the handling of this_line_start_pos, etc., in redisplay
11580 handles the same cases. */
11581 && !EQ (window, minibuf_window)
11582 /* When splitting windows or for new windows, it happens that
11583 redisplay is called with a nil window_end_vpos or one being
11584 larger than the window. This should really be fixed in
11585 window.c. I don't have this on my list, now, so we do
11586 approximately the same as the old redisplay code. --gerd. */
11587 && INTEGERP (w->window_end_vpos)
11588 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11589 && (FRAME_WINDOW_P (f)
11590 || !overlay_arrow_in_current_buffer_p ()))
11591 {
11592 int this_scroll_margin, top_scroll_margin;
11593 struct glyph_row *row = NULL;
11594
11595 #if GLYPH_DEBUG
11596 debug_method_add (w, "cursor movement");
11597 #endif
11598
11599 /* Scroll if point within this distance from the top or bottom
11600 of the window. This is a pixel value. */
11601 this_scroll_margin = max (0, scroll_margin);
11602 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11603 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11604
11605 top_scroll_margin = this_scroll_margin;
11606 if (WINDOW_WANTS_HEADER_LINE_P (w))
11607 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11608
11609 /* Start with the row the cursor was displayed during the last
11610 not paused redisplay. Give up if that row is not valid. */
11611 if (w->last_cursor.vpos < 0
11612 || w->last_cursor.vpos >= w->current_matrix->nrows)
11613 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11614 else
11615 {
11616 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11617 if (row->mode_line_p)
11618 ++row;
11619 if (!row->enabled_p)
11620 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11621 }
11622
11623 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11624 {
11625 int scroll_p = 0;
11626 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11627
11628 if (PT > XFASTINT (w->last_point))
11629 {
11630 /* Point has moved forward. */
11631 while (MATRIX_ROW_END_CHARPOS (row) < PT
11632 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11633 {
11634 xassert (row->enabled_p);
11635 ++row;
11636 }
11637
11638 /* The end position of a row equals the start position
11639 of the next row. If PT is there, we would rather
11640 display it in the next line. */
11641 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11642 && MATRIX_ROW_END_CHARPOS (row) == PT
11643 && !cursor_row_p (w, row))
11644 ++row;
11645
11646 /* If within the scroll margin, scroll. Note that
11647 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11648 the next line would be drawn, and that
11649 this_scroll_margin can be zero. */
11650 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11651 || PT > MATRIX_ROW_END_CHARPOS (row)
11652 /* Line is completely visible last line in window
11653 and PT is to be set in the next line. */
11654 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11655 && PT == MATRIX_ROW_END_CHARPOS (row)
11656 && !row->ends_at_zv_p
11657 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11658 scroll_p = 1;
11659 }
11660 else if (PT < XFASTINT (w->last_point))
11661 {
11662 /* Cursor has to be moved backward. Note that PT >=
11663 CHARPOS (startp) because of the outer if-statement. */
11664 while (!row->mode_line_p
11665 && (MATRIX_ROW_START_CHARPOS (row) > PT
11666 || (MATRIX_ROW_START_CHARPOS (row) == PT
11667 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11668 && (row->y > top_scroll_margin
11669 || CHARPOS (startp) == BEGV))
11670 {
11671 xassert (row->enabled_p);
11672 --row;
11673 }
11674
11675 /* Consider the following case: Window starts at BEGV,
11676 there is invisible, intangible text at BEGV, so that
11677 display starts at some point START > BEGV. It can
11678 happen that we are called with PT somewhere between
11679 BEGV and START. Try to handle that case. */
11680 if (row < w->current_matrix->rows
11681 || row->mode_line_p)
11682 {
11683 row = w->current_matrix->rows;
11684 if (row->mode_line_p)
11685 ++row;
11686 }
11687
11688 /* Due to newlines in overlay strings, we may have to
11689 skip forward over overlay strings. */
11690 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11691 && MATRIX_ROW_END_CHARPOS (row) == PT
11692 && !cursor_row_p (w, row))
11693 ++row;
11694
11695 /* If within the scroll margin, scroll. */
11696 if (row->y < top_scroll_margin
11697 && CHARPOS (startp) != BEGV)
11698 scroll_p = 1;
11699 }
11700 else
11701 {
11702 /* Cursor did not move. So don't scroll even if cursor line
11703 is partially visible, as it was so before. */
11704 rc = CURSOR_MOVEMENT_SUCCESS;
11705 }
11706
11707 if (PT < MATRIX_ROW_START_CHARPOS (row)
11708 || PT > MATRIX_ROW_END_CHARPOS (row))
11709 {
11710 /* if PT is not in the glyph row, give up. */
11711 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11712 }
11713 else if (rc != CURSOR_MOVEMENT_SUCCESS
11714 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11715 && make_cursor_line_fully_visible_p)
11716 {
11717 if (PT == MATRIX_ROW_END_CHARPOS (row)
11718 && !row->ends_at_zv_p
11719 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11720 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11721 else if (row->height > window_box_height (w))
11722 {
11723 /* If we end up in a partially visible line, let's
11724 make it fully visible, except when it's taller
11725 than the window, in which case we can't do much
11726 about it. */
11727 *scroll_step = 1;
11728 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11729 }
11730 else
11731 {
11732 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11733 if (!cursor_row_fully_visible_p (w, 0, 1))
11734 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11735 else
11736 rc = CURSOR_MOVEMENT_SUCCESS;
11737 }
11738 }
11739 else if (scroll_p)
11740 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11741 else
11742 {
11743 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11744 rc = CURSOR_MOVEMENT_SUCCESS;
11745 }
11746 }
11747 }
11748
11749 return rc;
11750 }
11751
11752 void
11753 set_vertical_scroll_bar (w)
11754 struct window *w;
11755 {
11756 int start, end, whole;
11757
11758 /* Calculate the start and end positions for the current window.
11759 At some point, it would be nice to choose between scrollbars
11760 which reflect the whole buffer size, with special markers
11761 indicating narrowing, and scrollbars which reflect only the
11762 visible region.
11763
11764 Note that mini-buffers sometimes aren't displaying any text. */
11765 if (!MINI_WINDOW_P (w)
11766 || (w == XWINDOW (minibuf_window)
11767 && NILP (echo_area_buffer[0])))
11768 {
11769 struct buffer *buf = XBUFFER (w->buffer);
11770 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11771 start = marker_position (w->start) - BUF_BEGV (buf);
11772 /* I don't think this is guaranteed to be right. For the
11773 moment, we'll pretend it is. */
11774 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11775
11776 if (end < start)
11777 end = start;
11778 if (whole < (end - start))
11779 whole = end - start;
11780 }
11781 else
11782 start = end = whole = 0;
11783
11784 /* Indicate what this scroll bar ought to be displaying now. */
11785 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11786 }
11787
11788
11789 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11790 selected_window is redisplayed.
11791
11792 We can return without actually redisplaying the window if
11793 fonts_changed_p is nonzero. In that case, redisplay_internal will
11794 retry. */
11795
11796 static void
11797 redisplay_window (window, just_this_one_p)
11798 Lisp_Object window;
11799 int just_this_one_p;
11800 {
11801 struct window *w = XWINDOW (window);
11802 struct frame *f = XFRAME (w->frame);
11803 struct buffer *buffer = XBUFFER (w->buffer);
11804 struct buffer *old = current_buffer;
11805 struct text_pos lpoint, opoint, startp;
11806 int update_mode_line;
11807 int tem;
11808 struct it it;
11809 /* Record it now because it's overwritten. */
11810 int current_matrix_up_to_date_p = 0;
11811 int used_current_matrix_p = 0;
11812 /* This is less strict than current_matrix_up_to_date_p.
11813 It indictes that the buffer contents and narrowing are unchanged. */
11814 int buffer_unchanged_p = 0;
11815 int temp_scroll_step = 0;
11816 int count = SPECPDL_INDEX ();
11817 int rc;
11818 int centering_position = -1;
11819 int last_line_misfit = 0;
11820
11821 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11822 opoint = lpoint;
11823
11824 /* W must be a leaf window here. */
11825 xassert (!NILP (w->buffer));
11826 #if GLYPH_DEBUG
11827 *w->desired_matrix->method = 0;
11828 #endif
11829
11830 specbind (Qinhibit_point_motion_hooks, Qt);
11831
11832 reconsider_clip_changes (w, buffer);
11833
11834 /* Has the mode line to be updated? */
11835 update_mode_line = (!NILP (w->update_mode_line)
11836 || update_mode_lines
11837 || buffer->clip_changed
11838 || buffer->prevent_redisplay_optimizations_p);
11839
11840 if (MINI_WINDOW_P (w))
11841 {
11842 if (w == XWINDOW (echo_area_window)
11843 && !NILP (echo_area_buffer[0]))
11844 {
11845 if (update_mode_line)
11846 /* We may have to update a tty frame's menu bar or a
11847 tool-bar. Example `M-x C-h C-h C-g'. */
11848 goto finish_menu_bars;
11849 else
11850 /* We've already displayed the echo area glyphs in this window. */
11851 goto finish_scroll_bars;
11852 }
11853 else if ((w != XWINDOW (minibuf_window)
11854 || minibuf_level == 0)
11855 /* When buffer is nonempty, redisplay window normally. */
11856 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
11857 /* Quail displays non-mini buffers in minibuffer window.
11858 In that case, redisplay the window normally. */
11859 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
11860 {
11861 /* W is a mini-buffer window, but it's not active, so clear
11862 it. */
11863 int yb = window_text_bottom_y (w);
11864 struct glyph_row *row;
11865 int y;
11866
11867 for (y = 0, row = w->desired_matrix->rows;
11868 y < yb;
11869 y += row->height, ++row)
11870 blank_row (w, row, y);
11871 goto finish_scroll_bars;
11872 }
11873
11874 clear_glyph_matrix (w->desired_matrix);
11875 }
11876
11877 /* Otherwise set up data on this window; select its buffer and point
11878 value. */
11879 /* Really select the buffer, for the sake of buffer-local
11880 variables. */
11881 set_buffer_internal_1 (XBUFFER (w->buffer));
11882 SET_TEXT_POS (opoint, PT, PT_BYTE);
11883
11884 current_matrix_up_to_date_p
11885 = (!NILP (w->window_end_valid)
11886 && !current_buffer->clip_changed
11887 && !current_buffer->prevent_redisplay_optimizations_p
11888 && XFASTINT (w->last_modified) >= MODIFF
11889 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11890
11891 buffer_unchanged_p
11892 = (!NILP (w->window_end_valid)
11893 && !current_buffer->clip_changed
11894 && XFASTINT (w->last_modified) >= MODIFF
11895 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
11896
11897 /* When windows_or_buffers_changed is non-zero, we can't rely on
11898 the window end being valid, so set it to nil there. */
11899 if (windows_or_buffers_changed)
11900 {
11901 /* If window starts on a continuation line, maybe adjust the
11902 window start in case the window's width changed. */
11903 if (XMARKER (w->start)->buffer == current_buffer)
11904 compute_window_start_on_continuation_line (w);
11905
11906 w->window_end_valid = Qnil;
11907 }
11908
11909 /* Some sanity checks. */
11910 CHECK_WINDOW_END (w);
11911 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
11912 abort ();
11913 if (BYTEPOS (opoint) < CHARPOS (opoint))
11914 abort ();
11915
11916 /* If %c is in mode line, update it if needed. */
11917 if (!NILP (w->column_number_displayed)
11918 /* This alternative quickly identifies a common case
11919 where no change is needed. */
11920 && !(PT == XFASTINT (w->last_point)
11921 && XFASTINT (w->last_modified) >= MODIFF
11922 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
11923 && (XFASTINT (w->column_number_displayed)
11924 != (int) current_column ())) /* iftc */
11925 update_mode_line = 1;
11926
11927 /* Count number of windows showing the selected buffer. An indirect
11928 buffer counts as its base buffer. */
11929 if (!just_this_one_p)
11930 {
11931 struct buffer *current_base, *window_base;
11932 current_base = current_buffer;
11933 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
11934 if (current_base->base_buffer)
11935 current_base = current_base->base_buffer;
11936 if (window_base->base_buffer)
11937 window_base = window_base->base_buffer;
11938 if (current_base == window_base)
11939 buffer_shared++;
11940 }
11941
11942 /* Point refers normally to the selected window. For any other
11943 window, set up appropriate value. */
11944 if (!EQ (window, selected_window))
11945 {
11946 int new_pt = XMARKER (w->pointm)->charpos;
11947 int new_pt_byte = marker_byte_position (w->pointm);
11948 if (new_pt < BEGV)
11949 {
11950 new_pt = BEGV;
11951 new_pt_byte = BEGV_BYTE;
11952 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
11953 }
11954 else if (new_pt > (ZV - 1))
11955 {
11956 new_pt = ZV;
11957 new_pt_byte = ZV_BYTE;
11958 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
11959 }
11960
11961 /* We don't use SET_PT so that the point-motion hooks don't run. */
11962 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
11963 }
11964
11965 /* If any of the character widths specified in the display table
11966 have changed, invalidate the width run cache. It's true that
11967 this may be a bit late to catch such changes, but the rest of
11968 redisplay goes (non-fatally) haywire when the display table is
11969 changed, so why should we worry about doing any better? */
11970 if (current_buffer->width_run_cache)
11971 {
11972 struct Lisp_Char_Table *disptab = buffer_display_table ();
11973
11974 if (! disptab_matches_widthtab (disptab,
11975 XVECTOR (current_buffer->width_table)))
11976 {
11977 invalidate_region_cache (current_buffer,
11978 current_buffer->width_run_cache,
11979 BEG, Z);
11980 recompute_width_table (current_buffer, disptab);
11981 }
11982 }
11983
11984 /* If window-start is screwed up, choose a new one. */
11985 if (XMARKER (w->start)->buffer != current_buffer)
11986 goto recenter;
11987
11988 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11989
11990 /* If someone specified a new starting point but did not insist,
11991 check whether it can be used. */
11992 if (!NILP (w->optional_new_start)
11993 && CHARPOS (startp) >= BEGV
11994 && CHARPOS (startp) <= ZV)
11995 {
11996 w->optional_new_start = Qnil;
11997 start_display (&it, w, startp);
11998 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11999 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12000 if (IT_CHARPOS (it) == PT)
12001 w->force_start = Qt;
12002 /* IT may overshoot PT if text at PT is invisible. */
12003 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12004 w->force_start = Qt;
12005
12006
12007 }
12008
12009 /* Handle case where place to start displaying has been specified,
12010 unless the specified location is outside the accessible range. */
12011 if (!NILP (w->force_start)
12012 || w->frozen_window_start_p)
12013 {
12014 /* We set this later on if we have to adjust point. */
12015 int new_vpos = -1;
12016
12017 w->force_start = Qnil;
12018 w->vscroll = 0;
12019 w->window_end_valid = Qnil;
12020
12021 /* Forget any recorded base line for line number display. */
12022 if (!buffer_unchanged_p)
12023 w->base_line_number = Qnil;
12024
12025 /* Redisplay the mode line. Select the buffer properly for that.
12026 Also, run the hook window-scroll-functions
12027 because we have scrolled. */
12028 /* Note, we do this after clearing force_start because
12029 if there's an error, it is better to forget about force_start
12030 than to get into an infinite loop calling the hook functions
12031 and having them get more errors. */
12032 if (!update_mode_line
12033 || ! NILP (Vwindow_scroll_functions))
12034 {
12035 update_mode_line = 1;
12036 w->update_mode_line = Qt;
12037 startp = run_window_scroll_functions (window, startp);
12038 }
12039
12040 w->last_modified = make_number (0);
12041 w->last_overlay_modified = make_number (0);
12042 if (CHARPOS (startp) < BEGV)
12043 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12044 else if (CHARPOS (startp) > ZV)
12045 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12046
12047 /* Redisplay, then check if cursor has been set during the
12048 redisplay. Give up if new fonts were loaded. */
12049 if (!try_window (window, startp))
12050 {
12051 w->force_start = Qt;
12052 clear_glyph_matrix (w->desired_matrix);
12053 goto need_larger_matrices;
12054 }
12055
12056 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12057 {
12058 /* If point does not appear, try to move point so it does
12059 appear. The desired matrix has been built above, so we
12060 can use it here. */
12061 new_vpos = window_box_height (w) / 2;
12062 }
12063
12064 if (!cursor_row_fully_visible_p (w, 0, 0))
12065 {
12066 /* Point does appear, but on a line partly visible at end of window.
12067 Move it back to a fully-visible line. */
12068 new_vpos = window_box_height (w);
12069 }
12070
12071 /* If we need to move point for either of the above reasons,
12072 now actually do it. */
12073 if (new_vpos >= 0)
12074 {
12075 struct glyph_row *row;
12076
12077 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12078 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12079 ++row;
12080
12081 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12082 MATRIX_ROW_START_BYTEPOS (row));
12083
12084 if (w != XWINDOW (selected_window))
12085 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12086 else if (current_buffer == old)
12087 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12088
12089 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12090
12091 /* If we are highlighting the region, then we just changed
12092 the region, so redisplay to show it. */
12093 if (!NILP (Vtransient_mark_mode)
12094 && !NILP (current_buffer->mark_active))
12095 {
12096 clear_glyph_matrix (w->desired_matrix);
12097 if (!try_window (window, startp))
12098 goto need_larger_matrices;
12099 }
12100 }
12101
12102 #if GLYPH_DEBUG
12103 debug_method_add (w, "forced window start");
12104 #endif
12105 goto done;
12106 }
12107
12108 /* Handle case where text has not changed, only point, and it has
12109 not moved off the frame, and we are not retrying after hscroll.
12110 (current_matrix_up_to_date_p is nonzero when retrying.) */
12111 if (current_matrix_up_to_date_p
12112 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12113 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12114 {
12115 switch (rc)
12116 {
12117 case CURSOR_MOVEMENT_SUCCESS:
12118 used_current_matrix_p = 1;
12119 goto done;
12120
12121 #if 0 /* try_cursor_movement never returns this value. */
12122 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12123 goto need_larger_matrices;
12124 #endif
12125
12126 case CURSOR_MOVEMENT_MUST_SCROLL:
12127 goto try_to_scroll;
12128
12129 default:
12130 abort ();
12131 }
12132 }
12133 /* If current starting point was originally the beginning of a line
12134 but no longer is, find a new starting point. */
12135 else if (!NILP (w->start_at_line_beg)
12136 && !(CHARPOS (startp) <= BEGV
12137 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12138 {
12139 #if GLYPH_DEBUG
12140 debug_method_add (w, "recenter 1");
12141 #endif
12142 goto recenter;
12143 }
12144
12145 /* Try scrolling with try_window_id. Value is > 0 if update has
12146 been done, it is -1 if we know that the same window start will
12147 not work. It is 0 if unsuccessful for some other reason. */
12148 else if ((tem = try_window_id (w)) != 0)
12149 {
12150 #if GLYPH_DEBUG
12151 debug_method_add (w, "try_window_id %d", tem);
12152 #endif
12153
12154 if (fonts_changed_p)
12155 goto need_larger_matrices;
12156 if (tem > 0)
12157 goto done;
12158
12159 /* Otherwise try_window_id has returned -1 which means that we
12160 don't want the alternative below this comment to execute. */
12161 }
12162 else if (CHARPOS (startp) >= BEGV
12163 && CHARPOS (startp) <= ZV
12164 && PT >= CHARPOS (startp)
12165 && (CHARPOS (startp) < ZV
12166 /* Avoid starting at end of buffer. */
12167 || CHARPOS (startp) == BEGV
12168 || (XFASTINT (w->last_modified) >= MODIFF
12169 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12170 {
12171 #if GLYPH_DEBUG
12172 debug_method_add (w, "same window start");
12173 #endif
12174
12175 /* Try to redisplay starting at same place as before.
12176 If point has not moved off frame, accept the results. */
12177 if (!current_matrix_up_to_date_p
12178 /* Don't use try_window_reusing_current_matrix in this case
12179 because a window scroll function can have changed the
12180 buffer. */
12181 || !NILP (Vwindow_scroll_functions)
12182 || MINI_WINDOW_P (w)
12183 || !(used_current_matrix_p
12184 = try_window_reusing_current_matrix (w)))
12185 {
12186 IF_DEBUG (debug_method_add (w, "1"));
12187 try_window (window, startp);
12188 }
12189
12190 if (fonts_changed_p)
12191 goto need_larger_matrices;
12192
12193 if (w->cursor.vpos >= 0)
12194 {
12195 if (!just_this_one_p
12196 || current_buffer->clip_changed
12197 || BEG_UNCHANGED < CHARPOS (startp))
12198 /* Forget any recorded base line for line number display. */
12199 w->base_line_number = Qnil;
12200
12201 if (!cursor_row_fully_visible_p (w, 1, 0))
12202 {
12203 clear_glyph_matrix (w->desired_matrix);
12204 last_line_misfit = 1;
12205 }
12206 /* Drop through and scroll. */
12207 else
12208 goto done;
12209 }
12210 else
12211 clear_glyph_matrix (w->desired_matrix);
12212 }
12213
12214 try_to_scroll:
12215
12216 w->last_modified = make_number (0);
12217 w->last_overlay_modified = make_number (0);
12218
12219 /* Redisplay the mode line. Select the buffer properly for that. */
12220 if (!update_mode_line)
12221 {
12222 update_mode_line = 1;
12223 w->update_mode_line = Qt;
12224 }
12225
12226 /* Try to scroll by specified few lines. */
12227 if ((scroll_conservatively
12228 || scroll_step
12229 || temp_scroll_step
12230 || NUMBERP (current_buffer->scroll_up_aggressively)
12231 || NUMBERP (current_buffer->scroll_down_aggressively))
12232 && !current_buffer->clip_changed
12233 && CHARPOS (startp) >= BEGV
12234 && CHARPOS (startp) <= ZV)
12235 {
12236 /* The function returns -1 if new fonts were loaded, 1 if
12237 successful, 0 if not successful. */
12238 int rc = try_scrolling (window, just_this_one_p,
12239 scroll_conservatively,
12240 scroll_step,
12241 temp_scroll_step, last_line_misfit);
12242 switch (rc)
12243 {
12244 case SCROLLING_SUCCESS:
12245 goto done;
12246
12247 case SCROLLING_NEED_LARGER_MATRICES:
12248 goto need_larger_matrices;
12249
12250 case SCROLLING_FAILED:
12251 break;
12252
12253 default:
12254 abort ();
12255 }
12256 }
12257
12258 /* Finally, just choose place to start which centers point */
12259
12260 recenter:
12261 if (centering_position < 0)
12262 centering_position = window_box_height (w) / 2;
12263
12264 #if GLYPH_DEBUG
12265 debug_method_add (w, "recenter");
12266 #endif
12267
12268 /* w->vscroll = 0; */
12269
12270 /* Forget any previously recorded base line for line number display. */
12271 if (!buffer_unchanged_p)
12272 w->base_line_number = Qnil;
12273
12274 /* Move backward half the height of the window. */
12275 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12276 it.current_y = it.last_visible_y;
12277 move_it_vertically_backward (&it, centering_position);
12278 xassert (IT_CHARPOS (it) >= BEGV);
12279
12280 /* The function move_it_vertically_backward may move over more
12281 than the specified y-distance. If it->w is small, e.g. a
12282 mini-buffer window, we may end up in front of the window's
12283 display area. Start displaying at the start of the line
12284 containing PT in this case. */
12285 if (it.current_y <= 0)
12286 {
12287 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12288 move_it_vertically_backward (&it, 0);
12289 #if 0
12290 /* I think this assert is bogus if buffer contains
12291 invisible text or images. KFS. */
12292 xassert (IT_CHARPOS (it) <= PT);
12293 #endif
12294 it.current_y = 0;
12295 }
12296
12297 it.current_x = it.hpos = 0;
12298
12299 /* Set startp here explicitly in case that helps avoid an infinite loop
12300 in case the window-scroll-functions functions get errors. */
12301 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12302
12303 /* Run scroll hooks. */
12304 startp = run_window_scroll_functions (window, it.current.pos);
12305
12306 /* Redisplay the window. */
12307 if (!current_matrix_up_to_date_p
12308 || windows_or_buffers_changed
12309 || cursor_type_changed
12310 /* Don't use try_window_reusing_current_matrix in this case
12311 because it can have changed the buffer. */
12312 || !NILP (Vwindow_scroll_functions)
12313 || !just_this_one_p
12314 || MINI_WINDOW_P (w)
12315 || !(used_current_matrix_p
12316 = try_window_reusing_current_matrix (w)))
12317 try_window (window, startp);
12318
12319 /* If new fonts have been loaded (due to fontsets), give up. We
12320 have to start a new redisplay since we need to re-adjust glyph
12321 matrices. */
12322 if (fonts_changed_p)
12323 goto need_larger_matrices;
12324
12325 /* If cursor did not appear assume that the middle of the window is
12326 in the first line of the window. Do it again with the next line.
12327 (Imagine a window of height 100, displaying two lines of height
12328 60. Moving back 50 from it->last_visible_y will end in the first
12329 line.) */
12330 if (w->cursor.vpos < 0)
12331 {
12332 if (!NILP (w->window_end_valid)
12333 && PT >= Z - XFASTINT (w->window_end_pos))
12334 {
12335 clear_glyph_matrix (w->desired_matrix);
12336 move_it_by_lines (&it, 1, 0);
12337 try_window (window, it.current.pos);
12338 }
12339 else if (PT < IT_CHARPOS (it))
12340 {
12341 clear_glyph_matrix (w->desired_matrix);
12342 move_it_by_lines (&it, -1, 0);
12343 try_window (window, it.current.pos);
12344 }
12345 else
12346 {
12347 /* Not much we can do about it. */
12348 }
12349 }
12350
12351 /* Consider the following case: Window starts at BEGV, there is
12352 invisible, intangible text at BEGV, so that display starts at
12353 some point START > BEGV. It can happen that we are called with
12354 PT somewhere between BEGV and START. Try to handle that case. */
12355 if (w->cursor.vpos < 0)
12356 {
12357 struct glyph_row *row = w->current_matrix->rows;
12358 if (row->mode_line_p)
12359 ++row;
12360 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12361 }
12362
12363 if (!cursor_row_fully_visible_p (w, 0, 0))
12364 {
12365 /* If vscroll is enabled, disable it and try again. */
12366 if (w->vscroll)
12367 {
12368 w->vscroll = 0;
12369 clear_glyph_matrix (w->desired_matrix);
12370 goto recenter;
12371 }
12372
12373 /* If centering point failed to make the whole line visible,
12374 put point at the top instead. That has to make the whole line
12375 visible, if it can be done. */
12376 if (centering_position == 0)
12377 goto done;
12378
12379 clear_glyph_matrix (w->desired_matrix);
12380 centering_position = 0;
12381 goto recenter;
12382 }
12383
12384 done:
12385
12386 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12387 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12388 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12389 ? Qt : Qnil);
12390
12391 /* Display the mode line, if we must. */
12392 if ((update_mode_line
12393 /* If window not full width, must redo its mode line
12394 if (a) the window to its side is being redone and
12395 (b) we do a frame-based redisplay. This is a consequence
12396 of how inverted lines are drawn in frame-based redisplay. */
12397 || (!just_this_one_p
12398 && !FRAME_WINDOW_P (f)
12399 && !WINDOW_FULL_WIDTH_P (w))
12400 /* Line number to display. */
12401 || INTEGERP (w->base_line_pos)
12402 /* Column number is displayed and different from the one displayed. */
12403 || (!NILP (w->column_number_displayed)
12404 && (XFASTINT (w->column_number_displayed)
12405 != (int) current_column ()))) /* iftc */
12406 /* This means that the window has a mode line. */
12407 && (WINDOW_WANTS_MODELINE_P (w)
12408 || WINDOW_WANTS_HEADER_LINE_P (w)))
12409 {
12410 display_mode_lines (w);
12411
12412 /* If mode line height has changed, arrange for a thorough
12413 immediate redisplay using the correct mode line height. */
12414 if (WINDOW_WANTS_MODELINE_P (w)
12415 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12416 {
12417 fonts_changed_p = 1;
12418 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12419 = DESIRED_MODE_LINE_HEIGHT (w);
12420 }
12421
12422 /* If top line height has changed, arrange for a thorough
12423 immediate redisplay using the correct mode line height. */
12424 if (WINDOW_WANTS_HEADER_LINE_P (w)
12425 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12426 {
12427 fonts_changed_p = 1;
12428 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12429 = DESIRED_HEADER_LINE_HEIGHT (w);
12430 }
12431
12432 if (fonts_changed_p)
12433 goto need_larger_matrices;
12434 }
12435
12436 if (!line_number_displayed
12437 && !BUFFERP (w->base_line_pos))
12438 {
12439 w->base_line_pos = Qnil;
12440 w->base_line_number = Qnil;
12441 }
12442
12443 finish_menu_bars:
12444
12445 /* When we reach a frame's selected window, redo the frame's menu bar. */
12446 if (update_mode_line
12447 && EQ (FRAME_SELECTED_WINDOW (f), window))
12448 {
12449 int redisplay_menu_p = 0;
12450 int redisplay_tool_bar_p = 0;
12451
12452 if (FRAME_WINDOW_P (f))
12453 {
12454 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12455 || defined (USE_GTK)
12456 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12457 #else
12458 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12459 #endif
12460 }
12461 else
12462 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12463
12464 if (redisplay_menu_p)
12465 display_menu_bar (w);
12466
12467 #ifdef HAVE_WINDOW_SYSTEM
12468 #ifdef USE_GTK
12469 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12470 #else
12471 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12472 && (FRAME_TOOL_BAR_LINES (f) > 0
12473 || auto_resize_tool_bars_p);
12474
12475 #endif
12476
12477 if (redisplay_tool_bar_p)
12478 redisplay_tool_bar (f);
12479 #endif
12480 }
12481
12482 #ifdef HAVE_WINDOW_SYSTEM
12483 if (FRAME_WINDOW_P (f)
12484 && update_window_fringes (w, 0)
12485 && !just_this_one_p
12486 && (used_current_matrix_p || overlay_arrow_seen)
12487 && !w->pseudo_window_p)
12488 {
12489 update_begin (f);
12490 BLOCK_INPUT;
12491 if (draw_window_fringes (w, 1))
12492 x_draw_vertical_border (w);
12493 UNBLOCK_INPUT;
12494 update_end (f);
12495 }
12496 #endif /* HAVE_WINDOW_SYSTEM */
12497
12498 /* We go to this label, with fonts_changed_p nonzero,
12499 if it is necessary to try again using larger glyph matrices.
12500 We have to redeem the scroll bar even in this case,
12501 because the loop in redisplay_internal expects that. */
12502 need_larger_matrices:
12503 ;
12504 finish_scroll_bars:
12505
12506 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12507 {
12508 /* Set the thumb's position and size. */
12509 set_vertical_scroll_bar (w);
12510
12511 /* Note that we actually used the scroll bar attached to this
12512 window, so it shouldn't be deleted at the end of redisplay. */
12513 redeem_scroll_bar_hook (w);
12514 }
12515
12516 /* Restore current_buffer and value of point in it. */
12517 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12518 set_buffer_internal_1 (old);
12519 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12520
12521 unbind_to (count, Qnil);
12522 }
12523
12524
12525 /* Build the complete desired matrix of WINDOW with a window start
12526 buffer position POS. Value is non-zero if successful. It is zero
12527 if fonts were loaded during redisplay which makes re-adjusting
12528 glyph matrices necessary. */
12529
12530 int
12531 try_window (window, pos)
12532 Lisp_Object window;
12533 struct text_pos pos;
12534 {
12535 struct window *w = XWINDOW (window);
12536 struct it it;
12537 struct glyph_row *last_text_row = NULL;
12538
12539 /* Make POS the new window start. */
12540 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12541
12542 /* Mark cursor position as unknown. No overlay arrow seen. */
12543 w->cursor.vpos = -1;
12544 overlay_arrow_seen = 0;
12545
12546 /* Initialize iterator and info to start at POS. */
12547 start_display (&it, w, pos);
12548
12549 /* Display all lines of W. */
12550 while (it.current_y < it.last_visible_y)
12551 {
12552 if (display_line (&it))
12553 last_text_row = it.glyph_row - 1;
12554 if (fonts_changed_p)
12555 return 0;
12556 }
12557
12558 /* If bottom moved off end of frame, change mode line percentage. */
12559 if (XFASTINT (w->window_end_pos) <= 0
12560 && Z != IT_CHARPOS (it))
12561 w->update_mode_line = Qt;
12562
12563 /* Set window_end_pos to the offset of the last character displayed
12564 on the window from the end of current_buffer. Set
12565 window_end_vpos to its row number. */
12566 if (last_text_row)
12567 {
12568 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12569 w->window_end_bytepos
12570 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12571 w->window_end_pos
12572 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12573 w->window_end_vpos
12574 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12575 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12576 ->displays_text_p);
12577 }
12578 else
12579 {
12580 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12581 w->window_end_pos = make_number (Z - ZV);
12582 w->window_end_vpos = make_number (0);
12583 }
12584
12585 /* But that is not valid info until redisplay finishes. */
12586 w->window_end_valid = Qnil;
12587 return 1;
12588 }
12589
12590
12591 \f
12592 /************************************************************************
12593 Window redisplay reusing current matrix when buffer has not changed
12594 ************************************************************************/
12595
12596 /* Try redisplay of window W showing an unchanged buffer with a
12597 different window start than the last time it was displayed by
12598 reusing its current matrix. Value is non-zero if successful.
12599 W->start is the new window start. */
12600
12601 static int
12602 try_window_reusing_current_matrix (w)
12603 struct window *w;
12604 {
12605 struct frame *f = XFRAME (w->frame);
12606 struct glyph_row *row, *bottom_row;
12607 struct it it;
12608 struct run run;
12609 struct text_pos start, new_start;
12610 int nrows_scrolled, i;
12611 struct glyph_row *last_text_row;
12612 struct glyph_row *last_reused_text_row;
12613 struct glyph_row *start_row;
12614 int start_vpos, min_y, max_y;
12615
12616 #if GLYPH_DEBUG
12617 if (inhibit_try_window_reusing)
12618 return 0;
12619 #endif
12620
12621 if (/* This function doesn't handle terminal frames. */
12622 !FRAME_WINDOW_P (f)
12623 /* Don't try to reuse the display if windows have been split
12624 or such. */
12625 || windows_or_buffers_changed
12626 || cursor_type_changed)
12627 return 0;
12628
12629 /* Can't do this if region may have changed. */
12630 if ((!NILP (Vtransient_mark_mode)
12631 && !NILP (current_buffer->mark_active))
12632 || !NILP (w->region_showing)
12633 || !NILP (Vshow_trailing_whitespace))
12634 return 0;
12635
12636 /* If top-line visibility has changed, give up. */
12637 if (WINDOW_WANTS_HEADER_LINE_P (w)
12638 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12639 return 0;
12640
12641 /* Give up if old or new display is scrolled vertically. We could
12642 make this function handle this, but right now it doesn't. */
12643 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12644 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12645 return 0;
12646
12647 /* The variable new_start now holds the new window start. The old
12648 start `start' can be determined from the current matrix. */
12649 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12650 start = start_row->start.pos;
12651 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12652
12653 /* Clear the desired matrix for the display below. */
12654 clear_glyph_matrix (w->desired_matrix);
12655
12656 if (CHARPOS (new_start) <= CHARPOS (start))
12657 {
12658 int first_row_y;
12659
12660 /* Don't use this method if the display starts with an ellipsis
12661 displayed for invisible text. It's not easy to handle that case
12662 below, and it's certainly not worth the effort since this is
12663 not a frequent case. */
12664 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12665 return 0;
12666
12667 IF_DEBUG (debug_method_add (w, "twu1"));
12668
12669 /* Display up to a row that can be reused. The variable
12670 last_text_row is set to the last row displayed that displays
12671 text. Note that it.vpos == 0 if or if not there is a
12672 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12673 start_display (&it, w, new_start);
12674 first_row_y = it.current_y;
12675 w->cursor.vpos = -1;
12676 last_text_row = last_reused_text_row = NULL;
12677
12678 while (it.current_y < it.last_visible_y
12679 && !fonts_changed_p)
12680 {
12681 /* If we have reached into the characters in the START row,
12682 that means the line boundaries have changed. So we
12683 can't start copying with the row START. Maybe it will
12684 work to start copying with the following row. */
12685 while (IT_CHARPOS (it) > CHARPOS (start))
12686 {
12687 /* Advance to the next row as the "start". */
12688 start_row++;
12689 start = start_row->start.pos;
12690 /* If there are no more rows to try, or just one, give up. */
12691 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12692 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12693 || CHARPOS (start) == ZV)
12694 {
12695 clear_glyph_matrix (w->desired_matrix);
12696 return 0;
12697 }
12698
12699 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12700 }
12701 /* If we have reached alignment,
12702 we can copy the rest of the rows. */
12703 if (IT_CHARPOS (it) == CHARPOS (start))
12704 break;
12705
12706 if (display_line (&it))
12707 last_text_row = it.glyph_row - 1;
12708 }
12709
12710 /* A value of current_y < last_visible_y means that we stopped
12711 at the previous window start, which in turn means that we
12712 have at least one reusable row. */
12713 if (it.current_y < it.last_visible_y)
12714 {
12715 /* IT.vpos always starts from 0; it counts text lines. */
12716 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12717
12718 /* Find PT if not already found in the lines displayed. */
12719 if (w->cursor.vpos < 0)
12720 {
12721 int dy = it.current_y - start_row->y;
12722
12723 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12724 row = row_containing_pos (w, PT, row, NULL, dy);
12725 if (row)
12726 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12727 dy, nrows_scrolled);
12728 else
12729 {
12730 clear_glyph_matrix (w->desired_matrix);
12731 return 0;
12732 }
12733 }
12734
12735 /* Scroll the display. Do it before the current matrix is
12736 changed. The problem here is that update has not yet
12737 run, i.e. part of the current matrix is not up to date.
12738 scroll_run_hook will clear the cursor, and use the
12739 current matrix to get the height of the row the cursor is
12740 in. */
12741 run.current_y = start_row->y;
12742 run.desired_y = it.current_y;
12743 run.height = it.last_visible_y - it.current_y;
12744
12745 if (run.height > 0 && run.current_y != run.desired_y)
12746 {
12747 update_begin (f);
12748 rif->update_window_begin_hook (w);
12749 rif->clear_window_mouse_face (w);
12750 rif->scroll_run_hook (w, &run);
12751 rif->update_window_end_hook (w, 0, 0);
12752 update_end (f);
12753 }
12754
12755 /* Shift current matrix down by nrows_scrolled lines. */
12756 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12757 rotate_matrix (w->current_matrix,
12758 start_vpos,
12759 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12760 nrows_scrolled);
12761
12762 /* Disable lines that must be updated. */
12763 for (i = 0; i < it.vpos; ++i)
12764 (start_row + i)->enabled_p = 0;
12765
12766 /* Re-compute Y positions. */
12767 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12768 max_y = it.last_visible_y;
12769 for (row = start_row + nrows_scrolled;
12770 row < bottom_row;
12771 ++row)
12772 {
12773 row->y = it.current_y;
12774 row->visible_height = row->height;
12775
12776 if (row->y < min_y)
12777 row->visible_height -= min_y - row->y;
12778 if (row->y + row->height > max_y)
12779 row->visible_height -= row->y + row->height - max_y;
12780 row->redraw_fringe_bitmaps_p = 1;
12781
12782 it.current_y += row->height;
12783
12784 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12785 last_reused_text_row = row;
12786 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12787 break;
12788 }
12789
12790 /* Disable lines in the current matrix which are now
12791 below the window. */
12792 for (++row; row < bottom_row; ++row)
12793 row->enabled_p = 0;
12794 }
12795
12796 /* Update window_end_pos etc.; last_reused_text_row is the last
12797 reused row from the current matrix containing text, if any.
12798 The value of last_text_row is the last displayed line
12799 containing text. */
12800 if (last_reused_text_row)
12801 {
12802 w->window_end_bytepos
12803 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
12804 w->window_end_pos
12805 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
12806 w->window_end_vpos
12807 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
12808 w->current_matrix));
12809 }
12810 else if (last_text_row)
12811 {
12812 w->window_end_bytepos
12813 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12814 w->window_end_pos
12815 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12816 w->window_end_vpos
12817 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12818 }
12819 else
12820 {
12821 /* This window must be completely empty. */
12822 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12823 w->window_end_pos = make_number (Z - ZV);
12824 w->window_end_vpos = make_number (0);
12825 }
12826 w->window_end_valid = Qnil;
12827
12828 /* Update hint: don't try scrolling again in update_window. */
12829 w->desired_matrix->no_scrolling_p = 1;
12830
12831 #if GLYPH_DEBUG
12832 debug_method_add (w, "try_window_reusing_current_matrix 1");
12833 #endif
12834 return 1;
12835 }
12836 else if (CHARPOS (new_start) > CHARPOS (start))
12837 {
12838 struct glyph_row *pt_row, *row;
12839 struct glyph_row *first_reusable_row;
12840 struct glyph_row *first_row_to_display;
12841 int dy;
12842 int yb = window_text_bottom_y (w);
12843
12844 /* Find the row starting at new_start, if there is one. Don't
12845 reuse a partially visible line at the end. */
12846 first_reusable_row = start_row;
12847 while (first_reusable_row->enabled_p
12848 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
12849 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12850 < CHARPOS (new_start)))
12851 ++first_reusable_row;
12852
12853 /* Give up if there is no row to reuse. */
12854 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
12855 || !first_reusable_row->enabled_p
12856 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
12857 != CHARPOS (new_start)))
12858 return 0;
12859
12860 /* We can reuse fully visible rows beginning with
12861 first_reusable_row to the end of the window. Set
12862 first_row_to_display to the first row that cannot be reused.
12863 Set pt_row to the row containing point, if there is any. */
12864 pt_row = NULL;
12865 for (first_row_to_display = first_reusable_row;
12866 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
12867 ++first_row_to_display)
12868 {
12869 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
12870 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
12871 pt_row = first_row_to_display;
12872 }
12873
12874 /* Start displaying at the start of first_row_to_display. */
12875 xassert (first_row_to_display->y < yb);
12876 init_to_row_start (&it, w, first_row_to_display);
12877
12878 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
12879 - start_vpos);
12880 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
12881 - nrows_scrolled);
12882 it.current_y = (first_row_to_display->y - first_reusable_row->y
12883 + WINDOW_HEADER_LINE_HEIGHT (w));
12884
12885 /* Display lines beginning with first_row_to_display in the
12886 desired matrix. Set last_text_row to the last row displayed
12887 that displays text. */
12888 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
12889 if (pt_row == NULL)
12890 w->cursor.vpos = -1;
12891 last_text_row = NULL;
12892 while (it.current_y < it.last_visible_y && !fonts_changed_p)
12893 if (display_line (&it))
12894 last_text_row = it.glyph_row - 1;
12895
12896 /* Give up If point isn't in a row displayed or reused. */
12897 if (w->cursor.vpos < 0)
12898 {
12899 clear_glyph_matrix (w->desired_matrix);
12900 return 0;
12901 }
12902
12903 /* If point is in a reused row, adjust y and vpos of the cursor
12904 position. */
12905 if (pt_row)
12906 {
12907 w->cursor.vpos -= nrows_scrolled;
12908 w->cursor.y -= first_reusable_row->y - start_row->y;
12909 }
12910
12911 /* Scroll the display. */
12912 run.current_y = first_reusable_row->y;
12913 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
12914 run.height = it.last_visible_y - run.current_y;
12915 dy = run.current_y - run.desired_y;
12916
12917 if (run.height)
12918 {
12919 update_begin (f);
12920 rif->update_window_begin_hook (w);
12921 rif->clear_window_mouse_face (w);
12922 rif->scroll_run_hook (w, &run);
12923 rif->update_window_end_hook (w, 0, 0);
12924 update_end (f);
12925 }
12926
12927 /* Adjust Y positions of reused rows. */
12928 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12929 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12930 max_y = it.last_visible_y;
12931 for (row = first_reusable_row; row < first_row_to_display; ++row)
12932 {
12933 row->y -= dy;
12934 row->visible_height = row->height;
12935 if (row->y < min_y)
12936 row->visible_height -= min_y - row->y;
12937 if (row->y + row->height > max_y)
12938 row->visible_height -= row->y + row->height - max_y;
12939 row->redraw_fringe_bitmaps_p = 1;
12940 }
12941
12942 /* Scroll the current matrix. */
12943 xassert (nrows_scrolled > 0);
12944 rotate_matrix (w->current_matrix,
12945 start_vpos,
12946 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12947 -nrows_scrolled);
12948
12949 /* Disable rows not reused. */
12950 for (row -= nrows_scrolled; row < bottom_row; ++row)
12951 row->enabled_p = 0;
12952
12953 /* Point may have moved to a different line, so we cannot assume that
12954 the previous cursor position is valid; locate the correct row. */
12955 if (pt_row)
12956 {
12957 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12958 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
12959 row++)
12960 {
12961 w->cursor.vpos++;
12962 w->cursor.y = row->y;
12963 }
12964 if (row < bottom_row)
12965 {
12966 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
12967 while (glyph->charpos < PT)
12968 {
12969 w->cursor.hpos++;
12970 w->cursor.x += glyph->pixel_width;
12971 glyph++;
12972 }
12973 }
12974 }
12975
12976 /* Adjust window end. A null value of last_text_row means that
12977 the window end is in reused rows which in turn means that
12978 only its vpos can have changed. */
12979 if (last_text_row)
12980 {
12981 w->window_end_bytepos
12982 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12983 w->window_end_pos
12984 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12985 w->window_end_vpos
12986 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12987 }
12988 else
12989 {
12990 w->window_end_vpos
12991 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
12992 }
12993
12994 w->window_end_valid = Qnil;
12995 w->desired_matrix->no_scrolling_p = 1;
12996
12997 #if GLYPH_DEBUG
12998 debug_method_add (w, "try_window_reusing_current_matrix 2");
12999 #endif
13000 return 1;
13001 }
13002
13003 return 0;
13004 }
13005
13006
13007 \f
13008 /************************************************************************
13009 Window redisplay reusing current matrix when buffer has changed
13010 ************************************************************************/
13011
13012 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13013 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13014 int *, int *));
13015 static struct glyph_row *
13016 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13017 struct glyph_row *));
13018
13019
13020 /* Return the last row in MATRIX displaying text. If row START is
13021 non-null, start searching with that row. IT gives the dimensions
13022 of the display. Value is null if matrix is empty; otherwise it is
13023 a pointer to the row found. */
13024
13025 static struct glyph_row *
13026 find_last_row_displaying_text (matrix, it, start)
13027 struct glyph_matrix *matrix;
13028 struct it *it;
13029 struct glyph_row *start;
13030 {
13031 struct glyph_row *row, *row_found;
13032
13033 /* Set row_found to the last row in IT->w's current matrix
13034 displaying text. The loop looks funny but think of partially
13035 visible lines. */
13036 row_found = NULL;
13037 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13038 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13039 {
13040 xassert (row->enabled_p);
13041 row_found = row;
13042 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13043 break;
13044 ++row;
13045 }
13046
13047 return row_found;
13048 }
13049
13050
13051 /* Return the last row in the current matrix of W that is not affected
13052 by changes at the start of current_buffer that occurred since W's
13053 current matrix was built. Value is null if no such row exists.
13054
13055 BEG_UNCHANGED us the number of characters unchanged at the start of
13056 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13057 first changed character in current_buffer. Characters at positions <
13058 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13059 when the current matrix was built. */
13060
13061 static struct glyph_row *
13062 find_last_unchanged_at_beg_row (w)
13063 struct window *w;
13064 {
13065 int first_changed_pos = BEG + BEG_UNCHANGED;
13066 struct glyph_row *row;
13067 struct glyph_row *row_found = NULL;
13068 int yb = window_text_bottom_y (w);
13069
13070 /* Find the last row displaying unchanged text. */
13071 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13072 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13073 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13074 {
13075 if (/* If row ends before first_changed_pos, it is unchanged,
13076 except in some case. */
13077 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13078 /* When row ends in ZV and we write at ZV it is not
13079 unchanged. */
13080 && !row->ends_at_zv_p
13081 /* When first_changed_pos is the end of a continued line,
13082 row is not unchanged because it may be no longer
13083 continued. */
13084 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13085 && (row->continued_p
13086 || row->exact_window_width_line_p)))
13087 row_found = row;
13088
13089 /* Stop if last visible row. */
13090 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13091 break;
13092
13093 ++row;
13094 }
13095
13096 return row_found;
13097 }
13098
13099
13100 /* Find the first glyph row in the current matrix of W that is not
13101 affected by changes at the end of current_buffer since the
13102 time W's current matrix was built.
13103
13104 Return in *DELTA the number of chars by which buffer positions in
13105 unchanged text at the end of current_buffer must be adjusted.
13106
13107 Return in *DELTA_BYTES the corresponding number of bytes.
13108
13109 Value is null if no such row exists, i.e. all rows are affected by
13110 changes. */
13111
13112 static struct glyph_row *
13113 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13114 struct window *w;
13115 int *delta, *delta_bytes;
13116 {
13117 struct glyph_row *row;
13118 struct glyph_row *row_found = NULL;
13119
13120 *delta = *delta_bytes = 0;
13121
13122 /* Display must not have been paused, otherwise the current matrix
13123 is not up to date. */
13124 if (NILP (w->window_end_valid))
13125 abort ();
13126
13127 /* A value of window_end_pos >= END_UNCHANGED means that the window
13128 end is in the range of changed text. If so, there is no
13129 unchanged row at the end of W's current matrix. */
13130 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13131 return NULL;
13132
13133 /* Set row to the last row in W's current matrix displaying text. */
13134 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13135
13136 /* If matrix is entirely empty, no unchanged row exists. */
13137 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13138 {
13139 /* The value of row is the last glyph row in the matrix having a
13140 meaningful buffer position in it. The end position of row
13141 corresponds to window_end_pos. This allows us to translate
13142 buffer positions in the current matrix to current buffer
13143 positions for characters not in changed text. */
13144 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13145 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13146 int last_unchanged_pos, last_unchanged_pos_old;
13147 struct glyph_row *first_text_row
13148 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13149
13150 *delta = Z - Z_old;
13151 *delta_bytes = Z_BYTE - Z_BYTE_old;
13152
13153 /* Set last_unchanged_pos to the buffer position of the last
13154 character in the buffer that has not been changed. Z is the
13155 index + 1 of the last character in current_buffer, i.e. by
13156 subtracting END_UNCHANGED we get the index of the last
13157 unchanged character, and we have to add BEG to get its buffer
13158 position. */
13159 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13160 last_unchanged_pos_old = last_unchanged_pos - *delta;
13161
13162 /* Search backward from ROW for a row displaying a line that
13163 starts at a minimum position >= last_unchanged_pos_old. */
13164 for (; row > first_text_row; --row)
13165 {
13166 /* This used to abort, but it can happen.
13167 It is ok to just stop the search instead here. KFS. */
13168 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13169 break;
13170
13171 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13172 row_found = row;
13173 }
13174 }
13175
13176 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13177 abort ();
13178
13179 return row_found;
13180 }
13181
13182
13183 /* Make sure that glyph rows in the current matrix of window W
13184 reference the same glyph memory as corresponding rows in the
13185 frame's frame matrix. This function is called after scrolling W's
13186 current matrix on a terminal frame in try_window_id and
13187 try_window_reusing_current_matrix. */
13188
13189 static void
13190 sync_frame_with_window_matrix_rows (w)
13191 struct window *w;
13192 {
13193 struct frame *f = XFRAME (w->frame);
13194 struct glyph_row *window_row, *window_row_end, *frame_row;
13195
13196 /* Preconditions: W must be a leaf window and full-width. Its frame
13197 must have a frame matrix. */
13198 xassert (NILP (w->hchild) && NILP (w->vchild));
13199 xassert (WINDOW_FULL_WIDTH_P (w));
13200 xassert (!FRAME_WINDOW_P (f));
13201
13202 /* If W is a full-width window, glyph pointers in W's current matrix
13203 have, by definition, to be the same as glyph pointers in the
13204 corresponding frame matrix. Note that frame matrices have no
13205 marginal areas (see build_frame_matrix). */
13206 window_row = w->current_matrix->rows;
13207 window_row_end = window_row + w->current_matrix->nrows;
13208 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13209 while (window_row < window_row_end)
13210 {
13211 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13212 struct glyph *end = window_row->glyphs[LAST_AREA];
13213
13214 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13215 frame_row->glyphs[TEXT_AREA] = start;
13216 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13217 frame_row->glyphs[LAST_AREA] = end;
13218
13219 /* Disable frame rows whose corresponding window rows have
13220 been disabled in try_window_id. */
13221 if (!window_row->enabled_p)
13222 frame_row->enabled_p = 0;
13223
13224 ++window_row, ++frame_row;
13225 }
13226 }
13227
13228
13229 /* Find the glyph row in window W containing CHARPOS. Consider all
13230 rows between START and END (not inclusive). END null means search
13231 all rows to the end of the display area of W. Value is the row
13232 containing CHARPOS or null. */
13233
13234 struct glyph_row *
13235 row_containing_pos (w, charpos, start, end, dy)
13236 struct window *w;
13237 int charpos;
13238 struct glyph_row *start, *end;
13239 int dy;
13240 {
13241 struct glyph_row *row = start;
13242 int last_y;
13243
13244 /* If we happen to start on a header-line, skip that. */
13245 if (row->mode_line_p)
13246 ++row;
13247
13248 if ((end && row >= end) || !row->enabled_p)
13249 return NULL;
13250
13251 last_y = window_text_bottom_y (w) - dy;
13252
13253 while (1)
13254 {
13255 /* Give up if we have gone too far. */
13256 if (end && row >= end)
13257 return NULL;
13258 /* This formerly returned if they were equal.
13259 I think that both quantities are of a "last plus one" type;
13260 if so, when they are equal, the row is within the screen. -- rms. */
13261 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13262 return NULL;
13263
13264 /* If it is in this row, return this row. */
13265 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13266 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13267 /* The end position of a row equals the start
13268 position of the next row. If CHARPOS is there, we
13269 would rather display it in the next line, except
13270 when this line ends in ZV. */
13271 && !row->ends_at_zv_p
13272 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13273 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13274 return row;
13275 ++row;
13276 }
13277 }
13278
13279
13280 /* Try to redisplay window W by reusing its existing display. W's
13281 current matrix must be up to date when this function is called,
13282 i.e. window_end_valid must not be nil.
13283
13284 Value is
13285
13286 1 if display has been updated
13287 0 if otherwise unsuccessful
13288 -1 if redisplay with same window start is known not to succeed
13289
13290 The following steps are performed:
13291
13292 1. Find the last row in the current matrix of W that is not
13293 affected by changes at the start of current_buffer. If no such row
13294 is found, give up.
13295
13296 2. Find the first row in W's current matrix that is not affected by
13297 changes at the end of current_buffer. Maybe there is no such row.
13298
13299 3. Display lines beginning with the row + 1 found in step 1 to the
13300 row found in step 2 or, if step 2 didn't find a row, to the end of
13301 the window.
13302
13303 4. If cursor is not known to appear on the window, give up.
13304
13305 5. If display stopped at the row found in step 2, scroll the
13306 display and current matrix as needed.
13307
13308 6. Maybe display some lines at the end of W, if we must. This can
13309 happen under various circumstances, like a partially visible line
13310 becoming fully visible, or because newly displayed lines are displayed
13311 in smaller font sizes.
13312
13313 7. Update W's window end information. */
13314
13315 static int
13316 try_window_id (w)
13317 struct window *w;
13318 {
13319 struct frame *f = XFRAME (w->frame);
13320 struct glyph_matrix *current_matrix = w->current_matrix;
13321 struct glyph_matrix *desired_matrix = w->desired_matrix;
13322 struct glyph_row *last_unchanged_at_beg_row;
13323 struct glyph_row *first_unchanged_at_end_row;
13324 struct glyph_row *row;
13325 struct glyph_row *bottom_row;
13326 int bottom_vpos;
13327 struct it it;
13328 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13329 struct text_pos start_pos;
13330 struct run run;
13331 int first_unchanged_at_end_vpos = 0;
13332 struct glyph_row *last_text_row, *last_text_row_at_end;
13333 struct text_pos start;
13334 int first_changed_charpos, last_changed_charpos;
13335
13336 #if GLYPH_DEBUG
13337 if (inhibit_try_window_id)
13338 return 0;
13339 #endif
13340
13341 /* This is handy for debugging. */
13342 #if 0
13343 #define GIVE_UP(X) \
13344 do { \
13345 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13346 return 0; \
13347 } while (0)
13348 #else
13349 #define GIVE_UP(X) return 0
13350 #endif
13351
13352 SET_TEXT_POS_FROM_MARKER (start, w->start);
13353
13354 /* Don't use this for mini-windows because these can show
13355 messages and mini-buffers, and we don't handle that here. */
13356 if (MINI_WINDOW_P (w))
13357 GIVE_UP (1);
13358
13359 /* This flag is used to prevent redisplay optimizations. */
13360 if (windows_or_buffers_changed || cursor_type_changed)
13361 GIVE_UP (2);
13362
13363 /* Verify that narrowing has not changed.
13364 Also verify that we were not told to prevent redisplay optimizations.
13365 It would be nice to further
13366 reduce the number of cases where this prevents try_window_id. */
13367 if (current_buffer->clip_changed
13368 || current_buffer->prevent_redisplay_optimizations_p)
13369 GIVE_UP (3);
13370
13371 /* Window must either use window-based redisplay or be full width. */
13372 if (!FRAME_WINDOW_P (f)
13373 && (!line_ins_del_ok
13374 || !WINDOW_FULL_WIDTH_P (w)))
13375 GIVE_UP (4);
13376
13377 /* Give up if point is not known NOT to appear in W. */
13378 if (PT < CHARPOS (start))
13379 GIVE_UP (5);
13380
13381 /* Another way to prevent redisplay optimizations. */
13382 if (XFASTINT (w->last_modified) == 0)
13383 GIVE_UP (6);
13384
13385 /* Verify that window is not hscrolled. */
13386 if (XFASTINT (w->hscroll) != 0)
13387 GIVE_UP (7);
13388
13389 /* Verify that display wasn't paused. */
13390 if (NILP (w->window_end_valid))
13391 GIVE_UP (8);
13392
13393 /* Can't use this if highlighting a region because a cursor movement
13394 will do more than just set the cursor. */
13395 if (!NILP (Vtransient_mark_mode)
13396 && !NILP (current_buffer->mark_active))
13397 GIVE_UP (9);
13398
13399 /* Likewise if highlighting trailing whitespace. */
13400 if (!NILP (Vshow_trailing_whitespace))
13401 GIVE_UP (11);
13402
13403 /* Likewise if showing a region. */
13404 if (!NILP (w->region_showing))
13405 GIVE_UP (10);
13406
13407 /* Can use this if overlay arrow position and or string have changed. */
13408 if (overlay_arrows_changed_p ())
13409 GIVE_UP (12);
13410
13411
13412 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13413 only if buffer has really changed. The reason is that the gap is
13414 initially at Z for freshly visited files. The code below would
13415 set end_unchanged to 0 in that case. */
13416 if (MODIFF > SAVE_MODIFF
13417 /* This seems to happen sometimes after saving a buffer. */
13418 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13419 {
13420 if (GPT - BEG < BEG_UNCHANGED)
13421 BEG_UNCHANGED = GPT - BEG;
13422 if (Z - GPT < END_UNCHANGED)
13423 END_UNCHANGED = Z - GPT;
13424 }
13425
13426 /* The position of the first and last character that has been changed. */
13427 first_changed_charpos = BEG + BEG_UNCHANGED;
13428 last_changed_charpos = Z - END_UNCHANGED;
13429
13430 /* If window starts after a line end, and the last change is in
13431 front of that newline, then changes don't affect the display.
13432 This case happens with stealth-fontification. Note that although
13433 the display is unchanged, glyph positions in the matrix have to
13434 be adjusted, of course. */
13435 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13436 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13437 && ((last_changed_charpos < CHARPOS (start)
13438 && CHARPOS (start) == BEGV)
13439 || (last_changed_charpos < CHARPOS (start) - 1
13440 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13441 {
13442 int Z_old, delta, Z_BYTE_old, delta_bytes;
13443 struct glyph_row *r0;
13444
13445 /* Compute how many chars/bytes have been added to or removed
13446 from the buffer. */
13447 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13448 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13449 delta = Z - Z_old;
13450 delta_bytes = Z_BYTE - Z_BYTE_old;
13451
13452 /* Give up if PT is not in the window. Note that it already has
13453 been checked at the start of try_window_id that PT is not in
13454 front of the window start. */
13455 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13456 GIVE_UP (13);
13457
13458 /* If window start is unchanged, we can reuse the whole matrix
13459 as is, after adjusting glyph positions. No need to compute
13460 the window end again, since its offset from Z hasn't changed. */
13461 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13462 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13463 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13464 /* PT must not be in a partially visible line. */
13465 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13466 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13467 {
13468 /* Adjust positions in the glyph matrix. */
13469 if (delta || delta_bytes)
13470 {
13471 struct glyph_row *r1
13472 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13473 increment_matrix_positions (w->current_matrix,
13474 MATRIX_ROW_VPOS (r0, current_matrix),
13475 MATRIX_ROW_VPOS (r1, current_matrix),
13476 delta, delta_bytes);
13477 }
13478
13479 /* Set the cursor. */
13480 row = row_containing_pos (w, PT, r0, NULL, 0);
13481 if (row)
13482 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13483 else
13484 abort ();
13485 return 1;
13486 }
13487 }
13488
13489 /* Handle the case that changes are all below what is displayed in
13490 the window, and that PT is in the window. This shortcut cannot
13491 be taken if ZV is visible in the window, and text has been added
13492 there that is visible in the window. */
13493 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13494 /* ZV is not visible in the window, or there are no
13495 changes at ZV, actually. */
13496 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13497 || first_changed_charpos == last_changed_charpos))
13498 {
13499 struct glyph_row *r0;
13500
13501 /* Give up if PT is not in the window. Note that it already has
13502 been checked at the start of try_window_id that PT is not in
13503 front of the window start. */
13504 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13505 GIVE_UP (14);
13506
13507 /* If window start is unchanged, we can reuse the whole matrix
13508 as is, without changing glyph positions since no text has
13509 been added/removed in front of the window end. */
13510 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13511 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13512 /* PT must not be in a partially visible line. */
13513 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13514 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13515 {
13516 /* We have to compute the window end anew since text
13517 can have been added/removed after it. */
13518 w->window_end_pos
13519 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13520 w->window_end_bytepos
13521 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13522
13523 /* Set the cursor. */
13524 row = row_containing_pos (w, PT, r0, NULL, 0);
13525 if (row)
13526 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13527 else
13528 abort ();
13529 return 2;
13530 }
13531 }
13532
13533 /* Give up if window start is in the changed area.
13534
13535 The condition used to read
13536
13537 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13538
13539 but why that was tested escapes me at the moment. */
13540 if (CHARPOS (start) >= first_changed_charpos
13541 && CHARPOS (start) <= last_changed_charpos)
13542 GIVE_UP (15);
13543
13544 /* Check that window start agrees with the start of the first glyph
13545 row in its current matrix. Check this after we know the window
13546 start is not in changed text, otherwise positions would not be
13547 comparable. */
13548 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13549 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13550 GIVE_UP (16);
13551
13552 /* Give up if the window ends in strings. Overlay strings
13553 at the end are difficult to handle, so don't try. */
13554 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13555 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13556 GIVE_UP (20);
13557
13558 /* Compute the position at which we have to start displaying new
13559 lines. Some of the lines at the top of the window might be
13560 reusable because they are not displaying changed text. Find the
13561 last row in W's current matrix not affected by changes at the
13562 start of current_buffer. Value is null if changes start in the
13563 first line of window. */
13564 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13565 if (last_unchanged_at_beg_row)
13566 {
13567 /* Avoid starting to display in the moddle of a character, a TAB
13568 for instance. This is easier than to set up the iterator
13569 exactly, and it's not a frequent case, so the additional
13570 effort wouldn't really pay off. */
13571 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13572 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13573 && last_unchanged_at_beg_row > w->current_matrix->rows)
13574 --last_unchanged_at_beg_row;
13575
13576 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13577 GIVE_UP (17);
13578
13579 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13580 GIVE_UP (18);
13581 start_pos = it.current.pos;
13582
13583 /* Start displaying new lines in the desired matrix at the same
13584 vpos we would use in the current matrix, i.e. below
13585 last_unchanged_at_beg_row. */
13586 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13587 current_matrix);
13588 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13589 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13590
13591 xassert (it.hpos == 0 && it.current_x == 0);
13592 }
13593 else
13594 {
13595 /* There are no reusable lines at the start of the window.
13596 Start displaying in the first text line. */
13597 start_display (&it, w, start);
13598 it.vpos = it.first_vpos;
13599 start_pos = it.current.pos;
13600 }
13601
13602 /* Find the first row that is not affected by changes at the end of
13603 the buffer. Value will be null if there is no unchanged row, in
13604 which case we must redisplay to the end of the window. delta
13605 will be set to the value by which buffer positions beginning with
13606 first_unchanged_at_end_row have to be adjusted due to text
13607 changes. */
13608 first_unchanged_at_end_row
13609 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13610 IF_DEBUG (debug_delta = delta);
13611 IF_DEBUG (debug_delta_bytes = delta_bytes);
13612
13613 /* Set stop_pos to the buffer position up to which we will have to
13614 display new lines. If first_unchanged_at_end_row != NULL, this
13615 is the buffer position of the start of the line displayed in that
13616 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13617 that we don't stop at a buffer position. */
13618 stop_pos = 0;
13619 if (first_unchanged_at_end_row)
13620 {
13621 xassert (last_unchanged_at_beg_row == NULL
13622 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13623
13624 /* If this is a continuation line, move forward to the next one
13625 that isn't. Changes in lines above affect this line.
13626 Caution: this may move first_unchanged_at_end_row to a row
13627 not displaying text. */
13628 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13629 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13630 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13631 < it.last_visible_y))
13632 ++first_unchanged_at_end_row;
13633
13634 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13635 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13636 >= it.last_visible_y))
13637 first_unchanged_at_end_row = NULL;
13638 else
13639 {
13640 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13641 + delta);
13642 first_unchanged_at_end_vpos
13643 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13644 xassert (stop_pos >= Z - END_UNCHANGED);
13645 }
13646 }
13647 else if (last_unchanged_at_beg_row == NULL)
13648 GIVE_UP (19);
13649
13650
13651 #if GLYPH_DEBUG
13652
13653 /* Either there is no unchanged row at the end, or the one we have
13654 now displays text. This is a necessary condition for the window
13655 end pos calculation at the end of this function. */
13656 xassert (first_unchanged_at_end_row == NULL
13657 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13658
13659 debug_last_unchanged_at_beg_vpos
13660 = (last_unchanged_at_beg_row
13661 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13662 : -1);
13663 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13664
13665 #endif /* GLYPH_DEBUG != 0 */
13666
13667
13668 /* Display new lines. Set last_text_row to the last new line
13669 displayed which has text on it, i.e. might end up as being the
13670 line where the window_end_vpos is. */
13671 w->cursor.vpos = -1;
13672 last_text_row = NULL;
13673 overlay_arrow_seen = 0;
13674 while (it.current_y < it.last_visible_y
13675 && !fonts_changed_p
13676 && (first_unchanged_at_end_row == NULL
13677 || IT_CHARPOS (it) < stop_pos))
13678 {
13679 if (display_line (&it))
13680 last_text_row = it.glyph_row - 1;
13681 }
13682
13683 if (fonts_changed_p)
13684 return -1;
13685
13686
13687 /* Compute differences in buffer positions, y-positions etc. for
13688 lines reused at the bottom of the window. Compute what we can
13689 scroll. */
13690 if (first_unchanged_at_end_row
13691 /* No lines reused because we displayed everything up to the
13692 bottom of the window. */
13693 && it.current_y < it.last_visible_y)
13694 {
13695 dvpos = (it.vpos
13696 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13697 current_matrix));
13698 dy = it.current_y - first_unchanged_at_end_row->y;
13699 run.current_y = first_unchanged_at_end_row->y;
13700 run.desired_y = run.current_y + dy;
13701 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13702 }
13703 else
13704 {
13705 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13706 first_unchanged_at_end_row = NULL;
13707 }
13708 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13709
13710
13711 /* Find the cursor if not already found. We have to decide whether
13712 PT will appear on this window (it sometimes doesn't, but this is
13713 not a very frequent case.) This decision has to be made before
13714 the current matrix is altered. A value of cursor.vpos < 0 means
13715 that PT is either in one of the lines beginning at
13716 first_unchanged_at_end_row or below the window. Don't care for
13717 lines that might be displayed later at the window end; as
13718 mentioned, this is not a frequent case. */
13719 if (w->cursor.vpos < 0)
13720 {
13721 /* Cursor in unchanged rows at the top? */
13722 if (PT < CHARPOS (start_pos)
13723 && last_unchanged_at_beg_row)
13724 {
13725 row = row_containing_pos (w, PT,
13726 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13727 last_unchanged_at_beg_row + 1, 0);
13728 if (row)
13729 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13730 }
13731
13732 /* Start from first_unchanged_at_end_row looking for PT. */
13733 else if (first_unchanged_at_end_row)
13734 {
13735 row = row_containing_pos (w, PT - delta,
13736 first_unchanged_at_end_row, NULL, 0);
13737 if (row)
13738 set_cursor_from_row (w, row, w->current_matrix, delta,
13739 delta_bytes, dy, dvpos);
13740 }
13741
13742 /* Give up if cursor was not found. */
13743 if (w->cursor.vpos < 0)
13744 {
13745 clear_glyph_matrix (w->desired_matrix);
13746 return -1;
13747 }
13748 }
13749
13750 /* Don't let the cursor end in the scroll margins. */
13751 {
13752 int this_scroll_margin, cursor_height;
13753
13754 this_scroll_margin = max (0, scroll_margin);
13755 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13756 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13757 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13758
13759 if ((w->cursor.y < this_scroll_margin
13760 && CHARPOS (start) > BEGV)
13761 /* Old redisplay didn't take scroll margin into account at the bottom,
13762 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13763 || (w->cursor.y + (make_cursor_line_fully_visible_p
13764 ? cursor_height + this_scroll_margin
13765 : 1)) > it.last_visible_y)
13766 {
13767 w->cursor.vpos = -1;
13768 clear_glyph_matrix (w->desired_matrix);
13769 return -1;
13770 }
13771 }
13772
13773 /* Scroll the display. Do it before changing the current matrix so
13774 that xterm.c doesn't get confused about where the cursor glyph is
13775 found. */
13776 if (dy && run.height)
13777 {
13778 update_begin (f);
13779
13780 if (FRAME_WINDOW_P (f))
13781 {
13782 rif->update_window_begin_hook (w);
13783 rif->clear_window_mouse_face (w);
13784 rif->scroll_run_hook (w, &run);
13785 rif->update_window_end_hook (w, 0, 0);
13786 }
13787 else
13788 {
13789 /* Terminal frame. In this case, dvpos gives the number of
13790 lines to scroll by; dvpos < 0 means scroll up. */
13791 int first_unchanged_at_end_vpos
13792 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13793 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13794 int end = (WINDOW_TOP_EDGE_LINE (w)
13795 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13796 + window_internal_height (w));
13797
13798 /* Perform the operation on the screen. */
13799 if (dvpos > 0)
13800 {
13801 /* Scroll last_unchanged_at_beg_row to the end of the
13802 window down dvpos lines. */
13803 set_terminal_window (end);
13804
13805 /* On dumb terminals delete dvpos lines at the end
13806 before inserting dvpos empty lines. */
13807 if (!scroll_region_ok)
13808 ins_del_lines (end - dvpos, -dvpos);
13809
13810 /* Insert dvpos empty lines in front of
13811 last_unchanged_at_beg_row. */
13812 ins_del_lines (from, dvpos);
13813 }
13814 else if (dvpos < 0)
13815 {
13816 /* Scroll up last_unchanged_at_beg_vpos to the end of
13817 the window to last_unchanged_at_beg_vpos - |dvpos|. */
13818 set_terminal_window (end);
13819
13820 /* Delete dvpos lines in front of
13821 last_unchanged_at_beg_vpos. ins_del_lines will set
13822 the cursor to the given vpos and emit |dvpos| delete
13823 line sequences. */
13824 ins_del_lines (from + dvpos, dvpos);
13825
13826 /* On a dumb terminal insert dvpos empty lines at the
13827 end. */
13828 if (!scroll_region_ok)
13829 ins_del_lines (end + dvpos, -dvpos);
13830 }
13831
13832 set_terminal_window (0);
13833 }
13834
13835 update_end (f);
13836 }
13837
13838 /* Shift reused rows of the current matrix to the right position.
13839 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
13840 text. */
13841 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13842 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
13843 if (dvpos < 0)
13844 {
13845 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
13846 bottom_vpos, dvpos);
13847 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
13848 bottom_vpos, 0);
13849 }
13850 else if (dvpos > 0)
13851 {
13852 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
13853 bottom_vpos, dvpos);
13854 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
13855 first_unchanged_at_end_vpos + dvpos, 0);
13856 }
13857
13858 /* For frame-based redisplay, make sure that current frame and window
13859 matrix are in sync with respect to glyph memory. */
13860 if (!FRAME_WINDOW_P (f))
13861 sync_frame_with_window_matrix_rows (w);
13862
13863 /* Adjust buffer positions in reused rows. */
13864 if (delta)
13865 increment_matrix_positions (current_matrix,
13866 first_unchanged_at_end_vpos + dvpos,
13867 bottom_vpos, delta, delta_bytes);
13868
13869 /* Adjust Y positions. */
13870 if (dy)
13871 shift_glyph_matrix (w, current_matrix,
13872 first_unchanged_at_end_vpos + dvpos,
13873 bottom_vpos, dy);
13874
13875 if (first_unchanged_at_end_row)
13876 {
13877 first_unchanged_at_end_row += dvpos;
13878 if (first_unchanged_at_end_row->y >= it.last_visible_y
13879 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
13880 first_unchanged_at_end_row = NULL;
13881 }
13882
13883 /* If scrolling up, there may be some lines to display at the end of
13884 the window. */
13885 last_text_row_at_end = NULL;
13886 if (dy < 0)
13887 {
13888 /* Scrolling up can leave for example a partially visible line
13889 at the end of the window to be redisplayed. */
13890 /* Set last_row to the glyph row in the current matrix where the
13891 window end line is found. It has been moved up or down in
13892 the matrix by dvpos. */
13893 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
13894 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
13895
13896 /* If last_row is the window end line, it should display text. */
13897 xassert (last_row->displays_text_p);
13898
13899 /* If window end line was partially visible before, begin
13900 displaying at that line. Otherwise begin displaying with the
13901 line following it. */
13902 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
13903 {
13904 init_to_row_start (&it, w, last_row);
13905 it.vpos = last_vpos;
13906 it.current_y = last_row->y;
13907 }
13908 else
13909 {
13910 init_to_row_end (&it, w, last_row);
13911 it.vpos = 1 + last_vpos;
13912 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
13913 ++last_row;
13914 }
13915
13916 /* We may start in a continuation line. If so, we have to
13917 get the right continuation_lines_width and current_x. */
13918 it.continuation_lines_width = last_row->continuation_lines_width;
13919 it.hpos = it.current_x = 0;
13920
13921 /* Display the rest of the lines at the window end. */
13922 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13923 while (it.current_y < it.last_visible_y
13924 && !fonts_changed_p)
13925 {
13926 /* Is it always sure that the display agrees with lines in
13927 the current matrix? I don't think so, so we mark rows
13928 displayed invalid in the current matrix by setting their
13929 enabled_p flag to zero. */
13930 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
13931 if (display_line (&it))
13932 last_text_row_at_end = it.glyph_row - 1;
13933 }
13934 }
13935
13936 /* Update window_end_pos and window_end_vpos. */
13937 if (first_unchanged_at_end_row
13938 && !last_text_row_at_end)
13939 {
13940 /* Window end line if one of the preserved rows from the current
13941 matrix. Set row to the last row displaying text in current
13942 matrix starting at first_unchanged_at_end_row, after
13943 scrolling. */
13944 xassert (first_unchanged_at_end_row->displays_text_p);
13945 row = find_last_row_displaying_text (w->current_matrix, &it,
13946 first_unchanged_at_end_row);
13947 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
13948
13949 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13950 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13951 w->window_end_vpos
13952 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
13953 xassert (w->window_end_bytepos >= 0);
13954 IF_DEBUG (debug_method_add (w, "A"));
13955 }
13956 else if (last_text_row_at_end)
13957 {
13958 w->window_end_pos
13959 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
13960 w->window_end_bytepos
13961 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
13962 w->window_end_vpos
13963 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
13964 xassert (w->window_end_bytepos >= 0);
13965 IF_DEBUG (debug_method_add (w, "B"));
13966 }
13967 else if (last_text_row)
13968 {
13969 /* We have displayed either to the end of the window or at the
13970 end of the window, i.e. the last row with text is to be found
13971 in the desired matrix. */
13972 w->window_end_pos
13973 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13974 w->window_end_bytepos
13975 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13976 w->window_end_vpos
13977 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
13978 xassert (w->window_end_bytepos >= 0);
13979 }
13980 else if (first_unchanged_at_end_row == NULL
13981 && last_text_row == NULL
13982 && last_text_row_at_end == NULL)
13983 {
13984 /* Displayed to end of window, but no line containing text was
13985 displayed. Lines were deleted at the end of the window. */
13986 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
13987 int vpos = XFASTINT (w->window_end_vpos);
13988 struct glyph_row *current_row = current_matrix->rows + vpos;
13989 struct glyph_row *desired_row = desired_matrix->rows + vpos;
13990
13991 for (row = NULL;
13992 row == NULL && vpos >= first_vpos;
13993 --vpos, --current_row, --desired_row)
13994 {
13995 if (desired_row->enabled_p)
13996 {
13997 if (desired_row->displays_text_p)
13998 row = desired_row;
13999 }
14000 else if (current_row->displays_text_p)
14001 row = current_row;
14002 }
14003
14004 xassert (row != NULL);
14005 w->window_end_vpos = make_number (vpos + 1);
14006 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14007 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14008 xassert (w->window_end_bytepos >= 0);
14009 IF_DEBUG (debug_method_add (w, "C"));
14010 }
14011 else
14012 abort ();
14013
14014 #if 0 /* This leads to problems, for instance when the cursor is
14015 at ZV, and the cursor line displays no text. */
14016 /* Disable rows below what's displayed in the window. This makes
14017 debugging easier. */
14018 enable_glyph_matrix_rows (current_matrix,
14019 XFASTINT (w->window_end_vpos) + 1,
14020 bottom_vpos, 0);
14021 #endif
14022
14023 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14024 debug_end_vpos = XFASTINT (w->window_end_vpos));
14025
14026 /* Record that display has not been completed. */
14027 w->window_end_valid = Qnil;
14028 w->desired_matrix->no_scrolling_p = 1;
14029 return 3;
14030
14031 #undef GIVE_UP
14032 }
14033
14034
14035 \f
14036 /***********************************************************************
14037 More debugging support
14038 ***********************************************************************/
14039
14040 #if GLYPH_DEBUG
14041
14042 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14043 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14044 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14045
14046
14047 /* Dump the contents of glyph matrix MATRIX on stderr.
14048
14049 GLYPHS 0 means don't show glyph contents.
14050 GLYPHS 1 means show glyphs in short form
14051 GLYPHS > 1 means show glyphs in long form. */
14052
14053 void
14054 dump_glyph_matrix (matrix, glyphs)
14055 struct glyph_matrix *matrix;
14056 int glyphs;
14057 {
14058 int i;
14059 for (i = 0; i < matrix->nrows; ++i)
14060 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14061 }
14062
14063
14064 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14065 the glyph row and area where the glyph comes from. */
14066
14067 void
14068 dump_glyph (row, glyph, area)
14069 struct glyph_row *row;
14070 struct glyph *glyph;
14071 int area;
14072 {
14073 if (glyph->type == CHAR_GLYPH)
14074 {
14075 fprintf (stderr,
14076 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14077 glyph - row->glyphs[TEXT_AREA],
14078 'C',
14079 glyph->charpos,
14080 (BUFFERP (glyph->object)
14081 ? 'B'
14082 : (STRINGP (glyph->object)
14083 ? 'S'
14084 : '-')),
14085 glyph->pixel_width,
14086 glyph->u.ch,
14087 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14088 ? glyph->u.ch
14089 : '.'),
14090 glyph->face_id,
14091 glyph->left_box_line_p,
14092 glyph->right_box_line_p);
14093 }
14094 else if (glyph->type == STRETCH_GLYPH)
14095 {
14096 fprintf (stderr,
14097 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14098 glyph - row->glyphs[TEXT_AREA],
14099 'S',
14100 glyph->charpos,
14101 (BUFFERP (glyph->object)
14102 ? 'B'
14103 : (STRINGP (glyph->object)
14104 ? 'S'
14105 : '-')),
14106 glyph->pixel_width,
14107 0,
14108 '.',
14109 glyph->face_id,
14110 glyph->left_box_line_p,
14111 glyph->right_box_line_p);
14112 }
14113 else if (glyph->type == IMAGE_GLYPH)
14114 {
14115 fprintf (stderr,
14116 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14117 glyph - row->glyphs[TEXT_AREA],
14118 'I',
14119 glyph->charpos,
14120 (BUFFERP (glyph->object)
14121 ? 'B'
14122 : (STRINGP (glyph->object)
14123 ? 'S'
14124 : '-')),
14125 glyph->pixel_width,
14126 glyph->u.img_id,
14127 '.',
14128 glyph->face_id,
14129 glyph->left_box_line_p,
14130 glyph->right_box_line_p);
14131 }
14132 }
14133
14134
14135 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14136 GLYPHS 0 means don't show glyph contents.
14137 GLYPHS 1 means show glyphs in short form
14138 GLYPHS > 1 means show glyphs in long form. */
14139
14140 void
14141 dump_glyph_row (row, vpos, glyphs)
14142 struct glyph_row *row;
14143 int vpos, glyphs;
14144 {
14145 if (glyphs != 1)
14146 {
14147 fprintf (stderr, "Row Start End Used oEI><O\\CTZFesm X Y W H V A P\n");
14148 fprintf (stderr, "=======================================================================\n");
14149
14150 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d%1.1d\
14151 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14152 vpos,
14153 MATRIX_ROW_START_CHARPOS (row),
14154 MATRIX_ROW_END_CHARPOS (row),
14155 row->used[TEXT_AREA],
14156 row->contains_overlapping_glyphs_p,
14157 row->enabled_p,
14158 row->truncated_on_left_p,
14159 row->truncated_on_right_p,
14160 row->overlay_arrow_p,
14161 row->continued_p,
14162 MATRIX_ROW_CONTINUATION_LINE_P (row),
14163 row->displays_text_p,
14164 row->ends_at_zv_p,
14165 row->fill_line_p,
14166 row->ends_in_middle_of_char_p,
14167 row->starts_in_middle_of_char_p,
14168 row->mouse_face_p,
14169 row->x,
14170 row->y,
14171 row->pixel_width,
14172 row->height,
14173 row->visible_height,
14174 row->ascent,
14175 row->phys_ascent);
14176 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14177 row->end.overlay_string_index,
14178 row->continuation_lines_width);
14179 fprintf (stderr, "%9d %5d\n",
14180 CHARPOS (row->start.string_pos),
14181 CHARPOS (row->end.string_pos));
14182 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14183 row->end.dpvec_index);
14184 }
14185
14186 if (glyphs > 1)
14187 {
14188 int area;
14189
14190 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14191 {
14192 struct glyph *glyph = row->glyphs[area];
14193 struct glyph *glyph_end = glyph + row->used[area];
14194
14195 /* Glyph for a line end in text. */
14196 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14197 ++glyph_end;
14198
14199 if (glyph < glyph_end)
14200 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14201
14202 for (; glyph < glyph_end; ++glyph)
14203 dump_glyph (row, glyph, area);
14204 }
14205 }
14206 else if (glyphs == 1)
14207 {
14208 int area;
14209
14210 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14211 {
14212 char *s = (char *) alloca (row->used[area] + 1);
14213 int i;
14214
14215 for (i = 0; i < row->used[area]; ++i)
14216 {
14217 struct glyph *glyph = row->glyphs[area] + i;
14218 if (glyph->type == CHAR_GLYPH
14219 && glyph->u.ch < 0x80
14220 && glyph->u.ch >= ' ')
14221 s[i] = glyph->u.ch;
14222 else
14223 s[i] = '.';
14224 }
14225
14226 s[i] = '\0';
14227 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14228 }
14229 }
14230 }
14231
14232
14233 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14234 Sdump_glyph_matrix, 0, 1, "p",
14235 doc: /* Dump the current matrix of the selected window to stderr.
14236 Shows contents of glyph row structures. With non-nil
14237 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14238 glyphs in short form, otherwise show glyphs in long form. */)
14239 (glyphs)
14240 Lisp_Object glyphs;
14241 {
14242 struct window *w = XWINDOW (selected_window);
14243 struct buffer *buffer = XBUFFER (w->buffer);
14244
14245 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14246 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14247 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14248 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14249 fprintf (stderr, "=============================================\n");
14250 dump_glyph_matrix (w->current_matrix,
14251 NILP (glyphs) ? 0 : XINT (glyphs));
14252 return Qnil;
14253 }
14254
14255
14256 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14257 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14258 ()
14259 {
14260 struct frame *f = XFRAME (selected_frame);
14261 dump_glyph_matrix (f->current_matrix, 1);
14262 return Qnil;
14263 }
14264
14265
14266 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14267 doc: /* Dump glyph row ROW to stderr.
14268 GLYPH 0 means don't dump glyphs.
14269 GLYPH 1 means dump glyphs in short form.
14270 GLYPH > 1 or omitted means dump glyphs in long form. */)
14271 (row, glyphs)
14272 Lisp_Object row, glyphs;
14273 {
14274 struct glyph_matrix *matrix;
14275 int vpos;
14276
14277 CHECK_NUMBER (row);
14278 matrix = XWINDOW (selected_window)->current_matrix;
14279 vpos = XINT (row);
14280 if (vpos >= 0 && vpos < matrix->nrows)
14281 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14282 vpos,
14283 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14284 return Qnil;
14285 }
14286
14287
14288 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14289 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14290 GLYPH 0 means don't dump glyphs.
14291 GLYPH 1 means dump glyphs in short form.
14292 GLYPH > 1 or omitted means dump glyphs in long form. */)
14293 (row, glyphs)
14294 Lisp_Object row, glyphs;
14295 {
14296 struct frame *sf = SELECTED_FRAME ();
14297 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14298 int vpos;
14299
14300 CHECK_NUMBER (row);
14301 vpos = XINT (row);
14302 if (vpos >= 0 && vpos < m->nrows)
14303 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14304 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14305 return Qnil;
14306 }
14307
14308
14309 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14310 doc: /* Toggle tracing of redisplay.
14311 With ARG, turn tracing on if and only if ARG is positive. */)
14312 (arg)
14313 Lisp_Object arg;
14314 {
14315 if (NILP (arg))
14316 trace_redisplay_p = !trace_redisplay_p;
14317 else
14318 {
14319 arg = Fprefix_numeric_value (arg);
14320 trace_redisplay_p = XINT (arg) > 0;
14321 }
14322
14323 return Qnil;
14324 }
14325
14326
14327 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14328 doc: /* Like `format', but print result to stderr.
14329 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14330 (nargs, args)
14331 int nargs;
14332 Lisp_Object *args;
14333 {
14334 Lisp_Object s = Fformat (nargs, args);
14335 fprintf (stderr, "%s", SDATA (s));
14336 return Qnil;
14337 }
14338
14339 #endif /* GLYPH_DEBUG */
14340
14341
14342 \f
14343 /***********************************************************************
14344 Building Desired Matrix Rows
14345 ***********************************************************************/
14346
14347 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14348 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14349
14350 static struct glyph_row *
14351 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14352 struct window *w;
14353 Lisp_Object overlay_arrow_string;
14354 {
14355 struct frame *f = XFRAME (WINDOW_FRAME (w));
14356 struct buffer *buffer = XBUFFER (w->buffer);
14357 struct buffer *old = current_buffer;
14358 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14359 int arrow_len = SCHARS (overlay_arrow_string);
14360 const unsigned char *arrow_end = arrow_string + arrow_len;
14361 const unsigned char *p;
14362 struct it it;
14363 int multibyte_p;
14364 int n_glyphs_before;
14365
14366 set_buffer_temp (buffer);
14367 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14368 it.glyph_row->used[TEXT_AREA] = 0;
14369 SET_TEXT_POS (it.position, 0, 0);
14370
14371 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14372 p = arrow_string;
14373 while (p < arrow_end)
14374 {
14375 Lisp_Object face, ilisp;
14376
14377 /* Get the next character. */
14378 if (multibyte_p)
14379 it.c = string_char_and_length (p, arrow_len, &it.len);
14380 else
14381 it.c = *p, it.len = 1;
14382 p += it.len;
14383
14384 /* Get its face. */
14385 ilisp = make_number (p - arrow_string);
14386 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14387 it.face_id = compute_char_face (f, it.c, face);
14388
14389 /* Compute its width, get its glyphs. */
14390 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14391 SET_TEXT_POS (it.position, -1, -1);
14392 PRODUCE_GLYPHS (&it);
14393
14394 /* If this character doesn't fit any more in the line, we have
14395 to remove some glyphs. */
14396 if (it.current_x > it.last_visible_x)
14397 {
14398 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14399 break;
14400 }
14401 }
14402
14403 set_buffer_temp (old);
14404 return it.glyph_row;
14405 }
14406
14407
14408 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14409 glyphs are only inserted for terminal frames since we can't really
14410 win with truncation glyphs when partially visible glyphs are
14411 involved. Which glyphs to insert is determined by
14412 produce_special_glyphs. */
14413
14414 static void
14415 insert_left_trunc_glyphs (it)
14416 struct it *it;
14417 {
14418 struct it truncate_it;
14419 struct glyph *from, *end, *to, *toend;
14420
14421 xassert (!FRAME_WINDOW_P (it->f));
14422
14423 /* Get the truncation glyphs. */
14424 truncate_it = *it;
14425 truncate_it.current_x = 0;
14426 truncate_it.face_id = DEFAULT_FACE_ID;
14427 truncate_it.glyph_row = &scratch_glyph_row;
14428 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14429 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14430 truncate_it.object = make_number (0);
14431 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14432
14433 /* Overwrite glyphs from IT with truncation glyphs. */
14434 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14435 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14436 to = it->glyph_row->glyphs[TEXT_AREA];
14437 toend = to + it->glyph_row->used[TEXT_AREA];
14438
14439 while (from < end)
14440 *to++ = *from++;
14441
14442 /* There may be padding glyphs left over. Overwrite them too. */
14443 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14444 {
14445 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14446 while (from < end)
14447 *to++ = *from++;
14448 }
14449
14450 if (to > toend)
14451 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14452 }
14453
14454
14455 /* Compute the pixel height and width of IT->glyph_row.
14456
14457 Most of the time, ascent and height of a display line will be equal
14458 to the max_ascent and max_height values of the display iterator
14459 structure. This is not the case if
14460
14461 1. We hit ZV without displaying anything. In this case, max_ascent
14462 and max_height will be zero.
14463
14464 2. We have some glyphs that don't contribute to the line height.
14465 (The glyph row flag contributes_to_line_height_p is for future
14466 pixmap extensions).
14467
14468 The first case is easily covered by using default values because in
14469 these cases, the line height does not really matter, except that it
14470 must not be zero. */
14471
14472 static void
14473 compute_line_metrics (it)
14474 struct it *it;
14475 {
14476 struct glyph_row *row = it->glyph_row;
14477 int area, i;
14478
14479 if (FRAME_WINDOW_P (it->f))
14480 {
14481 int i, min_y, max_y;
14482
14483 /* The line may consist of one space only, that was added to
14484 place the cursor on it. If so, the row's height hasn't been
14485 computed yet. */
14486 if (row->height == 0)
14487 {
14488 if (it->max_ascent + it->max_descent == 0)
14489 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14490 row->ascent = it->max_ascent;
14491 row->height = it->max_ascent + it->max_descent;
14492 row->phys_ascent = it->max_phys_ascent;
14493 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14494 row->extra_line_spacing = it->max_extra_line_spacing;
14495 }
14496
14497 /* Compute the width of this line. */
14498 row->pixel_width = row->x;
14499 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14500 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14501
14502 xassert (row->pixel_width >= 0);
14503 xassert (row->ascent >= 0 && row->height > 0);
14504
14505 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14506 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14507
14508 /* If first line's physical ascent is larger than its logical
14509 ascent, use the physical ascent, and make the row taller.
14510 This makes accented characters fully visible. */
14511 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14512 && row->phys_ascent > row->ascent)
14513 {
14514 row->height += row->phys_ascent - row->ascent;
14515 row->ascent = row->phys_ascent;
14516 }
14517
14518 /* Compute how much of the line is visible. */
14519 row->visible_height = row->height;
14520
14521 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14522 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14523
14524 if (row->y < min_y)
14525 row->visible_height -= min_y - row->y;
14526 if (row->y + row->height > max_y)
14527 row->visible_height -= row->y + row->height - max_y;
14528 }
14529 else
14530 {
14531 row->pixel_width = row->used[TEXT_AREA];
14532 if (row->continued_p)
14533 row->pixel_width -= it->continuation_pixel_width;
14534 else if (row->truncated_on_right_p)
14535 row->pixel_width -= it->truncation_pixel_width;
14536 row->ascent = row->phys_ascent = 0;
14537 row->height = row->phys_height = row->visible_height = 1;
14538 row->extra_line_spacing = 0;
14539 }
14540
14541 /* Compute a hash code for this row. */
14542 row->hash = 0;
14543 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14544 for (i = 0; i < row->used[area]; ++i)
14545 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14546 + row->glyphs[area][i].u.val
14547 + row->glyphs[area][i].face_id
14548 + row->glyphs[area][i].padding_p
14549 + (row->glyphs[area][i].type << 2));
14550
14551 it->max_ascent = it->max_descent = 0;
14552 it->max_phys_ascent = it->max_phys_descent = 0;
14553 }
14554
14555
14556 /* Append one space to the glyph row of iterator IT if doing a
14557 window-based redisplay. The space has the same face as
14558 IT->face_id. Value is non-zero if a space was added.
14559
14560 This function is called to make sure that there is always one glyph
14561 at the end of a glyph row that the cursor can be set on under
14562 window-systems. (If there weren't such a glyph we would not know
14563 how wide and tall a box cursor should be displayed).
14564
14565 At the same time this space let's a nicely handle clearing to the
14566 end of the line if the row ends in italic text. */
14567
14568 static int
14569 append_space_for_newline (it, default_face_p)
14570 struct it *it;
14571 int default_face_p;
14572 {
14573 if (FRAME_WINDOW_P (it->f))
14574 {
14575 int n = it->glyph_row->used[TEXT_AREA];
14576
14577 if (it->glyph_row->glyphs[TEXT_AREA] + n
14578 < it->glyph_row->glyphs[1 + TEXT_AREA])
14579 {
14580 /* Save some values that must not be changed.
14581 Must save IT->c and IT->len because otherwise
14582 ITERATOR_AT_END_P wouldn't work anymore after
14583 append_space_for_newline has been called. */
14584 enum display_element_type saved_what = it->what;
14585 int saved_c = it->c, saved_len = it->len;
14586 int saved_x = it->current_x;
14587 int saved_face_id = it->face_id;
14588 struct text_pos saved_pos;
14589 Lisp_Object saved_object;
14590 struct face *face;
14591
14592 saved_object = it->object;
14593 saved_pos = it->position;
14594
14595 it->what = IT_CHARACTER;
14596 bzero (&it->position, sizeof it->position);
14597 it->object = make_number (0);
14598 it->c = ' ';
14599 it->len = 1;
14600
14601 if (default_face_p)
14602 it->face_id = DEFAULT_FACE_ID;
14603 else if (it->face_before_selective_p)
14604 it->face_id = it->saved_face_id;
14605 face = FACE_FROM_ID (it->f, it->face_id);
14606 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14607
14608 PRODUCE_GLYPHS (it);
14609
14610 it->override_ascent = -1;
14611 it->constrain_row_ascent_descent_p = 0;
14612 it->current_x = saved_x;
14613 it->object = saved_object;
14614 it->position = saved_pos;
14615 it->what = saved_what;
14616 it->face_id = saved_face_id;
14617 it->len = saved_len;
14618 it->c = saved_c;
14619 return 1;
14620 }
14621 }
14622
14623 return 0;
14624 }
14625
14626
14627 /* Extend the face of the last glyph in the text area of IT->glyph_row
14628 to the end of the display line. Called from display_line.
14629 If the glyph row is empty, add a space glyph to it so that we
14630 know the face to draw. Set the glyph row flag fill_line_p. */
14631
14632 static void
14633 extend_face_to_end_of_line (it)
14634 struct it *it;
14635 {
14636 struct face *face;
14637 struct frame *f = it->f;
14638
14639 /* If line is already filled, do nothing. */
14640 if (it->current_x >= it->last_visible_x)
14641 return;
14642
14643 /* Face extension extends the background and box of IT->face_id
14644 to the end of the line. If the background equals the background
14645 of the frame, we don't have to do anything. */
14646 if (it->face_before_selective_p)
14647 face = FACE_FROM_ID (it->f, it->saved_face_id);
14648 else
14649 face = FACE_FROM_ID (f, it->face_id);
14650
14651 if (FRAME_WINDOW_P (f)
14652 && face->box == FACE_NO_BOX
14653 && face->background == FRAME_BACKGROUND_PIXEL (f)
14654 && !face->stipple)
14655 return;
14656
14657 /* Set the glyph row flag indicating that the face of the last glyph
14658 in the text area has to be drawn to the end of the text area. */
14659 it->glyph_row->fill_line_p = 1;
14660
14661 /* If current character of IT is not ASCII, make sure we have the
14662 ASCII face. This will be automatically undone the next time
14663 get_next_display_element returns a multibyte character. Note
14664 that the character will always be single byte in unibyte text. */
14665 if (!SINGLE_BYTE_CHAR_P (it->c))
14666 {
14667 it->face_id = FACE_FOR_CHAR (f, face, 0);
14668 }
14669
14670 if (FRAME_WINDOW_P (f))
14671 {
14672 /* If the row is empty, add a space with the current face of IT,
14673 so that we know which face to draw. */
14674 if (it->glyph_row->used[TEXT_AREA] == 0)
14675 {
14676 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14677 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14678 it->glyph_row->used[TEXT_AREA] = 1;
14679 }
14680 }
14681 else
14682 {
14683 /* Save some values that must not be changed. */
14684 int saved_x = it->current_x;
14685 struct text_pos saved_pos;
14686 Lisp_Object saved_object;
14687 enum display_element_type saved_what = it->what;
14688 int saved_face_id = it->face_id;
14689
14690 saved_object = it->object;
14691 saved_pos = it->position;
14692
14693 it->what = IT_CHARACTER;
14694 bzero (&it->position, sizeof it->position);
14695 it->object = make_number (0);
14696 it->c = ' ';
14697 it->len = 1;
14698 it->face_id = face->id;
14699
14700 PRODUCE_GLYPHS (it);
14701
14702 while (it->current_x <= it->last_visible_x)
14703 PRODUCE_GLYPHS (it);
14704
14705 /* Don't count these blanks really. It would let us insert a left
14706 truncation glyph below and make us set the cursor on them, maybe. */
14707 it->current_x = saved_x;
14708 it->object = saved_object;
14709 it->position = saved_pos;
14710 it->what = saved_what;
14711 it->face_id = saved_face_id;
14712 }
14713 }
14714
14715
14716 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14717 trailing whitespace. */
14718
14719 static int
14720 trailing_whitespace_p (charpos)
14721 int charpos;
14722 {
14723 int bytepos = CHAR_TO_BYTE (charpos);
14724 int c = 0;
14725
14726 while (bytepos < ZV_BYTE
14727 && (c = FETCH_CHAR (bytepos),
14728 c == ' ' || c == '\t'))
14729 ++bytepos;
14730
14731 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14732 {
14733 if (bytepos != PT_BYTE)
14734 return 1;
14735 }
14736 return 0;
14737 }
14738
14739
14740 /* Highlight trailing whitespace, if any, in ROW. */
14741
14742 void
14743 highlight_trailing_whitespace (f, row)
14744 struct frame *f;
14745 struct glyph_row *row;
14746 {
14747 int used = row->used[TEXT_AREA];
14748
14749 if (used)
14750 {
14751 struct glyph *start = row->glyphs[TEXT_AREA];
14752 struct glyph *glyph = start + used - 1;
14753
14754 /* Skip over glyphs inserted to display the cursor at the
14755 end of a line, for extending the face of the last glyph
14756 to the end of the line on terminals, and for truncation
14757 and continuation glyphs. */
14758 while (glyph >= start
14759 && glyph->type == CHAR_GLYPH
14760 && INTEGERP (glyph->object))
14761 --glyph;
14762
14763 /* If last glyph is a space or stretch, and it's trailing
14764 whitespace, set the face of all trailing whitespace glyphs in
14765 IT->glyph_row to `trailing-whitespace'. */
14766 if (glyph >= start
14767 && BUFFERP (glyph->object)
14768 && (glyph->type == STRETCH_GLYPH
14769 || (glyph->type == CHAR_GLYPH
14770 && glyph->u.ch == ' '))
14771 && trailing_whitespace_p (glyph->charpos))
14772 {
14773 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14774 if (face_id < 0)
14775 return;
14776
14777 while (glyph >= start
14778 && BUFFERP (glyph->object)
14779 && (glyph->type == STRETCH_GLYPH
14780 || (glyph->type == CHAR_GLYPH
14781 && glyph->u.ch == ' ')))
14782 (glyph--)->face_id = face_id;
14783 }
14784 }
14785 }
14786
14787
14788 /* Value is non-zero if glyph row ROW in window W should be
14789 used to hold the cursor. */
14790
14791 static int
14792 cursor_row_p (w, row)
14793 struct window *w;
14794 struct glyph_row *row;
14795 {
14796 int cursor_row_p = 1;
14797
14798 if (PT == MATRIX_ROW_END_CHARPOS (row))
14799 {
14800 /* If the row ends with a newline from a string, we don't want
14801 the cursor there (if the row is continued it doesn't end in a
14802 newline). */
14803 if (CHARPOS (row->end.string_pos) >= 0)
14804 cursor_row_p = row->continued_p;
14805 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14806 {
14807 /* If the row ends in middle of a real character,
14808 and the line is continued, we want the cursor here.
14809 That's because MATRIX_ROW_END_CHARPOS would equal
14810 PT if PT is before the character. */
14811 if (!row->ends_in_ellipsis_p)
14812 cursor_row_p = row->continued_p;
14813 else
14814 /* If the row ends in an ellipsis, then
14815 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
14816 We want that position to be displayed after the ellipsis. */
14817 cursor_row_p = 0;
14818 }
14819 /* If the row ends at ZV, display the cursor at the end of that
14820 row instead of at the start of the row below. */
14821 else if (row->ends_at_zv_p)
14822 cursor_row_p = 1;
14823 else
14824 cursor_row_p = 0;
14825 }
14826
14827 return cursor_row_p;
14828 }
14829
14830
14831 /* Construct the glyph row IT->glyph_row in the desired matrix of
14832 IT->w from text at the current position of IT. See dispextern.h
14833 for an overview of struct it. Value is non-zero if
14834 IT->glyph_row displays text, as opposed to a line displaying ZV
14835 only. */
14836
14837 static int
14838 display_line (it)
14839 struct it *it;
14840 {
14841 struct glyph_row *row = it->glyph_row;
14842 int overlay_arrow_bitmap;
14843 Lisp_Object overlay_arrow_string;
14844
14845 /* We always start displaying at hpos zero even if hscrolled. */
14846 xassert (it->hpos == 0 && it->current_x == 0);
14847
14848 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
14849 >= it->w->desired_matrix->nrows)
14850 {
14851 it->w->nrows_scale_factor++;
14852 fonts_changed_p = 1;
14853 return 0;
14854 }
14855
14856 /* Is IT->w showing the region? */
14857 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
14858
14859 /* Clear the result glyph row and enable it. */
14860 prepare_desired_row (row);
14861
14862 row->y = it->current_y;
14863 row->start = it->start;
14864 row->continuation_lines_width = it->continuation_lines_width;
14865 row->displays_text_p = 1;
14866 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
14867 it->starts_in_middle_of_char_p = 0;
14868
14869 /* Arrange the overlays nicely for our purposes. Usually, we call
14870 display_line on only one line at a time, in which case this
14871 can't really hurt too much, or we call it on lines which appear
14872 one after another in the buffer, in which case all calls to
14873 recenter_overlay_lists but the first will be pretty cheap. */
14874 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
14875
14876 /* Move over display elements that are not visible because we are
14877 hscrolled. This may stop at an x-position < IT->first_visible_x
14878 if the first glyph is partially visible or if we hit a line end. */
14879 if (it->current_x < it->first_visible_x)
14880 {
14881 move_it_in_display_line_to (it, ZV, it->first_visible_x,
14882 MOVE_TO_POS | MOVE_TO_X);
14883 }
14884
14885 /* Get the initial row height. This is either the height of the
14886 text hscrolled, if there is any, or zero. */
14887 row->ascent = it->max_ascent;
14888 row->height = it->max_ascent + it->max_descent;
14889 row->phys_ascent = it->max_phys_ascent;
14890 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14891 row->extra_line_spacing = it->max_extra_line_spacing;
14892
14893 /* Loop generating characters. The loop is left with IT on the next
14894 character to display. */
14895 while (1)
14896 {
14897 int n_glyphs_before, hpos_before, x_before;
14898 int x, i, nglyphs;
14899 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
14900
14901 /* Retrieve the next thing to display. Value is zero if end of
14902 buffer reached. */
14903 if (!get_next_display_element (it))
14904 {
14905 /* Maybe add a space at the end of this line that is used to
14906 display the cursor there under X. Set the charpos of the
14907 first glyph of blank lines not corresponding to any text
14908 to -1. */
14909 #ifdef HAVE_WINDOW_SYSTEM
14910 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
14911 row->exact_window_width_line_p = 1;
14912 else
14913 #endif /* HAVE_WINDOW_SYSTEM */
14914 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
14915 || row->used[TEXT_AREA] == 0)
14916 {
14917 row->glyphs[TEXT_AREA]->charpos = -1;
14918 row->displays_text_p = 0;
14919
14920 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
14921 && (!MINI_WINDOW_P (it->w)
14922 || (minibuf_level && EQ (it->window, minibuf_window))))
14923 row->indicate_empty_line_p = 1;
14924 }
14925
14926 it->continuation_lines_width = 0;
14927 row->ends_at_zv_p = 1;
14928 break;
14929 }
14930
14931 /* Now, get the metrics of what we want to display. This also
14932 generates glyphs in `row' (which is IT->glyph_row). */
14933 n_glyphs_before = row->used[TEXT_AREA];
14934 x = it->current_x;
14935
14936 /* Remember the line height so far in case the next element doesn't
14937 fit on the line. */
14938 if (!it->truncate_lines_p)
14939 {
14940 ascent = it->max_ascent;
14941 descent = it->max_descent;
14942 phys_ascent = it->max_phys_ascent;
14943 phys_descent = it->max_phys_descent;
14944 }
14945
14946 PRODUCE_GLYPHS (it);
14947
14948 /* If this display element was in marginal areas, continue with
14949 the next one. */
14950 if (it->area != TEXT_AREA)
14951 {
14952 row->ascent = max (row->ascent, it->max_ascent);
14953 row->height = max (row->height, it->max_ascent + it->max_descent);
14954 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14955 row->phys_height = max (row->phys_height,
14956 it->max_phys_ascent + it->max_phys_descent);
14957 row->extra_line_spacing = max (row->extra_line_spacing,
14958 it->max_extra_line_spacing);
14959 set_iterator_to_next (it, 1);
14960 continue;
14961 }
14962
14963 /* Does the display element fit on the line? If we truncate
14964 lines, we should draw past the right edge of the window. If
14965 we don't truncate, we want to stop so that we can display the
14966 continuation glyph before the right margin. If lines are
14967 continued, there are two possible strategies for characters
14968 resulting in more than 1 glyph (e.g. tabs): Display as many
14969 glyphs as possible in this line and leave the rest for the
14970 continuation line, or display the whole element in the next
14971 line. Original redisplay did the former, so we do it also. */
14972 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
14973 hpos_before = it->hpos;
14974 x_before = x;
14975
14976 if (/* Not a newline. */
14977 nglyphs > 0
14978 /* Glyphs produced fit entirely in the line. */
14979 && it->current_x < it->last_visible_x)
14980 {
14981 it->hpos += nglyphs;
14982 row->ascent = max (row->ascent, it->max_ascent);
14983 row->height = max (row->height, it->max_ascent + it->max_descent);
14984 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
14985 row->phys_height = max (row->phys_height,
14986 it->max_phys_ascent + it->max_phys_descent);
14987 row->extra_line_spacing = max (row->extra_line_spacing,
14988 it->max_extra_line_spacing);
14989 if (it->current_x - it->pixel_width < it->first_visible_x)
14990 row->x = x - it->first_visible_x;
14991 }
14992 else
14993 {
14994 int new_x;
14995 struct glyph *glyph;
14996
14997 for (i = 0; i < nglyphs; ++i, x = new_x)
14998 {
14999 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15000 new_x = x + glyph->pixel_width;
15001
15002 if (/* Lines are continued. */
15003 !it->truncate_lines_p
15004 && (/* Glyph doesn't fit on the line. */
15005 new_x > it->last_visible_x
15006 /* Or it fits exactly on a window system frame. */
15007 || (new_x == it->last_visible_x
15008 && FRAME_WINDOW_P (it->f))))
15009 {
15010 /* End of a continued line. */
15011
15012 if (it->hpos == 0
15013 || (new_x == it->last_visible_x
15014 && FRAME_WINDOW_P (it->f)))
15015 {
15016 /* Current glyph is the only one on the line or
15017 fits exactly on the line. We must continue
15018 the line because we can't draw the cursor
15019 after the glyph. */
15020 row->continued_p = 1;
15021 it->current_x = new_x;
15022 it->continuation_lines_width += new_x;
15023 ++it->hpos;
15024 if (i == nglyphs - 1)
15025 {
15026 set_iterator_to_next (it, 1);
15027 #ifdef HAVE_WINDOW_SYSTEM
15028 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15029 {
15030 if (!get_next_display_element (it))
15031 {
15032 row->exact_window_width_line_p = 1;
15033 it->continuation_lines_width = 0;
15034 row->continued_p = 0;
15035 row->ends_at_zv_p = 1;
15036 }
15037 else if (ITERATOR_AT_END_OF_LINE_P (it))
15038 {
15039 row->continued_p = 0;
15040 row->exact_window_width_line_p = 1;
15041 }
15042 }
15043 #endif /* HAVE_WINDOW_SYSTEM */
15044 }
15045 }
15046 else if (CHAR_GLYPH_PADDING_P (*glyph)
15047 && !FRAME_WINDOW_P (it->f))
15048 {
15049 /* A padding glyph that doesn't fit on this line.
15050 This means the whole character doesn't fit
15051 on the line. */
15052 row->used[TEXT_AREA] = n_glyphs_before;
15053
15054 /* Fill the rest of the row with continuation
15055 glyphs like in 20.x. */
15056 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15057 < row->glyphs[1 + TEXT_AREA])
15058 produce_special_glyphs (it, IT_CONTINUATION);
15059
15060 row->continued_p = 1;
15061 it->current_x = x_before;
15062 it->continuation_lines_width += x_before;
15063
15064 /* Restore the height to what it was before the
15065 element not fitting on the line. */
15066 it->max_ascent = ascent;
15067 it->max_descent = descent;
15068 it->max_phys_ascent = phys_ascent;
15069 it->max_phys_descent = phys_descent;
15070 }
15071 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15072 {
15073 /* A TAB that extends past the right edge of the
15074 window. This produces a single glyph on
15075 window system frames. We leave the glyph in
15076 this row and let it fill the row, but don't
15077 consume the TAB. */
15078 it->continuation_lines_width += it->last_visible_x;
15079 row->ends_in_middle_of_char_p = 1;
15080 row->continued_p = 1;
15081 glyph->pixel_width = it->last_visible_x - x;
15082 it->starts_in_middle_of_char_p = 1;
15083 }
15084 else
15085 {
15086 /* Something other than a TAB that draws past
15087 the right edge of the window. Restore
15088 positions to values before the element. */
15089 row->used[TEXT_AREA] = n_glyphs_before + i;
15090
15091 /* Display continuation glyphs. */
15092 if (!FRAME_WINDOW_P (it->f))
15093 produce_special_glyphs (it, IT_CONTINUATION);
15094 row->continued_p = 1;
15095
15096 it->continuation_lines_width += x;
15097
15098 if (nglyphs > 1 && i > 0)
15099 {
15100 row->ends_in_middle_of_char_p = 1;
15101 it->starts_in_middle_of_char_p = 1;
15102 }
15103
15104 /* Restore the height to what it was before the
15105 element not fitting on the line. */
15106 it->max_ascent = ascent;
15107 it->max_descent = descent;
15108 it->max_phys_ascent = phys_ascent;
15109 it->max_phys_descent = phys_descent;
15110 }
15111
15112 break;
15113 }
15114 else if (new_x > it->first_visible_x)
15115 {
15116 /* Increment number of glyphs actually displayed. */
15117 ++it->hpos;
15118
15119 if (x < it->first_visible_x)
15120 /* Glyph is partially visible, i.e. row starts at
15121 negative X position. */
15122 row->x = x - it->first_visible_x;
15123 }
15124 else
15125 {
15126 /* Glyph is completely off the left margin of the
15127 window. This should not happen because of the
15128 move_it_in_display_line at the start of this
15129 function, unless the text display area of the
15130 window is empty. */
15131 xassert (it->first_visible_x <= it->last_visible_x);
15132 }
15133 }
15134
15135 row->ascent = max (row->ascent, it->max_ascent);
15136 row->height = max (row->height, it->max_ascent + it->max_descent);
15137 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15138 row->phys_height = max (row->phys_height,
15139 it->max_phys_ascent + it->max_phys_descent);
15140 row->extra_line_spacing = max (row->extra_line_spacing,
15141 it->max_extra_line_spacing);
15142
15143 /* End of this display line if row is continued. */
15144 if (row->continued_p || row->ends_at_zv_p)
15145 break;
15146 }
15147
15148 at_end_of_line:
15149 /* Is this a line end? If yes, we're also done, after making
15150 sure that a non-default face is extended up to the right
15151 margin of the window. */
15152 if (ITERATOR_AT_END_OF_LINE_P (it))
15153 {
15154 int used_before = row->used[TEXT_AREA];
15155
15156 row->ends_in_newline_from_string_p = STRINGP (it->object);
15157
15158 #ifdef HAVE_WINDOW_SYSTEM
15159 /* Add a space at the end of the line that is used to
15160 display the cursor there. */
15161 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15162 append_space_for_newline (it, 0);
15163 #endif /* HAVE_WINDOW_SYSTEM */
15164
15165 /* Extend the face to the end of the line. */
15166 extend_face_to_end_of_line (it);
15167
15168 /* Make sure we have the position. */
15169 if (used_before == 0)
15170 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15171
15172 /* Consume the line end. This skips over invisible lines. */
15173 set_iterator_to_next (it, 1);
15174 it->continuation_lines_width = 0;
15175 break;
15176 }
15177
15178 /* Proceed with next display element. Note that this skips
15179 over lines invisible because of selective display. */
15180 set_iterator_to_next (it, 1);
15181
15182 /* If we truncate lines, we are done when the last displayed
15183 glyphs reach past the right margin of the window. */
15184 if (it->truncate_lines_p
15185 && (FRAME_WINDOW_P (it->f)
15186 ? (it->current_x >= it->last_visible_x)
15187 : (it->current_x > it->last_visible_x)))
15188 {
15189 /* Maybe add truncation glyphs. */
15190 if (!FRAME_WINDOW_P (it->f))
15191 {
15192 int i, n;
15193
15194 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15195 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15196 break;
15197
15198 for (n = row->used[TEXT_AREA]; i < n; ++i)
15199 {
15200 row->used[TEXT_AREA] = i;
15201 produce_special_glyphs (it, IT_TRUNCATION);
15202 }
15203 }
15204 #ifdef HAVE_WINDOW_SYSTEM
15205 else
15206 {
15207 /* Don't truncate if we can overflow newline into fringe. */
15208 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15209 {
15210 if (!get_next_display_element (it))
15211 {
15212 it->continuation_lines_width = 0;
15213 row->ends_at_zv_p = 1;
15214 row->exact_window_width_line_p = 1;
15215 break;
15216 }
15217 if (ITERATOR_AT_END_OF_LINE_P (it))
15218 {
15219 row->exact_window_width_line_p = 1;
15220 goto at_end_of_line;
15221 }
15222 }
15223 }
15224 #endif /* HAVE_WINDOW_SYSTEM */
15225
15226 row->truncated_on_right_p = 1;
15227 it->continuation_lines_width = 0;
15228 reseat_at_next_visible_line_start (it, 0);
15229 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15230 it->hpos = hpos_before;
15231 it->current_x = x_before;
15232 break;
15233 }
15234 }
15235
15236 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15237 at the left window margin. */
15238 if (it->first_visible_x
15239 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15240 {
15241 if (!FRAME_WINDOW_P (it->f))
15242 insert_left_trunc_glyphs (it);
15243 row->truncated_on_left_p = 1;
15244 }
15245
15246 /* If the start of this line is the overlay arrow-position, then
15247 mark this glyph row as the one containing the overlay arrow.
15248 This is clearly a mess with variable size fonts. It would be
15249 better to let it be displayed like cursors under X. */
15250 if (! overlay_arrow_seen
15251 && (overlay_arrow_string
15252 = overlay_arrow_at_row (it, row, &overlay_arrow_bitmap),
15253 !NILP (overlay_arrow_string)))
15254 {
15255 /* Overlay arrow in window redisplay is a fringe bitmap. */
15256 if (STRINGP (overlay_arrow_string))
15257 {
15258 struct glyph_row *arrow_row
15259 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15260 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15261 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15262 struct glyph *p = row->glyphs[TEXT_AREA];
15263 struct glyph *p2, *end;
15264
15265 /* Copy the arrow glyphs. */
15266 while (glyph < arrow_end)
15267 *p++ = *glyph++;
15268
15269 /* Throw away padding glyphs. */
15270 p2 = p;
15271 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15272 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15273 ++p2;
15274 if (p2 > p)
15275 {
15276 while (p2 < end)
15277 *p++ = *p2++;
15278 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15279 }
15280 }
15281 else
15282 {
15283 it->w->overlay_arrow_bitmap = overlay_arrow_bitmap;
15284 row->overlay_arrow_p = 1;
15285 }
15286 overlay_arrow_seen = 1;
15287 }
15288
15289 /* Compute pixel dimensions of this line. */
15290 compute_line_metrics (it);
15291
15292 /* Remember the position at which this line ends. */
15293 row->end = it->current;
15294
15295 /* Record whether this row ends inside an ellipsis. */
15296 row->ends_in_ellipsis_p
15297 = (it->method == GET_FROM_DISPLAY_VECTOR
15298 && it->ellipsis_p);
15299
15300 /* Save fringe bitmaps in this row. */
15301 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15302 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15303 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15304 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15305
15306 it->left_user_fringe_bitmap = 0;
15307 it->left_user_fringe_face_id = 0;
15308 it->right_user_fringe_bitmap = 0;
15309 it->right_user_fringe_face_id = 0;
15310
15311 /* Maybe set the cursor. */
15312 if (it->w->cursor.vpos < 0
15313 && PT >= MATRIX_ROW_START_CHARPOS (row)
15314 && PT <= MATRIX_ROW_END_CHARPOS (row)
15315 && cursor_row_p (it->w, row))
15316 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15317
15318 /* Highlight trailing whitespace. */
15319 if (!NILP (Vshow_trailing_whitespace))
15320 highlight_trailing_whitespace (it->f, it->glyph_row);
15321
15322 /* Prepare for the next line. This line starts horizontally at (X
15323 HPOS) = (0 0). Vertical positions are incremented. As a
15324 convenience for the caller, IT->glyph_row is set to the next
15325 row to be used. */
15326 it->current_x = it->hpos = 0;
15327 it->current_y += row->height;
15328 ++it->vpos;
15329 ++it->glyph_row;
15330 it->start = it->current;
15331 return row->displays_text_p;
15332 }
15333
15334
15335 \f
15336 /***********************************************************************
15337 Menu Bar
15338 ***********************************************************************/
15339
15340 /* Redisplay the menu bar in the frame for window W.
15341
15342 The menu bar of X frames that don't have X toolkit support is
15343 displayed in a special window W->frame->menu_bar_window.
15344
15345 The menu bar of terminal frames is treated specially as far as
15346 glyph matrices are concerned. Menu bar lines are not part of
15347 windows, so the update is done directly on the frame matrix rows
15348 for the menu bar. */
15349
15350 static void
15351 display_menu_bar (w)
15352 struct window *w;
15353 {
15354 struct frame *f = XFRAME (WINDOW_FRAME (w));
15355 struct it it;
15356 Lisp_Object items;
15357 int i;
15358
15359 /* Don't do all this for graphical frames. */
15360 #ifdef HAVE_NTGUI
15361 if (!NILP (Vwindow_system))
15362 return;
15363 #endif
15364 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15365 if (FRAME_X_P (f))
15366 return;
15367 #endif
15368 #ifdef MAC_OS
15369 if (FRAME_MAC_P (f))
15370 return;
15371 #endif
15372
15373 #ifdef USE_X_TOOLKIT
15374 xassert (!FRAME_WINDOW_P (f));
15375 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15376 it.first_visible_x = 0;
15377 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15378 #else /* not USE_X_TOOLKIT */
15379 if (FRAME_WINDOW_P (f))
15380 {
15381 /* Menu bar lines are displayed in the desired matrix of the
15382 dummy window menu_bar_window. */
15383 struct window *menu_w;
15384 xassert (WINDOWP (f->menu_bar_window));
15385 menu_w = XWINDOW (f->menu_bar_window);
15386 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15387 MENU_FACE_ID);
15388 it.first_visible_x = 0;
15389 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15390 }
15391 else
15392 {
15393 /* This is a TTY frame, i.e. character hpos/vpos are used as
15394 pixel x/y. */
15395 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15396 MENU_FACE_ID);
15397 it.first_visible_x = 0;
15398 it.last_visible_x = FRAME_COLS (f);
15399 }
15400 #endif /* not USE_X_TOOLKIT */
15401
15402 if (! mode_line_inverse_video)
15403 /* Force the menu-bar to be displayed in the default face. */
15404 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15405
15406 /* Clear all rows of the menu bar. */
15407 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15408 {
15409 struct glyph_row *row = it.glyph_row + i;
15410 clear_glyph_row (row);
15411 row->enabled_p = 1;
15412 row->full_width_p = 1;
15413 }
15414
15415 /* Display all items of the menu bar. */
15416 items = FRAME_MENU_BAR_ITEMS (it.f);
15417 for (i = 0; i < XVECTOR (items)->size; i += 4)
15418 {
15419 Lisp_Object string;
15420
15421 /* Stop at nil string. */
15422 string = AREF (items, i + 1);
15423 if (NILP (string))
15424 break;
15425
15426 /* Remember where item was displayed. */
15427 AREF (items, i + 3) = make_number (it.hpos);
15428
15429 /* Display the item, pad with one space. */
15430 if (it.current_x < it.last_visible_x)
15431 display_string (NULL, string, Qnil, 0, 0, &it,
15432 SCHARS (string) + 1, 0, 0, -1);
15433 }
15434
15435 /* Fill out the line with spaces. */
15436 if (it.current_x < it.last_visible_x)
15437 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15438
15439 /* Compute the total height of the lines. */
15440 compute_line_metrics (&it);
15441 }
15442
15443
15444 \f
15445 /***********************************************************************
15446 Mode Line
15447 ***********************************************************************/
15448
15449 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15450 FORCE is non-zero, redisplay mode lines unconditionally.
15451 Otherwise, redisplay only mode lines that are garbaged. Value is
15452 the number of windows whose mode lines were redisplayed. */
15453
15454 static int
15455 redisplay_mode_lines (window, force)
15456 Lisp_Object window;
15457 int force;
15458 {
15459 int nwindows = 0;
15460
15461 while (!NILP (window))
15462 {
15463 struct window *w = XWINDOW (window);
15464
15465 if (WINDOWP (w->hchild))
15466 nwindows += redisplay_mode_lines (w->hchild, force);
15467 else if (WINDOWP (w->vchild))
15468 nwindows += redisplay_mode_lines (w->vchild, force);
15469 else if (force
15470 || FRAME_GARBAGED_P (XFRAME (w->frame))
15471 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15472 {
15473 struct text_pos lpoint;
15474 struct buffer *old = current_buffer;
15475
15476 /* Set the window's buffer for the mode line display. */
15477 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15478 set_buffer_internal_1 (XBUFFER (w->buffer));
15479
15480 /* Point refers normally to the selected window. For any
15481 other window, set up appropriate value. */
15482 if (!EQ (window, selected_window))
15483 {
15484 struct text_pos pt;
15485
15486 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15487 if (CHARPOS (pt) < BEGV)
15488 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15489 else if (CHARPOS (pt) > (ZV - 1))
15490 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15491 else
15492 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15493 }
15494
15495 /* Display mode lines. */
15496 clear_glyph_matrix (w->desired_matrix);
15497 if (display_mode_lines (w))
15498 {
15499 ++nwindows;
15500 w->must_be_updated_p = 1;
15501 }
15502
15503 /* Restore old settings. */
15504 set_buffer_internal_1 (old);
15505 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15506 }
15507
15508 window = w->next;
15509 }
15510
15511 return nwindows;
15512 }
15513
15514
15515 /* Display the mode and/or top line of window W. Value is the number
15516 of mode lines displayed. */
15517
15518 static int
15519 display_mode_lines (w)
15520 struct window *w;
15521 {
15522 Lisp_Object old_selected_window, old_selected_frame;
15523 int n = 0;
15524
15525 old_selected_frame = selected_frame;
15526 selected_frame = w->frame;
15527 old_selected_window = selected_window;
15528 XSETWINDOW (selected_window, w);
15529
15530 /* These will be set while the mode line specs are processed. */
15531 line_number_displayed = 0;
15532 w->column_number_displayed = Qnil;
15533
15534 if (WINDOW_WANTS_MODELINE_P (w))
15535 {
15536 struct window *sel_w = XWINDOW (old_selected_window);
15537
15538 /* Select mode line face based on the real selected window. */
15539 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15540 current_buffer->mode_line_format);
15541 ++n;
15542 }
15543
15544 if (WINDOW_WANTS_HEADER_LINE_P (w))
15545 {
15546 display_mode_line (w, HEADER_LINE_FACE_ID,
15547 current_buffer->header_line_format);
15548 ++n;
15549 }
15550
15551 selected_frame = old_selected_frame;
15552 selected_window = old_selected_window;
15553 return n;
15554 }
15555
15556
15557 /* Display mode or top line of window W. FACE_ID specifies which line
15558 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15559 FORMAT is the mode line format to display. Value is the pixel
15560 height of the mode line displayed. */
15561
15562 static int
15563 display_mode_line (w, face_id, format)
15564 struct window *w;
15565 enum face_id face_id;
15566 Lisp_Object format;
15567 {
15568 struct it it;
15569 struct face *face;
15570
15571 init_iterator (&it, w, -1, -1, NULL, face_id);
15572 prepare_desired_row (it.glyph_row);
15573
15574 it.glyph_row->mode_line_p = 1;
15575
15576 if (! mode_line_inverse_video)
15577 /* Force the mode-line to be displayed in the default face. */
15578 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15579
15580 /* Temporarily make frame's keyboard the current kboard so that
15581 kboard-local variables in the mode_line_format will get the right
15582 values. */
15583 push_frame_kboard (it.f);
15584 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15585 pop_frame_kboard ();
15586
15587 /* Fill up with spaces. */
15588 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15589
15590 compute_line_metrics (&it);
15591 it.glyph_row->full_width_p = 1;
15592 it.glyph_row->continued_p = 0;
15593 it.glyph_row->truncated_on_left_p = 0;
15594 it.glyph_row->truncated_on_right_p = 0;
15595
15596 /* Make a 3D mode-line have a shadow at its right end. */
15597 face = FACE_FROM_ID (it.f, face_id);
15598 extend_face_to_end_of_line (&it);
15599 if (face->box != FACE_NO_BOX)
15600 {
15601 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15602 + it.glyph_row->used[TEXT_AREA] - 1);
15603 last->right_box_line_p = 1;
15604 }
15605
15606 return it.glyph_row->height;
15607 }
15608
15609 /* Alist that caches the results of :propertize.
15610 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
15611 Lisp_Object mode_line_proptrans_alist;
15612
15613 /* List of strings making up the mode-line. */
15614 Lisp_Object mode_line_string_list;
15615
15616 /* Base face property when building propertized mode line string. */
15617 static Lisp_Object mode_line_string_face;
15618 static Lisp_Object mode_line_string_face_prop;
15619
15620
15621 /* Contribute ELT to the mode line for window IT->w. How it
15622 translates into text depends on its data type.
15623
15624 IT describes the display environment in which we display, as usual.
15625
15626 DEPTH is the depth in recursion. It is used to prevent
15627 infinite recursion here.
15628
15629 FIELD_WIDTH is the number of characters the display of ELT should
15630 occupy in the mode line, and PRECISION is the maximum number of
15631 characters to display from ELT's representation. See
15632 display_string for details.
15633
15634 Returns the hpos of the end of the text generated by ELT.
15635
15636 PROPS is a property list to add to any string we encounter.
15637
15638 If RISKY is nonzero, remove (disregard) any properties in any string
15639 we encounter, and ignore :eval and :propertize.
15640
15641 If the global variable `frame_title_ptr' is non-NULL, then the output
15642 is passed to `store_frame_title' instead of `display_string'. */
15643
15644 static int
15645 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15646 struct it *it;
15647 int depth;
15648 int field_width, precision;
15649 Lisp_Object elt, props;
15650 int risky;
15651 {
15652 int n = 0, field, prec;
15653 int literal = 0;
15654
15655 tail_recurse:
15656 if (depth > 100)
15657 elt = build_string ("*too-deep*");
15658
15659 depth++;
15660
15661 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15662 {
15663 case Lisp_String:
15664 {
15665 /* A string: output it and check for %-constructs within it. */
15666 unsigned char c;
15667 const unsigned char *this, *lisp_string;
15668
15669 if (!NILP (props) || risky)
15670 {
15671 Lisp_Object oprops, aelt;
15672 oprops = Ftext_properties_at (make_number (0), elt);
15673
15674 /* If the starting string's properties are not what
15675 we want, translate the string. Also, if the string
15676 is risky, do that anyway. */
15677
15678 if (NILP (Fequal (props, oprops)) || risky)
15679 {
15680 /* If the starting string has properties,
15681 merge the specified ones onto the existing ones. */
15682 if (! NILP (oprops) && !risky)
15683 {
15684 Lisp_Object tem;
15685
15686 oprops = Fcopy_sequence (oprops);
15687 tem = props;
15688 while (CONSP (tem))
15689 {
15690 oprops = Fplist_put (oprops, XCAR (tem),
15691 XCAR (XCDR (tem)));
15692 tem = XCDR (XCDR (tem));
15693 }
15694 props = oprops;
15695 }
15696
15697 aelt = Fassoc (elt, mode_line_proptrans_alist);
15698 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15699 {
15700 mode_line_proptrans_alist
15701 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15702 elt = XCAR (aelt);
15703 }
15704 else
15705 {
15706 Lisp_Object tem;
15707
15708 elt = Fcopy_sequence (elt);
15709 Fset_text_properties (make_number (0), Flength (elt),
15710 props, elt);
15711 /* Add this item to mode_line_proptrans_alist. */
15712 mode_line_proptrans_alist
15713 = Fcons (Fcons (elt, props),
15714 mode_line_proptrans_alist);
15715 /* Truncate mode_line_proptrans_alist
15716 to at most 50 elements. */
15717 tem = Fnthcdr (make_number (50),
15718 mode_line_proptrans_alist);
15719 if (! NILP (tem))
15720 XSETCDR (tem, Qnil);
15721 }
15722 }
15723 }
15724
15725 this = SDATA (elt);
15726 lisp_string = this;
15727
15728 if (literal)
15729 {
15730 prec = precision - n;
15731 if (frame_title_ptr)
15732 n += store_frame_title (SDATA (elt), -1, prec);
15733 else if (!NILP (mode_line_string_list))
15734 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15735 else
15736 n += display_string (NULL, elt, Qnil, 0, 0, it,
15737 0, prec, 0, STRING_MULTIBYTE (elt));
15738
15739 break;
15740 }
15741
15742 while ((precision <= 0 || n < precision)
15743 && *this
15744 && (frame_title_ptr
15745 || !NILP (mode_line_string_list)
15746 || it->current_x < it->last_visible_x))
15747 {
15748 const unsigned char *last = this;
15749
15750 /* Advance to end of string or next format specifier. */
15751 while ((c = *this++) != '\0' && c != '%')
15752 ;
15753
15754 if (this - 1 != last)
15755 {
15756 int nchars, nbytes;
15757
15758 /* Output to end of string or up to '%'. Field width
15759 is length of string. Don't output more than
15760 PRECISION allows us. */
15761 --this;
15762
15763 prec = c_string_width (last, this - last, precision - n,
15764 &nchars, &nbytes);
15765
15766 if (frame_title_ptr)
15767 n += store_frame_title (last, 0, prec);
15768 else if (!NILP (mode_line_string_list))
15769 {
15770 int bytepos = last - lisp_string;
15771 int charpos = string_byte_to_char (elt, bytepos);
15772 int endpos = (precision <= 0
15773 ? string_byte_to_char (elt,
15774 this - lisp_string)
15775 : charpos + nchars);
15776
15777 n += store_mode_line_string (NULL,
15778 Fsubstring (elt, make_number (charpos),
15779 make_number (endpos)),
15780 0, 0, 0, Qnil);
15781 }
15782 else
15783 {
15784 int bytepos = last - lisp_string;
15785 int charpos = string_byte_to_char (elt, bytepos);
15786 n += display_string (NULL, elt, Qnil, 0, charpos,
15787 it, 0, prec, 0,
15788 STRING_MULTIBYTE (elt));
15789 }
15790 }
15791 else /* c == '%' */
15792 {
15793 const unsigned char *percent_position = this;
15794
15795 /* Get the specified minimum width. Zero means
15796 don't pad. */
15797 field = 0;
15798 while ((c = *this++) >= '0' && c <= '9')
15799 field = field * 10 + c - '0';
15800
15801 /* Don't pad beyond the total padding allowed. */
15802 if (field_width - n > 0 && field > field_width - n)
15803 field = field_width - n;
15804
15805 /* Note that either PRECISION <= 0 or N < PRECISION. */
15806 prec = precision - n;
15807
15808 if (c == 'M')
15809 n += display_mode_element (it, depth, field, prec,
15810 Vglobal_mode_string, props,
15811 risky);
15812 else if (c != 0)
15813 {
15814 int multibyte;
15815 int bytepos, charpos;
15816 unsigned char *spec;
15817
15818 bytepos = percent_position - lisp_string;
15819 charpos = (STRING_MULTIBYTE (elt)
15820 ? string_byte_to_char (elt, bytepos)
15821 : bytepos);
15822
15823 spec
15824 = decode_mode_spec (it->w, c, field, prec, &multibyte);
15825
15826 if (frame_title_ptr)
15827 n += store_frame_title (spec, field, prec);
15828 else if (!NILP (mode_line_string_list))
15829 {
15830 int len = strlen (spec);
15831 Lisp_Object tem = make_string (spec, len);
15832 props = Ftext_properties_at (make_number (charpos), elt);
15833 /* Should only keep face property in props */
15834 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
15835 }
15836 else
15837 {
15838 int nglyphs_before, nwritten;
15839
15840 nglyphs_before = it->glyph_row->used[TEXT_AREA];
15841 nwritten = display_string (spec, Qnil, elt,
15842 charpos, 0, it,
15843 field, prec, 0,
15844 multibyte);
15845
15846 /* Assign to the glyphs written above the
15847 string where the `%x' came from, position
15848 of the `%'. */
15849 if (nwritten > 0)
15850 {
15851 struct glyph *glyph
15852 = (it->glyph_row->glyphs[TEXT_AREA]
15853 + nglyphs_before);
15854 int i;
15855
15856 for (i = 0; i < nwritten; ++i)
15857 {
15858 glyph[i].object = elt;
15859 glyph[i].charpos = charpos;
15860 }
15861
15862 n += nwritten;
15863 }
15864 }
15865 }
15866 else /* c == 0 */
15867 break;
15868 }
15869 }
15870 }
15871 break;
15872
15873 case Lisp_Symbol:
15874 /* A symbol: process the value of the symbol recursively
15875 as if it appeared here directly. Avoid error if symbol void.
15876 Special case: if value of symbol is a string, output the string
15877 literally. */
15878 {
15879 register Lisp_Object tem;
15880
15881 /* If the variable is not marked as risky to set
15882 then its contents are risky to use. */
15883 if (NILP (Fget (elt, Qrisky_local_variable)))
15884 risky = 1;
15885
15886 tem = Fboundp (elt);
15887 if (!NILP (tem))
15888 {
15889 tem = Fsymbol_value (elt);
15890 /* If value is a string, output that string literally:
15891 don't check for % within it. */
15892 if (STRINGP (tem))
15893 literal = 1;
15894
15895 if (!EQ (tem, elt))
15896 {
15897 /* Give up right away for nil or t. */
15898 elt = tem;
15899 goto tail_recurse;
15900 }
15901 }
15902 }
15903 break;
15904
15905 case Lisp_Cons:
15906 {
15907 register Lisp_Object car, tem;
15908
15909 /* A cons cell: five distinct cases.
15910 If first element is :eval or :propertize, do something special.
15911 If first element is a string or a cons, process all the elements
15912 and effectively concatenate them.
15913 If first element is a negative number, truncate displaying cdr to
15914 at most that many characters. If positive, pad (with spaces)
15915 to at least that many characters.
15916 If first element is a symbol, process the cadr or caddr recursively
15917 according to whether the symbol's value is non-nil or nil. */
15918 car = XCAR (elt);
15919 if (EQ (car, QCeval))
15920 {
15921 /* An element of the form (:eval FORM) means evaluate FORM
15922 and use the result as mode line elements. */
15923
15924 if (risky)
15925 break;
15926
15927 if (CONSP (XCDR (elt)))
15928 {
15929 Lisp_Object spec;
15930 spec = safe_eval (XCAR (XCDR (elt)));
15931 n += display_mode_element (it, depth, field_width - n,
15932 precision - n, spec, props,
15933 risky);
15934 }
15935 }
15936 else if (EQ (car, QCpropertize))
15937 {
15938 /* An element of the form (:propertize ELT PROPS...)
15939 means display ELT but applying properties PROPS. */
15940
15941 if (risky)
15942 break;
15943
15944 if (CONSP (XCDR (elt)))
15945 n += display_mode_element (it, depth, field_width - n,
15946 precision - n, XCAR (XCDR (elt)),
15947 XCDR (XCDR (elt)), risky);
15948 }
15949 else if (SYMBOLP (car))
15950 {
15951 tem = Fboundp (car);
15952 elt = XCDR (elt);
15953 if (!CONSP (elt))
15954 goto invalid;
15955 /* elt is now the cdr, and we know it is a cons cell.
15956 Use its car if CAR has a non-nil value. */
15957 if (!NILP (tem))
15958 {
15959 tem = Fsymbol_value (car);
15960 if (!NILP (tem))
15961 {
15962 elt = XCAR (elt);
15963 goto tail_recurse;
15964 }
15965 }
15966 /* Symbol's value is nil (or symbol is unbound)
15967 Get the cddr of the original list
15968 and if possible find the caddr and use that. */
15969 elt = XCDR (elt);
15970 if (NILP (elt))
15971 break;
15972 else if (!CONSP (elt))
15973 goto invalid;
15974 elt = XCAR (elt);
15975 goto tail_recurse;
15976 }
15977 else if (INTEGERP (car))
15978 {
15979 register int lim = XINT (car);
15980 elt = XCDR (elt);
15981 if (lim < 0)
15982 {
15983 /* Negative int means reduce maximum width. */
15984 if (precision <= 0)
15985 precision = -lim;
15986 else
15987 precision = min (precision, -lim);
15988 }
15989 else if (lim > 0)
15990 {
15991 /* Padding specified. Don't let it be more than
15992 current maximum. */
15993 if (precision > 0)
15994 lim = min (precision, lim);
15995
15996 /* If that's more padding than already wanted, queue it.
15997 But don't reduce padding already specified even if
15998 that is beyond the current truncation point. */
15999 field_width = max (lim, field_width);
16000 }
16001 goto tail_recurse;
16002 }
16003 else if (STRINGP (car) || CONSP (car))
16004 {
16005 register int limit = 50;
16006 /* Limit is to protect against circular lists. */
16007 while (CONSP (elt)
16008 && --limit > 0
16009 && (precision <= 0 || n < precision))
16010 {
16011 n += display_mode_element (it, depth, field_width - n,
16012 precision - n, XCAR (elt),
16013 props, risky);
16014 elt = XCDR (elt);
16015 }
16016 }
16017 }
16018 break;
16019
16020 default:
16021 invalid:
16022 elt = build_string ("*invalid*");
16023 goto tail_recurse;
16024 }
16025
16026 /* Pad to FIELD_WIDTH. */
16027 if (field_width > 0 && n < field_width)
16028 {
16029 if (frame_title_ptr)
16030 n += store_frame_title ("", field_width - n, 0);
16031 else if (!NILP (mode_line_string_list))
16032 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16033 else
16034 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16035 0, 0, 0);
16036 }
16037
16038 return n;
16039 }
16040
16041 /* Store a mode-line string element in mode_line_string_list.
16042
16043 If STRING is non-null, display that C string. Otherwise, the Lisp
16044 string LISP_STRING is displayed.
16045
16046 FIELD_WIDTH is the minimum number of output glyphs to produce.
16047 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16048 with spaces. FIELD_WIDTH <= 0 means don't pad.
16049
16050 PRECISION is the maximum number of characters to output from
16051 STRING. PRECISION <= 0 means don't truncate the string.
16052
16053 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16054 properties to the string.
16055
16056 PROPS are the properties to add to the string.
16057 The mode_line_string_face face property is always added to the string.
16058 */
16059
16060 static int
16061 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16062 char *string;
16063 Lisp_Object lisp_string;
16064 int copy_string;
16065 int field_width;
16066 int precision;
16067 Lisp_Object props;
16068 {
16069 int len;
16070 int n = 0;
16071
16072 if (string != NULL)
16073 {
16074 len = strlen (string);
16075 if (precision > 0 && len > precision)
16076 len = precision;
16077 lisp_string = make_string (string, len);
16078 if (NILP (props))
16079 props = mode_line_string_face_prop;
16080 else if (!NILP (mode_line_string_face))
16081 {
16082 Lisp_Object face = Fsafe_plist_get (props, Qface);
16083 props = Fcopy_sequence (props);
16084 if (NILP (face))
16085 face = mode_line_string_face;
16086 else
16087 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16088 props = Fplist_put (props, Qface, face);
16089 }
16090 Fadd_text_properties (make_number (0), make_number (len),
16091 props, lisp_string);
16092 }
16093 else
16094 {
16095 len = XFASTINT (Flength (lisp_string));
16096 if (precision > 0 && len > precision)
16097 {
16098 len = precision;
16099 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16100 precision = -1;
16101 }
16102 if (!NILP (mode_line_string_face))
16103 {
16104 Lisp_Object face;
16105 if (NILP (props))
16106 props = Ftext_properties_at (make_number (0), lisp_string);
16107 face = Fsafe_plist_get (props, Qface);
16108 if (NILP (face))
16109 face = mode_line_string_face;
16110 else
16111 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16112 props = Fcons (Qface, Fcons (face, Qnil));
16113 if (copy_string)
16114 lisp_string = Fcopy_sequence (lisp_string);
16115 }
16116 if (!NILP (props))
16117 Fadd_text_properties (make_number (0), make_number (len),
16118 props, lisp_string);
16119 }
16120
16121 if (len > 0)
16122 {
16123 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16124 n += len;
16125 }
16126
16127 if (field_width > len)
16128 {
16129 field_width -= len;
16130 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16131 if (!NILP (props))
16132 Fadd_text_properties (make_number (0), make_number (field_width),
16133 props, lisp_string);
16134 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16135 n += field_width;
16136 }
16137
16138 return n;
16139 }
16140
16141
16142 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16143 1, 4, 0,
16144 doc: /* Format a string out of a mode line format specification.
16145 First arg FORMAT specifies the mode line format (see `mode-line-format'
16146 for details) to use.
16147
16148 Optional second arg FACE specifies the face property to put
16149 on all characters for which no face is specified.
16150 t means whatever face the window's mode line currently uses
16151 \(either `mode-line' or `mode-line-inactive', depending).
16152 nil means the default is no face property.
16153 If FACE is an integer, the value string has no text properties.
16154
16155 Optional third and fourth args WINDOW and BUFFER specify the window
16156 and buffer to use as the context for the formatting (defaults
16157 are the selected window and the window's buffer). */)
16158 (format, face, window, buffer)
16159 Lisp_Object format, face, window, buffer;
16160 {
16161 struct it it;
16162 int len;
16163 struct window *w;
16164 struct buffer *old_buffer = NULL;
16165 int face_id = -1;
16166 int no_props = INTEGERP (face);
16167
16168 if (NILP (window))
16169 window = selected_window;
16170 CHECK_WINDOW (window);
16171 w = XWINDOW (window);
16172
16173 if (NILP (buffer))
16174 buffer = w->buffer;
16175 CHECK_BUFFER (buffer);
16176
16177 if (NILP (format))
16178 return build_string ("");
16179
16180 if (no_props)
16181 face = Qnil;
16182
16183 if (!NILP (face))
16184 {
16185 if (EQ (face, Qt))
16186 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16187 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16188 }
16189
16190 if (face_id < 0)
16191 face_id = DEFAULT_FACE_ID;
16192
16193 if (XBUFFER (buffer) != current_buffer)
16194 {
16195 old_buffer = current_buffer;
16196 set_buffer_internal_1 (XBUFFER (buffer));
16197 }
16198
16199 init_iterator (&it, w, -1, -1, NULL, face_id);
16200
16201 if (!no_props)
16202 {
16203 mode_line_string_face = face;
16204 mode_line_string_face_prop
16205 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16206
16207 /* We need a dummy last element in mode_line_string_list to
16208 indicate we are building the propertized mode-line string.
16209 Using mode_line_string_face_prop here GC protects it. */
16210 mode_line_string_list
16211 = Fcons (mode_line_string_face_prop, Qnil);
16212 frame_title_ptr = NULL;
16213 }
16214 else
16215 {
16216 mode_line_string_face_prop = Qnil;
16217 mode_line_string_list = Qnil;
16218 frame_title_ptr = frame_title_buf;
16219 }
16220
16221 push_frame_kboard (it.f);
16222 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16223 pop_frame_kboard ();
16224
16225 if (old_buffer)
16226 set_buffer_internal_1 (old_buffer);
16227
16228 if (!no_props)
16229 {
16230 Lisp_Object str;
16231 mode_line_string_list = Fnreverse (mode_line_string_list);
16232 str = Fmapconcat (intern ("identity"), XCDR (mode_line_string_list),
16233 make_string ("", 0));
16234 mode_line_string_face_prop = Qnil;
16235 mode_line_string_list = Qnil;
16236 return str;
16237 }
16238
16239 len = frame_title_ptr - frame_title_buf;
16240 if (len > 0 && frame_title_ptr[-1] == '-')
16241 {
16242 /* Mode lines typically ends with numerous dashes; reduce to two dashes. */
16243 while (frame_title_ptr > frame_title_buf && *--frame_title_ptr == '-')
16244 ;
16245 frame_title_ptr += 3; /* restore last non-dash + two dashes */
16246 if (len > frame_title_ptr - frame_title_buf)
16247 len = frame_title_ptr - frame_title_buf;
16248 }
16249
16250 frame_title_ptr = NULL;
16251 return make_string (frame_title_buf, len);
16252 }
16253
16254 /* Write a null-terminated, right justified decimal representation of
16255 the positive integer D to BUF using a minimal field width WIDTH. */
16256
16257 static void
16258 pint2str (buf, width, d)
16259 register char *buf;
16260 register int width;
16261 register int d;
16262 {
16263 register char *p = buf;
16264
16265 if (d <= 0)
16266 *p++ = '0';
16267 else
16268 {
16269 while (d > 0)
16270 {
16271 *p++ = d % 10 + '0';
16272 d /= 10;
16273 }
16274 }
16275
16276 for (width -= (int) (p - buf); width > 0; --width)
16277 *p++ = ' ';
16278 *p-- = '\0';
16279 while (p > buf)
16280 {
16281 d = *buf;
16282 *buf++ = *p;
16283 *p-- = d;
16284 }
16285 }
16286
16287 /* Write a null-terminated, right justified decimal and "human
16288 readable" representation of the nonnegative integer D to BUF using
16289 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16290
16291 static const char power_letter[] =
16292 {
16293 0, /* not used */
16294 'k', /* kilo */
16295 'M', /* mega */
16296 'G', /* giga */
16297 'T', /* tera */
16298 'P', /* peta */
16299 'E', /* exa */
16300 'Z', /* zetta */
16301 'Y' /* yotta */
16302 };
16303
16304 static void
16305 pint2hrstr (buf, width, d)
16306 char *buf;
16307 int width;
16308 int d;
16309 {
16310 /* We aim to represent the nonnegative integer D as
16311 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16312 int quotient = d;
16313 int remainder = 0;
16314 /* -1 means: do not use TENTHS. */
16315 int tenths = -1;
16316 int exponent = 0;
16317
16318 /* Length of QUOTIENT.TENTHS as a string. */
16319 int length;
16320
16321 char * psuffix;
16322 char * p;
16323
16324 if (1000 <= quotient)
16325 {
16326 /* Scale to the appropriate EXPONENT. */
16327 do
16328 {
16329 remainder = quotient % 1000;
16330 quotient /= 1000;
16331 exponent++;
16332 }
16333 while (1000 <= quotient);
16334
16335 /* Round to nearest and decide whether to use TENTHS or not. */
16336 if (quotient <= 9)
16337 {
16338 tenths = remainder / 100;
16339 if (50 <= remainder % 100)
16340 {
16341 if (tenths < 9)
16342 tenths++;
16343 else
16344 {
16345 quotient++;
16346 if (quotient == 10)
16347 tenths = -1;
16348 else
16349 tenths = 0;
16350 }
16351 }
16352 }
16353 else
16354 if (500 <= remainder)
16355 {
16356 if (quotient < 999)
16357 quotient++;
16358 else
16359 {
16360 quotient = 1;
16361 exponent++;
16362 tenths = 0;
16363 }
16364 }
16365 }
16366
16367 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16368 if (tenths == -1 && quotient <= 99)
16369 if (quotient <= 9)
16370 length = 1;
16371 else
16372 length = 2;
16373 else
16374 length = 3;
16375 p = psuffix = buf + max (width, length);
16376
16377 /* Print EXPONENT. */
16378 if (exponent)
16379 *psuffix++ = power_letter[exponent];
16380 *psuffix = '\0';
16381
16382 /* Print TENTHS. */
16383 if (tenths >= 0)
16384 {
16385 *--p = '0' + tenths;
16386 *--p = '.';
16387 }
16388
16389 /* Print QUOTIENT. */
16390 do
16391 {
16392 int digit = quotient % 10;
16393 *--p = '0' + digit;
16394 }
16395 while ((quotient /= 10) != 0);
16396
16397 /* Print leading spaces. */
16398 while (buf < p)
16399 *--p = ' ';
16400 }
16401
16402 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16403 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16404 type of CODING_SYSTEM. Return updated pointer into BUF. */
16405
16406 static unsigned char invalid_eol_type[] = "(*invalid*)";
16407
16408 static char *
16409 decode_mode_spec_coding (coding_system, buf, eol_flag)
16410 Lisp_Object coding_system;
16411 register char *buf;
16412 int eol_flag;
16413 {
16414 Lisp_Object val;
16415 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16416 const unsigned char *eol_str;
16417 int eol_str_len;
16418 /* The EOL conversion we are using. */
16419 Lisp_Object eoltype;
16420
16421 val = Fget (coding_system, Qcoding_system);
16422 eoltype = Qnil;
16423
16424 if (!VECTORP (val)) /* Not yet decided. */
16425 {
16426 if (multibyte)
16427 *buf++ = '-';
16428 if (eol_flag)
16429 eoltype = eol_mnemonic_undecided;
16430 /* Don't mention EOL conversion if it isn't decided. */
16431 }
16432 else
16433 {
16434 Lisp_Object eolvalue;
16435
16436 eolvalue = Fget (coding_system, Qeol_type);
16437
16438 if (multibyte)
16439 *buf++ = XFASTINT (AREF (val, 1));
16440
16441 if (eol_flag)
16442 {
16443 /* The EOL conversion that is normal on this system. */
16444
16445 if (NILP (eolvalue)) /* Not yet decided. */
16446 eoltype = eol_mnemonic_undecided;
16447 else if (VECTORP (eolvalue)) /* Not yet decided. */
16448 eoltype = eol_mnemonic_undecided;
16449 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16450 eoltype = (XFASTINT (eolvalue) == 0
16451 ? eol_mnemonic_unix
16452 : (XFASTINT (eolvalue) == 1
16453 ? eol_mnemonic_dos : eol_mnemonic_mac));
16454 }
16455 }
16456
16457 if (eol_flag)
16458 {
16459 /* Mention the EOL conversion if it is not the usual one. */
16460 if (STRINGP (eoltype))
16461 {
16462 eol_str = SDATA (eoltype);
16463 eol_str_len = SBYTES (eoltype);
16464 }
16465 else if (INTEGERP (eoltype)
16466 && CHAR_VALID_P (XINT (eoltype), 0))
16467 {
16468 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16469 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16470 eol_str = tmp;
16471 }
16472 else
16473 {
16474 eol_str = invalid_eol_type;
16475 eol_str_len = sizeof (invalid_eol_type) - 1;
16476 }
16477 bcopy (eol_str, buf, eol_str_len);
16478 buf += eol_str_len;
16479 }
16480
16481 return buf;
16482 }
16483
16484 /* Return a string for the output of a mode line %-spec for window W,
16485 generated by character C. PRECISION >= 0 means don't return a
16486 string longer than that value. FIELD_WIDTH > 0 means pad the
16487 string returned with spaces to that value. Return 1 in *MULTIBYTE
16488 if the result is multibyte text.
16489
16490 Note we operate on the current buffer for most purposes,
16491 the exception being w->base_line_pos. */
16492
16493 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16494
16495 static char *
16496 decode_mode_spec (w, c, field_width, precision, multibyte)
16497 struct window *w;
16498 register int c;
16499 int field_width, precision;
16500 int *multibyte;
16501 {
16502 Lisp_Object obj;
16503 struct frame *f = XFRAME (WINDOW_FRAME (w));
16504 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16505 struct buffer *b = current_buffer;
16506
16507 obj = Qnil;
16508 *multibyte = 0;
16509
16510 switch (c)
16511 {
16512 case '*':
16513 if (!NILP (b->read_only))
16514 return "%";
16515 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16516 return "*";
16517 return "-";
16518
16519 case '+':
16520 /* This differs from %* only for a modified read-only buffer. */
16521 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16522 return "*";
16523 if (!NILP (b->read_only))
16524 return "%";
16525 return "-";
16526
16527 case '&':
16528 /* This differs from %* in ignoring read-only-ness. */
16529 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16530 return "*";
16531 return "-";
16532
16533 case '%':
16534 return "%";
16535
16536 case '[':
16537 {
16538 int i;
16539 char *p;
16540
16541 if (command_loop_level > 5)
16542 return "[[[... ";
16543 p = decode_mode_spec_buf;
16544 for (i = 0; i < command_loop_level; i++)
16545 *p++ = '[';
16546 *p = 0;
16547 return decode_mode_spec_buf;
16548 }
16549
16550 case ']':
16551 {
16552 int i;
16553 char *p;
16554
16555 if (command_loop_level > 5)
16556 return " ...]]]";
16557 p = decode_mode_spec_buf;
16558 for (i = 0; i < command_loop_level; i++)
16559 *p++ = ']';
16560 *p = 0;
16561 return decode_mode_spec_buf;
16562 }
16563
16564 case '-':
16565 {
16566 register int i;
16567
16568 /* Let lots_of_dashes be a string of infinite length. */
16569 if (!NILP (mode_line_string_list))
16570 return "--";
16571 if (field_width <= 0
16572 || field_width > sizeof (lots_of_dashes))
16573 {
16574 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16575 decode_mode_spec_buf[i] = '-';
16576 decode_mode_spec_buf[i] = '\0';
16577 return decode_mode_spec_buf;
16578 }
16579 else
16580 return lots_of_dashes;
16581 }
16582
16583 case 'b':
16584 obj = b->name;
16585 break;
16586
16587 case 'c':
16588 {
16589 int col = (int) current_column (); /* iftc */
16590 w->column_number_displayed = make_number (col);
16591 pint2str (decode_mode_spec_buf, field_width, col);
16592 return decode_mode_spec_buf;
16593 }
16594
16595 case 'F':
16596 /* %F displays the frame name. */
16597 if (!NILP (f->title))
16598 return (char *) SDATA (f->title);
16599 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16600 return (char *) SDATA (f->name);
16601 return "Emacs";
16602
16603 case 'f':
16604 obj = b->filename;
16605 break;
16606
16607 case 'i':
16608 {
16609 int size = ZV - BEGV;
16610 pint2str (decode_mode_spec_buf, field_width, size);
16611 return decode_mode_spec_buf;
16612 }
16613
16614 case 'I':
16615 {
16616 int size = ZV - BEGV;
16617 pint2hrstr (decode_mode_spec_buf, field_width, size);
16618 return decode_mode_spec_buf;
16619 }
16620
16621 case 'l':
16622 {
16623 int startpos = XMARKER (w->start)->charpos;
16624 int startpos_byte = marker_byte_position (w->start);
16625 int line, linepos, linepos_byte, topline;
16626 int nlines, junk;
16627 int height = WINDOW_TOTAL_LINES (w);
16628
16629 /* If we decided that this buffer isn't suitable for line numbers,
16630 don't forget that too fast. */
16631 if (EQ (w->base_line_pos, w->buffer))
16632 goto no_value;
16633 /* But do forget it, if the window shows a different buffer now. */
16634 else if (BUFFERP (w->base_line_pos))
16635 w->base_line_pos = Qnil;
16636
16637 /* If the buffer is very big, don't waste time. */
16638 if (INTEGERP (Vline_number_display_limit)
16639 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16640 {
16641 w->base_line_pos = Qnil;
16642 w->base_line_number = Qnil;
16643 goto no_value;
16644 }
16645
16646 if (!NILP (w->base_line_number)
16647 && !NILP (w->base_line_pos)
16648 && XFASTINT (w->base_line_pos) <= startpos)
16649 {
16650 line = XFASTINT (w->base_line_number);
16651 linepos = XFASTINT (w->base_line_pos);
16652 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16653 }
16654 else
16655 {
16656 line = 1;
16657 linepos = BUF_BEGV (b);
16658 linepos_byte = BUF_BEGV_BYTE (b);
16659 }
16660
16661 /* Count lines from base line to window start position. */
16662 nlines = display_count_lines (linepos, linepos_byte,
16663 startpos_byte,
16664 startpos, &junk);
16665
16666 topline = nlines + line;
16667
16668 /* Determine a new base line, if the old one is too close
16669 or too far away, or if we did not have one.
16670 "Too close" means it's plausible a scroll-down would
16671 go back past it. */
16672 if (startpos == BUF_BEGV (b))
16673 {
16674 w->base_line_number = make_number (topline);
16675 w->base_line_pos = make_number (BUF_BEGV (b));
16676 }
16677 else if (nlines < height + 25 || nlines > height * 3 + 50
16678 || linepos == BUF_BEGV (b))
16679 {
16680 int limit = BUF_BEGV (b);
16681 int limit_byte = BUF_BEGV_BYTE (b);
16682 int position;
16683 int distance = (height * 2 + 30) * line_number_display_limit_width;
16684
16685 if (startpos - distance > limit)
16686 {
16687 limit = startpos - distance;
16688 limit_byte = CHAR_TO_BYTE (limit);
16689 }
16690
16691 nlines = display_count_lines (startpos, startpos_byte,
16692 limit_byte,
16693 - (height * 2 + 30),
16694 &position);
16695 /* If we couldn't find the lines we wanted within
16696 line_number_display_limit_width chars per line,
16697 give up on line numbers for this window. */
16698 if (position == limit_byte && limit == startpos - distance)
16699 {
16700 w->base_line_pos = w->buffer;
16701 w->base_line_number = Qnil;
16702 goto no_value;
16703 }
16704
16705 w->base_line_number = make_number (topline - nlines);
16706 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16707 }
16708
16709 /* Now count lines from the start pos to point. */
16710 nlines = display_count_lines (startpos, startpos_byte,
16711 PT_BYTE, PT, &junk);
16712
16713 /* Record that we did display the line number. */
16714 line_number_displayed = 1;
16715
16716 /* Make the string to show. */
16717 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16718 return decode_mode_spec_buf;
16719 no_value:
16720 {
16721 char* p = decode_mode_spec_buf;
16722 int pad = field_width - 2;
16723 while (pad-- > 0)
16724 *p++ = ' ';
16725 *p++ = '?';
16726 *p++ = '?';
16727 *p = '\0';
16728 return decode_mode_spec_buf;
16729 }
16730 }
16731 break;
16732
16733 case 'm':
16734 obj = b->mode_name;
16735 break;
16736
16737 case 'n':
16738 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16739 return " Narrow";
16740 break;
16741
16742 case 'p':
16743 {
16744 int pos = marker_position (w->start);
16745 int total = BUF_ZV (b) - BUF_BEGV (b);
16746
16747 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16748 {
16749 if (pos <= BUF_BEGV (b))
16750 return "All";
16751 else
16752 return "Bottom";
16753 }
16754 else if (pos <= BUF_BEGV (b))
16755 return "Top";
16756 else
16757 {
16758 if (total > 1000000)
16759 /* Do it differently for a large value, to avoid overflow. */
16760 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16761 else
16762 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16763 /* We can't normally display a 3-digit number,
16764 so get us a 2-digit number that is close. */
16765 if (total == 100)
16766 total = 99;
16767 sprintf (decode_mode_spec_buf, "%2d%%", total);
16768 return decode_mode_spec_buf;
16769 }
16770 }
16771
16772 /* Display percentage of size above the bottom of the screen. */
16773 case 'P':
16774 {
16775 int toppos = marker_position (w->start);
16776 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16777 int total = BUF_ZV (b) - BUF_BEGV (b);
16778
16779 if (botpos >= BUF_ZV (b))
16780 {
16781 if (toppos <= BUF_BEGV (b))
16782 return "All";
16783 else
16784 return "Bottom";
16785 }
16786 else
16787 {
16788 if (total > 1000000)
16789 /* Do it differently for a large value, to avoid overflow. */
16790 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16791 else
16792 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
16793 /* We can't normally display a 3-digit number,
16794 so get us a 2-digit number that is close. */
16795 if (total == 100)
16796 total = 99;
16797 if (toppos <= BUF_BEGV (b))
16798 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
16799 else
16800 sprintf (decode_mode_spec_buf, "%2d%%", total);
16801 return decode_mode_spec_buf;
16802 }
16803 }
16804
16805 case 's':
16806 /* status of process */
16807 obj = Fget_buffer_process (Fcurrent_buffer ());
16808 if (NILP (obj))
16809 return "no process";
16810 #ifdef subprocesses
16811 obj = Fsymbol_name (Fprocess_status (obj));
16812 #endif
16813 break;
16814
16815 case 't': /* indicate TEXT or BINARY */
16816 #ifdef MODE_LINE_BINARY_TEXT
16817 return MODE_LINE_BINARY_TEXT (b);
16818 #else
16819 return "T";
16820 #endif
16821
16822 case 'z':
16823 /* coding-system (not including end-of-line format) */
16824 case 'Z':
16825 /* coding-system (including end-of-line type) */
16826 {
16827 int eol_flag = (c == 'Z');
16828 char *p = decode_mode_spec_buf;
16829
16830 if (! FRAME_WINDOW_P (f))
16831 {
16832 /* No need to mention EOL here--the terminal never needs
16833 to do EOL conversion. */
16834 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
16835 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
16836 }
16837 p = decode_mode_spec_coding (b->buffer_file_coding_system,
16838 p, eol_flag);
16839
16840 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
16841 #ifdef subprocesses
16842 obj = Fget_buffer_process (Fcurrent_buffer ());
16843 if (PROCESSP (obj))
16844 {
16845 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
16846 p, eol_flag);
16847 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
16848 p, eol_flag);
16849 }
16850 #endif /* subprocesses */
16851 #endif /* 0 */
16852 *p = 0;
16853 return decode_mode_spec_buf;
16854 }
16855 }
16856
16857 if (STRINGP (obj))
16858 {
16859 *multibyte = STRING_MULTIBYTE (obj);
16860 return (char *) SDATA (obj);
16861 }
16862 else
16863 return "";
16864 }
16865
16866
16867 /* Count up to COUNT lines starting from START / START_BYTE.
16868 But don't go beyond LIMIT_BYTE.
16869 Return the number of lines thus found (always nonnegative).
16870
16871 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
16872
16873 static int
16874 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
16875 int start, start_byte, limit_byte, count;
16876 int *byte_pos_ptr;
16877 {
16878 register unsigned char *cursor;
16879 unsigned char *base;
16880
16881 register int ceiling;
16882 register unsigned char *ceiling_addr;
16883 int orig_count = count;
16884
16885 /* If we are not in selective display mode,
16886 check only for newlines. */
16887 int selective_display = (!NILP (current_buffer->selective_display)
16888 && !INTEGERP (current_buffer->selective_display));
16889
16890 if (count > 0)
16891 {
16892 while (start_byte < limit_byte)
16893 {
16894 ceiling = BUFFER_CEILING_OF (start_byte);
16895 ceiling = min (limit_byte - 1, ceiling);
16896 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
16897 base = (cursor = BYTE_POS_ADDR (start_byte));
16898 while (1)
16899 {
16900 if (selective_display)
16901 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
16902 ;
16903 else
16904 while (*cursor != '\n' && ++cursor != ceiling_addr)
16905 ;
16906
16907 if (cursor != ceiling_addr)
16908 {
16909 if (--count == 0)
16910 {
16911 start_byte += cursor - base + 1;
16912 *byte_pos_ptr = start_byte;
16913 return orig_count;
16914 }
16915 else
16916 if (++cursor == ceiling_addr)
16917 break;
16918 }
16919 else
16920 break;
16921 }
16922 start_byte += cursor - base;
16923 }
16924 }
16925 else
16926 {
16927 while (start_byte > limit_byte)
16928 {
16929 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
16930 ceiling = max (limit_byte, ceiling);
16931 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
16932 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
16933 while (1)
16934 {
16935 if (selective_display)
16936 while (--cursor != ceiling_addr
16937 && *cursor != '\n' && *cursor != 015)
16938 ;
16939 else
16940 while (--cursor != ceiling_addr && *cursor != '\n')
16941 ;
16942
16943 if (cursor != ceiling_addr)
16944 {
16945 if (++count == 0)
16946 {
16947 start_byte += cursor - base + 1;
16948 *byte_pos_ptr = start_byte;
16949 /* When scanning backwards, we should
16950 not count the newline posterior to which we stop. */
16951 return - orig_count - 1;
16952 }
16953 }
16954 else
16955 break;
16956 }
16957 /* Here we add 1 to compensate for the last decrement
16958 of CURSOR, which took it past the valid range. */
16959 start_byte += cursor - base + 1;
16960 }
16961 }
16962
16963 *byte_pos_ptr = limit_byte;
16964
16965 if (count < 0)
16966 return - orig_count + count;
16967 return orig_count - count;
16968
16969 }
16970
16971
16972 \f
16973 /***********************************************************************
16974 Displaying strings
16975 ***********************************************************************/
16976
16977 /* Display a NUL-terminated string, starting with index START.
16978
16979 If STRING is non-null, display that C string. Otherwise, the Lisp
16980 string LISP_STRING is displayed.
16981
16982 If FACE_STRING is not nil, FACE_STRING_POS is a position in
16983 FACE_STRING. Display STRING or LISP_STRING with the face at
16984 FACE_STRING_POS in FACE_STRING:
16985
16986 Display the string in the environment given by IT, but use the
16987 standard display table, temporarily.
16988
16989 FIELD_WIDTH is the minimum number of output glyphs to produce.
16990 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16991 with spaces. If STRING has more characters, more than FIELD_WIDTH
16992 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
16993
16994 PRECISION is the maximum number of characters to output from
16995 STRING. PRECISION < 0 means don't truncate the string.
16996
16997 This is roughly equivalent to printf format specifiers:
16998
16999 FIELD_WIDTH PRECISION PRINTF
17000 ----------------------------------------
17001 -1 -1 %s
17002 -1 10 %.10s
17003 10 -1 %10s
17004 20 10 %20.10s
17005
17006 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17007 display them, and < 0 means obey the current buffer's value of
17008 enable_multibyte_characters.
17009
17010 Value is the number of glyphs produced. */
17011
17012 static int
17013 display_string (string, lisp_string, face_string, face_string_pos,
17014 start, it, field_width, precision, max_x, multibyte)
17015 unsigned char *string;
17016 Lisp_Object lisp_string;
17017 Lisp_Object face_string;
17018 int face_string_pos;
17019 int start;
17020 struct it *it;
17021 int field_width, precision, max_x;
17022 int multibyte;
17023 {
17024 int hpos_at_start = it->hpos;
17025 int saved_face_id = it->face_id;
17026 struct glyph_row *row = it->glyph_row;
17027
17028 /* Initialize the iterator IT for iteration over STRING beginning
17029 with index START. */
17030 reseat_to_string (it, string, lisp_string, start,
17031 precision, field_width, multibyte);
17032
17033 /* If displaying STRING, set up the face of the iterator
17034 from LISP_STRING, if that's given. */
17035 if (STRINGP (face_string))
17036 {
17037 int endptr;
17038 struct face *face;
17039
17040 it->face_id
17041 = face_at_string_position (it->w, face_string, face_string_pos,
17042 0, it->region_beg_charpos,
17043 it->region_end_charpos,
17044 &endptr, it->base_face_id, 0);
17045 face = FACE_FROM_ID (it->f, it->face_id);
17046 it->face_box_p = face->box != FACE_NO_BOX;
17047 }
17048
17049 /* Set max_x to the maximum allowed X position. Don't let it go
17050 beyond the right edge of the window. */
17051 if (max_x <= 0)
17052 max_x = it->last_visible_x;
17053 else
17054 max_x = min (max_x, it->last_visible_x);
17055
17056 /* Skip over display elements that are not visible. because IT->w is
17057 hscrolled. */
17058 if (it->current_x < it->first_visible_x)
17059 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17060 MOVE_TO_POS | MOVE_TO_X);
17061
17062 row->ascent = it->max_ascent;
17063 row->height = it->max_ascent + it->max_descent;
17064 row->phys_ascent = it->max_phys_ascent;
17065 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17066 row->extra_line_spacing = it->max_extra_line_spacing;
17067
17068 /* This condition is for the case that we are called with current_x
17069 past last_visible_x. */
17070 while (it->current_x < max_x)
17071 {
17072 int x_before, x, n_glyphs_before, i, nglyphs;
17073
17074 /* Get the next display element. */
17075 if (!get_next_display_element (it))
17076 break;
17077
17078 /* Produce glyphs. */
17079 x_before = it->current_x;
17080 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17081 PRODUCE_GLYPHS (it);
17082
17083 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17084 i = 0;
17085 x = x_before;
17086 while (i < nglyphs)
17087 {
17088 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17089
17090 if (!it->truncate_lines_p
17091 && x + glyph->pixel_width > max_x)
17092 {
17093 /* End of continued line or max_x reached. */
17094 if (CHAR_GLYPH_PADDING_P (*glyph))
17095 {
17096 /* A wide character is unbreakable. */
17097 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17098 it->current_x = x_before;
17099 }
17100 else
17101 {
17102 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17103 it->current_x = x;
17104 }
17105 break;
17106 }
17107 else if (x + glyph->pixel_width > it->first_visible_x)
17108 {
17109 /* Glyph is at least partially visible. */
17110 ++it->hpos;
17111 if (x < it->first_visible_x)
17112 it->glyph_row->x = x - it->first_visible_x;
17113 }
17114 else
17115 {
17116 /* Glyph is off the left margin of the display area.
17117 Should not happen. */
17118 abort ();
17119 }
17120
17121 row->ascent = max (row->ascent, it->max_ascent);
17122 row->height = max (row->height, it->max_ascent + it->max_descent);
17123 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17124 row->phys_height = max (row->phys_height,
17125 it->max_phys_ascent + it->max_phys_descent);
17126 row->extra_line_spacing = max (row->extra_line_spacing,
17127 it->max_extra_line_spacing);
17128 x += glyph->pixel_width;
17129 ++i;
17130 }
17131
17132 /* Stop if max_x reached. */
17133 if (i < nglyphs)
17134 break;
17135
17136 /* Stop at line ends. */
17137 if (ITERATOR_AT_END_OF_LINE_P (it))
17138 {
17139 it->continuation_lines_width = 0;
17140 break;
17141 }
17142
17143 set_iterator_to_next (it, 1);
17144
17145 /* Stop if truncating at the right edge. */
17146 if (it->truncate_lines_p
17147 && it->current_x >= it->last_visible_x)
17148 {
17149 /* Add truncation mark, but don't do it if the line is
17150 truncated at a padding space. */
17151 if (IT_CHARPOS (*it) < it->string_nchars)
17152 {
17153 if (!FRAME_WINDOW_P (it->f))
17154 {
17155 int i, n;
17156
17157 if (it->current_x > it->last_visible_x)
17158 {
17159 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17160 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17161 break;
17162 for (n = row->used[TEXT_AREA]; i < n; ++i)
17163 {
17164 row->used[TEXT_AREA] = i;
17165 produce_special_glyphs (it, IT_TRUNCATION);
17166 }
17167 }
17168 produce_special_glyphs (it, IT_TRUNCATION);
17169 }
17170 it->glyph_row->truncated_on_right_p = 1;
17171 }
17172 break;
17173 }
17174 }
17175
17176 /* Maybe insert a truncation at the left. */
17177 if (it->first_visible_x
17178 && IT_CHARPOS (*it) > 0)
17179 {
17180 if (!FRAME_WINDOW_P (it->f))
17181 insert_left_trunc_glyphs (it);
17182 it->glyph_row->truncated_on_left_p = 1;
17183 }
17184
17185 it->face_id = saved_face_id;
17186
17187 /* Value is number of columns displayed. */
17188 return it->hpos - hpos_at_start;
17189 }
17190
17191
17192 \f
17193 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17194 appears as an element of LIST or as the car of an element of LIST.
17195 If PROPVAL is a list, compare each element against LIST in that
17196 way, and return 1/2 if any element of PROPVAL is found in LIST.
17197 Otherwise return 0. This function cannot quit.
17198 The return value is 2 if the text is invisible but with an ellipsis
17199 and 1 if it's invisible and without an ellipsis. */
17200
17201 int
17202 invisible_p (propval, list)
17203 register Lisp_Object propval;
17204 Lisp_Object list;
17205 {
17206 register Lisp_Object tail, proptail;
17207
17208 for (tail = list; CONSP (tail); tail = XCDR (tail))
17209 {
17210 register Lisp_Object tem;
17211 tem = XCAR (tail);
17212 if (EQ (propval, tem))
17213 return 1;
17214 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17215 return NILP (XCDR (tem)) ? 1 : 2;
17216 }
17217
17218 if (CONSP (propval))
17219 {
17220 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17221 {
17222 Lisp_Object propelt;
17223 propelt = XCAR (proptail);
17224 for (tail = list; CONSP (tail); tail = XCDR (tail))
17225 {
17226 register Lisp_Object tem;
17227 tem = XCAR (tail);
17228 if (EQ (propelt, tem))
17229 return 1;
17230 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17231 return NILP (XCDR (tem)) ? 1 : 2;
17232 }
17233 }
17234 }
17235
17236 return 0;
17237 }
17238
17239 /* Calculate a width or height in pixels from a specification using
17240 the following elements:
17241
17242 SPEC ::=
17243 NUM - a (fractional) multiple of the default font width/height
17244 (NUM) - specifies exactly NUM pixels
17245 UNIT - a fixed number of pixels, see below.
17246 ELEMENT - size of a display element in pixels, see below.
17247 (NUM . SPEC) - equals NUM * SPEC
17248 (+ SPEC SPEC ...) - add pixel values
17249 (- SPEC SPEC ...) - subtract pixel values
17250 (- SPEC) - negate pixel value
17251
17252 NUM ::=
17253 INT or FLOAT - a number constant
17254 SYMBOL - use symbol's (buffer local) variable binding.
17255
17256 UNIT ::=
17257 in - pixels per inch *)
17258 mm - pixels per 1/1000 meter *)
17259 cm - pixels per 1/100 meter *)
17260 width - width of current font in pixels.
17261 height - height of current font in pixels.
17262
17263 *) using the ratio(s) defined in display-pixels-per-inch.
17264
17265 ELEMENT ::=
17266
17267 left-fringe - left fringe width in pixels
17268 right-fringe - right fringe width in pixels
17269
17270 left-margin - left margin width in pixels
17271 right-margin - right margin width in pixels
17272
17273 scroll-bar - scroll-bar area width in pixels
17274
17275 Examples:
17276
17277 Pixels corresponding to 5 inches:
17278 (5 . in)
17279
17280 Total width of non-text areas on left side of window (if scroll-bar is on left):
17281 '(space :width (+ left-fringe left-margin scroll-bar))
17282
17283 Align to first text column (in header line):
17284 '(space :align-to 0)
17285
17286 Align to middle of text area minus half the width of variable `my-image'
17287 containing a loaded image:
17288 '(space :align-to (0.5 . (- text my-image)))
17289
17290 Width of left margin minus width of 1 character in the default font:
17291 '(space :width (- left-margin 1))
17292
17293 Width of left margin minus width of 2 characters in the current font:
17294 '(space :width (- left-margin (2 . width)))
17295
17296 Center 1 character over left-margin (in header line):
17297 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17298
17299 Different ways to express width of left fringe plus left margin minus one pixel:
17300 '(space :width (- (+ left-fringe left-margin) (1)))
17301 '(space :width (+ left-fringe left-margin (- (1))))
17302 '(space :width (+ left-fringe left-margin (-1)))
17303
17304 */
17305
17306 #define NUMVAL(X) \
17307 ((INTEGERP (X) || FLOATP (X)) \
17308 ? XFLOATINT (X) \
17309 : - 1)
17310
17311 int
17312 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17313 double *res;
17314 struct it *it;
17315 Lisp_Object prop;
17316 void *font;
17317 int width_p, *align_to;
17318 {
17319 double pixels;
17320
17321 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17322 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17323
17324 if (NILP (prop))
17325 return OK_PIXELS (0);
17326
17327 if (SYMBOLP (prop))
17328 {
17329 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17330 {
17331 char *unit = SDATA (SYMBOL_NAME (prop));
17332
17333 if (unit[0] == 'i' && unit[1] == 'n')
17334 pixels = 1.0;
17335 else if (unit[0] == 'm' && unit[1] == 'm')
17336 pixels = 25.4;
17337 else if (unit[0] == 'c' && unit[1] == 'm')
17338 pixels = 2.54;
17339 else
17340 pixels = 0;
17341 if (pixels > 0)
17342 {
17343 double ppi;
17344 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17345 || (CONSP (Vdisplay_pixels_per_inch)
17346 && (ppi = (width_p
17347 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17348 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17349 ppi > 0)))
17350 return OK_PIXELS (ppi / pixels);
17351
17352 return 0;
17353 }
17354 }
17355
17356 #ifdef HAVE_WINDOW_SYSTEM
17357 if (EQ (prop, Qheight))
17358 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17359 if (EQ (prop, Qwidth))
17360 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17361 #else
17362 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17363 return OK_PIXELS (1);
17364 #endif
17365
17366 if (EQ (prop, Qtext))
17367 return OK_PIXELS (width_p
17368 ? window_box_width (it->w, TEXT_AREA)
17369 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17370
17371 if (align_to && *align_to < 0)
17372 {
17373 *res = 0;
17374 if (EQ (prop, Qleft))
17375 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17376 if (EQ (prop, Qright))
17377 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17378 if (EQ (prop, Qcenter))
17379 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17380 + window_box_width (it->w, TEXT_AREA) / 2);
17381 if (EQ (prop, Qleft_fringe))
17382 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17383 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17384 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17385 if (EQ (prop, Qright_fringe))
17386 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17387 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17388 : window_box_right_offset (it->w, TEXT_AREA));
17389 if (EQ (prop, Qleft_margin))
17390 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17391 if (EQ (prop, Qright_margin))
17392 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17393 if (EQ (prop, Qscroll_bar))
17394 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17395 ? 0
17396 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17397 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17398 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17399 : 0)));
17400 }
17401 else
17402 {
17403 if (EQ (prop, Qleft_fringe))
17404 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17405 if (EQ (prop, Qright_fringe))
17406 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17407 if (EQ (prop, Qleft_margin))
17408 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17409 if (EQ (prop, Qright_margin))
17410 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17411 if (EQ (prop, Qscroll_bar))
17412 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17413 }
17414
17415 prop = Fbuffer_local_value (prop, it->w->buffer);
17416 }
17417
17418 if (INTEGERP (prop) || FLOATP (prop))
17419 {
17420 int base_unit = (width_p
17421 ? FRAME_COLUMN_WIDTH (it->f)
17422 : FRAME_LINE_HEIGHT (it->f));
17423 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17424 }
17425
17426 if (CONSP (prop))
17427 {
17428 Lisp_Object car = XCAR (prop);
17429 Lisp_Object cdr = XCDR (prop);
17430
17431 if (SYMBOLP (car))
17432 {
17433 #ifdef HAVE_WINDOW_SYSTEM
17434 if (valid_image_p (prop))
17435 {
17436 int id = lookup_image (it->f, prop);
17437 struct image *img = IMAGE_FROM_ID (it->f, id);
17438
17439 return OK_PIXELS (width_p ? img->width : img->height);
17440 }
17441 #endif
17442 if (EQ (car, Qplus) || EQ (car, Qminus))
17443 {
17444 int first = 1;
17445 double px;
17446
17447 pixels = 0;
17448 while (CONSP (cdr))
17449 {
17450 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17451 font, width_p, align_to))
17452 return 0;
17453 if (first)
17454 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17455 else
17456 pixels += px;
17457 cdr = XCDR (cdr);
17458 }
17459 if (EQ (car, Qminus))
17460 pixels = -pixels;
17461 return OK_PIXELS (pixels);
17462 }
17463
17464 car = Fbuffer_local_value (car, it->w->buffer);
17465 }
17466
17467 if (INTEGERP (car) || FLOATP (car))
17468 {
17469 double fact;
17470 pixels = XFLOATINT (car);
17471 if (NILP (cdr))
17472 return OK_PIXELS (pixels);
17473 if (calc_pixel_width_or_height (&fact, it, cdr,
17474 font, width_p, align_to))
17475 return OK_PIXELS (pixels * fact);
17476 return 0;
17477 }
17478
17479 return 0;
17480 }
17481
17482 return 0;
17483 }
17484
17485 \f
17486 /***********************************************************************
17487 Glyph Display
17488 ***********************************************************************/
17489
17490 #ifdef HAVE_WINDOW_SYSTEM
17491
17492 #if GLYPH_DEBUG
17493
17494 void
17495 dump_glyph_string (s)
17496 struct glyph_string *s;
17497 {
17498 fprintf (stderr, "glyph string\n");
17499 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17500 s->x, s->y, s->width, s->height);
17501 fprintf (stderr, " ybase = %d\n", s->ybase);
17502 fprintf (stderr, " hl = %d\n", s->hl);
17503 fprintf (stderr, " left overhang = %d, right = %d\n",
17504 s->left_overhang, s->right_overhang);
17505 fprintf (stderr, " nchars = %d\n", s->nchars);
17506 fprintf (stderr, " extends to end of line = %d\n",
17507 s->extends_to_end_of_line_p);
17508 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17509 fprintf (stderr, " bg width = %d\n", s->background_width);
17510 }
17511
17512 #endif /* GLYPH_DEBUG */
17513
17514 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17515 of XChar2b structures for S; it can't be allocated in
17516 init_glyph_string because it must be allocated via `alloca'. W
17517 is the window on which S is drawn. ROW and AREA are the glyph row
17518 and area within the row from which S is constructed. START is the
17519 index of the first glyph structure covered by S. HL is a
17520 face-override for drawing S. */
17521
17522 #ifdef HAVE_NTGUI
17523 #define OPTIONAL_HDC(hdc) hdc,
17524 #define DECLARE_HDC(hdc) HDC hdc;
17525 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17526 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17527 #endif
17528
17529 #ifndef OPTIONAL_HDC
17530 #define OPTIONAL_HDC(hdc)
17531 #define DECLARE_HDC(hdc)
17532 #define ALLOCATE_HDC(hdc, f)
17533 #define RELEASE_HDC(hdc, f)
17534 #endif
17535
17536 static void
17537 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17538 struct glyph_string *s;
17539 DECLARE_HDC (hdc)
17540 XChar2b *char2b;
17541 struct window *w;
17542 struct glyph_row *row;
17543 enum glyph_row_area area;
17544 int start;
17545 enum draw_glyphs_face hl;
17546 {
17547 bzero (s, sizeof *s);
17548 s->w = w;
17549 s->f = XFRAME (w->frame);
17550 #ifdef HAVE_NTGUI
17551 s->hdc = hdc;
17552 #endif
17553 s->display = FRAME_X_DISPLAY (s->f);
17554 s->window = FRAME_X_WINDOW (s->f);
17555 s->char2b = char2b;
17556 s->hl = hl;
17557 s->row = row;
17558 s->area = area;
17559 s->first_glyph = row->glyphs[area] + start;
17560 s->height = row->height;
17561 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17562
17563 /* Display the internal border below the tool-bar window. */
17564 if (WINDOWP (s->f->tool_bar_window)
17565 && s->w == XWINDOW (s->f->tool_bar_window))
17566 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17567
17568 s->ybase = s->y + row->ascent;
17569 }
17570
17571
17572 /* Append the list of glyph strings with head H and tail T to the list
17573 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17574
17575 static INLINE void
17576 append_glyph_string_lists (head, tail, h, t)
17577 struct glyph_string **head, **tail;
17578 struct glyph_string *h, *t;
17579 {
17580 if (h)
17581 {
17582 if (*head)
17583 (*tail)->next = h;
17584 else
17585 *head = h;
17586 h->prev = *tail;
17587 *tail = t;
17588 }
17589 }
17590
17591
17592 /* Prepend the list of glyph strings with head H and tail T to the
17593 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17594 result. */
17595
17596 static INLINE void
17597 prepend_glyph_string_lists (head, tail, h, t)
17598 struct glyph_string **head, **tail;
17599 struct glyph_string *h, *t;
17600 {
17601 if (h)
17602 {
17603 if (*head)
17604 (*head)->prev = t;
17605 else
17606 *tail = t;
17607 t->next = *head;
17608 *head = h;
17609 }
17610 }
17611
17612
17613 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17614 Set *HEAD and *TAIL to the resulting list. */
17615
17616 static INLINE void
17617 append_glyph_string (head, tail, s)
17618 struct glyph_string **head, **tail;
17619 struct glyph_string *s;
17620 {
17621 s->next = s->prev = NULL;
17622 append_glyph_string_lists (head, tail, s, s);
17623 }
17624
17625
17626 /* Get face and two-byte form of character glyph GLYPH on frame F.
17627 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17628 a pointer to a realized face that is ready for display. */
17629
17630 static INLINE struct face *
17631 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17632 struct frame *f;
17633 struct glyph *glyph;
17634 XChar2b *char2b;
17635 int *two_byte_p;
17636 {
17637 struct face *face;
17638
17639 xassert (glyph->type == CHAR_GLYPH);
17640 face = FACE_FROM_ID (f, glyph->face_id);
17641
17642 if (two_byte_p)
17643 *two_byte_p = 0;
17644
17645 if (!glyph->multibyte_p)
17646 {
17647 /* Unibyte case. We don't have to encode, but we have to make
17648 sure to use a face suitable for unibyte. */
17649 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17650 }
17651 else if (glyph->u.ch < 128
17652 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17653 {
17654 /* Case of ASCII in a face known to fit ASCII. */
17655 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17656 }
17657 else
17658 {
17659 int c1, c2, charset;
17660
17661 /* Split characters into bytes. If c2 is -1 afterwards, C is
17662 really a one-byte character so that byte1 is zero. */
17663 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17664 if (c2 > 0)
17665 STORE_XCHAR2B (char2b, c1, c2);
17666 else
17667 STORE_XCHAR2B (char2b, 0, c1);
17668
17669 /* Maybe encode the character in *CHAR2B. */
17670 if (charset != CHARSET_ASCII)
17671 {
17672 struct font_info *font_info
17673 = FONT_INFO_FROM_ID (f, face->font_info_id);
17674 if (font_info)
17675 glyph->font_type
17676 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17677 }
17678 }
17679
17680 /* Make sure X resources of the face are allocated. */
17681 xassert (face != NULL);
17682 PREPARE_FACE_FOR_DISPLAY (f, face);
17683 return face;
17684 }
17685
17686
17687 /* Fill glyph string S with composition components specified by S->cmp.
17688
17689 FACES is an array of faces for all components of this composition.
17690 S->gidx is the index of the first component for S.
17691 OVERLAPS_P non-zero means S should draw the foreground only, and
17692 use its physical height for clipping.
17693
17694 Value is the index of a component not in S. */
17695
17696 static int
17697 fill_composite_glyph_string (s, faces, overlaps_p)
17698 struct glyph_string *s;
17699 struct face **faces;
17700 int overlaps_p;
17701 {
17702 int i;
17703
17704 xassert (s);
17705
17706 s->for_overlaps_p = overlaps_p;
17707
17708 s->face = faces[s->gidx];
17709 s->font = s->face->font;
17710 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17711
17712 /* For all glyphs of this composition, starting at the offset
17713 S->gidx, until we reach the end of the definition or encounter a
17714 glyph that requires the different face, add it to S. */
17715 ++s->nchars;
17716 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17717 ++s->nchars;
17718
17719 /* All glyph strings for the same composition has the same width,
17720 i.e. the width set for the first component of the composition. */
17721
17722 s->width = s->first_glyph->pixel_width;
17723
17724 /* If the specified font could not be loaded, use the frame's
17725 default font, but record the fact that we couldn't load it in
17726 the glyph string so that we can draw rectangles for the
17727 characters of the glyph string. */
17728 if (s->font == NULL)
17729 {
17730 s->font_not_found_p = 1;
17731 s->font = FRAME_FONT (s->f);
17732 }
17733
17734 /* Adjust base line for subscript/superscript text. */
17735 s->ybase += s->first_glyph->voffset;
17736
17737 xassert (s->face && s->face->gc);
17738
17739 /* This glyph string must always be drawn with 16-bit functions. */
17740 s->two_byte_p = 1;
17741
17742 return s->gidx + s->nchars;
17743 }
17744
17745
17746 /* Fill glyph string S from a sequence of character glyphs.
17747
17748 FACE_ID is the face id of the string. START is the index of the
17749 first glyph to consider, END is the index of the last + 1.
17750 OVERLAPS_P non-zero means S should draw the foreground only, and
17751 use its physical height for clipping.
17752
17753 Value is the index of the first glyph not in S. */
17754
17755 static int
17756 fill_glyph_string (s, face_id, start, end, overlaps_p)
17757 struct glyph_string *s;
17758 int face_id;
17759 int start, end, overlaps_p;
17760 {
17761 struct glyph *glyph, *last;
17762 int voffset;
17763 int glyph_not_available_p;
17764
17765 xassert (s->f == XFRAME (s->w->frame));
17766 xassert (s->nchars == 0);
17767 xassert (start >= 0 && end > start);
17768
17769 s->for_overlaps_p = overlaps_p,
17770 glyph = s->row->glyphs[s->area] + start;
17771 last = s->row->glyphs[s->area] + end;
17772 voffset = glyph->voffset;
17773
17774 glyph_not_available_p = glyph->glyph_not_available_p;
17775
17776 while (glyph < last
17777 && glyph->type == CHAR_GLYPH
17778 && glyph->voffset == voffset
17779 /* Same face id implies same font, nowadays. */
17780 && glyph->face_id == face_id
17781 && glyph->glyph_not_available_p == glyph_not_available_p)
17782 {
17783 int two_byte_p;
17784
17785 s->face = get_glyph_face_and_encoding (s->f, glyph,
17786 s->char2b + s->nchars,
17787 &two_byte_p);
17788 s->two_byte_p = two_byte_p;
17789 ++s->nchars;
17790 xassert (s->nchars <= end - start);
17791 s->width += glyph->pixel_width;
17792 ++glyph;
17793 }
17794
17795 s->font = s->face->font;
17796 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17797
17798 /* If the specified font could not be loaded, use the frame's font,
17799 but record the fact that we couldn't load it in
17800 S->font_not_found_p so that we can draw rectangles for the
17801 characters of the glyph string. */
17802 if (s->font == NULL || glyph_not_available_p)
17803 {
17804 s->font_not_found_p = 1;
17805 s->font = FRAME_FONT (s->f);
17806 }
17807
17808 /* Adjust base line for subscript/superscript text. */
17809 s->ybase += voffset;
17810
17811 xassert (s->face && s->face->gc);
17812 return glyph - s->row->glyphs[s->area];
17813 }
17814
17815
17816 /* Fill glyph string S from image glyph S->first_glyph. */
17817
17818 static void
17819 fill_image_glyph_string (s)
17820 struct glyph_string *s;
17821 {
17822 xassert (s->first_glyph->type == IMAGE_GLYPH);
17823 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
17824 xassert (s->img);
17825 s->slice = s->first_glyph->slice;
17826 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
17827 s->font = s->face->font;
17828 s->width = s->first_glyph->pixel_width;
17829
17830 /* Adjust base line for subscript/superscript text. */
17831 s->ybase += s->first_glyph->voffset;
17832 }
17833
17834
17835 /* Fill glyph string S from a sequence of stretch glyphs.
17836
17837 ROW is the glyph row in which the glyphs are found, AREA is the
17838 area within the row. START is the index of the first glyph to
17839 consider, END is the index of the last + 1.
17840
17841 Value is the index of the first glyph not in S. */
17842
17843 static int
17844 fill_stretch_glyph_string (s, row, area, start, end)
17845 struct glyph_string *s;
17846 struct glyph_row *row;
17847 enum glyph_row_area area;
17848 int start, end;
17849 {
17850 struct glyph *glyph, *last;
17851 int voffset, face_id;
17852
17853 xassert (s->first_glyph->type == STRETCH_GLYPH);
17854
17855 glyph = s->row->glyphs[s->area] + start;
17856 last = s->row->glyphs[s->area] + end;
17857 face_id = glyph->face_id;
17858 s->face = FACE_FROM_ID (s->f, face_id);
17859 s->font = s->face->font;
17860 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17861 s->width = glyph->pixel_width;
17862 voffset = glyph->voffset;
17863
17864 for (++glyph;
17865 (glyph < last
17866 && glyph->type == STRETCH_GLYPH
17867 && glyph->voffset == voffset
17868 && glyph->face_id == face_id);
17869 ++glyph)
17870 s->width += glyph->pixel_width;
17871
17872 /* Adjust base line for subscript/superscript text. */
17873 s->ybase += voffset;
17874
17875 /* The case that face->gc == 0 is handled when drawing the glyph
17876 string by calling PREPARE_FACE_FOR_DISPLAY. */
17877 xassert (s->face);
17878 return glyph - s->row->glyphs[s->area];
17879 }
17880
17881
17882 /* EXPORT for RIF:
17883 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
17884 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
17885 assumed to be zero. */
17886
17887 void
17888 x_get_glyph_overhangs (glyph, f, left, right)
17889 struct glyph *glyph;
17890 struct frame *f;
17891 int *left, *right;
17892 {
17893 *left = *right = 0;
17894
17895 if (glyph->type == CHAR_GLYPH)
17896 {
17897 XFontStruct *font;
17898 struct face *face;
17899 struct font_info *font_info;
17900 XChar2b char2b;
17901 XCharStruct *pcm;
17902
17903 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
17904 font = face->font;
17905 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
17906 if (font /* ++KFS: Should this be font_info ? */
17907 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
17908 {
17909 if (pcm->rbearing > pcm->width)
17910 *right = pcm->rbearing - pcm->width;
17911 if (pcm->lbearing < 0)
17912 *left = -pcm->lbearing;
17913 }
17914 }
17915 }
17916
17917
17918 /* Return the index of the first glyph preceding glyph string S that
17919 is overwritten by S because of S's left overhang. Value is -1
17920 if no glyphs are overwritten. */
17921
17922 static int
17923 left_overwritten (s)
17924 struct glyph_string *s;
17925 {
17926 int k;
17927
17928 if (s->left_overhang)
17929 {
17930 int x = 0, i;
17931 struct glyph *glyphs = s->row->glyphs[s->area];
17932 int first = s->first_glyph - glyphs;
17933
17934 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
17935 x -= glyphs[i].pixel_width;
17936
17937 k = i + 1;
17938 }
17939 else
17940 k = -1;
17941
17942 return k;
17943 }
17944
17945
17946 /* Return the index of the first glyph preceding glyph string S that
17947 is overwriting S because of its right overhang. Value is -1 if no
17948 glyph in front of S overwrites S. */
17949
17950 static int
17951 left_overwriting (s)
17952 struct glyph_string *s;
17953 {
17954 int i, k, x;
17955 struct glyph *glyphs = s->row->glyphs[s->area];
17956 int first = s->first_glyph - glyphs;
17957
17958 k = -1;
17959 x = 0;
17960 for (i = first - 1; i >= 0; --i)
17961 {
17962 int left, right;
17963 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
17964 if (x + right > 0)
17965 k = i;
17966 x -= glyphs[i].pixel_width;
17967 }
17968
17969 return k;
17970 }
17971
17972
17973 /* Return the index of the last glyph following glyph string S that is
17974 not overwritten by S because of S's right overhang. Value is -1 if
17975 no such glyph is found. */
17976
17977 static int
17978 right_overwritten (s)
17979 struct glyph_string *s;
17980 {
17981 int k = -1;
17982
17983 if (s->right_overhang)
17984 {
17985 int x = 0, i;
17986 struct glyph *glyphs = s->row->glyphs[s->area];
17987 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
17988 int end = s->row->used[s->area];
17989
17990 for (i = first; i < end && s->right_overhang > x; ++i)
17991 x += glyphs[i].pixel_width;
17992
17993 k = i;
17994 }
17995
17996 return k;
17997 }
17998
17999
18000 /* Return the index of the last glyph following glyph string S that
18001 overwrites S because of its left overhang. Value is negative
18002 if no such glyph is found. */
18003
18004 static int
18005 right_overwriting (s)
18006 struct glyph_string *s;
18007 {
18008 int i, k, x;
18009 int end = s->row->used[s->area];
18010 struct glyph *glyphs = s->row->glyphs[s->area];
18011 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18012
18013 k = -1;
18014 x = 0;
18015 for (i = first; i < end; ++i)
18016 {
18017 int left, right;
18018 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18019 if (x - left < 0)
18020 k = i;
18021 x += glyphs[i].pixel_width;
18022 }
18023
18024 return k;
18025 }
18026
18027
18028 /* Get face and two-byte form of character C in face FACE_ID on frame
18029 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18030 means we want to display multibyte text. DISPLAY_P non-zero means
18031 make sure that X resources for the face returned are allocated.
18032 Value is a pointer to a realized face that is ready for display if
18033 DISPLAY_P is non-zero. */
18034
18035 static INLINE struct face *
18036 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18037 struct frame *f;
18038 int c, face_id;
18039 XChar2b *char2b;
18040 int multibyte_p, display_p;
18041 {
18042 struct face *face = FACE_FROM_ID (f, face_id);
18043
18044 if (!multibyte_p)
18045 {
18046 /* Unibyte case. We don't have to encode, but we have to make
18047 sure to use a face suitable for unibyte. */
18048 STORE_XCHAR2B (char2b, 0, c);
18049 face_id = FACE_FOR_CHAR (f, face, c);
18050 face = FACE_FROM_ID (f, face_id);
18051 }
18052 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18053 {
18054 /* Case of ASCII in a face known to fit ASCII. */
18055 STORE_XCHAR2B (char2b, 0, c);
18056 }
18057 else
18058 {
18059 int c1, c2, charset;
18060
18061 /* Split characters into bytes. If c2 is -1 afterwards, C is
18062 really a one-byte character so that byte1 is zero. */
18063 SPLIT_CHAR (c, charset, c1, c2);
18064 if (c2 > 0)
18065 STORE_XCHAR2B (char2b, c1, c2);
18066 else
18067 STORE_XCHAR2B (char2b, 0, c1);
18068
18069 /* Maybe encode the character in *CHAR2B. */
18070 if (face->font != NULL)
18071 {
18072 struct font_info *font_info
18073 = FONT_INFO_FROM_ID (f, face->font_info_id);
18074 if (font_info)
18075 rif->encode_char (c, char2b, font_info, 0);
18076 }
18077 }
18078
18079 /* Make sure X resources of the face are allocated. */
18080 #ifdef HAVE_X_WINDOWS
18081 if (display_p)
18082 #endif
18083 {
18084 xassert (face != NULL);
18085 PREPARE_FACE_FOR_DISPLAY (f, face);
18086 }
18087
18088 return face;
18089 }
18090
18091
18092 /* Set background width of glyph string S. START is the index of the
18093 first glyph following S. LAST_X is the right-most x-position + 1
18094 in the drawing area. */
18095
18096 static INLINE void
18097 set_glyph_string_background_width (s, start, last_x)
18098 struct glyph_string *s;
18099 int start;
18100 int last_x;
18101 {
18102 /* If the face of this glyph string has to be drawn to the end of
18103 the drawing area, set S->extends_to_end_of_line_p. */
18104 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18105
18106 if (start == s->row->used[s->area]
18107 && s->area == TEXT_AREA
18108 && ((s->hl == DRAW_NORMAL_TEXT
18109 && (s->row->fill_line_p
18110 || s->face->background != default_face->background
18111 || s->face->stipple != default_face->stipple
18112 || s->row->mouse_face_p))
18113 || s->hl == DRAW_MOUSE_FACE
18114 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18115 && s->row->fill_line_p)))
18116 s->extends_to_end_of_line_p = 1;
18117
18118 /* If S extends its face to the end of the line, set its
18119 background_width to the distance to the right edge of the drawing
18120 area. */
18121 if (s->extends_to_end_of_line_p)
18122 s->background_width = last_x - s->x + 1;
18123 else
18124 s->background_width = s->width;
18125 }
18126
18127
18128 /* Compute overhangs and x-positions for glyph string S and its
18129 predecessors, or successors. X is the starting x-position for S.
18130 BACKWARD_P non-zero means process predecessors. */
18131
18132 static void
18133 compute_overhangs_and_x (s, x, backward_p)
18134 struct glyph_string *s;
18135 int x;
18136 int backward_p;
18137 {
18138 if (backward_p)
18139 {
18140 while (s)
18141 {
18142 if (rif->compute_glyph_string_overhangs)
18143 rif->compute_glyph_string_overhangs (s);
18144 x -= s->width;
18145 s->x = x;
18146 s = s->prev;
18147 }
18148 }
18149 else
18150 {
18151 while (s)
18152 {
18153 if (rif->compute_glyph_string_overhangs)
18154 rif->compute_glyph_string_overhangs (s);
18155 s->x = x;
18156 x += s->width;
18157 s = s->next;
18158 }
18159 }
18160 }
18161
18162
18163
18164 /* The following macros are only called from draw_glyphs below.
18165 They reference the following parameters of that function directly:
18166 `w', `row', `area', and `overlap_p'
18167 as well as the following local variables:
18168 `s', `f', and `hdc' (in W32) */
18169
18170 #ifdef HAVE_NTGUI
18171 /* On W32, silently add local `hdc' variable to argument list of
18172 init_glyph_string. */
18173 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18174 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18175 #else
18176 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18177 init_glyph_string (s, char2b, w, row, area, start, hl)
18178 #endif
18179
18180 /* Add a glyph string for a stretch glyph to the list of strings
18181 between HEAD and TAIL. START is the index of the stretch glyph in
18182 row area AREA of glyph row ROW. END is the index of the last glyph
18183 in that glyph row area. X is the current output position assigned
18184 to the new glyph string constructed. HL overrides that face of the
18185 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18186 is the right-most x-position of the drawing area. */
18187
18188 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18189 and below -- keep them on one line. */
18190 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18191 do \
18192 { \
18193 s = (struct glyph_string *) alloca (sizeof *s); \
18194 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18195 START = fill_stretch_glyph_string (s, row, area, START, END); \
18196 append_glyph_string (&HEAD, &TAIL, s); \
18197 s->x = (X); \
18198 } \
18199 while (0)
18200
18201
18202 /* Add a glyph string for an image glyph to the list of strings
18203 between HEAD and TAIL. START is the index of the image glyph in
18204 row area AREA of glyph row ROW. END is the index of the last glyph
18205 in that glyph row area. X is the current output position assigned
18206 to the new glyph string constructed. HL overrides that face of the
18207 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18208 is the right-most x-position of the drawing area. */
18209
18210 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18211 do \
18212 { \
18213 s = (struct glyph_string *) alloca (sizeof *s); \
18214 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18215 fill_image_glyph_string (s); \
18216 append_glyph_string (&HEAD, &TAIL, s); \
18217 ++START; \
18218 s->x = (X); \
18219 } \
18220 while (0)
18221
18222
18223 /* Add a glyph string for a sequence of character glyphs to the list
18224 of strings between HEAD and TAIL. START is the index of the first
18225 glyph in row area AREA of glyph row ROW that is part of the new
18226 glyph string. END is the index of the last glyph in that glyph row
18227 area. X is the current output position assigned to the new glyph
18228 string constructed. HL overrides that face of the glyph; e.g. it
18229 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18230 right-most x-position of the drawing area. */
18231
18232 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18233 do \
18234 { \
18235 int c, face_id; \
18236 XChar2b *char2b; \
18237 \
18238 c = (row)->glyphs[area][START].u.ch; \
18239 face_id = (row)->glyphs[area][START].face_id; \
18240 \
18241 s = (struct glyph_string *) alloca (sizeof *s); \
18242 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18243 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18244 append_glyph_string (&HEAD, &TAIL, s); \
18245 s->x = (X); \
18246 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18247 } \
18248 while (0)
18249
18250
18251 /* Add a glyph string for a composite sequence to the list of strings
18252 between HEAD and TAIL. START is the index of the first glyph in
18253 row area AREA of glyph row ROW that is part of the new glyph
18254 string. END is the index of the last glyph in that glyph row area.
18255 X is the current output position assigned to the new glyph string
18256 constructed. HL overrides that face of the glyph; e.g. it is
18257 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18258 x-position of the drawing area. */
18259
18260 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18261 do { \
18262 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18263 int face_id = (row)->glyphs[area][START].face_id; \
18264 struct face *base_face = FACE_FROM_ID (f, face_id); \
18265 struct composition *cmp = composition_table[cmp_id]; \
18266 int glyph_len = cmp->glyph_len; \
18267 XChar2b *char2b; \
18268 struct face **faces; \
18269 struct glyph_string *first_s = NULL; \
18270 int n; \
18271 \
18272 base_face = base_face->ascii_face; \
18273 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18274 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18275 /* At first, fill in `char2b' and `faces'. */ \
18276 for (n = 0; n < glyph_len; n++) \
18277 { \
18278 int c = COMPOSITION_GLYPH (cmp, n); \
18279 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18280 faces[n] = FACE_FROM_ID (f, this_face_id); \
18281 get_char_face_and_encoding (f, c, this_face_id, \
18282 char2b + n, 1, 1); \
18283 } \
18284 \
18285 /* Make glyph_strings for each glyph sequence that is drawable by \
18286 the same face, and append them to HEAD/TAIL. */ \
18287 for (n = 0; n < cmp->glyph_len;) \
18288 { \
18289 s = (struct glyph_string *) alloca (sizeof *s); \
18290 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18291 append_glyph_string (&(HEAD), &(TAIL), s); \
18292 s->cmp = cmp; \
18293 s->gidx = n; \
18294 s->x = (X); \
18295 \
18296 if (n == 0) \
18297 first_s = s; \
18298 \
18299 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18300 } \
18301 \
18302 ++START; \
18303 s = first_s; \
18304 } while (0)
18305
18306
18307 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18308 of AREA of glyph row ROW on window W between indices START and END.
18309 HL overrides the face for drawing glyph strings, e.g. it is
18310 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18311 x-positions of the drawing area.
18312
18313 This is an ugly monster macro construct because we must use alloca
18314 to allocate glyph strings (because draw_glyphs can be called
18315 asynchronously). */
18316
18317 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18318 do \
18319 { \
18320 HEAD = TAIL = NULL; \
18321 while (START < END) \
18322 { \
18323 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18324 switch (first_glyph->type) \
18325 { \
18326 case CHAR_GLYPH: \
18327 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18328 HL, X, LAST_X); \
18329 break; \
18330 \
18331 case COMPOSITE_GLYPH: \
18332 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18333 HL, X, LAST_X); \
18334 break; \
18335 \
18336 case STRETCH_GLYPH: \
18337 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18338 HL, X, LAST_X); \
18339 break; \
18340 \
18341 case IMAGE_GLYPH: \
18342 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18343 HL, X, LAST_X); \
18344 break; \
18345 \
18346 default: \
18347 abort (); \
18348 } \
18349 \
18350 set_glyph_string_background_width (s, START, LAST_X); \
18351 (X) += s->width; \
18352 } \
18353 } \
18354 while (0)
18355
18356
18357 /* Draw glyphs between START and END in AREA of ROW on window W,
18358 starting at x-position X. X is relative to AREA in W. HL is a
18359 face-override with the following meaning:
18360
18361 DRAW_NORMAL_TEXT draw normally
18362 DRAW_CURSOR draw in cursor face
18363 DRAW_MOUSE_FACE draw in mouse face.
18364 DRAW_INVERSE_VIDEO draw in mode line face
18365 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18366 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18367
18368 If OVERLAPS_P is non-zero, draw only the foreground of characters
18369 and clip to the physical height of ROW.
18370
18371 Value is the x-position reached, relative to AREA of W. */
18372
18373 static int
18374 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18375 struct window *w;
18376 int x;
18377 struct glyph_row *row;
18378 enum glyph_row_area area;
18379 int start, end;
18380 enum draw_glyphs_face hl;
18381 int overlaps_p;
18382 {
18383 struct glyph_string *head, *tail;
18384 struct glyph_string *s;
18385 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18386 int last_x, area_width;
18387 int x_reached;
18388 int i, j;
18389 struct frame *f = XFRAME (WINDOW_FRAME (w));
18390 DECLARE_HDC (hdc);
18391
18392 ALLOCATE_HDC (hdc, f);
18393
18394 /* Let's rather be paranoid than getting a SEGV. */
18395 end = min (end, row->used[area]);
18396 start = max (0, start);
18397 start = min (end, start);
18398
18399 /* Translate X to frame coordinates. Set last_x to the right
18400 end of the drawing area. */
18401 if (row->full_width_p)
18402 {
18403 /* X is relative to the left edge of W, without scroll bars
18404 or fringes. */
18405 x += WINDOW_LEFT_EDGE_X (w);
18406 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18407 }
18408 else
18409 {
18410 int area_left = window_box_left (w, area);
18411 x += area_left;
18412 area_width = window_box_width (w, area);
18413 last_x = area_left + area_width;
18414 }
18415
18416 /* Build a doubly-linked list of glyph_string structures between
18417 head and tail from what we have to draw. Note that the macro
18418 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18419 the reason we use a separate variable `i'. */
18420 i = start;
18421 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18422 if (tail)
18423 x_reached = tail->x + tail->background_width;
18424 else
18425 x_reached = x;
18426
18427 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18428 the row, redraw some glyphs in front or following the glyph
18429 strings built above. */
18430 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18431 {
18432 int dummy_x = 0;
18433 struct glyph_string *h, *t;
18434
18435 /* Compute overhangs for all glyph strings. */
18436 if (rif->compute_glyph_string_overhangs)
18437 for (s = head; s; s = s->next)
18438 rif->compute_glyph_string_overhangs (s);
18439
18440 /* Prepend glyph strings for glyphs in front of the first glyph
18441 string that are overwritten because of the first glyph
18442 string's left overhang. The background of all strings
18443 prepended must be drawn because the first glyph string
18444 draws over it. */
18445 i = left_overwritten (head);
18446 if (i >= 0)
18447 {
18448 j = i;
18449 BUILD_GLYPH_STRINGS (j, start, h, t,
18450 DRAW_NORMAL_TEXT, dummy_x, last_x);
18451 start = i;
18452 compute_overhangs_and_x (t, head->x, 1);
18453 prepend_glyph_string_lists (&head, &tail, h, t);
18454 clip_head = head;
18455 }
18456
18457 /* Prepend glyph strings for glyphs in front of the first glyph
18458 string that overwrite that glyph string because of their
18459 right overhang. For these strings, only the foreground must
18460 be drawn, because it draws over the glyph string at `head'.
18461 The background must not be drawn because this would overwrite
18462 right overhangs of preceding glyphs for which no glyph
18463 strings exist. */
18464 i = left_overwriting (head);
18465 if (i >= 0)
18466 {
18467 clip_head = head;
18468 BUILD_GLYPH_STRINGS (i, start, h, t,
18469 DRAW_NORMAL_TEXT, dummy_x, last_x);
18470 for (s = h; s; s = s->next)
18471 s->background_filled_p = 1;
18472 compute_overhangs_and_x (t, head->x, 1);
18473 prepend_glyph_string_lists (&head, &tail, h, t);
18474 }
18475
18476 /* Append glyphs strings for glyphs following the last glyph
18477 string tail that are overwritten by tail. The background of
18478 these strings has to be drawn because tail's foreground draws
18479 over it. */
18480 i = right_overwritten (tail);
18481 if (i >= 0)
18482 {
18483 BUILD_GLYPH_STRINGS (end, i, h, t,
18484 DRAW_NORMAL_TEXT, x, last_x);
18485 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18486 append_glyph_string_lists (&head, &tail, h, t);
18487 clip_tail = tail;
18488 }
18489
18490 /* Append glyph strings for glyphs following the last glyph
18491 string tail that overwrite tail. The foreground of such
18492 glyphs has to be drawn because it writes into the background
18493 of tail. The background must not be drawn because it could
18494 paint over the foreground of following glyphs. */
18495 i = right_overwriting (tail);
18496 if (i >= 0)
18497 {
18498 clip_tail = tail;
18499 BUILD_GLYPH_STRINGS (end, i, h, t,
18500 DRAW_NORMAL_TEXT, x, last_x);
18501 for (s = h; s; s = s->next)
18502 s->background_filled_p = 1;
18503 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18504 append_glyph_string_lists (&head, &tail, h, t);
18505 }
18506 if (clip_head || clip_tail)
18507 for (s = head; s; s = s->next)
18508 {
18509 s->clip_head = clip_head;
18510 s->clip_tail = clip_tail;
18511 }
18512 }
18513
18514 /* Draw all strings. */
18515 for (s = head; s; s = s->next)
18516 rif->draw_glyph_string (s);
18517
18518 if (area == TEXT_AREA
18519 && !row->full_width_p
18520 /* When drawing overlapping rows, only the glyph strings'
18521 foreground is drawn, which doesn't erase a cursor
18522 completely. */
18523 && !overlaps_p)
18524 {
18525 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18526 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18527 : (tail ? tail->x + tail->background_width : x));
18528
18529 int text_left = window_box_left (w, TEXT_AREA);
18530 x0 -= text_left;
18531 x1 -= text_left;
18532
18533 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18534 row->y, MATRIX_ROW_BOTTOM_Y (row));
18535 }
18536
18537 /* Value is the x-position up to which drawn, relative to AREA of W.
18538 This doesn't include parts drawn because of overhangs. */
18539 if (row->full_width_p)
18540 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18541 else
18542 x_reached -= window_box_left (w, area);
18543
18544 RELEASE_HDC (hdc, f);
18545
18546 return x_reached;
18547 }
18548
18549 /* Expand row matrix if too narrow. Don't expand if area
18550 is not present. */
18551
18552 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18553 { \
18554 if (!fonts_changed_p \
18555 && (it->glyph_row->glyphs[area] \
18556 < it->glyph_row->glyphs[area + 1])) \
18557 { \
18558 it->w->ncols_scale_factor++; \
18559 fonts_changed_p = 1; \
18560 } \
18561 }
18562
18563 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18564 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18565
18566 static INLINE void
18567 append_glyph (it)
18568 struct it *it;
18569 {
18570 struct glyph *glyph;
18571 enum glyph_row_area area = it->area;
18572
18573 xassert (it->glyph_row);
18574 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18575
18576 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18577 if (glyph < it->glyph_row->glyphs[area + 1])
18578 {
18579 glyph->charpos = CHARPOS (it->position);
18580 glyph->object = it->object;
18581 glyph->pixel_width = it->pixel_width;
18582 glyph->ascent = it->ascent;
18583 glyph->descent = it->descent;
18584 glyph->voffset = it->voffset;
18585 glyph->type = CHAR_GLYPH;
18586 glyph->multibyte_p = it->multibyte_p;
18587 glyph->left_box_line_p = it->start_of_box_run_p;
18588 glyph->right_box_line_p = it->end_of_box_run_p;
18589 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18590 || it->phys_descent > it->descent);
18591 glyph->padding_p = 0;
18592 glyph->glyph_not_available_p = it->glyph_not_available_p;
18593 glyph->face_id = it->face_id;
18594 glyph->u.ch = it->char_to_display;
18595 glyph->slice = null_glyph_slice;
18596 glyph->font_type = FONT_TYPE_UNKNOWN;
18597 ++it->glyph_row->used[area];
18598 }
18599 else
18600 IT_EXPAND_MATRIX_WIDTH (it, area);
18601 }
18602
18603 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18604 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18605
18606 static INLINE void
18607 append_composite_glyph (it)
18608 struct it *it;
18609 {
18610 struct glyph *glyph;
18611 enum glyph_row_area area = it->area;
18612
18613 xassert (it->glyph_row);
18614
18615 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18616 if (glyph < it->glyph_row->glyphs[area + 1])
18617 {
18618 glyph->charpos = CHARPOS (it->position);
18619 glyph->object = it->object;
18620 glyph->pixel_width = it->pixel_width;
18621 glyph->ascent = it->ascent;
18622 glyph->descent = it->descent;
18623 glyph->voffset = it->voffset;
18624 glyph->type = COMPOSITE_GLYPH;
18625 glyph->multibyte_p = it->multibyte_p;
18626 glyph->left_box_line_p = it->start_of_box_run_p;
18627 glyph->right_box_line_p = it->end_of_box_run_p;
18628 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18629 || it->phys_descent > it->descent);
18630 glyph->padding_p = 0;
18631 glyph->glyph_not_available_p = 0;
18632 glyph->face_id = it->face_id;
18633 glyph->u.cmp_id = it->cmp_id;
18634 glyph->slice = null_glyph_slice;
18635 glyph->font_type = FONT_TYPE_UNKNOWN;
18636 ++it->glyph_row->used[area];
18637 }
18638 else
18639 IT_EXPAND_MATRIX_WIDTH (it, area);
18640 }
18641
18642
18643 /* Change IT->ascent and IT->height according to the setting of
18644 IT->voffset. */
18645
18646 static INLINE void
18647 take_vertical_position_into_account (it)
18648 struct it *it;
18649 {
18650 if (it->voffset)
18651 {
18652 if (it->voffset < 0)
18653 /* Increase the ascent so that we can display the text higher
18654 in the line. */
18655 it->ascent -= it->voffset;
18656 else
18657 /* Increase the descent so that we can display the text lower
18658 in the line. */
18659 it->descent += it->voffset;
18660 }
18661 }
18662
18663
18664 /* Produce glyphs/get display metrics for the image IT is loaded with.
18665 See the description of struct display_iterator in dispextern.h for
18666 an overview of struct display_iterator. */
18667
18668 static void
18669 produce_image_glyph (it)
18670 struct it *it;
18671 {
18672 struct image *img;
18673 struct face *face;
18674 int glyph_ascent;
18675 struct glyph_slice slice;
18676
18677 xassert (it->what == IT_IMAGE);
18678
18679 face = FACE_FROM_ID (it->f, it->face_id);
18680 xassert (face);
18681 /* Make sure X resources of the face is loaded. */
18682 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18683
18684 if (it->image_id < 0)
18685 {
18686 /* Fringe bitmap. */
18687 it->ascent = it->phys_ascent = 0;
18688 it->descent = it->phys_descent = 0;
18689 it->pixel_width = 0;
18690 it->nglyphs = 0;
18691 return;
18692 }
18693
18694 img = IMAGE_FROM_ID (it->f, it->image_id);
18695 xassert (img);
18696 /* Make sure X resources of the image is loaded. */
18697 prepare_image_for_display (it->f, img);
18698
18699 slice.x = slice.y = 0;
18700 slice.width = img->width;
18701 slice.height = img->height;
18702
18703 if (INTEGERP (it->slice.x))
18704 slice.x = XINT (it->slice.x);
18705 else if (FLOATP (it->slice.x))
18706 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18707
18708 if (INTEGERP (it->slice.y))
18709 slice.y = XINT (it->slice.y);
18710 else if (FLOATP (it->slice.y))
18711 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18712
18713 if (INTEGERP (it->slice.width))
18714 slice.width = XINT (it->slice.width);
18715 else if (FLOATP (it->slice.width))
18716 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18717
18718 if (INTEGERP (it->slice.height))
18719 slice.height = XINT (it->slice.height);
18720 else if (FLOATP (it->slice.height))
18721 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18722
18723 if (slice.x >= img->width)
18724 slice.x = img->width;
18725 if (slice.y >= img->height)
18726 slice.y = img->height;
18727 if (slice.x + slice.width >= img->width)
18728 slice.width = img->width - slice.x;
18729 if (slice.y + slice.height > img->height)
18730 slice.height = img->height - slice.y;
18731
18732 if (slice.width == 0 || slice.height == 0)
18733 return;
18734
18735 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18736
18737 it->descent = slice.height - glyph_ascent;
18738 if (slice.y == 0)
18739 it->descent += img->vmargin;
18740 if (slice.y + slice.height == img->height)
18741 it->descent += img->vmargin;
18742 it->phys_descent = it->descent;
18743
18744 it->pixel_width = slice.width;
18745 if (slice.x == 0)
18746 it->pixel_width += img->hmargin;
18747 if (slice.x + slice.width == img->width)
18748 it->pixel_width += img->hmargin;
18749
18750 /* It's quite possible for images to have an ascent greater than
18751 their height, so don't get confused in that case. */
18752 if (it->descent < 0)
18753 it->descent = 0;
18754
18755 #if 0 /* this breaks image tiling */
18756 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18757 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18758 if (face_ascent > it->ascent)
18759 it->ascent = it->phys_ascent = face_ascent;
18760 #endif
18761
18762 it->nglyphs = 1;
18763
18764 if (face->box != FACE_NO_BOX)
18765 {
18766 if (face->box_line_width > 0)
18767 {
18768 if (slice.y == 0)
18769 it->ascent += face->box_line_width;
18770 if (slice.y + slice.height == img->height)
18771 it->descent += face->box_line_width;
18772 }
18773
18774 if (it->start_of_box_run_p && slice.x == 0)
18775 it->pixel_width += abs (face->box_line_width);
18776 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18777 it->pixel_width += abs (face->box_line_width);
18778 }
18779
18780 take_vertical_position_into_account (it);
18781
18782 if (it->glyph_row)
18783 {
18784 struct glyph *glyph;
18785 enum glyph_row_area area = it->area;
18786
18787 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18788 if (glyph < it->glyph_row->glyphs[area + 1])
18789 {
18790 glyph->charpos = CHARPOS (it->position);
18791 glyph->object = it->object;
18792 glyph->pixel_width = it->pixel_width;
18793 glyph->ascent = glyph_ascent;
18794 glyph->descent = it->descent;
18795 glyph->voffset = it->voffset;
18796 glyph->type = IMAGE_GLYPH;
18797 glyph->multibyte_p = it->multibyte_p;
18798 glyph->left_box_line_p = it->start_of_box_run_p;
18799 glyph->right_box_line_p = it->end_of_box_run_p;
18800 glyph->overlaps_vertically_p = 0;
18801 glyph->padding_p = 0;
18802 glyph->glyph_not_available_p = 0;
18803 glyph->face_id = it->face_id;
18804 glyph->u.img_id = img->id;
18805 glyph->slice = slice;
18806 glyph->font_type = FONT_TYPE_UNKNOWN;
18807 ++it->glyph_row->used[area];
18808 }
18809 else
18810 IT_EXPAND_MATRIX_WIDTH (it, area);
18811 }
18812 }
18813
18814
18815 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
18816 of the glyph, WIDTH and HEIGHT are the width and height of the
18817 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
18818
18819 static void
18820 append_stretch_glyph (it, object, width, height, ascent)
18821 struct it *it;
18822 Lisp_Object object;
18823 int width, height;
18824 int ascent;
18825 {
18826 struct glyph *glyph;
18827 enum glyph_row_area area = it->area;
18828
18829 xassert (ascent >= 0 && ascent <= height);
18830
18831 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18832 if (glyph < it->glyph_row->glyphs[area + 1])
18833 {
18834 glyph->charpos = CHARPOS (it->position);
18835 glyph->object = object;
18836 glyph->pixel_width = width;
18837 glyph->ascent = ascent;
18838 glyph->descent = height - ascent;
18839 glyph->voffset = it->voffset;
18840 glyph->type = STRETCH_GLYPH;
18841 glyph->multibyte_p = it->multibyte_p;
18842 glyph->left_box_line_p = it->start_of_box_run_p;
18843 glyph->right_box_line_p = it->end_of_box_run_p;
18844 glyph->overlaps_vertically_p = 0;
18845 glyph->padding_p = 0;
18846 glyph->glyph_not_available_p = 0;
18847 glyph->face_id = it->face_id;
18848 glyph->u.stretch.ascent = ascent;
18849 glyph->u.stretch.height = height;
18850 glyph->slice = null_glyph_slice;
18851 glyph->font_type = FONT_TYPE_UNKNOWN;
18852 ++it->glyph_row->used[area];
18853 }
18854 else
18855 IT_EXPAND_MATRIX_WIDTH (it, area);
18856 }
18857
18858
18859 /* Produce a stretch glyph for iterator IT. IT->object is the value
18860 of the glyph property displayed. The value must be a list
18861 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
18862 being recognized:
18863
18864 1. `:width WIDTH' specifies that the space should be WIDTH *
18865 canonical char width wide. WIDTH may be an integer or floating
18866 point number.
18867
18868 2. `:relative-width FACTOR' specifies that the width of the stretch
18869 should be computed from the width of the first character having the
18870 `glyph' property, and should be FACTOR times that width.
18871
18872 3. `:align-to HPOS' specifies that the space should be wide enough
18873 to reach HPOS, a value in canonical character units.
18874
18875 Exactly one of the above pairs must be present.
18876
18877 4. `:height HEIGHT' specifies that the height of the stretch produced
18878 should be HEIGHT, measured in canonical character units.
18879
18880 5. `:relative-height FACTOR' specifies that the height of the
18881 stretch should be FACTOR times the height of the characters having
18882 the glyph property.
18883
18884 Either none or exactly one of 4 or 5 must be present.
18885
18886 6. `:ascent ASCENT' specifies that ASCENT percent of the height
18887 of the stretch should be used for the ascent of the stretch.
18888 ASCENT must be in the range 0 <= ASCENT <= 100. */
18889
18890 static void
18891 produce_stretch_glyph (it)
18892 struct it *it;
18893 {
18894 /* (space :width WIDTH :height HEIGHT ...) */
18895 Lisp_Object prop, plist;
18896 int width = 0, height = 0, align_to = -1;
18897 int zero_width_ok_p = 0, zero_height_ok_p = 0;
18898 int ascent = 0;
18899 double tem;
18900 struct face *face = FACE_FROM_ID (it->f, it->face_id);
18901 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
18902
18903 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18904
18905 /* List should start with `space'. */
18906 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
18907 plist = XCDR (it->object);
18908
18909 /* Compute the width of the stretch. */
18910 if ((prop = Fsafe_plist_get (plist, QCwidth), !NILP (prop))
18911 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
18912 {
18913 /* Absolute width `:width WIDTH' specified and valid. */
18914 zero_width_ok_p = 1;
18915 width = (int)tem;
18916 }
18917 else if (prop = Fsafe_plist_get (plist, QCrelative_width),
18918 NUMVAL (prop) > 0)
18919 {
18920 /* Relative width `:relative-width FACTOR' specified and valid.
18921 Compute the width of the characters having the `glyph'
18922 property. */
18923 struct it it2;
18924 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
18925
18926 it2 = *it;
18927 if (it->multibyte_p)
18928 {
18929 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
18930 - IT_BYTEPOS (*it));
18931 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
18932 }
18933 else
18934 it2.c = *p, it2.len = 1;
18935
18936 it2.glyph_row = NULL;
18937 it2.what = IT_CHARACTER;
18938 x_produce_glyphs (&it2);
18939 width = NUMVAL (prop) * it2.pixel_width;
18940 }
18941 else if ((prop = Fsafe_plist_get (plist, QCalign_to), !NILP (prop))
18942 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
18943 {
18944 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
18945 align_to = (align_to < 0
18946 ? 0
18947 : align_to - window_box_left_offset (it->w, TEXT_AREA));
18948 else if (align_to < 0)
18949 align_to = window_box_left_offset (it->w, TEXT_AREA);
18950 width = max (0, (int)tem + align_to - it->current_x);
18951 zero_width_ok_p = 1;
18952 }
18953 else
18954 /* Nothing specified -> width defaults to canonical char width. */
18955 width = FRAME_COLUMN_WIDTH (it->f);
18956
18957 if (width <= 0 && (width < 0 || !zero_width_ok_p))
18958 width = 1;
18959
18960 /* Compute height. */
18961 if ((prop = Fsafe_plist_get (plist, QCheight), !NILP (prop))
18962 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18963 {
18964 height = (int)tem;
18965 zero_height_ok_p = 1;
18966 }
18967 else if (prop = Fsafe_plist_get (plist, QCrelative_height),
18968 NUMVAL (prop) > 0)
18969 height = FONT_HEIGHT (font) * NUMVAL (prop);
18970 else
18971 height = FONT_HEIGHT (font);
18972
18973 if (height <= 0 && (height < 0 || !zero_height_ok_p))
18974 height = 1;
18975
18976 /* Compute percentage of height used for ascent. If
18977 `:ascent ASCENT' is present and valid, use that. Otherwise,
18978 derive the ascent from the font in use. */
18979 if (prop = Fsafe_plist_get (plist, QCascent),
18980 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
18981 ascent = height * NUMVAL (prop) / 100.0;
18982 else if (!NILP (prop)
18983 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
18984 ascent = min (max (0, (int)tem), height);
18985 else
18986 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
18987
18988 if (width > 0 && height > 0 && it->glyph_row)
18989 {
18990 Lisp_Object object = it->stack[it->sp - 1].string;
18991 if (!STRINGP (object))
18992 object = it->w->buffer;
18993 append_stretch_glyph (it, object, width, height, ascent);
18994 }
18995
18996 it->pixel_width = width;
18997 it->ascent = it->phys_ascent = ascent;
18998 it->descent = it->phys_descent = height - it->ascent;
18999 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19000
19001 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19002 {
19003 if (face->box_line_width > 0)
19004 {
19005 it->ascent += face->box_line_width;
19006 it->descent += face->box_line_width;
19007 }
19008
19009 if (it->start_of_box_run_p)
19010 it->pixel_width += abs (face->box_line_width);
19011 if (it->end_of_box_run_p)
19012 it->pixel_width += abs (face->box_line_width);
19013 }
19014
19015 take_vertical_position_into_account (it);
19016 }
19017
19018 /* Get line-height and line-spacing property at point.
19019 If line-height has format (HEIGHT TOTAL), return TOTAL
19020 in TOTAL_HEIGHT. */
19021
19022 static Lisp_Object
19023 get_line_height_property (it, prop)
19024 struct it *it;
19025 Lisp_Object prop;
19026 {
19027 Lisp_Object position, val;
19028
19029 if (STRINGP (it->object))
19030 position = make_number (IT_STRING_CHARPOS (*it));
19031 else if (BUFFERP (it->object))
19032 position = make_number (IT_CHARPOS (*it));
19033 else
19034 return Qnil;
19035
19036 return Fget_char_property (position, prop, it->object);
19037 }
19038
19039 /* Calculate line-height and line-spacing properties.
19040 An integer value specifies explicit pixel value.
19041 A float value specifies relative value to current face height.
19042 A cons (float . face-name) specifies relative value to
19043 height of specified face font.
19044
19045 Returns height in pixels, or nil. */
19046
19047
19048 static Lisp_Object
19049 calc_line_height_property (it, val, font, boff, override)
19050 struct it *it;
19051 Lisp_Object val;
19052 XFontStruct *font;
19053 int boff, override;
19054 {
19055 Lisp_Object face_name = Qnil;
19056 int ascent, descent, height;
19057
19058 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19059 return val;
19060
19061 if (CONSP (val))
19062 {
19063 face_name = XCAR (val);
19064 val = XCDR (val);
19065 if (!NUMBERP (val))
19066 val = make_number (1);
19067 if (NILP (face_name))
19068 {
19069 height = it->ascent + it->descent;
19070 goto scale;
19071 }
19072 }
19073
19074 if (NILP (face_name))
19075 {
19076 font = FRAME_FONT (it->f);
19077 boff = FRAME_BASELINE_OFFSET (it->f);
19078 }
19079 else if (EQ (face_name, Qt))
19080 {
19081 override = 0;
19082 }
19083 else
19084 {
19085 int face_id;
19086 struct face *face;
19087 struct font_info *font_info;
19088
19089 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19090 if (face_id < 0)
19091 return make_number (-1);
19092
19093 face = FACE_FROM_ID (it->f, face_id);
19094 font = face->font;
19095 if (font == NULL)
19096 return make_number (-1);
19097
19098 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19099 boff = font_info->baseline_offset;
19100 if (font_info->vertical_centering)
19101 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19102 }
19103
19104 ascent = FONT_BASE (font) + boff;
19105 descent = FONT_DESCENT (font) - boff;
19106
19107 if (override)
19108 {
19109 it->override_ascent = ascent;
19110 it->override_descent = descent;
19111 it->override_boff = boff;
19112 }
19113
19114 height = ascent + descent;
19115
19116 scale:
19117 if (FLOATP (val))
19118 height = (int)(XFLOAT_DATA (val) * height);
19119 else if (INTEGERP (val))
19120 height *= XINT (val);
19121
19122 return make_number (height);
19123 }
19124
19125
19126 /* RIF:
19127 Produce glyphs/get display metrics for the display element IT is
19128 loaded with. See the description of struct display_iterator in
19129 dispextern.h for an overview of struct display_iterator. */
19130
19131 void
19132 x_produce_glyphs (it)
19133 struct it *it;
19134 {
19135 int extra_line_spacing = it->extra_line_spacing;
19136
19137 it->glyph_not_available_p = 0;
19138
19139 if (it->what == IT_CHARACTER)
19140 {
19141 XChar2b char2b;
19142 XFontStruct *font;
19143 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19144 XCharStruct *pcm;
19145 int font_not_found_p;
19146 struct font_info *font_info;
19147 int boff; /* baseline offset */
19148 /* We may change it->multibyte_p upon unibyte<->multibyte
19149 conversion. So, save the current value now and restore it
19150 later.
19151
19152 Note: It seems that we don't have to record multibyte_p in
19153 struct glyph because the character code itself tells if or
19154 not the character is multibyte. Thus, in the future, we must
19155 consider eliminating the field `multibyte_p' in the struct
19156 glyph. */
19157 int saved_multibyte_p = it->multibyte_p;
19158
19159 /* Maybe translate single-byte characters to multibyte, or the
19160 other way. */
19161 it->char_to_display = it->c;
19162 if (!ASCII_BYTE_P (it->c))
19163 {
19164 if (unibyte_display_via_language_environment
19165 && SINGLE_BYTE_CHAR_P (it->c)
19166 && (it->c >= 0240
19167 || !NILP (Vnonascii_translation_table)))
19168 {
19169 it->char_to_display = unibyte_char_to_multibyte (it->c);
19170 it->multibyte_p = 1;
19171 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19172 face = FACE_FROM_ID (it->f, it->face_id);
19173 }
19174 else if (!SINGLE_BYTE_CHAR_P (it->c)
19175 && !it->multibyte_p)
19176 {
19177 it->multibyte_p = 1;
19178 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19179 face = FACE_FROM_ID (it->f, it->face_id);
19180 }
19181 }
19182
19183 /* Get font to use. Encode IT->char_to_display. */
19184 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19185 &char2b, it->multibyte_p, 0);
19186 font = face->font;
19187
19188 /* When no suitable font found, use the default font. */
19189 font_not_found_p = font == NULL;
19190 if (font_not_found_p)
19191 {
19192 font = FRAME_FONT (it->f);
19193 boff = FRAME_BASELINE_OFFSET (it->f);
19194 font_info = NULL;
19195 }
19196 else
19197 {
19198 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19199 boff = font_info->baseline_offset;
19200 if (font_info->vertical_centering)
19201 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19202 }
19203
19204 if (it->char_to_display >= ' '
19205 && (!it->multibyte_p || it->char_to_display < 128))
19206 {
19207 /* Either unibyte or ASCII. */
19208 int stretched_p;
19209
19210 it->nglyphs = 1;
19211
19212 pcm = rif->per_char_metric (font, &char2b,
19213 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19214
19215 if (it->override_ascent >= 0)
19216 {
19217 it->ascent = it->override_ascent;
19218 it->descent = it->override_descent;
19219 boff = it->override_boff;
19220 }
19221 else
19222 {
19223 it->ascent = FONT_BASE (font) + boff;
19224 it->descent = FONT_DESCENT (font) - boff;
19225 }
19226
19227 if (pcm)
19228 {
19229 it->phys_ascent = pcm->ascent + boff;
19230 it->phys_descent = pcm->descent - boff;
19231 it->pixel_width = pcm->width;
19232 }
19233 else
19234 {
19235 it->glyph_not_available_p = 1;
19236 it->phys_ascent = it->ascent;
19237 it->phys_descent = it->descent;
19238 it->pixel_width = FONT_WIDTH (font);
19239 }
19240
19241 if (it->constrain_row_ascent_descent_p)
19242 {
19243 if (it->descent > it->max_descent)
19244 {
19245 it->ascent += it->descent - it->max_descent;
19246 it->descent = it->max_descent;
19247 }
19248 if (it->ascent > it->max_ascent)
19249 {
19250 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19251 it->ascent = it->max_ascent;
19252 }
19253 it->phys_ascent = min (it->phys_ascent, it->ascent);
19254 it->phys_descent = min (it->phys_descent, it->descent);
19255 extra_line_spacing = 0;
19256 }
19257
19258 /* If this is a space inside a region of text with
19259 `space-width' property, change its width. */
19260 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19261 if (stretched_p)
19262 it->pixel_width *= XFLOATINT (it->space_width);
19263
19264 /* If face has a box, add the box thickness to the character
19265 height. If character has a box line to the left and/or
19266 right, add the box line width to the character's width. */
19267 if (face->box != FACE_NO_BOX)
19268 {
19269 int thick = face->box_line_width;
19270
19271 if (thick > 0)
19272 {
19273 it->ascent += thick;
19274 it->descent += thick;
19275 }
19276 else
19277 thick = -thick;
19278
19279 if (it->start_of_box_run_p)
19280 it->pixel_width += thick;
19281 if (it->end_of_box_run_p)
19282 it->pixel_width += thick;
19283 }
19284
19285 /* If face has an overline, add the height of the overline
19286 (1 pixel) and a 1 pixel margin to the character height. */
19287 if (face->overline_p)
19288 it->ascent += 2;
19289
19290 if (it->constrain_row_ascent_descent_p)
19291 {
19292 if (it->ascent > it->max_ascent)
19293 it->ascent = it->max_ascent;
19294 if (it->descent > it->max_descent)
19295 it->descent = it->max_descent;
19296 }
19297
19298 take_vertical_position_into_account (it);
19299
19300 /* If we have to actually produce glyphs, do it. */
19301 if (it->glyph_row)
19302 {
19303 if (stretched_p)
19304 {
19305 /* Translate a space with a `space-width' property
19306 into a stretch glyph. */
19307 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19308 / FONT_HEIGHT (font));
19309 append_stretch_glyph (it, it->object, it->pixel_width,
19310 it->ascent + it->descent, ascent);
19311 }
19312 else
19313 append_glyph (it);
19314
19315 /* If characters with lbearing or rbearing are displayed
19316 in this line, record that fact in a flag of the
19317 glyph row. This is used to optimize X output code. */
19318 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19319 it->glyph_row->contains_overlapping_glyphs_p = 1;
19320 }
19321 }
19322 else if (it->char_to_display == '\n')
19323 {
19324 /* A newline has no width but we need the height of the line.
19325 But if previous part of the line set a height, don't
19326 increase that height */
19327
19328 Lisp_Object height;
19329 Lisp_Object total_height = Qnil;
19330
19331 it->override_ascent = -1;
19332 it->pixel_width = 0;
19333 it->nglyphs = 0;
19334
19335 height = get_line_height_property(it, Qline_height);
19336 /* Split (line-height total-height) list */
19337 if (CONSP (height)
19338 && CONSP (XCDR (height))
19339 && NILP (XCDR (XCDR (height))))
19340 {
19341 total_height = XCAR (XCDR (height));
19342 height = XCAR (height);
19343 }
19344 height = calc_line_height_property(it, height, font, boff, 1);
19345
19346 if (it->override_ascent >= 0)
19347 {
19348 it->ascent = it->override_ascent;
19349 it->descent = it->override_descent;
19350 boff = it->override_boff;
19351 }
19352 else
19353 {
19354 it->ascent = FONT_BASE (font) + boff;
19355 it->descent = FONT_DESCENT (font) - boff;
19356 }
19357
19358 if (EQ (height, Qt))
19359 {
19360 if (it->descent > it->max_descent)
19361 {
19362 it->ascent += it->descent - it->max_descent;
19363 it->descent = it->max_descent;
19364 }
19365 if (it->ascent > it->max_ascent)
19366 {
19367 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19368 it->ascent = it->max_ascent;
19369 }
19370 it->phys_ascent = min (it->phys_ascent, it->ascent);
19371 it->phys_descent = min (it->phys_descent, it->descent);
19372 it->constrain_row_ascent_descent_p = 1;
19373 extra_line_spacing = 0;
19374 }
19375 else
19376 {
19377 Lisp_Object spacing;
19378 int total = 0;
19379
19380 it->phys_ascent = it->ascent;
19381 it->phys_descent = it->descent;
19382
19383 if ((it->max_ascent > 0 || it->max_descent > 0)
19384 && face->box != FACE_NO_BOX
19385 && face->box_line_width > 0)
19386 {
19387 it->ascent += face->box_line_width;
19388 it->descent += face->box_line_width;
19389 }
19390 if (!NILP (height)
19391 && XINT (height) > it->ascent + it->descent)
19392 it->ascent = XINT (height) - it->descent;
19393
19394 if (!NILP (total_height))
19395 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19396 else
19397 {
19398 spacing = get_line_height_property(it, Qline_spacing);
19399 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19400 }
19401 if (INTEGERP (spacing))
19402 {
19403 extra_line_spacing = XINT (spacing);
19404 if (!NILP (total_height))
19405 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19406 }
19407 }
19408 }
19409 else if (it->char_to_display == '\t')
19410 {
19411 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19412 int x = it->current_x + it->continuation_lines_width;
19413 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19414
19415 /* If the distance from the current position to the next tab
19416 stop is less than a space character width, use the
19417 tab stop after that. */
19418 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19419 next_tab_x += tab_width;
19420
19421 it->pixel_width = next_tab_x - x;
19422 it->nglyphs = 1;
19423 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19424 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19425
19426 if (it->glyph_row)
19427 {
19428 append_stretch_glyph (it, it->object, it->pixel_width,
19429 it->ascent + it->descent, it->ascent);
19430 }
19431 }
19432 else
19433 {
19434 /* A multi-byte character. Assume that the display width of the
19435 character is the width of the character multiplied by the
19436 width of the font. */
19437
19438 /* If we found a font, this font should give us the right
19439 metrics. If we didn't find a font, use the frame's
19440 default font and calculate the width of the character
19441 from the charset width; this is what old redisplay code
19442 did. */
19443
19444 pcm = rif->per_char_metric (font, &char2b,
19445 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19446
19447 if (font_not_found_p || !pcm)
19448 {
19449 int charset = CHAR_CHARSET (it->char_to_display);
19450
19451 it->glyph_not_available_p = 1;
19452 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19453 * CHARSET_WIDTH (charset));
19454 it->phys_ascent = FONT_BASE (font) + boff;
19455 it->phys_descent = FONT_DESCENT (font) - boff;
19456 }
19457 else
19458 {
19459 it->pixel_width = pcm->width;
19460 it->phys_ascent = pcm->ascent + boff;
19461 it->phys_descent = pcm->descent - boff;
19462 if (it->glyph_row
19463 && (pcm->lbearing < 0
19464 || pcm->rbearing > pcm->width))
19465 it->glyph_row->contains_overlapping_glyphs_p = 1;
19466 }
19467 it->nglyphs = 1;
19468 it->ascent = FONT_BASE (font) + boff;
19469 it->descent = FONT_DESCENT (font) - boff;
19470 if (face->box != FACE_NO_BOX)
19471 {
19472 int thick = face->box_line_width;
19473
19474 if (thick > 0)
19475 {
19476 it->ascent += thick;
19477 it->descent += thick;
19478 }
19479 else
19480 thick = - thick;
19481
19482 if (it->start_of_box_run_p)
19483 it->pixel_width += thick;
19484 if (it->end_of_box_run_p)
19485 it->pixel_width += thick;
19486 }
19487
19488 /* If face has an overline, add the height of the overline
19489 (1 pixel) and a 1 pixel margin to the character height. */
19490 if (face->overline_p)
19491 it->ascent += 2;
19492
19493 take_vertical_position_into_account (it);
19494
19495 if (it->glyph_row)
19496 append_glyph (it);
19497 }
19498 it->multibyte_p = saved_multibyte_p;
19499 }
19500 else if (it->what == IT_COMPOSITION)
19501 {
19502 /* Note: A composition is represented as one glyph in the
19503 glyph matrix. There are no padding glyphs. */
19504 XChar2b char2b;
19505 XFontStruct *font;
19506 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19507 XCharStruct *pcm;
19508 int font_not_found_p;
19509 struct font_info *font_info;
19510 int boff; /* baseline offset */
19511 struct composition *cmp = composition_table[it->cmp_id];
19512
19513 /* Maybe translate single-byte characters to multibyte. */
19514 it->char_to_display = it->c;
19515 if (unibyte_display_via_language_environment
19516 && SINGLE_BYTE_CHAR_P (it->c)
19517 && (it->c >= 0240
19518 || (it->c >= 0200
19519 && !NILP (Vnonascii_translation_table))))
19520 {
19521 it->char_to_display = unibyte_char_to_multibyte (it->c);
19522 }
19523
19524 /* Get face and font to use. Encode IT->char_to_display. */
19525 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19526 face = FACE_FROM_ID (it->f, it->face_id);
19527 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19528 &char2b, it->multibyte_p, 0);
19529 font = face->font;
19530
19531 /* When no suitable font found, use the default font. */
19532 font_not_found_p = font == NULL;
19533 if (font_not_found_p)
19534 {
19535 font = FRAME_FONT (it->f);
19536 boff = FRAME_BASELINE_OFFSET (it->f);
19537 font_info = NULL;
19538 }
19539 else
19540 {
19541 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19542 boff = font_info->baseline_offset;
19543 if (font_info->vertical_centering)
19544 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19545 }
19546
19547 /* There are no padding glyphs, so there is only one glyph to
19548 produce for the composition. Important is that pixel_width,
19549 ascent and descent are the values of what is drawn by
19550 draw_glyphs (i.e. the values of the overall glyphs composed). */
19551 it->nglyphs = 1;
19552
19553 /* If we have not yet calculated pixel size data of glyphs of
19554 the composition for the current face font, calculate them
19555 now. Theoretically, we have to check all fonts for the
19556 glyphs, but that requires much time and memory space. So,
19557 here we check only the font of the first glyph. This leads
19558 to incorrect display very rarely, and C-l (recenter) can
19559 correct the display anyway. */
19560 if (cmp->font != (void *) font)
19561 {
19562 /* Ascent and descent of the font of the first character of
19563 this composition (adjusted by baseline offset). Ascent
19564 and descent of overall glyphs should not be less than
19565 them respectively. */
19566 int font_ascent = FONT_BASE (font) + boff;
19567 int font_descent = FONT_DESCENT (font) - boff;
19568 /* Bounding box of the overall glyphs. */
19569 int leftmost, rightmost, lowest, highest;
19570 int i, width, ascent, descent;
19571
19572 cmp->font = (void *) font;
19573
19574 /* Initialize the bounding box. */
19575 if (font_info
19576 && (pcm = rif->per_char_metric (font, &char2b,
19577 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19578 {
19579 width = pcm->width;
19580 ascent = pcm->ascent;
19581 descent = pcm->descent;
19582 }
19583 else
19584 {
19585 width = FONT_WIDTH (font);
19586 ascent = FONT_BASE (font);
19587 descent = FONT_DESCENT (font);
19588 }
19589
19590 rightmost = width;
19591 lowest = - descent + boff;
19592 highest = ascent + boff;
19593 leftmost = 0;
19594
19595 if (font_info
19596 && font_info->default_ascent
19597 && CHAR_TABLE_P (Vuse_default_ascent)
19598 && !NILP (Faref (Vuse_default_ascent,
19599 make_number (it->char_to_display))))
19600 highest = font_info->default_ascent + boff;
19601
19602 /* Draw the first glyph at the normal position. It may be
19603 shifted to right later if some other glyphs are drawn at
19604 the left. */
19605 cmp->offsets[0] = 0;
19606 cmp->offsets[1] = boff;
19607
19608 /* Set cmp->offsets for the remaining glyphs. */
19609 for (i = 1; i < cmp->glyph_len; i++)
19610 {
19611 int left, right, btm, top;
19612 int ch = COMPOSITION_GLYPH (cmp, i);
19613 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19614
19615 face = FACE_FROM_ID (it->f, face_id);
19616 get_char_face_and_encoding (it->f, ch, face->id,
19617 &char2b, it->multibyte_p, 0);
19618 font = face->font;
19619 if (font == NULL)
19620 {
19621 font = FRAME_FONT (it->f);
19622 boff = FRAME_BASELINE_OFFSET (it->f);
19623 font_info = NULL;
19624 }
19625 else
19626 {
19627 font_info
19628 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19629 boff = font_info->baseline_offset;
19630 if (font_info->vertical_centering)
19631 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19632 }
19633
19634 if (font_info
19635 && (pcm = rif->per_char_metric (font, &char2b,
19636 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19637 {
19638 width = pcm->width;
19639 ascent = pcm->ascent;
19640 descent = pcm->descent;
19641 }
19642 else
19643 {
19644 width = FONT_WIDTH (font);
19645 ascent = 1;
19646 descent = 0;
19647 }
19648
19649 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19650 {
19651 /* Relative composition with or without
19652 alternate chars. */
19653 left = (leftmost + rightmost - width) / 2;
19654 btm = - descent + boff;
19655 if (font_info && font_info->relative_compose
19656 && (! CHAR_TABLE_P (Vignore_relative_composition)
19657 || NILP (Faref (Vignore_relative_composition,
19658 make_number (ch)))))
19659 {
19660
19661 if (- descent >= font_info->relative_compose)
19662 /* One extra pixel between two glyphs. */
19663 btm = highest + 1;
19664 else if (ascent <= 0)
19665 /* One extra pixel between two glyphs. */
19666 btm = lowest - 1 - ascent - descent;
19667 }
19668 }
19669 else
19670 {
19671 /* A composition rule is specified by an integer
19672 value that encodes global and new reference
19673 points (GREF and NREF). GREF and NREF are
19674 specified by numbers as below:
19675
19676 0---1---2 -- ascent
19677 | |
19678 | |
19679 | |
19680 9--10--11 -- center
19681 | |
19682 ---3---4---5--- baseline
19683 | |
19684 6---7---8 -- descent
19685 */
19686 int rule = COMPOSITION_RULE (cmp, i);
19687 int gref, nref, grefx, grefy, nrefx, nrefy;
19688
19689 COMPOSITION_DECODE_RULE (rule, gref, nref);
19690 grefx = gref % 3, nrefx = nref % 3;
19691 grefy = gref / 3, nrefy = nref / 3;
19692
19693 left = (leftmost
19694 + grefx * (rightmost - leftmost) / 2
19695 - nrefx * width / 2);
19696 btm = ((grefy == 0 ? highest
19697 : grefy == 1 ? 0
19698 : grefy == 2 ? lowest
19699 : (highest + lowest) / 2)
19700 - (nrefy == 0 ? ascent + descent
19701 : nrefy == 1 ? descent - boff
19702 : nrefy == 2 ? 0
19703 : (ascent + descent) / 2));
19704 }
19705
19706 cmp->offsets[i * 2] = left;
19707 cmp->offsets[i * 2 + 1] = btm + descent;
19708
19709 /* Update the bounding box of the overall glyphs. */
19710 right = left + width;
19711 top = btm + descent + ascent;
19712 if (left < leftmost)
19713 leftmost = left;
19714 if (right > rightmost)
19715 rightmost = right;
19716 if (top > highest)
19717 highest = top;
19718 if (btm < lowest)
19719 lowest = btm;
19720 }
19721
19722 /* If there are glyphs whose x-offsets are negative,
19723 shift all glyphs to the right and make all x-offsets
19724 non-negative. */
19725 if (leftmost < 0)
19726 {
19727 for (i = 0; i < cmp->glyph_len; i++)
19728 cmp->offsets[i * 2] -= leftmost;
19729 rightmost -= leftmost;
19730 }
19731
19732 cmp->pixel_width = rightmost;
19733 cmp->ascent = highest;
19734 cmp->descent = - lowest;
19735 if (cmp->ascent < font_ascent)
19736 cmp->ascent = font_ascent;
19737 if (cmp->descent < font_descent)
19738 cmp->descent = font_descent;
19739 }
19740
19741 it->pixel_width = cmp->pixel_width;
19742 it->ascent = it->phys_ascent = cmp->ascent;
19743 it->descent = it->phys_descent = cmp->descent;
19744
19745 if (face->box != FACE_NO_BOX)
19746 {
19747 int thick = face->box_line_width;
19748
19749 if (thick > 0)
19750 {
19751 it->ascent += thick;
19752 it->descent += thick;
19753 }
19754 else
19755 thick = - thick;
19756
19757 if (it->start_of_box_run_p)
19758 it->pixel_width += thick;
19759 if (it->end_of_box_run_p)
19760 it->pixel_width += thick;
19761 }
19762
19763 /* If face has an overline, add the height of the overline
19764 (1 pixel) and a 1 pixel margin to the character height. */
19765 if (face->overline_p)
19766 it->ascent += 2;
19767
19768 take_vertical_position_into_account (it);
19769
19770 if (it->glyph_row)
19771 append_composite_glyph (it);
19772 }
19773 else if (it->what == IT_IMAGE)
19774 produce_image_glyph (it);
19775 else if (it->what == IT_STRETCH)
19776 produce_stretch_glyph (it);
19777
19778 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19779 because this isn't true for images with `:ascent 100'. */
19780 xassert (it->ascent >= 0 && it->descent >= 0);
19781 if (it->area == TEXT_AREA)
19782 it->current_x += it->pixel_width;
19783
19784 if (extra_line_spacing > 0)
19785 {
19786 it->descent += extra_line_spacing;
19787 if (extra_line_spacing > it->max_extra_line_spacing)
19788 it->max_extra_line_spacing = extra_line_spacing;
19789 }
19790
19791 it->max_ascent = max (it->max_ascent, it->ascent);
19792 it->max_descent = max (it->max_descent, it->descent);
19793 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
19794 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
19795 }
19796
19797 /* EXPORT for RIF:
19798 Output LEN glyphs starting at START at the nominal cursor position.
19799 Advance the nominal cursor over the text. The global variable
19800 updated_window contains the window being updated, updated_row is
19801 the glyph row being updated, and updated_area is the area of that
19802 row being updated. */
19803
19804 void
19805 x_write_glyphs (start, len)
19806 struct glyph *start;
19807 int len;
19808 {
19809 int x, hpos;
19810
19811 xassert (updated_window && updated_row);
19812 BLOCK_INPUT;
19813
19814 /* Write glyphs. */
19815
19816 hpos = start - updated_row->glyphs[updated_area];
19817 x = draw_glyphs (updated_window, output_cursor.x,
19818 updated_row, updated_area,
19819 hpos, hpos + len,
19820 DRAW_NORMAL_TEXT, 0);
19821
19822 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
19823 if (updated_area == TEXT_AREA
19824 && updated_window->phys_cursor_on_p
19825 && updated_window->phys_cursor.vpos == output_cursor.vpos
19826 && updated_window->phys_cursor.hpos >= hpos
19827 && updated_window->phys_cursor.hpos < hpos + len)
19828 updated_window->phys_cursor_on_p = 0;
19829
19830 UNBLOCK_INPUT;
19831
19832 /* Advance the output cursor. */
19833 output_cursor.hpos += len;
19834 output_cursor.x = x;
19835 }
19836
19837
19838 /* EXPORT for RIF:
19839 Insert LEN glyphs from START at the nominal cursor position. */
19840
19841 void
19842 x_insert_glyphs (start, len)
19843 struct glyph *start;
19844 int len;
19845 {
19846 struct frame *f;
19847 struct window *w;
19848 int line_height, shift_by_width, shifted_region_width;
19849 struct glyph_row *row;
19850 struct glyph *glyph;
19851 int frame_x, frame_y, hpos;
19852
19853 xassert (updated_window && updated_row);
19854 BLOCK_INPUT;
19855 w = updated_window;
19856 f = XFRAME (WINDOW_FRAME (w));
19857
19858 /* Get the height of the line we are in. */
19859 row = updated_row;
19860 line_height = row->height;
19861
19862 /* Get the width of the glyphs to insert. */
19863 shift_by_width = 0;
19864 for (glyph = start; glyph < start + len; ++glyph)
19865 shift_by_width += glyph->pixel_width;
19866
19867 /* Get the width of the region to shift right. */
19868 shifted_region_width = (window_box_width (w, updated_area)
19869 - output_cursor.x
19870 - shift_by_width);
19871
19872 /* Shift right. */
19873 frame_x = window_box_left (w, updated_area) + output_cursor.x;
19874 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
19875
19876 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
19877 line_height, shift_by_width);
19878
19879 /* Write the glyphs. */
19880 hpos = start - row->glyphs[updated_area];
19881 draw_glyphs (w, output_cursor.x, row, updated_area,
19882 hpos, hpos + len,
19883 DRAW_NORMAL_TEXT, 0);
19884
19885 /* Advance the output cursor. */
19886 output_cursor.hpos += len;
19887 output_cursor.x += shift_by_width;
19888 UNBLOCK_INPUT;
19889 }
19890
19891
19892 /* EXPORT for RIF:
19893 Erase the current text line from the nominal cursor position
19894 (inclusive) to pixel column TO_X (exclusive). The idea is that
19895 everything from TO_X onward is already erased.
19896
19897 TO_X is a pixel position relative to updated_area of
19898 updated_window. TO_X == -1 means clear to the end of this area. */
19899
19900 void
19901 x_clear_end_of_line (to_x)
19902 int to_x;
19903 {
19904 struct frame *f;
19905 struct window *w = updated_window;
19906 int max_x, min_y, max_y;
19907 int from_x, from_y, to_y;
19908
19909 xassert (updated_window && updated_row);
19910 f = XFRAME (w->frame);
19911
19912 if (updated_row->full_width_p)
19913 max_x = WINDOW_TOTAL_WIDTH (w);
19914 else
19915 max_x = window_box_width (w, updated_area);
19916 max_y = window_text_bottom_y (w);
19917
19918 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
19919 of window. For TO_X > 0, truncate to end of drawing area. */
19920 if (to_x == 0)
19921 return;
19922 else if (to_x < 0)
19923 to_x = max_x;
19924 else
19925 to_x = min (to_x, max_x);
19926
19927 to_y = min (max_y, output_cursor.y + updated_row->height);
19928
19929 /* Notice if the cursor will be cleared by this operation. */
19930 if (!updated_row->full_width_p)
19931 notice_overwritten_cursor (w, updated_area,
19932 output_cursor.x, -1,
19933 updated_row->y,
19934 MATRIX_ROW_BOTTOM_Y (updated_row));
19935
19936 from_x = output_cursor.x;
19937
19938 /* Translate to frame coordinates. */
19939 if (updated_row->full_width_p)
19940 {
19941 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
19942 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
19943 }
19944 else
19945 {
19946 int area_left = window_box_left (w, updated_area);
19947 from_x += area_left;
19948 to_x += area_left;
19949 }
19950
19951 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
19952 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
19953 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
19954
19955 /* Prevent inadvertently clearing to end of the X window. */
19956 if (to_x > from_x && to_y > from_y)
19957 {
19958 BLOCK_INPUT;
19959 rif->clear_frame_area (f, from_x, from_y,
19960 to_x - from_x, to_y - from_y);
19961 UNBLOCK_INPUT;
19962 }
19963 }
19964
19965 #endif /* HAVE_WINDOW_SYSTEM */
19966
19967
19968 \f
19969 /***********************************************************************
19970 Cursor types
19971 ***********************************************************************/
19972
19973 /* Value is the internal representation of the specified cursor type
19974 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
19975 of the bar cursor. */
19976
19977 static enum text_cursor_kinds
19978 get_specified_cursor_type (arg, width)
19979 Lisp_Object arg;
19980 int *width;
19981 {
19982 enum text_cursor_kinds type;
19983
19984 if (NILP (arg))
19985 return NO_CURSOR;
19986
19987 if (EQ (arg, Qbox))
19988 return FILLED_BOX_CURSOR;
19989
19990 if (EQ (arg, Qhollow))
19991 return HOLLOW_BOX_CURSOR;
19992
19993 if (EQ (arg, Qbar))
19994 {
19995 *width = 2;
19996 return BAR_CURSOR;
19997 }
19998
19999 if (CONSP (arg)
20000 && EQ (XCAR (arg), Qbar)
20001 && INTEGERP (XCDR (arg))
20002 && XINT (XCDR (arg)) >= 0)
20003 {
20004 *width = XINT (XCDR (arg));
20005 return BAR_CURSOR;
20006 }
20007
20008 if (EQ (arg, Qhbar))
20009 {
20010 *width = 2;
20011 return HBAR_CURSOR;
20012 }
20013
20014 if (CONSP (arg)
20015 && EQ (XCAR (arg), Qhbar)
20016 && INTEGERP (XCDR (arg))
20017 && XINT (XCDR (arg)) >= 0)
20018 {
20019 *width = XINT (XCDR (arg));
20020 return HBAR_CURSOR;
20021 }
20022
20023 /* Treat anything unknown as "hollow box cursor".
20024 It was bad to signal an error; people have trouble fixing
20025 .Xdefaults with Emacs, when it has something bad in it. */
20026 type = HOLLOW_BOX_CURSOR;
20027
20028 return type;
20029 }
20030
20031 /* Set the default cursor types for specified frame. */
20032 void
20033 set_frame_cursor_types (f, arg)
20034 struct frame *f;
20035 Lisp_Object arg;
20036 {
20037 int width;
20038 Lisp_Object tem;
20039
20040 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20041 FRAME_CURSOR_WIDTH (f) = width;
20042
20043 /* By default, set up the blink-off state depending on the on-state. */
20044
20045 tem = Fassoc (arg, Vblink_cursor_alist);
20046 if (!NILP (tem))
20047 {
20048 FRAME_BLINK_OFF_CURSOR (f)
20049 = get_specified_cursor_type (XCDR (tem), &width);
20050 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20051 }
20052 else
20053 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20054 }
20055
20056
20057 /* Return the cursor we want to be displayed in window W. Return
20058 width of bar/hbar cursor through WIDTH arg. Return with
20059 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20060 (i.e. if the `system caret' should track this cursor).
20061
20062 In a mini-buffer window, we want the cursor only to appear if we
20063 are reading input from this window. For the selected window, we
20064 want the cursor type given by the frame parameter or buffer local
20065 setting of cursor-type. If explicitly marked off, draw no cursor.
20066 In all other cases, we want a hollow box cursor. */
20067
20068 static enum text_cursor_kinds
20069 get_window_cursor_type (w, glyph, width, active_cursor)
20070 struct window *w;
20071 struct glyph *glyph;
20072 int *width;
20073 int *active_cursor;
20074 {
20075 struct frame *f = XFRAME (w->frame);
20076 struct buffer *b = XBUFFER (w->buffer);
20077 int cursor_type = DEFAULT_CURSOR;
20078 Lisp_Object alt_cursor;
20079 int non_selected = 0;
20080
20081 *active_cursor = 1;
20082
20083 /* Echo area */
20084 if (cursor_in_echo_area
20085 && FRAME_HAS_MINIBUF_P (f)
20086 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20087 {
20088 if (w == XWINDOW (echo_area_window))
20089 {
20090 *width = FRAME_CURSOR_WIDTH (f);
20091 return FRAME_DESIRED_CURSOR (f);
20092 }
20093
20094 *active_cursor = 0;
20095 non_selected = 1;
20096 }
20097
20098 /* Nonselected window or nonselected frame. */
20099 else if (w != XWINDOW (f->selected_window)
20100 #ifdef HAVE_WINDOW_SYSTEM
20101 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20102 #endif
20103 )
20104 {
20105 *active_cursor = 0;
20106
20107 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20108 return NO_CURSOR;
20109
20110 non_selected = 1;
20111 }
20112
20113 /* Never display a cursor in a window in which cursor-type is nil. */
20114 if (NILP (b->cursor_type))
20115 return NO_CURSOR;
20116
20117 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20118 if (non_selected)
20119 {
20120 alt_cursor = Fbuffer_local_value (Qcursor_in_non_selected_windows, w->buffer);
20121 return get_specified_cursor_type (alt_cursor, width);
20122 }
20123
20124 /* Get the normal cursor type for this window. */
20125 if (EQ (b->cursor_type, Qt))
20126 {
20127 cursor_type = FRAME_DESIRED_CURSOR (f);
20128 *width = FRAME_CURSOR_WIDTH (f);
20129 }
20130 else
20131 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20132
20133 /* Use normal cursor if not blinked off. */
20134 if (!w->cursor_off_p)
20135 {
20136 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20137 if (cursor_type == FILLED_BOX_CURSOR)
20138 cursor_type = HOLLOW_BOX_CURSOR;
20139 }
20140 return cursor_type;
20141 }
20142
20143 /* Cursor is blinked off, so determine how to "toggle" it. */
20144
20145 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20146 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20147 return get_specified_cursor_type (XCDR (alt_cursor), width);
20148
20149 /* Then see if frame has specified a specific blink off cursor type. */
20150 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20151 {
20152 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20153 return FRAME_BLINK_OFF_CURSOR (f);
20154 }
20155
20156 #if 0
20157 /* Some people liked having a permanently visible blinking cursor,
20158 while others had very strong opinions against it. So it was
20159 decided to remove it. KFS 2003-09-03 */
20160
20161 /* Finally perform built-in cursor blinking:
20162 filled box <-> hollow box
20163 wide [h]bar <-> narrow [h]bar
20164 narrow [h]bar <-> no cursor
20165 other type <-> no cursor */
20166
20167 if (cursor_type == FILLED_BOX_CURSOR)
20168 return HOLLOW_BOX_CURSOR;
20169
20170 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20171 {
20172 *width = 1;
20173 return cursor_type;
20174 }
20175 #endif
20176
20177 return NO_CURSOR;
20178 }
20179
20180
20181 #ifdef HAVE_WINDOW_SYSTEM
20182
20183 /* Notice when the text cursor of window W has been completely
20184 overwritten by a drawing operation that outputs glyphs in AREA
20185 starting at X0 and ending at X1 in the line starting at Y0 and
20186 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20187 the rest of the line after X0 has been written. Y coordinates
20188 are window-relative. */
20189
20190 static void
20191 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20192 struct window *w;
20193 enum glyph_row_area area;
20194 int x0, y0, x1, y1;
20195 {
20196 int cx0, cx1, cy0, cy1;
20197 struct glyph_row *row;
20198
20199 if (!w->phys_cursor_on_p)
20200 return;
20201 if (area != TEXT_AREA)
20202 return;
20203
20204 row = w->current_matrix->rows + w->phys_cursor.vpos;
20205 if (!row->displays_text_p)
20206 return;
20207
20208 if (row->cursor_in_fringe_p)
20209 {
20210 row->cursor_in_fringe_p = 0;
20211 draw_fringe_bitmap (w, row, 0);
20212 w->phys_cursor_on_p = 0;
20213 return;
20214 }
20215
20216 cx0 = w->phys_cursor.x;
20217 cx1 = cx0 + w->phys_cursor_width;
20218 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20219 return;
20220
20221 /* The cursor image will be completely removed from the
20222 screen if the output area intersects the cursor area in
20223 y-direction. When we draw in [y0 y1[, and some part of
20224 the cursor is at y < y0, that part must have been drawn
20225 before. When scrolling, the cursor is erased before
20226 actually scrolling, so we don't come here. When not
20227 scrolling, the rows above the old cursor row must have
20228 changed, and in this case these rows must have written
20229 over the cursor image.
20230
20231 Likewise if part of the cursor is below y1, with the
20232 exception of the cursor being in the first blank row at
20233 the buffer and window end because update_text_area
20234 doesn't draw that row. (Except when it does, but
20235 that's handled in update_text_area.) */
20236
20237 cy0 = w->phys_cursor.y;
20238 cy1 = cy0 + w->phys_cursor_height;
20239 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20240 return;
20241
20242 w->phys_cursor_on_p = 0;
20243 }
20244
20245 #endif /* HAVE_WINDOW_SYSTEM */
20246
20247 \f
20248 /************************************************************************
20249 Mouse Face
20250 ************************************************************************/
20251
20252 #ifdef HAVE_WINDOW_SYSTEM
20253
20254 /* EXPORT for RIF:
20255 Fix the display of area AREA of overlapping row ROW in window W. */
20256
20257 void
20258 x_fix_overlapping_area (w, row, area)
20259 struct window *w;
20260 struct glyph_row *row;
20261 enum glyph_row_area area;
20262 {
20263 int i, x;
20264
20265 BLOCK_INPUT;
20266
20267 x = 0;
20268 for (i = 0; i < row->used[area];)
20269 {
20270 if (row->glyphs[area][i].overlaps_vertically_p)
20271 {
20272 int start = i, start_x = x;
20273
20274 do
20275 {
20276 x += row->glyphs[area][i].pixel_width;
20277 ++i;
20278 }
20279 while (i < row->used[area]
20280 && row->glyphs[area][i].overlaps_vertically_p);
20281
20282 draw_glyphs (w, start_x, row, area,
20283 start, i,
20284 DRAW_NORMAL_TEXT, 1);
20285 }
20286 else
20287 {
20288 x += row->glyphs[area][i].pixel_width;
20289 ++i;
20290 }
20291 }
20292
20293 UNBLOCK_INPUT;
20294 }
20295
20296
20297 /* EXPORT:
20298 Draw the cursor glyph of window W in glyph row ROW. See the
20299 comment of draw_glyphs for the meaning of HL. */
20300
20301 void
20302 draw_phys_cursor_glyph (w, row, hl)
20303 struct window *w;
20304 struct glyph_row *row;
20305 enum draw_glyphs_face hl;
20306 {
20307 /* If cursor hpos is out of bounds, don't draw garbage. This can
20308 happen in mini-buffer windows when switching between echo area
20309 glyphs and mini-buffer. */
20310 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20311 {
20312 int on_p = w->phys_cursor_on_p;
20313 int x1;
20314 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20315 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20316 hl, 0);
20317 w->phys_cursor_on_p = on_p;
20318
20319 if (hl == DRAW_CURSOR)
20320 w->phys_cursor_width = x1 - w->phys_cursor.x;
20321 /* When we erase the cursor, and ROW is overlapped by other
20322 rows, make sure that these overlapping parts of other rows
20323 are redrawn. */
20324 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20325 {
20326 if (row > w->current_matrix->rows
20327 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20328 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20329
20330 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20331 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20332 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20333 }
20334 }
20335 }
20336
20337
20338 /* EXPORT:
20339 Erase the image of a cursor of window W from the screen. */
20340
20341 void
20342 erase_phys_cursor (w)
20343 struct window *w;
20344 {
20345 struct frame *f = XFRAME (w->frame);
20346 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20347 int hpos = w->phys_cursor.hpos;
20348 int vpos = w->phys_cursor.vpos;
20349 int mouse_face_here_p = 0;
20350 struct glyph_matrix *active_glyphs = w->current_matrix;
20351 struct glyph_row *cursor_row;
20352 struct glyph *cursor_glyph;
20353 enum draw_glyphs_face hl;
20354
20355 /* No cursor displayed or row invalidated => nothing to do on the
20356 screen. */
20357 if (w->phys_cursor_type == NO_CURSOR)
20358 goto mark_cursor_off;
20359
20360 /* VPOS >= active_glyphs->nrows means that window has been resized.
20361 Don't bother to erase the cursor. */
20362 if (vpos >= active_glyphs->nrows)
20363 goto mark_cursor_off;
20364
20365 /* If row containing cursor is marked invalid, there is nothing we
20366 can do. */
20367 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20368 if (!cursor_row->enabled_p)
20369 goto mark_cursor_off;
20370
20371 /* If line spacing is > 0, old cursor may only be partially visible in
20372 window after split-window. So adjust visible height. */
20373 cursor_row->visible_height = min (cursor_row->visible_height,
20374 window_text_bottom_y (w) - cursor_row->y);
20375
20376 /* If row is completely invisible, don't attempt to delete a cursor which
20377 isn't there. This can happen if cursor is at top of a window, and
20378 we switch to a buffer with a header line in that window. */
20379 if (cursor_row->visible_height <= 0)
20380 goto mark_cursor_off;
20381
20382 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20383 if (cursor_row->cursor_in_fringe_p)
20384 {
20385 cursor_row->cursor_in_fringe_p = 0;
20386 draw_fringe_bitmap (w, cursor_row, 0);
20387 goto mark_cursor_off;
20388 }
20389
20390 /* This can happen when the new row is shorter than the old one.
20391 In this case, either draw_glyphs or clear_end_of_line
20392 should have cleared the cursor. Note that we wouldn't be
20393 able to erase the cursor in this case because we don't have a
20394 cursor glyph at hand. */
20395 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20396 goto mark_cursor_off;
20397
20398 /* If the cursor is in the mouse face area, redisplay that when
20399 we clear the cursor. */
20400 if (! NILP (dpyinfo->mouse_face_window)
20401 && w == XWINDOW (dpyinfo->mouse_face_window)
20402 && (vpos > dpyinfo->mouse_face_beg_row
20403 || (vpos == dpyinfo->mouse_face_beg_row
20404 && hpos >= dpyinfo->mouse_face_beg_col))
20405 && (vpos < dpyinfo->mouse_face_end_row
20406 || (vpos == dpyinfo->mouse_face_end_row
20407 && hpos < dpyinfo->mouse_face_end_col))
20408 /* Don't redraw the cursor's spot in mouse face if it is at the
20409 end of a line (on a newline). The cursor appears there, but
20410 mouse highlighting does not. */
20411 && cursor_row->used[TEXT_AREA] > hpos)
20412 mouse_face_here_p = 1;
20413
20414 /* Maybe clear the display under the cursor. */
20415 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20416 {
20417 int x, y;
20418 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20419 int width;
20420
20421 cursor_glyph = get_phys_cursor_glyph (w);
20422 if (cursor_glyph == NULL)
20423 goto mark_cursor_off;
20424
20425 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20426 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20427 width = min (cursor_glyph->pixel_width,
20428 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20429
20430 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20431 }
20432
20433 /* Erase the cursor by redrawing the character underneath it. */
20434 if (mouse_face_here_p)
20435 hl = DRAW_MOUSE_FACE;
20436 else
20437 hl = DRAW_NORMAL_TEXT;
20438 draw_phys_cursor_glyph (w, cursor_row, hl);
20439
20440 mark_cursor_off:
20441 w->phys_cursor_on_p = 0;
20442 w->phys_cursor_type = NO_CURSOR;
20443 }
20444
20445
20446 /* EXPORT:
20447 Display or clear cursor of window W. If ON is zero, clear the
20448 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20449 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20450
20451 void
20452 display_and_set_cursor (w, on, hpos, vpos, x, y)
20453 struct window *w;
20454 int on, hpos, vpos, x, y;
20455 {
20456 struct frame *f = XFRAME (w->frame);
20457 int new_cursor_type;
20458 int new_cursor_width;
20459 int active_cursor;
20460 struct glyph_row *glyph_row;
20461 struct glyph *glyph;
20462
20463 /* This is pointless on invisible frames, and dangerous on garbaged
20464 windows and frames; in the latter case, the frame or window may
20465 be in the midst of changing its size, and x and y may be off the
20466 window. */
20467 if (! FRAME_VISIBLE_P (f)
20468 || FRAME_GARBAGED_P (f)
20469 || vpos >= w->current_matrix->nrows
20470 || hpos >= w->current_matrix->matrix_w)
20471 return;
20472
20473 /* If cursor is off and we want it off, return quickly. */
20474 if (!on && !w->phys_cursor_on_p)
20475 return;
20476
20477 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20478 /* If cursor row is not enabled, we don't really know where to
20479 display the cursor. */
20480 if (!glyph_row->enabled_p)
20481 {
20482 w->phys_cursor_on_p = 0;
20483 return;
20484 }
20485
20486 glyph = NULL;
20487 if (!glyph_row->exact_window_width_line_p
20488 || hpos < glyph_row->used[TEXT_AREA])
20489 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20490
20491 xassert (interrupt_input_blocked);
20492
20493 /* Set new_cursor_type to the cursor we want to be displayed. */
20494 new_cursor_type = get_window_cursor_type (w, glyph,
20495 &new_cursor_width, &active_cursor);
20496
20497 /* If cursor is currently being shown and we don't want it to be or
20498 it is in the wrong place, or the cursor type is not what we want,
20499 erase it. */
20500 if (w->phys_cursor_on_p
20501 && (!on
20502 || w->phys_cursor.x != x
20503 || w->phys_cursor.y != y
20504 || new_cursor_type != w->phys_cursor_type
20505 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20506 && new_cursor_width != w->phys_cursor_width)))
20507 erase_phys_cursor (w);
20508
20509 /* Don't check phys_cursor_on_p here because that flag is only set
20510 to zero in some cases where we know that the cursor has been
20511 completely erased, to avoid the extra work of erasing the cursor
20512 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20513 still not be visible, or it has only been partly erased. */
20514 if (on)
20515 {
20516 w->phys_cursor_ascent = glyph_row->ascent;
20517 w->phys_cursor_height = glyph_row->height;
20518
20519 /* Set phys_cursor_.* before x_draw_.* is called because some
20520 of them may need the information. */
20521 w->phys_cursor.x = x;
20522 w->phys_cursor.y = glyph_row->y;
20523 w->phys_cursor.hpos = hpos;
20524 w->phys_cursor.vpos = vpos;
20525 }
20526
20527 rif->draw_window_cursor (w, glyph_row, x, y,
20528 new_cursor_type, new_cursor_width,
20529 on, active_cursor);
20530 }
20531
20532
20533 /* Switch the display of W's cursor on or off, according to the value
20534 of ON. */
20535
20536 static void
20537 update_window_cursor (w, on)
20538 struct window *w;
20539 int on;
20540 {
20541 /* Don't update cursor in windows whose frame is in the process
20542 of being deleted. */
20543 if (w->current_matrix)
20544 {
20545 BLOCK_INPUT;
20546 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20547 w->phys_cursor.x, w->phys_cursor.y);
20548 UNBLOCK_INPUT;
20549 }
20550 }
20551
20552
20553 /* Call update_window_cursor with parameter ON_P on all leaf windows
20554 in the window tree rooted at W. */
20555
20556 static void
20557 update_cursor_in_window_tree (w, on_p)
20558 struct window *w;
20559 int on_p;
20560 {
20561 while (w)
20562 {
20563 if (!NILP (w->hchild))
20564 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20565 else if (!NILP (w->vchild))
20566 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20567 else
20568 update_window_cursor (w, on_p);
20569
20570 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20571 }
20572 }
20573
20574
20575 /* EXPORT:
20576 Display the cursor on window W, or clear it, according to ON_P.
20577 Don't change the cursor's position. */
20578
20579 void
20580 x_update_cursor (f, on_p)
20581 struct frame *f;
20582 int on_p;
20583 {
20584 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20585 }
20586
20587
20588 /* EXPORT:
20589 Clear the cursor of window W to background color, and mark the
20590 cursor as not shown. This is used when the text where the cursor
20591 is is about to be rewritten. */
20592
20593 void
20594 x_clear_cursor (w)
20595 struct window *w;
20596 {
20597 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20598 update_window_cursor (w, 0);
20599 }
20600
20601
20602 /* EXPORT:
20603 Display the active region described by mouse_face_* according to DRAW. */
20604
20605 void
20606 show_mouse_face (dpyinfo, draw)
20607 Display_Info *dpyinfo;
20608 enum draw_glyphs_face draw;
20609 {
20610 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20611 struct frame *f = XFRAME (WINDOW_FRAME (w));
20612
20613 if (/* If window is in the process of being destroyed, don't bother
20614 to do anything. */
20615 w->current_matrix != NULL
20616 /* Don't update mouse highlight if hidden */
20617 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20618 /* Recognize when we are called to operate on rows that don't exist
20619 anymore. This can happen when a window is split. */
20620 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20621 {
20622 int phys_cursor_on_p = w->phys_cursor_on_p;
20623 struct glyph_row *row, *first, *last;
20624
20625 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20626 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20627
20628 for (row = first; row <= last && row->enabled_p; ++row)
20629 {
20630 int start_hpos, end_hpos, start_x;
20631
20632 /* For all but the first row, the highlight starts at column 0. */
20633 if (row == first)
20634 {
20635 start_hpos = dpyinfo->mouse_face_beg_col;
20636 start_x = dpyinfo->mouse_face_beg_x;
20637 }
20638 else
20639 {
20640 start_hpos = 0;
20641 start_x = 0;
20642 }
20643
20644 if (row == last)
20645 end_hpos = dpyinfo->mouse_face_end_col;
20646 else
20647 end_hpos = row->used[TEXT_AREA];
20648
20649 if (end_hpos > start_hpos)
20650 {
20651 draw_glyphs (w, start_x, row, TEXT_AREA,
20652 start_hpos, end_hpos,
20653 draw, 0);
20654
20655 row->mouse_face_p
20656 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20657 }
20658 }
20659
20660 /* When we've written over the cursor, arrange for it to
20661 be displayed again. */
20662 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20663 {
20664 BLOCK_INPUT;
20665 display_and_set_cursor (w, 1,
20666 w->phys_cursor.hpos, w->phys_cursor.vpos,
20667 w->phys_cursor.x, w->phys_cursor.y);
20668 UNBLOCK_INPUT;
20669 }
20670 }
20671
20672 /* Change the mouse cursor. */
20673 if (draw == DRAW_NORMAL_TEXT)
20674 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20675 else if (draw == DRAW_MOUSE_FACE)
20676 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20677 else
20678 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20679 }
20680
20681 /* EXPORT:
20682 Clear out the mouse-highlighted active region.
20683 Redraw it un-highlighted first. Value is non-zero if mouse
20684 face was actually drawn unhighlighted. */
20685
20686 int
20687 clear_mouse_face (dpyinfo)
20688 Display_Info *dpyinfo;
20689 {
20690 int cleared = 0;
20691
20692 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20693 {
20694 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20695 cleared = 1;
20696 }
20697
20698 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20699 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20700 dpyinfo->mouse_face_window = Qnil;
20701 dpyinfo->mouse_face_overlay = Qnil;
20702 return cleared;
20703 }
20704
20705
20706 /* EXPORT:
20707 Non-zero if physical cursor of window W is within mouse face. */
20708
20709 int
20710 cursor_in_mouse_face_p (w)
20711 struct window *w;
20712 {
20713 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20714 int in_mouse_face = 0;
20715
20716 if (WINDOWP (dpyinfo->mouse_face_window)
20717 && XWINDOW (dpyinfo->mouse_face_window) == w)
20718 {
20719 int hpos = w->phys_cursor.hpos;
20720 int vpos = w->phys_cursor.vpos;
20721
20722 if (vpos >= dpyinfo->mouse_face_beg_row
20723 && vpos <= dpyinfo->mouse_face_end_row
20724 && (vpos > dpyinfo->mouse_face_beg_row
20725 || hpos >= dpyinfo->mouse_face_beg_col)
20726 && (vpos < dpyinfo->mouse_face_end_row
20727 || hpos < dpyinfo->mouse_face_end_col
20728 || dpyinfo->mouse_face_past_end))
20729 in_mouse_face = 1;
20730 }
20731
20732 return in_mouse_face;
20733 }
20734
20735
20736
20737 \f
20738 /* Find the glyph matrix position of buffer position CHARPOS in window
20739 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20740 current glyphs must be up to date. If CHARPOS is above window
20741 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20742 of last line in W. In the row containing CHARPOS, stop before glyphs
20743 having STOP as object. */
20744
20745 #if 1 /* This is a version of fast_find_position that's more correct
20746 in the presence of hscrolling, for example. I didn't install
20747 it right away because the problem fixed is minor, it failed
20748 in 20.x as well, and I think it's too risky to install
20749 so near the release of 21.1. 2001-09-25 gerd. */
20750
20751 static int
20752 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20753 struct window *w;
20754 int charpos;
20755 int *hpos, *vpos, *x, *y;
20756 Lisp_Object stop;
20757 {
20758 struct glyph_row *row, *first;
20759 struct glyph *glyph, *end;
20760 int past_end = 0;
20761
20762 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20763 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20764 {
20765 *x = first->x;
20766 *y = first->y;
20767 *hpos = 0;
20768 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20769 return 1;
20770 }
20771
20772 row = row_containing_pos (w, charpos, first, NULL, 0);
20773 if (row == NULL)
20774 {
20775 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20776 past_end = 1;
20777 }
20778
20779 /* If whole rows or last part of a row came from a display overlay,
20780 row_containing_pos will skip over such rows because their end pos
20781 equals the start pos of the overlay or interval.
20782
20783 Move back if we have a STOP object and previous row's
20784 end glyph came from STOP. */
20785 if (!NILP (stop))
20786 {
20787 struct glyph_row *prev;
20788 while ((prev = row - 1, prev >= first)
20789 && MATRIX_ROW_END_CHARPOS (prev) == charpos
20790 && prev->used[TEXT_AREA] > 0)
20791 {
20792 struct glyph *beg = prev->glyphs[TEXT_AREA];
20793 glyph = beg + prev->used[TEXT_AREA];
20794 while (--glyph >= beg
20795 && INTEGERP (glyph->object));
20796 if (glyph < beg
20797 || !EQ (stop, glyph->object))
20798 break;
20799 row = prev;
20800 }
20801 }
20802
20803 *x = row->x;
20804 *y = row->y;
20805 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20806
20807 glyph = row->glyphs[TEXT_AREA];
20808 end = glyph + row->used[TEXT_AREA];
20809
20810 /* Skip over glyphs not having an object at the start of the row.
20811 These are special glyphs like truncation marks on terminal
20812 frames. */
20813 if (row->displays_text_p)
20814 while (glyph < end
20815 && INTEGERP (glyph->object)
20816 && !EQ (stop, glyph->object)
20817 && glyph->charpos < 0)
20818 {
20819 *x += glyph->pixel_width;
20820 ++glyph;
20821 }
20822
20823 while (glyph < end
20824 && !INTEGERP (glyph->object)
20825 && !EQ (stop, glyph->object)
20826 && (!BUFFERP (glyph->object)
20827 || glyph->charpos < charpos))
20828 {
20829 *x += glyph->pixel_width;
20830 ++glyph;
20831 }
20832
20833 *hpos = glyph - row->glyphs[TEXT_AREA];
20834 return !past_end;
20835 }
20836
20837 #else /* not 1 */
20838
20839 static int
20840 fast_find_position (w, pos, hpos, vpos, x, y, stop)
20841 struct window *w;
20842 int pos;
20843 int *hpos, *vpos, *x, *y;
20844 Lisp_Object stop;
20845 {
20846 int i;
20847 int lastcol;
20848 int maybe_next_line_p = 0;
20849 int line_start_position;
20850 int yb = window_text_bottom_y (w);
20851 struct glyph_row *row, *best_row;
20852 int row_vpos, best_row_vpos;
20853 int current_x;
20854
20855 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20856 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
20857
20858 while (row->y < yb)
20859 {
20860 if (row->used[TEXT_AREA])
20861 line_start_position = row->glyphs[TEXT_AREA]->charpos;
20862 else
20863 line_start_position = 0;
20864
20865 if (line_start_position > pos)
20866 break;
20867 /* If the position sought is the end of the buffer,
20868 don't include the blank lines at the bottom of the window. */
20869 else if (line_start_position == pos
20870 && pos == BUF_ZV (XBUFFER (w->buffer)))
20871 {
20872 maybe_next_line_p = 1;
20873 break;
20874 }
20875 else if (line_start_position > 0)
20876 {
20877 best_row = row;
20878 best_row_vpos = row_vpos;
20879 }
20880
20881 if (row->y + row->height >= yb)
20882 break;
20883
20884 ++row;
20885 ++row_vpos;
20886 }
20887
20888 /* Find the right column within BEST_ROW. */
20889 lastcol = 0;
20890 current_x = best_row->x;
20891 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
20892 {
20893 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
20894 int charpos = glyph->charpos;
20895
20896 if (BUFFERP (glyph->object))
20897 {
20898 if (charpos == pos)
20899 {
20900 *hpos = i;
20901 *vpos = best_row_vpos;
20902 *x = current_x;
20903 *y = best_row->y;
20904 return 1;
20905 }
20906 else if (charpos > pos)
20907 break;
20908 }
20909 else if (EQ (glyph->object, stop))
20910 break;
20911
20912 if (charpos > 0)
20913 lastcol = i;
20914 current_x += glyph->pixel_width;
20915 }
20916
20917 /* If we're looking for the end of the buffer,
20918 and we didn't find it in the line we scanned,
20919 use the start of the following line. */
20920 if (maybe_next_line_p)
20921 {
20922 ++best_row;
20923 ++best_row_vpos;
20924 lastcol = 0;
20925 current_x = best_row->x;
20926 }
20927
20928 *vpos = best_row_vpos;
20929 *hpos = lastcol + 1;
20930 *x = current_x;
20931 *y = best_row->y;
20932 return 0;
20933 }
20934
20935 #endif /* not 1 */
20936
20937
20938 /* Find the position of the glyph for position POS in OBJECT in
20939 window W's current matrix, and return in *X, *Y the pixel
20940 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
20941
20942 RIGHT_P non-zero means return the position of the right edge of the
20943 glyph, RIGHT_P zero means return the left edge position.
20944
20945 If no glyph for POS exists in the matrix, return the position of
20946 the glyph with the next smaller position that is in the matrix, if
20947 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
20948 exists in the matrix, return the position of the glyph with the
20949 next larger position in OBJECT.
20950
20951 Value is non-zero if a glyph was found. */
20952
20953 static int
20954 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
20955 struct window *w;
20956 int pos;
20957 Lisp_Object object;
20958 int *hpos, *vpos, *x, *y;
20959 int right_p;
20960 {
20961 int yb = window_text_bottom_y (w);
20962 struct glyph_row *r;
20963 struct glyph *best_glyph = NULL;
20964 struct glyph_row *best_row = NULL;
20965 int best_x = 0;
20966
20967 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20968 r->enabled_p && r->y < yb;
20969 ++r)
20970 {
20971 struct glyph *g = r->glyphs[TEXT_AREA];
20972 struct glyph *e = g + r->used[TEXT_AREA];
20973 int gx;
20974
20975 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
20976 if (EQ (g->object, object))
20977 {
20978 if (g->charpos == pos)
20979 {
20980 best_glyph = g;
20981 best_x = gx;
20982 best_row = r;
20983 goto found;
20984 }
20985 else if (best_glyph == NULL
20986 || ((abs (g->charpos - pos)
20987 < abs (best_glyph->charpos - pos))
20988 && (right_p
20989 ? g->charpos < pos
20990 : g->charpos > pos)))
20991 {
20992 best_glyph = g;
20993 best_x = gx;
20994 best_row = r;
20995 }
20996 }
20997 }
20998
20999 found:
21000
21001 if (best_glyph)
21002 {
21003 *x = best_x;
21004 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21005
21006 if (right_p)
21007 {
21008 *x += best_glyph->pixel_width;
21009 ++*hpos;
21010 }
21011
21012 *y = best_row->y;
21013 *vpos = best_row - w->current_matrix->rows;
21014 }
21015
21016 return best_glyph != NULL;
21017 }
21018
21019
21020 /* See if position X, Y is within a hot-spot of an image. */
21021
21022 static int
21023 on_hot_spot_p (hot_spot, x, y)
21024 Lisp_Object hot_spot;
21025 int x, y;
21026 {
21027 if (!CONSP (hot_spot))
21028 return 0;
21029
21030 if (EQ (XCAR (hot_spot), Qrect))
21031 {
21032 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21033 Lisp_Object rect = XCDR (hot_spot);
21034 Lisp_Object tem;
21035 if (!CONSP (rect))
21036 return 0;
21037 if (!CONSP (XCAR (rect)))
21038 return 0;
21039 if (!CONSP (XCDR (rect)))
21040 return 0;
21041 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21042 return 0;
21043 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21044 return 0;
21045 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21046 return 0;
21047 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21048 return 0;
21049 return 1;
21050 }
21051 else if (EQ (XCAR (hot_spot), Qcircle))
21052 {
21053 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21054 Lisp_Object circ = XCDR (hot_spot);
21055 Lisp_Object lr, lx0, ly0;
21056 if (CONSP (circ)
21057 && CONSP (XCAR (circ))
21058 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21059 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21060 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21061 {
21062 double r = XFLOATINT (lr);
21063 double dx = XINT (lx0) - x;
21064 double dy = XINT (ly0) - y;
21065 return (dx * dx + dy * dy <= r * r);
21066 }
21067 }
21068 else if (EQ (XCAR (hot_spot), Qpoly))
21069 {
21070 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21071 if (VECTORP (XCDR (hot_spot)))
21072 {
21073 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21074 Lisp_Object *poly = v->contents;
21075 int n = v->size;
21076 int i;
21077 int inside = 0;
21078 Lisp_Object lx, ly;
21079 int x0, y0;
21080
21081 /* Need an even number of coordinates, and at least 3 edges. */
21082 if (n < 6 || n & 1)
21083 return 0;
21084
21085 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21086 If count is odd, we are inside polygon. Pixels on edges
21087 may or may not be included depending on actual geometry of the
21088 polygon. */
21089 if ((lx = poly[n-2], !INTEGERP (lx))
21090 || (ly = poly[n-1], !INTEGERP (lx)))
21091 return 0;
21092 x0 = XINT (lx), y0 = XINT (ly);
21093 for (i = 0; i < n; i += 2)
21094 {
21095 int x1 = x0, y1 = y0;
21096 if ((lx = poly[i], !INTEGERP (lx))
21097 || (ly = poly[i+1], !INTEGERP (ly)))
21098 return 0;
21099 x0 = XINT (lx), y0 = XINT (ly);
21100
21101 /* Does this segment cross the X line? */
21102 if (x0 >= x)
21103 {
21104 if (x1 >= x)
21105 continue;
21106 }
21107 else if (x1 < x)
21108 continue;
21109 if (y > y0 && y > y1)
21110 continue;
21111 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21112 inside = !inside;
21113 }
21114 return inside;
21115 }
21116 }
21117 /* If we don't understand the format, pretend we're not in the hot-spot. */
21118 return 0;
21119 }
21120
21121 Lisp_Object
21122 find_hot_spot (map, x, y)
21123 Lisp_Object map;
21124 int x, y;
21125 {
21126 while (CONSP (map))
21127 {
21128 if (CONSP (XCAR (map))
21129 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21130 return XCAR (map);
21131 map = XCDR (map);
21132 }
21133
21134 return Qnil;
21135 }
21136
21137 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21138 3, 3, 0,
21139 doc: /* Lookup in image map MAP coordinates X and Y.
21140 An image map is an alist where each element has the format (AREA ID PLIST).
21141 An AREA is specified as either a rectangle, a circle, or a polygon:
21142 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21143 pixel coordinates of the upper left and bottom right corners.
21144 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21145 and the radius of the circle; r may be a float or integer.
21146 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21147 vector describes one corner in the polygon.
21148 Returns the alist element for the first matching AREA in MAP. */)
21149 (map, x, y)
21150 Lisp_Object map;
21151 Lisp_Object x, y;
21152 {
21153 if (NILP (map))
21154 return Qnil;
21155
21156 CHECK_NUMBER (x);
21157 CHECK_NUMBER (y);
21158
21159 return find_hot_spot (map, XINT (x), XINT (y));
21160 }
21161
21162
21163 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21164 static void
21165 define_frame_cursor1 (f, cursor, pointer)
21166 struct frame *f;
21167 Cursor cursor;
21168 Lisp_Object pointer;
21169 {
21170 /* Do not change cursor shape while dragging mouse. */
21171 if (!NILP (do_mouse_tracking))
21172 return;
21173
21174 if (!NILP (pointer))
21175 {
21176 if (EQ (pointer, Qarrow))
21177 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21178 else if (EQ (pointer, Qhand))
21179 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21180 else if (EQ (pointer, Qtext))
21181 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21182 else if (EQ (pointer, intern ("hdrag")))
21183 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21184 #ifdef HAVE_X_WINDOWS
21185 else if (EQ (pointer, intern ("vdrag")))
21186 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21187 #endif
21188 else if (EQ (pointer, intern ("hourglass")))
21189 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21190 else if (EQ (pointer, Qmodeline))
21191 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21192 else
21193 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21194 }
21195
21196 if (cursor != No_Cursor)
21197 rif->define_frame_cursor (f, cursor);
21198 }
21199
21200 /* Take proper action when mouse has moved to the mode or header line
21201 or marginal area AREA of window W, x-position X and y-position Y.
21202 X is relative to the start of the text display area of W, so the
21203 width of bitmap areas and scroll bars must be subtracted to get a
21204 position relative to the start of the mode line. */
21205
21206 static void
21207 note_mode_line_or_margin_highlight (w, x, y, area)
21208 struct window *w;
21209 int x, y;
21210 enum window_part area;
21211 {
21212 struct frame *f = XFRAME (w->frame);
21213 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21214 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21215 Lisp_Object pointer = Qnil;
21216 int charpos, dx, dy, width, height;
21217 Lisp_Object string, object = Qnil;
21218 Lisp_Object pos, help;
21219
21220 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21221 string = mode_line_string (w, area, &x, &y, &charpos,
21222 &object, &dx, &dy, &width, &height);
21223 else
21224 {
21225 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21226 string = marginal_area_string (w, area, &x, &y, &charpos,
21227 &object, &dx, &dy, &width, &height);
21228 }
21229
21230 help = Qnil;
21231
21232 if (IMAGEP (object))
21233 {
21234 Lisp_Object image_map, hotspot;
21235 if ((image_map = Fsafe_plist_get (XCDR (object), QCmap),
21236 !NILP (image_map))
21237 && (hotspot = find_hot_spot (image_map, dx, dy),
21238 CONSP (hotspot))
21239 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21240 {
21241 Lisp_Object area_id, plist;
21242
21243 area_id = XCAR (hotspot);
21244 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21245 If so, we could look for mouse-enter, mouse-leave
21246 properties in PLIST (and do something...). */
21247 hotspot = XCDR (hotspot);
21248 if (CONSP (hotspot)
21249 && (plist = XCAR (hotspot), CONSP (plist)))
21250 {
21251 pointer = Fsafe_plist_get (plist, Qpointer);
21252 if (NILP (pointer))
21253 pointer = Qhand;
21254 help = Fsafe_plist_get (plist, Qhelp_echo);
21255 if (!NILP (help))
21256 {
21257 help_echo_string = help;
21258 /* Is this correct? ++kfs */
21259 XSETWINDOW (help_echo_window, w);
21260 help_echo_object = w->buffer;
21261 help_echo_pos = charpos;
21262 }
21263 }
21264 }
21265 if (NILP (pointer))
21266 pointer = Fsafe_plist_get (XCDR (object), QCpointer);
21267 }
21268
21269 if (STRINGP (string))
21270 {
21271 pos = make_number (charpos);
21272 /* If we're on a string with `help-echo' text property, arrange
21273 for the help to be displayed. This is done by setting the
21274 global variable help_echo_string to the help string. */
21275 if (NILP (help))
21276 {
21277 help = Fget_text_property (pos, Qhelp_echo, string);
21278 if (!NILP (help))
21279 {
21280 help_echo_string = help;
21281 XSETWINDOW (help_echo_window, w);
21282 help_echo_object = string;
21283 help_echo_pos = charpos;
21284 }
21285 }
21286
21287 if (NILP (pointer))
21288 pointer = Fget_text_property (pos, Qpointer, string);
21289
21290 /* Change the mouse pointer according to what is under X/Y. */
21291 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21292 {
21293 Lisp_Object map;
21294 map = Fget_text_property (pos, Qlocal_map, string);
21295 if (!KEYMAPP (map))
21296 map = Fget_text_property (pos, Qkeymap, string);
21297 if (!KEYMAPP (map))
21298 cursor = dpyinfo->vertical_scroll_bar_cursor;
21299 }
21300 }
21301
21302 define_frame_cursor1 (f, cursor, pointer);
21303 }
21304
21305
21306 /* EXPORT:
21307 Take proper action when the mouse has moved to position X, Y on
21308 frame F as regards highlighting characters that have mouse-face
21309 properties. Also de-highlighting chars where the mouse was before.
21310 X and Y can be negative or out of range. */
21311
21312 void
21313 note_mouse_highlight (f, x, y)
21314 struct frame *f;
21315 int x, y;
21316 {
21317 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21318 enum window_part part;
21319 Lisp_Object window;
21320 struct window *w;
21321 Cursor cursor = No_Cursor;
21322 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21323 struct buffer *b;
21324
21325 /* When a menu is active, don't highlight because this looks odd. */
21326 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21327 if (popup_activated ())
21328 return;
21329 #endif
21330
21331 if (NILP (Vmouse_highlight)
21332 || !f->glyphs_initialized_p)
21333 return;
21334
21335 dpyinfo->mouse_face_mouse_x = x;
21336 dpyinfo->mouse_face_mouse_y = y;
21337 dpyinfo->mouse_face_mouse_frame = f;
21338
21339 if (dpyinfo->mouse_face_defer)
21340 return;
21341
21342 if (gc_in_progress)
21343 {
21344 dpyinfo->mouse_face_deferred_gc = 1;
21345 return;
21346 }
21347
21348 /* Which window is that in? */
21349 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21350
21351 /* If we were displaying active text in another window, clear that.
21352 Also clear if we move out of text area in same window. */
21353 if (! EQ (window, dpyinfo->mouse_face_window)
21354 || (part != ON_TEXT && !NILP (dpyinfo->mouse_face_window)))
21355 clear_mouse_face (dpyinfo);
21356
21357 /* Not on a window -> return. */
21358 if (!WINDOWP (window))
21359 return;
21360
21361 /* Reset help_echo_string. It will get recomputed below. */
21362 help_echo_string = Qnil;
21363
21364 /* Convert to window-relative pixel coordinates. */
21365 w = XWINDOW (window);
21366 frame_to_window_pixel_xy (w, &x, &y);
21367
21368 /* Handle tool-bar window differently since it doesn't display a
21369 buffer. */
21370 if (EQ (window, f->tool_bar_window))
21371 {
21372 note_tool_bar_highlight (f, x, y);
21373 return;
21374 }
21375
21376 /* Mouse is on the mode, header line or margin? */
21377 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21378 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21379 {
21380 note_mode_line_or_margin_highlight (w, x, y, part);
21381 return;
21382 }
21383
21384 if (part == ON_VERTICAL_BORDER)
21385 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21386 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21387 || part == ON_SCROLL_BAR)
21388 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21389 else
21390 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21391
21392 /* Are we in a window whose display is up to date?
21393 And verify the buffer's text has not changed. */
21394 b = XBUFFER (w->buffer);
21395 if (part == ON_TEXT
21396 && EQ (w->window_end_valid, w->buffer)
21397 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21398 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21399 {
21400 int hpos, vpos, pos, i, dx, dy, area;
21401 struct glyph *glyph;
21402 Lisp_Object object;
21403 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21404 Lisp_Object *overlay_vec = NULL;
21405 int noverlays;
21406 struct buffer *obuf;
21407 int obegv, ozv, same_region;
21408
21409 /* Find the glyph under X/Y. */
21410 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21411
21412 /* Look for :pointer property on image. */
21413 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21414 {
21415 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21416 if (img != NULL && IMAGEP (img->spec))
21417 {
21418 Lisp_Object image_map, hotspot;
21419 if ((image_map = Fsafe_plist_get (XCDR (img->spec), QCmap),
21420 !NILP (image_map))
21421 && (hotspot = find_hot_spot (image_map,
21422 glyph->slice.x + dx,
21423 glyph->slice.y + dy),
21424 CONSP (hotspot))
21425 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21426 {
21427 Lisp_Object area_id, plist;
21428
21429 area_id = XCAR (hotspot);
21430 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21431 If so, we could look for mouse-enter, mouse-leave
21432 properties in PLIST (and do something...). */
21433 hotspot = XCDR (hotspot);
21434 if (CONSP (hotspot)
21435 && (plist = XCAR (hotspot), CONSP (plist)))
21436 {
21437 pointer = Fsafe_plist_get (plist, Qpointer);
21438 if (NILP (pointer))
21439 pointer = Qhand;
21440 help_echo_string = Fsafe_plist_get (plist, Qhelp_echo);
21441 if (!NILP (help_echo_string))
21442 {
21443 help_echo_window = window;
21444 help_echo_object = glyph->object;
21445 help_echo_pos = glyph->charpos;
21446 }
21447 }
21448 }
21449 if (NILP (pointer))
21450 pointer = Fsafe_plist_get (XCDR (img->spec), QCpointer);
21451 }
21452 }
21453
21454 /* Clear mouse face if X/Y not over text. */
21455 if (glyph == NULL
21456 || area != TEXT_AREA
21457 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21458 {
21459 if (clear_mouse_face (dpyinfo))
21460 cursor = No_Cursor;
21461 if (NILP (pointer))
21462 {
21463 if (area != TEXT_AREA)
21464 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21465 else
21466 pointer = Vvoid_text_area_pointer;
21467 }
21468 goto set_cursor;
21469 }
21470
21471 pos = glyph->charpos;
21472 object = glyph->object;
21473 if (!STRINGP (object) && !BUFFERP (object))
21474 goto set_cursor;
21475
21476 /* If we get an out-of-range value, return now; avoid an error. */
21477 if (BUFFERP (object) && pos > BUF_Z (b))
21478 goto set_cursor;
21479
21480 /* Make the window's buffer temporarily current for
21481 overlays_at and compute_char_face. */
21482 obuf = current_buffer;
21483 current_buffer = b;
21484 obegv = BEGV;
21485 ozv = ZV;
21486 BEGV = BEG;
21487 ZV = Z;
21488
21489 /* Is this char mouse-active or does it have help-echo? */
21490 position = make_number (pos);
21491
21492 if (BUFFERP (object))
21493 {
21494 /* Put all the overlays we want in a vector in overlay_vec. */
21495 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21496 /* Sort overlays into increasing priority order. */
21497 noverlays = sort_overlays (overlay_vec, noverlays, w);
21498 }
21499 else
21500 noverlays = 0;
21501
21502 same_region = (EQ (window, dpyinfo->mouse_face_window)
21503 && vpos >= dpyinfo->mouse_face_beg_row
21504 && vpos <= dpyinfo->mouse_face_end_row
21505 && (vpos > dpyinfo->mouse_face_beg_row
21506 || hpos >= dpyinfo->mouse_face_beg_col)
21507 && (vpos < dpyinfo->mouse_face_end_row
21508 || hpos < dpyinfo->mouse_face_end_col
21509 || dpyinfo->mouse_face_past_end));
21510
21511 if (same_region)
21512 cursor = No_Cursor;
21513
21514 /* Check mouse-face highlighting. */
21515 if (! same_region
21516 /* If there exists an overlay with mouse-face overlapping
21517 the one we are currently highlighting, we have to
21518 check if we enter the overlapping overlay, and then
21519 highlight only that. */
21520 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21521 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21522 {
21523 /* Find the highest priority overlay that has a mouse-face
21524 property. */
21525 overlay = Qnil;
21526 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21527 {
21528 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21529 if (!NILP (mouse_face))
21530 overlay = overlay_vec[i];
21531 }
21532
21533 /* If we're actually highlighting the same overlay as
21534 before, there's no need to do that again. */
21535 if (!NILP (overlay)
21536 && EQ (overlay, dpyinfo->mouse_face_overlay))
21537 goto check_help_echo;
21538
21539 dpyinfo->mouse_face_overlay = overlay;
21540
21541 /* Clear the display of the old active region, if any. */
21542 if (clear_mouse_face (dpyinfo))
21543 cursor = No_Cursor;
21544
21545 /* If no overlay applies, get a text property. */
21546 if (NILP (overlay))
21547 mouse_face = Fget_text_property (position, Qmouse_face, object);
21548
21549 /* Handle the overlay case. */
21550 if (!NILP (overlay))
21551 {
21552 /* Find the range of text around this char that
21553 should be active. */
21554 Lisp_Object before, after;
21555 int ignore;
21556
21557 before = Foverlay_start (overlay);
21558 after = Foverlay_end (overlay);
21559 /* Record this as the current active region. */
21560 fast_find_position (w, XFASTINT (before),
21561 &dpyinfo->mouse_face_beg_col,
21562 &dpyinfo->mouse_face_beg_row,
21563 &dpyinfo->mouse_face_beg_x,
21564 &dpyinfo->mouse_face_beg_y, Qnil);
21565
21566 dpyinfo->mouse_face_past_end
21567 = !fast_find_position (w, XFASTINT (after),
21568 &dpyinfo->mouse_face_end_col,
21569 &dpyinfo->mouse_face_end_row,
21570 &dpyinfo->mouse_face_end_x,
21571 &dpyinfo->mouse_face_end_y, Qnil);
21572 dpyinfo->mouse_face_window = window;
21573
21574 dpyinfo->mouse_face_face_id
21575 = face_at_buffer_position (w, pos, 0, 0,
21576 &ignore, pos + 1,
21577 !dpyinfo->mouse_face_hidden);
21578
21579 /* Display it as active. */
21580 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21581 cursor = No_Cursor;
21582 }
21583 /* Handle the text property case. */
21584 else if (!NILP (mouse_face) && BUFFERP (object))
21585 {
21586 /* Find the range of text around this char that
21587 should be active. */
21588 Lisp_Object before, after, beginning, end;
21589 int ignore;
21590
21591 beginning = Fmarker_position (w->start);
21592 end = make_number (BUF_Z (XBUFFER (object))
21593 - XFASTINT (w->window_end_pos));
21594 before
21595 = Fprevious_single_property_change (make_number (pos + 1),
21596 Qmouse_face,
21597 object, beginning);
21598 after
21599 = Fnext_single_property_change (position, Qmouse_face,
21600 object, end);
21601
21602 /* Record this as the current active region. */
21603 fast_find_position (w, XFASTINT (before),
21604 &dpyinfo->mouse_face_beg_col,
21605 &dpyinfo->mouse_face_beg_row,
21606 &dpyinfo->mouse_face_beg_x,
21607 &dpyinfo->mouse_face_beg_y, Qnil);
21608 dpyinfo->mouse_face_past_end
21609 = !fast_find_position (w, XFASTINT (after),
21610 &dpyinfo->mouse_face_end_col,
21611 &dpyinfo->mouse_face_end_row,
21612 &dpyinfo->mouse_face_end_x,
21613 &dpyinfo->mouse_face_end_y, Qnil);
21614 dpyinfo->mouse_face_window = window;
21615
21616 if (BUFFERP (object))
21617 dpyinfo->mouse_face_face_id
21618 = face_at_buffer_position (w, pos, 0, 0,
21619 &ignore, pos + 1,
21620 !dpyinfo->mouse_face_hidden);
21621
21622 /* Display it as active. */
21623 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21624 cursor = No_Cursor;
21625 }
21626 else if (!NILP (mouse_face) && STRINGP (object))
21627 {
21628 Lisp_Object b, e;
21629 int ignore;
21630
21631 b = Fprevious_single_property_change (make_number (pos + 1),
21632 Qmouse_face,
21633 object, Qnil);
21634 e = Fnext_single_property_change (position, Qmouse_face,
21635 object, Qnil);
21636 if (NILP (b))
21637 b = make_number (0);
21638 if (NILP (e))
21639 e = make_number (SCHARS (object) - 1);
21640 fast_find_string_pos (w, XINT (b), object,
21641 &dpyinfo->mouse_face_beg_col,
21642 &dpyinfo->mouse_face_beg_row,
21643 &dpyinfo->mouse_face_beg_x,
21644 &dpyinfo->mouse_face_beg_y, 0);
21645 fast_find_string_pos (w, XINT (e), object,
21646 &dpyinfo->mouse_face_end_col,
21647 &dpyinfo->mouse_face_end_row,
21648 &dpyinfo->mouse_face_end_x,
21649 &dpyinfo->mouse_face_end_y, 1);
21650 dpyinfo->mouse_face_past_end = 0;
21651 dpyinfo->mouse_face_window = window;
21652 dpyinfo->mouse_face_face_id
21653 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
21654 glyph->face_id, 1);
21655 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21656 cursor = No_Cursor;
21657 }
21658 else if (STRINGP (object) && NILP (mouse_face))
21659 {
21660 /* A string which doesn't have mouse-face, but
21661 the text ``under'' it might have. */
21662 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
21663 int start = MATRIX_ROW_START_CHARPOS (r);
21664
21665 pos = string_buffer_position (w, object, start);
21666 if (pos > 0)
21667 mouse_face = get_char_property_and_overlay (make_number (pos),
21668 Qmouse_face,
21669 w->buffer,
21670 &overlay);
21671 if (!NILP (mouse_face) && !NILP (overlay))
21672 {
21673 Lisp_Object before = Foverlay_start (overlay);
21674 Lisp_Object after = Foverlay_end (overlay);
21675 int ignore;
21676
21677 /* Note that we might not be able to find position
21678 BEFORE in the glyph matrix if the overlay is
21679 entirely covered by a `display' property. In
21680 this case, we overshoot. So let's stop in
21681 the glyph matrix before glyphs for OBJECT. */
21682 fast_find_position (w, XFASTINT (before),
21683 &dpyinfo->mouse_face_beg_col,
21684 &dpyinfo->mouse_face_beg_row,
21685 &dpyinfo->mouse_face_beg_x,
21686 &dpyinfo->mouse_face_beg_y,
21687 object);
21688
21689 dpyinfo->mouse_face_past_end
21690 = !fast_find_position (w, XFASTINT (after),
21691 &dpyinfo->mouse_face_end_col,
21692 &dpyinfo->mouse_face_end_row,
21693 &dpyinfo->mouse_face_end_x,
21694 &dpyinfo->mouse_face_end_y,
21695 Qnil);
21696 dpyinfo->mouse_face_window = window;
21697 dpyinfo->mouse_face_face_id
21698 = face_at_buffer_position (w, pos, 0, 0,
21699 &ignore, pos + 1,
21700 !dpyinfo->mouse_face_hidden);
21701
21702 /* Display it as active. */
21703 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21704 cursor = No_Cursor;
21705 }
21706 }
21707 }
21708
21709 check_help_echo:
21710
21711 /* Look for a `help-echo' property. */
21712 if (NILP (help_echo_string)) {
21713 Lisp_Object help, overlay;
21714
21715 /* Check overlays first. */
21716 help = overlay = Qnil;
21717 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
21718 {
21719 overlay = overlay_vec[i];
21720 help = Foverlay_get (overlay, Qhelp_echo);
21721 }
21722
21723 if (!NILP (help))
21724 {
21725 help_echo_string = help;
21726 help_echo_window = window;
21727 help_echo_object = overlay;
21728 help_echo_pos = pos;
21729 }
21730 else
21731 {
21732 Lisp_Object object = glyph->object;
21733 int charpos = glyph->charpos;
21734
21735 /* Try text properties. */
21736 if (STRINGP (object)
21737 && charpos >= 0
21738 && charpos < SCHARS (object))
21739 {
21740 help = Fget_text_property (make_number (charpos),
21741 Qhelp_echo, object);
21742 if (NILP (help))
21743 {
21744 /* If the string itself doesn't specify a help-echo,
21745 see if the buffer text ``under'' it does. */
21746 struct glyph_row *r
21747 = MATRIX_ROW (w->current_matrix, vpos);
21748 int start = MATRIX_ROW_START_CHARPOS (r);
21749 int pos = string_buffer_position (w, object, start);
21750 if (pos > 0)
21751 {
21752 help = Fget_char_property (make_number (pos),
21753 Qhelp_echo, w->buffer);
21754 if (!NILP (help))
21755 {
21756 charpos = pos;
21757 object = w->buffer;
21758 }
21759 }
21760 }
21761 }
21762 else if (BUFFERP (object)
21763 && charpos >= BEGV
21764 && charpos < ZV)
21765 help = Fget_text_property (make_number (charpos), Qhelp_echo,
21766 object);
21767
21768 if (!NILP (help))
21769 {
21770 help_echo_string = help;
21771 help_echo_window = window;
21772 help_echo_object = object;
21773 help_echo_pos = charpos;
21774 }
21775 }
21776 }
21777
21778 /* Look for a `pointer' property. */
21779 if (NILP (pointer))
21780 {
21781 /* Check overlays first. */
21782 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
21783 pointer = Foverlay_get (overlay_vec[i], Qpointer);
21784
21785 if (NILP (pointer))
21786 {
21787 Lisp_Object object = glyph->object;
21788 int charpos = glyph->charpos;
21789
21790 /* Try text properties. */
21791 if (STRINGP (object)
21792 && charpos >= 0
21793 && charpos < SCHARS (object))
21794 {
21795 pointer = Fget_text_property (make_number (charpos),
21796 Qpointer, object);
21797 if (NILP (pointer))
21798 {
21799 /* If the string itself doesn't specify a pointer,
21800 see if the buffer text ``under'' it does. */
21801 struct glyph_row *r
21802 = MATRIX_ROW (w->current_matrix, vpos);
21803 int start = MATRIX_ROW_START_CHARPOS (r);
21804 int pos = string_buffer_position (w, object, start);
21805 if (pos > 0)
21806 pointer = Fget_char_property (make_number (pos),
21807 Qpointer, w->buffer);
21808 }
21809 }
21810 else if (BUFFERP (object)
21811 && charpos >= BEGV
21812 && charpos < ZV)
21813 pointer = Fget_text_property (make_number (charpos),
21814 Qpointer, object);
21815 }
21816 }
21817
21818 BEGV = obegv;
21819 ZV = ozv;
21820 current_buffer = obuf;
21821 }
21822
21823 set_cursor:
21824
21825 define_frame_cursor1 (f, cursor, pointer);
21826 }
21827
21828
21829 /* EXPORT for RIF:
21830 Clear any mouse-face on window W. This function is part of the
21831 redisplay interface, and is called from try_window_id and similar
21832 functions to ensure the mouse-highlight is off. */
21833
21834 void
21835 x_clear_window_mouse_face (w)
21836 struct window *w;
21837 {
21838 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21839 Lisp_Object window;
21840
21841 BLOCK_INPUT;
21842 XSETWINDOW (window, w);
21843 if (EQ (window, dpyinfo->mouse_face_window))
21844 clear_mouse_face (dpyinfo);
21845 UNBLOCK_INPUT;
21846 }
21847
21848
21849 /* EXPORT:
21850 Just discard the mouse face information for frame F, if any.
21851 This is used when the size of F is changed. */
21852
21853 void
21854 cancel_mouse_face (f)
21855 struct frame *f;
21856 {
21857 Lisp_Object window;
21858 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21859
21860 window = dpyinfo->mouse_face_window;
21861 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
21862 {
21863 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21864 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21865 dpyinfo->mouse_face_window = Qnil;
21866 }
21867 }
21868
21869
21870 #endif /* HAVE_WINDOW_SYSTEM */
21871
21872 \f
21873 /***********************************************************************
21874 Exposure Events
21875 ***********************************************************************/
21876
21877 #ifdef HAVE_WINDOW_SYSTEM
21878
21879 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
21880 which intersects rectangle R. R is in window-relative coordinates. */
21881
21882 static void
21883 expose_area (w, row, r, area)
21884 struct window *w;
21885 struct glyph_row *row;
21886 XRectangle *r;
21887 enum glyph_row_area area;
21888 {
21889 struct glyph *first = row->glyphs[area];
21890 struct glyph *end = row->glyphs[area] + row->used[area];
21891 struct glyph *last;
21892 int first_x, start_x, x;
21893
21894 if (area == TEXT_AREA && row->fill_line_p)
21895 /* If row extends face to end of line write the whole line. */
21896 draw_glyphs (w, 0, row, area,
21897 0, row->used[area],
21898 DRAW_NORMAL_TEXT, 0);
21899 else
21900 {
21901 /* Set START_X to the window-relative start position for drawing glyphs of
21902 AREA. The first glyph of the text area can be partially visible.
21903 The first glyphs of other areas cannot. */
21904 start_x = window_box_left_offset (w, area);
21905 x = start_x;
21906 if (area == TEXT_AREA)
21907 x += row->x;
21908
21909 /* Find the first glyph that must be redrawn. */
21910 while (first < end
21911 && x + first->pixel_width < r->x)
21912 {
21913 x += first->pixel_width;
21914 ++first;
21915 }
21916
21917 /* Find the last one. */
21918 last = first;
21919 first_x = x;
21920 while (last < end
21921 && x < r->x + r->width)
21922 {
21923 x += last->pixel_width;
21924 ++last;
21925 }
21926
21927 /* Repaint. */
21928 if (last > first)
21929 draw_glyphs (w, first_x - start_x, row, area,
21930 first - row->glyphs[area], last - row->glyphs[area],
21931 DRAW_NORMAL_TEXT, 0);
21932 }
21933 }
21934
21935
21936 /* Redraw the parts of the glyph row ROW on window W intersecting
21937 rectangle R. R is in window-relative coordinates. Value is
21938 non-zero if mouse-face was overwritten. */
21939
21940 static int
21941 expose_line (w, row, r)
21942 struct window *w;
21943 struct glyph_row *row;
21944 XRectangle *r;
21945 {
21946 xassert (row->enabled_p);
21947
21948 if (row->mode_line_p || w->pseudo_window_p)
21949 draw_glyphs (w, 0, row, TEXT_AREA,
21950 0, row->used[TEXT_AREA],
21951 DRAW_NORMAL_TEXT, 0);
21952 else
21953 {
21954 if (row->used[LEFT_MARGIN_AREA])
21955 expose_area (w, row, r, LEFT_MARGIN_AREA);
21956 if (row->used[TEXT_AREA])
21957 expose_area (w, row, r, TEXT_AREA);
21958 if (row->used[RIGHT_MARGIN_AREA])
21959 expose_area (w, row, r, RIGHT_MARGIN_AREA);
21960 draw_row_fringe_bitmaps (w, row);
21961 }
21962
21963 return row->mouse_face_p;
21964 }
21965
21966
21967 /* Redraw those parts of glyphs rows during expose event handling that
21968 overlap other rows. Redrawing of an exposed line writes over parts
21969 of lines overlapping that exposed line; this function fixes that.
21970
21971 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
21972 row in W's current matrix that is exposed and overlaps other rows.
21973 LAST_OVERLAPPING_ROW is the last such row. */
21974
21975 static void
21976 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
21977 struct window *w;
21978 struct glyph_row *first_overlapping_row;
21979 struct glyph_row *last_overlapping_row;
21980 {
21981 struct glyph_row *row;
21982
21983 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
21984 if (row->overlapping_p)
21985 {
21986 xassert (row->enabled_p && !row->mode_line_p);
21987
21988 if (row->used[LEFT_MARGIN_AREA])
21989 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
21990
21991 if (row->used[TEXT_AREA])
21992 x_fix_overlapping_area (w, row, TEXT_AREA);
21993
21994 if (row->used[RIGHT_MARGIN_AREA])
21995 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
21996 }
21997 }
21998
21999
22000 /* Return non-zero if W's cursor intersects rectangle R. */
22001
22002 static int
22003 phys_cursor_in_rect_p (w, r)
22004 struct window *w;
22005 XRectangle *r;
22006 {
22007 XRectangle cr, result;
22008 struct glyph *cursor_glyph;
22009
22010 cursor_glyph = get_phys_cursor_glyph (w);
22011 if (cursor_glyph)
22012 {
22013 /* r is relative to W's box, but w->phys_cursor.x is relative
22014 to left edge of W's TEXT area. Adjust it. */
22015 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22016 cr.y = w->phys_cursor.y;
22017 cr.width = cursor_glyph->pixel_width;
22018 cr.height = w->phys_cursor_height;
22019 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22020 I assume the effect is the same -- and this is portable. */
22021 return x_intersect_rectangles (&cr, r, &result);
22022 }
22023 else
22024 return 0;
22025 }
22026
22027
22028 /* EXPORT:
22029 Draw a vertical window border to the right of window W if W doesn't
22030 have vertical scroll bars. */
22031
22032 void
22033 x_draw_vertical_border (w)
22034 struct window *w;
22035 {
22036 /* We could do better, if we knew what type of scroll-bar the adjacent
22037 windows (on either side) have... But we don't :-(
22038 However, I think this works ok. ++KFS 2003-04-25 */
22039
22040 /* Redraw borders between horizontally adjacent windows. Don't
22041 do it for frames with vertical scroll bars because either the
22042 right scroll bar of a window, or the left scroll bar of its
22043 neighbor will suffice as a border. */
22044 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22045 return;
22046
22047 if (!WINDOW_RIGHTMOST_P (w)
22048 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22049 {
22050 int x0, x1, y0, y1;
22051
22052 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22053 y1 -= 1;
22054
22055 rif->draw_vertical_window_border (w, x1, y0, y1);
22056 }
22057 else if (!WINDOW_LEFTMOST_P (w)
22058 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22059 {
22060 int x0, x1, y0, y1;
22061
22062 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22063 y1 -= 1;
22064
22065 rif->draw_vertical_window_border (w, x0, y0, y1);
22066 }
22067 }
22068
22069
22070 /* Redraw the part of window W intersection rectangle FR. Pixel
22071 coordinates in FR are frame-relative. Call this function with
22072 input blocked. Value is non-zero if the exposure overwrites
22073 mouse-face. */
22074
22075 static int
22076 expose_window (w, fr)
22077 struct window *w;
22078 XRectangle *fr;
22079 {
22080 struct frame *f = XFRAME (w->frame);
22081 XRectangle wr, r;
22082 int mouse_face_overwritten_p = 0;
22083
22084 /* If window is not yet fully initialized, do nothing. This can
22085 happen when toolkit scroll bars are used and a window is split.
22086 Reconfiguring the scroll bar will generate an expose for a newly
22087 created window. */
22088 if (w->current_matrix == NULL)
22089 return 0;
22090
22091 /* When we're currently updating the window, display and current
22092 matrix usually don't agree. Arrange for a thorough display
22093 later. */
22094 if (w == updated_window)
22095 {
22096 SET_FRAME_GARBAGED (f);
22097 return 0;
22098 }
22099
22100 /* Frame-relative pixel rectangle of W. */
22101 wr.x = WINDOW_LEFT_EDGE_X (w);
22102 wr.y = WINDOW_TOP_EDGE_Y (w);
22103 wr.width = WINDOW_TOTAL_WIDTH (w);
22104 wr.height = WINDOW_TOTAL_HEIGHT (w);
22105
22106 if (x_intersect_rectangles (fr, &wr, &r))
22107 {
22108 int yb = window_text_bottom_y (w);
22109 struct glyph_row *row;
22110 int cursor_cleared_p;
22111 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22112
22113 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22114 r.x, r.y, r.width, r.height));
22115
22116 /* Convert to window coordinates. */
22117 r.x -= WINDOW_LEFT_EDGE_X (w);
22118 r.y -= WINDOW_TOP_EDGE_Y (w);
22119
22120 /* Turn off the cursor. */
22121 if (!w->pseudo_window_p
22122 && phys_cursor_in_rect_p (w, &r))
22123 {
22124 x_clear_cursor (w);
22125 cursor_cleared_p = 1;
22126 }
22127 else
22128 cursor_cleared_p = 0;
22129
22130 /* Update lines intersecting rectangle R. */
22131 first_overlapping_row = last_overlapping_row = NULL;
22132 for (row = w->current_matrix->rows;
22133 row->enabled_p;
22134 ++row)
22135 {
22136 int y0 = row->y;
22137 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22138
22139 if ((y0 >= r.y && y0 < r.y + r.height)
22140 || (y1 > r.y && y1 < r.y + r.height)
22141 || (r.y >= y0 && r.y < y1)
22142 || (r.y + r.height > y0 && r.y + r.height < y1))
22143 {
22144 /* A header line may be overlapping, but there is no need
22145 to fix overlapping areas for them. KFS 2005-02-12 */
22146 if (row->overlapping_p && !row->mode_line_p)
22147 {
22148 if (first_overlapping_row == NULL)
22149 first_overlapping_row = row;
22150 last_overlapping_row = row;
22151 }
22152
22153 if (expose_line (w, row, &r))
22154 mouse_face_overwritten_p = 1;
22155 }
22156
22157 if (y1 >= yb)
22158 break;
22159 }
22160
22161 /* Display the mode line if there is one. */
22162 if (WINDOW_WANTS_MODELINE_P (w)
22163 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22164 row->enabled_p)
22165 && row->y < r.y + r.height)
22166 {
22167 if (expose_line (w, row, &r))
22168 mouse_face_overwritten_p = 1;
22169 }
22170
22171 if (!w->pseudo_window_p)
22172 {
22173 /* Fix the display of overlapping rows. */
22174 if (first_overlapping_row)
22175 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22176
22177 /* Draw border between windows. */
22178 x_draw_vertical_border (w);
22179
22180 /* Turn the cursor on again. */
22181 if (cursor_cleared_p)
22182 update_window_cursor (w, 1);
22183 }
22184 }
22185
22186 return mouse_face_overwritten_p;
22187 }
22188
22189
22190
22191 /* Redraw (parts) of all windows in the window tree rooted at W that
22192 intersect R. R contains frame pixel coordinates. Value is
22193 non-zero if the exposure overwrites mouse-face. */
22194
22195 static int
22196 expose_window_tree (w, r)
22197 struct window *w;
22198 XRectangle *r;
22199 {
22200 struct frame *f = XFRAME (w->frame);
22201 int mouse_face_overwritten_p = 0;
22202
22203 while (w && !FRAME_GARBAGED_P (f))
22204 {
22205 if (!NILP (w->hchild))
22206 mouse_face_overwritten_p
22207 |= expose_window_tree (XWINDOW (w->hchild), r);
22208 else if (!NILP (w->vchild))
22209 mouse_face_overwritten_p
22210 |= expose_window_tree (XWINDOW (w->vchild), r);
22211 else
22212 mouse_face_overwritten_p |= expose_window (w, r);
22213
22214 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22215 }
22216
22217 return mouse_face_overwritten_p;
22218 }
22219
22220
22221 /* EXPORT:
22222 Redisplay an exposed area of frame F. X and Y are the upper-left
22223 corner of the exposed rectangle. W and H are width and height of
22224 the exposed area. All are pixel values. W or H zero means redraw
22225 the entire frame. */
22226
22227 void
22228 expose_frame (f, x, y, w, h)
22229 struct frame *f;
22230 int x, y, w, h;
22231 {
22232 XRectangle r;
22233 int mouse_face_overwritten_p = 0;
22234
22235 TRACE ((stderr, "expose_frame "));
22236
22237 /* No need to redraw if frame will be redrawn soon. */
22238 if (FRAME_GARBAGED_P (f))
22239 {
22240 TRACE ((stderr, " garbaged\n"));
22241 return;
22242 }
22243
22244 /* If basic faces haven't been realized yet, there is no point in
22245 trying to redraw anything. This can happen when we get an expose
22246 event while Emacs is starting, e.g. by moving another window. */
22247 if (FRAME_FACE_CACHE (f) == NULL
22248 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22249 {
22250 TRACE ((stderr, " no faces\n"));
22251 return;
22252 }
22253
22254 if (w == 0 || h == 0)
22255 {
22256 r.x = r.y = 0;
22257 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22258 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22259 }
22260 else
22261 {
22262 r.x = x;
22263 r.y = y;
22264 r.width = w;
22265 r.height = h;
22266 }
22267
22268 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22269 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22270
22271 if (WINDOWP (f->tool_bar_window))
22272 mouse_face_overwritten_p
22273 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22274
22275 #ifdef HAVE_X_WINDOWS
22276 #ifndef MSDOS
22277 #ifndef USE_X_TOOLKIT
22278 if (WINDOWP (f->menu_bar_window))
22279 mouse_face_overwritten_p
22280 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22281 #endif /* not USE_X_TOOLKIT */
22282 #endif
22283 #endif
22284
22285 /* Some window managers support a focus-follows-mouse style with
22286 delayed raising of frames. Imagine a partially obscured frame,
22287 and moving the mouse into partially obscured mouse-face on that
22288 frame. The visible part of the mouse-face will be highlighted,
22289 then the WM raises the obscured frame. With at least one WM, KDE
22290 2.1, Emacs is not getting any event for the raising of the frame
22291 (even tried with SubstructureRedirectMask), only Expose events.
22292 These expose events will draw text normally, i.e. not
22293 highlighted. Which means we must redo the highlight here.
22294 Subsume it under ``we love X''. --gerd 2001-08-15 */
22295 /* Included in Windows version because Windows most likely does not
22296 do the right thing if any third party tool offers
22297 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22298 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22299 {
22300 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22301 if (f == dpyinfo->mouse_face_mouse_frame)
22302 {
22303 int x = dpyinfo->mouse_face_mouse_x;
22304 int y = dpyinfo->mouse_face_mouse_y;
22305 clear_mouse_face (dpyinfo);
22306 note_mouse_highlight (f, x, y);
22307 }
22308 }
22309 }
22310
22311
22312 /* EXPORT:
22313 Determine the intersection of two rectangles R1 and R2. Return
22314 the intersection in *RESULT. Value is non-zero if RESULT is not
22315 empty. */
22316
22317 int
22318 x_intersect_rectangles (r1, r2, result)
22319 XRectangle *r1, *r2, *result;
22320 {
22321 XRectangle *left, *right;
22322 XRectangle *upper, *lower;
22323 int intersection_p = 0;
22324
22325 /* Rearrange so that R1 is the left-most rectangle. */
22326 if (r1->x < r2->x)
22327 left = r1, right = r2;
22328 else
22329 left = r2, right = r1;
22330
22331 /* X0 of the intersection is right.x0, if this is inside R1,
22332 otherwise there is no intersection. */
22333 if (right->x <= left->x + left->width)
22334 {
22335 result->x = right->x;
22336
22337 /* The right end of the intersection is the minimum of the
22338 the right ends of left and right. */
22339 result->width = (min (left->x + left->width, right->x + right->width)
22340 - result->x);
22341
22342 /* Same game for Y. */
22343 if (r1->y < r2->y)
22344 upper = r1, lower = r2;
22345 else
22346 upper = r2, lower = r1;
22347
22348 /* The upper end of the intersection is lower.y0, if this is inside
22349 of upper. Otherwise, there is no intersection. */
22350 if (lower->y <= upper->y + upper->height)
22351 {
22352 result->y = lower->y;
22353
22354 /* The lower end of the intersection is the minimum of the lower
22355 ends of upper and lower. */
22356 result->height = (min (lower->y + lower->height,
22357 upper->y + upper->height)
22358 - result->y);
22359 intersection_p = 1;
22360 }
22361 }
22362
22363 return intersection_p;
22364 }
22365
22366 #endif /* HAVE_WINDOW_SYSTEM */
22367
22368 \f
22369 /***********************************************************************
22370 Initialization
22371 ***********************************************************************/
22372
22373 void
22374 syms_of_xdisp ()
22375 {
22376 Vwith_echo_area_save_vector = Qnil;
22377 staticpro (&Vwith_echo_area_save_vector);
22378
22379 Vmessage_stack = Qnil;
22380 staticpro (&Vmessage_stack);
22381
22382 Qinhibit_redisplay = intern ("inhibit-redisplay");
22383 staticpro (&Qinhibit_redisplay);
22384
22385 message_dolog_marker1 = Fmake_marker ();
22386 staticpro (&message_dolog_marker1);
22387 message_dolog_marker2 = Fmake_marker ();
22388 staticpro (&message_dolog_marker2);
22389 message_dolog_marker3 = Fmake_marker ();
22390 staticpro (&message_dolog_marker3);
22391
22392 #if GLYPH_DEBUG
22393 defsubr (&Sdump_frame_glyph_matrix);
22394 defsubr (&Sdump_glyph_matrix);
22395 defsubr (&Sdump_glyph_row);
22396 defsubr (&Sdump_tool_bar_row);
22397 defsubr (&Strace_redisplay);
22398 defsubr (&Strace_to_stderr);
22399 #endif
22400 #ifdef HAVE_WINDOW_SYSTEM
22401 defsubr (&Stool_bar_lines_needed);
22402 defsubr (&Slookup_image_map);
22403 #endif
22404 defsubr (&Sformat_mode_line);
22405
22406 staticpro (&Qmenu_bar_update_hook);
22407 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22408
22409 staticpro (&Qoverriding_terminal_local_map);
22410 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22411
22412 staticpro (&Qoverriding_local_map);
22413 Qoverriding_local_map = intern ("overriding-local-map");
22414
22415 staticpro (&Qwindow_scroll_functions);
22416 Qwindow_scroll_functions = intern ("window-scroll-functions");
22417
22418 staticpro (&Qredisplay_end_trigger_functions);
22419 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22420
22421 staticpro (&Qinhibit_point_motion_hooks);
22422 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22423
22424 QCdata = intern (":data");
22425 staticpro (&QCdata);
22426 Qdisplay = intern ("display");
22427 staticpro (&Qdisplay);
22428 Qspace_width = intern ("space-width");
22429 staticpro (&Qspace_width);
22430 Qraise = intern ("raise");
22431 staticpro (&Qraise);
22432 Qslice = intern ("slice");
22433 staticpro (&Qslice);
22434 Qspace = intern ("space");
22435 staticpro (&Qspace);
22436 Qmargin = intern ("margin");
22437 staticpro (&Qmargin);
22438 Qpointer = intern ("pointer");
22439 staticpro (&Qpointer);
22440 Qleft_margin = intern ("left-margin");
22441 staticpro (&Qleft_margin);
22442 Qright_margin = intern ("right-margin");
22443 staticpro (&Qright_margin);
22444 Qcenter = intern ("center");
22445 staticpro (&Qcenter);
22446 Qline_height = intern ("line-height");
22447 staticpro (&Qline_height);
22448 QCalign_to = intern (":align-to");
22449 staticpro (&QCalign_to);
22450 QCrelative_width = intern (":relative-width");
22451 staticpro (&QCrelative_width);
22452 QCrelative_height = intern (":relative-height");
22453 staticpro (&QCrelative_height);
22454 QCeval = intern (":eval");
22455 staticpro (&QCeval);
22456 QCpropertize = intern (":propertize");
22457 staticpro (&QCpropertize);
22458 QCfile = intern (":file");
22459 staticpro (&QCfile);
22460 Qfontified = intern ("fontified");
22461 staticpro (&Qfontified);
22462 Qfontification_functions = intern ("fontification-functions");
22463 staticpro (&Qfontification_functions);
22464 Qtrailing_whitespace = intern ("trailing-whitespace");
22465 staticpro (&Qtrailing_whitespace);
22466 Qescape_glyph = intern ("escape-glyph");
22467 staticpro (&Qescape_glyph);
22468 Qimage = intern ("image");
22469 staticpro (&Qimage);
22470 QCmap = intern (":map");
22471 staticpro (&QCmap);
22472 QCpointer = intern (":pointer");
22473 staticpro (&QCpointer);
22474 Qrect = intern ("rect");
22475 staticpro (&Qrect);
22476 Qcircle = intern ("circle");
22477 staticpro (&Qcircle);
22478 Qpoly = intern ("poly");
22479 staticpro (&Qpoly);
22480 Qmessage_truncate_lines = intern ("message-truncate-lines");
22481 staticpro (&Qmessage_truncate_lines);
22482 Qcursor_in_non_selected_windows = intern ("cursor-in-non-selected-windows");
22483 staticpro (&Qcursor_in_non_selected_windows);
22484 Qgrow_only = intern ("grow-only");
22485 staticpro (&Qgrow_only);
22486 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22487 staticpro (&Qinhibit_menubar_update);
22488 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22489 staticpro (&Qinhibit_eval_during_redisplay);
22490 Qposition = intern ("position");
22491 staticpro (&Qposition);
22492 Qbuffer_position = intern ("buffer-position");
22493 staticpro (&Qbuffer_position);
22494 Qobject = intern ("object");
22495 staticpro (&Qobject);
22496 Qbar = intern ("bar");
22497 staticpro (&Qbar);
22498 Qhbar = intern ("hbar");
22499 staticpro (&Qhbar);
22500 Qbox = intern ("box");
22501 staticpro (&Qbox);
22502 Qhollow = intern ("hollow");
22503 staticpro (&Qhollow);
22504 Qhand = intern ("hand");
22505 staticpro (&Qhand);
22506 Qarrow = intern ("arrow");
22507 staticpro (&Qarrow);
22508 Qtext = intern ("text");
22509 staticpro (&Qtext);
22510 Qrisky_local_variable = intern ("risky-local-variable");
22511 staticpro (&Qrisky_local_variable);
22512 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22513 staticpro (&Qinhibit_free_realized_faces);
22514
22515 list_of_error = Fcons (Fcons (intern ("error"),
22516 Fcons (intern ("void-variable"), Qnil)),
22517 Qnil);
22518 staticpro (&list_of_error);
22519
22520 Qlast_arrow_position = intern ("last-arrow-position");
22521 staticpro (&Qlast_arrow_position);
22522 Qlast_arrow_string = intern ("last-arrow-string");
22523 staticpro (&Qlast_arrow_string);
22524
22525 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22526 staticpro (&Qoverlay_arrow_string);
22527 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22528 staticpro (&Qoverlay_arrow_bitmap);
22529
22530 echo_buffer[0] = echo_buffer[1] = Qnil;
22531 staticpro (&echo_buffer[0]);
22532 staticpro (&echo_buffer[1]);
22533
22534 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22535 staticpro (&echo_area_buffer[0]);
22536 staticpro (&echo_area_buffer[1]);
22537
22538 Vmessages_buffer_name = build_string ("*Messages*");
22539 staticpro (&Vmessages_buffer_name);
22540
22541 mode_line_proptrans_alist = Qnil;
22542 staticpro (&mode_line_proptrans_alist);
22543
22544 mode_line_string_list = Qnil;
22545 staticpro (&mode_line_string_list);
22546
22547 help_echo_string = Qnil;
22548 staticpro (&help_echo_string);
22549 help_echo_object = Qnil;
22550 staticpro (&help_echo_object);
22551 help_echo_window = Qnil;
22552 staticpro (&help_echo_window);
22553 previous_help_echo_string = Qnil;
22554 staticpro (&previous_help_echo_string);
22555 help_echo_pos = -1;
22556
22557 #ifdef HAVE_WINDOW_SYSTEM
22558 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22559 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22560 For example, if a block cursor is over a tab, it will be drawn as
22561 wide as that tab on the display. */);
22562 x_stretch_cursor_p = 0;
22563 #endif
22564
22565 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22566 doc: /* *Non-nil means highlight trailing whitespace.
22567 The face used for trailing whitespace is `trailing-whitespace'. */);
22568 Vshow_trailing_whitespace = Qnil;
22569
22570 DEFVAR_LISP ("show-nonbreak-escape", &Vshow_nonbreak_escape,
22571 doc: /* *Non-nil means display escape character before non-break space and hyphen. */);
22572 Vshow_nonbreak_escape = Qt;
22573
22574 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22575 doc: /* *The pointer shape to show in void text areas.
22576 Nil means to show the text pointer. Other options are `arrow', `text',
22577 `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22578 Vvoid_text_area_pointer = Qarrow;
22579
22580 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22581 doc: /* Non-nil means don't actually do any redisplay.
22582 This is used for internal purposes. */);
22583 Vinhibit_redisplay = Qnil;
22584
22585 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22586 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22587 Vglobal_mode_string = Qnil;
22588
22589 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22590 doc: /* Marker for where to display an arrow on top of the buffer text.
22591 This must be the beginning of a line in order to work.
22592 See also `overlay-arrow-string'. */);
22593 Voverlay_arrow_position = Qnil;
22594
22595 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22596 doc: /* String to display as an arrow in non-window frames.
22597 See also `overlay-arrow-position'. */);
22598 Voverlay_arrow_string = Qnil;
22599
22600 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22601 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22602 The symbols on this list are examined during redisplay to determine
22603 where to display overlay arrows. */);
22604 Voverlay_arrow_variable_list
22605 = Fcons (intern ("overlay-arrow-position"), Qnil);
22606
22607 DEFVAR_INT ("scroll-step", &scroll_step,
22608 doc: /* *The number of lines to try scrolling a window by when point moves out.
22609 If that fails to bring point back on frame, point is centered instead.
22610 If this is zero, point is always centered after it moves off frame.
22611 If you want scrolling to always be a line at a time, you should set
22612 `scroll-conservatively' to a large value rather than set this to 1. */);
22613
22614 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22615 doc: /* *Scroll up to this many lines, to bring point back on screen.
22616 A value of zero means to scroll the text to center point vertically
22617 in the window. */);
22618 scroll_conservatively = 0;
22619
22620 DEFVAR_INT ("scroll-margin", &scroll_margin,
22621 doc: /* *Number of lines of margin at the top and bottom of a window.
22622 Recenter the window whenever point gets within this many lines
22623 of the top or bottom of the window. */);
22624 scroll_margin = 0;
22625
22626 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22627 doc: /* Pixels per inch on current display.
22628 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22629 Vdisplay_pixels_per_inch = make_float (72.0);
22630
22631 #if GLYPH_DEBUG
22632 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22633 #endif
22634
22635 DEFVAR_BOOL ("truncate-partial-width-windows",
22636 &truncate_partial_width_windows,
22637 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
22638 truncate_partial_width_windows = 1;
22639
22640 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
22641 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
22642 Any other value means to use the appropriate face, `mode-line',
22643 `header-line', or `menu' respectively. */);
22644 mode_line_inverse_video = 1;
22645
22646 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
22647 doc: /* *Maximum buffer size for which line number should be displayed.
22648 If the buffer is bigger than this, the line number does not appear
22649 in the mode line. A value of nil means no limit. */);
22650 Vline_number_display_limit = Qnil;
22651
22652 DEFVAR_INT ("line-number-display-limit-width",
22653 &line_number_display_limit_width,
22654 doc: /* *Maximum line width (in characters) for line number display.
22655 If the average length of the lines near point is bigger than this, then the
22656 line number may be omitted from the mode line. */);
22657 line_number_display_limit_width = 200;
22658
22659 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
22660 doc: /* *Non-nil means highlight region even in nonselected windows. */);
22661 highlight_nonselected_windows = 0;
22662
22663 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
22664 doc: /* Non-nil if more than one frame is visible on this display.
22665 Minibuffer-only frames don't count, but iconified frames do.
22666 This variable is not guaranteed to be accurate except while processing
22667 `frame-title-format' and `icon-title-format'. */);
22668
22669 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
22670 doc: /* Template for displaying the title bar of visible frames.
22671 \(Assuming the window manager supports this feature.)
22672 This variable has the same structure as `mode-line-format' (which see),
22673 and is used only on frames for which no explicit name has been set
22674 \(see `modify-frame-parameters'). */);
22675
22676 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
22677 doc: /* Template for displaying the title bar of an iconified frame.
22678 \(Assuming the window manager supports this feature.)
22679 This variable has the same structure as `mode-line-format' (which see),
22680 and is used only on frames for which no explicit name has been set
22681 \(see `modify-frame-parameters'). */);
22682 Vicon_title_format
22683 = Vframe_title_format
22684 = Fcons (intern ("multiple-frames"),
22685 Fcons (build_string ("%b"),
22686 Fcons (Fcons (empty_string,
22687 Fcons (intern ("invocation-name"),
22688 Fcons (build_string ("@"),
22689 Fcons (intern ("system-name"),
22690 Qnil)))),
22691 Qnil)));
22692
22693 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
22694 doc: /* Maximum number of lines to keep in the message log buffer.
22695 If nil, disable message logging. If t, log messages but don't truncate
22696 the buffer when it becomes large. */);
22697 Vmessage_log_max = make_number (50);
22698
22699 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
22700 doc: /* Functions called before redisplay, if window sizes have changed.
22701 The value should be a list of functions that take one argument.
22702 Just before redisplay, for each frame, if any of its windows have changed
22703 size since the last redisplay, or have been split or deleted,
22704 all the functions in the list are called, with the frame as argument. */);
22705 Vwindow_size_change_functions = Qnil;
22706
22707 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
22708 doc: /* List of functions to call before redisplaying a window with scrolling.
22709 Each function is called with two arguments, the window
22710 and its new display-start position. Note that the value of `window-end'
22711 is not valid when these functions are called. */);
22712 Vwindow_scroll_functions = Qnil;
22713
22714 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
22715 doc: /* *Non-nil means autoselect window with mouse pointer. */);
22716 mouse_autoselect_window = 0;
22717
22718 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
22719 doc: /* *Non-nil means automatically resize tool-bars.
22720 This increases a tool-bar's height if not all tool-bar items are visible.
22721 It decreases a tool-bar's height when it would display blank lines
22722 otherwise. */);
22723 auto_resize_tool_bars_p = 1;
22724
22725 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
22726 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
22727 auto_raise_tool_bar_buttons_p = 1;
22728
22729 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
22730 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
22731 make_cursor_line_fully_visible_p = 1;
22732
22733 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
22734 doc: /* *Margin around tool-bar buttons in pixels.
22735 If an integer, use that for both horizontal and vertical margins.
22736 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
22737 HORZ specifying the horizontal margin, and VERT specifying the
22738 vertical margin. */);
22739 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
22740
22741 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
22742 doc: /* *Relief thickness of tool-bar buttons. */);
22743 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
22744
22745 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
22746 doc: /* List of functions to call to fontify regions of text.
22747 Each function is called with one argument POS. Functions must
22748 fontify a region starting at POS in the current buffer, and give
22749 fontified regions the property `fontified'. */);
22750 Vfontification_functions = Qnil;
22751 Fmake_variable_buffer_local (Qfontification_functions);
22752
22753 DEFVAR_BOOL ("unibyte-display-via-language-environment",
22754 &unibyte_display_via_language_environment,
22755 doc: /* *Non-nil means display unibyte text according to language environment.
22756 Specifically this means that unibyte non-ASCII characters
22757 are displayed by converting them to the equivalent multibyte characters
22758 according to the current language environment. As a result, they are
22759 displayed according to the current fontset. */);
22760 unibyte_display_via_language_environment = 0;
22761
22762 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
22763 doc: /* *Maximum height for resizing mini-windows.
22764 If a float, it specifies a fraction of the mini-window frame's height.
22765 If an integer, it specifies a number of lines. */);
22766 Vmax_mini_window_height = make_float (0.25);
22767
22768 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
22769 doc: /* *How to resize mini-windows.
22770 A value of nil means don't automatically resize mini-windows.
22771 A value of t means resize them to fit the text displayed in them.
22772 A value of `grow-only', the default, means let mini-windows grow
22773 only, until their display becomes empty, at which point the windows
22774 go back to their normal size. */);
22775 Vresize_mini_windows = Qgrow_only;
22776
22777 DEFVAR_LISP ("cursor-in-non-selected-windows",
22778 &Vcursor_in_non_selected_windows,
22779 doc: /* *Cursor type to display in non-selected windows.
22780 t means to use hollow box cursor. See `cursor-type' for other values. */);
22781 Vcursor_in_non_selected_windows = Qt;
22782
22783 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
22784 doc: /* Alist specifying how to blink the cursor off.
22785 Each element has the form (ON-STATE . OFF-STATE). Whenever the
22786 `cursor-type' frame-parameter or variable equals ON-STATE,
22787 comparing using `equal', Emacs uses OFF-STATE to specify
22788 how to blink it off. */);
22789 Vblink_cursor_alist = Qnil;
22790
22791 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
22792 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
22793 automatic_hscrolling_p = 1;
22794
22795 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
22796 doc: /* *How many columns away from the window edge point is allowed to get
22797 before automatic hscrolling will horizontally scroll the window. */);
22798 hscroll_margin = 5;
22799
22800 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
22801 doc: /* *How many columns to scroll the window when point gets too close to the edge.
22802 When point is less than `automatic-hscroll-margin' columns from the window
22803 edge, automatic hscrolling will scroll the window by the amount of columns
22804 determined by this variable. If its value is a positive integer, scroll that
22805 many columns. If it's a positive floating-point number, it specifies the
22806 fraction of the window's width to scroll. If it's nil or zero, point will be
22807 centered horizontally after the scroll. Any other value, including negative
22808 numbers, are treated as if the value were zero.
22809
22810 Automatic hscrolling always moves point outside the scroll margin, so if
22811 point was more than scroll step columns inside the margin, the window will
22812 scroll more than the value given by the scroll step.
22813
22814 Note that the lower bound for automatic hscrolling specified by `scroll-left'
22815 and `scroll-right' overrides this variable's effect. */);
22816 Vhscroll_step = make_number (0);
22817
22818 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
22819 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
22820 Bind this around calls to `message' to let it take effect. */);
22821 message_truncate_lines = 0;
22822
22823 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
22824 doc: /* Normal hook run to update the menu bar definitions.
22825 Redisplay runs this hook before it redisplays the menu bar.
22826 This is used to update submenus such as Buffers,
22827 whose contents depend on various data. */);
22828 Vmenu_bar_update_hook = Qnil;
22829
22830 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
22831 doc: /* Non-nil means don't update menu bars. Internal use only. */);
22832 inhibit_menubar_update = 0;
22833
22834 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
22835 doc: /* Non-nil means don't eval Lisp during redisplay. */);
22836 inhibit_eval_during_redisplay = 0;
22837
22838 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
22839 doc: /* Non-nil means don't free realized faces. Internal use only. */);
22840 inhibit_free_realized_faces = 0;
22841
22842 #if GLYPH_DEBUG
22843 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
22844 doc: /* Inhibit try_window_id display optimization. */);
22845 inhibit_try_window_id = 0;
22846
22847 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
22848 doc: /* Inhibit try_window_reusing display optimization. */);
22849 inhibit_try_window_reusing = 0;
22850
22851 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
22852 doc: /* Inhibit try_cursor_movement display optimization. */);
22853 inhibit_try_cursor_movement = 0;
22854 #endif /* GLYPH_DEBUG */
22855 }
22856
22857
22858 /* Initialize this module when Emacs starts. */
22859
22860 void
22861 init_xdisp ()
22862 {
22863 Lisp_Object root_window;
22864 struct window *mini_w;
22865
22866 current_header_line_height = current_mode_line_height = -1;
22867
22868 CHARPOS (this_line_start_pos) = 0;
22869
22870 mini_w = XWINDOW (minibuf_window);
22871 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
22872
22873 if (!noninteractive)
22874 {
22875 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
22876 int i;
22877
22878 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
22879 set_window_height (root_window,
22880 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
22881 0);
22882 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
22883 set_window_height (minibuf_window, 1, 0);
22884
22885 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
22886 mini_w->total_cols = make_number (FRAME_COLS (f));
22887
22888 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
22889 scratch_glyph_row.glyphs[TEXT_AREA + 1]
22890 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
22891
22892 /* The default ellipsis glyphs `...'. */
22893 for (i = 0; i < 3; ++i)
22894 default_invis_vector[i] = make_number ('.');
22895 }
22896
22897 {
22898 /* Allocate the buffer for frame titles.
22899 Also used for `format-mode-line'. */
22900 int size = 100;
22901 frame_title_buf = (char *) xmalloc (size);
22902 frame_title_buf_end = frame_title_buf + size;
22903 frame_title_ptr = NULL;
22904 }
22905
22906 help_echo_showing_p = 0;
22907 }
22908
22909
22910 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
22911 (do not change this comment) */