Update FSF's address.
[bpt/emacs.git] / 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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 Vnobreak_char_display;
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 /* Name and number of the face used to highlight non-breaking spaces. */
354
355 Lisp_Object Qnobreak_space;
356
357 /* The symbol `image' which is the car of the lists used to represent
358 images in Lisp. */
359
360 Lisp_Object Qimage;
361
362 /* The image map types. */
363 Lisp_Object QCmap, QCpointer;
364 Lisp_Object Qrect, Qcircle, Qpoly;
365
366 /* Non-zero means print newline to stdout before next mini-buffer
367 message. */
368
369 int noninteractive_need_newline;
370
371 /* Non-zero means print newline to message log before next message. */
372
373 static int message_log_need_newline;
374
375 /* Three markers that message_dolog uses.
376 It could allocate them itself, but that causes trouble
377 in handling memory-full errors. */
378 static Lisp_Object message_dolog_marker1;
379 static Lisp_Object message_dolog_marker2;
380 static Lisp_Object message_dolog_marker3;
381 \f
382 /* The buffer position of the first character appearing entirely or
383 partially on the line of the selected window which contains the
384 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
385 redisplay optimization in redisplay_internal. */
386
387 static struct text_pos this_line_start_pos;
388
389 /* Number of characters past the end of the line above, including the
390 terminating newline. */
391
392 static struct text_pos this_line_end_pos;
393
394 /* The vertical positions and the height of this line. */
395
396 static int this_line_vpos;
397 static int this_line_y;
398 static int this_line_pixel_height;
399
400 /* X position at which this display line starts. Usually zero;
401 negative if first character is partially visible. */
402
403 static int this_line_start_x;
404
405 /* Buffer that this_line_.* variables are referring to. */
406
407 static struct buffer *this_line_buffer;
408
409 /* Nonzero means truncate lines in all windows less wide than the
410 frame. */
411
412 int truncate_partial_width_windows;
413
414 /* A flag to control how to display unibyte 8-bit character. */
415
416 int unibyte_display_via_language_environment;
417
418 /* Nonzero means we have more than one non-mini-buffer-only frame.
419 Not guaranteed to be accurate except while parsing
420 frame-title-format. */
421
422 int multiple_frames;
423
424 Lisp_Object Vglobal_mode_string;
425
426
427 /* List of variables (symbols) which hold markers for overlay arrows.
428 The symbols on this list are examined during redisplay to determine
429 where to display overlay arrows. */
430
431 Lisp_Object Voverlay_arrow_variable_list;
432
433 /* Marker for where to display an arrow on top of the buffer text. */
434
435 Lisp_Object Voverlay_arrow_position;
436
437 /* String to display for the arrow. Only used on terminal frames. */
438
439 Lisp_Object Voverlay_arrow_string;
440
441 /* Values of those variables at last redisplay are stored as
442 properties on `overlay-arrow-position' symbol. However, if
443 Voverlay_arrow_position is a marker, last-arrow-position is its
444 numerical position. */
445
446 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
447
448 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
449 properties on a symbol in overlay-arrow-variable-list. */
450
451 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
452
453 /* Like mode-line-format, but for the title bar on a visible frame. */
454
455 Lisp_Object Vframe_title_format;
456
457 /* Like mode-line-format, but for the title bar on an iconified frame. */
458
459 Lisp_Object Vicon_title_format;
460
461 /* List of functions to call when a window's size changes. These
462 functions get one arg, a frame on which one or more windows' sizes
463 have changed. */
464
465 static Lisp_Object Vwindow_size_change_functions;
466
467 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
468
469 /* Nonzero if an overlay arrow has been displayed in this window. */
470
471 static int overlay_arrow_seen;
472
473 /* Nonzero means highlight the region even in nonselected windows. */
474
475 int highlight_nonselected_windows;
476
477 /* If cursor motion alone moves point off frame, try scrolling this
478 many lines up or down if that will bring it back. */
479
480 static EMACS_INT scroll_step;
481
482 /* Nonzero means scroll just far enough to bring point back on the
483 screen, when appropriate. */
484
485 static EMACS_INT scroll_conservatively;
486
487 /* Recenter the window whenever point gets within this many lines of
488 the top or bottom of the window. This value is translated into a
489 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
490 that there is really a fixed pixel height scroll margin. */
491
492 EMACS_INT scroll_margin;
493
494 /* Number of windows showing the buffer of the selected window (or
495 another buffer with the same base buffer). keyboard.c refers to
496 this. */
497
498 int buffer_shared;
499
500 /* Vector containing glyphs for an ellipsis `...'. */
501
502 static Lisp_Object default_invis_vector[3];
503
504 /* Zero means display the mode-line/header-line/menu-bar in the default face
505 (this slightly odd definition is for compatibility with previous versions
506 of emacs), non-zero means display them using their respective faces.
507
508 This variable is deprecated. */
509
510 int mode_line_inverse_video;
511
512 /* Prompt to display in front of the mini-buffer contents. */
513
514 Lisp_Object minibuf_prompt;
515
516 /* Width of current mini-buffer prompt. Only set after display_line
517 of the line that contains the prompt. */
518
519 int minibuf_prompt_width;
520
521 /* This is the window where the echo area message was displayed. It
522 is always a mini-buffer window, but it may not be the same window
523 currently active as a mini-buffer. */
524
525 Lisp_Object echo_area_window;
526
527 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
528 pushes the current message and the value of
529 message_enable_multibyte on the stack, the function restore_message
530 pops the stack and displays MESSAGE again. */
531
532 Lisp_Object Vmessage_stack;
533
534 /* Nonzero means multibyte characters were enabled when the echo area
535 message was specified. */
536
537 int message_enable_multibyte;
538
539 /* Nonzero if we should redraw the mode lines on the next redisplay. */
540
541 int update_mode_lines;
542
543 /* Nonzero if window sizes or contents have changed since last
544 redisplay that finished. */
545
546 int windows_or_buffers_changed;
547
548 /* Nonzero means a frame's cursor type has been changed. */
549
550 int cursor_type_changed;
551
552 /* Nonzero after display_mode_line if %l was used and it displayed a
553 line number. */
554
555 int line_number_displayed;
556
557 /* Maximum buffer size for which to display line numbers. */
558
559 Lisp_Object Vline_number_display_limit;
560
561 /* Line width to consider when repositioning for line number display. */
562
563 static EMACS_INT line_number_display_limit_width;
564
565 /* Number of lines to keep in the message log buffer. t means
566 infinite. nil means don't log at all. */
567
568 Lisp_Object Vmessage_log_max;
569
570 /* The name of the *Messages* buffer, a string. */
571
572 static Lisp_Object Vmessages_buffer_name;
573
574 /* Index 0 is the buffer that holds the current (desired) echo area message,
575 or nil if none is desired right now.
576
577 Index 1 is the buffer that holds the previously displayed echo area message,
578 or nil to indicate no message. This is normally what's on the screen now.
579
580 These two can point to the same buffer. That happens when the last
581 message output by the user (or made by echoing) has been displayed. */
582
583 Lisp_Object echo_area_buffer[2];
584
585 /* Permanent pointers to the two buffers that are used for echo area
586 purposes. Once the two buffers are made, and their pointers are
587 placed here, these two slots remain unchanged unless those buffers
588 need to be created afresh. */
589
590 static Lisp_Object echo_buffer[2];
591
592 /* A vector saved used in with_area_buffer to reduce consing. */
593
594 static Lisp_Object Vwith_echo_area_save_vector;
595
596 /* Non-zero means display_echo_area should display the last echo area
597 message again. Set by redisplay_preserve_echo_area. */
598
599 static int display_last_displayed_message_p;
600
601 /* Nonzero if echo area is being used by print; zero if being used by
602 message. */
603
604 int message_buf_print;
605
606 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
607
608 Lisp_Object Qinhibit_menubar_update;
609 int inhibit_menubar_update;
610
611 /* Maximum height for resizing mini-windows. Either a float
612 specifying a fraction of the available height, or an integer
613 specifying a number of lines. */
614
615 Lisp_Object Vmax_mini_window_height;
616
617 /* Non-zero means messages should be displayed with truncated
618 lines instead of being continued. */
619
620 int message_truncate_lines;
621 Lisp_Object Qmessage_truncate_lines;
622
623 /* Set to 1 in clear_message to make redisplay_internal aware
624 of an emptied echo area. */
625
626 static int message_cleared_p;
627
628 /* How to blink the default frame cursor off. */
629 Lisp_Object Vblink_cursor_alist;
630
631 /* A scratch glyph row with contents used for generating truncation
632 glyphs. Also used in direct_output_for_insert. */
633
634 #define MAX_SCRATCH_GLYPHS 100
635 struct glyph_row scratch_glyph_row;
636 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
637
638 /* Ascent and height of the last line processed by move_it_to. */
639
640 static int last_max_ascent, last_height;
641
642 /* Non-zero if there's a help-echo in the echo area. */
643
644 int help_echo_showing_p;
645
646 /* If >= 0, computed, exact values of mode-line and header-line height
647 to use in the macros CURRENT_MODE_LINE_HEIGHT and
648 CURRENT_HEADER_LINE_HEIGHT. */
649
650 int current_mode_line_height, current_header_line_height;
651
652 /* The maximum distance to look ahead for text properties. Values
653 that are too small let us call compute_char_face and similar
654 functions too often which is expensive. Values that are too large
655 let us call compute_char_face and alike too often because we
656 might not be interested in text properties that far away. */
657
658 #define TEXT_PROP_DISTANCE_LIMIT 100
659
660 #if GLYPH_DEBUG
661
662 /* Variables to turn off display optimizations from Lisp. */
663
664 int inhibit_try_window_id, inhibit_try_window_reusing;
665 int inhibit_try_cursor_movement;
666
667 /* Non-zero means print traces of redisplay if compiled with
668 GLYPH_DEBUG != 0. */
669
670 int trace_redisplay_p;
671
672 #endif /* GLYPH_DEBUG */
673
674 #ifdef DEBUG_TRACE_MOVE
675 /* Non-zero means trace with TRACE_MOVE to stderr. */
676 int trace_move;
677
678 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
679 #else
680 #define TRACE_MOVE(x) (void) 0
681 #endif
682
683 /* Non-zero means automatically scroll windows horizontally to make
684 point visible. */
685
686 int automatic_hscrolling_p;
687
688 /* How close to the margin can point get before the window is scrolled
689 horizontally. */
690 EMACS_INT hscroll_margin;
691
692 /* How much to scroll horizontally when point is inside the above margin. */
693 Lisp_Object Vhscroll_step;
694
695 /* The variable `resize-mini-windows'. If nil, don't resize
696 mini-windows. If t, always resize them to fit the text they
697 display. If `grow-only', let mini-windows grow only until they
698 become empty. */
699
700 Lisp_Object Vresize_mini_windows;
701
702 /* Buffer being redisplayed -- for redisplay_window_error. */
703
704 struct buffer *displayed_buffer;
705
706 /* Value returned from text property handlers (see below). */
707
708 enum prop_handled
709 {
710 HANDLED_NORMALLY,
711 HANDLED_RECOMPUTE_PROPS,
712 HANDLED_OVERLAY_STRING_CONSUMED,
713 HANDLED_RETURN
714 };
715
716 /* A description of text properties that redisplay is interested
717 in. */
718
719 struct props
720 {
721 /* The name of the property. */
722 Lisp_Object *name;
723
724 /* A unique index for the property. */
725 enum prop_idx idx;
726
727 /* A handler function called to set up iterator IT from the property
728 at IT's current position. Value is used to steer handle_stop. */
729 enum prop_handled (*handler) P_ ((struct it *it));
730 };
731
732 static enum prop_handled handle_face_prop P_ ((struct it *));
733 static enum prop_handled handle_invisible_prop P_ ((struct it *));
734 static enum prop_handled handle_display_prop P_ ((struct it *));
735 static enum prop_handled handle_composition_prop P_ ((struct it *));
736 static enum prop_handled handle_overlay_change P_ ((struct it *));
737 static enum prop_handled handle_fontified_prop P_ ((struct it *));
738
739 /* Properties handled by iterators. */
740
741 static struct props it_props[] =
742 {
743 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
744 /* Handle `face' before `display' because some sub-properties of
745 `display' need to know the face. */
746 {&Qface, FACE_PROP_IDX, handle_face_prop},
747 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
748 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
749 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
750 {NULL, 0, NULL}
751 };
752
753 /* Value is the position described by X. If X is a marker, value is
754 the marker_position of X. Otherwise, value is X. */
755
756 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
757
758 /* Enumeration returned by some move_it_.* functions internally. */
759
760 enum move_it_result
761 {
762 /* Not used. Undefined value. */
763 MOVE_UNDEFINED,
764
765 /* Move ended at the requested buffer position or ZV. */
766 MOVE_POS_MATCH_OR_ZV,
767
768 /* Move ended at the requested X pixel position. */
769 MOVE_X_REACHED,
770
771 /* Move within a line ended at the end of a line that must be
772 continued. */
773 MOVE_LINE_CONTINUED,
774
775 /* Move within a line ended at the end of a line that would
776 be displayed truncated. */
777 MOVE_LINE_TRUNCATED,
778
779 /* Move within a line ended at a line end. */
780 MOVE_NEWLINE_OR_CR
781 };
782
783 /* This counter is used to clear the face cache every once in a while
784 in redisplay_internal. It is incremented for each redisplay.
785 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
786 cleared. */
787
788 #define CLEAR_FACE_CACHE_COUNT 500
789 static int clear_face_cache_count;
790
791 /* Similarly for the image cache. */
792
793 #ifdef HAVE_WINDOW_SYSTEM
794 #define CLEAR_IMAGE_CACHE_COUNT 101
795 static int clear_image_cache_count;
796 #endif
797
798 /* Record the previous terminal frame we displayed. */
799
800 static struct frame *previous_terminal_frame;
801
802 /* Non-zero while redisplay_internal is in progress. */
803
804 int redisplaying_p;
805
806 /* Non-zero means don't free realized faces. Bound while freeing
807 realized faces is dangerous because glyph matrices might still
808 reference them. */
809
810 int inhibit_free_realized_faces;
811 Lisp_Object Qinhibit_free_realized_faces;
812
813 /* If a string, XTread_socket generates an event to display that string.
814 (The display is done in read_char.) */
815
816 Lisp_Object help_echo_string;
817 Lisp_Object help_echo_window;
818 Lisp_Object help_echo_object;
819 int help_echo_pos;
820
821 /* Temporary variable for XTread_socket. */
822
823 Lisp_Object previous_help_echo_string;
824
825 /* Null glyph slice */
826
827 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
828
829 \f
830 /* Function prototypes. */
831
832 static void setup_for_ellipsis P_ ((struct it *, int));
833 static void mark_window_display_accurate_1 P_ ((struct window *, int));
834 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
835 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
836 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
837 static int redisplay_mode_lines P_ ((Lisp_Object, int));
838 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
839
840 #if 0
841 static int invisible_text_between_p P_ ((struct it *, int, int));
842 #endif
843
844 static void pint2str P_ ((char *, int, int));
845 static void pint2hrstr P_ ((char *, int, int));
846 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
847 struct text_pos));
848 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
849 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
850 static void store_mode_line_noprop_char P_ ((char));
851 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
852 static void x_consider_frame_title P_ ((Lisp_Object));
853 static void handle_stop P_ ((struct it *));
854 static int tool_bar_lines_needed P_ ((struct frame *));
855 static int single_display_spec_intangible_p P_ ((Lisp_Object));
856 static void ensure_echo_area_buffers P_ ((void));
857 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
858 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
859 static int with_echo_area_buffer P_ ((struct window *, int,
860 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
861 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
862 static void clear_garbaged_frames P_ ((void));
863 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
864 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
865 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
866 static int display_echo_area P_ ((struct window *));
867 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
868 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
869 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
870 static int string_char_and_length P_ ((const unsigned char *, int, int *));
871 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
872 struct text_pos));
873 static int compute_window_start_on_continuation_line P_ ((struct window *));
874 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
875 static void insert_left_trunc_glyphs P_ ((struct it *));
876 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
877 Lisp_Object));
878 static void extend_face_to_end_of_line P_ ((struct it *));
879 static int append_space_for_newline P_ ((struct it *, int));
880 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
881 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
882 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
883 static int trailing_whitespace_p P_ ((int));
884 static int message_log_check_duplicate P_ ((int, int, int, int));
885 static void push_it P_ ((struct it *));
886 static void pop_it P_ ((struct it *));
887 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
888 static void select_frame_for_redisplay P_ ((Lisp_Object));
889 static void redisplay_internal P_ ((int));
890 static int echo_area_display P_ ((int));
891 static void redisplay_windows P_ ((Lisp_Object));
892 static void redisplay_window P_ ((Lisp_Object, int));
893 static Lisp_Object redisplay_window_error ();
894 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
895 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
896 static void update_menu_bar P_ ((struct frame *, int));
897 static int try_window_reusing_current_matrix P_ ((struct window *));
898 static int try_window_id P_ ((struct window *));
899 static int display_line P_ ((struct it *));
900 static int display_mode_lines P_ ((struct window *));
901 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
902 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
903 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
904 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
905 static void display_menu_bar P_ ((struct window *));
906 static int display_count_lines P_ ((int, int, int, int, int *));
907 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
908 int, int, struct it *, int, int, int, int));
909 static void compute_line_metrics P_ ((struct it *));
910 static void run_redisplay_end_trigger_hook P_ ((struct it *));
911 static int get_overlay_strings P_ ((struct it *, int));
912 static void next_overlay_string P_ ((struct it *));
913 static void reseat P_ ((struct it *, struct text_pos, int));
914 static void reseat_1 P_ ((struct it *, struct text_pos, int));
915 static void back_to_previous_visible_line_start P_ ((struct it *));
916 void reseat_at_previous_visible_line_start P_ ((struct it *));
917 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
918 static int next_element_from_ellipsis P_ ((struct it *));
919 static int next_element_from_display_vector P_ ((struct it *));
920 static int next_element_from_string P_ ((struct it *));
921 static int next_element_from_c_string P_ ((struct it *));
922 static int next_element_from_buffer P_ ((struct it *));
923 static int next_element_from_composition P_ ((struct it *));
924 static int next_element_from_image P_ ((struct it *));
925 static int next_element_from_stretch P_ ((struct it *));
926 static void load_overlay_strings P_ ((struct it *, int));
927 static int init_from_display_pos P_ ((struct it *, struct window *,
928 struct display_pos *));
929 static void reseat_to_string P_ ((struct it *, unsigned char *,
930 Lisp_Object, int, int, int, int));
931 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
932 int, int, int));
933 void move_it_vertically_backward P_ ((struct it *, int));
934 static void init_to_row_start P_ ((struct it *, struct window *,
935 struct glyph_row *));
936 static int init_to_row_end P_ ((struct it *, struct window *,
937 struct glyph_row *));
938 static void back_to_previous_line_start P_ ((struct it *));
939 static int forward_to_next_line_start P_ ((struct it *, int *));
940 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
941 Lisp_Object, int));
942 static struct text_pos string_pos P_ ((int, Lisp_Object));
943 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
944 static int number_of_chars P_ ((unsigned char *, int));
945 static void compute_stop_pos P_ ((struct it *));
946 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
947 Lisp_Object));
948 static int face_before_or_after_it_pos P_ ((struct it *, int));
949 static int next_overlay_change P_ ((int));
950 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
951 Lisp_Object, struct text_pos *,
952 int));
953 static int underlying_face_id P_ ((struct it *));
954 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
955 struct window *));
956
957 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
958 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
959
960 #ifdef HAVE_WINDOW_SYSTEM
961
962 static void update_tool_bar P_ ((struct frame *, int));
963 static void build_desired_tool_bar_string P_ ((struct frame *f));
964 static int redisplay_tool_bar P_ ((struct frame *));
965 static void display_tool_bar_line P_ ((struct it *));
966 static void notice_overwritten_cursor P_ ((struct window *,
967 enum glyph_row_area,
968 int, int, int, int));
969
970
971
972 #endif /* HAVE_WINDOW_SYSTEM */
973
974 \f
975 /***********************************************************************
976 Window display dimensions
977 ***********************************************************************/
978
979 /* Return the bottom boundary y-position for text lines in window W.
980 This is the first y position at which a line cannot start.
981 It is relative to the top of the window.
982
983 This is the height of W minus the height of a mode line, if any. */
984
985 INLINE int
986 window_text_bottom_y (w)
987 struct window *w;
988 {
989 int height = WINDOW_TOTAL_HEIGHT (w);
990
991 if (WINDOW_WANTS_MODELINE_P (w))
992 height -= CURRENT_MODE_LINE_HEIGHT (w);
993 return height;
994 }
995
996 /* Return the pixel width of display area AREA of window W. AREA < 0
997 means return the total width of W, not including fringes to
998 the left and right of the window. */
999
1000 INLINE int
1001 window_box_width (w, area)
1002 struct window *w;
1003 int area;
1004 {
1005 int cols = XFASTINT (w->total_cols);
1006 int pixels = 0;
1007
1008 if (!w->pseudo_window_p)
1009 {
1010 cols -= WINDOW_SCROLL_BAR_COLS (w);
1011
1012 if (area == TEXT_AREA)
1013 {
1014 if (INTEGERP (w->left_margin_cols))
1015 cols -= XFASTINT (w->left_margin_cols);
1016 if (INTEGERP (w->right_margin_cols))
1017 cols -= XFASTINT (w->right_margin_cols);
1018 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1019 }
1020 else if (area == LEFT_MARGIN_AREA)
1021 {
1022 cols = (INTEGERP (w->left_margin_cols)
1023 ? XFASTINT (w->left_margin_cols) : 0);
1024 pixels = 0;
1025 }
1026 else if (area == RIGHT_MARGIN_AREA)
1027 {
1028 cols = (INTEGERP (w->right_margin_cols)
1029 ? XFASTINT (w->right_margin_cols) : 0);
1030 pixels = 0;
1031 }
1032 }
1033
1034 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1035 }
1036
1037
1038 /* Return the pixel height of the display area of window W, not
1039 including mode lines of W, if any. */
1040
1041 INLINE int
1042 window_box_height (w)
1043 struct window *w;
1044 {
1045 struct frame *f = XFRAME (w->frame);
1046 int height = WINDOW_TOTAL_HEIGHT (w);
1047
1048 xassert (height >= 0);
1049
1050 /* Note: the code below that determines the mode-line/header-line
1051 height is essentially the same as that contained in the macro
1052 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1053 the appropriate glyph row has its `mode_line_p' flag set,
1054 and if it doesn't, uses estimate_mode_line_height instead. */
1055
1056 if (WINDOW_WANTS_MODELINE_P (w))
1057 {
1058 struct glyph_row *ml_row
1059 = (w->current_matrix && w->current_matrix->rows
1060 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1061 : 0);
1062 if (ml_row && ml_row->mode_line_p)
1063 height -= ml_row->height;
1064 else
1065 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1066 }
1067
1068 if (WINDOW_WANTS_HEADER_LINE_P (w))
1069 {
1070 struct glyph_row *hl_row
1071 = (w->current_matrix && w->current_matrix->rows
1072 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1073 : 0);
1074 if (hl_row && hl_row->mode_line_p)
1075 height -= hl_row->height;
1076 else
1077 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1078 }
1079
1080 /* With a very small font and a mode-line that's taller than
1081 default, we might end up with a negative height. */
1082 return max (0, height);
1083 }
1084
1085 /* Return the window-relative coordinate of the left edge of display
1086 area AREA of window W. AREA < 0 means return the left edge of the
1087 whole window, to the right of the left fringe of W. */
1088
1089 INLINE int
1090 window_box_left_offset (w, area)
1091 struct window *w;
1092 int area;
1093 {
1094 int x;
1095
1096 if (w->pseudo_window_p)
1097 return 0;
1098
1099 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1100
1101 if (area == TEXT_AREA)
1102 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1103 + window_box_width (w, LEFT_MARGIN_AREA));
1104 else if (area == RIGHT_MARGIN_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA)
1107 + window_box_width (w, TEXT_AREA)
1108 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1109 ? 0
1110 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1111 else if (area == LEFT_MARGIN_AREA
1112 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1113 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1114
1115 return x;
1116 }
1117
1118
1119 /* Return the window-relative coordinate of the right edge of display
1120 area AREA of window W. AREA < 0 means return the left edge of the
1121 whole window, to the left of the right fringe of W. */
1122
1123 INLINE int
1124 window_box_right_offset (w, area)
1125 struct window *w;
1126 int area;
1127 {
1128 return window_box_left_offset (w, area) + window_box_width (w, area);
1129 }
1130
1131 /* Return the frame-relative coordinate of the left edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the right of the left fringe of W. */
1134
1135 INLINE int
1136 window_box_left (w, area)
1137 struct window *w;
1138 int area;
1139 {
1140 struct frame *f = XFRAME (w->frame);
1141 int x;
1142
1143 if (w->pseudo_window_p)
1144 return FRAME_INTERNAL_BORDER_WIDTH (f);
1145
1146 x = (WINDOW_LEFT_EDGE_X (w)
1147 + window_box_left_offset (w, area));
1148
1149 return x;
1150 }
1151
1152
1153 /* Return the frame-relative coordinate of the right edge of display
1154 area AREA of window W. AREA < 0 means return the left edge of the
1155 whole window, to the left of the right fringe of W. */
1156
1157 INLINE int
1158 window_box_right (w, area)
1159 struct window *w;
1160 int area;
1161 {
1162 return window_box_left (w, area) + window_box_width (w, area);
1163 }
1164
1165 /* Get the bounding box of the display area AREA of window W, without
1166 mode lines, in frame-relative coordinates. AREA < 0 means the
1167 whole window, not including the left and right fringes of
1168 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1169 coordinates of the upper-left corner of the box. Return in
1170 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1171
1172 INLINE void
1173 window_box (w, area, box_x, box_y, box_width, box_height)
1174 struct window *w;
1175 int area;
1176 int *box_x, *box_y, *box_width, *box_height;
1177 {
1178 if (box_width)
1179 *box_width = window_box_width (w, area);
1180 if (box_height)
1181 *box_height = window_box_height (w);
1182 if (box_x)
1183 *box_x = window_box_left (w, area);
1184 if (box_y)
1185 {
1186 *box_y = WINDOW_TOP_EDGE_Y (w);
1187 if (WINDOW_WANTS_HEADER_LINE_P (w))
1188 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1189 }
1190 }
1191
1192
1193 /* Get the bounding box of the display area AREA of window W, without
1194 mode lines. AREA < 0 means the whole window, not including the
1195 left and right fringe of the window. Return in *TOP_LEFT_X
1196 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1197 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1198 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1199 box. */
1200
1201 INLINE void
1202 window_box_edges (w, area, top_left_x, top_left_y,
1203 bottom_right_x, bottom_right_y)
1204 struct window *w;
1205 int area;
1206 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1207 {
1208 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1209 bottom_right_y);
1210 *bottom_right_x += *top_left_x;
1211 *bottom_right_y += *top_left_y;
1212 }
1213
1214
1215 \f
1216 /***********************************************************************
1217 Utilities
1218 ***********************************************************************/
1219
1220 /* Return the bottom y-position of the line the iterator IT is in.
1221 This can modify IT's settings. */
1222
1223 int
1224 line_bottom_y (it)
1225 struct it *it;
1226 {
1227 int line_height = it->max_ascent + it->max_descent;
1228 int line_top_y = it->current_y;
1229
1230 if (line_height == 0)
1231 {
1232 if (last_height)
1233 line_height = last_height;
1234 else if (IT_CHARPOS (*it) < ZV)
1235 {
1236 move_it_by_lines (it, 1, 1);
1237 line_height = (it->max_ascent || it->max_descent
1238 ? it->max_ascent + it->max_descent
1239 : last_height);
1240 }
1241 else
1242 {
1243 struct glyph_row *row = it->glyph_row;
1244
1245 /* Use the default character height. */
1246 it->glyph_row = NULL;
1247 it->what = IT_CHARACTER;
1248 it->c = ' ';
1249 it->len = 1;
1250 PRODUCE_GLYPHS (it);
1251 line_height = it->ascent + it->descent;
1252 it->glyph_row = row;
1253 }
1254 }
1255
1256 return line_top_y + line_height;
1257 }
1258
1259
1260 /* Return 1 if position CHARPOS is visible in window W.
1261 If visible, set *X and *Y to pixel coordinates of top left corner.
1262 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1263 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1264 and header-lines heights. */
1265
1266 int
1267 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1268 struct window *w;
1269 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1270 {
1271 struct it it;
1272 struct text_pos top;
1273 int visible_p = 0;
1274 struct buffer *old_buffer = NULL;
1275
1276 if (noninteractive)
1277 return visible_p;
1278
1279 if (XBUFFER (w->buffer) != current_buffer)
1280 {
1281 old_buffer = current_buffer;
1282 set_buffer_internal_1 (XBUFFER (w->buffer));
1283 }
1284
1285 SET_TEXT_POS_FROM_MARKER (top, w->start);
1286
1287 /* Compute exact mode line heights, if requested. */
1288 if (exact_mode_line_heights_p)
1289 {
1290 if (WINDOW_WANTS_MODELINE_P (w))
1291 current_mode_line_height
1292 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1293 current_buffer->mode_line_format);
1294
1295 if (WINDOW_WANTS_HEADER_LINE_P (w))
1296 current_header_line_height
1297 = display_mode_line (w, HEADER_LINE_FACE_ID,
1298 current_buffer->header_line_format);
1299 }
1300
1301 start_display (&it, w, top);
1302 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1303 MOVE_TO_POS | MOVE_TO_Y);
1304
1305 /* Note that we may overshoot because of invisible text. */
1306 if (IT_CHARPOS (it) >= charpos)
1307 {
1308 int top_x = it.current_x;
1309 int top_y = it.current_y;
1310 int bottom_y = (last_height = 0, line_bottom_y (&it));
1311 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1312
1313 if (top_y < window_top_y)
1314 visible_p = bottom_y > window_top_y;
1315 else if (top_y < it.last_visible_y)
1316 visible_p = 1;
1317 if (visible_p)
1318 {
1319 *x = top_x;
1320 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1321 *rtop = max (0, window_top_y - top_y);
1322 *rbot = max (0, bottom_y - it.last_visible_y);
1323 }
1324 }
1325 else
1326 {
1327 struct it it2;
1328
1329 it2 = it;
1330 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1331 move_it_by_lines (&it, 1, 0);
1332 if (charpos < IT_CHARPOS (it))
1333 {
1334 visible_p = 1;
1335 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1336 *x = it2.current_x;
1337 *y = it2.current_y + it2.max_ascent - it2.ascent;
1338 *rtop = max (0, -it2.current_y);
1339 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1340 - it.last_visible_y));
1341 }
1342 }
1343
1344 if (old_buffer)
1345 set_buffer_internal_1 (old_buffer);
1346
1347 current_header_line_height = current_mode_line_height = -1;
1348
1349 return visible_p;
1350 }
1351
1352
1353 /* Return the next character from STR which is MAXLEN bytes long.
1354 Return in *LEN the length of the character. This is like
1355 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1356 we find one, we return a `?', but with the length of the invalid
1357 character. */
1358
1359 static INLINE int
1360 string_char_and_length (str, maxlen, len)
1361 const unsigned char *str;
1362 int maxlen, *len;
1363 {
1364 int c;
1365
1366 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1367 if (!CHAR_VALID_P (c, 1))
1368 /* We may not change the length here because other places in Emacs
1369 don't use this function, i.e. they silently accept invalid
1370 characters. */
1371 c = '?';
1372
1373 return c;
1374 }
1375
1376
1377
1378 /* Given a position POS containing a valid character and byte position
1379 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1380
1381 static struct text_pos
1382 string_pos_nchars_ahead (pos, string, nchars)
1383 struct text_pos pos;
1384 Lisp_Object string;
1385 int nchars;
1386 {
1387 xassert (STRINGP (string) && nchars >= 0);
1388
1389 if (STRING_MULTIBYTE (string))
1390 {
1391 int rest = SBYTES (string) - BYTEPOS (pos);
1392 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1393 int len;
1394
1395 while (nchars--)
1396 {
1397 string_char_and_length (p, rest, &len);
1398 p += len, rest -= len;
1399 xassert (rest >= 0);
1400 CHARPOS (pos) += 1;
1401 BYTEPOS (pos) += len;
1402 }
1403 }
1404 else
1405 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1406
1407 return pos;
1408 }
1409
1410
1411 /* Value is the text position, i.e. character and byte position,
1412 for character position CHARPOS in STRING. */
1413
1414 static INLINE struct text_pos
1415 string_pos (charpos, string)
1416 int charpos;
1417 Lisp_Object string;
1418 {
1419 struct text_pos pos;
1420 xassert (STRINGP (string));
1421 xassert (charpos >= 0);
1422 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1423 return pos;
1424 }
1425
1426
1427 /* Value is a text position, i.e. character and byte position, for
1428 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1429 means recognize multibyte characters. */
1430
1431 static struct text_pos
1432 c_string_pos (charpos, s, multibyte_p)
1433 int charpos;
1434 unsigned char *s;
1435 int multibyte_p;
1436 {
1437 struct text_pos pos;
1438
1439 xassert (s != NULL);
1440 xassert (charpos >= 0);
1441
1442 if (multibyte_p)
1443 {
1444 int rest = strlen (s), len;
1445
1446 SET_TEXT_POS (pos, 0, 0);
1447 while (charpos--)
1448 {
1449 string_char_and_length (s, rest, &len);
1450 s += len, rest -= len;
1451 xassert (rest >= 0);
1452 CHARPOS (pos) += 1;
1453 BYTEPOS (pos) += len;
1454 }
1455 }
1456 else
1457 SET_TEXT_POS (pos, charpos, charpos);
1458
1459 return pos;
1460 }
1461
1462
1463 /* Value is the number of characters in C string S. MULTIBYTE_P
1464 non-zero means recognize multibyte characters. */
1465
1466 static int
1467 number_of_chars (s, multibyte_p)
1468 unsigned char *s;
1469 int multibyte_p;
1470 {
1471 int nchars;
1472
1473 if (multibyte_p)
1474 {
1475 int rest = strlen (s), len;
1476 unsigned char *p = (unsigned char *) s;
1477
1478 for (nchars = 0; rest > 0; ++nchars)
1479 {
1480 string_char_and_length (p, rest, &len);
1481 rest -= len, p += len;
1482 }
1483 }
1484 else
1485 nchars = strlen (s);
1486
1487 return nchars;
1488 }
1489
1490
1491 /* Compute byte position NEWPOS->bytepos corresponding to
1492 NEWPOS->charpos. POS is a known position in string STRING.
1493 NEWPOS->charpos must be >= POS.charpos. */
1494
1495 static void
1496 compute_string_pos (newpos, pos, string)
1497 struct text_pos *newpos, pos;
1498 Lisp_Object string;
1499 {
1500 xassert (STRINGP (string));
1501 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1502
1503 if (STRING_MULTIBYTE (string))
1504 *newpos = string_pos_nchars_ahead (pos, string,
1505 CHARPOS (*newpos) - CHARPOS (pos));
1506 else
1507 BYTEPOS (*newpos) = CHARPOS (*newpos);
1508 }
1509
1510 /* EXPORT:
1511 Return an estimation of the pixel height of mode or top lines on
1512 frame F. FACE_ID specifies what line's height to estimate. */
1513
1514 int
1515 estimate_mode_line_height (f, face_id)
1516 struct frame *f;
1517 enum face_id face_id;
1518 {
1519 #ifdef HAVE_WINDOW_SYSTEM
1520 if (FRAME_WINDOW_P (f))
1521 {
1522 int height = FONT_HEIGHT (FRAME_FONT (f));
1523
1524 /* This function is called so early when Emacs starts that the face
1525 cache and mode line face are not yet initialized. */
1526 if (FRAME_FACE_CACHE (f))
1527 {
1528 struct face *face = FACE_FROM_ID (f, face_id);
1529 if (face)
1530 {
1531 if (face->font)
1532 height = FONT_HEIGHT (face->font);
1533 if (face->box_line_width > 0)
1534 height += 2 * face->box_line_width;
1535 }
1536 }
1537
1538 return height;
1539 }
1540 #endif
1541
1542 return 1;
1543 }
1544
1545 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1546 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1547 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1548 not force the value into range. */
1549
1550 void
1551 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1552 FRAME_PTR f;
1553 register int pix_x, pix_y;
1554 int *x, *y;
1555 NativeRectangle *bounds;
1556 int noclip;
1557 {
1558
1559 #ifdef HAVE_WINDOW_SYSTEM
1560 if (FRAME_WINDOW_P (f))
1561 {
1562 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1563 even for negative values. */
1564 if (pix_x < 0)
1565 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1566 if (pix_y < 0)
1567 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1568
1569 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1570 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1571
1572 if (bounds)
1573 STORE_NATIVE_RECT (*bounds,
1574 FRAME_COL_TO_PIXEL_X (f, pix_x),
1575 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1576 FRAME_COLUMN_WIDTH (f) - 1,
1577 FRAME_LINE_HEIGHT (f) - 1);
1578
1579 if (!noclip)
1580 {
1581 if (pix_x < 0)
1582 pix_x = 0;
1583 else if (pix_x > FRAME_TOTAL_COLS (f))
1584 pix_x = FRAME_TOTAL_COLS (f);
1585
1586 if (pix_y < 0)
1587 pix_y = 0;
1588 else if (pix_y > FRAME_LINES (f))
1589 pix_y = FRAME_LINES (f);
1590 }
1591 }
1592 #endif
1593
1594 *x = pix_x;
1595 *y = pix_y;
1596 }
1597
1598
1599 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1600 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1601 can't tell the positions because W's display is not up to date,
1602 return 0. */
1603
1604 int
1605 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1606 struct window *w;
1607 int hpos, vpos;
1608 int *frame_x, *frame_y;
1609 {
1610 #ifdef HAVE_WINDOW_SYSTEM
1611 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1612 {
1613 int success_p;
1614
1615 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1616 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1617
1618 if (display_completed)
1619 {
1620 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1621 struct glyph *glyph = row->glyphs[TEXT_AREA];
1622 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1623
1624 hpos = row->x;
1625 vpos = row->y;
1626 while (glyph < end)
1627 {
1628 hpos += glyph->pixel_width;
1629 ++glyph;
1630 }
1631
1632 /* If first glyph is partially visible, its first visible position is still 0. */
1633 if (hpos < 0)
1634 hpos = 0;
1635
1636 success_p = 1;
1637 }
1638 else
1639 {
1640 hpos = vpos = 0;
1641 success_p = 0;
1642 }
1643
1644 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1645 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1646 return success_p;
1647 }
1648 #endif
1649
1650 *frame_x = hpos;
1651 *frame_y = vpos;
1652 return 1;
1653 }
1654
1655
1656 #ifdef HAVE_WINDOW_SYSTEM
1657
1658 /* Find the glyph under window-relative coordinates X/Y in window W.
1659 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1660 strings. Return in *HPOS and *VPOS the row and column number of
1661 the glyph found. Return in *AREA the glyph area containing X.
1662 Value is a pointer to the glyph found or null if X/Y is not on
1663 text, or we can't tell because W's current matrix is not up to
1664 date. */
1665
1666 static struct glyph *
1667 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1668 struct window *w;
1669 int x, y;
1670 int *hpos, *vpos, *dx, *dy, *area;
1671 {
1672 struct glyph *glyph, *end;
1673 struct glyph_row *row = NULL;
1674 int x0, i;
1675
1676 /* Find row containing Y. Give up if some row is not enabled. */
1677 for (i = 0; i < w->current_matrix->nrows; ++i)
1678 {
1679 row = MATRIX_ROW (w->current_matrix, i);
1680 if (!row->enabled_p)
1681 return NULL;
1682 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1683 break;
1684 }
1685
1686 *vpos = i;
1687 *hpos = 0;
1688
1689 /* Give up if Y is not in the window. */
1690 if (i == w->current_matrix->nrows)
1691 return NULL;
1692
1693 /* Get the glyph area containing X. */
1694 if (w->pseudo_window_p)
1695 {
1696 *area = TEXT_AREA;
1697 x0 = 0;
1698 }
1699 else
1700 {
1701 if (x < window_box_left_offset (w, TEXT_AREA))
1702 {
1703 *area = LEFT_MARGIN_AREA;
1704 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1705 }
1706 else if (x < window_box_right_offset (w, TEXT_AREA))
1707 {
1708 *area = TEXT_AREA;
1709 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1710 }
1711 else
1712 {
1713 *area = RIGHT_MARGIN_AREA;
1714 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1715 }
1716 }
1717
1718 /* Find glyph containing X. */
1719 glyph = row->glyphs[*area];
1720 end = glyph + row->used[*area];
1721 x -= x0;
1722 while (glyph < end && x >= glyph->pixel_width)
1723 {
1724 x -= glyph->pixel_width;
1725 ++glyph;
1726 }
1727
1728 if (glyph == end)
1729 return NULL;
1730
1731 if (dx)
1732 {
1733 *dx = x;
1734 *dy = y - (row->y + row->ascent - glyph->ascent);
1735 }
1736
1737 *hpos = glyph - row->glyphs[*area];
1738 return glyph;
1739 }
1740
1741
1742 /* EXPORT:
1743 Convert frame-relative x/y to coordinates relative to window W.
1744 Takes pseudo-windows into account. */
1745
1746 void
1747 frame_to_window_pixel_xy (w, x, y)
1748 struct window *w;
1749 int *x, *y;
1750 {
1751 if (w->pseudo_window_p)
1752 {
1753 /* A pseudo-window is always full-width, and starts at the
1754 left edge of the frame, plus a frame border. */
1755 struct frame *f = XFRAME (w->frame);
1756 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1757 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1758 }
1759 else
1760 {
1761 *x -= WINDOW_LEFT_EDGE_X (w);
1762 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1763 }
1764 }
1765
1766 /* EXPORT:
1767 Return in *R the clipping rectangle for glyph string S. */
1768
1769 void
1770 get_glyph_string_clip_rect (s, nr)
1771 struct glyph_string *s;
1772 NativeRectangle *nr;
1773 {
1774 XRectangle r;
1775
1776 if (s->row->full_width_p)
1777 {
1778 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1779 r.x = WINDOW_LEFT_EDGE_X (s->w);
1780 r.width = WINDOW_TOTAL_WIDTH (s->w);
1781
1782 /* Unless displaying a mode or menu bar line, which are always
1783 fully visible, clip to the visible part of the row. */
1784 if (s->w->pseudo_window_p)
1785 r.height = s->row->visible_height;
1786 else
1787 r.height = s->height;
1788 }
1789 else
1790 {
1791 /* This is a text line that may be partially visible. */
1792 r.x = window_box_left (s->w, s->area);
1793 r.width = window_box_width (s->w, s->area);
1794 r.height = s->row->visible_height;
1795 }
1796
1797 if (s->clip_head)
1798 if (r.x < s->clip_head->x)
1799 {
1800 if (r.width >= s->clip_head->x - r.x)
1801 r.width -= s->clip_head->x - r.x;
1802 else
1803 r.width = 0;
1804 r.x = s->clip_head->x;
1805 }
1806 if (s->clip_tail)
1807 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1808 {
1809 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1810 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1811 else
1812 r.width = 0;
1813 }
1814
1815 /* If S draws overlapping rows, it's sufficient to use the top and
1816 bottom of the window for clipping because this glyph string
1817 intentionally draws over other lines. */
1818 if (s->for_overlaps_p)
1819 {
1820 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1821 r.height = window_text_bottom_y (s->w) - r.y;
1822 }
1823 else
1824 {
1825 /* Don't use S->y for clipping because it doesn't take partially
1826 visible lines into account. For example, it can be negative for
1827 partially visible lines at the top of a window. */
1828 if (!s->row->full_width_p
1829 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1830 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1831 else
1832 r.y = max (0, s->row->y);
1833
1834 /* If drawing a tool-bar window, draw it over the internal border
1835 at the top of the window. */
1836 if (WINDOWP (s->f->tool_bar_window)
1837 && s->w == XWINDOW (s->f->tool_bar_window))
1838 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1839 }
1840
1841 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1842
1843 /* If drawing the cursor, don't let glyph draw outside its
1844 advertised boundaries. Cleartype does this under some circumstances. */
1845 if (s->hl == DRAW_CURSOR)
1846 {
1847 struct glyph *glyph = s->first_glyph;
1848 int height, max_y;
1849
1850 if (s->x > r.x)
1851 {
1852 r.width -= s->x - r.x;
1853 r.x = s->x;
1854 }
1855 r.width = min (r.width, glyph->pixel_width);
1856
1857 /* If r.y is below window bottom, ensure that we still see a cursor. */
1858 height = min (glyph->ascent + glyph->descent,
1859 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1860 max_y = window_text_bottom_y (s->w) - height;
1861 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1862 if (s->ybase - glyph->ascent > max_y)
1863 {
1864 r.y = max_y;
1865 r.height = height;
1866 }
1867 else
1868 {
1869 /* Don't draw cursor glyph taller than our actual glyph. */
1870 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1871 if (height < r.height)
1872 {
1873 max_y = r.y + r.height;
1874 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1875 r.height = min (max_y - r.y, height);
1876 }
1877 }
1878 }
1879
1880 #ifdef CONVERT_FROM_XRECT
1881 CONVERT_FROM_XRECT (r, *nr);
1882 #else
1883 *nr = r;
1884 #endif
1885 }
1886
1887
1888 /* EXPORT:
1889 Return the position and height of the phys cursor in window W.
1890 Set w->phys_cursor_width to width of phys cursor.
1891 */
1892
1893 int
1894 get_phys_cursor_geometry (w, row, glyph, heightp)
1895 struct window *w;
1896 struct glyph_row *row;
1897 struct glyph *glyph;
1898 int *heightp;
1899 {
1900 struct frame *f = XFRAME (WINDOW_FRAME (w));
1901 int y, wd, h, h0, y0;
1902
1903 /* Compute the width of the rectangle to draw. If on a stretch
1904 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1905 rectangle as wide as the glyph, but use a canonical character
1906 width instead. */
1907 wd = glyph->pixel_width - 1;
1908 #ifdef HAVE_NTGUI
1909 wd++; /* Why? */
1910 #endif
1911 if (glyph->type == STRETCH_GLYPH
1912 && !x_stretch_cursor_p)
1913 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1914 w->phys_cursor_width = wd;
1915
1916 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1917
1918 /* If y is below window bottom, ensure that we still see a cursor. */
1919 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1920
1921 h = max (h0, glyph->ascent + glyph->descent);
1922 h0 = min (h0, glyph->ascent + glyph->descent);
1923
1924 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1925 if (y < y0)
1926 {
1927 h = max (h - (y0 - y) + 1, h0);
1928 y = y0 - 1;
1929 }
1930 else
1931 {
1932 y0 = window_text_bottom_y (w) - h0;
1933 if (y > y0)
1934 {
1935 h += y - y0;
1936 y = y0;
1937 }
1938 }
1939
1940 *heightp = h - 1;
1941 return WINDOW_TO_FRAME_PIXEL_Y (w, y);
1942 }
1943
1944
1945 #endif /* HAVE_WINDOW_SYSTEM */
1946
1947 \f
1948 /***********************************************************************
1949 Lisp form evaluation
1950 ***********************************************************************/
1951
1952 /* Error handler for safe_eval and safe_call. */
1953
1954 static Lisp_Object
1955 safe_eval_handler (arg)
1956 Lisp_Object arg;
1957 {
1958 add_to_log ("Error during redisplay: %s", arg, Qnil);
1959 return Qnil;
1960 }
1961
1962
1963 /* Evaluate SEXPR and return the result, or nil if something went
1964 wrong. Prevent redisplay during the evaluation. */
1965
1966 Lisp_Object
1967 safe_eval (sexpr)
1968 Lisp_Object sexpr;
1969 {
1970 Lisp_Object val;
1971
1972 if (inhibit_eval_during_redisplay)
1973 val = Qnil;
1974 else
1975 {
1976 int count = SPECPDL_INDEX ();
1977 struct gcpro gcpro1;
1978
1979 GCPRO1 (sexpr);
1980 specbind (Qinhibit_redisplay, Qt);
1981 /* Use Qt to ensure debugger does not run,
1982 so there is no possibility of wanting to redisplay. */
1983 val = internal_condition_case_1 (Feval, sexpr, Qt,
1984 safe_eval_handler);
1985 UNGCPRO;
1986 val = unbind_to (count, val);
1987 }
1988
1989 return val;
1990 }
1991
1992
1993 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
1994 Return the result, or nil if something went wrong. Prevent
1995 redisplay during the evaluation. */
1996
1997 Lisp_Object
1998 safe_call (nargs, args)
1999 int nargs;
2000 Lisp_Object *args;
2001 {
2002 Lisp_Object val;
2003
2004 if (inhibit_eval_during_redisplay)
2005 val = Qnil;
2006 else
2007 {
2008 int count = SPECPDL_INDEX ();
2009 struct gcpro gcpro1;
2010
2011 GCPRO1 (args[0]);
2012 gcpro1.nvars = nargs;
2013 specbind (Qinhibit_redisplay, Qt);
2014 /* Use Qt to ensure debugger does not run,
2015 so there is no possibility of wanting to redisplay. */
2016 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2017 safe_eval_handler);
2018 UNGCPRO;
2019 val = unbind_to (count, val);
2020 }
2021
2022 return val;
2023 }
2024
2025
2026 /* Call function FN with one argument ARG.
2027 Return the result, or nil if something went wrong. */
2028
2029 Lisp_Object
2030 safe_call1 (fn, arg)
2031 Lisp_Object fn, arg;
2032 {
2033 Lisp_Object args[2];
2034 args[0] = fn;
2035 args[1] = arg;
2036 return safe_call (2, args);
2037 }
2038
2039
2040 \f
2041 /***********************************************************************
2042 Debugging
2043 ***********************************************************************/
2044
2045 #if 0
2046
2047 /* Define CHECK_IT to perform sanity checks on iterators.
2048 This is for debugging. It is too slow to do unconditionally. */
2049
2050 static void
2051 check_it (it)
2052 struct it *it;
2053 {
2054 if (it->method == GET_FROM_STRING)
2055 {
2056 xassert (STRINGP (it->string));
2057 xassert (IT_STRING_CHARPOS (*it) >= 0);
2058 }
2059 else
2060 {
2061 xassert (IT_STRING_CHARPOS (*it) < 0);
2062 if (it->method == GET_FROM_BUFFER)
2063 {
2064 /* Check that character and byte positions agree. */
2065 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2066 }
2067 }
2068
2069 if (it->dpvec)
2070 xassert (it->current.dpvec_index >= 0);
2071 else
2072 xassert (it->current.dpvec_index < 0);
2073 }
2074
2075 #define CHECK_IT(IT) check_it ((IT))
2076
2077 #else /* not 0 */
2078
2079 #define CHECK_IT(IT) (void) 0
2080
2081 #endif /* not 0 */
2082
2083
2084 #if GLYPH_DEBUG
2085
2086 /* Check that the window end of window W is what we expect it
2087 to be---the last row in the current matrix displaying text. */
2088
2089 static void
2090 check_window_end (w)
2091 struct window *w;
2092 {
2093 if (!MINI_WINDOW_P (w)
2094 && !NILP (w->window_end_valid))
2095 {
2096 struct glyph_row *row;
2097 xassert ((row = MATRIX_ROW (w->current_matrix,
2098 XFASTINT (w->window_end_vpos)),
2099 !row->enabled_p
2100 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2101 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2102 }
2103 }
2104
2105 #define CHECK_WINDOW_END(W) check_window_end ((W))
2106
2107 #else /* not GLYPH_DEBUG */
2108
2109 #define CHECK_WINDOW_END(W) (void) 0
2110
2111 #endif /* not GLYPH_DEBUG */
2112
2113
2114 \f
2115 /***********************************************************************
2116 Iterator initialization
2117 ***********************************************************************/
2118
2119 /* Initialize IT for displaying current_buffer in window W, starting
2120 at character position CHARPOS. CHARPOS < 0 means that no buffer
2121 position is specified which is useful when the iterator is assigned
2122 a position later. BYTEPOS is the byte position corresponding to
2123 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2124
2125 If ROW is not null, calls to produce_glyphs with IT as parameter
2126 will produce glyphs in that row.
2127
2128 BASE_FACE_ID is the id of a base face to use. It must be one of
2129 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2130 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2131 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2132
2133 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2134 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2135 will be initialized to use the corresponding mode line glyph row of
2136 the desired matrix of W. */
2137
2138 void
2139 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2140 struct it *it;
2141 struct window *w;
2142 int charpos, bytepos;
2143 struct glyph_row *row;
2144 enum face_id base_face_id;
2145 {
2146 int highlight_region_p;
2147
2148 /* Some precondition checks. */
2149 xassert (w != NULL && it != NULL);
2150 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2151 && charpos <= ZV));
2152
2153 /* If face attributes have been changed since the last redisplay,
2154 free realized faces now because they depend on face definitions
2155 that might have changed. Don't free faces while there might be
2156 desired matrices pending which reference these faces. */
2157 if (face_change_count && !inhibit_free_realized_faces)
2158 {
2159 face_change_count = 0;
2160 free_all_realized_faces (Qnil);
2161 }
2162
2163 /* Use one of the mode line rows of W's desired matrix if
2164 appropriate. */
2165 if (row == NULL)
2166 {
2167 if (base_face_id == MODE_LINE_FACE_ID
2168 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2169 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2170 else if (base_face_id == HEADER_LINE_FACE_ID)
2171 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2172 }
2173
2174 /* Clear IT. */
2175 bzero (it, sizeof *it);
2176 it->current.overlay_string_index = -1;
2177 it->current.dpvec_index = -1;
2178 it->base_face_id = base_face_id;
2179 it->string = Qnil;
2180 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2181
2182 /* The window in which we iterate over current_buffer: */
2183 XSETWINDOW (it->window, w);
2184 it->w = w;
2185 it->f = XFRAME (w->frame);
2186
2187 /* Extra space between lines (on window systems only). */
2188 if (base_face_id == DEFAULT_FACE_ID
2189 && FRAME_WINDOW_P (it->f))
2190 {
2191 if (NATNUMP (current_buffer->extra_line_spacing))
2192 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2193 else if (FLOATP (current_buffer->extra_line_spacing))
2194 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2195 * FRAME_LINE_HEIGHT (it->f));
2196 else if (it->f->extra_line_spacing > 0)
2197 it->extra_line_spacing = it->f->extra_line_spacing;
2198 it->max_extra_line_spacing = 0;
2199 }
2200
2201 /* If realized faces have been removed, e.g. because of face
2202 attribute changes of named faces, recompute them. When running
2203 in batch mode, the face cache of Vterminal_frame is null. If
2204 we happen to get called, make a dummy face cache. */
2205 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2206 init_frame_faces (it->f);
2207 if (FRAME_FACE_CACHE (it->f)->used == 0)
2208 recompute_basic_faces (it->f);
2209
2210 /* Current value of the `slice', `space-width', and 'height' properties. */
2211 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2212 it->space_width = Qnil;
2213 it->font_height = Qnil;
2214 it->override_ascent = -1;
2215
2216 /* Are control characters displayed as `^C'? */
2217 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2218
2219 /* -1 means everything between a CR and the following line end
2220 is invisible. >0 means lines indented more than this value are
2221 invisible. */
2222 it->selective = (INTEGERP (current_buffer->selective_display)
2223 ? XFASTINT (current_buffer->selective_display)
2224 : (!NILP (current_buffer->selective_display)
2225 ? -1 : 0));
2226 it->selective_display_ellipsis_p
2227 = !NILP (current_buffer->selective_display_ellipses);
2228
2229 /* Display table to use. */
2230 it->dp = window_display_table (w);
2231
2232 /* Are multibyte characters enabled in current_buffer? */
2233 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2234
2235 /* Non-zero if we should highlight the region. */
2236 highlight_region_p
2237 = (!NILP (Vtransient_mark_mode)
2238 && !NILP (current_buffer->mark_active)
2239 && XMARKER (current_buffer->mark)->buffer != 0);
2240
2241 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2242 start and end of a visible region in window IT->w. Set both to
2243 -1 to indicate no region. */
2244 if (highlight_region_p
2245 /* Maybe highlight only in selected window. */
2246 && (/* Either show region everywhere. */
2247 highlight_nonselected_windows
2248 /* Or show region in the selected window. */
2249 || w == XWINDOW (selected_window)
2250 /* Or show the region if we are in the mini-buffer and W is
2251 the window the mini-buffer refers to. */
2252 || (MINI_WINDOW_P (XWINDOW (selected_window))
2253 && WINDOWP (minibuf_selected_window)
2254 && w == XWINDOW (minibuf_selected_window))))
2255 {
2256 int charpos = marker_position (current_buffer->mark);
2257 it->region_beg_charpos = min (PT, charpos);
2258 it->region_end_charpos = max (PT, charpos);
2259 }
2260 else
2261 it->region_beg_charpos = it->region_end_charpos = -1;
2262
2263 /* Get the position at which the redisplay_end_trigger hook should
2264 be run, if it is to be run at all. */
2265 if (MARKERP (w->redisplay_end_trigger)
2266 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2267 it->redisplay_end_trigger_charpos
2268 = marker_position (w->redisplay_end_trigger);
2269 else if (INTEGERP (w->redisplay_end_trigger))
2270 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2271
2272 /* Correct bogus values of tab_width. */
2273 it->tab_width = XINT (current_buffer->tab_width);
2274 if (it->tab_width <= 0 || it->tab_width > 1000)
2275 it->tab_width = 8;
2276
2277 /* Are lines in the display truncated? */
2278 it->truncate_lines_p
2279 = (base_face_id != DEFAULT_FACE_ID
2280 || XINT (it->w->hscroll)
2281 || (truncate_partial_width_windows
2282 && !WINDOW_FULL_WIDTH_P (it->w))
2283 || !NILP (current_buffer->truncate_lines));
2284
2285 /* Get dimensions of truncation and continuation glyphs. These are
2286 displayed as fringe bitmaps under X, so we don't need them for such
2287 frames. */
2288 if (!FRAME_WINDOW_P (it->f))
2289 {
2290 if (it->truncate_lines_p)
2291 {
2292 /* We will need the truncation glyph. */
2293 xassert (it->glyph_row == NULL);
2294 produce_special_glyphs (it, IT_TRUNCATION);
2295 it->truncation_pixel_width = it->pixel_width;
2296 }
2297 else
2298 {
2299 /* We will need the continuation glyph. */
2300 xassert (it->glyph_row == NULL);
2301 produce_special_glyphs (it, IT_CONTINUATION);
2302 it->continuation_pixel_width = it->pixel_width;
2303 }
2304
2305 /* Reset these values to zero because the produce_special_glyphs
2306 above has changed them. */
2307 it->pixel_width = it->ascent = it->descent = 0;
2308 it->phys_ascent = it->phys_descent = 0;
2309 }
2310
2311 /* Set this after getting the dimensions of truncation and
2312 continuation glyphs, so that we don't produce glyphs when calling
2313 produce_special_glyphs, above. */
2314 it->glyph_row = row;
2315 it->area = TEXT_AREA;
2316
2317 /* Get the dimensions of the display area. The display area
2318 consists of the visible window area plus a horizontally scrolled
2319 part to the left of the window. All x-values are relative to the
2320 start of this total display area. */
2321 if (base_face_id != DEFAULT_FACE_ID)
2322 {
2323 /* Mode lines, menu bar in terminal frames. */
2324 it->first_visible_x = 0;
2325 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2326 }
2327 else
2328 {
2329 it->first_visible_x
2330 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2331 it->last_visible_x = (it->first_visible_x
2332 + window_box_width (w, TEXT_AREA));
2333
2334 /* If we truncate lines, leave room for the truncator glyph(s) at
2335 the right margin. Otherwise, leave room for the continuation
2336 glyph(s). Truncation and continuation glyphs are not inserted
2337 for window-based redisplay. */
2338 if (!FRAME_WINDOW_P (it->f))
2339 {
2340 if (it->truncate_lines_p)
2341 it->last_visible_x -= it->truncation_pixel_width;
2342 else
2343 it->last_visible_x -= it->continuation_pixel_width;
2344 }
2345
2346 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2347 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2348 }
2349
2350 /* Leave room for a border glyph. */
2351 if (!FRAME_WINDOW_P (it->f)
2352 && !WINDOW_RIGHTMOST_P (it->w))
2353 it->last_visible_x -= 1;
2354
2355 it->last_visible_y = window_text_bottom_y (w);
2356
2357 /* For mode lines and alike, arrange for the first glyph having a
2358 left box line if the face specifies a box. */
2359 if (base_face_id != DEFAULT_FACE_ID)
2360 {
2361 struct face *face;
2362
2363 it->face_id = base_face_id;
2364
2365 /* If we have a boxed mode line, make the first character appear
2366 with a left box line. */
2367 face = FACE_FROM_ID (it->f, base_face_id);
2368 if (face->box != FACE_NO_BOX)
2369 it->start_of_box_run_p = 1;
2370 }
2371
2372 /* If a buffer position was specified, set the iterator there,
2373 getting overlays and face properties from that position. */
2374 if (charpos >= BUF_BEG (current_buffer))
2375 {
2376 it->end_charpos = ZV;
2377 it->face_id = -1;
2378 IT_CHARPOS (*it) = charpos;
2379
2380 /* Compute byte position if not specified. */
2381 if (bytepos < charpos)
2382 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2383 else
2384 IT_BYTEPOS (*it) = bytepos;
2385
2386 it->start = it->current;
2387
2388 /* Compute faces etc. */
2389 reseat (it, it->current.pos, 1);
2390 }
2391
2392 CHECK_IT (it);
2393 }
2394
2395
2396 /* Initialize IT for the display of window W with window start POS. */
2397
2398 void
2399 start_display (it, w, pos)
2400 struct it *it;
2401 struct window *w;
2402 struct text_pos pos;
2403 {
2404 struct glyph_row *row;
2405 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2406
2407 row = w->desired_matrix->rows + first_vpos;
2408 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2409 it->first_vpos = first_vpos;
2410
2411 if (!it->truncate_lines_p)
2412 {
2413 int start_at_line_beg_p;
2414 int first_y = it->current_y;
2415
2416 /* If window start is not at a line start, skip forward to POS to
2417 get the correct continuation lines width. */
2418 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2419 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2420 if (!start_at_line_beg_p)
2421 {
2422 int new_x;
2423
2424 reseat_at_previous_visible_line_start (it);
2425 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2426
2427 new_x = it->current_x + it->pixel_width;
2428
2429 /* If lines are continued, this line may end in the middle
2430 of a multi-glyph character (e.g. a control character
2431 displayed as \003, or in the middle of an overlay
2432 string). In this case move_it_to above will not have
2433 taken us to the start of the continuation line but to the
2434 end of the continued line. */
2435 if (it->current_x > 0
2436 && !it->truncate_lines_p /* Lines are continued. */
2437 && (/* And glyph doesn't fit on the line. */
2438 new_x > it->last_visible_x
2439 /* Or it fits exactly and we're on a window
2440 system frame. */
2441 || (new_x == it->last_visible_x
2442 && FRAME_WINDOW_P (it->f))))
2443 {
2444 if (it->current.dpvec_index >= 0
2445 || it->current.overlay_string_index >= 0)
2446 {
2447 set_iterator_to_next (it, 1);
2448 move_it_in_display_line_to (it, -1, -1, 0);
2449 }
2450
2451 it->continuation_lines_width += it->current_x;
2452 }
2453
2454 /* We're starting a new display line, not affected by the
2455 height of the continued line, so clear the appropriate
2456 fields in the iterator structure. */
2457 it->max_ascent = it->max_descent = 0;
2458 it->max_phys_ascent = it->max_phys_descent = 0;
2459
2460 it->current_y = first_y;
2461 it->vpos = 0;
2462 it->current_x = it->hpos = 0;
2463 }
2464 }
2465
2466 #if 0 /* Don't assert the following because start_display is sometimes
2467 called intentionally with a window start that is not at a
2468 line start. Please leave this code in as a comment. */
2469
2470 /* Window start should be on a line start, now. */
2471 xassert (it->continuation_lines_width
2472 || IT_CHARPOS (it) == BEGV
2473 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2474 #endif /* 0 */
2475 }
2476
2477
2478 /* Return 1 if POS is a position in ellipses displayed for invisible
2479 text. W is the window we display, for text property lookup. */
2480
2481 static int
2482 in_ellipses_for_invisible_text_p (pos, w)
2483 struct display_pos *pos;
2484 struct window *w;
2485 {
2486 Lisp_Object prop, window;
2487 int ellipses_p = 0;
2488 int charpos = CHARPOS (pos->pos);
2489
2490 /* If POS specifies a position in a display vector, this might
2491 be for an ellipsis displayed for invisible text. We won't
2492 get the iterator set up for delivering that ellipsis unless
2493 we make sure that it gets aware of the invisible text. */
2494 if (pos->dpvec_index >= 0
2495 && pos->overlay_string_index < 0
2496 && CHARPOS (pos->string_pos) < 0
2497 && charpos > BEGV
2498 && (XSETWINDOW (window, w),
2499 prop = Fget_char_property (make_number (charpos),
2500 Qinvisible, window),
2501 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2502 {
2503 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2504 window);
2505 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2506 }
2507
2508 return ellipses_p;
2509 }
2510
2511
2512 /* Initialize IT for stepping through current_buffer in window W,
2513 starting at position POS that includes overlay string and display
2514 vector/ control character translation position information. Value
2515 is zero if there are overlay strings with newlines at POS. */
2516
2517 static int
2518 init_from_display_pos (it, w, pos)
2519 struct it *it;
2520 struct window *w;
2521 struct display_pos *pos;
2522 {
2523 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2524 int i, overlay_strings_with_newlines = 0;
2525
2526 /* If POS specifies a position in a display vector, this might
2527 be for an ellipsis displayed for invisible text. We won't
2528 get the iterator set up for delivering that ellipsis unless
2529 we make sure that it gets aware of the invisible text. */
2530 if (in_ellipses_for_invisible_text_p (pos, w))
2531 {
2532 --charpos;
2533 bytepos = 0;
2534 }
2535
2536 /* Keep in mind: the call to reseat in init_iterator skips invisible
2537 text, so we might end up at a position different from POS. This
2538 is only a problem when POS is a row start after a newline and an
2539 overlay starts there with an after-string, and the overlay has an
2540 invisible property. Since we don't skip invisible text in
2541 display_line and elsewhere immediately after consuming the
2542 newline before the row start, such a POS will not be in a string,
2543 but the call to init_iterator below will move us to the
2544 after-string. */
2545 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2546
2547 /* This only scans the current chunk -- it should scan all chunks.
2548 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2549 to 16 in 22.1 to make this a lesser problem. */
2550 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2551 {
2552 const char *s = SDATA (it->overlay_strings[i]);
2553 const char *e = s + SBYTES (it->overlay_strings[i]);
2554
2555 while (s < e && *s != '\n')
2556 ++s;
2557
2558 if (s < e)
2559 {
2560 overlay_strings_with_newlines = 1;
2561 break;
2562 }
2563 }
2564
2565 /* If position is within an overlay string, set up IT to the right
2566 overlay string. */
2567 if (pos->overlay_string_index >= 0)
2568 {
2569 int relative_index;
2570
2571 /* If the first overlay string happens to have a `display'
2572 property for an image, the iterator will be set up for that
2573 image, and we have to undo that setup first before we can
2574 correct the overlay string index. */
2575 if (it->method == GET_FROM_IMAGE)
2576 pop_it (it);
2577
2578 /* We already have the first chunk of overlay strings in
2579 IT->overlay_strings. Load more until the one for
2580 pos->overlay_string_index is in IT->overlay_strings. */
2581 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2582 {
2583 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2584 it->current.overlay_string_index = 0;
2585 while (n--)
2586 {
2587 load_overlay_strings (it, 0);
2588 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2589 }
2590 }
2591
2592 it->current.overlay_string_index = pos->overlay_string_index;
2593 relative_index = (it->current.overlay_string_index
2594 % OVERLAY_STRING_CHUNK_SIZE);
2595 it->string = it->overlay_strings[relative_index];
2596 xassert (STRINGP (it->string));
2597 it->current.string_pos = pos->string_pos;
2598 it->method = GET_FROM_STRING;
2599 }
2600
2601 #if 0 /* This is bogus because POS not having an overlay string
2602 position does not mean it's after the string. Example: A
2603 line starting with a before-string and initialization of IT
2604 to the previous row's end position. */
2605 else if (it->current.overlay_string_index >= 0)
2606 {
2607 /* If POS says we're already after an overlay string ending at
2608 POS, make sure to pop the iterator because it will be in
2609 front of that overlay string. When POS is ZV, we've thereby
2610 also ``processed'' overlay strings at ZV. */
2611 while (it->sp)
2612 pop_it (it);
2613 it->current.overlay_string_index = -1;
2614 it->method = GET_FROM_BUFFER;
2615 if (CHARPOS (pos->pos) == ZV)
2616 it->overlay_strings_at_end_processed_p = 1;
2617 }
2618 #endif /* 0 */
2619
2620 if (CHARPOS (pos->string_pos) >= 0)
2621 {
2622 /* Recorded position is not in an overlay string, but in another
2623 string. This can only be a string from a `display' property.
2624 IT should already be filled with that string. */
2625 it->current.string_pos = pos->string_pos;
2626 xassert (STRINGP (it->string));
2627 }
2628
2629 /* Restore position in display vector translations, control
2630 character translations or ellipses. */
2631 if (pos->dpvec_index >= 0)
2632 {
2633 if (it->dpvec == NULL)
2634 get_next_display_element (it);
2635 xassert (it->dpvec && it->current.dpvec_index == 0);
2636 it->current.dpvec_index = pos->dpvec_index;
2637 }
2638
2639 CHECK_IT (it);
2640 return !overlay_strings_with_newlines;
2641 }
2642
2643
2644 /* Initialize IT for stepping through current_buffer in window W
2645 starting at ROW->start. */
2646
2647 static void
2648 init_to_row_start (it, w, row)
2649 struct it *it;
2650 struct window *w;
2651 struct glyph_row *row;
2652 {
2653 init_from_display_pos (it, w, &row->start);
2654 it->start = row->start;
2655 it->continuation_lines_width = row->continuation_lines_width;
2656 CHECK_IT (it);
2657 }
2658
2659
2660 /* Initialize IT for stepping through current_buffer in window W
2661 starting in the line following ROW, i.e. starting at ROW->end.
2662 Value is zero if there are overlay strings with newlines at ROW's
2663 end position. */
2664
2665 static int
2666 init_to_row_end (it, w, row)
2667 struct it *it;
2668 struct window *w;
2669 struct glyph_row *row;
2670 {
2671 int success = 0;
2672
2673 if (init_from_display_pos (it, w, &row->end))
2674 {
2675 if (row->continued_p)
2676 it->continuation_lines_width
2677 = row->continuation_lines_width + row->pixel_width;
2678 CHECK_IT (it);
2679 success = 1;
2680 }
2681
2682 return success;
2683 }
2684
2685
2686
2687 \f
2688 /***********************************************************************
2689 Text properties
2690 ***********************************************************************/
2691
2692 /* Called when IT reaches IT->stop_charpos. Handle text property and
2693 overlay changes. Set IT->stop_charpos to the next position where
2694 to stop. */
2695
2696 static void
2697 handle_stop (it)
2698 struct it *it;
2699 {
2700 enum prop_handled handled;
2701 int handle_overlay_change_p = 1;
2702 struct props *p;
2703
2704 it->dpvec = NULL;
2705 it->current.dpvec_index = -1;
2706
2707 /* Use face of preceding text for ellipsis (if invisible) */
2708 if (it->selective_display_ellipsis_p)
2709 it->saved_face_id = it->face_id;
2710
2711 do
2712 {
2713 handled = HANDLED_NORMALLY;
2714
2715 /* Call text property handlers. */
2716 for (p = it_props; p->handler; ++p)
2717 {
2718 handled = p->handler (it);
2719
2720 if (handled == HANDLED_RECOMPUTE_PROPS)
2721 break;
2722 else if (handled == HANDLED_RETURN)
2723 return;
2724 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2725 handle_overlay_change_p = 0;
2726 }
2727
2728 if (handled != HANDLED_RECOMPUTE_PROPS)
2729 {
2730 /* Don't check for overlay strings below when set to deliver
2731 characters from a display vector. */
2732 if (it->method == GET_FROM_DISPLAY_VECTOR)
2733 handle_overlay_change_p = 0;
2734
2735 /* Handle overlay changes. */
2736 if (handle_overlay_change_p)
2737 handled = handle_overlay_change (it);
2738
2739 /* Determine where to stop next. */
2740 if (handled == HANDLED_NORMALLY)
2741 compute_stop_pos (it);
2742 }
2743 }
2744 while (handled == HANDLED_RECOMPUTE_PROPS);
2745 }
2746
2747
2748 /* Compute IT->stop_charpos from text property and overlay change
2749 information for IT's current position. */
2750
2751 static void
2752 compute_stop_pos (it)
2753 struct it *it;
2754 {
2755 register INTERVAL iv, next_iv;
2756 Lisp_Object object, limit, position;
2757
2758 /* If nowhere else, stop at the end. */
2759 it->stop_charpos = it->end_charpos;
2760
2761 if (STRINGP (it->string))
2762 {
2763 /* Strings are usually short, so don't limit the search for
2764 properties. */
2765 object = it->string;
2766 limit = Qnil;
2767 position = make_number (IT_STRING_CHARPOS (*it));
2768 }
2769 else
2770 {
2771 int charpos;
2772
2773 /* If next overlay change is in front of the current stop pos
2774 (which is IT->end_charpos), stop there. Note: value of
2775 next_overlay_change is point-max if no overlay change
2776 follows. */
2777 charpos = next_overlay_change (IT_CHARPOS (*it));
2778 if (charpos < it->stop_charpos)
2779 it->stop_charpos = charpos;
2780
2781 /* If showing the region, we have to stop at the region
2782 start or end because the face might change there. */
2783 if (it->region_beg_charpos > 0)
2784 {
2785 if (IT_CHARPOS (*it) < it->region_beg_charpos)
2786 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
2787 else if (IT_CHARPOS (*it) < it->region_end_charpos)
2788 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
2789 }
2790
2791 /* Set up variables for computing the stop position from text
2792 property changes. */
2793 XSETBUFFER (object, current_buffer);
2794 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
2795 position = make_number (IT_CHARPOS (*it));
2796
2797 }
2798
2799 /* Get the interval containing IT's position. Value is a null
2800 interval if there isn't such an interval. */
2801 iv = validate_interval_range (object, &position, &position, 0);
2802 if (!NULL_INTERVAL_P (iv))
2803 {
2804 Lisp_Object values_here[LAST_PROP_IDX];
2805 struct props *p;
2806
2807 /* Get properties here. */
2808 for (p = it_props; p->handler; ++p)
2809 values_here[p->idx] = textget (iv->plist, *p->name);
2810
2811 /* Look for an interval following iv that has different
2812 properties. */
2813 for (next_iv = next_interval (iv);
2814 (!NULL_INTERVAL_P (next_iv)
2815 && (NILP (limit)
2816 || XFASTINT (limit) > next_iv->position));
2817 next_iv = next_interval (next_iv))
2818 {
2819 for (p = it_props; p->handler; ++p)
2820 {
2821 Lisp_Object new_value;
2822
2823 new_value = textget (next_iv->plist, *p->name);
2824 if (!EQ (values_here[p->idx], new_value))
2825 break;
2826 }
2827
2828 if (p->handler)
2829 break;
2830 }
2831
2832 if (!NULL_INTERVAL_P (next_iv))
2833 {
2834 if (INTEGERP (limit)
2835 && next_iv->position >= XFASTINT (limit))
2836 /* No text property change up to limit. */
2837 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
2838 else
2839 /* Text properties change in next_iv. */
2840 it->stop_charpos = min (it->stop_charpos, next_iv->position);
2841 }
2842 }
2843
2844 xassert (STRINGP (it->string)
2845 || (it->stop_charpos >= BEGV
2846 && it->stop_charpos >= IT_CHARPOS (*it)));
2847 }
2848
2849
2850 /* Return the position of the next overlay change after POS in
2851 current_buffer. Value is point-max if no overlay change
2852 follows. This is like `next-overlay-change' but doesn't use
2853 xmalloc. */
2854
2855 static int
2856 next_overlay_change (pos)
2857 int pos;
2858 {
2859 int noverlays;
2860 int endpos;
2861 Lisp_Object *overlays;
2862 int i;
2863
2864 /* Get all overlays at the given position. */
2865 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
2866
2867 /* If any of these overlays ends before endpos,
2868 use its ending point instead. */
2869 for (i = 0; i < noverlays; ++i)
2870 {
2871 Lisp_Object oend;
2872 int oendpos;
2873
2874 oend = OVERLAY_END (overlays[i]);
2875 oendpos = OVERLAY_POSITION (oend);
2876 endpos = min (endpos, oendpos);
2877 }
2878
2879 return endpos;
2880 }
2881
2882
2883 \f
2884 /***********************************************************************
2885 Fontification
2886 ***********************************************************************/
2887
2888 /* Handle changes in the `fontified' property of the current buffer by
2889 calling hook functions from Qfontification_functions to fontify
2890 regions of text. */
2891
2892 static enum prop_handled
2893 handle_fontified_prop (it)
2894 struct it *it;
2895 {
2896 Lisp_Object prop, pos;
2897 enum prop_handled handled = HANDLED_NORMALLY;
2898
2899 /* Get the value of the `fontified' property at IT's current buffer
2900 position. (The `fontified' property doesn't have a special
2901 meaning in strings.) If the value is nil, call functions from
2902 Qfontification_functions. */
2903 if (!STRINGP (it->string)
2904 && it->s == NULL
2905 && !NILP (Vfontification_functions)
2906 && !NILP (Vrun_hooks)
2907 && (pos = make_number (IT_CHARPOS (*it)),
2908 prop = Fget_char_property (pos, Qfontified, Qnil),
2909 NILP (prop)))
2910 {
2911 int count = SPECPDL_INDEX ();
2912 Lisp_Object val;
2913
2914 val = Vfontification_functions;
2915 specbind (Qfontification_functions, Qnil);
2916
2917 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
2918 safe_call1 (val, pos);
2919 else
2920 {
2921 Lisp_Object globals, fn;
2922 struct gcpro gcpro1, gcpro2;
2923
2924 globals = Qnil;
2925 GCPRO2 (val, globals);
2926
2927 for (; CONSP (val); val = XCDR (val))
2928 {
2929 fn = XCAR (val);
2930
2931 if (EQ (fn, Qt))
2932 {
2933 /* A value of t indicates this hook has a local
2934 binding; it means to run the global binding too.
2935 In a global value, t should not occur. If it
2936 does, we must ignore it to avoid an endless
2937 loop. */
2938 for (globals = Fdefault_value (Qfontification_functions);
2939 CONSP (globals);
2940 globals = XCDR (globals))
2941 {
2942 fn = XCAR (globals);
2943 if (!EQ (fn, Qt))
2944 safe_call1 (fn, pos);
2945 }
2946 }
2947 else
2948 safe_call1 (fn, pos);
2949 }
2950
2951 UNGCPRO;
2952 }
2953
2954 unbind_to (count, Qnil);
2955
2956 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
2957 something. This avoids an endless loop if they failed to
2958 fontify the text for which reason ever. */
2959 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
2960 handled = HANDLED_RECOMPUTE_PROPS;
2961 }
2962
2963 return handled;
2964 }
2965
2966
2967 \f
2968 /***********************************************************************
2969 Faces
2970 ***********************************************************************/
2971
2972 /* Set up iterator IT from face properties at its current position.
2973 Called from handle_stop. */
2974
2975 static enum prop_handled
2976 handle_face_prop (it)
2977 struct it *it;
2978 {
2979 int new_face_id, next_stop;
2980
2981 if (!STRINGP (it->string))
2982 {
2983 new_face_id
2984 = face_at_buffer_position (it->w,
2985 IT_CHARPOS (*it),
2986 it->region_beg_charpos,
2987 it->region_end_charpos,
2988 &next_stop,
2989 (IT_CHARPOS (*it)
2990 + TEXT_PROP_DISTANCE_LIMIT),
2991 0);
2992
2993 /* Is this a start of a run of characters with box face?
2994 Caveat: this can be called for a freshly initialized
2995 iterator; face_id is -1 in this case. We know that the new
2996 face will not change until limit, i.e. if the new face has a
2997 box, all characters up to limit will have one. But, as
2998 usual, we don't know whether limit is really the end. */
2999 if (new_face_id != it->face_id)
3000 {
3001 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3002
3003 /* If new face has a box but old face has not, this is
3004 the start of a run of characters with box, i.e. it has
3005 a shadow on the left side. The value of face_id of the
3006 iterator will be -1 if this is the initial call that gets
3007 the face. In this case, we have to look in front of IT's
3008 position and see whether there is a face != new_face_id. */
3009 it->start_of_box_run_p
3010 = (new_face->box != FACE_NO_BOX
3011 && (it->face_id >= 0
3012 || IT_CHARPOS (*it) == BEG
3013 || new_face_id != face_before_it_pos (it)));
3014 it->face_box_p = new_face->box != FACE_NO_BOX;
3015 }
3016 }
3017 else
3018 {
3019 int base_face_id, bufpos;
3020
3021 if (it->current.overlay_string_index >= 0)
3022 bufpos = IT_CHARPOS (*it);
3023 else
3024 bufpos = 0;
3025
3026 /* For strings from a buffer, i.e. overlay strings or strings
3027 from a `display' property, use the face at IT's current
3028 buffer position as the base face to merge with, so that
3029 overlay strings appear in the same face as surrounding
3030 text, unless they specify their own faces. */
3031 base_face_id = underlying_face_id (it);
3032
3033 new_face_id = face_at_string_position (it->w,
3034 it->string,
3035 IT_STRING_CHARPOS (*it),
3036 bufpos,
3037 it->region_beg_charpos,
3038 it->region_end_charpos,
3039 &next_stop,
3040 base_face_id, 0);
3041
3042 #if 0 /* This shouldn't be neccessary. Let's check it. */
3043 /* If IT is used to display a mode line we would really like to
3044 use the mode line face instead of the frame's default face. */
3045 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3046 && new_face_id == DEFAULT_FACE_ID)
3047 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3048 #endif
3049
3050 /* Is this a start of a run of characters with box? Caveat:
3051 this can be called for a freshly allocated iterator; face_id
3052 is -1 is this case. We know that the new face will not
3053 change until the next check pos, i.e. if the new face has a
3054 box, all characters up to that position will have a
3055 box. But, as usual, we don't know whether that position
3056 is really the end. */
3057 if (new_face_id != it->face_id)
3058 {
3059 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3060 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3061
3062 /* If new face has a box but old face hasn't, this is the
3063 start of a run of characters with box, i.e. it has a
3064 shadow on the left side. */
3065 it->start_of_box_run_p
3066 = new_face->box && (old_face == NULL || !old_face->box);
3067 it->face_box_p = new_face->box != FACE_NO_BOX;
3068 }
3069 }
3070
3071 it->face_id = new_face_id;
3072 return HANDLED_NORMALLY;
3073 }
3074
3075
3076 /* Return the ID of the face ``underlying'' IT's current position,
3077 which is in a string. If the iterator is associated with a
3078 buffer, return the face at IT's current buffer position.
3079 Otherwise, use the iterator's base_face_id. */
3080
3081 static int
3082 underlying_face_id (it)
3083 struct it *it;
3084 {
3085 int face_id = it->base_face_id, i;
3086
3087 xassert (STRINGP (it->string));
3088
3089 for (i = it->sp - 1; i >= 0; --i)
3090 if (NILP (it->stack[i].string))
3091 face_id = it->stack[i].face_id;
3092
3093 return face_id;
3094 }
3095
3096
3097 /* Compute the face one character before or after the current position
3098 of IT. BEFORE_P non-zero means get the face in front of IT's
3099 position. Value is the id of the face. */
3100
3101 static int
3102 face_before_or_after_it_pos (it, before_p)
3103 struct it *it;
3104 int before_p;
3105 {
3106 int face_id, limit;
3107 int next_check_charpos;
3108 struct text_pos pos;
3109
3110 xassert (it->s == NULL);
3111
3112 if (STRINGP (it->string))
3113 {
3114 int bufpos, base_face_id;
3115
3116 /* No face change past the end of the string (for the case
3117 we are padding with spaces). No face change before the
3118 string start. */
3119 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3120 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3121 return it->face_id;
3122
3123 /* Set pos to the position before or after IT's current position. */
3124 if (before_p)
3125 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3126 else
3127 /* For composition, we must check the character after the
3128 composition. */
3129 pos = (it->what == IT_COMPOSITION
3130 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3131 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3132
3133 if (it->current.overlay_string_index >= 0)
3134 bufpos = IT_CHARPOS (*it);
3135 else
3136 bufpos = 0;
3137
3138 base_face_id = underlying_face_id (it);
3139
3140 /* Get the face for ASCII, or unibyte. */
3141 face_id = face_at_string_position (it->w,
3142 it->string,
3143 CHARPOS (pos),
3144 bufpos,
3145 it->region_beg_charpos,
3146 it->region_end_charpos,
3147 &next_check_charpos,
3148 base_face_id, 0);
3149
3150 /* Correct the face for charsets different from ASCII. Do it
3151 for the multibyte case only. The face returned above is
3152 suitable for unibyte text if IT->string is unibyte. */
3153 if (STRING_MULTIBYTE (it->string))
3154 {
3155 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3156 int rest = SBYTES (it->string) - BYTEPOS (pos);
3157 int c, len;
3158 struct face *face = FACE_FROM_ID (it->f, face_id);
3159
3160 c = string_char_and_length (p, rest, &len);
3161 face_id = FACE_FOR_CHAR (it->f, face, c);
3162 }
3163 }
3164 else
3165 {
3166 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3167 || (IT_CHARPOS (*it) <= BEGV && before_p))
3168 return it->face_id;
3169
3170 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3171 pos = it->current.pos;
3172
3173 if (before_p)
3174 DEC_TEXT_POS (pos, it->multibyte_p);
3175 else
3176 {
3177 if (it->what == IT_COMPOSITION)
3178 /* For composition, we must check the position after the
3179 composition. */
3180 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3181 else
3182 INC_TEXT_POS (pos, it->multibyte_p);
3183 }
3184
3185 /* Determine face for CHARSET_ASCII, or unibyte. */
3186 face_id = face_at_buffer_position (it->w,
3187 CHARPOS (pos),
3188 it->region_beg_charpos,
3189 it->region_end_charpos,
3190 &next_check_charpos,
3191 limit, 0);
3192
3193 /* Correct the face for charsets different from ASCII. Do it
3194 for the multibyte case only. The face returned above is
3195 suitable for unibyte text if current_buffer is unibyte. */
3196 if (it->multibyte_p)
3197 {
3198 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3199 struct face *face = FACE_FROM_ID (it->f, face_id);
3200 face_id = FACE_FOR_CHAR (it->f, face, c);
3201 }
3202 }
3203
3204 return face_id;
3205 }
3206
3207
3208 \f
3209 /***********************************************************************
3210 Invisible text
3211 ***********************************************************************/
3212
3213 /* Set up iterator IT from invisible properties at its current
3214 position. Called from handle_stop. */
3215
3216 static enum prop_handled
3217 handle_invisible_prop (it)
3218 struct it *it;
3219 {
3220 enum prop_handled handled = HANDLED_NORMALLY;
3221
3222 if (STRINGP (it->string))
3223 {
3224 extern Lisp_Object Qinvisible;
3225 Lisp_Object prop, end_charpos, limit, charpos;
3226
3227 /* Get the value of the invisible text property at the
3228 current position. Value will be nil if there is no such
3229 property. */
3230 charpos = make_number (IT_STRING_CHARPOS (*it));
3231 prop = Fget_text_property (charpos, Qinvisible, it->string);
3232
3233 if (!NILP (prop)
3234 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3235 {
3236 handled = HANDLED_RECOMPUTE_PROPS;
3237
3238 /* Get the position at which the next change of the
3239 invisible text property can be found in IT->string.
3240 Value will be nil if the property value is the same for
3241 all the rest of IT->string. */
3242 XSETINT (limit, SCHARS (it->string));
3243 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3244 it->string, limit);
3245
3246 /* Text at current position is invisible. The next
3247 change in the property is at position end_charpos.
3248 Move IT's current position to that position. */
3249 if (INTEGERP (end_charpos)
3250 && XFASTINT (end_charpos) < XFASTINT (limit))
3251 {
3252 struct text_pos old;
3253 old = it->current.string_pos;
3254 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3255 compute_string_pos (&it->current.string_pos, old, it->string);
3256 }
3257 else
3258 {
3259 /* The rest of the string is invisible. If this is an
3260 overlay string, proceed with the next overlay string
3261 or whatever comes and return a character from there. */
3262 if (it->current.overlay_string_index >= 0)
3263 {
3264 next_overlay_string (it);
3265 /* Don't check for overlay strings when we just
3266 finished processing them. */
3267 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3268 }
3269 else
3270 {
3271 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3272 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3273 }
3274 }
3275 }
3276 }
3277 else
3278 {
3279 int invis_p, newpos, next_stop, start_charpos;
3280 Lisp_Object pos, prop, overlay;
3281
3282 /* First of all, is there invisible text at this position? */
3283 start_charpos = IT_CHARPOS (*it);
3284 pos = make_number (IT_CHARPOS (*it));
3285 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3286 &overlay);
3287 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3288
3289 /* If we are on invisible text, skip over it. */
3290 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3291 {
3292 /* Record whether we have to display an ellipsis for the
3293 invisible text. */
3294 int display_ellipsis_p = invis_p == 2;
3295
3296 handled = HANDLED_RECOMPUTE_PROPS;
3297
3298 /* Loop skipping over invisible text. The loop is left at
3299 ZV or with IT on the first char being visible again. */
3300 do
3301 {
3302 /* Try to skip some invisible text. Return value is the
3303 position reached which can be equal to IT's position
3304 if there is nothing invisible here. This skips both
3305 over invisible text properties and overlays with
3306 invisible property. */
3307 newpos = skip_invisible (IT_CHARPOS (*it),
3308 &next_stop, ZV, it->window);
3309
3310 /* If we skipped nothing at all we weren't at invisible
3311 text in the first place. If everything to the end of
3312 the buffer was skipped, end the loop. */
3313 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3314 invis_p = 0;
3315 else
3316 {
3317 /* We skipped some characters but not necessarily
3318 all there are. Check if we ended up on visible
3319 text. Fget_char_property returns the property of
3320 the char before the given position, i.e. if we
3321 get invis_p = 0, this means that the char at
3322 newpos is visible. */
3323 pos = make_number (newpos);
3324 prop = Fget_char_property (pos, Qinvisible, it->window);
3325 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3326 }
3327
3328 /* If we ended up on invisible text, proceed to
3329 skip starting with next_stop. */
3330 if (invis_p)
3331 IT_CHARPOS (*it) = next_stop;
3332 }
3333 while (invis_p);
3334
3335 /* The position newpos is now either ZV or on visible text. */
3336 IT_CHARPOS (*it) = newpos;
3337 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3338
3339 /* If there are before-strings at the start of invisible
3340 text, and the text is invisible because of a text
3341 property, arrange to show before-strings because 20.x did
3342 it that way. (If the text is invisible because of an
3343 overlay property instead of a text property, this is
3344 already handled in the overlay code.) */
3345 if (NILP (overlay)
3346 && get_overlay_strings (it, start_charpos))
3347 {
3348 handled = HANDLED_RECOMPUTE_PROPS;
3349 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3350 }
3351 else if (display_ellipsis_p)
3352 setup_for_ellipsis (it, 0);
3353 }
3354 }
3355
3356 return handled;
3357 }
3358
3359
3360 /* Make iterator IT return `...' next.
3361 Replaces LEN characters from buffer. */
3362
3363 static void
3364 setup_for_ellipsis (it, len)
3365 struct it *it;
3366 int len;
3367 {
3368 /* Use the display table definition for `...'. Invalid glyphs
3369 will be handled by the method returning elements from dpvec. */
3370 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3371 {
3372 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3373 it->dpvec = v->contents;
3374 it->dpend = v->contents + v->size;
3375 }
3376 else
3377 {
3378 /* Default `...'. */
3379 it->dpvec = default_invis_vector;
3380 it->dpend = default_invis_vector + 3;
3381 }
3382
3383 it->dpvec_char_len = len;
3384 it->current.dpvec_index = 0;
3385 it->dpvec_face_id = -1;
3386
3387 /* Remember the current face id in case glyphs specify faces.
3388 IT's face is restored in set_iterator_to_next.
3389 saved_face_id was set to preceding char's face in handle_stop. */
3390 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3391 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3392
3393 it->method = GET_FROM_DISPLAY_VECTOR;
3394 it->ellipsis_p = 1;
3395 }
3396
3397
3398 \f
3399 /***********************************************************************
3400 'display' property
3401 ***********************************************************************/
3402
3403 /* Set up iterator IT from `display' property at its current position.
3404 Called from handle_stop.
3405 We return HANDLED_RETURN if some part of the display property
3406 overrides the display of the buffer text itself.
3407 Otherwise we return HANDLED_NORMALLY. */
3408
3409 static enum prop_handled
3410 handle_display_prop (it)
3411 struct it *it;
3412 {
3413 Lisp_Object prop, object;
3414 struct text_pos *position;
3415 /* Nonzero if some property replaces the display of the text itself. */
3416 int display_replaced_p = 0;
3417
3418 if (STRINGP (it->string))
3419 {
3420 object = it->string;
3421 position = &it->current.string_pos;
3422 }
3423 else
3424 {
3425 object = it->w->buffer;
3426 position = &it->current.pos;
3427 }
3428
3429 /* Reset those iterator values set from display property values. */
3430 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3431 it->space_width = Qnil;
3432 it->font_height = Qnil;
3433 it->voffset = 0;
3434
3435 /* We don't support recursive `display' properties, i.e. string
3436 values that have a string `display' property, that have a string
3437 `display' property etc. */
3438 if (!it->string_from_display_prop_p)
3439 it->area = TEXT_AREA;
3440
3441 prop = Fget_char_property (make_number (position->charpos),
3442 Qdisplay, object);
3443 if (NILP (prop))
3444 return HANDLED_NORMALLY;
3445
3446 if (CONSP (prop)
3447 /* Simple properties. */
3448 && !EQ (XCAR (prop), Qimage)
3449 && !EQ (XCAR (prop), Qspace)
3450 && !EQ (XCAR (prop), Qwhen)
3451 && !EQ (XCAR (prop), Qslice)
3452 && !EQ (XCAR (prop), Qspace_width)
3453 && !EQ (XCAR (prop), Qheight)
3454 && !EQ (XCAR (prop), Qraise)
3455 /* Marginal area specifications. */
3456 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3457 && !EQ (XCAR (prop), Qleft_fringe)
3458 && !EQ (XCAR (prop), Qright_fringe)
3459 && !NILP (XCAR (prop)))
3460 {
3461 for (; CONSP (prop); prop = XCDR (prop))
3462 {
3463 if (handle_single_display_spec (it, XCAR (prop), object,
3464 position, display_replaced_p))
3465 display_replaced_p = 1;
3466 }
3467 }
3468 else if (VECTORP (prop))
3469 {
3470 int i;
3471 for (i = 0; i < ASIZE (prop); ++i)
3472 if (handle_single_display_spec (it, AREF (prop, i), object,
3473 position, display_replaced_p))
3474 display_replaced_p = 1;
3475 }
3476 else
3477 {
3478 int ret = handle_single_display_spec (it, prop, object, position, 0);
3479 if (ret < 0) /* Replaced by "", i.e. nothing. */
3480 return HANDLED_RECOMPUTE_PROPS;
3481 if (ret)
3482 display_replaced_p = 1;
3483 }
3484
3485 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3486 }
3487
3488
3489 /* Value is the position of the end of the `display' property starting
3490 at START_POS in OBJECT. */
3491
3492 static struct text_pos
3493 display_prop_end (it, object, start_pos)
3494 struct it *it;
3495 Lisp_Object object;
3496 struct text_pos start_pos;
3497 {
3498 Lisp_Object end;
3499 struct text_pos end_pos;
3500
3501 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3502 Qdisplay, object, Qnil);
3503 CHARPOS (end_pos) = XFASTINT (end);
3504 if (STRINGP (object))
3505 compute_string_pos (&end_pos, start_pos, it->string);
3506 else
3507 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3508
3509 return end_pos;
3510 }
3511
3512
3513 /* Set up IT from a single `display' specification PROP. OBJECT
3514 is the object in which the `display' property was found. *POSITION
3515 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3516 means that we previously saw a display specification which already
3517 replaced text display with something else, for example an image;
3518 we ignore such properties after the first one has been processed.
3519
3520 If PROP is a `space' or `image' specification, and in some other
3521 cases too, set *POSITION to the position where the `display'
3522 property ends.
3523
3524 Value is non-zero if something was found which replaces the display
3525 of buffer or string text. Specifically, the value is -1 if that
3526 "something" is "nothing". */
3527
3528 static int
3529 handle_single_display_spec (it, spec, object, position,
3530 display_replaced_before_p)
3531 struct it *it;
3532 Lisp_Object spec;
3533 Lisp_Object object;
3534 struct text_pos *position;
3535 int display_replaced_before_p;
3536 {
3537 Lisp_Object form;
3538 Lisp_Object location, value;
3539 struct text_pos start_pos;
3540 int valid_p;
3541
3542 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3543 If the result is non-nil, use VALUE instead of SPEC. */
3544 form = Qt;
3545 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3546 {
3547 spec = XCDR (spec);
3548 if (!CONSP (spec))
3549 return 0;
3550 form = XCAR (spec);
3551 spec = XCDR (spec);
3552 }
3553
3554 if (!NILP (form) && !EQ (form, Qt))
3555 {
3556 int count = SPECPDL_INDEX ();
3557 struct gcpro gcpro1;
3558
3559 /* Bind `object' to the object having the `display' property, a
3560 buffer or string. Bind `position' to the position in the
3561 object where the property was found, and `buffer-position'
3562 to the current position in the buffer. */
3563 specbind (Qobject, object);
3564 specbind (Qposition, make_number (CHARPOS (*position)));
3565 specbind (Qbuffer_position,
3566 make_number (STRINGP (object)
3567 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3568 GCPRO1 (form);
3569 form = safe_eval (form);
3570 UNGCPRO;
3571 unbind_to (count, Qnil);
3572 }
3573
3574 if (NILP (form))
3575 return 0;
3576
3577 /* Handle `(height HEIGHT)' specifications. */
3578 if (CONSP (spec)
3579 && EQ (XCAR (spec), Qheight)
3580 && CONSP (XCDR (spec)))
3581 {
3582 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3583 return 0;
3584
3585 it->font_height = XCAR (XCDR (spec));
3586 if (!NILP (it->font_height))
3587 {
3588 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3589 int new_height = -1;
3590
3591 if (CONSP (it->font_height)
3592 && (EQ (XCAR (it->font_height), Qplus)
3593 || EQ (XCAR (it->font_height), Qminus))
3594 && CONSP (XCDR (it->font_height))
3595 && INTEGERP (XCAR (XCDR (it->font_height))))
3596 {
3597 /* `(+ N)' or `(- N)' where N is an integer. */
3598 int steps = XINT (XCAR (XCDR (it->font_height)));
3599 if (EQ (XCAR (it->font_height), Qplus))
3600 steps = - steps;
3601 it->face_id = smaller_face (it->f, it->face_id, steps);
3602 }
3603 else if (FUNCTIONP (it->font_height))
3604 {
3605 /* Call function with current height as argument.
3606 Value is the new height. */
3607 Lisp_Object height;
3608 height = safe_call1 (it->font_height,
3609 face->lface[LFACE_HEIGHT_INDEX]);
3610 if (NUMBERP (height))
3611 new_height = XFLOATINT (height);
3612 }
3613 else if (NUMBERP (it->font_height))
3614 {
3615 /* Value is a multiple of the canonical char height. */
3616 struct face *face;
3617
3618 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3619 new_height = (XFLOATINT (it->font_height)
3620 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3621 }
3622 else
3623 {
3624 /* Evaluate IT->font_height with `height' bound to the
3625 current specified height to get the new height. */
3626 int count = SPECPDL_INDEX ();
3627
3628 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3629 value = safe_eval (it->font_height);
3630 unbind_to (count, Qnil);
3631
3632 if (NUMBERP (value))
3633 new_height = XFLOATINT (value);
3634 }
3635
3636 if (new_height > 0)
3637 it->face_id = face_with_height (it->f, it->face_id, new_height);
3638 }
3639
3640 return 0;
3641 }
3642
3643 /* Handle `(space_width WIDTH)'. */
3644 if (CONSP (spec)
3645 && EQ (XCAR (spec), Qspace_width)
3646 && CONSP (XCDR (spec)))
3647 {
3648 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3649 return 0;
3650
3651 value = XCAR (XCDR (spec));
3652 if (NUMBERP (value) && XFLOATINT (value) > 0)
3653 it->space_width = value;
3654
3655 return 0;
3656 }
3657
3658 /* Handle `(slice X Y WIDTH HEIGHT)'. */
3659 if (CONSP (spec)
3660 && EQ (XCAR (spec), Qslice))
3661 {
3662 Lisp_Object tem;
3663
3664 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3665 return 0;
3666
3667 if (tem = XCDR (spec), CONSP (tem))
3668 {
3669 it->slice.x = XCAR (tem);
3670 if (tem = XCDR (tem), CONSP (tem))
3671 {
3672 it->slice.y = XCAR (tem);
3673 if (tem = XCDR (tem), CONSP (tem))
3674 {
3675 it->slice.width = XCAR (tem);
3676 if (tem = XCDR (tem), CONSP (tem))
3677 it->slice.height = XCAR (tem);
3678 }
3679 }
3680 }
3681
3682 return 0;
3683 }
3684
3685 /* Handle `(raise FACTOR)'. */
3686 if (CONSP (spec)
3687 && EQ (XCAR (spec), Qraise)
3688 && CONSP (XCDR (spec)))
3689 {
3690 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3691 return 0;
3692
3693 #ifdef HAVE_WINDOW_SYSTEM
3694 value = XCAR (XCDR (spec));
3695 if (NUMBERP (value))
3696 {
3697 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3698 it->voffset = - (XFLOATINT (value)
3699 * (FONT_HEIGHT (face->font)));
3700 }
3701 #endif /* HAVE_WINDOW_SYSTEM */
3702
3703 return 0;
3704 }
3705
3706 /* Don't handle the other kinds of display specifications
3707 inside a string that we got from a `display' property. */
3708 if (it->string_from_display_prop_p)
3709 return 0;
3710
3711 /* Characters having this form of property are not displayed, so
3712 we have to find the end of the property. */
3713 start_pos = *position;
3714 *position = display_prop_end (it, object, start_pos);
3715 value = Qnil;
3716
3717 /* Stop the scan at that end position--we assume that all
3718 text properties change there. */
3719 it->stop_charpos = position->charpos;
3720
3721 /* Handle `(left-fringe BITMAP [FACE])'
3722 and `(right-fringe BITMAP [FACE])'. */
3723 if (CONSP (spec)
3724 && (EQ (XCAR (spec), Qleft_fringe)
3725 || EQ (XCAR (spec), Qright_fringe))
3726 && CONSP (XCDR (spec)))
3727 {
3728 int face_id = DEFAULT_FACE_ID;
3729 int fringe_bitmap;
3730
3731 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3732 /* If we return here, POSITION has been advanced
3733 across the text with this property. */
3734 return 0;
3735
3736 #ifdef HAVE_WINDOW_SYSTEM
3737 value = XCAR (XCDR (spec));
3738 if (!SYMBOLP (value)
3739 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
3740 /* If we return here, POSITION has been advanced
3741 across the text with this property. */
3742 return 0;
3743
3744 if (CONSP (XCDR (XCDR (spec))))
3745 {
3746 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
3747 int face_id2 = lookup_derived_face (it->f, face_name,
3748 'A', FRINGE_FACE_ID, 0);
3749 if (face_id2 >= 0)
3750 face_id = face_id2;
3751 }
3752
3753 /* Save current settings of IT so that we can restore them
3754 when we are finished with the glyph property value. */
3755
3756 push_it (it);
3757
3758 it->area = TEXT_AREA;
3759 it->what = IT_IMAGE;
3760 it->image_id = -1; /* no image */
3761 it->position = start_pos;
3762 it->object = NILP (object) ? it->w->buffer : object;
3763 it->method = GET_FROM_IMAGE;
3764 it->face_id = face_id;
3765
3766 /* Say that we haven't consumed the characters with
3767 `display' property yet. The call to pop_it in
3768 set_iterator_to_next will clean this up. */
3769 *position = start_pos;
3770
3771 if (EQ (XCAR (spec), Qleft_fringe))
3772 {
3773 it->left_user_fringe_bitmap = fringe_bitmap;
3774 it->left_user_fringe_face_id = face_id;
3775 }
3776 else
3777 {
3778 it->right_user_fringe_bitmap = fringe_bitmap;
3779 it->right_user_fringe_face_id = face_id;
3780 }
3781 #endif /* HAVE_WINDOW_SYSTEM */
3782 return 1;
3783 }
3784
3785 /* Prepare to handle `((margin left-margin) ...)',
3786 `((margin right-margin) ...)' and `((margin nil) ...)'
3787 prefixes for display specifications. */
3788 location = Qunbound;
3789 if (CONSP (spec) && CONSP (XCAR (spec)))
3790 {
3791 Lisp_Object tem;
3792
3793 value = XCDR (spec);
3794 if (CONSP (value))
3795 value = XCAR (value);
3796
3797 tem = XCAR (spec);
3798 if (EQ (XCAR (tem), Qmargin)
3799 && (tem = XCDR (tem),
3800 tem = CONSP (tem) ? XCAR (tem) : Qnil,
3801 (NILP (tem)
3802 || EQ (tem, Qleft_margin)
3803 || EQ (tem, Qright_margin))))
3804 location = tem;
3805 }
3806
3807 if (EQ (location, Qunbound))
3808 {
3809 location = Qnil;
3810 value = spec;
3811 }
3812
3813 /* After this point, VALUE is the property after any
3814 margin prefix has been stripped. It must be a string,
3815 an image specification, or `(space ...)'.
3816
3817 LOCATION specifies where to display: `left-margin',
3818 `right-margin' or nil. */
3819
3820 valid_p = (STRINGP (value)
3821 #ifdef HAVE_WINDOW_SYSTEM
3822 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
3823 #endif /* not HAVE_WINDOW_SYSTEM */
3824 || (CONSP (value) && EQ (XCAR (value), Qspace)));
3825
3826 if (valid_p && !display_replaced_before_p)
3827 {
3828 /* Save current settings of IT so that we can restore them
3829 when we are finished with the glyph property value. */
3830 push_it (it);
3831
3832 if (NILP (location))
3833 it->area = TEXT_AREA;
3834 else if (EQ (location, Qleft_margin))
3835 it->area = LEFT_MARGIN_AREA;
3836 else
3837 it->area = RIGHT_MARGIN_AREA;
3838
3839 if (STRINGP (value))
3840 {
3841 if (SCHARS (value) == 0)
3842 {
3843 pop_it (it);
3844 return -1; /* Replaced by "", i.e. nothing. */
3845 }
3846 it->string = value;
3847 it->multibyte_p = STRING_MULTIBYTE (it->string);
3848 it->current.overlay_string_index = -1;
3849 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
3850 it->end_charpos = it->string_nchars = SCHARS (it->string);
3851 it->method = GET_FROM_STRING;
3852 it->stop_charpos = 0;
3853 it->string_from_display_prop_p = 1;
3854 /* Say that we haven't consumed the characters with
3855 `display' property yet. The call to pop_it in
3856 set_iterator_to_next will clean this up. */
3857 *position = start_pos;
3858 }
3859 else if (CONSP (value) && EQ (XCAR (value), Qspace))
3860 {
3861 it->method = GET_FROM_STRETCH;
3862 it->object = value;
3863 it->current.pos = it->position = start_pos;
3864 }
3865 #ifdef HAVE_WINDOW_SYSTEM
3866 else
3867 {
3868 it->what = IT_IMAGE;
3869 it->image_id = lookup_image (it->f, value);
3870 it->position = start_pos;
3871 it->object = NILP (object) ? it->w->buffer : object;
3872 it->method = GET_FROM_IMAGE;
3873
3874 /* Say that we haven't consumed the characters with
3875 `display' property yet. The call to pop_it in
3876 set_iterator_to_next will clean this up. */
3877 *position = start_pos;
3878 }
3879 #endif /* HAVE_WINDOW_SYSTEM */
3880
3881 return 1;
3882 }
3883
3884 /* Invalid property or property not supported. Restore
3885 POSITION to what it was before. */
3886 *position = start_pos;
3887 return 0;
3888 }
3889
3890
3891 /* Check if SPEC is a display specification value whose text should be
3892 treated as intangible. */
3893
3894 static int
3895 single_display_spec_intangible_p (prop)
3896 Lisp_Object prop;
3897 {
3898 /* Skip over `when FORM'. */
3899 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3900 {
3901 prop = XCDR (prop);
3902 if (!CONSP (prop))
3903 return 0;
3904 prop = XCDR (prop);
3905 }
3906
3907 if (STRINGP (prop))
3908 return 1;
3909
3910 if (!CONSP (prop))
3911 return 0;
3912
3913 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
3914 we don't need to treat text as intangible. */
3915 if (EQ (XCAR (prop), Qmargin))
3916 {
3917 prop = XCDR (prop);
3918 if (!CONSP (prop))
3919 return 0;
3920
3921 prop = XCDR (prop);
3922 if (!CONSP (prop)
3923 || EQ (XCAR (prop), Qleft_margin)
3924 || EQ (XCAR (prop), Qright_margin))
3925 return 0;
3926 }
3927
3928 return (CONSP (prop)
3929 && (EQ (XCAR (prop), Qimage)
3930 || EQ (XCAR (prop), Qspace)));
3931 }
3932
3933
3934 /* Check if PROP is a display property value whose text should be
3935 treated as intangible. */
3936
3937 int
3938 display_prop_intangible_p (prop)
3939 Lisp_Object prop;
3940 {
3941 if (CONSP (prop)
3942 && CONSP (XCAR (prop))
3943 && !EQ (Qmargin, XCAR (XCAR (prop))))
3944 {
3945 /* A list of sub-properties. */
3946 while (CONSP (prop))
3947 {
3948 if (single_display_spec_intangible_p (XCAR (prop)))
3949 return 1;
3950 prop = XCDR (prop);
3951 }
3952 }
3953 else if (VECTORP (prop))
3954 {
3955 /* A vector of sub-properties. */
3956 int i;
3957 for (i = 0; i < ASIZE (prop); ++i)
3958 if (single_display_spec_intangible_p (AREF (prop, i)))
3959 return 1;
3960 }
3961 else
3962 return single_display_spec_intangible_p (prop);
3963
3964 return 0;
3965 }
3966
3967
3968 /* Return 1 if PROP is a display sub-property value containing STRING. */
3969
3970 static int
3971 single_display_spec_string_p (prop, string)
3972 Lisp_Object prop, string;
3973 {
3974 if (EQ (string, prop))
3975 return 1;
3976
3977 /* Skip over `when FORM'. */
3978 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
3979 {
3980 prop = XCDR (prop);
3981 if (!CONSP (prop))
3982 return 0;
3983 prop = XCDR (prop);
3984 }
3985
3986 if (CONSP (prop))
3987 /* Skip over `margin LOCATION'. */
3988 if (EQ (XCAR (prop), Qmargin))
3989 {
3990 prop = XCDR (prop);
3991 if (!CONSP (prop))
3992 return 0;
3993
3994 prop = XCDR (prop);
3995 if (!CONSP (prop))
3996 return 0;
3997 }
3998
3999 return CONSP (prop) && EQ (XCAR (prop), string);
4000 }
4001
4002
4003 /* Return 1 if STRING appears in the `display' property PROP. */
4004
4005 static int
4006 display_prop_string_p (prop, string)
4007 Lisp_Object prop, string;
4008 {
4009 if (CONSP (prop)
4010 && CONSP (XCAR (prop))
4011 && !EQ (Qmargin, XCAR (XCAR (prop))))
4012 {
4013 /* A list of sub-properties. */
4014 while (CONSP (prop))
4015 {
4016 if (single_display_spec_string_p (XCAR (prop), string))
4017 return 1;
4018 prop = XCDR (prop);
4019 }
4020 }
4021 else if (VECTORP (prop))
4022 {
4023 /* A vector of sub-properties. */
4024 int i;
4025 for (i = 0; i < ASIZE (prop); ++i)
4026 if (single_display_spec_string_p (AREF (prop, i), string))
4027 return 1;
4028 }
4029 else
4030 return single_display_spec_string_p (prop, string);
4031
4032 return 0;
4033 }
4034
4035
4036 /* Determine from which buffer position in W's buffer STRING comes
4037 from. AROUND_CHARPOS is an approximate position where it could
4038 be from. Value is the buffer position or 0 if it couldn't be
4039 determined.
4040
4041 W's buffer must be current.
4042
4043 This function is necessary because we don't record buffer positions
4044 in glyphs generated from strings (to keep struct glyph small).
4045 This function may only use code that doesn't eval because it is
4046 called asynchronously from note_mouse_highlight. */
4047
4048 int
4049 string_buffer_position (w, string, around_charpos)
4050 struct window *w;
4051 Lisp_Object string;
4052 int around_charpos;
4053 {
4054 Lisp_Object limit, prop, pos;
4055 const int MAX_DISTANCE = 1000;
4056 int found = 0;
4057
4058 pos = make_number (around_charpos);
4059 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4060 while (!found && !EQ (pos, limit))
4061 {
4062 prop = Fget_char_property (pos, Qdisplay, Qnil);
4063 if (!NILP (prop) && display_prop_string_p (prop, string))
4064 found = 1;
4065 else
4066 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4067 }
4068
4069 if (!found)
4070 {
4071 pos = make_number (around_charpos);
4072 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4073 while (!found && !EQ (pos, limit))
4074 {
4075 prop = Fget_char_property (pos, Qdisplay, Qnil);
4076 if (!NILP (prop) && display_prop_string_p (prop, string))
4077 found = 1;
4078 else
4079 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4080 limit);
4081 }
4082 }
4083
4084 return found ? XINT (pos) : 0;
4085 }
4086
4087
4088 \f
4089 /***********************************************************************
4090 `composition' property
4091 ***********************************************************************/
4092
4093 /* Set up iterator IT from `composition' property at its current
4094 position. Called from handle_stop. */
4095
4096 static enum prop_handled
4097 handle_composition_prop (it)
4098 struct it *it;
4099 {
4100 Lisp_Object prop, string;
4101 int pos, pos_byte, end;
4102 enum prop_handled handled = HANDLED_NORMALLY;
4103
4104 if (STRINGP (it->string))
4105 {
4106 pos = IT_STRING_CHARPOS (*it);
4107 pos_byte = IT_STRING_BYTEPOS (*it);
4108 string = it->string;
4109 }
4110 else
4111 {
4112 pos = IT_CHARPOS (*it);
4113 pos_byte = IT_BYTEPOS (*it);
4114 string = Qnil;
4115 }
4116
4117 /* If there's a valid composition and point is not inside of the
4118 composition (in the case that the composition is from the current
4119 buffer), draw a glyph composed from the composition components. */
4120 if (find_composition (pos, -1, &pos, &end, &prop, string)
4121 && COMPOSITION_VALID_P (pos, end, prop)
4122 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4123 {
4124 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4125
4126 if (id >= 0)
4127 {
4128 it->method = GET_FROM_COMPOSITION;
4129 it->cmp_id = id;
4130 it->cmp_len = COMPOSITION_LENGTH (prop);
4131 /* For a terminal, draw only the first character of the
4132 components. */
4133 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4134 it->len = (STRINGP (it->string)
4135 ? string_char_to_byte (it->string, end)
4136 : CHAR_TO_BYTE (end)) - pos_byte;
4137 it->stop_charpos = end;
4138 handled = HANDLED_RETURN;
4139 }
4140 }
4141
4142 return handled;
4143 }
4144
4145
4146 \f
4147 /***********************************************************************
4148 Overlay strings
4149 ***********************************************************************/
4150
4151 /* The following structure is used to record overlay strings for
4152 later sorting in load_overlay_strings. */
4153
4154 struct overlay_entry
4155 {
4156 Lisp_Object overlay;
4157 Lisp_Object string;
4158 int priority;
4159 int after_string_p;
4160 };
4161
4162
4163 /* Set up iterator IT from overlay strings at its current position.
4164 Called from handle_stop. */
4165
4166 static enum prop_handled
4167 handle_overlay_change (it)
4168 struct it *it;
4169 {
4170 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4171 return HANDLED_RECOMPUTE_PROPS;
4172 else
4173 return HANDLED_NORMALLY;
4174 }
4175
4176
4177 /* Set up the next overlay string for delivery by IT, if there is an
4178 overlay string to deliver. Called by set_iterator_to_next when the
4179 end of the current overlay string is reached. If there are more
4180 overlay strings to display, IT->string and
4181 IT->current.overlay_string_index are set appropriately here.
4182 Otherwise IT->string is set to nil. */
4183
4184 static void
4185 next_overlay_string (it)
4186 struct it *it;
4187 {
4188 ++it->current.overlay_string_index;
4189 if (it->current.overlay_string_index == it->n_overlay_strings)
4190 {
4191 /* No more overlay strings. Restore IT's settings to what
4192 they were before overlay strings were processed, and
4193 continue to deliver from current_buffer. */
4194 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4195
4196 pop_it (it);
4197 xassert (it->stop_charpos >= BEGV
4198 && it->stop_charpos <= it->end_charpos);
4199 it->string = Qnil;
4200 it->current.overlay_string_index = -1;
4201 SET_TEXT_POS (it->current.string_pos, -1, -1);
4202 it->n_overlay_strings = 0;
4203 it->method = GET_FROM_BUFFER;
4204
4205 /* If we're at the end of the buffer, record that we have
4206 processed the overlay strings there already, so that
4207 next_element_from_buffer doesn't try it again. */
4208 if (IT_CHARPOS (*it) >= it->end_charpos)
4209 it->overlay_strings_at_end_processed_p = 1;
4210
4211 /* If we have to display `...' for invisible text, set
4212 the iterator up for that. */
4213 if (display_ellipsis_p)
4214 setup_for_ellipsis (it, 0);
4215 }
4216 else
4217 {
4218 /* There are more overlay strings to process. If
4219 IT->current.overlay_string_index has advanced to a position
4220 where we must load IT->overlay_strings with more strings, do
4221 it. */
4222 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4223
4224 if (it->current.overlay_string_index && i == 0)
4225 load_overlay_strings (it, 0);
4226
4227 /* Initialize IT to deliver display elements from the overlay
4228 string. */
4229 it->string = it->overlay_strings[i];
4230 it->multibyte_p = STRING_MULTIBYTE (it->string);
4231 SET_TEXT_POS (it->current.string_pos, 0, 0);
4232 it->method = GET_FROM_STRING;
4233 it->stop_charpos = 0;
4234 }
4235
4236 CHECK_IT (it);
4237 }
4238
4239
4240 /* Compare two overlay_entry structures E1 and E2. Used as a
4241 comparison function for qsort in load_overlay_strings. Overlay
4242 strings for the same position are sorted so that
4243
4244 1. All after-strings come in front of before-strings, except
4245 when they come from the same overlay.
4246
4247 2. Within after-strings, strings are sorted so that overlay strings
4248 from overlays with higher priorities come first.
4249
4250 2. Within before-strings, strings are sorted so that overlay
4251 strings from overlays with higher priorities come last.
4252
4253 Value is analogous to strcmp. */
4254
4255
4256 static int
4257 compare_overlay_entries (e1, e2)
4258 void *e1, *e2;
4259 {
4260 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4261 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4262 int result;
4263
4264 if (entry1->after_string_p != entry2->after_string_p)
4265 {
4266 /* Let after-strings appear in front of before-strings if
4267 they come from different overlays. */
4268 if (EQ (entry1->overlay, entry2->overlay))
4269 result = entry1->after_string_p ? 1 : -1;
4270 else
4271 result = entry1->after_string_p ? -1 : 1;
4272 }
4273 else if (entry1->after_string_p)
4274 /* After-strings sorted in order of decreasing priority. */
4275 result = entry2->priority - entry1->priority;
4276 else
4277 /* Before-strings sorted in order of increasing priority. */
4278 result = entry1->priority - entry2->priority;
4279
4280 return result;
4281 }
4282
4283
4284 /* Load the vector IT->overlay_strings with overlay strings from IT's
4285 current buffer position, or from CHARPOS if that is > 0. Set
4286 IT->n_overlays to the total number of overlay strings found.
4287
4288 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4289 a time. On entry into load_overlay_strings,
4290 IT->current.overlay_string_index gives the number of overlay
4291 strings that have already been loaded by previous calls to this
4292 function.
4293
4294 IT->add_overlay_start contains an additional overlay start
4295 position to consider for taking overlay strings from, if non-zero.
4296 This position comes into play when the overlay has an `invisible'
4297 property, and both before and after-strings. When we've skipped to
4298 the end of the overlay, because of its `invisible' property, we
4299 nevertheless want its before-string to appear.
4300 IT->add_overlay_start will contain the overlay start position
4301 in this case.
4302
4303 Overlay strings are sorted so that after-string strings come in
4304 front of before-string strings. Within before and after-strings,
4305 strings are sorted by overlay priority. See also function
4306 compare_overlay_entries. */
4307
4308 static void
4309 load_overlay_strings (it, charpos)
4310 struct it *it;
4311 int charpos;
4312 {
4313 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4314 Lisp_Object overlay, window, str, invisible;
4315 struct Lisp_Overlay *ov;
4316 int start, end;
4317 int size = 20;
4318 int n = 0, i, j, invis_p;
4319 struct overlay_entry *entries
4320 = (struct overlay_entry *) alloca (size * sizeof *entries);
4321
4322 if (charpos <= 0)
4323 charpos = IT_CHARPOS (*it);
4324
4325 /* Append the overlay string STRING of overlay OVERLAY to vector
4326 `entries' which has size `size' and currently contains `n'
4327 elements. AFTER_P non-zero means STRING is an after-string of
4328 OVERLAY. */
4329 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4330 do \
4331 { \
4332 Lisp_Object priority; \
4333 \
4334 if (n == size) \
4335 { \
4336 int new_size = 2 * size; \
4337 struct overlay_entry *old = entries; \
4338 entries = \
4339 (struct overlay_entry *) alloca (new_size \
4340 * sizeof *entries); \
4341 bcopy (old, entries, size * sizeof *entries); \
4342 size = new_size; \
4343 } \
4344 \
4345 entries[n].string = (STRING); \
4346 entries[n].overlay = (OVERLAY); \
4347 priority = Foverlay_get ((OVERLAY), Qpriority); \
4348 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4349 entries[n].after_string_p = (AFTER_P); \
4350 ++n; \
4351 } \
4352 while (0)
4353
4354 /* Process overlay before the overlay center. */
4355 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4356 {
4357 XSETMISC (overlay, ov);
4358 xassert (OVERLAYP (overlay));
4359 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4360 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4361
4362 if (end < charpos)
4363 break;
4364
4365 /* Skip this overlay if it doesn't start or end at IT's current
4366 position. */
4367 if (end != charpos && start != charpos)
4368 continue;
4369
4370 /* Skip this overlay if it doesn't apply to IT->w. */
4371 window = Foverlay_get (overlay, Qwindow);
4372 if (WINDOWP (window) && XWINDOW (window) != it->w)
4373 continue;
4374
4375 /* If the text ``under'' the overlay is invisible, both before-
4376 and after-strings from this overlay are visible; start and
4377 end position are indistinguishable. */
4378 invisible = Foverlay_get (overlay, Qinvisible);
4379 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4380
4381 /* If overlay has a non-empty before-string, record it. */
4382 if ((start == charpos || (end == charpos && invis_p))
4383 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4384 && SCHARS (str))
4385 RECORD_OVERLAY_STRING (overlay, str, 0);
4386
4387 /* If overlay has a non-empty after-string, record it. */
4388 if ((end == charpos || (start == charpos && invis_p))
4389 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4390 && SCHARS (str))
4391 RECORD_OVERLAY_STRING (overlay, str, 1);
4392 }
4393
4394 /* Process overlays after the overlay center. */
4395 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4396 {
4397 XSETMISC (overlay, ov);
4398 xassert (OVERLAYP (overlay));
4399 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4400 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4401
4402 if (start > charpos)
4403 break;
4404
4405 /* Skip this overlay if it doesn't start or end at IT's current
4406 position. */
4407 if (end != charpos && start != charpos)
4408 continue;
4409
4410 /* Skip this overlay if it doesn't apply to IT->w. */
4411 window = Foverlay_get (overlay, Qwindow);
4412 if (WINDOWP (window) && XWINDOW (window) != it->w)
4413 continue;
4414
4415 /* If the text ``under'' the overlay is invisible, it has a zero
4416 dimension, and both before- and after-strings apply. */
4417 invisible = Foverlay_get (overlay, Qinvisible);
4418 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4419
4420 /* If overlay has a non-empty before-string, record it. */
4421 if ((start == charpos || (end == charpos && invis_p))
4422 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4423 && SCHARS (str))
4424 RECORD_OVERLAY_STRING (overlay, str, 0);
4425
4426 /* If overlay has a non-empty after-string, record it. */
4427 if ((end == charpos || (start == charpos && invis_p))
4428 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4429 && SCHARS (str))
4430 RECORD_OVERLAY_STRING (overlay, str, 1);
4431 }
4432
4433 #undef RECORD_OVERLAY_STRING
4434
4435 /* Sort entries. */
4436 if (n > 1)
4437 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4438
4439 /* Record the total number of strings to process. */
4440 it->n_overlay_strings = n;
4441
4442 /* IT->current.overlay_string_index is the number of overlay strings
4443 that have already been consumed by IT. Copy some of the
4444 remaining overlay strings to IT->overlay_strings. */
4445 i = 0;
4446 j = it->current.overlay_string_index;
4447 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4448 it->overlay_strings[i++] = entries[j++].string;
4449
4450 CHECK_IT (it);
4451 }
4452
4453
4454 /* Get the first chunk of overlay strings at IT's current buffer
4455 position, or at CHARPOS if that is > 0. Value is non-zero if at
4456 least one overlay string was found. */
4457
4458 static int
4459 get_overlay_strings (it, charpos)
4460 struct it *it;
4461 int charpos;
4462 {
4463 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4464 process. This fills IT->overlay_strings with strings, and sets
4465 IT->n_overlay_strings to the total number of strings to process.
4466 IT->pos.overlay_string_index has to be set temporarily to zero
4467 because load_overlay_strings needs this; it must be set to -1
4468 when no overlay strings are found because a zero value would
4469 indicate a position in the first overlay string. */
4470 it->current.overlay_string_index = 0;
4471 load_overlay_strings (it, charpos);
4472
4473 /* If we found overlay strings, set up IT to deliver display
4474 elements from the first one. Otherwise set up IT to deliver
4475 from current_buffer. */
4476 if (it->n_overlay_strings)
4477 {
4478 /* Make sure we know settings in current_buffer, so that we can
4479 restore meaningful values when we're done with the overlay
4480 strings. */
4481 compute_stop_pos (it);
4482 xassert (it->face_id >= 0);
4483
4484 /* Save IT's settings. They are restored after all overlay
4485 strings have been processed. */
4486 xassert (it->sp == 0);
4487 push_it (it);
4488
4489 /* Set up IT to deliver display elements from the first overlay
4490 string. */
4491 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4492 it->string = it->overlay_strings[0];
4493 it->stop_charpos = 0;
4494 xassert (STRINGP (it->string));
4495 it->end_charpos = SCHARS (it->string);
4496 it->multibyte_p = STRING_MULTIBYTE (it->string);
4497 it->method = GET_FROM_STRING;
4498 }
4499 else
4500 {
4501 it->string = Qnil;
4502 it->current.overlay_string_index = -1;
4503 it->method = GET_FROM_BUFFER;
4504 }
4505
4506 CHECK_IT (it);
4507
4508 /* Value is non-zero if we found at least one overlay string. */
4509 return STRINGP (it->string);
4510 }
4511
4512
4513 \f
4514 /***********************************************************************
4515 Saving and restoring state
4516 ***********************************************************************/
4517
4518 /* Save current settings of IT on IT->stack. Called, for example,
4519 before setting up IT for an overlay string, to be able to restore
4520 IT's settings to what they were after the overlay string has been
4521 processed. */
4522
4523 static void
4524 push_it (it)
4525 struct it *it;
4526 {
4527 struct iterator_stack_entry *p;
4528
4529 xassert (it->sp < 2);
4530 p = it->stack + it->sp;
4531
4532 p->stop_charpos = it->stop_charpos;
4533 xassert (it->face_id >= 0);
4534 p->face_id = it->face_id;
4535 p->string = it->string;
4536 p->pos = it->current;
4537 p->end_charpos = it->end_charpos;
4538 p->string_nchars = it->string_nchars;
4539 p->area = it->area;
4540 p->multibyte_p = it->multibyte_p;
4541 p->slice = it->slice;
4542 p->space_width = it->space_width;
4543 p->font_height = it->font_height;
4544 p->voffset = it->voffset;
4545 p->string_from_display_prop_p = it->string_from_display_prop_p;
4546 p->display_ellipsis_p = 0;
4547 ++it->sp;
4548 }
4549
4550
4551 /* Restore IT's settings from IT->stack. Called, for example, when no
4552 more overlay strings must be processed, and we return to delivering
4553 display elements from a buffer, or when the end of a string from a
4554 `display' property is reached and we return to delivering display
4555 elements from an overlay string, or from a buffer. */
4556
4557 static void
4558 pop_it (it)
4559 struct it *it;
4560 {
4561 struct iterator_stack_entry *p;
4562
4563 xassert (it->sp > 0);
4564 --it->sp;
4565 p = it->stack + it->sp;
4566 it->stop_charpos = p->stop_charpos;
4567 it->face_id = p->face_id;
4568 it->string = p->string;
4569 it->current = p->pos;
4570 it->end_charpos = p->end_charpos;
4571 it->string_nchars = p->string_nchars;
4572 it->area = p->area;
4573 it->multibyte_p = p->multibyte_p;
4574 it->slice = p->slice;
4575 it->space_width = p->space_width;
4576 it->font_height = p->font_height;
4577 it->voffset = p->voffset;
4578 it->string_from_display_prop_p = p->string_from_display_prop_p;
4579 }
4580
4581
4582 \f
4583 /***********************************************************************
4584 Moving over lines
4585 ***********************************************************************/
4586
4587 /* Set IT's current position to the previous line start. */
4588
4589 static void
4590 back_to_previous_line_start (it)
4591 struct it *it;
4592 {
4593 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
4594 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
4595 }
4596
4597
4598 /* Move IT to the next line start.
4599
4600 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
4601 we skipped over part of the text (as opposed to moving the iterator
4602 continuously over the text). Otherwise, don't change the value
4603 of *SKIPPED_P.
4604
4605 Newlines may come from buffer text, overlay strings, or strings
4606 displayed via the `display' property. That's the reason we can't
4607 simply use find_next_newline_no_quit.
4608
4609 Note that this function may not skip over invisible text that is so
4610 because of text properties and immediately follows a newline. If
4611 it would, function reseat_at_next_visible_line_start, when called
4612 from set_iterator_to_next, would effectively make invisible
4613 characters following a newline part of the wrong glyph row, which
4614 leads to wrong cursor motion. */
4615
4616 static int
4617 forward_to_next_line_start (it, skipped_p)
4618 struct it *it;
4619 int *skipped_p;
4620 {
4621 int old_selective, newline_found_p, n;
4622 const int MAX_NEWLINE_DISTANCE = 500;
4623
4624 /* If already on a newline, just consume it to avoid unintended
4625 skipping over invisible text below. */
4626 if (it->what == IT_CHARACTER
4627 && it->c == '\n'
4628 && CHARPOS (it->position) == IT_CHARPOS (*it))
4629 {
4630 set_iterator_to_next (it, 0);
4631 it->c = 0;
4632 return 1;
4633 }
4634
4635 /* Don't handle selective display in the following. It's (a)
4636 unnecessary because it's done by the caller, and (b) leads to an
4637 infinite recursion because next_element_from_ellipsis indirectly
4638 calls this function. */
4639 old_selective = it->selective;
4640 it->selective = 0;
4641
4642 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
4643 from buffer text. */
4644 for (n = newline_found_p = 0;
4645 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
4646 n += STRINGP (it->string) ? 0 : 1)
4647 {
4648 if (!get_next_display_element (it))
4649 return 0;
4650 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
4651 set_iterator_to_next (it, 0);
4652 }
4653
4654 /* If we didn't find a newline near enough, see if we can use a
4655 short-cut. */
4656 if (!newline_found_p)
4657 {
4658 int start = IT_CHARPOS (*it);
4659 int limit = find_next_newline_no_quit (start, 1);
4660 Lisp_Object pos;
4661
4662 xassert (!STRINGP (it->string));
4663
4664 /* If there isn't any `display' property in sight, and no
4665 overlays, we can just use the position of the newline in
4666 buffer text. */
4667 if (it->stop_charpos >= limit
4668 || ((pos = Fnext_single_property_change (make_number (start),
4669 Qdisplay,
4670 Qnil, make_number (limit)),
4671 NILP (pos))
4672 && next_overlay_change (start) == ZV))
4673 {
4674 IT_CHARPOS (*it) = limit;
4675 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
4676 *skipped_p = newline_found_p = 1;
4677 }
4678 else
4679 {
4680 while (get_next_display_element (it)
4681 && !newline_found_p)
4682 {
4683 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
4684 set_iterator_to_next (it, 0);
4685 }
4686 }
4687 }
4688
4689 it->selective = old_selective;
4690 return newline_found_p;
4691 }
4692
4693
4694 /* Set IT's current position to the previous visible line start. Skip
4695 invisible text that is so either due to text properties or due to
4696 selective display. Caution: this does not change IT->current_x and
4697 IT->hpos. */
4698
4699 static void
4700 back_to_previous_visible_line_start (it)
4701 struct it *it;
4702 {
4703 while (IT_CHARPOS (*it) > BEGV)
4704 {
4705 back_to_previous_line_start (it);
4706 if (IT_CHARPOS (*it) <= BEGV)
4707 break;
4708
4709 /* If selective > 0, then lines indented more than that values
4710 are invisible. */
4711 if (it->selective > 0
4712 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4713 (double) it->selective)) /* iftc */
4714 continue;
4715
4716 /* Check the newline before point for invisibility. */
4717 {
4718 Lisp_Object prop;
4719 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
4720 Qinvisible, it->window);
4721 if (TEXT_PROP_MEANS_INVISIBLE (prop))
4722 continue;
4723 }
4724
4725 /* If newline has a display property that replaces the newline with something
4726 else (image or text), find start of overlay or interval and continue search
4727 from that point. */
4728 if (IT_CHARPOS (*it) > BEGV)
4729 {
4730 struct it it2 = *it;
4731 int pos;
4732 int beg, end;
4733 Lisp_Object val, overlay;
4734
4735 pos = --IT_CHARPOS (it2);
4736 --IT_BYTEPOS (it2);
4737 it2.sp = 0;
4738 if (handle_display_prop (&it2) == HANDLED_RETURN
4739 && !NILP (val = get_char_property_and_overlay
4740 (make_number (pos), Qdisplay, Qnil, &overlay))
4741 && (OVERLAYP (overlay)
4742 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
4743 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
4744 {
4745 if (beg < BEGV)
4746 beg = BEGV;
4747 IT_CHARPOS (*it) = beg;
4748 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
4749 continue;
4750 }
4751 }
4752
4753 break;
4754 }
4755
4756 xassert (IT_CHARPOS (*it) >= BEGV);
4757 xassert (IT_CHARPOS (*it) == BEGV
4758 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4759 CHECK_IT (it);
4760 }
4761
4762
4763 /* Reseat iterator IT at the previous visible line start. Skip
4764 invisible text that is so either due to text properties or due to
4765 selective display. At the end, update IT's overlay information,
4766 face information etc. */
4767
4768 void
4769 reseat_at_previous_visible_line_start (it)
4770 struct it *it;
4771 {
4772 back_to_previous_visible_line_start (it);
4773 reseat (it, it->current.pos, 1);
4774 CHECK_IT (it);
4775 }
4776
4777
4778 /* Reseat iterator IT on the next visible line start in the current
4779 buffer. ON_NEWLINE_P non-zero means position IT on the newline
4780 preceding the line start. Skip over invisible text that is so
4781 because of selective display. Compute faces, overlays etc at the
4782 new position. Note that this function does not skip over text that
4783 is invisible because of text properties. */
4784
4785 static void
4786 reseat_at_next_visible_line_start (it, on_newline_p)
4787 struct it *it;
4788 int on_newline_p;
4789 {
4790 int newline_found_p, skipped_p = 0;
4791
4792 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4793
4794 /* Skip over lines that are invisible because they are indented
4795 more than the value of IT->selective. */
4796 if (it->selective > 0)
4797 while (IT_CHARPOS (*it) < ZV
4798 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
4799 (double) it->selective)) /* iftc */
4800 {
4801 xassert (FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
4802 newline_found_p = forward_to_next_line_start (it, &skipped_p);
4803 }
4804
4805 /* Position on the newline if that's what's requested. */
4806 if (on_newline_p && newline_found_p)
4807 {
4808 if (STRINGP (it->string))
4809 {
4810 if (IT_STRING_CHARPOS (*it) > 0)
4811 {
4812 --IT_STRING_CHARPOS (*it);
4813 --IT_STRING_BYTEPOS (*it);
4814 }
4815 }
4816 else if (IT_CHARPOS (*it) > BEGV)
4817 {
4818 --IT_CHARPOS (*it);
4819 --IT_BYTEPOS (*it);
4820 reseat (it, it->current.pos, 0);
4821 }
4822 }
4823 else if (skipped_p)
4824 reseat (it, it->current.pos, 0);
4825
4826 CHECK_IT (it);
4827 }
4828
4829
4830 \f
4831 /***********************************************************************
4832 Changing an iterator's position
4833 ***********************************************************************/
4834
4835 /* Change IT's current position to POS in current_buffer. If FORCE_P
4836 is non-zero, always check for text properties at the new position.
4837 Otherwise, text properties are only looked up if POS >=
4838 IT->check_charpos of a property. */
4839
4840 static void
4841 reseat (it, pos, force_p)
4842 struct it *it;
4843 struct text_pos pos;
4844 int force_p;
4845 {
4846 int original_pos = IT_CHARPOS (*it);
4847
4848 reseat_1 (it, pos, 0);
4849
4850 /* Determine where to check text properties. Avoid doing it
4851 where possible because text property lookup is very expensive. */
4852 if (force_p
4853 || CHARPOS (pos) > it->stop_charpos
4854 || CHARPOS (pos) < original_pos)
4855 handle_stop (it);
4856
4857 CHECK_IT (it);
4858 }
4859
4860
4861 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
4862 IT->stop_pos to POS, also. */
4863
4864 static void
4865 reseat_1 (it, pos, set_stop_p)
4866 struct it *it;
4867 struct text_pos pos;
4868 int set_stop_p;
4869 {
4870 /* Don't call this function when scanning a C string. */
4871 xassert (it->s == NULL);
4872
4873 /* POS must be a reasonable value. */
4874 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
4875
4876 it->current.pos = it->position = pos;
4877 XSETBUFFER (it->object, current_buffer);
4878 it->end_charpos = ZV;
4879 it->dpvec = NULL;
4880 it->current.dpvec_index = -1;
4881 it->current.overlay_string_index = -1;
4882 IT_STRING_CHARPOS (*it) = -1;
4883 IT_STRING_BYTEPOS (*it) = -1;
4884 it->string = Qnil;
4885 it->method = GET_FROM_BUFFER;
4886 /* RMS: I added this to fix a bug in move_it_vertically_backward
4887 where it->area continued to relate to the starting point
4888 for the backward motion. Bug report from
4889 Nick Roberts <nick@nick.uklinux.net> on 19 May 2003.
4890 However, I am not sure whether reseat still does the right thing
4891 in general after this change. */
4892 it->area = TEXT_AREA;
4893 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
4894 it->sp = 0;
4895 it->face_before_selective_p = 0;
4896
4897 if (set_stop_p)
4898 it->stop_charpos = CHARPOS (pos);
4899 }
4900
4901
4902 /* Set up IT for displaying a string, starting at CHARPOS in window W.
4903 If S is non-null, it is a C string to iterate over. Otherwise,
4904 STRING gives a Lisp string to iterate over.
4905
4906 If PRECISION > 0, don't return more then PRECISION number of
4907 characters from the string.
4908
4909 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
4910 characters have been returned. FIELD_WIDTH < 0 means an infinite
4911 field width.
4912
4913 MULTIBYTE = 0 means disable processing of multibyte characters,
4914 MULTIBYTE > 0 means enable it,
4915 MULTIBYTE < 0 means use IT->multibyte_p.
4916
4917 IT must be initialized via a prior call to init_iterator before
4918 calling this function. */
4919
4920 static void
4921 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
4922 struct it *it;
4923 unsigned char *s;
4924 Lisp_Object string;
4925 int charpos;
4926 int precision, field_width, multibyte;
4927 {
4928 /* No region in strings. */
4929 it->region_beg_charpos = it->region_end_charpos = -1;
4930
4931 /* No text property checks performed by default, but see below. */
4932 it->stop_charpos = -1;
4933
4934 /* Set iterator position and end position. */
4935 bzero (&it->current, sizeof it->current);
4936 it->current.overlay_string_index = -1;
4937 it->current.dpvec_index = -1;
4938 xassert (charpos >= 0);
4939
4940 /* If STRING is specified, use its multibyteness, otherwise use the
4941 setting of MULTIBYTE, if specified. */
4942 if (multibyte >= 0)
4943 it->multibyte_p = multibyte > 0;
4944
4945 if (s == NULL)
4946 {
4947 xassert (STRINGP (string));
4948 it->string = string;
4949 it->s = NULL;
4950 it->end_charpos = it->string_nchars = SCHARS (string);
4951 it->method = GET_FROM_STRING;
4952 it->current.string_pos = string_pos (charpos, string);
4953 }
4954 else
4955 {
4956 it->s = s;
4957 it->string = Qnil;
4958
4959 /* Note that we use IT->current.pos, not it->current.string_pos,
4960 for displaying C strings. */
4961 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
4962 if (it->multibyte_p)
4963 {
4964 it->current.pos = c_string_pos (charpos, s, 1);
4965 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
4966 }
4967 else
4968 {
4969 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
4970 it->end_charpos = it->string_nchars = strlen (s);
4971 }
4972
4973 it->method = GET_FROM_C_STRING;
4974 }
4975
4976 /* PRECISION > 0 means don't return more than PRECISION characters
4977 from the string. */
4978 if (precision > 0 && it->end_charpos - charpos > precision)
4979 it->end_charpos = it->string_nchars = charpos + precision;
4980
4981 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
4982 characters have been returned. FIELD_WIDTH == 0 means don't pad,
4983 FIELD_WIDTH < 0 means infinite field width. This is useful for
4984 padding with `-' at the end of a mode line. */
4985 if (field_width < 0)
4986 field_width = INFINITY;
4987 if (field_width > it->end_charpos - charpos)
4988 it->end_charpos = charpos + field_width;
4989
4990 /* Use the standard display table for displaying strings. */
4991 if (DISP_TABLE_P (Vstandard_display_table))
4992 it->dp = XCHAR_TABLE (Vstandard_display_table);
4993
4994 it->stop_charpos = charpos;
4995 CHECK_IT (it);
4996 }
4997
4998
4999 \f
5000 /***********************************************************************
5001 Iteration
5002 ***********************************************************************/
5003
5004 /* Map enum it_method value to corresponding next_element_from_* function. */
5005
5006 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5007 {
5008 next_element_from_buffer,
5009 next_element_from_display_vector,
5010 next_element_from_composition,
5011 next_element_from_string,
5012 next_element_from_c_string,
5013 next_element_from_image,
5014 next_element_from_stretch
5015 };
5016
5017
5018 /* Load IT's display element fields with information about the next
5019 display element from the current position of IT. Value is zero if
5020 end of buffer (or C string) is reached. */
5021
5022 int
5023 get_next_display_element (it)
5024 struct it *it;
5025 {
5026 /* Non-zero means that we found a display element. Zero means that
5027 we hit the end of what we iterate over. Performance note: the
5028 function pointer `method' used here turns out to be faster than
5029 using a sequence of if-statements. */
5030 int success_p;
5031
5032 get_next:
5033 success_p = (*get_next_element[it->method]) (it);
5034
5035 if (it->what == IT_CHARACTER)
5036 {
5037 /* Map via display table or translate control characters.
5038 IT->c, IT->len etc. have been set to the next character by
5039 the function call above. If we have a display table, and it
5040 contains an entry for IT->c, translate it. Don't do this if
5041 IT->c itself comes from a display table, otherwise we could
5042 end up in an infinite recursion. (An alternative could be to
5043 count the recursion depth of this function and signal an
5044 error when a certain maximum depth is reached.) Is it worth
5045 it? */
5046 if (success_p && it->dpvec == NULL)
5047 {
5048 Lisp_Object dv;
5049
5050 if (it->dp
5051 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5052 VECTORP (dv)))
5053 {
5054 struct Lisp_Vector *v = XVECTOR (dv);
5055
5056 /* Return the first character from the display table
5057 entry, if not empty. If empty, don't display the
5058 current character. */
5059 if (v->size)
5060 {
5061 it->dpvec_char_len = it->len;
5062 it->dpvec = v->contents;
5063 it->dpend = v->contents + v->size;
5064 it->current.dpvec_index = 0;
5065 it->dpvec_face_id = -1;
5066 it->saved_face_id = it->face_id;
5067 it->method = GET_FROM_DISPLAY_VECTOR;
5068 it->ellipsis_p = 0;
5069 }
5070 else
5071 {
5072 set_iterator_to_next (it, 0);
5073 }
5074 goto get_next;
5075 }
5076
5077 /* Translate control characters into `\003' or `^C' form.
5078 Control characters coming from a display table entry are
5079 currently not translated because we use IT->dpvec to hold
5080 the translation. This could easily be changed but I
5081 don't believe that it is worth doing.
5082
5083 If it->multibyte_p is nonzero, eight-bit characters and
5084 non-printable multibyte characters are also translated to
5085 octal form.
5086
5087 If it->multibyte_p is zero, eight-bit characters that
5088 don't have corresponding multibyte char code are also
5089 translated to octal form. */
5090 else if ((it->c < ' '
5091 && (it->area != TEXT_AREA
5092 /* In mode line, treat \n like other crl chars. */
5093 || (it->c != '\t'
5094 && it->glyph_row && it->glyph_row->mode_line_p)
5095 || (it->c != '\n' && it->c != '\t')))
5096 || (it->multibyte_p
5097 ? ((it->c >= 127
5098 && it->len == 1)
5099 || !CHAR_PRINTABLE_P (it->c)
5100 || (!NILP (Vnobreak_char_display)
5101 && (it->c == 0x8a0 || it->c == 0x8ad
5102 || it->c == 0x920 || it->c == 0x92d
5103 || it->c == 0xe20 || it->c == 0xe2d
5104 || it->c == 0xf20 || it->c == 0xf2d)))
5105 : (it->c >= 127
5106 && (!unibyte_display_via_language_environment
5107 || it->c == unibyte_char_to_multibyte (it->c)))))
5108 {
5109 /* IT->c is a control character which must be displayed
5110 either as '\003' or as `^C' where the '\\' and '^'
5111 can be defined in the display table. Fill
5112 IT->ctl_chars with glyphs for what we have to
5113 display. Then, set IT->dpvec to these glyphs. */
5114 GLYPH g;
5115 int ctl_len;
5116 int face_id, lface_id = 0 ;
5117 GLYPH escape_glyph;
5118
5119 /* Handle control characters with ^. */
5120
5121 if (it->c < 128 && it->ctl_arrow_p)
5122 {
5123 g = '^'; /* default glyph for Control */
5124 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5125 if (it->dp
5126 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5127 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5128 {
5129 g = XINT (DISP_CTRL_GLYPH (it->dp));
5130 lface_id = FAST_GLYPH_FACE (g);
5131 }
5132 if (lface_id)
5133 {
5134 g = FAST_GLYPH_CHAR (g);
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 XSETINT (it->ctl_chars[0], g);
5146 g = it->c ^ 0100;
5147 XSETINT (it->ctl_chars[1], g);
5148 ctl_len = 2;
5149 goto display_control;
5150 }
5151
5152 /* Handle non-break space in the mode where it only gets
5153 highlighting. */
5154
5155 if (EQ (Vnobreak_char_display, Qt)
5156 && (it->c == 0x8a0 || it->c == 0x920
5157 || it->c == 0xe20 || it->c == 0xf20))
5158 {
5159 /* Merge the no-break-space face into the current face. */
5160 face_id = merge_faces (it->f, Qnobreak_space, 0,
5161 it->face_id);
5162
5163 g = it->c = ' ';
5164 XSETINT (it->ctl_chars[0], g);
5165 ctl_len = 1;
5166 goto display_control;
5167 }
5168
5169 /* Handle sequences that start with the "escape glyph". */
5170
5171 /* the default escape glyph is \. */
5172 escape_glyph = '\\';
5173
5174 if (it->dp
5175 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5176 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5177 {
5178 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5179 lface_id = FAST_GLYPH_FACE (escape_glyph);
5180 }
5181 if (lface_id)
5182 {
5183 /* The display table specified a face.
5184 Merge it into face_id and also into escape_glyph. */
5185 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5186 face_id = merge_faces (it->f, Qt, lface_id,
5187 it->face_id);
5188 }
5189 else
5190 {
5191 /* Merge the escape-glyph face into the current face. */
5192 face_id = merge_faces (it->f, Qescape_glyph, 0,
5193 it->face_id);
5194 }
5195
5196 /* Handle soft hyphens in the mode where they only get
5197 highlighting. */
5198
5199 if (EQ (Vnobreak_char_display, Qt)
5200 && (it->c == 0x8ad || it->c == 0x92d
5201 || it->c == 0xe2d || it->c == 0xf2d))
5202 {
5203 g = it->c = '-';
5204 XSETINT (it->ctl_chars[0], g);
5205 ctl_len = 1;
5206 goto display_control;
5207 }
5208
5209 /* Handle non-break space and soft hyphen
5210 with the escape glyph. */
5211
5212 if (it->c == 0x8a0 || it->c == 0x8ad
5213 || it->c == 0x920 || it->c == 0x92d
5214 || it->c == 0xe20 || it->c == 0xe2d
5215 || it->c == 0xf20 || it->c == 0xf2d)
5216 {
5217 XSETINT (it->ctl_chars[0], escape_glyph);
5218 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5219 XSETINT (it->ctl_chars[1], g);
5220 ctl_len = 2;
5221 goto display_control;
5222 }
5223
5224 {
5225 unsigned char str[MAX_MULTIBYTE_LENGTH];
5226 int len;
5227 int i;
5228
5229 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5230 if (SINGLE_BYTE_CHAR_P (it->c))
5231 str[0] = it->c, len = 1;
5232 else
5233 {
5234 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5235 if (len < 0)
5236 {
5237 /* It's an invalid character, which shouldn't
5238 happen actually, but due to bugs it may
5239 happen. Let's print the char as is, there's
5240 not much meaningful we can do with it. */
5241 str[0] = it->c;
5242 str[1] = it->c >> 8;
5243 str[2] = it->c >> 16;
5244 str[3] = it->c >> 24;
5245 len = 4;
5246 }
5247 }
5248
5249 for (i = 0; i < len; i++)
5250 {
5251 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5252 /* Insert three more glyphs into IT->ctl_chars for
5253 the octal display of the character. */
5254 g = ((str[i] >> 6) & 7) + '0';
5255 XSETINT (it->ctl_chars[i * 4 + 1], g);
5256 g = ((str[i] >> 3) & 7) + '0';
5257 XSETINT (it->ctl_chars[i * 4 + 2], g);
5258 g = (str[i] & 7) + '0';
5259 XSETINT (it->ctl_chars[i * 4 + 3], g);
5260 }
5261 ctl_len = len * 4;
5262 }
5263
5264 display_control:
5265 /* Set up IT->dpvec and return first character from it. */
5266 it->dpvec_char_len = it->len;
5267 it->dpvec = it->ctl_chars;
5268 it->dpend = it->dpvec + ctl_len;
5269 it->current.dpvec_index = 0;
5270 it->dpvec_face_id = face_id;
5271 it->saved_face_id = it->face_id;
5272 it->method = GET_FROM_DISPLAY_VECTOR;
5273 it->ellipsis_p = 0;
5274 goto get_next;
5275 }
5276 }
5277
5278 /* Adjust face id for a multibyte character. There are no
5279 multibyte character in unibyte text. */
5280 if (it->multibyte_p
5281 && success_p
5282 && FRAME_WINDOW_P (it->f))
5283 {
5284 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5285 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5286 }
5287 }
5288
5289 /* Is this character the last one of a run of characters with
5290 box? If yes, set IT->end_of_box_run_p to 1. */
5291 if (it->face_box_p
5292 && it->s == NULL)
5293 {
5294 int face_id;
5295 struct face *face;
5296
5297 it->end_of_box_run_p
5298 = ((face_id = face_after_it_pos (it),
5299 face_id != it->face_id)
5300 && (face = FACE_FROM_ID (it->f, face_id),
5301 face->box == FACE_NO_BOX));
5302 }
5303
5304 /* Value is 0 if end of buffer or string reached. */
5305 return success_p;
5306 }
5307
5308
5309 /* Move IT to the next display element.
5310
5311 RESEAT_P non-zero means if called on a newline in buffer text,
5312 skip to the next visible line start.
5313
5314 Functions get_next_display_element and set_iterator_to_next are
5315 separate because I find this arrangement easier to handle than a
5316 get_next_display_element function that also increments IT's
5317 position. The way it is we can first look at an iterator's current
5318 display element, decide whether it fits on a line, and if it does,
5319 increment the iterator position. The other way around we probably
5320 would either need a flag indicating whether the iterator has to be
5321 incremented the next time, or we would have to implement a
5322 decrement position function which would not be easy to write. */
5323
5324 void
5325 set_iterator_to_next (it, reseat_p)
5326 struct it *it;
5327 int reseat_p;
5328 {
5329 /* Reset flags indicating start and end of a sequence of characters
5330 with box. Reset them at the start of this function because
5331 moving the iterator to a new position might set them. */
5332 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5333
5334 switch (it->method)
5335 {
5336 case GET_FROM_BUFFER:
5337 /* The current display element of IT is a character from
5338 current_buffer. Advance in the buffer, and maybe skip over
5339 invisible lines that are so because of selective display. */
5340 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5341 reseat_at_next_visible_line_start (it, 0);
5342 else
5343 {
5344 xassert (it->len != 0);
5345 IT_BYTEPOS (*it) += it->len;
5346 IT_CHARPOS (*it) += 1;
5347 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5348 }
5349 break;
5350
5351 case GET_FROM_COMPOSITION:
5352 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5353 if (STRINGP (it->string))
5354 {
5355 IT_STRING_BYTEPOS (*it) += it->len;
5356 IT_STRING_CHARPOS (*it) += it->cmp_len;
5357 it->method = GET_FROM_STRING;
5358 goto consider_string_end;
5359 }
5360 else
5361 {
5362 IT_BYTEPOS (*it) += it->len;
5363 IT_CHARPOS (*it) += it->cmp_len;
5364 it->method = GET_FROM_BUFFER;
5365 }
5366 break;
5367
5368 case GET_FROM_C_STRING:
5369 /* Current display element of IT is from a C string. */
5370 IT_BYTEPOS (*it) += it->len;
5371 IT_CHARPOS (*it) += 1;
5372 break;
5373
5374 case GET_FROM_DISPLAY_VECTOR:
5375 /* Current display element of IT is from a display table entry.
5376 Advance in the display table definition. Reset it to null if
5377 end reached, and continue with characters from buffers/
5378 strings. */
5379 ++it->current.dpvec_index;
5380
5381 /* Restore face of the iterator to what they were before the
5382 display vector entry (these entries may contain faces). */
5383 it->face_id = it->saved_face_id;
5384
5385 if (it->dpvec + it->current.dpvec_index == it->dpend)
5386 {
5387 if (it->s)
5388 it->method = GET_FROM_C_STRING;
5389 else if (STRINGP (it->string))
5390 it->method = GET_FROM_STRING;
5391 else
5392 it->method = GET_FROM_BUFFER;
5393
5394 it->dpvec = NULL;
5395 it->current.dpvec_index = -1;
5396
5397 /* Skip over characters which were displayed via IT->dpvec. */
5398 if (it->dpvec_char_len < 0)
5399 reseat_at_next_visible_line_start (it, 1);
5400 else if (it->dpvec_char_len > 0)
5401 {
5402 it->len = it->dpvec_char_len;
5403 set_iterator_to_next (it, reseat_p);
5404 }
5405
5406 /* Recheck faces after display vector */
5407 it->stop_charpos = IT_CHARPOS (*it);
5408 }
5409 break;
5410
5411 case GET_FROM_STRING:
5412 /* Current display element is a character from a Lisp string. */
5413 xassert (it->s == NULL && STRINGP (it->string));
5414 IT_STRING_BYTEPOS (*it) += it->len;
5415 IT_STRING_CHARPOS (*it) += 1;
5416
5417 consider_string_end:
5418
5419 if (it->current.overlay_string_index >= 0)
5420 {
5421 /* IT->string is an overlay string. Advance to the
5422 next, if there is one. */
5423 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5424 next_overlay_string (it);
5425 }
5426 else
5427 {
5428 /* IT->string is not an overlay string. If we reached
5429 its end, and there is something on IT->stack, proceed
5430 with what is on the stack. This can be either another
5431 string, this time an overlay string, or a buffer. */
5432 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5433 && it->sp > 0)
5434 {
5435 pop_it (it);
5436 if (STRINGP (it->string))
5437 goto consider_string_end;
5438 it->method = GET_FROM_BUFFER;
5439 }
5440 }
5441 break;
5442
5443 case GET_FROM_IMAGE:
5444 case GET_FROM_STRETCH:
5445 /* The position etc with which we have to proceed are on
5446 the stack. The position may be at the end of a string,
5447 if the `display' property takes up the whole string. */
5448 xassert (it->sp > 0);
5449 pop_it (it);
5450 it->image_id = 0;
5451 if (STRINGP (it->string))
5452 {
5453 it->method = GET_FROM_STRING;
5454 goto consider_string_end;
5455 }
5456 it->method = GET_FROM_BUFFER;
5457 break;
5458
5459 default:
5460 /* There are no other methods defined, so this should be a bug. */
5461 abort ();
5462 }
5463
5464 xassert (it->method != GET_FROM_STRING
5465 || (STRINGP (it->string)
5466 && IT_STRING_CHARPOS (*it) >= 0));
5467 }
5468
5469 /* Load IT's display element fields with information about the next
5470 display element which comes from a display table entry or from the
5471 result of translating a control character to one of the forms `^C'
5472 or `\003'.
5473
5474 IT->dpvec holds the glyphs to return as characters.
5475 IT->saved_face_id holds the face id before the display vector--
5476 it is restored into IT->face_idin set_iterator_to_next. */
5477
5478 static int
5479 next_element_from_display_vector (it)
5480 struct it *it;
5481 {
5482 /* Precondition. */
5483 xassert (it->dpvec && it->current.dpvec_index >= 0);
5484
5485 it->face_id = it->saved_face_id;
5486
5487 if (INTEGERP (*it->dpvec)
5488 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5489 {
5490 GLYPH g;
5491
5492 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5493 it->c = FAST_GLYPH_CHAR (g);
5494 it->len = CHAR_BYTES (it->c);
5495
5496 /* The entry may contain a face id to use. Such a face id is
5497 the id of a Lisp face, not a realized face. A face id of
5498 zero means no face is specified. */
5499 if (it->dpvec_face_id >= 0)
5500 it->face_id = it->dpvec_face_id;
5501 else
5502 {
5503 int lface_id = FAST_GLYPH_FACE (g);
5504 if (lface_id > 0)
5505 it->face_id = merge_faces (it->f, Qt, lface_id,
5506 it->saved_face_id);
5507 }
5508 }
5509 else
5510 /* Display table entry is invalid. Return a space. */
5511 it->c = ' ', it->len = 1;
5512
5513 /* Don't change position and object of the iterator here. They are
5514 still the values of the character that had this display table
5515 entry or was translated, and that's what we want. */
5516 it->what = IT_CHARACTER;
5517 return 1;
5518 }
5519
5520
5521 /* Load IT with the next display element from Lisp string IT->string.
5522 IT->current.string_pos is the current position within the string.
5523 If IT->current.overlay_string_index >= 0, the Lisp string is an
5524 overlay string. */
5525
5526 static int
5527 next_element_from_string (it)
5528 struct it *it;
5529 {
5530 struct text_pos position;
5531
5532 xassert (STRINGP (it->string));
5533 xassert (IT_STRING_CHARPOS (*it) >= 0);
5534 position = it->current.string_pos;
5535
5536 /* Time to check for invisible text? */
5537 if (IT_STRING_CHARPOS (*it) < it->end_charpos
5538 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
5539 {
5540 handle_stop (it);
5541
5542 /* Since a handler may have changed IT->method, we must
5543 recurse here. */
5544 return get_next_display_element (it);
5545 }
5546
5547 if (it->current.overlay_string_index >= 0)
5548 {
5549 /* Get the next character from an overlay string. In overlay
5550 strings, There is no field width or padding with spaces to
5551 do. */
5552 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5553 {
5554 it->what = IT_EOB;
5555 return 0;
5556 }
5557 else if (STRING_MULTIBYTE (it->string))
5558 {
5559 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5560 const unsigned char *s = (SDATA (it->string)
5561 + IT_STRING_BYTEPOS (*it));
5562 it->c = string_char_and_length (s, remaining, &it->len);
5563 }
5564 else
5565 {
5566 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5567 it->len = 1;
5568 }
5569 }
5570 else
5571 {
5572 /* Get the next character from a Lisp string that is not an
5573 overlay string. Such strings come from the mode line, for
5574 example. We may have to pad with spaces, or truncate the
5575 string. See also next_element_from_c_string. */
5576 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
5577 {
5578 it->what = IT_EOB;
5579 return 0;
5580 }
5581 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
5582 {
5583 /* Pad with spaces. */
5584 it->c = ' ', it->len = 1;
5585 CHARPOS (position) = BYTEPOS (position) = -1;
5586 }
5587 else if (STRING_MULTIBYTE (it->string))
5588 {
5589 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
5590 const unsigned char *s = (SDATA (it->string)
5591 + IT_STRING_BYTEPOS (*it));
5592 it->c = string_char_and_length (s, maxlen, &it->len);
5593 }
5594 else
5595 {
5596 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
5597 it->len = 1;
5598 }
5599 }
5600
5601 /* Record what we have and where it came from. Note that we store a
5602 buffer position in IT->position although it could arguably be a
5603 string position. */
5604 it->what = IT_CHARACTER;
5605 it->object = it->string;
5606 it->position = position;
5607 return 1;
5608 }
5609
5610
5611 /* Load IT with next display element from C string IT->s.
5612 IT->string_nchars is the maximum number of characters to return
5613 from the string. IT->end_charpos may be greater than
5614 IT->string_nchars when this function is called, in which case we
5615 may have to return padding spaces. Value is zero if end of string
5616 reached, including padding spaces. */
5617
5618 static int
5619 next_element_from_c_string (it)
5620 struct it *it;
5621 {
5622 int success_p = 1;
5623
5624 xassert (it->s);
5625 it->what = IT_CHARACTER;
5626 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
5627 it->object = Qnil;
5628
5629 /* IT's position can be greater IT->string_nchars in case a field
5630 width or precision has been specified when the iterator was
5631 initialized. */
5632 if (IT_CHARPOS (*it) >= it->end_charpos)
5633 {
5634 /* End of the game. */
5635 it->what = IT_EOB;
5636 success_p = 0;
5637 }
5638 else if (IT_CHARPOS (*it) >= it->string_nchars)
5639 {
5640 /* Pad with spaces. */
5641 it->c = ' ', it->len = 1;
5642 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
5643 }
5644 else if (it->multibyte_p)
5645 {
5646 /* Implementation note: The calls to strlen apparently aren't a
5647 performance problem because there is no noticeable performance
5648 difference between Emacs running in unibyte or multibyte mode. */
5649 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
5650 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
5651 maxlen, &it->len);
5652 }
5653 else
5654 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
5655
5656 return success_p;
5657 }
5658
5659
5660 /* Set up IT to return characters from an ellipsis, if appropriate.
5661 The definition of the ellipsis glyphs may come from a display table
5662 entry. This function Fills IT with the first glyph from the
5663 ellipsis if an ellipsis is to be displayed. */
5664
5665 static int
5666 next_element_from_ellipsis (it)
5667 struct it *it;
5668 {
5669 if (it->selective_display_ellipsis_p)
5670 setup_for_ellipsis (it, it->len);
5671 else
5672 {
5673 /* The face at the current position may be different from the
5674 face we find after the invisible text. Remember what it
5675 was in IT->saved_face_id, and signal that it's there by
5676 setting face_before_selective_p. */
5677 it->saved_face_id = it->face_id;
5678 it->method = GET_FROM_BUFFER;
5679 reseat_at_next_visible_line_start (it, 1);
5680 it->face_before_selective_p = 1;
5681 }
5682
5683 return get_next_display_element (it);
5684 }
5685
5686
5687 /* Deliver an image display element. The iterator IT is already
5688 filled with image information (done in handle_display_prop). Value
5689 is always 1. */
5690
5691
5692 static int
5693 next_element_from_image (it)
5694 struct it *it;
5695 {
5696 it->what = IT_IMAGE;
5697 return 1;
5698 }
5699
5700
5701 /* Fill iterator IT with next display element from a stretch glyph
5702 property. IT->object is the value of the text property. Value is
5703 always 1. */
5704
5705 static int
5706 next_element_from_stretch (it)
5707 struct it *it;
5708 {
5709 it->what = IT_STRETCH;
5710 return 1;
5711 }
5712
5713
5714 /* Load IT with the next display element from current_buffer. Value
5715 is zero if end of buffer reached. IT->stop_charpos is the next
5716 position at which to stop and check for text properties or buffer
5717 end. */
5718
5719 static int
5720 next_element_from_buffer (it)
5721 struct it *it;
5722 {
5723 int success_p = 1;
5724
5725 /* Check this assumption, otherwise, we would never enter the
5726 if-statement, below. */
5727 xassert (IT_CHARPOS (*it) >= BEGV
5728 && IT_CHARPOS (*it) <= it->stop_charpos);
5729
5730 if (IT_CHARPOS (*it) >= it->stop_charpos)
5731 {
5732 if (IT_CHARPOS (*it) >= it->end_charpos)
5733 {
5734 int overlay_strings_follow_p;
5735
5736 /* End of the game, except when overlay strings follow that
5737 haven't been returned yet. */
5738 if (it->overlay_strings_at_end_processed_p)
5739 overlay_strings_follow_p = 0;
5740 else
5741 {
5742 it->overlay_strings_at_end_processed_p = 1;
5743 overlay_strings_follow_p = get_overlay_strings (it, 0);
5744 }
5745
5746 if (overlay_strings_follow_p)
5747 success_p = get_next_display_element (it);
5748 else
5749 {
5750 it->what = IT_EOB;
5751 it->position = it->current.pos;
5752 success_p = 0;
5753 }
5754 }
5755 else
5756 {
5757 handle_stop (it);
5758 return get_next_display_element (it);
5759 }
5760 }
5761 else
5762 {
5763 /* No face changes, overlays etc. in sight, so just return a
5764 character from current_buffer. */
5765 unsigned char *p;
5766
5767 /* Maybe run the redisplay end trigger hook. Performance note:
5768 This doesn't seem to cost measurable time. */
5769 if (it->redisplay_end_trigger_charpos
5770 && it->glyph_row
5771 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
5772 run_redisplay_end_trigger_hook (it);
5773
5774 /* Get the next character, maybe multibyte. */
5775 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
5776 if (it->multibyte_p && !ASCII_BYTE_P (*p))
5777 {
5778 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
5779 - IT_BYTEPOS (*it));
5780 it->c = string_char_and_length (p, maxlen, &it->len);
5781 }
5782 else
5783 it->c = *p, it->len = 1;
5784
5785 /* Record what we have and where it came from. */
5786 it->what = IT_CHARACTER;;
5787 it->object = it->w->buffer;
5788 it->position = it->current.pos;
5789
5790 /* Normally we return the character found above, except when we
5791 really want to return an ellipsis for selective display. */
5792 if (it->selective)
5793 {
5794 if (it->c == '\n')
5795 {
5796 /* A value of selective > 0 means hide lines indented more
5797 than that number of columns. */
5798 if (it->selective > 0
5799 && IT_CHARPOS (*it) + 1 < ZV
5800 && indented_beyond_p (IT_CHARPOS (*it) + 1,
5801 IT_BYTEPOS (*it) + 1,
5802 (double) it->selective)) /* iftc */
5803 {
5804 success_p = next_element_from_ellipsis (it);
5805 it->dpvec_char_len = -1;
5806 }
5807 }
5808 else if (it->c == '\r' && it->selective == -1)
5809 {
5810 /* A value of selective == -1 means that everything from the
5811 CR to the end of the line is invisible, with maybe an
5812 ellipsis displayed for it. */
5813 success_p = next_element_from_ellipsis (it);
5814 it->dpvec_char_len = -1;
5815 }
5816 }
5817 }
5818
5819 /* Value is zero if end of buffer reached. */
5820 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
5821 return success_p;
5822 }
5823
5824
5825 /* Run the redisplay end trigger hook for IT. */
5826
5827 static void
5828 run_redisplay_end_trigger_hook (it)
5829 struct it *it;
5830 {
5831 Lisp_Object args[3];
5832
5833 /* IT->glyph_row should be non-null, i.e. we should be actually
5834 displaying something, or otherwise we should not run the hook. */
5835 xassert (it->glyph_row);
5836
5837 /* Set up hook arguments. */
5838 args[0] = Qredisplay_end_trigger_functions;
5839 args[1] = it->window;
5840 XSETINT (args[2], it->redisplay_end_trigger_charpos);
5841 it->redisplay_end_trigger_charpos = 0;
5842
5843 /* Since we are *trying* to run these functions, don't try to run
5844 them again, even if they get an error. */
5845 it->w->redisplay_end_trigger = Qnil;
5846 Frun_hook_with_args (3, args);
5847
5848 /* Notice if it changed the face of the character we are on. */
5849 handle_face_prop (it);
5850 }
5851
5852
5853 /* Deliver a composition display element. The iterator IT is already
5854 filled with composition information (done in
5855 handle_composition_prop). Value is always 1. */
5856
5857 static int
5858 next_element_from_composition (it)
5859 struct it *it;
5860 {
5861 it->what = IT_COMPOSITION;
5862 it->position = (STRINGP (it->string)
5863 ? it->current.string_pos
5864 : it->current.pos);
5865 return 1;
5866 }
5867
5868
5869 \f
5870 /***********************************************************************
5871 Moving an iterator without producing glyphs
5872 ***********************************************************************/
5873
5874 /* Move iterator IT to a specified buffer or X position within one
5875 line on the display without producing glyphs.
5876
5877 OP should be a bit mask including some or all of these bits:
5878 MOVE_TO_X: Stop on reaching x-position TO_X.
5879 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
5880 Regardless of OP's value, stop in reaching the end of the display line.
5881
5882 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
5883 This means, in particular, that TO_X includes window's horizontal
5884 scroll amount.
5885
5886 The return value has several possible values that
5887 say what condition caused the scan to stop:
5888
5889 MOVE_POS_MATCH_OR_ZV
5890 - when TO_POS or ZV was reached.
5891
5892 MOVE_X_REACHED
5893 -when TO_X was reached before TO_POS or ZV were reached.
5894
5895 MOVE_LINE_CONTINUED
5896 - when we reached the end of the display area and the line must
5897 be continued.
5898
5899 MOVE_LINE_TRUNCATED
5900 - when we reached the end of the display area and the line is
5901 truncated.
5902
5903 MOVE_NEWLINE_OR_CR
5904 - when we stopped at a line end, i.e. a newline or a CR and selective
5905 display is on. */
5906
5907 static enum move_it_result
5908 move_it_in_display_line_to (it, to_charpos, to_x, op)
5909 struct it *it;
5910 int to_charpos, to_x, op;
5911 {
5912 enum move_it_result result = MOVE_UNDEFINED;
5913 struct glyph_row *saved_glyph_row;
5914
5915 /* Don't produce glyphs in produce_glyphs. */
5916 saved_glyph_row = it->glyph_row;
5917 it->glyph_row = NULL;
5918
5919 #define BUFFER_POS_REACHED_P() \
5920 ((op & MOVE_TO_POS) != 0 \
5921 && BUFFERP (it->object) \
5922 && IT_CHARPOS (*it) >= to_charpos \
5923 && (it->method == GET_FROM_BUFFER \
5924 || (it->method == GET_FROM_DISPLAY_VECTOR \
5925 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
5926
5927
5928 while (1)
5929 {
5930 int x, i, ascent = 0, descent = 0;
5931
5932 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
5933 if ((op & MOVE_TO_POS) != 0
5934 && BUFFERP (it->object)
5935 && it->method == GET_FROM_BUFFER
5936 && IT_CHARPOS (*it) > to_charpos)
5937 {
5938 result = MOVE_POS_MATCH_OR_ZV;
5939 break;
5940 }
5941
5942 /* Stop when ZV reached.
5943 We used to stop here when TO_CHARPOS reached as well, but that is
5944 too soon if this glyph does not fit on this line. So we handle it
5945 explicitly below. */
5946 if (!get_next_display_element (it)
5947 || (it->truncate_lines_p
5948 && BUFFER_POS_REACHED_P ()))
5949 {
5950 result = MOVE_POS_MATCH_OR_ZV;
5951 break;
5952 }
5953
5954 /* The call to produce_glyphs will get the metrics of the
5955 display element IT is loaded with. We record in x the
5956 x-position before this display element in case it does not
5957 fit on the line. */
5958 x = it->current_x;
5959
5960 /* Remember the line height so far in case the next element doesn't
5961 fit on the line. */
5962 if (!it->truncate_lines_p)
5963 {
5964 ascent = it->max_ascent;
5965 descent = it->max_descent;
5966 }
5967
5968 PRODUCE_GLYPHS (it);
5969
5970 if (it->area != TEXT_AREA)
5971 {
5972 set_iterator_to_next (it, 1);
5973 continue;
5974 }
5975
5976 /* The number of glyphs we get back in IT->nglyphs will normally
5977 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
5978 character on a terminal frame, or (iii) a line end. For the
5979 second case, IT->nglyphs - 1 padding glyphs will be present
5980 (on X frames, there is only one glyph produced for a
5981 composite character.
5982
5983 The behavior implemented below means, for continuation lines,
5984 that as many spaces of a TAB as fit on the current line are
5985 displayed there. For terminal frames, as many glyphs of a
5986 multi-glyph character are displayed in the current line, too.
5987 This is what the old redisplay code did, and we keep it that
5988 way. Under X, the whole shape of a complex character must
5989 fit on the line or it will be completely displayed in the
5990 next line.
5991
5992 Note that both for tabs and padding glyphs, all glyphs have
5993 the same width. */
5994 if (it->nglyphs)
5995 {
5996 /* More than one glyph or glyph doesn't fit on line. All
5997 glyphs have the same width. */
5998 int single_glyph_width = it->pixel_width / it->nglyphs;
5999 int new_x;
6000
6001 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6002 {
6003 new_x = x + single_glyph_width;
6004
6005 /* We want to leave anything reaching TO_X to the caller. */
6006 if ((op & MOVE_TO_X) && new_x > to_x)
6007 {
6008 if (BUFFER_POS_REACHED_P ())
6009 goto buffer_pos_reached;
6010 it->current_x = x;
6011 result = MOVE_X_REACHED;
6012 break;
6013 }
6014 else if (/* Lines are continued. */
6015 !it->truncate_lines_p
6016 && (/* And glyph doesn't fit on the line. */
6017 new_x > it->last_visible_x
6018 /* Or it fits exactly and we're on a window
6019 system frame. */
6020 || (new_x == it->last_visible_x
6021 && FRAME_WINDOW_P (it->f))))
6022 {
6023 if (/* IT->hpos == 0 means the very first glyph
6024 doesn't fit on the line, e.g. a wide image. */
6025 it->hpos == 0
6026 || (new_x == it->last_visible_x
6027 && FRAME_WINDOW_P (it->f)))
6028 {
6029 ++it->hpos;
6030 it->current_x = new_x;
6031 if (i == it->nglyphs - 1)
6032 {
6033 set_iterator_to_next (it, 1);
6034 #ifdef HAVE_WINDOW_SYSTEM
6035 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6036 {
6037 if (!get_next_display_element (it))
6038 {
6039 result = MOVE_POS_MATCH_OR_ZV;
6040 break;
6041 }
6042 if (BUFFER_POS_REACHED_P ())
6043 {
6044 if (ITERATOR_AT_END_OF_LINE_P (it))
6045 result = MOVE_POS_MATCH_OR_ZV;
6046 else
6047 result = MOVE_LINE_CONTINUED;
6048 break;
6049 }
6050 if (ITERATOR_AT_END_OF_LINE_P (it))
6051 {
6052 result = MOVE_NEWLINE_OR_CR;
6053 break;
6054 }
6055 }
6056 #endif /* HAVE_WINDOW_SYSTEM */
6057 }
6058 }
6059 else
6060 {
6061 it->current_x = x;
6062 it->max_ascent = ascent;
6063 it->max_descent = descent;
6064 }
6065
6066 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6067 IT_CHARPOS (*it)));
6068 result = MOVE_LINE_CONTINUED;
6069 break;
6070 }
6071 else if (BUFFER_POS_REACHED_P ())
6072 goto buffer_pos_reached;
6073 else if (new_x > it->first_visible_x)
6074 {
6075 /* Glyph is visible. Increment number of glyphs that
6076 would be displayed. */
6077 ++it->hpos;
6078 }
6079 else
6080 {
6081 /* Glyph is completely off the left margin of the display
6082 area. Nothing to do. */
6083 }
6084 }
6085
6086 if (result != MOVE_UNDEFINED)
6087 break;
6088 }
6089 else if (BUFFER_POS_REACHED_P ())
6090 {
6091 buffer_pos_reached:
6092 it->current_x = x;
6093 it->max_ascent = ascent;
6094 it->max_descent = descent;
6095 result = MOVE_POS_MATCH_OR_ZV;
6096 break;
6097 }
6098 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6099 {
6100 /* Stop when TO_X specified and reached. This check is
6101 necessary here because of lines consisting of a line end,
6102 only. The line end will not produce any glyphs and we
6103 would never get MOVE_X_REACHED. */
6104 xassert (it->nglyphs == 0);
6105 result = MOVE_X_REACHED;
6106 break;
6107 }
6108
6109 /* Is this a line end? If yes, we're done. */
6110 if (ITERATOR_AT_END_OF_LINE_P (it))
6111 {
6112 result = MOVE_NEWLINE_OR_CR;
6113 break;
6114 }
6115
6116 /* The current display element has been consumed. Advance
6117 to the next. */
6118 set_iterator_to_next (it, 1);
6119
6120 /* Stop if lines are truncated and IT's current x-position is
6121 past the right edge of the window now. */
6122 if (it->truncate_lines_p
6123 && it->current_x >= it->last_visible_x)
6124 {
6125 #ifdef HAVE_WINDOW_SYSTEM
6126 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6127 {
6128 if (!get_next_display_element (it)
6129 || BUFFER_POS_REACHED_P ())
6130 {
6131 result = MOVE_POS_MATCH_OR_ZV;
6132 break;
6133 }
6134 if (ITERATOR_AT_END_OF_LINE_P (it))
6135 {
6136 result = MOVE_NEWLINE_OR_CR;
6137 break;
6138 }
6139 }
6140 #endif /* HAVE_WINDOW_SYSTEM */
6141 result = MOVE_LINE_TRUNCATED;
6142 break;
6143 }
6144 }
6145
6146 #undef BUFFER_POS_REACHED_P
6147
6148 /* Restore the iterator settings altered at the beginning of this
6149 function. */
6150 it->glyph_row = saved_glyph_row;
6151 return result;
6152 }
6153
6154
6155 /* Move IT forward until it satisfies one or more of the criteria in
6156 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6157
6158 OP is a bit-mask that specifies where to stop, and in particular,
6159 which of those four position arguments makes a difference. See the
6160 description of enum move_operation_enum.
6161
6162 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6163 screen line, this function will set IT to the next position >
6164 TO_CHARPOS. */
6165
6166 void
6167 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6168 struct it *it;
6169 int to_charpos, to_x, to_y, to_vpos;
6170 int op;
6171 {
6172 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6173 int line_height;
6174 int reached = 0;
6175
6176 for (;;)
6177 {
6178 if (op & MOVE_TO_VPOS)
6179 {
6180 /* If no TO_CHARPOS and no TO_X specified, stop at the
6181 start of the line TO_VPOS. */
6182 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6183 {
6184 if (it->vpos == to_vpos)
6185 {
6186 reached = 1;
6187 break;
6188 }
6189 else
6190 skip = move_it_in_display_line_to (it, -1, -1, 0);
6191 }
6192 else
6193 {
6194 /* TO_VPOS >= 0 means stop at TO_X in the line at
6195 TO_VPOS, or at TO_POS, whichever comes first. */
6196 if (it->vpos == to_vpos)
6197 {
6198 reached = 2;
6199 break;
6200 }
6201
6202 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6203
6204 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6205 {
6206 reached = 3;
6207 break;
6208 }
6209 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6210 {
6211 /* We have reached TO_X but not in the line we want. */
6212 skip = move_it_in_display_line_to (it, to_charpos,
6213 -1, MOVE_TO_POS);
6214 if (skip == MOVE_POS_MATCH_OR_ZV)
6215 {
6216 reached = 4;
6217 break;
6218 }
6219 }
6220 }
6221 }
6222 else if (op & MOVE_TO_Y)
6223 {
6224 struct it it_backup;
6225
6226 /* TO_Y specified means stop at TO_X in the line containing
6227 TO_Y---or at TO_CHARPOS if this is reached first. The
6228 problem is that we can't really tell whether the line
6229 contains TO_Y before we have completely scanned it, and
6230 this may skip past TO_X. What we do is to first scan to
6231 TO_X.
6232
6233 If TO_X is not specified, use a TO_X of zero. The reason
6234 is to make the outcome of this function more predictable.
6235 If we didn't use TO_X == 0, we would stop at the end of
6236 the line which is probably not what a caller would expect
6237 to happen. */
6238 skip = move_it_in_display_line_to (it, to_charpos,
6239 ((op & MOVE_TO_X)
6240 ? to_x : 0),
6241 (MOVE_TO_X
6242 | (op & MOVE_TO_POS)));
6243
6244 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6245 if (skip == MOVE_POS_MATCH_OR_ZV)
6246 {
6247 reached = 5;
6248 break;
6249 }
6250
6251 /* If TO_X was reached, we would like to know whether TO_Y
6252 is in the line. This can only be said if we know the
6253 total line height which requires us to scan the rest of
6254 the line. */
6255 if (skip == MOVE_X_REACHED)
6256 {
6257 it_backup = *it;
6258 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6259 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6260 op & MOVE_TO_POS);
6261 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6262 }
6263
6264 /* Now, decide whether TO_Y is in this line. */
6265 line_height = it->max_ascent + it->max_descent;
6266 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6267
6268 if (to_y >= it->current_y
6269 && to_y < it->current_y + line_height)
6270 {
6271 if (skip == MOVE_X_REACHED)
6272 /* If TO_Y is in this line and TO_X was reached above,
6273 we scanned too far. We have to restore IT's settings
6274 to the ones before skipping. */
6275 *it = it_backup;
6276 reached = 6;
6277 }
6278 else if (skip == MOVE_X_REACHED)
6279 {
6280 skip = skip2;
6281 if (skip == MOVE_POS_MATCH_OR_ZV)
6282 reached = 7;
6283 }
6284
6285 if (reached)
6286 break;
6287 }
6288 else
6289 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6290
6291 switch (skip)
6292 {
6293 case MOVE_POS_MATCH_OR_ZV:
6294 reached = 8;
6295 goto out;
6296
6297 case MOVE_NEWLINE_OR_CR:
6298 set_iterator_to_next (it, 1);
6299 it->continuation_lines_width = 0;
6300 break;
6301
6302 case MOVE_LINE_TRUNCATED:
6303 it->continuation_lines_width = 0;
6304 reseat_at_next_visible_line_start (it, 0);
6305 if ((op & MOVE_TO_POS) != 0
6306 && IT_CHARPOS (*it) > to_charpos)
6307 {
6308 reached = 9;
6309 goto out;
6310 }
6311 break;
6312
6313 case MOVE_LINE_CONTINUED:
6314 it->continuation_lines_width += it->current_x;
6315 break;
6316
6317 default:
6318 abort ();
6319 }
6320
6321 /* Reset/increment for the next run. */
6322 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6323 it->current_x = it->hpos = 0;
6324 it->current_y += it->max_ascent + it->max_descent;
6325 ++it->vpos;
6326 last_height = it->max_ascent + it->max_descent;
6327 last_max_ascent = it->max_ascent;
6328 it->max_ascent = it->max_descent = 0;
6329 }
6330
6331 out:
6332
6333 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6334 }
6335
6336
6337 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6338
6339 If DY > 0, move IT backward at least that many pixels. DY = 0
6340 means move IT backward to the preceding line start or BEGV. This
6341 function may move over more than DY pixels if IT->current_y - DY
6342 ends up in the middle of a line; in this case IT->current_y will be
6343 set to the top of the line moved to. */
6344
6345 void
6346 move_it_vertically_backward (it, dy)
6347 struct it *it;
6348 int dy;
6349 {
6350 int nlines, h;
6351 struct it it2, it3;
6352 int start_pos;
6353
6354 move_further_back:
6355 xassert (dy >= 0);
6356
6357 start_pos = IT_CHARPOS (*it);
6358
6359 /* Estimate how many newlines we must move back. */
6360 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6361
6362 /* Set the iterator's position that many lines back. */
6363 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6364 back_to_previous_visible_line_start (it);
6365
6366 /* Reseat the iterator here. When moving backward, we don't want
6367 reseat to skip forward over invisible text, set up the iterator
6368 to deliver from overlay strings at the new position etc. So,
6369 use reseat_1 here. */
6370 reseat_1 (it, it->current.pos, 1);
6371
6372 /* We are now surely at a line start. */
6373 it->current_x = it->hpos = 0;
6374 it->continuation_lines_width = 0;
6375
6376 /* Move forward and see what y-distance we moved. First move to the
6377 start of the next line so that we get its height. We need this
6378 height to be able to tell whether we reached the specified
6379 y-distance. */
6380 it2 = *it;
6381 it2.max_ascent = it2.max_descent = 0;
6382 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6383 MOVE_TO_POS | MOVE_TO_VPOS);
6384 xassert (IT_CHARPOS (*it) >= BEGV);
6385 it3 = it2;
6386
6387 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6388 xassert (IT_CHARPOS (*it) >= BEGV);
6389 /* H is the actual vertical distance from the position in *IT
6390 and the starting position. */
6391 h = it2.current_y - it->current_y;
6392 /* NLINES is the distance in number of lines. */
6393 nlines = it2.vpos - it->vpos;
6394
6395 /* Correct IT's y and vpos position
6396 so that they are relative to the starting point. */
6397 it->vpos -= nlines;
6398 it->current_y -= h;
6399
6400 if (dy == 0)
6401 {
6402 /* DY == 0 means move to the start of the screen line. The
6403 value of nlines is > 0 if continuation lines were involved. */
6404 if (nlines > 0)
6405 move_it_by_lines (it, nlines, 1);
6406 #if 0
6407 /* I think this assert is bogus if buffer contains
6408 invisible text or images. KFS. */
6409 xassert (IT_CHARPOS (*it) <= start_pos);
6410 #endif
6411 }
6412 else
6413 {
6414 /* The y-position we try to reach, relative to *IT.
6415 Note that H has been subtracted in front of the if-statement. */
6416 int target_y = it->current_y + h - dy;
6417 int y0 = it3.current_y;
6418 int y1 = line_bottom_y (&it3);
6419 int line_height = y1 - y0;
6420
6421 /* If we did not reach target_y, try to move further backward if
6422 we can. If we moved too far backward, try to move forward. */
6423 if (target_y < it->current_y
6424 /* This is heuristic. In a window that's 3 lines high, with
6425 a line height of 13 pixels each, recentering with point
6426 on the bottom line will try to move -39/2 = 19 pixels
6427 backward. Try to avoid moving into the first line. */
6428 && (it->current_y - target_y
6429 > min (window_box_height (it->w), line_height * 2 / 3))
6430 && IT_CHARPOS (*it) > BEGV)
6431 {
6432 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6433 target_y - it->current_y));
6434 dy = it->current_y - target_y;
6435 goto move_further_back;
6436 }
6437 else if (target_y >= it->current_y + line_height
6438 && IT_CHARPOS (*it) < ZV)
6439 {
6440 /* Should move forward by at least one line, maybe more.
6441
6442 Note: Calling move_it_by_lines can be expensive on
6443 terminal frames, where compute_motion is used (via
6444 vmotion) to do the job, when there are very long lines
6445 and truncate-lines is nil. That's the reason for
6446 treating terminal frames specially here. */
6447
6448 if (!FRAME_WINDOW_P (it->f))
6449 move_it_vertically (it, target_y - (it->current_y + line_height));
6450 else
6451 {
6452 do
6453 {
6454 move_it_by_lines (it, 1, 1);
6455 }
6456 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6457 }
6458
6459 #if 0
6460 /* I think this assert is bogus if buffer contains
6461 invisible text or images. KFS. */
6462 xassert (IT_CHARPOS (*it) >= BEGV);
6463 #endif
6464 }
6465 }
6466 }
6467
6468
6469 /* Move IT by a specified amount of pixel lines DY. DY negative means
6470 move backwards. DY = 0 means move to start of screen line. At the
6471 end, IT will be on the start of a screen line. */
6472
6473 void
6474 move_it_vertically (it, dy)
6475 struct it *it;
6476 int dy;
6477 {
6478 if (dy <= 0)
6479 move_it_vertically_backward (it, -dy);
6480 else
6481 {
6482 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6483 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6484 MOVE_TO_POS | MOVE_TO_Y);
6485 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6486
6487 /* If buffer ends in ZV without a newline, move to the start of
6488 the line to satisfy the post-condition. */
6489 if (IT_CHARPOS (*it) == ZV
6490 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6491 move_it_by_lines (it, 0, 0);
6492 }
6493 }
6494
6495
6496 /* Move iterator IT past the end of the text line it is in. */
6497
6498 void
6499 move_it_past_eol (it)
6500 struct it *it;
6501 {
6502 enum move_it_result rc;
6503
6504 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
6505 if (rc == MOVE_NEWLINE_OR_CR)
6506 set_iterator_to_next (it, 0);
6507 }
6508
6509
6510 #if 0 /* Currently not used. */
6511
6512 /* Return non-zero if some text between buffer positions START_CHARPOS
6513 and END_CHARPOS is invisible. IT->window is the window for text
6514 property lookup. */
6515
6516 static int
6517 invisible_text_between_p (it, start_charpos, end_charpos)
6518 struct it *it;
6519 int start_charpos, end_charpos;
6520 {
6521 Lisp_Object prop, limit;
6522 int invisible_found_p;
6523
6524 xassert (it != NULL && start_charpos <= end_charpos);
6525
6526 /* Is text at START invisible? */
6527 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
6528 it->window);
6529 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6530 invisible_found_p = 1;
6531 else
6532 {
6533 limit = Fnext_single_char_property_change (make_number (start_charpos),
6534 Qinvisible, Qnil,
6535 make_number (end_charpos));
6536 invisible_found_p = XFASTINT (limit) < end_charpos;
6537 }
6538
6539 return invisible_found_p;
6540 }
6541
6542 #endif /* 0 */
6543
6544
6545 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
6546 negative means move up. DVPOS == 0 means move to the start of the
6547 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
6548 NEED_Y_P is zero, IT->current_y will be left unchanged.
6549
6550 Further optimization ideas: If we would know that IT->f doesn't use
6551 a face with proportional font, we could be faster for
6552 truncate-lines nil. */
6553
6554 void
6555 move_it_by_lines (it, dvpos, need_y_p)
6556 struct it *it;
6557 int dvpos, need_y_p;
6558 {
6559 struct position pos;
6560
6561 if (!FRAME_WINDOW_P (it->f))
6562 {
6563 struct text_pos textpos;
6564
6565 /* We can use vmotion on frames without proportional fonts. */
6566 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
6567 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
6568 reseat (it, textpos, 1);
6569 it->vpos += pos.vpos;
6570 it->current_y += pos.vpos;
6571 }
6572 else if (dvpos == 0)
6573 {
6574 /* DVPOS == 0 means move to the start of the screen line. */
6575 move_it_vertically_backward (it, 0);
6576 xassert (it->current_x == 0 && it->hpos == 0);
6577 /* Let next call to line_bottom_y calculate real line height */
6578 last_height = 0;
6579 }
6580 else if (dvpos > 0)
6581 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
6582 else
6583 {
6584 struct it it2;
6585 int start_charpos, i;
6586
6587 /* Start at the beginning of the screen line containing IT's
6588 position. */
6589 move_it_vertically_backward (it, 0);
6590
6591 /* Go back -DVPOS visible lines and reseat the iterator there. */
6592 start_charpos = IT_CHARPOS (*it);
6593 for (i = -dvpos; i && IT_CHARPOS (*it) > BEGV; --i)
6594 back_to_previous_visible_line_start (it);
6595 reseat (it, it->current.pos, 1);
6596 it->current_x = it->hpos = 0;
6597
6598 /* Above call may have moved too far if continuation lines
6599 are involved. Scan forward and see if it did. */
6600 it2 = *it;
6601 it2.vpos = it2.current_y = 0;
6602 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
6603 it->vpos -= it2.vpos;
6604 it->current_y -= it2.current_y;
6605 it->current_x = it->hpos = 0;
6606
6607 /* If we moved too far back, move IT some lines forward. */
6608 if (it2.vpos > -dvpos)
6609 {
6610 int delta = it2.vpos + dvpos;
6611 it2 = *it;
6612 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
6613 /* Move back again if we got too far ahead. */
6614 if (IT_CHARPOS (*it) >= start_charpos)
6615 *it = it2;
6616 }
6617 }
6618 }
6619
6620 /* Return 1 if IT points into the middle of a display vector. */
6621
6622 int
6623 in_display_vector_p (it)
6624 struct it *it;
6625 {
6626 return (it->method == GET_FROM_DISPLAY_VECTOR
6627 && it->current.dpvec_index > 0
6628 && it->dpvec + it->current.dpvec_index != it->dpend);
6629 }
6630
6631 \f
6632 /***********************************************************************
6633 Messages
6634 ***********************************************************************/
6635
6636
6637 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
6638 to *Messages*. */
6639
6640 void
6641 add_to_log (format, arg1, arg2)
6642 char *format;
6643 Lisp_Object arg1, arg2;
6644 {
6645 Lisp_Object args[3];
6646 Lisp_Object msg, fmt;
6647 char *buffer;
6648 int len;
6649 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
6650 USE_SAFE_ALLOCA;
6651
6652 /* Do nothing if called asynchronously. Inserting text into
6653 a buffer may call after-change-functions and alike and
6654 that would means running Lisp asynchronously. */
6655 if (handling_signal)
6656 return;
6657
6658 fmt = msg = Qnil;
6659 GCPRO4 (fmt, msg, arg1, arg2);
6660
6661 args[0] = fmt = build_string (format);
6662 args[1] = arg1;
6663 args[2] = arg2;
6664 msg = Fformat (3, args);
6665
6666 len = SBYTES (msg) + 1;
6667 SAFE_ALLOCA (buffer, char *, len);
6668 bcopy (SDATA (msg), buffer, len);
6669
6670 message_dolog (buffer, len - 1, 1, 0);
6671 SAFE_FREE ();
6672
6673 UNGCPRO;
6674 }
6675
6676
6677 /* Output a newline in the *Messages* buffer if "needs" one. */
6678
6679 void
6680 message_log_maybe_newline ()
6681 {
6682 if (message_log_need_newline)
6683 message_dolog ("", 0, 1, 0);
6684 }
6685
6686
6687 /* Add a string M of length NBYTES to the message log, optionally
6688 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
6689 nonzero, means interpret the contents of M as multibyte. This
6690 function calls low-level routines in order to bypass text property
6691 hooks, etc. which might not be safe to run. */
6692
6693 void
6694 message_dolog (m, nbytes, nlflag, multibyte)
6695 const char *m;
6696 int nbytes, nlflag, multibyte;
6697 {
6698 if (!NILP (Vmemory_full))
6699 return;
6700
6701 if (!NILP (Vmessage_log_max))
6702 {
6703 struct buffer *oldbuf;
6704 Lisp_Object oldpoint, oldbegv, oldzv;
6705 int old_windows_or_buffers_changed = windows_or_buffers_changed;
6706 int point_at_end = 0;
6707 int zv_at_end = 0;
6708 Lisp_Object old_deactivate_mark, tem;
6709 struct gcpro gcpro1;
6710
6711 old_deactivate_mark = Vdeactivate_mark;
6712 oldbuf = current_buffer;
6713 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
6714 current_buffer->undo_list = Qt;
6715
6716 oldpoint = message_dolog_marker1;
6717 set_marker_restricted (oldpoint, make_number (PT), Qnil);
6718 oldbegv = message_dolog_marker2;
6719 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
6720 oldzv = message_dolog_marker3;
6721 set_marker_restricted (oldzv, make_number (ZV), Qnil);
6722 GCPRO1 (old_deactivate_mark);
6723
6724 if (PT == Z)
6725 point_at_end = 1;
6726 if (ZV == Z)
6727 zv_at_end = 1;
6728
6729 BEGV = BEG;
6730 BEGV_BYTE = BEG_BYTE;
6731 ZV = Z;
6732 ZV_BYTE = Z_BYTE;
6733 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6734
6735 /* Insert the string--maybe converting multibyte to single byte
6736 or vice versa, so that all the text fits the buffer. */
6737 if (multibyte
6738 && NILP (current_buffer->enable_multibyte_characters))
6739 {
6740 int i, c, char_bytes;
6741 unsigned char work[1];
6742
6743 /* Convert a multibyte string to single-byte
6744 for the *Message* buffer. */
6745 for (i = 0; i < nbytes; i += char_bytes)
6746 {
6747 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
6748 work[0] = (SINGLE_BYTE_CHAR_P (c)
6749 ? c
6750 : multibyte_char_to_unibyte (c, Qnil));
6751 insert_1_both (work, 1, 1, 1, 0, 0);
6752 }
6753 }
6754 else if (! multibyte
6755 && ! NILP (current_buffer->enable_multibyte_characters))
6756 {
6757 int i, c, char_bytes;
6758 unsigned char *msg = (unsigned char *) m;
6759 unsigned char str[MAX_MULTIBYTE_LENGTH];
6760 /* Convert a single-byte string to multibyte
6761 for the *Message* buffer. */
6762 for (i = 0; i < nbytes; i++)
6763 {
6764 c = unibyte_char_to_multibyte (msg[i]);
6765 char_bytes = CHAR_STRING (c, str);
6766 insert_1_both (str, 1, char_bytes, 1, 0, 0);
6767 }
6768 }
6769 else if (nbytes)
6770 insert_1 (m, nbytes, 1, 0, 0);
6771
6772 if (nlflag)
6773 {
6774 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
6775 insert_1 ("\n", 1, 1, 0, 0);
6776
6777 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
6778 this_bol = PT;
6779 this_bol_byte = PT_BYTE;
6780
6781 /* See if this line duplicates the previous one.
6782 If so, combine duplicates. */
6783 if (this_bol > BEG)
6784 {
6785 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
6786 prev_bol = PT;
6787 prev_bol_byte = PT_BYTE;
6788
6789 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
6790 this_bol, this_bol_byte);
6791 if (dup)
6792 {
6793 del_range_both (prev_bol, prev_bol_byte,
6794 this_bol, this_bol_byte, 0);
6795 if (dup > 1)
6796 {
6797 char dupstr[40];
6798 int duplen;
6799
6800 /* If you change this format, don't forget to also
6801 change message_log_check_duplicate. */
6802 sprintf (dupstr, " [%d times]", dup);
6803 duplen = strlen (dupstr);
6804 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
6805 insert_1 (dupstr, duplen, 1, 0, 1);
6806 }
6807 }
6808 }
6809
6810 /* If we have more than the desired maximum number of lines
6811 in the *Messages* buffer now, delete the oldest ones.
6812 This is safe because we don't have undo in this buffer. */
6813
6814 if (NATNUMP (Vmessage_log_max))
6815 {
6816 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
6817 -XFASTINT (Vmessage_log_max) - 1, 0);
6818 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
6819 }
6820 }
6821 BEGV = XMARKER (oldbegv)->charpos;
6822 BEGV_BYTE = marker_byte_position (oldbegv);
6823
6824 if (zv_at_end)
6825 {
6826 ZV = Z;
6827 ZV_BYTE = Z_BYTE;
6828 }
6829 else
6830 {
6831 ZV = XMARKER (oldzv)->charpos;
6832 ZV_BYTE = marker_byte_position (oldzv);
6833 }
6834
6835 if (point_at_end)
6836 TEMP_SET_PT_BOTH (Z, Z_BYTE);
6837 else
6838 /* We can't do Fgoto_char (oldpoint) because it will run some
6839 Lisp code. */
6840 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
6841 XMARKER (oldpoint)->bytepos);
6842
6843 UNGCPRO;
6844 unchain_marker (XMARKER (oldpoint));
6845 unchain_marker (XMARKER (oldbegv));
6846 unchain_marker (XMARKER (oldzv));
6847
6848 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
6849 set_buffer_internal (oldbuf);
6850 if (NILP (tem))
6851 windows_or_buffers_changed = old_windows_or_buffers_changed;
6852 message_log_need_newline = !nlflag;
6853 Vdeactivate_mark = old_deactivate_mark;
6854 }
6855 }
6856
6857
6858 /* We are at the end of the buffer after just having inserted a newline.
6859 (Note: We depend on the fact we won't be crossing the gap.)
6860 Check to see if the most recent message looks a lot like the previous one.
6861 Return 0 if different, 1 if the new one should just replace it, or a
6862 value N > 1 if we should also append " [N times]". */
6863
6864 static int
6865 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
6866 int prev_bol, this_bol;
6867 int prev_bol_byte, this_bol_byte;
6868 {
6869 int i;
6870 int len = Z_BYTE - 1 - this_bol_byte;
6871 int seen_dots = 0;
6872 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
6873 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
6874
6875 for (i = 0; i < len; i++)
6876 {
6877 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
6878 seen_dots = 1;
6879 if (p1[i] != p2[i])
6880 return seen_dots;
6881 }
6882 p1 += len;
6883 if (*p1 == '\n')
6884 return 2;
6885 if (*p1++ == ' ' && *p1++ == '[')
6886 {
6887 int n = 0;
6888 while (*p1 >= '0' && *p1 <= '9')
6889 n = n * 10 + *p1++ - '0';
6890 if (strncmp (p1, " times]\n", 8) == 0)
6891 return n+1;
6892 }
6893 return 0;
6894 }
6895 \f
6896
6897 /* Display an echo area message M with a specified length of NBYTES
6898 bytes. The string may include null characters. If M is 0, clear
6899 out any existing message, and let the mini-buffer text show
6900 through.
6901
6902 The buffer M must continue to exist until after the echo area gets
6903 cleared or some other message gets displayed there. This means do
6904 not pass text that is stored in a Lisp string; do not pass text in
6905 a buffer that was alloca'd. */
6906
6907 void
6908 message2 (m, nbytes, multibyte)
6909 const char *m;
6910 int nbytes;
6911 int multibyte;
6912 {
6913 /* First flush out any partial line written with print. */
6914 message_log_maybe_newline ();
6915 if (m)
6916 message_dolog (m, nbytes, 1, multibyte);
6917 message2_nolog (m, nbytes, multibyte);
6918 }
6919
6920
6921 /* The non-logging counterpart of message2. */
6922
6923 void
6924 message2_nolog (m, nbytes, multibyte)
6925 const char *m;
6926 int nbytes, multibyte;
6927 {
6928 struct frame *sf = SELECTED_FRAME ();
6929 message_enable_multibyte = multibyte;
6930
6931 if (noninteractive)
6932 {
6933 if (noninteractive_need_newline)
6934 putc ('\n', stderr);
6935 noninteractive_need_newline = 0;
6936 if (m)
6937 fwrite (m, nbytes, 1, stderr);
6938 if (cursor_in_echo_area == 0)
6939 fprintf (stderr, "\n");
6940 fflush (stderr);
6941 }
6942 /* A null message buffer means that the frame hasn't really been
6943 initialized yet. Error messages get reported properly by
6944 cmd_error, so this must be just an informative message; toss it. */
6945 else if (INTERACTIVE
6946 && sf->glyphs_initialized_p
6947 && FRAME_MESSAGE_BUF (sf))
6948 {
6949 Lisp_Object mini_window;
6950 struct frame *f;
6951
6952 /* Get the frame containing the mini-buffer
6953 that the selected frame is using. */
6954 mini_window = FRAME_MINIBUF_WINDOW (sf);
6955 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
6956
6957 FRAME_SAMPLE_VISIBILITY (f);
6958 if (FRAME_VISIBLE_P (sf)
6959 && ! FRAME_VISIBLE_P (f))
6960 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
6961
6962 if (m)
6963 {
6964 set_message (m, Qnil, nbytes, multibyte);
6965 if (minibuffer_auto_raise)
6966 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
6967 }
6968 else
6969 clear_message (1, 1);
6970
6971 do_pending_window_change (0);
6972 echo_area_display (1);
6973 do_pending_window_change (0);
6974 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
6975 (*frame_up_to_date_hook) (f);
6976 }
6977 }
6978
6979
6980 /* Display an echo area message M with a specified length of NBYTES
6981 bytes. The string may include null characters. If M is not a
6982 string, clear out any existing message, and let the mini-buffer
6983 text show through.
6984
6985 This function cancels echoing. */
6986
6987 void
6988 message3 (m, nbytes, multibyte)
6989 Lisp_Object m;
6990 int nbytes;
6991 int multibyte;
6992 {
6993 struct gcpro gcpro1;
6994
6995 GCPRO1 (m);
6996 clear_message (1,1);
6997 cancel_echoing ();
6998
6999 /* First flush out any partial line written with print. */
7000 message_log_maybe_newline ();
7001 if (STRINGP (m))
7002 message_dolog (SDATA (m), nbytes, 1, multibyte);
7003 message3_nolog (m, nbytes, multibyte);
7004
7005 UNGCPRO;
7006 }
7007
7008
7009 /* The non-logging version of message3.
7010 This does not cancel echoing, because it is used for echoing.
7011 Perhaps we need to make a separate function for echoing
7012 and make this cancel echoing. */
7013
7014 void
7015 message3_nolog (m, nbytes, multibyte)
7016 Lisp_Object m;
7017 int nbytes, multibyte;
7018 {
7019 struct frame *sf = SELECTED_FRAME ();
7020 message_enable_multibyte = multibyte;
7021
7022 if (noninteractive)
7023 {
7024 if (noninteractive_need_newline)
7025 putc ('\n', stderr);
7026 noninteractive_need_newline = 0;
7027 if (STRINGP (m))
7028 fwrite (SDATA (m), nbytes, 1, stderr);
7029 if (cursor_in_echo_area == 0)
7030 fprintf (stderr, "\n");
7031 fflush (stderr);
7032 }
7033 /* A null message buffer means that the frame hasn't really been
7034 initialized yet. Error messages get reported properly by
7035 cmd_error, so this must be just an informative message; toss it. */
7036 else if (INTERACTIVE
7037 && sf->glyphs_initialized_p
7038 && FRAME_MESSAGE_BUF (sf))
7039 {
7040 Lisp_Object mini_window;
7041 Lisp_Object frame;
7042 struct frame *f;
7043
7044 /* Get the frame containing the mini-buffer
7045 that the selected frame is using. */
7046 mini_window = FRAME_MINIBUF_WINDOW (sf);
7047 frame = XWINDOW (mini_window)->frame;
7048 f = XFRAME (frame);
7049
7050 FRAME_SAMPLE_VISIBILITY (f);
7051 if (FRAME_VISIBLE_P (sf)
7052 && !FRAME_VISIBLE_P (f))
7053 Fmake_frame_visible (frame);
7054
7055 if (STRINGP (m) && SCHARS (m) > 0)
7056 {
7057 set_message (NULL, m, nbytes, multibyte);
7058 if (minibuffer_auto_raise)
7059 Fraise_frame (frame);
7060 }
7061 else
7062 clear_message (1, 1);
7063
7064 do_pending_window_change (0);
7065 echo_area_display (1);
7066 do_pending_window_change (0);
7067 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7068 (*frame_up_to_date_hook) (f);
7069 }
7070 }
7071
7072
7073 /* Display a null-terminated echo area message M. If M is 0, clear
7074 out any existing message, and let the mini-buffer text show through.
7075
7076 The buffer M must continue to exist until after the echo area gets
7077 cleared or some other message gets displayed there. Do not pass
7078 text that is stored in a Lisp string. Do not pass text in a buffer
7079 that was alloca'd. */
7080
7081 void
7082 message1 (m)
7083 char *m;
7084 {
7085 message2 (m, (m ? strlen (m) : 0), 0);
7086 }
7087
7088
7089 /* The non-logging counterpart of message1. */
7090
7091 void
7092 message1_nolog (m)
7093 char *m;
7094 {
7095 message2_nolog (m, (m ? strlen (m) : 0), 0);
7096 }
7097
7098 /* Display a message M which contains a single %s
7099 which gets replaced with STRING. */
7100
7101 void
7102 message_with_string (m, string, log)
7103 char *m;
7104 Lisp_Object string;
7105 int log;
7106 {
7107 CHECK_STRING (string);
7108
7109 if (noninteractive)
7110 {
7111 if (m)
7112 {
7113 if (noninteractive_need_newline)
7114 putc ('\n', stderr);
7115 noninteractive_need_newline = 0;
7116 fprintf (stderr, m, SDATA (string));
7117 if (cursor_in_echo_area == 0)
7118 fprintf (stderr, "\n");
7119 fflush (stderr);
7120 }
7121 }
7122 else if (INTERACTIVE)
7123 {
7124 /* The frame whose minibuffer we're going to display the message on.
7125 It may be larger than the selected frame, so we need
7126 to use its buffer, not the selected frame's buffer. */
7127 Lisp_Object mini_window;
7128 struct frame *f, *sf = SELECTED_FRAME ();
7129
7130 /* Get the frame containing the minibuffer
7131 that the selected frame is using. */
7132 mini_window = FRAME_MINIBUF_WINDOW (sf);
7133 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7134
7135 /* A null message buffer means that the frame hasn't really been
7136 initialized yet. Error messages get reported properly by
7137 cmd_error, so this must be just an informative message; toss it. */
7138 if (FRAME_MESSAGE_BUF (f))
7139 {
7140 Lisp_Object args[2], message;
7141 struct gcpro gcpro1, gcpro2;
7142
7143 args[0] = build_string (m);
7144 args[1] = message = string;
7145 GCPRO2 (args[0], message);
7146 gcpro1.nvars = 2;
7147
7148 message = Fformat (2, args);
7149
7150 if (log)
7151 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7152 else
7153 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7154
7155 UNGCPRO;
7156
7157 /* Print should start at the beginning of the message
7158 buffer next time. */
7159 message_buf_print = 0;
7160 }
7161 }
7162 }
7163
7164
7165 /* Dump an informative message to the minibuf. If M is 0, clear out
7166 any existing message, and let the mini-buffer text show through. */
7167
7168 /* VARARGS 1 */
7169 void
7170 message (m, a1, a2, a3)
7171 char *m;
7172 EMACS_INT a1, a2, a3;
7173 {
7174 if (noninteractive)
7175 {
7176 if (m)
7177 {
7178 if (noninteractive_need_newline)
7179 putc ('\n', stderr);
7180 noninteractive_need_newline = 0;
7181 fprintf (stderr, m, a1, a2, a3);
7182 if (cursor_in_echo_area == 0)
7183 fprintf (stderr, "\n");
7184 fflush (stderr);
7185 }
7186 }
7187 else if (INTERACTIVE)
7188 {
7189 /* The frame whose mini-buffer we're going to display the message
7190 on. It may be larger than the selected frame, so we need to
7191 use its buffer, not the selected frame's buffer. */
7192 Lisp_Object mini_window;
7193 struct frame *f, *sf = SELECTED_FRAME ();
7194
7195 /* Get the frame containing the mini-buffer
7196 that the selected frame is using. */
7197 mini_window = FRAME_MINIBUF_WINDOW (sf);
7198 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7199
7200 /* A null message buffer means that the frame hasn't really been
7201 initialized yet. Error messages get reported properly by
7202 cmd_error, so this must be just an informative message; toss
7203 it. */
7204 if (FRAME_MESSAGE_BUF (f))
7205 {
7206 if (m)
7207 {
7208 int len;
7209 #ifdef NO_ARG_ARRAY
7210 char *a[3];
7211 a[0] = (char *) a1;
7212 a[1] = (char *) a2;
7213 a[2] = (char *) a3;
7214
7215 len = doprnt (FRAME_MESSAGE_BUF (f),
7216 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7217 #else
7218 len = doprnt (FRAME_MESSAGE_BUF (f),
7219 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7220 (char **) &a1);
7221 #endif /* NO_ARG_ARRAY */
7222
7223 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7224 }
7225 else
7226 message1 (0);
7227
7228 /* Print should start at the beginning of the message
7229 buffer next time. */
7230 message_buf_print = 0;
7231 }
7232 }
7233 }
7234
7235
7236 /* The non-logging version of message. */
7237
7238 void
7239 message_nolog (m, a1, a2, a3)
7240 char *m;
7241 EMACS_INT a1, a2, a3;
7242 {
7243 Lisp_Object old_log_max;
7244 old_log_max = Vmessage_log_max;
7245 Vmessage_log_max = Qnil;
7246 message (m, a1, a2, a3);
7247 Vmessage_log_max = old_log_max;
7248 }
7249
7250
7251 /* Display the current message in the current mini-buffer. This is
7252 only called from error handlers in process.c, and is not time
7253 critical. */
7254
7255 void
7256 update_echo_area ()
7257 {
7258 if (!NILP (echo_area_buffer[0]))
7259 {
7260 Lisp_Object string;
7261 string = Fcurrent_message ();
7262 message3 (string, SBYTES (string),
7263 !NILP (current_buffer->enable_multibyte_characters));
7264 }
7265 }
7266
7267
7268 /* Make sure echo area buffers in `echo_buffers' are live.
7269 If they aren't, make new ones. */
7270
7271 static void
7272 ensure_echo_area_buffers ()
7273 {
7274 int i;
7275
7276 for (i = 0; i < 2; ++i)
7277 if (!BUFFERP (echo_buffer[i])
7278 || NILP (XBUFFER (echo_buffer[i])->name))
7279 {
7280 char name[30];
7281 Lisp_Object old_buffer;
7282 int j;
7283
7284 old_buffer = echo_buffer[i];
7285 sprintf (name, " *Echo Area %d*", i);
7286 echo_buffer[i] = Fget_buffer_create (build_string (name));
7287 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7288
7289 for (j = 0; j < 2; ++j)
7290 if (EQ (old_buffer, echo_area_buffer[j]))
7291 echo_area_buffer[j] = echo_buffer[i];
7292 }
7293 }
7294
7295
7296 /* Call FN with args A1..A4 with either the current or last displayed
7297 echo_area_buffer as current buffer.
7298
7299 WHICH zero means use the current message buffer
7300 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7301 from echo_buffer[] and clear it.
7302
7303 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7304 suitable buffer from echo_buffer[] and clear it.
7305
7306 Value is what FN returns. */
7307
7308 static int
7309 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7310 struct window *w;
7311 int which;
7312 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7313 EMACS_INT a1;
7314 Lisp_Object a2;
7315 EMACS_INT a3, a4;
7316 {
7317 Lisp_Object buffer;
7318 int this_one, the_other, clear_buffer_p, rc;
7319 int count = SPECPDL_INDEX ();
7320
7321 /* If buffers aren't live, make new ones. */
7322 ensure_echo_area_buffers ();
7323
7324 clear_buffer_p = 0;
7325
7326 if (which == 0)
7327 this_one = 0, the_other = 1;
7328 else if (which > 0)
7329 this_one = 1, the_other = 0;
7330
7331 /* Choose a suitable buffer from echo_buffer[] is we don't
7332 have one. */
7333 if (NILP (echo_area_buffer[this_one]))
7334 {
7335 echo_area_buffer[this_one]
7336 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7337 ? echo_buffer[the_other]
7338 : echo_buffer[this_one]);
7339 clear_buffer_p = 1;
7340 }
7341
7342 buffer = echo_area_buffer[this_one];
7343
7344 /* Don't get confused by reusing the buffer used for echoing
7345 for a different purpose. */
7346 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7347 cancel_echoing ();
7348
7349 record_unwind_protect (unwind_with_echo_area_buffer,
7350 with_echo_area_buffer_unwind_data (w));
7351
7352 /* Make the echo area buffer current. Note that for display
7353 purposes, it is not necessary that the displayed window's buffer
7354 == current_buffer, except for text property lookup. So, let's
7355 only set that buffer temporarily here without doing a full
7356 Fset_window_buffer. We must also change w->pointm, though,
7357 because otherwise an assertions in unshow_buffer fails, and Emacs
7358 aborts. */
7359 set_buffer_internal_1 (XBUFFER (buffer));
7360 if (w)
7361 {
7362 w->buffer = buffer;
7363 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7364 }
7365
7366 current_buffer->undo_list = Qt;
7367 current_buffer->read_only = Qnil;
7368 specbind (Qinhibit_read_only, Qt);
7369 specbind (Qinhibit_modification_hooks, Qt);
7370
7371 if (clear_buffer_p && Z > BEG)
7372 del_range (BEG, Z);
7373
7374 xassert (BEGV >= BEG);
7375 xassert (ZV <= Z && ZV >= BEGV);
7376
7377 rc = fn (a1, a2, a3, a4);
7378
7379 xassert (BEGV >= BEG);
7380 xassert (ZV <= Z && ZV >= BEGV);
7381
7382 unbind_to (count, Qnil);
7383 return rc;
7384 }
7385
7386
7387 /* Save state that should be preserved around the call to the function
7388 FN called in with_echo_area_buffer. */
7389
7390 static Lisp_Object
7391 with_echo_area_buffer_unwind_data (w)
7392 struct window *w;
7393 {
7394 int i = 0;
7395 Lisp_Object vector;
7396
7397 /* Reduce consing by keeping one vector in
7398 Vwith_echo_area_save_vector. */
7399 vector = Vwith_echo_area_save_vector;
7400 Vwith_echo_area_save_vector = Qnil;
7401
7402 if (NILP (vector))
7403 vector = Fmake_vector (make_number (7), Qnil);
7404
7405 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7406 AREF (vector, i) = Vdeactivate_mark, ++i;
7407 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7408
7409 if (w)
7410 {
7411 XSETWINDOW (AREF (vector, i), w); ++i;
7412 AREF (vector, i) = w->buffer; ++i;
7413 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7414 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7415 }
7416 else
7417 {
7418 int end = i + 4;
7419 for (; i < end; ++i)
7420 AREF (vector, i) = Qnil;
7421 }
7422
7423 xassert (i == ASIZE (vector));
7424 return vector;
7425 }
7426
7427
7428 /* Restore global state from VECTOR which was created by
7429 with_echo_area_buffer_unwind_data. */
7430
7431 static Lisp_Object
7432 unwind_with_echo_area_buffer (vector)
7433 Lisp_Object vector;
7434 {
7435 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7436 Vdeactivate_mark = AREF (vector, 1);
7437 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7438
7439 if (WINDOWP (AREF (vector, 3)))
7440 {
7441 struct window *w;
7442 Lisp_Object buffer, charpos, bytepos;
7443
7444 w = XWINDOW (AREF (vector, 3));
7445 buffer = AREF (vector, 4);
7446 charpos = AREF (vector, 5);
7447 bytepos = AREF (vector, 6);
7448
7449 w->buffer = buffer;
7450 set_marker_both (w->pointm, buffer,
7451 XFASTINT (charpos), XFASTINT (bytepos));
7452 }
7453
7454 Vwith_echo_area_save_vector = vector;
7455 return Qnil;
7456 }
7457
7458
7459 /* Set up the echo area for use by print functions. MULTIBYTE_P
7460 non-zero means we will print multibyte. */
7461
7462 void
7463 setup_echo_area_for_printing (multibyte_p)
7464 int multibyte_p;
7465 {
7466 /* If we can't find an echo area any more, exit. */
7467 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
7468 Fkill_emacs (Qnil);
7469
7470 ensure_echo_area_buffers ();
7471
7472 if (!message_buf_print)
7473 {
7474 /* A message has been output since the last time we printed.
7475 Choose a fresh echo area buffer. */
7476 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7477 echo_area_buffer[0] = echo_buffer[1];
7478 else
7479 echo_area_buffer[0] = echo_buffer[0];
7480
7481 /* Switch to that buffer and clear it. */
7482 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7483 current_buffer->truncate_lines = Qnil;
7484
7485 if (Z > BEG)
7486 {
7487 int count = SPECPDL_INDEX ();
7488 specbind (Qinhibit_read_only, Qt);
7489 /* Note that undo recording is always disabled. */
7490 del_range (BEG, Z);
7491 unbind_to (count, Qnil);
7492 }
7493 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7494
7495 /* Set up the buffer for the multibyteness we need. */
7496 if (multibyte_p
7497 != !NILP (current_buffer->enable_multibyte_characters))
7498 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
7499
7500 /* Raise the frame containing the echo area. */
7501 if (minibuffer_auto_raise)
7502 {
7503 struct frame *sf = SELECTED_FRAME ();
7504 Lisp_Object mini_window;
7505 mini_window = FRAME_MINIBUF_WINDOW (sf);
7506 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7507 }
7508
7509 message_log_maybe_newline ();
7510 message_buf_print = 1;
7511 }
7512 else
7513 {
7514 if (NILP (echo_area_buffer[0]))
7515 {
7516 if (EQ (echo_area_buffer[1], echo_buffer[0]))
7517 echo_area_buffer[0] = echo_buffer[1];
7518 else
7519 echo_area_buffer[0] = echo_buffer[0];
7520 }
7521
7522 if (current_buffer != XBUFFER (echo_area_buffer[0]))
7523 {
7524 /* Someone switched buffers between print requests. */
7525 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
7526 current_buffer->truncate_lines = Qnil;
7527 }
7528 }
7529 }
7530
7531
7532 /* Display an echo area message in window W. Value is non-zero if W's
7533 height is changed. If display_last_displayed_message_p is
7534 non-zero, display the message that was last displayed, otherwise
7535 display the current message. */
7536
7537 static int
7538 display_echo_area (w)
7539 struct window *w;
7540 {
7541 int i, no_message_p, window_height_changed_p, count;
7542
7543 /* Temporarily disable garbage collections while displaying the echo
7544 area. This is done because a GC can print a message itself.
7545 That message would modify the echo area buffer's contents while a
7546 redisplay of the buffer is going on, and seriously confuse
7547 redisplay. */
7548 count = inhibit_garbage_collection ();
7549
7550 /* If there is no message, we must call display_echo_area_1
7551 nevertheless because it resizes the window. But we will have to
7552 reset the echo_area_buffer in question to nil at the end because
7553 with_echo_area_buffer will sets it to an empty buffer. */
7554 i = display_last_displayed_message_p ? 1 : 0;
7555 no_message_p = NILP (echo_area_buffer[i]);
7556
7557 window_height_changed_p
7558 = with_echo_area_buffer (w, display_last_displayed_message_p,
7559 display_echo_area_1,
7560 (EMACS_INT) w, Qnil, 0, 0);
7561
7562 if (no_message_p)
7563 echo_area_buffer[i] = Qnil;
7564
7565 unbind_to (count, Qnil);
7566 return window_height_changed_p;
7567 }
7568
7569
7570 /* Helper for display_echo_area. Display the current buffer which
7571 contains the current echo area message in window W, a mini-window,
7572 a pointer to which is passed in A1. A2..A4 are currently not used.
7573 Change the height of W so that all of the message is displayed.
7574 Value is non-zero if height of W was changed. */
7575
7576 static int
7577 display_echo_area_1 (a1, a2, a3, a4)
7578 EMACS_INT a1;
7579 Lisp_Object a2;
7580 EMACS_INT a3, a4;
7581 {
7582 struct window *w = (struct window *) a1;
7583 Lisp_Object window;
7584 struct text_pos start;
7585 int window_height_changed_p = 0;
7586
7587 /* Do this before displaying, so that we have a large enough glyph
7588 matrix for the display. */
7589 window_height_changed_p = resize_mini_window (w, 0);
7590
7591 /* Display. */
7592 clear_glyph_matrix (w->desired_matrix);
7593 XSETWINDOW (window, w);
7594 SET_TEXT_POS (start, BEG, BEG_BYTE);
7595 try_window (window, start, 0);
7596
7597 return window_height_changed_p;
7598 }
7599
7600
7601 /* Resize the echo area window to exactly the size needed for the
7602 currently displayed message, if there is one. If a mini-buffer
7603 is active, don't shrink it. */
7604
7605 void
7606 resize_echo_area_exactly ()
7607 {
7608 if (BUFFERP (echo_area_buffer[0])
7609 && WINDOWP (echo_area_window))
7610 {
7611 struct window *w = XWINDOW (echo_area_window);
7612 int resized_p;
7613 Lisp_Object resize_exactly;
7614
7615 if (minibuf_level == 0)
7616 resize_exactly = Qt;
7617 else
7618 resize_exactly = Qnil;
7619
7620 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
7621 (EMACS_INT) w, resize_exactly, 0, 0);
7622 if (resized_p)
7623 {
7624 ++windows_or_buffers_changed;
7625 ++update_mode_lines;
7626 redisplay_internal (0);
7627 }
7628 }
7629 }
7630
7631
7632 /* Callback function for with_echo_area_buffer, when used from
7633 resize_echo_area_exactly. A1 contains a pointer to the window to
7634 resize, EXACTLY non-nil means resize the mini-window exactly to the
7635 size of the text displayed. A3 and A4 are not used. Value is what
7636 resize_mini_window returns. */
7637
7638 static int
7639 resize_mini_window_1 (a1, exactly, a3, a4)
7640 EMACS_INT a1;
7641 Lisp_Object exactly;
7642 EMACS_INT a3, a4;
7643 {
7644 return resize_mini_window ((struct window *) a1, !NILP (exactly));
7645 }
7646
7647
7648 /* Resize mini-window W to fit the size of its contents. EXACT:P
7649 means size the window exactly to the size needed. Otherwise, it's
7650 only enlarged until W's buffer is empty. Value is non-zero if
7651 the window height has been changed. */
7652
7653 int
7654 resize_mini_window (w, exact_p)
7655 struct window *w;
7656 int exact_p;
7657 {
7658 struct frame *f = XFRAME (w->frame);
7659 int window_height_changed_p = 0;
7660
7661 xassert (MINI_WINDOW_P (w));
7662
7663 /* Don't resize windows while redisplaying a window; it would
7664 confuse redisplay functions when the size of the window they are
7665 displaying changes from under them. Such a resizing can happen,
7666 for instance, when which-func prints a long message while
7667 we are running fontification-functions. We're running these
7668 functions with safe_call which binds inhibit-redisplay to t. */
7669 if (!NILP (Vinhibit_redisplay))
7670 return 0;
7671
7672 /* Nil means don't try to resize. */
7673 if (NILP (Vresize_mini_windows)
7674 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
7675 return 0;
7676
7677 if (!FRAME_MINIBUF_ONLY_P (f))
7678 {
7679 struct it it;
7680 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
7681 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
7682 int height, max_height;
7683 int unit = FRAME_LINE_HEIGHT (f);
7684 struct text_pos start;
7685 struct buffer *old_current_buffer = NULL;
7686
7687 if (current_buffer != XBUFFER (w->buffer))
7688 {
7689 old_current_buffer = current_buffer;
7690 set_buffer_internal (XBUFFER (w->buffer));
7691 }
7692
7693 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
7694
7695 /* Compute the max. number of lines specified by the user. */
7696 if (FLOATP (Vmax_mini_window_height))
7697 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
7698 else if (INTEGERP (Vmax_mini_window_height))
7699 max_height = XINT (Vmax_mini_window_height);
7700 else
7701 max_height = total_height / 4;
7702
7703 /* Correct that max. height if it's bogus. */
7704 max_height = max (1, max_height);
7705 max_height = min (total_height, max_height);
7706
7707 /* Find out the height of the text in the window. */
7708 if (it.truncate_lines_p)
7709 height = 1;
7710 else
7711 {
7712 last_height = 0;
7713 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
7714 if (it.max_ascent == 0 && it.max_descent == 0)
7715 height = it.current_y + last_height;
7716 else
7717 height = it.current_y + it.max_ascent + it.max_descent;
7718 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
7719 height = (height + unit - 1) / unit;
7720 }
7721
7722 /* Compute a suitable window start. */
7723 if (height > max_height)
7724 {
7725 height = max_height;
7726 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
7727 move_it_vertically_backward (&it, (height - 1) * unit);
7728 start = it.current.pos;
7729 }
7730 else
7731 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
7732 SET_MARKER_FROM_TEXT_POS (w->start, start);
7733
7734 if (EQ (Vresize_mini_windows, Qgrow_only))
7735 {
7736 /* Let it grow only, until we display an empty message, in which
7737 case the window shrinks again. */
7738 if (height > WINDOW_TOTAL_LINES (w))
7739 {
7740 int old_height = WINDOW_TOTAL_LINES (w);
7741 freeze_window_starts (f, 1);
7742 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7743 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7744 }
7745 else if (height < WINDOW_TOTAL_LINES (w)
7746 && (exact_p || BEGV == ZV))
7747 {
7748 int old_height = WINDOW_TOTAL_LINES (w);
7749 freeze_window_starts (f, 0);
7750 shrink_mini_window (w);
7751 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7752 }
7753 }
7754 else
7755 {
7756 /* Always resize to exact size needed. */
7757 if (height > WINDOW_TOTAL_LINES (w))
7758 {
7759 int old_height = WINDOW_TOTAL_LINES (w);
7760 freeze_window_starts (f, 1);
7761 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7762 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7763 }
7764 else if (height < WINDOW_TOTAL_LINES (w))
7765 {
7766 int old_height = WINDOW_TOTAL_LINES (w);
7767 freeze_window_starts (f, 0);
7768 shrink_mini_window (w);
7769
7770 if (height)
7771 {
7772 freeze_window_starts (f, 1);
7773 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
7774 }
7775
7776 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
7777 }
7778 }
7779
7780 if (old_current_buffer)
7781 set_buffer_internal (old_current_buffer);
7782 }
7783
7784 return window_height_changed_p;
7785 }
7786
7787
7788 /* Value is the current message, a string, or nil if there is no
7789 current message. */
7790
7791 Lisp_Object
7792 current_message ()
7793 {
7794 Lisp_Object msg;
7795
7796 if (NILP (echo_area_buffer[0]))
7797 msg = Qnil;
7798 else
7799 {
7800 with_echo_area_buffer (0, 0, current_message_1,
7801 (EMACS_INT) &msg, Qnil, 0, 0);
7802 if (NILP (msg))
7803 echo_area_buffer[0] = Qnil;
7804 }
7805
7806 return msg;
7807 }
7808
7809
7810 static int
7811 current_message_1 (a1, a2, a3, a4)
7812 EMACS_INT a1;
7813 Lisp_Object a2;
7814 EMACS_INT a3, a4;
7815 {
7816 Lisp_Object *msg = (Lisp_Object *) a1;
7817
7818 if (Z > BEG)
7819 *msg = make_buffer_string (BEG, Z, 1);
7820 else
7821 *msg = Qnil;
7822 return 0;
7823 }
7824
7825
7826 /* Push the current message on Vmessage_stack for later restauration
7827 by restore_message. Value is non-zero if the current message isn't
7828 empty. This is a relatively infrequent operation, so it's not
7829 worth optimizing. */
7830
7831 int
7832 push_message ()
7833 {
7834 Lisp_Object msg;
7835 msg = current_message ();
7836 Vmessage_stack = Fcons (msg, Vmessage_stack);
7837 return STRINGP (msg);
7838 }
7839
7840
7841 /* Restore message display from the top of Vmessage_stack. */
7842
7843 void
7844 restore_message ()
7845 {
7846 Lisp_Object msg;
7847
7848 xassert (CONSP (Vmessage_stack));
7849 msg = XCAR (Vmessage_stack);
7850 if (STRINGP (msg))
7851 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
7852 else
7853 message3_nolog (msg, 0, 0);
7854 }
7855
7856
7857 /* Handler for record_unwind_protect calling pop_message. */
7858
7859 Lisp_Object
7860 pop_message_unwind (dummy)
7861 Lisp_Object dummy;
7862 {
7863 pop_message ();
7864 return Qnil;
7865 }
7866
7867 /* Pop the top-most entry off Vmessage_stack. */
7868
7869 void
7870 pop_message ()
7871 {
7872 xassert (CONSP (Vmessage_stack));
7873 Vmessage_stack = XCDR (Vmessage_stack);
7874 }
7875
7876
7877 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
7878 exits. If the stack is not empty, we have a missing pop_message
7879 somewhere. */
7880
7881 void
7882 check_message_stack ()
7883 {
7884 if (!NILP (Vmessage_stack))
7885 abort ();
7886 }
7887
7888
7889 /* Truncate to NCHARS what will be displayed in the echo area the next
7890 time we display it---but don't redisplay it now. */
7891
7892 void
7893 truncate_echo_area (nchars)
7894 int nchars;
7895 {
7896 if (nchars == 0)
7897 echo_area_buffer[0] = Qnil;
7898 /* A null message buffer means that the frame hasn't really been
7899 initialized yet. Error messages get reported properly by
7900 cmd_error, so this must be just an informative message; toss it. */
7901 else if (!noninteractive
7902 && INTERACTIVE
7903 && !NILP (echo_area_buffer[0]))
7904 {
7905 struct frame *sf = SELECTED_FRAME ();
7906 if (FRAME_MESSAGE_BUF (sf))
7907 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
7908 }
7909 }
7910
7911
7912 /* Helper function for truncate_echo_area. Truncate the current
7913 message to at most NCHARS characters. */
7914
7915 static int
7916 truncate_message_1 (nchars, a2, a3, a4)
7917 EMACS_INT nchars;
7918 Lisp_Object a2;
7919 EMACS_INT a3, a4;
7920 {
7921 if (BEG + nchars < Z)
7922 del_range (BEG + nchars, Z);
7923 if (Z == BEG)
7924 echo_area_buffer[0] = Qnil;
7925 return 0;
7926 }
7927
7928
7929 /* Set the current message to a substring of S or STRING.
7930
7931 If STRING is a Lisp string, set the message to the first NBYTES
7932 bytes from STRING. NBYTES zero means use the whole string. If
7933 STRING is multibyte, the message will be displayed multibyte.
7934
7935 If S is not null, set the message to the first LEN bytes of S. LEN
7936 zero means use the whole string. MULTIBYTE_P non-zero means S is
7937 multibyte. Display the message multibyte in that case. */
7938
7939 void
7940 set_message (s, string, nbytes, multibyte_p)
7941 const char *s;
7942 Lisp_Object string;
7943 int nbytes, multibyte_p;
7944 {
7945 message_enable_multibyte
7946 = ((s && multibyte_p)
7947 || (STRINGP (string) && STRING_MULTIBYTE (string)));
7948
7949 with_echo_area_buffer (0, 0, set_message_1,
7950 (EMACS_INT) s, string, nbytes, multibyte_p);
7951 message_buf_print = 0;
7952 help_echo_showing_p = 0;
7953 }
7954
7955
7956 /* Helper function for set_message. Arguments have the same meaning
7957 as there, with A1 corresponding to S and A2 corresponding to STRING
7958 This function is called with the echo area buffer being
7959 current. */
7960
7961 static int
7962 set_message_1 (a1, a2, nbytes, multibyte_p)
7963 EMACS_INT a1;
7964 Lisp_Object a2;
7965 EMACS_INT nbytes, multibyte_p;
7966 {
7967 const char *s = (const char *) a1;
7968 Lisp_Object string = a2;
7969
7970 /* Change multibyteness of the echo buffer appropriately. */
7971 if (message_enable_multibyte
7972 != !NILP (current_buffer->enable_multibyte_characters))
7973 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
7974
7975 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
7976
7977 /* Insert new message at BEG. */
7978 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
7979 Ferase_buffer ();
7980
7981 if (STRINGP (string))
7982 {
7983 int nchars;
7984
7985 if (nbytes == 0)
7986 nbytes = SBYTES (string);
7987 nchars = string_byte_to_char (string, nbytes);
7988
7989 /* This function takes care of single/multibyte conversion. We
7990 just have to ensure that the echo area buffer has the right
7991 setting of enable_multibyte_characters. */
7992 insert_from_string (string, 0, 0, nchars, nbytes, 1);
7993 }
7994 else if (s)
7995 {
7996 if (nbytes == 0)
7997 nbytes = strlen (s);
7998
7999 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8000 {
8001 /* Convert from multi-byte to single-byte. */
8002 int i, c, n;
8003 unsigned char work[1];
8004
8005 /* Convert a multibyte string to single-byte. */
8006 for (i = 0; i < nbytes; i += n)
8007 {
8008 c = string_char_and_length (s + i, nbytes - i, &n);
8009 work[0] = (SINGLE_BYTE_CHAR_P (c)
8010 ? c
8011 : multibyte_char_to_unibyte (c, Qnil));
8012 insert_1_both (work, 1, 1, 1, 0, 0);
8013 }
8014 }
8015 else if (!multibyte_p
8016 && !NILP (current_buffer->enable_multibyte_characters))
8017 {
8018 /* Convert from single-byte to multi-byte. */
8019 int i, c, n;
8020 const unsigned char *msg = (const unsigned char *) s;
8021 unsigned char str[MAX_MULTIBYTE_LENGTH];
8022
8023 /* Convert a single-byte string to multibyte. */
8024 for (i = 0; i < nbytes; i++)
8025 {
8026 c = unibyte_char_to_multibyte (msg[i]);
8027 n = CHAR_STRING (c, str);
8028 insert_1_both (str, 1, n, 1, 0, 0);
8029 }
8030 }
8031 else
8032 insert_1 (s, nbytes, 1, 0, 0);
8033 }
8034
8035 return 0;
8036 }
8037
8038
8039 /* Clear messages. CURRENT_P non-zero means clear the current
8040 message. LAST_DISPLAYED_P non-zero means clear the message
8041 last displayed. */
8042
8043 void
8044 clear_message (current_p, last_displayed_p)
8045 int current_p, last_displayed_p;
8046 {
8047 if (current_p)
8048 {
8049 echo_area_buffer[0] = Qnil;
8050 message_cleared_p = 1;
8051 }
8052
8053 if (last_displayed_p)
8054 echo_area_buffer[1] = Qnil;
8055
8056 message_buf_print = 0;
8057 }
8058
8059 /* Clear garbaged frames.
8060
8061 This function is used where the old redisplay called
8062 redraw_garbaged_frames which in turn called redraw_frame which in
8063 turn called clear_frame. The call to clear_frame was a source of
8064 flickering. I believe a clear_frame is not necessary. It should
8065 suffice in the new redisplay to invalidate all current matrices,
8066 and ensure a complete redisplay of all windows. */
8067
8068 static void
8069 clear_garbaged_frames ()
8070 {
8071 if (frame_garbaged)
8072 {
8073 Lisp_Object tail, frame;
8074 int changed_count = 0;
8075
8076 FOR_EACH_FRAME (tail, frame)
8077 {
8078 struct frame *f = XFRAME (frame);
8079
8080 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8081 {
8082 if (f->resized_p)
8083 {
8084 Fredraw_frame (frame);
8085 f->force_flush_display_p = 1;
8086 }
8087 clear_current_matrices (f);
8088 changed_count++;
8089 f->garbaged = 0;
8090 f->resized_p = 0;
8091 }
8092 }
8093
8094 frame_garbaged = 0;
8095 if (changed_count)
8096 ++windows_or_buffers_changed;
8097 }
8098 }
8099
8100
8101 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8102 is non-zero update selected_frame. Value is non-zero if the
8103 mini-windows height has been changed. */
8104
8105 static int
8106 echo_area_display (update_frame_p)
8107 int update_frame_p;
8108 {
8109 Lisp_Object mini_window;
8110 struct window *w;
8111 struct frame *f;
8112 int window_height_changed_p = 0;
8113 struct frame *sf = SELECTED_FRAME ();
8114
8115 mini_window = FRAME_MINIBUF_WINDOW (sf);
8116 w = XWINDOW (mini_window);
8117 f = XFRAME (WINDOW_FRAME (w));
8118
8119 /* Don't display if frame is invisible or not yet initialized. */
8120 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8121 return 0;
8122
8123 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8124 #ifndef MAC_OS8
8125 #ifdef HAVE_WINDOW_SYSTEM
8126 /* When Emacs starts, selected_frame may be a visible terminal
8127 frame, even if we run under a window system. If we let this
8128 through, a message would be displayed on the terminal. */
8129 if (EQ (selected_frame, Vterminal_frame)
8130 && !NILP (Vwindow_system))
8131 return 0;
8132 #endif /* HAVE_WINDOW_SYSTEM */
8133 #endif
8134
8135 /* Redraw garbaged frames. */
8136 if (frame_garbaged)
8137 clear_garbaged_frames ();
8138
8139 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8140 {
8141 echo_area_window = mini_window;
8142 window_height_changed_p = display_echo_area (w);
8143 w->must_be_updated_p = 1;
8144
8145 /* Update the display, unless called from redisplay_internal.
8146 Also don't update the screen during redisplay itself. The
8147 update will happen at the end of redisplay, and an update
8148 here could cause confusion. */
8149 if (update_frame_p && !redisplaying_p)
8150 {
8151 int n = 0;
8152
8153 /* If the display update has been interrupted by pending
8154 input, update mode lines in the frame. Due to the
8155 pending input, it might have been that redisplay hasn't
8156 been called, so that mode lines above the echo area are
8157 garbaged. This looks odd, so we prevent it here. */
8158 if (!display_completed)
8159 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8160
8161 if (window_height_changed_p
8162 /* Don't do this if Emacs is shutting down. Redisplay
8163 needs to run hooks. */
8164 && !NILP (Vrun_hooks))
8165 {
8166 /* Must update other windows. Likewise as in other
8167 cases, don't let this update be interrupted by
8168 pending input. */
8169 int count = SPECPDL_INDEX ();
8170 specbind (Qredisplay_dont_pause, Qt);
8171 windows_or_buffers_changed = 1;
8172 redisplay_internal (0);
8173 unbind_to (count, Qnil);
8174 }
8175 else if (FRAME_WINDOW_P (f) && n == 0)
8176 {
8177 /* Window configuration is the same as before.
8178 Can do with a display update of the echo area,
8179 unless we displayed some mode lines. */
8180 update_single_window (w, 1);
8181 rif->flush_display (f);
8182 }
8183 else
8184 update_frame (f, 1, 1);
8185
8186 /* If cursor is in the echo area, make sure that the next
8187 redisplay displays the minibuffer, so that the cursor will
8188 be replaced with what the minibuffer wants. */
8189 if (cursor_in_echo_area)
8190 ++windows_or_buffers_changed;
8191 }
8192 }
8193 else if (!EQ (mini_window, selected_window))
8194 windows_or_buffers_changed++;
8195
8196 /* The current message is now also the last one displayed. */
8197 echo_area_buffer[1] = echo_area_buffer[0];
8198
8199 /* Prevent redisplay optimization in redisplay_internal by resetting
8200 this_line_start_pos. This is done because the mini-buffer now
8201 displays the message instead of its buffer text. */
8202 if (EQ (mini_window, selected_window))
8203 CHARPOS (this_line_start_pos) = 0;
8204
8205 return window_height_changed_p;
8206 }
8207
8208
8209 \f
8210 /***********************************************************************
8211 Mode Lines and Frame Titles
8212 ***********************************************************************/
8213
8214 /* A buffer for constructing non-propertized mode-line strings and
8215 frame titles in it; allocated from the heap in init_xdisp and
8216 resized as needed in store_mode_line_noprop_char. */
8217
8218 static char *mode_line_noprop_buf;
8219
8220 /* The buffer's end, and a current output position in it. */
8221
8222 static char *mode_line_noprop_buf_end;
8223 static char *mode_line_noprop_ptr;
8224
8225 #define MODE_LINE_NOPROP_LEN(start) \
8226 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8227
8228 static enum {
8229 MODE_LINE_DISPLAY = 0,
8230 MODE_LINE_TITLE,
8231 MODE_LINE_NOPROP,
8232 MODE_LINE_STRING
8233 } mode_line_target;
8234
8235 /* Alist that caches the results of :propertize.
8236 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8237 static Lisp_Object mode_line_proptrans_alist;
8238
8239 /* List of strings making up the mode-line. */
8240 static Lisp_Object mode_line_string_list;
8241
8242 /* Base face property when building propertized mode line string. */
8243 static Lisp_Object mode_line_string_face;
8244 static Lisp_Object mode_line_string_face_prop;
8245
8246
8247 /* Unwind data for mode line strings */
8248
8249 static Lisp_Object Vmode_line_unwind_vector;
8250
8251 static Lisp_Object
8252 format_mode_line_unwind_data (obuf)
8253 struct buffer *obuf;
8254 {
8255 Lisp_Object vector;
8256
8257 /* Reduce consing by keeping one vector in
8258 Vwith_echo_area_save_vector. */
8259 vector = Vmode_line_unwind_vector;
8260 Vmode_line_unwind_vector = Qnil;
8261
8262 if (NILP (vector))
8263 vector = Fmake_vector (make_number (7), Qnil);
8264
8265 AREF (vector, 0) = make_number (mode_line_target);
8266 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8267 AREF (vector, 2) = mode_line_string_list;
8268 AREF (vector, 3) = mode_line_proptrans_alist;
8269 AREF (vector, 4) = mode_line_string_face;
8270 AREF (vector, 5) = mode_line_string_face_prop;
8271
8272 if (obuf)
8273 XSETBUFFER (AREF (vector, 6), obuf);
8274 else
8275 AREF (vector, 6) = Qnil;
8276
8277 return vector;
8278 }
8279
8280 static Lisp_Object
8281 unwind_format_mode_line (vector)
8282 Lisp_Object vector;
8283 {
8284 mode_line_target = XINT (AREF (vector, 0));
8285 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8286 mode_line_string_list = AREF (vector, 2);
8287 mode_line_proptrans_alist = AREF (vector, 3);
8288 mode_line_string_face = AREF (vector, 4);
8289 mode_line_string_face_prop = AREF (vector, 5);
8290
8291 if (!NILP (AREF (vector, 6)))
8292 {
8293 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8294 AREF (vector, 6) = Qnil;
8295 }
8296
8297 Vmode_line_unwind_vector = vector;
8298 return Qnil;
8299 }
8300
8301
8302 /* Store a single character C for the frame title in mode_line_noprop_buf.
8303 Re-allocate mode_line_noprop_buf if necessary. */
8304
8305 static void
8306 #ifdef PROTOTYPES
8307 store_mode_line_noprop_char (char c)
8308 #else
8309 store_mode_line_noprop_char (c)
8310 char c;
8311 #endif
8312 {
8313 /* If output position has reached the end of the allocated buffer,
8314 double the buffer's size. */
8315 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8316 {
8317 int len = MODE_LINE_NOPROP_LEN (0);
8318 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8319 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8320 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8321 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8322 }
8323
8324 *mode_line_noprop_ptr++ = c;
8325 }
8326
8327
8328 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8329 mode_line_noprop_ptr. STR is the string to store. Do not copy
8330 characters that yield more columns than PRECISION; PRECISION <= 0
8331 means copy the whole string. Pad with spaces until FIELD_WIDTH
8332 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8333 pad. Called from display_mode_element when it is used to build a
8334 frame title. */
8335
8336 static int
8337 store_mode_line_noprop (str, field_width, precision)
8338 const unsigned char *str;
8339 int field_width, precision;
8340 {
8341 int n = 0;
8342 int dummy, nbytes;
8343
8344 /* Copy at most PRECISION chars from STR. */
8345 nbytes = strlen (str);
8346 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8347 while (nbytes--)
8348 store_mode_line_noprop_char (*str++);
8349
8350 /* Fill up with spaces until FIELD_WIDTH reached. */
8351 while (field_width > 0
8352 && n < field_width)
8353 {
8354 store_mode_line_noprop_char (' ');
8355 ++n;
8356 }
8357
8358 return n;
8359 }
8360
8361 /***********************************************************************
8362 Frame Titles
8363 ***********************************************************************/
8364
8365 #ifdef HAVE_WINDOW_SYSTEM
8366
8367 /* Set the title of FRAME, if it has changed. The title format is
8368 Vicon_title_format if FRAME is iconified, otherwise it is
8369 frame_title_format. */
8370
8371 static void
8372 x_consider_frame_title (frame)
8373 Lisp_Object frame;
8374 {
8375 struct frame *f = XFRAME (frame);
8376
8377 if (FRAME_WINDOW_P (f)
8378 || FRAME_MINIBUF_ONLY_P (f)
8379 || f->explicit_name)
8380 {
8381 /* Do we have more than one visible frame on this X display? */
8382 Lisp_Object tail;
8383 Lisp_Object fmt;
8384 int title_start;
8385 char *title;
8386 int len;
8387 struct it it;
8388 int count = SPECPDL_INDEX ();
8389
8390 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8391 {
8392 Lisp_Object other_frame = XCAR (tail);
8393 struct frame *tf = XFRAME (other_frame);
8394
8395 if (tf != f
8396 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8397 && !FRAME_MINIBUF_ONLY_P (tf)
8398 && !EQ (other_frame, tip_frame)
8399 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8400 break;
8401 }
8402
8403 /* Set global variable indicating that multiple frames exist. */
8404 multiple_frames = CONSP (tail);
8405
8406 /* Switch to the buffer of selected window of the frame. Set up
8407 mode_line_target so that display_mode_element will output into
8408 mode_line_noprop_buf; then display the title. */
8409 record_unwind_protect (unwind_format_mode_line,
8410 format_mode_line_unwind_data (current_buffer));
8411
8412 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8413 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8414
8415 mode_line_target = MODE_LINE_TITLE;
8416 title_start = MODE_LINE_NOPROP_LEN (0);
8417 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8418 NULL, DEFAULT_FACE_ID);
8419 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8420 len = MODE_LINE_NOPROP_LEN (title_start);
8421 title = mode_line_noprop_buf + title_start;
8422 unbind_to (count, Qnil);
8423
8424 /* Set the title only if it's changed. This avoids consing in
8425 the common case where it hasn't. (If it turns out that we've
8426 already wasted too much time by walking through the list with
8427 display_mode_element, then we might need to optimize at a
8428 higher level than this.) */
8429 if (! STRINGP (f->name)
8430 || SBYTES (f->name) != len
8431 || bcmp (title, SDATA (f->name), len) != 0)
8432 x_implicitly_set_name (f, make_string (title, len), Qnil);
8433 }
8434 }
8435
8436 #endif /* not HAVE_WINDOW_SYSTEM */
8437
8438
8439
8440 \f
8441 /***********************************************************************
8442 Menu Bars
8443 ***********************************************************************/
8444
8445
8446 /* Prepare for redisplay by updating menu-bar item lists when
8447 appropriate. This can call eval. */
8448
8449 void
8450 prepare_menu_bars ()
8451 {
8452 int all_windows;
8453 struct gcpro gcpro1, gcpro2;
8454 struct frame *f;
8455 Lisp_Object tooltip_frame;
8456
8457 #ifdef HAVE_WINDOW_SYSTEM
8458 tooltip_frame = tip_frame;
8459 #else
8460 tooltip_frame = Qnil;
8461 #endif
8462
8463 /* Update all frame titles based on their buffer names, etc. We do
8464 this before the menu bars so that the buffer-menu will show the
8465 up-to-date frame titles. */
8466 #ifdef HAVE_WINDOW_SYSTEM
8467 if (windows_or_buffers_changed || update_mode_lines)
8468 {
8469 Lisp_Object tail, frame;
8470
8471 FOR_EACH_FRAME (tail, frame)
8472 {
8473 f = XFRAME (frame);
8474 if (!EQ (frame, tooltip_frame)
8475 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
8476 x_consider_frame_title (frame);
8477 }
8478 }
8479 #endif /* HAVE_WINDOW_SYSTEM */
8480
8481 /* Update the menu bar item lists, if appropriate. This has to be
8482 done before any actual redisplay or generation of display lines. */
8483 all_windows = (update_mode_lines
8484 || buffer_shared > 1
8485 || windows_or_buffers_changed);
8486 if (all_windows)
8487 {
8488 Lisp_Object tail, frame;
8489 int count = SPECPDL_INDEX ();
8490
8491 record_unwind_save_match_data ();
8492
8493 FOR_EACH_FRAME (tail, frame)
8494 {
8495 f = XFRAME (frame);
8496
8497 /* Ignore tooltip frame. */
8498 if (EQ (frame, tooltip_frame))
8499 continue;
8500
8501 /* If a window on this frame changed size, report that to
8502 the user and clear the size-change flag. */
8503 if (FRAME_WINDOW_SIZES_CHANGED (f))
8504 {
8505 Lisp_Object functions;
8506
8507 /* Clear flag first in case we get an error below. */
8508 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
8509 functions = Vwindow_size_change_functions;
8510 GCPRO2 (tail, functions);
8511
8512 while (CONSP (functions))
8513 {
8514 call1 (XCAR (functions), frame);
8515 functions = XCDR (functions);
8516 }
8517 UNGCPRO;
8518 }
8519
8520 GCPRO1 (tail);
8521 update_menu_bar (f, 0);
8522 #ifdef HAVE_WINDOW_SYSTEM
8523 update_tool_bar (f, 0);
8524 #endif
8525 UNGCPRO;
8526 }
8527
8528 unbind_to (count, Qnil);
8529 }
8530 else
8531 {
8532 struct frame *sf = SELECTED_FRAME ();
8533 update_menu_bar (sf, 1);
8534 #ifdef HAVE_WINDOW_SYSTEM
8535 update_tool_bar (sf, 1);
8536 #endif
8537 }
8538
8539 /* Motif needs this. See comment in xmenu.c. Turn it off when
8540 pending_menu_activation is not defined. */
8541 #ifdef USE_X_TOOLKIT
8542 pending_menu_activation = 0;
8543 #endif
8544 }
8545
8546
8547 /* Update the menu bar item list for frame F. This has to be done
8548 before we start to fill in any display lines, because it can call
8549 eval.
8550
8551 If SAVE_MATCH_DATA is non-zero, we must save and restore it here. */
8552
8553 static void
8554 update_menu_bar (f, save_match_data)
8555 struct frame *f;
8556 int save_match_data;
8557 {
8558 Lisp_Object window;
8559 register struct window *w;
8560
8561 /* If called recursively during a menu update, do nothing. This can
8562 happen when, for instance, an activate-menubar-hook causes a
8563 redisplay. */
8564 if (inhibit_menubar_update)
8565 return;
8566
8567 window = FRAME_SELECTED_WINDOW (f);
8568 w = XWINDOW (window);
8569
8570 #if 0 /* The if statement below this if statement used to include the
8571 condition !NILP (w->update_mode_line), rather than using
8572 update_mode_lines directly, and this if statement may have
8573 been added to make that condition work. Now the if
8574 statement below matches its comment, this isn't needed. */
8575 if (update_mode_lines)
8576 w->update_mode_line = Qt;
8577 #endif
8578
8579 if (FRAME_WINDOW_P (f)
8580 ?
8581 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8582 || defined (USE_GTK)
8583 FRAME_EXTERNAL_MENU_BAR (f)
8584 #else
8585 FRAME_MENU_BAR_LINES (f) > 0
8586 #endif
8587 : FRAME_MENU_BAR_LINES (f) > 0)
8588 {
8589 /* If the user has switched buffers or windows, we need to
8590 recompute to reflect the new bindings. But we'll
8591 recompute when update_mode_lines is set too; that means
8592 that people can use force-mode-line-update to request
8593 that the menu bar be recomputed. The adverse effect on
8594 the rest of the redisplay algorithm is about the same as
8595 windows_or_buffers_changed anyway. */
8596 if (windows_or_buffers_changed
8597 /* This used to test w->update_mode_line, but we believe
8598 there is no need to recompute the menu in that case. */
8599 || update_mode_lines
8600 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8601 < BUF_MODIFF (XBUFFER (w->buffer)))
8602 != !NILP (w->last_had_star))
8603 || ((!NILP (Vtransient_mark_mode)
8604 && !NILP (XBUFFER (w->buffer)->mark_active))
8605 != !NILP (w->region_showing)))
8606 {
8607 struct buffer *prev = current_buffer;
8608 int count = SPECPDL_INDEX ();
8609
8610 specbind (Qinhibit_menubar_update, Qt);
8611
8612 set_buffer_internal_1 (XBUFFER (w->buffer));
8613 if (save_match_data)
8614 record_unwind_save_match_data ();
8615 if (NILP (Voverriding_local_map_menu_flag))
8616 {
8617 specbind (Qoverriding_terminal_local_map, Qnil);
8618 specbind (Qoverriding_local_map, Qnil);
8619 }
8620
8621 /* Run the Lucid hook. */
8622 safe_run_hooks (Qactivate_menubar_hook);
8623
8624 /* If it has changed current-menubar from previous value,
8625 really recompute the menu-bar from the value. */
8626 if (! NILP (Vlucid_menu_bar_dirty_flag))
8627 call0 (Qrecompute_lucid_menubar);
8628
8629 safe_run_hooks (Qmenu_bar_update_hook);
8630 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
8631
8632 /* Redisplay the menu bar in case we changed it. */
8633 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
8634 || defined (USE_GTK)
8635 if (FRAME_WINDOW_P (f)
8636 #if defined (MAC_OS)
8637 /* All frames on Mac OS share the same menubar. So only the
8638 selected frame should be allowed to set it. */
8639 && f == SELECTED_FRAME ()
8640 #endif
8641 )
8642 set_frame_menubar (f, 0, 0);
8643 else
8644 /* On a terminal screen, the menu bar is an ordinary screen
8645 line, and this makes it get updated. */
8646 w->update_mode_line = Qt;
8647 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8648 /* In the non-toolkit version, the menu bar is an ordinary screen
8649 line, and this makes it get updated. */
8650 w->update_mode_line = Qt;
8651 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
8652
8653 unbind_to (count, Qnil);
8654 set_buffer_internal_1 (prev);
8655 }
8656 }
8657 }
8658
8659
8660 \f
8661 /***********************************************************************
8662 Output Cursor
8663 ***********************************************************************/
8664
8665 #ifdef HAVE_WINDOW_SYSTEM
8666
8667 /* EXPORT:
8668 Nominal cursor position -- where to draw output.
8669 HPOS and VPOS are window relative glyph matrix coordinates.
8670 X and Y are window relative pixel coordinates. */
8671
8672 struct cursor_pos output_cursor;
8673
8674
8675 /* EXPORT:
8676 Set the global variable output_cursor to CURSOR. All cursor
8677 positions are relative to updated_window. */
8678
8679 void
8680 set_output_cursor (cursor)
8681 struct cursor_pos *cursor;
8682 {
8683 output_cursor.hpos = cursor->hpos;
8684 output_cursor.vpos = cursor->vpos;
8685 output_cursor.x = cursor->x;
8686 output_cursor.y = cursor->y;
8687 }
8688
8689
8690 /* EXPORT for RIF:
8691 Set a nominal cursor position.
8692
8693 HPOS and VPOS are column/row positions in a window glyph matrix. X
8694 and Y are window text area relative pixel positions.
8695
8696 If this is done during an update, updated_window will contain the
8697 window that is being updated and the position is the future output
8698 cursor position for that window. If updated_window is null, use
8699 selected_window and display the cursor at the given position. */
8700
8701 void
8702 x_cursor_to (vpos, hpos, y, x)
8703 int vpos, hpos, y, x;
8704 {
8705 struct window *w;
8706
8707 /* If updated_window is not set, work on selected_window. */
8708 if (updated_window)
8709 w = updated_window;
8710 else
8711 w = XWINDOW (selected_window);
8712
8713 /* Set the output cursor. */
8714 output_cursor.hpos = hpos;
8715 output_cursor.vpos = vpos;
8716 output_cursor.x = x;
8717 output_cursor.y = y;
8718
8719 /* If not called as part of an update, really display the cursor.
8720 This will also set the cursor position of W. */
8721 if (updated_window == NULL)
8722 {
8723 BLOCK_INPUT;
8724 display_and_set_cursor (w, 1, hpos, vpos, x, y);
8725 if (rif->flush_display_optional)
8726 rif->flush_display_optional (SELECTED_FRAME ());
8727 UNBLOCK_INPUT;
8728 }
8729 }
8730
8731 #endif /* HAVE_WINDOW_SYSTEM */
8732
8733 \f
8734 /***********************************************************************
8735 Tool-bars
8736 ***********************************************************************/
8737
8738 #ifdef HAVE_WINDOW_SYSTEM
8739
8740 /* Where the mouse was last time we reported a mouse event. */
8741
8742 FRAME_PTR last_mouse_frame;
8743
8744 /* Tool-bar item index of the item on which a mouse button was pressed
8745 or -1. */
8746
8747 int last_tool_bar_item;
8748
8749
8750 /* Update the tool-bar item list for frame F. This has to be done
8751 before we start to fill in any display lines. Called from
8752 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
8753 and restore it here. */
8754
8755 static void
8756 update_tool_bar (f, save_match_data)
8757 struct frame *f;
8758 int save_match_data;
8759 {
8760 #ifdef USE_GTK
8761 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
8762 #else
8763 int do_update = WINDOWP (f->tool_bar_window)
8764 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
8765 #endif
8766
8767 if (do_update)
8768 {
8769 Lisp_Object window;
8770 struct window *w;
8771
8772 window = FRAME_SELECTED_WINDOW (f);
8773 w = XWINDOW (window);
8774
8775 /* If the user has switched buffers or windows, we need to
8776 recompute to reflect the new bindings. But we'll
8777 recompute when update_mode_lines is set too; that means
8778 that people can use force-mode-line-update to request
8779 that the menu bar be recomputed. The adverse effect on
8780 the rest of the redisplay algorithm is about the same as
8781 windows_or_buffers_changed anyway. */
8782 if (windows_or_buffers_changed
8783 || !NILP (w->update_mode_line)
8784 || update_mode_lines
8785 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
8786 < BUF_MODIFF (XBUFFER (w->buffer)))
8787 != !NILP (w->last_had_star))
8788 || ((!NILP (Vtransient_mark_mode)
8789 && !NILP (XBUFFER (w->buffer)->mark_active))
8790 != !NILP (w->region_showing)))
8791 {
8792 struct buffer *prev = current_buffer;
8793 int count = SPECPDL_INDEX ();
8794 Lisp_Object new_tool_bar;
8795 int new_n_tool_bar;
8796 struct gcpro gcpro1;
8797
8798 /* Set current_buffer to the buffer of the selected
8799 window of the frame, so that we get the right local
8800 keymaps. */
8801 set_buffer_internal_1 (XBUFFER (w->buffer));
8802
8803 /* Save match data, if we must. */
8804 if (save_match_data)
8805 record_unwind_save_match_data ();
8806
8807 /* Make sure that we don't accidentally use bogus keymaps. */
8808 if (NILP (Voverriding_local_map_menu_flag))
8809 {
8810 specbind (Qoverriding_terminal_local_map, Qnil);
8811 specbind (Qoverriding_local_map, Qnil);
8812 }
8813
8814 GCPRO1 (new_tool_bar);
8815
8816 /* Build desired tool-bar items from keymaps. */
8817 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
8818 &new_n_tool_bar);
8819
8820 /* Redisplay the tool-bar if we changed it. */
8821 if (NILP (Fequal (new_tool_bar, f->tool_bar_items)))
8822 {
8823 /* Redisplay that happens asynchronously due to an expose event
8824 may access f->tool_bar_items. Make sure we update both
8825 variables within BLOCK_INPUT so no such event interrupts. */
8826 BLOCK_INPUT;
8827 f->tool_bar_items = new_tool_bar;
8828 f->n_tool_bar_items = new_n_tool_bar;
8829 w->update_mode_line = Qt;
8830 UNBLOCK_INPUT;
8831 }
8832
8833 UNGCPRO;
8834
8835 unbind_to (count, Qnil);
8836 set_buffer_internal_1 (prev);
8837 }
8838 }
8839 }
8840
8841
8842 /* Set F->desired_tool_bar_string to a Lisp string representing frame
8843 F's desired tool-bar contents. F->tool_bar_items must have
8844 been set up previously by calling prepare_menu_bars. */
8845
8846 static void
8847 build_desired_tool_bar_string (f)
8848 struct frame *f;
8849 {
8850 int i, size, size_needed;
8851 struct gcpro gcpro1, gcpro2, gcpro3;
8852 Lisp_Object image, plist, props;
8853
8854 image = plist = props = Qnil;
8855 GCPRO3 (image, plist, props);
8856
8857 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
8858 Otherwise, make a new string. */
8859
8860 /* The size of the string we might be able to reuse. */
8861 size = (STRINGP (f->desired_tool_bar_string)
8862 ? SCHARS (f->desired_tool_bar_string)
8863 : 0);
8864
8865 /* We need one space in the string for each image. */
8866 size_needed = f->n_tool_bar_items;
8867
8868 /* Reuse f->desired_tool_bar_string, if possible. */
8869 if (size < size_needed || NILP (f->desired_tool_bar_string))
8870 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
8871 make_number (' '));
8872 else
8873 {
8874 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
8875 Fremove_text_properties (make_number (0), make_number (size),
8876 props, f->desired_tool_bar_string);
8877 }
8878
8879 /* Put a `display' property on the string for the images to display,
8880 put a `menu_item' property on tool-bar items with a value that
8881 is the index of the item in F's tool-bar item vector. */
8882 for (i = 0; i < f->n_tool_bar_items; ++i)
8883 {
8884 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
8885
8886 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
8887 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
8888 int hmargin, vmargin, relief, idx, end;
8889 extern Lisp_Object QCrelief, QCmargin, QCconversion;
8890
8891 /* If image is a vector, choose the image according to the
8892 button state. */
8893 image = PROP (TOOL_BAR_ITEM_IMAGES);
8894 if (VECTORP (image))
8895 {
8896 if (enabled_p)
8897 idx = (selected_p
8898 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
8899 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
8900 else
8901 idx = (selected_p
8902 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
8903 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
8904
8905 xassert (ASIZE (image) >= idx);
8906 image = AREF (image, idx);
8907 }
8908 else
8909 idx = -1;
8910
8911 /* Ignore invalid image specifications. */
8912 if (!valid_image_p (image))
8913 continue;
8914
8915 /* Display the tool-bar button pressed, or depressed. */
8916 plist = Fcopy_sequence (XCDR (image));
8917
8918 /* Compute margin and relief to draw. */
8919 relief = (tool_bar_button_relief >= 0
8920 ? tool_bar_button_relief
8921 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
8922 hmargin = vmargin = relief;
8923
8924 if (INTEGERP (Vtool_bar_button_margin)
8925 && XINT (Vtool_bar_button_margin) > 0)
8926 {
8927 hmargin += XFASTINT (Vtool_bar_button_margin);
8928 vmargin += XFASTINT (Vtool_bar_button_margin);
8929 }
8930 else if (CONSP (Vtool_bar_button_margin))
8931 {
8932 if (INTEGERP (XCAR (Vtool_bar_button_margin))
8933 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
8934 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
8935
8936 if (INTEGERP (XCDR (Vtool_bar_button_margin))
8937 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
8938 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
8939 }
8940
8941 if (auto_raise_tool_bar_buttons_p)
8942 {
8943 /* Add a `:relief' property to the image spec if the item is
8944 selected. */
8945 if (selected_p)
8946 {
8947 plist = Fplist_put (plist, QCrelief, make_number (-relief));
8948 hmargin -= relief;
8949 vmargin -= relief;
8950 }
8951 }
8952 else
8953 {
8954 /* If image is selected, display it pressed, i.e. with a
8955 negative relief. If it's not selected, display it with a
8956 raised relief. */
8957 plist = Fplist_put (plist, QCrelief,
8958 (selected_p
8959 ? make_number (-relief)
8960 : make_number (relief)));
8961 hmargin -= relief;
8962 vmargin -= relief;
8963 }
8964
8965 /* Put a margin around the image. */
8966 if (hmargin || vmargin)
8967 {
8968 if (hmargin == vmargin)
8969 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
8970 else
8971 plist = Fplist_put (plist, QCmargin,
8972 Fcons (make_number (hmargin),
8973 make_number (vmargin)));
8974 }
8975
8976 /* If button is not enabled, and we don't have special images
8977 for the disabled state, make the image appear disabled by
8978 applying an appropriate algorithm to it. */
8979 if (!enabled_p && idx < 0)
8980 plist = Fplist_put (plist, QCconversion, Qdisabled);
8981
8982 /* Put a `display' text property on the string for the image to
8983 display. Put a `menu-item' property on the string that gives
8984 the start of this item's properties in the tool-bar items
8985 vector. */
8986 image = Fcons (Qimage, plist);
8987 props = list4 (Qdisplay, image,
8988 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
8989
8990 /* Let the last image hide all remaining spaces in the tool bar
8991 string. The string can be longer than needed when we reuse a
8992 previous string. */
8993 if (i + 1 == f->n_tool_bar_items)
8994 end = SCHARS (f->desired_tool_bar_string);
8995 else
8996 end = i + 1;
8997 Fadd_text_properties (make_number (i), make_number (end),
8998 props, f->desired_tool_bar_string);
8999 #undef PROP
9000 }
9001
9002 UNGCPRO;
9003 }
9004
9005
9006 /* Display one line of the tool-bar of frame IT->f. */
9007
9008 static void
9009 display_tool_bar_line (it)
9010 struct it *it;
9011 {
9012 struct glyph_row *row = it->glyph_row;
9013 int max_x = it->last_visible_x;
9014 struct glyph *last;
9015
9016 prepare_desired_row (row);
9017 row->y = it->current_y;
9018
9019 /* Note that this isn't made use of if the face hasn't a box,
9020 so there's no need to check the face here. */
9021 it->start_of_box_run_p = 1;
9022
9023 while (it->current_x < max_x)
9024 {
9025 int x_before, x, n_glyphs_before, i, nglyphs;
9026
9027 /* Get the next display element. */
9028 if (!get_next_display_element (it))
9029 break;
9030
9031 /* Produce glyphs. */
9032 x_before = it->current_x;
9033 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
9034 PRODUCE_GLYPHS (it);
9035
9036 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
9037 i = 0;
9038 x = x_before;
9039 while (i < nglyphs)
9040 {
9041 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9042
9043 if (x + glyph->pixel_width > max_x)
9044 {
9045 /* Glyph doesn't fit on line. */
9046 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
9047 it->current_x = x;
9048 goto out;
9049 }
9050
9051 ++it->hpos;
9052 x += glyph->pixel_width;
9053 ++i;
9054 }
9055
9056 /* Stop at line ends. */
9057 if (ITERATOR_AT_END_OF_LINE_P (it))
9058 break;
9059
9060 set_iterator_to_next (it, 1);
9061 }
9062
9063 out:;
9064
9065 row->displays_text_p = row->used[TEXT_AREA] != 0;
9066 extend_face_to_end_of_line (it);
9067 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9068 last->right_box_line_p = 1;
9069 if (last == row->glyphs[TEXT_AREA])
9070 last->left_box_line_p = 1;
9071 compute_line_metrics (it);
9072
9073 /* If line is empty, make it occupy the rest of the tool-bar. */
9074 if (!row->displays_text_p)
9075 {
9076 row->height = row->phys_height = it->last_visible_y - row->y;
9077 row->ascent = row->phys_ascent = 0;
9078 row->extra_line_spacing = 0;
9079 }
9080
9081 row->full_width_p = 1;
9082 row->continued_p = 0;
9083 row->truncated_on_left_p = 0;
9084 row->truncated_on_right_p = 0;
9085
9086 it->current_x = it->hpos = 0;
9087 it->current_y += row->height;
9088 ++it->vpos;
9089 ++it->glyph_row;
9090 }
9091
9092
9093 /* Value is the number of screen lines needed to make all tool-bar
9094 items of frame F visible. */
9095
9096 static int
9097 tool_bar_lines_needed (f)
9098 struct frame *f;
9099 {
9100 struct window *w = XWINDOW (f->tool_bar_window);
9101 struct it it;
9102
9103 /* Initialize an iterator for iteration over
9104 F->desired_tool_bar_string in the tool-bar window of frame F. */
9105 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9106 it.first_visible_x = 0;
9107 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9108 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9109
9110 while (!ITERATOR_AT_END_P (&it))
9111 {
9112 it.glyph_row = w->desired_matrix->rows;
9113 clear_glyph_row (it.glyph_row);
9114 display_tool_bar_line (&it);
9115 }
9116
9117 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9118 }
9119
9120
9121 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9122 0, 1, 0,
9123 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9124 (frame)
9125 Lisp_Object frame;
9126 {
9127 struct frame *f;
9128 struct window *w;
9129 int nlines = 0;
9130
9131 if (NILP (frame))
9132 frame = selected_frame;
9133 else
9134 CHECK_FRAME (frame);
9135 f = XFRAME (frame);
9136
9137 if (WINDOWP (f->tool_bar_window)
9138 || (w = XWINDOW (f->tool_bar_window),
9139 WINDOW_TOTAL_LINES (w) > 0))
9140 {
9141 update_tool_bar (f, 1);
9142 if (f->n_tool_bar_items)
9143 {
9144 build_desired_tool_bar_string (f);
9145 nlines = tool_bar_lines_needed (f);
9146 }
9147 }
9148
9149 return make_number (nlines);
9150 }
9151
9152
9153 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9154 height should be changed. */
9155
9156 static int
9157 redisplay_tool_bar (f)
9158 struct frame *f;
9159 {
9160 struct window *w;
9161 struct it it;
9162 struct glyph_row *row;
9163 int change_height_p = 0;
9164
9165 #ifdef USE_GTK
9166 if (FRAME_EXTERNAL_TOOL_BAR (f))
9167 update_frame_tool_bar (f);
9168 return 0;
9169 #endif
9170
9171 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9172 do anything. This means you must start with tool-bar-lines
9173 non-zero to get the auto-sizing effect. Or in other words, you
9174 can turn off tool-bars by specifying tool-bar-lines zero. */
9175 if (!WINDOWP (f->tool_bar_window)
9176 || (w = XWINDOW (f->tool_bar_window),
9177 WINDOW_TOTAL_LINES (w) == 0))
9178 return 0;
9179
9180 /* Set up an iterator for the tool-bar window. */
9181 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9182 it.first_visible_x = 0;
9183 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9184 row = it.glyph_row;
9185
9186 /* Build a string that represents the contents of the tool-bar. */
9187 build_desired_tool_bar_string (f);
9188 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9189
9190 /* Display as many lines as needed to display all tool-bar items. */
9191 while (it.current_y < it.last_visible_y)
9192 display_tool_bar_line (&it);
9193
9194 /* It doesn't make much sense to try scrolling in the tool-bar
9195 window, so don't do it. */
9196 w->desired_matrix->no_scrolling_p = 1;
9197 w->must_be_updated_p = 1;
9198
9199 if (auto_resize_tool_bars_p)
9200 {
9201 int nlines;
9202
9203 /* If we couldn't display everything, change the tool-bar's
9204 height. */
9205 if (IT_STRING_CHARPOS (it) < it.end_charpos)
9206 change_height_p = 1;
9207
9208 /* If there are blank lines at the end, except for a partially
9209 visible blank line at the end that is smaller than
9210 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9211 row = it.glyph_row - 1;
9212 if (!row->displays_text_p
9213 && row->height >= FRAME_LINE_HEIGHT (f))
9214 change_height_p = 1;
9215
9216 /* If row displays tool-bar items, but is partially visible,
9217 change the tool-bar's height. */
9218 if (row->displays_text_p
9219 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
9220 change_height_p = 1;
9221
9222 /* Resize windows as needed by changing the `tool-bar-lines'
9223 frame parameter. */
9224 if (change_height_p
9225 && (nlines = tool_bar_lines_needed (f),
9226 nlines != WINDOW_TOTAL_LINES (w)))
9227 {
9228 extern Lisp_Object Qtool_bar_lines;
9229 Lisp_Object frame;
9230 int old_height = WINDOW_TOTAL_LINES (w);
9231
9232 XSETFRAME (frame, f);
9233 clear_glyph_matrix (w->desired_matrix);
9234 Fmodify_frame_parameters (frame,
9235 Fcons (Fcons (Qtool_bar_lines,
9236 make_number (nlines)),
9237 Qnil));
9238 if (WINDOW_TOTAL_LINES (w) != old_height)
9239 fonts_changed_p = 1;
9240 }
9241 }
9242
9243 return change_height_p;
9244 }
9245
9246
9247 /* Get information about the tool-bar item which is displayed in GLYPH
9248 on frame F. Return in *PROP_IDX the index where tool-bar item
9249 properties start in F->tool_bar_items. Value is zero if
9250 GLYPH doesn't display a tool-bar item. */
9251
9252 static int
9253 tool_bar_item_info (f, glyph, prop_idx)
9254 struct frame *f;
9255 struct glyph *glyph;
9256 int *prop_idx;
9257 {
9258 Lisp_Object prop;
9259 int success_p;
9260 int charpos;
9261
9262 /* This function can be called asynchronously, which means we must
9263 exclude any possibility that Fget_text_property signals an
9264 error. */
9265 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9266 charpos = max (0, charpos);
9267
9268 /* Get the text property `menu-item' at pos. The value of that
9269 property is the start index of this item's properties in
9270 F->tool_bar_items. */
9271 prop = Fget_text_property (make_number (charpos),
9272 Qmenu_item, f->current_tool_bar_string);
9273 if (INTEGERP (prop))
9274 {
9275 *prop_idx = XINT (prop);
9276 success_p = 1;
9277 }
9278 else
9279 success_p = 0;
9280
9281 return success_p;
9282 }
9283
9284 \f
9285 /* Get information about the tool-bar item at position X/Y on frame F.
9286 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9287 the current matrix of the tool-bar window of F, or NULL if not
9288 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9289 item in F->tool_bar_items. Value is
9290
9291 -1 if X/Y is not on a tool-bar item
9292 0 if X/Y is on the same item that was highlighted before.
9293 1 otherwise. */
9294
9295 static int
9296 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9297 struct frame *f;
9298 int x, y;
9299 struct glyph **glyph;
9300 int *hpos, *vpos, *prop_idx;
9301 {
9302 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9303 struct window *w = XWINDOW (f->tool_bar_window);
9304 int area;
9305
9306 /* Find the glyph under X/Y. */
9307 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
9308 if (*glyph == NULL)
9309 return -1;
9310
9311 /* Get the start of this tool-bar item's properties in
9312 f->tool_bar_items. */
9313 if (!tool_bar_item_info (f, *glyph, prop_idx))
9314 return -1;
9315
9316 /* Is mouse on the highlighted item? */
9317 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
9318 && *vpos >= dpyinfo->mouse_face_beg_row
9319 && *vpos <= dpyinfo->mouse_face_end_row
9320 && (*vpos > dpyinfo->mouse_face_beg_row
9321 || *hpos >= dpyinfo->mouse_face_beg_col)
9322 && (*vpos < dpyinfo->mouse_face_end_row
9323 || *hpos < dpyinfo->mouse_face_end_col
9324 || dpyinfo->mouse_face_past_end))
9325 return 0;
9326
9327 return 1;
9328 }
9329
9330
9331 /* EXPORT:
9332 Handle mouse button event on the tool-bar of frame F, at
9333 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
9334 0 for button release. MODIFIERS is event modifiers for button
9335 release. */
9336
9337 void
9338 handle_tool_bar_click (f, x, y, down_p, modifiers)
9339 struct frame *f;
9340 int x, y, down_p;
9341 unsigned int modifiers;
9342 {
9343 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9344 struct window *w = XWINDOW (f->tool_bar_window);
9345 int hpos, vpos, prop_idx;
9346 struct glyph *glyph;
9347 Lisp_Object enabled_p;
9348
9349 /* If not on the highlighted tool-bar item, return. */
9350 frame_to_window_pixel_xy (w, &x, &y);
9351 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
9352 return;
9353
9354 /* If item is disabled, do nothing. */
9355 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9356 if (NILP (enabled_p))
9357 return;
9358
9359 if (down_p)
9360 {
9361 /* Show item in pressed state. */
9362 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
9363 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
9364 last_tool_bar_item = prop_idx;
9365 }
9366 else
9367 {
9368 Lisp_Object key, frame;
9369 struct input_event event;
9370 EVENT_INIT (event);
9371
9372 /* Show item in released state. */
9373 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
9374 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
9375
9376 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
9377
9378 XSETFRAME (frame, f);
9379 event.kind = TOOL_BAR_EVENT;
9380 event.frame_or_window = frame;
9381 event.arg = frame;
9382 kbd_buffer_store_event (&event);
9383
9384 event.kind = TOOL_BAR_EVENT;
9385 event.frame_or_window = frame;
9386 event.arg = key;
9387 event.modifiers = modifiers;
9388 kbd_buffer_store_event (&event);
9389 last_tool_bar_item = -1;
9390 }
9391 }
9392
9393
9394 /* Possibly highlight a tool-bar item on frame F when mouse moves to
9395 tool-bar window-relative coordinates X/Y. Called from
9396 note_mouse_highlight. */
9397
9398 static void
9399 note_tool_bar_highlight (f, x, y)
9400 struct frame *f;
9401 int x, y;
9402 {
9403 Lisp_Object window = f->tool_bar_window;
9404 struct window *w = XWINDOW (window);
9405 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9406 int hpos, vpos;
9407 struct glyph *glyph;
9408 struct glyph_row *row;
9409 int i;
9410 Lisp_Object enabled_p;
9411 int prop_idx;
9412 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
9413 int mouse_down_p, rc;
9414
9415 /* Function note_mouse_highlight is called with negative x(y
9416 values when mouse moves outside of the frame. */
9417 if (x <= 0 || y <= 0)
9418 {
9419 clear_mouse_face (dpyinfo);
9420 return;
9421 }
9422
9423 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
9424 if (rc < 0)
9425 {
9426 /* Not on tool-bar item. */
9427 clear_mouse_face (dpyinfo);
9428 return;
9429 }
9430 else if (rc == 0)
9431 /* On same tool-bar item as before. */
9432 goto set_help_echo;
9433
9434 clear_mouse_face (dpyinfo);
9435
9436 /* Mouse is down, but on different tool-bar item? */
9437 mouse_down_p = (dpyinfo->grabbed
9438 && f == last_mouse_frame
9439 && FRAME_LIVE_P (f));
9440 if (mouse_down_p
9441 && last_tool_bar_item != prop_idx)
9442 return;
9443
9444 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
9445 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
9446
9447 /* If tool-bar item is not enabled, don't highlight it. */
9448 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
9449 if (!NILP (enabled_p))
9450 {
9451 /* Compute the x-position of the glyph. In front and past the
9452 image is a space. We include this in the highlighted area. */
9453 row = MATRIX_ROW (w->current_matrix, vpos);
9454 for (i = x = 0; i < hpos; ++i)
9455 x += row->glyphs[TEXT_AREA][i].pixel_width;
9456
9457 /* Record this as the current active region. */
9458 dpyinfo->mouse_face_beg_col = hpos;
9459 dpyinfo->mouse_face_beg_row = vpos;
9460 dpyinfo->mouse_face_beg_x = x;
9461 dpyinfo->mouse_face_beg_y = row->y;
9462 dpyinfo->mouse_face_past_end = 0;
9463
9464 dpyinfo->mouse_face_end_col = hpos + 1;
9465 dpyinfo->mouse_face_end_row = vpos;
9466 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
9467 dpyinfo->mouse_face_end_y = row->y;
9468 dpyinfo->mouse_face_window = window;
9469 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
9470
9471 /* Display it as active. */
9472 show_mouse_face (dpyinfo, draw);
9473 dpyinfo->mouse_face_image_state = draw;
9474 }
9475
9476 set_help_echo:
9477
9478 /* Set help_echo_string to a help string to display for this tool-bar item.
9479 XTread_socket does the rest. */
9480 help_echo_object = help_echo_window = Qnil;
9481 help_echo_pos = -1;
9482 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
9483 if (NILP (help_echo_string))
9484 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
9485 }
9486
9487 #endif /* HAVE_WINDOW_SYSTEM */
9488
9489
9490 \f
9491 /************************************************************************
9492 Horizontal scrolling
9493 ************************************************************************/
9494
9495 static int hscroll_window_tree P_ ((Lisp_Object));
9496 static int hscroll_windows P_ ((Lisp_Object));
9497
9498 /* For all leaf windows in the window tree rooted at WINDOW, set their
9499 hscroll value so that PT is (i) visible in the window, and (ii) so
9500 that it is not within a certain margin at the window's left and
9501 right border. Value is non-zero if any window's hscroll has been
9502 changed. */
9503
9504 static int
9505 hscroll_window_tree (window)
9506 Lisp_Object window;
9507 {
9508 int hscrolled_p = 0;
9509 int hscroll_relative_p = FLOATP (Vhscroll_step);
9510 int hscroll_step_abs = 0;
9511 double hscroll_step_rel = 0;
9512
9513 if (hscroll_relative_p)
9514 {
9515 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
9516 if (hscroll_step_rel < 0)
9517 {
9518 hscroll_relative_p = 0;
9519 hscroll_step_abs = 0;
9520 }
9521 }
9522 else if (INTEGERP (Vhscroll_step))
9523 {
9524 hscroll_step_abs = XINT (Vhscroll_step);
9525 if (hscroll_step_abs < 0)
9526 hscroll_step_abs = 0;
9527 }
9528 else
9529 hscroll_step_abs = 0;
9530
9531 while (WINDOWP (window))
9532 {
9533 struct window *w = XWINDOW (window);
9534
9535 if (WINDOWP (w->hchild))
9536 hscrolled_p |= hscroll_window_tree (w->hchild);
9537 else if (WINDOWP (w->vchild))
9538 hscrolled_p |= hscroll_window_tree (w->vchild);
9539 else if (w->cursor.vpos >= 0)
9540 {
9541 int h_margin;
9542 int text_area_width;
9543 struct glyph_row *current_cursor_row
9544 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
9545 struct glyph_row *desired_cursor_row
9546 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
9547 struct glyph_row *cursor_row
9548 = (desired_cursor_row->enabled_p
9549 ? desired_cursor_row
9550 : current_cursor_row);
9551
9552 text_area_width = window_box_width (w, TEXT_AREA);
9553
9554 /* Scroll when cursor is inside this scroll margin. */
9555 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
9556
9557 if ((XFASTINT (w->hscroll)
9558 && w->cursor.x <= h_margin)
9559 || (cursor_row->enabled_p
9560 && cursor_row->truncated_on_right_p
9561 && (w->cursor.x >= text_area_width - h_margin)))
9562 {
9563 struct it it;
9564 int hscroll;
9565 struct buffer *saved_current_buffer;
9566 int pt;
9567 int wanted_x;
9568
9569 /* Find point in a display of infinite width. */
9570 saved_current_buffer = current_buffer;
9571 current_buffer = XBUFFER (w->buffer);
9572
9573 if (w == XWINDOW (selected_window))
9574 pt = BUF_PT (current_buffer);
9575 else
9576 {
9577 pt = marker_position (w->pointm);
9578 pt = max (BEGV, pt);
9579 pt = min (ZV, pt);
9580 }
9581
9582 /* Move iterator to pt starting at cursor_row->start in
9583 a line with infinite width. */
9584 init_to_row_start (&it, w, cursor_row);
9585 it.last_visible_x = INFINITY;
9586 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
9587 current_buffer = saved_current_buffer;
9588
9589 /* Position cursor in window. */
9590 if (!hscroll_relative_p && hscroll_step_abs == 0)
9591 hscroll = max (0, (it.current_x
9592 - (ITERATOR_AT_END_OF_LINE_P (&it)
9593 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
9594 : (text_area_width / 2))))
9595 / FRAME_COLUMN_WIDTH (it.f);
9596 else if (w->cursor.x >= text_area_width - h_margin)
9597 {
9598 if (hscroll_relative_p)
9599 wanted_x = text_area_width * (1 - hscroll_step_rel)
9600 - h_margin;
9601 else
9602 wanted_x = text_area_width
9603 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9604 - h_margin;
9605 hscroll
9606 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9607 }
9608 else
9609 {
9610 if (hscroll_relative_p)
9611 wanted_x = text_area_width * hscroll_step_rel
9612 + h_margin;
9613 else
9614 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
9615 + h_margin;
9616 hscroll
9617 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
9618 }
9619 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
9620
9621 /* Don't call Fset_window_hscroll if value hasn't
9622 changed because it will prevent redisplay
9623 optimizations. */
9624 if (XFASTINT (w->hscroll) != hscroll)
9625 {
9626 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
9627 w->hscroll = make_number (hscroll);
9628 hscrolled_p = 1;
9629 }
9630 }
9631 }
9632
9633 window = w->next;
9634 }
9635
9636 /* Value is non-zero if hscroll of any leaf window has been changed. */
9637 return hscrolled_p;
9638 }
9639
9640
9641 /* Set hscroll so that cursor is visible and not inside horizontal
9642 scroll margins for all windows in the tree rooted at WINDOW. See
9643 also hscroll_window_tree above. Value is non-zero if any window's
9644 hscroll has been changed. If it has, desired matrices on the frame
9645 of WINDOW are cleared. */
9646
9647 static int
9648 hscroll_windows (window)
9649 Lisp_Object window;
9650 {
9651 int hscrolled_p;
9652
9653 if (automatic_hscrolling_p)
9654 {
9655 hscrolled_p = hscroll_window_tree (window);
9656 if (hscrolled_p)
9657 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
9658 }
9659 else
9660 hscrolled_p = 0;
9661 return hscrolled_p;
9662 }
9663
9664
9665 \f
9666 /************************************************************************
9667 Redisplay
9668 ************************************************************************/
9669
9670 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
9671 to a non-zero value. This is sometimes handy to have in a debugger
9672 session. */
9673
9674 #if GLYPH_DEBUG
9675
9676 /* First and last unchanged row for try_window_id. */
9677
9678 int debug_first_unchanged_at_end_vpos;
9679 int debug_last_unchanged_at_beg_vpos;
9680
9681 /* Delta vpos and y. */
9682
9683 int debug_dvpos, debug_dy;
9684
9685 /* Delta in characters and bytes for try_window_id. */
9686
9687 int debug_delta, debug_delta_bytes;
9688
9689 /* Values of window_end_pos and window_end_vpos at the end of
9690 try_window_id. */
9691
9692 EMACS_INT debug_end_pos, debug_end_vpos;
9693
9694 /* Append a string to W->desired_matrix->method. FMT is a printf
9695 format string. A1...A9 are a supplement for a variable-length
9696 argument list. If trace_redisplay_p is non-zero also printf the
9697 resulting string to stderr. */
9698
9699 static void
9700 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
9701 struct window *w;
9702 char *fmt;
9703 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
9704 {
9705 char buffer[512];
9706 char *method = w->desired_matrix->method;
9707 int len = strlen (method);
9708 int size = sizeof w->desired_matrix->method;
9709 int remaining = size - len - 1;
9710
9711 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
9712 if (len && remaining)
9713 {
9714 method[len] = '|';
9715 --remaining, ++len;
9716 }
9717
9718 strncpy (method + len, buffer, remaining);
9719
9720 if (trace_redisplay_p)
9721 fprintf (stderr, "%p (%s): %s\n",
9722 w,
9723 ((BUFFERP (w->buffer)
9724 && STRINGP (XBUFFER (w->buffer)->name))
9725 ? (char *) SDATA (XBUFFER (w->buffer)->name)
9726 : "no buffer"),
9727 buffer);
9728 }
9729
9730 #endif /* GLYPH_DEBUG */
9731
9732
9733 /* Value is non-zero if all changes in window W, which displays
9734 current_buffer, are in the text between START and END. START is a
9735 buffer position, END is given as a distance from Z. Used in
9736 redisplay_internal for display optimization. */
9737
9738 static INLINE int
9739 text_outside_line_unchanged_p (w, start, end)
9740 struct window *w;
9741 int start, end;
9742 {
9743 int unchanged_p = 1;
9744
9745 /* If text or overlays have changed, see where. */
9746 if (XFASTINT (w->last_modified) < MODIFF
9747 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
9748 {
9749 /* Gap in the line? */
9750 if (GPT < start || Z - GPT < end)
9751 unchanged_p = 0;
9752
9753 /* Changes start in front of the line, or end after it? */
9754 if (unchanged_p
9755 && (BEG_UNCHANGED < start - 1
9756 || END_UNCHANGED < end))
9757 unchanged_p = 0;
9758
9759 /* If selective display, can't optimize if changes start at the
9760 beginning of the line. */
9761 if (unchanged_p
9762 && INTEGERP (current_buffer->selective_display)
9763 && XINT (current_buffer->selective_display) > 0
9764 && (BEG_UNCHANGED < start || GPT <= start))
9765 unchanged_p = 0;
9766
9767 /* If there are overlays at the start or end of the line, these
9768 may have overlay strings with newlines in them. A change at
9769 START, for instance, may actually concern the display of such
9770 overlay strings as well, and they are displayed on different
9771 lines. So, quickly rule out this case. (For the future, it
9772 might be desirable to implement something more telling than
9773 just BEG/END_UNCHANGED.) */
9774 if (unchanged_p)
9775 {
9776 if (BEG + BEG_UNCHANGED == start
9777 && overlay_touches_p (start))
9778 unchanged_p = 0;
9779 if (END_UNCHANGED == end
9780 && overlay_touches_p (Z - end))
9781 unchanged_p = 0;
9782 }
9783 }
9784
9785 return unchanged_p;
9786 }
9787
9788
9789 /* Do a frame update, taking possible shortcuts into account. This is
9790 the main external entry point for redisplay.
9791
9792 If the last redisplay displayed an echo area message and that message
9793 is no longer requested, we clear the echo area or bring back the
9794 mini-buffer if that is in use. */
9795
9796 void
9797 redisplay ()
9798 {
9799 redisplay_internal (0);
9800 }
9801
9802
9803 static Lisp_Object
9804 overlay_arrow_string_or_property (var)
9805 Lisp_Object var;
9806 {
9807 Lisp_Object val;
9808
9809 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
9810 return val;
9811
9812 return Voverlay_arrow_string;
9813 }
9814
9815 /* Return 1 if there are any overlay-arrows in current_buffer. */
9816 static int
9817 overlay_arrow_in_current_buffer_p ()
9818 {
9819 Lisp_Object vlist;
9820
9821 for (vlist = Voverlay_arrow_variable_list;
9822 CONSP (vlist);
9823 vlist = XCDR (vlist))
9824 {
9825 Lisp_Object var = XCAR (vlist);
9826 Lisp_Object val;
9827
9828 if (!SYMBOLP (var))
9829 continue;
9830 val = find_symbol_value (var);
9831 if (MARKERP (val)
9832 && current_buffer == XMARKER (val)->buffer)
9833 return 1;
9834 }
9835 return 0;
9836 }
9837
9838
9839 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
9840 has changed. */
9841
9842 static int
9843 overlay_arrows_changed_p ()
9844 {
9845 Lisp_Object vlist;
9846
9847 for (vlist = Voverlay_arrow_variable_list;
9848 CONSP (vlist);
9849 vlist = XCDR (vlist))
9850 {
9851 Lisp_Object var = XCAR (vlist);
9852 Lisp_Object val, pstr;
9853
9854 if (!SYMBOLP (var))
9855 continue;
9856 val = find_symbol_value (var);
9857 if (!MARKERP (val))
9858 continue;
9859 if (! EQ (COERCE_MARKER (val),
9860 Fget (var, Qlast_arrow_position))
9861 || ! (pstr = overlay_arrow_string_or_property (var),
9862 EQ (pstr, Fget (var, Qlast_arrow_string))))
9863 return 1;
9864 }
9865 return 0;
9866 }
9867
9868 /* Mark overlay arrows to be updated on next redisplay. */
9869
9870 static void
9871 update_overlay_arrows (up_to_date)
9872 int up_to_date;
9873 {
9874 Lisp_Object vlist;
9875
9876 for (vlist = Voverlay_arrow_variable_list;
9877 CONSP (vlist);
9878 vlist = XCDR (vlist))
9879 {
9880 Lisp_Object var = XCAR (vlist);
9881
9882 if (!SYMBOLP (var))
9883 continue;
9884
9885 if (up_to_date > 0)
9886 {
9887 Lisp_Object val = find_symbol_value (var);
9888 Fput (var, Qlast_arrow_position,
9889 COERCE_MARKER (val));
9890 Fput (var, Qlast_arrow_string,
9891 overlay_arrow_string_or_property (var));
9892 }
9893 else if (up_to_date < 0
9894 || !NILP (Fget (var, Qlast_arrow_position)))
9895 {
9896 Fput (var, Qlast_arrow_position, Qt);
9897 Fput (var, Qlast_arrow_string, Qt);
9898 }
9899 }
9900 }
9901
9902
9903 /* Return overlay arrow string to display at row.
9904 Return integer (bitmap number) for arrow bitmap in left fringe.
9905 Return nil if no overlay arrow. */
9906
9907 static Lisp_Object
9908 overlay_arrow_at_row (it, row)
9909 struct it *it;
9910 struct glyph_row *row;
9911 {
9912 Lisp_Object vlist;
9913
9914 for (vlist = Voverlay_arrow_variable_list;
9915 CONSP (vlist);
9916 vlist = XCDR (vlist))
9917 {
9918 Lisp_Object var = XCAR (vlist);
9919 Lisp_Object val;
9920
9921 if (!SYMBOLP (var))
9922 continue;
9923
9924 val = find_symbol_value (var);
9925
9926 if (MARKERP (val)
9927 && current_buffer == XMARKER (val)->buffer
9928 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
9929 {
9930 if (FRAME_WINDOW_P (it->f)
9931 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
9932 {
9933 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
9934 {
9935 int fringe_bitmap;
9936 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
9937 return make_number (fringe_bitmap);
9938 }
9939 return make_number (-1); /* Use default arrow bitmap */
9940 }
9941 return overlay_arrow_string_or_property (var);
9942 }
9943 }
9944
9945 return Qnil;
9946 }
9947
9948 /* Return 1 if point moved out of or into a composition. Otherwise
9949 return 0. PREV_BUF and PREV_PT are the last point buffer and
9950 position. BUF and PT are the current point buffer and position. */
9951
9952 int
9953 check_point_in_composition (prev_buf, prev_pt, buf, pt)
9954 struct buffer *prev_buf, *buf;
9955 int prev_pt, pt;
9956 {
9957 int start, end;
9958 Lisp_Object prop;
9959 Lisp_Object buffer;
9960
9961 XSETBUFFER (buffer, buf);
9962 /* Check a composition at the last point if point moved within the
9963 same buffer. */
9964 if (prev_buf == buf)
9965 {
9966 if (prev_pt == pt)
9967 /* Point didn't move. */
9968 return 0;
9969
9970 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
9971 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
9972 && COMPOSITION_VALID_P (start, end, prop)
9973 && start < prev_pt && end > prev_pt)
9974 /* The last point was within the composition. Return 1 iff
9975 point moved out of the composition. */
9976 return (pt <= start || pt >= end);
9977 }
9978
9979 /* Check a composition at the current point. */
9980 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
9981 && find_composition (pt, -1, &start, &end, &prop, buffer)
9982 && COMPOSITION_VALID_P (start, end, prop)
9983 && start < pt && end > pt);
9984 }
9985
9986
9987 /* Reconsider the setting of B->clip_changed which is displayed
9988 in window W. */
9989
9990 static INLINE void
9991 reconsider_clip_changes (w, b)
9992 struct window *w;
9993 struct buffer *b;
9994 {
9995 if (b->clip_changed
9996 && !NILP (w->window_end_valid)
9997 && w->current_matrix->buffer == b
9998 && w->current_matrix->zv == BUF_ZV (b)
9999 && w->current_matrix->begv == BUF_BEGV (b))
10000 b->clip_changed = 0;
10001
10002 /* If display wasn't paused, and W is not a tool bar window, see if
10003 point has been moved into or out of a composition. In that case,
10004 we set b->clip_changed to 1 to force updating the screen. If
10005 b->clip_changed has already been set to 1, we can skip this
10006 check. */
10007 if (!b->clip_changed
10008 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10009 {
10010 int pt;
10011
10012 if (w == XWINDOW (selected_window))
10013 pt = BUF_PT (current_buffer);
10014 else
10015 pt = marker_position (w->pointm);
10016
10017 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10018 || pt != XINT (w->last_point))
10019 && check_point_in_composition (w->current_matrix->buffer,
10020 XINT (w->last_point),
10021 XBUFFER (w->buffer), pt))
10022 b->clip_changed = 1;
10023 }
10024 }
10025 \f
10026
10027 /* Select FRAME to forward the values of frame-local variables into C
10028 variables so that the redisplay routines can access those values
10029 directly. */
10030
10031 static void
10032 select_frame_for_redisplay (frame)
10033 Lisp_Object frame;
10034 {
10035 Lisp_Object tail, sym, val;
10036 Lisp_Object old = selected_frame;
10037
10038 selected_frame = frame;
10039
10040 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10041 if (CONSP (XCAR (tail))
10042 && (sym = XCAR (XCAR (tail)),
10043 SYMBOLP (sym))
10044 && (sym = indirect_variable (sym),
10045 val = SYMBOL_VALUE (sym),
10046 (BUFFER_LOCAL_VALUEP (val)
10047 || SOME_BUFFER_LOCAL_VALUEP (val)))
10048 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10049 Fsymbol_value (sym);
10050
10051 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10052 if (CONSP (XCAR (tail))
10053 && (sym = XCAR (XCAR (tail)),
10054 SYMBOLP (sym))
10055 && (sym = indirect_variable (sym),
10056 val = SYMBOL_VALUE (sym),
10057 (BUFFER_LOCAL_VALUEP (val)
10058 || SOME_BUFFER_LOCAL_VALUEP (val)))
10059 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10060 Fsymbol_value (sym);
10061 }
10062
10063
10064 #define STOP_POLLING \
10065 do { if (! polling_stopped_here) stop_polling (); \
10066 polling_stopped_here = 1; } while (0)
10067
10068 #define RESUME_POLLING \
10069 do { if (polling_stopped_here) start_polling (); \
10070 polling_stopped_here = 0; } while (0)
10071
10072
10073 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10074 response to any user action; therefore, we should preserve the echo
10075 area. (Actually, our caller does that job.) Perhaps in the future
10076 avoid recentering windows if it is not necessary; currently that
10077 causes some problems. */
10078
10079 static void
10080 redisplay_internal (preserve_echo_area)
10081 int preserve_echo_area;
10082 {
10083 struct window *w = XWINDOW (selected_window);
10084 struct frame *f = XFRAME (w->frame);
10085 int pause;
10086 int must_finish = 0;
10087 struct text_pos tlbufpos, tlendpos;
10088 int number_of_visible_frames;
10089 int count;
10090 struct frame *sf = SELECTED_FRAME ();
10091 int polling_stopped_here = 0;
10092
10093 /* Non-zero means redisplay has to consider all windows on all
10094 frames. Zero means, only selected_window is considered. */
10095 int consider_all_windows_p;
10096
10097 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10098
10099 /* No redisplay if running in batch mode or frame is not yet fully
10100 initialized, or redisplay is explicitly turned off by setting
10101 Vinhibit_redisplay. */
10102 if (noninteractive
10103 || !NILP (Vinhibit_redisplay)
10104 || !f->glyphs_initialized_p)
10105 return;
10106
10107 /* The flag redisplay_performed_directly_p is set by
10108 direct_output_for_insert when it already did the whole screen
10109 update necessary. */
10110 if (redisplay_performed_directly_p)
10111 {
10112 redisplay_performed_directly_p = 0;
10113 if (!hscroll_windows (selected_window))
10114 return;
10115 }
10116
10117 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10118 if (popup_activated ())
10119 return;
10120 #endif
10121
10122 /* I don't think this happens but let's be paranoid. */
10123 if (redisplaying_p)
10124 return;
10125
10126 /* Record a function that resets redisplaying_p to its old value
10127 when we leave this function. */
10128 count = SPECPDL_INDEX ();
10129 record_unwind_protect (unwind_redisplay,
10130 Fcons (make_number (redisplaying_p), selected_frame));
10131 ++redisplaying_p;
10132 specbind (Qinhibit_free_realized_faces, Qnil);
10133
10134 retry:
10135 pause = 0;
10136 reconsider_clip_changes (w, current_buffer);
10137
10138 /* If new fonts have been loaded that make a glyph matrix adjustment
10139 necessary, do it. */
10140 if (fonts_changed_p)
10141 {
10142 adjust_glyphs (NULL);
10143 ++windows_or_buffers_changed;
10144 fonts_changed_p = 0;
10145 }
10146
10147 /* If face_change_count is non-zero, init_iterator will free all
10148 realized faces, which includes the faces referenced from current
10149 matrices. So, we can't reuse current matrices in this case. */
10150 if (face_change_count)
10151 ++windows_or_buffers_changed;
10152
10153 if (! FRAME_WINDOW_P (sf)
10154 && previous_terminal_frame != sf)
10155 {
10156 /* Since frames on an ASCII terminal share the same display
10157 area, displaying a different frame means redisplay the whole
10158 thing. */
10159 windows_or_buffers_changed++;
10160 SET_FRAME_GARBAGED (sf);
10161 XSETFRAME (Vterminal_frame, sf);
10162 }
10163 previous_terminal_frame = sf;
10164
10165 /* Set the visible flags for all frames. Do this before checking
10166 for resized or garbaged frames; they want to know if their frames
10167 are visible. See the comment in frame.h for
10168 FRAME_SAMPLE_VISIBILITY. */
10169 {
10170 Lisp_Object tail, frame;
10171
10172 number_of_visible_frames = 0;
10173
10174 FOR_EACH_FRAME (tail, frame)
10175 {
10176 struct frame *f = XFRAME (frame);
10177
10178 FRAME_SAMPLE_VISIBILITY (f);
10179 if (FRAME_VISIBLE_P (f))
10180 ++number_of_visible_frames;
10181 clear_desired_matrices (f);
10182 }
10183 }
10184
10185 /* Notice any pending interrupt request to change frame size. */
10186 do_pending_window_change (1);
10187
10188 /* Clear frames marked as garbaged. */
10189 if (frame_garbaged)
10190 clear_garbaged_frames ();
10191
10192 /* Build menubar and tool-bar items. */
10193 prepare_menu_bars ();
10194
10195 if (windows_or_buffers_changed)
10196 update_mode_lines++;
10197
10198 /* Detect case that we need to write or remove a star in the mode line. */
10199 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10200 {
10201 w->update_mode_line = Qt;
10202 if (buffer_shared > 1)
10203 update_mode_lines++;
10204 }
10205
10206 /* If %c is in the mode line, update it if needed. */
10207 if (!NILP (w->column_number_displayed)
10208 /* This alternative quickly identifies a common case
10209 where no change is needed. */
10210 && !(PT == XFASTINT (w->last_point)
10211 && XFASTINT (w->last_modified) >= MODIFF
10212 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10213 && (XFASTINT (w->column_number_displayed)
10214 != (int) current_column ())) /* iftc */
10215 w->update_mode_line = Qt;
10216
10217 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10218
10219 /* The variable buffer_shared is set in redisplay_window and
10220 indicates that we redisplay a buffer in different windows. See
10221 there. */
10222 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10223 || cursor_type_changed);
10224
10225 /* If specs for an arrow have changed, do thorough redisplay
10226 to ensure we remove any arrow that should no longer exist. */
10227 if (overlay_arrows_changed_p ())
10228 consider_all_windows_p = windows_or_buffers_changed = 1;
10229
10230 /* Normally the message* functions will have already displayed and
10231 updated the echo area, but the frame may have been trashed, or
10232 the update may have been preempted, so display the echo area
10233 again here. Checking message_cleared_p captures the case that
10234 the echo area should be cleared. */
10235 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10236 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10237 || (message_cleared_p
10238 && minibuf_level == 0
10239 /* If the mini-window is currently selected, this means the
10240 echo-area doesn't show through. */
10241 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10242 {
10243 int window_height_changed_p = echo_area_display (0);
10244 must_finish = 1;
10245
10246 /* If we don't display the current message, don't clear the
10247 message_cleared_p flag, because, if we did, we wouldn't clear
10248 the echo area in the next redisplay which doesn't preserve
10249 the echo area. */
10250 if (!display_last_displayed_message_p)
10251 message_cleared_p = 0;
10252
10253 if (fonts_changed_p)
10254 goto retry;
10255 else if (window_height_changed_p)
10256 {
10257 consider_all_windows_p = 1;
10258 ++update_mode_lines;
10259 ++windows_or_buffers_changed;
10260
10261 /* If window configuration was changed, frames may have been
10262 marked garbaged. Clear them or we will experience
10263 surprises wrt scrolling. */
10264 if (frame_garbaged)
10265 clear_garbaged_frames ();
10266 }
10267 }
10268 else if (EQ (selected_window, minibuf_window)
10269 && (current_buffer->clip_changed
10270 || XFASTINT (w->last_modified) < MODIFF
10271 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10272 && resize_mini_window (w, 0))
10273 {
10274 /* Resized active mini-window to fit the size of what it is
10275 showing if its contents might have changed. */
10276 must_finish = 1;
10277 consider_all_windows_p = 1;
10278 ++windows_or_buffers_changed;
10279 ++update_mode_lines;
10280
10281 /* If window configuration was changed, frames may have been
10282 marked garbaged. Clear them or we will experience
10283 surprises wrt scrolling. */
10284 if (frame_garbaged)
10285 clear_garbaged_frames ();
10286 }
10287
10288
10289 /* If showing the region, and mark has changed, we must redisplay
10290 the whole window. The assignment to this_line_start_pos prevents
10291 the optimization directly below this if-statement. */
10292 if (((!NILP (Vtransient_mark_mode)
10293 && !NILP (XBUFFER (w->buffer)->mark_active))
10294 != !NILP (w->region_showing))
10295 || (!NILP (w->region_showing)
10296 && !EQ (w->region_showing,
10297 Fmarker_position (XBUFFER (w->buffer)->mark))))
10298 CHARPOS (this_line_start_pos) = 0;
10299
10300 /* Optimize the case that only the line containing the cursor in the
10301 selected window has changed. Variables starting with this_ are
10302 set in display_line and record information about the line
10303 containing the cursor. */
10304 tlbufpos = this_line_start_pos;
10305 tlendpos = this_line_end_pos;
10306 if (!consider_all_windows_p
10307 && CHARPOS (tlbufpos) > 0
10308 && NILP (w->update_mode_line)
10309 && !current_buffer->clip_changed
10310 && !current_buffer->prevent_redisplay_optimizations_p
10311 && FRAME_VISIBLE_P (XFRAME (w->frame))
10312 && !FRAME_OBSCURED_P (XFRAME (w->frame))
10313 /* Make sure recorded data applies to current buffer, etc. */
10314 && this_line_buffer == current_buffer
10315 && current_buffer == XBUFFER (w->buffer)
10316 && NILP (w->force_start)
10317 && NILP (w->optional_new_start)
10318 /* Point must be on the line that we have info recorded about. */
10319 && PT >= CHARPOS (tlbufpos)
10320 && PT <= Z - CHARPOS (tlendpos)
10321 /* All text outside that line, including its final newline,
10322 must be unchanged */
10323 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
10324 CHARPOS (tlendpos)))
10325 {
10326 if (CHARPOS (tlbufpos) > BEGV
10327 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
10328 && (CHARPOS (tlbufpos) == ZV
10329 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
10330 /* Former continuation line has disappeared by becoming empty */
10331 goto cancel;
10332 else if (XFASTINT (w->last_modified) < MODIFF
10333 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
10334 || MINI_WINDOW_P (w))
10335 {
10336 /* We have to handle the case of continuation around a
10337 wide-column character (See the comment in indent.c around
10338 line 885).
10339
10340 For instance, in the following case:
10341
10342 -------- Insert --------
10343 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
10344 J_I_ ==> J_I_ `^^' are cursors.
10345 ^^ ^^
10346 -------- --------
10347
10348 As we have to redraw the line above, we should goto cancel. */
10349
10350 struct it it;
10351 int line_height_before = this_line_pixel_height;
10352
10353 /* Note that start_display will handle the case that the
10354 line starting at tlbufpos is a continuation lines. */
10355 start_display (&it, w, tlbufpos);
10356
10357 /* Implementation note: It this still necessary? */
10358 if (it.current_x != this_line_start_x)
10359 goto cancel;
10360
10361 TRACE ((stderr, "trying display optimization 1\n"));
10362 w->cursor.vpos = -1;
10363 overlay_arrow_seen = 0;
10364 it.vpos = this_line_vpos;
10365 it.current_y = this_line_y;
10366 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
10367 display_line (&it);
10368
10369 /* If line contains point, is not continued,
10370 and ends at same distance from eob as before, we win */
10371 if (w->cursor.vpos >= 0
10372 /* Line is not continued, otherwise this_line_start_pos
10373 would have been set to 0 in display_line. */
10374 && CHARPOS (this_line_start_pos)
10375 /* Line ends as before. */
10376 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
10377 /* Line has same height as before. Otherwise other lines
10378 would have to be shifted up or down. */
10379 && this_line_pixel_height == line_height_before)
10380 {
10381 /* If this is not the window's last line, we must adjust
10382 the charstarts of the lines below. */
10383 if (it.current_y < it.last_visible_y)
10384 {
10385 struct glyph_row *row
10386 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
10387 int delta, delta_bytes;
10388
10389 if (Z - CHARPOS (tlendpos) == ZV)
10390 {
10391 /* This line ends at end of (accessible part of)
10392 buffer. There is no newline to count. */
10393 delta = (Z
10394 - CHARPOS (tlendpos)
10395 - MATRIX_ROW_START_CHARPOS (row));
10396 delta_bytes = (Z_BYTE
10397 - BYTEPOS (tlendpos)
10398 - MATRIX_ROW_START_BYTEPOS (row));
10399 }
10400 else
10401 {
10402 /* This line ends in a newline. Must take
10403 account of the newline and the rest of the
10404 text that follows. */
10405 delta = (Z
10406 - CHARPOS (tlendpos)
10407 - MATRIX_ROW_START_CHARPOS (row));
10408 delta_bytes = (Z_BYTE
10409 - BYTEPOS (tlendpos)
10410 - MATRIX_ROW_START_BYTEPOS (row));
10411 }
10412
10413 increment_matrix_positions (w->current_matrix,
10414 this_line_vpos + 1,
10415 w->current_matrix->nrows,
10416 delta, delta_bytes);
10417 }
10418
10419 /* If this row displays text now but previously didn't,
10420 or vice versa, w->window_end_vpos may have to be
10421 adjusted. */
10422 if ((it.glyph_row - 1)->displays_text_p)
10423 {
10424 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
10425 XSETINT (w->window_end_vpos, this_line_vpos);
10426 }
10427 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
10428 && this_line_vpos > 0)
10429 XSETINT (w->window_end_vpos, this_line_vpos - 1);
10430 w->window_end_valid = Qnil;
10431
10432 /* Update hint: No need to try to scroll in update_window. */
10433 w->desired_matrix->no_scrolling_p = 1;
10434
10435 #if GLYPH_DEBUG
10436 *w->desired_matrix->method = 0;
10437 debug_method_add (w, "optimization 1");
10438 #endif
10439 #ifdef HAVE_WINDOW_SYSTEM
10440 update_window_fringes (w, 0);
10441 #endif
10442 goto update;
10443 }
10444 else
10445 goto cancel;
10446 }
10447 else if (/* Cursor position hasn't changed. */
10448 PT == XFASTINT (w->last_point)
10449 /* Make sure the cursor was last displayed
10450 in this window. Otherwise we have to reposition it. */
10451 && 0 <= w->cursor.vpos
10452 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
10453 {
10454 if (!must_finish)
10455 {
10456 do_pending_window_change (1);
10457
10458 /* We used to always goto end_of_redisplay here, but this
10459 isn't enough if we have a blinking cursor. */
10460 if (w->cursor_off_p == w->last_cursor_off_p)
10461 goto end_of_redisplay;
10462 }
10463 goto update;
10464 }
10465 /* If highlighting the region, or if the cursor is in the echo area,
10466 then we can't just move the cursor. */
10467 else if (! (!NILP (Vtransient_mark_mode)
10468 && !NILP (current_buffer->mark_active))
10469 && (EQ (selected_window, current_buffer->last_selected_window)
10470 || highlight_nonselected_windows)
10471 && NILP (w->region_showing)
10472 && NILP (Vshow_trailing_whitespace)
10473 && !cursor_in_echo_area)
10474 {
10475 struct it it;
10476 struct glyph_row *row;
10477
10478 /* Skip from tlbufpos to PT and see where it is. Note that
10479 PT may be in invisible text. If so, we will end at the
10480 next visible position. */
10481 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
10482 NULL, DEFAULT_FACE_ID);
10483 it.current_x = this_line_start_x;
10484 it.current_y = this_line_y;
10485 it.vpos = this_line_vpos;
10486
10487 /* The call to move_it_to stops in front of PT, but
10488 moves over before-strings. */
10489 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
10490
10491 if (it.vpos == this_line_vpos
10492 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
10493 row->enabled_p))
10494 {
10495 xassert (this_line_vpos == it.vpos);
10496 xassert (this_line_y == it.current_y);
10497 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
10498 #if GLYPH_DEBUG
10499 *w->desired_matrix->method = 0;
10500 debug_method_add (w, "optimization 3");
10501 #endif
10502 goto update;
10503 }
10504 else
10505 goto cancel;
10506 }
10507
10508 cancel:
10509 /* Text changed drastically or point moved off of line. */
10510 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
10511 }
10512
10513 CHARPOS (this_line_start_pos) = 0;
10514 consider_all_windows_p |= buffer_shared > 1;
10515 ++clear_face_cache_count;
10516 #ifdef HAVE_WINDOW_SYSTEM
10517 ++clear_image_cache_count;
10518 #endif
10519
10520 /* Build desired matrices, and update the display. If
10521 consider_all_windows_p is non-zero, do it for all windows on all
10522 frames. Otherwise do it for selected_window, only. */
10523
10524 if (consider_all_windows_p)
10525 {
10526 Lisp_Object tail, frame;
10527 int i, n = 0, size = 50;
10528 struct frame **updated
10529 = (struct frame **) alloca (size * sizeof *updated);
10530
10531 /* Recompute # windows showing selected buffer. This will be
10532 incremented each time such a window is displayed. */
10533 buffer_shared = 0;
10534
10535 FOR_EACH_FRAME (tail, frame)
10536 {
10537 struct frame *f = XFRAME (frame);
10538
10539 if (FRAME_WINDOW_P (f) || f == sf)
10540 {
10541 if (! EQ (frame, selected_frame))
10542 /* Select the frame, for the sake of frame-local
10543 variables. */
10544 select_frame_for_redisplay (frame);
10545
10546 /* Mark all the scroll bars to be removed; we'll redeem
10547 the ones we want when we redisplay their windows. */
10548 if (condemn_scroll_bars_hook)
10549 condemn_scroll_bars_hook (f);
10550
10551 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10552 redisplay_windows (FRAME_ROOT_WINDOW (f));
10553
10554 /* Any scroll bars which redisplay_windows should have
10555 nuked should now go away. */
10556 if (judge_scroll_bars_hook)
10557 judge_scroll_bars_hook (f);
10558
10559 /* If fonts changed, display again. */
10560 /* ??? rms: I suspect it is a mistake to jump all the way
10561 back to retry here. It should just retry this frame. */
10562 if (fonts_changed_p)
10563 goto retry;
10564
10565 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
10566 {
10567 /* See if we have to hscroll. */
10568 if (hscroll_windows (f->root_window))
10569 goto retry;
10570
10571 /* Prevent various kinds of signals during display
10572 update. stdio is not robust about handling
10573 signals, which can cause an apparent I/O
10574 error. */
10575 if (interrupt_input)
10576 unrequest_sigio ();
10577 STOP_POLLING;
10578
10579 /* Update the display. */
10580 set_window_update_flags (XWINDOW (f->root_window), 1);
10581 pause |= update_frame (f, 0, 0);
10582 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
10583 if (pause)
10584 break;
10585 #endif
10586
10587 if (n == size)
10588 {
10589 int nbytes = size * sizeof *updated;
10590 struct frame **p = (struct frame **) alloca (2 * nbytes);
10591 bcopy (updated, p, nbytes);
10592 size *= 2;
10593 }
10594
10595 updated[n++] = f;
10596 }
10597 }
10598 }
10599
10600 if (!pause)
10601 {
10602 /* Do the mark_window_display_accurate after all windows have
10603 been redisplayed because this call resets flags in buffers
10604 which are needed for proper redisplay. */
10605 for (i = 0; i < n; ++i)
10606 {
10607 struct frame *f = updated[i];
10608 mark_window_display_accurate (f->root_window, 1);
10609 if (frame_up_to_date_hook)
10610 frame_up_to_date_hook (f);
10611 }
10612 }
10613 }
10614 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10615 {
10616 Lisp_Object mini_window;
10617 struct frame *mini_frame;
10618
10619 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
10620 /* Use list_of_error, not Qerror, so that
10621 we catch only errors and don't run the debugger. */
10622 internal_condition_case_1 (redisplay_window_1, selected_window,
10623 list_of_error,
10624 redisplay_window_error);
10625
10626 /* Compare desired and current matrices, perform output. */
10627
10628 update:
10629 /* If fonts changed, display again. */
10630 if (fonts_changed_p)
10631 goto retry;
10632
10633 /* Prevent various kinds of signals during display update.
10634 stdio is not robust about handling signals,
10635 which can cause an apparent I/O error. */
10636 if (interrupt_input)
10637 unrequest_sigio ();
10638 STOP_POLLING;
10639
10640 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
10641 {
10642 if (hscroll_windows (selected_window))
10643 goto retry;
10644
10645 XWINDOW (selected_window)->must_be_updated_p = 1;
10646 pause = update_frame (sf, 0, 0);
10647 }
10648
10649 /* We may have called echo_area_display at the top of this
10650 function. If the echo area is on another frame, that may
10651 have put text on a frame other than the selected one, so the
10652 above call to update_frame would not have caught it. Catch
10653 it here. */
10654 mini_window = FRAME_MINIBUF_WINDOW (sf);
10655 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10656
10657 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
10658 {
10659 XWINDOW (mini_window)->must_be_updated_p = 1;
10660 pause |= update_frame (mini_frame, 0, 0);
10661 if (!pause && hscroll_windows (mini_window))
10662 goto retry;
10663 }
10664 }
10665
10666 /* If display was paused because of pending input, make sure we do a
10667 thorough update the next time. */
10668 if (pause)
10669 {
10670 /* Prevent the optimization at the beginning of
10671 redisplay_internal that tries a single-line update of the
10672 line containing the cursor in the selected window. */
10673 CHARPOS (this_line_start_pos) = 0;
10674
10675 /* Let the overlay arrow be updated the next time. */
10676 update_overlay_arrows (0);
10677
10678 /* If we pause after scrolling, some rows in the current
10679 matrices of some windows are not valid. */
10680 if (!WINDOW_FULL_WIDTH_P (w)
10681 && !FRAME_WINDOW_P (XFRAME (w->frame)))
10682 update_mode_lines = 1;
10683 }
10684 else
10685 {
10686 if (!consider_all_windows_p)
10687 {
10688 /* This has already been done above if
10689 consider_all_windows_p is set. */
10690 mark_window_display_accurate_1 (w, 1);
10691
10692 /* Say overlay arrows are up to date. */
10693 update_overlay_arrows (1);
10694
10695 if (frame_up_to_date_hook != 0)
10696 frame_up_to_date_hook (sf);
10697 }
10698
10699 update_mode_lines = 0;
10700 windows_or_buffers_changed = 0;
10701 cursor_type_changed = 0;
10702 }
10703
10704 /* Start SIGIO interrupts coming again. Having them off during the
10705 code above makes it less likely one will discard output, but not
10706 impossible, since there might be stuff in the system buffer here.
10707 But it is much hairier to try to do anything about that. */
10708 if (interrupt_input)
10709 request_sigio ();
10710 RESUME_POLLING;
10711
10712 /* If a frame has become visible which was not before, redisplay
10713 again, so that we display it. Expose events for such a frame
10714 (which it gets when becoming visible) don't call the parts of
10715 redisplay constructing glyphs, so simply exposing a frame won't
10716 display anything in this case. So, we have to display these
10717 frames here explicitly. */
10718 if (!pause)
10719 {
10720 Lisp_Object tail, frame;
10721 int new_count = 0;
10722
10723 FOR_EACH_FRAME (tail, frame)
10724 {
10725 int this_is_visible = 0;
10726
10727 if (XFRAME (frame)->visible)
10728 this_is_visible = 1;
10729 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
10730 if (XFRAME (frame)->visible)
10731 this_is_visible = 1;
10732
10733 if (this_is_visible)
10734 new_count++;
10735 }
10736
10737 if (new_count != number_of_visible_frames)
10738 windows_or_buffers_changed++;
10739 }
10740
10741 /* Change frame size now if a change is pending. */
10742 do_pending_window_change (1);
10743
10744 /* If we just did a pending size change, or have additional
10745 visible frames, redisplay again. */
10746 if (windows_or_buffers_changed && !pause)
10747 goto retry;
10748
10749 /* Clear the face cache eventually. */
10750 if (consider_all_windows_p)
10751 {
10752 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
10753 {
10754 clear_face_cache (0);
10755 clear_face_cache_count = 0;
10756 }
10757 #ifdef HAVE_WINDOW_SYSTEM
10758 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
10759 {
10760 Lisp_Object tail, frame;
10761 FOR_EACH_FRAME (tail, frame)
10762 {
10763 struct frame *f = XFRAME (frame);
10764 if (FRAME_WINDOW_P (f))
10765 clear_image_cache (f, 0);
10766 }
10767 clear_image_cache_count = 0;
10768 }
10769 #endif /* HAVE_WINDOW_SYSTEM */
10770 }
10771
10772 end_of_redisplay:
10773 unbind_to (count, Qnil);
10774 RESUME_POLLING;
10775 }
10776
10777
10778 /* Redisplay, but leave alone any recent echo area message unless
10779 another message has been requested in its place.
10780
10781 This is useful in situations where you need to redisplay but no
10782 user action has occurred, making it inappropriate for the message
10783 area to be cleared. See tracking_off and
10784 wait_reading_process_output for examples of these situations.
10785
10786 FROM_WHERE is an integer saying from where this function was
10787 called. This is useful for debugging. */
10788
10789 void
10790 redisplay_preserve_echo_area (from_where)
10791 int from_where;
10792 {
10793 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
10794
10795 if (!NILP (echo_area_buffer[1]))
10796 {
10797 /* We have a previously displayed message, but no current
10798 message. Redisplay the previous message. */
10799 display_last_displayed_message_p = 1;
10800 redisplay_internal (1);
10801 display_last_displayed_message_p = 0;
10802 }
10803 else
10804 redisplay_internal (1);
10805
10806 if (rif != NULL && rif->flush_display_optional)
10807 rif->flush_display_optional (NULL);
10808 }
10809
10810
10811 /* Function registered with record_unwind_protect in
10812 redisplay_internal. Reset redisplaying_p to the value it had
10813 before redisplay_internal was called, and clear
10814 prevent_freeing_realized_faces_p. It also selects the previously
10815 selected frame. */
10816
10817 static Lisp_Object
10818 unwind_redisplay (val)
10819 Lisp_Object val;
10820 {
10821 Lisp_Object old_redisplaying_p, old_frame;
10822
10823 old_redisplaying_p = XCAR (val);
10824 redisplaying_p = XFASTINT (old_redisplaying_p);
10825 old_frame = XCDR (val);
10826 if (! EQ (old_frame, selected_frame))
10827 select_frame_for_redisplay (old_frame);
10828 return Qnil;
10829 }
10830
10831
10832 /* Mark the display of window W as accurate or inaccurate. If
10833 ACCURATE_P is non-zero mark display of W as accurate. If
10834 ACCURATE_P is zero, arrange for W to be redisplayed the next time
10835 redisplay_internal is called. */
10836
10837 static void
10838 mark_window_display_accurate_1 (w, accurate_p)
10839 struct window *w;
10840 int accurate_p;
10841 {
10842 if (BUFFERP (w->buffer))
10843 {
10844 struct buffer *b = XBUFFER (w->buffer);
10845
10846 w->last_modified
10847 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
10848 w->last_overlay_modified
10849 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
10850 w->last_had_star
10851 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
10852
10853 if (accurate_p)
10854 {
10855 b->clip_changed = 0;
10856 b->prevent_redisplay_optimizations_p = 0;
10857
10858 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
10859 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
10860 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
10861 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
10862
10863 w->current_matrix->buffer = b;
10864 w->current_matrix->begv = BUF_BEGV (b);
10865 w->current_matrix->zv = BUF_ZV (b);
10866
10867 w->last_cursor = w->cursor;
10868 w->last_cursor_off_p = w->cursor_off_p;
10869
10870 if (w == XWINDOW (selected_window))
10871 w->last_point = make_number (BUF_PT (b));
10872 else
10873 w->last_point = make_number (XMARKER (w->pointm)->charpos);
10874 }
10875 }
10876
10877 if (accurate_p)
10878 {
10879 w->window_end_valid = w->buffer;
10880 #if 0 /* This is incorrect with variable-height lines. */
10881 xassert (XINT (w->window_end_vpos)
10882 < (WINDOW_TOTAL_LINES (w)
10883 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
10884 #endif
10885 w->update_mode_line = Qnil;
10886 }
10887 }
10888
10889
10890 /* Mark the display of windows in the window tree rooted at WINDOW as
10891 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
10892 windows as accurate. If ACCURATE_P is zero, arrange for windows to
10893 be redisplayed the next time redisplay_internal is called. */
10894
10895 void
10896 mark_window_display_accurate (window, accurate_p)
10897 Lisp_Object window;
10898 int accurate_p;
10899 {
10900 struct window *w;
10901
10902 for (; !NILP (window); window = w->next)
10903 {
10904 w = XWINDOW (window);
10905 mark_window_display_accurate_1 (w, accurate_p);
10906
10907 if (!NILP (w->vchild))
10908 mark_window_display_accurate (w->vchild, accurate_p);
10909 if (!NILP (w->hchild))
10910 mark_window_display_accurate (w->hchild, accurate_p);
10911 }
10912
10913 if (accurate_p)
10914 {
10915 update_overlay_arrows (1);
10916 }
10917 else
10918 {
10919 /* Force a thorough redisplay the next time by setting
10920 last_arrow_position and last_arrow_string to t, which is
10921 unequal to any useful value of Voverlay_arrow_... */
10922 update_overlay_arrows (-1);
10923 }
10924 }
10925
10926
10927 /* Return value in display table DP (Lisp_Char_Table *) for character
10928 C. Since a display table doesn't have any parent, we don't have to
10929 follow parent. Do not call this function directly but use the
10930 macro DISP_CHAR_VECTOR. */
10931
10932 Lisp_Object
10933 disp_char_vector (dp, c)
10934 struct Lisp_Char_Table *dp;
10935 int c;
10936 {
10937 int code[4], i;
10938 Lisp_Object val;
10939
10940 if (SINGLE_BYTE_CHAR_P (c))
10941 return (dp->contents[c]);
10942
10943 SPLIT_CHAR (c, code[0], code[1], code[2]);
10944 if (code[1] < 32)
10945 code[1] = -1;
10946 else if (code[2] < 32)
10947 code[2] = -1;
10948
10949 /* Here, the possible range of code[0] (== charset ID) is
10950 128..max_charset. Since the top level char table contains data
10951 for multibyte characters after 256th element, we must increment
10952 code[0] by 128 to get a correct index. */
10953 code[0] += 128;
10954 code[3] = -1; /* anchor */
10955
10956 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
10957 {
10958 val = dp->contents[code[i]];
10959 if (!SUB_CHAR_TABLE_P (val))
10960 return (NILP (val) ? dp->defalt : val);
10961 }
10962
10963 /* Here, val is a sub char table. We return the default value of
10964 it. */
10965 return (dp->defalt);
10966 }
10967
10968
10969 \f
10970 /***********************************************************************
10971 Window Redisplay
10972 ***********************************************************************/
10973
10974 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
10975
10976 static void
10977 redisplay_windows (window)
10978 Lisp_Object window;
10979 {
10980 while (!NILP (window))
10981 {
10982 struct window *w = XWINDOW (window);
10983
10984 if (!NILP (w->hchild))
10985 redisplay_windows (w->hchild);
10986 else if (!NILP (w->vchild))
10987 redisplay_windows (w->vchild);
10988 else
10989 {
10990 displayed_buffer = XBUFFER (w->buffer);
10991 /* Use list_of_error, not Qerror, so that
10992 we catch only errors and don't run the debugger. */
10993 internal_condition_case_1 (redisplay_window_0, window,
10994 list_of_error,
10995 redisplay_window_error);
10996 }
10997
10998 window = w->next;
10999 }
11000 }
11001
11002 static Lisp_Object
11003 redisplay_window_error ()
11004 {
11005 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11006 return Qnil;
11007 }
11008
11009 static Lisp_Object
11010 redisplay_window_0 (window)
11011 Lisp_Object window;
11012 {
11013 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11014 redisplay_window (window, 0);
11015 return Qnil;
11016 }
11017
11018 static Lisp_Object
11019 redisplay_window_1 (window)
11020 Lisp_Object window;
11021 {
11022 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11023 redisplay_window (window, 1);
11024 return Qnil;
11025 }
11026 \f
11027
11028 /* Increment GLYPH until it reaches END or CONDITION fails while
11029 adding (GLYPH)->pixel_width to X. */
11030
11031 #define SKIP_GLYPHS(glyph, end, x, condition) \
11032 do \
11033 { \
11034 (x) += (glyph)->pixel_width; \
11035 ++(glyph); \
11036 } \
11037 while ((glyph) < (end) && (condition))
11038
11039
11040 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11041 DELTA is the number of bytes by which positions recorded in ROW
11042 differ from current buffer positions. */
11043
11044 void
11045 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11046 struct window *w;
11047 struct glyph_row *row;
11048 struct glyph_matrix *matrix;
11049 int delta, delta_bytes, dy, dvpos;
11050 {
11051 struct glyph *glyph = row->glyphs[TEXT_AREA];
11052 struct glyph *end = glyph + row->used[TEXT_AREA];
11053 struct glyph *cursor = NULL;
11054 /* The first glyph that starts a sequence of glyphs from string. */
11055 struct glyph *string_start;
11056 /* The X coordinate of string_start. */
11057 int string_start_x;
11058 /* The last known character position. */
11059 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11060 /* The last known character position before string_start. */
11061 int string_before_pos;
11062 int x = row->x;
11063 int cursor_x = x;
11064 int cursor_from_overlay_pos = 0;
11065 int pt_old = PT - delta;
11066
11067 /* Skip over glyphs not having an object at the start of the row.
11068 These are special glyphs like truncation marks on terminal
11069 frames. */
11070 if (row->displays_text_p)
11071 while (glyph < end
11072 && INTEGERP (glyph->object)
11073 && glyph->charpos < 0)
11074 {
11075 x += glyph->pixel_width;
11076 ++glyph;
11077 }
11078
11079 string_start = NULL;
11080 while (glyph < end
11081 && !INTEGERP (glyph->object)
11082 && (!BUFFERP (glyph->object)
11083 || (last_pos = glyph->charpos) < pt_old))
11084 {
11085 if (! STRINGP (glyph->object))
11086 {
11087 string_start = NULL;
11088 x += glyph->pixel_width;
11089 ++glyph;
11090 if (cursor_from_overlay_pos
11091 && last_pos > cursor_from_overlay_pos)
11092 {
11093 cursor_from_overlay_pos = 0;
11094 cursor = 0;
11095 }
11096 }
11097 else
11098 {
11099 string_before_pos = last_pos;
11100 string_start = glyph;
11101 string_start_x = x;
11102 /* Skip all glyphs from string. */
11103 do
11104 {
11105 int pos;
11106 if ((cursor == NULL || glyph > cursor)
11107 && !NILP (Fget_char_property (make_number ((glyph)->charpos),
11108 Qcursor, (glyph)->object))
11109 && (pos = string_buffer_position (w, glyph->object,
11110 string_before_pos),
11111 (pos == 0 /* From overlay */
11112 || pos == pt_old)))
11113 {
11114 /* Estimate overlay buffer position from the buffer
11115 positions of the glyphs before and after the overlay.
11116 Add 1 to last_pos so that if point corresponds to the
11117 glyph right after the overlay, we still use a 'cursor'
11118 property found in that overlay. */
11119 cursor_from_overlay_pos = pos == 0 ? last_pos+1 : 0;
11120 cursor = glyph;
11121 cursor_x = x;
11122 }
11123 x += glyph->pixel_width;
11124 ++glyph;
11125 }
11126 while (glyph < end && STRINGP (glyph->object));
11127 }
11128 }
11129
11130 if (cursor != NULL)
11131 {
11132 glyph = cursor;
11133 x = cursor_x;
11134 }
11135 else if (row->ends_in_ellipsis_p && glyph == end)
11136 {
11137 /* Scan back over the ellipsis glyphs, decrementing positions. */
11138 while (glyph > row->glyphs[TEXT_AREA]
11139 && (glyph - 1)->charpos == last_pos)
11140 glyph--, x -= glyph->pixel_width;
11141 /* That loop always goes one position too far,
11142 including the glyph before the ellipsis.
11143 So scan forward over that one. */
11144 x += glyph->pixel_width;
11145 glyph++;
11146 }
11147 else if (string_start
11148 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11149 {
11150 /* We may have skipped over point because the previous glyphs
11151 are from string. As there's no easy way to know the
11152 character position of the current glyph, find the correct
11153 glyph on point by scanning from string_start again. */
11154 Lisp_Object limit;
11155 Lisp_Object string;
11156 int pos;
11157
11158 limit = make_number (pt_old + 1);
11159 end = glyph;
11160 glyph = string_start;
11161 x = string_start_x;
11162 string = glyph->object;
11163 pos = string_buffer_position (w, string, string_before_pos);
11164 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11165 because we always put cursor after overlay strings. */
11166 while (pos == 0 && glyph < end)
11167 {
11168 string = glyph->object;
11169 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11170 if (glyph < end)
11171 pos = string_buffer_position (w, glyph->object, string_before_pos);
11172 }
11173
11174 while (glyph < end)
11175 {
11176 pos = XINT (Fnext_single_char_property_change
11177 (make_number (pos), Qdisplay, Qnil, limit));
11178 if (pos > pt_old)
11179 break;
11180 /* Skip glyphs from the same string. */
11181 string = glyph->object;
11182 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11183 /* Skip glyphs from an overlay. */
11184 while (glyph < end
11185 && ! string_buffer_position (w, glyph->object, pos))
11186 {
11187 string = glyph->object;
11188 SKIP_GLYPHS (glyph, end, x, EQ (glyph->object, string));
11189 }
11190 }
11191 }
11192
11193 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11194 w->cursor.x = x;
11195 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11196 w->cursor.y = row->y + dy;
11197
11198 if (w == XWINDOW (selected_window))
11199 {
11200 if (!row->continued_p
11201 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11202 && row->x == 0)
11203 {
11204 this_line_buffer = XBUFFER (w->buffer);
11205
11206 CHARPOS (this_line_start_pos)
11207 = MATRIX_ROW_START_CHARPOS (row) + delta;
11208 BYTEPOS (this_line_start_pos)
11209 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11210
11211 CHARPOS (this_line_end_pos)
11212 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11213 BYTEPOS (this_line_end_pos)
11214 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11215
11216 this_line_y = w->cursor.y;
11217 this_line_pixel_height = row->height;
11218 this_line_vpos = w->cursor.vpos;
11219 this_line_start_x = row->x;
11220 }
11221 else
11222 CHARPOS (this_line_start_pos) = 0;
11223 }
11224 }
11225
11226
11227 /* Run window scroll functions, if any, for WINDOW with new window
11228 start STARTP. Sets the window start of WINDOW to that position.
11229
11230 We assume that the window's buffer is really current. */
11231
11232 static INLINE struct text_pos
11233 run_window_scroll_functions (window, startp)
11234 Lisp_Object window;
11235 struct text_pos startp;
11236 {
11237 struct window *w = XWINDOW (window);
11238 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11239
11240 if (current_buffer != XBUFFER (w->buffer))
11241 abort ();
11242
11243 if (!NILP (Vwindow_scroll_functions))
11244 {
11245 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11246 make_number (CHARPOS (startp)));
11247 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11248 /* In case the hook functions switch buffers. */
11249 if (current_buffer != XBUFFER (w->buffer))
11250 set_buffer_internal_1 (XBUFFER (w->buffer));
11251 }
11252
11253 return startp;
11254 }
11255
11256
11257 /* Make sure the line containing the cursor is fully visible.
11258 A value of 1 means there is nothing to be done.
11259 (Either the line is fully visible, or it cannot be made so,
11260 or we cannot tell.)
11261
11262 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11263 is higher than window.
11264
11265 A value of 0 means the caller should do scrolling
11266 as if point had gone off the screen. */
11267
11268 static int
11269 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11270 struct window *w;
11271 int force_p;
11272 {
11273 struct glyph_matrix *matrix;
11274 struct glyph_row *row;
11275 int window_height;
11276
11277 if (!make_cursor_line_fully_visible_p)
11278 return 1;
11279
11280 /* It's not always possible to find the cursor, e.g, when a window
11281 is full of overlay strings. Don't do anything in that case. */
11282 if (w->cursor.vpos < 0)
11283 return 1;
11284
11285 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
11286 row = MATRIX_ROW (matrix, w->cursor.vpos);
11287
11288 /* If the cursor row is not partially visible, there's nothing to do. */
11289 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
11290 return 1;
11291
11292 /* If the row the cursor is in is taller than the window's height,
11293 it's not clear what to do, so do nothing. */
11294 window_height = window_box_height (w);
11295 if (row->height >= window_height)
11296 {
11297 if (!force_p || w->vscroll)
11298 return 1;
11299 }
11300 return 0;
11301
11302 #if 0
11303 /* This code used to try to scroll the window just enough to make
11304 the line visible. It returned 0 to say that the caller should
11305 allocate larger glyph matrices. */
11306
11307 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
11308 {
11309 int dy = row->height - row->visible_height;
11310 w->vscroll = 0;
11311 w->cursor.y += dy;
11312 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11313 }
11314 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
11315 {
11316 int dy = - (row->height - row->visible_height);
11317 w->vscroll = dy;
11318 w->cursor.y += dy;
11319 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
11320 }
11321
11322 /* When we change the cursor y-position of the selected window,
11323 change this_line_y as well so that the display optimization for
11324 the cursor line of the selected window in redisplay_internal uses
11325 the correct y-position. */
11326 if (w == XWINDOW (selected_window))
11327 this_line_y = w->cursor.y;
11328
11329 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
11330 redisplay with larger matrices. */
11331 if (matrix->nrows < required_matrix_height (w))
11332 {
11333 fonts_changed_p = 1;
11334 return 0;
11335 }
11336
11337 return 1;
11338 #endif /* 0 */
11339 }
11340
11341
11342 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
11343 non-zero means only WINDOW is redisplayed in redisplay_internal.
11344 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
11345 in redisplay_window to bring a partially visible line into view in
11346 the case that only the cursor has moved.
11347
11348 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
11349 last screen line's vertical height extends past the end of the screen.
11350
11351 Value is
11352
11353 1 if scrolling succeeded
11354
11355 0 if scrolling didn't find point.
11356
11357 -1 if new fonts have been loaded so that we must interrupt
11358 redisplay, adjust glyph matrices, and try again. */
11359
11360 enum
11361 {
11362 SCROLLING_SUCCESS,
11363 SCROLLING_FAILED,
11364 SCROLLING_NEED_LARGER_MATRICES
11365 };
11366
11367 static int
11368 try_scrolling (window, just_this_one_p, scroll_conservatively,
11369 scroll_step, temp_scroll_step, last_line_misfit)
11370 Lisp_Object window;
11371 int just_this_one_p;
11372 EMACS_INT scroll_conservatively, scroll_step;
11373 int temp_scroll_step;
11374 int last_line_misfit;
11375 {
11376 struct window *w = XWINDOW (window);
11377 struct frame *f = XFRAME (w->frame);
11378 struct text_pos scroll_margin_pos;
11379 struct text_pos pos;
11380 struct text_pos startp;
11381 struct it it;
11382 Lisp_Object window_end;
11383 int this_scroll_margin;
11384 int dy = 0;
11385 int scroll_max;
11386 int rc;
11387 int amount_to_scroll = 0;
11388 Lisp_Object aggressive;
11389 int height;
11390 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
11391
11392 #if GLYPH_DEBUG
11393 debug_method_add (w, "try_scrolling");
11394 #endif
11395
11396 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11397
11398 /* Compute scroll margin height in pixels. We scroll when point is
11399 within this distance from the top or bottom of the window. */
11400 if (scroll_margin > 0)
11401 {
11402 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11403 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11404 }
11405 else
11406 this_scroll_margin = 0;
11407
11408 /* Force scroll_conservatively to have a reasonable value so it doesn't
11409 cause an overflow while computing how much to scroll. */
11410 if (scroll_conservatively)
11411 scroll_conservatively = min (scroll_conservatively,
11412 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
11413
11414 /* Compute how much we should try to scroll maximally to bring point
11415 into view. */
11416 if (scroll_step || scroll_conservatively || temp_scroll_step)
11417 scroll_max = max (scroll_step,
11418 max (scroll_conservatively, temp_scroll_step));
11419 else if (NUMBERP (current_buffer->scroll_down_aggressively)
11420 || NUMBERP (current_buffer->scroll_up_aggressively))
11421 /* We're trying to scroll because of aggressive scrolling
11422 but no scroll_step is set. Choose an arbitrary one. Maybe
11423 there should be a variable for this. */
11424 scroll_max = 10;
11425 else
11426 scroll_max = 0;
11427 scroll_max *= FRAME_LINE_HEIGHT (f);
11428
11429 /* Decide whether we have to scroll down. Start at the window end
11430 and move this_scroll_margin up to find the position of the scroll
11431 margin. */
11432 window_end = Fwindow_end (window, Qt);
11433
11434 too_near_end:
11435
11436 CHARPOS (scroll_margin_pos) = XINT (window_end);
11437 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
11438
11439 if (this_scroll_margin || extra_scroll_margin_lines)
11440 {
11441 start_display (&it, w, scroll_margin_pos);
11442 if (this_scroll_margin)
11443 move_it_vertically_backward (&it, this_scroll_margin);
11444 if (extra_scroll_margin_lines)
11445 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
11446 scroll_margin_pos = it.current.pos;
11447 }
11448
11449 if (PT >= CHARPOS (scroll_margin_pos))
11450 {
11451 int y0;
11452
11453 /* Point is in the scroll margin at the bottom of the window, or
11454 below. Compute a new window start that makes point visible. */
11455
11456 /* Compute the distance from the scroll margin to PT.
11457 Give up if the distance is greater than scroll_max. */
11458 start_display (&it, w, scroll_margin_pos);
11459 y0 = it.current_y;
11460 move_it_to (&it, PT, 0, it.last_visible_y, -1,
11461 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11462
11463 /* To make point visible, we have to move the window start
11464 down so that the line the cursor is in is visible, which
11465 means we have to add in the height of the cursor line. */
11466 dy = line_bottom_y (&it) - y0;
11467
11468 if (dy > scroll_max)
11469 return SCROLLING_FAILED;
11470
11471 /* Move the window start down. If scrolling conservatively,
11472 move it just enough down to make point visible. If
11473 scroll_step is set, move it down by scroll_step. */
11474 start_display (&it, w, startp);
11475
11476 if (scroll_conservatively)
11477 /* Set AMOUNT_TO_SCROLL to at least one line,
11478 and at most scroll_conservatively lines. */
11479 amount_to_scroll
11480 = min (max (dy, FRAME_LINE_HEIGHT (f)),
11481 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
11482 else if (scroll_step || temp_scroll_step)
11483 amount_to_scroll = scroll_max;
11484 else
11485 {
11486 aggressive = current_buffer->scroll_up_aggressively;
11487 height = WINDOW_BOX_TEXT_HEIGHT (w);
11488 if (NUMBERP (aggressive))
11489 {
11490 double float_amount = XFLOATINT (aggressive) * height;
11491 amount_to_scroll = float_amount;
11492 if (amount_to_scroll == 0 && float_amount > 0)
11493 amount_to_scroll = 1;
11494 }
11495 }
11496
11497 if (amount_to_scroll <= 0)
11498 return SCROLLING_FAILED;
11499
11500 /* If moving by amount_to_scroll leaves STARTP unchanged,
11501 move it down one screen line. */
11502
11503 move_it_vertically (&it, amount_to_scroll);
11504 if (CHARPOS (it.current.pos) == CHARPOS (startp))
11505 move_it_by_lines (&it, 1, 1);
11506 startp = it.current.pos;
11507 }
11508 else
11509 {
11510 /* See if point is inside the scroll margin at the top of the
11511 window. */
11512 scroll_margin_pos = startp;
11513 if (this_scroll_margin)
11514 {
11515 start_display (&it, w, startp);
11516 move_it_vertically (&it, this_scroll_margin);
11517 scroll_margin_pos = it.current.pos;
11518 }
11519
11520 if (PT < CHARPOS (scroll_margin_pos))
11521 {
11522 /* Point is in the scroll margin at the top of the window or
11523 above what is displayed in the window. */
11524 int y0;
11525
11526 /* Compute the vertical distance from PT to the scroll
11527 margin position. Give up if distance is greater than
11528 scroll_max. */
11529 SET_TEXT_POS (pos, PT, PT_BYTE);
11530 start_display (&it, w, pos);
11531 y0 = it.current_y;
11532 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
11533 it.last_visible_y, -1,
11534 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
11535 dy = it.current_y - y0;
11536 if (dy > scroll_max)
11537 return SCROLLING_FAILED;
11538
11539 /* Compute new window start. */
11540 start_display (&it, w, startp);
11541
11542 if (scroll_conservatively)
11543 amount_to_scroll
11544 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
11545 else if (scroll_step || temp_scroll_step)
11546 amount_to_scroll = scroll_max;
11547 else
11548 {
11549 aggressive = current_buffer->scroll_down_aggressively;
11550 height = WINDOW_BOX_TEXT_HEIGHT (w);
11551 if (NUMBERP (aggressive))
11552 {
11553 double float_amount = XFLOATINT (aggressive) * height;
11554 amount_to_scroll = float_amount;
11555 if (amount_to_scroll == 0 && float_amount > 0)
11556 amount_to_scroll = 1;
11557 }
11558 }
11559
11560 if (amount_to_scroll <= 0)
11561 return SCROLLING_FAILED;
11562
11563 move_it_vertically_backward (&it, amount_to_scroll);
11564 startp = it.current.pos;
11565 }
11566 }
11567
11568 /* Run window scroll functions. */
11569 startp = run_window_scroll_functions (window, startp);
11570
11571 /* Display the window. Give up if new fonts are loaded, or if point
11572 doesn't appear. */
11573 if (!try_window (window, startp, 0))
11574 rc = SCROLLING_NEED_LARGER_MATRICES;
11575 else if (w->cursor.vpos < 0)
11576 {
11577 clear_glyph_matrix (w->desired_matrix);
11578 rc = SCROLLING_FAILED;
11579 }
11580 else
11581 {
11582 /* Maybe forget recorded base line for line number display. */
11583 if (!just_this_one_p
11584 || current_buffer->clip_changed
11585 || BEG_UNCHANGED < CHARPOS (startp))
11586 w->base_line_number = Qnil;
11587
11588 /* If cursor ends up on a partially visible line,
11589 treat that as being off the bottom of the screen. */
11590 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
11591 {
11592 clear_glyph_matrix (w->desired_matrix);
11593 ++extra_scroll_margin_lines;
11594 goto too_near_end;
11595 }
11596 rc = SCROLLING_SUCCESS;
11597 }
11598
11599 return rc;
11600 }
11601
11602
11603 /* Compute a suitable window start for window W if display of W starts
11604 on a continuation line. Value is non-zero if a new window start
11605 was computed.
11606
11607 The new window start will be computed, based on W's width, starting
11608 from the start of the continued line. It is the start of the
11609 screen line with the minimum distance from the old start W->start. */
11610
11611 static int
11612 compute_window_start_on_continuation_line (w)
11613 struct window *w;
11614 {
11615 struct text_pos pos, start_pos;
11616 int window_start_changed_p = 0;
11617
11618 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
11619
11620 /* If window start is on a continuation line... Window start may be
11621 < BEGV in case there's invisible text at the start of the
11622 buffer (M-x rmail, for example). */
11623 if (CHARPOS (start_pos) > BEGV
11624 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
11625 {
11626 struct it it;
11627 struct glyph_row *row;
11628
11629 /* Handle the case that the window start is out of range. */
11630 if (CHARPOS (start_pos) < BEGV)
11631 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
11632 else if (CHARPOS (start_pos) > ZV)
11633 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
11634
11635 /* Find the start of the continued line. This should be fast
11636 because scan_buffer is fast (newline cache). */
11637 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
11638 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
11639 row, DEFAULT_FACE_ID);
11640 reseat_at_previous_visible_line_start (&it);
11641
11642 /* If the line start is "too far" away from the window start,
11643 say it takes too much time to compute a new window start. */
11644 if (CHARPOS (start_pos) - IT_CHARPOS (it)
11645 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
11646 {
11647 int min_distance, distance;
11648
11649 /* Move forward by display lines to find the new window
11650 start. If window width was enlarged, the new start can
11651 be expected to be > the old start. If window width was
11652 decreased, the new window start will be < the old start.
11653 So, we're looking for the display line start with the
11654 minimum distance from the old window start. */
11655 pos = it.current.pos;
11656 min_distance = INFINITY;
11657 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
11658 distance < min_distance)
11659 {
11660 min_distance = distance;
11661 pos = it.current.pos;
11662 move_it_by_lines (&it, 1, 0);
11663 }
11664
11665 /* Set the window start there. */
11666 SET_MARKER_FROM_TEXT_POS (w->start, pos);
11667 window_start_changed_p = 1;
11668 }
11669 }
11670
11671 return window_start_changed_p;
11672 }
11673
11674
11675 /* Try cursor movement in case text has not changed in window WINDOW,
11676 with window start STARTP. Value is
11677
11678 CURSOR_MOVEMENT_SUCCESS if successful
11679
11680 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
11681
11682 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
11683 display. *SCROLL_STEP is set to 1, under certain circumstances, if
11684 we want to scroll as if scroll-step were set to 1. See the code.
11685
11686 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
11687 which case we have to abort this redisplay, and adjust matrices
11688 first. */
11689
11690 enum
11691 {
11692 CURSOR_MOVEMENT_SUCCESS,
11693 CURSOR_MOVEMENT_CANNOT_BE_USED,
11694 CURSOR_MOVEMENT_MUST_SCROLL,
11695 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
11696 };
11697
11698 static int
11699 try_cursor_movement (window, startp, scroll_step)
11700 Lisp_Object window;
11701 struct text_pos startp;
11702 int *scroll_step;
11703 {
11704 struct window *w = XWINDOW (window);
11705 struct frame *f = XFRAME (w->frame);
11706 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
11707
11708 #if GLYPH_DEBUG
11709 if (inhibit_try_cursor_movement)
11710 return rc;
11711 #endif
11712
11713 /* Handle case where text has not changed, only point, and it has
11714 not moved off the frame. */
11715 if (/* Point may be in this window. */
11716 PT >= CHARPOS (startp)
11717 /* Selective display hasn't changed. */
11718 && !current_buffer->clip_changed
11719 /* Function force-mode-line-update is used to force a thorough
11720 redisplay. It sets either windows_or_buffers_changed or
11721 update_mode_lines. So don't take a shortcut here for these
11722 cases. */
11723 && !update_mode_lines
11724 && !windows_or_buffers_changed
11725 && !cursor_type_changed
11726 /* Can't use this case if highlighting a region. When a
11727 region exists, cursor movement has to do more than just
11728 set the cursor. */
11729 && !(!NILP (Vtransient_mark_mode)
11730 && !NILP (current_buffer->mark_active))
11731 && NILP (w->region_showing)
11732 && NILP (Vshow_trailing_whitespace)
11733 /* Right after splitting windows, last_point may be nil. */
11734 && INTEGERP (w->last_point)
11735 /* This code is not used for mini-buffer for the sake of the case
11736 of redisplaying to replace an echo area message; since in
11737 that case the mini-buffer contents per se are usually
11738 unchanged. This code is of no real use in the mini-buffer
11739 since the handling of this_line_start_pos, etc., in redisplay
11740 handles the same cases. */
11741 && !EQ (window, minibuf_window)
11742 /* When splitting windows or for new windows, it happens that
11743 redisplay is called with a nil window_end_vpos or one being
11744 larger than the window. This should really be fixed in
11745 window.c. I don't have this on my list, now, so we do
11746 approximately the same as the old redisplay code. --gerd. */
11747 && INTEGERP (w->window_end_vpos)
11748 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
11749 && (FRAME_WINDOW_P (f)
11750 || !overlay_arrow_in_current_buffer_p ()))
11751 {
11752 int this_scroll_margin, top_scroll_margin;
11753 struct glyph_row *row = NULL;
11754
11755 #if GLYPH_DEBUG
11756 debug_method_add (w, "cursor movement");
11757 #endif
11758
11759 /* Scroll if point within this distance from the top or bottom
11760 of the window. This is a pixel value. */
11761 this_scroll_margin = max (0, scroll_margin);
11762 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
11763 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
11764
11765 top_scroll_margin = this_scroll_margin;
11766 if (WINDOW_WANTS_HEADER_LINE_P (w))
11767 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
11768
11769 /* Start with the row the cursor was displayed during the last
11770 not paused redisplay. Give up if that row is not valid. */
11771 if (w->last_cursor.vpos < 0
11772 || w->last_cursor.vpos >= w->current_matrix->nrows)
11773 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11774 else
11775 {
11776 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
11777 if (row->mode_line_p)
11778 ++row;
11779 if (!row->enabled_p)
11780 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11781 }
11782
11783 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
11784 {
11785 int scroll_p = 0;
11786 int last_y = window_text_bottom_y (w) - this_scroll_margin;
11787
11788 if (PT > XFASTINT (w->last_point))
11789 {
11790 /* Point has moved forward. */
11791 while (MATRIX_ROW_END_CHARPOS (row) < PT
11792 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
11793 {
11794 xassert (row->enabled_p);
11795 ++row;
11796 }
11797
11798 /* The end position of a row equals the start position
11799 of the next row. If PT is there, we would rather
11800 display it in the next line. */
11801 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11802 && MATRIX_ROW_END_CHARPOS (row) == PT
11803 && !cursor_row_p (w, row))
11804 ++row;
11805
11806 /* If within the scroll margin, scroll. Note that
11807 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
11808 the next line would be drawn, and that
11809 this_scroll_margin can be zero. */
11810 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
11811 || PT > MATRIX_ROW_END_CHARPOS (row)
11812 /* Line is completely visible last line in window
11813 and PT is to be set in the next line. */
11814 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
11815 && PT == MATRIX_ROW_END_CHARPOS (row)
11816 && !row->ends_at_zv_p
11817 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
11818 scroll_p = 1;
11819 }
11820 else if (PT < XFASTINT (w->last_point))
11821 {
11822 /* Cursor has to be moved backward. Note that PT >=
11823 CHARPOS (startp) because of the outer if-statement. */
11824 while (!row->mode_line_p
11825 && (MATRIX_ROW_START_CHARPOS (row) > PT
11826 || (MATRIX_ROW_START_CHARPOS (row) == PT
11827 && MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)))
11828 && (row->y > top_scroll_margin
11829 || CHARPOS (startp) == BEGV))
11830 {
11831 xassert (row->enabled_p);
11832 --row;
11833 }
11834
11835 /* Consider the following case: Window starts at BEGV,
11836 there is invisible, intangible text at BEGV, so that
11837 display starts at some point START > BEGV. It can
11838 happen that we are called with PT somewhere between
11839 BEGV and START. Try to handle that case. */
11840 if (row < w->current_matrix->rows
11841 || row->mode_line_p)
11842 {
11843 row = w->current_matrix->rows;
11844 if (row->mode_line_p)
11845 ++row;
11846 }
11847
11848 /* Due to newlines in overlay strings, we may have to
11849 skip forward over overlay strings. */
11850 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
11851 && MATRIX_ROW_END_CHARPOS (row) == PT
11852 && !cursor_row_p (w, row))
11853 ++row;
11854
11855 /* If within the scroll margin, scroll. */
11856 if (row->y < top_scroll_margin
11857 && CHARPOS (startp) != BEGV)
11858 scroll_p = 1;
11859 }
11860 else
11861 {
11862 /* Cursor did not move. So don't scroll even if cursor line
11863 is partially visible, as it was so before. */
11864 rc = CURSOR_MOVEMENT_SUCCESS;
11865 }
11866
11867 if (PT < MATRIX_ROW_START_CHARPOS (row)
11868 || PT > MATRIX_ROW_END_CHARPOS (row))
11869 {
11870 /* if PT is not in the glyph row, give up. */
11871 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11872 }
11873 else if (rc != CURSOR_MOVEMENT_SUCCESS
11874 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
11875 && make_cursor_line_fully_visible_p)
11876 {
11877 if (PT == MATRIX_ROW_END_CHARPOS (row)
11878 && !row->ends_at_zv_p
11879 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
11880 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11881 else if (row->height > window_box_height (w))
11882 {
11883 /* If we end up in a partially visible line, let's
11884 make it fully visible, except when it's taller
11885 than the window, in which case we can't do much
11886 about it. */
11887 *scroll_step = 1;
11888 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11889 }
11890 else
11891 {
11892 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11893 if (!cursor_row_fully_visible_p (w, 0, 1))
11894 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11895 else
11896 rc = CURSOR_MOVEMENT_SUCCESS;
11897 }
11898 }
11899 else if (scroll_p)
11900 rc = CURSOR_MOVEMENT_MUST_SCROLL;
11901 else
11902 {
11903 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11904 rc = CURSOR_MOVEMENT_SUCCESS;
11905 }
11906 }
11907 }
11908
11909 return rc;
11910 }
11911
11912 void
11913 set_vertical_scroll_bar (w)
11914 struct window *w;
11915 {
11916 int start, end, whole;
11917
11918 /* Calculate the start and end positions for the current window.
11919 At some point, it would be nice to choose between scrollbars
11920 which reflect the whole buffer size, with special markers
11921 indicating narrowing, and scrollbars which reflect only the
11922 visible region.
11923
11924 Note that mini-buffers sometimes aren't displaying any text. */
11925 if (!MINI_WINDOW_P (w)
11926 || (w == XWINDOW (minibuf_window)
11927 && NILP (echo_area_buffer[0])))
11928 {
11929 struct buffer *buf = XBUFFER (w->buffer);
11930 whole = BUF_ZV (buf) - BUF_BEGV (buf);
11931 start = marker_position (w->start) - BUF_BEGV (buf);
11932 /* I don't think this is guaranteed to be right. For the
11933 moment, we'll pretend it is. */
11934 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
11935
11936 if (end < start)
11937 end = start;
11938 if (whole < (end - start))
11939 whole = end - start;
11940 }
11941 else
11942 start = end = whole = 0;
11943
11944 /* Indicate what this scroll bar ought to be displaying now. */
11945 set_vertical_scroll_bar_hook (w, end - start, whole, start);
11946 }
11947
11948
11949 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
11950 selected_window is redisplayed.
11951
11952 We can return without actually redisplaying the window if
11953 fonts_changed_p is nonzero. In that case, redisplay_internal will
11954 retry. */
11955
11956 static void
11957 redisplay_window (window, just_this_one_p)
11958 Lisp_Object window;
11959 int just_this_one_p;
11960 {
11961 struct window *w = XWINDOW (window);
11962 struct frame *f = XFRAME (w->frame);
11963 struct buffer *buffer = XBUFFER (w->buffer);
11964 struct buffer *old = current_buffer;
11965 struct text_pos lpoint, opoint, startp;
11966 int update_mode_line;
11967 int tem;
11968 struct it it;
11969 /* Record it now because it's overwritten. */
11970 int current_matrix_up_to_date_p = 0;
11971 int used_current_matrix_p = 0;
11972 /* This is less strict than current_matrix_up_to_date_p.
11973 It indictes that the buffer contents and narrowing are unchanged. */
11974 int buffer_unchanged_p = 0;
11975 int temp_scroll_step = 0;
11976 int count = SPECPDL_INDEX ();
11977 int rc;
11978 int centering_position = -1;
11979 int last_line_misfit = 0;
11980
11981 SET_TEXT_POS (lpoint, PT, PT_BYTE);
11982 opoint = lpoint;
11983
11984 /* W must be a leaf window here. */
11985 xassert (!NILP (w->buffer));
11986 #if GLYPH_DEBUG
11987 *w->desired_matrix->method = 0;
11988 #endif
11989
11990 specbind (Qinhibit_point_motion_hooks, Qt);
11991
11992 reconsider_clip_changes (w, buffer);
11993
11994 /* Has the mode line to be updated? */
11995 update_mode_line = (!NILP (w->update_mode_line)
11996 || update_mode_lines
11997 || buffer->clip_changed
11998 || buffer->prevent_redisplay_optimizations_p);
11999
12000 if (MINI_WINDOW_P (w))
12001 {
12002 if (w == XWINDOW (echo_area_window)
12003 && !NILP (echo_area_buffer[0]))
12004 {
12005 if (update_mode_line)
12006 /* We may have to update a tty frame's menu bar or a
12007 tool-bar. Example `M-x C-h C-h C-g'. */
12008 goto finish_menu_bars;
12009 else
12010 /* We've already displayed the echo area glyphs in this window. */
12011 goto finish_scroll_bars;
12012 }
12013 else if ((w != XWINDOW (minibuf_window)
12014 || minibuf_level == 0)
12015 /* When buffer is nonempty, redisplay window normally. */
12016 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12017 /* Quail displays non-mini buffers in minibuffer window.
12018 In that case, redisplay the window normally. */
12019 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12020 {
12021 /* W is a mini-buffer window, but it's not active, so clear
12022 it. */
12023 int yb = window_text_bottom_y (w);
12024 struct glyph_row *row;
12025 int y;
12026
12027 for (y = 0, row = w->desired_matrix->rows;
12028 y < yb;
12029 y += row->height, ++row)
12030 blank_row (w, row, y);
12031 goto finish_scroll_bars;
12032 }
12033
12034 clear_glyph_matrix (w->desired_matrix);
12035 }
12036
12037 /* Otherwise set up data on this window; select its buffer and point
12038 value. */
12039 /* Really select the buffer, for the sake of buffer-local
12040 variables. */
12041 set_buffer_internal_1 (XBUFFER (w->buffer));
12042 SET_TEXT_POS (opoint, PT, PT_BYTE);
12043
12044 current_matrix_up_to_date_p
12045 = (!NILP (w->window_end_valid)
12046 && !current_buffer->clip_changed
12047 && !current_buffer->prevent_redisplay_optimizations_p
12048 && XFASTINT (w->last_modified) >= MODIFF
12049 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12050
12051 buffer_unchanged_p
12052 = (!NILP (w->window_end_valid)
12053 && !current_buffer->clip_changed
12054 && XFASTINT (w->last_modified) >= MODIFF
12055 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12056
12057 /* When windows_or_buffers_changed is non-zero, we can't rely on
12058 the window end being valid, so set it to nil there. */
12059 if (windows_or_buffers_changed)
12060 {
12061 /* If window starts on a continuation line, maybe adjust the
12062 window start in case the window's width changed. */
12063 if (XMARKER (w->start)->buffer == current_buffer)
12064 compute_window_start_on_continuation_line (w);
12065
12066 w->window_end_valid = Qnil;
12067 }
12068
12069 /* Some sanity checks. */
12070 CHECK_WINDOW_END (w);
12071 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12072 abort ();
12073 if (BYTEPOS (opoint) < CHARPOS (opoint))
12074 abort ();
12075
12076 /* If %c is in mode line, update it if needed. */
12077 if (!NILP (w->column_number_displayed)
12078 /* This alternative quickly identifies a common case
12079 where no change is needed. */
12080 && !(PT == XFASTINT (w->last_point)
12081 && XFASTINT (w->last_modified) >= MODIFF
12082 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12083 && (XFASTINT (w->column_number_displayed)
12084 != (int) current_column ())) /* iftc */
12085 update_mode_line = 1;
12086
12087 /* Count number of windows showing the selected buffer. An indirect
12088 buffer counts as its base buffer. */
12089 if (!just_this_one_p)
12090 {
12091 struct buffer *current_base, *window_base;
12092 current_base = current_buffer;
12093 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12094 if (current_base->base_buffer)
12095 current_base = current_base->base_buffer;
12096 if (window_base->base_buffer)
12097 window_base = window_base->base_buffer;
12098 if (current_base == window_base)
12099 buffer_shared++;
12100 }
12101
12102 /* Point refers normally to the selected window. For any other
12103 window, set up appropriate value. */
12104 if (!EQ (window, selected_window))
12105 {
12106 int new_pt = XMARKER (w->pointm)->charpos;
12107 int new_pt_byte = marker_byte_position (w->pointm);
12108 if (new_pt < BEGV)
12109 {
12110 new_pt = BEGV;
12111 new_pt_byte = BEGV_BYTE;
12112 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12113 }
12114 else if (new_pt > (ZV - 1))
12115 {
12116 new_pt = ZV;
12117 new_pt_byte = ZV_BYTE;
12118 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12119 }
12120
12121 /* We don't use SET_PT so that the point-motion hooks don't run. */
12122 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12123 }
12124
12125 /* If any of the character widths specified in the display table
12126 have changed, invalidate the width run cache. It's true that
12127 this may be a bit late to catch such changes, but the rest of
12128 redisplay goes (non-fatally) haywire when the display table is
12129 changed, so why should we worry about doing any better? */
12130 if (current_buffer->width_run_cache)
12131 {
12132 struct Lisp_Char_Table *disptab = buffer_display_table ();
12133
12134 if (! disptab_matches_widthtab (disptab,
12135 XVECTOR (current_buffer->width_table)))
12136 {
12137 invalidate_region_cache (current_buffer,
12138 current_buffer->width_run_cache,
12139 BEG, Z);
12140 recompute_width_table (current_buffer, disptab);
12141 }
12142 }
12143
12144 /* If window-start is screwed up, choose a new one. */
12145 if (XMARKER (w->start)->buffer != current_buffer)
12146 goto recenter;
12147
12148 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12149
12150 /* If someone specified a new starting point but did not insist,
12151 check whether it can be used. */
12152 if (!NILP (w->optional_new_start)
12153 && CHARPOS (startp) >= BEGV
12154 && CHARPOS (startp) <= ZV)
12155 {
12156 w->optional_new_start = Qnil;
12157 start_display (&it, w, startp);
12158 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12159 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12160 if (IT_CHARPOS (it) == PT)
12161 w->force_start = Qt;
12162 /* IT may overshoot PT if text at PT is invisible. */
12163 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12164 w->force_start = Qt;
12165
12166
12167 }
12168
12169 /* Handle case where place to start displaying has been specified,
12170 unless the specified location is outside the accessible range. */
12171 if (!NILP (w->force_start)
12172 || w->frozen_window_start_p)
12173 {
12174 /* We set this later on if we have to adjust point. */
12175 int new_vpos = -1;
12176 int val;
12177
12178 w->force_start = Qnil;
12179 w->vscroll = 0;
12180 w->window_end_valid = Qnil;
12181
12182 /* Forget any recorded base line for line number display. */
12183 if (!buffer_unchanged_p)
12184 w->base_line_number = Qnil;
12185
12186 /* Redisplay the mode line. Select the buffer properly for that.
12187 Also, run the hook window-scroll-functions
12188 because we have scrolled. */
12189 /* Note, we do this after clearing force_start because
12190 if there's an error, it is better to forget about force_start
12191 than to get into an infinite loop calling the hook functions
12192 and having them get more errors. */
12193 if (!update_mode_line
12194 || ! NILP (Vwindow_scroll_functions))
12195 {
12196 update_mode_line = 1;
12197 w->update_mode_line = Qt;
12198 startp = run_window_scroll_functions (window, startp);
12199 }
12200
12201 w->last_modified = make_number (0);
12202 w->last_overlay_modified = make_number (0);
12203 if (CHARPOS (startp) < BEGV)
12204 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12205 else if (CHARPOS (startp) > ZV)
12206 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12207
12208 /* Redisplay, then check if cursor has been set during the
12209 redisplay. Give up if new fonts were loaded. */
12210 val = try_window (window, startp, 1);
12211 if (!val)
12212 {
12213 w->force_start = Qt;
12214 clear_glyph_matrix (w->desired_matrix);
12215 goto need_larger_matrices;
12216 }
12217 /* Point was outside the scroll margins. */
12218 if (val < 0)
12219 new_vpos = window_box_height (w) / 2;
12220
12221 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12222 {
12223 /* If point does not appear, try to move point so it does
12224 appear. The desired matrix has been built above, so we
12225 can use it here. */
12226 new_vpos = window_box_height (w) / 2;
12227 }
12228
12229 if (!cursor_row_fully_visible_p (w, 0, 0))
12230 {
12231 /* Point does appear, but on a line partly visible at end of window.
12232 Move it back to a fully-visible line. */
12233 new_vpos = window_box_height (w);
12234 }
12235
12236 /* If we need to move point for either of the above reasons,
12237 now actually do it. */
12238 if (new_vpos >= 0)
12239 {
12240 struct glyph_row *row;
12241
12242 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12243 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12244 ++row;
12245
12246 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12247 MATRIX_ROW_START_BYTEPOS (row));
12248
12249 if (w != XWINDOW (selected_window))
12250 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12251 else if (current_buffer == old)
12252 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12253
12254 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12255
12256 /* If we are highlighting the region, then we just changed
12257 the region, so redisplay to show it. */
12258 if (!NILP (Vtransient_mark_mode)
12259 && !NILP (current_buffer->mark_active))
12260 {
12261 clear_glyph_matrix (w->desired_matrix);
12262 if (!try_window (window, startp, 0))
12263 goto need_larger_matrices;
12264 }
12265 }
12266
12267 #if GLYPH_DEBUG
12268 debug_method_add (w, "forced window start");
12269 #endif
12270 goto done;
12271 }
12272
12273 /* Handle case where text has not changed, only point, and it has
12274 not moved off the frame, and we are not retrying after hscroll.
12275 (current_matrix_up_to_date_p is nonzero when retrying.) */
12276 if (current_matrix_up_to_date_p
12277 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
12278 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
12279 {
12280 switch (rc)
12281 {
12282 case CURSOR_MOVEMENT_SUCCESS:
12283 used_current_matrix_p = 1;
12284 goto done;
12285
12286 #if 0 /* try_cursor_movement never returns this value. */
12287 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
12288 goto need_larger_matrices;
12289 #endif
12290
12291 case CURSOR_MOVEMENT_MUST_SCROLL:
12292 goto try_to_scroll;
12293
12294 default:
12295 abort ();
12296 }
12297 }
12298 /* If current starting point was originally the beginning of a line
12299 but no longer is, find a new starting point. */
12300 else if (!NILP (w->start_at_line_beg)
12301 && !(CHARPOS (startp) <= BEGV
12302 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
12303 {
12304 #if GLYPH_DEBUG
12305 debug_method_add (w, "recenter 1");
12306 #endif
12307 goto recenter;
12308 }
12309
12310 /* Try scrolling with try_window_id. Value is > 0 if update has
12311 been done, it is -1 if we know that the same window start will
12312 not work. It is 0 if unsuccessful for some other reason. */
12313 else if ((tem = try_window_id (w)) != 0)
12314 {
12315 #if GLYPH_DEBUG
12316 debug_method_add (w, "try_window_id %d", tem);
12317 #endif
12318
12319 if (fonts_changed_p)
12320 goto need_larger_matrices;
12321 if (tem > 0)
12322 goto done;
12323
12324 /* Otherwise try_window_id has returned -1 which means that we
12325 don't want the alternative below this comment to execute. */
12326 }
12327 else if (CHARPOS (startp) >= BEGV
12328 && CHARPOS (startp) <= ZV
12329 && PT >= CHARPOS (startp)
12330 && (CHARPOS (startp) < ZV
12331 /* Avoid starting at end of buffer. */
12332 || CHARPOS (startp) == BEGV
12333 || (XFASTINT (w->last_modified) >= MODIFF
12334 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
12335 {
12336 #if GLYPH_DEBUG
12337 debug_method_add (w, "same window start");
12338 #endif
12339
12340 /* Try to redisplay starting at same place as before.
12341 If point has not moved off frame, accept the results. */
12342 if (!current_matrix_up_to_date_p
12343 /* Don't use try_window_reusing_current_matrix in this case
12344 because a window scroll function can have changed the
12345 buffer. */
12346 || !NILP (Vwindow_scroll_functions)
12347 || MINI_WINDOW_P (w)
12348 || !(used_current_matrix_p
12349 = try_window_reusing_current_matrix (w)))
12350 {
12351 IF_DEBUG (debug_method_add (w, "1"));
12352 if (try_window (window, startp, 1) < 0)
12353 /* -1 means we need to scroll.
12354 0 means we need new matrices, but fonts_changed_p
12355 is set in that case, so we will detect it below. */
12356 goto try_to_scroll;
12357 }
12358
12359 if (fonts_changed_p)
12360 goto need_larger_matrices;
12361
12362 if (w->cursor.vpos >= 0)
12363 {
12364 if (!just_this_one_p
12365 || current_buffer->clip_changed
12366 || BEG_UNCHANGED < CHARPOS (startp))
12367 /* Forget any recorded base line for line number display. */
12368 w->base_line_number = Qnil;
12369
12370 if (!cursor_row_fully_visible_p (w, 1, 0))
12371 {
12372 clear_glyph_matrix (w->desired_matrix);
12373 last_line_misfit = 1;
12374 }
12375 /* Drop through and scroll. */
12376 else
12377 goto done;
12378 }
12379 else
12380 clear_glyph_matrix (w->desired_matrix);
12381 }
12382
12383 try_to_scroll:
12384
12385 w->last_modified = make_number (0);
12386 w->last_overlay_modified = make_number (0);
12387
12388 /* Redisplay the mode line. Select the buffer properly for that. */
12389 if (!update_mode_line)
12390 {
12391 update_mode_line = 1;
12392 w->update_mode_line = Qt;
12393 }
12394
12395 /* Try to scroll by specified few lines. */
12396 if ((scroll_conservatively
12397 || scroll_step
12398 || temp_scroll_step
12399 || NUMBERP (current_buffer->scroll_up_aggressively)
12400 || NUMBERP (current_buffer->scroll_down_aggressively))
12401 && !current_buffer->clip_changed
12402 && CHARPOS (startp) >= BEGV
12403 && CHARPOS (startp) <= ZV)
12404 {
12405 /* The function returns -1 if new fonts were loaded, 1 if
12406 successful, 0 if not successful. */
12407 int rc = try_scrolling (window, just_this_one_p,
12408 scroll_conservatively,
12409 scroll_step,
12410 temp_scroll_step, last_line_misfit);
12411 switch (rc)
12412 {
12413 case SCROLLING_SUCCESS:
12414 goto done;
12415
12416 case SCROLLING_NEED_LARGER_MATRICES:
12417 goto need_larger_matrices;
12418
12419 case SCROLLING_FAILED:
12420 break;
12421
12422 default:
12423 abort ();
12424 }
12425 }
12426
12427 /* Finally, just choose place to start which centers point */
12428
12429 recenter:
12430 if (centering_position < 0)
12431 centering_position = window_box_height (w) / 2;
12432
12433 #if GLYPH_DEBUG
12434 debug_method_add (w, "recenter");
12435 #endif
12436
12437 /* w->vscroll = 0; */
12438
12439 /* Forget any previously recorded base line for line number display. */
12440 if (!buffer_unchanged_p)
12441 w->base_line_number = Qnil;
12442
12443 /* Move backward half the height of the window. */
12444 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12445 it.current_y = it.last_visible_y;
12446 move_it_vertically_backward (&it, centering_position);
12447 xassert (IT_CHARPOS (it) >= BEGV);
12448
12449 /* The function move_it_vertically_backward may move over more
12450 than the specified y-distance. If it->w is small, e.g. a
12451 mini-buffer window, we may end up in front of the window's
12452 display area. Start displaying at the start of the line
12453 containing PT in this case. */
12454 if (it.current_y <= 0)
12455 {
12456 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
12457 move_it_vertically_backward (&it, 0);
12458 #if 0
12459 /* I think this assert is bogus if buffer contains
12460 invisible text or images. KFS. */
12461 xassert (IT_CHARPOS (it) <= PT);
12462 #endif
12463 it.current_y = 0;
12464 }
12465
12466 it.current_x = it.hpos = 0;
12467
12468 /* Set startp here explicitly in case that helps avoid an infinite loop
12469 in case the window-scroll-functions functions get errors. */
12470 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
12471
12472 /* Run scroll hooks. */
12473 startp = run_window_scroll_functions (window, it.current.pos);
12474
12475 /* Redisplay the window. */
12476 if (!current_matrix_up_to_date_p
12477 || windows_or_buffers_changed
12478 || cursor_type_changed
12479 /* Don't use try_window_reusing_current_matrix in this case
12480 because it can have changed the buffer. */
12481 || !NILP (Vwindow_scroll_functions)
12482 || !just_this_one_p
12483 || MINI_WINDOW_P (w)
12484 || !(used_current_matrix_p
12485 = try_window_reusing_current_matrix (w)))
12486 try_window (window, startp, 0);
12487
12488 /* If new fonts have been loaded (due to fontsets), give up. We
12489 have to start a new redisplay since we need to re-adjust glyph
12490 matrices. */
12491 if (fonts_changed_p)
12492 goto need_larger_matrices;
12493
12494 /* If cursor did not appear assume that the middle of the window is
12495 in the first line of the window. Do it again with the next line.
12496 (Imagine a window of height 100, displaying two lines of height
12497 60. Moving back 50 from it->last_visible_y will end in the first
12498 line.) */
12499 if (w->cursor.vpos < 0)
12500 {
12501 if (!NILP (w->window_end_valid)
12502 && PT >= Z - XFASTINT (w->window_end_pos))
12503 {
12504 clear_glyph_matrix (w->desired_matrix);
12505 move_it_by_lines (&it, 1, 0);
12506 try_window (window, it.current.pos, 0);
12507 }
12508 else if (PT < IT_CHARPOS (it))
12509 {
12510 clear_glyph_matrix (w->desired_matrix);
12511 move_it_by_lines (&it, -1, 0);
12512 try_window (window, it.current.pos, 0);
12513 }
12514 else
12515 {
12516 /* Not much we can do about it. */
12517 }
12518 }
12519
12520 /* Consider the following case: Window starts at BEGV, there is
12521 invisible, intangible text at BEGV, so that display starts at
12522 some point START > BEGV. It can happen that we are called with
12523 PT somewhere between BEGV and START. Try to handle that case. */
12524 if (w->cursor.vpos < 0)
12525 {
12526 struct glyph_row *row = w->current_matrix->rows;
12527 if (row->mode_line_p)
12528 ++row;
12529 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12530 }
12531
12532 if (!cursor_row_fully_visible_p (w, 0, 0))
12533 {
12534 /* If vscroll is enabled, disable it and try again. */
12535 if (w->vscroll)
12536 {
12537 w->vscroll = 0;
12538 clear_glyph_matrix (w->desired_matrix);
12539 goto recenter;
12540 }
12541
12542 /* If centering point failed to make the whole line visible,
12543 put point at the top instead. That has to make the whole line
12544 visible, if it can be done. */
12545 if (centering_position == 0)
12546 goto done;
12547
12548 clear_glyph_matrix (w->desired_matrix);
12549 centering_position = 0;
12550 goto recenter;
12551 }
12552
12553 done:
12554
12555 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12556 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
12557 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
12558 ? Qt : Qnil);
12559
12560 /* Display the mode line, if we must. */
12561 if ((update_mode_line
12562 /* If window not full width, must redo its mode line
12563 if (a) the window to its side is being redone and
12564 (b) we do a frame-based redisplay. This is a consequence
12565 of how inverted lines are drawn in frame-based redisplay. */
12566 || (!just_this_one_p
12567 && !FRAME_WINDOW_P (f)
12568 && !WINDOW_FULL_WIDTH_P (w))
12569 /* Line number to display. */
12570 || INTEGERP (w->base_line_pos)
12571 /* Column number is displayed and different from the one displayed. */
12572 || (!NILP (w->column_number_displayed)
12573 && (XFASTINT (w->column_number_displayed)
12574 != (int) current_column ()))) /* iftc */
12575 /* This means that the window has a mode line. */
12576 && (WINDOW_WANTS_MODELINE_P (w)
12577 || WINDOW_WANTS_HEADER_LINE_P (w)))
12578 {
12579 display_mode_lines (w);
12580
12581 /* If mode line height has changed, arrange for a thorough
12582 immediate redisplay using the correct mode line height. */
12583 if (WINDOW_WANTS_MODELINE_P (w)
12584 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
12585 {
12586 fonts_changed_p = 1;
12587 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
12588 = DESIRED_MODE_LINE_HEIGHT (w);
12589 }
12590
12591 /* If top line height has changed, arrange for a thorough
12592 immediate redisplay using the correct mode line height. */
12593 if (WINDOW_WANTS_HEADER_LINE_P (w)
12594 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
12595 {
12596 fonts_changed_p = 1;
12597 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
12598 = DESIRED_HEADER_LINE_HEIGHT (w);
12599 }
12600
12601 if (fonts_changed_p)
12602 goto need_larger_matrices;
12603 }
12604
12605 if (!line_number_displayed
12606 && !BUFFERP (w->base_line_pos))
12607 {
12608 w->base_line_pos = Qnil;
12609 w->base_line_number = Qnil;
12610 }
12611
12612 finish_menu_bars:
12613
12614 /* When we reach a frame's selected window, redo the frame's menu bar. */
12615 if (update_mode_line
12616 && EQ (FRAME_SELECTED_WINDOW (f), window))
12617 {
12618 int redisplay_menu_p = 0;
12619 int redisplay_tool_bar_p = 0;
12620
12621 if (FRAME_WINDOW_P (f))
12622 {
12623 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
12624 || defined (USE_GTK)
12625 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
12626 #else
12627 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12628 #endif
12629 }
12630 else
12631 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
12632
12633 if (redisplay_menu_p)
12634 display_menu_bar (w);
12635
12636 #ifdef HAVE_WINDOW_SYSTEM
12637 #ifdef USE_GTK
12638 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
12639 #else
12640 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
12641 && (FRAME_TOOL_BAR_LINES (f) > 0
12642 || auto_resize_tool_bars_p);
12643
12644 #endif
12645
12646 if (redisplay_tool_bar_p)
12647 redisplay_tool_bar (f);
12648 #endif
12649 }
12650
12651 #ifdef HAVE_WINDOW_SYSTEM
12652 if (FRAME_WINDOW_P (f)
12653 && update_window_fringes (w, 0)
12654 && !just_this_one_p
12655 && (used_current_matrix_p || overlay_arrow_seen)
12656 && !w->pseudo_window_p)
12657 {
12658 update_begin (f);
12659 BLOCK_INPUT;
12660 if (draw_window_fringes (w, 1))
12661 x_draw_vertical_border (w);
12662 UNBLOCK_INPUT;
12663 update_end (f);
12664 }
12665 #endif /* HAVE_WINDOW_SYSTEM */
12666
12667 /* We go to this label, with fonts_changed_p nonzero,
12668 if it is necessary to try again using larger glyph matrices.
12669 We have to redeem the scroll bar even in this case,
12670 because the loop in redisplay_internal expects that. */
12671 need_larger_matrices:
12672 ;
12673 finish_scroll_bars:
12674
12675 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
12676 {
12677 /* Set the thumb's position and size. */
12678 set_vertical_scroll_bar (w);
12679
12680 /* Note that we actually used the scroll bar attached to this
12681 window, so it shouldn't be deleted at the end of redisplay. */
12682 redeem_scroll_bar_hook (w);
12683 }
12684
12685 /* Restore current_buffer and value of point in it. */
12686 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
12687 set_buffer_internal_1 (old);
12688 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
12689
12690 unbind_to (count, Qnil);
12691 }
12692
12693
12694 /* Build the complete desired matrix of WINDOW with a window start
12695 buffer position POS.
12696
12697 Value is 1 if successful. It is zero if fonts were loaded during
12698 redisplay which makes re-adjusting glyph matrices necessary, and -1
12699 if point would appear in the scroll margins.
12700 (We check that only if CHECK_MARGINS is nonzero. */
12701
12702 int
12703 try_window (window, pos, check_margins)
12704 Lisp_Object window;
12705 struct text_pos pos;
12706 int check_margins;
12707 {
12708 struct window *w = XWINDOW (window);
12709 struct it it;
12710 struct glyph_row *last_text_row = NULL;
12711
12712 /* Make POS the new window start. */
12713 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
12714
12715 /* Mark cursor position as unknown. No overlay arrow seen. */
12716 w->cursor.vpos = -1;
12717 overlay_arrow_seen = 0;
12718
12719 /* Initialize iterator and info to start at POS. */
12720 start_display (&it, w, pos);
12721
12722 /* Display all lines of W. */
12723 while (it.current_y < it.last_visible_y)
12724 {
12725 if (display_line (&it))
12726 last_text_row = it.glyph_row - 1;
12727 if (fonts_changed_p)
12728 return 0;
12729 }
12730
12731 /* Don't let the cursor end in the scroll margins. */
12732 if (check_margins)
12733 {
12734 int this_scroll_margin, cursor_height;
12735
12736 this_scroll_margin = max (0, scroll_margin);
12737 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12738 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
12739 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
12740
12741 if ((w->cursor.y < this_scroll_margin
12742 && CHARPOS (pos) > BEGV)
12743 /* rms: considering make_cursor_line_fully_visible_p here
12744 seems to give wrong results. We don't want to recenter
12745 when the last line is partly visible, we want to allow
12746 that case to be handled in the usual way. */
12747 || (w->cursor.y + 1) > it.last_visible_y)
12748 {
12749 w->cursor.vpos = -1;
12750 clear_glyph_matrix (w->desired_matrix);
12751 return -1;
12752 }
12753 }
12754
12755 /* If bottom moved off end of frame, change mode line percentage. */
12756 if (XFASTINT (w->window_end_pos) <= 0
12757 && Z != IT_CHARPOS (it))
12758 w->update_mode_line = Qt;
12759
12760 /* Set window_end_pos to the offset of the last character displayed
12761 on the window from the end of current_buffer. Set
12762 window_end_vpos to its row number. */
12763 if (last_text_row)
12764 {
12765 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
12766 w->window_end_bytepos
12767 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
12768 w->window_end_pos
12769 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
12770 w->window_end_vpos
12771 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
12772 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
12773 ->displays_text_p);
12774 }
12775 else
12776 {
12777 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
12778 w->window_end_pos = make_number (Z - ZV);
12779 w->window_end_vpos = make_number (0);
12780 }
12781
12782 /* But that is not valid info until redisplay finishes. */
12783 w->window_end_valid = Qnil;
12784 return 1;
12785 }
12786
12787
12788 \f
12789 /************************************************************************
12790 Window redisplay reusing current matrix when buffer has not changed
12791 ************************************************************************/
12792
12793 /* Try redisplay of window W showing an unchanged buffer with a
12794 different window start than the last time it was displayed by
12795 reusing its current matrix. Value is non-zero if successful.
12796 W->start is the new window start. */
12797
12798 static int
12799 try_window_reusing_current_matrix (w)
12800 struct window *w;
12801 {
12802 struct frame *f = XFRAME (w->frame);
12803 struct glyph_row *row, *bottom_row;
12804 struct it it;
12805 struct run run;
12806 struct text_pos start, new_start;
12807 int nrows_scrolled, i;
12808 struct glyph_row *last_text_row;
12809 struct glyph_row *last_reused_text_row;
12810 struct glyph_row *start_row;
12811 int start_vpos, min_y, max_y;
12812
12813 #if GLYPH_DEBUG
12814 if (inhibit_try_window_reusing)
12815 return 0;
12816 #endif
12817
12818 if (/* This function doesn't handle terminal frames. */
12819 !FRAME_WINDOW_P (f)
12820 /* Don't try to reuse the display if windows have been split
12821 or such. */
12822 || windows_or_buffers_changed
12823 || cursor_type_changed)
12824 return 0;
12825
12826 /* Can't do this if region may have changed. */
12827 if ((!NILP (Vtransient_mark_mode)
12828 && !NILP (current_buffer->mark_active))
12829 || !NILP (w->region_showing)
12830 || !NILP (Vshow_trailing_whitespace))
12831 return 0;
12832
12833 /* If top-line visibility has changed, give up. */
12834 if (WINDOW_WANTS_HEADER_LINE_P (w)
12835 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
12836 return 0;
12837
12838 /* Give up if old or new display is scrolled vertically. We could
12839 make this function handle this, but right now it doesn't. */
12840 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12841 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
12842 return 0;
12843
12844 /* The variable new_start now holds the new window start. The old
12845 start `start' can be determined from the current matrix. */
12846 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
12847 start = start_row->start.pos;
12848 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12849
12850 /* Clear the desired matrix for the display below. */
12851 clear_glyph_matrix (w->desired_matrix);
12852
12853 if (CHARPOS (new_start) <= CHARPOS (start))
12854 {
12855 int first_row_y;
12856
12857 /* Don't use this method if the display starts with an ellipsis
12858 displayed for invisible text. It's not easy to handle that case
12859 below, and it's certainly not worth the effort since this is
12860 not a frequent case. */
12861 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
12862 return 0;
12863
12864 IF_DEBUG (debug_method_add (w, "twu1"));
12865
12866 /* Display up to a row that can be reused. The variable
12867 last_text_row is set to the last row displayed that displays
12868 text. Note that it.vpos == 0 if or if not there is a
12869 header-line; it's not the same as the MATRIX_ROW_VPOS! */
12870 start_display (&it, w, new_start);
12871 first_row_y = it.current_y;
12872 w->cursor.vpos = -1;
12873 last_text_row = last_reused_text_row = NULL;
12874
12875 while (it.current_y < it.last_visible_y
12876 && !fonts_changed_p)
12877 {
12878 /* If we have reached into the characters in the START row,
12879 that means the line boundaries have changed. So we
12880 can't start copying with the row START. Maybe it will
12881 work to start copying with the following row. */
12882 while (IT_CHARPOS (it) > CHARPOS (start))
12883 {
12884 /* Advance to the next row as the "start". */
12885 start_row++;
12886 start = start_row->start.pos;
12887 /* If there are no more rows to try, or just one, give up. */
12888 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
12889 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
12890 || CHARPOS (start) == ZV)
12891 {
12892 clear_glyph_matrix (w->desired_matrix);
12893 return 0;
12894 }
12895
12896 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
12897 }
12898 /* If we have reached alignment,
12899 we can copy the rest of the rows. */
12900 if (IT_CHARPOS (it) == CHARPOS (start))
12901 break;
12902
12903 if (display_line (&it))
12904 last_text_row = it.glyph_row - 1;
12905 }
12906
12907 /* A value of current_y < last_visible_y means that we stopped
12908 at the previous window start, which in turn means that we
12909 have at least one reusable row. */
12910 if (it.current_y < it.last_visible_y)
12911 {
12912 /* IT.vpos always starts from 0; it counts text lines. */
12913 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
12914
12915 /* Find PT if not already found in the lines displayed. */
12916 if (w->cursor.vpos < 0)
12917 {
12918 int dy = it.current_y - start_row->y;
12919
12920 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
12921 row = row_containing_pos (w, PT, row, NULL, dy);
12922 if (row)
12923 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
12924 dy, nrows_scrolled);
12925 else
12926 {
12927 clear_glyph_matrix (w->desired_matrix);
12928 return 0;
12929 }
12930 }
12931
12932 /* Scroll the display. Do it before the current matrix is
12933 changed. The problem here is that update has not yet
12934 run, i.e. part of the current matrix is not up to date.
12935 scroll_run_hook will clear the cursor, and use the
12936 current matrix to get the height of the row the cursor is
12937 in. */
12938 run.current_y = start_row->y;
12939 run.desired_y = it.current_y;
12940 run.height = it.last_visible_y - it.current_y;
12941
12942 if (run.height > 0 && run.current_y != run.desired_y)
12943 {
12944 update_begin (f);
12945 rif->update_window_begin_hook (w);
12946 rif->clear_window_mouse_face (w);
12947 rif->scroll_run_hook (w, &run);
12948 rif->update_window_end_hook (w, 0, 0);
12949 update_end (f);
12950 }
12951
12952 /* Shift current matrix down by nrows_scrolled lines. */
12953 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12954 rotate_matrix (w->current_matrix,
12955 start_vpos,
12956 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
12957 nrows_scrolled);
12958
12959 /* Disable lines that must be updated. */
12960 for (i = 0; i < it.vpos; ++i)
12961 (start_row + i)->enabled_p = 0;
12962
12963 /* Re-compute Y positions. */
12964 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
12965 max_y = it.last_visible_y;
12966 for (row = start_row + nrows_scrolled;
12967 row < bottom_row;
12968 ++row)
12969 {
12970 row->y = it.current_y;
12971 row->visible_height = row->height;
12972
12973 if (row->y < min_y)
12974 row->visible_height -= min_y - row->y;
12975 if (row->y + row->height > max_y)
12976 row->visible_height -= row->y + row->height - max_y;
12977 row->redraw_fringe_bitmaps_p = 1;
12978
12979 it.current_y += row->height;
12980
12981 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
12982 last_reused_text_row = row;
12983 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
12984 break;
12985 }
12986
12987 /* Disable lines in the current matrix which are now
12988 below the window. */
12989 for (++row; row < bottom_row; ++row)
12990 row->enabled_p = 0;
12991 }
12992
12993 /* Update window_end_pos etc.; last_reused_text_row is the last
12994 reused row from the current matrix containing text, if any.
12995 The value of last_text_row is the last displayed line
12996 containing text. */
12997 if (last_reused_text_row)
12998 {
12999 w->window_end_bytepos
13000 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13001 w->window_end_pos
13002 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13003 w->window_end_vpos
13004 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13005 w->current_matrix));
13006 }
13007 else if (last_text_row)
13008 {
13009 w->window_end_bytepos
13010 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13011 w->window_end_pos
13012 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13013 w->window_end_vpos
13014 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13015 }
13016 else
13017 {
13018 /* This window must be completely empty. */
13019 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13020 w->window_end_pos = make_number (Z - ZV);
13021 w->window_end_vpos = make_number (0);
13022 }
13023 w->window_end_valid = Qnil;
13024
13025 /* Update hint: don't try scrolling again in update_window. */
13026 w->desired_matrix->no_scrolling_p = 1;
13027
13028 #if GLYPH_DEBUG
13029 debug_method_add (w, "try_window_reusing_current_matrix 1");
13030 #endif
13031 return 1;
13032 }
13033 else if (CHARPOS (new_start) > CHARPOS (start))
13034 {
13035 struct glyph_row *pt_row, *row;
13036 struct glyph_row *first_reusable_row;
13037 struct glyph_row *first_row_to_display;
13038 int dy;
13039 int yb = window_text_bottom_y (w);
13040
13041 /* Find the row starting at new_start, if there is one. Don't
13042 reuse a partially visible line at the end. */
13043 first_reusable_row = start_row;
13044 while (first_reusable_row->enabled_p
13045 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13046 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13047 < CHARPOS (new_start)))
13048 ++first_reusable_row;
13049
13050 /* Give up if there is no row to reuse. */
13051 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13052 || !first_reusable_row->enabled_p
13053 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13054 != CHARPOS (new_start)))
13055 return 0;
13056
13057 /* We can reuse fully visible rows beginning with
13058 first_reusable_row to the end of the window. Set
13059 first_row_to_display to the first row that cannot be reused.
13060 Set pt_row to the row containing point, if there is any. */
13061 pt_row = NULL;
13062 for (first_row_to_display = first_reusable_row;
13063 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13064 ++first_row_to_display)
13065 {
13066 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13067 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13068 pt_row = first_row_to_display;
13069 }
13070
13071 /* Start displaying at the start of first_row_to_display. */
13072 xassert (first_row_to_display->y < yb);
13073 init_to_row_start (&it, w, first_row_to_display);
13074
13075 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13076 - start_vpos);
13077 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13078 - nrows_scrolled);
13079 it.current_y = (first_row_to_display->y - first_reusable_row->y
13080 + WINDOW_HEADER_LINE_HEIGHT (w));
13081
13082 /* Display lines beginning with first_row_to_display in the
13083 desired matrix. Set last_text_row to the last row displayed
13084 that displays text. */
13085 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13086 if (pt_row == NULL)
13087 w->cursor.vpos = -1;
13088 last_text_row = NULL;
13089 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13090 if (display_line (&it))
13091 last_text_row = it.glyph_row - 1;
13092
13093 /* Give up If point isn't in a row displayed or reused. */
13094 if (w->cursor.vpos < 0)
13095 {
13096 clear_glyph_matrix (w->desired_matrix);
13097 return 0;
13098 }
13099
13100 /* If point is in a reused row, adjust y and vpos of the cursor
13101 position. */
13102 if (pt_row)
13103 {
13104 w->cursor.vpos -= nrows_scrolled;
13105 w->cursor.y -= first_reusable_row->y - start_row->y;
13106 }
13107
13108 /* Scroll the display. */
13109 run.current_y = first_reusable_row->y;
13110 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13111 run.height = it.last_visible_y - run.current_y;
13112 dy = run.current_y - run.desired_y;
13113
13114 if (run.height)
13115 {
13116 update_begin (f);
13117 rif->update_window_begin_hook (w);
13118 rif->clear_window_mouse_face (w);
13119 rif->scroll_run_hook (w, &run);
13120 rif->update_window_end_hook (w, 0, 0);
13121 update_end (f);
13122 }
13123
13124 /* Adjust Y positions of reused rows. */
13125 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13126 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13127 max_y = it.last_visible_y;
13128 for (row = first_reusable_row; row < first_row_to_display; ++row)
13129 {
13130 row->y -= dy;
13131 row->visible_height = row->height;
13132 if (row->y < min_y)
13133 row->visible_height -= min_y - row->y;
13134 if (row->y + row->height > max_y)
13135 row->visible_height -= row->y + row->height - max_y;
13136 row->redraw_fringe_bitmaps_p = 1;
13137 }
13138
13139 /* Scroll the current matrix. */
13140 xassert (nrows_scrolled > 0);
13141 rotate_matrix (w->current_matrix,
13142 start_vpos,
13143 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13144 -nrows_scrolled);
13145
13146 /* Disable rows not reused. */
13147 for (row -= nrows_scrolled; row < bottom_row; ++row)
13148 row->enabled_p = 0;
13149
13150 /* Point may have moved to a different line, so we cannot assume that
13151 the previous cursor position is valid; locate the correct row. */
13152 if (pt_row)
13153 {
13154 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13155 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13156 row++)
13157 {
13158 w->cursor.vpos++;
13159 w->cursor.y = row->y;
13160 }
13161 if (row < bottom_row)
13162 {
13163 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13164 while (glyph->charpos < PT)
13165 {
13166 w->cursor.hpos++;
13167 w->cursor.x += glyph->pixel_width;
13168 glyph++;
13169 }
13170 }
13171 }
13172
13173 /* Adjust window end. A null value of last_text_row means that
13174 the window end is in reused rows which in turn means that
13175 only its vpos can have changed. */
13176 if (last_text_row)
13177 {
13178 w->window_end_bytepos
13179 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13180 w->window_end_pos
13181 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13182 w->window_end_vpos
13183 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13184 }
13185 else
13186 {
13187 w->window_end_vpos
13188 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13189 }
13190
13191 w->window_end_valid = Qnil;
13192 w->desired_matrix->no_scrolling_p = 1;
13193
13194 #if GLYPH_DEBUG
13195 debug_method_add (w, "try_window_reusing_current_matrix 2");
13196 #endif
13197 return 1;
13198 }
13199
13200 return 0;
13201 }
13202
13203
13204 \f
13205 /************************************************************************
13206 Window redisplay reusing current matrix when buffer has changed
13207 ************************************************************************/
13208
13209 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13210 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13211 int *, int *));
13212 static struct glyph_row *
13213 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13214 struct glyph_row *));
13215
13216
13217 /* Return the last row in MATRIX displaying text. If row START is
13218 non-null, start searching with that row. IT gives the dimensions
13219 of the display. Value is null if matrix is empty; otherwise it is
13220 a pointer to the row found. */
13221
13222 static struct glyph_row *
13223 find_last_row_displaying_text (matrix, it, start)
13224 struct glyph_matrix *matrix;
13225 struct it *it;
13226 struct glyph_row *start;
13227 {
13228 struct glyph_row *row, *row_found;
13229
13230 /* Set row_found to the last row in IT->w's current matrix
13231 displaying text. The loop looks funny but think of partially
13232 visible lines. */
13233 row_found = NULL;
13234 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
13235 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13236 {
13237 xassert (row->enabled_p);
13238 row_found = row;
13239 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
13240 break;
13241 ++row;
13242 }
13243
13244 return row_found;
13245 }
13246
13247
13248 /* Return the last row in the current matrix of W that is not affected
13249 by changes at the start of current_buffer that occurred since W's
13250 current matrix was built. Value is null if no such row exists.
13251
13252 BEG_UNCHANGED us the number of characters unchanged at the start of
13253 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
13254 first changed character in current_buffer. Characters at positions <
13255 BEG + BEG_UNCHANGED are at the same buffer positions as they were
13256 when the current matrix was built. */
13257
13258 static struct glyph_row *
13259 find_last_unchanged_at_beg_row (w)
13260 struct window *w;
13261 {
13262 int first_changed_pos = BEG + BEG_UNCHANGED;
13263 struct glyph_row *row;
13264 struct glyph_row *row_found = NULL;
13265 int yb = window_text_bottom_y (w);
13266
13267 /* Find the last row displaying unchanged text. */
13268 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13269 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13270 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
13271 {
13272 if (/* If row ends before first_changed_pos, it is unchanged,
13273 except in some case. */
13274 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
13275 /* When row ends in ZV and we write at ZV it is not
13276 unchanged. */
13277 && !row->ends_at_zv_p
13278 /* When first_changed_pos is the end of a continued line,
13279 row is not unchanged because it may be no longer
13280 continued. */
13281 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
13282 && (row->continued_p
13283 || row->exact_window_width_line_p)))
13284 row_found = row;
13285
13286 /* Stop if last visible row. */
13287 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
13288 break;
13289
13290 ++row;
13291 }
13292
13293 return row_found;
13294 }
13295
13296
13297 /* Find the first glyph row in the current matrix of W that is not
13298 affected by changes at the end of current_buffer since the
13299 time W's current matrix was built.
13300
13301 Return in *DELTA the number of chars by which buffer positions in
13302 unchanged text at the end of current_buffer must be adjusted.
13303
13304 Return in *DELTA_BYTES the corresponding number of bytes.
13305
13306 Value is null if no such row exists, i.e. all rows are affected by
13307 changes. */
13308
13309 static struct glyph_row *
13310 find_first_unchanged_at_end_row (w, delta, delta_bytes)
13311 struct window *w;
13312 int *delta, *delta_bytes;
13313 {
13314 struct glyph_row *row;
13315 struct glyph_row *row_found = NULL;
13316
13317 *delta = *delta_bytes = 0;
13318
13319 /* Display must not have been paused, otherwise the current matrix
13320 is not up to date. */
13321 if (NILP (w->window_end_valid))
13322 abort ();
13323
13324 /* A value of window_end_pos >= END_UNCHANGED means that the window
13325 end is in the range of changed text. If so, there is no
13326 unchanged row at the end of W's current matrix. */
13327 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
13328 return NULL;
13329
13330 /* Set row to the last row in W's current matrix displaying text. */
13331 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13332
13333 /* If matrix is entirely empty, no unchanged row exists. */
13334 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13335 {
13336 /* The value of row is the last glyph row in the matrix having a
13337 meaningful buffer position in it. The end position of row
13338 corresponds to window_end_pos. This allows us to translate
13339 buffer positions in the current matrix to current buffer
13340 positions for characters not in changed text. */
13341 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13342 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13343 int last_unchanged_pos, last_unchanged_pos_old;
13344 struct glyph_row *first_text_row
13345 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13346
13347 *delta = Z - Z_old;
13348 *delta_bytes = Z_BYTE - Z_BYTE_old;
13349
13350 /* Set last_unchanged_pos to the buffer position of the last
13351 character in the buffer that has not been changed. Z is the
13352 index + 1 of the last character in current_buffer, i.e. by
13353 subtracting END_UNCHANGED we get the index of the last
13354 unchanged character, and we have to add BEG to get its buffer
13355 position. */
13356 last_unchanged_pos = Z - END_UNCHANGED + BEG;
13357 last_unchanged_pos_old = last_unchanged_pos - *delta;
13358
13359 /* Search backward from ROW for a row displaying a line that
13360 starts at a minimum position >= last_unchanged_pos_old. */
13361 for (; row > first_text_row; --row)
13362 {
13363 /* This used to abort, but it can happen.
13364 It is ok to just stop the search instead here. KFS. */
13365 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
13366 break;
13367
13368 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
13369 row_found = row;
13370 }
13371 }
13372
13373 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
13374 abort ();
13375
13376 return row_found;
13377 }
13378
13379
13380 /* Make sure that glyph rows in the current matrix of window W
13381 reference the same glyph memory as corresponding rows in the
13382 frame's frame matrix. This function is called after scrolling W's
13383 current matrix on a terminal frame in try_window_id and
13384 try_window_reusing_current_matrix. */
13385
13386 static void
13387 sync_frame_with_window_matrix_rows (w)
13388 struct window *w;
13389 {
13390 struct frame *f = XFRAME (w->frame);
13391 struct glyph_row *window_row, *window_row_end, *frame_row;
13392
13393 /* Preconditions: W must be a leaf window and full-width. Its frame
13394 must have a frame matrix. */
13395 xassert (NILP (w->hchild) && NILP (w->vchild));
13396 xassert (WINDOW_FULL_WIDTH_P (w));
13397 xassert (!FRAME_WINDOW_P (f));
13398
13399 /* If W is a full-width window, glyph pointers in W's current matrix
13400 have, by definition, to be the same as glyph pointers in the
13401 corresponding frame matrix. Note that frame matrices have no
13402 marginal areas (see build_frame_matrix). */
13403 window_row = w->current_matrix->rows;
13404 window_row_end = window_row + w->current_matrix->nrows;
13405 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
13406 while (window_row < window_row_end)
13407 {
13408 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
13409 struct glyph *end = window_row->glyphs[LAST_AREA];
13410
13411 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
13412 frame_row->glyphs[TEXT_AREA] = start;
13413 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
13414 frame_row->glyphs[LAST_AREA] = end;
13415
13416 /* Disable frame rows whose corresponding window rows have
13417 been disabled in try_window_id. */
13418 if (!window_row->enabled_p)
13419 frame_row->enabled_p = 0;
13420
13421 ++window_row, ++frame_row;
13422 }
13423 }
13424
13425
13426 /* Find the glyph row in window W containing CHARPOS. Consider all
13427 rows between START and END (not inclusive). END null means search
13428 all rows to the end of the display area of W. Value is the row
13429 containing CHARPOS or null. */
13430
13431 struct glyph_row *
13432 row_containing_pos (w, charpos, start, end, dy)
13433 struct window *w;
13434 int charpos;
13435 struct glyph_row *start, *end;
13436 int dy;
13437 {
13438 struct glyph_row *row = start;
13439 int last_y;
13440
13441 /* If we happen to start on a header-line, skip that. */
13442 if (row->mode_line_p)
13443 ++row;
13444
13445 if ((end && row >= end) || !row->enabled_p)
13446 return NULL;
13447
13448 last_y = window_text_bottom_y (w) - dy;
13449
13450 while (1)
13451 {
13452 /* Give up if we have gone too far. */
13453 if (end && row >= end)
13454 return NULL;
13455 /* This formerly returned if they were equal.
13456 I think that both quantities are of a "last plus one" type;
13457 if so, when they are equal, the row is within the screen. -- rms. */
13458 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
13459 return NULL;
13460
13461 /* If it is in this row, return this row. */
13462 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
13463 || (MATRIX_ROW_END_CHARPOS (row) == charpos
13464 /* The end position of a row equals the start
13465 position of the next row. If CHARPOS is there, we
13466 would rather display it in the next line, except
13467 when this line ends in ZV. */
13468 && !row->ends_at_zv_p
13469 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
13470 && charpos >= MATRIX_ROW_START_CHARPOS (row))
13471 return row;
13472 ++row;
13473 }
13474 }
13475
13476
13477 /* Try to redisplay window W by reusing its existing display. W's
13478 current matrix must be up to date when this function is called,
13479 i.e. window_end_valid must not be nil.
13480
13481 Value is
13482
13483 1 if display has been updated
13484 0 if otherwise unsuccessful
13485 -1 if redisplay with same window start is known not to succeed
13486
13487 The following steps are performed:
13488
13489 1. Find the last row in the current matrix of W that is not
13490 affected by changes at the start of current_buffer. If no such row
13491 is found, give up.
13492
13493 2. Find the first row in W's current matrix that is not affected by
13494 changes at the end of current_buffer. Maybe there is no such row.
13495
13496 3. Display lines beginning with the row + 1 found in step 1 to the
13497 row found in step 2 or, if step 2 didn't find a row, to the end of
13498 the window.
13499
13500 4. If cursor is not known to appear on the window, give up.
13501
13502 5. If display stopped at the row found in step 2, scroll the
13503 display and current matrix as needed.
13504
13505 6. Maybe display some lines at the end of W, if we must. This can
13506 happen under various circumstances, like a partially visible line
13507 becoming fully visible, or because newly displayed lines are displayed
13508 in smaller font sizes.
13509
13510 7. Update W's window end information. */
13511
13512 static int
13513 try_window_id (w)
13514 struct window *w;
13515 {
13516 struct frame *f = XFRAME (w->frame);
13517 struct glyph_matrix *current_matrix = w->current_matrix;
13518 struct glyph_matrix *desired_matrix = w->desired_matrix;
13519 struct glyph_row *last_unchanged_at_beg_row;
13520 struct glyph_row *first_unchanged_at_end_row;
13521 struct glyph_row *row;
13522 struct glyph_row *bottom_row;
13523 int bottom_vpos;
13524 struct it it;
13525 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
13526 struct text_pos start_pos;
13527 struct run run;
13528 int first_unchanged_at_end_vpos = 0;
13529 struct glyph_row *last_text_row, *last_text_row_at_end;
13530 struct text_pos start;
13531 int first_changed_charpos, last_changed_charpos;
13532
13533 #if GLYPH_DEBUG
13534 if (inhibit_try_window_id)
13535 return 0;
13536 #endif
13537
13538 /* This is handy for debugging. */
13539 #if 0
13540 #define GIVE_UP(X) \
13541 do { \
13542 fprintf (stderr, "try_window_id give up %d\n", (X)); \
13543 return 0; \
13544 } while (0)
13545 #else
13546 #define GIVE_UP(X) return 0
13547 #endif
13548
13549 SET_TEXT_POS_FROM_MARKER (start, w->start);
13550
13551 /* Don't use this for mini-windows because these can show
13552 messages and mini-buffers, and we don't handle that here. */
13553 if (MINI_WINDOW_P (w))
13554 GIVE_UP (1);
13555
13556 /* This flag is used to prevent redisplay optimizations. */
13557 if (windows_or_buffers_changed || cursor_type_changed)
13558 GIVE_UP (2);
13559
13560 /* Verify that narrowing has not changed.
13561 Also verify that we were not told to prevent redisplay optimizations.
13562 It would be nice to further
13563 reduce the number of cases where this prevents try_window_id. */
13564 if (current_buffer->clip_changed
13565 || current_buffer->prevent_redisplay_optimizations_p)
13566 GIVE_UP (3);
13567
13568 /* Window must either use window-based redisplay or be full width. */
13569 if (!FRAME_WINDOW_P (f)
13570 && (!line_ins_del_ok
13571 || !WINDOW_FULL_WIDTH_P (w)))
13572 GIVE_UP (4);
13573
13574 /* Give up if point is not known NOT to appear in W. */
13575 if (PT < CHARPOS (start))
13576 GIVE_UP (5);
13577
13578 /* Another way to prevent redisplay optimizations. */
13579 if (XFASTINT (w->last_modified) == 0)
13580 GIVE_UP (6);
13581
13582 /* Verify that window is not hscrolled. */
13583 if (XFASTINT (w->hscroll) != 0)
13584 GIVE_UP (7);
13585
13586 /* Verify that display wasn't paused. */
13587 if (NILP (w->window_end_valid))
13588 GIVE_UP (8);
13589
13590 /* Can't use this if highlighting a region because a cursor movement
13591 will do more than just set the cursor. */
13592 if (!NILP (Vtransient_mark_mode)
13593 && !NILP (current_buffer->mark_active))
13594 GIVE_UP (9);
13595
13596 /* Likewise if highlighting trailing whitespace. */
13597 if (!NILP (Vshow_trailing_whitespace))
13598 GIVE_UP (11);
13599
13600 /* Likewise if showing a region. */
13601 if (!NILP (w->region_showing))
13602 GIVE_UP (10);
13603
13604 /* Can use this if overlay arrow position and or string have changed. */
13605 if (overlay_arrows_changed_p ())
13606 GIVE_UP (12);
13607
13608
13609 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
13610 only if buffer has really changed. The reason is that the gap is
13611 initially at Z for freshly visited files. The code below would
13612 set end_unchanged to 0 in that case. */
13613 if (MODIFF > SAVE_MODIFF
13614 /* This seems to happen sometimes after saving a buffer. */
13615 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13616 {
13617 if (GPT - BEG < BEG_UNCHANGED)
13618 BEG_UNCHANGED = GPT - BEG;
13619 if (Z - GPT < END_UNCHANGED)
13620 END_UNCHANGED = Z - GPT;
13621 }
13622
13623 /* The position of the first and last character that has been changed. */
13624 first_changed_charpos = BEG + BEG_UNCHANGED;
13625 last_changed_charpos = Z - END_UNCHANGED;
13626
13627 /* If window starts after a line end, and the last change is in
13628 front of that newline, then changes don't affect the display.
13629 This case happens with stealth-fontification. Note that although
13630 the display is unchanged, glyph positions in the matrix have to
13631 be adjusted, of course. */
13632 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
13633 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
13634 && ((last_changed_charpos < CHARPOS (start)
13635 && CHARPOS (start) == BEGV)
13636 || (last_changed_charpos < CHARPOS (start) - 1
13637 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
13638 {
13639 int Z_old, delta, Z_BYTE_old, delta_bytes;
13640 struct glyph_row *r0;
13641
13642 /* Compute how many chars/bytes have been added to or removed
13643 from the buffer. */
13644 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
13645 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
13646 delta = Z - Z_old;
13647 delta_bytes = Z_BYTE - Z_BYTE_old;
13648
13649 /* Give up if PT is not in the window. Note that it already has
13650 been checked at the start of try_window_id that PT is not in
13651 front of the window start. */
13652 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
13653 GIVE_UP (13);
13654
13655 /* If window start is unchanged, we can reuse the whole matrix
13656 as is, after adjusting glyph positions. No need to compute
13657 the window end again, since its offset from Z hasn't changed. */
13658 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13659 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
13660 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
13661 /* PT must not be in a partially visible line. */
13662 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
13663 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13664 {
13665 /* Adjust positions in the glyph matrix. */
13666 if (delta || delta_bytes)
13667 {
13668 struct glyph_row *r1
13669 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
13670 increment_matrix_positions (w->current_matrix,
13671 MATRIX_ROW_VPOS (r0, current_matrix),
13672 MATRIX_ROW_VPOS (r1, current_matrix),
13673 delta, delta_bytes);
13674 }
13675
13676 /* Set the cursor. */
13677 row = row_containing_pos (w, PT, r0, NULL, 0);
13678 if (row)
13679 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13680 else
13681 abort ();
13682 return 1;
13683 }
13684 }
13685
13686 /* Handle the case that changes are all below what is displayed in
13687 the window, and that PT is in the window. This shortcut cannot
13688 be taken if ZV is visible in the window, and text has been added
13689 there that is visible in the window. */
13690 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
13691 /* ZV is not visible in the window, or there are no
13692 changes at ZV, actually. */
13693 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
13694 || first_changed_charpos == last_changed_charpos))
13695 {
13696 struct glyph_row *r0;
13697
13698 /* Give up if PT is not in the window. Note that it already has
13699 been checked at the start of try_window_id that PT is not in
13700 front of the window start. */
13701 if (PT >= MATRIX_ROW_END_CHARPOS (row))
13702 GIVE_UP (14);
13703
13704 /* If window start is unchanged, we can reuse the whole matrix
13705 as is, without changing glyph positions since no text has
13706 been added/removed in front of the window end. */
13707 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
13708 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
13709 /* PT must not be in a partially visible line. */
13710 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
13711 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
13712 {
13713 /* We have to compute the window end anew since text
13714 can have been added/removed after it. */
13715 w->window_end_pos
13716 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
13717 w->window_end_bytepos
13718 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
13719
13720 /* Set the cursor. */
13721 row = row_containing_pos (w, PT, r0, NULL, 0);
13722 if (row)
13723 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
13724 else
13725 abort ();
13726 return 2;
13727 }
13728 }
13729
13730 /* Give up if window start is in the changed area.
13731
13732 The condition used to read
13733
13734 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
13735
13736 but why that was tested escapes me at the moment. */
13737 if (CHARPOS (start) >= first_changed_charpos
13738 && CHARPOS (start) <= last_changed_charpos)
13739 GIVE_UP (15);
13740
13741 /* Check that window start agrees with the start of the first glyph
13742 row in its current matrix. Check this after we know the window
13743 start is not in changed text, otherwise positions would not be
13744 comparable. */
13745 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
13746 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
13747 GIVE_UP (16);
13748
13749 /* Give up if the window ends in strings. Overlay strings
13750 at the end are difficult to handle, so don't try. */
13751 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
13752 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
13753 GIVE_UP (20);
13754
13755 /* Compute the position at which we have to start displaying new
13756 lines. Some of the lines at the top of the window might be
13757 reusable because they are not displaying changed text. Find the
13758 last row in W's current matrix not affected by changes at the
13759 start of current_buffer. Value is null if changes start in the
13760 first line of window. */
13761 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
13762 if (last_unchanged_at_beg_row)
13763 {
13764 /* Avoid starting to display in the moddle of a character, a TAB
13765 for instance. This is easier than to set up the iterator
13766 exactly, and it's not a frequent case, so the additional
13767 effort wouldn't really pay off. */
13768 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
13769 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
13770 && last_unchanged_at_beg_row > w->current_matrix->rows)
13771 --last_unchanged_at_beg_row;
13772
13773 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
13774 GIVE_UP (17);
13775
13776 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
13777 GIVE_UP (18);
13778 start_pos = it.current.pos;
13779
13780 /* Start displaying new lines in the desired matrix at the same
13781 vpos we would use in the current matrix, i.e. below
13782 last_unchanged_at_beg_row. */
13783 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
13784 current_matrix);
13785 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
13786 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
13787
13788 xassert (it.hpos == 0 && it.current_x == 0);
13789 }
13790 else
13791 {
13792 /* There are no reusable lines at the start of the window.
13793 Start displaying in the first text line. */
13794 start_display (&it, w, start);
13795 it.vpos = it.first_vpos;
13796 start_pos = it.current.pos;
13797 }
13798
13799 /* Find the first row that is not affected by changes at the end of
13800 the buffer. Value will be null if there is no unchanged row, in
13801 which case we must redisplay to the end of the window. delta
13802 will be set to the value by which buffer positions beginning with
13803 first_unchanged_at_end_row have to be adjusted due to text
13804 changes. */
13805 first_unchanged_at_end_row
13806 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
13807 IF_DEBUG (debug_delta = delta);
13808 IF_DEBUG (debug_delta_bytes = delta_bytes);
13809
13810 /* Set stop_pos to the buffer position up to which we will have to
13811 display new lines. If first_unchanged_at_end_row != NULL, this
13812 is the buffer position of the start of the line displayed in that
13813 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
13814 that we don't stop at a buffer position. */
13815 stop_pos = 0;
13816 if (first_unchanged_at_end_row)
13817 {
13818 xassert (last_unchanged_at_beg_row == NULL
13819 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
13820
13821 /* If this is a continuation line, move forward to the next one
13822 that isn't. Changes in lines above affect this line.
13823 Caution: this may move first_unchanged_at_end_row to a row
13824 not displaying text. */
13825 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
13826 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13827 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13828 < it.last_visible_y))
13829 ++first_unchanged_at_end_row;
13830
13831 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
13832 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
13833 >= it.last_visible_y))
13834 first_unchanged_at_end_row = NULL;
13835 else
13836 {
13837 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
13838 + delta);
13839 first_unchanged_at_end_vpos
13840 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
13841 xassert (stop_pos >= Z - END_UNCHANGED);
13842 }
13843 }
13844 else if (last_unchanged_at_beg_row == NULL)
13845 GIVE_UP (19);
13846
13847
13848 #if GLYPH_DEBUG
13849
13850 /* Either there is no unchanged row at the end, or the one we have
13851 now displays text. This is a necessary condition for the window
13852 end pos calculation at the end of this function. */
13853 xassert (first_unchanged_at_end_row == NULL
13854 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
13855
13856 debug_last_unchanged_at_beg_vpos
13857 = (last_unchanged_at_beg_row
13858 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
13859 : -1);
13860 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
13861
13862 #endif /* GLYPH_DEBUG != 0 */
13863
13864
13865 /* Display new lines. Set last_text_row to the last new line
13866 displayed which has text on it, i.e. might end up as being the
13867 line where the window_end_vpos is. */
13868 w->cursor.vpos = -1;
13869 last_text_row = NULL;
13870 overlay_arrow_seen = 0;
13871 while (it.current_y < it.last_visible_y
13872 && !fonts_changed_p
13873 && (first_unchanged_at_end_row == NULL
13874 || IT_CHARPOS (it) < stop_pos))
13875 {
13876 if (display_line (&it))
13877 last_text_row = it.glyph_row - 1;
13878 }
13879
13880 if (fonts_changed_p)
13881 return -1;
13882
13883
13884 /* Compute differences in buffer positions, y-positions etc. for
13885 lines reused at the bottom of the window. Compute what we can
13886 scroll. */
13887 if (first_unchanged_at_end_row
13888 /* No lines reused because we displayed everything up to the
13889 bottom of the window. */
13890 && it.current_y < it.last_visible_y)
13891 {
13892 dvpos = (it.vpos
13893 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
13894 current_matrix));
13895 dy = it.current_y - first_unchanged_at_end_row->y;
13896 run.current_y = first_unchanged_at_end_row->y;
13897 run.desired_y = run.current_y + dy;
13898 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
13899 }
13900 else
13901 {
13902 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
13903 first_unchanged_at_end_row = NULL;
13904 }
13905 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
13906
13907
13908 /* Find the cursor if not already found. We have to decide whether
13909 PT will appear on this window (it sometimes doesn't, but this is
13910 not a very frequent case.) This decision has to be made before
13911 the current matrix is altered. A value of cursor.vpos < 0 means
13912 that PT is either in one of the lines beginning at
13913 first_unchanged_at_end_row or below the window. Don't care for
13914 lines that might be displayed later at the window end; as
13915 mentioned, this is not a frequent case. */
13916 if (w->cursor.vpos < 0)
13917 {
13918 /* Cursor in unchanged rows at the top? */
13919 if (PT < CHARPOS (start_pos)
13920 && last_unchanged_at_beg_row)
13921 {
13922 row = row_containing_pos (w, PT,
13923 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
13924 last_unchanged_at_beg_row + 1, 0);
13925 if (row)
13926 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13927 }
13928
13929 /* Start from first_unchanged_at_end_row looking for PT. */
13930 else if (first_unchanged_at_end_row)
13931 {
13932 row = row_containing_pos (w, PT - delta,
13933 first_unchanged_at_end_row, NULL, 0);
13934 if (row)
13935 set_cursor_from_row (w, row, w->current_matrix, delta,
13936 delta_bytes, dy, dvpos);
13937 }
13938
13939 /* Give up if cursor was not found. */
13940 if (w->cursor.vpos < 0)
13941 {
13942 clear_glyph_matrix (w->desired_matrix);
13943 return -1;
13944 }
13945 }
13946
13947 /* Don't let the cursor end in the scroll margins. */
13948 {
13949 int this_scroll_margin, cursor_height;
13950
13951 this_scroll_margin = max (0, scroll_margin);
13952 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13953 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13954 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
13955
13956 if ((w->cursor.y < this_scroll_margin
13957 && CHARPOS (start) > BEGV)
13958 /* Old redisplay didn't take scroll margin into account at the bottom,
13959 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
13960 || (w->cursor.y + (make_cursor_line_fully_visible_p
13961 ? cursor_height + this_scroll_margin
13962 : 1)) > it.last_visible_y)
13963 {
13964 w->cursor.vpos = -1;
13965 clear_glyph_matrix (w->desired_matrix);
13966 return -1;
13967 }
13968 }
13969
13970 /* Scroll the display. Do it before changing the current matrix so
13971 that xterm.c doesn't get confused about where the cursor glyph is
13972 found. */
13973 if (dy && run.height)
13974 {
13975 update_begin (f);
13976
13977 if (FRAME_WINDOW_P (f))
13978 {
13979 rif->update_window_begin_hook (w);
13980 rif->clear_window_mouse_face (w);
13981 rif->scroll_run_hook (w, &run);
13982 rif->update_window_end_hook (w, 0, 0);
13983 }
13984 else
13985 {
13986 /* Terminal frame. In this case, dvpos gives the number of
13987 lines to scroll by; dvpos < 0 means scroll up. */
13988 int first_unchanged_at_end_vpos
13989 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
13990 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
13991 int end = (WINDOW_TOP_EDGE_LINE (w)
13992 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
13993 + window_internal_height (w));
13994
13995 /* Perform the operation on the screen. */
13996 if (dvpos > 0)
13997 {
13998 /* Scroll last_unchanged_at_beg_row to the end of the
13999 window down dvpos lines. */
14000 set_terminal_window (end);
14001
14002 /* On dumb terminals delete dvpos lines at the end
14003 before inserting dvpos empty lines. */
14004 if (!scroll_region_ok)
14005 ins_del_lines (end - dvpos, -dvpos);
14006
14007 /* Insert dvpos empty lines in front of
14008 last_unchanged_at_beg_row. */
14009 ins_del_lines (from, dvpos);
14010 }
14011 else if (dvpos < 0)
14012 {
14013 /* Scroll up last_unchanged_at_beg_vpos to the end of
14014 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14015 set_terminal_window (end);
14016
14017 /* Delete dvpos lines in front of
14018 last_unchanged_at_beg_vpos. ins_del_lines will set
14019 the cursor to the given vpos and emit |dvpos| delete
14020 line sequences. */
14021 ins_del_lines (from + dvpos, dvpos);
14022
14023 /* On a dumb terminal insert dvpos empty lines at the
14024 end. */
14025 if (!scroll_region_ok)
14026 ins_del_lines (end + dvpos, -dvpos);
14027 }
14028
14029 set_terminal_window (0);
14030 }
14031
14032 update_end (f);
14033 }
14034
14035 /* Shift reused rows of the current matrix to the right position.
14036 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14037 text. */
14038 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14039 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14040 if (dvpos < 0)
14041 {
14042 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14043 bottom_vpos, dvpos);
14044 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14045 bottom_vpos, 0);
14046 }
14047 else if (dvpos > 0)
14048 {
14049 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14050 bottom_vpos, dvpos);
14051 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14052 first_unchanged_at_end_vpos + dvpos, 0);
14053 }
14054
14055 /* For frame-based redisplay, make sure that current frame and window
14056 matrix are in sync with respect to glyph memory. */
14057 if (!FRAME_WINDOW_P (f))
14058 sync_frame_with_window_matrix_rows (w);
14059
14060 /* Adjust buffer positions in reused rows. */
14061 if (delta)
14062 increment_matrix_positions (current_matrix,
14063 first_unchanged_at_end_vpos + dvpos,
14064 bottom_vpos, delta, delta_bytes);
14065
14066 /* Adjust Y positions. */
14067 if (dy)
14068 shift_glyph_matrix (w, current_matrix,
14069 first_unchanged_at_end_vpos + dvpos,
14070 bottom_vpos, dy);
14071
14072 if (first_unchanged_at_end_row)
14073 {
14074 first_unchanged_at_end_row += dvpos;
14075 if (first_unchanged_at_end_row->y >= it.last_visible_y
14076 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14077 first_unchanged_at_end_row = NULL;
14078 }
14079
14080 /* If scrolling up, there may be some lines to display at the end of
14081 the window. */
14082 last_text_row_at_end = NULL;
14083 if (dy < 0)
14084 {
14085 /* Scrolling up can leave for example a partially visible line
14086 at the end of the window to be redisplayed. */
14087 /* Set last_row to the glyph row in the current matrix where the
14088 window end line is found. It has been moved up or down in
14089 the matrix by dvpos. */
14090 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14091 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14092
14093 /* If last_row is the window end line, it should display text. */
14094 xassert (last_row->displays_text_p);
14095
14096 /* If window end line was partially visible before, begin
14097 displaying at that line. Otherwise begin displaying with the
14098 line following it. */
14099 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14100 {
14101 init_to_row_start (&it, w, last_row);
14102 it.vpos = last_vpos;
14103 it.current_y = last_row->y;
14104 }
14105 else
14106 {
14107 init_to_row_end (&it, w, last_row);
14108 it.vpos = 1 + last_vpos;
14109 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14110 ++last_row;
14111 }
14112
14113 /* We may start in a continuation line. If so, we have to
14114 get the right continuation_lines_width and current_x. */
14115 it.continuation_lines_width = last_row->continuation_lines_width;
14116 it.hpos = it.current_x = 0;
14117
14118 /* Display the rest of the lines at the window end. */
14119 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14120 while (it.current_y < it.last_visible_y
14121 && !fonts_changed_p)
14122 {
14123 /* Is it always sure that the display agrees with lines in
14124 the current matrix? I don't think so, so we mark rows
14125 displayed invalid in the current matrix by setting their
14126 enabled_p flag to zero. */
14127 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14128 if (display_line (&it))
14129 last_text_row_at_end = it.glyph_row - 1;
14130 }
14131 }
14132
14133 /* Update window_end_pos and window_end_vpos. */
14134 if (first_unchanged_at_end_row
14135 && !last_text_row_at_end)
14136 {
14137 /* Window end line if one of the preserved rows from the current
14138 matrix. Set row to the last row displaying text in current
14139 matrix starting at first_unchanged_at_end_row, after
14140 scrolling. */
14141 xassert (first_unchanged_at_end_row->displays_text_p);
14142 row = find_last_row_displaying_text (w->current_matrix, &it,
14143 first_unchanged_at_end_row);
14144 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14145
14146 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14147 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14148 w->window_end_vpos
14149 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14150 xassert (w->window_end_bytepos >= 0);
14151 IF_DEBUG (debug_method_add (w, "A"));
14152 }
14153 else if (last_text_row_at_end)
14154 {
14155 w->window_end_pos
14156 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14157 w->window_end_bytepos
14158 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14159 w->window_end_vpos
14160 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14161 xassert (w->window_end_bytepos >= 0);
14162 IF_DEBUG (debug_method_add (w, "B"));
14163 }
14164 else if (last_text_row)
14165 {
14166 /* We have displayed either to the end of the window or at the
14167 end of the window, i.e. the last row with text is to be found
14168 in the desired matrix. */
14169 w->window_end_pos
14170 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14171 w->window_end_bytepos
14172 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14173 w->window_end_vpos
14174 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14175 xassert (w->window_end_bytepos >= 0);
14176 }
14177 else if (first_unchanged_at_end_row == NULL
14178 && last_text_row == NULL
14179 && last_text_row_at_end == NULL)
14180 {
14181 /* Displayed to end of window, but no line containing text was
14182 displayed. Lines were deleted at the end of the window. */
14183 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14184 int vpos = XFASTINT (w->window_end_vpos);
14185 struct glyph_row *current_row = current_matrix->rows + vpos;
14186 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14187
14188 for (row = NULL;
14189 row == NULL && vpos >= first_vpos;
14190 --vpos, --current_row, --desired_row)
14191 {
14192 if (desired_row->enabled_p)
14193 {
14194 if (desired_row->displays_text_p)
14195 row = desired_row;
14196 }
14197 else if (current_row->displays_text_p)
14198 row = current_row;
14199 }
14200
14201 xassert (row != NULL);
14202 w->window_end_vpos = make_number (vpos + 1);
14203 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14204 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14205 xassert (w->window_end_bytepos >= 0);
14206 IF_DEBUG (debug_method_add (w, "C"));
14207 }
14208 else
14209 abort ();
14210
14211 #if 0 /* This leads to problems, for instance when the cursor is
14212 at ZV, and the cursor line displays no text. */
14213 /* Disable rows below what's displayed in the window. This makes
14214 debugging easier. */
14215 enable_glyph_matrix_rows (current_matrix,
14216 XFASTINT (w->window_end_vpos) + 1,
14217 bottom_vpos, 0);
14218 #endif
14219
14220 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14221 debug_end_vpos = XFASTINT (w->window_end_vpos));
14222
14223 /* Record that display has not been completed. */
14224 w->window_end_valid = Qnil;
14225 w->desired_matrix->no_scrolling_p = 1;
14226 return 3;
14227
14228 #undef GIVE_UP
14229 }
14230
14231
14232 \f
14233 /***********************************************************************
14234 More debugging support
14235 ***********************************************************************/
14236
14237 #if GLYPH_DEBUG
14238
14239 void dump_glyph_row P_ ((struct glyph_row *, int, int));
14240 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
14241 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
14242
14243
14244 /* Dump the contents of glyph matrix MATRIX on stderr.
14245
14246 GLYPHS 0 means don't show glyph contents.
14247 GLYPHS 1 means show glyphs in short form
14248 GLYPHS > 1 means show glyphs in long form. */
14249
14250 void
14251 dump_glyph_matrix (matrix, glyphs)
14252 struct glyph_matrix *matrix;
14253 int glyphs;
14254 {
14255 int i;
14256 for (i = 0; i < matrix->nrows; ++i)
14257 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
14258 }
14259
14260
14261 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
14262 the glyph row and area where the glyph comes from. */
14263
14264 void
14265 dump_glyph (row, glyph, area)
14266 struct glyph_row *row;
14267 struct glyph *glyph;
14268 int area;
14269 {
14270 if (glyph->type == CHAR_GLYPH)
14271 {
14272 fprintf (stderr,
14273 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14274 glyph - row->glyphs[TEXT_AREA],
14275 'C',
14276 glyph->charpos,
14277 (BUFFERP (glyph->object)
14278 ? 'B'
14279 : (STRINGP (glyph->object)
14280 ? 'S'
14281 : '-')),
14282 glyph->pixel_width,
14283 glyph->u.ch,
14284 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
14285 ? glyph->u.ch
14286 : '.'),
14287 glyph->face_id,
14288 glyph->left_box_line_p,
14289 glyph->right_box_line_p);
14290 }
14291 else if (glyph->type == STRETCH_GLYPH)
14292 {
14293 fprintf (stderr,
14294 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14295 glyph - row->glyphs[TEXT_AREA],
14296 'S',
14297 glyph->charpos,
14298 (BUFFERP (glyph->object)
14299 ? 'B'
14300 : (STRINGP (glyph->object)
14301 ? 'S'
14302 : '-')),
14303 glyph->pixel_width,
14304 0,
14305 '.',
14306 glyph->face_id,
14307 glyph->left_box_line_p,
14308 glyph->right_box_line_p);
14309 }
14310 else if (glyph->type == IMAGE_GLYPH)
14311 {
14312 fprintf (stderr,
14313 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
14314 glyph - row->glyphs[TEXT_AREA],
14315 'I',
14316 glyph->charpos,
14317 (BUFFERP (glyph->object)
14318 ? 'B'
14319 : (STRINGP (glyph->object)
14320 ? 'S'
14321 : '-')),
14322 glyph->pixel_width,
14323 glyph->u.img_id,
14324 '.',
14325 glyph->face_id,
14326 glyph->left_box_line_p,
14327 glyph->right_box_line_p);
14328 }
14329 }
14330
14331
14332 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
14333 GLYPHS 0 means don't show glyph contents.
14334 GLYPHS 1 means show glyphs in short form
14335 GLYPHS > 1 means show glyphs in long form. */
14336
14337 void
14338 dump_glyph_row (row, vpos, glyphs)
14339 struct glyph_row *row;
14340 int vpos, glyphs;
14341 {
14342 if (glyphs != 1)
14343 {
14344 fprintf (stderr, "Row Start End Used oEI><\\CTZFesm X Y W H V A P\n");
14345 fprintf (stderr, "======================================================================\n");
14346
14347 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
14348 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
14349 vpos,
14350 MATRIX_ROW_START_CHARPOS (row),
14351 MATRIX_ROW_END_CHARPOS (row),
14352 row->used[TEXT_AREA],
14353 row->contains_overlapping_glyphs_p,
14354 row->enabled_p,
14355 row->truncated_on_left_p,
14356 row->truncated_on_right_p,
14357 row->continued_p,
14358 MATRIX_ROW_CONTINUATION_LINE_P (row),
14359 row->displays_text_p,
14360 row->ends_at_zv_p,
14361 row->fill_line_p,
14362 row->ends_in_middle_of_char_p,
14363 row->starts_in_middle_of_char_p,
14364 row->mouse_face_p,
14365 row->x,
14366 row->y,
14367 row->pixel_width,
14368 row->height,
14369 row->visible_height,
14370 row->ascent,
14371 row->phys_ascent);
14372 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
14373 row->end.overlay_string_index,
14374 row->continuation_lines_width);
14375 fprintf (stderr, "%9d %5d\n",
14376 CHARPOS (row->start.string_pos),
14377 CHARPOS (row->end.string_pos));
14378 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
14379 row->end.dpvec_index);
14380 }
14381
14382 if (glyphs > 1)
14383 {
14384 int area;
14385
14386 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14387 {
14388 struct glyph *glyph = row->glyphs[area];
14389 struct glyph *glyph_end = glyph + row->used[area];
14390
14391 /* Glyph for a line end in text. */
14392 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
14393 ++glyph_end;
14394
14395 if (glyph < glyph_end)
14396 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
14397
14398 for (; glyph < glyph_end; ++glyph)
14399 dump_glyph (row, glyph, area);
14400 }
14401 }
14402 else if (glyphs == 1)
14403 {
14404 int area;
14405
14406 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14407 {
14408 char *s = (char *) alloca (row->used[area] + 1);
14409 int i;
14410
14411 for (i = 0; i < row->used[area]; ++i)
14412 {
14413 struct glyph *glyph = row->glyphs[area] + i;
14414 if (glyph->type == CHAR_GLYPH
14415 && glyph->u.ch < 0x80
14416 && glyph->u.ch >= ' ')
14417 s[i] = glyph->u.ch;
14418 else
14419 s[i] = '.';
14420 }
14421
14422 s[i] = '\0';
14423 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
14424 }
14425 }
14426 }
14427
14428
14429 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
14430 Sdump_glyph_matrix, 0, 1, "p",
14431 doc: /* Dump the current matrix of the selected window to stderr.
14432 Shows contents of glyph row structures. With non-nil
14433 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
14434 glyphs in short form, otherwise show glyphs in long form. */)
14435 (glyphs)
14436 Lisp_Object glyphs;
14437 {
14438 struct window *w = XWINDOW (selected_window);
14439 struct buffer *buffer = XBUFFER (w->buffer);
14440
14441 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
14442 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
14443 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
14444 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
14445 fprintf (stderr, "=============================================\n");
14446 dump_glyph_matrix (w->current_matrix,
14447 NILP (glyphs) ? 0 : XINT (glyphs));
14448 return Qnil;
14449 }
14450
14451
14452 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
14453 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
14454 ()
14455 {
14456 struct frame *f = XFRAME (selected_frame);
14457 dump_glyph_matrix (f->current_matrix, 1);
14458 return Qnil;
14459 }
14460
14461
14462 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
14463 doc: /* Dump glyph row ROW to stderr.
14464 GLYPH 0 means don't dump glyphs.
14465 GLYPH 1 means dump glyphs in short form.
14466 GLYPH > 1 or omitted means dump glyphs in long form. */)
14467 (row, glyphs)
14468 Lisp_Object row, glyphs;
14469 {
14470 struct glyph_matrix *matrix;
14471 int vpos;
14472
14473 CHECK_NUMBER (row);
14474 matrix = XWINDOW (selected_window)->current_matrix;
14475 vpos = XINT (row);
14476 if (vpos >= 0 && vpos < matrix->nrows)
14477 dump_glyph_row (MATRIX_ROW (matrix, vpos),
14478 vpos,
14479 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14480 return Qnil;
14481 }
14482
14483
14484 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
14485 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
14486 GLYPH 0 means don't dump glyphs.
14487 GLYPH 1 means dump glyphs in short form.
14488 GLYPH > 1 or omitted means dump glyphs in long form. */)
14489 (row, glyphs)
14490 Lisp_Object row, glyphs;
14491 {
14492 struct frame *sf = SELECTED_FRAME ();
14493 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
14494 int vpos;
14495
14496 CHECK_NUMBER (row);
14497 vpos = XINT (row);
14498 if (vpos >= 0 && vpos < m->nrows)
14499 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
14500 INTEGERP (glyphs) ? XINT (glyphs) : 2);
14501 return Qnil;
14502 }
14503
14504
14505 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
14506 doc: /* Toggle tracing of redisplay.
14507 With ARG, turn tracing on if and only if ARG is positive. */)
14508 (arg)
14509 Lisp_Object arg;
14510 {
14511 if (NILP (arg))
14512 trace_redisplay_p = !trace_redisplay_p;
14513 else
14514 {
14515 arg = Fprefix_numeric_value (arg);
14516 trace_redisplay_p = XINT (arg) > 0;
14517 }
14518
14519 return Qnil;
14520 }
14521
14522
14523 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
14524 doc: /* Like `format', but print result to stderr.
14525 usage: (trace-to-stderr STRING &rest OBJECTS) */)
14526 (nargs, args)
14527 int nargs;
14528 Lisp_Object *args;
14529 {
14530 Lisp_Object s = Fformat (nargs, args);
14531 fprintf (stderr, "%s", SDATA (s));
14532 return Qnil;
14533 }
14534
14535 #endif /* GLYPH_DEBUG */
14536
14537
14538 \f
14539 /***********************************************************************
14540 Building Desired Matrix Rows
14541 ***********************************************************************/
14542
14543 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
14544 Used for non-window-redisplay windows, and for windows w/o left fringe. */
14545
14546 static struct glyph_row *
14547 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
14548 struct window *w;
14549 Lisp_Object overlay_arrow_string;
14550 {
14551 struct frame *f = XFRAME (WINDOW_FRAME (w));
14552 struct buffer *buffer = XBUFFER (w->buffer);
14553 struct buffer *old = current_buffer;
14554 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
14555 int arrow_len = SCHARS (overlay_arrow_string);
14556 const unsigned char *arrow_end = arrow_string + arrow_len;
14557 const unsigned char *p;
14558 struct it it;
14559 int multibyte_p;
14560 int n_glyphs_before;
14561
14562 set_buffer_temp (buffer);
14563 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
14564 it.glyph_row->used[TEXT_AREA] = 0;
14565 SET_TEXT_POS (it.position, 0, 0);
14566
14567 multibyte_p = !NILP (buffer->enable_multibyte_characters);
14568 p = arrow_string;
14569 while (p < arrow_end)
14570 {
14571 Lisp_Object face, ilisp;
14572
14573 /* Get the next character. */
14574 if (multibyte_p)
14575 it.c = string_char_and_length (p, arrow_len, &it.len);
14576 else
14577 it.c = *p, it.len = 1;
14578 p += it.len;
14579
14580 /* Get its face. */
14581 ilisp = make_number (p - arrow_string);
14582 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
14583 it.face_id = compute_char_face (f, it.c, face);
14584
14585 /* Compute its width, get its glyphs. */
14586 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
14587 SET_TEXT_POS (it.position, -1, -1);
14588 PRODUCE_GLYPHS (&it);
14589
14590 /* If this character doesn't fit any more in the line, we have
14591 to remove some glyphs. */
14592 if (it.current_x > it.last_visible_x)
14593 {
14594 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
14595 break;
14596 }
14597 }
14598
14599 set_buffer_temp (old);
14600 return it.glyph_row;
14601 }
14602
14603
14604 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
14605 glyphs are only inserted for terminal frames since we can't really
14606 win with truncation glyphs when partially visible glyphs are
14607 involved. Which glyphs to insert is determined by
14608 produce_special_glyphs. */
14609
14610 static void
14611 insert_left_trunc_glyphs (it)
14612 struct it *it;
14613 {
14614 struct it truncate_it;
14615 struct glyph *from, *end, *to, *toend;
14616
14617 xassert (!FRAME_WINDOW_P (it->f));
14618
14619 /* Get the truncation glyphs. */
14620 truncate_it = *it;
14621 truncate_it.current_x = 0;
14622 truncate_it.face_id = DEFAULT_FACE_ID;
14623 truncate_it.glyph_row = &scratch_glyph_row;
14624 truncate_it.glyph_row->used[TEXT_AREA] = 0;
14625 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
14626 truncate_it.object = make_number (0);
14627 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
14628
14629 /* Overwrite glyphs from IT with truncation glyphs. */
14630 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14631 end = from + truncate_it.glyph_row->used[TEXT_AREA];
14632 to = it->glyph_row->glyphs[TEXT_AREA];
14633 toend = to + it->glyph_row->used[TEXT_AREA];
14634
14635 while (from < end)
14636 *to++ = *from++;
14637
14638 /* There may be padding glyphs left over. Overwrite them too. */
14639 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
14640 {
14641 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
14642 while (from < end)
14643 *to++ = *from++;
14644 }
14645
14646 if (to > toend)
14647 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
14648 }
14649
14650
14651 /* Compute the pixel height and width of IT->glyph_row.
14652
14653 Most of the time, ascent and height of a display line will be equal
14654 to the max_ascent and max_height values of the display iterator
14655 structure. This is not the case if
14656
14657 1. We hit ZV without displaying anything. In this case, max_ascent
14658 and max_height will be zero.
14659
14660 2. We have some glyphs that don't contribute to the line height.
14661 (The glyph row flag contributes_to_line_height_p is for future
14662 pixmap extensions).
14663
14664 The first case is easily covered by using default values because in
14665 these cases, the line height does not really matter, except that it
14666 must not be zero. */
14667
14668 static void
14669 compute_line_metrics (it)
14670 struct it *it;
14671 {
14672 struct glyph_row *row = it->glyph_row;
14673 int area, i;
14674
14675 if (FRAME_WINDOW_P (it->f))
14676 {
14677 int i, min_y, max_y;
14678
14679 /* The line may consist of one space only, that was added to
14680 place the cursor on it. If so, the row's height hasn't been
14681 computed yet. */
14682 if (row->height == 0)
14683 {
14684 if (it->max_ascent + it->max_descent == 0)
14685 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
14686 row->ascent = it->max_ascent;
14687 row->height = it->max_ascent + it->max_descent;
14688 row->phys_ascent = it->max_phys_ascent;
14689 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
14690 row->extra_line_spacing = it->max_extra_line_spacing;
14691 }
14692
14693 /* Compute the width of this line. */
14694 row->pixel_width = row->x;
14695 for (i = 0; i < row->used[TEXT_AREA]; ++i)
14696 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
14697
14698 xassert (row->pixel_width >= 0);
14699 xassert (row->ascent >= 0 && row->height > 0);
14700
14701 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
14702 || MATRIX_ROW_OVERLAPS_PRED_P (row));
14703
14704 /* If first line's physical ascent is larger than its logical
14705 ascent, use the physical ascent, and make the row taller.
14706 This makes accented characters fully visible. */
14707 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
14708 && row->phys_ascent > row->ascent)
14709 {
14710 row->height += row->phys_ascent - row->ascent;
14711 row->ascent = row->phys_ascent;
14712 }
14713
14714 /* Compute how much of the line is visible. */
14715 row->visible_height = row->height;
14716
14717 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
14718 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
14719
14720 if (row->y < min_y)
14721 row->visible_height -= min_y - row->y;
14722 if (row->y + row->height > max_y)
14723 row->visible_height -= row->y + row->height - max_y;
14724 }
14725 else
14726 {
14727 row->pixel_width = row->used[TEXT_AREA];
14728 if (row->continued_p)
14729 row->pixel_width -= it->continuation_pixel_width;
14730 else if (row->truncated_on_right_p)
14731 row->pixel_width -= it->truncation_pixel_width;
14732 row->ascent = row->phys_ascent = 0;
14733 row->height = row->phys_height = row->visible_height = 1;
14734 row->extra_line_spacing = 0;
14735 }
14736
14737 /* Compute a hash code for this row. */
14738 row->hash = 0;
14739 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
14740 for (i = 0; i < row->used[area]; ++i)
14741 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
14742 + row->glyphs[area][i].u.val
14743 + row->glyphs[area][i].face_id
14744 + row->glyphs[area][i].padding_p
14745 + (row->glyphs[area][i].type << 2));
14746
14747 it->max_ascent = it->max_descent = 0;
14748 it->max_phys_ascent = it->max_phys_descent = 0;
14749 }
14750
14751
14752 /* Append one space to the glyph row of iterator IT if doing a
14753 window-based redisplay. The space has the same face as
14754 IT->face_id. Value is non-zero if a space was added.
14755
14756 This function is called to make sure that there is always one glyph
14757 at the end of a glyph row that the cursor can be set on under
14758 window-systems. (If there weren't such a glyph we would not know
14759 how wide and tall a box cursor should be displayed).
14760
14761 At the same time this space let's a nicely handle clearing to the
14762 end of the line if the row ends in italic text. */
14763
14764 static int
14765 append_space_for_newline (it, default_face_p)
14766 struct it *it;
14767 int default_face_p;
14768 {
14769 if (FRAME_WINDOW_P (it->f))
14770 {
14771 int n = it->glyph_row->used[TEXT_AREA];
14772
14773 if (it->glyph_row->glyphs[TEXT_AREA] + n
14774 < it->glyph_row->glyphs[1 + TEXT_AREA])
14775 {
14776 /* Save some values that must not be changed.
14777 Must save IT->c and IT->len because otherwise
14778 ITERATOR_AT_END_P wouldn't work anymore after
14779 append_space_for_newline has been called. */
14780 enum display_element_type saved_what = it->what;
14781 int saved_c = it->c, saved_len = it->len;
14782 int saved_x = it->current_x;
14783 int saved_face_id = it->face_id;
14784 struct text_pos saved_pos;
14785 Lisp_Object saved_object;
14786 struct face *face;
14787
14788 saved_object = it->object;
14789 saved_pos = it->position;
14790
14791 it->what = IT_CHARACTER;
14792 bzero (&it->position, sizeof it->position);
14793 it->object = make_number (0);
14794 it->c = ' ';
14795 it->len = 1;
14796
14797 if (default_face_p)
14798 it->face_id = DEFAULT_FACE_ID;
14799 else if (it->face_before_selective_p)
14800 it->face_id = it->saved_face_id;
14801 face = FACE_FROM_ID (it->f, it->face_id);
14802 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
14803
14804 PRODUCE_GLYPHS (it);
14805
14806 it->override_ascent = -1;
14807 it->constrain_row_ascent_descent_p = 0;
14808 it->current_x = saved_x;
14809 it->object = saved_object;
14810 it->position = saved_pos;
14811 it->what = saved_what;
14812 it->face_id = saved_face_id;
14813 it->len = saved_len;
14814 it->c = saved_c;
14815 return 1;
14816 }
14817 }
14818
14819 return 0;
14820 }
14821
14822
14823 /* Extend the face of the last glyph in the text area of IT->glyph_row
14824 to the end of the display line. Called from display_line.
14825 If the glyph row is empty, add a space glyph to it so that we
14826 know the face to draw. Set the glyph row flag fill_line_p. */
14827
14828 static void
14829 extend_face_to_end_of_line (it)
14830 struct it *it;
14831 {
14832 struct face *face;
14833 struct frame *f = it->f;
14834
14835 /* If line is already filled, do nothing. */
14836 if (it->current_x >= it->last_visible_x)
14837 return;
14838
14839 /* Face extension extends the background and box of IT->face_id
14840 to the end of the line. If the background equals the background
14841 of the frame, we don't have to do anything. */
14842 if (it->face_before_selective_p)
14843 face = FACE_FROM_ID (it->f, it->saved_face_id);
14844 else
14845 face = FACE_FROM_ID (f, it->face_id);
14846
14847 if (FRAME_WINDOW_P (f)
14848 && face->box == FACE_NO_BOX
14849 && face->background == FRAME_BACKGROUND_PIXEL (f)
14850 && !face->stipple)
14851 return;
14852
14853 /* Set the glyph row flag indicating that the face of the last glyph
14854 in the text area has to be drawn to the end of the text area. */
14855 it->glyph_row->fill_line_p = 1;
14856
14857 /* If current character of IT is not ASCII, make sure we have the
14858 ASCII face. This will be automatically undone the next time
14859 get_next_display_element returns a multibyte character. Note
14860 that the character will always be single byte in unibyte text. */
14861 if (!SINGLE_BYTE_CHAR_P (it->c))
14862 {
14863 it->face_id = FACE_FOR_CHAR (f, face, 0);
14864 }
14865
14866 if (FRAME_WINDOW_P (f))
14867 {
14868 /* If the row is empty, add a space with the current face of IT,
14869 so that we know which face to draw. */
14870 if (it->glyph_row->used[TEXT_AREA] == 0)
14871 {
14872 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
14873 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
14874 it->glyph_row->used[TEXT_AREA] = 1;
14875 }
14876 }
14877 else
14878 {
14879 /* Save some values that must not be changed. */
14880 int saved_x = it->current_x;
14881 struct text_pos saved_pos;
14882 Lisp_Object saved_object;
14883 enum display_element_type saved_what = it->what;
14884 int saved_face_id = it->face_id;
14885
14886 saved_object = it->object;
14887 saved_pos = it->position;
14888
14889 it->what = IT_CHARACTER;
14890 bzero (&it->position, sizeof it->position);
14891 it->object = make_number (0);
14892 it->c = ' ';
14893 it->len = 1;
14894 it->face_id = face->id;
14895
14896 PRODUCE_GLYPHS (it);
14897
14898 while (it->current_x <= it->last_visible_x)
14899 PRODUCE_GLYPHS (it);
14900
14901 /* Don't count these blanks really. It would let us insert a left
14902 truncation glyph below and make us set the cursor on them, maybe. */
14903 it->current_x = saved_x;
14904 it->object = saved_object;
14905 it->position = saved_pos;
14906 it->what = saved_what;
14907 it->face_id = saved_face_id;
14908 }
14909 }
14910
14911
14912 /* Value is non-zero if text starting at CHARPOS in current_buffer is
14913 trailing whitespace. */
14914
14915 static int
14916 trailing_whitespace_p (charpos)
14917 int charpos;
14918 {
14919 int bytepos = CHAR_TO_BYTE (charpos);
14920 int c = 0;
14921
14922 while (bytepos < ZV_BYTE
14923 && (c = FETCH_CHAR (bytepos),
14924 c == ' ' || c == '\t'))
14925 ++bytepos;
14926
14927 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
14928 {
14929 if (bytepos != PT_BYTE)
14930 return 1;
14931 }
14932 return 0;
14933 }
14934
14935
14936 /* Highlight trailing whitespace, if any, in ROW. */
14937
14938 void
14939 highlight_trailing_whitespace (f, row)
14940 struct frame *f;
14941 struct glyph_row *row;
14942 {
14943 int used = row->used[TEXT_AREA];
14944
14945 if (used)
14946 {
14947 struct glyph *start = row->glyphs[TEXT_AREA];
14948 struct glyph *glyph = start + used - 1;
14949
14950 /* Skip over glyphs inserted to display the cursor at the
14951 end of a line, for extending the face of the last glyph
14952 to the end of the line on terminals, and for truncation
14953 and continuation glyphs. */
14954 while (glyph >= start
14955 && glyph->type == CHAR_GLYPH
14956 && INTEGERP (glyph->object))
14957 --glyph;
14958
14959 /* If last glyph is a space or stretch, and it's trailing
14960 whitespace, set the face of all trailing whitespace glyphs in
14961 IT->glyph_row to `trailing-whitespace'. */
14962 if (glyph >= start
14963 && BUFFERP (glyph->object)
14964 && (glyph->type == STRETCH_GLYPH
14965 || (glyph->type == CHAR_GLYPH
14966 && glyph->u.ch == ' '))
14967 && trailing_whitespace_p (glyph->charpos))
14968 {
14969 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
14970 if (face_id < 0)
14971 return;
14972
14973 while (glyph >= start
14974 && BUFFERP (glyph->object)
14975 && (glyph->type == STRETCH_GLYPH
14976 || (glyph->type == CHAR_GLYPH
14977 && glyph->u.ch == ' ')))
14978 (glyph--)->face_id = face_id;
14979 }
14980 }
14981 }
14982
14983
14984 /* Value is non-zero if glyph row ROW in window W should be
14985 used to hold the cursor. */
14986
14987 static int
14988 cursor_row_p (w, row)
14989 struct window *w;
14990 struct glyph_row *row;
14991 {
14992 int cursor_row_p = 1;
14993
14994 if (PT == MATRIX_ROW_END_CHARPOS (row))
14995 {
14996 /* If the row ends with a newline from a string, we don't want
14997 the cursor there (if the row is continued it doesn't end in a
14998 newline). */
14999 if (CHARPOS (row->end.string_pos) >= 0)
15000 cursor_row_p = row->continued_p;
15001 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15002 {
15003 /* If the row ends in middle of a real character,
15004 and the line is continued, we want the cursor here.
15005 That's because MATRIX_ROW_END_CHARPOS would equal
15006 PT if PT is before the character. */
15007 if (!row->ends_in_ellipsis_p)
15008 cursor_row_p = row->continued_p;
15009 else
15010 /* If the row ends in an ellipsis, then
15011 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15012 We want that position to be displayed after the ellipsis. */
15013 cursor_row_p = 0;
15014 }
15015 /* If the row ends at ZV, display the cursor at the end of that
15016 row instead of at the start of the row below. */
15017 else if (row->ends_at_zv_p)
15018 cursor_row_p = 1;
15019 else
15020 cursor_row_p = 0;
15021 }
15022
15023 return cursor_row_p;
15024 }
15025
15026
15027 /* Construct the glyph row IT->glyph_row in the desired matrix of
15028 IT->w from text at the current position of IT. See dispextern.h
15029 for an overview of struct it. Value is non-zero if
15030 IT->glyph_row displays text, as opposed to a line displaying ZV
15031 only. */
15032
15033 static int
15034 display_line (it)
15035 struct it *it;
15036 {
15037 struct glyph_row *row = it->glyph_row;
15038 Lisp_Object overlay_arrow_string;
15039
15040 /* We always start displaying at hpos zero even if hscrolled. */
15041 xassert (it->hpos == 0 && it->current_x == 0);
15042
15043 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15044 >= it->w->desired_matrix->nrows)
15045 {
15046 it->w->nrows_scale_factor++;
15047 fonts_changed_p = 1;
15048 return 0;
15049 }
15050
15051 /* Is IT->w showing the region? */
15052 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15053
15054 /* Clear the result glyph row and enable it. */
15055 prepare_desired_row (row);
15056
15057 row->y = it->current_y;
15058 row->start = it->start;
15059 row->continuation_lines_width = it->continuation_lines_width;
15060 row->displays_text_p = 1;
15061 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15062 it->starts_in_middle_of_char_p = 0;
15063
15064 /* Arrange the overlays nicely for our purposes. Usually, we call
15065 display_line on only one line at a time, in which case this
15066 can't really hurt too much, or we call it on lines which appear
15067 one after another in the buffer, in which case all calls to
15068 recenter_overlay_lists but the first will be pretty cheap. */
15069 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15070
15071 /* Move over display elements that are not visible because we are
15072 hscrolled. This may stop at an x-position < IT->first_visible_x
15073 if the first glyph is partially visible or if we hit a line end. */
15074 if (it->current_x < it->first_visible_x)
15075 {
15076 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15077 MOVE_TO_POS | MOVE_TO_X);
15078 }
15079
15080 /* Get the initial row height. This is either the height of the
15081 text hscrolled, if there is any, or zero. */
15082 row->ascent = it->max_ascent;
15083 row->height = it->max_ascent + it->max_descent;
15084 row->phys_ascent = it->max_phys_ascent;
15085 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15086 row->extra_line_spacing = it->max_extra_line_spacing;
15087
15088 /* Loop generating characters. The loop is left with IT on the next
15089 character to display. */
15090 while (1)
15091 {
15092 int n_glyphs_before, hpos_before, x_before;
15093 int x, i, nglyphs;
15094 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15095
15096 /* Retrieve the next thing to display. Value is zero if end of
15097 buffer reached. */
15098 if (!get_next_display_element (it))
15099 {
15100 /* Maybe add a space at the end of this line that is used to
15101 display the cursor there under X. Set the charpos of the
15102 first glyph of blank lines not corresponding to any text
15103 to -1. */
15104 #ifdef HAVE_WINDOW_SYSTEM
15105 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15106 row->exact_window_width_line_p = 1;
15107 else
15108 #endif /* HAVE_WINDOW_SYSTEM */
15109 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15110 || row->used[TEXT_AREA] == 0)
15111 {
15112 row->glyphs[TEXT_AREA]->charpos = -1;
15113 row->displays_text_p = 0;
15114
15115 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15116 && (!MINI_WINDOW_P (it->w)
15117 || (minibuf_level && EQ (it->window, minibuf_window))))
15118 row->indicate_empty_line_p = 1;
15119 }
15120
15121 it->continuation_lines_width = 0;
15122 row->ends_at_zv_p = 1;
15123 break;
15124 }
15125
15126 /* Now, get the metrics of what we want to display. This also
15127 generates glyphs in `row' (which is IT->glyph_row). */
15128 n_glyphs_before = row->used[TEXT_AREA];
15129 x = it->current_x;
15130
15131 /* Remember the line height so far in case the next element doesn't
15132 fit on the line. */
15133 if (!it->truncate_lines_p)
15134 {
15135 ascent = it->max_ascent;
15136 descent = it->max_descent;
15137 phys_ascent = it->max_phys_ascent;
15138 phys_descent = it->max_phys_descent;
15139 }
15140
15141 PRODUCE_GLYPHS (it);
15142
15143 /* If this display element was in marginal areas, continue with
15144 the next one. */
15145 if (it->area != TEXT_AREA)
15146 {
15147 row->ascent = max (row->ascent, it->max_ascent);
15148 row->height = max (row->height, it->max_ascent + it->max_descent);
15149 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15150 row->phys_height = max (row->phys_height,
15151 it->max_phys_ascent + it->max_phys_descent);
15152 row->extra_line_spacing = max (row->extra_line_spacing,
15153 it->max_extra_line_spacing);
15154 set_iterator_to_next (it, 1);
15155 continue;
15156 }
15157
15158 /* Does the display element fit on the line? If we truncate
15159 lines, we should draw past the right edge of the window. If
15160 we don't truncate, we want to stop so that we can display the
15161 continuation glyph before the right margin. If lines are
15162 continued, there are two possible strategies for characters
15163 resulting in more than 1 glyph (e.g. tabs): Display as many
15164 glyphs as possible in this line and leave the rest for the
15165 continuation line, or display the whole element in the next
15166 line. Original redisplay did the former, so we do it also. */
15167 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15168 hpos_before = it->hpos;
15169 x_before = x;
15170
15171 if (/* Not a newline. */
15172 nglyphs > 0
15173 /* Glyphs produced fit entirely in the line. */
15174 && it->current_x < it->last_visible_x)
15175 {
15176 it->hpos += nglyphs;
15177 row->ascent = max (row->ascent, it->max_ascent);
15178 row->height = max (row->height, it->max_ascent + it->max_descent);
15179 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15180 row->phys_height = max (row->phys_height,
15181 it->max_phys_ascent + it->max_phys_descent);
15182 row->extra_line_spacing = max (row->extra_line_spacing,
15183 it->max_extra_line_spacing);
15184 if (it->current_x - it->pixel_width < it->first_visible_x)
15185 row->x = x - it->first_visible_x;
15186 }
15187 else
15188 {
15189 int new_x;
15190 struct glyph *glyph;
15191
15192 for (i = 0; i < nglyphs; ++i, x = new_x)
15193 {
15194 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15195 new_x = x + glyph->pixel_width;
15196
15197 if (/* Lines are continued. */
15198 !it->truncate_lines_p
15199 && (/* Glyph doesn't fit on the line. */
15200 new_x > it->last_visible_x
15201 /* Or it fits exactly on a window system frame. */
15202 || (new_x == it->last_visible_x
15203 && FRAME_WINDOW_P (it->f))))
15204 {
15205 /* End of a continued line. */
15206
15207 if (it->hpos == 0
15208 || (new_x == it->last_visible_x
15209 && FRAME_WINDOW_P (it->f)))
15210 {
15211 /* Current glyph is the only one on the line or
15212 fits exactly on the line. We must continue
15213 the line because we can't draw the cursor
15214 after the glyph. */
15215 row->continued_p = 1;
15216 it->current_x = new_x;
15217 it->continuation_lines_width += new_x;
15218 ++it->hpos;
15219 if (i == nglyphs - 1)
15220 {
15221 set_iterator_to_next (it, 1);
15222 #ifdef HAVE_WINDOW_SYSTEM
15223 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15224 {
15225 if (!get_next_display_element (it))
15226 {
15227 row->exact_window_width_line_p = 1;
15228 it->continuation_lines_width = 0;
15229 row->continued_p = 0;
15230 row->ends_at_zv_p = 1;
15231 }
15232 else if (ITERATOR_AT_END_OF_LINE_P (it))
15233 {
15234 row->continued_p = 0;
15235 row->exact_window_width_line_p = 1;
15236 }
15237 }
15238 #endif /* HAVE_WINDOW_SYSTEM */
15239 }
15240 }
15241 else if (CHAR_GLYPH_PADDING_P (*glyph)
15242 && !FRAME_WINDOW_P (it->f))
15243 {
15244 /* A padding glyph that doesn't fit on this line.
15245 This means the whole character doesn't fit
15246 on the line. */
15247 row->used[TEXT_AREA] = n_glyphs_before;
15248
15249 /* Fill the rest of the row with continuation
15250 glyphs like in 20.x. */
15251 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
15252 < row->glyphs[1 + TEXT_AREA])
15253 produce_special_glyphs (it, IT_CONTINUATION);
15254
15255 row->continued_p = 1;
15256 it->current_x = x_before;
15257 it->continuation_lines_width += x_before;
15258
15259 /* Restore the height to what it was before the
15260 element not fitting on the line. */
15261 it->max_ascent = ascent;
15262 it->max_descent = descent;
15263 it->max_phys_ascent = phys_ascent;
15264 it->max_phys_descent = phys_descent;
15265 }
15266 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
15267 {
15268 /* A TAB that extends past the right edge of the
15269 window. This produces a single glyph on
15270 window system frames. We leave the glyph in
15271 this row and let it fill the row, but don't
15272 consume the TAB. */
15273 it->continuation_lines_width += it->last_visible_x;
15274 row->ends_in_middle_of_char_p = 1;
15275 row->continued_p = 1;
15276 glyph->pixel_width = it->last_visible_x - x;
15277 it->starts_in_middle_of_char_p = 1;
15278 }
15279 else
15280 {
15281 /* Something other than a TAB that draws past
15282 the right edge of the window. Restore
15283 positions to values before the element. */
15284 row->used[TEXT_AREA] = n_glyphs_before + i;
15285
15286 /* Display continuation glyphs. */
15287 if (!FRAME_WINDOW_P (it->f))
15288 produce_special_glyphs (it, IT_CONTINUATION);
15289 row->continued_p = 1;
15290
15291 it->continuation_lines_width += x;
15292
15293 if (nglyphs > 1 && i > 0)
15294 {
15295 row->ends_in_middle_of_char_p = 1;
15296 it->starts_in_middle_of_char_p = 1;
15297 }
15298
15299 /* Restore the height to what it was before the
15300 element not fitting on the line. */
15301 it->max_ascent = ascent;
15302 it->max_descent = descent;
15303 it->max_phys_ascent = phys_ascent;
15304 it->max_phys_descent = phys_descent;
15305 }
15306
15307 break;
15308 }
15309 else if (new_x > it->first_visible_x)
15310 {
15311 /* Increment number of glyphs actually displayed. */
15312 ++it->hpos;
15313
15314 if (x < it->first_visible_x)
15315 /* Glyph is partially visible, i.e. row starts at
15316 negative X position. */
15317 row->x = x - it->first_visible_x;
15318 }
15319 else
15320 {
15321 /* Glyph is completely off the left margin of the
15322 window. This should not happen because of the
15323 move_it_in_display_line at the start of this
15324 function, unless the text display area of the
15325 window is empty. */
15326 xassert (it->first_visible_x <= it->last_visible_x);
15327 }
15328 }
15329
15330 row->ascent = max (row->ascent, it->max_ascent);
15331 row->height = max (row->height, it->max_ascent + it->max_descent);
15332 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15333 row->phys_height = max (row->phys_height,
15334 it->max_phys_ascent + it->max_phys_descent);
15335 row->extra_line_spacing = max (row->extra_line_spacing,
15336 it->max_extra_line_spacing);
15337
15338 /* End of this display line if row is continued. */
15339 if (row->continued_p || row->ends_at_zv_p)
15340 break;
15341 }
15342
15343 at_end_of_line:
15344 /* Is this a line end? If yes, we're also done, after making
15345 sure that a non-default face is extended up to the right
15346 margin of the window. */
15347 if (ITERATOR_AT_END_OF_LINE_P (it))
15348 {
15349 int used_before = row->used[TEXT_AREA];
15350
15351 row->ends_in_newline_from_string_p = STRINGP (it->object);
15352
15353 #ifdef HAVE_WINDOW_SYSTEM
15354 /* Add a space at the end of the line that is used to
15355 display the cursor there. */
15356 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15357 append_space_for_newline (it, 0);
15358 #endif /* HAVE_WINDOW_SYSTEM */
15359
15360 /* Extend the face to the end of the line. */
15361 extend_face_to_end_of_line (it);
15362
15363 /* Make sure we have the position. */
15364 if (used_before == 0)
15365 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
15366
15367 /* Consume the line end. This skips over invisible lines. */
15368 set_iterator_to_next (it, 1);
15369 it->continuation_lines_width = 0;
15370 break;
15371 }
15372
15373 /* Proceed with next display element. Note that this skips
15374 over lines invisible because of selective display. */
15375 set_iterator_to_next (it, 1);
15376
15377 /* If we truncate lines, we are done when the last displayed
15378 glyphs reach past the right margin of the window. */
15379 if (it->truncate_lines_p
15380 && (FRAME_WINDOW_P (it->f)
15381 ? (it->current_x >= it->last_visible_x)
15382 : (it->current_x > it->last_visible_x)))
15383 {
15384 /* Maybe add truncation glyphs. */
15385 if (!FRAME_WINDOW_P (it->f))
15386 {
15387 int i, n;
15388
15389 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
15390 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
15391 break;
15392
15393 for (n = row->used[TEXT_AREA]; i < n; ++i)
15394 {
15395 row->used[TEXT_AREA] = i;
15396 produce_special_glyphs (it, IT_TRUNCATION);
15397 }
15398 }
15399 #ifdef HAVE_WINDOW_SYSTEM
15400 else
15401 {
15402 /* Don't truncate if we can overflow newline into fringe. */
15403 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15404 {
15405 if (!get_next_display_element (it))
15406 {
15407 it->continuation_lines_width = 0;
15408 row->ends_at_zv_p = 1;
15409 row->exact_window_width_line_p = 1;
15410 break;
15411 }
15412 if (ITERATOR_AT_END_OF_LINE_P (it))
15413 {
15414 row->exact_window_width_line_p = 1;
15415 goto at_end_of_line;
15416 }
15417 }
15418 }
15419 #endif /* HAVE_WINDOW_SYSTEM */
15420
15421 row->truncated_on_right_p = 1;
15422 it->continuation_lines_width = 0;
15423 reseat_at_next_visible_line_start (it, 0);
15424 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
15425 it->hpos = hpos_before;
15426 it->current_x = x_before;
15427 break;
15428 }
15429 }
15430
15431 /* If line is not empty and hscrolled, maybe insert truncation glyphs
15432 at the left window margin. */
15433 if (it->first_visible_x
15434 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
15435 {
15436 if (!FRAME_WINDOW_P (it->f))
15437 insert_left_trunc_glyphs (it);
15438 row->truncated_on_left_p = 1;
15439 }
15440
15441 /* If the start of this line is the overlay arrow-position, then
15442 mark this glyph row as the one containing the overlay arrow.
15443 This is clearly a mess with variable size fonts. It would be
15444 better to let it be displayed like cursors under X. */
15445 if ((row->displays_text_p || !overlay_arrow_seen)
15446 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
15447 !NILP (overlay_arrow_string)))
15448 {
15449 /* Overlay arrow in window redisplay is a fringe bitmap. */
15450 if (STRINGP (overlay_arrow_string))
15451 {
15452 struct glyph_row *arrow_row
15453 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
15454 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
15455 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
15456 struct glyph *p = row->glyphs[TEXT_AREA];
15457 struct glyph *p2, *end;
15458
15459 /* Copy the arrow glyphs. */
15460 while (glyph < arrow_end)
15461 *p++ = *glyph++;
15462
15463 /* Throw away padding glyphs. */
15464 p2 = p;
15465 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
15466 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
15467 ++p2;
15468 if (p2 > p)
15469 {
15470 while (p2 < end)
15471 *p++ = *p2++;
15472 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
15473 }
15474 }
15475 else
15476 {
15477 xassert (INTEGERP (overlay_arrow_string));
15478 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
15479 }
15480 overlay_arrow_seen = 1;
15481 }
15482
15483 /* Compute pixel dimensions of this line. */
15484 compute_line_metrics (it);
15485
15486 /* Remember the position at which this line ends. */
15487 row->end = it->current;
15488
15489 /* Record whether this row ends inside an ellipsis. */
15490 row->ends_in_ellipsis_p
15491 = (it->method == GET_FROM_DISPLAY_VECTOR
15492 && it->ellipsis_p);
15493
15494 /* Save fringe bitmaps in this row. */
15495 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
15496 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
15497 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
15498 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
15499
15500 it->left_user_fringe_bitmap = 0;
15501 it->left_user_fringe_face_id = 0;
15502 it->right_user_fringe_bitmap = 0;
15503 it->right_user_fringe_face_id = 0;
15504
15505 /* Maybe set the cursor. */
15506 if (it->w->cursor.vpos < 0
15507 && PT >= MATRIX_ROW_START_CHARPOS (row)
15508 && PT <= MATRIX_ROW_END_CHARPOS (row)
15509 && cursor_row_p (it->w, row))
15510 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
15511
15512 /* Highlight trailing whitespace. */
15513 if (!NILP (Vshow_trailing_whitespace))
15514 highlight_trailing_whitespace (it->f, it->glyph_row);
15515
15516 /* Prepare for the next line. This line starts horizontally at (X
15517 HPOS) = (0 0). Vertical positions are incremented. As a
15518 convenience for the caller, IT->glyph_row is set to the next
15519 row to be used. */
15520 it->current_x = it->hpos = 0;
15521 it->current_y += row->height;
15522 ++it->vpos;
15523 ++it->glyph_row;
15524 it->start = it->current;
15525 return row->displays_text_p;
15526 }
15527
15528
15529 \f
15530 /***********************************************************************
15531 Menu Bar
15532 ***********************************************************************/
15533
15534 /* Redisplay the menu bar in the frame for window W.
15535
15536 The menu bar of X frames that don't have X toolkit support is
15537 displayed in a special window W->frame->menu_bar_window.
15538
15539 The menu bar of terminal frames is treated specially as far as
15540 glyph matrices are concerned. Menu bar lines are not part of
15541 windows, so the update is done directly on the frame matrix rows
15542 for the menu bar. */
15543
15544 static void
15545 display_menu_bar (w)
15546 struct window *w;
15547 {
15548 struct frame *f = XFRAME (WINDOW_FRAME (w));
15549 struct it it;
15550 Lisp_Object items;
15551 int i;
15552
15553 /* Don't do all this for graphical frames. */
15554 #ifdef HAVE_NTGUI
15555 if (!NILP (Vwindow_system))
15556 return;
15557 #endif
15558 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
15559 if (FRAME_X_P (f))
15560 return;
15561 #endif
15562 #ifdef MAC_OS
15563 if (FRAME_MAC_P (f))
15564 return;
15565 #endif
15566
15567 #ifdef USE_X_TOOLKIT
15568 xassert (!FRAME_WINDOW_P (f));
15569 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
15570 it.first_visible_x = 0;
15571 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15572 #else /* not USE_X_TOOLKIT */
15573 if (FRAME_WINDOW_P (f))
15574 {
15575 /* Menu bar lines are displayed in the desired matrix of the
15576 dummy window menu_bar_window. */
15577 struct window *menu_w;
15578 xassert (WINDOWP (f->menu_bar_window));
15579 menu_w = XWINDOW (f->menu_bar_window);
15580 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
15581 MENU_FACE_ID);
15582 it.first_visible_x = 0;
15583 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
15584 }
15585 else
15586 {
15587 /* This is a TTY frame, i.e. character hpos/vpos are used as
15588 pixel x/y. */
15589 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
15590 MENU_FACE_ID);
15591 it.first_visible_x = 0;
15592 it.last_visible_x = FRAME_COLS (f);
15593 }
15594 #endif /* not USE_X_TOOLKIT */
15595
15596 if (! mode_line_inverse_video)
15597 /* Force the menu-bar to be displayed in the default face. */
15598 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15599
15600 /* Clear all rows of the menu bar. */
15601 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
15602 {
15603 struct glyph_row *row = it.glyph_row + i;
15604 clear_glyph_row (row);
15605 row->enabled_p = 1;
15606 row->full_width_p = 1;
15607 }
15608
15609 /* Display all items of the menu bar. */
15610 items = FRAME_MENU_BAR_ITEMS (it.f);
15611 for (i = 0; i < XVECTOR (items)->size; i += 4)
15612 {
15613 Lisp_Object string;
15614
15615 /* Stop at nil string. */
15616 string = AREF (items, i + 1);
15617 if (NILP (string))
15618 break;
15619
15620 /* Remember where item was displayed. */
15621 AREF (items, i + 3) = make_number (it.hpos);
15622
15623 /* Display the item, pad with one space. */
15624 if (it.current_x < it.last_visible_x)
15625 display_string (NULL, string, Qnil, 0, 0, &it,
15626 SCHARS (string) + 1, 0, 0, -1);
15627 }
15628
15629 /* Fill out the line with spaces. */
15630 if (it.current_x < it.last_visible_x)
15631 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
15632
15633 /* Compute the total height of the lines. */
15634 compute_line_metrics (&it);
15635 }
15636
15637
15638 \f
15639 /***********************************************************************
15640 Mode Line
15641 ***********************************************************************/
15642
15643 /* Redisplay mode lines in the window tree whose root is WINDOW. If
15644 FORCE is non-zero, redisplay mode lines unconditionally.
15645 Otherwise, redisplay only mode lines that are garbaged. Value is
15646 the number of windows whose mode lines were redisplayed. */
15647
15648 static int
15649 redisplay_mode_lines (window, force)
15650 Lisp_Object window;
15651 int force;
15652 {
15653 int nwindows = 0;
15654
15655 while (!NILP (window))
15656 {
15657 struct window *w = XWINDOW (window);
15658
15659 if (WINDOWP (w->hchild))
15660 nwindows += redisplay_mode_lines (w->hchild, force);
15661 else if (WINDOWP (w->vchild))
15662 nwindows += redisplay_mode_lines (w->vchild, force);
15663 else if (force
15664 || FRAME_GARBAGED_P (XFRAME (w->frame))
15665 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
15666 {
15667 struct text_pos lpoint;
15668 struct buffer *old = current_buffer;
15669
15670 /* Set the window's buffer for the mode line display. */
15671 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15672 set_buffer_internal_1 (XBUFFER (w->buffer));
15673
15674 /* Point refers normally to the selected window. For any
15675 other window, set up appropriate value. */
15676 if (!EQ (window, selected_window))
15677 {
15678 struct text_pos pt;
15679
15680 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
15681 if (CHARPOS (pt) < BEGV)
15682 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15683 else if (CHARPOS (pt) > (ZV - 1))
15684 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
15685 else
15686 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
15687 }
15688
15689 /* Display mode lines. */
15690 clear_glyph_matrix (w->desired_matrix);
15691 if (display_mode_lines (w))
15692 {
15693 ++nwindows;
15694 w->must_be_updated_p = 1;
15695 }
15696
15697 /* Restore old settings. */
15698 set_buffer_internal_1 (old);
15699 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15700 }
15701
15702 window = w->next;
15703 }
15704
15705 return nwindows;
15706 }
15707
15708
15709 /* Display the mode and/or top line of window W. Value is the number
15710 of mode lines displayed. */
15711
15712 static int
15713 display_mode_lines (w)
15714 struct window *w;
15715 {
15716 Lisp_Object old_selected_window, old_selected_frame;
15717 int n = 0;
15718
15719 old_selected_frame = selected_frame;
15720 selected_frame = w->frame;
15721 old_selected_window = selected_window;
15722 XSETWINDOW (selected_window, w);
15723
15724 /* These will be set while the mode line specs are processed. */
15725 line_number_displayed = 0;
15726 w->column_number_displayed = Qnil;
15727
15728 if (WINDOW_WANTS_MODELINE_P (w))
15729 {
15730 struct window *sel_w = XWINDOW (old_selected_window);
15731
15732 /* Select mode line face based on the real selected window. */
15733 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
15734 current_buffer->mode_line_format);
15735 ++n;
15736 }
15737
15738 if (WINDOW_WANTS_HEADER_LINE_P (w))
15739 {
15740 display_mode_line (w, HEADER_LINE_FACE_ID,
15741 current_buffer->header_line_format);
15742 ++n;
15743 }
15744
15745 selected_frame = old_selected_frame;
15746 selected_window = old_selected_window;
15747 return n;
15748 }
15749
15750
15751 /* Display mode or top line of window W. FACE_ID specifies which line
15752 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
15753 FORMAT is the mode line format to display. Value is the pixel
15754 height of the mode line displayed. */
15755
15756 static int
15757 display_mode_line (w, face_id, format)
15758 struct window *w;
15759 enum face_id face_id;
15760 Lisp_Object format;
15761 {
15762 struct it it;
15763 struct face *face;
15764 int count = SPECPDL_INDEX ();
15765
15766 init_iterator (&it, w, -1, -1, NULL, face_id);
15767 prepare_desired_row (it.glyph_row);
15768
15769 it.glyph_row->mode_line_p = 1;
15770
15771 if (! mode_line_inverse_video)
15772 /* Force the mode-line to be displayed in the default face. */
15773 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
15774
15775 record_unwind_protect (unwind_format_mode_line,
15776 format_mode_line_unwind_data (NULL));
15777
15778 mode_line_target = MODE_LINE_DISPLAY;
15779
15780 /* Temporarily make frame's keyboard the current kboard so that
15781 kboard-local variables in the mode_line_format will get the right
15782 values. */
15783 push_frame_kboard (it.f);
15784 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
15785 pop_frame_kboard ();
15786
15787 unbind_to (count, Qnil);
15788
15789 /* Fill up with spaces. */
15790 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
15791
15792 compute_line_metrics (&it);
15793 it.glyph_row->full_width_p = 1;
15794 it.glyph_row->continued_p = 0;
15795 it.glyph_row->truncated_on_left_p = 0;
15796 it.glyph_row->truncated_on_right_p = 0;
15797
15798 /* Make a 3D mode-line have a shadow at its right end. */
15799 face = FACE_FROM_ID (it.f, face_id);
15800 extend_face_to_end_of_line (&it);
15801 if (face->box != FACE_NO_BOX)
15802 {
15803 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
15804 + it.glyph_row->used[TEXT_AREA] - 1);
15805 last->right_box_line_p = 1;
15806 }
15807
15808 return it.glyph_row->height;
15809 }
15810
15811 /* Contribute ELT to the mode line for window IT->w. How it
15812 translates into text depends on its data type.
15813
15814 IT describes the display environment in which we display, as usual.
15815
15816 DEPTH is the depth in recursion. It is used to prevent
15817 infinite recursion here.
15818
15819 FIELD_WIDTH is the number of characters the display of ELT should
15820 occupy in the mode line, and PRECISION is the maximum number of
15821 characters to display from ELT's representation. See
15822 display_string for details.
15823
15824 Returns the hpos of the end of the text generated by ELT.
15825
15826 PROPS is a property list to add to any string we encounter.
15827
15828 If RISKY is nonzero, remove (disregard) any properties in any string
15829 we encounter, and ignore :eval and :propertize.
15830
15831 The global variable `mode_line_target' determines whether the
15832 output is passed to `store_mode_line_noprop',
15833 `store_mode_line_string', or `display_string'. */
15834
15835 static int
15836 display_mode_element (it, depth, field_width, precision, elt, props, risky)
15837 struct it *it;
15838 int depth;
15839 int field_width, precision;
15840 Lisp_Object elt, props;
15841 int risky;
15842 {
15843 int n = 0, field, prec;
15844 int literal = 0;
15845
15846 tail_recurse:
15847 if (depth > 100)
15848 elt = build_string ("*too-deep*");
15849
15850 depth++;
15851
15852 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
15853 {
15854 case Lisp_String:
15855 {
15856 /* A string: output it and check for %-constructs within it. */
15857 unsigned char c;
15858 const unsigned char *this, *lisp_string;
15859
15860 if (!NILP (props) || risky)
15861 {
15862 Lisp_Object oprops, aelt;
15863 oprops = Ftext_properties_at (make_number (0), elt);
15864
15865 /* If the starting string's properties are not what
15866 we want, translate the string. Also, if the string
15867 is risky, do that anyway. */
15868
15869 if (NILP (Fequal (props, oprops)) || risky)
15870 {
15871 /* If the starting string has properties,
15872 merge the specified ones onto the existing ones. */
15873 if (! NILP (oprops) && !risky)
15874 {
15875 Lisp_Object tem;
15876
15877 oprops = Fcopy_sequence (oprops);
15878 tem = props;
15879 while (CONSP (tem))
15880 {
15881 oprops = Fplist_put (oprops, XCAR (tem),
15882 XCAR (XCDR (tem)));
15883 tem = XCDR (XCDR (tem));
15884 }
15885 props = oprops;
15886 }
15887
15888 aelt = Fassoc (elt, mode_line_proptrans_alist);
15889 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
15890 {
15891 mode_line_proptrans_alist
15892 = Fcons (aelt, Fdelq (aelt, mode_line_proptrans_alist));
15893 elt = XCAR (aelt);
15894 }
15895 else
15896 {
15897 Lisp_Object tem;
15898
15899 elt = Fcopy_sequence (elt);
15900 Fset_text_properties (make_number (0), Flength (elt),
15901 props, elt);
15902 /* Add this item to mode_line_proptrans_alist. */
15903 mode_line_proptrans_alist
15904 = Fcons (Fcons (elt, props),
15905 mode_line_proptrans_alist);
15906 /* Truncate mode_line_proptrans_alist
15907 to at most 50 elements. */
15908 tem = Fnthcdr (make_number (50),
15909 mode_line_proptrans_alist);
15910 if (! NILP (tem))
15911 XSETCDR (tem, Qnil);
15912 }
15913 }
15914 }
15915
15916 this = SDATA (elt);
15917 lisp_string = this;
15918
15919 if (literal)
15920 {
15921 prec = precision - n;
15922 switch (mode_line_target)
15923 {
15924 case MODE_LINE_NOPROP:
15925 case MODE_LINE_TITLE:
15926 n += store_mode_line_noprop (SDATA (elt), -1, prec);
15927 break;
15928 case MODE_LINE_STRING:
15929 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
15930 break;
15931 case MODE_LINE_DISPLAY:
15932 n += display_string (NULL, elt, Qnil, 0, 0, it,
15933 0, prec, 0, STRING_MULTIBYTE (elt));
15934 break;
15935 }
15936
15937 break;
15938 }
15939
15940 while ((precision <= 0 || n < precision)
15941 && *this
15942 && (mode_line_target != MODE_LINE_DISPLAY
15943 || it->current_x < it->last_visible_x))
15944 {
15945 const unsigned char *last = this;
15946
15947 /* Advance to end of string or next format specifier. */
15948 while ((c = *this++) != '\0' && c != '%')
15949 ;
15950
15951 if (this - 1 != last)
15952 {
15953 int nchars, nbytes;
15954
15955 /* Output to end of string or up to '%'. Field width
15956 is length of string. Don't output more than
15957 PRECISION allows us. */
15958 --this;
15959
15960 prec = c_string_width (last, this - last, precision - n,
15961 &nchars, &nbytes);
15962
15963 switch (mode_line_target)
15964 {
15965 case MODE_LINE_NOPROP:
15966 case MODE_LINE_TITLE:
15967 n += store_mode_line_noprop (last, 0, prec);
15968 break;
15969 case MODE_LINE_STRING:
15970 {
15971 int bytepos = last - lisp_string;
15972 int charpos = string_byte_to_char (elt, bytepos);
15973 int endpos = (precision <= 0
15974 ? string_byte_to_char (elt,
15975 this - lisp_string)
15976 : charpos + nchars);
15977
15978 n += store_mode_line_string (NULL,
15979 Fsubstring (elt, make_number (charpos),
15980 make_number (endpos)),
15981 0, 0, 0, Qnil);
15982 }
15983 break;
15984 case MODE_LINE_DISPLAY:
15985 {
15986 int bytepos = last - lisp_string;
15987 int charpos = string_byte_to_char (elt, bytepos);
15988 n += display_string (NULL, elt, Qnil, 0, charpos,
15989 it, 0, prec, 0,
15990 STRING_MULTIBYTE (elt));
15991 }
15992 break;
15993 }
15994 }
15995 else /* c == '%' */
15996 {
15997 const unsigned char *percent_position = this;
15998
15999 /* Get the specified minimum width. Zero means
16000 don't pad. */
16001 field = 0;
16002 while ((c = *this++) >= '0' && c <= '9')
16003 field = field * 10 + c - '0';
16004
16005 /* Don't pad beyond the total padding allowed. */
16006 if (field_width - n > 0 && field > field_width - n)
16007 field = field_width - n;
16008
16009 /* Note that either PRECISION <= 0 or N < PRECISION. */
16010 prec = precision - n;
16011
16012 if (c == 'M')
16013 n += display_mode_element (it, depth, field, prec,
16014 Vglobal_mode_string, props,
16015 risky);
16016 else if (c != 0)
16017 {
16018 int multibyte;
16019 int bytepos, charpos;
16020 unsigned char *spec;
16021
16022 bytepos = percent_position - lisp_string;
16023 charpos = (STRING_MULTIBYTE (elt)
16024 ? string_byte_to_char (elt, bytepos)
16025 : bytepos);
16026
16027 spec
16028 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16029
16030 switch (mode_line_target)
16031 {
16032 case MODE_LINE_NOPROP:
16033 case MODE_LINE_TITLE:
16034 n += store_mode_line_noprop (spec, field, prec);
16035 break;
16036 case MODE_LINE_STRING:
16037 {
16038 int len = strlen (spec);
16039 Lisp_Object tem = make_string (spec, len);
16040 props = Ftext_properties_at (make_number (charpos), elt);
16041 /* Should only keep face property in props */
16042 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16043 }
16044 break;
16045 case MODE_LINE_DISPLAY:
16046 {
16047 int nglyphs_before, nwritten;
16048
16049 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16050 nwritten = display_string (spec, Qnil, elt,
16051 charpos, 0, it,
16052 field, prec, 0,
16053 multibyte);
16054
16055 /* Assign to the glyphs written above the
16056 string where the `%x' came from, position
16057 of the `%'. */
16058 if (nwritten > 0)
16059 {
16060 struct glyph *glyph
16061 = (it->glyph_row->glyphs[TEXT_AREA]
16062 + nglyphs_before);
16063 int i;
16064
16065 for (i = 0; i < nwritten; ++i)
16066 {
16067 glyph[i].object = elt;
16068 glyph[i].charpos = charpos;
16069 }
16070
16071 n += nwritten;
16072 }
16073 }
16074 break;
16075 }
16076 }
16077 else /* c == 0 */
16078 break;
16079 }
16080 }
16081 }
16082 break;
16083
16084 case Lisp_Symbol:
16085 /* A symbol: process the value of the symbol recursively
16086 as if it appeared here directly. Avoid error if symbol void.
16087 Special case: if value of symbol is a string, output the string
16088 literally. */
16089 {
16090 register Lisp_Object tem;
16091
16092 /* If the variable is not marked as risky to set
16093 then its contents are risky to use. */
16094 if (NILP (Fget (elt, Qrisky_local_variable)))
16095 risky = 1;
16096
16097 tem = Fboundp (elt);
16098 if (!NILP (tem))
16099 {
16100 tem = Fsymbol_value (elt);
16101 /* If value is a string, output that string literally:
16102 don't check for % within it. */
16103 if (STRINGP (tem))
16104 literal = 1;
16105
16106 if (!EQ (tem, elt))
16107 {
16108 /* Give up right away for nil or t. */
16109 elt = tem;
16110 goto tail_recurse;
16111 }
16112 }
16113 }
16114 break;
16115
16116 case Lisp_Cons:
16117 {
16118 register Lisp_Object car, tem;
16119
16120 /* A cons cell: five distinct cases.
16121 If first element is :eval or :propertize, do something special.
16122 If first element is a string or a cons, process all the elements
16123 and effectively concatenate them.
16124 If first element is a negative number, truncate displaying cdr to
16125 at most that many characters. If positive, pad (with spaces)
16126 to at least that many characters.
16127 If first element is a symbol, process the cadr or caddr recursively
16128 according to whether the symbol's value is non-nil or nil. */
16129 car = XCAR (elt);
16130 if (EQ (car, QCeval))
16131 {
16132 /* An element of the form (:eval FORM) means evaluate FORM
16133 and use the result as mode line elements. */
16134
16135 if (risky)
16136 break;
16137
16138 if (CONSP (XCDR (elt)))
16139 {
16140 Lisp_Object spec;
16141 spec = safe_eval (XCAR (XCDR (elt)));
16142 n += display_mode_element (it, depth, field_width - n,
16143 precision - n, spec, props,
16144 risky);
16145 }
16146 }
16147 else if (EQ (car, QCpropertize))
16148 {
16149 /* An element of the form (:propertize ELT PROPS...)
16150 means display ELT but applying properties PROPS. */
16151
16152 if (risky)
16153 break;
16154
16155 if (CONSP (XCDR (elt)))
16156 n += display_mode_element (it, depth, field_width - n,
16157 precision - n, XCAR (XCDR (elt)),
16158 XCDR (XCDR (elt)), risky);
16159 }
16160 else if (SYMBOLP (car))
16161 {
16162 tem = Fboundp (car);
16163 elt = XCDR (elt);
16164 if (!CONSP (elt))
16165 goto invalid;
16166 /* elt is now the cdr, and we know it is a cons cell.
16167 Use its car if CAR has a non-nil value. */
16168 if (!NILP (tem))
16169 {
16170 tem = Fsymbol_value (car);
16171 if (!NILP (tem))
16172 {
16173 elt = XCAR (elt);
16174 goto tail_recurse;
16175 }
16176 }
16177 /* Symbol's value is nil (or symbol is unbound)
16178 Get the cddr of the original list
16179 and if possible find the caddr and use that. */
16180 elt = XCDR (elt);
16181 if (NILP (elt))
16182 break;
16183 else if (!CONSP (elt))
16184 goto invalid;
16185 elt = XCAR (elt);
16186 goto tail_recurse;
16187 }
16188 else if (INTEGERP (car))
16189 {
16190 register int lim = XINT (car);
16191 elt = XCDR (elt);
16192 if (lim < 0)
16193 {
16194 /* Negative int means reduce maximum width. */
16195 if (precision <= 0)
16196 precision = -lim;
16197 else
16198 precision = min (precision, -lim);
16199 }
16200 else if (lim > 0)
16201 {
16202 /* Padding specified. Don't let it be more than
16203 current maximum. */
16204 if (precision > 0)
16205 lim = min (precision, lim);
16206
16207 /* If that's more padding than already wanted, queue it.
16208 But don't reduce padding already specified even if
16209 that is beyond the current truncation point. */
16210 field_width = max (lim, field_width);
16211 }
16212 goto tail_recurse;
16213 }
16214 else if (STRINGP (car) || CONSP (car))
16215 {
16216 register int limit = 50;
16217 /* Limit is to protect against circular lists. */
16218 while (CONSP (elt)
16219 && --limit > 0
16220 && (precision <= 0 || n < precision))
16221 {
16222 n += display_mode_element (it, depth,
16223 /* Do padding only after the last
16224 element in the list. */
16225 (! CONSP (XCDR (elt))
16226 ? field_width - n
16227 : 0),
16228 precision - n, XCAR (elt),
16229 props, risky);
16230 elt = XCDR (elt);
16231 }
16232 }
16233 }
16234 break;
16235
16236 default:
16237 invalid:
16238 elt = build_string ("*invalid*");
16239 goto tail_recurse;
16240 }
16241
16242 /* Pad to FIELD_WIDTH. */
16243 if (field_width > 0 && n < field_width)
16244 {
16245 switch (mode_line_target)
16246 {
16247 case MODE_LINE_NOPROP:
16248 case MODE_LINE_TITLE:
16249 n += store_mode_line_noprop ("", field_width - n, 0);
16250 break;
16251 case MODE_LINE_STRING:
16252 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
16253 break;
16254 case MODE_LINE_DISPLAY:
16255 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
16256 0, 0, 0);
16257 break;
16258 }
16259 }
16260
16261 return n;
16262 }
16263
16264 /* Store a mode-line string element in mode_line_string_list.
16265
16266 If STRING is non-null, display that C string. Otherwise, the Lisp
16267 string LISP_STRING is displayed.
16268
16269 FIELD_WIDTH is the minimum number of output glyphs to produce.
16270 If STRING has fewer characters than FIELD_WIDTH, pad to the right
16271 with spaces. FIELD_WIDTH <= 0 means don't pad.
16272
16273 PRECISION is the maximum number of characters to output from
16274 STRING. PRECISION <= 0 means don't truncate the string.
16275
16276 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
16277 properties to the string.
16278
16279 PROPS are the properties to add to the string.
16280 The mode_line_string_face face property is always added to the string.
16281 */
16282
16283 static int
16284 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
16285 char *string;
16286 Lisp_Object lisp_string;
16287 int copy_string;
16288 int field_width;
16289 int precision;
16290 Lisp_Object props;
16291 {
16292 int len;
16293 int n = 0;
16294
16295 if (string != NULL)
16296 {
16297 len = strlen (string);
16298 if (precision > 0 && len > precision)
16299 len = precision;
16300 lisp_string = make_string (string, len);
16301 if (NILP (props))
16302 props = mode_line_string_face_prop;
16303 else if (!NILP (mode_line_string_face))
16304 {
16305 Lisp_Object face = Fplist_get (props, Qface);
16306 props = Fcopy_sequence (props);
16307 if (NILP (face))
16308 face = mode_line_string_face;
16309 else
16310 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16311 props = Fplist_put (props, Qface, face);
16312 }
16313 Fadd_text_properties (make_number (0), make_number (len),
16314 props, lisp_string);
16315 }
16316 else
16317 {
16318 len = XFASTINT (Flength (lisp_string));
16319 if (precision > 0 && len > precision)
16320 {
16321 len = precision;
16322 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
16323 precision = -1;
16324 }
16325 if (!NILP (mode_line_string_face))
16326 {
16327 Lisp_Object face;
16328 if (NILP (props))
16329 props = Ftext_properties_at (make_number (0), lisp_string);
16330 face = Fplist_get (props, Qface);
16331 if (NILP (face))
16332 face = mode_line_string_face;
16333 else
16334 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
16335 props = Fcons (Qface, Fcons (face, Qnil));
16336 if (copy_string)
16337 lisp_string = Fcopy_sequence (lisp_string);
16338 }
16339 if (!NILP (props))
16340 Fadd_text_properties (make_number (0), make_number (len),
16341 props, lisp_string);
16342 }
16343
16344 if (len > 0)
16345 {
16346 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16347 n += len;
16348 }
16349
16350 if (field_width > len)
16351 {
16352 field_width -= len;
16353 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
16354 if (!NILP (props))
16355 Fadd_text_properties (make_number (0), make_number (field_width),
16356 props, lisp_string);
16357 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
16358 n += field_width;
16359 }
16360
16361 return n;
16362 }
16363
16364
16365 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
16366 1, 4, 0,
16367 doc: /* Format a string out of a mode line format specification.
16368 First arg FORMAT specifies the mode line format (see `mode-line-format'
16369 for details) to use.
16370
16371 Optional second arg FACE specifies the face property to put
16372 on all characters for which no face is specified.
16373 t means whatever face the window's mode line currently uses
16374 \(either `mode-line' or `mode-line-inactive', depending).
16375 nil means the default is no face property.
16376 If FACE is an integer, the value string has no text properties.
16377
16378 Optional third and fourth args WINDOW and BUFFER specify the window
16379 and buffer to use as the context for the formatting (defaults
16380 are the selected window and the window's buffer). */)
16381 (format, face, window, buffer)
16382 Lisp_Object format, face, window, buffer;
16383 {
16384 struct it it;
16385 int len;
16386 struct window *w;
16387 struct buffer *old_buffer = NULL;
16388 int face_id = -1;
16389 int no_props = INTEGERP (face);
16390 int count = SPECPDL_INDEX ();
16391 Lisp_Object str;
16392 int string_start = 0;
16393
16394 if (NILP (window))
16395 window = selected_window;
16396 CHECK_WINDOW (window);
16397 w = XWINDOW (window);
16398
16399 if (NILP (buffer))
16400 buffer = w->buffer;
16401 CHECK_BUFFER (buffer);
16402
16403 if (NILP (format))
16404 return build_string ("");
16405
16406 if (no_props)
16407 face = Qnil;
16408
16409 if (!NILP (face))
16410 {
16411 if (EQ (face, Qt))
16412 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
16413 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
16414 }
16415
16416 if (face_id < 0)
16417 face_id = DEFAULT_FACE_ID;
16418
16419 if (XBUFFER (buffer) != current_buffer)
16420 old_buffer = current_buffer;
16421
16422 record_unwind_protect (unwind_format_mode_line,
16423 format_mode_line_unwind_data (old_buffer));
16424
16425 if (old_buffer)
16426 set_buffer_internal_1 (XBUFFER (buffer));
16427
16428 init_iterator (&it, w, -1, -1, NULL, face_id);
16429
16430 if (no_props)
16431 {
16432 mode_line_target = MODE_LINE_NOPROP;
16433 mode_line_string_face_prop = Qnil;
16434 mode_line_string_list = Qnil;
16435 string_start = MODE_LINE_NOPROP_LEN (0);
16436 }
16437 else
16438 {
16439 mode_line_target = MODE_LINE_STRING;
16440 mode_line_string_list = Qnil;
16441 mode_line_string_face = face;
16442 mode_line_string_face_prop
16443 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
16444 }
16445
16446 push_frame_kboard (it.f);
16447 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16448 pop_frame_kboard ();
16449
16450 if (no_props)
16451 {
16452 len = MODE_LINE_NOPROP_LEN (string_start);
16453 str = make_string (mode_line_noprop_buf + string_start, len);
16454 }
16455 else
16456 {
16457 mode_line_string_list = Fnreverse (mode_line_string_list);
16458 str = Fmapconcat (intern ("identity"), mode_line_string_list,
16459 make_string ("", 0));
16460 }
16461
16462 unbind_to (count, Qnil);
16463 return str;
16464 }
16465
16466 /* Write a null-terminated, right justified decimal representation of
16467 the positive integer D to BUF using a minimal field width WIDTH. */
16468
16469 static void
16470 pint2str (buf, width, d)
16471 register char *buf;
16472 register int width;
16473 register int d;
16474 {
16475 register char *p = buf;
16476
16477 if (d <= 0)
16478 *p++ = '0';
16479 else
16480 {
16481 while (d > 0)
16482 {
16483 *p++ = d % 10 + '0';
16484 d /= 10;
16485 }
16486 }
16487
16488 for (width -= (int) (p - buf); width > 0; --width)
16489 *p++ = ' ';
16490 *p-- = '\0';
16491 while (p > buf)
16492 {
16493 d = *buf;
16494 *buf++ = *p;
16495 *p-- = d;
16496 }
16497 }
16498
16499 /* Write a null-terminated, right justified decimal and "human
16500 readable" representation of the nonnegative integer D to BUF using
16501 a minimal field width WIDTH. D should be smaller than 999.5e24. */
16502
16503 static const char power_letter[] =
16504 {
16505 0, /* not used */
16506 'k', /* kilo */
16507 'M', /* mega */
16508 'G', /* giga */
16509 'T', /* tera */
16510 'P', /* peta */
16511 'E', /* exa */
16512 'Z', /* zetta */
16513 'Y' /* yotta */
16514 };
16515
16516 static void
16517 pint2hrstr (buf, width, d)
16518 char *buf;
16519 int width;
16520 int d;
16521 {
16522 /* We aim to represent the nonnegative integer D as
16523 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
16524 int quotient = d;
16525 int remainder = 0;
16526 /* -1 means: do not use TENTHS. */
16527 int tenths = -1;
16528 int exponent = 0;
16529
16530 /* Length of QUOTIENT.TENTHS as a string. */
16531 int length;
16532
16533 char * psuffix;
16534 char * p;
16535
16536 if (1000 <= quotient)
16537 {
16538 /* Scale to the appropriate EXPONENT. */
16539 do
16540 {
16541 remainder = quotient % 1000;
16542 quotient /= 1000;
16543 exponent++;
16544 }
16545 while (1000 <= quotient);
16546
16547 /* Round to nearest and decide whether to use TENTHS or not. */
16548 if (quotient <= 9)
16549 {
16550 tenths = remainder / 100;
16551 if (50 <= remainder % 100)
16552 {
16553 if (tenths < 9)
16554 tenths++;
16555 else
16556 {
16557 quotient++;
16558 if (quotient == 10)
16559 tenths = -1;
16560 else
16561 tenths = 0;
16562 }
16563 }
16564 }
16565 else
16566 if (500 <= remainder)
16567 {
16568 if (quotient < 999)
16569 quotient++;
16570 else
16571 {
16572 quotient = 1;
16573 exponent++;
16574 tenths = 0;
16575 }
16576 }
16577 }
16578
16579 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
16580 if (tenths == -1 && quotient <= 99)
16581 if (quotient <= 9)
16582 length = 1;
16583 else
16584 length = 2;
16585 else
16586 length = 3;
16587 p = psuffix = buf + max (width, length);
16588
16589 /* Print EXPONENT. */
16590 if (exponent)
16591 *psuffix++ = power_letter[exponent];
16592 *psuffix = '\0';
16593
16594 /* Print TENTHS. */
16595 if (tenths >= 0)
16596 {
16597 *--p = '0' + tenths;
16598 *--p = '.';
16599 }
16600
16601 /* Print QUOTIENT. */
16602 do
16603 {
16604 int digit = quotient % 10;
16605 *--p = '0' + digit;
16606 }
16607 while ((quotient /= 10) != 0);
16608
16609 /* Print leading spaces. */
16610 while (buf < p)
16611 *--p = ' ';
16612 }
16613
16614 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
16615 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
16616 type of CODING_SYSTEM. Return updated pointer into BUF. */
16617
16618 static unsigned char invalid_eol_type[] = "(*invalid*)";
16619
16620 static char *
16621 decode_mode_spec_coding (coding_system, buf, eol_flag)
16622 Lisp_Object coding_system;
16623 register char *buf;
16624 int eol_flag;
16625 {
16626 Lisp_Object val;
16627 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
16628 const unsigned char *eol_str;
16629 int eol_str_len;
16630 /* The EOL conversion we are using. */
16631 Lisp_Object eoltype;
16632
16633 val = Fget (coding_system, Qcoding_system);
16634 eoltype = Qnil;
16635
16636 if (!VECTORP (val)) /* Not yet decided. */
16637 {
16638 if (multibyte)
16639 *buf++ = '-';
16640 if (eol_flag)
16641 eoltype = eol_mnemonic_undecided;
16642 /* Don't mention EOL conversion if it isn't decided. */
16643 }
16644 else
16645 {
16646 Lisp_Object eolvalue;
16647
16648 eolvalue = Fget (coding_system, Qeol_type);
16649
16650 if (multibyte)
16651 *buf++ = XFASTINT (AREF (val, 1));
16652
16653 if (eol_flag)
16654 {
16655 /* The EOL conversion that is normal on this system. */
16656
16657 if (NILP (eolvalue)) /* Not yet decided. */
16658 eoltype = eol_mnemonic_undecided;
16659 else if (VECTORP (eolvalue)) /* Not yet decided. */
16660 eoltype = eol_mnemonic_undecided;
16661 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
16662 eoltype = (XFASTINT (eolvalue) == 0
16663 ? eol_mnemonic_unix
16664 : (XFASTINT (eolvalue) == 1
16665 ? eol_mnemonic_dos : eol_mnemonic_mac));
16666 }
16667 }
16668
16669 if (eol_flag)
16670 {
16671 /* Mention the EOL conversion if it is not the usual one. */
16672 if (STRINGP (eoltype))
16673 {
16674 eol_str = SDATA (eoltype);
16675 eol_str_len = SBYTES (eoltype);
16676 }
16677 else if (INTEGERP (eoltype)
16678 && CHAR_VALID_P (XINT (eoltype), 0))
16679 {
16680 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
16681 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
16682 eol_str = tmp;
16683 }
16684 else
16685 {
16686 eol_str = invalid_eol_type;
16687 eol_str_len = sizeof (invalid_eol_type) - 1;
16688 }
16689 bcopy (eol_str, buf, eol_str_len);
16690 buf += eol_str_len;
16691 }
16692
16693 return buf;
16694 }
16695
16696 /* Return a string for the output of a mode line %-spec for window W,
16697 generated by character C. PRECISION >= 0 means don't return a
16698 string longer than that value. FIELD_WIDTH > 0 means pad the
16699 string returned with spaces to that value. Return 1 in *MULTIBYTE
16700 if the result is multibyte text.
16701
16702 Note we operate on the current buffer for most purposes,
16703 the exception being w->base_line_pos. */
16704
16705 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
16706
16707 static char *
16708 decode_mode_spec (w, c, field_width, precision, multibyte)
16709 struct window *w;
16710 register int c;
16711 int field_width, precision;
16712 int *multibyte;
16713 {
16714 Lisp_Object obj;
16715 struct frame *f = XFRAME (WINDOW_FRAME (w));
16716 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
16717 struct buffer *b = current_buffer;
16718
16719 obj = Qnil;
16720 *multibyte = 0;
16721
16722 switch (c)
16723 {
16724 case '*':
16725 if (!NILP (b->read_only))
16726 return "%";
16727 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16728 return "*";
16729 return "-";
16730
16731 case '+':
16732 /* This differs from %* only for a modified read-only buffer. */
16733 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16734 return "*";
16735 if (!NILP (b->read_only))
16736 return "%";
16737 return "-";
16738
16739 case '&':
16740 /* This differs from %* in ignoring read-only-ness. */
16741 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
16742 return "*";
16743 return "-";
16744
16745 case '%':
16746 return "%";
16747
16748 case '[':
16749 {
16750 int i;
16751 char *p;
16752
16753 if (command_loop_level > 5)
16754 return "[[[... ";
16755 p = decode_mode_spec_buf;
16756 for (i = 0; i < command_loop_level; i++)
16757 *p++ = '[';
16758 *p = 0;
16759 return decode_mode_spec_buf;
16760 }
16761
16762 case ']':
16763 {
16764 int i;
16765 char *p;
16766
16767 if (command_loop_level > 5)
16768 return " ...]]]";
16769 p = decode_mode_spec_buf;
16770 for (i = 0; i < command_loop_level; i++)
16771 *p++ = ']';
16772 *p = 0;
16773 return decode_mode_spec_buf;
16774 }
16775
16776 case '-':
16777 {
16778 register int i;
16779
16780 /* Let lots_of_dashes be a string of infinite length. */
16781 if (mode_line_target == MODE_LINE_NOPROP ||
16782 mode_line_target == MODE_LINE_STRING)
16783 return "--";
16784 if (field_width <= 0
16785 || field_width > sizeof (lots_of_dashes))
16786 {
16787 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
16788 decode_mode_spec_buf[i] = '-';
16789 decode_mode_spec_buf[i] = '\0';
16790 return decode_mode_spec_buf;
16791 }
16792 else
16793 return lots_of_dashes;
16794 }
16795
16796 case 'b':
16797 obj = b->name;
16798 break;
16799
16800 case 'c':
16801 {
16802 int col = (int) current_column (); /* iftc */
16803 w->column_number_displayed = make_number (col);
16804 pint2str (decode_mode_spec_buf, field_width, col);
16805 return decode_mode_spec_buf;
16806 }
16807
16808 case 'F':
16809 /* %F displays the frame name. */
16810 if (!NILP (f->title))
16811 return (char *) SDATA (f->title);
16812 if (f->explicit_name || ! FRAME_WINDOW_P (f))
16813 return (char *) SDATA (f->name);
16814 return "Emacs";
16815
16816 case 'f':
16817 obj = b->filename;
16818 break;
16819
16820 case 'i':
16821 {
16822 int size = ZV - BEGV;
16823 pint2str (decode_mode_spec_buf, field_width, size);
16824 return decode_mode_spec_buf;
16825 }
16826
16827 case 'I':
16828 {
16829 int size = ZV - BEGV;
16830 pint2hrstr (decode_mode_spec_buf, field_width, size);
16831 return decode_mode_spec_buf;
16832 }
16833
16834 case 'l':
16835 {
16836 int startpos = XMARKER (w->start)->charpos;
16837 int startpos_byte = marker_byte_position (w->start);
16838 int line, linepos, linepos_byte, topline;
16839 int nlines, junk;
16840 int height = WINDOW_TOTAL_LINES (w);
16841
16842 /* If we decided that this buffer isn't suitable for line numbers,
16843 don't forget that too fast. */
16844 if (EQ (w->base_line_pos, w->buffer))
16845 goto no_value;
16846 /* But do forget it, if the window shows a different buffer now. */
16847 else if (BUFFERP (w->base_line_pos))
16848 w->base_line_pos = Qnil;
16849
16850 /* If the buffer is very big, don't waste time. */
16851 if (INTEGERP (Vline_number_display_limit)
16852 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
16853 {
16854 w->base_line_pos = Qnil;
16855 w->base_line_number = Qnil;
16856 goto no_value;
16857 }
16858
16859 if (!NILP (w->base_line_number)
16860 && !NILP (w->base_line_pos)
16861 && XFASTINT (w->base_line_pos) <= startpos)
16862 {
16863 line = XFASTINT (w->base_line_number);
16864 linepos = XFASTINT (w->base_line_pos);
16865 linepos_byte = buf_charpos_to_bytepos (b, linepos);
16866 }
16867 else
16868 {
16869 line = 1;
16870 linepos = BUF_BEGV (b);
16871 linepos_byte = BUF_BEGV_BYTE (b);
16872 }
16873
16874 /* Count lines from base line to window start position. */
16875 nlines = display_count_lines (linepos, linepos_byte,
16876 startpos_byte,
16877 startpos, &junk);
16878
16879 topline = nlines + line;
16880
16881 /* Determine a new base line, if the old one is too close
16882 or too far away, or if we did not have one.
16883 "Too close" means it's plausible a scroll-down would
16884 go back past it. */
16885 if (startpos == BUF_BEGV (b))
16886 {
16887 w->base_line_number = make_number (topline);
16888 w->base_line_pos = make_number (BUF_BEGV (b));
16889 }
16890 else if (nlines < height + 25 || nlines > height * 3 + 50
16891 || linepos == BUF_BEGV (b))
16892 {
16893 int limit = BUF_BEGV (b);
16894 int limit_byte = BUF_BEGV_BYTE (b);
16895 int position;
16896 int distance = (height * 2 + 30) * line_number_display_limit_width;
16897
16898 if (startpos - distance > limit)
16899 {
16900 limit = startpos - distance;
16901 limit_byte = CHAR_TO_BYTE (limit);
16902 }
16903
16904 nlines = display_count_lines (startpos, startpos_byte,
16905 limit_byte,
16906 - (height * 2 + 30),
16907 &position);
16908 /* If we couldn't find the lines we wanted within
16909 line_number_display_limit_width chars per line,
16910 give up on line numbers for this window. */
16911 if (position == limit_byte && limit == startpos - distance)
16912 {
16913 w->base_line_pos = w->buffer;
16914 w->base_line_number = Qnil;
16915 goto no_value;
16916 }
16917
16918 w->base_line_number = make_number (topline - nlines);
16919 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
16920 }
16921
16922 /* Now count lines from the start pos to point. */
16923 nlines = display_count_lines (startpos, startpos_byte,
16924 PT_BYTE, PT, &junk);
16925
16926 /* Record that we did display the line number. */
16927 line_number_displayed = 1;
16928
16929 /* Make the string to show. */
16930 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
16931 return decode_mode_spec_buf;
16932 no_value:
16933 {
16934 char* p = decode_mode_spec_buf;
16935 int pad = field_width - 2;
16936 while (pad-- > 0)
16937 *p++ = ' ';
16938 *p++ = '?';
16939 *p++ = '?';
16940 *p = '\0';
16941 return decode_mode_spec_buf;
16942 }
16943 }
16944 break;
16945
16946 case 'm':
16947 obj = b->mode_name;
16948 break;
16949
16950 case 'n':
16951 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
16952 return " Narrow";
16953 break;
16954
16955 case 'p':
16956 {
16957 int pos = marker_position (w->start);
16958 int total = BUF_ZV (b) - BUF_BEGV (b);
16959
16960 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
16961 {
16962 if (pos <= BUF_BEGV (b))
16963 return "All";
16964 else
16965 return "Bottom";
16966 }
16967 else if (pos <= BUF_BEGV (b))
16968 return "Top";
16969 else
16970 {
16971 if (total > 1000000)
16972 /* Do it differently for a large value, to avoid overflow. */
16973 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
16974 else
16975 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
16976 /* We can't normally display a 3-digit number,
16977 so get us a 2-digit number that is close. */
16978 if (total == 100)
16979 total = 99;
16980 sprintf (decode_mode_spec_buf, "%2d%%", total);
16981 return decode_mode_spec_buf;
16982 }
16983 }
16984
16985 /* Display percentage of size above the bottom of the screen. */
16986 case 'P':
16987 {
16988 int toppos = marker_position (w->start);
16989 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
16990 int total = BUF_ZV (b) - BUF_BEGV (b);
16991
16992 if (botpos >= BUF_ZV (b))
16993 {
16994 if (toppos <= BUF_BEGV (b))
16995 return "All";
16996 else
16997 return "Bottom";
16998 }
16999 else
17000 {
17001 if (total > 1000000)
17002 /* Do it differently for a large value, to avoid overflow. */
17003 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17004 else
17005 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17006 /* We can't normally display a 3-digit number,
17007 so get us a 2-digit number that is close. */
17008 if (total == 100)
17009 total = 99;
17010 if (toppos <= BUF_BEGV (b))
17011 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17012 else
17013 sprintf (decode_mode_spec_buf, "%2d%%", total);
17014 return decode_mode_spec_buf;
17015 }
17016 }
17017
17018 case 's':
17019 /* status of process */
17020 obj = Fget_buffer_process (Fcurrent_buffer ());
17021 if (NILP (obj))
17022 return "no process";
17023 #ifdef subprocesses
17024 obj = Fsymbol_name (Fprocess_status (obj));
17025 #endif
17026 break;
17027
17028 case 't': /* indicate TEXT or BINARY */
17029 #ifdef MODE_LINE_BINARY_TEXT
17030 return MODE_LINE_BINARY_TEXT (b);
17031 #else
17032 return "T";
17033 #endif
17034
17035 case 'z':
17036 /* coding-system (not including end-of-line format) */
17037 case 'Z':
17038 /* coding-system (including end-of-line type) */
17039 {
17040 int eol_flag = (c == 'Z');
17041 char *p = decode_mode_spec_buf;
17042
17043 if (! FRAME_WINDOW_P (f))
17044 {
17045 /* No need to mention EOL here--the terminal never needs
17046 to do EOL conversion. */
17047 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17048 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17049 }
17050 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17051 p, eol_flag);
17052
17053 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17054 #ifdef subprocesses
17055 obj = Fget_buffer_process (Fcurrent_buffer ());
17056 if (PROCESSP (obj))
17057 {
17058 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17059 p, eol_flag);
17060 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17061 p, eol_flag);
17062 }
17063 #endif /* subprocesses */
17064 #endif /* 0 */
17065 *p = 0;
17066 return decode_mode_spec_buf;
17067 }
17068 }
17069
17070 if (STRINGP (obj))
17071 {
17072 *multibyte = STRING_MULTIBYTE (obj);
17073 return (char *) SDATA (obj);
17074 }
17075 else
17076 return "";
17077 }
17078
17079
17080 /* Count up to COUNT lines starting from START / START_BYTE.
17081 But don't go beyond LIMIT_BYTE.
17082 Return the number of lines thus found (always nonnegative).
17083
17084 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17085
17086 static int
17087 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17088 int start, start_byte, limit_byte, count;
17089 int *byte_pos_ptr;
17090 {
17091 register unsigned char *cursor;
17092 unsigned char *base;
17093
17094 register int ceiling;
17095 register unsigned char *ceiling_addr;
17096 int orig_count = count;
17097
17098 /* If we are not in selective display mode,
17099 check only for newlines. */
17100 int selective_display = (!NILP (current_buffer->selective_display)
17101 && !INTEGERP (current_buffer->selective_display));
17102
17103 if (count > 0)
17104 {
17105 while (start_byte < limit_byte)
17106 {
17107 ceiling = BUFFER_CEILING_OF (start_byte);
17108 ceiling = min (limit_byte - 1, ceiling);
17109 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17110 base = (cursor = BYTE_POS_ADDR (start_byte));
17111 while (1)
17112 {
17113 if (selective_display)
17114 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17115 ;
17116 else
17117 while (*cursor != '\n' && ++cursor != ceiling_addr)
17118 ;
17119
17120 if (cursor != ceiling_addr)
17121 {
17122 if (--count == 0)
17123 {
17124 start_byte += cursor - base + 1;
17125 *byte_pos_ptr = start_byte;
17126 return orig_count;
17127 }
17128 else
17129 if (++cursor == ceiling_addr)
17130 break;
17131 }
17132 else
17133 break;
17134 }
17135 start_byte += cursor - base;
17136 }
17137 }
17138 else
17139 {
17140 while (start_byte > limit_byte)
17141 {
17142 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
17143 ceiling = max (limit_byte, ceiling);
17144 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
17145 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
17146 while (1)
17147 {
17148 if (selective_display)
17149 while (--cursor != ceiling_addr
17150 && *cursor != '\n' && *cursor != 015)
17151 ;
17152 else
17153 while (--cursor != ceiling_addr && *cursor != '\n')
17154 ;
17155
17156 if (cursor != ceiling_addr)
17157 {
17158 if (++count == 0)
17159 {
17160 start_byte += cursor - base + 1;
17161 *byte_pos_ptr = start_byte;
17162 /* When scanning backwards, we should
17163 not count the newline posterior to which we stop. */
17164 return - orig_count - 1;
17165 }
17166 }
17167 else
17168 break;
17169 }
17170 /* Here we add 1 to compensate for the last decrement
17171 of CURSOR, which took it past the valid range. */
17172 start_byte += cursor - base + 1;
17173 }
17174 }
17175
17176 *byte_pos_ptr = limit_byte;
17177
17178 if (count < 0)
17179 return - orig_count + count;
17180 return orig_count - count;
17181
17182 }
17183
17184
17185 \f
17186 /***********************************************************************
17187 Displaying strings
17188 ***********************************************************************/
17189
17190 /* Display a NUL-terminated string, starting with index START.
17191
17192 If STRING is non-null, display that C string. Otherwise, the Lisp
17193 string LISP_STRING is displayed.
17194
17195 If FACE_STRING is not nil, FACE_STRING_POS is a position in
17196 FACE_STRING. Display STRING or LISP_STRING with the face at
17197 FACE_STRING_POS in FACE_STRING:
17198
17199 Display the string in the environment given by IT, but use the
17200 standard display table, temporarily.
17201
17202 FIELD_WIDTH is the minimum number of output glyphs to produce.
17203 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17204 with spaces. If STRING has more characters, more than FIELD_WIDTH
17205 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
17206
17207 PRECISION is the maximum number of characters to output from
17208 STRING. PRECISION < 0 means don't truncate the string.
17209
17210 This is roughly equivalent to printf format specifiers:
17211
17212 FIELD_WIDTH PRECISION PRINTF
17213 ----------------------------------------
17214 -1 -1 %s
17215 -1 10 %.10s
17216 10 -1 %10s
17217 20 10 %20.10s
17218
17219 MULTIBYTE zero means do not display multibyte chars, > 0 means do
17220 display them, and < 0 means obey the current buffer's value of
17221 enable_multibyte_characters.
17222
17223 Value is the number of glyphs produced. */
17224
17225 static int
17226 display_string (string, lisp_string, face_string, face_string_pos,
17227 start, it, field_width, precision, max_x, multibyte)
17228 unsigned char *string;
17229 Lisp_Object lisp_string;
17230 Lisp_Object face_string;
17231 int face_string_pos;
17232 int start;
17233 struct it *it;
17234 int field_width, precision, max_x;
17235 int multibyte;
17236 {
17237 int hpos_at_start = it->hpos;
17238 int saved_face_id = it->face_id;
17239 struct glyph_row *row = it->glyph_row;
17240
17241 /* Initialize the iterator IT for iteration over STRING beginning
17242 with index START. */
17243 reseat_to_string (it, string, lisp_string, start,
17244 precision, field_width, multibyte);
17245
17246 /* If displaying STRING, set up the face of the iterator
17247 from LISP_STRING, if that's given. */
17248 if (STRINGP (face_string))
17249 {
17250 int endptr;
17251 struct face *face;
17252
17253 it->face_id
17254 = face_at_string_position (it->w, face_string, face_string_pos,
17255 0, it->region_beg_charpos,
17256 it->region_end_charpos,
17257 &endptr, it->base_face_id, 0);
17258 face = FACE_FROM_ID (it->f, it->face_id);
17259 it->face_box_p = face->box != FACE_NO_BOX;
17260 }
17261
17262 /* Set max_x to the maximum allowed X position. Don't let it go
17263 beyond the right edge of the window. */
17264 if (max_x <= 0)
17265 max_x = it->last_visible_x;
17266 else
17267 max_x = min (max_x, it->last_visible_x);
17268
17269 /* Skip over display elements that are not visible. because IT->w is
17270 hscrolled. */
17271 if (it->current_x < it->first_visible_x)
17272 move_it_in_display_line_to (it, 100000, it->first_visible_x,
17273 MOVE_TO_POS | MOVE_TO_X);
17274
17275 row->ascent = it->max_ascent;
17276 row->height = it->max_ascent + it->max_descent;
17277 row->phys_ascent = it->max_phys_ascent;
17278 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17279 row->extra_line_spacing = it->max_extra_line_spacing;
17280
17281 /* This condition is for the case that we are called with current_x
17282 past last_visible_x. */
17283 while (it->current_x < max_x)
17284 {
17285 int x_before, x, n_glyphs_before, i, nglyphs;
17286
17287 /* Get the next display element. */
17288 if (!get_next_display_element (it))
17289 break;
17290
17291 /* Produce glyphs. */
17292 x_before = it->current_x;
17293 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
17294 PRODUCE_GLYPHS (it);
17295
17296 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
17297 i = 0;
17298 x = x_before;
17299 while (i < nglyphs)
17300 {
17301 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
17302
17303 if (!it->truncate_lines_p
17304 && x + glyph->pixel_width > max_x)
17305 {
17306 /* End of continued line or max_x reached. */
17307 if (CHAR_GLYPH_PADDING_P (*glyph))
17308 {
17309 /* A wide character is unbreakable. */
17310 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
17311 it->current_x = x_before;
17312 }
17313 else
17314 {
17315 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
17316 it->current_x = x;
17317 }
17318 break;
17319 }
17320 else if (x + glyph->pixel_width > it->first_visible_x)
17321 {
17322 /* Glyph is at least partially visible. */
17323 ++it->hpos;
17324 if (x < it->first_visible_x)
17325 it->glyph_row->x = x - it->first_visible_x;
17326 }
17327 else
17328 {
17329 /* Glyph is off the left margin of the display area.
17330 Should not happen. */
17331 abort ();
17332 }
17333
17334 row->ascent = max (row->ascent, it->max_ascent);
17335 row->height = max (row->height, it->max_ascent + it->max_descent);
17336 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
17337 row->phys_height = max (row->phys_height,
17338 it->max_phys_ascent + it->max_phys_descent);
17339 row->extra_line_spacing = max (row->extra_line_spacing,
17340 it->max_extra_line_spacing);
17341 x += glyph->pixel_width;
17342 ++i;
17343 }
17344
17345 /* Stop if max_x reached. */
17346 if (i < nglyphs)
17347 break;
17348
17349 /* Stop at line ends. */
17350 if (ITERATOR_AT_END_OF_LINE_P (it))
17351 {
17352 it->continuation_lines_width = 0;
17353 break;
17354 }
17355
17356 set_iterator_to_next (it, 1);
17357
17358 /* Stop if truncating at the right edge. */
17359 if (it->truncate_lines_p
17360 && it->current_x >= it->last_visible_x)
17361 {
17362 /* Add truncation mark, but don't do it if the line is
17363 truncated at a padding space. */
17364 if (IT_CHARPOS (*it) < it->string_nchars)
17365 {
17366 if (!FRAME_WINDOW_P (it->f))
17367 {
17368 int i, n;
17369
17370 if (it->current_x > it->last_visible_x)
17371 {
17372 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
17373 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
17374 break;
17375 for (n = row->used[TEXT_AREA]; i < n; ++i)
17376 {
17377 row->used[TEXT_AREA] = i;
17378 produce_special_glyphs (it, IT_TRUNCATION);
17379 }
17380 }
17381 produce_special_glyphs (it, IT_TRUNCATION);
17382 }
17383 it->glyph_row->truncated_on_right_p = 1;
17384 }
17385 break;
17386 }
17387 }
17388
17389 /* Maybe insert a truncation at the left. */
17390 if (it->first_visible_x
17391 && IT_CHARPOS (*it) > 0)
17392 {
17393 if (!FRAME_WINDOW_P (it->f))
17394 insert_left_trunc_glyphs (it);
17395 it->glyph_row->truncated_on_left_p = 1;
17396 }
17397
17398 it->face_id = saved_face_id;
17399
17400 /* Value is number of columns displayed. */
17401 return it->hpos - hpos_at_start;
17402 }
17403
17404
17405 \f
17406 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
17407 appears as an element of LIST or as the car of an element of LIST.
17408 If PROPVAL is a list, compare each element against LIST in that
17409 way, and return 1/2 if any element of PROPVAL is found in LIST.
17410 Otherwise return 0. This function cannot quit.
17411 The return value is 2 if the text is invisible but with an ellipsis
17412 and 1 if it's invisible and without an ellipsis. */
17413
17414 int
17415 invisible_p (propval, list)
17416 register Lisp_Object propval;
17417 Lisp_Object list;
17418 {
17419 register Lisp_Object tail, proptail;
17420
17421 for (tail = list; CONSP (tail); tail = XCDR (tail))
17422 {
17423 register Lisp_Object tem;
17424 tem = XCAR (tail);
17425 if (EQ (propval, tem))
17426 return 1;
17427 if (CONSP (tem) && EQ (propval, XCAR (tem)))
17428 return NILP (XCDR (tem)) ? 1 : 2;
17429 }
17430
17431 if (CONSP (propval))
17432 {
17433 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
17434 {
17435 Lisp_Object propelt;
17436 propelt = XCAR (proptail);
17437 for (tail = list; CONSP (tail); tail = XCDR (tail))
17438 {
17439 register Lisp_Object tem;
17440 tem = XCAR (tail);
17441 if (EQ (propelt, tem))
17442 return 1;
17443 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
17444 return NILP (XCDR (tem)) ? 1 : 2;
17445 }
17446 }
17447 }
17448
17449 return 0;
17450 }
17451
17452 /* Calculate a width or height in pixels from a specification using
17453 the following elements:
17454
17455 SPEC ::=
17456 NUM - a (fractional) multiple of the default font width/height
17457 (NUM) - specifies exactly NUM pixels
17458 UNIT - a fixed number of pixels, see below.
17459 ELEMENT - size of a display element in pixels, see below.
17460 (NUM . SPEC) - equals NUM * SPEC
17461 (+ SPEC SPEC ...) - add pixel values
17462 (- SPEC SPEC ...) - subtract pixel values
17463 (- SPEC) - negate pixel value
17464
17465 NUM ::=
17466 INT or FLOAT - a number constant
17467 SYMBOL - use symbol's (buffer local) variable binding.
17468
17469 UNIT ::=
17470 in - pixels per inch *)
17471 mm - pixels per 1/1000 meter *)
17472 cm - pixels per 1/100 meter *)
17473 width - width of current font in pixels.
17474 height - height of current font in pixels.
17475
17476 *) using the ratio(s) defined in display-pixels-per-inch.
17477
17478 ELEMENT ::=
17479
17480 left-fringe - left fringe width in pixels
17481 right-fringe - right fringe width in pixels
17482
17483 left-margin - left margin width in pixels
17484 right-margin - right margin width in pixels
17485
17486 scroll-bar - scroll-bar area width in pixels
17487
17488 Examples:
17489
17490 Pixels corresponding to 5 inches:
17491 (5 . in)
17492
17493 Total width of non-text areas on left side of window (if scroll-bar is on left):
17494 '(space :width (+ left-fringe left-margin scroll-bar))
17495
17496 Align to first text column (in header line):
17497 '(space :align-to 0)
17498
17499 Align to middle of text area minus half the width of variable `my-image'
17500 containing a loaded image:
17501 '(space :align-to (0.5 . (- text my-image)))
17502
17503 Width of left margin minus width of 1 character in the default font:
17504 '(space :width (- left-margin 1))
17505
17506 Width of left margin minus width of 2 characters in the current font:
17507 '(space :width (- left-margin (2 . width)))
17508
17509 Center 1 character over left-margin (in header line):
17510 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
17511
17512 Different ways to express width of left fringe plus left margin minus one pixel:
17513 '(space :width (- (+ left-fringe left-margin) (1)))
17514 '(space :width (+ left-fringe left-margin (- (1))))
17515 '(space :width (+ left-fringe left-margin (-1)))
17516
17517 */
17518
17519 #define NUMVAL(X) \
17520 ((INTEGERP (X) || FLOATP (X)) \
17521 ? XFLOATINT (X) \
17522 : - 1)
17523
17524 int
17525 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
17526 double *res;
17527 struct it *it;
17528 Lisp_Object prop;
17529 void *font;
17530 int width_p, *align_to;
17531 {
17532 double pixels;
17533
17534 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
17535 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
17536
17537 if (NILP (prop))
17538 return OK_PIXELS (0);
17539
17540 if (SYMBOLP (prop))
17541 {
17542 if (SCHARS (SYMBOL_NAME (prop)) == 2)
17543 {
17544 char *unit = SDATA (SYMBOL_NAME (prop));
17545
17546 if (unit[0] == 'i' && unit[1] == 'n')
17547 pixels = 1.0;
17548 else if (unit[0] == 'm' && unit[1] == 'm')
17549 pixels = 25.4;
17550 else if (unit[0] == 'c' && unit[1] == 'm')
17551 pixels = 2.54;
17552 else
17553 pixels = 0;
17554 if (pixels > 0)
17555 {
17556 double ppi;
17557 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
17558 || (CONSP (Vdisplay_pixels_per_inch)
17559 && (ppi = (width_p
17560 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
17561 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
17562 ppi > 0)))
17563 return OK_PIXELS (ppi / pixels);
17564
17565 return 0;
17566 }
17567 }
17568
17569 #ifdef HAVE_WINDOW_SYSTEM
17570 if (EQ (prop, Qheight))
17571 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
17572 if (EQ (prop, Qwidth))
17573 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
17574 #else
17575 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
17576 return OK_PIXELS (1);
17577 #endif
17578
17579 if (EQ (prop, Qtext))
17580 return OK_PIXELS (width_p
17581 ? window_box_width (it->w, TEXT_AREA)
17582 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
17583
17584 if (align_to && *align_to < 0)
17585 {
17586 *res = 0;
17587 if (EQ (prop, Qleft))
17588 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
17589 if (EQ (prop, Qright))
17590 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
17591 if (EQ (prop, Qcenter))
17592 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
17593 + window_box_width (it->w, TEXT_AREA) / 2);
17594 if (EQ (prop, Qleft_fringe))
17595 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17596 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
17597 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
17598 if (EQ (prop, Qright_fringe))
17599 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17600 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17601 : window_box_right_offset (it->w, TEXT_AREA));
17602 if (EQ (prop, Qleft_margin))
17603 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
17604 if (EQ (prop, Qright_margin))
17605 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
17606 if (EQ (prop, Qscroll_bar))
17607 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
17608 ? 0
17609 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
17610 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
17611 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
17612 : 0)));
17613 }
17614 else
17615 {
17616 if (EQ (prop, Qleft_fringe))
17617 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
17618 if (EQ (prop, Qright_fringe))
17619 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
17620 if (EQ (prop, Qleft_margin))
17621 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
17622 if (EQ (prop, Qright_margin))
17623 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
17624 if (EQ (prop, Qscroll_bar))
17625 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
17626 }
17627
17628 prop = Fbuffer_local_value (prop, it->w->buffer);
17629 }
17630
17631 if (INTEGERP (prop) || FLOATP (prop))
17632 {
17633 int base_unit = (width_p
17634 ? FRAME_COLUMN_WIDTH (it->f)
17635 : FRAME_LINE_HEIGHT (it->f));
17636 return OK_PIXELS (XFLOATINT (prop) * base_unit);
17637 }
17638
17639 if (CONSP (prop))
17640 {
17641 Lisp_Object car = XCAR (prop);
17642 Lisp_Object cdr = XCDR (prop);
17643
17644 if (SYMBOLP (car))
17645 {
17646 #ifdef HAVE_WINDOW_SYSTEM
17647 if (valid_image_p (prop))
17648 {
17649 int id = lookup_image (it->f, prop);
17650 struct image *img = IMAGE_FROM_ID (it->f, id);
17651
17652 return OK_PIXELS (width_p ? img->width : img->height);
17653 }
17654 #endif
17655 if (EQ (car, Qplus) || EQ (car, Qminus))
17656 {
17657 int first = 1;
17658 double px;
17659
17660 pixels = 0;
17661 while (CONSP (cdr))
17662 {
17663 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
17664 font, width_p, align_to))
17665 return 0;
17666 if (first)
17667 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
17668 else
17669 pixels += px;
17670 cdr = XCDR (cdr);
17671 }
17672 if (EQ (car, Qminus))
17673 pixels = -pixels;
17674 return OK_PIXELS (pixels);
17675 }
17676
17677 car = Fbuffer_local_value (car, it->w->buffer);
17678 }
17679
17680 if (INTEGERP (car) || FLOATP (car))
17681 {
17682 double fact;
17683 pixels = XFLOATINT (car);
17684 if (NILP (cdr))
17685 return OK_PIXELS (pixels);
17686 if (calc_pixel_width_or_height (&fact, it, cdr,
17687 font, width_p, align_to))
17688 return OK_PIXELS (pixels * fact);
17689 return 0;
17690 }
17691
17692 return 0;
17693 }
17694
17695 return 0;
17696 }
17697
17698 \f
17699 /***********************************************************************
17700 Glyph Display
17701 ***********************************************************************/
17702
17703 #ifdef HAVE_WINDOW_SYSTEM
17704
17705 #if GLYPH_DEBUG
17706
17707 void
17708 dump_glyph_string (s)
17709 struct glyph_string *s;
17710 {
17711 fprintf (stderr, "glyph string\n");
17712 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
17713 s->x, s->y, s->width, s->height);
17714 fprintf (stderr, " ybase = %d\n", s->ybase);
17715 fprintf (stderr, " hl = %d\n", s->hl);
17716 fprintf (stderr, " left overhang = %d, right = %d\n",
17717 s->left_overhang, s->right_overhang);
17718 fprintf (stderr, " nchars = %d\n", s->nchars);
17719 fprintf (stderr, " extends to end of line = %d\n",
17720 s->extends_to_end_of_line_p);
17721 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
17722 fprintf (stderr, " bg width = %d\n", s->background_width);
17723 }
17724
17725 #endif /* GLYPH_DEBUG */
17726
17727 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
17728 of XChar2b structures for S; it can't be allocated in
17729 init_glyph_string because it must be allocated via `alloca'. W
17730 is the window on which S is drawn. ROW and AREA are the glyph row
17731 and area within the row from which S is constructed. START is the
17732 index of the first glyph structure covered by S. HL is a
17733 face-override for drawing S. */
17734
17735 #ifdef HAVE_NTGUI
17736 #define OPTIONAL_HDC(hdc) hdc,
17737 #define DECLARE_HDC(hdc) HDC hdc;
17738 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
17739 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
17740 #endif
17741
17742 #ifndef OPTIONAL_HDC
17743 #define OPTIONAL_HDC(hdc)
17744 #define DECLARE_HDC(hdc)
17745 #define ALLOCATE_HDC(hdc, f)
17746 #define RELEASE_HDC(hdc, f)
17747 #endif
17748
17749 static void
17750 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
17751 struct glyph_string *s;
17752 DECLARE_HDC (hdc)
17753 XChar2b *char2b;
17754 struct window *w;
17755 struct glyph_row *row;
17756 enum glyph_row_area area;
17757 int start;
17758 enum draw_glyphs_face hl;
17759 {
17760 bzero (s, sizeof *s);
17761 s->w = w;
17762 s->f = XFRAME (w->frame);
17763 #ifdef HAVE_NTGUI
17764 s->hdc = hdc;
17765 #endif
17766 s->display = FRAME_X_DISPLAY (s->f);
17767 s->window = FRAME_X_WINDOW (s->f);
17768 s->char2b = char2b;
17769 s->hl = hl;
17770 s->row = row;
17771 s->area = area;
17772 s->first_glyph = row->glyphs[area] + start;
17773 s->height = row->height;
17774 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
17775
17776 /* Display the internal border below the tool-bar window. */
17777 if (WINDOWP (s->f->tool_bar_window)
17778 && s->w == XWINDOW (s->f->tool_bar_window))
17779 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
17780
17781 s->ybase = s->y + row->ascent;
17782 }
17783
17784
17785 /* Append the list of glyph strings with head H and tail T to the list
17786 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
17787
17788 static INLINE void
17789 append_glyph_string_lists (head, tail, h, t)
17790 struct glyph_string **head, **tail;
17791 struct glyph_string *h, *t;
17792 {
17793 if (h)
17794 {
17795 if (*head)
17796 (*tail)->next = h;
17797 else
17798 *head = h;
17799 h->prev = *tail;
17800 *tail = t;
17801 }
17802 }
17803
17804
17805 /* Prepend the list of glyph strings with head H and tail T to the
17806 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
17807 result. */
17808
17809 static INLINE void
17810 prepend_glyph_string_lists (head, tail, h, t)
17811 struct glyph_string **head, **tail;
17812 struct glyph_string *h, *t;
17813 {
17814 if (h)
17815 {
17816 if (*head)
17817 (*head)->prev = t;
17818 else
17819 *tail = t;
17820 t->next = *head;
17821 *head = h;
17822 }
17823 }
17824
17825
17826 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
17827 Set *HEAD and *TAIL to the resulting list. */
17828
17829 static INLINE void
17830 append_glyph_string (head, tail, s)
17831 struct glyph_string **head, **tail;
17832 struct glyph_string *s;
17833 {
17834 s->next = s->prev = NULL;
17835 append_glyph_string_lists (head, tail, s, s);
17836 }
17837
17838
17839 /* Get face and two-byte form of character glyph GLYPH on frame F.
17840 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
17841 a pointer to a realized face that is ready for display. */
17842
17843 static INLINE struct face *
17844 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
17845 struct frame *f;
17846 struct glyph *glyph;
17847 XChar2b *char2b;
17848 int *two_byte_p;
17849 {
17850 struct face *face;
17851
17852 xassert (glyph->type == CHAR_GLYPH);
17853 face = FACE_FROM_ID (f, glyph->face_id);
17854
17855 if (two_byte_p)
17856 *two_byte_p = 0;
17857
17858 if (!glyph->multibyte_p)
17859 {
17860 /* Unibyte case. We don't have to encode, but we have to make
17861 sure to use a face suitable for unibyte. */
17862 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17863 }
17864 else if (glyph->u.ch < 128
17865 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
17866 {
17867 /* Case of ASCII in a face known to fit ASCII. */
17868 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
17869 }
17870 else
17871 {
17872 int c1, c2, charset;
17873
17874 /* Split characters into bytes. If c2 is -1 afterwards, C is
17875 really a one-byte character so that byte1 is zero. */
17876 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
17877 if (c2 > 0)
17878 STORE_XCHAR2B (char2b, c1, c2);
17879 else
17880 STORE_XCHAR2B (char2b, 0, c1);
17881
17882 /* Maybe encode the character in *CHAR2B. */
17883 if (charset != CHARSET_ASCII)
17884 {
17885 struct font_info *font_info
17886 = FONT_INFO_FROM_ID (f, face->font_info_id);
17887 if (font_info)
17888 glyph->font_type
17889 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
17890 }
17891 }
17892
17893 /* Make sure X resources of the face are allocated. */
17894 xassert (face != NULL);
17895 PREPARE_FACE_FOR_DISPLAY (f, face);
17896 return face;
17897 }
17898
17899
17900 /* Fill glyph string S with composition components specified by S->cmp.
17901
17902 FACES is an array of faces for all components of this composition.
17903 S->gidx is the index of the first component for S.
17904 OVERLAPS_P non-zero means S should draw the foreground only, and
17905 use its physical height for clipping.
17906
17907 Value is the index of a component not in S. */
17908
17909 static int
17910 fill_composite_glyph_string (s, faces, overlaps_p)
17911 struct glyph_string *s;
17912 struct face **faces;
17913 int overlaps_p;
17914 {
17915 int i;
17916
17917 xassert (s);
17918
17919 s->for_overlaps_p = overlaps_p;
17920
17921 s->face = faces[s->gidx];
17922 s->font = s->face->font;
17923 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
17924
17925 /* For all glyphs of this composition, starting at the offset
17926 S->gidx, until we reach the end of the definition or encounter a
17927 glyph that requires the different face, add it to S. */
17928 ++s->nchars;
17929 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
17930 ++s->nchars;
17931
17932 /* All glyph strings for the same composition has the same width,
17933 i.e. the width set for the first component of the composition. */
17934
17935 s->width = s->first_glyph->pixel_width;
17936
17937 /* If the specified font could not be loaded, use the frame's
17938 default font, but record the fact that we couldn't load it in
17939 the glyph string so that we can draw rectangles for the
17940 characters of the glyph string. */
17941 if (s->font == NULL)
17942 {
17943 s->font_not_found_p = 1;
17944 s->font = FRAME_FONT (s->f);
17945 }
17946
17947 /* Adjust base line for subscript/superscript text. */
17948 s->ybase += s->first_glyph->voffset;
17949
17950 xassert (s->face && s->face->gc);
17951
17952 /* This glyph string must always be drawn with 16-bit functions. */
17953 s->two_byte_p = 1;
17954
17955 return s->gidx + s->nchars;
17956 }
17957
17958
17959 /* Fill glyph string S from a sequence of character glyphs.
17960
17961 FACE_ID is the face id of the string. START is the index of the
17962 first glyph to consider, END is the index of the last + 1.
17963 OVERLAPS_P non-zero means S should draw the foreground only, and
17964 use its physical height for clipping.
17965
17966 Value is the index of the first glyph not in S. */
17967
17968 static int
17969 fill_glyph_string (s, face_id, start, end, overlaps_p)
17970 struct glyph_string *s;
17971 int face_id;
17972 int start, end, overlaps_p;
17973 {
17974 struct glyph *glyph, *last;
17975 int voffset;
17976 int glyph_not_available_p;
17977
17978 xassert (s->f == XFRAME (s->w->frame));
17979 xassert (s->nchars == 0);
17980 xassert (start >= 0 && end > start);
17981
17982 s->for_overlaps_p = overlaps_p,
17983 glyph = s->row->glyphs[s->area] + start;
17984 last = s->row->glyphs[s->area] + end;
17985 voffset = glyph->voffset;
17986
17987 glyph_not_available_p = glyph->glyph_not_available_p;
17988
17989 while (glyph < last
17990 && glyph->type == CHAR_GLYPH
17991 && glyph->voffset == voffset
17992 /* Same face id implies same font, nowadays. */
17993 && glyph->face_id == face_id
17994 && glyph->glyph_not_available_p == glyph_not_available_p)
17995 {
17996 int two_byte_p;
17997
17998 s->face = get_glyph_face_and_encoding (s->f, glyph,
17999 s->char2b + s->nchars,
18000 &two_byte_p);
18001 s->two_byte_p = two_byte_p;
18002 ++s->nchars;
18003 xassert (s->nchars <= end - start);
18004 s->width += glyph->pixel_width;
18005 ++glyph;
18006 }
18007
18008 s->font = s->face->font;
18009 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18010
18011 /* If the specified font could not be loaded, use the frame's font,
18012 but record the fact that we couldn't load it in
18013 S->font_not_found_p so that we can draw rectangles for the
18014 characters of the glyph string. */
18015 if (s->font == NULL || glyph_not_available_p)
18016 {
18017 s->font_not_found_p = 1;
18018 s->font = FRAME_FONT (s->f);
18019 }
18020
18021 /* Adjust base line for subscript/superscript text. */
18022 s->ybase += voffset;
18023
18024 xassert (s->face && s->face->gc);
18025 return glyph - s->row->glyphs[s->area];
18026 }
18027
18028
18029 /* Fill glyph string S from image glyph S->first_glyph. */
18030
18031 static void
18032 fill_image_glyph_string (s)
18033 struct glyph_string *s;
18034 {
18035 xassert (s->first_glyph->type == IMAGE_GLYPH);
18036 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18037 xassert (s->img);
18038 s->slice = s->first_glyph->slice;
18039 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18040 s->font = s->face->font;
18041 s->width = s->first_glyph->pixel_width;
18042
18043 /* Adjust base line for subscript/superscript text. */
18044 s->ybase += s->first_glyph->voffset;
18045 }
18046
18047
18048 /* Fill glyph string S from a sequence of stretch glyphs.
18049
18050 ROW is the glyph row in which the glyphs are found, AREA is the
18051 area within the row. START is the index of the first glyph to
18052 consider, END is the index of the last + 1.
18053
18054 Value is the index of the first glyph not in S. */
18055
18056 static int
18057 fill_stretch_glyph_string (s, row, area, start, end)
18058 struct glyph_string *s;
18059 struct glyph_row *row;
18060 enum glyph_row_area area;
18061 int start, end;
18062 {
18063 struct glyph *glyph, *last;
18064 int voffset, face_id;
18065
18066 xassert (s->first_glyph->type == STRETCH_GLYPH);
18067
18068 glyph = s->row->glyphs[s->area] + start;
18069 last = s->row->glyphs[s->area] + end;
18070 face_id = glyph->face_id;
18071 s->face = FACE_FROM_ID (s->f, face_id);
18072 s->font = s->face->font;
18073 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18074 s->width = glyph->pixel_width;
18075 voffset = glyph->voffset;
18076
18077 for (++glyph;
18078 (glyph < last
18079 && glyph->type == STRETCH_GLYPH
18080 && glyph->voffset == voffset
18081 && glyph->face_id == face_id);
18082 ++glyph)
18083 s->width += glyph->pixel_width;
18084
18085 /* Adjust base line for subscript/superscript text. */
18086 s->ybase += voffset;
18087
18088 /* The case that face->gc == 0 is handled when drawing the glyph
18089 string by calling PREPARE_FACE_FOR_DISPLAY. */
18090 xassert (s->face);
18091 return glyph - s->row->glyphs[s->area];
18092 }
18093
18094
18095 /* EXPORT for RIF:
18096 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18097 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18098 assumed to be zero. */
18099
18100 void
18101 x_get_glyph_overhangs (glyph, f, left, right)
18102 struct glyph *glyph;
18103 struct frame *f;
18104 int *left, *right;
18105 {
18106 *left = *right = 0;
18107
18108 if (glyph->type == CHAR_GLYPH)
18109 {
18110 XFontStruct *font;
18111 struct face *face;
18112 struct font_info *font_info;
18113 XChar2b char2b;
18114 XCharStruct *pcm;
18115
18116 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18117 font = face->font;
18118 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18119 if (font /* ++KFS: Should this be font_info ? */
18120 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18121 {
18122 if (pcm->rbearing > pcm->width)
18123 *right = pcm->rbearing - pcm->width;
18124 if (pcm->lbearing < 0)
18125 *left = -pcm->lbearing;
18126 }
18127 }
18128 }
18129
18130
18131 /* Return the index of the first glyph preceding glyph string S that
18132 is overwritten by S because of S's left overhang. Value is -1
18133 if no glyphs are overwritten. */
18134
18135 static int
18136 left_overwritten (s)
18137 struct glyph_string *s;
18138 {
18139 int k;
18140
18141 if (s->left_overhang)
18142 {
18143 int x = 0, i;
18144 struct glyph *glyphs = s->row->glyphs[s->area];
18145 int first = s->first_glyph - glyphs;
18146
18147 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
18148 x -= glyphs[i].pixel_width;
18149
18150 k = i + 1;
18151 }
18152 else
18153 k = -1;
18154
18155 return k;
18156 }
18157
18158
18159 /* Return the index of the first glyph preceding glyph string S that
18160 is overwriting S because of its right overhang. Value is -1 if no
18161 glyph in front of S overwrites S. */
18162
18163 static int
18164 left_overwriting (s)
18165 struct glyph_string *s;
18166 {
18167 int i, k, x;
18168 struct glyph *glyphs = s->row->glyphs[s->area];
18169 int first = s->first_glyph - glyphs;
18170
18171 k = -1;
18172 x = 0;
18173 for (i = first - 1; i >= 0; --i)
18174 {
18175 int left, right;
18176 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18177 if (x + right > 0)
18178 k = i;
18179 x -= glyphs[i].pixel_width;
18180 }
18181
18182 return k;
18183 }
18184
18185
18186 /* Return the index of the last glyph following glyph string S that is
18187 not overwritten by S because of S's right overhang. Value is -1 if
18188 no such glyph is found. */
18189
18190 static int
18191 right_overwritten (s)
18192 struct glyph_string *s;
18193 {
18194 int k = -1;
18195
18196 if (s->right_overhang)
18197 {
18198 int x = 0, i;
18199 struct glyph *glyphs = s->row->glyphs[s->area];
18200 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18201 int end = s->row->used[s->area];
18202
18203 for (i = first; i < end && s->right_overhang > x; ++i)
18204 x += glyphs[i].pixel_width;
18205
18206 k = i;
18207 }
18208
18209 return k;
18210 }
18211
18212
18213 /* Return the index of the last glyph following glyph string S that
18214 overwrites S because of its left overhang. Value is negative
18215 if no such glyph is found. */
18216
18217 static int
18218 right_overwriting (s)
18219 struct glyph_string *s;
18220 {
18221 int i, k, x;
18222 int end = s->row->used[s->area];
18223 struct glyph *glyphs = s->row->glyphs[s->area];
18224 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
18225
18226 k = -1;
18227 x = 0;
18228 for (i = first; i < end; ++i)
18229 {
18230 int left, right;
18231 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
18232 if (x - left < 0)
18233 k = i;
18234 x += glyphs[i].pixel_width;
18235 }
18236
18237 return k;
18238 }
18239
18240
18241 /* Get face and two-byte form of character C in face FACE_ID on frame
18242 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
18243 means we want to display multibyte text. DISPLAY_P non-zero means
18244 make sure that X resources for the face returned are allocated.
18245 Value is a pointer to a realized face that is ready for display if
18246 DISPLAY_P is non-zero. */
18247
18248 static INLINE struct face *
18249 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
18250 struct frame *f;
18251 int c, face_id;
18252 XChar2b *char2b;
18253 int multibyte_p, display_p;
18254 {
18255 struct face *face = FACE_FROM_ID (f, face_id);
18256
18257 if (!multibyte_p)
18258 {
18259 /* Unibyte case. We don't have to encode, but we have to make
18260 sure to use a face suitable for unibyte. */
18261 STORE_XCHAR2B (char2b, 0, c);
18262 face_id = FACE_FOR_CHAR (f, face, c);
18263 face = FACE_FROM_ID (f, face_id);
18264 }
18265 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
18266 {
18267 /* Case of ASCII in a face known to fit ASCII. */
18268 STORE_XCHAR2B (char2b, 0, c);
18269 }
18270 else
18271 {
18272 int c1, c2, charset;
18273
18274 /* Split characters into bytes. If c2 is -1 afterwards, C is
18275 really a one-byte character so that byte1 is zero. */
18276 SPLIT_CHAR (c, charset, c1, c2);
18277 if (c2 > 0)
18278 STORE_XCHAR2B (char2b, c1, c2);
18279 else
18280 STORE_XCHAR2B (char2b, 0, c1);
18281
18282 /* Maybe encode the character in *CHAR2B. */
18283 if (face->font != NULL)
18284 {
18285 struct font_info *font_info
18286 = FONT_INFO_FROM_ID (f, face->font_info_id);
18287 if (font_info)
18288 rif->encode_char (c, char2b, font_info, 0);
18289 }
18290 }
18291
18292 /* Make sure X resources of the face are allocated. */
18293 #ifdef HAVE_X_WINDOWS
18294 if (display_p)
18295 #endif
18296 {
18297 xassert (face != NULL);
18298 PREPARE_FACE_FOR_DISPLAY (f, face);
18299 }
18300
18301 return face;
18302 }
18303
18304
18305 /* Set background width of glyph string S. START is the index of the
18306 first glyph following S. LAST_X is the right-most x-position + 1
18307 in the drawing area. */
18308
18309 static INLINE void
18310 set_glyph_string_background_width (s, start, last_x)
18311 struct glyph_string *s;
18312 int start;
18313 int last_x;
18314 {
18315 /* If the face of this glyph string has to be drawn to the end of
18316 the drawing area, set S->extends_to_end_of_line_p. */
18317 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
18318
18319 if (start == s->row->used[s->area]
18320 && s->area == TEXT_AREA
18321 && ((s->hl == DRAW_NORMAL_TEXT
18322 && (s->row->fill_line_p
18323 || s->face->background != default_face->background
18324 || s->face->stipple != default_face->stipple
18325 || s->row->mouse_face_p))
18326 || s->hl == DRAW_MOUSE_FACE
18327 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
18328 && s->row->fill_line_p)))
18329 s->extends_to_end_of_line_p = 1;
18330
18331 /* If S extends its face to the end of the line, set its
18332 background_width to the distance to the right edge of the drawing
18333 area. */
18334 if (s->extends_to_end_of_line_p)
18335 s->background_width = last_x - s->x + 1;
18336 else
18337 s->background_width = s->width;
18338 }
18339
18340
18341 /* Compute overhangs and x-positions for glyph string S and its
18342 predecessors, or successors. X is the starting x-position for S.
18343 BACKWARD_P non-zero means process predecessors. */
18344
18345 static void
18346 compute_overhangs_and_x (s, x, backward_p)
18347 struct glyph_string *s;
18348 int x;
18349 int backward_p;
18350 {
18351 if (backward_p)
18352 {
18353 while (s)
18354 {
18355 if (rif->compute_glyph_string_overhangs)
18356 rif->compute_glyph_string_overhangs (s);
18357 x -= s->width;
18358 s->x = x;
18359 s = s->prev;
18360 }
18361 }
18362 else
18363 {
18364 while (s)
18365 {
18366 if (rif->compute_glyph_string_overhangs)
18367 rif->compute_glyph_string_overhangs (s);
18368 s->x = x;
18369 x += s->width;
18370 s = s->next;
18371 }
18372 }
18373 }
18374
18375
18376
18377 /* The following macros are only called from draw_glyphs below.
18378 They reference the following parameters of that function directly:
18379 `w', `row', `area', and `overlap_p'
18380 as well as the following local variables:
18381 `s', `f', and `hdc' (in W32) */
18382
18383 #ifdef HAVE_NTGUI
18384 /* On W32, silently add local `hdc' variable to argument list of
18385 init_glyph_string. */
18386 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18387 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
18388 #else
18389 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
18390 init_glyph_string (s, char2b, w, row, area, start, hl)
18391 #endif
18392
18393 /* Add a glyph string for a stretch glyph to the list of strings
18394 between HEAD and TAIL. START is the index of the stretch glyph in
18395 row area AREA of glyph row ROW. END is the index of the last glyph
18396 in that glyph row area. X is the current output position assigned
18397 to the new glyph string constructed. HL overrides that face of the
18398 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18399 is the right-most x-position of the drawing area. */
18400
18401 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
18402 and below -- keep them on one line. */
18403 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18404 do \
18405 { \
18406 s = (struct glyph_string *) alloca (sizeof *s); \
18407 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18408 START = fill_stretch_glyph_string (s, row, area, START, END); \
18409 append_glyph_string (&HEAD, &TAIL, s); \
18410 s->x = (X); \
18411 } \
18412 while (0)
18413
18414
18415 /* Add a glyph string for an image glyph to the list of strings
18416 between HEAD and TAIL. START is the index of the image glyph in
18417 row area AREA of glyph row ROW. END is the index of the last glyph
18418 in that glyph row area. X is the current output position assigned
18419 to the new glyph string constructed. HL overrides that face of the
18420 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
18421 is the right-most x-position of the drawing area. */
18422
18423 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18424 do \
18425 { \
18426 s = (struct glyph_string *) alloca (sizeof *s); \
18427 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
18428 fill_image_glyph_string (s); \
18429 append_glyph_string (&HEAD, &TAIL, s); \
18430 ++START; \
18431 s->x = (X); \
18432 } \
18433 while (0)
18434
18435
18436 /* Add a glyph string for a sequence of character glyphs to the list
18437 of strings between HEAD and TAIL. START is the index of the first
18438 glyph in row area AREA of glyph row ROW that is part of the new
18439 glyph string. END is the index of the last glyph in that glyph row
18440 area. X is the current output position assigned to the new glyph
18441 string constructed. HL overrides that face of the glyph; e.g. it
18442 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
18443 right-most x-position of the drawing area. */
18444
18445 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18446 do \
18447 { \
18448 int c, face_id; \
18449 XChar2b *char2b; \
18450 \
18451 c = (row)->glyphs[area][START].u.ch; \
18452 face_id = (row)->glyphs[area][START].face_id; \
18453 \
18454 s = (struct glyph_string *) alloca (sizeof *s); \
18455 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
18456 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
18457 append_glyph_string (&HEAD, &TAIL, s); \
18458 s->x = (X); \
18459 START = fill_glyph_string (s, face_id, START, END, overlaps_p); \
18460 } \
18461 while (0)
18462
18463
18464 /* Add a glyph string for a composite sequence to the list of strings
18465 between HEAD and TAIL. START is the index of the first glyph in
18466 row area AREA of glyph row ROW that is part of the new glyph
18467 string. END is the index of the last glyph in that glyph row area.
18468 X is the current output position assigned to the new glyph string
18469 constructed. HL overrides that face of the glyph; e.g. it is
18470 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
18471 x-position of the drawing area. */
18472
18473 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
18474 do { \
18475 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
18476 int face_id = (row)->glyphs[area][START].face_id; \
18477 struct face *base_face = FACE_FROM_ID (f, face_id); \
18478 struct composition *cmp = composition_table[cmp_id]; \
18479 int glyph_len = cmp->glyph_len; \
18480 XChar2b *char2b; \
18481 struct face **faces; \
18482 struct glyph_string *first_s = NULL; \
18483 int n; \
18484 \
18485 base_face = base_face->ascii_face; \
18486 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
18487 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
18488 /* At first, fill in `char2b' and `faces'. */ \
18489 for (n = 0; n < glyph_len; n++) \
18490 { \
18491 int c = COMPOSITION_GLYPH (cmp, n); \
18492 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
18493 faces[n] = FACE_FROM_ID (f, this_face_id); \
18494 get_char_face_and_encoding (f, c, this_face_id, \
18495 char2b + n, 1, 1); \
18496 } \
18497 \
18498 /* Make glyph_strings for each glyph sequence that is drawable by \
18499 the same face, and append them to HEAD/TAIL. */ \
18500 for (n = 0; n < cmp->glyph_len;) \
18501 { \
18502 s = (struct glyph_string *) alloca (sizeof *s); \
18503 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
18504 append_glyph_string (&(HEAD), &(TAIL), s); \
18505 s->cmp = cmp; \
18506 s->gidx = n; \
18507 s->x = (X); \
18508 \
18509 if (n == 0) \
18510 first_s = s; \
18511 \
18512 n = fill_composite_glyph_string (s, faces, overlaps_p); \
18513 } \
18514 \
18515 ++START; \
18516 s = first_s; \
18517 } while (0)
18518
18519
18520 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
18521 of AREA of glyph row ROW on window W between indices START and END.
18522 HL overrides the face for drawing glyph strings, e.g. it is
18523 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
18524 x-positions of the drawing area.
18525
18526 This is an ugly monster macro construct because we must use alloca
18527 to allocate glyph strings (because draw_glyphs can be called
18528 asynchronously). */
18529
18530 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
18531 do \
18532 { \
18533 HEAD = TAIL = NULL; \
18534 while (START < END) \
18535 { \
18536 struct glyph *first_glyph = (row)->glyphs[area] + START; \
18537 switch (first_glyph->type) \
18538 { \
18539 case CHAR_GLYPH: \
18540 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
18541 HL, X, LAST_X); \
18542 break; \
18543 \
18544 case COMPOSITE_GLYPH: \
18545 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
18546 HL, X, LAST_X); \
18547 break; \
18548 \
18549 case STRETCH_GLYPH: \
18550 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
18551 HL, X, LAST_X); \
18552 break; \
18553 \
18554 case IMAGE_GLYPH: \
18555 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
18556 HL, X, LAST_X); \
18557 break; \
18558 \
18559 default: \
18560 abort (); \
18561 } \
18562 \
18563 set_glyph_string_background_width (s, START, LAST_X); \
18564 (X) += s->width; \
18565 } \
18566 } \
18567 while (0)
18568
18569
18570 /* Draw glyphs between START and END in AREA of ROW on window W,
18571 starting at x-position X. X is relative to AREA in W. HL is a
18572 face-override with the following meaning:
18573
18574 DRAW_NORMAL_TEXT draw normally
18575 DRAW_CURSOR draw in cursor face
18576 DRAW_MOUSE_FACE draw in mouse face.
18577 DRAW_INVERSE_VIDEO draw in mode line face
18578 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
18579 DRAW_IMAGE_RAISED draw an image with a raised relief around it
18580
18581 If OVERLAPS_P is non-zero, draw only the foreground of characters
18582 and clip to the physical height of ROW.
18583
18584 Value is the x-position reached, relative to AREA of W. */
18585
18586 static int
18587 draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
18588 struct window *w;
18589 int x;
18590 struct glyph_row *row;
18591 enum glyph_row_area area;
18592 int start, end;
18593 enum draw_glyphs_face hl;
18594 int overlaps_p;
18595 {
18596 struct glyph_string *head, *tail;
18597 struct glyph_string *s;
18598 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
18599 int last_x, area_width;
18600 int x_reached;
18601 int i, j;
18602 struct frame *f = XFRAME (WINDOW_FRAME (w));
18603 DECLARE_HDC (hdc);
18604
18605 ALLOCATE_HDC (hdc, f);
18606
18607 /* Let's rather be paranoid than getting a SEGV. */
18608 end = min (end, row->used[area]);
18609 start = max (0, start);
18610 start = min (end, start);
18611
18612 /* Translate X to frame coordinates. Set last_x to the right
18613 end of the drawing area. */
18614 if (row->full_width_p)
18615 {
18616 /* X is relative to the left edge of W, without scroll bars
18617 or fringes. */
18618 x += WINDOW_LEFT_EDGE_X (w);
18619 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
18620 }
18621 else
18622 {
18623 int area_left = window_box_left (w, area);
18624 x += area_left;
18625 area_width = window_box_width (w, area);
18626 last_x = area_left + area_width;
18627 }
18628
18629 /* Build a doubly-linked list of glyph_string structures between
18630 head and tail from what we have to draw. Note that the macro
18631 BUILD_GLYPH_STRINGS will modify its start parameter. That's
18632 the reason we use a separate variable `i'. */
18633 i = start;
18634 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
18635 if (tail)
18636 x_reached = tail->x + tail->background_width;
18637 else
18638 x_reached = x;
18639
18640 /* If there are any glyphs with lbearing < 0 or rbearing > width in
18641 the row, redraw some glyphs in front or following the glyph
18642 strings built above. */
18643 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
18644 {
18645 int dummy_x = 0;
18646 struct glyph_string *h, *t;
18647
18648 /* Compute overhangs for all glyph strings. */
18649 if (rif->compute_glyph_string_overhangs)
18650 for (s = head; s; s = s->next)
18651 rif->compute_glyph_string_overhangs (s);
18652
18653 /* Prepend glyph strings for glyphs in front of the first glyph
18654 string that are overwritten because of the first glyph
18655 string's left overhang. The background of all strings
18656 prepended must be drawn because the first glyph string
18657 draws over it. */
18658 i = left_overwritten (head);
18659 if (i >= 0)
18660 {
18661 j = i;
18662 BUILD_GLYPH_STRINGS (j, start, h, t,
18663 DRAW_NORMAL_TEXT, dummy_x, last_x);
18664 start = i;
18665 compute_overhangs_and_x (t, head->x, 1);
18666 prepend_glyph_string_lists (&head, &tail, h, t);
18667 clip_head = head;
18668 }
18669
18670 /* Prepend glyph strings for glyphs in front of the first glyph
18671 string that overwrite that glyph string because of their
18672 right overhang. For these strings, only the foreground must
18673 be drawn, because it draws over the glyph string at `head'.
18674 The background must not be drawn because this would overwrite
18675 right overhangs of preceding glyphs for which no glyph
18676 strings exist. */
18677 i = left_overwriting (head);
18678 if (i >= 0)
18679 {
18680 clip_head = head;
18681 BUILD_GLYPH_STRINGS (i, start, h, t,
18682 DRAW_NORMAL_TEXT, dummy_x, last_x);
18683 for (s = h; s; s = s->next)
18684 s->background_filled_p = 1;
18685 compute_overhangs_and_x (t, head->x, 1);
18686 prepend_glyph_string_lists (&head, &tail, h, t);
18687 }
18688
18689 /* Append glyphs strings for glyphs following the last glyph
18690 string tail that are overwritten by tail. The background of
18691 these strings has to be drawn because tail's foreground draws
18692 over it. */
18693 i = right_overwritten (tail);
18694 if (i >= 0)
18695 {
18696 BUILD_GLYPH_STRINGS (end, i, h, t,
18697 DRAW_NORMAL_TEXT, x, last_x);
18698 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18699 append_glyph_string_lists (&head, &tail, h, t);
18700 clip_tail = tail;
18701 }
18702
18703 /* Append glyph strings for glyphs following the last glyph
18704 string tail that overwrite tail. The foreground of such
18705 glyphs has to be drawn because it writes into the background
18706 of tail. The background must not be drawn because it could
18707 paint over the foreground of following glyphs. */
18708 i = right_overwriting (tail);
18709 if (i >= 0)
18710 {
18711 clip_tail = tail;
18712 BUILD_GLYPH_STRINGS (end, i, h, t,
18713 DRAW_NORMAL_TEXT, x, last_x);
18714 for (s = h; s; s = s->next)
18715 s->background_filled_p = 1;
18716 compute_overhangs_and_x (h, tail->x + tail->width, 0);
18717 append_glyph_string_lists (&head, &tail, h, t);
18718 }
18719 if (clip_head || clip_tail)
18720 for (s = head; s; s = s->next)
18721 {
18722 s->clip_head = clip_head;
18723 s->clip_tail = clip_tail;
18724 }
18725 }
18726
18727 /* Draw all strings. */
18728 for (s = head; s; s = s->next)
18729 rif->draw_glyph_string (s);
18730
18731 if (area == TEXT_AREA
18732 && !row->full_width_p
18733 /* When drawing overlapping rows, only the glyph strings'
18734 foreground is drawn, which doesn't erase a cursor
18735 completely. */
18736 && !overlaps_p)
18737 {
18738 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
18739 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
18740 : (tail ? tail->x + tail->background_width : x));
18741
18742 int text_left = window_box_left (w, TEXT_AREA);
18743 x0 -= text_left;
18744 x1 -= text_left;
18745
18746 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
18747 row->y, MATRIX_ROW_BOTTOM_Y (row));
18748 }
18749
18750 /* Value is the x-position up to which drawn, relative to AREA of W.
18751 This doesn't include parts drawn because of overhangs. */
18752 if (row->full_width_p)
18753 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
18754 else
18755 x_reached -= window_box_left (w, area);
18756
18757 RELEASE_HDC (hdc, f);
18758
18759 return x_reached;
18760 }
18761
18762 /* Expand row matrix if too narrow. Don't expand if area
18763 is not present. */
18764
18765 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
18766 { \
18767 if (!fonts_changed_p \
18768 && (it->glyph_row->glyphs[area] \
18769 < it->glyph_row->glyphs[area + 1])) \
18770 { \
18771 it->w->ncols_scale_factor++; \
18772 fonts_changed_p = 1; \
18773 } \
18774 }
18775
18776 /* Store one glyph for IT->char_to_display in IT->glyph_row.
18777 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18778
18779 static INLINE void
18780 append_glyph (it)
18781 struct it *it;
18782 {
18783 struct glyph *glyph;
18784 enum glyph_row_area area = it->area;
18785
18786 xassert (it->glyph_row);
18787 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
18788
18789 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18790 if (glyph < it->glyph_row->glyphs[area + 1])
18791 {
18792 glyph->charpos = CHARPOS (it->position);
18793 glyph->object = it->object;
18794 glyph->pixel_width = it->pixel_width;
18795 glyph->ascent = it->ascent;
18796 glyph->descent = it->descent;
18797 glyph->voffset = it->voffset;
18798 glyph->type = CHAR_GLYPH;
18799 glyph->multibyte_p = it->multibyte_p;
18800 glyph->left_box_line_p = it->start_of_box_run_p;
18801 glyph->right_box_line_p = it->end_of_box_run_p;
18802 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18803 || it->phys_descent > it->descent);
18804 glyph->padding_p = 0;
18805 glyph->glyph_not_available_p = it->glyph_not_available_p;
18806 glyph->face_id = it->face_id;
18807 glyph->u.ch = it->char_to_display;
18808 glyph->slice = null_glyph_slice;
18809 glyph->font_type = FONT_TYPE_UNKNOWN;
18810 ++it->glyph_row->used[area];
18811 }
18812 else
18813 IT_EXPAND_MATRIX_WIDTH (it, area);
18814 }
18815
18816 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
18817 Called from x_produce_glyphs when IT->glyph_row is non-null. */
18818
18819 static INLINE void
18820 append_composite_glyph (it)
18821 struct it *it;
18822 {
18823 struct glyph *glyph;
18824 enum glyph_row_area area = it->area;
18825
18826 xassert (it->glyph_row);
18827
18828 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
18829 if (glyph < it->glyph_row->glyphs[area + 1])
18830 {
18831 glyph->charpos = CHARPOS (it->position);
18832 glyph->object = it->object;
18833 glyph->pixel_width = it->pixel_width;
18834 glyph->ascent = it->ascent;
18835 glyph->descent = it->descent;
18836 glyph->voffset = it->voffset;
18837 glyph->type = COMPOSITE_GLYPH;
18838 glyph->multibyte_p = it->multibyte_p;
18839 glyph->left_box_line_p = it->start_of_box_run_p;
18840 glyph->right_box_line_p = it->end_of_box_run_p;
18841 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
18842 || it->phys_descent > it->descent);
18843 glyph->padding_p = 0;
18844 glyph->glyph_not_available_p = 0;
18845 glyph->face_id = it->face_id;
18846 glyph->u.cmp_id = it->cmp_id;
18847 glyph->slice = null_glyph_slice;
18848 glyph->font_type = FONT_TYPE_UNKNOWN;
18849 ++it->glyph_row->used[area];
18850 }
18851 else
18852 IT_EXPAND_MATRIX_WIDTH (it, area);
18853 }
18854
18855
18856 /* Change IT->ascent and IT->height according to the setting of
18857 IT->voffset. */
18858
18859 static INLINE void
18860 take_vertical_position_into_account (it)
18861 struct it *it;
18862 {
18863 if (it->voffset)
18864 {
18865 if (it->voffset < 0)
18866 /* Increase the ascent so that we can display the text higher
18867 in the line. */
18868 it->ascent -= it->voffset;
18869 else
18870 /* Increase the descent so that we can display the text lower
18871 in the line. */
18872 it->descent += it->voffset;
18873 }
18874 }
18875
18876
18877 /* Produce glyphs/get display metrics for the image IT is loaded with.
18878 See the description of struct display_iterator in dispextern.h for
18879 an overview of struct display_iterator. */
18880
18881 static void
18882 produce_image_glyph (it)
18883 struct it *it;
18884 {
18885 struct image *img;
18886 struct face *face;
18887 int glyph_ascent;
18888 struct glyph_slice slice;
18889
18890 xassert (it->what == IT_IMAGE);
18891
18892 face = FACE_FROM_ID (it->f, it->face_id);
18893 xassert (face);
18894 /* Make sure X resources of the face is loaded. */
18895 PREPARE_FACE_FOR_DISPLAY (it->f, face);
18896
18897 if (it->image_id < 0)
18898 {
18899 /* Fringe bitmap. */
18900 it->ascent = it->phys_ascent = 0;
18901 it->descent = it->phys_descent = 0;
18902 it->pixel_width = 0;
18903 it->nglyphs = 0;
18904 return;
18905 }
18906
18907 img = IMAGE_FROM_ID (it->f, it->image_id);
18908 xassert (img);
18909 /* Make sure X resources of the image is loaded. */
18910 prepare_image_for_display (it->f, img);
18911
18912 slice.x = slice.y = 0;
18913 slice.width = img->width;
18914 slice.height = img->height;
18915
18916 if (INTEGERP (it->slice.x))
18917 slice.x = XINT (it->slice.x);
18918 else if (FLOATP (it->slice.x))
18919 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
18920
18921 if (INTEGERP (it->slice.y))
18922 slice.y = XINT (it->slice.y);
18923 else if (FLOATP (it->slice.y))
18924 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
18925
18926 if (INTEGERP (it->slice.width))
18927 slice.width = XINT (it->slice.width);
18928 else if (FLOATP (it->slice.width))
18929 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
18930
18931 if (INTEGERP (it->slice.height))
18932 slice.height = XINT (it->slice.height);
18933 else if (FLOATP (it->slice.height))
18934 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
18935
18936 if (slice.x >= img->width)
18937 slice.x = img->width;
18938 if (slice.y >= img->height)
18939 slice.y = img->height;
18940 if (slice.x + slice.width >= img->width)
18941 slice.width = img->width - slice.x;
18942 if (slice.y + slice.height > img->height)
18943 slice.height = img->height - slice.y;
18944
18945 if (slice.width == 0 || slice.height == 0)
18946 return;
18947
18948 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
18949
18950 it->descent = slice.height - glyph_ascent;
18951 if (slice.y == 0)
18952 it->descent += img->vmargin;
18953 if (slice.y + slice.height == img->height)
18954 it->descent += img->vmargin;
18955 it->phys_descent = it->descent;
18956
18957 it->pixel_width = slice.width;
18958 if (slice.x == 0)
18959 it->pixel_width += img->hmargin;
18960 if (slice.x + slice.width == img->width)
18961 it->pixel_width += img->hmargin;
18962
18963 /* It's quite possible for images to have an ascent greater than
18964 their height, so don't get confused in that case. */
18965 if (it->descent < 0)
18966 it->descent = 0;
18967
18968 #if 0 /* this breaks image tiling */
18969 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
18970 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
18971 if (face_ascent > it->ascent)
18972 it->ascent = it->phys_ascent = face_ascent;
18973 #endif
18974
18975 it->nglyphs = 1;
18976
18977 if (face->box != FACE_NO_BOX)
18978 {
18979 if (face->box_line_width > 0)
18980 {
18981 if (slice.y == 0)
18982 it->ascent += face->box_line_width;
18983 if (slice.y + slice.height == img->height)
18984 it->descent += face->box_line_width;
18985 }
18986
18987 if (it->start_of_box_run_p && slice.x == 0)
18988 it->pixel_width += abs (face->box_line_width);
18989 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
18990 it->pixel_width += abs (face->box_line_width);
18991 }
18992
18993 take_vertical_position_into_account (it);
18994
18995 if (it->glyph_row)
18996 {
18997 struct glyph *glyph;
18998 enum glyph_row_area area = it->area;
18999
19000 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19001 if (glyph < it->glyph_row->glyphs[area + 1])
19002 {
19003 glyph->charpos = CHARPOS (it->position);
19004 glyph->object = it->object;
19005 glyph->pixel_width = it->pixel_width;
19006 glyph->ascent = glyph_ascent;
19007 glyph->descent = it->descent;
19008 glyph->voffset = it->voffset;
19009 glyph->type = IMAGE_GLYPH;
19010 glyph->multibyte_p = it->multibyte_p;
19011 glyph->left_box_line_p = it->start_of_box_run_p;
19012 glyph->right_box_line_p = it->end_of_box_run_p;
19013 glyph->overlaps_vertically_p = 0;
19014 glyph->padding_p = 0;
19015 glyph->glyph_not_available_p = 0;
19016 glyph->face_id = it->face_id;
19017 glyph->u.img_id = img->id;
19018 glyph->slice = slice;
19019 glyph->font_type = FONT_TYPE_UNKNOWN;
19020 ++it->glyph_row->used[area];
19021 }
19022 else
19023 IT_EXPAND_MATRIX_WIDTH (it, area);
19024 }
19025 }
19026
19027
19028 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19029 of the glyph, WIDTH and HEIGHT are the width and height of the
19030 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19031
19032 static void
19033 append_stretch_glyph (it, object, width, height, ascent)
19034 struct it *it;
19035 Lisp_Object object;
19036 int width, height;
19037 int ascent;
19038 {
19039 struct glyph *glyph;
19040 enum glyph_row_area area = it->area;
19041
19042 xassert (ascent >= 0 && ascent <= height);
19043
19044 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19045 if (glyph < it->glyph_row->glyphs[area + 1])
19046 {
19047 glyph->charpos = CHARPOS (it->position);
19048 glyph->object = object;
19049 glyph->pixel_width = width;
19050 glyph->ascent = ascent;
19051 glyph->descent = height - ascent;
19052 glyph->voffset = it->voffset;
19053 glyph->type = STRETCH_GLYPH;
19054 glyph->multibyte_p = it->multibyte_p;
19055 glyph->left_box_line_p = it->start_of_box_run_p;
19056 glyph->right_box_line_p = it->end_of_box_run_p;
19057 glyph->overlaps_vertically_p = 0;
19058 glyph->padding_p = 0;
19059 glyph->glyph_not_available_p = 0;
19060 glyph->face_id = it->face_id;
19061 glyph->u.stretch.ascent = ascent;
19062 glyph->u.stretch.height = height;
19063 glyph->slice = null_glyph_slice;
19064 glyph->font_type = FONT_TYPE_UNKNOWN;
19065 ++it->glyph_row->used[area];
19066 }
19067 else
19068 IT_EXPAND_MATRIX_WIDTH (it, area);
19069 }
19070
19071
19072 /* Produce a stretch glyph for iterator IT. IT->object is the value
19073 of the glyph property displayed. The value must be a list
19074 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19075 being recognized:
19076
19077 1. `:width WIDTH' specifies that the space should be WIDTH *
19078 canonical char width wide. WIDTH may be an integer or floating
19079 point number.
19080
19081 2. `:relative-width FACTOR' specifies that the width of the stretch
19082 should be computed from the width of the first character having the
19083 `glyph' property, and should be FACTOR times that width.
19084
19085 3. `:align-to HPOS' specifies that the space should be wide enough
19086 to reach HPOS, a value in canonical character units.
19087
19088 Exactly one of the above pairs must be present.
19089
19090 4. `:height HEIGHT' specifies that the height of the stretch produced
19091 should be HEIGHT, measured in canonical character units.
19092
19093 5. `:relative-height FACTOR' specifies that the height of the
19094 stretch should be FACTOR times the height of the characters having
19095 the glyph property.
19096
19097 Either none or exactly one of 4 or 5 must be present.
19098
19099 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19100 of the stretch should be used for the ascent of the stretch.
19101 ASCENT must be in the range 0 <= ASCENT <= 100. */
19102
19103 static void
19104 produce_stretch_glyph (it)
19105 struct it *it;
19106 {
19107 /* (space :width WIDTH :height HEIGHT ...) */
19108 Lisp_Object prop, plist;
19109 int width = 0, height = 0, align_to = -1;
19110 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19111 int ascent = 0;
19112 double tem;
19113 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19114 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19115
19116 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19117
19118 /* List should start with `space'. */
19119 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19120 plist = XCDR (it->object);
19121
19122 /* Compute the width of the stretch. */
19123 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19124 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19125 {
19126 /* Absolute width `:width WIDTH' specified and valid. */
19127 zero_width_ok_p = 1;
19128 width = (int)tem;
19129 }
19130 else if (prop = Fplist_get (plist, QCrelative_width),
19131 NUMVAL (prop) > 0)
19132 {
19133 /* Relative width `:relative-width FACTOR' specified and valid.
19134 Compute the width of the characters having the `glyph'
19135 property. */
19136 struct it it2;
19137 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
19138
19139 it2 = *it;
19140 if (it->multibyte_p)
19141 {
19142 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
19143 - IT_BYTEPOS (*it));
19144 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
19145 }
19146 else
19147 it2.c = *p, it2.len = 1;
19148
19149 it2.glyph_row = NULL;
19150 it2.what = IT_CHARACTER;
19151 x_produce_glyphs (&it2);
19152 width = NUMVAL (prop) * it2.pixel_width;
19153 }
19154 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
19155 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
19156 {
19157 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
19158 align_to = (align_to < 0
19159 ? 0
19160 : align_to - window_box_left_offset (it->w, TEXT_AREA));
19161 else if (align_to < 0)
19162 align_to = window_box_left_offset (it->w, TEXT_AREA);
19163 width = max (0, (int)tem + align_to - it->current_x);
19164 zero_width_ok_p = 1;
19165 }
19166 else
19167 /* Nothing specified -> width defaults to canonical char width. */
19168 width = FRAME_COLUMN_WIDTH (it->f);
19169
19170 if (width <= 0 && (width < 0 || !zero_width_ok_p))
19171 width = 1;
19172
19173 /* Compute height. */
19174 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
19175 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19176 {
19177 height = (int)tem;
19178 zero_height_ok_p = 1;
19179 }
19180 else if (prop = Fplist_get (plist, QCrelative_height),
19181 NUMVAL (prop) > 0)
19182 height = FONT_HEIGHT (font) * NUMVAL (prop);
19183 else
19184 height = FONT_HEIGHT (font);
19185
19186 if (height <= 0 && (height < 0 || !zero_height_ok_p))
19187 height = 1;
19188
19189 /* Compute percentage of height used for ascent. If
19190 `:ascent ASCENT' is present and valid, use that. Otherwise,
19191 derive the ascent from the font in use. */
19192 if (prop = Fplist_get (plist, QCascent),
19193 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
19194 ascent = height * NUMVAL (prop) / 100.0;
19195 else if (!NILP (prop)
19196 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
19197 ascent = min (max (0, (int)tem), height);
19198 else
19199 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
19200
19201 if (width > 0 && height > 0 && it->glyph_row)
19202 {
19203 Lisp_Object object = it->stack[it->sp - 1].string;
19204 if (!STRINGP (object))
19205 object = it->w->buffer;
19206 append_stretch_glyph (it, object, width, height, ascent);
19207 }
19208
19209 it->pixel_width = width;
19210 it->ascent = it->phys_ascent = ascent;
19211 it->descent = it->phys_descent = height - it->ascent;
19212 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
19213
19214 if (width > 0 && height > 0 && face->box != FACE_NO_BOX)
19215 {
19216 if (face->box_line_width > 0)
19217 {
19218 it->ascent += face->box_line_width;
19219 it->descent += face->box_line_width;
19220 }
19221
19222 if (it->start_of_box_run_p)
19223 it->pixel_width += abs (face->box_line_width);
19224 if (it->end_of_box_run_p)
19225 it->pixel_width += abs (face->box_line_width);
19226 }
19227
19228 take_vertical_position_into_account (it);
19229 }
19230
19231 /* Get line-height and line-spacing property at point.
19232 If line-height has format (HEIGHT TOTAL), return TOTAL
19233 in TOTAL_HEIGHT. */
19234
19235 static Lisp_Object
19236 get_line_height_property (it, prop)
19237 struct it *it;
19238 Lisp_Object prop;
19239 {
19240 Lisp_Object position;
19241
19242 if (STRINGP (it->object))
19243 position = make_number (IT_STRING_CHARPOS (*it));
19244 else if (BUFFERP (it->object))
19245 position = make_number (IT_CHARPOS (*it));
19246 else
19247 return Qnil;
19248
19249 return Fget_char_property (position, prop, it->object);
19250 }
19251
19252 /* Calculate line-height and line-spacing properties.
19253 An integer value specifies explicit pixel value.
19254 A float value specifies relative value to current face height.
19255 A cons (float . face-name) specifies relative value to
19256 height of specified face font.
19257
19258 Returns height in pixels, or nil. */
19259
19260
19261 static Lisp_Object
19262 calc_line_height_property (it, val, font, boff, override)
19263 struct it *it;
19264 Lisp_Object val;
19265 XFontStruct *font;
19266 int boff, override;
19267 {
19268 Lisp_Object face_name = Qnil;
19269 int ascent, descent, height;
19270
19271 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
19272 return val;
19273
19274 if (CONSP (val))
19275 {
19276 face_name = XCAR (val);
19277 val = XCDR (val);
19278 if (!NUMBERP (val))
19279 val = make_number (1);
19280 if (NILP (face_name))
19281 {
19282 height = it->ascent + it->descent;
19283 goto scale;
19284 }
19285 }
19286
19287 if (NILP (face_name))
19288 {
19289 font = FRAME_FONT (it->f);
19290 boff = FRAME_BASELINE_OFFSET (it->f);
19291 }
19292 else if (EQ (face_name, Qt))
19293 {
19294 override = 0;
19295 }
19296 else
19297 {
19298 int face_id;
19299 struct face *face;
19300 struct font_info *font_info;
19301
19302 face_id = lookup_named_face (it->f, face_name, ' ', 0);
19303 if (face_id < 0)
19304 return make_number (-1);
19305
19306 face = FACE_FROM_ID (it->f, face_id);
19307 font = face->font;
19308 if (font == NULL)
19309 return make_number (-1);
19310
19311 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19312 boff = font_info->baseline_offset;
19313 if (font_info->vertical_centering)
19314 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19315 }
19316
19317 ascent = FONT_BASE (font) + boff;
19318 descent = FONT_DESCENT (font) - boff;
19319
19320 if (override)
19321 {
19322 it->override_ascent = ascent;
19323 it->override_descent = descent;
19324 it->override_boff = boff;
19325 }
19326
19327 height = ascent + descent;
19328
19329 scale:
19330 if (FLOATP (val))
19331 height = (int)(XFLOAT_DATA (val) * height);
19332 else if (INTEGERP (val))
19333 height *= XINT (val);
19334
19335 return make_number (height);
19336 }
19337
19338
19339 /* RIF:
19340 Produce glyphs/get display metrics for the display element IT is
19341 loaded with. See the description of struct display_iterator in
19342 dispextern.h for an overview of struct display_iterator. */
19343
19344 void
19345 x_produce_glyphs (it)
19346 struct it *it;
19347 {
19348 int extra_line_spacing = it->extra_line_spacing;
19349
19350 it->glyph_not_available_p = 0;
19351
19352 if (it->what == IT_CHARACTER)
19353 {
19354 XChar2b char2b;
19355 XFontStruct *font;
19356 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19357 XCharStruct *pcm;
19358 int font_not_found_p;
19359 struct font_info *font_info;
19360 int boff; /* baseline offset */
19361 /* We may change it->multibyte_p upon unibyte<->multibyte
19362 conversion. So, save the current value now and restore it
19363 later.
19364
19365 Note: It seems that we don't have to record multibyte_p in
19366 struct glyph because the character code itself tells if or
19367 not the character is multibyte. Thus, in the future, we must
19368 consider eliminating the field `multibyte_p' in the struct
19369 glyph. */
19370 int saved_multibyte_p = it->multibyte_p;
19371
19372 /* Maybe translate single-byte characters to multibyte, or the
19373 other way. */
19374 it->char_to_display = it->c;
19375 if (!ASCII_BYTE_P (it->c))
19376 {
19377 if (unibyte_display_via_language_environment
19378 && SINGLE_BYTE_CHAR_P (it->c)
19379 && (it->c >= 0240
19380 || !NILP (Vnonascii_translation_table)))
19381 {
19382 it->char_to_display = unibyte_char_to_multibyte (it->c);
19383 it->multibyte_p = 1;
19384 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19385 face = FACE_FROM_ID (it->f, it->face_id);
19386 }
19387 else if (!SINGLE_BYTE_CHAR_P (it->c)
19388 && !it->multibyte_p)
19389 {
19390 it->multibyte_p = 1;
19391 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19392 face = FACE_FROM_ID (it->f, it->face_id);
19393 }
19394 }
19395
19396 /* Get font to use. Encode IT->char_to_display. */
19397 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19398 &char2b, it->multibyte_p, 0);
19399 font = face->font;
19400
19401 /* When no suitable font found, use the default font. */
19402 font_not_found_p = font == NULL;
19403 if (font_not_found_p)
19404 {
19405 font = FRAME_FONT (it->f);
19406 boff = FRAME_BASELINE_OFFSET (it->f);
19407 font_info = NULL;
19408 }
19409 else
19410 {
19411 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19412 boff = font_info->baseline_offset;
19413 if (font_info->vertical_centering)
19414 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19415 }
19416
19417 if (it->char_to_display >= ' '
19418 && (!it->multibyte_p || it->char_to_display < 128))
19419 {
19420 /* Either unibyte or ASCII. */
19421 int stretched_p;
19422
19423 it->nglyphs = 1;
19424
19425 pcm = rif->per_char_metric (font, &char2b,
19426 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
19427
19428 if (it->override_ascent >= 0)
19429 {
19430 it->ascent = it->override_ascent;
19431 it->descent = it->override_descent;
19432 boff = it->override_boff;
19433 }
19434 else
19435 {
19436 it->ascent = FONT_BASE (font) + boff;
19437 it->descent = FONT_DESCENT (font) - boff;
19438 }
19439
19440 if (pcm)
19441 {
19442 it->phys_ascent = pcm->ascent + boff;
19443 it->phys_descent = pcm->descent - boff;
19444 it->pixel_width = pcm->width;
19445 }
19446 else
19447 {
19448 it->glyph_not_available_p = 1;
19449 it->phys_ascent = it->ascent;
19450 it->phys_descent = it->descent;
19451 it->pixel_width = FONT_WIDTH (font);
19452 }
19453
19454 if (it->constrain_row_ascent_descent_p)
19455 {
19456 if (it->descent > it->max_descent)
19457 {
19458 it->ascent += it->descent - it->max_descent;
19459 it->descent = it->max_descent;
19460 }
19461 if (it->ascent > it->max_ascent)
19462 {
19463 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19464 it->ascent = it->max_ascent;
19465 }
19466 it->phys_ascent = min (it->phys_ascent, it->ascent);
19467 it->phys_descent = min (it->phys_descent, it->descent);
19468 extra_line_spacing = 0;
19469 }
19470
19471 /* If this is a space inside a region of text with
19472 `space-width' property, change its width. */
19473 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
19474 if (stretched_p)
19475 it->pixel_width *= XFLOATINT (it->space_width);
19476
19477 /* If face has a box, add the box thickness to the character
19478 height. If character has a box line to the left and/or
19479 right, add the box line width to the character's width. */
19480 if (face->box != FACE_NO_BOX)
19481 {
19482 int thick = face->box_line_width;
19483
19484 if (thick > 0)
19485 {
19486 it->ascent += thick;
19487 it->descent += thick;
19488 }
19489 else
19490 thick = -thick;
19491
19492 if (it->start_of_box_run_p)
19493 it->pixel_width += thick;
19494 if (it->end_of_box_run_p)
19495 it->pixel_width += thick;
19496 }
19497
19498 /* If face has an overline, add the height of the overline
19499 (1 pixel) and a 1 pixel margin to the character height. */
19500 if (face->overline_p)
19501 it->ascent += 2;
19502
19503 if (it->constrain_row_ascent_descent_p)
19504 {
19505 if (it->ascent > it->max_ascent)
19506 it->ascent = it->max_ascent;
19507 if (it->descent > it->max_descent)
19508 it->descent = it->max_descent;
19509 }
19510
19511 take_vertical_position_into_account (it);
19512
19513 /* If we have to actually produce glyphs, do it. */
19514 if (it->glyph_row)
19515 {
19516 if (stretched_p)
19517 {
19518 /* Translate a space with a `space-width' property
19519 into a stretch glyph. */
19520 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
19521 / FONT_HEIGHT (font));
19522 append_stretch_glyph (it, it->object, it->pixel_width,
19523 it->ascent + it->descent, ascent);
19524 }
19525 else
19526 append_glyph (it);
19527
19528 /* If characters with lbearing or rbearing are displayed
19529 in this line, record that fact in a flag of the
19530 glyph row. This is used to optimize X output code. */
19531 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
19532 it->glyph_row->contains_overlapping_glyphs_p = 1;
19533 }
19534 }
19535 else if (it->char_to_display == '\n')
19536 {
19537 /* A newline has no width but we need the height of the line.
19538 But if previous part of the line set a height, don't
19539 increase that height */
19540
19541 Lisp_Object height;
19542 Lisp_Object total_height = Qnil;
19543
19544 it->override_ascent = -1;
19545 it->pixel_width = 0;
19546 it->nglyphs = 0;
19547
19548 height = get_line_height_property(it, Qline_height);
19549 /* Split (line-height total-height) list */
19550 if (CONSP (height)
19551 && CONSP (XCDR (height))
19552 && NILP (XCDR (XCDR (height))))
19553 {
19554 total_height = XCAR (XCDR (height));
19555 height = XCAR (height);
19556 }
19557 height = calc_line_height_property(it, height, font, boff, 1);
19558
19559 if (it->override_ascent >= 0)
19560 {
19561 it->ascent = it->override_ascent;
19562 it->descent = it->override_descent;
19563 boff = it->override_boff;
19564 }
19565 else
19566 {
19567 it->ascent = FONT_BASE (font) + boff;
19568 it->descent = FONT_DESCENT (font) - boff;
19569 }
19570
19571 if (EQ (height, Qt))
19572 {
19573 if (it->descent > it->max_descent)
19574 {
19575 it->ascent += it->descent - it->max_descent;
19576 it->descent = it->max_descent;
19577 }
19578 if (it->ascent > it->max_ascent)
19579 {
19580 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
19581 it->ascent = it->max_ascent;
19582 }
19583 it->phys_ascent = min (it->phys_ascent, it->ascent);
19584 it->phys_descent = min (it->phys_descent, it->descent);
19585 it->constrain_row_ascent_descent_p = 1;
19586 extra_line_spacing = 0;
19587 }
19588 else
19589 {
19590 Lisp_Object spacing;
19591
19592 it->phys_ascent = it->ascent;
19593 it->phys_descent = it->descent;
19594
19595 if ((it->max_ascent > 0 || it->max_descent > 0)
19596 && face->box != FACE_NO_BOX
19597 && face->box_line_width > 0)
19598 {
19599 it->ascent += face->box_line_width;
19600 it->descent += face->box_line_width;
19601 }
19602 if (!NILP (height)
19603 && XINT (height) > it->ascent + it->descent)
19604 it->ascent = XINT (height) - it->descent;
19605
19606 if (!NILP (total_height))
19607 spacing = calc_line_height_property(it, total_height, font, boff, 0);
19608 else
19609 {
19610 spacing = get_line_height_property(it, Qline_spacing);
19611 spacing = calc_line_height_property(it, spacing, font, boff, 0);
19612 }
19613 if (INTEGERP (spacing))
19614 {
19615 extra_line_spacing = XINT (spacing);
19616 if (!NILP (total_height))
19617 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
19618 }
19619 }
19620 }
19621 else if (it->char_to_display == '\t')
19622 {
19623 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
19624 int x = it->current_x + it->continuation_lines_width;
19625 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
19626
19627 /* If the distance from the current position to the next tab
19628 stop is less than a space character width, use the
19629 tab stop after that. */
19630 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
19631 next_tab_x += tab_width;
19632
19633 it->pixel_width = next_tab_x - x;
19634 it->nglyphs = 1;
19635 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
19636 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
19637
19638 if (it->glyph_row)
19639 {
19640 append_stretch_glyph (it, it->object, it->pixel_width,
19641 it->ascent + it->descent, it->ascent);
19642 }
19643 }
19644 else
19645 {
19646 /* A multi-byte character. Assume that the display width of the
19647 character is the width of the character multiplied by the
19648 width of the font. */
19649
19650 /* If we found a font, this font should give us the right
19651 metrics. If we didn't find a font, use the frame's
19652 default font and calculate the width of the character
19653 from the charset width; this is what old redisplay code
19654 did. */
19655
19656 pcm = rif->per_char_metric (font, &char2b,
19657 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
19658
19659 if (font_not_found_p || !pcm)
19660 {
19661 int charset = CHAR_CHARSET (it->char_to_display);
19662
19663 it->glyph_not_available_p = 1;
19664 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
19665 * CHARSET_WIDTH (charset));
19666 it->phys_ascent = FONT_BASE (font) + boff;
19667 it->phys_descent = FONT_DESCENT (font) - boff;
19668 }
19669 else
19670 {
19671 it->pixel_width = pcm->width;
19672 it->phys_ascent = pcm->ascent + boff;
19673 it->phys_descent = pcm->descent - boff;
19674 if (it->glyph_row
19675 && (pcm->lbearing < 0
19676 || pcm->rbearing > pcm->width))
19677 it->glyph_row->contains_overlapping_glyphs_p = 1;
19678 }
19679 it->nglyphs = 1;
19680 it->ascent = FONT_BASE (font) + boff;
19681 it->descent = FONT_DESCENT (font) - boff;
19682 if (face->box != FACE_NO_BOX)
19683 {
19684 int thick = face->box_line_width;
19685
19686 if (thick > 0)
19687 {
19688 it->ascent += thick;
19689 it->descent += thick;
19690 }
19691 else
19692 thick = - thick;
19693
19694 if (it->start_of_box_run_p)
19695 it->pixel_width += thick;
19696 if (it->end_of_box_run_p)
19697 it->pixel_width += thick;
19698 }
19699
19700 /* If face has an overline, add the height of the overline
19701 (1 pixel) and a 1 pixel margin to the character height. */
19702 if (face->overline_p)
19703 it->ascent += 2;
19704
19705 take_vertical_position_into_account (it);
19706
19707 if (it->glyph_row)
19708 append_glyph (it);
19709 }
19710 it->multibyte_p = saved_multibyte_p;
19711 }
19712 else if (it->what == IT_COMPOSITION)
19713 {
19714 /* Note: A composition is represented as one glyph in the
19715 glyph matrix. There are no padding glyphs. */
19716 XChar2b char2b;
19717 XFontStruct *font;
19718 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19719 XCharStruct *pcm;
19720 int font_not_found_p;
19721 struct font_info *font_info;
19722 int boff; /* baseline offset */
19723 struct composition *cmp = composition_table[it->cmp_id];
19724
19725 /* Maybe translate single-byte characters to multibyte. */
19726 it->char_to_display = it->c;
19727 if (unibyte_display_via_language_environment
19728 && SINGLE_BYTE_CHAR_P (it->c)
19729 && (it->c >= 0240
19730 || (it->c >= 0200
19731 && !NILP (Vnonascii_translation_table))))
19732 {
19733 it->char_to_display = unibyte_char_to_multibyte (it->c);
19734 }
19735
19736 /* Get face and font to use. Encode IT->char_to_display. */
19737 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
19738 face = FACE_FROM_ID (it->f, it->face_id);
19739 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
19740 &char2b, it->multibyte_p, 0);
19741 font = face->font;
19742
19743 /* When no suitable font found, use the default font. */
19744 font_not_found_p = font == NULL;
19745 if (font_not_found_p)
19746 {
19747 font = FRAME_FONT (it->f);
19748 boff = FRAME_BASELINE_OFFSET (it->f);
19749 font_info = NULL;
19750 }
19751 else
19752 {
19753 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19754 boff = font_info->baseline_offset;
19755 if (font_info->vertical_centering)
19756 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19757 }
19758
19759 /* There are no padding glyphs, so there is only one glyph to
19760 produce for the composition. Important is that pixel_width,
19761 ascent and descent are the values of what is drawn by
19762 draw_glyphs (i.e. the values of the overall glyphs composed). */
19763 it->nglyphs = 1;
19764
19765 /* If we have not yet calculated pixel size data of glyphs of
19766 the composition for the current face font, calculate them
19767 now. Theoretically, we have to check all fonts for the
19768 glyphs, but that requires much time and memory space. So,
19769 here we check only the font of the first glyph. This leads
19770 to incorrect display very rarely, and C-l (recenter) can
19771 correct the display anyway. */
19772 if (cmp->font != (void *) font)
19773 {
19774 /* Ascent and descent of the font of the first character of
19775 this composition (adjusted by baseline offset). Ascent
19776 and descent of overall glyphs should not be less than
19777 them respectively. */
19778 int font_ascent = FONT_BASE (font) + boff;
19779 int font_descent = FONT_DESCENT (font) - boff;
19780 /* Bounding box of the overall glyphs. */
19781 int leftmost, rightmost, lowest, highest;
19782 int i, width, ascent, descent;
19783
19784 cmp->font = (void *) font;
19785
19786 /* Initialize the bounding box. */
19787 if (font_info
19788 && (pcm = rif->per_char_metric (font, &char2b,
19789 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
19790 {
19791 width = pcm->width;
19792 ascent = pcm->ascent;
19793 descent = pcm->descent;
19794 }
19795 else
19796 {
19797 width = FONT_WIDTH (font);
19798 ascent = FONT_BASE (font);
19799 descent = FONT_DESCENT (font);
19800 }
19801
19802 rightmost = width;
19803 lowest = - descent + boff;
19804 highest = ascent + boff;
19805 leftmost = 0;
19806
19807 if (font_info
19808 && font_info->default_ascent
19809 && CHAR_TABLE_P (Vuse_default_ascent)
19810 && !NILP (Faref (Vuse_default_ascent,
19811 make_number (it->char_to_display))))
19812 highest = font_info->default_ascent + boff;
19813
19814 /* Draw the first glyph at the normal position. It may be
19815 shifted to right later if some other glyphs are drawn at
19816 the left. */
19817 cmp->offsets[0] = 0;
19818 cmp->offsets[1] = boff;
19819
19820 /* Set cmp->offsets for the remaining glyphs. */
19821 for (i = 1; i < cmp->glyph_len; i++)
19822 {
19823 int left, right, btm, top;
19824 int ch = COMPOSITION_GLYPH (cmp, i);
19825 int face_id = FACE_FOR_CHAR (it->f, face, ch);
19826
19827 face = FACE_FROM_ID (it->f, face_id);
19828 get_char_face_and_encoding (it->f, ch, face->id,
19829 &char2b, it->multibyte_p, 0);
19830 font = face->font;
19831 if (font == NULL)
19832 {
19833 font = FRAME_FONT (it->f);
19834 boff = FRAME_BASELINE_OFFSET (it->f);
19835 font_info = NULL;
19836 }
19837 else
19838 {
19839 font_info
19840 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
19841 boff = font_info->baseline_offset;
19842 if (font_info->vertical_centering)
19843 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
19844 }
19845
19846 if (font_info
19847 && (pcm = rif->per_char_metric (font, &char2b,
19848 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
19849 {
19850 width = pcm->width;
19851 ascent = pcm->ascent;
19852 descent = pcm->descent;
19853 }
19854 else
19855 {
19856 width = FONT_WIDTH (font);
19857 ascent = 1;
19858 descent = 0;
19859 }
19860
19861 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
19862 {
19863 /* Relative composition with or without
19864 alternate chars. */
19865 left = (leftmost + rightmost - width) / 2;
19866 btm = - descent + boff;
19867 if (font_info && font_info->relative_compose
19868 && (! CHAR_TABLE_P (Vignore_relative_composition)
19869 || NILP (Faref (Vignore_relative_composition,
19870 make_number (ch)))))
19871 {
19872
19873 if (- descent >= font_info->relative_compose)
19874 /* One extra pixel between two glyphs. */
19875 btm = highest + 1;
19876 else if (ascent <= 0)
19877 /* One extra pixel between two glyphs. */
19878 btm = lowest - 1 - ascent - descent;
19879 }
19880 }
19881 else
19882 {
19883 /* A composition rule is specified by an integer
19884 value that encodes global and new reference
19885 points (GREF and NREF). GREF and NREF are
19886 specified by numbers as below:
19887
19888 0---1---2 -- ascent
19889 | |
19890 | |
19891 | |
19892 9--10--11 -- center
19893 | |
19894 ---3---4---5--- baseline
19895 | |
19896 6---7---8 -- descent
19897 */
19898 int rule = COMPOSITION_RULE (cmp, i);
19899 int gref, nref, grefx, grefy, nrefx, nrefy;
19900
19901 COMPOSITION_DECODE_RULE (rule, gref, nref);
19902 grefx = gref % 3, nrefx = nref % 3;
19903 grefy = gref / 3, nrefy = nref / 3;
19904
19905 left = (leftmost
19906 + grefx * (rightmost - leftmost) / 2
19907 - nrefx * width / 2);
19908 btm = ((grefy == 0 ? highest
19909 : grefy == 1 ? 0
19910 : grefy == 2 ? lowest
19911 : (highest + lowest) / 2)
19912 - (nrefy == 0 ? ascent + descent
19913 : nrefy == 1 ? descent - boff
19914 : nrefy == 2 ? 0
19915 : (ascent + descent) / 2));
19916 }
19917
19918 cmp->offsets[i * 2] = left;
19919 cmp->offsets[i * 2 + 1] = btm + descent;
19920
19921 /* Update the bounding box of the overall glyphs. */
19922 right = left + width;
19923 top = btm + descent + ascent;
19924 if (left < leftmost)
19925 leftmost = left;
19926 if (right > rightmost)
19927 rightmost = right;
19928 if (top > highest)
19929 highest = top;
19930 if (btm < lowest)
19931 lowest = btm;
19932 }
19933
19934 /* If there are glyphs whose x-offsets are negative,
19935 shift all glyphs to the right and make all x-offsets
19936 non-negative. */
19937 if (leftmost < 0)
19938 {
19939 for (i = 0; i < cmp->glyph_len; i++)
19940 cmp->offsets[i * 2] -= leftmost;
19941 rightmost -= leftmost;
19942 }
19943
19944 cmp->pixel_width = rightmost;
19945 cmp->ascent = highest;
19946 cmp->descent = - lowest;
19947 if (cmp->ascent < font_ascent)
19948 cmp->ascent = font_ascent;
19949 if (cmp->descent < font_descent)
19950 cmp->descent = font_descent;
19951 }
19952
19953 it->pixel_width = cmp->pixel_width;
19954 it->ascent = it->phys_ascent = cmp->ascent;
19955 it->descent = it->phys_descent = cmp->descent;
19956
19957 if (face->box != FACE_NO_BOX)
19958 {
19959 int thick = face->box_line_width;
19960
19961 if (thick > 0)
19962 {
19963 it->ascent += thick;
19964 it->descent += thick;
19965 }
19966 else
19967 thick = - thick;
19968
19969 if (it->start_of_box_run_p)
19970 it->pixel_width += thick;
19971 if (it->end_of_box_run_p)
19972 it->pixel_width += thick;
19973 }
19974
19975 /* If face has an overline, add the height of the overline
19976 (1 pixel) and a 1 pixel margin to the character height. */
19977 if (face->overline_p)
19978 it->ascent += 2;
19979
19980 take_vertical_position_into_account (it);
19981
19982 if (it->glyph_row)
19983 append_composite_glyph (it);
19984 }
19985 else if (it->what == IT_IMAGE)
19986 produce_image_glyph (it);
19987 else if (it->what == IT_STRETCH)
19988 produce_stretch_glyph (it);
19989
19990 /* Accumulate dimensions. Note: can't assume that it->descent > 0
19991 because this isn't true for images with `:ascent 100'. */
19992 xassert (it->ascent >= 0 && it->descent >= 0);
19993 if (it->area == TEXT_AREA)
19994 it->current_x += it->pixel_width;
19995
19996 if (extra_line_spacing > 0)
19997 {
19998 it->descent += extra_line_spacing;
19999 if (extra_line_spacing > it->max_extra_line_spacing)
20000 it->max_extra_line_spacing = extra_line_spacing;
20001 }
20002
20003 it->max_ascent = max (it->max_ascent, it->ascent);
20004 it->max_descent = max (it->max_descent, it->descent);
20005 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20006 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20007 }
20008
20009 /* EXPORT for RIF:
20010 Output LEN glyphs starting at START at the nominal cursor position.
20011 Advance the nominal cursor over the text. The global variable
20012 updated_window contains the window being updated, updated_row is
20013 the glyph row being updated, and updated_area is the area of that
20014 row being updated. */
20015
20016 void
20017 x_write_glyphs (start, len)
20018 struct glyph *start;
20019 int len;
20020 {
20021 int x, hpos;
20022
20023 xassert (updated_window && updated_row);
20024 BLOCK_INPUT;
20025
20026 /* Write glyphs. */
20027
20028 hpos = start - updated_row->glyphs[updated_area];
20029 x = draw_glyphs (updated_window, output_cursor.x,
20030 updated_row, updated_area,
20031 hpos, hpos + len,
20032 DRAW_NORMAL_TEXT, 0);
20033
20034 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20035 if (updated_area == TEXT_AREA
20036 && updated_window->phys_cursor_on_p
20037 && updated_window->phys_cursor.vpos == output_cursor.vpos
20038 && updated_window->phys_cursor.hpos >= hpos
20039 && updated_window->phys_cursor.hpos < hpos + len)
20040 updated_window->phys_cursor_on_p = 0;
20041
20042 UNBLOCK_INPUT;
20043
20044 /* Advance the output cursor. */
20045 output_cursor.hpos += len;
20046 output_cursor.x = x;
20047 }
20048
20049
20050 /* EXPORT for RIF:
20051 Insert LEN glyphs from START at the nominal cursor position. */
20052
20053 void
20054 x_insert_glyphs (start, len)
20055 struct glyph *start;
20056 int len;
20057 {
20058 struct frame *f;
20059 struct window *w;
20060 int line_height, shift_by_width, shifted_region_width;
20061 struct glyph_row *row;
20062 struct glyph *glyph;
20063 int frame_x, frame_y, hpos;
20064
20065 xassert (updated_window && updated_row);
20066 BLOCK_INPUT;
20067 w = updated_window;
20068 f = XFRAME (WINDOW_FRAME (w));
20069
20070 /* Get the height of the line we are in. */
20071 row = updated_row;
20072 line_height = row->height;
20073
20074 /* Get the width of the glyphs to insert. */
20075 shift_by_width = 0;
20076 for (glyph = start; glyph < start + len; ++glyph)
20077 shift_by_width += glyph->pixel_width;
20078
20079 /* Get the width of the region to shift right. */
20080 shifted_region_width = (window_box_width (w, updated_area)
20081 - output_cursor.x
20082 - shift_by_width);
20083
20084 /* Shift right. */
20085 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20086 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20087
20088 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20089 line_height, shift_by_width);
20090
20091 /* Write the glyphs. */
20092 hpos = start - row->glyphs[updated_area];
20093 draw_glyphs (w, output_cursor.x, row, updated_area,
20094 hpos, hpos + len,
20095 DRAW_NORMAL_TEXT, 0);
20096
20097 /* Advance the output cursor. */
20098 output_cursor.hpos += len;
20099 output_cursor.x += shift_by_width;
20100 UNBLOCK_INPUT;
20101 }
20102
20103
20104 /* EXPORT for RIF:
20105 Erase the current text line from the nominal cursor position
20106 (inclusive) to pixel column TO_X (exclusive). The idea is that
20107 everything from TO_X onward is already erased.
20108
20109 TO_X is a pixel position relative to updated_area of
20110 updated_window. TO_X == -1 means clear to the end of this area. */
20111
20112 void
20113 x_clear_end_of_line (to_x)
20114 int to_x;
20115 {
20116 struct frame *f;
20117 struct window *w = updated_window;
20118 int max_x, min_y, max_y;
20119 int from_x, from_y, to_y;
20120
20121 xassert (updated_window && updated_row);
20122 f = XFRAME (w->frame);
20123
20124 if (updated_row->full_width_p)
20125 max_x = WINDOW_TOTAL_WIDTH (w);
20126 else
20127 max_x = window_box_width (w, updated_area);
20128 max_y = window_text_bottom_y (w);
20129
20130 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20131 of window. For TO_X > 0, truncate to end of drawing area. */
20132 if (to_x == 0)
20133 return;
20134 else if (to_x < 0)
20135 to_x = max_x;
20136 else
20137 to_x = min (to_x, max_x);
20138
20139 to_y = min (max_y, output_cursor.y + updated_row->height);
20140
20141 /* Notice if the cursor will be cleared by this operation. */
20142 if (!updated_row->full_width_p)
20143 notice_overwritten_cursor (w, updated_area,
20144 output_cursor.x, -1,
20145 updated_row->y,
20146 MATRIX_ROW_BOTTOM_Y (updated_row));
20147
20148 from_x = output_cursor.x;
20149
20150 /* Translate to frame coordinates. */
20151 if (updated_row->full_width_p)
20152 {
20153 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
20154 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
20155 }
20156 else
20157 {
20158 int area_left = window_box_left (w, updated_area);
20159 from_x += area_left;
20160 to_x += area_left;
20161 }
20162
20163 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
20164 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
20165 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
20166
20167 /* Prevent inadvertently clearing to end of the X window. */
20168 if (to_x > from_x && to_y > from_y)
20169 {
20170 BLOCK_INPUT;
20171 rif->clear_frame_area (f, from_x, from_y,
20172 to_x - from_x, to_y - from_y);
20173 UNBLOCK_INPUT;
20174 }
20175 }
20176
20177 #endif /* HAVE_WINDOW_SYSTEM */
20178
20179
20180 \f
20181 /***********************************************************************
20182 Cursor types
20183 ***********************************************************************/
20184
20185 /* Value is the internal representation of the specified cursor type
20186 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
20187 of the bar cursor. */
20188
20189 static enum text_cursor_kinds
20190 get_specified_cursor_type (arg, width)
20191 Lisp_Object arg;
20192 int *width;
20193 {
20194 enum text_cursor_kinds type;
20195
20196 if (NILP (arg))
20197 return NO_CURSOR;
20198
20199 if (EQ (arg, Qbox))
20200 return FILLED_BOX_CURSOR;
20201
20202 if (EQ (arg, Qhollow))
20203 return HOLLOW_BOX_CURSOR;
20204
20205 if (EQ (arg, Qbar))
20206 {
20207 *width = 2;
20208 return BAR_CURSOR;
20209 }
20210
20211 if (CONSP (arg)
20212 && EQ (XCAR (arg), Qbar)
20213 && INTEGERP (XCDR (arg))
20214 && XINT (XCDR (arg)) >= 0)
20215 {
20216 *width = XINT (XCDR (arg));
20217 return BAR_CURSOR;
20218 }
20219
20220 if (EQ (arg, Qhbar))
20221 {
20222 *width = 2;
20223 return HBAR_CURSOR;
20224 }
20225
20226 if (CONSP (arg)
20227 && EQ (XCAR (arg), Qhbar)
20228 && INTEGERP (XCDR (arg))
20229 && XINT (XCDR (arg)) >= 0)
20230 {
20231 *width = XINT (XCDR (arg));
20232 return HBAR_CURSOR;
20233 }
20234
20235 /* Treat anything unknown as "hollow box cursor".
20236 It was bad to signal an error; people have trouble fixing
20237 .Xdefaults with Emacs, when it has something bad in it. */
20238 type = HOLLOW_BOX_CURSOR;
20239
20240 return type;
20241 }
20242
20243 /* Set the default cursor types for specified frame. */
20244 void
20245 set_frame_cursor_types (f, arg)
20246 struct frame *f;
20247 Lisp_Object arg;
20248 {
20249 int width;
20250 Lisp_Object tem;
20251
20252 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
20253 FRAME_CURSOR_WIDTH (f) = width;
20254
20255 /* By default, set up the blink-off state depending on the on-state. */
20256
20257 tem = Fassoc (arg, Vblink_cursor_alist);
20258 if (!NILP (tem))
20259 {
20260 FRAME_BLINK_OFF_CURSOR (f)
20261 = get_specified_cursor_type (XCDR (tem), &width);
20262 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
20263 }
20264 else
20265 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
20266 }
20267
20268
20269 /* Return the cursor we want to be displayed in window W. Return
20270 width of bar/hbar cursor through WIDTH arg. Return with
20271 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
20272 (i.e. if the `system caret' should track this cursor).
20273
20274 In a mini-buffer window, we want the cursor only to appear if we
20275 are reading input from this window. For the selected window, we
20276 want the cursor type given by the frame parameter or buffer local
20277 setting of cursor-type. If explicitly marked off, draw no cursor.
20278 In all other cases, we want a hollow box cursor. */
20279
20280 static enum text_cursor_kinds
20281 get_window_cursor_type (w, glyph, width, active_cursor)
20282 struct window *w;
20283 struct glyph *glyph;
20284 int *width;
20285 int *active_cursor;
20286 {
20287 struct frame *f = XFRAME (w->frame);
20288 struct buffer *b = XBUFFER (w->buffer);
20289 int cursor_type = DEFAULT_CURSOR;
20290 Lisp_Object alt_cursor;
20291 int non_selected = 0;
20292
20293 *active_cursor = 1;
20294
20295 /* Echo area */
20296 if (cursor_in_echo_area
20297 && FRAME_HAS_MINIBUF_P (f)
20298 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
20299 {
20300 if (w == XWINDOW (echo_area_window))
20301 {
20302 *width = FRAME_CURSOR_WIDTH (f);
20303 return FRAME_DESIRED_CURSOR (f);
20304 }
20305
20306 *active_cursor = 0;
20307 non_selected = 1;
20308 }
20309
20310 /* Nonselected window or nonselected frame. */
20311 else if (w != XWINDOW (f->selected_window)
20312 #ifdef HAVE_WINDOW_SYSTEM
20313 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
20314 #endif
20315 )
20316 {
20317 *active_cursor = 0;
20318
20319 if (MINI_WINDOW_P (w) && minibuf_level == 0)
20320 return NO_CURSOR;
20321
20322 non_selected = 1;
20323 }
20324
20325 /* Never display a cursor in a window in which cursor-type is nil. */
20326 if (NILP (b->cursor_type))
20327 return NO_CURSOR;
20328
20329 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
20330 if (non_selected)
20331 {
20332 alt_cursor = XBUFFER (w->buffer)->cursor_in_non_selected_windows;
20333 return get_specified_cursor_type (alt_cursor, width);
20334 }
20335
20336 /* Get the normal cursor type for this window. */
20337 if (EQ (b->cursor_type, Qt))
20338 {
20339 cursor_type = FRAME_DESIRED_CURSOR (f);
20340 *width = FRAME_CURSOR_WIDTH (f);
20341 }
20342 else
20343 cursor_type = get_specified_cursor_type (b->cursor_type, width);
20344
20345 /* Use normal cursor if not blinked off. */
20346 if (!w->cursor_off_p)
20347 {
20348 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
20349 if (cursor_type == FILLED_BOX_CURSOR)
20350 cursor_type = HOLLOW_BOX_CURSOR;
20351 }
20352 return cursor_type;
20353 }
20354
20355 /* Cursor is blinked off, so determine how to "toggle" it. */
20356
20357 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
20358 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
20359 return get_specified_cursor_type (XCDR (alt_cursor), width);
20360
20361 /* Then see if frame has specified a specific blink off cursor type. */
20362 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
20363 {
20364 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
20365 return FRAME_BLINK_OFF_CURSOR (f);
20366 }
20367
20368 #if 0
20369 /* Some people liked having a permanently visible blinking cursor,
20370 while others had very strong opinions against it. So it was
20371 decided to remove it. KFS 2003-09-03 */
20372
20373 /* Finally perform built-in cursor blinking:
20374 filled box <-> hollow box
20375 wide [h]bar <-> narrow [h]bar
20376 narrow [h]bar <-> no cursor
20377 other type <-> no cursor */
20378
20379 if (cursor_type == FILLED_BOX_CURSOR)
20380 return HOLLOW_BOX_CURSOR;
20381
20382 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
20383 {
20384 *width = 1;
20385 return cursor_type;
20386 }
20387 #endif
20388
20389 return NO_CURSOR;
20390 }
20391
20392
20393 #ifdef HAVE_WINDOW_SYSTEM
20394
20395 /* Notice when the text cursor of window W has been completely
20396 overwritten by a drawing operation that outputs glyphs in AREA
20397 starting at X0 and ending at X1 in the line starting at Y0 and
20398 ending at Y1. X coordinates are area-relative. X1 < 0 means all
20399 the rest of the line after X0 has been written. Y coordinates
20400 are window-relative. */
20401
20402 static void
20403 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
20404 struct window *w;
20405 enum glyph_row_area area;
20406 int x0, y0, x1, y1;
20407 {
20408 int cx0, cx1, cy0, cy1;
20409 struct glyph_row *row;
20410
20411 if (!w->phys_cursor_on_p)
20412 return;
20413 if (area != TEXT_AREA)
20414 return;
20415
20416 if (w->phys_cursor.vpos < 0
20417 || w->phys_cursor.vpos >= w->current_matrix->nrows
20418 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
20419 !(row->enabled_p && row->displays_text_p)))
20420 return;
20421
20422 if (row->cursor_in_fringe_p)
20423 {
20424 row->cursor_in_fringe_p = 0;
20425 draw_fringe_bitmap (w, row, 0);
20426 w->phys_cursor_on_p = 0;
20427 return;
20428 }
20429
20430 cx0 = w->phys_cursor.x;
20431 cx1 = cx0 + w->phys_cursor_width;
20432 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
20433 return;
20434
20435 /* The cursor image will be completely removed from the
20436 screen if the output area intersects the cursor area in
20437 y-direction. When we draw in [y0 y1[, and some part of
20438 the cursor is at y < y0, that part must have been drawn
20439 before. When scrolling, the cursor is erased before
20440 actually scrolling, so we don't come here. When not
20441 scrolling, the rows above the old cursor row must have
20442 changed, and in this case these rows must have written
20443 over the cursor image.
20444
20445 Likewise if part of the cursor is below y1, with the
20446 exception of the cursor being in the first blank row at
20447 the buffer and window end because update_text_area
20448 doesn't draw that row. (Except when it does, but
20449 that's handled in update_text_area.) */
20450
20451 cy0 = w->phys_cursor.y;
20452 cy1 = cy0 + w->phys_cursor_height;
20453 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
20454 return;
20455
20456 w->phys_cursor_on_p = 0;
20457 }
20458
20459 #endif /* HAVE_WINDOW_SYSTEM */
20460
20461 \f
20462 /************************************************************************
20463 Mouse Face
20464 ************************************************************************/
20465
20466 #ifdef HAVE_WINDOW_SYSTEM
20467
20468 /* EXPORT for RIF:
20469 Fix the display of area AREA of overlapping row ROW in window W. */
20470
20471 void
20472 x_fix_overlapping_area (w, row, area)
20473 struct window *w;
20474 struct glyph_row *row;
20475 enum glyph_row_area area;
20476 {
20477 int i, x;
20478
20479 BLOCK_INPUT;
20480
20481 x = 0;
20482 for (i = 0; i < row->used[area];)
20483 {
20484 if (row->glyphs[area][i].overlaps_vertically_p)
20485 {
20486 int start = i, start_x = x;
20487
20488 do
20489 {
20490 x += row->glyphs[area][i].pixel_width;
20491 ++i;
20492 }
20493 while (i < row->used[area]
20494 && row->glyphs[area][i].overlaps_vertically_p);
20495
20496 draw_glyphs (w, start_x, row, area,
20497 start, i,
20498 DRAW_NORMAL_TEXT, 1);
20499 }
20500 else
20501 {
20502 x += row->glyphs[area][i].pixel_width;
20503 ++i;
20504 }
20505 }
20506
20507 UNBLOCK_INPUT;
20508 }
20509
20510
20511 /* EXPORT:
20512 Draw the cursor glyph of window W in glyph row ROW. See the
20513 comment of draw_glyphs for the meaning of HL. */
20514
20515 void
20516 draw_phys_cursor_glyph (w, row, hl)
20517 struct window *w;
20518 struct glyph_row *row;
20519 enum draw_glyphs_face hl;
20520 {
20521 /* If cursor hpos is out of bounds, don't draw garbage. This can
20522 happen in mini-buffer windows when switching between echo area
20523 glyphs and mini-buffer. */
20524 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
20525 {
20526 int on_p = w->phys_cursor_on_p;
20527 int x1;
20528 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
20529 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
20530 hl, 0);
20531 w->phys_cursor_on_p = on_p;
20532
20533 if (hl == DRAW_CURSOR)
20534 w->phys_cursor_width = x1 - w->phys_cursor.x;
20535 /* When we erase the cursor, and ROW is overlapped by other
20536 rows, make sure that these overlapping parts of other rows
20537 are redrawn. */
20538 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
20539 {
20540 if (row > w->current_matrix->rows
20541 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
20542 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
20543
20544 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
20545 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
20546 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
20547 }
20548 }
20549 }
20550
20551
20552 /* EXPORT:
20553 Erase the image of a cursor of window W from the screen. */
20554
20555 void
20556 erase_phys_cursor (w)
20557 struct window *w;
20558 {
20559 struct frame *f = XFRAME (w->frame);
20560 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
20561 int hpos = w->phys_cursor.hpos;
20562 int vpos = w->phys_cursor.vpos;
20563 int mouse_face_here_p = 0;
20564 struct glyph_matrix *active_glyphs = w->current_matrix;
20565 struct glyph_row *cursor_row;
20566 struct glyph *cursor_glyph;
20567 enum draw_glyphs_face hl;
20568
20569 /* No cursor displayed or row invalidated => nothing to do on the
20570 screen. */
20571 if (w->phys_cursor_type == NO_CURSOR)
20572 goto mark_cursor_off;
20573
20574 /* VPOS >= active_glyphs->nrows means that window has been resized.
20575 Don't bother to erase the cursor. */
20576 if (vpos >= active_glyphs->nrows)
20577 goto mark_cursor_off;
20578
20579 /* If row containing cursor is marked invalid, there is nothing we
20580 can do. */
20581 cursor_row = MATRIX_ROW (active_glyphs, vpos);
20582 if (!cursor_row->enabled_p)
20583 goto mark_cursor_off;
20584
20585 /* If line spacing is > 0, old cursor may only be partially visible in
20586 window after split-window. So adjust visible height. */
20587 cursor_row->visible_height = min (cursor_row->visible_height,
20588 window_text_bottom_y (w) - cursor_row->y);
20589
20590 /* If row is completely invisible, don't attempt to delete a cursor which
20591 isn't there. This can happen if cursor is at top of a window, and
20592 we switch to a buffer with a header line in that window. */
20593 if (cursor_row->visible_height <= 0)
20594 goto mark_cursor_off;
20595
20596 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
20597 if (cursor_row->cursor_in_fringe_p)
20598 {
20599 cursor_row->cursor_in_fringe_p = 0;
20600 draw_fringe_bitmap (w, cursor_row, 0);
20601 goto mark_cursor_off;
20602 }
20603
20604 /* This can happen when the new row is shorter than the old one.
20605 In this case, either draw_glyphs or clear_end_of_line
20606 should have cleared the cursor. Note that we wouldn't be
20607 able to erase the cursor in this case because we don't have a
20608 cursor glyph at hand. */
20609 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
20610 goto mark_cursor_off;
20611
20612 /* If the cursor is in the mouse face area, redisplay that when
20613 we clear the cursor. */
20614 if (! NILP (dpyinfo->mouse_face_window)
20615 && w == XWINDOW (dpyinfo->mouse_face_window)
20616 && (vpos > dpyinfo->mouse_face_beg_row
20617 || (vpos == dpyinfo->mouse_face_beg_row
20618 && hpos >= dpyinfo->mouse_face_beg_col))
20619 && (vpos < dpyinfo->mouse_face_end_row
20620 || (vpos == dpyinfo->mouse_face_end_row
20621 && hpos < dpyinfo->mouse_face_end_col))
20622 /* Don't redraw the cursor's spot in mouse face if it is at the
20623 end of a line (on a newline). The cursor appears there, but
20624 mouse highlighting does not. */
20625 && cursor_row->used[TEXT_AREA] > hpos)
20626 mouse_face_here_p = 1;
20627
20628 /* Maybe clear the display under the cursor. */
20629 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
20630 {
20631 int x, y;
20632 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
20633 int width;
20634
20635 cursor_glyph = get_phys_cursor_glyph (w);
20636 if (cursor_glyph == NULL)
20637 goto mark_cursor_off;
20638
20639 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
20640 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
20641 width = min (cursor_glyph->pixel_width,
20642 window_box_width (w, TEXT_AREA) - w->phys_cursor.x);
20643
20644 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
20645 }
20646
20647 /* Erase the cursor by redrawing the character underneath it. */
20648 if (mouse_face_here_p)
20649 hl = DRAW_MOUSE_FACE;
20650 else
20651 hl = DRAW_NORMAL_TEXT;
20652 draw_phys_cursor_glyph (w, cursor_row, hl);
20653
20654 mark_cursor_off:
20655 w->phys_cursor_on_p = 0;
20656 w->phys_cursor_type = NO_CURSOR;
20657 }
20658
20659
20660 /* EXPORT:
20661 Display or clear cursor of window W. If ON is zero, clear the
20662 cursor. If it is non-zero, display the cursor. If ON is nonzero,
20663 where to put the cursor is specified by HPOS, VPOS, X and Y. */
20664
20665 void
20666 display_and_set_cursor (w, on, hpos, vpos, x, y)
20667 struct window *w;
20668 int on, hpos, vpos, x, y;
20669 {
20670 struct frame *f = XFRAME (w->frame);
20671 int new_cursor_type;
20672 int new_cursor_width;
20673 int active_cursor;
20674 struct glyph_row *glyph_row;
20675 struct glyph *glyph;
20676
20677 /* This is pointless on invisible frames, and dangerous on garbaged
20678 windows and frames; in the latter case, the frame or window may
20679 be in the midst of changing its size, and x and y may be off the
20680 window. */
20681 if (! FRAME_VISIBLE_P (f)
20682 || FRAME_GARBAGED_P (f)
20683 || vpos >= w->current_matrix->nrows
20684 || hpos >= w->current_matrix->matrix_w)
20685 return;
20686
20687 /* If cursor is off and we want it off, return quickly. */
20688 if (!on && !w->phys_cursor_on_p)
20689 return;
20690
20691 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
20692 /* If cursor row is not enabled, we don't really know where to
20693 display the cursor. */
20694 if (!glyph_row->enabled_p)
20695 {
20696 w->phys_cursor_on_p = 0;
20697 return;
20698 }
20699
20700 glyph = NULL;
20701 if (!glyph_row->exact_window_width_line_p
20702 || hpos < glyph_row->used[TEXT_AREA])
20703 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
20704
20705 xassert (interrupt_input_blocked);
20706
20707 /* Set new_cursor_type to the cursor we want to be displayed. */
20708 new_cursor_type = get_window_cursor_type (w, glyph,
20709 &new_cursor_width, &active_cursor);
20710
20711 /* If cursor is currently being shown and we don't want it to be or
20712 it is in the wrong place, or the cursor type is not what we want,
20713 erase it. */
20714 if (w->phys_cursor_on_p
20715 && (!on
20716 || w->phys_cursor.x != x
20717 || w->phys_cursor.y != y
20718 || new_cursor_type != w->phys_cursor_type
20719 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
20720 && new_cursor_width != w->phys_cursor_width)))
20721 erase_phys_cursor (w);
20722
20723 /* Don't check phys_cursor_on_p here because that flag is only set
20724 to zero in some cases where we know that the cursor has been
20725 completely erased, to avoid the extra work of erasing the cursor
20726 twice. In other words, phys_cursor_on_p can be 1 and the cursor
20727 still not be visible, or it has only been partly erased. */
20728 if (on)
20729 {
20730 w->phys_cursor_ascent = glyph_row->ascent;
20731 w->phys_cursor_height = glyph_row->height;
20732
20733 /* Set phys_cursor_.* before x_draw_.* is called because some
20734 of them may need the information. */
20735 w->phys_cursor.x = x;
20736 w->phys_cursor.y = glyph_row->y;
20737 w->phys_cursor.hpos = hpos;
20738 w->phys_cursor.vpos = vpos;
20739 }
20740
20741 rif->draw_window_cursor (w, glyph_row, x, y,
20742 new_cursor_type, new_cursor_width,
20743 on, active_cursor);
20744 }
20745
20746
20747 /* Switch the display of W's cursor on or off, according to the value
20748 of ON. */
20749
20750 static void
20751 update_window_cursor (w, on)
20752 struct window *w;
20753 int on;
20754 {
20755 /* Don't update cursor in windows whose frame is in the process
20756 of being deleted. */
20757 if (w->current_matrix)
20758 {
20759 BLOCK_INPUT;
20760 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
20761 w->phys_cursor.x, w->phys_cursor.y);
20762 UNBLOCK_INPUT;
20763 }
20764 }
20765
20766
20767 /* Call update_window_cursor with parameter ON_P on all leaf windows
20768 in the window tree rooted at W. */
20769
20770 static void
20771 update_cursor_in_window_tree (w, on_p)
20772 struct window *w;
20773 int on_p;
20774 {
20775 while (w)
20776 {
20777 if (!NILP (w->hchild))
20778 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
20779 else if (!NILP (w->vchild))
20780 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
20781 else
20782 update_window_cursor (w, on_p);
20783
20784 w = NILP (w->next) ? 0 : XWINDOW (w->next);
20785 }
20786 }
20787
20788
20789 /* EXPORT:
20790 Display the cursor on window W, or clear it, according to ON_P.
20791 Don't change the cursor's position. */
20792
20793 void
20794 x_update_cursor (f, on_p)
20795 struct frame *f;
20796 int on_p;
20797 {
20798 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
20799 }
20800
20801
20802 /* EXPORT:
20803 Clear the cursor of window W to background color, and mark the
20804 cursor as not shown. This is used when the text where the cursor
20805 is is about to be rewritten. */
20806
20807 void
20808 x_clear_cursor (w)
20809 struct window *w;
20810 {
20811 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
20812 update_window_cursor (w, 0);
20813 }
20814
20815
20816 /* EXPORT:
20817 Display the active region described by mouse_face_* according to DRAW. */
20818
20819 void
20820 show_mouse_face (dpyinfo, draw)
20821 Display_Info *dpyinfo;
20822 enum draw_glyphs_face draw;
20823 {
20824 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
20825 struct frame *f = XFRAME (WINDOW_FRAME (w));
20826
20827 if (/* If window is in the process of being destroyed, don't bother
20828 to do anything. */
20829 w->current_matrix != NULL
20830 /* Don't update mouse highlight if hidden */
20831 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
20832 /* Recognize when we are called to operate on rows that don't exist
20833 anymore. This can happen when a window is split. */
20834 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
20835 {
20836 int phys_cursor_on_p = w->phys_cursor_on_p;
20837 struct glyph_row *row, *first, *last;
20838
20839 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
20840 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
20841
20842 for (row = first; row <= last && row->enabled_p; ++row)
20843 {
20844 int start_hpos, end_hpos, start_x;
20845
20846 /* For all but the first row, the highlight starts at column 0. */
20847 if (row == first)
20848 {
20849 start_hpos = dpyinfo->mouse_face_beg_col;
20850 start_x = dpyinfo->mouse_face_beg_x;
20851 }
20852 else
20853 {
20854 start_hpos = 0;
20855 start_x = 0;
20856 }
20857
20858 if (row == last)
20859 end_hpos = dpyinfo->mouse_face_end_col;
20860 else
20861 end_hpos = row->used[TEXT_AREA];
20862
20863 if (end_hpos > start_hpos)
20864 {
20865 draw_glyphs (w, start_x, row, TEXT_AREA,
20866 start_hpos, end_hpos,
20867 draw, 0);
20868
20869 row->mouse_face_p
20870 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
20871 }
20872 }
20873
20874 /* When we've written over the cursor, arrange for it to
20875 be displayed again. */
20876 if (phys_cursor_on_p && !w->phys_cursor_on_p)
20877 {
20878 BLOCK_INPUT;
20879 display_and_set_cursor (w, 1,
20880 w->phys_cursor.hpos, w->phys_cursor.vpos,
20881 w->phys_cursor.x, w->phys_cursor.y);
20882 UNBLOCK_INPUT;
20883 }
20884 }
20885
20886 /* Change the mouse cursor. */
20887 if (draw == DRAW_NORMAL_TEXT)
20888 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
20889 else if (draw == DRAW_MOUSE_FACE)
20890 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
20891 else
20892 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
20893 }
20894
20895 /* EXPORT:
20896 Clear out the mouse-highlighted active region.
20897 Redraw it un-highlighted first. Value is non-zero if mouse
20898 face was actually drawn unhighlighted. */
20899
20900 int
20901 clear_mouse_face (dpyinfo)
20902 Display_Info *dpyinfo;
20903 {
20904 int cleared = 0;
20905
20906 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
20907 {
20908 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
20909 cleared = 1;
20910 }
20911
20912 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
20913 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
20914 dpyinfo->mouse_face_window = Qnil;
20915 dpyinfo->mouse_face_overlay = Qnil;
20916 return cleared;
20917 }
20918
20919
20920 /* EXPORT:
20921 Non-zero if physical cursor of window W is within mouse face. */
20922
20923 int
20924 cursor_in_mouse_face_p (w)
20925 struct window *w;
20926 {
20927 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
20928 int in_mouse_face = 0;
20929
20930 if (WINDOWP (dpyinfo->mouse_face_window)
20931 && XWINDOW (dpyinfo->mouse_face_window) == w)
20932 {
20933 int hpos = w->phys_cursor.hpos;
20934 int vpos = w->phys_cursor.vpos;
20935
20936 if (vpos >= dpyinfo->mouse_face_beg_row
20937 && vpos <= dpyinfo->mouse_face_end_row
20938 && (vpos > dpyinfo->mouse_face_beg_row
20939 || hpos >= dpyinfo->mouse_face_beg_col)
20940 && (vpos < dpyinfo->mouse_face_end_row
20941 || hpos < dpyinfo->mouse_face_end_col
20942 || dpyinfo->mouse_face_past_end))
20943 in_mouse_face = 1;
20944 }
20945
20946 return in_mouse_face;
20947 }
20948
20949
20950
20951 \f
20952 /* Find the glyph matrix position of buffer position CHARPOS in window
20953 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
20954 current glyphs must be up to date. If CHARPOS is above window
20955 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
20956 of last line in W. In the row containing CHARPOS, stop before glyphs
20957 having STOP as object. */
20958
20959 #if 1 /* This is a version of fast_find_position that's more correct
20960 in the presence of hscrolling, for example. I didn't install
20961 it right away because the problem fixed is minor, it failed
20962 in 20.x as well, and I think it's too risky to install
20963 so near the release of 21.1. 2001-09-25 gerd. */
20964
20965 static int
20966 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
20967 struct window *w;
20968 int charpos;
20969 int *hpos, *vpos, *x, *y;
20970 Lisp_Object stop;
20971 {
20972 struct glyph_row *row, *first;
20973 struct glyph *glyph, *end;
20974 int past_end = 0;
20975
20976 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
20977 if (charpos < MATRIX_ROW_START_CHARPOS (first))
20978 {
20979 *x = first->x;
20980 *y = first->y;
20981 *hpos = 0;
20982 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
20983 return 1;
20984 }
20985
20986 row = row_containing_pos (w, charpos, first, NULL, 0);
20987 if (row == NULL)
20988 {
20989 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
20990 past_end = 1;
20991 }
20992
20993 /* If whole rows or last part of a row came from a display overlay,
20994 row_containing_pos will skip over such rows because their end pos
20995 equals the start pos of the overlay or interval.
20996
20997 Move back if we have a STOP object and previous row's
20998 end glyph came from STOP. */
20999 if (!NILP (stop))
21000 {
21001 struct glyph_row *prev;
21002 while ((prev = row - 1, prev >= first)
21003 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21004 && prev->used[TEXT_AREA] > 0)
21005 {
21006 struct glyph *beg = prev->glyphs[TEXT_AREA];
21007 glyph = beg + prev->used[TEXT_AREA];
21008 while (--glyph >= beg
21009 && INTEGERP (glyph->object));
21010 if (glyph < beg
21011 || !EQ (stop, glyph->object))
21012 break;
21013 row = prev;
21014 }
21015 }
21016
21017 *x = row->x;
21018 *y = row->y;
21019 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21020
21021 glyph = row->glyphs[TEXT_AREA];
21022 end = glyph + row->used[TEXT_AREA];
21023
21024 /* Skip over glyphs not having an object at the start of the row.
21025 These are special glyphs like truncation marks on terminal
21026 frames. */
21027 if (row->displays_text_p)
21028 while (glyph < end
21029 && INTEGERP (glyph->object)
21030 && !EQ (stop, glyph->object)
21031 && glyph->charpos < 0)
21032 {
21033 *x += glyph->pixel_width;
21034 ++glyph;
21035 }
21036
21037 while (glyph < end
21038 && !INTEGERP (glyph->object)
21039 && !EQ (stop, glyph->object)
21040 && (!BUFFERP (glyph->object)
21041 || glyph->charpos < charpos))
21042 {
21043 *x += glyph->pixel_width;
21044 ++glyph;
21045 }
21046
21047 *hpos = glyph - row->glyphs[TEXT_AREA];
21048 return !past_end;
21049 }
21050
21051 #else /* not 1 */
21052
21053 static int
21054 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21055 struct window *w;
21056 int pos;
21057 int *hpos, *vpos, *x, *y;
21058 Lisp_Object stop;
21059 {
21060 int i;
21061 int lastcol;
21062 int maybe_next_line_p = 0;
21063 int line_start_position;
21064 int yb = window_text_bottom_y (w);
21065 struct glyph_row *row, *best_row;
21066 int row_vpos, best_row_vpos;
21067 int current_x;
21068
21069 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21070 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21071
21072 while (row->y < yb)
21073 {
21074 if (row->used[TEXT_AREA])
21075 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21076 else
21077 line_start_position = 0;
21078
21079 if (line_start_position > pos)
21080 break;
21081 /* If the position sought is the end of the buffer,
21082 don't include the blank lines at the bottom of the window. */
21083 else if (line_start_position == pos
21084 && pos == BUF_ZV (XBUFFER (w->buffer)))
21085 {
21086 maybe_next_line_p = 1;
21087 break;
21088 }
21089 else if (line_start_position > 0)
21090 {
21091 best_row = row;
21092 best_row_vpos = row_vpos;
21093 }
21094
21095 if (row->y + row->height >= yb)
21096 break;
21097
21098 ++row;
21099 ++row_vpos;
21100 }
21101
21102 /* Find the right column within BEST_ROW. */
21103 lastcol = 0;
21104 current_x = best_row->x;
21105 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21106 {
21107 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21108 int charpos = glyph->charpos;
21109
21110 if (BUFFERP (glyph->object))
21111 {
21112 if (charpos == pos)
21113 {
21114 *hpos = i;
21115 *vpos = best_row_vpos;
21116 *x = current_x;
21117 *y = best_row->y;
21118 return 1;
21119 }
21120 else if (charpos > pos)
21121 break;
21122 }
21123 else if (EQ (glyph->object, stop))
21124 break;
21125
21126 if (charpos > 0)
21127 lastcol = i;
21128 current_x += glyph->pixel_width;
21129 }
21130
21131 /* If we're looking for the end of the buffer,
21132 and we didn't find it in the line we scanned,
21133 use the start of the following line. */
21134 if (maybe_next_line_p)
21135 {
21136 ++best_row;
21137 ++best_row_vpos;
21138 lastcol = 0;
21139 current_x = best_row->x;
21140 }
21141
21142 *vpos = best_row_vpos;
21143 *hpos = lastcol + 1;
21144 *x = current_x;
21145 *y = best_row->y;
21146 return 0;
21147 }
21148
21149 #endif /* not 1 */
21150
21151
21152 /* Find the position of the glyph for position POS in OBJECT in
21153 window W's current matrix, and return in *X, *Y the pixel
21154 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
21155
21156 RIGHT_P non-zero means return the position of the right edge of the
21157 glyph, RIGHT_P zero means return the left edge position.
21158
21159 If no glyph for POS exists in the matrix, return the position of
21160 the glyph with the next smaller position that is in the matrix, if
21161 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
21162 exists in the matrix, return the position of the glyph with the
21163 next larger position in OBJECT.
21164
21165 Value is non-zero if a glyph was found. */
21166
21167 static int
21168 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
21169 struct window *w;
21170 int pos;
21171 Lisp_Object object;
21172 int *hpos, *vpos, *x, *y;
21173 int right_p;
21174 {
21175 int yb = window_text_bottom_y (w);
21176 struct glyph_row *r;
21177 struct glyph *best_glyph = NULL;
21178 struct glyph_row *best_row = NULL;
21179 int best_x = 0;
21180
21181 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21182 r->enabled_p && r->y < yb;
21183 ++r)
21184 {
21185 struct glyph *g = r->glyphs[TEXT_AREA];
21186 struct glyph *e = g + r->used[TEXT_AREA];
21187 int gx;
21188
21189 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
21190 if (EQ (g->object, object))
21191 {
21192 if (g->charpos == pos)
21193 {
21194 best_glyph = g;
21195 best_x = gx;
21196 best_row = r;
21197 goto found;
21198 }
21199 else if (best_glyph == NULL
21200 || ((abs (g->charpos - pos)
21201 < abs (best_glyph->charpos - pos))
21202 && (right_p
21203 ? g->charpos < pos
21204 : g->charpos > pos)))
21205 {
21206 best_glyph = g;
21207 best_x = gx;
21208 best_row = r;
21209 }
21210 }
21211 }
21212
21213 found:
21214
21215 if (best_glyph)
21216 {
21217 *x = best_x;
21218 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
21219
21220 if (right_p)
21221 {
21222 *x += best_glyph->pixel_width;
21223 ++*hpos;
21224 }
21225
21226 *y = best_row->y;
21227 *vpos = best_row - w->current_matrix->rows;
21228 }
21229
21230 return best_glyph != NULL;
21231 }
21232
21233
21234 /* See if position X, Y is within a hot-spot of an image. */
21235
21236 static int
21237 on_hot_spot_p (hot_spot, x, y)
21238 Lisp_Object hot_spot;
21239 int x, y;
21240 {
21241 if (!CONSP (hot_spot))
21242 return 0;
21243
21244 if (EQ (XCAR (hot_spot), Qrect))
21245 {
21246 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
21247 Lisp_Object rect = XCDR (hot_spot);
21248 Lisp_Object tem;
21249 if (!CONSP (rect))
21250 return 0;
21251 if (!CONSP (XCAR (rect)))
21252 return 0;
21253 if (!CONSP (XCDR (rect)))
21254 return 0;
21255 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
21256 return 0;
21257 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
21258 return 0;
21259 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
21260 return 0;
21261 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
21262 return 0;
21263 return 1;
21264 }
21265 else if (EQ (XCAR (hot_spot), Qcircle))
21266 {
21267 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
21268 Lisp_Object circ = XCDR (hot_spot);
21269 Lisp_Object lr, lx0, ly0;
21270 if (CONSP (circ)
21271 && CONSP (XCAR (circ))
21272 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
21273 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
21274 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
21275 {
21276 double r = XFLOATINT (lr);
21277 double dx = XINT (lx0) - x;
21278 double dy = XINT (ly0) - y;
21279 return (dx * dx + dy * dy <= r * r);
21280 }
21281 }
21282 else if (EQ (XCAR (hot_spot), Qpoly))
21283 {
21284 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
21285 if (VECTORP (XCDR (hot_spot)))
21286 {
21287 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
21288 Lisp_Object *poly = v->contents;
21289 int n = v->size;
21290 int i;
21291 int inside = 0;
21292 Lisp_Object lx, ly;
21293 int x0, y0;
21294
21295 /* Need an even number of coordinates, and at least 3 edges. */
21296 if (n < 6 || n & 1)
21297 return 0;
21298
21299 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
21300 If count is odd, we are inside polygon. Pixels on edges
21301 may or may not be included depending on actual geometry of the
21302 polygon. */
21303 if ((lx = poly[n-2], !INTEGERP (lx))
21304 || (ly = poly[n-1], !INTEGERP (lx)))
21305 return 0;
21306 x0 = XINT (lx), y0 = XINT (ly);
21307 for (i = 0; i < n; i += 2)
21308 {
21309 int x1 = x0, y1 = y0;
21310 if ((lx = poly[i], !INTEGERP (lx))
21311 || (ly = poly[i+1], !INTEGERP (ly)))
21312 return 0;
21313 x0 = XINT (lx), y0 = XINT (ly);
21314
21315 /* Does this segment cross the X line? */
21316 if (x0 >= x)
21317 {
21318 if (x1 >= x)
21319 continue;
21320 }
21321 else if (x1 < x)
21322 continue;
21323 if (y > y0 && y > y1)
21324 continue;
21325 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
21326 inside = !inside;
21327 }
21328 return inside;
21329 }
21330 }
21331 /* If we don't understand the format, pretend we're not in the hot-spot. */
21332 return 0;
21333 }
21334
21335 Lisp_Object
21336 find_hot_spot (map, x, y)
21337 Lisp_Object map;
21338 int x, y;
21339 {
21340 while (CONSP (map))
21341 {
21342 if (CONSP (XCAR (map))
21343 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
21344 return XCAR (map);
21345 map = XCDR (map);
21346 }
21347
21348 return Qnil;
21349 }
21350
21351 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
21352 3, 3, 0,
21353 doc: /* Lookup in image map MAP coordinates X and Y.
21354 An image map is an alist where each element has the format (AREA ID PLIST).
21355 An AREA is specified as either a rectangle, a circle, or a polygon:
21356 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
21357 pixel coordinates of the upper left and bottom right corners.
21358 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
21359 and the radius of the circle; r may be a float or integer.
21360 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
21361 vector describes one corner in the polygon.
21362 Returns the alist element for the first matching AREA in MAP. */)
21363 (map, x, y)
21364 Lisp_Object map;
21365 Lisp_Object x, y;
21366 {
21367 if (NILP (map))
21368 return Qnil;
21369
21370 CHECK_NUMBER (x);
21371 CHECK_NUMBER (y);
21372
21373 return find_hot_spot (map, XINT (x), XINT (y));
21374 }
21375
21376
21377 /* Display frame CURSOR, optionally using shape defined by POINTER. */
21378 static void
21379 define_frame_cursor1 (f, cursor, pointer)
21380 struct frame *f;
21381 Cursor cursor;
21382 Lisp_Object pointer;
21383 {
21384 /* Do not change cursor shape while dragging mouse. */
21385 if (!NILP (do_mouse_tracking))
21386 return;
21387
21388 if (!NILP (pointer))
21389 {
21390 if (EQ (pointer, Qarrow))
21391 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21392 else if (EQ (pointer, Qhand))
21393 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
21394 else if (EQ (pointer, Qtext))
21395 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21396 else if (EQ (pointer, intern ("hdrag")))
21397 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21398 #ifdef HAVE_X_WINDOWS
21399 else if (EQ (pointer, intern ("vdrag")))
21400 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
21401 #endif
21402 else if (EQ (pointer, intern ("hourglass")))
21403 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
21404 else if (EQ (pointer, Qmodeline))
21405 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
21406 else
21407 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21408 }
21409
21410 if (cursor != No_Cursor)
21411 rif->define_frame_cursor (f, cursor);
21412 }
21413
21414 /* Take proper action when mouse has moved to the mode or header line
21415 or marginal area AREA of window W, x-position X and y-position Y.
21416 X is relative to the start of the text display area of W, so the
21417 width of bitmap areas and scroll bars must be subtracted to get a
21418 position relative to the start of the mode line. */
21419
21420 static void
21421 note_mode_line_or_margin_highlight (window, x, y, area)
21422 Lisp_Object window;
21423 int x, y;
21424 enum window_part area;
21425 {
21426 struct window *w = XWINDOW (window);
21427 struct frame *f = XFRAME (w->frame);
21428 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21429 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21430 Lisp_Object pointer = Qnil;
21431 int charpos, dx, dy, width, height;
21432 Lisp_Object string, object = Qnil;
21433 Lisp_Object pos, help;
21434
21435 Lisp_Object mouse_face;
21436 int original_x_pixel = x;
21437 struct glyph * glyph = NULL;
21438 struct glyph_row *row;
21439
21440 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
21441 {
21442 int x0;
21443 struct glyph *end;
21444
21445 string = mode_line_string (w, area, &x, &y, &charpos,
21446 &object, &dx, &dy, &width, &height);
21447
21448 row = (area == ON_MODE_LINE
21449 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
21450 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
21451
21452 /* Find glyph */
21453 if (row->mode_line_p && row->enabled_p)
21454 {
21455 glyph = row->glyphs[TEXT_AREA];
21456 end = glyph + row->used[TEXT_AREA];
21457
21458 for (x0 = original_x_pixel;
21459 glyph < end && x0 >= glyph->pixel_width;
21460 ++glyph)
21461 x0 -= glyph->pixel_width;
21462
21463 if (glyph >= end)
21464 glyph = NULL;
21465 }
21466 }
21467 else
21468 {
21469 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
21470 string = marginal_area_string (w, area, &x, &y, &charpos,
21471 &object, &dx, &dy, &width, &height);
21472 }
21473
21474 help = Qnil;
21475
21476 if (IMAGEP (object))
21477 {
21478 Lisp_Object image_map, hotspot;
21479 if ((image_map = Fplist_get (XCDR (object), QCmap),
21480 !NILP (image_map))
21481 && (hotspot = find_hot_spot (image_map, dx, dy),
21482 CONSP (hotspot))
21483 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21484 {
21485 Lisp_Object area_id, plist;
21486
21487 area_id = XCAR (hotspot);
21488 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21489 If so, we could look for mouse-enter, mouse-leave
21490 properties in PLIST (and do something...). */
21491 hotspot = XCDR (hotspot);
21492 if (CONSP (hotspot)
21493 && (plist = XCAR (hotspot), CONSP (plist)))
21494 {
21495 pointer = Fplist_get (plist, Qpointer);
21496 if (NILP (pointer))
21497 pointer = Qhand;
21498 help = Fplist_get (plist, Qhelp_echo);
21499 if (!NILP (help))
21500 {
21501 help_echo_string = help;
21502 /* Is this correct? ++kfs */
21503 XSETWINDOW (help_echo_window, w);
21504 help_echo_object = w->buffer;
21505 help_echo_pos = charpos;
21506 }
21507 }
21508 }
21509 if (NILP (pointer))
21510 pointer = Fplist_get (XCDR (object), QCpointer);
21511 }
21512
21513 if (STRINGP (string))
21514 {
21515 pos = make_number (charpos);
21516 /* If we're on a string with `help-echo' text property, arrange
21517 for the help to be displayed. This is done by setting the
21518 global variable help_echo_string to the help string. */
21519 if (NILP (help))
21520 {
21521 help = Fget_text_property (pos, Qhelp_echo, string);
21522 if (!NILP (help))
21523 {
21524 help_echo_string = help;
21525 XSETWINDOW (help_echo_window, w);
21526 help_echo_object = string;
21527 help_echo_pos = charpos;
21528 }
21529 }
21530
21531 if (NILP (pointer))
21532 pointer = Fget_text_property (pos, Qpointer, string);
21533
21534 /* Change the mouse pointer according to what is under X/Y. */
21535 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
21536 {
21537 Lisp_Object map;
21538 map = Fget_text_property (pos, Qlocal_map, string);
21539 if (!KEYMAPP (map))
21540 map = Fget_text_property (pos, Qkeymap, string);
21541 if (!KEYMAPP (map))
21542 cursor = dpyinfo->vertical_scroll_bar_cursor;
21543 }
21544
21545 /* Change the mouse face according to what is under X/Y. */
21546 mouse_face = Fget_text_property (pos, Qmouse_face, string);
21547 if (!NILP (mouse_face)
21548 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21549 && glyph)
21550 {
21551 Lisp_Object b, e;
21552
21553 struct glyph * tmp_glyph;
21554
21555 int gpos;
21556 int gseq_length;
21557 int total_pixel_width;
21558 int ignore;
21559
21560 int vpos, hpos;
21561
21562 b = Fprevious_single_property_change (make_number (charpos + 1),
21563 Qmouse_face, string, Qnil);
21564 if (NILP (b))
21565 b = make_number (0);
21566
21567 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
21568 if (NILP (e))
21569 e = make_number (SCHARS (string));
21570
21571 /* Calculate the position(glyph position: GPOS) of GLYPH in
21572 displayed string. GPOS is different from CHARPOS.
21573
21574 CHARPOS is the position of glyph in internal string
21575 object. A mode line string format has structures which
21576 is converted to a flatten by emacs lisp interpreter.
21577 The internal string is an element of the structures.
21578 The displayed string is the flatten string. */
21579 for (tmp_glyph = glyph - 1, gpos = 0;
21580 tmp_glyph->charpos >= XINT (b);
21581 tmp_glyph--, gpos++)
21582 {
21583 if (!EQ (tmp_glyph->object, glyph->object))
21584 break;
21585 }
21586
21587 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
21588 displayed string holding GLYPH.
21589
21590 GSEQ_LENGTH is different from SCHARS (STRING).
21591 SCHARS (STRING) returns the length of the internal string. */
21592 for (tmp_glyph = glyph, gseq_length = gpos;
21593 tmp_glyph->charpos < XINT (e);
21594 tmp_glyph++, gseq_length++)
21595 {
21596 if (!EQ (tmp_glyph->object, glyph->object))
21597 break;
21598 }
21599
21600 total_pixel_width = 0;
21601 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
21602 total_pixel_width += tmp_glyph->pixel_width;
21603
21604 /* Pre calculation of re-rendering position */
21605 vpos = (x - gpos);
21606 hpos = (area == ON_MODE_LINE
21607 ? (w->current_matrix)->nrows - 1
21608 : 0);
21609
21610 /* If the re-rendering position is included in the last
21611 re-rendering area, we should do nothing. */
21612 if ( EQ (window, dpyinfo->mouse_face_window)
21613 && dpyinfo->mouse_face_beg_col <= vpos
21614 && vpos < dpyinfo->mouse_face_end_col
21615 && dpyinfo->mouse_face_beg_row == hpos )
21616 return;
21617
21618 if (clear_mouse_face (dpyinfo))
21619 cursor = No_Cursor;
21620
21621 dpyinfo->mouse_face_beg_col = vpos;
21622 dpyinfo->mouse_face_beg_row = hpos;
21623
21624 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
21625 dpyinfo->mouse_face_beg_y = 0;
21626
21627 dpyinfo->mouse_face_end_col = vpos + gseq_length;
21628 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
21629
21630 dpyinfo->mouse_face_end_x = 0;
21631 dpyinfo->mouse_face_end_y = 0;
21632
21633 dpyinfo->mouse_face_past_end = 0;
21634 dpyinfo->mouse_face_window = window;
21635
21636 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
21637 charpos,
21638 0, 0, 0, &ignore,
21639 glyph->face_id, 1);
21640 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21641
21642 if (NILP (pointer))
21643 pointer = Qhand;
21644 }
21645 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
21646 clear_mouse_face (dpyinfo);
21647 }
21648 define_frame_cursor1 (f, cursor, pointer);
21649 }
21650
21651
21652 /* EXPORT:
21653 Take proper action when the mouse has moved to position X, Y on
21654 frame F as regards highlighting characters that have mouse-face
21655 properties. Also de-highlighting chars where the mouse was before.
21656 X and Y can be negative or out of range. */
21657
21658 void
21659 note_mouse_highlight (f, x, y)
21660 struct frame *f;
21661 int x, y;
21662 {
21663 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21664 enum window_part part;
21665 Lisp_Object window;
21666 struct window *w;
21667 Cursor cursor = No_Cursor;
21668 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
21669 struct buffer *b;
21670
21671 /* When a menu is active, don't highlight because this looks odd. */
21672 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
21673 if (popup_activated ())
21674 return;
21675 #endif
21676
21677 if (NILP (Vmouse_highlight)
21678 || !f->glyphs_initialized_p)
21679 return;
21680
21681 dpyinfo->mouse_face_mouse_x = x;
21682 dpyinfo->mouse_face_mouse_y = y;
21683 dpyinfo->mouse_face_mouse_frame = f;
21684
21685 if (dpyinfo->mouse_face_defer)
21686 return;
21687
21688 if (gc_in_progress)
21689 {
21690 dpyinfo->mouse_face_deferred_gc = 1;
21691 return;
21692 }
21693
21694 /* Which window is that in? */
21695 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
21696
21697 /* If we were displaying active text in another window, clear that.
21698 Also clear if we move out of text area in same window. */
21699 if (! EQ (window, dpyinfo->mouse_face_window)
21700 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
21701 && !NILP (dpyinfo->mouse_face_window)))
21702 clear_mouse_face (dpyinfo);
21703
21704 /* Not on a window -> return. */
21705 if (!WINDOWP (window))
21706 return;
21707
21708 /* Reset help_echo_string. It will get recomputed below. */
21709 help_echo_string = Qnil;
21710
21711 /* Convert to window-relative pixel coordinates. */
21712 w = XWINDOW (window);
21713 frame_to_window_pixel_xy (w, &x, &y);
21714
21715 /* Handle tool-bar window differently since it doesn't display a
21716 buffer. */
21717 if (EQ (window, f->tool_bar_window))
21718 {
21719 note_tool_bar_highlight (f, x, y);
21720 return;
21721 }
21722
21723 /* Mouse is on the mode, header line or margin? */
21724 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
21725 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
21726 {
21727 note_mode_line_or_margin_highlight (window, x, y, part);
21728 return;
21729 }
21730
21731 if (part == ON_VERTICAL_BORDER)
21732 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
21733 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
21734 || part == ON_SCROLL_BAR)
21735 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21736 else
21737 cursor = FRAME_X_OUTPUT (f)->text_cursor;
21738
21739 /* Are we in a window whose display is up to date?
21740 And verify the buffer's text has not changed. */
21741 b = XBUFFER (w->buffer);
21742 if (part == ON_TEXT
21743 && EQ (w->window_end_valid, w->buffer)
21744 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
21745 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
21746 {
21747 int hpos, vpos, pos, i, dx, dy, area;
21748 struct glyph *glyph;
21749 Lisp_Object object;
21750 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
21751 Lisp_Object *overlay_vec = NULL;
21752 int noverlays;
21753 struct buffer *obuf;
21754 int obegv, ozv, same_region;
21755
21756 /* Find the glyph under X/Y. */
21757 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
21758
21759 /* Look for :pointer property on image. */
21760 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21761 {
21762 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21763 if (img != NULL && IMAGEP (img->spec))
21764 {
21765 Lisp_Object image_map, hotspot;
21766 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
21767 !NILP (image_map))
21768 && (hotspot = find_hot_spot (image_map,
21769 glyph->slice.x + dx,
21770 glyph->slice.y + dy),
21771 CONSP (hotspot))
21772 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
21773 {
21774 Lisp_Object area_id, plist;
21775
21776 area_id = XCAR (hotspot);
21777 /* Could check AREA_ID to see if we enter/leave this hot-spot.
21778 If so, we could look for mouse-enter, mouse-leave
21779 properties in PLIST (and do something...). */
21780 hotspot = XCDR (hotspot);
21781 if (CONSP (hotspot)
21782 && (plist = XCAR (hotspot), CONSP (plist)))
21783 {
21784 pointer = Fplist_get (plist, Qpointer);
21785 if (NILP (pointer))
21786 pointer = Qhand;
21787 help_echo_string = Fplist_get (plist, Qhelp_echo);
21788 if (!NILP (help_echo_string))
21789 {
21790 help_echo_window = window;
21791 help_echo_object = glyph->object;
21792 help_echo_pos = glyph->charpos;
21793 }
21794 }
21795 }
21796 if (NILP (pointer))
21797 pointer = Fplist_get (XCDR (img->spec), QCpointer);
21798 }
21799 }
21800
21801 /* Clear mouse face if X/Y not over text. */
21802 if (glyph == NULL
21803 || area != TEXT_AREA
21804 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
21805 {
21806 if (clear_mouse_face (dpyinfo))
21807 cursor = No_Cursor;
21808 if (NILP (pointer))
21809 {
21810 if (area != TEXT_AREA)
21811 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
21812 else
21813 pointer = Vvoid_text_area_pointer;
21814 }
21815 goto set_cursor;
21816 }
21817
21818 pos = glyph->charpos;
21819 object = glyph->object;
21820 if (!STRINGP (object) && !BUFFERP (object))
21821 goto set_cursor;
21822
21823 /* If we get an out-of-range value, return now; avoid an error. */
21824 if (BUFFERP (object) && pos > BUF_Z (b))
21825 goto set_cursor;
21826
21827 /* Make the window's buffer temporarily current for
21828 overlays_at and compute_char_face. */
21829 obuf = current_buffer;
21830 current_buffer = b;
21831 obegv = BEGV;
21832 ozv = ZV;
21833 BEGV = BEG;
21834 ZV = Z;
21835
21836 /* Is this char mouse-active or does it have help-echo? */
21837 position = make_number (pos);
21838
21839 if (BUFFERP (object))
21840 {
21841 /* Put all the overlays we want in a vector in overlay_vec. */
21842 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
21843 /* Sort overlays into increasing priority order. */
21844 noverlays = sort_overlays (overlay_vec, noverlays, w);
21845 }
21846 else
21847 noverlays = 0;
21848
21849 same_region = (EQ (window, dpyinfo->mouse_face_window)
21850 && vpos >= dpyinfo->mouse_face_beg_row
21851 && vpos <= dpyinfo->mouse_face_end_row
21852 && (vpos > dpyinfo->mouse_face_beg_row
21853 || hpos >= dpyinfo->mouse_face_beg_col)
21854 && (vpos < dpyinfo->mouse_face_end_row
21855 || hpos < dpyinfo->mouse_face_end_col
21856 || dpyinfo->mouse_face_past_end));
21857
21858 if (same_region)
21859 cursor = No_Cursor;
21860
21861 /* Check mouse-face highlighting. */
21862 if (! same_region
21863 /* If there exists an overlay with mouse-face overlapping
21864 the one we are currently highlighting, we have to
21865 check if we enter the overlapping overlay, and then
21866 highlight only that. */
21867 || (OVERLAYP (dpyinfo->mouse_face_overlay)
21868 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
21869 {
21870 /* Find the highest priority overlay that has a mouse-face
21871 property. */
21872 overlay = Qnil;
21873 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
21874 {
21875 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
21876 if (!NILP (mouse_face))
21877 overlay = overlay_vec[i];
21878 }
21879
21880 /* If we're actually highlighting the same overlay as
21881 before, there's no need to do that again. */
21882 if (!NILP (overlay)
21883 && EQ (overlay, dpyinfo->mouse_face_overlay))
21884 goto check_help_echo;
21885
21886 dpyinfo->mouse_face_overlay = overlay;
21887
21888 /* Clear the display of the old active region, if any. */
21889 if (clear_mouse_face (dpyinfo))
21890 cursor = No_Cursor;
21891
21892 /* If no overlay applies, get a text property. */
21893 if (NILP (overlay))
21894 mouse_face = Fget_text_property (position, Qmouse_face, object);
21895
21896 /* Handle the overlay case. */
21897 if (!NILP (overlay))
21898 {
21899 /* Find the range of text around this char that
21900 should be active. */
21901 Lisp_Object before, after;
21902 int ignore;
21903
21904 before = Foverlay_start (overlay);
21905 after = Foverlay_end (overlay);
21906 /* Record this as the current active region. */
21907 fast_find_position (w, XFASTINT (before),
21908 &dpyinfo->mouse_face_beg_col,
21909 &dpyinfo->mouse_face_beg_row,
21910 &dpyinfo->mouse_face_beg_x,
21911 &dpyinfo->mouse_face_beg_y, Qnil);
21912
21913 dpyinfo->mouse_face_past_end
21914 = !fast_find_position (w, XFASTINT (after),
21915 &dpyinfo->mouse_face_end_col,
21916 &dpyinfo->mouse_face_end_row,
21917 &dpyinfo->mouse_face_end_x,
21918 &dpyinfo->mouse_face_end_y, Qnil);
21919 dpyinfo->mouse_face_window = window;
21920
21921 dpyinfo->mouse_face_face_id
21922 = face_at_buffer_position (w, pos, 0, 0,
21923 &ignore, pos + 1,
21924 !dpyinfo->mouse_face_hidden);
21925
21926 /* Display it as active. */
21927 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21928 cursor = No_Cursor;
21929 }
21930 /* Handle the text property case. */
21931 else if (!NILP (mouse_face) && BUFFERP (object))
21932 {
21933 /* Find the range of text around this char that
21934 should be active. */
21935 Lisp_Object before, after, beginning, end;
21936 int ignore;
21937
21938 beginning = Fmarker_position (w->start);
21939 end = make_number (BUF_Z (XBUFFER (object))
21940 - XFASTINT (w->window_end_pos));
21941 before
21942 = Fprevious_single_property_change (make_number (pos + 1),
21943 Qmouse_face,
21944 object, beginning);
21945 after
21946 = Fnext_single_property_change (position, Qmouse_face,
21947 object, end);
21948
21949 /* Record this as the current active region. */
21950 fast_find_position (w, XFASTINT (before),
21951 &dpyinfo->mouse_face_beg_col,
21952 &dpyinfo->mouse_face_beg_row,
21953 &dpyinfo->mouse_face_beg_x,
21954 &dpyinfo->mouse_face_beg_y, Qnil);
21955 dpyinfo->mouse_face_past_end
21956 = !fast_find_position (w, XFASTINT (after),
21957 &dpyinfo->mouse_face_end_col,
21958 &dpyinfo->mouse_face_end_row,
21959 &dpyinfo->mouse_face_end_x,
21960 &dpyinfo->mouse_face_end_y, Qnil);
21961 dpyinfo->mouse_face_window = window;
21962
21963 if (BUFFERP (object))
21964 dpyinfo->mouse_face_face_id
21965 = face_at_buffer_position (w, pos, 0, 0,
21966 &ignore, pos + 1,
21967 !dpyinfo->mouse_face_hidden);
21968
21969 /* Display it as active. */
21970 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
21971 cursor = No_Cursor;
21972 }
21973 else if (!NILP (mouse_face) && STRINGP (object))
21974 {
21975 Lisp_Object b, e;
21976 int ignore;
21977
21978 b = Fprevious_single_property_change (make_number (pos + 1),
21979 Qmouse_face,
21980 object, Qnil);
21981 e = Fnext_single_property_change (position, Qmouse_face,
21982 object, Qnil);
21983 if (NILP (b))
21984 b = make_number (0);
21985 if (NILP (e))
21986 e = make_number (SCHARS (object) - 1);
21987
21988 fast_find_string_pos (w, XINT (b), object,
21989 &dpyinfo->mouse_face_beg_col,
21990 &dpyinfo->mouse_face_beg_row,
21991 &dpyinfo->mouse_face_beg_x,
21992 &dpyinfo->mouse_face_beg_y, 0);
21993 fast_find_string_pos (w, XINT (e), object,
21994 &dpyinfo->mouse_face_end_col,
21995 &dpyinfo->mouse_face_end_row,
21996 &dpyinfo->mouse_face_end_x,
21997 &dpyinfo->mouse_face_end_y, 1);
21998 dpyinfo->mouse_face_past_end = 0;
21999 dpyinfo->mouse_face_window = window;
22000 dpyinfo->mouse_face_face_id
22001 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22002 glyph->face_id, 1);
22003 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22004 cursor = No_Cursor;
22005 }
22006 else if (STRINGP (object) && NILP (mouse_face))
22007 {
22008 /* A string which doesn't have mouse-face, but
22009 the text ``under'' it might have. */
22010 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22011 int start = MATRIX_ROW_START_CHARPOS (r);
22012
22013 pos = string_buffer_position (w, object, start);
22014 if (pos > 0)
22015 mouse_face = get_char_property_and_overlay (make_number (pos),
22016 Qmouse_face,
22017 w->buffer,
22018 &overlay);
22019 if (!NILP (mouse_face) && !NILP (overlay))
22020 {
22021 Lisp_Object before = Foverlay_start (overlay);
22022 Lisp_Object after = Foverlay_end (overlay);
22023 int ignore;
22024
22025 /* Note that we might not be able to find position
22026 BEFORE in the glyph matrix if the overlay is
22027 entirely covered by a `display' property. In
22028 this case, we overshoot. So let's stop in
22029 the glyph matrix before glyphs for OBJECT. */
22030 fast_find_position (w, XFASTINT (before),
22031 &dpyinfo->mouse_face_beg_col,
22032 &dpyinfo->mouse_face_beg_row,
22033 &dpyinfo->mouse_face_beg_x,
22034 &dpyinfo->mouse_face_beg_y,
22035 object);
22036
22037 dpyinfo->mouse_face_past_end
22038 = !fast_find_position (w, XFASTINT (after),
22039 &dpyinfo->mouse_face_end_col,
22040 &dpyinfo->mouse_face_end_row,
22041 &dpyinfo->mouse_face_end_x,
22042 &dpyinfo->mouse_face_end_y,
22043 Qnil);
22044 dpyinfo->mouse_face_window = window;
22045 dpyinfo->mouse_face_face_id
22046 = face_at_buffer_position (w, pos, 0, 0,
22047 &ignore, pos + 1,
22048 !dpyinfo->mouse_face_hidden);
22049
22050 /* Display it as active. */
22051 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22052 cursor = No_Cursor;
22053 }
22054 }
22055 }
22056
22057 check_help_echo:
22058
22059 /* Look for a `help-echo' property. */
22060 if (NILP (help_echo_string)) {
22061 Lisp_Object help, overlay;
22062
22063 /* Check overlays first. */
22064 help = overlay = Qnil;
22065 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22066 {
22067 overlay = overlay_vec[i];
22068 help = Foverlay_get (overlay, Qhelp_echo);
22069 }
22070
22071 if (!NILP (help))
22072 {
22073 help_echo_string = help;
22074 help_echo_window = window;
22075 help_echo_object = overlay;
22076 help_echo_pos = pos;
22077 }
22078 else
22079 {
22080 Lisp_Object object = glyph->object;
22081 int charpos = glyph->charpos;
22082
22083 /* Try text properties. */
22084 if (STRINGP (object)
22085 && charpos >= 0
22086 && charpos < SCHARS (object))
22087 {
22088 help = Fget_text_property (make_number (charpos),
22089 Qhelp_echo, object);
22090 if (NILP (help))
22091 {
22092 /* If the string itself doesn't specify a help-echo,
22093 see if the buffer text ``under'' it does. */
22094 struct glyph_row *r
22095 = MATRIX_ROW (w->current_matrix, vpos);
22096 int start = MATRIX_ROW_START_CHARPOS (r);
22097 int pos = string_buffer_position (w, object, start);
22098 if (pos > 0)
22099 {
22100 help = Fget_char_property (make_number (pos),
22101 Qhelp_echo, w->buffer);
22102 if (!NILP (help))
22103 {
22104 charpos = pos;
22105 object = w->buffer;
22106 }
22107 }
22108 }
22109 }
22110 else if (BUFFERP (object)
22111 && charpos >= BEGV
22112 && charpos < ZV)
22113 help = Fget_text_property (make_number (charpos), Qhelp_echo,
22114 object);
22115
22116 if (!NILP (help))
22117 {
22118 help_echo_string = help;
22119 help_echo_window = window;
22120 help_echo_object = object;
22121 help_echo_pos = charpos;
22122 }
22123 }
22124 }
22125
22126 /* Look for a `pointer' property. */
22127 if (NILP (pointer))
22128 {
22129 /* Check overlays first. */
22130 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
22131 pointer = Foverlay_get (overlay_vec[i], Qpointer);
22132
22133 if (NILP (pointer))
22134 {
22135 Lisp_Object object = glyph->object;
22136 int charpos = glyph->charpos;
22137
22138 /* Try text properties. */
22139 if (STRINGP (object)
22140 && charpos >= 0
22141 && charpos < SCHARS (object))
22142 {
22143 pointer = Fget_text_property (make_number (charpos),
22144 Qpointer, object);
22145 if (NILP (pointer))
22146 {
22147 /* If the string itself doesn't specify a pointer,
22148 see if the buffer text ``under'' it does. */
22149 struct glyph_row *r
22150 = MATRIX_ROW (w->current_matrix, vpos);
22151 int start = MATRIX_ROW_START_CHARPOS (r);
22152 int pos = string_buffer_position (w, object, start);
22153 if (pos > 0)
22154 pointer = Fget_char_property (make_number (pos),
22155 Qpointer, w->buffer);
22156 }
22157 }
22158 else if (BUFFERP (object)
22159 && charpos >= BEGV
22160 && charpos < ZV)
22161 pointer = Fget_text_property (make_number (charpos),
22162 Qpointer, object);
22163 }
22164 }
22165
22166 BEGV = obegv;
22167 ZV = ozv;
22168 current_buffer = obuf;
22169 }
22170
22171 set_cursor:
22172
22173 define_frame_cursor1 (f, cursor, pointer);
22174 }
22175
22176
22177 /* EXPORT for RIF:
22178 Clear any mouse-face on window W. This function is part of the
22179 redisplay interface, and is called from try_window_id and similar
22180 functions to ensure the mouse-highlight is off. */
22181
22182 void
22183 x_clear_window_mouse_face (w)
22184 struct window *w;
22185 {
22186 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
22187 Lisp_Object window;
22188
22189 BLOCK_INPUT;
22190 XSETWINDOW (window, w);
22191 if (EQ (window, dpyinfo->mouse_face_window))
22192 clear_mouse_face (dpyinfo);
22193 UNBLOCK_INPUT;
22194 }
22195
22196
22197 /* EXPORT:
22198 Just discard the mouse face information for frame F, if any.
22199 This is used when the size of F is changed. */
22200
22201 void
22202 cancel_mouse_face (f)
22203 struct frame *f;
22204 {
22205 Lisp_Object window;
22206 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22207
22208 window = dpyinfo->mouse_face_window;
22209 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
22210 {
22211 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
22212 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
22213 dpyinfo->mouse_face_window = Qnil;
22214 }
22215 }
22216
22217
22218 #endif /* HAVE_WINDOW_SYSTEM */
22219
22220 \f
22221 /***********************************************************************
22222 Exposure Events
22223 ***********************************************************************/
22224
22225 #ifdef HAVE_WINDOW_SYSTEM
22226
22227 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
22228 which intersects rectangle R. R is in window-relative coordinates. */
22229
22230 static void
22231 expose_area (w, row, r, area)
22232 struct window *w;
22233 struct glyph_row *row;
22234 XRectangle *r;
22235 enum glyph_row_area area;
22236 {
22237 struct glyph *first = row->glyphs[area];
22238 struct glyph *end = row->glyphs[area] + row->used[area];
22239 struct glyph *last;
22240 int first_x, start_x, x;
22241
22242 if (area == TEXT_AREA && row->fill_line_p)
22243 /* If row extends face to end of line write the whole line. */
22244 draw_glyphs (w, 0, row, area,
22245 0, row->used[area],
22246 DRAW_NORMAL_TEXT, 0);
22247 else
22248 {
22249 /* Set START_X to the window-relative start position for drawing glyphs of
22250 AREA. The first glyph of the text area can be partially visible.
22251 The first glyphs of other areas cannot. */
22252 start_x = window_box_left_offset (w, area);
22253 x = start_x;
22254 if (area == TEXT_AREA)
22255 x += row->x;
22256
22257 /* Find the first glyph that must be redrawn. */
22258 while (first < end
22259 && x + first->pixel_width < r->x)
22260 {
22261 x += first->pixel_width;
22262 ++first;
22263 }
22264
22265 /* Find the last one. */
22266 last = first;
22267 first_x = x;
22268 while (last < end
22269 && x < r->x + r->width)
22270 {
22271 x += last->pixel_width;
22272 ++last;
22273 }
22274
22275 /* Repaint. */
22276 if (last > first)
22277 draw_glyphs (w, first_x - start_x, row, area,
22278 first - row->glyphs[area], last - row->glyphs[area],
22279 DRAW_NORMAL_TEXT, 0);
22280 }
22281 }
22282
22283
22284 /* Redraw the parts of the glyph row ROW on window W intersecting
22285 rectangle R. R is in window-relative coordinates. Value is
22286 non-zero if mouse-face was overwritten. */
22287
22288 static int
22289 expose_line (w, row, r)
22290 struct window *w;
22291 struct glyph_row *row;
22292 XRectangle *r;
22293 {
22294 xassert (row->enabled_p);
22295
22296 if (row->mode_line_p || w->pseudo_window_p)
22297 draw_glyphs (w, 0, row, TEXT_AREA,
22298 0, row->used[TEXT_AREA],
22299 DRAW_NORMAL_TEXT, 0);
22300 else
22301 {
22302 if (row->used[LEFT_MARGIN_AREA])
22303 expose_area (w, row, r, LEFT_MARGIN_AREA);
22304 if (row->used[TEXT_AREA])
22305 expose_area (w, row, r, TEXT_AREA);
22306 if (row->used[RIGHT_MARGIN_AREA])
22307 expose_area (w, row, r, RIGHT_MARGIN_AREA);
22308 draw_row_fringe_bitmaps (w, row);
22309 }
22310
22311 return row->mouse_face_p;
22312 }
22313
22314
22315 /* Redraw those parts of glyphs rows during expose event handling that
22316 overlap other rows. Redrawing of an exposed line writes over parts
22317 of lines overlapping that exposed line; this function fixes that.
22318
22319 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
22320 row in W's current matrix that is exposed and overlaps other rows.
22321 LAST_OVERLAPPING_ROW is the last such row. */
22322
22323 static void
22324 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
22325 struct window *w;
22326 struct glyph_row *first_overlapping_row;
22327 struct glyph_row *last_overlapping_row;
22328 {
22329 struct glyph_row *row;
22330
22331 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
22332 if (row->overlapping_p)
22333 {
22334 xassert (row->enabled_p && !row->mode_line_p);
22335
22336 if (row->used[LEFT_MARGIN_AREA])
22337 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
22338
22339 if (row->used[TEXT_AREA])
22340 x_fix_overlapping_area (w, row, TEXT_AREA);
22341
22342 if (row->used[RIGHT_MARGIN_AREA])
22343 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
22344 }
22345 }
22346
22347
22348 /* Return non-zero if W's cursor intersects rectangle R. */
22349
22350 static int
22351 phys_cursor_in_rect_p (w, r)
22352 struct window *w;
22353 XRectangle *r;
22354 {
22355 XRectangle cr, result;
22356 struct glyph *cursor_glyph;
22357
22358 cursor_glyph = get_phys_cursor_glyph (w);
22359 if (cursor_glyph)
22360 {
22361 /* r is relative to W's box, but w->phys_cursor.x is relative
22362 to left edge of W's TEXT area. Adjust it. */
22363 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
22364 cr.y = w->phys_cursor.y;
22365 cr.width = cursor_glyph->pixel_width;
22366 cr.height = w->phys_cursor_height;
22367 /* ++KFS: W32 version used W32-specific IntersectRect here, but
22368 I assume the effect is the same -- and this is portable. */
22369 return x_intersect_rectangles (&cr, r, &result);
22370 }
22371 else
22372 return 0;
22373 }
22374
22375
22376 /* EXPORT:
22377 Draw a vertical window border to the right of window W if W doesn't
22378 have vertical scroll bars. */
22379
22380 void
22381 x_draw_vertical_border (w)
22382 struct window *w;
22383 {
22384 /* We could do better, if we knew what type of scroll-bar the adjacent
22385 windows (on either side) have... But we don't :-(
22386 However, I think this works ok. ++KFS 2003-04-25 */
22387
22388 /* Redraw borders between horizontally adjacent windows. Don't
22389 do it for frames with vertical scroll bars because either the
22390 right scroll bar of a window, or the left scroll bar of its
22391 neighbor will suffice as a border. */
22392 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
22393 return;
22394
22395 if (!WINDOW_RIGHTMOST_P (w)
22396 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
22397 {
22398 int x0, x1, y0, y1;
22399
22400 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22401 y1 -= 1;
22402
22403 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22404 x1 -= 1;
22405
22406 rif->draw_vertical_window_border (w, x1, y0, y1);
22407 }
22408 else if (!WINDOW_LEFTMOST_P (w)
22409 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
22410 {
22411 int x0, x1, y0, y1;
22412
22413 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
22414 y1 -= 1;
22415
22416 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
22417 x0 -= 1;
22418
22419 rif->draw_vertical_window_border (w, x0, y0, y1);
22420 }
22421 }
22422
22423
22424 /* Redraw the part of window W intersection rectangle FR. Pixel
22425 coordinates in FR are frame-relative. Call this function with
22426 input blocked. Value is non-zero if the exposure overwrites
22427 mouse-face. */
22428
22429 static int
22430 expose_window (w, fr)
22431 struct window *w;
22432 XRectangle *fr;
22433 {
22434 struct frame *f = XFRAME (w->frame);
22435 XRectangle wr, r;
22436 int mouse_face_overwritten_p = 0;
22437
22438 /* If window is not yet fully initialized, do nothing. This can
22439 happen when toolkit scroll bars are used and a window is split.
22440 Reconfiguring the scroll bar will generate an expose for a newly
22441 created window. */
22442 if (w->current_matrix == NULL)
22443 return 0;
22444
22445 /* When we're currently updating the window, display and current
22446 matrix usually don't agree. Arrange for a thorough display
22447 later. */
22448 if (w == updated_window)
22449 {
22450 SET_FRAME_GARBAGED (f);
22451 return 0;
22452 }
22453
22454 /* Frame-relative pixel rectangle of W. */
22455 wr.x = WINDOW_LEFT_EDGE_X (w);
22456 wr.y = WINDOW_TOP_EDGE_Y (w);
22457 wr.width = WINDOW_TOTAL_WIDTH (w);
22458 wr.height = WINDOW_TOTAL_HEIGHT (w);
22459
22460 if (x_intersect_rectangles (fr, &wr, &r))
22461 {
22462 int yb = window_text_bottom_y (w);
22463 struct glyph_row *row;
22464 int cursor_cleared_p;
22465 struct glyph_row *first_overlapping_row, *last_overlapping_row;
22466
22467 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
22468 r.x, r.y, r.width, r.height));
22469
22470 /* Convert to window coordinates. */
22471 r.x -= WINDOW_LEFT_EDGE_X (w);
22472 r.y -= WINDOW_TOP_EDGE_Y (w);
22473
22474 /* Turn off the cursor. */
22475 if (!w->pseudo_window_p
22476 && phys_cursor_in_rect_p (w, &r))
22477 {
22478 x_clear_cursor (w);
22479 cursor_cleared_p = 1;
22480 }
22481 else
22482 cursor_cleared_p = 0;
22483
22484 /* Update lines intersecting rectangle R. */
22485 first_overlapping_row = last_overlapping_row = NULL;
22486 for (row = w->current_matrix->rows;
22487 row->enabled_p;
22488 ++row)
22489 {
22490 int y0 = row->y;
22491 int y1 = MATRIX_ROW_BOTTOM_Y (row);
22492
22493 if ((y0 >= r.y && y0 < r.y + r.height)
22494 || (y1 > r.y && y1 < r.y + r.height)
22495 || (r.y >= y0 && r.y < y1)
22496 || (r.y + r.height > y0 && r.y + r.height < y1))
22497 {
22498 /* A header line may be overlapping, but there is no need
22499 to fix overlapping areas for them. KFS 2005-02-12 */
22500 if (row->overlapping_p && !row->mode_line_p)
22501 {
22502 if (first_overlapping_row == NULL)
22503 first_overlapping_row = row;
22504 last_overlapping_row = row;
22505 }
22506
22507 if (expose_line (w, row, &r))
22508 mouse_face_overwritten_p = 1;
22509 }
22510
22511 if (y1 >= yb)
22512 break;
22513 }
22514
22515 /* Display the mode line if there is one. */
22516 if (WINDOW_WANTS_MODELINE_P (w)
22517 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
22518 row->enabled_p)
22519 && row->y < r.y + r.height)
22520 {
22521 if (expose_line (w, row, &r))
22522 mouse_face_overwritten_p = 1;
22523 }
22524
22525 if (!w->pseudo_window_p)
22526 {
22527 /* Fix the display of overlapping rows. */
22528 if (first_overlapping_row)
22529 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
22530
22531 /* Draw border between windows. */
22532 x_draw_vertical_border (w);
22533
22534 /* Turn the cursor on again. */
22535 if (cursor_cleared_p)
22536 update_window_cursor (w, 1);
22537 }
22538 }
22539
22540 return mouse_face_overwritten_p;
22541 }
22542
22543
22544
22545 /* Redraw (parts) of all windows in the window tree rooted at W that
22546 intersect R. R contains frame pixel coordinates. Value is
22547 non-zero if the exposure overwrites mouse-face. */
22548
22549 static int
22550 expose_window_tree (w, r)
22551 struct window *w;
22552 XRectangle *r;
22553 {
22554 struct frame *f = XFRAME (w->frame);
22555 int mouse_face_overwritten_p = 0;
22556
22557 while (w && !FRAME_GARBAGED_P (f))
22558 {
22559 if (!NILP (w->hchild))
22560 mouse_face_overwritten_p
22561 |= expose_window_tree (XWINDOW (w->hchild), r);
22562 else if (!NILP (w->vchild))
22563 mouse_face_overwritten_p
22564 |= expose_window_tree (XWINDOW (w->vchild), r);
22565 else
22566 mouse_face_overwritten_p |= expose_window (w, r);
22567
22568 w = NILP (w->next) ? NULL : XWINDOW (w->next);
22569 }
22570
22571 return mouse_face_overwritten_p;
22572 }
22573
22574
22575 /* EXPORT:
22576 Redisplay an exposed area of frame F. X and Y are the upper-left
22577 corner of the exposed rectangle. W and H are width and height of
22578 the exposed area. All are pixel values. W or H zero means redraw
22579 the entire frame. */
22580
22581 void
22582 expose_frame (f, x, y, w, h)
22583 struct frame *f;
22584 int x, y, w, h;
22585 {
22586 XRectangle r;
22587 int mouse_face_overwritten_p = 0;
22588
22589 TRACE ((stderr, "expose_frame "));
22590
22591 /* No need to redraw if frame will be redrawn soon. */
22592 if (FRAME_GARBAGED_P (f))
22593 {
22594 TRACE ((stderr, " garbaged\n"));
22595 return;
22596 }
22597
22598 /* If basic faces haven't been realized yet, there is no point in
22599 trying to redraw anything. This can happen when we get an expose
22600 event while Emacs is starting, e.g. by moving another window. */
22601 if (FRAME_FACE_CACHE (f) == NULL
22602 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
22603 {
22604 TRACE ((stderr, " no faces\n"));
22605 return;
22606 }
22607
22608 if (w == 0 || h == 0)
22609 {
22610 r.x = r.y = 0;
22611 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
22612 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
22613 }
22614 else
22615 {
22616 r.x = x;
22617 r.y = y;
22618 r.width = w;
22619 r.height = h;
22620 }
22621
22622 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
22623 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
22624
22625 if (WINDOWP (f->tool_bar_window))
22626 mouse_face_overwritten_p
22627 |= expose_window (XWINDOW (f->tool_bar_window), &r);
22628
22629 #ifdef HAVE_X_WINDOWS
22630 #ifndef MSDOS
22631 #ifndef USE_X_TOOLKIT
22632 if (WINDOWP (f->menu_bar_window))
22633 mouse_face_overwritten_p
22634 |= expose_window (XWINDOW (f->menu_bar_window), &r);
22635 #endif /* not USE_X_TOOLKIT */
22636 #endif
22637 #endif
22638
22639 /* Some window managers support a focus-follows-mouse style with
22640 delayed raising of frames. Imagine a partially obscured frame,
22641 and moving the mouse into partially obscured mouse-face on that
22642 frame. The visible part of the mouse-face will be highlighted,
22643 then the WM raises the obscured frame. With at least one WM, KDE
22644 2.1, Emacs is not getting any event for the raising of the frame
22645 (even tried with SubstructureRedirectMask), only Expose events.
22646 These expose events will draw text normally, i.e. not
22647 highlighted. Which means we must redo the highlight here.
22648 Subsume it under ``we love X''. --gerd 2001-08-15 */
22649 /* Included in Windows version because Windows most likely does not
22650 do the right thing if any third party tool offers
22651 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
22652 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
22653 {
22654 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22655 if (f == dpyinfo->mouse_face_mouse_frame)
22656 {
22657 int x = dpyinfo->mouse_face_mouse_x;
22658 int y = dpyinfo->mouse_face_mouse_y;
22659 clear_mouse_face (dpyinfo);
22660 note_mouse_highlight (f, x, y);
22661 }
22662 }
22663 }
22664
22665
22666 /* EXPORT:
22667 Determine the intersection of two rectangles R1 and R2. Return
22668 the intersection in *RESULT. Value is non-zero if RESULT is not
22669 empty. */
22670
22671 int
22672 x_intersect_rectangles (r1, r2, result)
22673 XRectangle *r1, *r2, *result;
22674 {
22675 XRectangle *left, *right;
22676 XRectangle *upper, *lower;
22677 int intersection_p = 0;
22678
22679 /* Rearrange so that R1 is the left-most rectangle. */
22680 if (r1->x < r2->x)
22681 left = r1, right = r2;
22682 else
22683 left = r2, right = r1;
22684
22685 /* X0 of the intersection is right.x0, if this is inside R1,
22686 otherwise there is no intersection. */
22687 if (right->x <= left->x + left->width)
22688 {
22689 result->x = right->x;
22690
22691 /* The right end of the intersection is the minimum of the
22692 the right ends of left and right. */
22693 result->width = (min (left->x + left->width, right->x + right->width)
22694 - result->x);
22695
22696 /* Same game for Y. */
22697 if (r1->y < r2->y)
22698 upper = r1, lower = r2;
22699 else
22700 upper = r2, lower = r1;
22701
22702 /* The upper end of the intersection is lower.y0, if this is inside
22703 of upper. Otherwise, there is no intersection. */
22704 if (lower->y <= upper->y + upper->height)
22705 {
22706 result->y = lower->y;
22707
22708 /* The lower end of the intersection is the minimum of the lower
22709 ends of upper and lower. */
22710 result->height = (min (lower->y + lower->height,
22711 upper->y + upper->height)
22712 - result->y);
22713 intersection_p = 1;
22714 }
22715 }
22716
22717 return intersection_p;
22718 }
22719
22720 #endif /* HAVE_WINDOW_SYSTEM */
22721
22722 \f
22723 /***********************************************************************
22724 Initialization
22725 ***********************************************************************/
22726
22727 void
22728 syms_of_xdisp ()
22729 {
22730 Vwith_echo_area_save_vector = Qnil;
22731 staticpro (&Vwith_echo_area_save_vector);
22732
22733 Vmessage_stack = Qnil;
22734 staticpro (&Vmessage_stack);
22735
22736 Qinhibit_redisplay = intern ("inhibit-redisplay");
22737 staticpro (&Qinhibit_redisplay);
22738
22739 message_dolog_marker1 = Fmake_marker ();
22740 staticpro (&message_dolog_marker1);
22741 message_dolog_marker2 = Fmake_marker ();
22742 staticpro (&message_dolog_marker2);
22743 message_dolog_marker3 = Fmake_marker ();
22744 staticpro (&message_dolog_marker3);
22745
22746 #if GLYPH_DEBUG
22747 defsubr (&Sdump_frame_glyph_matrix);
22748 defsubr (&Sdump_glyph_matrix);
22749 defsubr (&Sdump_glyph_row);
22750 defsubr (&Sdump_tool_bar_row);
22751 defsubr (&Strace_redisplay);
22752 defsubr (&Strace_to_stderr);
22753 #endif
22754 #ifdef HAVE_WINDOW_SYSTEM
22755 defsubr (&Stool_bar_lines_needed);
22756 defsubr (&Slookup_image_map);
22757 #endif
22758 defsubr (&Sformat_mode_line);
22759
22760 staticpro (&Qmenu_bar_update_hook);
22761 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
22762
22763 staticpro (&Qoverriding_terminal_local_map);
22764 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
22765
22766 staticpro (&Qoverriding_local_map);
22767 Qoverriding_local_map = intern ("overriding-local-map");
22768
22769 staticpro (&Qwindow_scroll_functions);
22770 Qwindow_scroll_functions = intern ("window-scroll-functions");
22771
22772 staticpro (&Qredisplay_end_trigger_functions);
22773 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
22774
22775 staticpro (&Qinhibit_point_motion_hooks);
22776 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
22777
22778 QCdata = intern (":data");
22779 staticpro (&QCdata);
22780 Qdisplay = intern ("display");
22781 staticpro (&Qdisplay);
22782 Qspace_width = intern ("space-width");
22783 staticpro (&Qspace_width);
22784 Qraise = intern ("raise");
22785 staticpro (&Qraise);
22786 Qslice = intern ("slice");
22787 staticpro (&Qslice);
22788 Qspace = intern ("space");
22789 staticpro (&Qspace);
22790 Qmargin = intern ("margin");
22791 staticpro (&Qmargin);
22792 Qpointer = intern ("pointer");
22793 staticpro (&Qpointer);
22794 Qleft_margin = intern ("left-margin");
22795 staticpro (&Qleft_margin);
22796 Qright_margin = intern ("right-margin");
22797 staticpro (&Qright_margin);
22798 Qcenter = intern ("center");
22799 staticpro (&Qcenter);
22800 Qline_height = intern ("line-height");
22801 staticpro (&Qline_height);
22802 QCalign_to = intern (":align-to");
22803 staticpro (&QCalign_to);
22804 QCrelative_width = intern (":relative-width");
22805 staticpro (&QCrelative_width);
22806 QCrelative_height = intern (":relative-height");
22807 staticpro (&QCrelative_height);
22808 QCeval = intern (":eval");
22809 staticpro (&QCeval);
22810 QCpropertize = intern (":propertize");
22811 staticpro (&QCpropertize);
22812 QCfile = intern (":file");
22813 staticpro (&QCfile);
22814 Qfontified = intern ("fontified");
22815 staticpro (&Qfontified);
22816 Qfontification_functions = intern ("fontification-functions");
22817 staticpro (&Qfontification_functions);
22818 Qtrailing_whitespace = intern ("trailing-whitespace");
22819 staticpro (&Qtrailing_whitespace);
22820 Qescape_glyph = intern ("escape-glyph");
22821 staticpro (&Qescape_glyph);
22822 Qnobreak_space = intern ("nobreak-space");
22823 staticpro (&Qnobreak_space);
22824 Qimage = intern ("image");
22825 staticpro (&Qimage);
22826 QCmap = intern (":map");
22827 staticpro (&QCmap);
22828 QCpointer = intern (":pointer");
22829 staticpro (&QCpointer);
22830 Qrect = intern ("rect");
22831 staticpro (&Qrect);
22832 Qcircle = intern ("circle");
22833 staticpro (&Qcircle);
22834 Qpoly = intern ("poly");
22835 staticpro (&Qpoly);
22836 Qmessage_truncate_lines = intern ("message-truncate-lines");
22837 staticpro (&Qmessage_truncate_lines);
22838 Qgrow_only = intern ("grow-only");
22839 staticpro (&Qgrow_only);
22840 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
22841 staticpro (&Qinhibit_menubar_update);
22842 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
22843 staticpro (&Qinhibit_eval_during_redisplay);
22844 Qposition = intern ("position");
22845 staticpro (&Qposition);
22846 Qbuffer_position = intern ("buffer-position");
22847 staticpro (&Qbuffer_position);
22848 Qobject = intern ("object");
22849 staticpro (&Qobject);
22850 Qbar = intern ("bar");
22851 staticpro (&Qbar);
22852 Qhbar = intern ("hbar");
22853 staticpro (&Qhbar);
22854 Qbox = intern ("box");
22855 staticpro (&Qbox);
22856 Qhollow = intern ("hollow");
22857 staticpro (&Qhollow);
22858 Qhand = intern ("hand");
22859 staticpro (&Qhand);
22860 Qarrow = intern ("arrow");
22861 staticpro (&Qarrow);
22862 Qtext = intern ("text");
22863 staticpro (&Qtext);
22864 Qrisky_local_variable = intern ("risky-local-variable");
22865 staticpro (&Qrisky_local_variable);
22866 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
22867 staticpro (&Qinhibit_free_realized_faces);
22868
22869 list_of_error = Fcons (Fcons (intern ("error"),
22870 Fcons (intern ("void-variable"), Qnil)),
22871 Qnil);
22872 staticpro (&list_of_error);
22873
22874 Qlast_arrow_position = intern ("last-arrow-position");
22875 staticpro (&Qlast_arrow_position);
22876 Qlast_arrow_string = intern ("last-arrow-string");
22877 staticpro (&Qlast_arrow_string);
22878
22879 Qoverlay_arrow_string = intern ("overlay-arrow-string");
22880 staticpro (&Qoverlay_arrow_string);
22881 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
22882 staticpro (&Qoverlay_arrow_bitmap);
22883
22884 echo_buffer[0] = echo_buffer[1] = Qnil;
22885 staticpro (&echo_buffer[0]);
22886 staticpro (&echo_buffer[1]);
22887
22888 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
22889 staticpro (&echo_area_buffer[0]);
22890 staticpro (&echo_area_buffer[1]);
22891
22892 Vmessages_buffer_name = build_string ("*Messages*");
22893 staticpro (&Vmessages_buffer_name);
22894
22895 mode_line_proptrans_alist = Qnil;
22896 staticpro (&mode_line_proptrans_alist);
22897 mode_line_string_list = Qnil;
22898 staticpro (&mode_line_string_list);
22899 mode_line_string_face = Qnil;
22900 staticpro (&mode_line_string_face);
22901 mode_line_string_face_prop = Qnil;
22902 staticpro (&mode_line_string_face_prop);
22903 Vmode_line_unwind_vector = Qnil;
22904 staticpro (&Vmode_line_unwind_vector);
22905
22906 help_echo_string = Qnil;
22907 staticpro (&help_echo_string);
22908 help_echo_object = Qnil;
22909 staticpro (&help_echo_object);
22910 help_echo_window = Qnil;
22911 staticpro (&help_echo_window);
22912 previous_help_echo_string = Qnil;
22913 staticpro (&previous_help_echo_string);
22914 help_echo_pos = -1;
22915
22916 #ifdef HAVE_WINDOW_SYSTEM
22917 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
22918 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
22919 For example, if a block cursor is over a tab, it will be drawn as
22920 wide as that tab on the display. */);
22921 x_stretch_cursor_p = 0;
22922 #endif
22923
22924 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
22925 doc: /* *Non-nil means highlight trailing whitespace.
22926 The face used for trailing whitespace is `trailing-whitespace'. */);
22927 Vshow_trailing_whitespace = Qnil;
22928
22929 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
22930 doc: /* *Control highlighting of nobreak space and soft hyphen.
22931 A value of t means highlight the character itself (for nobreak space,
22932 use face `nobreak-space').
22933 A value of nil means no highlighting.
22934 Other values mean display the escape glyph followed by an ordinary
22935 space or ordinary hyphen. */);
22936 Vnobreak_char_display = Qt;
22937
22938 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
22939 doc: /* *The pointer shape to show in void text areas.
22940 A value of nil means to show the text pointer. Other options are `arrow',
22941 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
22942 Vvoid_text_area_pointer = Qarrow;
22943
22944 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
22945 doc: /* Non-nil means don't actually do any redisplay.
22946 This is used for internal purposes. */);
22947 Vinhibit_redisplay = Qnil;
22948
22949 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
22950 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
22951 Vglobal_mode_string = Qnil;
22952
22953 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
22954 doc: /* Marker for where to display an arrow on top of the buffer text.
22955 This must be the beginning of a line in order to work.
22956 See also `overlay-arrow-string'. */);
22957 Voverlay_arrow_position = Qnil;
22958
22959 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
22960 doc: /* String to display as an arrow in non-window frames.
22961 See also `overlay-arrow-position'. */);
22962 Voverlay_arrow_string = build_string ("=>");
22963
22964 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
22965 doc: /* List of variables (symbols) which hold markers for overlay arrows.
22966 The symbols on this list are examined during redisplay to determine
22967 where to display overlay arrows. */);
22968 Voverlay_arrow_variable_list
22969 = Fcons (intern ("overlay-arrow-position"), Qnil);
22970
22971 DEFVAR_INT ("scroll-step", &scroll_step,
22972 doc: /* *The number of lines to try scrolling a window by when point moves out.
22973 If that fails to bring point back on frame, point is centered instead.
22974 If this is zero, point is always centered after it moves off frame.
22975 If you want scrolling to always be a line at a time, you should set
22976 `scroll-conservatively' to a large value rather than set this to 1. */);
22977
22978 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
22979 doc: /* *Scroll up to this many lines, to bring point back on screen.
22980 A value of zero means to scroll the text to center point vertically
22981 in the window. */);
22982 scroll_conservatively = 0;
22983
22984 DEFVAR_INT ("scroll-margin", &scroll_margin,
22985 doc: /* *Number of lines of margin at the top and bottom of a window.
22986 Recenter the window whenever point gets within this many lines
22987 of the top or bottom of the window. */);
22988 scroll_margin = 0;
22989
22990 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
22991 doc: /* Pixels per inch on current display.
22992 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
22993 Vdisplay_pixels_per_inch = make_float (72.0);
22994
22995 #if GLYPH_DEBUG
22996 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
22997 #endif
22998
22999 DEFVAR_BOOL ("truncate-partial-width-windows",
23000 &truncate_partial_width_windows,
23001 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23002 truncate_partial_width_windows = 1;
23003
23004 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23005 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23006 Any other value means to use the appropriate face, `mode-line',
23007 `header-line', or `menu' respectively. */);
23008 mode_line_inverse_video = 1;
23009
23010 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23011 doc: /* *Maximum buffer size for which line number should be displayed.
23012 If the buffer is bigger than this, the line number does not appear
23013 in the mode line. A value of nil means no limit. */);
23014 Vline_number_display_limit = Qnil;
23015
23016 DEFVAR_INT ("line-number-display-limit-width",
23017 &line_number_display_limit_width,
23018 doc: /* *Maximum line width (in characters) for line number display.
23019 If the average length of the lines near point is bigger than this, then the
23020 line number may be omitted from the mode line. */);
23021 line_number_display_limit_width = 200;
23022
23023 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23024 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23025 highlight_nonselected_windows = 0;
23026
23027 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23028 doc: /* Non-nil if more than one frame is visible on this display.
23029 Minibuffer-only frames don't count, but iconified frames do.
23030 This variable is not guaranteed to be accurate except while processing
23031 `frame-title-format' and `icon-title-format'. */);
23032
23033 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23034 doc: /* Template for displaying the title bar of visible frames.
23035 \(Assuming the window manager supports this feature.)
23036 This variable has the same structure as `mode-line-format' (which see),
23037 and is used only on frames for which no explicit name has been set
23038 \(see `modify-frame-parameters'). */);
23039
23040 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23041 doc: /* Template for displaying the title bar of an iconified frame.
23042 \(Assuming the window manager supports this feature.)
23043 This variable has the same structure as `mode-line-format' (which see),
23044 and is used only on frames for which no explicit name has been set
23045 \(see `modify-frame-parameters'). */);
23046 Vicon_title_format
23047 = Vframe_title_format
23048 = Fcons (intern ("multiple-frames"),
23049 Fcons (build_string ("%b"),
23050 Fcons (Fcons (empty_string,
23051 Fcons (intern ("invocation-name"),
23052 Fcons (build_string ("@"),
23053 Fcons (intern ("system-name"),
23054 Qnil)))),
23055 Qnil)));
23056
23057 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23058 doc: /* Maximum number of lines to keep in the message log buffer.
23059 If nil, disable message logging. If t, log messages but don't truncate
23060 the buffer when it becomes large. */);
23061 Vmessage_log_max = make_number (50);
23062
23063 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23064 doc: /* Functions called before redisplay, if window sizes have changed.
23065 The value should be a list of functions that take one argument.
23066 Just before redisplay, for each frame, if any of its windows have changed
23067 size since the last redisplay, or have been split or deleted,
23068 all the functions in the list are called, with the frame as argument. */);
23069 Vwindow_size_change_functions = Qnil;
23070
23071 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23072 doc: /* List of functions to call before redisplaying a window with scrolling.
23073 Each function is called with two arguments, the window
23074 and its new display-start position. Note that the value of `window-end'
23075 is not valid when these functions are called. */);
23076 Vwindow_scroll_functions = Qnil;
23077
23078 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23079 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23080 mouse_autoselect_window = 0;
23081
23082 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23083 doc: /* *Non-nil means automatically resize tool-bars.
23084 This increases a tool-bar's height if not all tool-bar items are visible.
23085 It decreases a tool-bar's height when it would display blank lines
23086 otherwise. */);
23087 auto_resize_tool_bars_p = 1;
23088
23089 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23090 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23091 auto_raise_tool_bar_buttons_p = 1;
23092
23093 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23094 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23095 make_cursor_line_fully_visible_p = 1;
23096
23097 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23098 doc: /* *Margin around tool-bar buttons in pixels.
23099 If an integer, use that for both horizontal and vertical margins.
23100 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
23101 HORZ specifying the horizontal margin, and VERT specifying the
23102 vertical margin. */);
23103 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
23104
23105 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
23106 doc: /* *Relief thickness of tool-bar buttons. */);
23107 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
23108
23109 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
23110 doc: /* List of functions to call to fontify regions of text.
23111 Each function is called with one argument POS. Functions must
23112 fontify a region starting at POS in the current buffer, and give
23113 fontified regions the property `fontified'. */);
23114 Vfontification_functions = Qnil;
23115 Fmake_variable_buffer_local (Qfontification_functions);
23116
23117 DEFVAR_BOOL ("unibyte-display-via-language-environment",
23118 &unibyte_display_via_language_environment,
23119 doc: /* *Non-nil means display unibyte text according to language environment.
23120 Specifically this means that unibyte non-ASCII characters
23121 are displayed by converting them to the equivalent multibyte characters
23122 according to the current language environment. As a result, they are
23123 displayed according to the current fontset. */);
23124 unibyte_display_via_language_environment = 0;
23125
23126 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
23127 doc: /* *Maximum height for resizing mini-windows.
23128 If a float, it specifies a fraction of the mini-window frame's height.
23129 If an integer, it specifies a number of lines. */);
23130 Vmax_mini_window_height = make_float (0.25);
23131
23132 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
23133 doc: /* *How to resize mini-windows.
23134 A value of nil means don't automatically resize mini-windows.
23135 A value of t means resize them to fit the text displayed in them.
23136 A value of `grow-only', the default, means let mini-windows grow
23137 only, until their display becomes empty, at which point the windows
23138 go back to their normal size. */);
23139 Vresize_mini_windows = Qgrow_only;
23140
23141 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
23142 doc: /* Alist specifying how to blink the cursor off.
23143 Each element has the form (ON-STATE . OFF-STATE). Whenever the
23144 `cursor-type' frame-parameter or variable equals ON-STATE,
23145 comparing using `equal', Emacs uses OFF-STATE to specify
23146 how to blink it off. */);
23147 Vblink_cursor_alist = Qnil;
23148
23149 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
23150 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
23151 automatic_hscrolling_p = 1;
23152
23153 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
23154 doc: /* *How many columns away from the window edge point is allowed to get
23155 before automatic hscrolling will horizontally scroll the window. */);
23156 hscroll_margin = 5;
23157
23158 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
23159 doc: /* *How many columns to scroll the window when point gets too close to the edge.
23160 When point is less than `automatic-hscroll-margin' columns from the window
23161 edge, automatic hscrolling will scroll the window by the amount of columns
23162 determined by this variable. If its value is a positive integer, scroll that
23163 many columns. If it's a positive floating-point number, it specifies the
23164 fraction of the window's width to scroll. If it's nil or zero, point will be
23165 centered horizontally after the scroll. Any other value, including negative
23166 numbers, are treated as if the value were zero.
23167
23168 Automatic hscrolling always moves point outside the scroll margin, so if
23169 point was more than scroll step columns inside the margin, the window will
23170 scroll more than the value given by the scroll step.
23171
23172 Note that the lower bound for automatic hscrolling specified by `scroll-left'
23173 and `scroll-right' overrides this variable's effect. */);
23174 Vhscroll_step = make_number (0);
23175
23176 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
23177 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
23178 Bind this around calls to `message' to let it take effect. */);
23179 message_truncate_lines = 0;
23180
23181 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
23182 doc: /* Normal hook run to update the menu bar definitions.
23183 Redisplay runs this hook before it redisplays the menu bar.
23184 This is used to update submenus such as Buffers,
23185 whose contents depend on various data. */);
23186 Vmenu_bar_update_hook = Qnil;
23187
23188 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
23189 doc: /* Non-nil means don't update menu bars. Internal use only. */);
23190 inhibit_menubar_update = 0;
23191
23192 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
23193 doc: /* Non-nil means don't eval Lisp during redisplay. */);
23194 inhibit_eval_during_redisplay = 0;
23195
23196 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
23197 doc: /* Non-nil means don't free realized faces. Internal use only. */);
23198 inhibit_free_realized_faces = 0;
23199
23200 #if GLYPH_DEBUG
23201 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
23202 doc: /* Inhibit try_window_id display optimization. */);
23203 inhibit_try_window_id = 0;
23204
23205 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
23206 doc: /* Inhibit try_window_reusing display optimization. */);
23207 inhibit_try_window_reusing = 0;
23208
23209 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
23210 doc: /* Inhibit try_cursor_movement display optimization. */);
23211 inhibit_try_cursor_movement = 0;
23212 #endif /* GLYPH_DEBUG */
23213 }
23214
23215
23216 /* Initialize this module when Emacs starts. */
23217
23218 void
23219 init_xdisp ()
23220 {
23221 Lisp_Object root_window;
23222 struct window *mini_w;
23223
23224 current_header_line_height = current_mode_line_height = -1;
23225
23226 CHARPOS (this_line_start_pos) = 0;
23227
23228 mini_w = XWINDOW (minibuf_window);
23229 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
23230
23231 if (!noninteractive)
23232 {
23233 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
23234 int i;
23235
23236 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
23237 set_window_height (root_window,
23238 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
23239 0);
23240 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
23241 set_window_height (minibuf_window, 1, 0);
23242
23243 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
23244 mini_w->total_cols = make_number (FRAME_COLS (f));
23245
23246 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
23247 scratch_glyph_row.glyphs[TEXT_AREA + 1]
23248 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
23249
23250 /* The default ellipsis glyphs `...'. */
23251 for (i = 0; i < 3; ++i)
23252 default_invis_vector[i] = make_number ('.');
23253 }
23254
23255 {
23256 /* Allocate the buffer for frame titles.
23257 Also used for `format-mode-line'. */
23258 int size = 100;
23259 mode_line_noprop_buf = (char *) xmalloc (size);
23260 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
23261 mode_line_noprop_ptr = mode_line_noprop_buf;
23262 mode_line_target = MODE_LINE_DISPLAY;
23263 }
23264
23265 help_echo_showing_p = 0;
23266 }
23267
23268
23269 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
23270 (do not change this comment) */