]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(Fwindow_end): Fix recent change.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2 Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
3 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
24
25 Redisplay.
26
27 Emacs separates the task of updating the display from code
28 modifying global state, e.g. buffer text. This way functions
29 operating on buffers don't also have to be concerned with updating
30 the display.
31
32 Updating the display is triggered by the Lisp interpreter when it
33 decides it's time to do it. This is done either automatically for
34 you as part of the interpreter's command loop or as the result of
35 calling Lisp functions like `sit-for'. The C function `redisplay'
36 in xdisp.c is the only entry into the inner redisplay code. (Or,
37 let's say almost---see the description of direct update
38 operations, below.)
39
40 The following diagram shows how redisplay code is invoked. As you
41 can see, Lisp calls redisplay and vice versa. Under window systems
42 like X, some portions of the redisplay code are also called
43 asynchronously during mouse movement or expose events. It is very
44 important that these code parts do NOT use the C library (malloc,
45 free) because many C libraries under Unix are not reentrant. They
46 may also NOT call functions of the Lisp interpreter which could
47 change the interpreter's state. If you don't follow these rules,
48 you will encounter bugs which are very hard to explain.
49
50 (Direct functions, see below)
51 direct_output_for_insert,
52 direct_forward_char (dispnew.c)
53 +---------------------------------+
54 | |
55 | V
56 +--------------+ redisplay +----------------+
57 | Lisp machine |---------------->| Redisplay code |<--+
58 +--------------+ (xdisp.c) +----------------+ |
59 ^ | |
60 +----------------------------------+ |
61 Don't use this path when called |
62 asynchronously! |
63 |
64 expose_window (asynchronous) |
65 |
66 X expose events -----+
67
68 What does redisplay do? Obviously, it has to figure out somehow what
69 has been changed since the last time the display has been updated,
70 and to make these changes visible. Preferably it would do that in
71 a moderately intelligent way, i.e. fast.
72
73 Changes in buffer text can be deduced from window and buffer
74 structures, and from some global variables like `beg_unchanged' and
75 `end_unchanged'. The contents of the display are additionally
76 recorded in a `glyph matrix', a two-dimensional matrix of glyph
77 structures. Each row in such a matrix corresponds to a line on the
78 display, and each glyph in a row corresponds to a column displaying
79 a character, an image, or what else. This matrix is called the
80 `current glyph matrix' or `current matrix' in redisplay
81 terminology.
82
83 For buffer parts that have been changed since the last update, a
84 second glyph matrix is constructed, the so called `desired glyph
85 matrix' or short `desired matrix'. Current and desired matrix are
86 then compared to find a cheap way to update the display, e.g. by
87 reusing part of the display by scrolling lines.
88
89
90 Direct operations.
91
92 You will find a lot of redisplay optimizations when you start
93 looking at the innards of redisplay. The overall goal of all these
94 optimizations is to make redisplay fast because it is done
95 frequently.
96
97 Two optimizations are not found in xdisp.c. These are the direct
98 operations mentioned above. As the name suggests they follow a
99 different principle than the rest of redisplay. Instead of
100 building a desired matrix and then comparing it with the current
101 display, they perform their actions directly on the display and on
102 the current matrix.
103
104 One direct operation updates the display after one character has
105 been entered. The other one moves the cursor by one position
106 forward or backward. You find these functions under the names
107 `direct_output_for_insert' and `direct_output_forward_char' in
108 dispnew.c.
109
110
111 Desired matrices.
112
113 Desired matrices are always built per Emacs window. The function
114 `display_line' is the central function to look at if you are
115 interested. It constructs one row in a desired matrix given an
116 iterator structure containing both a buffer position and a
117 description of the environment in which the text is to be
118 displayed. But this is too early, read on.
119
120 Characters and pixmaps displayed for a range of buffer text depend
121 on various settings of buffers and windows, on overlays and text
122 properties, on display tables, on selective display. The good news
123 is that all this hairy stuff is hidden behind a small set of
124 interface functions taking an iterator structure (struct it)
125 argument.
126
127 Iteration over things to be displayed is then simple. It is
128 started by initializing an iterator with a call to init_iterator.
129 Calls to get_next_display_element fill the iterator structure with
130 relevant information about the next thing to display. Calls to
131 set_iterator_to_next move the iterator to the next thing.
132
133 Besides this, an iterator also contains information about the
134 display environment in which glyphs for display elements are to be
135 produced. It has fields for the width and height of the display,
136 the information whether long lines are truncated or continued, a
137 current X and Y position, and lots of other stuff you can better
138 see in dispextern.h.
139
140 Glyphs in a desired matrix are normally constructed in a loop
141 calling get_next_display_element and then produce_glyphs. The call
142 to produce_glyphs will fill the iterator structure with pixel
143 information about the element being displayed and at the same time
144 produce glyphs for it. If the display element fits on the line
145 being displayed, set_iterator_to_next is called next, otherwise the
146 glyphs produced are discarded.
147
148
149 Frame matrices.
150
151 That just couldn't be all, could it? What about terminal types not
152 supporting operations on sub-windows of the screen? To update the
153 display on such a terminal, window-based glyph matrices are not
154 well suited. To be able to reuse part of the display (scrolling
155 lines up and down), we must instead have a view of the whole
156 screen. This is what `frame matrices' are for. They are a trick.
157
158 Frames on terminals like above have a glyph pool. Windows on such
159 a frame sub-allocate their glyph memory from their frame's glyph
160 pool. The frame itself is given its own glyph matrices. By
161 coincidence---or maybe something else---rows in window glyph
162 matrices are slices of corresponding rows in frame matrices. Thus
163 writing to window matrices implicitly updates a frame matrix which
164 provides us with the view of the whole screen that we originally
165 wanted to have without having to move many bytes around. To be
166 honest, there is a little bit more done, but not much more. If you
167 plan to extend that code, take a look at dispnew.c. The function
168 build_frame_matrix is a good starting point. */
169
170 #include <config.h>
171 #include <stdio.h>
172
173 #include "lisp.h"
174 #include "keyboard.h"
175 #include "frame.h"
176 #include "window.h"
177 #include "termchar.h"
178 #include "dispextern.h"
179 #include "buffer.h"
180 #include "charset.h"
181 #include "indent.h"
182 #include "commands.h"
183 #include "keymap.h"
184 #include "macros.h"
185 #include "disptab.h"
186 #include "termhooks.h"
187 #include "intervals.h"
188 #include "coding.h"
189 #include "process.h"
190 #include "region-cache.h"
191 #include "fontset.h"
192 #include "blockinput.h"
193
194 #ifdef HAVE_X_WINDOWS
195 #include "xterm.h"
196 #endif
197 #ifdef WINDOWSNT
198 #include "w32term.h"
199 #endif
200 #ifdef MAC_OS
201 #include "macterm.h"
202 #endif
203
204 #ifndef FRAME_X_OUTPUT
205 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
206 #endif
207
208 #define INFINITY 10000000
209
210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
211 || defined (USE_GTK)
212 extern void set_frame_menubar P_ ((struct frame *f, int, int));
213 extern int pending_menu_activation;
214 #endif
215
216 extern int interrupt_input;
217 extern int command_loop_level;
218
219 extern Lisp_Object do_mouse_tracking;
220
221 extern int minibuffer_auto_raise;
222 extern Lisp_Object Vminibuffer_list;
223
224 extern Lisp_Object Qface;
225 extern Lisp_Object Qmode_line, Qmode_line_inactive, Qheader_line;
226
227 extern Lisp_Object Voverriding_local_map;
228 extern Lisp_Object Voverriding_local_map_menu_flag;
229 extern Lisp_Object Qmenu_item;
230 extern Lisp_Object Qwhen;
231 extern Lisp_Object Qhelp_echo;
232
233 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
234 Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
235 Lisp_Object Qredisplay_end_trigger_functions, Vredisplay_end_trigger_functions;
236 Lisp_Object Qinhibit_point_motion_hooks;
237 Lisp_Object QCeval, QCfile, QCdata, QCpropertize;
238 Lisp_Object Qfontified;
239 Lisp_Object Qgrow_only;
240 Lisp_Object Qinhibit_eval_during_redisplay;
241 Lisp_Object Qbuffer_position, Qposition, Qobject;
242
243 /* Cursor shapes */
244 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
245
246 /* Pointer shapes */
247 Lisp_Object Qarrow, Qhand, Qtext;
248
249 Lisp_Object Qrisky_local_variable;
250
251 /* Holds the list (error). */
252 Lisp_Object list_of_error;
253
254 /* Functions called to fontify regions of text. */
255
256 Lisp_Object Vfontification_functions;
257 Lisp_Object Qfontification_functions;
258
259 /* Non-nil means automatically select any window when the mouse
260 cursor moves into it. */
261 Lisp_Object Vmouse_autoselect_window;
262
263 /* Non-zero means draw tool bar buttons raised when the mouse moves
264 over them. */
265
266 int auto_raise_tool_bar_buttons_p;
267
268 /* Non-zero means to reposition window if cursor line is only partially visible. */
269
270 int make_cursor_line_fully_visible_p;
271
272 /* Margin below tool bar in pixels. 0 or nil means no margin.
273 If value is `internal-border-width' or `border-width',
274 the corresponding frame parameter is used. */
275
276 Lisp_Object Vtool_bar_border;
277
278 /* Margin around tool bar buttons in pixels. */
279
280 Lisp_Object Vtool_bar_button_margin;
281
282 /* Thickness of shadow to draw around tool bar buttons. */
283
284 EMACS_INT tool_bar_button_relief;
285
286 /* Non-zero means automatically resize tool-bars so that all tool-bar
287 items are visible, and no blank lines remain. */
288
289 int auto_resize_tool_bars_p;
290
291 /* Non-zero means draw block and hollow cursor as wide as the glyph
292 under it. For example, if a block cursor is over a tab, it will be
293 drawn as wide as that tab on the display. */
294
295 int x_stretch_cursor_p;
296
297 /* Non-nil means don't actually do any redisplay. */
298
299 Lisp_Object Vinhibit_redisplay, Qinhibit_redisplay;
300
301 /* Non-zero means Lisp evaluation during redisplay is inhibited. */
302
303 int inhibit_eval_during_redisplay;
304
305 /* Names of text properties relevant for redisplay. */
306
307 Lisp_Object Qdisplay;
308 extern Lisp_Object Qface, Qinvisible, Qwidth;
309
310 /* Symbols used in text property values. */
311
312 Lisp_Object Vdisplay_pixels_per_inch;
313 Lisp_Object Qspace, QCalign_to, QCrelative_width, QCrelative_height;
314 Lisp_Object Qleft_margin, Qright_margin, Qspace_width, Qraise;
315 Lisp_Object Qslice;
316 Lisp_Object Qcenter;
317 Lisp_Object Qmargin, Qpointer;
318 Lisp_Object Qline_height;
319 extern Lisp_Object Qheight;
320 extern Lisp_Object QCwidth, QCheight, QCascent;
321 extern Lisp_Object Qscroll_bar;
322 extern Lisp_Object Qcursor;
323
324 /* Non-nil means highlight trailing whitespace. */
325
326 Lisp_Object Vshow_trailing_whitespace;
327
328 /* Non-nil means escape non-break space and hyphens. */
329
330 Lisp_Object Vnobreak_char_display;
331
332 #ifdef HAVE_WINDOW_SYSTEM
333 extern Lisp_Object Voverflow_newline_into_fringe;
334
335 /* Test if overflow newline into fringe. Called with iterator IT
336 at or past right window margin, and with IT->current_x set. */
337
338 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) \
339 (!NILP (Voverflow_newline_into_fringe) \
340 && FRAME_WINDOW_P (it->f) \
341 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) > 0 \
342 && it->current_x == it->last_visible_x)
343
344 #endif /* HAVE_WINDOW_SYSTEM */
345
346 /* Non-nil means show the text cursor in void text areas
347 i.e. in blank areas after eol and eob. This used to be
348 the default in 21.3. */
349
350 Lisp_Object Vvoid_text_area_pointer;
351
352 /* Name of the face used to highlight trailing whitespace. */
353
354 Lisp_Object Qtrailing_whitespace;
355
356 /* Name and number of the face used to highlight escape glyphs. */
357
358 Lisp_Object Qescape_glyph;
359
360 /* Name and number of the face used to highlight non-breaking spaces. */
361
362 Lisp_Object Qnobreak_space;
363
364 /* The symbol `image' which is the car of the lists used to represent
365 images in Lisp. */
366
367 Lisp_Object Qimage;
368
369 /* The image map types. */
370 Lisp_Object QCmap, QCpointer;
371 Lisp_Object Qrect, Qcircle, Qpoly;
372
373 /* Non-zero means print newline to stdout before next mini-buffer
374 message. */
375
376 int noninteractive_need_newline;
377
378 /* Non-zero means print newline to message log before next message. */
379
380 static int message_log_need_newline;
381
382 /* Three markers that message_dolog uses.
383 It could allocate them itself, but that causes trouble
384 in handling memory-full errors. */
385 static Lisp_Object message_dolog_marker1;
386 static Lisp_Object message_dolog_marker2;
387 static Lisp_Object message_dolog_marker3;
388 \f
389 /* The buffer position of the first character appearing entirely or
390 partially on the line of the selected window which contains the
391 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
392 redisplay optimization in redisplay_internal. */
393
394 static struct text_pos this_line_start_pos;
395
396 /* Number of characters past the end of the line above, including the
397 terminating newline. */
398
399 static struct text_pos this_line_end_pos;
400
401 /* The vertical positions and the height of this line. */
402
403 static int this_line_vpos;
404 static int this_line_y;
405 static int this_line_pixel_height;
406
407 /* X position at which this display line starts. Usually zero;
408 negative if first character is partially visible. */
409
410 static int this_line_start_x;
411
412 /* Buffer that this_line_.* variables are referring to. */
413
414 static struct buffer *this_line_buffer;
415
416 /* Nonzero means truncate lines in all windows less wide than the
417 frame. */
418
419 int truncate_partial_width_windows;
420
421 /* A flag to control how to display unibyte 8-bit character. */
422
423 int unibyte_display_via_language_environment;
424
425 /* Nonzero means we have more than one non-mini-buffer-only frame.
426 Not guaranteed to be accurate except while parsing
427 frame-title-format. */
428
429 int multiple_frames;
430
431 Lisp_Object Vglobal_mode_string;
432
433
434 /* List of variables (symbols) which hold markers for overlay arrows.
435 The symbols on this list are examined during redisplay to determine
436 where to display overlay arrows. */
437
438 Lisp_Object Voverlay_arrow_variable_list;
439
440 /* Marker for where to display an arrow on top of the buffer text. */
441
442 Lisp_Object Voverlay_arrow_position;
443
444 /* String to display for the arrow. Only used on terminal frames. */
445
446 Lisp_Object Voverlay_arrow_string;
447
448 /* Values of those variables at last redisplay are stored as
449 properties on `overlay-arrow-position' symbol. However, if
450 Voverlay_arrow_position is a marker, last-arrow-position is its
451 numerical position. */
452
453 Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
454
455 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
456 properties on a symbol in overlay-arrow-variable-list. */
457
458 Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
459
460 /* Like mode-line-format, but for the title bar on a visible frame. */
461
462 Lisp_Object Vframe_title_format;
463
464 /* Like mode-line-format, but for the title bar on an iconified frame. */
465
466 Lisp_Object Vicon_title_format;
467
468 /* List of functions to call when a window's size changes. These
469 functions get one arg, a frame on which one or more windows' sizes
470 have changed. */
471
472 static Lisp_Object Vwindow_size_change_functions;
473
474 Lisp_Object Qmenu_bar_update_hook, Vmenu_bar_update_hook;
475
476 /* Nonzero if an overlay arrow has been displayed in this window. */
477
478 static int overlay_arrow_seen;
479
480 /* Nonzero means highlight the region even in nonselected windows. */
481
482 int highlight_nonselected_windows;
483
484 /* If cursor motion alone moves point off frame, try scrolling this
485 many lines up or down if that will bring it back. */
486
487 static EMACS_INT scroll_step;
488
489 /* Nonzero means scroll just far enough to bring point back on the
490 screen, when appropriate. */
491
492 static EMACS_INT scroll_conservatively;
493
494 /* Recenter the window whenever point gets within this many lines of
495 the top or bottom of the window. This value is translated into a
496 pixel value by multiplying it with FRAME_LINE_HEIGHT, which means
497 that there is really a fixed pixel height scroll margin. */
498
499 EMACS_INT scroll_margin;
500
501 /* Number of windows showing the buffer of the selected window (or
502 another buffer with the same base buffer). keyboard.c refers to
503 this. */
504
505 int buffer_shared;
506
507 /* Vector containing glyphs for an ellipsis `...'. */
508
509 static Lisp_Object default_invis_vector[3];
510
511 /* Zero means display the mode-line/header-line/menu-bar in the default face
512 (this slightly odd definition is for compatibility with previous versions
513 of emacs), non-zero means display them using their respective faces.
514
515 This variable is deprecated. */
516
517 int mode_line_inverse_video;
518
519 /* Prompt to display in front of the mini-buffer contents. */
520
521 Lisp_Object minibuf_prompt;
522
523 /* Width of current mini-buffer prompt. Only set after display_line
524 of the line that contains the prompt. */
525
526 int minibuf_prompt_width;
527
528 /* This is the window where the echo area message was displayed. It
529 is always a mini-buffer window, but it may not be the same window
530 currently active as a mini-buffer. */
531
532 Lisp_Object echo_area_window;
533
534 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
535 pushes the current message and the value of
536 message_enable_multibyte on the stack, the function restore_message
537 pops the stack and displays MESSAGE again. */
538
539 Lisp_Object Vmessage_stack;
540
541 /* Nonzero means multibyte characters were enabled when the echo area
542 message was specified. */
543
544 int message_enable_multibyte;
545
546 /* Nonzero if we should redraw the mode lines on the next redisplay. */
547
548 int update_mode_lines;
549
550 /* Nonzero if window sizes or contents have changed since last
551 redisplay that finished. */
552
553 int windows_or_buffers_changed;
554
555 /* Nonzero means a frame's cursor type has been changed. */
556
557 int cursor_type_changed;
558
559 /* Nonzero after display_mode_line if %l was used and it displayed a
560 line number. */
561
562 int line_number_displayed;
563
564 /* Maximum buffer size for which to display line numbers. */
565
566 Lisp_Object Vline_number_display_limit;
567
568 /* Line width to consider when repositioning for line number display. */
569
570 static EMACS_INT line_number_display_limit_width;
571
572 /* Number of lines to keep in the message log buffer. t means
573 infinite. nil means don't log at all. */
574
575 Lisp_Object Vmessage_log_max;
576
577 /* The name of the *Messages* buffer, a string. */
578
579 static Lisp_Object Vmessages_buffer_name;
580
581 /* Index 0 is the buffer that holds the current (desired) echo area message,
582 or nil if none is desired right now.
583
584 Index 1 is the buffer that holds the previously displayed echo area message,
585 or nil to indicate no message. This is normally what's on the screen now.
586
587 These two can point to the same buffer. That happens when the last
588 message output by the user (or made by echoing) has been displayed. */
589
590 Lisp_Object echo_area_buffer[2];
591
592 /* Permanent pointers to the two buffers that are used for echo area
593 purposes. Once the two buffers are made, and their pointers are
594 placed here, these two slots remain unchanged unless those buffers
595 need to be created afresh. */
596
597 static Lisp_Object echo_buffer[2];
598
599 /* A vector saved used in with_area_buffer to reduce consing. */
600
601 static Lisp_Object Vwith_echo_area_save_vector;
602
603 /* Non-zero means display_echo_area should display the last echo area
604 message again. Set by redisplay_preserve_echo_area. */
605
606 static int display_last_displayed_message_p;
607
608 /* Nonzero if echo area is being used by print; zero if being used by
609 message. */
610
611 int message_buf_print;
612
613 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
614
615 Lisp_Object Qinhibit_menubar_update;
616 int inhibit_menubar_update;
617
618 /* When evaluating expressions from menu bar items (enable conditions,
619 for instance), this is the frame they are being processed for. */
620
621 Lisp_Object Vmenu_updating_frame;
622
623 /* Maximum height for resizing mini-windows. Either a float
624 specifying a fraction of the available height, or an integer
625 specifying a number of lines. */
626
627 Lisp_Object Vmax_mini_window_height;
628
629 /* Non-zero means messages should be displayed with truncated
630 lines instead of being continued. */
631
632 int message_truncate_lines;
633 Lisp_Object Qmessage_truncate_lines;
634
635 /* Set to 1 in clear_message to make redisplay_internal aware
636 of an emptied echo area. */
637
638 static int message_cleared_p;
639
640 /* How to blink the default frame cursor off. */
641 Lisp_Object Vblink_cursor_alist;
642
643 /* A scratch glyph row with contents used for generating truncation
644 glyphs. Also used in direct_output_for_insert. */
645
646 #define MAX_SCRATCH_GLYPHS 100
647 struct glyph_row scratch_glyph_row;
648 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
649
650 /* Ascent and height of the last line processed by move_it_to. */
651
652 static int last_max_ascent, last_height;
653
654 /* Non-zero if there's a help-echo in the echo area. */
655
656 int help_echo_showing_p;
657
658 /* If >= 0, computed, exact values of mode-line and header-line height
659 to use in the macros CURRENT_MODE_LINE_HEIGHT and
660 CURRENT_HEADER_LINE_HEIGHT. */
661
662 int current_mode_line_height, current_header_line_height;
663
664 /* The maximum distance to look ahead for text properties. Values
665 that are too small let us call compute_char_face and similar
666 functions too often which is expensive. Values that are too large
667 let us call compute_char_face and alike too often because we
668 might not be interested in text properties that far away. */
669
670 #define TEXT_PROP_DISTANCE_LIMIT 100
671
672 #if GLYPH_DEBUG
673
674 /* Variables to turn off display optimizations from Lisp. */
675
676 int inhibit_try_window_id, inhibit_try_window_reusing;
677 int inhibit_try_cursor_movement;
678
679 /* Non-zero means print traces of redisplay if compiled with
680 GLYPH_DEBUG != 0. */
681
682 int trace_redisplay_p;
683
684 #endif /* GLYPH_DEBUG */
685
686 #ifdef DEBUG_TRACE_MOVE
687 /* Non-zero means trace with TRACE_MOVE to stderr. */
688 int trace_move;
689
690 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
691 #else
692 #define TRACE_MOVE(x) (void) 0
693 #endif
694
695 /* Non-zero means automatically scroll windows horizontally to make
696 point visible. */
697
698 int automatic_hscrolling_p;
699
700 /* How close to the margin can point get before the window is scrolled
701 horizontally. */
702 EMACS_INT hscroll_margin;
703
704 /* How much to scroll horizontally when point is inside the above margin. */
705 Lisp_Object Vhscroll_step;
706
707 /* The variable `resize-mini-windows'. If nil, don't resize
708 mini-windows. If t, always resize them to fit the text they
709 display. If `grow-only', let mini-windows grow only until they
710 become empty. */
711
712 Lisp_Object Vresize_mini_windows;
713
714 /* Buffer being redisplayed -- for redisplay_window_error. */
715
716 struct buffer *displayed_buffer;
717
718 /* Space between overline and text. */
719
720 EMACS_INT overline_margin;
721
722 /* Value returned from text property handlers (see below). */
723
724 enum prop_handled
725 {
726 HANDLED_NORMALLY,
727 HANDLED_RECOMPUTE_PROPS,
728 HANDLED_OVERLAY_STRING_CONSUMED,
729 HANDLED_RETURN
730 };
731
732 /* A description of text properties that redisplay is interested
733 in. */
734
735 struct props
736 {
737 /* The name of the property. */
738 Lisp_Object *name;
739
740 /* A unique index for the property. */
741 enum prop_idx idx;
742
743 /* A handler function called to set up iterator IT from the property
744 at IT's current position. Value is used to steer handle_stop. */
745 enum prop_handled (*handler) P_ ((struct it *it));
746 };
747
748 static enum prop_handled handle_face_prop P_ ((struct it *));
749 static enum prop_handled handle_invisible_prop P_ ((struct it *));
750 static enum prop_handled handle_display_prop P_ ((struct it *));
751 static enum prop_handled handle_composition_prop P_ ((struct it *));
752 static enum prop_handled handle_overlay_change P_ ((struct it *));
753 static enum prop_handled handle_fontified_prop P_ ((struct it *));
754
755 /* Properties handled by iterators. */
756
757 static struct props it_props[] =
758 {
759 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
760 /* Handle `face' before `display' because some sub-properties of
761 `display' need to know the face. */
762 {&Qface, FACE_PROP_IDX, handle_face_prop},
763 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
764 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
765 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
766 {NULL, 0, NULL}
767 };
768
769 /* Value is the position described by X. If X is a marker, value is
770 the marker_position of X. Otherwise, value is X. */
771
772 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
773
774 /* Enumeration returned by some move_it_.* functions internally. */
775
776 enum move_it_result
777 {
778 /* Not used. Undefined value. */
779 MOVE_UNDEFINED,
780
781 /* Move ended at the requested buffer position or ZV. */
782 MOVE_POS_MATCH_OR_ZV,
783
784 /* Move ended at the requested X pixel position. */
785 MOVE_X_REACHED,
786
787 /* Move within a line ended at the end of a line that must be
788 continued. */
789 MOVE_LINE_CONTINUED,
790
791 /* Move within a line ended at the end of a line that would
792 be displayed truncated. */
793 MOVE_LINE_TRUNCATED,
794
795 /* Move within a line ended at a line end. */
796 MOVE_NEWLINE_OR_CR
797 };
798
799 /* This counter is used to clear the face cache every once in a while
800 in redisplay_internal. It is incremented for each redisplay.
801 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
802 cleared. */
803
804 #define CLEAR_FACE_CACHE_COUNT 500
805 static int clear_face_cache_count;
806
807 /* Similarly for the image cache. */
808
809 #ifdef HAVE_WINDOW_SYSTEM
810 #define CLEAR_IMAGE_CACHE_COUNT 101
811 static int clear_image_cache_count;
812 #endif
813
814 /* Record the previous terminal frame we displayed. */
815
816 static struct frame *previous_terminal_frame;
817
818 /* Non-zero while redisplay_internal is in progress. */
819
820 int redisplaying_p;
821
822 /* Non-zero means don't free realized faces. Bound while freeing
823 realized faces is dangerous because glyph matrices might still
824 reference them. */
825
826 int inhibit_free_realized_faces;
827 Lisp_Object Qinhibit_free_realized_faces;
828
829 /* If a string, XTread_socket generates an event to display that string.
830 (The display is done in read_char.) */
831
832 Lisp_Object help_echo_string;
833 Lisp_Object help_echo_window;
834 Lisp_Object help_echo_object;
835 int help_echo_pos;
836
837 /* Temporary variable for XTread_socket. */
838
839 Lisp_Object previous_help_echo_string;
840
841 /* Null glyph slice */
842
843 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
844
845 \f
846 /* Function prototypes. */
847
848 static void setup_for_ellipsis P_ ((struct it *, int));
849 static void mark_window_display_accurate_1 P_ ((struct window *, int));
850 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
851 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
852 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
853 static int redisplay_mode_lines P_ ((Lisp_Object, int));
854 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
855
856 #if 0
857 static int invisible_text_between_p P_ ((struct it *, int, int));
858 #endif
859
860 static void pint2str P_ ((char *, int, int));
861 static void pint2hrstr P_ ((char *, int, int));
862 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
863 struct text_pos));
864 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
865 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
866 static void store_mode_line_noprop_char P_ ((char));
867 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
868 static void x_consider_frame_title P_ ((Lisp_Object));
869 static void handle_stop P_ ((struct it *));
870 static int tool_bar_lines_needed P_ ((struct frame *, int *));
871 static int single_display_spec_intangible_p P_ ((Lisp_Object));
872 static void ensure_echo_area_buffers P_ ((void));
873 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
874 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
875 static int with_echo_area_buffer P_ ((struct window *, int,
876 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
877 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
878 static void clear_garbaged_frames P_ ((void));
879 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
881 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
882 static int display_echo_area P_ ((struct window *));
883 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
884 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
885 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
886 static int string_char_and_length P_ ((const unsigned char *, int, int *));
887 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
888 struct text_pos));
889 static int compute_window_start_on_continuation_line P_ ((struct window *));
890 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
891 static void insert_left_trunc_glyphs P_ ((struct it *));
892 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
893 Lisp_Object));
894 static void extend_face_to_end_of_line P_ ((struct it *));
895 static int append_space_for_newline P_ ((struct it *, int));
896 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
897 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
898 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
899 static int trailing_whitespace_p P_ ((int));
900 static int message_log_check_duplicate P_ ((int, int, int, int));
901 static void push_it P_ ((struct it *));
902 static void pop_it P_ ((struct it *));
903 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
904 static void select_frame_for_redisplay P_ ((Lisp_Object));
905 static void redisplay_internal P_ ((int));
906 static int echo_area_display P_ ((int));
907 static void redisplay_windows P_ ((Lisp_Object));
908 static void redisplay_window P_ ((Lisp_Object, int));
909 static Lisp_Object redisplay_window_error ();
910 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
911 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
912 static int update_menu_bar P_ ((struct frame *, int, int));
913 static int try_window_reusing_current_matrix P_ ((struct window *));
914 static int try_window_id P_ ((struct window *));
915 static int display_line P_ ((struct it *));
916 static int display_mode_lines P_ ((struct window *));
917 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
918 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
919 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
920 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
921 static void display_menu_bar P_ ((struct window *));
922 static int display_count_lines P_ ((int, int, int, int, int *));
923 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
924 int, int, struct it *, int, int, int, int));
925 static void compute_line_metrics P_ ((struct it *));
926 static void run_redisplay_end_trigger_hook P_ ((struct it *));
927 static int get_overlay_strings P_ ((struct it *, int));
928 static int get_overlay_strings_1 P_ ((struct it *, int, int));
929 static void next_overlay_string P_ ((struct it *));
930 static void reseat P_ ((struct it *, struct text_pos, int));
931 static void reseat_1 P_ ((struct it *, struct text_pos, int));
932 static void back_to_previous_visible_line_start P_ ((struct it *));
933 void reseat_at_previous_visible_line_start P_ ((struct it *));
934 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
935 static int next_element_from_ellipsis P_ ((struct it *));
936 static int next_element_from_display_vector P_ ((struct it *));
937 static int next_element_from_string P_ ((struct it *));
938 static int next_element_from_c_string P_ ((struct it *));
939 static int next_element_from_buffer P_ ((struct it *));
940 static int next_element_from_composition P_ ((struct it *));
941 static int next_element_from_image P_ ((struct it *));
942 static int next_element_from_stretch P_ ((struct it *));
943 static void load_overlay_strings P_ ((struct it *, int));
944 static int init_from_display_pos P_ ((struct it *, struct window *,
945 struct display_pos *));
946 static void reseat_to_string P_ ((struct it *, unsigned char *,
947 Lisp_Object, int, int, int, int));
948 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
949 int, int, int));
950 void move_it_vertically_backward P_ ((struct it *, int));
951 static void init_to_row_start P_ ((struct it *, struct window *,
952 struct glyph_row *));
953 static int init_to_row_end P_ ((struct it *, struct window *,
954 struct glyph_row *));
955 static void back_to_previous_line_start P_ ((struct it *));
956 static int forward_to_next_line_start P_ ((struct it *, int *));
957 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
958 Lisp_Object, int));
959 static struct text_pos string_pos P_ ((int, Lisp_Object));
960 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
961 static int number_of_chars P_ ((unsigned char *, int));
962 static void compute_stop_pos P_ ((struct it *));
963 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
964 Lisp_Object));
965 static int face_before_or_after_it_pos P_ ((struct it *, int));
966 static int next_overlay_change P_ ((int));
967 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
968 Lisp_Object, struct text_pos *,
969 int));
970 static int underlying_face_id P_ ((struct it *));
971 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
972 struct window *));
973
974 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
975 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
976
977 #ifdef HAVE_WINDOW_SYSTEM
978
979 static void update_tool_bar P_ ((struct frame *, int));
980 static void build_desired_tool_bar_string P_ ((struct frame *f));
981 static int redisplay_tool_bar P_ ((struct frame *));
982 static void display_tool_bar_line P_ ((struct it *, int));
983 static void notice_overwritten_cursor P_ ((struct window *,
984 enum glyph_row_area,
985 int, int, int, int));
986
987
988
989 #endif /* HAVE_WINDOW_SYSTEM */
990
991 \f
992 /***********************************************************************
993 Window display dimensions
994 ***********************************************************************/
995
996 /* Return the bottom boundary y-position for text lines in window W.
997 This is the first y position at which a line cannot start.
998 It is relative to the top of the window.
999
1000 This is the height of W minus the height of a mode line, if any. */
1001
1002 INLINE int
1003 window_text_bottom_y (w)
1004 struct window *w;
1005 {
1006 int height = WINDOW_TOTAL_HEIGHT (w);
1007
1008 if (WINDOW_WANTS_MODELINE_P (w))
1009 height -= CURRENT_MODE_LINE_HEIGHT (w);
1010 return height;
1011 }
1012
1013 /* Return the pixel width of display area AREA of window W. AREA < 0
1014 means return the total width of W, not including fringes to
1015 the left and right of the window. */
1016
1017 INLINE int
1018 window_box_width (w, area)
1019 struct window *w;
1020 int area;
1021 {
1022 int cols = XFASTINT (w->total_cols);
1023 int pixels = 0;
1024
1025 if (!w->pseudo_window_p)
1026 {
1027 cols -= WINDOW_SCROLL_BAR_COLS (w);
1028
1029 if (area == TEXT_AREA)
1030 {
1031 if (INTEGERP (w->left_margin_cols))
1032 cols -= XFASTINT (w->left_margin_cols);
1033 if (INTEGERP (w->right_margin_cols))
1034 cols -= XFASTINT (w->right_margin_cols);
1035 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1036 }
1037 else if (area == LEFT_MARGIN_AREA)
1038 {
1039 cols = (INTEGERP (w->left_margin_cols)
1040 ? XFASTINT (w->left_margin_cols) : 0);
1041 pixels = 0;
1042 }
1043 else if (area == RIGHT_MARGIN_AREA)
1044 {
1045 cols = (INTEGERP (w->right_margin_cols)
1046 ? XFASTINT (w->right_margin_cols) : 0);
1047 pixels = 0;
1048 }
1049 }
1050
1051 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1052 }
1053
1054
1055 /* Return the pixel height of the display area of window W, not
1056 including mode lines of W, if any. */
1057
1058 INLINE int
1059 window_box_height (w)
1060 struct window *w;
1061 {
1062 struct frame *f = XFRAME (w->frame);
1063 int height = WINDOW_TOTAL_HEIGHT (w);
1064
1065 xassert (height >= 0);
1066
1067 /* Note: the code below that determines the mode-line/header-line
1068 height is essentially the same as that contained in the macro
1069 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1070 the appropriate glyph row has its `mode_line_p' flag set,
1071 and if it doesn't, uses estimate_mode_line_height instead. */
1072
1073 if (WINDOW_WANTS_MODELINE_P (w))
1074 {
1075 struct glyph_row *ml_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (ml_row && ml_row->mode_line_p)
1080 height -= ml_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1083 }
1084
1085 if (WINDOW_WANTS_HEADER_LINE_P (w))
1086 {
1087 struct glyph_row *hl_row
1088 = (w->current_matrix && w->current_matrix->rows
1089 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1090 : 0);
1091 if (hl_row && hl_row->mode_line_p)
1092 height -= hl_row->height;
1093 else
1094 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1095 }
1096
1097 /* With a very small font and a mode-line that's taller than
1098 default, we might end up with a negative height. */
1099 return max (0, height);
1100 }
1101
1102 /* Return the window-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1105
1106 INLINE int
1107 window_box_left_offset (w, area)
1108 struct window *w;
1109 int area;
1110 {
1111 int x;
1112
1113 if (w->pseudo_window_p)
1114 return 0;
1115
1116 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1117
1118 if (area == TEXT_AREA)
1119 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1120 + window_box_width (w, LEFT_MARGIN_AREA));
1121 else if (area == RIGHT_MARGIN_AREA)
1122 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1123 + window_box_width (w, LEFT_MARGIN_AREA)
1124 + window_box_width (w, TEXT_AREA)
1125 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1126 ? 0
1127 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1128 else if (area == LEFT_MARGIN_AREA
1129 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1130 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1131
1132 return x;
1133 }
1134
1135
1136 /* Return the window-relative coordinate of the right edge of display
1137 area AREA of window W. AREA < 0 means return the left edge of the
1138 whole window, to the left of the right fringe of W. */
1139
1140 INLINE int
1141 window_box_right_offset (w, area)
1142 struct window *w;
1143 int area;
1144 {
1145 return window_box_left_offset (w, area) + window_box_width (w, area);
1146 }
1147
1148 /* Return the frame-relative coordinate of the left edge of display
1149 area AREA of window W. AREA < 0 means return the left edge of the
1150 whole window, to the right of the left fringe of W. */
1151
1152 INLINE int
1153 window_box_left (w, area)
1154 struct window *w;
1155 int area;
1156 {
1157 struct frame *f = XFRAME (w->frame);
1158 int x;
1159
1160 if (w->pseudo_window_p)
1161 return FRAME_INTERNAL_BORDER_WIDTH (f);
1162
1163 x = (WINDOW_LEFT_EDGE_X (w)
1164 + window_box_left_offset (w, area));
1165
1166 return x;
1167 }
1168
1169
1170 /* Return the frame-relative coordinate of the right edge of display
1171 area AREA of window W. AREA < 0 means return the left edge of the
1172 whole window, to the left of the right fringe of W. */
1173
1174 INLINE int
1175 window_box_right (w, area)
1176 struct window *w;
1177 int area;
1178 {
1179 return window_box_left (w, area) + window_box_width (w, area);
1180 }
1181
1182 /* Get the bounding box of the display area AREA of window W, without
1183 mode lines, in frame-relative coordinates. AREA < 0 means the
1184 whole window, not including the left and right fringes of
1185 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1186 coordinates of the upper-left corner of the box. Return in
1187 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1188
1189 INLINE void
1190 window_box (w, area, box_x, box_y, box_width, box_height)
1191 struct window *w;
1192 int area;
1193 int *box_x, *box_y, *box_width, *box_height;
1194 {
1195 if (box_width)
1196 *box_width = window_box_width (w, area);
1197 if (box_height)
1198 *box_height = window_box_height (w);
1199 if (box_x)
1200 *box_x = window_box_left (w, area);
1201 if (box_y)
1202 {
1203 *box_y = WINDOW_TOP_EDGE_Y (w);
1204 if (WINDOW_WANTS_HEADER_LINE_P (w))
1205 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1206 }
1207 }
1208
1209
1210 /* Get the bounding box of the display area AREA of window W, without
1211 mode lines. AREA < 0 means the whole window, not including the
1212 left and right fringe of the window. Return in *TOP_LEFT_X
1213 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1214 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1215 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1216 box. */
1217
1218 INLINE void
1219 window_box_edges (w, area, top_left_x, top_left_y,
1220 bottom_right_x, bottom_right_y)
1221 struct window *w;
1222 int area;
1223 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1224 {
1225 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1226 bottom_right_y);
1227 *bottom_right_x += *top_left_x;
1228 *bottom_right_y += *top_left_y;
1229 }
1230
1231
1232 \f
1233 /***********************************************************************
1234 Utilities
1235 ***********************************************************************/
1236
1237 /* Return the bottom y-position of the line the iterator IT is in.
1238 This can modify IT's settings. */
1239
1240 int
1241 line_bottom_y (it)
1242 struct it *it;
1243 {
1244 int line_height = it->max_ascent + it->max_descent;
1245 int line_top_y = it->current_y;
1246
1247 if (line_height == 0)
1248 {
1249 if (last_height)
1250 line_height = last_height;
1251 else if (IT_CHARPOS (*it) < ZV)
1252 {
1253 move_it_by_lines (it, 1, 1);
1254 line_height = (it->max_ascent || it->max_descent
1255 ? it->max_ascent + it->max_descent
1256 : last_height);
1257 }
1258 else
1259 {
1260 struct glyph_row *row = it->glyph_row;
1261
1262 /* Use the default character height. */
1263 it->glyph_row = NULL;
1264 it->what = IT_CHARACTER;
1265 it->c = ' ';
1266 it->len = 1;
1267 PRODUCE_GLYPHS (it);
1268 line_height = it->ascent + it->descent;
1269 it->glyph_row = row;
1270 }
1271 }
1272
1273 return line_top_y + line_height;
1274 }
1275
1276
1277 /* Return 1 if position CHARPOS is visible in window W.
1278 If visible, set *X and *Y to pixel coordinates of top left corner.
1279 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1280 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1281
1282 int
1283 pos_visible_p (w, charpos, x, y, rtop, rbot, rowh, vpos)
1284 struct window *w;
1285 int charpos, *x, *y, *rtop, *rbot, *rowh, *vpos;
1286 {
1287 struct it it;
1288 struct text_pos top;
1289 int visible_p = 0;
1290 struct buffer *old_buffer = NULL;
1291
1292 if (noninteractive)
1293 return visible_p;
1294
1295 if (XBUFFER (w->buffer) != current_buffer)
1296 {
1297 old_buffer = current_buffer;
1298 set_buffer_internal_1 (XBUFFER (w->buffer));
1299 }
1300
1301 SET_TEXT_POS_FROM_MARKER (top, w->start);
1302
1303 /* Compute exact mode line heights. */
1304 if (WINDOW_WANTS_MODELINE_P (w))
1305 current_mode_line_height
1306 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1307 current_buffer->mode_line_format);
1308
1309 if (WINDOW_WANTS_HEADER_LINE_P (w))
1310 current_header_line_height
1311 = display_mode_line (w, HEADER_LINE_FACE_ID,
1312 current_buffer->header_line_format);
1313
1314 start_display (&it, w, top);
1315 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1316 MOVE_TO_POS | MOVE_TO_Y);
1317
1318 /* Note that we may overshoot because of invisible text. */
1319 if (IT_CHARPOS (it) >= charpos)
1320 {
1321 int top_x = it.current_x;
1322 int top_y = it.current_y;
1323 int bottom_y = (last_height = 0, line_bottom_y (&it));
1324 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1325
1326 if (top_y < window_top_y)
1327 visible_p = bottom_y > window_top_y;
1328 else if (top_y < it.last_visible_y)
1329 visible_p = 1;
1330 if (visible_p)
1331 {
1332 *x = top_x;
1333 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1334 *rtop = max (0, window_top_y - top_y);
1335 *rbot = max (0, bottom_y - it.last_visible_y);
1336 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1337 - max (top_y, window_top_y)));
1338 *vpos = it.vpos;
1339 }
1340 }
1341 else
1342 {
1343 struct it it2;
1344
1345 it2 = it;
1346 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1347 move_it_by_lines (&it, 1, 0);
1348 if (charpos < IT_CHARPOS (it))
1349 {
1350 visible_p = 1;
1351 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1352 *x = it2.current_x;
1353 *y = it2.current_y + it2.max_ascent - it2.ascent;
1354 *rtop = max (0, -it2.current_y);
1355 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1356 - it.last_visible_y));
1357 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1358 it.last_visible_y)
1359 - max (it2.current_y,
1360 WINDOW_HEADER_LINE_HEIGHT (w))));
1361 *vpos = it2.vpos;
1362 }
1363 }
1364
1365 if (old_buffer)
1366 set_buffer_internal_1 (old_buffer);
1367
1368 current_header_line_height = current_mode_line_height = -1;
1369
1370 if (visible_p && XFASTINT (w->hscroll) > 0)
1371 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1372
1373 #if 0
1374 /* Debugging code. */
1375 if (visible_p)
1376 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1377 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1378 else
1379 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1380 #endif
1381
1382 return visible_p;
1383 }
1384
1385
1386 /* Return the next character from STR which is MAXLEN bytes long.
1387 Return in *LEN the length of the character. This is like
1388 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1389 we find one, we return a `?', but with the length of the invalid
1390 character. */
1391
1392 static INLINE int
1393 string_char_and_length (str, maxlen, len)
1394 const unsigned char *str;
1395 int maxlen, *len;
1396 {
1397 int c;
1398
1399 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1400 if (!CHAR_VALID_P (c, 1))
1401 /* We may not change the length here because other places in Emacs
1402 don't use this function, i.e. they silently accept invalid
1403 characters. */
1404 c = '?';
1405
1406 return c;
1407 }
1408
1409
1410
1411 /* Given a position POS containing a valid character and byte position
1412 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1413
1414 static struct text_pos
1415 string_pos_nchars_ahead (pos, string, nchars)
1416 struct text_pos pos;
1417 Lisp_Object string;
1418 int nchars;
1419 {
1420 xassert (STRINGP (string) && nchars >= 0);
1421
1422 if (STRING_MULTIBYTE (string))
1423 {
1424 int rest = SBYTES (string) - BYTEPOS (pos);
1425 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1426 int len;
1427
1428 while (nchars--)
1429 {
1430 string_char_and_length (p, rest, &len);
1431 p += len, rest -= len;
1432 xassert (rest >= 0);
1433 CHARPOS (pos) += 1;
1434 BYTEPOS (pos) += len;
1435 }
1436 }
1437 else
1438 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1439
1440 return pos;
1441 }
1442
1443
1444 /* Value is the text position, i.e. character and byte position,
1445 for character position CHARPOS in STRING. */
1446
1447 static INLINE struct text_pos
1448 string_pos (charpos, string)
1449 int charpos;
1450 Lisp_Object string;
1451 {
1452 struct text_pos pos;
1453 xassert (STRINGP (string));
1454 xassert (charpos >= 0);
1455 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1456 return pos;
1457 }
1458
1459
1460 /* Value is a text position, i.e. character and byte position, for
1461 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1462 means recognize multibyte characters. */
1463
1464 static struct text_pos
1465 c_string_pos (charpos, s, multibyte_p)
1466 int charpos;
1467 unsigned char *s;
1468 int multibyte_p;
1469 {
1470 struct text_pos pos;
1471
1472 xassert (s != NULL);
1473 xassert (charpos >= 0);
1474
1475 if (multibyte_p)
1476 {
1477 int rest = strlen (s), len;
1478
1479 SET_TEXT_POS (pos, 0, 0);
1480 while (charpos--)
1481 {
1482 string_char_and_length (s, rest, &len);
1483 s += len, rest -= len;
1484 xassert (rest >= 0);
1485 CHARPOS (pos) += 1;
1486 BYTEPOS (pos) += len;
1487 }
1488 }
1489 else
1490 SET_TEXT_POS (pos, charpos, charpos);
1491
1492 return pos;
1493 }
1494
1495
1496 /* Value is the number of characters in C string S. MULTIBYTE_P
1497 non-zero means recognize multibyte characters. */
1498
1499 static int
1500 number_of_chars (s, multibyte_p)
1501 unsigned char *s;
1502 int multibyte_p;
1503 {
1504 int nchars;
1505
1506 if (multibyte_p)
1507 {
1508 int rest = strlen (s), len;
1509 unsigned char *p = (unsigned char *) s;
1510
1511 for (nchars = 0; rest > 0; ++nchars)
1512 {
1513 string_char_and_length (p, rest, &len);
1514 rest -= len, p += len;
1515 }
1516 }
1517 else
1518 nchars = strlen (s);
1519
1520 return nchars;
1521 }
1522
1523
1524 /* Compute byte position NEWPOS->bytepos corresponding to
1525 NEWPOS->charpos. POS is a known position in string STRING.
1526 NEWPOS->charpos must be >= POS.charpos. */
1527
1528 static void
1529 compute_string_pos (newpos, pos, string)
1530 struct text_pos *newpos, pos;
1531 Lisp_Object string;
1532 {
1533 xassert (STRINGP (string));
1534 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1535
1536 if (STRING_MULTIBYTE (string))
1537 *newpos = string_pos_nchars_ahead (pos, string,
1538 CHARPOS (*newpos) - CHARPOS (pos));
1539 else
1540 BYTEPOS (*newpos) = CHARPOS (*newpos);
1541 }
1542
1543 /* EXPORT:
1544 Return an estimation of the pixel height of mode or top lines on
1545 frame F. FACE_ID specifies what line's height to estimate. */
1546
1547 int
1548 estimate_mode_line_height (f, face_id)
1549 struct frame *f;
1550 enum face_id face_id;
1551 {
1552 #ifdef HAVE_WINDOW_SYSTEM
1553 if (FRAME_WINDOW_P (f))
1554 {
1555 int height = FONT_HEIGHT (FRAME_FONT (f));
1556
1557 /* This function is called so early when Emacs starts that the face
1558 cache and mode line face are not yet initialized. */
1559 if (FRAME_FACE_CACHE (f))
1560 {
1561 struct face *face = FACE_FROM_ID (f, face_id);
1562 if (face)
1563 {
1564 if (face->font)
1565 height = FONT_HEIGHT (face->font);
1566 if (face->box_line_width > 0)
1567 height += 2 * face->box_line_width;
1568 }
1569 }
1570
1571 return height;
1572 }
1573 #endif
1574
1575 return 1;
1576 }
1577
1578 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1579 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1580 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1581 not force the value into range. */
1582
1583 void
1584 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1585 FRAME_PTR f;
1586 register int pix_x, pix_y;
1587 int *x, *y;
1588 NativeRectangle *bounds;
1589 int noclip;
1590 {
1591
1592 #ifdef HAVE_WINDOW_SYSTEM
1593 if (FRAME_WINDOW_P (f))
1594 {
1595 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1596 even for negative values. */
1597 if (pix_x < 0)
1598 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1599 if (pix_y < 0)
1600 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1601
1602 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1603 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1604
1605 if (bounds)
1606 STORE_NATIVE_RECT (*bounds,
1607 FRAME_COL_TO_PIXEL_X (f, pix_x),
1608 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1609 FRAME_COLUMN_WIDTH (f) - 1,
1610 FRAME_LINE_HEIGHT (f) - 1);
1611
1612 if (!noclip)
1613 {
1614 if (pix_x < 0)
1615 pix_x = 0;
1616 else if (pix_x > FRAME_TOTAL_COLS (f))
1617 pix_x = FRAME_TOTAL_COLS (f);
1618
1619 if (pix_y < 0)
1620 pix_y = 0;
1621 else if (pix_y > FRAME_LINES (f))
1622 pix_y = FRAME_LINES (f);
1623 }
1624 }
1625 #endif
1626
1627 *x = pix_x;
1628 *y = pix_y;
1629 }
1630
1631
1632 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1633 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1634 can't tell the positions because W's display is not up to date,
1635 return 0. */
1636
1637 int
1638 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1639 struct window *w;
1640 int hpos, vpos;
1641 int *frame_x, *frame_y;
1642 {
1643 #ifdef HAVE_WINDOW_SYSTEM
1644 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1645 {
1646 int success_p;
1647
1648 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1649 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1650
1651 if (display_completed)
1652 {
1653 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1654 struct glyph *glyph = row->glyphs[TEXT_AREA];
1655 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1656
1657 hpos = row->x;
1658 vpos = row->y;
1659 while (glyph < end)
1660 {
1661 hpos += glyph->pixel_width;
1662 ++glyph;
1663 }
1664
1665 /* If first glyph is partially visible, its first visible position is still 0. */
1666 if (hpos < 0)
1667 hpos = 0;
1668
1669 success_p = 1;
1670 }
1671 else
1672 {
1673 hpos = vpos = 0;
1674 success_p = 0;
1675 }
1676
1677 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1678 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1679 return success_p;
1680 }
1681 #endif
1682
1683 *frame_x = hpos;
1684 *frame_y = vpos;
1685 return 1;
1686 }
1687
1688
1689 #ifdef HAVE_WINDOW_SYSTEM
1690
1691 /* Find the glyph under window-relative coordinates X/Y in window W.
1692 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1693 strings. Return in *HPOS and *VPOS the row and column number of
1694 the glyph found. Return in *AREA the glyph area containing X.
1695 Value is a pointer to the glyph found or null if X/Y is not on
1696 text, or we can't tell because W's current matrix is not up to
1697 date. */
1698
1699 static struct glyph *
1700 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1701 struct window *w;
1702 int x, y;
1703 int *hpos, *vpos, *dx, *dy, *area;
1704 {
1705 struct glyph *glyph, *end;
1706 struct glyph_row *row = NULL;
1707 int x0, i;
1708
1709 /* Find row containing Y. Give up if some row is not enabled. */
1710 for (i = 0; i < w->current_matrix->nrows; ++i)
1711 {
1712 row = MATRIX_ROW (w->current_matrix, i);
1713 if (!row->enabled_p)
1714 return NULL;
1715 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1716 break;
1717 }
1718
1719 *vpos = i;
1720 *hpos = 0;
1721
1722 /* Give up if Y is not in the window. */
1723 if (i == w->current_matrix->nrows)
1724 return NULL;
1725
1726 /* Get the glyph area containing X. */
1727 if (w->pseudo_window_p)
1728 {
1729 *area = TEXT_AREA;
1730 x0 = 0;
1731 }
1732 else
1733 {
1734 if (x < window_box_left_offset (w, TEXT_AREA))
1735 {
1736 *area = LEFT_MARGIN_AREA;
1737 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1738 }
1739 else if (x < window_box_right_offset (w, TEXT_AREA))
1740 {
1741 *area = TEXT_AREA;
1742 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1743 }
1744 else
1745 {
1746 *area = RIGHT_MARGIN_AREA;
1747 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1748 }
1749 }
1750
1751 /* Find glyph containing X. */
1752 glyph = row->glyphs[*area];
1753 end = glyph + row->used[*area];
1754 x -= x0;
1755 while (glyph < end && x >= glyph->pixel_width)
1756 {
1757 x -= glyph->pixel_width;
1758 ++glyph;
1759 }
1760
1761 if (glyph == end)
1762 return NULL;
1763
1764 if (dx)
1765 {
1766 *dx = x;
1767 *dy = y - (row->y + row->ascent - glyph->ascent);
1768 }
1769
1770 *hpos = glyph - row->glyphs[*area];
1771 return glyph;
1772 }
1773
1774
1775 /* EXPORT:
1776 Convert frame-relative x/y to coordinates relative to window W.
1777 Takes pseudo-windows into account. */
1778
1779 void
1780 frame_to_window_pixel_xy (w, x, y)
1781 struct window *w;
1782 int *x, *y;
1783 {
1784 if (w->pseudo_window_p)
1785 {
1786 /* A pseudo-window is always full-width, and starts at the
1787 left edge of the frame, plus a frame border. */
1788 struct frame *f = XFRAME (w->frame);
1789 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1790 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1791 }
1792 else
1793 {
1794 *x -= WINDOW_LEFT_EDGE_X (w);
1795 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1796 }
1797 }
1798
1799 /* EXPORT:
1800 Return in RECTS[] at most N clipping rectangles for glyph string S.
1801 Return the number of stored rectangles. */
1802
1803 int
1804 get_glyph_string_clip_rects (s, rects, n)
1805 struct glyph_string *s;
1806 NativeRectangle *rects;
1807 int n;
1808 {
1809 XRectangle r;
1810
1811 if (n <= 0)
1812 return 0;
1813
1814 if (s->row->full_width_p)
1815 {
1816 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1817 r.x = WINDOW_LEFT_EDGE_X (s->w);
1818 r.width = WINDOW_TOTAL_WIDTH (s->w);
1819
1820 /* Unless displaying a mode or menu bar line, which are always
1821 fully visible, clip to the visible part of the row. */
1822 if (s->w->pseudo_window_p)
1823 r.height = s->row->visible_height;
1824 else
1825 r.height = s->height;
1826 }
1827 else
1828 {
1829 /* This is a text line that may be partially visible. */
1830 r.x = window_box_left (s->w, s->area);
1831 r.width = window_box_width (s->w, s->area);
1832 r.height = s->row->visible_height;
1833 }
1834
1835 if (s->clip_head)
1836 if (r.x < s->clip_head->x)
1837 {
1838 if (r.width >= s->clip_head->x - r.x)
1839 r.width -= s->clip_head->x - r.x;
1840 else
1841 r.width = 0;
1842 r.x = s->clip_head->x;
1843 }
1844 if (s->clip_tail)
1845 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1846 {
1847 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1848 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1849 else
1850 r.width = 0;
1851 }
1852
1853 /* If S draws overlapping rows, it's sufficient to use the top and
1854 bottom of the window for clipping because this glyph string
1855 intentionally draws over other lines. */
1856 if (s->for_overlaps)
1857 {
1858 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1859 r.height = window_text_bottom_y (s->w) - r.y;
1860
1861 /* Alas, the above simple strategy does not work for the
1862 environments with anti-aliased text: if the same text is
1863 drawn onto the same place multiple times, it gets thicker.
1864 If the overlap we are processing is for the erased cursor, we
1865 take the intersection with the rectagle of the cursor. */
1866 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1867 {
1868 XRectangle rc, r_save = r;
1869
1870 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1871 rc.y = s->w->phys_cursor.y;
1872 rc.width = s->w->phys_cursor_width;
1873 rc.height = s->w->phys_cursor_height;
1874
1875 x_intersect_rectangles (&r_save, &rc, &r);
1876 }
1877 }
1878 else
1879 {
1880 /* Don't use S->y for clipping because it doesn't take partially
1881 visible lines into account. For example, it can be negative for
1882 partially visible lines at the top of a window. */
1883 if (!s->row->full_width_p
1884 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1885 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1886 else
1887 r.y = max (0, s->row->y);
1888
1889 /* If drawing a tool-bar window, draw it over the internal border
1890 at the top of the window. */
1891 if (WINDOWP (s->f->tool_bar_window)
1892 && s->w == XWINDOW (s->f->tool_bar_window))
1893 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1894 }
1895
1896 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1897
1898 /* If drawing the cursor, don't let glyph draw outside its
1899 advertised boundaries. Cleartype does this under some circumstances. */
1900 if (s->hl == DRAW_CURSOR)
1901 {
1902 struct glyph *glyph = s->first_glyph;
1903 int height, max_y;
1904
1905 if (s->x > r.x)
1906 {
1907 r.width -= s->x - r.x;
1908 r.x = s->x;
1909 }
1910 r.width = min (r.width, glyph->pixel_width);
1911
1912 /* If r.y is below window bottom, ensure that we still see a cursor. */
1913 height = min (glyph->ascent + glyph->descent,
1914 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1915 max_y = window_text_bottom_y (s->w) - height;
1916 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1917 if (s->ybase - glyph->ascent > max_y)
1918 {
1919 r.y = max_y;
1920 r.height = height;
1921 }
1922 else
1923 {
1924 /* Don't draw cursor glyph taller than our actual glyph. */
1925 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1926 if (height < r.height)
1927 {
1928 max_y = r.y + r.height;
1929 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1930 r.height = min (max_y - r.y, height);
1931 }
1932 }
1933 }
1934
1935 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1936 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1937 {
1938 #ifdef CONVERT_FROM_XRECT
1939 CONVERT_FROM_XRECT (r, *rects);
1940 #else
1941 *rects = r;
1942 #endif
1943 return 1;
1944 }
1945 else
1946 {
1947 /* If we are processing overlapping and allowed to return
1948 multiple clipping rectangles, we exclude the row of the glyph
1949 string from the clipping rectangle. This is to avoid drawing
1950 the same text on the environment with anti-aliasing. */
1951 #ifdef CONVERT_FROM_XRECT
1952 XRectangle rs[2];
1953 #else
1954 XRectangle *rs = rects;
1955 #endif
1956 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1957
1958 if (s->for_overlaps & OVERLAPS_PRED)
1959 {
1960 rs[i] = r;
1961 if (r.y + r.height > row_y)
1962 {
1963 if (r.y < row_y)
1964 rs[i].height = row_y - r.y;
1965 else
1966 rs[i].height = 0;
1967 }
1968 i++;
1969 }
1970 if (s->for_overlaps & OVERLAPS_SUCC)
1971 {
1972 rs[i] = r;
1973 if (r.y < row_y + s->row->visible_height)
1974 {
1975 if (r.y + r.height > row_y + s->row->visible_height)
1976 {
1977 rs[i].y = row_y + s->row->visible_height;
1978 rs[i].height = r.y + r.height - rs[i].y;
1979 }
1980 else
1981 rs[i].height = 0;
1982 }
1983 i++;
1984 }
1985
1986 n = i;
1987 #ifdef CONVERT_FROM_XRECT
1988 for (i = 0; i < n; i++)
1989 CONVERT_FROM_XRECT (rs[i], rects[i]);
1990 #endif
1991 return n;
1992 }
1993 }
1994
1995 /* EXPORT:
1996 Return in *NR the clipping rectangle for glyph string S. */
1997
1998 void
1999 get_glyph_string_clip_rect (s, nr)
2000 struct glyph_string *s;
2001 NativeRectangle *nr;
2002 {
2003 get_glyph_string_clip_rects (s, nr, 1);
2004 }
2005
2006
2007 /* EXPORT:
2008 Return the position and height of the phys cursor in window W.
2009 Set w->phys_cursor_width to width of phys cursor.
2010 */
2011
2012 void
2013 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
2014 struct window *w;
2015 struct glyph_row *row;
2016 struct glyph *glyph;
2017 int *xp, *yp, *heightp;
2018 {
2019 struct frame *f = XFRAME (WINDOW_FRAME (w));
2020 int x, y, wd, h, h0, y0;
2021
2022 /* Compute the width of the rectangle to draw. If on a stretch
2023 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2024 rectangle as wide as the glyph, but use a canonical character
2025 width instead. */
2026 wd = glyph->pixel_width - 1;
2027 #ifdef HAVE_NTGUI
2028 wd++; /* Why? */
2029 #endif
2030
2031 x = w->phys_cursor.x;
2032 if (x < 0)
2033 {
2034 wd += x;
2035 x = 0;
2036 }
2037
2038 if (glyph->type == STRETCH_GLYPH
2039 && !x_stretch_cursor_p)
2040 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2041 w->phys_cursor_width = wd;
2042
2043 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2044
2045 /* If y is below window bottom, ensure that we still see a cursor. */
2046 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2047
2048 h = max (h0, glyph->ascent + glyph->descent);
2049 h0 = min (h0, glyph->ascent + glyph->descent);
2050
2051 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2052 if (y < y0)
2053 {
2054 h = max (h - (y0 - y) + 1, h0);
2055 y = y0 - 1;
2056 }
2057 else
2058 {
2059 y0 = window_text_bottom_y (w) - h0;
2060 if (y > y0)
2061 {
2062 h += y - y0;
2063 y = y0;
2064 }
2065 }
2066
2067 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2068 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2069 *heightp = h;
2070 }
2071
2072 /*
2073 * Remember which glyph the mouse is over.
2074 */
2075
2076 void
2077 remember_mouse_glyph (f, gx, gy, rect)
2078 struct frame *f;
2079 int gx, gy;
2080 NativeRectangle *rect;
2081 {
2082 Lisp_Object window;
2083 struct window *w;
2084 struct glyph_row *r, *gr, *end_row;
2085 enum window_part part;
2086 enum glyph_row_area area;
2087 int x, y, width, height;
2088
2089 /* Try to determine frame pixel position and size of the glyph under
2090 frame pixel coordinates X/Y on frame F. */
2091
2092 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2093 if (NILP (window))
2094 {
2095 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2096 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2097 goto virtual_glyph;
2098 }
2099
2100 w = XWINDOW (window);
2101 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2102 height = WINDOW_FRAME_LINE_HEIGHT (w);
2103
2104 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2105 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2106
2107 if (w->pseudo_window_p)
2108 {
2109 area = TEXT_AREA;
2110 part = ON_MODE_LINE; /* Don't adjust margin. */
2111 goto text_glyph;
2112 }
2113
2114 switch (part)
2115 {
2116 case ON_LEFT_MARGIN:
2117 area = LEFT_MARGIN_AREA;
2118 goto text_glyph;
2119
2120 case ON_RIGHT_MARGIN:
2121 area = RIGHT_MARGIN_AREA;
2122 goto text_glyph;
2123
2124 case ON_HEADER_LINE:
2125 case ON_MODE_LINE:
2126 gr = (part == ON_HEADER_LINE
2127 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2128 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2129 gy = gr->y;
2130 area = TEXT_AREA;
2131 goto text_glyph_row_found;
2132
2133 case ON_TEXT:
2134 area = TEXT_AREA;
2135
2136 text_glyph:
2137 gr = 0; gy = 0;
2138 for (; r <= end_row && r->enabled_p; ++r)
2139 if (r->y + r->height > y)
2140 {
2141 gr = r; gy = r->y;
2142 break;
2143 }
2144
2145 text_glyph_row_found:
2146 if (gr && gy <= y)
2147 {
2148 struct glyph *g = gr->glyphs[area];
2149 struct glyph *end = g + gr->used[area];
2150
2151 height = gr->height;
2152 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2153 if (gx + g->pixel_width > x)
2154 break;
2155
2156 if (g < end)
2157 {
2158 if (g->type == IMAGE_GLYPH)
2159 {
2160 /* Don't remember when mouse is over image, as
2161 image may have hot-spots. */
2162 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2163 return;
2164 }
2165 width = g->pixel_width;
2166 }
2167 else
2168 {
2169 /* Use nominal char spacing at end of line. */
2170 x -= gx;
2171 gx += (x / width) * width;
2172 }
2173
2174 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2175 gx += window_box_left_offset (w, area);
2176 }
2177 else
2178 {
2179 /* Use nominal line height at end of window. */
2180 gx = (x / width) * width;
2181 y -= gy;
2182 gy += (y / height) * height;
2183 }
2184 break;
2185
2186 case ON_LEFT_FRINGE:
2187 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2188 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2189 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2190 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2191 goto row_glyph;
2192
2193 case ON_RIGHT_FRINGE:
2194 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2195 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2196 : window_box_right_offset (w, TEXT_AREA));
2197 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2198 goto row_glyph;
2199
2200 case ON_SCROLL_BAR:
2201 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2202 ? 0
2203 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2204 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2205 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2206 : 0)));
2207 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2208
2209 row_glyph:
2210 gr = 0, gy = 0;
2211 for (; r <= end_row && r->enabled_p; ++r)
2212 if (r->y + r->height > y)
2213 {
2214 gr = r; gy = r->y;
2215 break;
2216 }
2217
2218 if (gr && gy <= y)
2219 height = gr->height;
2220 else
2221 {
2222 /* Use nominal line height at end of window. */
2223 y -= gy;
2224 gy += (y / height) * height;
2225 }
2226 break;
2227
2228 default:
2229 ;
2230 virtual_glyph:
2231 /* If there is no glyph under the mouse, then we divide the screen
2232 into a grid of the smallest glyph in the frame, and use that
2233 as our "glyph". */
2234
2235 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2236 round down even for negative values. */
2237 if (gx < 0)
2238 gx -= width - 1;
2239 if (gy < 0)
2240 gy -= height - 1;
2241
2242 gx = (gx / width) * width;
2243 gy = (gy / height) * height;
2244
2245 goto store_rect;
2246 }
2247
2248 gx += WINDOW_LEFT_EDGE_X (w);
2249 gy += WINDOW_TOP_EDGE_Y (w);
2250
2251 store_rect:
2252 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2253
2254 /* Visible feedback for debugging. */
2255 #if 0
2256 #if HAVE_X_WINDOWS
2257 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2258 f->output_data.x->normal_gc,
2259 gx, gy, width, height);
2260 #endif
2261 #endif
2262 }
2263
2264
2265 #endif /* HAVE_WINDOW_SYSTEM */
2266
2267 \f
2268 /***********************************************************************
2269 Lisp form evaluation
2270 ***********************************************************************/
2271
2272 /* Error handler for safe_eval and safe_call. */
2273
2274 static Lisp_Object
2275 safe_eval_handler (arg)
2276 Lisp_Object arg;
2277 {
2278 add_to_log ("Error during redisplay: %s", arg, Qnil);
2279 return Qnil;
2280 }
2281
2282
2283 /* Evaluate SEXPR and return the result, or nil if something went
2284 wrong. Prevent redisplay during the evaluation. */
2285
2286 Lisp_Object
2287 safe_eval (sexpr)
2288 Lisp_Object sexpr;
2289 {
2290 Lisp_Object val;
2291
2292 if (inhibit_eval_during_redisplay)
2293 val = Qnil;
2294 else
2295 {
2296 int count = SPECPDL_INDEX ();
2297 struct gcpro gcpro1;
2298
2299 GCPRO1 (sexpr);
2300 specbind (Qinhibit_redisplay, Qt);
2301 /* Use Qt to ensure debugger does not run,
2302 so there is no possibility of wanting to redisplay. */
2303 val = internal_condition_case_1 (Feval, sexpr, Qt,
2304 safe_eval_handler);
2305 UNGCPRO;
2306 val = unbind_to (count, val);
2307 }
2308
2309 return val;
2310 }
2311
2312
2313 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2314 Return the result, or nil if something went wrong. Prevent
2315 redisplay during the evaluation. */
2316
2317 Lisp_Object
2318 safe_call (nargs, args)
2319 int nargs;
2320 Lisp_Object *args;
2321 {
2322 Lisp_Object val;
2323
2324 if (inhibit_eval_during_redisplay)
2325 val = Qnil;
2326 else
2327 {
2328 int count = SPECPDL_INDEX ();
2329 struct gcpro gcpro1;
2330
2331 GCPRO1 (args[0]);
2332 gcpro1.nvars = nargs;
2333 specbind (Qinhibit_redisplay, Qt);
2334 /* Use Qt to ensure debugger does not run,
2335 so there is no possibility of wanting to redisplay. */
2336 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2337 safe_eval_handler);
2338 UNGCPRO;
2339 val = unbind_to (count, val);
2340 }
2341
2342 return val;
2343 }
2344
2345
2346 /* Call function FN with one argument ARG.
2347 Return the result, or nil if something went wrong. */
2348
2349 Lisp_Object
2350 safe_call1 (fn, arg)
2351 Lisp_Object fn, arg;
2352 {
2353 Lisp_Object args[2];
2354 args[0] = fn;
2355 args[1] = arg;
2356 return safe_call (2, args);
2357 }
2358
2359
2360 \f
2361 /***********************************************************************
2362 Debugging
2363 ***********************************************************************/
2364
2365 #if 0
2366
2367 /* Define CHECK_IT to perform sanity checks on iterators.
2368 This is for debugging. It is too slow to do unconditionally. */
2369
2370 static void
2371 check_it (it)
2372 struct it *it;
2373 {
2374 if (it->method == GET_FROM_STRING)
2375 {
2376 xassert (STRINGP (it->string));
2377 xassert (IT_STRING_CHARPOS (*it) >= 0);
2378 }
2379 else
2380 {
2381 xassert (IT_STRING_CHARPOS (*it) < 0);
2382 if (it->method == GET_FROM_BUFFER)
2383 {
2384 /* Check that character and byte positions agree. */
2385 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2386 }
2387 }
2388
2389 if (it->dpvec)
2390 xassert (it->current.dpvec_index >= 0);
2391 else
2392 xassert (it->current.dpvec_index < 0);
2393 }
2394
2395 #define CHECK_IT(IT) check_it ((IT))
2396
2397 #else /* not 0 */
2398
2399 #define CHECK_IT(IT) (void) 0
2400
2401 #endif /* not 0 */
2402
2403
2404 #if GLYPH_DEBUG
2405
2406 /* Check that the window end of window W is what we expect it
2407 to be---the last row in the current matrix displaying text. */
2408
2409 static void
2410 check_window_end (w)
2411 struct window *w;
2412 {
2413 if (!MINI_WINDOW_P (w)
2414 && !NILP (w->window_end_valid))
2415 {
2416 struct glyph_row *row;
2417 xassert ((row = MATRIX_ROW (w->current_matrix,
2418 XFASTINT (w->window_end_vpos)),
2419 !row->enabled_p
2420 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2421 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2422 }
2423 }
2424
2425 #define CHECK_WINDOW_END(W) check_window_end ((W))
2426
2427 #else /* not GLYPH_DEBUG */
2428
2429 #define CHECK_WINDOW_END(W) (void) 0
2430
2431 #endif /* not GLYPH_DEBUG */
2432
2433
2434 \f
2435 /***********************************************************************
2436 Iterator initialization
2437 ***********************************************************************/
2438
2439 /* Initialize IT for displaying current_buffer in window W, starting
2440 at character position CHARPOS. CHARPOS < 0 means that no buffer
2441 position is specified which is useful when the iterator is assigned
2442 a position later. BYTEPOS is the byte position corresponding to
2443 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2444
2445 If ROW is not null, calls to produce_glyphs with IT as parameter
2446 will produce glyphs in that row.
2447
2448 BASE_FACE_ID is the id of a base face to use. It must be one of
2449 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2450 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2451 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2452
2453 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2454 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2455 will be initialized to use the corresponding mode line glyph row of
2456 the desired matrix of W. */
2457
2458 void
2459 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2460 struct it *it;
2461 struct window *w;
2462 int charpos, bytepos;
2463 struct glyph_row *row;
2464 enum face_id base_face_id;
2465 {
2466 int highlight_region_p;
2467
2468 /* Some precondition checks. */
2469 xassert (w != NULL && it != NULL);
2470 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2471 && charpos <= ZV));
2472
2473 /* If face attributes have been changed since the last redisplay,
2474 free realized faces now because they depend on face definitions
2475 that might have changed. Don't free faces while there might be
2476 desired matrices pending which reference these faces. */
2477 if (face_change_count && !inhibit_free_realized_faces)
2478 {
2479 face_change_count = 0;
2480 free_all_realized_faces (Qnil);
2481 }
2482
2483 /* Use one of the mode line rows of W's desired matrix if
2484 appropriate. */
2485 if (row == NULL)
2486 {
2487 if (base_face_id == MODE_LINE_FACE_ID
2488 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2489 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2490 else if (base_face_id == HEADER_LINE_FACE_ID)
2491 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2492 }
2493
2494 /* Clear IT. */
2495 bzero (it, sizeof *it);
2496 it->current.overlay_string_index = -1;
2497 it->current.dpvec_index = -1;
2498 it->base_face_id = base_face_id;
2499 it->string = Qnil;
2500 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2501
2502 /* The window in which we iterate over current_buffer: */
2503 XSETWINDOW (it->window, w);
2504 it->w = w;
2505 it->f = XFRAME (w->frame);
2506
2507 /* Extra space between lines (on window systems only). */
2508 if (base_face_id == DEFAULT_FACE_ID
2509 && FRAME_WINDOW_P (it->f))
2510 {
2511 if (NATNUMP (current_buffer->extra_line_spacing))
2512 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2513 else if (FLOATP (current_buffer->extra_line_spacing))
2514 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2515 * FRAME_LINE_HEIGHT (it->f));
2516 else if (it->f->extra_line_spacing > 0)
2517 it->extra_line_spacing = it->f->extra_line_spacing;
2518 it->max_extra_line_spacing = 0;
2519 }
2520
2521 /* If realized faces have been removed, e.g. because of face
2522 attribute changes of named faces, recompute them. When running
2523 in batch mode, the face cache of Vterminal_frame is null. If
2524 we happen to get called, make a dummy face cache. */
2525 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2526 init_frame_faces (it->f);
2527 if (FRAME_FACE_CACHE (it->f)->used == 0)
2528 recompute_basic_faces (it->f);
2529
2530 /* Current value of the `slice', `space-width', and 'height' properties. */
2531 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2532 it->space_width = Qnil;
2533 it->font_height = Qnil;
2534 it->override_ascent = -1;
2535
2536 /* Are control characters displayed as `^C'? */
2537 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2538
2539 /* -1 means everything between a CR and the following line end
2540 is invisible. >0 means lines indented more than this value are
2541 invisible. */
2542 it->selective = (INTEGERP (current_buffer->selective_display)
2543 ? XFASTINT (current_buffer->selective_display)
2544 : (!NILP (current_buffer->selective_display)
2545 ? -1 : 0));
2546 it->selective_display_ellipsis_p
2547 = !NILP (current_buffer->selective_display_ellipses);
2548
2549 /* Display table to use. */
2550 it->dp = window_display_table (w);
2551
2552 /* Are multibyte characters enabled in current_buffer? */
2553 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2554
2555 /* Non-zero if we should highlight the region. */
2556 highlight_region_p
2557 = (!NILP (Vtransient_mark_mode)
2558 && !NILP (current_buffer->mark_active)
2559 && XMARKER (current_buffer->mark)->buffer != 0);
2560
2561 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2562 start and end of a visible region in window IT->w. Set both to
2563 -1 to indicate no region. */
2564 if (highlight_region_p
2565 /* Maybe highlight only in selected window. */
2566 && (/* Either show region everywhere. */
2567 highlight_nonselected_windows
2568 /* Or show region in the selected window. */
2569 || w == XWINDOW (selected_window)
2570 /* Or show the region if we are in the mini-buffer and W is
2571 the window the mini-buffer refers to. */
2572 || (MINI_WINDOW_P (XWINDOW (selected_window))
2573 && WINDOWP (minibuf_selected_window)
2574 && w == XWINDOW (minibuf_selected_window))))
2575 {
2576 int charpos = marker_position (current_buffer->mark);
2577 it->region_beg_charpos = min (PT, charpos);
2578 it->region_end_charpos = max (PT, charpos);
2579 }
2580 else
2581 it->region_beg_charpos = it->region_end_charpos = -1;
2582
2583 /* Get the position at which the redisplay_end_trigger hook should
2584 be run, if it is to be run at all. */
2585 if (MARKERP (w->redisplay_end_trigger)
2586 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2587 it->redisplay_end_trigger_charpos
2588 = marker_position (w->redisplay_end_trigger);
2589 else if (INTEGERP (w->redisplay_end_trigger))
2590 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2591
2592 /* Correct bogus values of tab_width. */
2593 it->tab_width = XINT (current_buffer->tab_width);
2594 if (it->tab_width <= 0 || it->tab_width > 1000)
2595 it->tab_width = 8;
2596
2597 /* Are lines in the display truncated? */
2598 it->truncate_lines_p
2599 = (base_face_id != DEFAULT_FACE_ID
2600 || XINT (it->w->hscroll)
2601 || (truncate_partial_width_windows
2602 && !WINDOW_FULL_WIDTH_P (it->w))
2603 || !NILP (current_buffer->truncate_lines));
2604
2605 /* Get dimensions of truncation and continuation glyphs. These are
2606 displayed as fringe bitmaps under X, so we don't need them for such
2607 frames. */
2608 if (!FRAME_WINDOW_P (it->f))
2609 {
2610 if (it->truncate_lines_p)
2611 {
2612 /* We will need the truncation glyph. */
2613 xassert (it->glyph_row == NULL);
2614 produce_special_glyphs (it, IT_TRUNCATION);
2615 it->truncation_pixel_width = it->pixel_width;
2616 }
2617 else
2618 {
2619 /* We will need the continuation glyph. */
2620 xassert (it->glyph_row == NULL);
2621 produce_special_glyphs (it, IT_CONTINUATION);
2622 it->continuation_pixel_width = it->pixel_width;
2623 }
2624
2625 /* Reset these values to zero because the produce_special_glyphs
2626 above has changed them. */
2627 it->pixel_width = it->ascent = it->descent = 0;
2628 it->phys_ascent = it->phys_descent = 0;
2629 }
2630
2631 /* Set this after getting the dimensions of truncation and
2632 continuation glyphs, so that we don't produce glyphs when calling
2633 produce_special_glyphs, above. */
2634 it->glyph_row = row;
2635 it->area = TEXT_AREA;
2636
2637 /* Get the dimensions of the display area. The display area
2638 consists of the visible window area plus a horizontally scrolled
2639 part to the left of the window. All x-values are relative to the
2640 start of this total display area. */
2641 if (base_face_id != DEFAULT_FACE_ID)
2642 {
2643 /* Mode lines, menu bar in terminal frames. */
2644 it->first_visible_x = 0;
2645 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2646 }
2647 else
2648 {
2649 it->first_visible_x
2650 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2651 it->last_visible_x = (it->first_visible_x
2652 + window_box_width (w, TEXT_AREA));
2653
2654 /* If we truncate lines, leave room for the truncator glyph(s) at
2655 the right margin. Otherwise, leave room for the continuation
2656 glyph(s). Truncation and continuation glyphs are not inserted
2657 for window-based redisplay. */
2658 if (!FRAME_WINDOW_P (it->f))
2659 {
2660 if (it->truncate_lines_p)
2661 it->last_visible_x -= it->truncation_pixel_width;
2662 else
2663 it->last_visible_x -= it->continuation_pixel_width;
2664 }
2665
2666 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2667 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2668 }
2669
2670 /* Leave room for a border glyph. */
2671 if (!FRAME_WINDOW_P (it->f)
2672 && !WINDOW_RIGHTMOST_P (it->w))
2673 it->last_visible_x -= 1;
2674
2675 it->last_visible_y = window_text_bottom_y (w);
2676
2677 /* For mode lines and alike, arrange for the first glyph having a
2678 left box line if the face specifies a box. */
2679 if (base_face_id != DEFAULT_FACE_ID)
2680 {
2681 struct face *face;
2682
2683 it->face_id = base_face_id;
2684
2685 /* If we have a boxed mode line, make the first character appear
2686 with a left box line. */
2687 face = FACE_FROM_ID (it->f, base_face_id);
2688 if (face->box != FACE_NO_BOX)
2689 it->start_of_box_run_p = 1;
2690 }
2691
2692 /* If a buffer position was specified, set the iterator there,
2693 getting overlays and face properties from that position. */
2694 if (charpos >= BUF_BEG (current_buffer))
2695 {
2696 it->end_charpos = ZV;
2697 it->face_id = -1;
2698 IT_CHARPOS (*it) = charpos;
2699
2700 /* Compute byte position if not specified. */
2701 if (bytepos < charpos)
2702 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2703 else
2704 IT_BYTEPOS (*it) = bytepos;
2705
2706 it->start = it->current;
2707
2708 /* Compute faces etc. */
2709 reseat (it, it->current.pos, 1);
2710 }
2711
2712 CHECK_IT (it);
2713 }
2714
2715
2716 /* Initialize IT for the display of window W with window start POS. */
2717
2718 void
2719 start_display (it, w, pos)
2720 struct it *it;
2721 struct window *w;
2722 struct text_pos pos;
2723 {
2724 struct glyph_row *row;
2725 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2726
2727 row = w->desired_matrix->rows + first_vpos;
2728 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2729 it->first_vpos = first_vpos;
2730
2731 /* Don't reseat to previous visible line start if current start
2732 position is in a string or image. */
2733 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2734 {
2735 int start_at_line_beg_p;
2736 int first_y = it->current_y;
2737
2738 /* If window start is not at a line start, skip forward to POS to
2739 get the correct continuation lines width. */
2740 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2741 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2742 if (!start_at_line_beg_p)
2743 {
2744 int new_x;
2745
2746 reseat_at_previous_visible_line_start (it);
2747 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2748
2749 new_x = it->current_x + it->pixel_width;
2750
2751 /* If lines are continued, this line may end in the middle
2752 of a multi-glyph character (e.g. a control character
2753 displayed as \003, or in the middle of an overlay
2754 string). In this case move_it_to above will not have
2755 taken us to the start of the continuation line but to the
2756 end of the continued line. */
2757 if (it->current_x > 0
2758 && !it->truncate_lines_p /* Lines are continued. */
2759 && (/* And glyph doesn't fit on the line. */
2760 new_x > it->last_visible_x
2761 /* Or it fits exactly and we're on a window
2762 system frame. */
2763 || (new_x == it->last_visible_x
2764 && FRAME_WINDOW_P (it->f))))
2765 {
2766 if (it->current.dpvec_index >= 0
2767 || it->current.overlay_string_index >= 0)
2768 {
2769 set_iterator_to_next (it, 1);
2770 move_it_in_display_line_to (it, -1, -1, 0);
2771 }
2772
2773 it->continuation_lines_width += it->current_x;
2774 }
2775
2776 /* We're starting a new display line, not affected by the
2777 height of the continued line, so clear the appropriate
2778 fields in the iterator structure. */
2779 it->max_ascent = it->max_descent = 0;
2780 it->max_phys_ascent = it->max_phys_descent = 0;
2781
2782 it->current_y = first_y;
2783 it->vpos = 0;
2784 it->current_x = it->hpos = 0;
2785 }
2786 }
2787
2788 #if 0 /* Don't assert the following because start_display is sometimes
2789 called intentionally with a window start that is not at a
2790 line start. Please leave this code in as a comment. */
2791
2792 /* Window start should be on a line start, now. */
2793 xassert (it->continuation_lines_width
2794 || IT_CHARPOS (it) == BEGV
2795 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2796 #endif /* 0 */
2797 }
2798
2799
2800 /* Return 1 if POS is a position in ellipses displayed for invisible
2801 text. W is the window we display, for text property lookup. */
2802
2803 static int
2804 in_ellipses_for_invisible_text_p (pos, w)
2805 struct display_pos *pos;
2806 struct window *w;
2807 {
2808 Lisp_Object prop, window;
2809 int ellipses_p = 0;
2810 int charpos = CHARPOS (pos->pos);
2811
2812 /* If POS specifies a position in a display vector, this might
2813 be for an ellipsis displayed for invisible text. We won't
2814 get the iterator set up for delivering that ellipsis unless
2815 we make sure that it gets aware of the invisible text. */
2816 if (pos->dpvec_index >= 0
2817 && pos->overlay_string_index < 0
2818 && CHARPOS (pos->string_pos) < 0
2819 && charpos > BEGV
2820 && (XSETWINDOW (window, w),
2821 prop = Fget_char_property (make_number (charpos),
2822 Qinvisible, window),
2823 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2824 {
2825 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2826 window);
2827 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2828 }
2829
2830 return ellipses_p;
2831 }
2832
2833
2834 /* Initialize IT for stepping through current_buffer in window W,
2835 starting at position POS that includes overlay string and display
2836 vector/ control character translation position information. Value
2837 is zero if there are overlay strings with newlines at POS. */
2838
2839 static int
2840 init_from_display_pos (it, w, pos)
2841 struct it *it;
2842 struct window *w;
2843 struct display_pos *pos;
2844 {
2845 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2846 int i, overlay_strings_with_newlines = 0;
2847
2848 /* If POS specifies a position in a display vector, this might
2849 be for an ellipsis displayed for invisible text. We won't
2850 get the iterator set up for delivering that ellipsis unless
2851 we make sure that it gets aware of the invisible text. */
2852 if (in_ellipses_for_invisible_text_p (pos, w))
2853 {
2854 --charpos;
2855 bytepos = 0;
2856 }
2857
2858 /* Keep in mind: the call to reseat in init_iterator skips invisible
2859 text, so we might end up at a position different from POS. This
2860 is only a problem when POS is a row start after a newline and an
2861 overlay starts there with an after-string, and the overlay has an
2862 invisible property. Since we don't skip invisible text in
2863 display_line and elsewhere immediately after consuming the
2864 newline before the row start, such a POS will not be in a string,
2865 but the call to init_iterator below will move us to the
2866 after-string. */
2867 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2868
2869 /* This only scans the current chunk -- it should scan all chunks.
2870 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2871 to 16 in 22.1 to make this a lesser problem. */
2872 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2873 {
2874 const char *s = SDATA (it->overlay_strings[i]);
2875 const char *e = s + SBYTES (it->overlay_strings[i]);
2876
2877 while (s < e && *s != '\n')
2878 ++s;
2879
2880 if (s < e)
2881 {
2882 overlay_strings_with_newlines = 1;
2883 break;
2884 }
2885 }
2886
2887 /* If position is within an overlay string, set up IT to the right
2888 overlay string. */
2889 if (pos->overlay_string_index >= 0)
2890 {
2891 int relative_index;
2892
2893 /* If the first overlay string happens to have a `display'
2894 property for an image, the iterator will be set up for that
2895 image, and we have to undo that setup first before we can
2896 correct the overlay string index. */
2897 if (it->method == GET_FROM_IMAGE)
2898 pop_it (it);
2899
2900 /* We already have the first chunk of overlay strings in
2901 IT->overlay_strings. Load more until the one for
2902 pos->overlay_string_index is in IT->overlay_strings. */
2903 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2904 {
2905 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2906 it->current.overlay_string_index = 0;
2907 while (n--)
2908 {
2909 load_overlay_strings (it, 0);
2910 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2911 }
2912 }
2913
2914 it->current.overlay_string_index = pos->overlay_string_index;
2915 relative_index = (it->current.overlay_string_index
2916 % OVERLAY_STRING_CHUNK_SIZE);
2917 it->string = it->overlay_strings[relative_index];
2918 xassert (STRINGP (it->string));
2919 it->current.string_pos = pos->string_pos;
2920 it->method = GET_FROM_STRING;
2921 }
2922
2923 #if 0 /* This is bogus because POS not having an overlay string
2924 position does not mean it's after the string. Example: A
2925 line starting with a before-string and initialization of IT
2926 to the previous row's end position. */
2927 else if (it->current.overlay_string_index >= 0)
2928 {
2929 /* If POS says we're already after an overlay string ending at
2930 POS, make sure to pop the iterator because it will be in
2931 front of that overlay string. When POS is ZV, we've thereby
2932 also ``processed'' overlay strings at ZV. */
2933 while (it->sp)
2934 pop_it (it);
2935 xassert (it->current.overlay_string_index == -1);
2936 xassert (it->method == GET_FROM_BUFFER);
2937 if (CHARPOS (pos->pos) == ZV)
2938 it->overlay_strings_at_end_processed_p = 1;
2939 }
2940 #endif /* 0 */
2941
2942 if (CHARPOS (pos->string_pos) >= 0)
2943 {
2944 /* Recorded position is not in an overlay string, but in another
2945 string. This can only be a string from a `display' property.
2946 IT should already be filled with that string. */
2947 it->current.string_pos = pos->string_pos;
2948 xassert (STRINGP (it->string));
2949 }
2950
2951 /* Restore position in display vector translations, control
2952 character translations or ellipses. */
2953 if (pos->dpvec_index >= 0)
2954 {
2955 if (it->dpvec == NULL)
2956 get_next_display_element (it);
2957 xassert (it->dpvec && it->current.dpvec_index == 0);
2958 it->current.dpvec_index = pos->dpvec_index;
2959 }
2960
2961 CHECK_IT (it);
2962 return !overlay_strings_with_newlines;
2963 }
2964
2965
2966 /* Initialize IT for stepping through current_buffer in window W
2967 starting at ROW->start. */
2968
2969 static void
2970 init_to_row_start (it, w, row)
2971 struct it *it;
2972 struct window *w;
2973 struct glyph_row *row;
2974 {
2975 init_from_display_pos (it, w, &row->start);
2976 it->start = row->start;
2977 it->continuation_lines_width = row->continuation_lines_width;
2978 CHECK_IT (it);
2979 }
2980
2981
2982 /* Initialize IT for stepping through current_buffer in window W
2983 starting in the line following ROW, i.e. starting at ROW->end.
2984 Value is zero if there are overlay strings with newlines at ROW's
2985 end position. */
2986
2987 static int
2988 init_to_row_end (it, w, row)
2989 struct it *it;
2990 struct window *w;
2991 struct glyph_row *row;
2992 {
2993 int success = 0;
2994
2995 if (init_from_display_pos (it, w, &row->end))
2996 {
2997 if (row->continued_p)
2998 it->continuation_lines_width
2999 = row->continuation_lines_width + row->pixel_width;
3000 CHECK_IT (it);
3001 success = 1;
3002 }
3003
3004 return success;
3005 }
3006
3007
3008
3009 \f
3010 /***********************************************************************
3011 Text properties
3012 ***********************************************************************/
3013
3014 /* Called when IT reaches IT->stop_charpos. Handle text property and
3015 overlay changes. Set IT->stop_charpos to the next position where
3016 to stop. */
3017
3018 static void
3019 handle_stop (it)
3020 struct it *it;
3021 {
3022 enum prop_handled handled;
3023 int handle_overlay_change_p;
3024 struct props *p;
3025
3026 it->dpvec = NULL;
3027 it->current.dpvec_index = -1;
3028 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3029 it->ignore_overlay_strings_at_pos_p = 0;
3030
3031 /* Use face of preceding text for ellipsis (if invisible) */
3032 if (it->selective_display_ellipsis_p)
3033 it->saved_face_id = it->face_id;
3034
3035 do
3036 {
3037 handled = HANDLED_NORMALLY;
3038
3039 /* Call text property handlers. */
3040 for (p = it_props; p->handler; ++p)
3041 {
3042 handled = p->handler (it);
3043
3044 if (handled == HANDLED_RECOMPUTE_PROPS)
3045 break;
3046 else if (handled == HANDLED_RETURN)
3047 {
3048 /* We still want to show before and after strings from
3049 overlays even if the actual buffer text is replaced. */
3050 if (!handle_overlay_change_p || it->sp > 1)
3051 return;
3052 if (!get_overlay_strings_1 (it, 0, 0))
3053 return;
3054 it->ignore_overlay_strings_at_pos_p = 1;
3055 it->string_from_display_prop_p = 0;
3056 handle_overlay_change_p = 0;
3057 handled = HANDLED_RECOMPUTE_PROPS;
3058 break;
3059 }
3060 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3061 handle_overlay_change_p = 0;
3062 }
3063
3064 if (handled != HANDLED_RECOMPUTE_PROPS)
3065 {
3066 /* Don't check for overlay strings below when set to deliver
3067 characters from a display vector. */
3068 if (it->method == GET_FROM_DISPLAY_VECTOR)
3069 handle_overlay_change_p = 0;
3070
3071 /* Handle overlay changes. */
3072 if (handle_overlay_change_p)
3073 handled = handle_overlay_change (it);
3074
3075 /* Determine where to stop next. */
3076 if (handled == HANDLED_NORMALLY)
3077 compute_stop_pos (it);
3078 }
3079 }
3080 while (handled == HANDLED_RECOMPUTE_PROPS);
3081 }
3082
3083
3084 /* Compute IT->stop_charpos from text property and overlay change
3085 information for IT's current position. */
3086
3087 static void
3088 compute_stop_pos (it)
3089 struct it *it;
3090 {
3091 register INTERVAL iv, next_iv;
3092 Lisp_Object object, limit, position;
3093
3094 /* If nowhere else, stop at the end. */
3095 it->stop_charpos = it->end_charpos;
3096
3097 if (STRINGP (it->string))
3098 {
3099 /* Strings are usually short, so don't limit the search for
3100 properties. */
3101 object = it->string;
3102 limit = Qnil;
3103 position = make_number (IT_STRING_CHARPOS (*it));
3104 }
3105 else
3106 {
3107 int charpos;
3108
3109 /* If next overlay change is in front of the current stop pos
3110 (which is IT->end_charpos), stop there. Note: value of
3111 next_overlay_change is point-max if no overlay change
3112 follows. */
3113 charpos = next_overlay_change (IT_CHARPOS (*it));
3114 if (charpos < it->stop_charpos)
3115 it->stop_charpos = charpos;
3116
3117 /* If showing the region, we have to stop at the region
3118 start or end because the face might change there. */
3119 if (it->region_beg_charpos > 0)
3120 {
3121 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3122 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3123 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3124 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3125 }
3126
3127 /* Set up variables for computing the stop position from text
3128 property changes. */
3129 XSETBUFFER (object, current_buffer);
3130 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3131 position = make_number (IT_CHARPOS (*it));
3132
3133 }
3134
3135 /* Get the interval containing IT's position. Value is a null
3136 interval if there isn't such an interval. */
3137 iv = validate_interval_range (object, &position, &position, 0);
3138 if (!NULL_INTERVAL_P (iv))
3139 {
3140 Lisp_Object values_here[LAST_PROP_IDX];
3141 struct props *p;
3142
3143 /* Get properties here. */
3144 for (p = it_props; p->handler; ++p)
3145 values_here[p->idx] = textget (iv->plist, *p->name);
3146
3147 /* Look for an interval following iv that has different
3148 properties. */
3149 for (next_iv = next_interval (iv);
3150 (!NULL_INTERVAL_P (next_iv)
3151 && (NILP (limit)
3152 || XFASTINT (limit) > next_iv->position));
3153 next_iv = next_interval (next_iv))
3154 {
3155 for (p = it_props; p->handler; ++p)
3156 {
3157 Lisp_Object new_value;
3158
3159 new_value = textget (next_iv->plist, *p->name);
3160 if (!EQ (values_here[p->idx], new_value))
3161 break;
3162 }
3163
3164 if (p->handler)
3165 break;
3166 }
3167
3168 if (!NULL_INTERVAL_P (next_iv))
3169 {
3170 if (INTEGERP (limit)
3171 && next_iv->position >= XFASTINT (limit))
3172 /* No text property change up to limit. */
3173 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3174 else
3175 /* Text properties change in next_iv. */
3176 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3177 }
3178 }
3179
3180 xassert (STRINGP (it->string)
3181 || (it->stop_charpos >= BEGV
3182 && it->stop_charpos >= IT_CHARPOS (*it)));
3183 }
3184
3185
3186 /* Return the position of the next overlay change after POS in
3187 current_buffer. Value is point-max if no overlay change
3188 follows. This is like `next-overlay-change' but doesn't use
3189 xmalloc. */
3190
3191 static int
3192 next_overlay_change (pos)
3193 int pos;
3194 {
3195 int noverlays;
3196 int endpos;
3197 Lisp_Object *overlays;
3198 int i;
3199
3200 /* Get all overlays at the given position. */
3201 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3202
3203 /* If any of these overlays ends before endpos,
3204 use its ending point instead. */
3205 for (i = 0; i < noverlays; ++i)
3206 {
3207 Lisp_Object oend;
3208 int oendpos;
3209
3210 oend = OVERLAY_END (overlays[i]);
3211 oendpos = OVERLAY_POSITION (oend);
3212 endpos = min (endpos, oendpos);
3213 }
3214
3215 return endpos;
3216 }
3217
3218
3219 \f
3220 /***********************************************************************
3221 Fontification
3222 ***********************************************************************/
3223
3224 /* Handle changes in the `fontified' property of the current buffer by
3225 calling hook functions from Qfontification_functions to fontify
3226 regions of text. */
3227
3228 static enum prop_handled
3229 handle_fontified_prop (it)
3230 struct it *it;
3231 {
3232 Lisp_Object prop, pos;
3233 enum prop_handled handled = HANDLED_NORMALLY;
3234
3235 if (!NILP (Vmemory_full))
3236 return handled;
3237
3238 /* Get the value of the `fontified' property at IT's current buffer
3239 position. (The `fontified' property doesn't have a special
3240 meaning in strings.) If the value is nil, call functions from
3241 Qfontification_functions. */
3242 if (!STRINGP (it->string)
3243 && it->s == NULL
3244 && !NILP (Vfontification_functions)
3245 && !NILP (Vrun_hooks)
3246 && (pos = make_number (IT_CHARPOS (*it)),
3247 prop = Fget_char_property (pos, Qfontified, Qnil),
3248 NILP (prop)))
3249 {
3250 int count = SPECPDL_INDEX ();
3251 Lisp_Object val;
3252
3253 val = Vfontification_functions;
3254 specbind (Qfontification_functions, Qnil);
3255
3256 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3257 safe_call1 (val, pos);
3258 else
3259 {
3260 Lisp_Object globals, fn;
3261 struct gcpro gcpro1, gcpro2;
3262
3263 globals = Qnil;
3264 GCPRO2 (val, globals);
3265
3266 for (; CONSP (val); val = XCDR (val))
3267 {
3268 fn = XCAR (val);
3269
3270 if (EQ (fn, Qt))
3271 {
3272 /* A value of t indicates this hook has a local
3273 binding; it means to run the global binding too.
3274 In a global value, t should not occur. If it
3275 does, we must ignore it to avoid an endless
3276 loop. */
3277 for (globals = Fdefault_value (Qfontification_functions);
3278 CONSP (globals);
3279 globals = XCDR (globals))
3280 {
3281 fn = XCAR (globals);
3282 if (!EQ (fn, Qt))
3283 safe_call1 (fn, pos);
3284 }
3285 }
3286 else
3287 safe_call1 (fn, pos);
3288 }
3289
3290 UNGCPRO;
3291 }
3292
3293 unbind_to (count, Qnil);
3294
3295 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3296 something. This avoids an endless loop if they failed to
3297 fontify the text for which reason ever. */
3298 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3299 handled = HANDLED_RECOMPUTE_PROPS;
3300 }
3301
3302 return handled;
3303 }
3304
3305
3306 \f
3307 /***********************************************************************
3308 Faces
3309 ***********************************************************************/
3310
3311 /* Set up iterator IT from face properties at its current position.
3312 Called from handle_stop. */
3313
3314 static enum prop_handled
3315 handle_face_prop (it)
3316 struct it *it;
3317 {
3318 int new_face_id, next_stop;
3319
3320 if (!STRINGP (it->string))
3321 {
3322 new_face_id
3323 = face_at_buffer_position (it->w,
3324 IT_CHARPOS (*it),
3325 it->region_beg_charpos,
3326 it->region_end_charpos,
3327 &next_stop,
3328 (IT_CHARPOS (*it)
3329 + TEXT_PROP_DISTANCE_LIMIT),
3330 0);
3331
3332 /* Is this a start of a run of characters with box face?
3333 Caveat: this can be called for a freshly initialized
3334 iterator; face_id is -1 in this case. We know that the new
3335 face will not change until limit, i.e. if the new face has a
3336 box, all characters up to limit will have one. But, as
3337 usual, we don't know whether limit is really the end. */
3338 if (new_face_id != it->face_id)
3339 {
3340 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3341
3342 /* If new face has a box but old face has not, this is
3343 the start of a run of characters with box, i.e. it has
3344 a shadow on the left side. The value of face_id of the
3345 iterator will be -1 if this is the initial call that gets
3346 the face. In this case, we have to look in front of IT's
3347 position and see whether there is a face != new_face_id. */
3348 it->start_of_box_run_p
3349 = (new_face->box != FACE_NO_BOX
3350 && (it->face_id >= 0
3351 || IT_CHARPOS (*it) == BEG
3352 || new_face_id != face_before_it_pos (it)));
3353 it->face_box_p = new_face->box != FACE_NO_BOX;
3354 }
3355 }
3356 else
3357 {
3358 int base_face_id, bufpos;
3359
3360 if (it->current.overlay_string_index >= 0)
3361 bufpos = IT_CHARPOS (*it);
3362 else
3363 bufpos = 0;
3364
3365 /* For strings from a buffer, i.e. overlay strings or strings
3366 from a `display' property, use the face at IT's current
3367 buffer position as the base face to merge with, so that
3368 overlay strings appear in the same face as surrounding
3369 text, unless they specify their own faces. */
3370 base_face_id = underlying_face_id (it);
3371
3372 new_face_id = face_at_string_position (it->w,
3373 it->string,
3374 IT_STRING_CHARPOS (*it),
3375 bufpos,
3376 it->region_beg_charpos,
3377 it->region_end_charpos,
3378 &next_stop,
3379 base_face_id, 0);
3380
3381 #if 0 /* This shouldn't be neccessary. Let's check it. */
3382 /* If IT is used to display a mode line we would really like to
3383 use the mode line face instead of the frame's default face. */
3384 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3385 && new_face_id == DEFAULT_FACE_ID)
3386 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3387 #endif
3388
3389 /* Is this a start of a run of characters with box? Caveat:
3390 this can be called for a freshly allocated iterator; face_id
3391 is -1 is this case. We know that the new face will not
3392 change until the next check pos, i.e. if the new face has a
3393 box, all characters up to that position will have a
3394 box. But, as usual, we don't know whether that position
3395 is really the end. */
3396 if (new_face_id != it->face_id)
3397 {
3398 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3399 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3400
3401 /* If new face has a box but old face hasn't, this is the
3402 start of a run of characters with box, i.e. it has a
3403 shadow on the left side. */
3404 it->start_of_box_run_p
3405 = new_face->box && (old_face == NULL || !old_face->box);
3406 it->face_box_p = new_face->box != FACE_NO_BOX;
3407 }
3408 }
3409
3410 it->face_id = new_face_id;
3411 return HANDLED_NORMALLY;
3412 }
3413
3414
3415 /* Return the ID of the face ``underlying'' IT's current position,
3416 which is in a string. If the iterator is associated with a
3417 buffer, return the face at IT's current buffer position.
3418 Otherwise, use the iterator's base_face_id. */
3419
3420 static int
3421 underlying_face_id (it)
3422 struct it *it;
3423 {
3424 int face_id = it->base_face_id, i;
3425
3426 xassert (STRINGP (it->string));
3427
3428 for (i = it->sp - 1; i >= 0; --i)
3429 if (NILP (it->stack[i].string))
3430 face_id = it->stack[i].face_id;
3431
3432 return face_id;
3433 }
3434
3435
3436 /* Compute the face one character before or after the current position
3437 of IT. BEFORE_P non-zero means get the face in front of IT's
3438 position. Value is the id of the face. */
3439
3440 static int
3441 face_before_or_after_it_pos (it, before_p)
3442 struct it *it;
3443 int before_p;
3444 {
3445 int face_id, limit;
3446 int next_check_charpos;
3447 struct text_pos pos;
3448
3449 xassert (it->s == NULL);
3450
3451 if (STRINGP (it->string))
3452 {
3453 int bufpos, base_face_id;
3454
3455 /* No face change past the end of the string (for the case
3456 we are padding with spaces). No face change before the
3457 string start. */
3458 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3459 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3460 return it->face_id;
3461
3462 /* Set pos to the position before or after IT's current position. */
3463 if (before_p)
3464 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3465 else
3466 /* For composition, we must check the character after the
3467 composition. */
3468 pos = (it->what == IT_COMPOSITION
3469 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3470 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3471
3472 if (it->current.overlay_string_index >= 0)
3473 bufpos = IT_CHARPOS (*it);
3474 else
3475 bufpos = 0;
3476
3477 base_face_id = underlying_face_id (it);
3478
3479 /* Get the face for ASCII, or unibyte. */
3480 face_id = face_at_string_position (it->w,
3481 it->string,
3482 CHARPOS (pos),
3483 bufpos,
3484 it->region_beg_charpos,
3485 it->region_end_charpos,
3486 &next_check_charpos,
3487 base_face_id, 0);
3488
3489 /* Correct the face for charsets different from ASCII. Do it
3490 for the multibyte case only. The face returned above is
3491 suitable for unibyte text if IT->string is unibyte. */
3492 if (STRING_MULTIBYTE (it->string))
3493 {
3494 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3495 int rest = SBYTES (it->string) - BYTEPOS (pos);
3496 int c, len;
3497 struct face *face = FACE_FROM_ID (it->f, face_id);
3498
3499 c = string_char_and_length (p, rest, &len);
3500 face_id = FACE_FOR_CHAR (it->f, face, c);
3501 }
3502 }
3503 else
3504 {
3505 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3506 || (IT_CHARPOS (*it) <= BEGV && before_p))
3507 return it->face_id;
3508
3509 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3510 pos = it->current.pos;
3511
3512 if (before_p)
3513 DEC_TEXT_POS (pos, it->multibyte_p);
3514 else
3515 {
3516 if (it->what == IT_COMPOSITION)
3517 /* For composition, we must check the position after the
3518 composition. */
3519 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3520 else
3521 INC_TEXT_POS (pos, it->multibyte_p);
3522 }
3523
3524 /* Determine face for CHARSET_ASCII, or unibyte. */
3525 face_id = face_at_buffer_position (it->w,
3526 CHARPOS (pos),
3527 it->region_beg_charpos,
3528 it->region_end_charpos,
3529 &next_check_charpos,
3530 limit, 0);
3531
3532 /* Correct the face for charsets different from ASCII. Do it
3533 for the multibyte case only. The face returned above is
3534 suitable for unibyte text if current_buffer is unibyte. */
3535 if (it->multibyte_p)
3536 {
3537 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3538 struct face *face = FACE_FROM_ID (it->f, face_id);
3539 face_id = FACE_FOR_CHAR (it->f, face, c);
3540 }
3541 }
3542
3543 return face_id;
3544 }
3545
3546
3547 \f
3548 /***********************************************************************
3549 Invisible text
3550 ***********************************************************************/
3551
3552 /* Set up iterator IT from invisible properties at its current
3553 position. Called from handle_stop. */
3554
3555 static enum prop_handled
3556 handle_invisible_prop (it)
3557 struct it *it;
3558 {
3559 enum prop_handled handled = HANDLED_NORMALLY;
3560
3561 if (STRINGP (it->string))
3562 {
3563 extern Lisp_Object Qinvisible;
3564 Lisp_Object prop, end_charpos, limit, charpos;
3565
3566 /* Get the value of the invisible text property at the
3567 current position. Value will be nil if there is no such
3568 property. */
3569 charpos = make_number (IT_STRING_CHARPOS (*it));
3570 prop = Fget_text_property (charpos, Qinvisible, it->string);
3571
3572 if (!NILP (prop)
3573 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3574 {
3575 handled = HANDLED_RECOMPUTE_PROPS;
3576
3577 /* Get the position at which the next change of the
3578 invisible text property can be found in IT->string.
3579 Value will be nil if the property value is the same for
3580 all the rest of IT->string. */
3581 XSETINT (limit, SCHARS (it->string));
3582 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3583 it->string, limit);
3584
3585 /* Text at current position is invisible. The next
3586 change in the property is at position end_charpos.
3587 Move IT's current position to that position. */
3588 if (INTEGERP (end_charpos)
3589 && XFASTINT (end_charpos) < XFASTINT (limit))
3590 {
3591 struct text_pos old;
3592 old = it->current.string_pos;
3593 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3594 compute_string_pos (&it->current.string_pos, old, it->string);
3595 }
3596 else
3597 {
3598 /* The rest of the string is invisible. If this is an
3599 overlay string, proceed with the next overlay string
3600 or whatever comes and return a character from there. */
3601 if (it->current.overlay_string_index >= 0)
3602 {
3603 next_overlay_string (it);
3604 /* Don't check for overlay strings when we just
3605 finished processing them. */
3606 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3607 }
3608 else
3609 {
3610 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3611 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3612 }
3613 }
3614 }
3615 }
3616 else
3617 {
3618 int invis_p, newpos, next_stop, start_charpos;
3619 Lisp_Object pos, prop, overlay;
3620
3621 /* First of all, is there invisible text at this position? */
3622 start_charpos = IT_CHARPOS (*it);
3623 pos = make_number (IT_CHARPOS (*it));
3624 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3625 &overlay);
3626 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3627
3628 /* If we are on invisible text, skip over it. */
3629 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3630 {
3631 /* Record whether we have to display an ellipsis for the
3632 invisible text. */
3633 int display_ellipsis_p = invis_p == 2;
3634
3635 handled = HANDLED_RECOMPUTE_PROPS;
3636
3637 /* Loop skipping over invisible text. The loop is left at
3638 ZV or with IT on the first char being visible again. */
3639 do
3640 {
3641 /* Try to skip some invisible text. Return value is the
3642 position reached which can be equal to IT's position
3643 if there is nothing invisible here. This skips both
3644 over invisible text properties and overlays with
3645 invisible property. */
3646 newpos = skip_invisible (IT_CHARPOS (*it),
3647 &next_stop, ZV, it->window);
3648
3649 /* If we skipped nothing at all we weren't at invisible
3650 text in the first place. If everything to the end of
3651 the buffer was skipped, end the loop. */
3652 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3653 invis_p = 0;
3654 else
3655 {
3656 /* We skipped some characters but not necessarily
3657 all there are. Check if we ended up on visible
3658 text. Fget_char_property returns the property of
3659 the char before the given position, i.e. if we
3660 get invis_p = 0, this means that the char at
3661 newpos is visible. */
3662 pos = make_number (newpos);
3663 prop = Fget_char_property (pos, Qinvisible, it->window);
3664 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3665 }
3666
3667 /* If we ended up on invisible text, proceed to
3668 skip starting with next_stop. */
3669 if (invis_p)
3670 IT_CHARPOS (*it) = next_stop;
3671
3672 /* If there are adjacent invisible texts, don't lose the
3673 second one's ellipsis. */
3674 if (invis_p == 2)
3675 display_ellipsis_p = 1;
3676 }
3677 while (invis_p);
3678
3679 /* The position newpos is now either ZV or on visible text. */
3680 IT_CHARPOS (*it) = newpos;
3681 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3682
3683 /* If there are before-strings at the start of invisible
3684 text, and the text is invisible because of a text
3685 property, arrange to show before-strings because 20.x did
3686 it that way. (If the text is invisible because of an
3687 overlay property instead of a text property, this is
3688 already handled in the overlay code.) */
3689 if (NILP (overlay)
3690 && get_overlay_strings (it, start_charpos))
3691 {
3692 handled = HANDLED_RECOMPUTE_PROPS;
3693 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3694 }
3695 else if (display_ellipsis_p)
3696 {
3697 /* Make sure that the glyphs of the ellipsis will get
3698 correct `charpos' values. If we would not update
3699 it->position here, the glyphs would belong to the
3700 last visible character _before_ the invisible
3701 text, which confuses `set_cursor_from_row'.
3702
3703 We use the last invisible position instead of the
3704 first because this way the cursor is always drawn on
3705 the first "." of the ellipsis, whenever PT is inside
3706 the invisible text. Otherwise the cursor would be
3707 placed _after_ the ellipsis when the point is after the
3708 first invisible character. */
3709 if (!STRINGP (it->object))
3710 {
3711 it->position.charpos = IT_CHARPOS (*it) - 1;
3712 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3713 }
3714 setup_for_ellipsis (it, 0);
3715 }
3716 }
3717 }
3718
3719 return handled;
3720 }
3721
3722
3723 /* Make iterator IT return `...' next.
3724 Replaces LEN characters from buffer. */
3725
3726 static void
3727 setup_for_ellipsis (it, len)
3728 struct it *it;
3729 int len;
3730 {
3731 /* Use the display table definition for `...'. Invalid glyphs
3732 will be handled by the method returning elements from dpvec. */
3733 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3734 {
3735 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3736 it->dpvec = v->contents;
3737 it->dpend = v->contents + v->size;
3738 }
3739 else
3740 {
3741 /* Default `...'. */
3742 it->dpvec = default_invis_vector;
3743 it->dpend = default_invis_vector + 3;
3744 }
3745
3746 it->dpvec_char_len = len;
3747 it->current.dpvec_index = 0;
3748 it->dpvec_face_id = -1;
3749
3750 /* Remember the current face id in case glyphs specify faces.
3751 IT's face is restored in set_iterator_to_next.
3752 saved_face_id was set to preceding char's face in handle_stop. */
3753 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3754 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3755
3756 it->method = GET_FROM_DISPLAY_VECTOR;
3757 it->ellipsis_p = 1;
3758 }
3759
3760
3761 \f
3762 /***********************************************************************
3763 'display' property
3764 ***********************************************************************/
3765
3766 /* Set up iterator IT from `display' property at its current position.
3767 Called from handle_stop.
3768 We return HANDLED_RETURN if some part of the display property
3769 overrides the display of the buffer text itself.
3770 Otherwise we return HANDLED_NORMALLY. */
3771
3772 static enum prop_handled
3773 handle_display_prop (it)
3774 struct it *it;
3775 {
3776 Lisp_Object prop, object;
3777 struct text_pos *position;
3778 /* Nonzero if some property replaces the display of the text itself. */
3779 int display_replaced_p = 0;
3780
3781 if (STRINGP (it->string))
3782 {
3783 object = it->string;
3784 position = &it->current.string_pos;
3785 }
3786 else
3787 {
3788 XSETWINDOW (object, it->w);
3789 position = &it->current.pos;
3790 }
3791
3792 /* Reset those iterator values set from display property values. */
3793 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3794 it->space_width = Qnil;
3795 it->font_height = Qnil;
3796 it->voffset = 0;
3797
3798 /* We don't support recursive `display' properties, i.e. string
3799 values that have a string `display' property, that have a string
3800 `display' property etc. */
3801 if (!it->string_from_display_prop_p)
3802 it->area = TEXT_AREA;
3803
3804 prop = Fget_char_property (make_number (position->charpos),
3805 Qdisplay, object);
3806 if (NILP (prop))
3807 return HANDLED_NORMALLY;
3808
3809 if (!STRINGP (it->string))
3810 object = it->w->buffer;
3811
3812 if (CONSP (prop)
3813 /* Simple properties. */
3814 && !EQ (XCAR (prop), Qimage)
3815 && !EQ (XCAR (prop), Qspace)
3816 && !EQ (XCAR (prop), Qwhen)
3817 && !EQ (XCAR (prop), Qslice)
3818 && !EQ (XCAR (prop), Qspace_width)
3819 && !EQ (XCAR (prop), Qheight)
3820 && !EQ (XCAR (prop), Qraise)
3821 /* Marginal area specifications. */
3822 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3823 && !EQ (XCAR (prop), Qleft_fringe)
3824 && !EQ (XCAR (prop), Qright_fringe)
3825 && !NILP (XCAR (prop)))
3826 {
3827 for (; CONSP (prop); prop = XCDR (prop))
3828 {
3829 if (handle_single_display_spec (it, XCAR (prop), object,
3830 position, display_replaced_p))
3831 display_replaced_p = 1;
3832 }
3833 }
3834 else if (VECTORP (prop))
3835 {
3836 int i;
3837 for (i = 0; i < ASIZE (prop); ++i)
3838 if (handle_single_display_spec (it, AREF (prop, i), object,
3839 position, display_replaced_p))
3840 display_replaced_p = 1;
3841 }
3842 else
3843 {
3844 int ret = handle_single_display_spec (it, prop, object, position, 0);
3845 if (ret < 0) /* Replaced by "", i.e. nothing. */
3846 return HANDLED_RECOMPUTE_PROPS;
3847 if (ret)
3848 display_replaced_p = 1;
3849 }
3850
3851 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3852 }
3853
3854
3855 /* Value is the position of the end of the `display' property starting
3856 at START_POS in OBJECT. */
3857
3858 static struct text_pos
3859 display_prop_end (it, object, start_pos)
3860 struct it *it;
3861 Lisp_Object object;
3862 struct text_pos start_pos;
3863 {
3864 Lisp_Object end;
3865 struct text_pos end_pos;
3866
3867 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3868 Qdisplay, object, Qnil);
3869 CHARPOS (end_pos) = XFASTINT (end);
3870 if (STRINGP (object))
3871 compute_string_pos (&end_pos, start_pos, it->string);
3872 else
3873 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3874
3875 return end_pos;
3876 }
3877
3878
3879 /* Set up IT from a single `display' specification PROP. OBJECT
3880 is the object in which the `display' property was found. *POSITION
3881 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3882 means that we previously saw a display specification which already
3883 replaced text display with something else, for example an image;
3884 we ignore such properties after the first one has been processed.
3885
3886 If PROP is a `space' or `image' specification, and in some other
3887 cases too, set *POSITION to the position where the `display'
3888 property ends.
3889
3890 Value is non-zero if something was found which replaces the display
3891 of buffer or string text. Specifically, the value is -1 if that
3892 "something" is "nothing". */
3893
3894 static int
3895 handle_single_display_spec (it, spec, object, position,
3896 display_replaced_before_p)
3897 struct it *it;
3898 Lisp_Object spec;
3899 Lisp_Object object;
3900 struct text_pos *position;
3901 int display_replaced_before_p;
3902 {
3903 Lisp_Object form;
3904 Lisp_Object location, value;
3905 struct text_pos start_pos, save_pos;
3906 int valid_p;
3907
3908 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3909 If the result is non-nil, use VALUE instead of SPEC. */
3910 form = Qt;
3911 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3912 {
3913 spec = XCDR (spec);
3914 if (!CONSP (spec))
3915 return 0;
3916 form = XCAR (spec);
3917 spec = XCDR (spec);
3918 }
3919
3920 if (!NILP (form) && !EQ (form, Qt))
3921 {
3922 int count = SPECPDL_INDEX ();
3923 struct gcpro gcpro1;
3924
3925 /* Bind `object' to the object having the `display' property, a
3926 buffer or string. Bind `position' to the position in the
3927 object where the property was found, and `buffer-position'
3928 to the current position in the buffer. */
3929 specbind (Qobject, object);
3930 specbind (Qposition, make_number (CHARPOS (*position)));
3931 specbind (Qbuffer_position,
3932 make_number (STRINGP (object)
3933 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3934 GCPRO1 (form);
3935 form = safe_eval (form);
3936 UNGCPRO;
3937 unbind_to (count, Qnil);
3938 }
3939
3940 if (NILP (form))
3941 return 0;
3942
3943 /* Handle `(height HEIGHT)' specifications. */
3944 if (CONSP (spec)
3945 && EQ (XCAR (spec), Qheight)
3946 && CONSP (XCDR (spec)))
3947 {
3948 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3949 return 0;
3950
3951 it->font_height = XCAR (XCDR (spec));
3952 if (!NILP (it->font_height))
3953 {
3954 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3955 int new_height = -1;
3956
3957 if (CONSP (it->font_height)
3958 && (EQ (XCAR (it->font_height), Qplus)
3959 || EQ (XCAR (it->font_height), Qminus))
3960 && CONSP (XCDR (it->font_height))
3961 && INTEGERP (XCAR (XCDR (it->font_height))))
3962 {
3963 /* `(+ N)' or `(- N)' where N is an integer. */
3964 int steps = XINT (XCAR (XCDR (it->font_height)));
3965 if (EQ (XCAR (it->font_height), Qplus))
3966 steps = - steps;
3967 it->face_id = smaller_face (it->f, it->face_id, steps);
3968 }
3969 else if (FUNCTIONP (it->font_height))
3970 {
3971 /* Call function with current height as argument.
3972 Value is the new height. */
3973 Lisp_Object height;
3974 height = safe_call1 (it->font_height,
3975 face->lface[LFACE_HEIGHT_INDEX]);
3976 if (NUMBERP (height))
3977 new_height = XFLOATINT (height);
3978 }
3979 else if (NUMBERP (it->font_height))
3980 {
3981 /* Value is a multiple of the canonical char height. */
3982 struct face *face;
3983
3984 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3985 new_height = (XFLOATINT (it->font_height)
3986 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3987 }
3988 else
3989 {
3990 /* Evaluate IT->font_height with `height' bound to the
3991 current specified height to get the new height. */
3992 int count = SPECPDL_INDEX ();
3993
3994 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3995 value = safe_eval (it->font_height);
3996 unbind_to (count, Qnil);
3997
3998 if (NUMBERP (value))
3999 new_height = XFLOATINT (value);
4000 }
4001
4002 if (new_height > 0)
4003 it->face_id = face_with_height (it->f, it->face_id, new_height);
4004 }
4005
4006 return 0;
4007 }
4008
4009 /* Handle `(space_width WIDTH)'. */
4010 if (CONSP (spec)
4011 && EQ (XCAR (spec), Qspace_width)
4012 && CONSP (XCDR (spec)))
4013 {
4014 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4015 return 0;
4016
4017 value = XCAR (XCDR (spec));
4018 if (NUMBERP (value) && XFLOATINT (value) > 0)
4019 it->space_width = value;
4020
4021 return 0;
4022 }
4023
4024 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4025 if (CONSP (spec)
4026 && EQ (XCAR (spec), Qslice))
4027 {
4028 Lisp_Object tem;
4029
4030 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4031 return 0;
4032
4033 if (tem = XCDR (spec), CONSP (tem))
4034 {
4035 it->slice.x = XCAR (tem);
4036 if (tem = XCDR (tem), CONSP (tem))
4037 {
4038 it->slice.y = XCAR (tem);
4039 if (tem = XCDR (tem), CONSP (tem))
4040 {
4041 it->slice.width = XCAR (tem);
4042 if (tem = XCDR (tem), CONSP (tem))
4043 it->slice.height = XCAR (tem);
4044 }
4045 }
4046 }
4047
4048 return 0;
4049 }
4050
4051 /* Handle `(raise FACTOR)'. */
4052 if (CONSP (spec)
4053 && EQ (XCAR (spec), Qraise)
4054 && CONSP (XCDR (spec)))
4055 {
4056 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4057 return 0;
4058
4059 #ifdef HAVE_WINDOW_SYSTEM
4060 value = XCAR (XCDR (spec));
4061 if (NUMBERP (value))
4062 {
4063 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4064 it->voffset = - (XFLOATINT (value)
4065 * (FONT_HEIGHT (face->font)));
4066 }
4067 #endif /* HAVE_WINDOW_SYSTEM */
4068
4069 return 0;
4070 }
4071
4072 /* Don't handle the other kinds of display specifications
4073 inside a string that we got from a `display' property. */
4074 if (it->string_from_display_prop_p)
4075 return 0;
4076
4077 /* Characters having this form of property are not displayed, so
4078 we have to find the end of the property. */
4079 start_pos = *position;
4080 *position = display_prop_end (it, object, start_pos);
4081 value = Qnil;
4082
4083 /* Stop the scan at that end position--we assume that all
4084 text properties change there. */
4085 it->stop_charpos = position->charpos;
4086
4087 /* Handle `(left-fringe BITMAP [FACE])'
4088 and `(right-fringe BITMAP [FACE])'. */
4089 if (CONSP (spec)
4090 && (EQ (XCAR (spec), Qleft_fringe)
4091 || EQ (XCAR (spec), Qright_fringe))
4092 && CONSP (XCDR (spec)))
4093 {
4094 int face_id = DEFAULT_FACE_ID;
4095 int fringe_bitmap;
4096
4097 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4098 /* If we return here, POSITION has been advanced
4099 across the text with this property. */
4100 return 0;
4101
4102 #ifdef HAVE_WINDOW_SYSTEM
4103 value = XCAR (XCDR (spec));
4104 if (!SYMBOLP (value)
4105 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4106 /* If we return here, POSITION has been advanced
4107 across the text with this property. */
4108 return 0;
4109
4110 if (CONSP (XCDR (XCDR (spec))))
4111 {
4112 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4113 int face_id2 = lookup_derived_face (it->f, face_name,
4114 'A', FRINGE_FACE_ID, 0);
4115 if (face_id2 >= 0)
4116 face_id = face_id2;
4117 }
4118
4119 /* Save current settings of IT so that we can restore them
4120 when we are finished with the glyph property value. */
4121
4122 save_pos = it->position;
4123 it->position = *position;
4124 push_it (it);
4125 it->position = save_pos;
4126
4127 it->area = TEXT_AREA;
4128 it->what = IT_IMAGE;
4129 it->image_id = -1; /* no image */
4130 it->position = start_pos;
4131 it->object = NILP (object) ? it->w->buffer : object;
4132 it->method = GET_FROM_IMAGE;
4133 it->face_id = face_id;
4134
4135 /* Say that we haven't consumed the characters with
4136 `display' property yet. The call to pop_it in
4137 set_iterator_to_next will clean this up. */
4138 *position = start_pos;
4139
4140 if (EQ (XCAR (spec), Qleft_fringe))
4141 {
4142 it->left_user_fringe_bitmap = fringe_bitmap;
4143 it->left_user_fringe_face_id = face_id;
4144 }
4145 else
4146 {
4147 it->right_user_fringe_bitmap = fringe_bitmap;
4148 it->right_user_fringe_face_id = face_id;
4149 }
4150 #endif /* HAVE_WINDOW_SYSTEM */
4151 return 1;
4152 }
4153
4154 /* Prepare to handle `((margin left-margin) ...)',
4155 `((margin right-margin) ...)' and `((margin nil) ...)'
4156 prefixes for display specifications. */
4157 location = Qunbound;
4158 if (CONSP (spec) && CONSP (XCAR (spec)))
4159 {
4160 Lisp_Object tem;
4161
4162 value = XCDR (spec);
4163 if (CONSP (value))
4164 value = XCAR (value);
4165
4166 tem = XCAR (spec);
4167 if (EQ (XCAR (tem), Qmargin)
4168 && (tem = XCDR (tem),
4169 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4170 (NILP (tem)
4171 || EQ (tem, Qleft_margin)
4172 || EQ (tem, Qright_margin))))
4173 location = tem;
4174 }
4175
4176 if (EQ (location, Qunbound))
4177 {
4178 location = Qnil;
4179 value = spec;
4180 }
4181
4182 /* After this point, VALUE is the property after any
4183 margin prefix has been stripped. It must be a string,
4184 an image specification, or `(space ...)'.
4185
4186 LOCATION specifies where to display: `left-margin',
4187 `right-margin' or nil. */
4188
4189 valid_p = (STRINGP (value)
4190 #ifdef HAVE_WINDOW_SYSTEM
4191 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4192 #endif /* not HAVE_WINDOW_SYSTEM */
4193 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4194
4195 if (valid_p && !display_replaced_before_p)
4196 {
4197 /* Save current settings of IT so that we can restore them
4198 when we are finished with the glyph property value. */
4199 save_pos = it->position;
4200 it->position = *position;
4201 push_it (it);
4202 it->position = save_pos;
4203
4204 if (NILP (location))
4205 it->area = TEXT_AREA;
4206 else if (EQ (location, Qleft_margin))
4207 it->area = LEFT_MARGIN_AREA;
4208 else
4209 it->area = RIGHT_MARGIN_AREA;
4210
4211 if (STRINGP (value))
4212 {
4213 if (SCHARS (value) == 0)
4214 {
4215 pop_it (it);
4216 return -1; /* Replaced by "", i.e. nothing. */
4217 }
4218 it->string = value;
4219 it->multibyte_p = STRING_MULTIBYTE (it->string);
4220 it->current.overlay_string_index = -1;
4221 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4222 it->end_charpos = it->string_nchars = SCHARS (it->string);
4223 it->method = GET_FROM_STRING;
4224 it->stop_charpos = 0;
4225 it->string_from_display_prop_p = 1;
4226 /* Say that we haven't consumed the characters with
4227 `display' property yet. The call to pop_it in
4228 set_iterator_to_next will clean this up. */
4229 *position = start_pos;
4230 }
4231 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4232 {
4233 it->method = GET_FROM_STRETCH;
4234 it->object = value;
4235 *position = it->position = start_pos;
4236 }
4237 #ifdef HAVE_WINDOW_SYSTEM
4238 else
4239 {
4240 it->what = IT_IMAGE;
4241 it->image_id = lookup_image (it->f, value);
4242 it->position = start_pos;
4243 it->object = NILP (object) ? it->w->buffer : object;
4244 it->method = GET_FROM_IMAGE;
4245
4246 /* Say that we haven't consumed the characters with
4247 `display' property yet. The call to pop_it in
4248 set_iterator_to_next will clean this up. */
4249 *position = start_pos;
4250 }
4251 #endif /* HAVE_WINDOW_SYSTEM */
4252
4253 return 1;
4254 }
4255
4256 /* Invalid property or property not supported. Restore
4257 POSITION to what it was before. */
4258 *position = start_pos;
4259 return 0;
4260 }
4261
4262
4263 /* Check if SPEC is a display specification value whose text should be
4264 treated as intangible. */
4265
4266 static int
4267 single_display_spec_intangible_p (prop)
4268 Lisp_Object prop;
4269 {
4270 /* Skip over `when FORM'. */
4271 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4272 {
4273 prop = XCDR (prop);
4274 if (!CONSP (prop))
4275 return 0;
4276 prop = XCDR (prop);
4277 }
4278
4279 if (STRINGP (prop))
4280 return 1;
4281
4282 if (!CONSP (prop))
4283 return 0;
4284
4285 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4286 we don't need to treat text as intangible. */
4287 if (EQ (XCAR (prop), Qmargin))
4288 {
4289 prop = XCDR (prop);
4290 if (!CONSP (prop))
4291 return 0;
4292
4293 prop = XCDR (prop);
4294 if (!CONSP (prop)
4295 || EQ (XCAR (prop), Qleft_margin)
4296 || EQ (XCAR (prop), Qright_margin))
4297 return 0;
4298 }
4299
4300 return (CONSP (prop)
4301 && (EQ (XCAR (prop), Qimage)
4302 || EQ (XCAR (prop), Qspace)));
4303 }
4304
4305
4306 /* Check if PROP is a display property value whose text should be
4307 treated as intangible. */
4308
4309 int
4310 display_prop_intangible_p (prop)
4311 Lisp_Object prop;
4312 {
4313 if (CONSP (prop)
4314 && CONSP (XCAR (prop))
4315 && !EQ (Qmargin, XCAR (XCAR (prop))))
4316 {
4317 /* A list of sub-properties. */
4318 while (CONSP (prop))
4319 {
4320 if (single_display_spec_intangible_p (XCAR (prop)))
4321 return 1;
4322 prop = XCDR (prop);
4323 }
4324 }
4325 else if (VECTORP (prop))
4326 {
4327 /* A vector of sub-properties. */
4328 int i;
4329 for (i = 0; i < ASIZE (prop); ++i)
4330 if (single_display_spec_intangible_p (AREF (prop, i)))
4331 return 1;
4332 }
4333 else
4334 return single_display_spec_intangible_p (prop);
4335
4336 return 0;
4337 }
4338
4339
4340 /* Return 1 if PROP is a display sub-property value containing STRING. */
4341
4342 static int
4343 single_display_spec_string_p (prop, string)
4344 Lisp_Object prop, string;
4345 {
4346 if (EQ (string, prop))
4347 return 1;
4348
4349 /* Skip over `when FORM'. */
4350 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4351 {
4352 prop = XCDR (prop);
4353 if (!CONSP (prop))
4354 return 0;
4355 prop = XCDR (prop);
4356 }
4357
4358 if (CONSP (prop))
4359 /* Skip over `margin LOCATION'. */
4360 if (EQ (XCAR (prop), Qmargin))
4361 {
4362 prop = XCDR (prop);
4363 if (!CONSP (prop))
4364 return 0;
4365
4366 prop = XCDR (prop);
4367 if (!CONSP (prop))
4368 return 0;
4369 }
4370
4371 return CONSP (prop) && EQ (XCAR (prop), string);
4372 }
4373
4374
4375 /* Return 1 if STRING appears in the `display' property PROP. */
4376
4377 static int
4378 display_prop_string_p (prop, string)
4379 Lisp_Object prop, string;
4380 {
4381 if (CONSP (prop)
4382 && CONSP (XCAR (prop))
4383 && !EQ (Qmargin, XCAR (XCAR (prop))))
4384 {
4385 /* A list of sub-properties. */
4386 while (CONSP (prop))
4387 {
4388 if (single_display_spec_string_p (XCAR (prop), string))
4389 return 1;
4390 prop = XCDR (prop);
4391 }
4392 }
4393 else if (VECTORP (prop))
4394 {
4395 /* A vector of sub-properties. */
4396 int i;
4397 for (i = 0; i < ASIZE (prop); ++i)
4398 if (single_display_spec_string_p (AREF (prop, i), string))
4399 return 1;
4400 }
4401 else
4402 return single_display_spec_string_p (prop, string);
4403
4404 return 0;
4405 }
4406
4407
4408 /* Determine from which buffer position in W's buffer STRING comes
4409 from. AROUND_CHARPOS is an approximate position where it could
4410 be from. Value is the buffer position or 0 if it couldn't be
4411 determined.
4412
4413 W's buffer must be current.
4414
4415 This function is necessary because we don't record buffer positions
4416 in glyphs generated from strings (to keep struct glyph small).
4417 This function may only use code that doesn't eval because it is
4418 called asynchronously from note_mouse_highlight. */
4419
4420 int
4421 string_buffer_position (w, string, around_charpos)
4422 struct window *w;
4423 Lisp_Object string;
4424 int around_charpos;
4425 {
4426 Lisp_Object limit, prop, pos;
4427 const int MAX_DISTANCE = 1000;
4428 int found = 0;
4429
4430 pos = make_number (around_charpos);
4431 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4432 while (!found && !EQ (pos, limit))
4433 {
4434 prop = Fget_char_property (pos, Qdisplay, Qnil);
4435 if (!NILP (prop) && display_prop_string_p (prop, string))
4436 found = 1;
4437 else
4438 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4439 }
4440
4441 if (!found)
4442 {
4443 pos = make_number (around_charpos);
4444 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4445 while (!found && !EQ (pos, limit))
4446 {
4447 prop = Fget_char_property (pos, Qdisplay, Qnil);
4448 if (!NILP (prop) && display_prop_string_p (prop, string))
4449 found = 1;
4450 else
4451 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4452 limit);
4453 }
4454 }
4455
4456 return found ? XINT (pos) : 0;
4457 }
4458
4459
4460 \f
4461 /***********************************************************************
4462 `composition' property
4463 ***********************************************************************/
4464
4465 /* Set up iterator IT from `composition' property at its current
4466 position. Called from handle_stop. */
4467
4468 static enum prop_handled
4469 handle_composition_prop (it)
4470 struct it *it;
4471 {
4472 Lisp_Object prop, string;
4473 int pos, pos_byte, end;
4474 enum prop_handled handled = HANDLED_NORMALLY;
4475
4476 if (STRINGP (it->string))
4477 {
4478 pos = IT_STRING_CHARPOS (*it);
4479 pos_byte = IT_STRING_BYTEPOS (*it);
4480 string = it->string;
4481 }
4482 else
4483 {
4484 pos = IT_CHARPOS (*it);
4485 pos_byte = IT_BYTEPOS (*it);
4486 string = Qnil;
4487 }
4488
4489 /* If there's a valid composition and point is not inside of the
4490 composition (in the case that the composition is from the current
4491 buffer), draw a glyph composed from the composition components. */
4492 if (find_composition (pos, -1, &pos, &end, &prop, string)
4493 && COMPOSITION_VALID_P (pos, end, prop)
4494 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4495 {
4496 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4497
4498 if (id >= 0)
4499 {
4500 struct composition *cmp = composition_table[id];
4501
4502 if (cmp->glyph_len == 0)
4503 {
4504 /* No glyph. */
4505 if (STRINGP (it->string))
4506 {
4507 IT_STRING_CHARPOS (*it) = end;
4508 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4509 end);
4510 }
4511 else
4512 {
4513 IT_CHARPOS (*it) = end;
4514 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4515 }
4516 return HANDLED_RECOMPUTE_PROPS;
4517 }
4518
4519 it->stop_charpos = end;
4520 push_it (it);
4521
4522 it->method = GET_FROM_COMPOSITION;
4523 it->cmp_id = id;
4524 it->cmp_len = COMPOSITION_LENGTH (prop);
4525 /* For a terminal, draw only the first character of the
4526 components. */
4527 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4528 it->len = (STRINGP (it->string)
4529 ? string_char_to_byte (it->string, end)
4530 : CHAR_TO_BYTE (end)) - pos_byte;
4531 handled = HANDLED_RETURN;
4532 }
4533 }
4534
4535 return handled;
4536 }
4537
4538
4539 \f
4540 /***********************************************************************
4541 Overlay strings
4542 ***********************************************************************/
4543
4544 /* The following structure is used to record overlay strings for
4545 later sorting in load_overlay_strings. */
4546
4547 struct overlay_entry
4548 {
4549 Lisp_Object overlay;
4550 Lisp_Object string;
4551 int priority;
4552 int after_string_p;
4553 };
4554
4555
4556 /* Set up iterator IT from overlay strings at its current position.
4557 Called from handle_stop. */
4558
4559 static enum prop_handled
4560 handle_overlay_change (it)
4561 struct it *it;
4562 {
4563 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4564 return HANDLED_RECOMPUTE_PROPS;
4565 else
4566 return HANDLED_NORMALLY;
4567 }
4568
4569
4570 /* Set up the next overlay string for delivery by IT, if there is an
4571 overlay string to deliver. Called by set_iterator_to_next when the
4572 end of the current overlay string is reached. If there are more
4573 overlay strings to display, IT->string and
4574 IT->current.overlay_string_index are set appropriately here.
4575 Otherwise IT->string is set to nil. */
4576
4577 static void
4578 next_overlay_string (it)
4579 struct it *it;
4580 {
4581 ++it->current.overlay_string_index;
4582 if (it->current.overlay_string_index == it->n_overlay_strings)
4583 {
4584 /* No more overlay strings. Restore IT's settings to what
4585 they were before overlay strings were processed, and
4586 continue to deliver from current_buffer. */
4587 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4588
4589 pop_it (it);
4590 xassert (it->sp > 0
4591 || it->method == GET_FROM_COMPOSITION
4592 || (NILP (it->string)
4593 && it->method == GET_FROM_BUFFER
4594 && it->stop_charpos >= BEGV
4595 && it->stop_charpos <= it->end_charpos));
4596 it->current.overlay_string_index = -1;
4597 it->n_overlay_strings = 0;
4598
4599 /* If we're at the end of the buffer, record that we have
4600 processed the overlay strings there already, so that
4601 next_element_from_buffer doesn't try it again. */
4602 if (IT_CHARPOS (*it) >= it->end_charpos)
4603 it->overlay_strings_at_end_processed_p = 1;
4604
4605 /* If we have to display `...' for invisible text, set
4606 the iterator up for that. */
4607 if (display_ellipsis_p)
4608 setup_for_ellipsis (it, 0);
4609 }
4610 else
4611 {
4612 /* There are more overlay strings to process. If
4613 IT->current.overlay_string_index has advanced to a position
4614 where we must load IT->overlay_strings with more strings, do
4615 it. */
4616 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4617
4618 if (it->current.overlay_string_index && i == 0)
4619 load_overlay_strings (it, 0);
4620
4621 /* Initialize IT to deliver display elements from the overlay
4622 string. */
4623 it->string = it->overlay_strings[i];
4624 it->multibyte_p = STRING_MULTIBYTE (it->string);
4625 SET_TEXT_POS (it->current.string_pos, 0, 0);
4626 it->method = GET_FROM_STRING;
4627 it->stop_charpos = 0;
4628 }
4629
4630 CHECK_IT (it);
4631 }
4632
4633
4634 /* Compare two overlay_entry structures E1 and E2. Used as a
4635 comparison function for qsort in load_overlay_strings. Overlay
4636 strings for the same position are sorted so that
4637
4638 1. All after-strings come in front of before-strings, except
4639 when they come from the same overlay.
4640
4641 2. Within after-strings, strings are sorted so that overlay strings
4642 from overlays with higher priorities come first.
4643
4644 2. Within before-strings, strings are sorted so that overlay
4645 strings from overlays with higher priorities come last.
4646
4647 Value is analogous to strcmp. */
4648
4649
4650 static int
4651 compare_overlay_entries (e1, e2)
4652 void *e1, *e2;
4653 {
4654 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4655 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4656 int result;
4657
4658 if (entry1->after_string_p != entry2->after_string_p)
4659 {
4660 /* Let after-strings appear in front of before-strings if
4661 they come from different overlays. */
4662 if (EQ (entry1->overlay, entry2->overlay))
4663 result = entry1->after_string_p ? 1 : -1;
4664 else
4665 result = entry1->after_string_p ? -1 : 1;
4666 }
4667 else if (entry1->after_string_p)
4668 /* After-strings sorted in order of decreasing priority. */
4669 result = entry2->priority - entry1->priority;
4670 else
4671 /* Before-strings sorted in order of increasing priority. */
4672 result = entry1->priority - entry2->priority;
4673
4674 return result;
4675 }
4676
4677
4678 /* Load the vector IT->overlay_strings with overlay strings from IT's
4679 current buffer position, or from CHARPOS if that is > 0. Set
4680 IT->n_overlays to the total number of overlay strings found.
4681
4682 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4683 a time. On entry into load_overlay_strings,
4684 IT->current.overlay_string_index gives the number of overlay
4685 strings that have already been loaded by previous calls to this
4686 function.
4687
4688 IT->add_overlay_start contains an additional overlay start
4689 position to consider for taking overlay strings from, if non-zero.
4690 This position comes into play when the overlay has an `invisible'
4691 property, and both before and after-strings. When we've skipped to
4692 the end of the overlay, because of its `invisible' property, we
4693 nevertheless want its before-string to appear.
4694 IT->add_overlay_start will contain the overlay start position
4695 in this case.
4696
4697 Overlay strings are sorted so that after-string strings come in
4698 front of before-string strings. Within before and after-strings,
4699 strings are sorted by overlay priority. See also function
4700 compare_overlay_entries. */
4701
4702 static void
4703 load_overlay_strings (it, charpos)
4704 struct it *it;
4705 int charpos;
4706 {
4707 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4708 Lisp_Object overlay, window, str, invisible;
4709 struct Lisp_Overlay *ov;
4710 int start, end;
4711 int size = 20;
4712 int n = 0, i, j, invis_p;
4713 struct overlay_entry *entries
4714 = (struct overlay_entry *) alloca (size * sizeof *entries);
4715
4716 if (charpos <= 0)
4717 charpos = IT_CHARPOS (*it);
4718
4719 /* Append the overlay string STRING of overlay OVERLAY to vector
4720 `entries' which has size `size' and currently contains `n'
4721 elements. AFTER_P non-zero means STRING is an after-string of
4722 OVERLAY. */
4723 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4724 do \
4725 { \
4726 Lisp_Object priority; \
4727 \
4728 if (n == size) \
4729 { \
4730 int new_size = 2 * size; \
4731 struct overlay_entry *old = entries; \
4732 entries = \
4733 (struct overlay_entry *) alloca (new_size \
4734 * sizeof *entries); \
4735 bcopy (old, entries, size * sizeof *entries); \
4736 size = new_size; \
4737 } \
4738 \
4739 entries[n].string = (STRING); \
4740 entries[n].overlay = (OVERLAY); \
4741 priority = Foverlay_get ((OVERLAY), Qpriority); \
4742 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4743 entries[n].after_string_p = (AFTER_P); \
4744 ++n; \
4745 } \
4746 while (0)
4747
4748 /* Process overlay before the overlay center. */
4749 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4750 {
4751 XSETMISC (overlay, ov);
4752 xassert (OVERLAYP (overlay));
4753 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4754 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4755
4756 if (end < charpos)
4757 break;
4758
4759 /* Skip this overlay if it doesn't start or end at IT's current
4760 position. */
4761 if (end != charpos && start != charpos)
4762 continue;
4763
4764 /* Skip this overlay if it doesn't apply to IT->w. */
4765 window = Foverlay_get (overlay, Qwindow);
4766 if (WINDOWP (window) && XWINDOW (window) != it->w)
4767 continue;
4768
4769 /* If the text ``under'' the overlay is invisible, both before-
4770 and after-strings from this overlay are visible; start and
4771 end position are indistinguishable. */
4772 invisible = Foverlay_get (overlay, Qinvisible);
4773 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4774
4775 /* If overlay has a non-empty before-string, record it. */
4776 if ((start == charpos || (end == charpos && invis_p))
4777 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4778 && SCHARS (str))
4779 RECORD_OVERLAY_STRING (overlay, str, 0);
4780
4781 /* If overlay has a non-empty after-string, record it. */
4782 if ((end == charpos || (start == charpos && invis_p))
4783 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4784 && SCHARS (str))
4785 RECORD_OVERLAY_STRING (overlay, str, 1);
4786 }
4787
4788 /* Process overlays after the overlay center. */
4789 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4790 {
4791 XSETMISC (overlay, ov);
4792 xassert (OVERLAYP (overlay));
4793 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4794 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4795
4796 if (start > charpos)
4797 break;
4798
4799 /* Skip this overlay if it doesn't start or end at IT's current
4800 position. */
4801 if (end != charpos && start != charpos)
4802 continue;
4803
4804 /* Skip this overlay if it doesn't apply to IT->w. */
4805 window = Foverlay_get (overlay, Qwindow);
4806 if (WINDOWP (window) && XWINDOW (window) != it->w)
4807 continue;
4808
4809 /* If the text ``under'' the overlay is invisible, it has a zero
4810 dimension, and both before- and after-strings apply. */
4811 invisible = Foverlay_get (overlay, Qinvisible);
4812 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4813
4814 /* If overlay has a non-empty before-string, record it. */
4815 if ((start == charpos || (end == charpos && invis_p))
4816 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4817 && SCHARS (str))
4818 RECORD_OVERLAY_STRING (overlay, str, 0);
4819
4820 /* If overlay has a non-empty after-string, record it. */
4821 if ((end == charpos || (start == charpos && invis_p))
4822 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4823 && SCHARS (str))
4824 RECORD_OVERLAY_STRING (overlay, str, 1);
4825 }
4826
4827 #undef RECORD_OVERLAY_STRING
4828
4829 /* Sort entries. */
4830 if (n > 1)
4831 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4832
4833 /* Record the total number of strings to process. */
4834 it->n_overlay_strings = n;
4835
4836 /* IT->current.overlay_string_index is the number of overlay strings
4837 that have already been consumed by IT. Copy some of the
4838 remaining overlay strings to IT->overlay_strings. */
4839 i = 0;
4840 j = it->current.overlay_string_index;
4841 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4842 it->overlay_strings[i++] = entries[j++].string;
4843
4844 CHECK_IT (it);
4845 }
4846
4847
4848 /* Get the first chunk of overlay strings at IT's current buffer
4849 position, or at CHARPOS if that is > 0. Value is non-zero if at
4850 least one overlay string was found. */
4851
4852 static int
4853 get_overlay_strings_1 (it, charpos, compute_stop_p)
4854 struct it *it;
4855 int charpos;
4856 {
4857 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4858 process. This fills IT->overlay_strings with strings, and sets
4859 IT->n_overlay_strings to the total number of strings to process.
4860 IT->pos.overlay_string_index has to be set temporarily to zero
4861 because load_overlay_strings needs this; it must be set to -1
4862 when no overlay strings are found because a zero value would
4863 indicate a position in the first overlay string. */
4864 it->current.overlay_string_index = 0;
4865 load_overlay_strings (it, charpos);
4866
4867 /* If we found overlay strings, set up IT to deliver display
4868 elements from the first one. Otherwise set up IT to deliver
4869 from current_buffer. */
4870 if (it->n_overlay_strings)
4871 {
4872 /* Make sure we know settings in current_buffer, so that we can
4873 restore meaningful values when we're done with the overlay
4874 strings. */
4875 if (compute_stop_p)
4876 compute_stop_pos (it);
4877 xassert (it->face_id >= 0);
4878
4879 /* Save IT's settings. They are restored after all overlay
4880 strings have been processed. */
4881 xassert (!compute_stop_p || it->sp == 0);
4882 push_it (it);
4883
4884 /* Set up IT to deliver display elements from the first overlay
4885 string. */
4886 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4887 it->string = it->overlay_strings[0];
4888 it->stop_charpos = 0;
4889 xassert (STRINGP (it->string));
4890 it->end_charpos = SCHARS (it->string);
4891 it->multibyte_p = STRING_MULTIBYTE (it->string);
4892 it->method = GET_FROM_STRING;
4893 return 1;
4894 }
4895
4896 it->current.overlay_string_index = -1;
4897 return 0;
4898 }
4899
4900 static int
4901 get_overlay_strings (it, charpos)
4902 struct it *it;
4903 int charpos;
4904 {
4905 it->string = Qnil;
4906 it->method = GET_FROM_BUFFER;
4907
4908 (void) get_overlay_strings_1 (it, charpos, 1);
4909
4910 CHECK_IT (it);
4911
4912 /* Value is non-zero if we found at least one overlay string. */
4913 return STRINGP (it->string);
4914 }
4915
4916
4917 \f
4918 /***********************************************************************
4919 Saving and restoring state
4920 ***********************************************************************/
4921
4922 /* Save current settings of IT on IT->stack. Called, for example,
4923 before setting up IT for an overlay string, to be able to restore
4924 IT's settings to what they were after the overlay string has been
4925 processed. */
4926
4927 static void
4928 push_it (it)
4929 struct it *it;
4930 {
4931 struct iterator_stack_entry *p;
4932
4933 xassert (it->sp < IT_STACK_SIZE);
4934 p = it->stack + it->sp;
4935
4936 p->stop_charpos = it->stop_charpos;
4937 xassert (it->face_id >= 0);
4938 p->face_id = it->face_id;
4939 p->string = it->string;
4940 p->method = it->method;
4941 switch (p->method)
4942 {
4943 case GET_FROM_IMAGE:
4944 p->u.image.object = it->object;
4945 p->u.image.image_id = it->image_id;
4946 p->u.image.slice = it->slice;
4947 break;
4948 case GET_FROM_COMPOSITION:
4949 p->u.comp.object = it->object;
4950 p->u.comp.c = it->c;
4951 p->u.comp.len = it->len;
4952 p->u.comp.cmp_id = it->cmp_id;
4953 p->u.comp.cmp_len = it->cmp_len;
4954 break;
4955 case GET_FROM_STRETCH:
4956 p->u.stretch.object = it->object;
4957 break;
4958 }
4959 p->position = it->position;
4960 p->current = it->current;
4961 p->end_charpos = it->end_charpos;
4962 p->string_nchars = it->string_nchars;
4963 p->area = it->area;
4964 p->multibyte_p = it->multibyte_p;
4965 p->space_width = it->space_width;
4966 p->font_height = it->font_height;
4967 p->voffset = it->voffset;
4968 p->string_from_display_prop_p = it->string_from_display_prop_p;
4969 p->display_ellipsis_p = 0;
4970 ++it->sp;
4971 }
4972
4973
4974 /* Restore IT's settings from IT->stack. Called, for example, when no
4975 more overlay strings must be processed, and we return to delivering
4976 display elements from a buffer, or when the end of a string from a
4977 `display' property is reached and we return to delivering display
4978 elements from an overlay string, or from a buffer. */
4979
4980 static void
4981 pop_it (it)
4982 struct it *it;
4983 {
4984 struct iterator_stack_entry *p;
4985
4986 xassert (it->sp > 0);
4987 --it->sp;
4988 p = it->stack + it->sp;
4989 it->stop_charpos = p->stop_charpos;
4990 it->face_id = p->face_id;
4991 it->current = p->current;
4992 it->position = p->position;
4993 it->string = p->string;
4994 if (NILP (it->string))
4995 SET_TEXT_POS (it->current.string_pos, -1, -1);
4996 it->method = p->method;
4997 switch (it->method)
4998 {
4999 case GET_FROM_IMAGE:
5000 it->image_id = p->u.image.image_id;
5001 it->object = p->u.image.object;
5002 it->slice = p->u.image.slice;
5003 break;
5004 case GET_FROM_COMPOSITION:
5005 it->object = p->u.comp.object;
5006 it->c = p->u.comp.c;
5007 it->len = p->u.comp.len;
5008 it->cmp_id = p->u.comp.cmp_id;
5009 it->cmp_len = p->u.comp.cmp_len;
5010 break;
5011 case GET_FROM_STRETCH:
5012 it->object = p->u.comp.object;
5013 break;
5014 case GET_FROM_BUFFER:
5015 it->object = it->w->buffer;
5016 break;
5017 case GET_FROM_STRING:
5018 it->object = it->string;
5019 break;
5020 }
5021 it->end_charpos = p->end_charpos;
5022 it->string_nchars = p->string_nchars;
5023 it->area = p->area;
5024 it->multibyte_p = p->multibyte_p;
5025 it->space_width = p->space_width;
5026 it->font_height = p->font_height;
5027 it->voffset = p->voffset;
5028 it->string_from_display_prop_p = p->string_from_display_prop_p;
5029 }
5030
5031
5032 \f
5033 /***********************************************************************
5034 Moving over lines
5035 ***********************************************************************/
5036
5037 /* Set IT's current position to the previous line start. */
5038
5039 static void
5040 back_to_previous_line_start (it)
5041 struct it *it;
5042 {
5043 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5044 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5045 }
5046
5047
5048 /* Move IT to the next line start.
5049
5050 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5051 we skipped over part of the text (as opposed to moving the iterator
5052 continuously over the text). Otherwise, don't change the value
5053 of *SKIPPED_P.
5054
5055 Newlines may come from buffer text, overlay strings, or strings
5056 displayed via the `display' property. That's the reason we can't
5057 simply use find_next_newline_no_quit.
5058
5059 Note that this function may not skip over invisible text that is so
5060 because of text properties and immediately follows a newline. If
5061 it would, function reseat_at_next_visible_line_start, when called
5062 from set_iterator_to_next, would effectively make invisible
5063 characters following a newline part of the wrong glyph row, which
5064 leads to wrong cursor motion. */
5065
5066 static int
5067 forward_to_next_line_start (it, skipped_p)
5068 struct it *it;
5069 int *skipped_p;
5070 {
5071 int old_selective, newline_found_p, n;
5072 const int MAX_NEWLINE_DISTANCE = 500;
5073
5074 /* If already on a newline, just consume it to avoid unintended
5075 skipping over invisible text below. */
5076 if (it->what == IT_CHARACTER
5077 && it->c == '\n'
5078 && CHARPOS (it->position) == IT_CHARPOS (*it))
5079 {
5080 set_iterator_to_next (it, 0);
5081 it->c = 0;
5082 return 1;
5083 }
5084
5085 /* Don't handle selective display in the following. It's (a)
5086 unnecessary because it's done by the caller, and (b) leads to an
5087 infinite recursion because next_element_from_ellipsis indirectly
5088 calls this function. */
5089 old_selective = it->selective;
5090 it->selective = 0;
5091
5092 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5093 from buffer text. */
5094 for (n = newline_found_p = 0;
5095 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5096 n += STRINGP (it->string) ? 0 : 1)
5097 {
5098 if (!get_next_display_element (it))
5099 return 0;
5100 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5101 set_iterator_to_next (it, 0);
5102 }
5103
5104 /* If we didn't find a newline near enough, see if we can use a
5105 short-cut. */
5106 if (!newline_found_p)
5107 {
5108 int start = IT_CHARPOS (*it);
5109 int limit = find_next_newline_no_quit (start, 1);
5110 Lisp_Object pos;
5111
5112 xassert (!STRINGP (it->string));
5113
5114 /* If there isn't any `display' property in sight, and no
5115 overlays, we can just use the position of the newline in
5116 buffer text. */
5117 if (it->stop_charpos >= limit
5118 || ((pos = Fnext_single_property_change (make_number (start),
5119 Qdisplay,
5120 Qnil, make_number (limit)),
5121 NILP (pos))
5122 && next_overlay_change (start) == ZV))
5123 {
5124 IT_CHARPOS (*it) = limit;
5125 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5126 *skipped_p = newline_found_p = 1;
5127 }
5128 else
5129 {
5130 while (get_next_display_element (it)
5131 && !newline_found_p)
5132 {
5133 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5134 set_iterator_to_next (it, 0);
5135 }
5136 }
5137 }
5138
5139 it->selective = old_selective;
5140 return newline_found_p;
5141 }
5142
5143
5144 /* Set IT's current position to the previous visible line start. Skip
5145 invisible text that is so either due to text properties or due to
5146 selective display. Caution: this does not change IT->current_x and
5147 IT->hpos. */
5148
5149 static void
5150 back_to_previous_visible_line_start (it)
5151 struct it *it;
5152 {
5153 while (IT_CHARPOS (*it) > BEGV)
5154 {
5155 back_to_previous_line_start (it);
5156
5157 if (IT_CHARPOS (*it) <= BEGV)
5158 break;
5159
5160 /* If selective > 0, then lines indented more than that values
5161 are invisible. */
5162 if (it->selective > 0
5163 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5164 (double) it->selective)) /* iftc */
5165 continue;
5166
5167 /* Check the newline before point for invisibility. */
5168 {
5169 Lisp_Object prop;
5170 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5171 Qinvisible, it->window);
5172 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5173 continue;
5174 }
5175
5176 if (IT_CHARPOS (*it) <= BEGV)
5177 break;
5178
5179 {
5180 struct it it2;
5181 int pos;
5182 int beg, end;
5183 Lisp_Object val, overlay;
5184
5185 /* If newline is part of a composition, continue from start of composition */
5186 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5187 && beg < IT_CHARPOS (*it))
5188 goto replaced;
5189
5190 /* If newline is replaced by a display property, find start of overlay
5191 or interval and continue search from that point. */
5192 it2 = *it;
5193 pos = --IT_CHARPOS (it2);
5194 --IT_BYTEPOS (it2);
5195 it2.sp = 0;
5196 if (handle_display_prop (&it2) == HANDLED_RETURN
5197 && !NILP (val = get_char_property_and_overlay
5198 (make_number (pos), Qdisplay, Qnil, &overlay))
5199 && (OVERLAYP (overlay)
5200 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5201 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5202 goto replaced;
5203
5204 /* Newline is not replaced by anything -- so we are done. */
5205 break;
5206
5207 replaced:
5208 if (beg < BEGV)
5209 beg = BEGV;
5210 IT_CHARPOS (*it) = beg;
5211 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5212 }
5213 }
5214
5215 it->continuation_lines_width = 0;
5216
5217 xassert (IT_CHARPOS (*it) >= BEGV);
5218 xassert (IT_CHARPOS (*it) == BEGV
5219 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5220 CHECK_IT (it);
5221 }
5222
5223
5224 /* Reseat iterator IT at the previous visible line start. Skip
5225 invisible text that is so either due to text properties or due to
5226 selective display. At the end, update IT's overlay information,
5227 face information etc. */
5228
5229 void
5230 reseat_at_previous_visible_line_start (it)
5231 struct it *it;
5232 {
5233 back_to_previous_visible_line_start (it);
5234 reseat (it, it->current.pos, 1);
5235 CHECK_IT (it);
5236 }
5237
5238
5239 /* Reseat iterator IT on the next visible line start in the current
5240 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5241 preceding the line start. Skip over invisible text that is so
5242 because of selective display. Compute faces, overlays etc at the
5243 new position. Note that this function does not skip over text that
5244 is invisible because of text properties. */
5245
5246 static void
5247 reseat_at_next_visible_line_start (it, on_newline_p)
5248 struct it *it;
5249 int on_newline_p;
5250 {
5251 int newline_found_p, skipped_p = 0;
5252
5253 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5254
5255 /* Skip over lines that are invisible because they are indented
5256 more than the value of IT->selective. */
5257 if (it->selective > 0)
5258 while (IT_CHARPOS (*it) < ZV
5259 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5260 (double) it->selective)) /* iftc */
5261 {
5262 xassert (IT_BYTEPOS (*it) == BEGV
5263 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5264 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5265 }
5266
5267 /* Position on the newline if that's what's requested. */
5268 if (on_newline_p && newline_found_p)
5269 {
5270 if (STRINGP (it->string))
5271 {
5272 if (IT_STRING_CHARPOS (*it) > 0)
5273 {
5274 --IT_STRING_CHARPOS (*it);
5275 --IT_STRING_BYTEPOS (*it);
5276 }
5277 }
5278 else if (IT_CHARPOS (*it) > BEGV)
5279 {
5280 --IT_CHARPOS (*it);
5281 --IT_BYTEPOS (*it);
5282 reseat (it, it->current.pos, 0);
5283 }
5284 }
5285 else if (skipped_p)
5286 reseat (it, it->current.pos, 0);
5287
5288 CHECK_IT (it);
5289 }
5290
5291
5292 \f
5293 /***********************************************************************
5294 Changing an iterator's position
5295 ***********************************************************************/
5296
5297 /* Change IT's current position to POS in current_buffer. If FORCE_P
5298 is non-zero, always check for text properties at the new position.
5299 Otherwise, text properties are only looked up if POS >=
5300 IT->check_charpos of a property. */
5301
5302 static void
5303 reseat (it, pos, force_p)
5304 struct it *it;
5305 struct text_pos pos;
5306 int force_p;
5307 {
5308 int original_pos = IT_CHARPOS (*it);
5309
5310 reseat_1 (it, pos, 0);
5311
5312 /* Determine where to check text properties. Avoid doing it
5313 where possible because text property lookup is very expensive. */
5314 if (force_p
5315 || CHARPOS (pos) > it->stop_charpos
5316 || CHARPOS (pos) < original_pos)
5317 handle_stop (it);
5318
5319 CHECK_IT (it);
5320 }
5321
5322
5323 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5324 IT->stop_pos to POS, also. */
5325
5326 static void
5327 reseat_1 (it, pos, set_stop_p)
5328 struct it *it;
5329 struct text_pos pos;
5330 int set_stop_p;
5331 {
5332 /* Don't call this function when scanning a C string. */
5333 xassert (it->s == NULL);
5334
5335 /* POS must be a reasonable value. */
5336 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5337
5338 it->current.pos = it->position = pos;
5339 it->end_charpos = ZV;
5340 it->dpvec = NULL;
5341 it->current.dpvec_index = -1;
5342 it->current.overlay_string_index = -1;
5343 IT_STRING_CHARPOS (*it) = -1;
5344 IT_STRING_BYTEPOS (*it) = -1;
5345 it->string = Qnil;
5346 it->method = GET_FROM_BUFFER;
5347 it->object = it->w->buffer;
5348 it->area = TEXT_AREA;
5349 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5350 it->sp = 0;
5351 it->string_from_display_prop_p = 0;
5352 it->face_before_selective_p = 0;
5353
5354 if (set_stop_p)
5355 it->stop_charpos = CHARPOS (pos);
5356 }
5357
5358
5359 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5360 If S is non-null, it is a C string to iterate over. Otherwise,
5361 STRING gives a Lisp string to iterate over.
5362
5363 If PRECISION > 0, don't return more then PRECISION number of
5364 characters from the string.
5365
5366 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5367 characters have been returned. FIELD_WIDTH < 0 means an infinite
5368 field width.
5369
5370 MULTIBYTE = 0 means disable processing of multibyte characters,
5371 MULTIBYTE > 0 means enable it,
5372 MULTIBYTE < 0 means use IT->multibyte_p.
5373
5374 IT must be initialized via a prior call to init_iterator before
5375 calling this function. */
5376
5377 static void
5378 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5379 struct it *it;
5380 unsigned char *s;
5381 Lisp_Object string;
5382 int charpos;
5383 int precision, field_width, multibyte;
5384 {
5385 /* No region in strings. */
5386 it->region_beg_charpos = it->region_end_charpos = -1;
5387
5388 /* No text property checks performed by default, but see below. */
5389 it->stop_charpos = -1;
5390
5391 /* Set iterator position and end position. */
5392 bzero (&it->current, sizeof it->current);
5393 it->current.overlay_string_index = -1;
5394 it->current.dpvec_index = -1;
5395 xassert (charpos >= 0);
5396
5397 /* If STRING is specified, use its multibyteness, otherwise use the
5398 setting of MULTIBYTE, if specified. */
5399 if (multibyte >= 0)
5400 it->multibyte_p = multibyte > 0;
5401
5402 if (s == NULL)
5403 {
5404 xassert (STRINGP (string));
5405 it->string = string;
5406 it->s = NULL;
5407 it->end_charpos = it->string_nchars = SCHARS (string);
5408 it->method = GET_FROM_STRING;
5409 it->current.string_pos = string_pos (charpos, string);
5410 }
5411 else
5412 {
5413 it->s = s;
5414 it->string = Qnil;
5415
5416 /* Note that we use IT->current.pos, not it->current.string_pos,
5417 for displaying C strings. */
5418 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5419 if (it->multibyte_p)
5420 {
5421 it->current.pos = c_string_pos (charpos, s, 1);
5422 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5423 }
5424 else
5425 {
5426 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5427 it->end_charpos = it->string_nchars = strlen (s);
5428 }
5429
5430 it->method = GET_FROM_C_STRING;
5431 }
5432
5433 /* PRECISION > 0 means don't return more than PRECISION characters
5434 from the string. */
5435 if (precision > 0 && it->end_charpos - charpos > precision)
5436 it->end_charpos = it->string_nchars = charpos + precision;
5437
5438 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5439 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5440 FIELD_WIDTH < 0 means infinite field width. This is useful for
5441 padding with `-' at the end of a mode line. */
5442 if (field_width < 0)
5443 field_width = INFINITY;
5444 if (field_width > it->end_charpos - charpos)
5445 it->end_charpos = charpos + field_width;
5446
5447 /* Use the standard display table for displaying strings. */
5448 if (DISP_TABLE_P (Vstandard_display_table))
5449 it->dp = XCHAR_TABLE (Vstandard_display_table);
5450
5451 it->stop_charpos = charpos;
5452 CHECK_IT (it);
5453 }
5454
5455
5456 \f
5457 /***********************************************************************
5458 Iteration
5459 ***********************************************************************/
5460
5461 /* Map enum it_method value to corresponding next_element_from_* function. */
5462
5463 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5464 {
5465 next_element_from_buffer,
5466 next_element_from_display_vector,
5467 next_element_from_composition,
5468 next_element_from_string,
5469 next_element_from_c_string,
5470 next_element_from_image,
5471 next_element_from_stretch
5472 };
5473
5474
5475 /* Load IT's display element fields with information about the next
5476 display element from the current position of IT. Value is zero if
5477 end of buffer (or C string) is reached. */
5478
5479 static struct frame *last_escape_glyph_frame = NULL;
5480 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5481 static int last_escape_glyph_merged_face_id = 0;
5482
5483 int
5484 get_next_display_element (it)
5485 struct it *it;
5486 {
5487 /* Non-zero means that we found a display element. Zero means that
5488 we hit the end of what we iterate over. Performance note: the
5489 function pointer `method' used here turns out to be faster than
5490 using a sequence of if-statements. */
5491 int success_p;
5492
5493 get_next:
5494 success_p = (*get_next_element[it->method]) (it);
5495
5496 if (it->what == IT_CHARACTER)
5497 {
5498 /* Map via display table or translate control characters.
5499 IT->c, IT->len etc. have been set to the next character by
5500 the function call above. If we have a display table, and it
5501 contains an entry for IT->c, translate it. Don't do this if
5502 IT->c itself comes from a display table, otherwise we could
5503 end up in an infinite recursion. (An alternative could be to
5504 count the recursion depth of this function and signal an
5505 error when a certain maximum depth is reached.) Is it worth
5506 it? */
5507 if (success_p && it->dpvec == NULL)
5508 {
5509 Lisp_Object dv;
5510
5511 if (it->dp
5512 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5513 VECTORP (dv)))
5514 {
5515 struct Lisp_Vector *v = XVECTOR (dv);
5516
5517 /* Return the first character from the display table
5518 entry, if not empty. If empty, don't display the
5519 current character. */
5520 if (v->size)
5521 {
5522 it->dpvec_char_len = it->len;
5523 it->dpvec = v->contents;
5524 it->dpend = v->contents + v->size;
5525 it->current.dpvec_index = 0;
5526 it->dpvec_face_id = -1;
5527 it->saved_face_id = it->face_id;
5528 it->method = GET_FROM_DISPLAY_VECTOR;
5529 it->ellipsis_p = 0;
5530 }
5531 else
5532 {
5533 set_iterator_to_next (it, 0);
5534 }
5535 goto get_next;
5536 }
5537
5538 /* Translate control characters into `\003' or `^C' form.
5539 Control characters coming from a display table entry are
5540 currently not translated because we use IT->dpvec to hold
5541 the translation. This could easily be changed but I
5542 don't believe that it is worth doing.
5543
5544 If it->multibyte_p is nonzero, eight-bit characters and
5545 non-printable multibyte characters are also translated to
5546 octal form.
5547
5548 If it->multibyte_p is zero, eight-bit characters that
5549 don't have corresponding multibyte char code are also
5550 translated to octal form. */
5551 else if ((it->c < ' '
5552 && (it->area != TEXT_AREA
5553 /* In mode line, treat \n like other crl chars. */
5554 || (it->c != '\t'
5555 && it->glyph_row && it->glyph_row->mode_line_p)
5556 || (it->c != '\n' && it->c != '\t')))
5557 || (it->multibyte_p
5558 ? ((it->c >= 127
5559 && it->len == 1)
5560 || !CHAR_PRINTABLE_P (it->c)
5561 || (!NILP (Vnobreak_char_display)
5562 && (it->c == 0x8a0 || it->c == 0x8ad
5563 || it->c == 0x920 || it->c == 0x92d
5564 || it->c == 0xe20 || it->c == 0xe2d
5565 || it->c == 0xf20 || it->c == 0xf2d)))
5566 : (it->c >= 127
5567 && (!unibyte_display_via_language_environment
5568 || it->c == unibyte_char_to_multibyte (it->c)))))
5569 {
5570 /* IT->c is a control character which must be displayed
5571 either as '\003' or as `^C' where the '\\' and '^'
5572 can be defined in the display table. Fill
5573 IT->ctl_chars with glyphs for what we have to
5574 display. Then, set IT->dpvec to these glyphs. */
5575 GLYPH g;
5576 int ctl_len;
5577 int face_id, lface_id = 0 ;
5578 GLYPH escape_glyph;
5579
5580 /* Handle control characters with ^. */
5581
5582 if (it->c < 128 && it->ctl_arrow_p)
5583 {
5584 g = '^'; /* default glyph for Control */
5585 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5586 if (it->dp
5587 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5588 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5589 {
5590 g = XINT (DISP_CTRL_GLYPH (it->dp));
5591 lface_id = FAST_GLYPH_FACE (g);
5592 }
5593 if (lface_id)
5594 {
5595 g = FAST_GLYPH_CHAR (g);
5596 face_id = merge_faces (it->f, Qt, lface_id,
5597 it->face_id);
5598 }
5599 else if (it->f == last_escape_glyph_frame
5600 && it->face_id == last_escape_glyph_face_id)
5601 {
5602 face_id = last_escape_glyph_merged_face_id;
5603 }
5604 else
5605 {
5606 /* Merge the escape-glyph face into the current face. */
5607 face_id = merge_faces (it->f, Qescape_glyph, 0,
5608 it->face_id);
5609 last_escape_glyph_frame = it->f;
5610 last_escape_glyph_face_id = it->face_id;
5611 last_escape_glyph_merged_face_id = face_id;
5612 }
5613
5614 XSETINT (it->ctl_chars[0], g);
5615 g = it->c ^ 0100;
5616 XSETINT (it->ctl_chars[1], g);
5617 ctl_len = 2;
5618 goto display_control;
5619 }
5620
5621 /* Handle non-break space in the mode where it only gets
5622 highlighting. */
5623
5624 if (EQ (Vnobreak_char_display, Qt)
5625 && (it->c == 0x8a0 || it->c == 0x920
5626 || it->c == 0xe20 || it->c == 0xf20))
5627 {
5628 /* Merge the no-break-space face into the current face. */
5629 face_id = merge_faces (it->f, Qnobreak_space, 0,
5630 it->face_id);
5631
5632 g = it->c = ' ';
5633 XSETINT (it->ctl_chars[0], g);
5634 ctl_len = 1;
5635 goto display_control;
5636 }
5637
5638 /* Handle sequences that start with the "escape glyph". */
5639
5640 /* the default escape glyph is \. */
5641 escape_glyph = '\\';
5642
5643 if (it->dp
5644 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5645 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5646 {
5647 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5648 lface_id = FAST_GLYPH_FACE (escape_glyph);
5649 }
5650 if (lface_id)
5651 {
5652 /* The display table specified a face.
5653 Merge it into face_id and also into escape_glyph. */
5654 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5655 face_id = merge_faces (it->f, Qt, lface_id,
5656 it->face_id);
5657 }
5658 else if (it->f == last_escape_glyph_frame
5659 && it->face_id == last_escape_glyph_face_id)
5660 {
5661 face_id = last_escape_glyph_merged_face_id;
5662 }
5663 else
5664 {
5665 /* Merge the escape-glyph face into the current face. */
5666 face_id = merge_faces (it->f, Qescape_glyph, 0,
5667 it->face_id);
5668 last_escape_glyph_frame = it->f;
5669 last_escape_glyph_face_id = it->face_id;
5670 last_escape_glyph_merged_face_id = face_id;
5671 }
5672
5673 /* Handle soft hyphens in the mode where they only get
5674 highlighting. */
5675
5676 if (EQ (Vnobreak_char_display, Qt)
5677 && (it->c == 0x8ad || it->c == 0x92d
5678 || it->c == 0xe2d || it->c == 0xf2d))
5679 {
5680 g = it->c = '-';
5681 XSETINT (it->ctl_chars[0], g);
5682 ctl_len = 1;
5683 goto display_control;
5684 }
5685
5686 /* Handle non-break space and soft hyphen
5687 with the escape glyph. */
5688
5689 if (it->c == 0x8a0 || it->c == 0x8ad
5690 || it->c == 0x920 || it->c == 0x92d
5691 || it->c == 0xe20 || it->c == 0xe2d
5692 || it->c == 0xf20 || it->c == 0xf2d)
5693 {
5694 XSETINT (it->ctl_chars[0], escape_glyph);
5695 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5696 XSETINT (it->ctl_chars[1], g);
5697 ctl_len = 2;
5698 goto display_control;
5699 }
5700
5701 {
5702 unsigned char str[MAX_MULTIBYTE_LENGTH];
5703 int len;
5704 int i;
5705
5706 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5707 if (SINGLE_BYTE_CHAR_P (it->c))
5708 str[0] = it->c, len = 1;
5709 else
5710 {
5711 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5712 if (len < 0)
5713 {
5714 /* It's an invalid character, which shouldn't
5715 happen actually, but due to bugs it may
5716 happen. Let's print the char as is, there's
5717 not much meaningful we can do with it. */
5718 str[0] = it->c;
5719 str[1] = it->c >> 8;
5720 str[2] = it->c >> 16;
5721 str[3] = it->c >> 24;
5722 len = 4;
5723 }
5724 }
5725
5726 for (i = 0; i < len; i++)
5727 {
5728 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5729 /* Insert three more glyphs into IT->ctl_chars for
5730 the octal display of the character. */
5731 g = ((str[i] >> 6) & 7) + '0';
5732 XSETINT (it->ctl_chars[i * 4 + 1], g);
5733 g = ((str[i] >> 3) & 7) + '0';
5734 XSETINT (it->ctl_chars[i * 4 + 2], g);
5735 g = (str[i] & 7) + '0';
5736 XSETINT (it->ctl_chars[i * 4 + 3], g);
5737 }
5738 ctl_len = len * 4;
5739 }
5740
5741 display_control:
5742 /* Set up IT->dpvec and return first character from it. */
5743 it->dpvec_char_len = it->len;
5744 it->dpvec = it->ctl_chars;
5745 it->dpend = it->dpvec + ctl_len;
5746 it->current.dpvec_index = 0;
5747 it->dpvec_face_id = face_id;
5748 it->saved_face_id = it->face_id;
5749 it->method = GET_FROM_DISPLAY_VECTOR;
5750 it->ellipsis_p = 0;
5751 goto get_next;
5752 }
5753 }
5754
5755 /* Adjust face id for a multibyte character. There are no
5756 multibyte character in unibyte text. */
5757 if (it->multibyte_p
5758 && success_p
5759 && FRAME_WINDOW_P (it->f))
5760 {
5761 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5762 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5763 }
5764 }
5765
5766 /* Is this character the last one of a run of characters with
5767 box? If yes, set IT->end_of_box_run_p to 1. */
5768 if (it->face_box_p
5769 && it->s == NULL)
5770 {
5771 int face_id;
5772 struct face *face;
5773
5774 it->end_of_box_run_p
5775 = ((face_id = face_after_it_pos (it),
5776 face_id != it->face_id)
5777 && (face = FACE_FROM_ID (it->f, face_id),
5778 face->box == FACE_NO_BOX));
5779 }
5780
5781 /* Value is 0 if end of buffer or string reached. */
5782 return success_p;
5783 }
5784
5785
5786 /* Move IT to the next display element.
5787
5788 RESEAT_P non-zero means if called on a newline in buffer text,
5789 skip to the next visible line start.
5790
5791 Functions get_next_display_element and set_iterator_to_next are
5792 separate because I find this arrangement easier to handle than a
5793 get_next_display_element function that also increments IT's
5794 position. The way it is we can first look at an iterator's current
5795 display element, decide whether it fits on a line, and if it does,
5796 increment the iterator position. The other way around we probably
5797 would either need a flag indicating whether the iterator has to be
5798 incremented the next time, or we would have to implement a
5799 decrement position function which would not be easy to write. */
5800
5801 void
5802 set_iterator_to_next (it, reseat_p)
5803 struct it *it;
5804 int reseat_p;
5805 {
5806 /* Reset flags indicating start and end of a sequence of characters
5807 with box. Reset them at the start of this function because
5808 moving the iterator to a new position might set them. */
5809 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5810
5811 switch (it->method)
5812 {
5813 case GET_FROM_BUFFER:
5814 /* The current display element of IT is a character from
5815 current_buffer. Advance in the buffer, and maybe skip over
5816 invisible lines that are so because of selective display. */
5817 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5818 reseat_at_next_visible_line_start (it, 0);
5819 else
5820 {
5821 xassert (it->len != 0);
5822 IT_BYTEPOS (*it) += it->len;
5823 IT_CHARPOS (*it) += 1;
5824 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5825 }
5826 break;
5827
5828 case GET_FROM_COMPOSITION:
5829 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5830 xassert (it->sp > 0);
5831 pop_it (it);
5832 if (it->method == GET_FROM_STRING)
5833 {
5834 IT_STRING_BYTEPOS (*it) += it->len;
5835 IT_STRING_CHARPOS (*it) += it->cmp_len;
5836 goto consider_string_end;
5837 }
5838 else if (it->method == GET_FROM_BUFFER)
5839 {
5840 IT_BYTEPOS (*it) += it->len;
5841 IT_CHARPOS (*it) += it->cmp_len;
5842 }
5843 break;
5844
5845 case GET_FROM_C_STRING:
5846 /* Current display element of IT is from a C string. */
5847 IT_BYTEPOS (*it) += it->len;
5848 IT_CHARPOS (*it) += 1;
5849 break;
5850
5851 case GET_FROM_DISPLAY_VECTOR:
5852 /* Current display element of IT is from a display table entry.
5853 Advance in the display table definition. Reset it to null if
5854 end reached, and continue with characters from buffers/
5855 strings. */
5856 ++it->current.dpvec_index;
5857
5858 /* Restore face of the iterator to what they were before the
5859 display vector entry (these entries may contain faces). */
5860 it->face_id = it->saved_face_id;
5861
5862 if (it->dpvec + it->current.dpvec_index == it->dpend)
5863 {
5864 int recheck_faces = it->ellipsis_p;
5865
5866 if (it->s)
5867 it->method = GET_FROM_C_STRING;
5868 else if (STRINGP (it->string))
5869 it->method = GET_FROM_STRING;
5870 else
5871 {
5872 it->method = GET_FROM_BUFFER;
5873 it->object = it->w->buffer;
5874 }
5875
5876 it->dpvec = NULL;
5877 it->current.dpvec_index = -1;
5878
5879 /* Skip over characters which were displayed via IT->dpvec. */
5880 if (it->dpvec_char_len < 0)
5881 reseat_at_next_visible_line_start (it, 1);
5882 else if (it->dpvec_char_len > 0)
5883 {
5884 if (it->method == GET_FROM_STRING
5885 && it->n_overlay_strings > 0)
5886 it->ignore_overlay_strings_at_pos_p = 1;
5887 it->len = it->dpvec_char_len;
5888 set_iterator_to_next (it, reseat_p);
5889 }
5890
5891 /* Maybe recheck faces after display vector */
5892 if (recheck_faces)
5893 it->stop_charpos = IT_CHARPOS (*it);
5894 }
5895 break;
5896
5897 case GET_FROM_STRING:
5898 /* Current display element is a character from a Lisp string. */
5899 xassert (it->s == NULL && STRINGP (it->string));
5900 IT_STRING_BYTEPOS (*it) += it->len;
5901 IT_STRING_CHARPOS (*it) += 1;
5902
5903 consider_string_end:
5904
5905 if (it->current.overlay_string_index >= 0)
5906 {
5907 /* IT->string is an overlay string. Advance to the
5908 next, if there is one. */
5909 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5910 next_overlay_string (it);
5911 }
5912 else
5913 {
5914 /* IT->string is not an overlay string. If we reached
5915 its end, and there is something on IT->stack, proceed
5916 with what is on the stack. This can be either another
5917 string, this time an overlay string, or a buffer. */
5918 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5919 && it->sp > 0)
5920 {
5921 pop_it (it);
5922 if (it->method == GET_FROM_STRING)
5923 goto consider_string_end;
5924 }
5925 }
5926 break;
5927
5928 case GET_FROM_IMAGE:
5929 case GET_FROM_STRETCH:
5930 /* The position etc with which we have to proceed are on
5931 the stack. The position may be at the end of a string,
5932 if the `display' property takes up the whole string. */
5933 xassert (it->sp > 0);
5934 pop_it (it);
5935 if (it->method == GET_FROM_STRING)
5936 goto consider_string_end;
5937 break;
5938
5939 default:
5940 /* There are no other methods defined, so this should be a bug. */
5941 abort ();
5942 }
5943
5944 xassert (it->method != GET_FROM_STRING
5945 || (STRINGP (it->string)
5946 && IT_STRING_CHARPOS (*it) >= 0));
5947 }
5948
5949 /* Load IT's display element fields with information about the next
5950 display element which comes from a display table entry or from the
5951 result of translating a control character to one of the forms `^C'
5952 or `\003'.
5953
5954 IT->dpvec holds the glyphs to return as characters.
5955 IT->saved_face_id holds the face id before the display vector--
5956 it is restored into IT->face_idin set_iterator_to_next. */
5957
5958 static int
5959 next_element_from_display_vector (it)
5960 struct it *it;
5961 {
5962 /* Precondition. */
5963 xassert (it->dpvec && it->current.dpvec_index >= 0);
5964
5965 it->face_id = it->saved_face_id;
5966
5967 if (INTEGERP (*it->dpvec)
5968 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5969 {
5970 GLYPH g;
5971
5972 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5973 it->c = FAST_GLYPH_CHAR (g);
5974 it->len = CHAR_BYTES (it->c);
5975
5976 /* The entry may contain a face id to use. Such a face id is
5977 the id of a Lisp face, not a realized face. A face id of
5978 zero means no face is specified. */
5979 if (it->dpvec_face_id >= 0)
5980 it->face_id = it->dpvec_face_id;
5981 else
5982 {
5983 int lface_id = FAST_GLYPH_FACE (g);
5984 if (lface_id > 0)
5985 it->face_id = merge_faces (it->f, Qt, lface_id,
5986 it->saved_face_id);
5987 }
5988 }
5989 else
5990 /* Display table entry is invalid. Return a space. */
5991 it->c = ' ', it->len = 1;
5992
5993 /* Don't change position and object of the iterator here. They are
5994 still the values of the character that had this display table
5995 entry or was translated, and that's what we want. */
5996 it->what = IT_CHARACTER;
5997 return 1;
5998 }
5999
6000
6001 /* Load IT with the next display element from Lisp string IT->string.
6002 IT->current.string_pos is the current position within the string.
6003 If IT->current.overlay_string_index >= 0, the Lisp string is an
6004 overlay string. */
6005
6006 static int
6007 next_element_from_string (it)
6008 struct it *it;
6009 {
6010 struct text_pos position;
6011
6012 xassert (STRINGP (it->string));
6013 xassert (IT_STRING_CHARPOS (*it) >= 0);
6014 position = it->current.string_pos;
6015
6016 /* Time to check for invisible text? */
6017 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6018 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6019 {
6020 handle_stop (it);
6021
6022 /* Since a handler may have changed IT->method, we must
6023 recurse here. */
6024 return get_next_display_element (it);
6025 }
6026
6027 if (it->current.overlay_string_index >= 0)
6028 {
6029 /* Get the next character from an overlay string. In overlay
6030 strings, There is no field width or padding with spaces to
6031 do. */
6032 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6033 {
6034 it->what = IT_EOB;
6035 return 0;
6036 }
6037 else if (STRING_MULTIBYTE (it->string))
6038 {
6039 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6040 const unsigned char *s = (SDATA (it->string)
6041 + IT_STRING_BYTEPOS (*it));
6042 it->c = string_char_and_length (s, remaining, &it->len);
6043 }
6044 else
6045 {
6046 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6047 it->len = 1;
6048 }
6049 }
6050 else
6051 {
6052 /* Get the next character from a Lisp string that is not an
6053 overlay string. Such strings come from the mode line, for
6054 example. We may have to pad with spaces, or truncate the
6055 string. See also next_element_from_c_string. */
6056 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6057 {
6058 it->what = IT_EOB;
6059 return 0;
6060 }
6061 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6062 {
6063 /* Pad with spaces. */
6064 it->c = ' ', it->len = 1;
6065 CHARPOS (position) = BYTEPOS (position) = -1;
6066 }
6067 else if (STRING_MULTIBYTE (it->string))
6068 {
6069 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6070 const unsigned char *s = (SDATA (it->string)
6071 + IT_STRING_BYTEPOS (*it));
6072 it->c = string_char_and_length (s, maxlen, &it->len);
6073 }
6074 else
6075 {
6076 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6077 it->len = 1;
6078 }
6079 }
6080
6081 /* Record what we have and where it came from. */
6082 it->what = IT_CHARACTER;
6083 it->object = it->string;
6084 it->position = position;
6085 return 1;
6086 }
6087
6088
6089 /* Load IT with next display element from C string IT->s.
6090 IT->string_nchars is the maximum number of characters to return
6091 from the string. IT->end_charpos may be greater than
6092 IT->string_nchars when this function is called, in which case we
6093 may have to return padding spaces. Value is zero if end of string
6094 reached, including padding spaces. */
6095
6096 static int
6097 next_element_from_c_string (it)
6098 struct it *it;
6099 {
6100 int success_p = 1;
6101
6102 xassert (it->s);
6103 it->what = IT_CHARACTER;
6104 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6105 it->object = Qnil;
6106
6107 /* IT's position can be greater IT->string_nchars in case a field
6108 width or precision has been specified when the iterator was
6109 initialized. */
6110 if (IT_CHARPOS (*it) >= it->end_charpos)
6111 {
6112 /* End of the game. */
6113 it->what = IT_EOB;
6114 success_p = 0;
6115 }
6116 else if (IT_CHARPOS (*it) >= it->string_nchars)
6117 {
6118 /* Pad with spaces. */
6119 it->c = ' ', it->len = 1;
6120 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6121 }
6122 else if (it->multibyte_p)
6123 {
6124 /* Implementation note: The calls to strlen apparently aren't a
6125 performance problem because there is no noticeable performance
6126 difference between Emacs running in unibyte or multibyte mode. */
6127 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6128 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6129 maxlen, &it->len);
6130 }
6131 else
6132 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6133
6134 return success_p;
6135 }
6136
6137
6138 /* Set up IT to return characters from an ellipsis, if appropriate.
6139 The definition of the ellipsis glyphs may come from a display table
6140 entry. This function Fills IT with the first glyph from the
6141 ellipsis if an ellipsis is to be displayed. */
6142
6143 static int
6144 next_element_from_ellipsis (it)
6145 struct it *it;
6146 {
6147 if (it->selective_display_ellipsis_p)
6148 setup_for_ellipsis (it, it->len);
6149 else
6150 {
6151 /* The face at the current position may be different from the
6152 face we find after the invisible text. Remember what it
6153 was in IT->saved_face_id, and signal that it's there by
6154 setting face_before_selective_p. */
6155 it->saved_face_id = it->face_id;
6156 it->method = GET_FROM_BUFFER;
6157 it->object = it->w->buffer;
6158 reseat_at_next_visible_line_start (it, 1);
6159 it->face_before_selective_p = 1;
6160 }
6161
6162 return get_next_display_element (it);
6163 }
6164
6165
6166 /* Deliver an image display element. The iterator IT is already
6167 filled with image information (done in handle_display_prop). Value
6168 is always 1. */
6169
6170
6171 static int
6172 next_element_from_image (it)
6173 struct it *it;
6174 {
6175 it->what = IT_IMAGE;
6176 return 1;
6177 }
6178
6179
6180 /* Fill iterator IT with next display element from a stretch glyph
6181 property. IT->object is the value of the text property. Value is
6182 always 1. */
6183
6184 static int
6185 next_element_from_stretch (it)
6186 struct it *it;
6187 {
6188 it->what = IT_STRETCH;
6189 return 1;
6190 }
6191
6192
6193 /* Load IT with the next display element from current_buffer. Value
6194 is zero if end of buffer reached. IT->stop_charpos is the next
6195 position at which to stop and check for text properties or buffer
6196 end. */
6197
6198 static int
6199 next_element_from_buffer (it)
6200 struct it *it;
6201 {
6202 int success_p = 1;
6203
6204 /* Check this assumption, otherwise, we would never enter the
6205 if-statement, below. */
6206 xassert (IT_CHARPOS (*it) >= BEGV
6207 && IT_CHARPOS (*it) <= it->stop_charpos);
6208
6209 if (IT_CHARPOS (*it) >= it->stop_charpos)
6210 {
6211 if (IT_CHARPOS (*it) >= it->end_charpos)
6212 {
6213 int overlay_strings_follow_p;
6214
6215 /* End of the game, except when overlay strings follow that
6216 haven't been returned yet. */
6217 if (it->overlay_strings_at_end_processed_p)
6218 overlay_strings_follow_p = 0;
6219 else
6220 {
6221 it->overlay_strings_at_end_processed_p = 1;
6222 overlay_strings_follow_p = get_overlay_strings (it, 0);
6223 }
6224
6225 if (overlay_strings_follow_p)
6226 success_p = get_next_display_element (it);
6227 else
6228 {
6229 it->what = IT_EOB;
6230 it->position = it->current.pos;
6231 success_p = 0;
6232 }
6233 }
6234 else
6235 {
6236 handle_stop (it);
6237 return get_next_display_element (it);
6238 }
6239 }
6240 else
6241 {
6242 /* No face changes, overlays etc. in sight, so just return a
6243 character from current_buffer. */
6244 unsigned char *p;
6245
6246 /* Maybe run the redisplay end trigger hook. Performance note:
6247 This doesn't seem to cost measurable time. */
6248 if (it->redisplay_end_trigger_charpos
6249 && it->glyph_row
6250 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6251 run_redisplay_end_trigger_hook (it);
6252
6253 /* Get the next character, maybe multibyte. */
6254 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6255 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6256 {
6257 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6258 - IT_BYTEPOS (*it));
6259 it->c = string_char_and_length (p, maxlen, &it->len);
6260 }
6261 else
6262 it->c = *p, it->len = 1;
6263
6264 /* Record what we have and where it came from. */
6265 it->what = IT_CHARACTER;;
6266 it->object = it->w->buffer;
6267 it->position = it->current.pos;
6268
6269 /* Normally we return the character found above, except when we
6270 really want to return an ellipsis for selective display. */
6271 if (it->selective)
6272 {
6273 if (it->c == '\n')
6274 {
6275 /* A value of selective > 0 means hide lines indented more
6276 than that number of columns. */
6277 if (it->selective > 0
6278 && IT_CHARPOS (*it) + 1 < ZV
6279 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6280 IT_BYTEPOS (*it) + 1,
6281 (double) it->selective)) /* iftc */
6282 {
6283 success_p = next_element_from_ellipsis (it);
6284 it->dpvec_char_len = -1;
6285 }
6286 }
6287 else if (it->c == '\r' && it->selective == -1)
6288 {
6289 /* A value of selective == -1 means that everything from the
6290 CR to the end of the line is invisible, with maybe an
6291 ellipsis displayed for it. */
6292 success_p = next_element_from_ellipsis (it);
6293 it->dpvec_char_len = -1;
6294 }
6295 }
6296 }
6297
6298 /* Value is zero if end of buffer reached. */
6299 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6300 return success_p;
6301 }
6302
6303
6304 /* Run the redisplay end trigger hook for IT. */
6305
6306 static void
6307 run_redisplay_end_trigger_hook (it)
6308 struct it *it;
6309 {
6310 Lisp_Object args[3];
6311
6312 /* IT->glyph_row should be non-null, i.e. we should be actually
6313 displaying something, or otherwise we should not run the hook. */
6314 xassert (it->glyph_row);
6315
6316 /* Set up hook arguments. */
6317 args[0] = Qredisplay_end_trigger_functions;
6318 args[1] = it->window;
6319 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6320 it->redisplay_end_trigger_charpos = 0;
6321
6322 /* Since we are *trying* to run these functions, don't try to run
6323 them again, even if they get an error. */
6324 it->w->redisplay_end_trigger = Qnil;
6325 Frun_hook_with_args (3, args);
6326
6327 /* Notice if it changed the face of the character we are on. */
6328 handle_face_prop (it);
6329 }
6330
6331
6332 /* Deliver a composition display element. The iterator IT is already
6333 filled with composition information (done in
6334 handle_composition_prop). Value is always 1. */
6335
6336 static int
6337 next_element_from_composition (it)
6338 struct it *it;
6339 {
6340 it->what = IT_COMPOSITION;
6341 it->position = (STRINGP (it->string)
6342 ? it->current.string_pos
6343 : it->current.pos);
6344 if (STRINGP (it->string))
6345 it->object = it->string;
6346 else
6347 it->object = it->w->buffer;
6348 return 1;
6349 }
6350
6351
6352 \f
6353 /***********************************************************************
6354 Moving an iterator without producing glyphs
6355 ***********************************************************************/
6356
6357 /* Check if iterator is at a position corresponding to a valid buffer
6358 position after some move_it_ call. */
6359
6360 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6361 ((it)->method == GET_FROM_STRING \
6362 ? IT_STRING_CHARPOS (*it) == 0 \
6363 : 1)
6364
6365
6366 /* Move iterator IT to a specified buffer or X position within one
6367 line on the display without producing glyphs.
6368
6369 OP should be a bit mask including some or all of these bits:
6370 MOVE_TO_X: Stop on reaching x-position TO_X.
6371 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6372 Regardless of OP's value, stop in reaching the end of the display line.
6373
6374 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6375 This means, in particular, that TO_X includes window's horizontal
6376 scroll amount.
6377
6378 The return value has several possible values that
6379 say what condition caused the scan to stop:
6380
6381 MOVE_POS_MATCH_OR_ZV
6382 - when TO_POS or ZV was reached.
6383
6384 MOVE_X_REACHED
6385 -when TO_X was reached before TO_POS or ZV were reached.
6386
6387 MOVE_LINE_CONTINUED
6388 - when we reached the end of the display area and the line must
6389 be continued.
6390
6391 MOVE_LINE_TRUNCATED
6392 - when we reached the end of the display area and the line is
6393 truncated.
6394
6395 MOVE_NEWLINE_OR_CR
6396 - when we stopped at a line end, i.e. a newline or a CR and selective
6397 display is on. */
6398
6399 static enum move_it_result
6400 move_it_in_display_line_to (it, to_charpos, to_x, op)
6401 struct it *it;
6402 int to_charpos, to_x, op;
6403 {
6404 enum move_it_result result = MOVE_UNDEFINED;
6405 struct glyph_row *saved_glyph_row;
6406
6407 /* Don't produce glyphs in produce_glyphs. */
6408 saved_glyph_row = it->glyph_row;
6409 it->glyph_row = NULL;
6410
6411 #define BUFFER_POS_REACHED_P() \
6412 ((op & MOVE_TO_POS) != 0 \
6413 && BUFFERP (it->object) \
6414 && IT_CHARPOS (*it) >= to_charpos \
6415 && (it->method == GET_FROM_BUFFER \
6416 || (it->method == GET_FROM_DISPLAY_VECTOR \
6417 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6418
6419
6420 while (1)
6421 {
6422 int x, i, ascent = 0, descent = 0;
6423
6424 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6425 if ((op & MOVE_TO_POS) != 0
6426 && BUFFERP (it->object)
6427 && it->method == GET_FROM_BUFFER
6428 && IT_CHARPOS (*it) > to_charpos)
6429 {
6430 result = MOVE_POS_MATCH_OR_ZV;
6431 break;
6432 }
6433
6434 /* Stop when ZV reached.
6435 We used to stop here when TO_CHARPOS reached as well, but that is
6436 too soon if this glyph does not fit on this line. So we handle it
6437 explicitly below. */
6438 if (!get_next_display_element (it)
6439 || (it->truncate_lines_p
6440 && BUFFER_POS_REACHED_P ()))
6441 {
6442 result = MOVE_POS_MATCH_OR_ZV;
6443 break;
6444 }
6445
6446 /* The call to produce_glyphs will get the metrics of the
6447 display element IT is loaded with. We record in x the
6448 x-position before this display element in case it does not
6449 fit on the line. */
6450 x = it->current_x;
6451
6452 /* Remember the line height so far in case the next element doesn't
6453 fit on the line. */
6454 if (!it->truncate_lines_p)
6455 {
6456 ascent = it->max_ascent;
6457 descent = it->max_descent;
6458 }
6459
6460 PRODUCE_GLYPHS (it);
6461
6462 if (it->area != TEXT_AREA)
6463 {
6464 set_iterator_to_next (it, 1);
6465 continue;
6466 }
6467
6468 /* The number of glyphs we get back in IT->nglyphs will normally
6469 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6470 character on a terminal frame, or (iii) a line end. For the
6471 second case, IT->nglyphs - 1 padding glyphs will be present
6472 (on X frames, there is only one glyph produced for a
6473 composite character.
6474
6475 The behavior implemented below means, for continuation lines,
6476 that as many spaces of a TAB as fit on the current line are
6477 displayed there. For terminal frames, as many glyphs of a
6478 multi-glyph character are displayed in the current line, too.
6479 This is what the old redisplay code did, and we keep it that
6480 way. Under X, the whole shape of a complex character must
6481 fit on the line or it will be completely displayed in the
6482 next line.
6483
6484 Note that both for tabs and padding glyphs, all glyphs have
6485 the same width. */
6486 if (it->nglyphs)
6487 {
6488 /* More than one glyph or glyph doesn't fit on line. All
6489 glyphs have the same width. */
6490 int single_glyph_width = it->pixel_width / it->nglyphs;
6491 int new_x;
6492 int x_before_this_char = x;
6493 int hpos_before_this_char = it->hpos;
6494
6495 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6496 {
6497 new_x = x + single_glyph_width;
6498
6499 /* We want to leave anything reaching TO_X to the caller. */
6500 if ((op & MOVE_TO_X) && new_x > to_x)
6501 {
6502 if (BUFFER_POS_REACHED_P ())
6503 goto buffer_pos_reached;
6504 it->current_x = x;
6505 result = MOVE_X_REACHED;
6506 break;
6507 }
6508 else if (/* Lines are continued. */
6509 !it->truncate_lines_p
6510 && (/* And glyph doesn't fit on the line. */
6511 new_x > it->last_visible_x
6512 /* Or it fits exactly and we're on a window
6513 system frame. */
6514 || (new_x == it->last_visible_x
6515 && FRAME_WINDOW_P (it->f))))
6516 {
6517 if (/* IT->hpos == 0 means the very first glyph
6518 doesn't fit on the line, e.g. a wide image. */
6519 it->hpos == 0
6520 || (new_x == it->last_visible_x
6521 && FRAME_WINDOW_P (it->f)))
6522 {
6523 ++it->hpos;
6524 it->current_x = new_x;
6525
6526 /* The character's last glyph just barely fits
6527 in this row. */
6528 if (i == it->nglyphs - 1)
6529 {
6530 /* If this is the destination position,
6531 return a position *before* it in this row,
6532 now that we know it fits in this row. */
6533 if (BUFFER_POS_REACHED_P ())
6534 {
6535 it->hpos = hpos_before_this_char;
6536 it->current_x = x_before_this_char;
6537 result = MOVE_POS_MATCH_OR_ZV;
6538 break;
6539 }
6540
6541 set_iterator_to_next (it, 1);
6542 #ifdef HAVE_WINDOW_SYSTEM
6543 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6544 {
6545 if (!get_next_display_element (it))
6546 {
6547 result = MOVE_POS_MATCH_OR_ZV;
6548 break;
6549 }
6550 if (BUFFER_POS_REACHED_P ())
6551 {
6552 if (ITERATOR_AT_END_OF_LINE_P (it))
6553 result = MOVE_POS_MATCH_OR_ZV;
6554 else
6555 result = MOVE_LINE_CONTINUED;
6556 break;
6557 }
6558 if (ITERATOR_AT_END_OF_LINE_P (it))
6559 {
6560 result = MOVE_NEWLINE_OR_CR;
6561 break;
6562 }
6563 }
6564 #endif /* HAVE_WINDOW_SYSTEM */
6565 }
6566 }
6567 else
6568 {
6569 it->current_x = x;
6570 it->max_ascent = ascent;
6571 it->max_descent = descent;
6572 }
6573
6574 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6575 IT_CHARPOS (*it)));
6576 result = MOVE_LINE_CONTINUED;
6577 break;
6578 }
6579 else if (BUFFER_POS_REACHED_P ())
6580 goto buffer_pos_reached;
6581 else if (new_x > it->first_visible_x)
6582 {
6583 /* Glyph is visible. Increment number of glyphs that
6584 would be displayed. */
6585 ++it->hpos;
6586 }
6587 else
6588 {
6589 /* Glyph is completely off the left margin of the display
6590 area. Nothing to do. */
6591 }
6592 }
6593
6594 if (result != MOVE_UNDEFINED)
6595 break;
6596 }
6597 else if (BUFFER_POS_REACHED_P ())
6598 {
6599 buffer_pos_reached:
6600 it->current_x = x;
6601 it->max_ascent = ascent;
6602 it->max_descent = descent;
6603 result = MOVE_POS_MATCH_OR_ZV;
6604 break;
6605 }
6606 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6607 {
6608 /* Stop when TO_X specified and reached. This check is
6609 necessary here because of lines consisting of a line end,
6610 only. The line end will not produce any glyphs and we
6611 would never get MOVE_X_REACHED. */
6612 xassert (it->nglyphs == 0);
6613 result = MOVE_X_REACHED;
6614 break;
6615 }
6616
6617 /* Is this a line end? If yes, we're done. */
6618 if (ITERATOR_AT_END_OF_LINE_P (it))
6619 {
6620 result = MOVE_NEWLINE_OR_CR;
6621 break;
6622 }
6623
6624 /* The current display element has been consumed. Advance
6625 to the next. */
6626 set_iterator_to_next (it, 1);
6627
6628 /* Stop if lines are truncated and IT's current x-position is
6629 past the right edge of the window now. */
6630 if (it->truncate_lines_p
6631 && it->current_x >= it->last_visible_x)
6632 {
6633 #ifdef HAVE_WINDOW_SYSTEM
6634 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6635 {
6636 if (!get_next_display_element (it)
6637 || BUFFER_POS_REACHED_P ())
6638 {
6639 result = MOVE_POS_MATCH_OR_ZV;
6640 break;
6641 }
6642 if (ITERATOR_AT_END_OF_LINE_P (it))
6643 {
6644 result = MOVE_NEWLINE_OR_CR;
6645 break;
6646 }
6647 }
6648 #endif /* HAVE_WINDOW_SYSTEM */
6649 result = MOVE_LINE_TRUNCATED;
6650 break;
6651 }
6652 }
6653
6654 #undef BUFFER_POS_REACHED_P
6655
6656 /* Restore the iterator settings altered at the beginning of this
6657 function. */
6658 it->glyph_row = saved_glyph_row;
6659 return result;
6660 }
6661
6662
6663 /* Move IT forward until it satisfies one or more of the criteria in
6664 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6665
6666 OP is a bit-mask that specifies where to stop, and in particular,
6667 which of those four position arguments makes a difference. See the
6668 description of enum move_operation_enum.
6669
6670 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6671 screen line, this function will set IT to the next position >
6672 TO_CHARPOS. */
6673
6674 void
6675 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6676 struct it *it;
6677 int to_charpos, to_x, to_y, to_vpos;
6678 int op;
6679 {
6680 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6681 int line_height;
6682 int reached = 0;
6683
6684 for (;;)
6685 {
6686 if (op & MOVE_TO_VPOS)
6687 {
6688 /* If no TO_CHARPOS and no TO_X specified, stop at the
6689 start of the line TO_VPOS. */
6690 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6691 {
6692 if (it->vpos == to_vpos)
6693 {
6694 reached = 1;
6695 break;
6696 }
6697 else
6698 skip = move_it_in_display_line_to (it, -1, -1, 0);
6699 }
6700 else
6701 {
6702 /* TO_VPOS >= 0 means stop at TO_X in the line at
6703 TO_VPOS, or at TO_POS, whichever comes first. */
6704 if (it->vpos == to_vpos)
6705 {
6706 reached = 2;
6707 break;
6708 }
6709
6710 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6711
6712 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6713 {
6714 reached = 3;
6715 break;
6716 }
6717 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6718 {
6719 /* We have reached TO_X but not in the line we want. */
6720 skip = move_it_in_display_line_to (it, to_charpos,
6721 -1, MOVE_TO_POS);
6722 if (skip == MOVE_POS_MATCH_OR_ZV)
6723 {
6724 reached = 4;
6725 break;
6726 }
6727 }
6728 }
6729 }
6730 else if (op & MOVE_TO_Y)
6731 {
6732 struct it it_backup;
6733
6734 /* TO_Y specified means stop at TO_X in the line containing
6735 TO_Y---or at TO_CHARPOS if this is reached first. The
6736 problem is that we can't really tell whether the line
6737 contains TO_Y before we have completely scanned it, and
6738 this may skip past TO_X. What we do is to first scan to
6739 TO_X.
6740
6741 If TO_X is not specified, use a TO_X of zero. The reason
6742 is to make the outcome of this function more predictable.
6743 If we didn't use TO_X == 0, we would stop at the end of
6744 the line which is probably not what a caller would expect
6745 to happen. */
6746 skip = move_it_in_display_line_to (it, to_charpos,
6747 ((op & MOVE_TO_X)
6748 ? to_x : 0),
6749 (MOVE_TO_X
6750 | (op & MOVE_TO_POS)));
6751
6752 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6753 if (skip == MOVE_POS_MATCH_OR_ZV)
6754 {
6755 reached = 5;
6756 break;
6757 }
6758
6759 /* If TO_X was reached, we would like to know whether TO_Y
6760 is in the line. This can only be said if we know the
6761 total line height which requires us to scan the rest of
6762 the line. */
6763 if (skip == MOVE_X_REACHED)
6764 {
6765 it_backup = *it;
6766 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6767 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6768 op & MOVE_TO_POS);
6769 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6770 }
6771
6772 /* Now, decide whether TO_Y is in this line. */
6773 line_height = it->max_ascent + it->max_descent;
6774 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6775
6776 if (to_y >= it->current_y
6777 && to_y < it->current_y + line_height)
6778 {
6779 if (skip == MOVE_X_REACHED)
6780 /* If TO_Y is in this line and TO_X was reached above,
6781 we scanned too far. We have to restore IT's settings
6782 to the ones before skipping. */
6783 *it = it_backup;
6784 reached = 6;
6785 }
6786 else if (skip == MOVE_X_REACHED)
6787 {
6788 skip = skip2;
6789 if (skip == MOVE_POS_MATCH_OR_ZV)
6790 reached = 7;
6791 }
6792
6793 if (reached)
6794 break;
6795 }
6796 else if (BUFFERP (it->object)
6797 && it->method == GET_FROM_BUFFER
6798 && IT_CHARPOS (*it) >= to_charpos)
6799 skip = MOVE_POS_MATCH_OR_ZV;
6800 else
6801 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6802
6803 switch (skip)
6804 {
6805 case MOVE_POS_MATCH_OR_ZV:
6806 reached = 8;
6807 goto out;
6808
6809 case MOVE_NEWLINE_OR_CR:
6810 set_iterator_to_next (it, 1);
6811 it->continuation_lines_width = 0;
6812 break;
6813
6814 case MOVE_LINE_TRUNCATED:
6815 it->continuation_lines_width = 0;
6816 reseat_at_next_visible_line_start (it, 0);
6817 if ((op & MOVE_TO_POS) != 0
6818 && IT_CHARPOS (*it) > to_charpos)
6819 {
6820 reached = 9;
6821 goto out;
6822 }
6823 break;
6824
6825 case MOVE_LINE_CONTINUED:
6826 it->continuation_lines_width += it->current_x;
6827 break;
6828
6829 default:
6830 abort ();
6831 }
6832
6833 /* Reset/increment for the next run. */
6834 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6835 it->current_x = it->hpos = 0;
6836 it->current_y += it->max_ascent + it->max_descent;
6837 ++it->vpos;
6838 last_height = it->max_ascent + it->max_descent;
6839 last_max_ascent = it->max_ascent;
6840 it->max_ascent = it->max_descent = 0;
6841 }
6842
6843 out:
6844
6845 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6846 }
6847
6848
6849 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6850
6851 If DY > 0, move IT backward at least that many pixels. DY = 0
6852 means move IT backward to the preceding line start or BEGV. This
6853 function may move over more than DY pixels if IT->current_y - DY
6854 ends up in the middle of a line; in this case IT->current_y will be
6855 set to the top of the line moved to. */
6856
6857 void
6858 move_it_vertically_backward (it, dy)
6859 struct it *it;
6860 int dy;
6861 {
6862 int nlines, h;
6863 struct it it2, it3;
6864 int start_pos;
6865
6866 move_further_back:
6867 xassert (dy >= 0);
6868
6869 start_pos = IT_CHARPOS (*it);
6870
6871 /* Estimate how many newlines we must move back. */
6872 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6873
6874 /* Set the iterator's position that many lines back. */
6875 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6876 back_to_previous_visible_line_start (it);
6877
6878 /* Reseat the iterator here. When moving backward, we don't want
6879 reseat to skip forward over invisible text, set up the iterator
6880 to deliver from overlay strings at the new position etc. So,
6881 use reseat_1 here. */
6882 reseat_1 (it, it->current.pos, 1);
6883
6884 /* We are now surely at a line start. */
6885 it->current_x = it->hpos = 0;
6886 it->continuation_lines_width = 0;
6887
6888 /* Move forward and see what y-distance we moved. First move to the
6889 start of the next line so that we get its height. We need this
6890 height to be able to tell whether we reached the specified
6891 y-distance. */
6892 it2 = *it;
6893 it2.max_ascent = it2.max_descent = 0;
6894 do
6895 {
6896 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6897 MOVE_TO_POS | MOVE_TO_VPOS);
6898 }
6899 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6900 xassert (IT_CHARPOS (*it) >= BEGV);
6901 it3 = it2;
6902
6903 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6904 xassert (IT_CHARPOS (*it) >= BEGV);
6905 /* H is the actual vertical distance from the position in *IT
6906 and the starting position. */
6907 h = it2.current_y - it->current_y;
6908 /* NLINES is the distance in number of lines. */
6909 nlines = it2.vpos - it->vpos;
6910
6911 /* Correct IT's y and vpos position
6912 so that they are relative to the starting point. */
6913 it->vpos -= nlines;
6914 it->current_y -= h;
6915
6916 if (dy == 0)
6917 {
6918 /* DY == 0 means move to the start of the screen line. The
6919 value of nlines is > 0 if continuation lines were involved. */
6920 if (nlines > 0)
6921 move_it_by_lines (it, nlines, 1);
6922 #if 0
6923 /* I think this assert is bogus if buffer contains
6924 invisible text or images. KFS. */
6925 xassert (IT_CHARPOS (*it) <= start_pos);
6926 #endif
6927 }
6928 else
6929 {
6930 /* The y-position we try to reach, relative to *IT.
6931 Note that H has been subtracted in front of the if-statement. */
6932 int target_y = it->current_y + h - dy;
6933 int y0 = it3.current_y;
6934 int y1 = line_bottom_y (&it3);
6935 int line_height = y1 - y0;
6936
6937 /* If we did not reach target_y, try to move further backward if
6938 we can. If we moved too far backward, try to move forward. */
6939 if (target_y < it->current_y
6940 /* This is heuristic. In a window that's 3 lines high, with
6941 a line height of 13 pixels each, recentering with point
6942 on the bottom line will try to move -39/2 = 19 pixels
6943 backward. Try to avoid moving into the first line. */
6944 && (it->current_y - target_y
6945 > min (window_box_height (it->w), line_height * 2 / 3))
6946 && IT_CHARPOS (*it) > BEGV)
6947 {
6948 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6949 target_y - it->current_y));
6950 dy = it->current_y - target_y;
6951 goto move_further_back;
6952 }
6953 else if (target_y >= it->current_y + line_height
6954 && IT_CHARPOS (*it) < ZV)
6955 {
6956 /* Should move forward by at least one line, maybe more.
6957
6958 Note: Calling move_it_by_lines can be expensive on
6959 terminal frames, where compute_motion is used (via
6960 vmotion) to do the job, when there are very long lines
6961 and truncate-lines is nil. That's the reason for
6962 treating terminal frames specially here. */
6963
6964 if (!FRAME_WINDOW_P (it->f))
6965 move_it_vertically (it, target_y - (it->current_y + line_height));
6966 else
6967 {
6968 do
6969 {
6970 move_it_by_lines (it, 1, 1);
6971 }
6972 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6973 }
6974
6975 #if 0
6976 /* I think this assert is bogus if buffer contains
6977 invisible text or images. KFS. */
6978 xassert (IT_CHARPOS (*it) >= BEGV);
6979 #endif
6980 }
6981 }
6982 }
6983
6984
6985 /* Move IT by a specified amount of pixel lines DY. DY negative means
6986 move backwards. DY = 0 means move to start of screen line. At the
6987 end, IT will be on the start of a screen line. */
6988
6989 void
6990 move_it_vertically (it, dy)
6991 struct it *it;
6992 int dy;
6993 {
6994 if (dy <= 0)
6995 move_it_vertically_backward (it, -dy);
6996 else
6997 {
6998 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6999 move_it_to (it, ZV, -1, it->current_y + dy, -1,
7000 MOVE_TO_POS | MOVE_TO_Y);
7001 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
7002
7003 /* If buffer ends in ZV without a newline, move to the start of
7004 the line to satisfy the post-condition. */
7005 if (IT_CHARPOS (*it) == ZV
7006 && ZV > BEGV
7007 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
7008 move_it_by_lines (it, 0, 0);
7009 }
7010 }
7011
7012
7013 /* Move iterator IT past the end of the text line it is in. */
7014
7015 void
7016 move_it_past_eol (it)
7017 struct it *it;
7018 {
7019 enum move_it_result rc;
7020
7021 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7022 if (rc == MOVE_NEWLINE_OR_CR)
7023 set_iterator_to_next (it, 0);
7024 }
7025
7026
7027 #if 0 /* Currently not used. */
7028
7029 /* Return non-zero if some text between buffer positions START_CHARPOS
7030 and END_CHARPOS is invisible. IT->window is the window for text
7031 property lookup. */
7032
7033 static int
7034 invisible_text_between_p (it, start_charpos, end_charpos)
7035 struct it *it;
7036 int start_charpos, end_charpos;
7037 {
7038 Lisp_Object prop, limit;
7039 int invisible_found_p;
7040
7041 xassert (it != NULL && start_charpos <= end_charpos);
7042
7043 /* Is text at START invisible? */
7044 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7045 it->window);
7046 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7047 invisible_found_p = 1;
7048 else
7049 {
7050 limit = Fnext_single_char_property_change (make_number (start_charpos),
7051 Qinvisible, Qnil,
7052 make_number (end_charpos));
7053 invisible_found_p = XFASTINT (limit) < end_charpos;
7054 }
7055
7056 return invisible_found_p;
7057 }
7058
7059 #endif /* 0 */
7060
7061
7062 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7063 negative means move up. DVPOS == 0 means move to the start of the
7064 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7065 NEED_Y_P is zero, IT->current_y will be left unchanged.
7066
7067 Further optimization ideas: If we would know that IT->f doesn't use
7068 a face with proportional font, we could be faster for
7069 truncate-lines nil. */
7070
7071 void
7072 move_it_by_lines (it, dvpos, need_y_p)
7073 struct it *it;
7074 int dvpos, need_y_p;
7075 {
7076 struct position pos;
7077
7078 if (!FRAME_WINDOW_P (it->f))
7079 {
7080 struct text_pos textpos;
7081
7082 /* We can use vmotion on frames without proportional fonts. */
7083 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7084 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7085 reseat (it, textpos, 1);
7086 it->vpos += pos.vpos;
7087 it->current_y += pos.vpos;
7088 }
7089 else if (dvpos == 0)
7090 {
7091 /* DVPOS == 0 means move to the start of the screen line. */
7092 move_it_vertically_backward (it, 0);
7093 xassert (it->current_x == 0 && it->hpos == 0);
7094 /* Let next call to line_bottom_y calculate real line height */
7095 last_height = 0;
7096 }
7097 else if (dvpos > 0)
7098 {
7099 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7100 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7101 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7102 }
7103 else
7104 {
7105 struct it it2;
7106 int start_charpos, i;
7107
7108 /* Start at the beginning of the screen line containing IT's
7109 position. This may actually move vertically backwards,
7110 in case of overlays, so adjust dvpos accordingly. */
7111 dvpos += it->vpos;
7112 move_it_vertically_backward (it, 0);
7113 dvpos -= it->vpos;
7114
7115 /* Go back -DVPOS visible lines and reseat the iterator there. */
7116 start_charpos = IT_CHARPOS (*it);
7117 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7118 back_to_previous_visible_line_start (it);
7119 reseat (it, it->current.pos, 1);
7120
7121 /* Move further back if we end up in a string or an image. */
7122 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7123 {
7124 /* First try to move to start of display line. */
7125 dvpos += it->vpos;
7126 move_it_vertically_backward (it, 0);
7127 dvpos -= it->vpos;
7128 if (IT_POS_VALID_AFTER_MOVE_P (it))
7129 break;
7130 /* If start of line is still in string or image,
7131 move further back. */
7132 back_to_previous_visible_line_start (it);
7133 reseat (it, it->current.pos, 1);
7134 dvpos--;
7135 }
7136
7137 it->current_x = it->hpos = 0;
7138
7139 /* Above call may have moved too far if continuation lines
7140 are involved. Scan forward and see if it did. */
7141 it2 = *it;
7142 it2.vpos = it2.current_y = 0;
7143 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7144 it->vpos -= it2.vpos;
7145 it->current_y -= it2.current_y;
7146 it->current_x = it->hpos = 0;
7147
7148 /* If we moved too far back, move IT some lines forward. */
7149 if (it2.vpos > -dvpos)
7150 {
7151 int delta = it2.vpos + dvpos;
7152 it2 = *it;
7153 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7154 /* Move back again if we got too far ahead. */
7155 if (IT_CHARPOS (*it) >= start_charpos)
7156 *it = it2;
7157 }
7158 }
7159 }
7160
7161 /* Return 1 if IT points into the middle of a display vector. */
7162
7163 int
7164 in_display_vector_p (it)
7165 struct it *it;
7166 {
7167 return (it->method == GET_FROM_DISPLAY_VECTOR
7168 && it->current.dpvec_index > 0
7169 && it->dpvec + it->current.dpvec_index != it->dpend);
7170 }
7171
7172 \f
7173 /***********************************************************************
7174 Messages
7175 ***********************************************************************/
7176
7177
7178 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7179 to *Messages*. */
7180
7181 void
7182 add_to_log (format, arg1, arg2)
7183 char *format;
7184 Lisp_Object arg1, arg2;
7185 {
7186 Lisp_Object args[3];
7187 Lisp_Object msg, fmt;
7188 char *buffer;
7189 int len;
7190 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7191 USE_SAFE_ALLOCA;
7192
7193 /* Do nothing if called asynchronously. Inserting text into
7194 a buffer may call after-change-functions and alike and
7195 that would means running Lisp asynchronously. */
7196 if (handling_signal)
7197 return;
7198
7199 fmt = msg = Qnil;
7200 GCPRO4 (fmt, msg, arg1, arg2);
7201
7202 args[0] = fmt = build_string (format);
7203 args[1] = arg1;
7204 args[2] = arg2;
7205 msg = Fformat (3, args);
7206
7207 len = SBYTES (msg) + 1;
7208 SAFE_ALLOCA (buffer, char *, len);
7209 bcopy (SDATA (msg), buffer, len);
7210
7211 message_dolog (buffer, len - 1, 1, 0);
7212 SAFE_FREE ();
7213
7214 UNGCPRO;
7215 }
7216
7217
7218 /* Output a newline in the *Messages* buffer if "needs" one. */
7219
7220 void
7221 message_log_maybe_newline ()
7222 {
7223 if (message_log_need_newline)
7224 message_dolog ("", 0, 1, 0);
7225 }
7226
7227
7228 /* Add a string M of length NBYTES to the message log, optionally
7229 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7230 nonzero, means interpret the contents of M as multibyte. This
7231 function calls low-level routines in order to bypass text property
7232 hooks, etc. which might not be safe to run.
7233
7234 This may GC (insert may run before/after change hooks),
7235 so the buffer M must NOT point to a Lisp string. */
7236
7237 void
7238 message_dolog (m, nbytes, nlflag, multibyte)
7239 const char *m;
7240 int nbytes, nlflag, multibyte;
7241 {
7242 if (!NILP (Vmemory_full))
7243 return;
7244
7245 if (!NILP (Vmessage_log_max))
7246 {
7247 struct buffer *oldbuf;
7248 Lisp_Object oldpoint, oldbegv, oldzv;
7249 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7250 int point_at_end = 0;
7251 int zv_at_end = 0;
7252 Lisp_Object old_deactivate_mark, tem;
7253 struct gcpro gcpro1;
7254
7255 old_deactivate_mark = Vdeactivate_mark;
7256 oldbuf = current_buffer;
7257 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7258 current_buffer->undo_list = Qt;
7259
7260 oldpoint = message_dolog_marker1;
7261 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7262 oldbegv = message_dolog_marker2;
7263 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7264 oldzv = message_dolog_marker3;
7265 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7266 GCPRO1 (old_deactivate_mark);
7267
7268 if (PT == Z)
7269 point_at_end = 1;
7270 if (ZV == Z)
7271 zv_at_end = 1;
7272
7273 BEGV = BEG;
7274 BEGV_BYTE = BEG_BYTE;
7275 ZV = Z;
7276 ZV_BYTE = Z_BYTE;
7277 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7278
7279 /* Insert the string--maybe converting multibyte to single byte
7280 or vice versa, so that all the text fits the buffer. */
7281 if (multibyte
7282 && NILP (current_buffer->enable_multibyte_characters))
7283 {
7284 int i, c, char_bytes;
7285 unsigned char work[1];
7286
7287 /* Convert a multibyte string to single-byte
7288 for the *Message* buffer. */
7289 for (i = 0; i < nbytes; i += char_bytes)
7290 {
7291 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7292 work[0] = (SINGLE_BYTE_CHAR_P (c)
7293 ? c
7294 : multibyte_char_to_unibyte (c, Qnil));
7295 insert_1_both (work, 1, 1, 1, 0, 0);
7296 }
7297 }
7298 else if (! multibyte
7299 && ! NILP (current_buffer->enable_multibyte_characters))
7300 {
7301 int i, c, char_bytes;
7302 unsigned char *msg = (unsigned char *) m;
7303 unsigned char str[MAX_MULTIBYTE_LENGTH];
7304 /* Convert a single-byte string to multibyte
7305 for the *Message* buffer. */
7306 for (i = 0; i < nbytes; i++)
7307 {
7308 c = unibyte_char_to_multibyte (msg[i]);
7309 char_bytes = CHAR_STRING (c, str);
7310 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7311 }
7312 }
7313 else if (nbytes)
7314 insert_1 (m, nbytes, 1, 0, 0);
7315
7316 if (nlflag)
7317 {
7318 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7319 insert_1 ("\n", 1, 1, 0, 0);
7320
7321 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7322 this_bol = PT;
7323 this_bol_byte = PT_BYTE;
7324
7325 /* See if this line duplicates the previous one.
7326 If so, combine duplicates. */
7327 if (this_bol > BEG)
7328 {
7329 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7330 prev_bol = PT;
7331 prev_bol_byte = PT_BYTE;
7332
7333 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7334 this_bol, this_bol_byte);
7335 if (dup)
7336 {
7337 del_range_both (prev_bol, prev_bol_byte,
7338 this_bol, this_bol_byte, 0);
7339 if (dup > 1)
7340 {
7341 char dupstr[40];
7342 int duplen;
7343
7344 /* If you change this format, don't forget to also
7345 change message_log_check_duplicate. */
7346 sprintf (dupstr, " [%d times]", dup);
7347 duplen = strlen (dupstr);
7348 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7349 insert_1 (dupstr, duplen, 1, 0, 1);
7350 }
7351 }
7352 }
7353
7354 /* If we have more than the desired maximum number of lines
7355 in the *Messages* buffer now, delete the oldest ones.
7356 This is safe because we don't have undo in this buffer. */
7357
7358 if (NATNUMP (Vmessage_log_max))
7359 {
7360 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7361 -XFASTINT (Vmessage_log_max) - 1, 0);
7362 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7363 }
7364 }
7365 BEGV = XMARKER (oldbegv)->charpos;
7366 BEGV_BYTE = marker_byte_position (oldbegv);
7367
7368 if (zv_at_end)
7369 {
7370 ZV = Z;
7371 ZV_BYTE = Z_BYTE;
7372 }
7373 else
7374 {
7375 ZV = XMARKER (oldzv)->charpos;
7376 ZV_BYTE = marker_byte_position (oldzv);
7377 }
7378
7379 if (point_at_end)
7380 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7381 else
7382 /* We can't do Fgoto_char (oldpoint) because it will run some
7383 Lisp code. */
7384 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7385 XMARKER (oldpoint)->bytepos);
7386
7387 UNGCPRO;
7388 unchain_marker (XMARKER (oldpoint));
7389 unchain_marker (XMARKER (oldbegv));
7390 unchain_marker (XMARKER (oldzv));
7391
7392 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7393 set_buffer_internal (oldbuf);
7394 if (NILP (tem))
7395 windows_or_buffers_changed = old_windows_or_buffers_changed;
7396 message_log_need_newline = !nlflag;
7397 Vdeactivate_mark = old_deactivate_mark;
7398 }
7399 }
7400
7401
7402 /* We are at the end of the buffer after just having inserted a newline.
7403 (Note: We depend on the fact we won't be crossing the gap.)
7404 Check to see if the most recent message looks a lot like the previous one.
7405 Return 0 if different, 1 if the new one should just replace it, or a
7406 value N > 1 if we should also append " [N times]". */
7407
7408 static int
7409 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7410 int prev_bol, this_bol;
7411 int prev_bol_byte, this_bol_byte;
7412 {
7413 int i;
7414 int len = Z_BYTE - 1 - this_bol_byte;
7415 int seen_dots = 0;
7416 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7417 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7418
7419 for (i = 0; i < len; i++)
7420 {
7421 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7422 seen_dots = 1;
7423 if (p1[i] != p2[i])
7424 return seen_dots;
7425 }
7426 p1 += len;
7427 if (*p1 == '\n')
7428 return 2;
7429 if (*p1++ == ' ' && *p1++ == '[')
7430 {
7431 int n = 0;
7432 while (*p1 >= '0' && *p1 <= '9')
7433 n = n * 10 + *p1++ - '0';
7434 if (strncmp (p1, " times]\n", 8) == 0)
7435 return n+1;
7436 }
7437 return 0;
7438 }
7439 \f
7440
7441 /* Display an echo area message M with a specified length of NBYTES
7442 bytes. The string may include null characters. If M is 0, clear
7443 out any existing message, and let the mini-buffer text show
7444 through.
7445
7446 This may GC, so the buffer M must NOT point to a Lisp string. */
7447
7448 void
7449 message2 (m, nbytes, multibyte)
7450 const char *m;
7451 int nbytes;
7452 int multibyte;
7453 {
7454 /* First flush out any partial line written with print. */
7455 message_log_maybe_newline ();
7456 if (m)
7457 message_dolog (m, nbytes, 1, multibyte);
7458 message2_nolog (m, nbytes, multibyte);
7459 }
7460
7461
7462 /* The non-logging counterpart of message2. */
7463
7464 void
7465 message2_nolog (m, nbytes, multibyte)
7466 const char *m;
7467 int nbytes, multibyte;
7468 {
7469 struct frame *sf = SELECTED_FRAME ();
7470 message_enable_multibyte = multibyte;
7471
7472 if (noninteractive)
7473 {
7474 if (noninteractive_need_newline)
7475 putc ('\n', stderr);
7476 noninteractive_need_newline = 0;
7477 if (m)
7478 fwrite (m, nbytes, 1, stderr);
7479 if (cursor_in_echo_area == 0)
7480 fprintf (stderr, "\n");
7481 fflush (stderr);
7482 }
7483 /* A null message buffer means that the frame hasn't really been
7484 initialized yet. Error messages get reported properly by
7485 cmd_error, so this must be just an informative message; toss it. */
7486 else if (INTERACTIVE
7487 && sf->glyphs_initialized_p
7488 && FRAME_MESSAGE_BUF (sf))
7489 {
7490 Lisp_Object mini_window;
7491 struct frame *f;
7492
7493 /* Get the frame containing the mini-buffer
7494 that the selected frame is using. */
7495 mini_window = FRAME_MINIBUF_WINDOW (sf);
7496 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7497
7498 FRAME_SAMPLE_VISIBILITY (f);
7499 if (FRAME_VISIBLE_P (sf)
7500 && ! FRAME_VISIBLE_P (f))
7501 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7502
7503 if (m)
7504 {
7505 set_message (m, Qnil, nbytes, multibyte);
7506 if (minibuffer_auto_raise)
7507 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7508 }
7509 else
7510 clear_message (1, 1);
7511
7512 do_pending_window_change (0);
7513 echo_area_display (1);
7514 do_pending_window_change (0);
7515 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7516 (*frame_up_to_date_hook) (f);
7517 }
7518 }
7519
7520
7521 /* Display an echo area message M with a specified length of NBYTES
7522 bytes. The string may include null characters. If M is not a
7523 string, clear out any existing message, and let the mini-buffer
7524 text show through.
7525
7526 This function cancels echoing. */
7527
7528 void
7529 message3 (m, nbytes, multibyte)
7530 Lisp_Object m;
7531 int nbytes;
7532 int multibyte;
7533 {
7534 struct gcpro gcpro1;
7535
7536 GCPRO1 (m);
7537 clear_message (1,1);
7538 cancel_echoing ();
7539
7540 /* First flush out any partial line written with print. */
7541 message_log_maybe_newline ();
7542 if (STRINGP (m))
7543 {
7544 char *buffer;
7545 USE_SAFE_ALLOCA;
7546
7547 SAFE_ALLOCA (buffer, char *, nbytes);
7548 bcopy (SDATA (m), buffer, nbytes);
7549 message_dolog (buffer, nbytes, 1, multibyte);
7550 SAFE_FREE ();
7551 }
7552 message3_nolog (m, nbytes, multibyte);
7553
7554 UNGCPRO;
7555 }
7556
7557
7558 /* The non-logging version of message3.
7559 This does not cancel echoing, because it is used for echoing.
7560 Perhaps we need to make a separate function for echoing
7561 and make this cancel echoing. */
7562
7563 void
7564 message3_nolog (m, nbytes, multibyte)
7565 Lisp_Object m;
7566 int nbytes, multibyte;
7567 {
7568 struct frame *sf = SELECTED_FRAME ();
7569 message_enable_multibyte = multibyte;
7570
7571 if (noninteractive)
7572 {
7573 if (noninteractive_need_newline)
7574 putc ('\n', stderr);
7575 noninteractive_need_newline = 0;
7576 if (STRINGP (m))
7577 fwrite (SDATA (m), nbytes, 1, stderr);
7578 if (cursor_in_echo_area == 0)
7579 fprintf (stderr, "\n");
7580 fflush (stderr);
7581 }
7582 /* A null message buffer means that the frame hasn't really been
7583 initialized yet. Error messages get reported properly by
7584 cmd_error, so this must be just an informative message; toss it. */
7585 else if (INTERACTIVE
7586 && sf->glyphs_initialized_p
7587 && FRAME_MESSAGE_BUF (sf))
7588 {
7589 Lisp_Object mini_window;
7590 Lisp_Object frame;
7591 struct frame *f;
7592
7593 /* Get the frame containing the mini-buffer
7594 that the selected frame is using. */
7595 mini_window = FRAME_MINIBUF_WINDOW (sf);
7596 frame = XWINDOW (mini_window)->frame;
7597 f = XFRAME (frame);
7598
7599 FRAME_SAMPLE_VISIBILITY (f);
7600 if (FRAME_VISIBLE_P (sf)
7601 && !FRAME_VISIBLE_P (f))
7602 Fmake_frame_visible (frame);
7603
7604 if (STRINGP (m) && SCHARS (m) > 0)
7605 {
7606 set_message (NULL, m, nbytes, multibyte);
7607 if (minibuffer_auto_raise)
7608 Fraise_frame (frame);
7609 /* Assume we are not echoing.
7610 (If we are, echo_now will override this.) */
7611 echo_message_buffer = Qnil;
7612 }
7613 else
7614 clear_message (1, 1);
7615
7616 do_pending_window_change (0);
7617 echo_area_display (1);
7618 do_pending_window_change (0);
7619 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7620 (*frame_up_to_date_hook) (f);
7621 }
7622 }
7623
7624
7625 /* Display a null-terminated echo area message M. If M is 0, clear
7626 out any existing message, and let the mini-buffer text show through.
7627
7628 The buffer M must continue to exist until after the echo area gets
7629 cleared or some other message gets displayed there. Do not pass
7630 text that is stored in a Lisp string. Do not pass text in a buffer
7631 that was alloca'd. */
7632
7633 void
7634 message1 (m)
7635 char *m;
7636 {
7637 message2 (m, (m ? strlen (m) : 0), 0);
7638 }
7639
7640
7641 /* The non-logging counterpart of message1. */
7642
7643 void
7644 message1_nolog (m)
7645 char *m;
7646 {
7647 message2_nolog (m, (m ? strlen (m) : 0), 0);
7648 }
7649
7650 /* Display a message M which contains a single %s
7651 which gets replaced with STRING. */
7652
7653 void
7654 message_with_string (m, string, log)
7655 char *m;
7656 Lisp_Object string;
7657 int log;
7658 {
7659 CHECK_STRING (string);
7660
7661 if (noninteractive)
7662 {
7663 if (m)
7664 {
7665 if (noninteractive_need_newline)
7666 putc ('\n', stderr);
7667 noninteractive_need_newline = 0;
7668 fprintf (stderr, m, SDATA (string));
7669 if (cursor_in_echo_area == 0)
7670 fprintf (stderr, "\n");
7671 fflush (stderr);
7672 }
7673 }
7674 else if (INTERACTIVE)
7675 {
7676 /* The frame whose minibuffer we're going to display the message on.
7677 It may be larger than the selected frame, so we need
7678 to use its buffer, not the selected frame's buffer. */
7679 Lisp_Object mini_window;
7680 struct frame *f, *sf = SELECTED_FRAME ();
7681
7682 /* Get the frame containing the minibuffer
7683 that the selected frame is using. */
7684 mini_window = FRAME_MINIBUF_WINDOW (sf);
7685 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7686
7687 /* A null message buffer means that the frame hasn't really been
7688 initialized yet. Error messages get reported properly by
7689 cmd_error, so this must be just an informative message; toss it. */
7690 if (FRAME_MESSAGE_BUF (f))
7691 {
7692 Lisp_Object args[2], message;
7693 struct gcpro gcpro1, gcpro2;
7694
7695 args[0] = build_string (m);
7696 args[1] = message = string;
7697 GCPRO2 (args[0], message);
7698 gcpro1.nvars = 2;
7699
7700 message = Fformat (2, args);
7701
7702 if (log)
7703 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7704 else
7705 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7706
7707 UNGCPRO;
7708
7709 /* Print should start at the beginning of the message
7710 buffer next time. */
7711 message_buf_print = 0;
7712 }
7713 }
7714 }
7715
7716
7717 /* Dump an informative message to the minibuf. If M is 0, clear out
7718 any existing message, and let the mini-buffer text show through. */
7719
7720 /* VARARGS 1 */
7721 void
7722 message (m, a1, a2, a3)
7723 char *m;
7724 EMACS_INT a1, a2, a3;
7725 {
7726 if (noninteractive)
7727 {
7728 if (m)
7729 {
7730 if (noninteractive_need_newline)
7731 putc ('\n', stderr);
7732 noninteractive_need_newline = 0;
7733 fprintf (stderr, m, a1, a2, a3);
7734 if (cursor_in_echo_area == 0)
7735 fprintf (stderr, "\n");
7736 fflush (stderr);
7737 }
7738 }
7739 else if (INTERACTIVE)
7740 {
7741 /* The frame whose mini-buffer we're going to display the message
7742 on. It may be larger than the selected frame, so we need to
7743 use its buffer, not the selected frame's buffer. */
7744 Lisp_Object mini_window;
7745 struct frame *f, *sf = SELECTED_FRAME ();
7746
7747 /* Get the frame containing the mini-buffer
7748 that the selected frame is using. */
7749 mini_window = FRAME_MINIBUF_WINDOW (sf);
7750 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7751
7752 /* A null message buffer means that the frame hasn't really been
7753 initialized yet. Error messages get reported properly by
7754 cmd_error, so this must be just an informative message; toss
7755 it. */
7756 if (FRAME_MESSAGE_BUF (f))
7757 {
7758 if (m)
7759 {
7760 int len;
7761 #ifdef NO_ARG_ARRAY
7762 char *a[3];
7763 a[0] = (char *) a1;
7764 a[1] = (char *) a2;
7765 a[2] = (char *) a3;
7766
7767 len = doprnt (FRAME_MESSAGE_BUF (f),
7768 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7769 #else
7770 len = doprnt (FRAME_MESSAGE_BUF (f),
7771 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7772 (char **) &a1);
7773 #endif /* NO_ARG_ARRAY */
7774
7775 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7776 }
7777 else
7778 message1 (0);
7779
7780 /* Print should start at the beginning of the message
7781 buffer next time. */
7782 message_buf_print = 0;
7783 }
7784 }
7785 }
7786
7787
7788 /* The non-logging version of message. */
7789
7790 void
7791 message_nolog (m, a1, a2, a3)
7792 char *m;
7793 EMACS_INT a1, a2, a3;
7794 {
7795 Lisp_Object old_log_max;
7796 old_log_max = Vmessage_log_max;
7797 Vmessage_log_max = Qnil;
7798 message (m, a1, a2, a3);
7799 Vmessage_log_max = old_log_max;
7800 }
7801
7802
7803 /* Display the current message in the current mini-buffer. This is
7804 only called from error handlers in process.c, and is not time
7805 critical. */
7806
7807 void
7808 update_echo_area ()
7809 {
7810 if (!NILP (echo_area_buffer[0]))
7811 {
7812 Lisp_Object string;
7813 string = Fcurrent_message ();
7814 message3 (string, SBYTES (string),
7815 !NILP (current_buffer->enable_multibyte_characters));
7816 }
7817 }
7818
7819
7820 /* Make sure echo area buffers in `echo_buffers' are live.
7821 If they aren't, make new ones. */
7822
7823 static void
7824 ensure_echo_area_buffers ()
7825 {
7826 int i;
7827
7828 for (i = 0; i < 2; ++i)
7829 if (!BUFFERP (echo_buffer[i])
7830 || NILP (XBUFFER (echo_buffer[i])->name))
7831 {
7832 char name[30];
7833 Lisp_Object old_buffer;
7834 int j;
7835
7836 old_buffer = echo_buffer[i];
7837 sprintf (name, " *Echo Area %d*", i);
7838 echo_buffer[i] = Fget_buffer_create (build_string (name));
7839 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7840
7841 for (j = 0; j < 2; ++j)
7842 if (EQ (old_buffer, echo_area_buffer[j]))
7843 echo_area_buffer[j] = echo_buffer[i];
7844 }
7845 }
7846
7847
7848 /* Call FN with args A1..A4 with either the current or last displayed
7849 echo_area_buffer as current buffer.
7850
7851 WHICH zero means use the current message buffer
7852 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7853 from echo_buffer[] and clear it.
7854
7855 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7856 suitable buffer from echo_buffer[] and clear it.
7857
7858 Value is what FN returns. */
7859
7860 static int
7861 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7862 struct window *w;
7863 int which;
7864 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7865 EMACS_INT a1;
7866 Lisp_Object a2;
7867 EMACS_INT a3, a4;
7868 {
7869 Lisp_Object buffer;
7870 int this_one, the_other, clear_buffer_p, rc;
7871 int count = SPECPDL_INDEX ();
7872
7873 /* If buffers aren't live, make new ones. */
7874 ensure_echo_area_buffers ();
7875
7876 clear_buffer_p = 0;
7877
7878 if (which == 0)
7879 this_one = 0, the_other = 1;
7880 else if (which > 0)
7881 this_one = 1, the_other = 0;
7882
7883 /* Choose a suitable buffer from echo_buffer[] is we don't
7884 have one. */
7885 if (NILP (echo_area_buffer[this_one]))
7886 {
7887 echo_area_buffer[this_one]
7888 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7889 ? echo_buffer[the_other]
7890 : echo_buffer[this_one]);
7891 clear_buffer_p = 1;
7892 }
7893
7894 buffer = echo_area_buffer[this_one];
7895
7896 /* Don't get confused by reusing the buffer used for echoing
7897 for a different purpose. */
7898 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7899 cancel_echoing ();
7900
7901 record_unwind_protect (unwind_with_echo_area_buffer,
7902 with_echo_area_buffer_unwind_data (w));
7903
7904 /* Make the echo area buffer current. Note that for display
7905 purposes, it is not necessary that the displayed window's buffer
7906 == current_buffer, except for text property lookup. So, let's
7907 only set that buffer temporarily here without doing a full
7908 Fset_window_buffer. We must also change w->pointm, though,
7909 because otherwise an assertions in unshow_buffer fails, and Emacs
7910 aborts. */
7911 set_buffer_internal_1 (XBUFFER (buffer));
7912 if (w)
7913 {
7914 w->buffer = buffer;
7915 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7916 }
7917
7918 current_buffer->undo_list = Qt;
7919 current_buffer->read_only = Qnil;
7920 specbind (Qinhibit_read_only, Qt);
7921 specbind (Qinhibit_modification_hooks, Qt);
7922
7923 if (clear_buffer_p && Z > BEG)
7924 del_range (BEG, Z);
7925
7926 xassert (BEGV >= BEG);
7927 xassert (ZV <= Z && ZV >= BEGV);
7928
7929 rc = fn (a1, a2, a3, a4);
7930
7931 xassert (BEGV >= BEG);
7932 xassert (ZV <= Z && ZV >= BEGV);
7933
7934 unbind_to (count, Qnil);
7935 return rc;
7936 }
7937
7938
7939 /* Save state that should be preserved around the call to the function
7940 FN called in with_echo_area_buffer. */
7941
7942 static Lisp_Object
7943 with_echo_area_buffer_unwind_data (w)
7944 struct window *w;
7945 {
7946 int i = 0;
7947 Lisp_Object vector;
7948
7949 /* Reduce consing by keeping one vector in
7950 Vwith_echo_area_save_vector. */
7951 vector = Vwith_echo_area_save_vector;
7952 Vwith_echo_area_save_vector = Qnil;
7953
7954 if (NILP (vector))
7955 vector = Fmake_vector (make_number (7), Qnil);
7956
7957 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7958 AREF (vector, i) = Vdeactivate_mark, ++i;
7959 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7960
7961 if (w)
7962 {
7963 XSETWINDOW (AREF (vector, i), w); ++i;
7964 AREF (vector, i) = w->buffer; ++i;
7965 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7966 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7967 }
7968 else
7969 {
7970 int end = i + 4;
7971 for (; i < end; ++i)
7972 AREF (vector, i) = Qnil;
7973 }
7974
7975 xassert (i == ASIZE (vector));
7976 return vector;
7977 }
7978
7979
7980 /* Restore global state from VECTOR which was created by
7981 with_echo_area_buffer_unwind_data. */
7982
7983 static Lisp_Object
7984 unwind_with_echo_area_buffer (vector)
7985 Lisp_Object vector;
7986 {
7987 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7988 Vdeactivate_mark = AREF (vector, 1);
7989 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7990
7991 if (WINDOWP (AREF (vector, 3)))
7992 {
7993 struct window *w;
7994 Lisp_Object buffer, charpos, bytepos;
7995
7996 w = XWINDOW (AREF (vector, 3));
7997 buffer = AREF (vector, 4);
7998 charpos = AREF (vector, 5);
7999 bytepos = AREF (vector, 6);
8000
8001 w->buffer = buffer;
8002 set_marker_both (w->pointm, buffer,
8003 XFASTINT (charpos), XFASTINT (bytepos));
8004 }
8005
8006 Vwith_echo_area_save_vector = vector;
8007 return Qnil;
8008 }
8009
8010
8011 /* Set up the echo area for use by print functions. MULTIBYTE_P
8012 non-zero means we will print multibyte. */
8013
8014 void
8015 setup_echo_area_for_printing (multibyte_p)
8016 int multibyte_p;
8017 {
8018 /* If we can't find an echo area any more, exit. */
8019 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8020 Fkill_emacs (Qnil);
8021
8022 ensure_echo_area_buffers ();
8023
8024 if (!message_buf_print)
8025 {
8026 /* A message has been output since the last time we printed.
8027 Choose a fresh echo area buffer. */
8028 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8029 echo_area_buffer[0] = echo_buffer[1];
8030 else
8031 echo_area_buffer[0] = echo_buffer[0];
8032
8033 /* Switch to that buffer and clear it. */
8034 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8035 current_buffer->truncate_lines = Qnil;
8036
8037 if (Z > BEG)
8038 {
8039 int count = SPECPDL_INDEX ();
8040 specbind (Qinhibit_read_only, Qt);
8041 /* Note that undo recording is always disabled. */
8042 del_range (BEG, Z);
8043 unbind_to (count, Qnil);
8044 }
8045 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8046
8047 /* Set up the buffer for the multibyteness we need. */
8048 if (multibyte_p
8049 != !NILP (current_buffer->enable_multibyte_characters))
8050 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8051
8052 /* Raise the frame containing the echo area. */
8053 if (minibuffer_auto_raise)
8054 {
8055 struct frame *sf = SELECTED_FRAME ();
8056 Lisp_Object mini_window;
8057 mini_window = FRAME_MINIBUF_WINDOW (sf);
8058 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8059 }
8060
8061 message_log_maybe_newline ();
8062 message_buf_print = 1;
8063 }
8064 else
8065 {
8066 if (NILP (echo_area_buffer[0]))
8067 {
8068 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8069 echo_area_buffer[0] = echo_buffer[1];
8070 else
8071 echo_area_buffer[0] = echo_buffer[0];
8072 }
8073
8074 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8075 {
8076 /* Someone switched buffers between print requests. */
8077 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8078 current_buffer->truncate_lines = Qnil;
8079 }
8080 }
8081 }
8082
8083
8084 /* Display an echo area message in window W. Value is non-zero if W's
8085 height is changed. If display_last_displayed_message_p is
8086 non-zero, display the message that was last displayed, otherwise
8087 display the current message. */
8088
8089 static int
8090 display_echo_area (w)
8091 struct window *w;
8092 {
8093 int i, no_message_p, window_height_changed_p, count;
8094
8095 /* Temporarily disable garbage collections while displaying the echo
8096 area. This is done because a GC can print a message itself.
8097 That message would modify the echo area buffer's contents while a
8098 redisplay of the buffer is going on, and seriously confuse
8099 redisplay. */
8100 count = inhibit_garbage_collection ();
8101
8102 /* If there is no message, we must call display_echo_area_1
8103 nevertheless because it resizes the window. But we will have to
8104 reset the echo_area_buffer in question to nil at the end because
8105 with_echo_area_buffer will sets it to an empty buffer. */
8106 i = display_last_displayed_message_p ? 1 : 0;
8107 no_message_p = NILP (echo_area_buffer[i]);
8108
8109 window_height_changed_p
8110 = with_echo_area_buffer (w, display_last_displayed_message_p,
8111 display_echo_area_1,
8112 (EMACS_INT) w, Qnil, 0, 0);
8113
8114 if (no_message_p)
8115 echo_area_buffer[i] = Qnil;
8116
8117 unbind_to (count, Qnil);
8118 return window_height_changed_p;
8119 }
8120
8121
8122 /* Helper for display_echo_area. Display the current buffer which
8123 contains the current echo area message in window W, a mini-window,
8124 a pointer to which is passed in A1. A2..A4 are currently not used.
8125 Change the height of W so that all of the message is displayed.
8126 Value is non-zero if height of W was changed. */
8127
8128 static int
8129 display_echo_area_1 (a1, a2, a3, a4)
8130 EMACS_INT a1;
8131 Lisp_Object a2;
8132 EMACS_INT a3, a4;
8133 {
8134 struct window *w = (struct window *) a1;
8135 Lisp_Object window;
8136 struct text_pos start;
8137 int window_height_changed_p = 0;
8138
8139 /* Do this before displaying, so that we have a large enough glyph
8140 matrix for the display. If we can't get enough space for the
8141 whole text, display the last N lines. That works by setting w->start. */
8142 window_height_changed_p = resize_mini_window (w, 0);
8143
8144 /* Use the starting position chosen by resize_mini_window. */
8145 SET_TEXT_POS_FROM_MARKER (start, w->start);
8146
8147 /* Display. */
8148 clear_glyph_matrix (w->desired_matrix);
8149 XSETWINDOW (window, w);
8150 try_window (window, start, 0);
8151
8152 return window_height_changed_p;
8153 }
8154
8155
8156 /* Resize the echo area window to exactly the size needed for the
8157 currently displayed message, if there is one. If a mini-buffer
8158 is active, don't shrink it. */
8159
8160 void
8161 resize_echo_area_exactly ()
8162 {
8163 if (BUFFERP (echo_area_buffer[0])
8164 && WINDOWP (echo_area_window))
8165 {
8166 struct window *w = XWINDOW (echo_area_window);
8167 int resized_p;
8168 Lisp_Object resize_exactly;
8169
8170 if (minibuf_level == 0)
8171 resize_exactly = Qt;
8172 else
8173 resize_exactly = Qnil;
8174
8175 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8176 (EMACS_INT) w, resize_exactly, 0, 0);
8177 if (resized_p)
8178 {
8179 ++windows_or_buffers_changed;
8180 ++update_mode_lines;
8181 redisplay_internal (0);
8182 }
8183 }
8184 }
8185
8186
8187 /* Callback function for with_echo_area_buffer, when used from
8188 resize_echo_area_exactly. A1 contains a pointer to the window to
8189 resize, EXACTLY non-nil means resize the mini-window exactly to the
8190 size of the text displayed. A3 and A4 are not used. Value is what
8191 resize_mini_window returns. */
8192
8193 static int
8194 resize_mini_window_1 (a1, exactly, a3, a4)
8195 EMACS_INT a1;
8196 Lisp_Object exactly;
8197 EMACS_INT a3, a4;
8198 {
8199 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8200 }
8201
8202
8203 /* Resize mini-window W to fit the size of its contents. EXACT:P
8204 means size the window exactly to the size needed. Otherwise, it's
8205 only enlarged until W's buffer is empty.
8206
8207 Set W->start to the right place to begin display. If the whole
8208 contents fit, start at the beginning. Otherwise, start so as
8209 to make the end of the contents appear. This is particularly
8210 important for y-or-n-p, but seems desirable generally.
8211
8212 Value is non-zero if the window height has been changed. */
8213
8214 int
8215 resize_mini_window (w, exact_p)
8216 struct window *w;
8217 int exact_p;
8218 {
8219 struct frame *f = XFRAME (w->frame);
8220 int window_height_changed_p = 0;
8221
8222 xassert (MINI_WINDOW_P (w));
8223
8224 /* By default, start display at the beginning. */
8225 set_marker_both (w->start, w->buffer,
8226 BUF_BEGV (XBUFFER (w->buffer)),
8227 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8228
8229 /* Don't resize windows while redisplaying a window; it would
8230 confuse redisplay functions when the size of the window they are
8231 displaying changes from under them. Such a resizing can happen,
8232 for instance, when which-func prints a long message while
8233 we are running fontification-functions. We're running these
8234 functions with safe_call which binds inhibit-redisplay to t. */
8235 if (!NILP (Vinhibit_redisplay))
8236 return 0;
8237
8238 /* Nil means don't try to resize. */
8239 if (NILP (Vresize_mini_windows)
8240 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8241 return 0;
8242
8243 if (!FRAME_MINIBUF_ONLY_P (f))
8244 {
8245 struct it it;
8246 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8247 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8248 int height, max_height;
8249 int unit = FRAME_LINE_HEIGHT (f);
8250 struct text_pos start;
8251 struct buffer *old_current_buffer = NULL;
8252
8253 if (current_buffer != XBUFFER (w->buffer))
8254 {
8255 old_current_buffer = current_buffer;
8256 set_buffer_internal (XBUFFER (w->buffer));
8257 }
8258
8259 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8260
8261 /* Compute the max. number of lines specified by the user. */
8262 if (FLOATP (Vmax_mini_window_height))
8263 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8264 else if (INTEGERP (Vmax_mini_window_height))
8265 max_height = XINT (Vmax_mini_window_height);
8266 else
8267 max_height = total_height / 4;
8268
8269 /* Correct that max. height if it's bogus. */
8270 max_height = max (1, max_height);
8271 max_height = min (total_height, max_height);
8272
8273 /* Find out the height of the text in the window. */
8274 if (it.truncate_lines_p)
8275 height = 1;
8276 else
8277 {
8278 last_height = 0;
8279 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8280 if (it.max_ascent == 0 && it.max_descent == 0)
8281 height = it.current_y + last_height;
8282 else
8283 height = it.current_y + it.max_ascent + it.max_descent;
8284 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8285 height = (height + unit - 1) / unit;
8286 }
8287
8288 /* Compute a suitable window start. */
8289 if (height > max_height)
8290 {
8291 height = max_height;
8292 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8293 move_it_vertically_backward (&it, (height - 1) * unit);
8294 start = it.current.pos;
8295 }
8296 else
8297 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8298 SET_MARKER_FROM_TEXT_POS (w->start, start);
8299
8300 if (EQ (Vresize_mini_windows, Qgrow_only))
8301 {
8302 /* Let it grow only, until we display an empty message, in which
8303 case the window shrinks again. */
8304 if (height > WINDOW_TOTAL_LINES (w))
8305 {
8306 int old_height = WINDOW_TOTAL_LINES (w);
8307 freeze_window_starts (f, 1);
8308 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8309 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8310 }
8311 else if (height < WINDOW_TOTAL_LINES (w)
8312 && (exact_p || BEGV == ZV))
8313 {
8314 int old_height = WINDOW_TOTAL_LINES (w);
8315 freeze_window_starts (f, 0);
8316 shrink_mini_window (w);
8317 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8318 }
8319 }
8320 else
8321 {
8322 /* Always resize to exact size needed. */
8323 if (height > WINDOW_TOTAL_LINES (w))
8324 {
8325 int old_height = WINDOW_TOTAL_LINES (w);
8326 freeze_window_starts (f, 1);
8327 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8328 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8329 }
8330 else if (height < WINDOW_TOTAL_LINES (w))
8331 {
8332 int old_height = WINDOW_TOTAL_LINES (w);
8333 freeze_window_starts (f, 0);
8334 shrink_mini_window (w);
8335
8336 if (height)
8337 {
8338 freeze_window_starts (f, 1);
8339 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8340 }
8341
8342 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8343 }
8344 }
8345
8346 if (old_current_buffer)
8347 set_buffer_internal (old_current_buffer);
8348 }
8349
8350 return window_height_changed_p;
8351 }
8352
8353
8354 /* Value is the current message, a string, or nil if there is no
8355 current message. */
8356
8357 Lisp_Object
8358 current_message ()
8359 {
8360 Lisp_Object msg;
8361
8362 if (NILP (echo_area_buffer[0]))
8363 msg = Qnil;
8364 else
8365 {
8366 with_echo_area_buffer (0, 0, current_message_1,
8367 (EMACS_INT) &msg, Qnil, 0, 0);
8368 if (NILP (msg))
8369 echo_area_buffer[0] = Qnil;
8370 }
8371
8372 return msg;
8373 }
8374
8375
8376 static int
8377 current_message_1 (a1, a2, a3, a4)
8378 EMACS_INT a1;
8379 Lisp_Object a2;
8380 EMACS_INT a3, a4;
8381 {
8382 Lisp_Object *msg = (Lisp_Object *) a1;
8383
8384 if (Z > BEG)
8385 *msg = make_buffer_string (BEG, Z, 1);
8386 else
8387 *msg = Qnil;
8388 return 0;
8389 }
8390
8391
8392 /* Push the current message on Vmessage_stack for later restauration
8393 by restore_message. Value is non-zero if the current message isn't
8394 empty. This is a relatively infrequent operation, so it's not
8395 worth optimizing. */
8396
8397 int
8398 push_message ()
8399 {
8400 Lisp_Object msg;
8401 msg = current_message ();
8402 Vmessage_stack = Fcons (msg, Vmessage_stack);
8403 return STRINGP (msg);
8404 }
8405
8406
8407 /* Restore message display from the top of Vmessage_stack. */
8408
8409 void
8410 restore_message ()
8411 {
8412 Lisp_Object msg;
8413
8414 xassert (CONSP (Vmessage_stack));
8415 msg = XCAR (Vmessage_stack);
8416 if (STRINGP (msg))
8417 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8418 else
8419 message3_nolog (msg, 0, 0);
8420 }
8421
8422
8423 /* Handler for record_unwind_protect calling pop_message. */
8424
8425 Lisp_Object
8426 pop_message_unwind (dummy)
8427 Lisp_Object dummy;
8428 {
8429 pop_message ();
8430 return Qnil;
8431 }
8432
8433 /* Pop the top-most entry off Vmessage_stack. */
8434
8435 void
8436 pop_message ()
8437 {
8438 xassert (CONSP (Vmessage_stack));
8439 Vmessage_stack = XCDR (Vmessage_stack);
8440 }
8441
8442
8443 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8444 exits. If the stack is not empty, we have a missing pop_message
8445 somewhere. */
8446
8447 void
8448 check_message_stack ()
8449 {
8450 if (!NILP (Vmessage_stack))
8451 abort ();
8452 }
8453
8454
8455 /* Truncate to NCHARS what will be displayed in the echo area the next
8456 time we display it---but don't redisplay it now. */
8457
8458 void
8459 truncate_echo_area (nchars)
8460 int nchars;
8461 {
8462 if (nchars == 0)
8463 echo_area_buffer[0] = Qnil;
8464 /* A null message buffer means that the frame hasn't really been
8465 initialized yet. Error messages get reported properly by
8466 cmd_error, so this must be just an informative message; toss it. */
8467 else if (!noninteractive
8468 && INTERACTIVE
8469 && !NILP (echo_area_buffer[0]))
8470 {
8471 struct frame *sf = SELECTED_FRAME ();
8472 if (FRAME_MESSAGE_BUF (sf))
8473 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8474 }
8475 }
8476
8477
8478 /* Helper function for truncate_echo_area. Truncate the current
8479 message to at most NCHARS characters. */
8480
8481 static int
8482 truncate_message_1 (nchars, a2, a3, a4)
8483 EMACS_INT nchars;
8484 Lisp_Object a2;
8485 EMACS_INT a3, a4;
8486 {
8487 if (BEG + nchars < Z)
8488 del_range (BEG + nchars, Z);
8489 if (Z == BEG)
8490 echo_area_buffer[0] = Qnil;
8491 return 0;
8492 }
8493
8494
8495 /* Set the current message to a substring of S or STRING.
8496
8497 If STRING is a Lisp string, set the message to the first NBYTES
8498 bytes from STRING. NBYTES zero means use the whole string. If
8499 STRING is multibyte, the message will be displayed multibyte.
8500
8501 If S is not null, set the message to the first LEN bytes of S. LEN
8502 zero means use the whole string. MULTIBYTE_P non-zero means S is
8503 multibyte. Display the message multibyte in that case.
8504
8505 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8506 to t before calling set_message_1 (which calls insert).
8507 */
8508
8509 void
8510 set_message (s, string, nbytes, multibyte_p)
8511 const char *s;
8512 Lisp_Object string;
8513 int nbytes, multibyte_p;
8514 {
8515 message_enable_multibyte
8516 = ((s && multibyte_p)
8517 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8518
8519 with_echo_area_buffer (0, 0, set_message_1,
8520 (EMACS_INT) s, string, nbytes, multibyte_p);
8521 message_buf_print = 0;
8522 help_echo_showing_p = 0;
8523 }
8524
8525
8526 /* Helper function for set_message. Arguments have the same meaning
8527 as there, with A1 corresponding to S and A2 corresponding to STRING
8528 This function is called with the echo area buffer being
8529 current. */
8530
8531 static int
8532 set_message_1 (a1, a2, nbytes, multibyte_p)
8533 EMACS_INT a1;
8534 Lisp_Object a2;
8535 EMACS_INT nbytes, multibyte_p;
8536 {
8537 const char *s = (const char *) a1;
8538 Lisp_Object string = a2;
8539
8540 /* Change multibyteness of the echo buffer appropriately. */
8541 if (message_enable_multibyte
8542 != !NILP (current_buffer->enable_multibyte_characters))
8543 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8544
8545 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8546
8547 /* Insert new message at BEG. */
8548 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8549 Ferase_buffer ();
8550
8551 if (STRINGP (string))
8552 {
8553 int nchars;
8554
8555 if (nbytes == 0)
8556 nbytes = SBYTES (string);
8557 nchars = string_byte_to_char (string, nbytes);
8558
8559 /* This function takes care of single/multibyte conversion. We
8560 just have to ensure that the echo area buffer has the right
8561 setting of enable_multibyte_characters. */
8562 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8563 }
8564 else if (s)
8565 {
8566 if (nbytes == 0)
8567 nbytes = strlen (s);
8568
8569 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8570 {
8571 /* Convert from multi-byte to single-byte. */
8572 int i, c, n;
8573 unsigned char work[1];
8574
8575 /* Convert a multibyte string to single-byte. */
8576 for (i = 0; i < nbytes; i += n)
8577 {
8578 c = string_char_and_length (s + i, nbytes - i, &n);
8579 work[0] = (SINGLE_BYTE_CHAR_P (c)
8580 ? c
8581 : multibyte_char_to_unibyte (c, Qnil));
8582 insert_1_both (work, 1, 1, 1, 0, 0);
8583 }
8584 }
8585 else if (!multibyte_p
8586 && !NILP (current_buffer->enable_multibyte_characters))
8587 {
8588 /* Convert from single-byte to multi-byte. */
8589 int i, c, n;
8590 const unsigned char *msg = (const unsigned char *) s;
8591 unsigned char str[MAX_MULTIBYTE_LENGTH];
8592
8593 /* Convert a single-byte string to multibyte. */
8594 for (i = 0; i < nbytes; i++)
8595 {
8596 c = unibyte_char_to_multibyte (msg[i]);
8597 n = CHAR_STRING (c, str);
8598 insert_1_both (str, 1, n, 1, 0, 0);
8599 }
8600 }
8601 else
8602 insert_1 (s, nbytes, 1, 0, 0);
8603 }
8604
8605 return 0;
8606 }
8607
8608
8609 /* Clear messages. CURRENT_P non-zero means clear the current
8610 message. LAST_DISPLAYED_P non-zero means clear the message
8611 last displayed. */
8612
8613 void
8614 clear_message (current_p, last_displayed_p)
8615 int current_p, last_displayed_p;
8616 {
8617 if (current_p)
8618 {
8619 echo_area_buffer[0] = Qnil;
8620 message_cleared_p = 1;
8621 }
8622
8623 if (last_displayed_p)
8624 echo_area_buffer[1] = Qnil;
8625
8626 message_buf_print = 0;
8627 }
8628
8629 /* Clear garbaged frames.
8630
8631 This function is used where the old redisplay called
8632 redraw_garbaged_frames which in turn called redraw_frame which in
8633 turn called clear_frame. The call to clear_frame was a source of
8634 flickering. I believe a clear_frame is not necessary. It should
8635 suffice in the new redisplay to invalidate all current matrices,
8636 and ensure a complete redisplay of all windows. */
8637
8638 static void
8639 clear_garbaged_frames ()
8640 {
8641 if (frame_garbaged)
8642 {
8643 Lisp_Object tail, frame;
8644 int changed_count = 0;
8645
8646 FOR_EACH_FRAME (tail, frame)
8647 {
8648 struct frame *f = XFRAME (frame);
8649
8650 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8651 {
8652 if (f->resized_p)
8653 {
8654 Fredraw_frame (frame);
8655 f->force_flush_display_p = 1;
8656 }
8657 clear_current_matrices (f);
8658 changed_count++;
8659 f->garbaged = 0;
8660 f->resized_p = 0;
8661 }
8662 }
8663
8664 frame_garbaged = 0;
8665 if (changed_count)
8666 ++windows_or_buffers_changed;
8667 }
8668 }
8669
8670
8671 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8672 is non-zero update selected_frame. Value is non-zero if the
8673 mini-windows height has been changed. */
8674
8675 static int
8676 echo_area_display (update_frame_p)
8677 int update_frame_p;
8678 {
8679 Lisp_Object mini_window;
8680 struct window *w;
8681 struct frame *f;
8682 int window_height_changed_p = 0;
8683 struct frame *sf = SELECTED_FRAME ();
8684
8685 mini_window = FRAME_MINIBUF_WINDOW (sf);
8686 w = XWINDOW (mini_window);
8687 f = XFRAME (WINDOW_FRAME (w));
8688
8689 /* Don't display if frame is invisible or not yet initialized. */
8690 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8691 return 0;
8692
8693 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8694 #ifndef MAC_OS8
8695 #ifdef HAVE_WINDOW_SYSTEM
8696 /* When Emacs starts, selected_frame may be a visible terminal
8697 frame, even if we run under a window system. If we let this
8698 through, a message would be displayed on the terminal. */
8699 if (EQ (selected_frame, Vterminal_frame)
8700 && !NILP (Vwindow_system))
8701 return 0;
8702 #endif /* HAVE_WINDOW_SYSTEM */
8703 #endif
8704
8705 /* Redraw garbaged frames. */
8706 if (frame_garbaged)
8707 clear_garbaged_frames ();
8708
8709 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8710 {
8711 echo_area_window = mini_window;
8712 window_height_changed_p = display_echo_area (w);
8713 w->must_be_updated_p = 1;
8714
8715 /* Update the display, unless called from redisplay_internal.
8716 Also don't update the screen during redisplay itself. The
8717 update will happen at the end of redisplay, and an update
8718 here could cause confusion. */
8719 if (update_frame_p && !redisplaying_p)
8720 {
8721 int n = 0;
8722
8723 /* If the display update has been interrupted by pending
8724 input, update mode lines in the frame. Due to the
8725 pending input, it might have been that redisplay hasn't
8726 been called, so that mode lines above the echo area are
8727 garbaged. This looks odd, so we prevent it here. */
8728 if (!display_completed)
8729 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8730
8731 if (window_height_changed_p
8732 /* Don't do this if Emacs is shutting down. Redisplay
8733 needs to run hooks. */
8734 && !NILP (Vrun_hooks))
8735 {
8736 /* Must update other windows. Likewise as in other
8737 cases, don't let this update be interrupted by
8738 pending input. */
8739 int count = SPECPDL_INDEX ();
8740 specbind (Qredisplay_dont_pause, Qt);
8741 windows_or_buffers_changed = 1;
8742 redisplay_internal (0);
8743 unbind_to (count, Qnil);
8744 }
8745 else if (FRAME_WINDOW_P (f) && n == 0)
8746 {
8747 /* Window configuration is the same as before.
8748 Can do with a display update of the echo area,
8749 unless we displayed some mode lines. */
8750 update_single_window (w, 1);
8751 rif->flush_display (f);
8752 }
8753 else
8754 update_frame (f, 1, 1);
8755
8756 /* If cursor is in the echo area, make sure that the next
8757 redisplay displays the minibuffer, so that the cursor will
8758 be replaced with what the minibuffer wants. */
8759 if (cursor_in_echo_area)
8760 ++windows_or_buffers_changed;
8761 }
8762 }
8763 else if (!EQ (mini_window, selected_window))
8764 windows_or_buffers_changed++;
8765
8766 /* The current message is now also the last one displayed. */
8767 echo_area_buffer[1] = echo_area_buffer[0];
8768
8769 /* Prevent redisplay optimization in redisplay_internal by resetting
8770 this_line_start_pos. This is done because the mini-buffer now
8771 displays the message instead of its buffer text. */
8772 if (EQ (mini_window, selected_window))
8773 CHARPOS (this_line_start_pos) = 0;
8774
8775 return window_height_changed_p;
8776 }
8777
8778
8779 \f
8780 /***********************************************************************
8781 Mode Lines and Frame Titles
8782 ***********************************************************************/
8783
8784 /* A buffer for constructing non-propertized mode-line strings and
8785 frame titles in it; allocated from the heap in init_xdisp and
8786 resized as needed in store_mode_line_noprop_char. */
8787
8788 static char *mode_line_noprop_buf;
8789
8790 /* The buffer's end, and a current output position in it. */
8791
8792 static char *mode_line_noprop_buf_end;
8793 static char *mode_line_noprop_ptr;
8794
8795 #define MODE_LINE_NOPROP_LEN(start) \
8796 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8797
8798 static enum {
8799 MODE_LINE_DISPLAY = 0,
8800 MODE_LINE_TITLE,
8801 MODE_LINE_NOPROP,
8802 MODE_LINE_STRING
8803 } mode_line_target;
8804
8805 /* Alist that caches the results of :propertize.
8806 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8807 static Lisp_Object mode_line_proptrans_alist;
8808
8809 /* List of strings making up the mode-line. */
8810 static Lisp_Object mode_line_string_list;
8811
8812 /* Base face property when building propertized mode line string. */
8813 static Lisp_Object mode_line_string_face;
8814 static Lisp_Object mode_line_string_face_prop;
8815
8816
8817 /* Unwind data for mode line strings */
8818
8819 static Lisp_Object Vmode_line_unwind_vector;
8820
8821 static Lisp_Object
8822 format_mode_line_unwind_data (obuf, save_proptrans)
8823 struct buffer *obuf;
8824 {
8825 Lisp_Object vector;
8826
8827 /* Reduce consing by keeping one vector in
8828 Vwith_echo_area_save_vector. */
8829 vector = Vmode_line_unwind_vector;
8830 Vmode_line_unwind_vector = Qnil;
8831
8832 if (NILP (vector))
8833 vector = Fmake_vector (make_number (7), Qnil);
8834
8835 AREF (vector, 0) = make_number (mode_line_target);
8836 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8837 AREF (vector, 2) = mode_line_string_list;
8838 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8839 AREF (vector, 4) = mode_line_string_face;
8840 AREF (vector, 5) = mode_line_string_face_prop;
8841
8842 if (obuf)
8843 XSETBUFFER (AREF (vector, 6), obuf);
8844 else
8845 AREF (vector, 6) = Qnil;
8846
8847 return vector;
8848 }
8849
8850 static Lisp_Object
8851 unwind_format_mode_line (vector)
8852 Lisp_Object vector;
8853 {
8854 mode_line_target = XINT (AREF (vector, 0));
8855 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8856 mode_line_string_list = AREF (vector, 2);
8857 if (! EQ (AREF (vector, 3), Qt))
8858 mode_line_proptrans_alist = AREF (vector, 3);
8859 mode_line_string_face = AREF (vector, 4);
8860 mode_line_string_face_prop = AREF (vector, 5);
8861
8862 if (!NILP (AREF (vector, 6)))
8863 {
8864 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8865 AREF (vector, 6) = Qnil;
8866 }
8867
8868 Vmode_line_unwind_vector = vector;
8869 return Qnil;
8870 }
8871
8872
8873 /* Store a single character C for the frame title in mode_line_noprop_buf.
8874 Re-allocate mode_line_noprop_buf if necessary. */
8875
8876 static void
8877 #ifdef PROTOTYPES
8878 store_mode_line_noprop_char (char c)
8879 #else
8880 store_mode_line_noprop_char (c)
8881 char c;
8882 #endif
8883 {
8884 /* If output position has reached the end of the allocated buffer,
8885 double the buffer's size. */
8886 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8887 {
8888 int len = MODE_LINE_NOPROP_LEN (0);
8889 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8890 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8891 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8892 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8893 }
8894
8895 *mode_line_noprop_ptr++ = c;
8896 }
8897
8898
8899 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8900 mode_line_noprop_ptr. STR is the string to store. Do not copy
8901 characters that yield more columns than PRECISION; PRECISION <= 0
8902 means copy the whole string. Pad with spaces until FIELD_WIDTH
8903 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8904 pad. Called from display_mode_element when it is used to build a
8905 frame title. */
8906
8907 static int
8908 store_mode_line_noprop (str, field_width, precision)
8909 const unsigned char *str;
8910 int field_width, precision;
8911 {
8912 int n = 0;
8913 int dummy, nbytes;
8914
8915 /* Copy at most PRECISION chars from STR. */
8916 nbytes = strlen (str);
8917 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8918 while (nbytes--)
8919 store_mode_line_noprop_char (*str++);
8920
8921 /* Fill up with spaces until FIELD_WIDTH reached. */
8922 while (field_width > 0
8923 && n < field_width)
8924 {
8925 store_mode_line_noprop_char (' ');
8926 ++n;
8927 }
8928
8929 return n;
8930 }
8931
8932 /***********************************************************************
8933 Frame Titles
8934 ***********************************************************************/
8935
8936 #ifdef HAVE_WINDOW_SYSTEM
8937
8938 /* Set the title of FRAME, if it has changed. The title format is
8939 Vicon_title_format if FRAME is iconified, otherwise it is
8940 frame_title_format. */
8941
8942 static void
8943 x_consider_frame_title (frame)
8944 Lisp_Object frame;
8945 {
8946 struct frame *f = XFRAME (frame);
8947
8948 if (FRAME_WINDOW_P (f)
8949 || FRAME_MINIBUF_ONLY_P (f)
8950 || f->explicit_name)
8951 {
8952 /* Do we have more than one visible frame on this X display? */
8953 Lisp_Object tail;
8954 Lisp_Object fmt;
8955 int title_start;
8956 char *title;
8957 int len;
8958 struct it it;
8959 int count = SPECPDL_INDEX ();
8960
8961 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8962 {
8963 Lisp_Object other_frame = XCAR (tail);
8964 struct frame *tf = XFRAME (other_frame);
8965
8966 if (tf != f
8967 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8968 && !FRAME_MINIBUF_ONLY_P (tf)
8969 && !EQ (other_frame, tip_frame)
8970 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8971 break;
8972 }
8973
8974 /* Set global variable indicating that multiple frames exist. */
8975 multiple_frames = CONSP (tail);
8976
8977 /* Switch to the buffer of selected window of the frame. Set up
8978 mode_line_target so that display_mode_element will output into
8979 mode_line_noprop_buf; then display the title. */
8980 record_unwind_protect (unwind_format_mode_line,
8981 format_mode_line_unwind_data (current_buffer, 0));
8982
8983 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8984 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8985
8986 mode_line_target = MODE_LINE_TITLE;
8987 title_start = MODE_LINE_NOPROP_LEN (0);
8988 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8989 NULL, DEFAULT_FACE_ID);
8990 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8991 len = MODE_LINE_NOPROP_LEN (title_start);
8992 title = mode_line_noprop_buf + title_start;
8993 unbind_to (count, Qnil);
8994
8995 /* Set the title only if it's changed. This avoids consing in
8996 the common case where it hasn't. (If it turns out that we've
8997 already wasted too much time by walking through the list with
8998 display_mode_element, then we might need to optimize at a
8999 higher level than this.) */
9000 if (! STRINGP (f->name)
9001 || SBYTES (f->name) != len
9002 || bcmp (title, SDATA (f->name), len) != 0)
9003 x_implicitly_set_name (f, make_string (title, len), Qnil);
9004 }
9005 }
9006
9007 #endif /* not HAVE_WINDOW_SYSTEM */
9008
9009
9010
9011 \f
9012 /***********************************************************************
9013 Menu Bars
9014 ***********************************************************************/
9015
9016
9017 /* Prepare for redisplay by updating menu-bar item lists when
9018 appropriate. This can call eval. */
9019
9020 void
9021 prepare_menu_bars ()
9022 {
9023 int all_windows;
9024 struct gcpro gcpro1, gcpro2;
9025 struct frame *f;
9026 Lisp_Object tooltip_frame;
9027
9028 #ifdef HAVE_WINDOW_SYSTEM
9029 tooltip_frame = tip_frame;
9030 #else
9031 tooltip_frame = Qnil;
9032 #endif
9033
9034 /* Update all frame titles based on their buffer names, etc. We do
9035 this before the menu bars so that the buffer-menu will show the
9036 up-to-date frame titles. */
9037 #ifdef HAVE_WINDOW_SYSTEM
9038 if (windows_or_buffers_changed || update_mode_lines)
9039 {
9040 Lisp_Object tail, frame;
9041
9042 FOR_EACH_FRAME (tail, frame)
9043 {
9044 f = XFRAME (frame);
9045 if (!EQ (frame, tooltip_frame)
9046 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9047 x_consider_frame_title (frame);
9048 }
9049 }
9050 #endif /* HAVE_WINDOW_SYSTEM */
9051
9052 /* Update the menu bar item lists, if appropriate. This has to be
9053 done before any actual redisplay or generation of display lines. */
9054 all_windows = (update_mode_lines
9055 || buffer_shared > 1
9056 || windows_or_buffers_changed);
9057 if (all_windows)
9058 {
9059 Lisp_Object tail, frame;
9060 int count = SPECPDL_INDEX ();
9061 /* 1 means that update_menu_bar has run its hooks
9062 so any further calls to update_menu_bar shouldn't do so again. */
9063 int menu_bar_hooks_run = 0;
9064
9065 record_unwind_save_match_data ();
9066
9067 FOR_EACH_FRAME (tail, frame)
9068 {
9069 f = XFRAME (frame);
9070
9071 /* Ignore tooltip frame. */
9072 if (EQ (frame, tooltip_frame))
9073 continue;
9074
9075 /* If a window on this frame changed size, report that to
9076 the user and clear the size-change flag. */
9077 if (FRAME_WINDOW_SIZES_CHANGED (f))
9078 {
9079 Lisp_Object functions;
9080
9081 /* Clear flag first in case we get an error below. */
9082 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9083 functions = Vwindow_size_change_functions;
9084 GCPRO2 (tail, functions);
9085
9086 while (CONSP (functions))
9087 {
9088 call1 (XCAR (functions), frame);
9089 functions = XCDR (functions);
9090 }
9091 UNGCPRO;
9092 }
9093
9094 GCPRO1 (tail);
9095 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9096 #ifdef HAVE_WINDOW_SYSTEM
9097 update_tool_bar (f, 0);
9098 #ifdef MAC_OS
9099 mac_update_title_bar (f, 0);
9100 #endif
9101 #endif
9102 UNGCPRO;
9103 }
9104
9105 unbind_to (count, Qnil);
9106 }
9107 else
9108 {
9109 struct frame *sf = SELECTED_FRAME ();
9110 update_menu_bar (sf, 1, 0);
9111 #ifdef HAVE_WINDOW_SYSTEM
9112 update_tool_bar (sf, 1);
9113 #ifdef MAC_OS
9114 mac_update_title_bar (sf, 1);
9115 #endif
9116 #endif
9117 }
9118
9119 /* Motif needs this. See comment in xmenu.c. Turn it off when
9120 pending_menu_activation is not defined. */
9121 #ifdef USE_X_TOOLKIT
9122 pending_menu_activation = 0;
9123 #endif
9124 }
9125
9126
9127 /* Update the menu bar item list for frame F. This has to be done
9128 before we start to fill in any display lines, because it can call
9129 eval.
9130
9131 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9132
9133 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9134 already ran the menu bar hooks for this redisplay, so there
9135 is no need to run them again. The return value is the
9136 updated value of this flag, to pass to the next call. */
9137
9138 static int
9139 update_menu_bar (f, save_match_data, hooks_run)
9140 struct frame *f;
9141 int save_match_data;
9142 int hooks_run;
9143 {
9144 Lisp_Object window;
9145 register struct window *w;
9146
9147 /* If called recursively during a menu update, do nothing. This can
9148 happen when, for instance, an activate-menubar-hook causes a
9149 redisplay. */
9150 if (inhibit_menubar_update)
9151 return hooks_run;
9152
9153 window = FRAME_SELECTED_WINDOW (f);
9154 w = XWINDOW (window);
9155
9156 #if 0 /* The if statement below this if statement used to include the
9157 condition !NILP (w->update_mode_line), rather than using
9158 update_mode_lines directly, and this if statement may have
9159 been added to make that condition work. Now the if
9160 statement below matches its comment, this isn't needed. */
9161 if (update_mode_lines)
9162 w->update_mode_line = Qt;
9163 #endif
9164
9165 if (FRAME_WINDOW_P (f)
9166 ?
9167 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9168 || defined (USE_GTK)
9169 FRAME_EXTERNAL_MENU_BAR (f)
9170 #else
9171 FRAME_MENU_BAR_LINES (f) > 0
9172 #endif
9173 : FRAME_MENU_BAR_LINES (f) > 0)
9174 {
9175 /* If the user has switched buffers or windows, we need to
9176 recompute to reflect the new bindings. But we'll
9177 recompute when update_mode_lines is set too; that means
9178 that people can use force-mode-line-update to request
9179 that the menu bar be recomputed. The adverse effect on
9180 the rest of the redisplay algorithm is about the same as
9181 windows_or_buffers_changed anyway. */
9182 if (windows_or_buffers_changed
9183 /* This used to test w->update_mode_line, but we believe
9184 there is no need to recompute the menu in that case. */
9185 || update_mode_lines
9186 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9187 < BUF_MODIFF (XBUFFER (w->buffer)))
9188 != !NILP (w->last_had_star))
9189 || ((!NILP (Vtransient_mark_mode)
9190 && !NILP (XBUFFER (w->buffer)->mark_active))
9191 != !NILP (w->region_showing)))
9192 {
9193 struct buffer *prev = current_buffer;
9194 int count = SPECPDL_INDEX ();
9195
9196 specbind (Qinhibit_menubar_update, Qt);
9197
9198 set_buffer_internal_1 (XBUFFER (w->buffer));
9199 if (save_match_data)
9200 record_unwind_save_match_data ();
9201 if (NILP (Voverriding_local_map_menu_flag))
9202 {
9203 specbind (Qoverriding_terminal_local_map, Qnil);
9204 specbind (Qoverriding_local_map, Qnil);
9205 }
9206
9207 if (!hooks_run)
9208 {
9209 /* Run the Lucid hook. */
9210 safe_run_hooks (Qactivate_menubar_hook);
9211
9212 /* If it has changed current-menubar from previous value,
9213 really recompute the menu-bar from the value. */
9214 if (! NILP (Vlucid_menu_bar_dirty_flag))
9215 call0 (Qrecompute_lucid_menubar);
9216
9217 safe_run_hooks (Qmenu_bar_update_hook);
9218
9219 hooks_run = 1;
9220 }
9221
9222 XSETFRAME (Vmenu_updating_frame, f);
9223 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9224
9225 /* Redisplay the menu bar in case we changed it. */
9226 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9227 || defined (USE_GTK)
9228 if (FRAME_WINDOW_P (f))
9229 {
9230 #ifdef MAC_OS
9231 /* All frames on Mac OS share the same menubar. So only
9232 the selected frame should be allowed to set it. */
9233 if (f == SELECTED_FRAME ())
9234 #endif
9235 set_frame_menubar (f, 0, 0);
9236 }
9237 else
9238 /* On a terminal screen, the menu bar is an ordinary screen
9239 line, and this makes it get updated. */
9240 w->update_mode_line = Qt;
9241 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9242 /* In the non-toolkit version, the menu bar is an ordinary screen
9243 line, and this makes it get updated. */
9244 w->update_mode_line = Qt;
9245 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9246
9247 unbind_to (count, Qnil);
9248 set_buffer_internal_1 (prev);
9249 }
9250 }
9251
9252 return hooks_run;
9253 }
9254
9255
9256 \f
9257 /***********************************************************************
9258 Output Cursor
9259 ***********************************************************************/
9260
9261 #ifdef HAVE_WINDOW_SYSTEM
9262
9263 /* EXPORT:
9264 Nominal cursor position -- where to draw output.
9265 HPOS and VPOS are window relative glyph matrix coordinates.
9266 X and Y are window relative pixel coordinates. */
9267
9268 struct cursor_pos output_cursor;
9269
9270
9271 /* EXPORT:
9272 Set the global variable output_cursor to CURSOR. All cursor
9273 positions are relative to updated_window. */
9274
9275 void
9276 set_output_cursor (cursor)
9277 struct cursor_pos *cursor;
9278 {
9279 output_cursor.hpos = cursor->hpos;
9280 output_cursor.vpos = cursor->vpos;
9281 output_cursor.x = cursor->x;
9282 output_cursor.y = cursor->y;
9283 }
9284
9285
9286 /* EXPORT for RIF:
9287 Set a nominal cursor position.
9288
9289 HPOS and VPOS are column/row positions in a window glyph matrix. X
9290 and Y are window text area relative pixel positions.
9291
9292 If this is done during an update, updated_window will contain the
9293 window that is being updated and the position is the future output
9294 cursor position for that window. If updated_window is null, use
9295 selected_window and display the cursor at the given position. */
9296
9297 void
9298 x_cursor_to (vpos, hpos, y, x)
9299 int vpos, hpos, y, x;
9300 {
9301 struct window *w;
9302
9303 /* If updated_window is not set, work on selected_window. */
9304 if (updated_window)
9305 w = updated_window;
9306 else
9307 w = XWINDOW (selected_window);
9308
9309 /* Set the output cursor. */
9310 output_cursor.hpos = hpos;
9311 output_cursor.vpos = vpos;
9312 output_cursor.x = x;
9313 output_cursor.y = y;
9314
9315 /* If not called as part of an update, really display the cursor.
9316 This will also set the cursor position of W. */
9317 if (updated_window == NULL)
9318 {
9319 BLOCK_INPUT;
9320 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9321 if (rif->flush_display_optional)
9322 rif->flush_display_optional (SELECTED_FRAME ());
9323 UNBLOCK_INPUT;
9324 }
9325 }
9326
9327 #endif /* HAVE_WINDOW_SYSTEM */
9328
9329 \f
9330 /***********************************************************************
9331 Tool-bars
9332 ***********************************************************************/
9333
9334 #ifdef HAVE_WINDOW_SYSTEM
9335
9336 /* Where the mouse was last time we reported a mouse event. */
9337
9338 FRAME_PTR last_mouse_frame;
9339
9340 /* Tool-bar item index of the item on which a mouse button was pressed
9341 or -1. */
9342
9343 int last_tool_bar_item;
9344
9345
9346 /* Update the tool-bar item list for frame F. This has to be done
9347 before we start to fill in any display lines. Called from
9348 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9349 and restore it here. */
9350
9351 static void
9352 update_tool_bar (f, save_match_data)
9353 struct frame *f;
9354 int save_match_data;
9355 {
9356 #ifdef USE_GTK
9357 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9358 #else
9359 int do_update = WINDOWP (f->tool_bar_window)
9360 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9361 #endif
9362
9363 if (do_update)
9364 {
9365 Lisp_Object window;
9366 struct window *w;
9367
9368 window = FRAME_SELECTED_WINDOW (f);
9369 w = XWINDOW (window);
9370
9371 /* If the user has switched buffers or windows, we need to
9372 recompute to reflect the new bindings. But we'll
9373 recompute when update_mode_lines is set too; that means
9374 that people can use force-mode-line-update to request
9375 that the menu bar be recomputed. The adverse effect on
9376 the rest of the redisplay algorithm is about the same as
9377 windows_or_buffers_changed anyway. */
9378 if (windows_or_buffers_changed
9379 || !NILP (w->update_mode_line)
9380 || update_mode_lines
9381 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9382 < BUF_MODIFF (XBUFFER (w->buffer)))
9383 != !NILP (w->last_had_star))
9384 || ((!NILP (Vtransient_mark_mode)
9385 && !NILP (XBUFFER (w->buffer)->mark_active))
9386 != !NILP (w->region_showing)))
9387 {
9388 struct buffer *prev = current_buffer;
9389 int count = SPECPDL_INDEX ();
9390 Lisp_Object new_tool_bar;
9391 int new_n_tool_bar;
9392 struct gcpro gcpro1;
9393
9394 /* Set current_buffer to the buffer of the selected
9395 window of the frame, so that we get the right local
9396 keymaps. */
9397 set_buffer_internal_1 (XBUFFER (w->buffer));
9398
9399 /* Save match data, if we must. */
9400 if (save_match_data)
9401 record_unwind_save_match_data ();
9402
9403 /* Make sure that we don't accidentally use bogus keymaps. */
9404 if (NILP (Voverriding_local_map_menu_flag))
9405 {
9406 specbind (Qoverriding_terminal_local_map, Qnil);
9407 specbind (Qoverriding_local_map, Qnil);
9408 }
9409
9410 GCPRO1 (new_tool_bar);
9411
9412 /* Build desired tool-bar items from keymaps. */
9413 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9414 &new_n_tool_bar);
9415
9416 /* Redisplay the tool-bar if we changed it. */
9417 if (new_n_tool_bar != f->n_tool_bar_items
9418 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9419 {
9420 /* Redisplay that happens asynchronously due to an expose event
9421 may access f->tool_bar_items. Make sure we update both
9422 variables within BLOCK_INPUT so no such event interrupts. */
9423 BLOCK_INPUT;
9424 f->tool_bar_items = new_tool_bar;
9425 f->n_tool_bar_items = new_n_tool_bar;
9426 w->update_mode_line = Qt;
9427 UNBLOCK_INPUT;
9428 }
9429
9430 UNGCPRO;
9431
9432 unbind_to (count, Qnil);
9433 set_buffer_internal_1 (prev);
9434 }
9435 }
9436 }
9437
9438
9439 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9440 F's desired tool-bar contents. F->tool_bar_items must have
9441 been set up previously by calling prepare_menu_bars. */
9442
9443 static void
9444 build_desired_tool_bar_string (f)
9445 struct frame *f;
9446 {
9447 int i, size, size_needed;
9448 struct gcpro gcpro1, gcpro2, gcpro3;
9449 Lisp_Object image, plist, props;
9450
9451 image = plist = props = Qnil;
9452 GCPRO3 (image, plist, props);
9453
9454 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9455 Otherwise, make a new string. */
9456
9457 /* The size of the string we might be able to reuse. */
9458 size = (STRINGP (f->desired_tool_bar_string)
9459 ? SCHARS (f->desired_tool_bar_string)
9460 : 0);
9461
9462 /* We need one space in the string for each image. */
9463 size_needed = f->n_tool_bar_items;
9464
9465 /* Reuse f->desired_tool_bar_string, if possible. */
9466 if (size < size_needed || NILP (f->desired_tool_bar_string))
9467 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9468 make_number (' '));
9469 else
9470 {
9471 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9472 Fremove_text_properties (make_number (0), make_number (size),
9473 props, f->desired_tool_bar_string);
9474 }
9475
9476 /* Put a `display' property on the string for the images to display,
9477 put a `menu_item' property on tool-bar items with a value that
9478 is the index of the item in F's tool-bar item vector. */
9479 for (i = 0; i < f->n_tool_bar_items; ++i)
9480 {
9481 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9482
9483 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9484 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9485 int hmargin, vmargin, relief, idx, end;
9486 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9487
9488 /* If image is a vector, choose the image according to the
9489 button state. */
9490 image = PROP (TOOL_BAR_ITEM_IMAGES);
9491 if (VECTORP (image))
9492 {
9493 if (enabled_p)
9494 idx = (selected_p
9495 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9496 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9497 else
9498 idx = (selected_p
9499 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9500 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9501
9502 xassert (ASIZE (image) >= idx);
9503 image = AREF (image, idx);
9504 }
9505 else
9506 idx = -1;
9507
9508 /* Ignore invalid image specifications. */
9509 if (!valid_image_p (image))
9510 continue;
9511
9512 /* Display the tool-bar button pressed, or depressed. */
9513 plist = Fcopy_sequence (XCDR (image));
9514
9515 /* Compute margin and relief to draw. */
9516 relief = (tool_bar_button_relief >= 0
9517 ? tool_bar_button_relief
9518 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9519 hmargin = vmargin = relief;
9520
9521 if (INTEGERP (Vtool_bar_button_margin)
9522 && XINT (Vtool_bar_button_margin) > 0)
9523 {
9524 hmargin += XFASTINT (Vtool_bar_button_margin);
9525 vmargin += XFASTINT (Vtool_bar_button_margin);
9526 }
9527 else if (CONSP (Vtool_bar_button_margin))
9528 {
9529 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9530 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9531 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9532
9533 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9534 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9535 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9536 }
9537
9538 if (auto_raise_tool_bar_buttons_p)
9539 {
9540 /* Add a `:relief' property to the image spec if the item is
9541 selected. */
9542 if (selected_p)
9543 {
9544 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9545 hmargin -= relief;
9546 vmargin -= relief;
9547 }
9548 }
9549 else
9550 {
9551 /* If image is selected, display it pressed, i.e. with a
9552 negative relief. If it's not selected, display it with a
9553 raised relief. */
9554 plist = Fplist_put (plist, QCrelief,
9555 (selected_p
9556 ? make_number (-relief)
9557 : make_number (relief)));
9558 hmargin -= relief;
9559 vmargin -= relief;
9560 }
9561
9562 /* Put a margin around the image. */
9563 if (hmargin || vmargin)
9564 {
9565 if (hmargin == vmargin)
9566 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9567 else
9568 plist = Fplist_put (plist, QCmargin,
9569 Fcons (make_number (hmargin),
9570 make_number (vmargin)));
9571 }
9572
9573 /* If button is not enabled, and we don't have special images
9574 for the disabled state, make the image appear disabled by
9575 applying an appropriate algorithm to it. */
9576 if (!enabled_p && idx < 0)
9577 plist = Fplist_put (plist, QCconversion, Qdisabled);
9578
9579 /* Put a `display' text property on the string for the image to
9580 display. Put a `menu-item' property on the string that gives
9581 the start of this item's properties in the tool-bar items
9582 vector. */
9583 image = Fcons (Qimage, plist);
9584 props = list4 (Qdisplay, image,
9585 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9586
9587 /* Let the last image hide all remaining spaces in the tool bar
9588 string. The string can be longer than needed when we reuse a
9589 previous string. */
9590 if (i + 1 == f->n_tool_bar_items)
9591 end = SCHARS (f->desired_tool_bar_string);
9592 else
9593 end = i + 1;
9594 Fadd_text_properties (make_number (i), make_number (end),
9595 props, f->desired_tool_bar_string);
9596 #undef PROP
9597 }
9598
9599 UNGCPRO;
9600 }
9601
9602
9603 /* Display one line of the tool-bar of frame IT->f.
9604
9605 HEIGHT specifies the desired height of the tool-bar line.
9606 If the actual height of the glyph row is less than HEIGHT, the
9607 row's height is increased to HEIGHT, and the icons are centered
9608 vertically in the new height.
9609
9610 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9611 count a final empty row in case the tool-bar width exactly matches
9612 the window width.
9613 */
9614
9615 static void
9616 display_tool_bar_line (it, height)
9617 struct it *it;
9618 int height;
9619 {
9620 struct glyph_row *row = it->glyph_row;
9621 int max_x = it->last_visible_x;
9622 struct glyph *last;
9623
9624 prepare_desired_row (row);
9625 row->y = it->current_y;
9626
9627 /* Note that this isn't made use of if the face hasn't a box,
9628 so there's no need to check the face here. */
9629 it->start_of_box_run_p = 1;
9630
9631 while (it->current_x < max_x)
9632 {
9633 int x, n_glyphs_before, i, nglyphs;
9634 struct it it_before;
9635
9636 /* Get the next display element. */
9637 if (!get_next_display_element (it))
9638 {
9639 /* Don't count empty row if we are counting needed tool-bar lines. */
9640 if (height < 0 && !it->hpos)
9641 return;
9642 break;
9643 }
9644
9645 /* Produce glyphs. */
9646 n_glyphs_before = row->used[TEXT_AREA];
9647 it_before = *it;
9648
9649 PRODUCE_GLYPHS (it);
9650
9651 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9652 i = 0;
9653 x = it_before.current_x;
9654 while (i < nglyphs)
9655 {
9656 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9657
9658 if (x + glyph->pixel_width > max_x)
9659 {
9660 /* Glyph doesn't fit on line. Backtrack. */
9661 row->used[TEXT_AREA] = n_glyphs_before;
9662 *it = it_before;
9663 /* If this is the only glyph on this line, it will never fit on the
9664 toolbar, so skip it. But ensure there is at least one glyph,
9665 so we don't accidentally disable the tool-bar. */
9666 if (n_glyphs_before == 0
9667 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9668 break;
9669 goto out;
9670 }
9671
9672 ++it->hpos;
9673 x += glyph->pixel_width;
9674 ++i;
9675 }
9676
9677 /* Stop at line ends. */
9678 if (ITERATOR_AT_END_OF_LINE_P (it))
9679 break;
9680
9681 set_iterator_to_next (it, 1);
9682 }
9683
9684 out:;
9685
9686 row->displays_text_p = row->used[TEXT_AREA] != 0;
9687 /* Use default face for the border below the tool bar. */
9688 if (!row->displays_text_p)
9689 it->face_id = DEFAULT_FACE_ID;
9690 extend_face_to_end_of_line (it);
9691 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9692 last->right_box_line_p = 1;
9693 if (last == row->glyphs[TEXT_AREA])
9694 last->left_box_line_p = 1;
9695
9696 /* Make line the desired height and center it vertically. */
9697 if ((height -= it->max_ascent + it->max_descent) > 0)
9698 {
9699 /* Don't add more than one line height. */
9700 height %= FRAME_LINE_HEIGHT (it->f);
9701 it->max_ascent += height / 2;
9702 it->max_descent += (height + 1) / 2;
9703 }
9704
9705 compute_line_metrics (it);
9706
9707 /* If line is empty, make it occupy the rest of the tool-bar. */
9708 if (!row->displays_text_p)
9709 {
9710 row->height = row->phys_height = it->last_visible_y - row->y;
9711 row->ascent = row->phys_ascent = 0;
9712 row->extra_line_spacing = 0;
9713 }
9714
9715 row->full_width_p = 1;
9716 row->continued_p = 0;
9717 row->truncated_on_left_p = 0;
9718 row->truncated_on_right_p = 0;
9719
9720 it->current_x = it->hpos = 0;
9721 it->current_y += row->height;
9722 ++it->vpos;
9723 ++it->glyph_row;
9724 }
9725
9726
9727 /* Max tool-bar height. */
9728
9729 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9730 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9731
9732 /* Value is the number of screen lines needed to make all tool-bar
9733 items of frame F visible. The number of actual rows needed is
9734 returned in *N_ROWS if non-NULL. */
9735
9736 static int
9737 tool_bar_lines_needed (f, n_rows)
9738 struct frame *f;
9739 int *n_rows;
9740 {
9741 struct window *w = XWINDOW (f->tool_bar_window);
9742 struct it it;
9743 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9744 the desired matrix, so use (unused) mode-line row as temporary row to
9745 avoid destroying the first tool-bar row. */
9746 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9747
9748 /* Initialize an iterator for iteration over
9749 F->desired_tool_bar_string in the tool-bar window of frame F. */
9750 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9751 it.first_visible_x = 0;
9752 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9753 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9754
9755 while (!ITERATOR_AT_END_P (&it))
9756 {
9757 clear_glyph_row (temp_row);
9758 it.glyph_row = temp_row;
9759 display_tool_bar_line (&it, -1);
9760 }
9761 clear_glyph_row (temp_row);
9762
9763 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9764 if (n_rows)
9765 *n_rows = it.vpos > 0 ? it.vpos : -1;
9766
9767 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9768 }
9769
9770
9771 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9772 0, 1, 0,
9773 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9774 (frame)
9775 Lisp_Object frame;
9776 {
9777 struct frame *f;
9778 struct window *w;
9779 int nlines = 0;
9780
9781 if (NILP (frame))
9782 frame = selected_frame;
9783 else
9784 CHECK_FRAME (frame);
9785 f = XFRAME (frame);
9786
9787 if (WINDOWP (f->tool_bar_window)
9788 || (w = XWINDOW (f->tool_bar_window),
9789 WINDOW_TOTAL_LINES (w) > 0))
9790 {
9791 update_tool_bar (f, 1);
9792 if (f->n_tool_bar_items)
9793 {
9794 build_desired_tool_bar_string (f);
9795 nlines = tool_bar_lines_needed (f, NULL);
9796 }
9797 }
9798
9799 return make_number (nlines);
9800 }
9801
9802
9803 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9804 height should be changed. */
9805
9806 static int
9807 redisplay_tool_bar (f)
9808 struct frame *f;
9809 {
9810 struct window *w;
9811 struct it it;
9812 struct glyph_row *row;
9813 int change_height_p = 0;
9814
9815 #ifdef USE_GTK
9816 if (FRAME_EXTERNAL_TOOL_BAR (f))
9817 update_frame_tool_bar (f);
9818 return 0;
9819 #endif
9820
9821 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9822 do anything. This means you must start with tool-bar-lines
9823 non-zero to get the auto-sizing effect. Or in other words, you
9824 can turn off tool-bars by specifying tool-bar-lines zero. */
9825 if (!WINDOWP (f->tool_bar_window)
9826 || (w = XWINDOW (f->tool_bar_window),
9827 WINDOW_TOTAL_LINES (w) == 0))
9828 return 0;
9829
9830 /* Set up an iterator for the tool-bar window. */
9831 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9832 it.first_visible_x = 0;
9833 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9834 row = it.glyph_row;
9835
9836 /* Build a string that represents the contents of the tool-bar. */
9837 build_desired_tool_bar_string (f);
9838 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9839
9840 if (f->n_tool_bar_rows == 0)
9841 {
9842 int nlines;
9843
9844 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9845 nlines != WINDOW_TOTAL_LINES (w)))
9846 {
9847 extern Lisp_Object Qtool_bar_lines;
9848 Lisp_Object frame;
9849 int old_height = WINDOW_TOTAL_LINES (w);
9850
9851 XSETFRAME (frame, f);
9852 Fmodify_frame_parameters (frame,
9853 Fcons (Fcons (Qtool_bar_lines,
9854 make_number (nlines)),
9855 Qnil));
9856 if (WINDOW_TOTAL_LINES (w) != old_height)
9857 {
9858 clear_glyph_matrix (w->desired_matrix);
9859 fonts_changed_p = 1;
9860 return 1;
9861 }
9862 }
9863 }
9864
9865 /* Display as many lines as needed to display all tool-bar items. */
9866
9867 if (f->n_tool_bar_rows > 0)
9868 {
9869 int border, rows, height, extra;
9870
9871 if (INTEGERP (Vtool_bar_border))
9872 border = XINT (Vtool_bar_border);
9873 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9874 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9875 else if (EQ (Vtool_bar_border, Qborder_width))
9876 border = f->border_width;
9877 else
9878 border = 0;
9879 if (border < 0)
9880 border = 0;
9881
9882 rows = f->n_tool_bar_rows;
9883 height = max (1, (it.last_visible_y - border) / rows);
9884 extra = it.last_visible_y - border - height * rows;
9885
9886 while (it.current_y < it.last_visible_y)
9887 {
9888 int h = 0;
9889 if (extra > 0 && rows-- > 0)
9890 {
9891 h = (extra + rows - 1) / rows;
9892 extra -= h;
9893 }
9894 display_tool_bar_line (&it, height + h);
9895 }
9896 }
9897 else
9898 {
9899 while (it.current_y < it.last_visible_y)
9900 display_tool_bar_line (&it, 0);
9901 }
9902
9903 /* It doesn't make much sense to try scrolling in the tool-bar
9904 window, so don't do it. */
9905 w->desired_matrix->no_scrolling_p = 1;
9906 w->must_be_updated_p = 1;
9907
9908 if (auto_resize_tool_bars_p)
9909 {
9910 int nlines, nrows;
9911 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9912
9913 /* If we couldn't display everything, change the tool-bar's
9914 height if there is room for more. */
9915 if (IT_STRING_CHARPOS (it) < it.end_charpos
9916 && it.current_y < max_tool_bar_height)
9917 change_height_p = 1;
9918
9919 row = it.glyph_row - 1;
9920
9921 /* If there are blank lines at the end, except for a partially
9922 visible blank line at the end that is smaller than
9923 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9924 if (!row->displays_text_p
9925 && row->height >= FRAME_LINE_HEIGHT (f))
9926 change_height_p = 1;
9927
9928 /* If row displays tool-bar items, but is partially visible,
9929 change the tool-bar's height. */
9930 if (row->displays_text_p
9931 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9932 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9933 change_height_p = 1;
9934
9935 /* Resize windows as needed by changing the `tool-bar-lines'
9936 frame parameter. */
9937 if (change_height_p
9938 && (nlines = tool_bar_lines_needed (f, &nrows),
9939 nlines != WINDOW_TOTAL_LINES (w)))
9940 {
9941 extern Lisp_Object Qtool_bar_lines;
9942 Lisp_Object frame;
9943 int old_height = WINDOW_TOTAL_LINES (w);
9944
9945 XSETFRAME (frame, f);
9946 Fmodify_frame_parameters (frame,
9947 Fcons (Fcons (Qtool_bar_lines,
9948 make_number (nlines)),
9949 Qnil));
9950 if (WINDOW_TOTAL_LINES (w) != old_height)
9951 {
9952 clear_glyph_matrix (w->desired_matrix);
9953 f->n_tool_bar_rows = nrows;
9954 fonts_changed_p = 1;
9955 }
9956 }
9957 }
9958
9959 return change_height_p;
9960 }
9961
9962
9963 /* Get information about the tool-bar item which is displayed in GLYPH
9964 on frame F. Return in *PROP_IDX the index where tool-bar item
9965 properties start in F->tool_bar_items. Value is zero if
9966 GLYPH doesn't display a tool-bar item. */
9967
9968 static int
9969 tool_bar_item_info (f, glyph, prop_idx)
9970 struct frame *f;
9971 struct glyph *glyph;
9972 int *prop_idx;
9973 {
9974 Lisp_Object prop;
9975 int success_p;
9976 int charpos;
9977
9978 /* This function can be called asynchronously, which means we must
9979 exclude any possibility that Fget_text_property signals an
9980 error. */
9981 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9982 charpos = max (0, charpos);
9983
9984 /* Get the text property `menu-item' at pos. The value of that
9985 property is the start index of this item's properties in
9986 F->tool_bar_items. */
9987 prop = Fget_text_property (make_number (charpos),
9988 Qmenu_item, f->current_tool_bar_string);
9989 if (INTEGERP (prop))
9990 {
9991 *prop_idx = XINT (prop);
9992 success_p = 1;
9993 }
9994 else
9995 success_p = 0;
9996
9997 return success_p;
9998 }
9999
10000 \f
10001 /* Get information about the tool-bar item at position X/Y on frame F.
10002 Return in *GLYPH a pointer to the glyph of the tool-bar item in
10003 the current matrix of the tool-bar window of F, or NULL if not
10004 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
10005 item in F->tool_bar_items. Value is
10006
10007 -1 if X/Y is not on a tool-bar item
10008 0 if X/Y is on the same item that was highlighted before.
10009 1 otherwise. */
10010
10011 static int
10012 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
10013 struct frame *f;
10014 int x, y;
10015 struct glyph **glyph;
10016 int *hpos, *vpos, *prop_idx;
10017 {
10018 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10019 struct window *w = XWINDOW (f->tool_bar_window);
10020 int area;
10021
10022 /* Find the glyph under X/Y. */
10023 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10024 if (*glyph == NULL)
10025 return -1;
10026
10027 /* Get the start of this tool-bar item's properties in
10028 f->tool_bar_items. */
10029 if (!tool_bar_item_info (f, *glyph, prop_idx))
10030 return -1;
10031
10032 /* Is mouse on the highlighted item? */
10033 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10034 && *vpos >= dpyinfo->mouse_face_beg_row
10035 && *vpos <= dpyinfo->mouse_face_end_row
10036 && (*vpos > dpyinfo->mouse_face_beg_row
10037 || *hpos >= dpyinfo->mouse_face_beg_col)
10038 && (*vpos < dpyinfo->mouse_face_end_row
10039 || *hpos < dpyinfo->mouse_face_end_col
10040 || dpyinfo->mouse_face_past_end))
10041 return 0;
10042
10043 return 1;
10044 }
10045
10046
10047 /* EXPORT:
10048 Handle mouse button event on the tool-bar of frame F, at
10049 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10050 0 for button release. MODIFIERS is event modifiers for button
10051 release. */
10052
10053 void
10054 handle_tool_bar_click (f, x, y, down_p, modifiers)
10055 struct frame *f;
10056 int x, y, down_p;
10057 unsigned int modifiers;
10058 {
10059 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10060 struct window *w = XWINDOW (f->tool_bar_window);
10061 int hpos, vpos, prop_idx;
10062 struct glyph *glyph;
10063 Lisp_Object enabled_p;
10064
10065 /* If not on the highlighted tool-bar item, return. */
10066 frame_to_window_pixel_xy (w, &x, &y);
10067 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10068 return;
10069
10070 /* If item is disabled, do nothing. */
10071 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10072 if (NILP (enabled_p))
10073 return;
10074
10075 if (down_p)
10076 {
10077 /* Show item in pressed state. */
10078 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10079 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10080 last_tool_bar_item = prop_idx;
10081 }
10082 else
10083 {
10084 Lisp_Object key, frame;
10085 struct input_event event;
10086 EVENT_INIT (event);
10087
10088 /* Show item in released state. */
10089 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10090 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10091
10092 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10093
10094 XSETFRAME (frame, f);
10095 event.kind = TOOL_BAR_EVENT;
10096 event.frame_or_window = frame;
10097 event.arg = frame;
10098 kbd_buffer_store_event (&event);
10099
10100 event.kind = TOOL_BAR_EVENT;
10101 event.frame_or_window = frame;
10102 event.arg = key;
10103 event.modifiers = modifiers;
10104 kbd_buffer_store_event (&event);
10105 last_tool_bar_item = -1;
10106 }
10107 }
10108
10109
10110 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10111 tool-bar window-relative coordinates X/Y. Called from
10112 note_mouse_highlight. */
10113
10114 static void
10115 note_tool_bar_highlight (f, x, y)
10116 struct frame *f;
10117 int x, y;
10118 {
10119 Lisp_Object window = f->tool_bar_window;
10120 struct window *w = XWINDOW (window);
10121 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10122 int hpos, vpos;
10123 struct glyph *glyph;
10124 struct glyph_row *row;
10125 int i;
10126 Lisp_Object enabled_p;
10127 int prop_idx;
10128 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10129 int mouse_down_p, rc;
10130
10131 /* Function note_mouse_highlight is called with negative x(y
10132 values when mouse moves outside of the frame. */
10133 if (x <= 0 || y <= 0)
10134 {
10135 clear_mouse_face (dpyinfo);
10136 return;
10137 }
10138
10139 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10140 if (rc < 0)
10141 {
10142 /* Not on tool-bar item. */
10143 clear_mouse_face (dpyinfo);
10144 return;
10145 }
10146 else if (rc == 0)
10147 /* On same tool-bar item as before. */
10148 goto set_help_echo;
10149
10150 clear_mouse_face (dpyinfo);
10151
10152 /* Mouse is down, but on different tool-bar item? */
10153 mouse_down_p = (dpyinfo->grabbed
10154 && f == last_mouse_frame
10155 && FRAME_LIVE_P (f));
10156 if (mouse_down_p
10157 && last_tool_bar_item != prop_idx)
10158 return;
10159
10160 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10161 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10162
10163 /* If tool-bar item is not enabled, don't highlight it. */
10164 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10165 if (!NILP (enabled_p))
10166 {
10167 /* Compute the x-position of the glyph. In front and past the
10168 image is a space. We include this in the highlighted area. */
10169 row = MATRIX_ROW (w->current_matrix, vpos);
10170 for (i = x = 0; i < hpos; ++i)
10171 x += row->glyphs[TEXT_AREA][i].pixel_width;
10172
10173 /* Record this as the current active region. */
10174 dpyinfo->mouse_face_beg_col = hpos;
10175 dpyinfo->mouse_face_beg_row = vpos;
10176 dpyinfo->mouse_face_beg_x = x;
10177 dpyinfo->mouse_face_beg_y = row->y;
10178 dpyinfo->mouse_face_past_end = 0;
10179
10180 dpyinfo->mouse_face_end_col = hpos + 1;
10181 dpyinfo->mouse_face_end_row = vpos;
10182 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10183 dpyinfo->mouse_face_end_y = row->y;
10184 dpyinfo->mouse_face_window = window;
10185 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10186
10187 /* Display it as active. */
10188 show_mouse_face (dpyinfo, draw);
10189 dpyinfo->mouse_face_image_state = draw;
10190 }
10191
10192 set_help_echo:
10193
10194 /* Set help_echo_string to a help string to display for this tool-bar item.
10195 XTread_socket does the rest. */
10196 help_echo_object = help_echo_window = Qnil;
10197 help_echo_pos = -1;
10198 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10199 if (NILP (help_echo_string))
10200 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10201 }
10202
10203 #endif /* HAVE_WINDOW_SYSTEM */
10204
10205
10206 \f
10207 /************************************************************************
10208 Horizontal scrolling
10209 ************************************************************************/
10210
10211 static int hscroll_window_tree P_ ((Lisp_Object));
10212 static int hscroll_windows P_ ((Lisp_Object));
10213
10214 /* For all leaf windows in the window tree rooted at WINDOW, set their
10215 hscroll value so that PT is (i) visible in the window, and (ii) so
10216 that it is not within a certain margin at the window's left and
10217 right border. Value is non-zero if any window's hscroll has been
10218 changed. */
10219
10220 static int
10221 hscroll_window_tree (window)
10222 Lisp_Object window;
10223 {
10224 int hscrolled_p = 0;
10225 int hscroll_relative_p = FLOATP (Vhscroll_step);
10226 int hscroll_step_abs = 0;
10227 double hscroll_step_rel = 0;
10228
10229 if (hscroll_relative_p)
10230 {
10231 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10232 if (hscroll_step_rel < 0)
10233 {
10234 hscroll_relative_p = 0;
10235 hscroll_step_abs = 0;
10236 }
10237 }
10238 else if (INTEGERP (Vhscroll_step))
10239 {
10240 hscroll_step_abs = XINT (Vhscroll_step);
10241 if (hscroll_step_abs < 0)
10242 hscroll_step_abs = 0;
10243 }
10244 else
10245 hscroll_step_abs = 0;
10246
10247 while (WINDOWP (window))
10248 {
10249 struct window *w = XWINDOW (window);
10250
10251 if (WINDOWP (w->hchild))
10252 hscrolled_p |= hscroll_window_tree (w->hchild);
10253 else if (WINDOWP (w->vchild))
10254 hscrolled_p |= hscroll_window_tree (w->vchild);
10255 else if (w->cursor.vpos >= 0)
10256 {
10257 int h_margin;
10258 int text_area_width;
10259 struct glyph_row *current_cursor_row
10260 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10261 struct glyph_row *desired_cursor_row
10262 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10263 struct glyph_row *cursor_row
10264 = (desired_cursor_row->enabled_p
10265 ? desired_cursor_row
10266 : current_cursor_row);
10267
10268 text_area_width = window_box_width (w, TEXT_AREA);
10269
10270 /* Scroll when cursor is inside this scroll margin. */
10271 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10272
10273 if ((XFASTINT (w->hscroll)
10274 && w->cursor.x <= h_margin)
10275 || (cursor_row->enabled_p
10276 && cursor_row->truncated_on_right_p
10277 && (w->cursor.x >= text_area_width - h_margin)))
10278 {
10279 struct it it;
10280 int hscroll;
10281 struct buffer *saved_current_buffer;
10282 int pt;
10283 int wanted_x;
10284
10285 /* Find point in a display of infinite width. */
10286 saved_current_buffer = current_buffer;
10287 current_buffer = XBUFFER (w->buffer);
10288
10289 if (w == XWINDOW (selected_window))
10290 pt = BUF_PT (current_buffer);
10291 else
10292 {
10293 pt = marker_position (w->pointm);
10294 pt = max (BEGV, pt);
10295 pt = min (ZV, pt);
10296 }
10297
10298 /* Move iterator to pt starting at cursor_row->start in
10299 a line with infinite width. */
10300 init_to_row_start (&it, w, cursor_row);
10301 it.last_visible_x = INFINITY;
10302 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10303 current_buffer = saved_current_buffer;
10304
10305 /* Position cursor in window. */
10306 if (!hscroll_relative_p && hscroll_step_abs == 0)
10307 hscroll = max (0, (it.current_x
10308 - (ITERATOR_AT_END_OF_LINE_P (&it)
10309 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10310 : (text_area_width / 2))))
10311 / FRAME_COLUMN_WIDTH (it.f);
10312 else if (w->cursor.x >= text_area_width - h_margin)
10313 {
10314 if (hscroll_relative_p)
10315 wanted_x = text_area_width * (1 - hscroll_step_rel)
10316 - h_margin;
10317 else
10318 wanted_x = text_area_width
10319 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10320 - h_margin;
10321 hscroll
10322 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10323 }
10324 else
10325 {
10326 if (hscroll_relative_p)
10327 wanted_x = text_area_width * hscroll_step_rel
10328 + h_margin;
10329 else
10330 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10331 + h_margin;
10332 hscroll
10333 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10334 }
10335 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10336
10337 /* Don't call Fset_window_hscroll if value hasn't
10338 changed because it will prevent redisplay
10339 optimizations. */
10340 if (XFASTINT (w->hscroll) != hscroll)
10341 {
10342 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10343 w->hscroll = make_number (hscroll);
10344 hscrolled_p = 1;
10345 }
10346 }
10347 }
10348
10349 window = w->next;
10350 }
10351
10352 /* Value is non-zero if hscroll of any leaf window has been changed. */
10353 return hscrolled_p;
10354 }
10355
10356
10357 /* Set hscroll so that cursor is visible and not inside horizontal
10358 scroll margins for all windows in the tree rooted at WINDOW. See
10359 also hscroll_window_tree above. Value is non-zero if any window's
10360 hscroll has been changed. If it has, desired matrices on the frame
10361 of WINDOW are cleared. */
10362
10363 static int
10364 hscroll_windows (window)
10365 Lisp_Object window;
10366 {
10367 int hscrolled_p;
10368
10369 if (automatic_hscrolling_p)
10370 {
10371 hscrolled_p = hscroll_window_tree (window);
10372 if (hscrolled_p)
10373 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10374 }
10375 else
10376 hscrolled_p = 0;
10377 return hscrolled_p;
10378 }
10379
10380
10381 \f
10382 /************************************************************************
10383 Redisplay
10384 ************************************************************************/
10385
10386 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10387 to a non-zero value. This is sometimes handy to have in a debugger
10388 session. */
10389
10390 #if GLYPH_DEBUG
10391
10392 /* First and last unchanged row for try_window_id. */
10393
10394 int debug_first_unchanged_at_end_vpos;
10395 int debug_last_unchanged_at_beg_vpos;
10396
10397 /* Delta vpos and y. */
10398
10399 int debug_dvpos, debug_dy;
10400
10401 /* Delta in characters and bytes for try_window_id. */
10402
10403 int debug_delta, debug_delta_bytes;
10404
10405 /* Values of window_end_pos and window_end_vpos at the end of
10406 try_window_id. */
10407
10408 EMACS_INT debug_end_pos, debug_end_vpos;
10409
10410 /* Append a string to W->desired_matrix->method. FMT is a printf
10411 format string. A1...A9 are a supplement for a variable-length
10412 argument list. If trace_redisplay_p is non-zero also printf the
10413 resulting string to stderr. */
10414
10415 static void
10416 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10417 struct window *w;
10418 char *fmt;
10419 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10420 {
10421 char buffer[512];
10422 char *method = w->desired_matrix->method;
10423 int len = strlen (method);
10424 int size = sizeof w->desired_matrix->method;
10425 int remaining = size - len - 1;
10426
10427 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10428 if (len && remaining)
10429 {
10430 method[len] = '|';
10431 --remaining, ++len;
10432 }
10433
10434 strncpy (method + len, buffer, remaining);
10435
10436 if (trace_redisplay_p)
10437 fprintf (stderr, "%p (%s): %s\n",
10438 w,
10439 ((BUFFERP (w->buffer)
10440 && STRINGP (XBUFFER (w->buffer)->name))
10441 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10442 : "no buffer"),
10443 buffer);
10444 }
10445
10446 #endif /* GLYPH_DEBUG */
10447
10448
10449 /* Value is non-zero if all changes in window W, which displays
10450 current_buffer, are in the text between START and END. START is a
10451 buffer position, END is given as a distance from Z. Used in
10452 redisplay_internal for display optimization. */
10453
10454 static INLINE int
10455 text_outside_line_unchanged_p (w, start, end)
10456 struct window *w;
10457 int start, end;
10458 {
10459 int unchanged_p = 1;
10460
10461 /* If text or overlays have changed, see where. */
10462 if (XFASTINT (w->last_modified) < MODIFF
10463 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10464 {
10465 /* Gap in the line? */
10466 if (GPT < start || Z - GPT < end)
10467 unchanged_p = 0;
10468
10469 /* Changes start in front of the line, or end after it? */
10470 if (unchanged_p
10471 && (BEG_UNCHANGED < start - 1
10472 || END_UNCHANGED < end))
10473 unchanged_p = 0;
10474
10475 /* If selective display, can't optimize if changes start at the
10476 beginning of the line. */
10477 if (unchanged_p
10478 && INTEGERP (current_buffer->selective_display)
10479 && XINT (current_buffer->selective_display) > 0
10480 && (BEG_UNCHANGED < start || GPT <= start))
10481 unchanged_p = 0;
10482
10483 /* If there are overlays at the start or end of the line, these
10484 may have overlay strings with newlines in them. A change at
10485 START, for instance, may actually concern the display of such
10486 overlay strings as well, and they are displayed on different
10487 lines. So, quickly rule out this case. (For the future, it
10488 might be desirable to implement something more telling than
10489 just BEG/END_UNCHANGED.) */
10490 if (unchanged_p)
10491 {
10492 if (BEG + BEG_UNCHANGED == start
10493 && overlay_touches_p (start))
10494 unchanged_p = 0;
10495 if (END_UNCHANGED == end
10496 && overlay_touches_p (Z - end))
10497 unchanged_p = 0;
10498 }
10499 }
10500
10501 return unchanged_p;
10502 }
10503
10504
10505 /* Do a frame update, taking possible shortcuts into account. This is
10506 the main external entry point for redisplay.
10507
10508 If the last redisplay displayed an echo area message and that message
10509 is no longer requested, we clear the echo area or bring back the
10510 mini-buffer if that is in use. */
10511
10512 void
10513 redisplay ()
10514 {
10515 redisplay_internal (0);
10516 }
10517
10518
10519 static Lisp_Object
10520 overlay_arrow_string_or_property (var)
10521 Lisp_Object var;
10522 {
10523 Lisp_Object val;
10524
10525 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10526 return val;
10527
10528 return Voverlay_arrow_string;
10529 }
10530
10531 /* Return 1 if there are any overlay-arrows in current_buffer. */
10532 static int
10533 overlay_arrow_in_current_buffer_p ()
10534 {
10535 Lisp_Object vlist;
10536
10537 for (vlist = Voverlay_arrow_variable_list;
10538 CONSP (vlist);
10539 vlist = XCDR (vlist))
10540 {
10541 Lisp_Object var = XCAR (vlist);
10542 Lisp_Object val;
10543
10544 if (!SYMBOLP (var))
10545 continue;
10546 val = find_symbol_value (var);
10547 if (MARKERP (val)
10548 && current_buffer == XMARKER (val)->buffer)
10549 return 1;
10550 }
10551 return 0;
10552 }
10553
10554
10555 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10556 has changed. */
10557
10558 static int
10559 overlay_arrows_changed_p ()
10560 {
10561 Lisp_Object vlist;
10562
10563 for (vlist = Voverlay_arrow_variable_list;
10564 CONSP (vlist);
10565 vlist = XCDR (vlist))
10566 {
10567 Lisp_Object var = XCAR (vlist);
10568 Lisp_Object val, pstr;
10569
10570 if (!SYMBOLP (var))
10571 continue;
10572 val = find_symbol_value (var);
10573 if (!MARKERP (val))
10574 continue;
10575 if (! EQ (COERCE_MARKER (val),
10576 Fget (var, Qlast_arrow_position))
10577 || ! (pstr = overlay_arrow_string_or_property (var),
10578 EQ (pstr, Fget (var, Qlast_arrow_string))))
10579 return 1;
10580 }
10581 return 0;
10582 }
10583
10584 /* Mark overlay arrows to be updated on next redisplay. */
10585
10586 static void
10587 update_overlay_arrows (up_to_date)
10588 int up_to_date;
10589 {
10590 Lisp_Object vlist;
10591
10592 for (vlist = Voverlay_arrow_variable_list;
10593 CONSP (vlist);
10594 vlist = XCDR (vlist))
10595 {
10596 Lisp_Object var = XCAR (vlist);
10597
10598 if (!SYMBOLP (var))
10599 continue;
10600
10601 if (up_to_date > 0)
10602 {
10603 Lisp_Object val = find_symbol_value (var);
10604 Fput (var, Qlast_arrow_position,
10605 COERCE_MARKER (val));
10606 Fput (var, Qlast_arrow_string,
10607 overlay_arrow_string_or_property (var));
10608 }
10609 else if (up_to_date < 0
10610 || !NILP (Fget (var, Qlast_arrow_position)))
10611 {
10612 Fput (var, Qlast_arrow_position, Qt);
10613 Fput (var, Qlast_arrow_string, Qt);
10614 }
10615 }
10616 }
10617
10618
10619 /* Return overlay arrow string to display at row.
10620 Return integer (bitmap number) for arrow bitmap in left fringe.
10621 Return nil if no overlay arrow. */
10622
10623 static Lisp_Object
10624 overlay_arrow_at_row (it, row)
10625 struct it *it;
10626 struct glyph_row *row;
10627 {
10628 Lisp_Object vlist;
10629
10630 for (vlist = Voverlay_arrow_variable_list;
10631 CONSP (vlist);
10632 vlist = XCDR (vlist))
10633 {
10634 Lisp_Object var = XCAR (vlist);
10635 Lisp_Object val;
10636
10637 if (!SYMBOLP (var))
10638 continue;
10639
10640 val = find_symbol_value (var);
10641
10642 if (MARKERP (val)
10643 && current_buffer == XMARKER (val)->buffer
10644 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10645 {
10646 if (FRAME_WINDOW_P (it->f)
10647 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10648 {
10649 #ifdef HAVE_WINDOW_SYSTEM
10650 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10651 {
10652 int fringe_bitmap;
10653 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10654 return make_number (fringe_bitmap);
10655 }
10656 #endif
10657 return make_number (-1); /* Use default arrow bitmap */
10658 }
10659 return overlay_arrow_string_or_property (var);
10660 }
10661 }
10662
10663 return Qnil;
10664 }
10665
10666 /* Return 1 if point moved out of or into a composition. Otherwise
10667 return 0. PREV_BUF and PREV_PT are the last point buffer and
10668 position. BUF and PT are the current point buffer and position. */
10669
10670 int
10671 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10672 struct buffer *prev_buf, *buf;
10673 int prev_pt, pt;
10674 {
10675 int start, end;
10676 Lisp_Object prop;
10677 Lisp_Object buffer;
10678
10679 XSETBUFFER (buffer, buf);
10680 /* Check a composition at the last point if point moved within the
10681 same buffer. */
10682 if (prev_buf == buf)
10683 {
10684 if (prev_pt == pt)
10685 /* Point didn't move. */
10686 return 0;
10687
10688 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10689 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10690 && COMPOSITION_VALID_P (start, end, prop)
10691 && start < prev_pt && end > prev_pt)
10692 /* The last point was within the composition. Return 1 iff
10693 point moved out of the composition. */
10694 return (pt <= start || pt >= end);
10695 }
10696
10697 /* Check a composition at the current point. */
10698 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10699 && find_composition (pt, -1, &start, &end, &prop, buffer)
10700 && COMPOSITION_VALID_P (start, end, prop)
10701 && start < pt && end > pt);
10702 }
10703
10704
10705 /* Reconsider the setting of B->clip_changed which is displayed
10706 in window W. */
10707
10708 static INLINE void
10709 reconsider_clip_changes (w, b)
10710 struct window *w;
10711 struct buffer *b;
10712 {
10713 if (b->clip_changed
10714 && !NILP (w->window_end_valid)
10715 && w->current_matrix->buffer == b
10716 && w->current_matrix->zv == BUF_ZV (b)
10717 && w->current_matrix->begv == BUF_BEGV (b))
10718 b->clip_changed = 0;
10719
10720 /* If display wasn't paused, and W is not a tool bar window, see if
10721 point has been moved into or out of a composition. In that case,
10722 we set b->clip_changed to 1 to force updating the screen. If
10723 b->clip_changed has already been set to 1, we can skip this
10724 check. */
10725 if (!b->clip_changed
10726 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10727 {
10728 int pt;
10729
10730 if (w == XWINDOW (selected_window))
10731 pt = BUF_PT (current_buffer);
10732 else
10733 pt = marker_position (w->pointm);
10734
10735 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10736 || pt != XINT (w->last_point))
10737 && check_point_in_composition (w->current_matrix->buffer,
10738 XINT (w->last_point),
10739 XBUFFER (w->buffer), pt))
10740 b->clip_changed = 1;
10741 }
10742 }
10743 \f
10744
10745 /* Select FRAME to forward the values of frame-local variables into C
10746 variables so that the redisplay routines can access those values
10747 directly. */
10748
10749 static void
10750 select_frame_for_redisplay (frame)
10751 Lisp_Object frame;
10752 {
10753 Lisp_Object tail, sym, val;
10754 Lisp_Object old = selected_frame;
10755
10756 selected_frame = frame;
10757
10758 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10759 if (CONSP (XCAR (tail))
10760 && (sym = XCAR (XCAR (tail)),
10761 SYMBOLP (sym))
10762 && (sym = indirect_variable (sym),
10763 val = SYMBOL_VALUE (sym),
10764 (BUFFER_LOCAL_VALUEP (val)
10765 || SOME_BUFFER_LOCAL_VALUEP (val)))
10766 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10767 /* Use find_symbol_value rather than Fsymbol_value
10768 to avoid an error if it is void. */
10769 find_symbol_value (sym);
10770
10771 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10772 if (CONSP (XCAR (tail))
10773 && (sym = XCAR (XCAR (tail)),
10774 SYMBOLP (sym))
10775 && (sym = indirect_variable (sym),
10776 val = SYMBOL_VALUE (sym),
10777 (BUFFER_LOCAL_VALUEP (val)
10778 || SOME_BUFFER_LOCAL_VALUEP (val)))
10779 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10780 find_symbol_value (sym);
10781 }
10782
10783
10784 #define STOP_POLLING \
10785 do { if (! polling_stopped_here) stop_polling (); \
10786 polling_stopped_here = 1; } while (0)
10787
10788 #define RESUME_POLLING \
10789 do { if (polling_stopped_here) start_polling (); \
10790 polling_stopped_here = 0; } while (0)
10791
10792
10793 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10794 response to any user action; therefore, we should preserve the echo
10795 area. (Actually, our caller does that job.) Perhaps in the future
10796 avoid recentering windows if it is not necessary; currently that
10797 causes some problems. */
10798
10799 static void
10800 redisplay_internal (preserve_echo_area)
10801 int preserve_echo_area;
10802 {
10803 struct window *w = XWINDOW (selected_window);
10804 struct frame *f;
10805 int pause;
10806 int must_finish = 0;
10807 struct text_pos tlbufpos, tlendpos;
10808 int number_of_visible_frames;
10809 int count;
10810 struct frame *sf;
10811 int polling_stopped_here = 0;
10812
10813 /* Non-zero means redisplay has to consider all windows on all
10814 frames. Zero means, only selected_window is considered. */
10815 int consider_all_windows_p;
10816
10817 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10818
10819 /* No redisplay if running in batch mode or frame is not yet fully
10820 initialized, or redisplay is explicitly turned off by setting
10821 Vinhibit_redisplay. */
10822 if (noninteractive
10823 || !NILP (Vinhibit_redisplay))
10824 return;
10825
10826 /* Don't examine these until after testing Vinhibit_redisplay.
10827 When Emacs is shutting down, perhaps because its connection to
10828 X has dropped, we should not look at them at all. */
10829 f = XFRAME (w->frame);
10830 sf = SELECTED_FRAME ();
10831
10832 if (!f->glyphs_initialized_p)
10833 return;
10834
10835 /* The flag redisplay_performed_directly_p is set by
10836 direct_output_for_insert when it already did the whole screen
10837 update necessary. */
10838 if (redisplay_performed_directly_p)
10839 {
10840 redisplay_performed_directly_p = 0;
10841 if (!hscroll_windows (selected_window))
10842 return;
10843 }
10844
10845 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10846 if (popup_activated ())
10847 return;
10848 #endif
10849
10850 /* I don't think this happens but let's be paranoid. */
10851 if (redisplaying_p)
10852 return;
10853
10854 /* Record a function that resets redisplaying_p to its old value
10855 when we leave this function. */
10856 count = SPECPDL_INDEX ();
10857 record_unwind_protect (unwind_redisplay,
10858 Fcons (make_number (redisplaying_p), selected_frame));
10859 ++redisplaying_p;
10860 specbind (Qinhibit_free_realized_faces, Qnil);
10861
10862 {
10863 Lisp_Object tail, frame;
10864
10865 FOR_EACH_FRAME (tail, frame)
10866 {
10867 struct frame *f = XFRAME (frame);
10868 f->already_hscrolled_p = 0;
10869 }
10870 }
10871
10872 retry:
10873 pause = 0;
10874 reconsider_clip_changes (w, current_buffer);
10875 last_escape_glyph_frame = NULL;
10876 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10877
10878 /* If new fonts have been loaded that make a glyph matrix adjustment
10879 necessary, do it. */
10880 if (fonts_changed_p)
10881 {
10882 adjust_glyphs (NULL);
10883 ++windows_or_buffers_changed;
10884 fonts_changed_p = 0;
10885 }
10886
10887 /* If face_change_count is non-zero, init_iterator will free all
10888 realized faces, which includes the faces referenced from current
10889 matrices. So, we can't reuse current matrices in this case. */
10890 if (face_change_count)
10891 ++windows_or_buffers_changed;
10892
10893 if (! FRAME_WINDOW_P (sf)
10894 && previous_terminal_frame != sf)
10895 {
10896 /* Since frames on an ASCII terminal share the same display
10897 area, displaying a different frame means redisplay the whole
10898 thing. */
10899 windows_or_buffers_changed++;
10900 SET_FRAME_GARBAGED (sf);
10901 XSETFRAME (Vterminal_frame, sf);
10902 }
10903 previous_terminal_frame = sf;
10904
10905 /* Set the visible flags for all frames. Do this before checking
10906 for resized or garbaged frames; they want to know if their frames
10907 are visible. See the comment in frame.h for
10908 FRAME_SAMPLE_VISIBILITY. */
10909 {
10910 Lisp_Object tail, frame;
10911
10912 number_of_visible_frames = 0;
10913
10914 FOR_EACH_FRAME (tail, frame)
10915 {
10916 struct frame *f = XFRAME (frame);
10917
10918 FRAME_SAMPLE_VISIBILITY (f);
10919 if (FRAME_VISIBLE_P (f))
10920 ++number_of_visible_frames;
10921 clear_desired_matrices (f);
10922 }
10923 }
10924
10925 /* Notice any pending interrupt request to change frame size. */
10926 do_pending_window_change (1);
10927
10928 /* Clear frames marked as garbaged. */
10929 if (frame_garbaged)
10930 clear_garbaged_frames ();
10931
10932 /* Build menubar and tool-bar items. */
10933 if (NILP (Vmemory_full))
10934 prepare_menu_bars ();
10935
10936 if (windows_or_buffers_changed)
10937 update_mode_lines++;
10938
10939 /* Detect case that we need to write or remove a star in the mode line. */
10940 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10941 {
10942 w->update_mode_line = Qt;
10943 if (buffer_shared > 1)
10944 update_mode_lines++;
10945 }
10946
10947 /* If %c is in the mode line, update it if needed. */
10948 if (!NILP (w->column_number_displayed)
10949 /* This alternative quickly identifies a common case
10950 where no change is needed. */
10951 && !(PT == XFASTINT (w->last_point)
10952 && XFASTINT (w->last_modified) >= MODIFF
10953 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10954 && (XFASTINT (w->column_number_displayed)
10955 != (int) current_column ())) /* iftc */
10956 w->update_mode_line = Qt;
10957
10958 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10959
10960 /* The variable buffer_shared is set in redisplay_window and
10961 indicates that we redisplay a buffer in different windows. See
10962 there. */
10963 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10964 || cursor_type_changed);
10965
10966 /* If specs for an arrow have changed, do thorough redisplay
10967 to ensure we remove any arrow that should no longer exist. */
10968 if (overlay_arrows_changed_p ())
10969 consider_all_windows_p = windows_or_buffers_changed = 1;
10970
10971 /* Normally the message* functions will have already displayed and
10972 updated the echo area, but the frame may have been trashed, or
10973 the update may have been preempted, so display the echo area
10974 again here. Checking message_cleared_p captures the case that
10975 the echo area should be cleared. */
10976 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10977 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10978 || (message_cleared_p
10979 && minibuf_level == 0
10980 /* If the mini-window is currently selected, this means the
10981 echo-area doesn't show through. */
10982 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10983 {
10984 int window_height_changed_p = echo_area_display (0);
10985 must_finish = 1;
10986
10987 /* If we don't display the current message, don't clear the
10988 message_cleared_p flag, because, if we did, we wouldn't clear
10989 the echo area in the next redisplay which doesn't preserve
10990 the echo area. */
10991 if (!display_last_displayed_message_p)
10992 message_cleared_p = 0;
10993
10994 if (fonts_changed_p)
10995 goto retry;
10996 else if (window_height_changed_p)
10997 {
10998 consider_all_windows_p = 1;
10999 ++update_mode_lines;
11000 ++windows_or_buffers_changed;
11001
11002 /* If window configuration was changed, frames may have been
11003 marked garbaged. Clear them or we will experience
11004 surprises wrt scrolling. */
11005 if (frame_garbaged)
11006 clear_garbaged_frames ();
11007 }
11008 }
11009 else if (EQ (selected_window, minibuf_window)
11010 && (current_buffer->clip_changed
11011 || XFASTINT (w->last_modified) < MODIFF
11012 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11013 && resize_mini_window (w, 0))
11014 {
11015 /* Resized active mini-window to fit the size of what it is
11016 showing if its contents might have changed. */
11017 must_finish = 1;
11018 consider_all_windows_p = 1;
11019 ++windows_or_buffers_changed;
11020 ++update_mode_lines;
11021
11022 /* If window configuration was changed, frames may have been
11023 marked garbaged. Clear them or we will experience
11024 surprises wrt scrolling. */
11025 if (frame_garbaged)
11026 clear_garbaged_frames ();
11027 }
11028
11029
11030 /* If showing the region, and mark has changed, we must redisplay
11031 the whole window. The assignment to this_line_start_pos prevents
11032 the optimization directly below this if-statement. */
11033 if (((!NILP (Vtransient_mark_mode)
11034 && !NILP (XBUFFER (w->buffer)->mark_active))
11035 != !NILP (w->region_showing))
11036 || (!NILP (w->region_showing)
11037 && !EQ (w->region_showing,
11038 Fmarker_position (XBUFFER (w->buffer)->mark))))
11039 CHARPOS (this_line_start_pos) = 0;
11040
11041 /* Optimize the case that only the line containing the cursor in the
11042 selected window has changed. Variables starting with this_ are
11043 set in display_line and record information about the line
11044 containing the cursor. */
11045 tlbufpos = this_line_start_pos;
11046 tlendpos = this_line_end_pos;
11047 if (!consider_all_windows_p
11048 && CHARPOS (tlbufpos) > 0
11049 && NILP (w->update_mode_line)
11050 && !current_buffer->clip_changed
11051 && !current_buffer->prevent_redisplay_optimizations_p
11052 && FRAME_VISIBLE_P (XFRAME (w->frame))
11053 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11054 /* Make sure recorded data applies to current buffer, etc. */
11055 && this_line_buffer == current_buffer
11056 && current_buffer == XBUFFER (w->buffer)
11057 && NILP (w->force_start)
11058 && NILP (w->optional_new_start)
11059 /* Point must be on the line that we have info recorded about. */
11060 && PT >= CHARPOS (tlbufpos)
11061 && PT <= Z - CHARPOS (tlendpos)
11062 /* All text outside that line, including its final newline,
11063 must be unchanged */
11064 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11065 CHARPOS (tlendpos)))
11066 {
11067 if (CHARPOS (tlbufpos) > BEGV
11068 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11069 && (CHARPOS (tlbufpos) == ZV
11070 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11071 /* Former continuation line has disappeared by becoming empty */
11072 goto cancel;
11073 else if (XFASTINT (w->last_modified) < MODIFF
11074 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11075 || MINI_WINDOW_P (w))
11076 {
11077 /* We have to handle the case of continuation around a
11078 wide-column character (See the comment in indent.c around
11079 line 885).
11080
11081 For instance, in the following case:
11082
11083 -------- Insert --------
11084 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11085 J_I_ ==> J_I_ `^^' are cursors.
11086 ^^ ^^
11087 -------- --------
11088
11089 As we have to redraw the line above, we should goto cancel. */
11090
11091 struct it it;
11092 int line_height_before = this_line_pixel_height;
11093
11094 /* Note that start_display will handle the case that the
11095 line starting at tlbufpos is a continuation lines. */
11096 start_display (&it, w, tlbufpos);
11097
11098 /* Implementation note: It this still necessary? */
11099 if (it.current_x != this_line_start_x)
11100 goto cancel;
11101
11102 TRACE ((stderr, "trying display optimization 1\n"));
11103 w->cursor.vpos = -1;
11104 overlay_arrow_seen = 0;
11105 it.vpos = this_line_vpos;
11106 it.current_y = this_line_y;
11107 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11108 display_line (&it);
11109
11110 /* If line contains point, is not continued,
11111 and ends at same distance from eob as before, we win */
11112 if (w->cursor.vpos >= 0
11113 /* Line is not continued, otherwise this_line_start_pos
11114 would have been set to 0 in display_line. */
11115 && CHARPOS (this_line_start_pos)
11116 /* Line ends as before. */
11117 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11118 /* Line has same height as before. Otherwise other lines
11119 would have to be shifted up or down. */
11120 && this_line_pixel_height == line_height_before)
11121 {
11122 /* If this is not the window's last line, we must adjust
11123 the charstarts of the lines below. */
11124 if (it.current_y < it.last_visible_y)
11125 {
11126 struct glyph_row *row
11127 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11128 int delta, delta_bytes;
11129
11130 if (Z - CHARPOS (tlendpos) == ZV)
11131 {
11132 /* This line ends at end of (accessible part of)
11133 buffer. There is no newline to count. */
11134 delta = (Z
11135 - CHARPOS (tlendpos)
11136 - MATRIX_ROW_START_CHARPOS (row));
11137 delta_bytes = (Z_BYTE
11138 - BYTEPOS (tlendpos)
11139 - MATRIX_ROW_START_BYTEPOS (row));
11140 }
11141 else
11142 {
11143 /* This line ends in a newline. Must take
11144 account of the newline and the rest of the
11145 text that follows. */
11146 delta = (Z
11147 - CHARPOS (tlendpos)
11148 - MATRIX_ROW_START_CHARPOS (row));
11149 delta_bytes = (Z_BYTE
11150 - BYTEPOS (tlendpos)
11151 - MATRIX_ROW_START_BYTEPOS (row));
11152 }
11153
11154 increment_matrix_positions (w->current_matrix,
11155 this_line_vpos + 1,
11156 w->current_matrix->nrows,
11157 delta, delta_bytes);
11158 }
11159
11160 /* If this row displays text now but previously didn't,
11161 or vice versa, w->window_end_vpos may have to be
11162 adjusted. */
11163 if ((it.glyph_row - 1)->displays_text_p)
11164 {
11165 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11166 XSETINT (w->window_end_vpos, this_line_vpos);
11167 }
11168 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11169 && this_line_vpos > 0)
11170 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11171 w->window_end_valid = Qnil;
11172
11173 /* Update hint: No need to try to scroll in update_window. */
11174 w->desired_matrix->no_scrolling_p = 1;
11175
11176 #if GLYPH_DEBUG
11177 *w->desired_matrix->method = 0;
11178 debug_method_add (w, "optimization 1");
11179 #endif
11180 #ifdef HAVE_WINDOW_SYSTEM
11181 update_window_fringes (w, 0);
11182 #endif
11183 goto update;
11184 }
11185 else
11186 goto cancel;
11187 }
11188 else if (/* Cursor position hasn't changed. */
11189 PT == XFASTINT (w->last_point)
11190 /* Make sure the cursor was last displayed
11191 in this window. Otherwise we have to reposition it. */
11192 && 0 <= w->cursor.vpos
11193 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11194 {
11195 if (!must_finish)
11196 {
11197 do_pending_window_change (1);
11198
11199 /* We used to always goto end_of_redisplay here, but this
11200 isn't enough if we have a blinking cursor. */
11201 if (w->cursor_off_p == w->last_cursor_off_p)
11202 goto end_of_redisplay;
11203 }
11204 goto update;
11205 }
11206 /* If highlighting the region, or if the cursor is in the echo area,
11207 then we can't just move the cursor. */
11208 else if (! (!NILP (Vtransient_mark_mode)
11209 && !NILP (current_buffer->mark_active))
11210 && (EQ (selected_window, current_buffer->last_selected_window)
11211 || highlight_nonselected_windows)
11212 && NILP (w->region_showing)
11213 && NILP (Vshow_trailing_whitespace)
11214 && !cursor_in_echo_area)
11215 {
11216 struct it it;
11217 struct glyph_row *row;
11218
11219 /* Skip from tlbufpos to PT and see where it is. Note that
11220 PT may be in invisible text. If so, we will end at the
11221 next visible position. */
11222 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11223 NULL, DEFAULT_FACE_ID);
11224 it.current_x = this_line_start_x;
11225 it.current_y = this_line_y;
11226 it.vpos = this_line_vpos;
11227
11228 /* The call to move_it_to stops in front of PT, but
11229 moves over before-strings. */
11230 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11231
11232 if (it.vpos == this_line_vpos
11233 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11234 row->enabled_p))
11235 {
11236 xassert (this_line_vpos == it.vpos);
11237 xassert (this_line_y == it.current_y);
11238 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11239 #if GLYPH_DEBUG
11240 *w->desired_matrix->method = 0;
11241 debug_method_add (w, "optimization 3");
11242 #endif
11243 goto update;
11244 }
11245 else
11246 goto cancel;
11247 }
11248
11249 cancel:
11250 /* Text changed drastically or point moved off of line. */
11251 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11252 }
11253
11254 CHARPOS (this_line_start_pos) = 0;
11255 consider_all_windows_p |= buffer_shared > 1;
11256 ++clear_face_cache_count;
11257 #ifdef HAVE_WINDOW_SYSTEM
11258 ++clear_image_cache_count;
11259 #endif
11260
11261 /* Build desired matrices, and update the display. If
11262 consider_all_windows_p is non-zero, do it for all windows on all
11263 frames. Otherwise do it for selected_window, only. */
11264
11265 if (consider_all_windows_p)
11266 {
11267 Lisp_Object tail, frame;
11268
11269 FOR_EACH_FRAME (tail, frame)
11270 XFRAME (frame)->updated_p = 0;
11271
11272 /* Recompute # windows showing selected buffer. This will be
11273 incremented each time such a window is displayed. */
11274 buffer_shared = 0;
11275
11276 FOR_EACH_FRAME (tail, frame)
11277 {
11278 struct frame *f = XFRAME (frame);
11279
11280 if (FRAME_WINDOW_P (f) || f == sf)
11281 {
11282 if (! EQ (frame, selected_frame))
11283 /* Select the frame, for the sake of frame-local
11284 variables. */
11285 select_frame_for_redisplay (frame);
11286
11287 /* Mark all the scroll bars to be removed; we'll redeem
11288 the ones we want when we redisplay their windows. */
11289 if (condemn_scroll_bars_hook)
11290 condemn_scroll_bars_hook (f);
11291
11292 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11293 redisplay_windows (FRAME_ROOT_WINDOW (f));
11294
11295 /* Any scroll bars which redisplay_windows should have
11296 nuked should now go away. */
11297 if (judge_scroll_bars_hook)
11298 judge_scroll_bars_hook (f);
11299
11300 /* If fonts changed, display again. */
11301 /* ??? rms: I suspect it is a mistake to jump all the way
11302 back to retry here. It should just retry this frame. */
11303 if (fonts_changed_p)
11304 goto retry;
11305
11306 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11307 {
11308 /* See if we have to hscroll. */
11309 if (!f->already_hscrolled_p)
11310 {
11311 f->already_hscrolled_p = 1;
11312 if (hscroll_windows (f->root_window))
11313 goto retry;
11314 }
11315
11316 /* Prevent various kinds of signals during display
11317 update. stdio is not robust about handling
11318 signals, which can cause an apparent I/O
11319 error. */
11320 if (interrupt_input)
11321 unrequest_sigio ();
11322 STOP_POLLING;
11323
11324 /* Update the display. */
11325 set_window_update_flags (XWINDOW (f->root_window), 1);
11326 pause |= update_frame (f, 0, 0);
11327 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11328 if (pause)
11329 break;
11330 #endif
11331
11332 f->updated_p = 1;
11333 }
11334 }
11335 }
11336
11337 if (!pause)
11338 {
11339 /* Do the mark_window_display_accurate after all windows have
11340 been redisplayed because this call resets flags in buffers
11341 which are needed for proper redisplay. */
11342 FOR_EACH_FRAME (tail, frame)
11343 {
11344 struct frame *f = XFRAME (frame);
11345 if (f->updated_p)
11346 {
11347 mark_window_display_accurate (f->root_window, 1);
11348 if (frame_up_to_date_hook)
11349 frame_up_to_date_hook (f);
11350 }
11351 }
11352 }
11353 }
11354 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11355 {
11356 Lisp_Object mini_window;
11357 struct frame *mini_frame;
11358
11359 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11360 /* Use list_of_error, not Qerror, so that
11361 we catch only errors and don't run the debugger. */
11362 internal_condition_case_1 (redisplay_window_1, selected_window,
11363 list_of_error,
11364 redisplay_window_error);
11365
11366 /* Compare desired and current matrices, perform output. */
11367
11368 update:
11369 /* If fonts changed, display again. */
11370 if (fonts_changed_p)
11371 goto retry;
11372
11373 /* Prevent various kinds of signals during display update.
11374 stdio is not robust about handling signals,
11375 which can cause an apparent I/O error. */
11376 if (interrupt_input)
11377 unrequest_sigio ();
11378 STOP_POLLING;
11379
11380 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11381 {
11382 if (hscroll_windows (selected_window))
11383 goto retry;
11384
11385 XWINDOW (selected_window)->must_be_updated_p = 1;
11386 pause = update_frame (sf, 0, 0);
11387 }
11388
11389 /* We may have called echo_area_display at the top of this
11390 function. If the echo area is on another frame, that may
11391 have put text on a frame other than the selected one, so the
11392 above call to update_frame would not have caught it. Catch
11393 it here. */
11394 mini_window = FRAME_MINIBUF_WINDOW (sf);
11395 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11396
11397 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11398 {
11399 XWINDOW (mini_window)->must_be_updated_p = 1;
11400 pause |= update_frame (mini_frame, 0, 0);
11401 if (!pause && hscroll_windows (mini_window))
11402 goto retry;
11403 }
11404 }
11405
11406 /* If display was paused because of pending input, make sure we do a
11407 thorough update the next time. */
11408 if (pause)
11409 {
11410 /* Prevent the optimization at the beginning of
11411 redisplay_internal that tries a single-line update of the
11412 line containing the cursor in the selected window. */
11413 CHARPOS (this_line_start_pos) = 0;
11414
11415 /* Let the overlay arrow be updated the next time. */
11416 update_overlay_arrows (0);
11417
11418 /* If we pause after scrolling, some rows in the current
11419 matrices of some windows are not valid. */
11420 if (!WINDOW_FULL_WIDTH_P (w)
11421 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11422 update_mode_lines = 1;
11423 }
11424 else
11425 {
11426 if (!consider_all_windows_p)
11427 {
11428 /* This has already been done above if
11429 consider_all_windows_p is set. */
11430 mark_window_display_accurate_1 (w, 1);
11431
11432 /* Say overlay arrows are up to date. */
11433 update_overlay_arrows (1);
11434
11435 if (frame_up_to_date_hook != 0)
11436 frame_up_to_date_hook (sf);
11437 }
11438
11439 update_mode_lines = 0;
11440 windows_or_buffers_changed = 0;
11441 cursor_type_changed = 0;
11442 }
11443
11444 /* Start SIGIO interrupts coming again. Having them off during the
11445 code above makes it less likely one will discard output, but not
11446 impossible, since there might be stuff in the system buffer here.
11447 But it is much hairier to try to do anything about that. */
11448 if (interrupt_input)
11449 request_sigio ();
11450 RESUME_POLLING;
11451
11452 /* If a frame has become visible which was not before, redisplay
11453 again, so that we display it. Expose events for such a frame
11454 (which it gets when becoming visible) don't call the parts of
11455 redisplay constructing glyphs, so simply exposing a frame won't
11456 display anything in this case. So, we have to display these
11457 frames here explicitly. */
11458 if (!pause)
11459 {
11460 Lisp_Object tail, frame;
11461 int new_count = 0;
11462
11463 FOR_EACH_FRAME (tail, frame)
11464 {
11465 int this_is_visible = 0;
11466
11467 if (XFRAME (frame)->visible)
11468 this_is_visible = 1;
11469 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11470 if (XFRAME (frame)->visible)
11471 this_is_visible = 1;
11472
11473 if (this_is_visible)
11474 new_count++;
11475 }
11476
11477 if (new_count != number_of_visible_frames)
11478 windows_or_buffers_changed++;
11479 }
11480
11481 /* Change frame size now if a change is pending. */
11482 do_pending_window_change (1);
11483
11484 /* If we just did a pending size change, or have additional
11485 visible frames, redisplay again. */
11486 if (windows_or_buffers_changed && !pause)
11487 goto retry;
11488
11489 /* Clear the face cache eventually. */
11490 if (consider_all_windows_p)
11491 {
11492 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11493 {
11494 clear_face_cache (0);
11495 clear_face_cache_count = 0;
11496 }
11497 #ifdef HAVE_WINDOW_SYSTEM
11498 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11499 {
11500 Lisp_Object tail, frame;
11501 FOR_EACH_FRAME (tail, frame)
11502 {
11503 struct frame *f = XFRAME (frame);
11504 if (FRAME_WINDOW_P (f))
11505 clear_image_cache (f, 0);
11506 }
11507 clear_image_cache_count = 0;
11508 }
11509 #endif /* HAVE_WINDOW_SYSTEM */
11510 }
11511
11512 end_of_redisplay:
11513 unbind_to (count, Qnil);
11514 RESUME_POLLING;
11515 }
11516
11517
11518 /* Redisplay, but leave alone any recent echo area message unless
11519 another message has been requested in its place.
11520
11521 This is useful in situations where you need to redisplay but no
11522 user action has occurred, making it inappropriate for the message
11523 area to be cleared. See tracking_off and
11524 wait_reading_process_output for examples of these situations.
11525
11526 FROM_WHERE is an integer saying from where this function was
11527 called. This is useful for debugging. */
11528
11529 void
11530 redisplay_preserve_echo_area (from_where)
11531 int from_where;
11532 {
11533 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11534
11535 if (!NILP (echo_area_buffer[1]))
11536 {
11537 /* We have a previously displayed message, but no current
11538 message. Redisplay the previous message. */
11539 display_last_displayed_message_p = 1;
11540 redisplay_internal (1);
11541 display_last_displayed_message_p = 0;
11542 }
11543 else
11544 redisplay_internal (1);
11545
11546 if (rif != NULL && rif->flush_display_optional)
11547 rif->flush_display_optional (NULL);
11548 }
11549
11550
11551 /* Function registered with record_unwind_protect in
11552 redisplay_internal. Reset redisplaying_p to the value it had
11553 before redisplay_internal was called, and clear
11554 prevent_freeing_realized_faces_p. It also selects the previously
11555 selected frame. */
11556
11557 static Lisp_Object
11558 unwind_redisplay (val)
11559 Lisp_Object val;
11560 {
11561 Lisp_Object old_redisplaying_p, old_frame;
11562
11563 old_redisplaying_p = XCAR (val);
11564 redisplaying_p = XFASTINT (old_redisplaying_p);
11565 old_frame = XCDR (val);
11566 if (! EQ (old_frame, selected_frame))
11567 select_frame_for_redisplay (old_frame);
11568 return Qnil;
11569 }
11570
11571
11572 /* Mark the display of window W as accurate or inaccurate. If
11573 ACCURATE_P is non-zero mark display of W as accurate. If
11574 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11575 redisplay_internal is called. */
11576
11577 static void
11578 mark_window_display_accurate_1 (w, accurate_p)
11579 struct window *w;
11580 int accurate_p;
11581 {
11582 if (BUFFERP (w->buffer))
11583 {
11584 struct buffer *b = XBUFFER (w->buffer);
11585
11586 w->last_modified
11587 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11588 w->last_overlay_modified
11589 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11590 w->last_had_star
11591 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11592
11593 if (accurate_p)
11594 {
11595 b->clip_changed = 0;
11596 b->prevent_redisplay_optimizations_p = 0;
11597
11598 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11599 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11600 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11601 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11602
11603 w->current_matrix->buffer = b;
11604 w->current_matrix->begv = BUF_BEGV (b);
11605 w->current_matrix->zv = BUF_ZV (b);
11606
11607 w->last_cursor = w->cursor;
11608 w->last_cursor_off_p = w->cursor_off_p;
11609
11610 if (w == XWINDOW (selected_window))
11611 w->last_point = make_number (BUF_PT (b));
11612 else
11613 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11614 }
11615 }
11616
11617 if (accurate_p)
11618 {
11619 w->window_end_valid = w->buffer;
11620 #if 0 /* This is incorrect with variable-height lines. */
11621 xassert (XINT (w->window_end_vpos)
11622 < (WINDOW_TOTAL_LINES (w)
11623 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11624 #endif
11625 w->update_mode_line = Qnil;
11626 }
11627 }
11628
11629
11630 /* Mark the display of windows in the window tree rooted at WINDOW as
11631 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11632 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11633 be redisplayed the next time redisplay_internal is called. */
11634
11635 void
11636 mark_window_display_accurate (window, accurate_p)
11637 Lisp_Object window;
11638 int accurate_p;
11639 {
11640 struct window *w;
11641
11642 for (; !NILP (window); window = w->next)
11643 {
11644 w = XWINDOW (window);
11645 mark_window_display_accurate_1 (w, accurate_p);
11646
11647 if (!NILP (w->vchild))
11648 mark_window_display_accurate (w->vchild, accurate_p);
11649 if (!NILP (w->hchild))
11650 mark_window_display_accurate (w->hchild, accurate_p);
11651 }
11652
11653 if (accurate_p)
11654 {
11655 update_overlay_arrows (1);
11656 }
11657 else
11658 {
11659 /* Force a thorough redisplay the next time by setting
11660 last_arrow_position and last_arrow_string to t, which is
11661 unequal to any useful value of Voverlay_arrow_... */
11662 update_overlay_arrows (-1);
11663 }
11664 }
11665
11666
11667 /* Return value in display table DP (Lisp_Char_Table *) for character
11668 C. Since a display table doesn't have any parent, we don't have to
11669 follow parent. Do not call this function directly but use the
11670 macro DISP_CHAR_VECTOR. */
11671
11672 Lisp_Object
11673 disp_char_vector (dp, c)
11674 struct Lisp_Char_Table *dp;
11675 int c;
11676 {
11677 int code[4], i;
11678 Lisp_Object val;
11679
11680 if (SINGLE_BYTE_CHAR_P (c))
11681 return (dp->contents[c]);
11682
11683 SPLIT_CHAR (c, code[0], code[1], code[2]);
11684 if (code[1] < 32)
11685 code[1] = -1;
11686 else if (code[2] < 32)
11687 code[2] = -1;
11688
11689 /* Here, the possible range of code[0] (== charset ID) is
11690 128..max_charset. Since the top level char table contains data
11691 for multibyte characters after 256th element, we must increment
11692 code[0] by 128 to get a correct index. */
11693 code[0] += 128;
11694 code[3] = -1; /* anchor */
11695
11696 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11697 {
11698 val = dp->contents[code[i]];
11699 if (!SUB_CHAR_TABLE_P (val))
11700 return (NILP (val) ? dp->defalt : val);
11701 }
11702
11703 /* Here, val is a sub char table. We return the default value of
11704 it. */
11705 return (dp->defalt);
11706 }
11707
11708
11709 \f
11710 /***********************************************************************
11711 Window Redisplay
11712 ***********************************************************************/
11713
11714 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11715
11716 static void
11717 redisplay_windows (window)
11718 Lisp_Object window;
11719 {
11720 while (!NILP (window))
11721 {
11722 struct window *w = XWINDOW (window);
11723
11724 if (!NILP (w->hchild))
11725 redisplay_windows (w->hchild);
11726 else if (!NILP (w->vchild))
11727 redisplay_windows (w->vchild);
11728 else
11729 {
11730 displayed_buffer = XBUFFER (w->buffer);
11731 /* Use list_of_error, not Qerror, so that
11732 we catch only errors and don't run the debugger. */
11733 internal_condition_case_1 (redisplay_window_0, window,
11734 list_of_error,
11735 redisplay_window_error);
11736 }
11737
11738 window = w->next;
11739 }
11740 }
11741
11742 static Lisp_Object
11743 redisplay_window_error ()
11744 {
11745 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11746 return Qnil;
11747 }
11748
11749 static Lisp_Object
11750 redisplay_window_0 (window)
11751 Lisp_Object window;
11752 {
11753 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11754 redisplay_window (window, 0);
11755 return Qnil;
11756 }
11757
11758 static Lisp_Object
11759 redisplay_window_1 (window)
11760 Lisp_Object window;
11761 {
11762 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11763 redisplay_window (window, 1);
11764 return Qnil;
11765 }
11766 \f
11767
11768 /* Increment GLYPH until it reaches END or CONDITION fails while
11769 adding (GLYPH)->pixel_width to X. */
11770
11771 #define SKIP_GLYPHS(glyph, end, x, condition) \
11772 do \
11773 { \
11774 (x) += (glyph)->pixel_width; \
11775 ++(glyph); \
11776 } \
11777 while ((glyph) < (end) && (condition))
11778
11779
11780 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11781 DELTA is the number of bytes by which positions recorded in ROW
11782 differ from current buffer positions.
11783
11784 Return 0 if cursor is not on this row. 1 otherwise. */
11785
11786 int
11787 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11788 struct window *w;
11789 struct glyph_row *row;
11790 struct glyph_matrix *matrix;
11791 int delta, delta_bytes, dy, dvpos;
11792 {
11793 struct glyph *glyph = row->glyphs[TEXT_AREA];
11794 struct glyph *end = glyph + row->used[TEXT_AREA];
11795 struct glyph *cursor = NULL;
11796 /* The first glyph that starts a sequence of glyphs from string. */
11797 struct glyph *string_start;
11798 /* The X coordinate of string_start. */
11799 int string_start_x;
11800 /* The last known character position. */
11801 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11802 /* The last known character position before string_start. */
11803 int string_before_pos;
11804 int x = row->x;
11805 int cursor_x = x;
11806 int cursor_from_overlay_pos = 0;
11807 int pt_old = PT - delta;
11808
11809 /* Skip over glyphs not having an object at the start of the row.
11810 These are special glyphs like truncation marks on terminal
11811 frames. */
11812 if (row->displays_text_p)
11813 while (glyph < end
11814 && INTEGERP (glyph->object)
11815 && glyph->charpos < 0)
11816 {
11817 x += glyph->pixel_width;
11818 ++glyph;
11819 }
11820
11821 string_start = NULL;
11822 while (glyph < end
11823 && !INTEGERP (glyph->object)
11824 && (!BUFFERP (glyph->object)
11825 || (last_pos = glyph->charpos) < pt_old))
11826 {
11827 if (! STRINGP (glyph->object))
11828 {
11829 string_start = NULL;
11830 x += glyph->pixel_width;
11831 ++glyph;
11832 if (cursor_from_overlay_pos
11833 && last_pos >= cursor_from_overlay_pos)
11834 {
11835 cursor_from_overlay_pos = 0;
11836 cursor = 0;
11837 }
11838 }
11839 else
11840 {
11841 if (string_start == NULL)
11842 {
11843 string_before_pos = last_pos;
11844 string_start = glyph;
11845 string_start_x = x;
11846 }
11847 /* Skip all glyphs from string. */
11848 do
11849 {
11850 Lisp_Object cprop;
11851 int pos;
11852 if ((cursor == NULL || glyph > cursor)
11853 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11854 Qcursor, (glyph)->object),
11855 !NILP (cprop))
11856 && (pos = string_buffer_position (w, glyph->object,
11857 string_before_pos),
11858 (pos == 0 /* From overlay */
11859 || pos == pt_old)))
11860 {
11861 /* Estimate overlay buffer position from the buffer
11862 positions of the glyphs before and after the overlay.
11863 Add 1 to last_pos so that if point corresponds to the
11864 glyph right after the overlay, we still use a 'cursor'
11865 property found in that overlay. */
11866 cursor_from_overlay_pos = (pos ? 0 : last_pos
11867 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11868 cursor = glyph;
11869 cursor_x = x;
11870 }
11871 x += glyph->pixel_width;
11872 ++glyph;
11873 }
11874 while (glyph < end && EQ (glyph->object, string_start->object));
11875 }
11876 }
11877
11878 if (cursor != NULL)
11879 {
11880 glyph = cursor;
11881 x = cursor_x;
11882 }
11883 else if (row->ends_in_ellipsis_p && glyph == end)
11884 {
11885 /* Scan back over the ellipsis glyphs, decrementing positions. */
11886 while (glyph > row->glyphs[TEXT_AREA]
11887 && (glyph - 1)->charpos == last_pos)
11888 glyph--, x -= glyph->pixel_width;
11889 /* That loop always goes one position too far,
11890 including the glyph before the ellipsis.
11891 So scan forward over that one. */
11892 x += glyph->pixel_width;
11893 glyph++;
11894 }
11895 else if (string_start
11896 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11897 {
11898 /* We may have skipped over point because the previous glyphs
11899 are from string. As there's no easy way to know the
11900 character position of the current glyph, find the correct
11901 glyph on point by scanning from string_start again. */
11902 Lisp_Object limit;
11903 Lisp_Object string;
11904 struct glyph *stop = glyph;
11905 int pos;
11906
11907 limit = make_number (pt_old + 1);
11908 glyph = string_start;
11909 x = string_start_x;
11910 string = glyph->object;
11911 pos = string_buffer_position (w, string, string_before_pos);
11912 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11913 because we always put cursor after overlay strings. */
11914 while (pos == 0 && glyph < stop)
11915 {
11916 string = glyph->object;
11917 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11918 if (glyph < stop)
11919 pos = string_buffer_position (w, glyph->object, string_before_pos);
11920 }
11921
11922 while (glyph < stop)
11923 {
11924 pos = XINT (Fnext_single_char_property_change
11925 (make_number (pos), Qdisplay, Qnil, limit));
11926 if (pos > pt_old)
11927 break;
11928 /* Skip glyphs from the same string. */
11929 string = glyph->object;
11930 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11931 /* Skip glyphs from an overlay. */
11932 while (glyph < stop
11933 && ! string_buffer_position (w, glyph->object, pos))
11934 {
11935 string = glyph->object;
11936 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11937 }
11938 }
11939
11940 /* If we reached the end of the line, and end was from a string,
11941 cursor is not on this line. */
11942 if (glyph == end && row->continued_p)
11943 return 0;
11944 }
11945
11946 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11947 w->cursor.x = x;
11948 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11949 w->cursor.y = row->y + dy;
11950
11951 if (w == XWINDOW (selected_window))
11952 {
11953 if (!row->continued_p
11954 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11955 && row->x == 0)
11956 {
11957 this_line_buffer = XBUFFER (w->buffer);
11958
11959 CHARPOS (this_line_start_pos)
11960 = MATRIX_ROW_START_CHARPOS (row) + delta;
11961 BYTEPOS (this_line_start_pos)
11962 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11963
11964 CHARPOS (this_line_end_pos)
11965 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11966 BYTEPOS (this_line_end_pos)
11967 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11968
11969 this_line_y = w->cursor.y;
11970 this_line_pixel_height = row->height;
11971 this_line_vpos = w->cursor.vpos;
11972 this_line_start_x = row->x;
11973 }
11974 else
11975 CHARPOS (this_line_start_pos) = 0;
11976 }
11977
11978 return 1;
11979 }
11980
11981
11982 /* Run window scroll functions, if any, for WINDOW with new window
11983 start STARTP. Sets the window start of WINDOW to that position.
11984
11985 We assume that the window's buffer is really current. */
11986
11987 static INLINE struct text_pos
11988 run_window_scroll_functions (window, startp)
11989 Lisp_Object window;
11990 struct text_pos startp;
11991 {
11992 struct window *w = XWINDOW (window);
11993 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11994
11995 if (current_buffer != XBUFFER (w->buffer))
11996 abort ();
11997
11998 if (!NILP (Vwindow_scroll_functions))
11999 {
12000 run_hook_with_args_2 (Qwindow_scroll_functions, window,
12001 make_number (CHARPOS (startp)));
12002 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12003 /* In case the hook functions switch buffers. */
12004 if (current_buffer != XBUFFER (w->buffer))
12005 set_buffer_internal_1 (XBUFFER (w->buffer));
12006 }
12007
12008 return startp;
12009 }
12010
12011
12012 /* Make sure the line containing the cursor is fully visible.
12013 A value of 1 means there is nothing to be done.
12014 (Either the line is fully visible, or it cannot be made so,
12015 or we cannot tell.)
12016
12017 If FORCE_P is non-zero, return 0 even if partial visible cursor row
12018 is higher than window.
12019
12020 A value of 0 means the caller should do scrolling
12021 as if point had gone off the screen. */
12022
12023 static int
12024 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
12025 struct window *w;
12026 int force_p;
12027 int current_matrix_p;
12028 {
12029 struct glyph_matrix *matrix;
12030 struct glyph_row *row;
12031 int window_height;
12032
12033 if (!make_cursor_line_fully_visible_p)
12034 return 1;
12035
12036 /* It's not always possible to find the cursor, e.g, when a window
12037 is full of overlay strings. Don't do anything in that case. */
12038 if (w->cursor.vpos < 0)
12039 return 1;
12040
12041 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12042 row = MATRIX_ROW (matrix, w->cursor.vpos);
12043
12044 /* If the cursor row is not partially visible, there's nothing to do. */
12045 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12046 return 1;
12047
12048 /* If the row the cursor is in is taller than the window's height,
12049 it's not clear what to do, so do nothing. */
12050 window_height = window_box_height (w);
12051 if (row->height >= window_height)
12052 {
12053 if (!force_p || MINI_WINDOW_P (w)
12054 || w->vscroll || w->cursor.vpos == 0)
12055 return 1;
12056 }
12057 return 0;
12058
12059 #if 0
12060 /* This code used to try to scroll the window just enough to make
12061 the line visible. It returned 0 to say that the caller should
12062 allocate larger glyph matrices. */
12063
12064 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12065 {
12066 int dy = row->height - row->visible_height;
12067 w->vscroll = 0;
12068 w->cursor.y += dy;
12069 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12070 }
12071 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12072 {
12073 int dy = - (row->height - row->visible_height);
12074 w->vscroll = dy;
12075 w->cursor.y += dy;
12076 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12077 }
12078
12079 /* When we change the cursor y-position of the selected window,
12080 change this_line_y as well so that the display optimization for
12081 the cursor line of the selected window in redisplay_internal uses
12082 the correct y-position. */
12083 if (w == XWINDOW (selected_window))
12084 this_line_y = w->cursor.y;
12085
12086 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12087 redisplay with larger matrices. */
12088 if (matrix->nrows < required_matrix_height (w))
12089 {
12090 fonts_changed_p = 1;
12091 return 0;
12092 }
12093
12094 return 1;
12095 #endif /* 0 */
12096 }
12097
12098
12099 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12100 non-zero means only WINDOW is redisplayed in redisplay_internal.
12101 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12102 in redisplay_window to bring a partially visible line into view in
12103 the case that only the cursor has moved.
12104
12105 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12106 last screen line's vertical height extends past the end of the screen.
12107
12108 Value is
12109
12110 1 if scrolling succeeded
12111
12112 0 if scrolling didn't find point.
12113
12114 -1 if new fonts have been loaded so that we must interrupt
12115 redisplay, adjust glyph matrices, and try again. */
12116
12117 enum
12118 {
12119 SCROLLING_SUCCESS,
12120 SCROLLING_FAILED,
12121 SCROLLING_NEED_LARGER_MATRICES
12122 };
12123
12124 static int
12125 try_scrolling (window, just_this_one_p, scroll_conservatively,
12126 scroll_step, temp_scroll_step, last_line_misfit)
12127 Lisp_Object window;
12128 int just_this_one_p;
12129 EMACS_INT scroll_conservatively, scroll_step;
12130 int temp_scroll_step;
12131 int last_line_misfit;
12132 {
12133 struct window *w = XWINDOW (window);
12134 struct frame *f = XFRAME (w->frame);
12135 struct text_pos scroll_margin_pos;
12136 struct text_pos pos;
12137 struct text_pos startp;
12138 struct it it;
12139 Lisp_Object window_end;
12140 int this_scroll_margin;
12141 int dy = 0;
12142 int scroll_max;
12143 int rc;
12144 int amount_to_scroll = 0;
12145 Lisp_Object aggressive;
12146 int height;
12147 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12148
12149 #if GLYPH_DEBUG
12150 debug_method_add (w, "try_scrolling");
12151 #endif
12152
12153 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12154
12155 /* Compute scroll margin height in pixels. We scroll when point is
12156 within this distance from the top or bottom of the window. */
12157 if (scroll_margin > 0)
12158 {
12159 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12160 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12161 }
12162 else
12163 this_scroll_margin = 0;
12164
12165 /* Force scroll_conservatively to have a reasonable value so it doesn't
12166 cause an overflow while computing how much to scroll. */
12167 if (scroll_conservatively)
12168 scroll_conservatively = min (scroll_conservatively,
12169 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12170
12171 /* Compute how much we should try to scroll maximally to bring point
12172 into view. */
12173 if (scroll_step || scroll_conservatively || temp_scroll_step)
12174 scroll_max = max (scroll_step,
12175 max (scroll_conservatively, temp_scroll_step));
12176 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12177 || NUMBERP (current_buffer->scroll_up_aggressively))
12178 /* We're trying to scroll because of aggressive scrolling
12179 but no scroll_step is set. Choose an arbitrary one. Maybe
12180 there should be a variable for this. */
12181 scroll_max = 10;
12182 else
12183 scroll_max = 0;
12184 scroll_max *= FRAME_LINE_HEIGHT (f);
12185
12186 /* Decide whether we have to scroll down. Start at the window end
12187 and move this_scroll_margin up to find the position of the scroll
12188 margin. */
12189 window_end = Fwindow_end (window, Qt);
12190
12191 too_near_end:
12192
12193 CHARPOS (scroll_margin_pos) = XINT (window_end);
12194 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12195
12196 if (this_scroll_margin || extra_scroll_margin_lines)
12197 {
12198 start_display (&it, w, scroll_margin_pos);
12199 if (this_scroll_margin)
12200 move_it_vertically_backward (&it, this_scroll_margin);
12201 if (extra_scroll_margin_lines)
12202 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12203 scroll_margin_pos = it.current.pos;
12204 }
12205
12206 if (PT >= CHARPOS (scroll_margin_pos))
12207 {
12208 int y0;
12209
12210 /* Point is in the scroll margin at the bottom of the window, or
12211 below. Compute a new window start that makes point visible. */
12212
12213 /* Compute the distance from the scroll margin to PT.
12214 Give up if the distance is greater than scroll_max. */
12215 start_display (&it, w, scroll_margin_pos);
12216 y0 = it.current_y;
12217 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12218 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12219
12220 /* To make point visible, we have to move the window start
12221 down so that the line the cursor is in is visible, which
12222 means we have to add in the height of the cursor line. */
12223 dy = line_bottom_y (&it) - y0;
12224
12225 if (dy > scroll_max)
12226 return SCROLLING_FAILED;
12227
12228 /* Move the window start down. If scrolling conservatively,
12229 move it just enough down to make point visible. If
12230 scroll_step is set, move it down by scroll_step. */
12231 start_display (&it, w, startp);
12232
12233 if (scroll_conservatively)
12234 /* Set AMOUNT_TO_SCROLL to at least one line,
12235 and at most scroll_conservatively lines. */
12236 amount_to_scroll
12237 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12238 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12239 else if (scroll_step || temp_scroll_step)
12240 amount_to_scroll = scroll_max;
12241 else
12242 {
12243 aggressive = current_buffer->scroll_up_aggressively;
12244 height = WINDOW_BOX_TEXT_HEIGHT (w);
12245 if (NUMBERP (aggressive))
12246 {
12247 double float_amount = XFLOATINT (aggressive) * height;
12248 amount_to_scroll = float_amount;
12249 if (amount_to_scroll == 0 && float_amount > 0)
12250 amount_to_scroll = 1;
12251 }
12252 }
12253
12254 if (amount_to_scroll <= 0)
12255 return SCROLLING_FAILED;
12256
12257 /* If moving by amount_to_scroll leaves STARTP unchanged,
12258 move it down one screen line. */
12259
12260 move_it_vertically (&it, amount_to_scroll);
12261 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12262 move_it_by_lines (&it, 1, 1);
12263 startp = it.current.pos;
12264 }
12265 else
12266 {
12267 /* See if point is inside the scroll margin at the top of the
12268 window. */
12269 scroll_margin_pos = startp;
12270 if (this_scroll_margin)
12271 {
12272 start_display (&it, w, startp);
12273 move_it_vertically (&it, this_scroll_margin);
12274 scroll_margin_pos = it.current.pos;
12275 }
12276
12277 if (PT < CHARPOS (scroll_margin_pos))
12278 {
12279 /* Point is in the scroll margin at the top of the window or
12280 above what is displayed in the window. */
12281 int y0;
12282
12283 /* Compute the vertical distance from PT to the scroll
12284 margin position. Give up if distance is greater than
12285 scroll_max. */
12286 SET_TEXT_POS (pos, PT, PT_BYTE);
12287 start_display (&it, w, pos);
12288 y0 = it.current_y;
12289 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12290 it.last_visible_y, -1,
12291 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12292 dy = it.current_y - y0;
12293 if (dy > scroll_max)
12294 return SCROLLING_FAILED;
12295
12296 /* Compute new window start. */
12297 start_display (&it, w, startp);
12298
12299 if (scroll_conservatively)
12300 amount_to_scroll
12301 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12302 else if (scroll_step || temp_scroll_step)
12303 amount_to_scroll = scroll_max;
12304 else
12305 {
12306 aggressive = current_buffer->scroll_down_aggressively;
12307 height = WINDOW_BOX_TEXT_HEIGHT (w);
12308 if (NUMBERP (aggressive))
12309 {
12310 double float_amount = XFLOATINT (aggressive) * height;
12311 amount_to_scroll = float_amount;
12312 if (amount_to_scroll == 0 && float_amount > 0)
12313 amount_to_scroll = 1;
12314 }
12315 }
12316
12317 if (amount_to_scroll <= 0)
12318 return SCROLLING_FAILED;
12319
12320 move_it_vertically_backward (&it, amount_to_scroll);
12321 startp = it.current.pos;
12322 }
12323 }
12324
12325 /* Run window scroll functions. */
12326 startp = run_window_scroll_functions (window, startp);
12327
12328 /* Display the window. Give up if new fonts are loaded, or if point
12329 doesn't appear. */
12330 if (!try_window (window, startp, 0))
12331 rc = SCROLLING_NEED_LARGER_MATRICES;
12332 else if (w->cursor.vpos < 0)
12333 {
12334 clear_glyph_matrix (w->desired_matrix);
12335 rc = SCROLLING_FAILED;
12336 }
12337 else
12338 {
12339 /* Maybe forget recorded base line for line number display. */
12340 if (!just_this_one_p
12341 || current_buffer->clip_changed
12342 || BEG_UNCHANGED < CHARPOS (startp))
12343 w->base_line_number = Qnil;
12344
12345 /* If cursor ends up on a partially visible line,
12346 treat that as being off the bottom of the screen. */
12347 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12348 {
12349 clear_glyph_matrix (w->desired_matrix);
12350 ++extra_scroll_margin_lines;
12351 goto too_near_end;
12352 }
12353 rc = SCROLLING_SUCCESS;
12354 }
12355
12356 return rc;
12357 }
12358
12359
12360 /* Compute a suitable window start for window W if display of W starts
12361 on a continuation line. Value is non-zero if a new window start
12362 was computed.
12363
12364 The new window start will be computed, based on W's width, starting
12365 from the start of the continued line. It is the start of the
12366 screen line with the minimum distance from the old start W->start. */
12367
12368 static int
12369 compute_window_start_on_continuation_line (w)
12370 struct window *w;
12371 {
12372 struct text_pos pos, start_pos;
12373 int window_start_changed_p = 0;
12374
12375 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12376
12377 /* If window start is on a continuation line... Window start may be
12378 < BEGV in case there's invisible text at the start of the
12379 buffer (M-x rmail, for example). */
12380 if (CHARPOS (start_pos) > BEGV
12381 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12382 {
12383 struct it it;
12384 struct glyph_row *row;
12385
12386 /* Handle the case that the window start is out of range. */
12387 if (CHARPOS (start_pos) < BEGV)
12388 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12389 else if (CHARPOS (start_pos) > ZV)
12390 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12391
12392 /* Find the start of the continued line. This should be fast
12393 because scan_buffer is fast (newline cache). */
12394 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12395 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12396 row, DEFAULT_FACE_ID);
12397 reseat_at_previous_visible_line_start (&it);
12398
12399 /* If the line start is "too far" away from the window start,
12400 say it takes too much time to compute a new window start. */
12401 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12402 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12403 {
12404 int min_distance, distance;
12405
12406 /* Move forward by display lines to find the new window
12407 start. If window width was enlarged, the new start can
12408 be expected to be > the old start. If window width was
12409 decreased, the new window start will be < the old start.
12410 So, we're looking for the display line start with the
12411 minimum distance from the old window start. */
12412 pos = it.current.pos;
12413 min_distance = INFINITY;
12414 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12415 distance < min_distance)
12416 {
12417 min_distance = distance;
12418 pos = it.current.pos;
12419 move_it_by_lines (&it, 1, 0);
12420 }
12421
12422 /* Set the window start there. */
12423 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12424 window_start_changed_p = 1;
12425 }
12426 }
12427
12428 return window_start_changed_p;
12429 }
12430
12431
12432 /* Try cursor movement in case text has not changed in window WINDOW,
12433 with window start STARTP. Value is
12434
12435 CURSOR_MOVEMENT_SUCCESS if successful
12436
12437 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12438
12439 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12440 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12441 we want to scroll as if scroll-step were set to 1. See the code.
12442
12443 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12444 which case we have to abort this redisplay, and adjust matrices
12445 first. */
12446
12447 enum
12448 {
12449 CURSOR_MOVEMENT_SUCCESS,
12450 CURSOR_MOVEMENT_CANNOT_BE_USED,
12451 CURSOR_MOVEMENT_MUST_SCROLL,
12452 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12453 };
12454
12455 static int
12456 try_cursor_movement (window, startp, scroll_step)
12457 Lisp_Object window;
12458 struct text_pos startp;
12459 int *scroll_step;
12460 {
12461 struct window *w = XWINDOW (window);
12462 struct frame *f = XFRAME (w->frame);
12463 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12464
12465 #if GLYPH_DEBUG
12466 if (inhibit_try_cursor_movement)
12467 return rc;
12468 #endif
12469
12470 /* Handle case where text has not changed, only point, and it has
12471 not moved off the frame. */
12472 if (/* Point may be in this window. */
12473 PT >= CHARPOS (startp)
12474 /* Selective display hasn't changed. */
12475 && !current_buffer->clip_changed
12476 /* Function force-mode-line-update is used to force a thorough
12477 redisplay. It sets either windows_or_buffers_changed or
12478 update_mode_lines. So don't take a shortcut here for these
12479 cases. */
12480 && !update_mode_lines
12481 && !windows_or_buffers_changed
12482 && !cursor_type_changed
12483 /* Can't use this case if highlighting a region. When a
12484 region exists, cursor movement has to do more than just
12485 set the cursor. */
12486 && !(!NILP (Vtransient_mark_mode)
12487 && !NILP (current_buffer->mark_active))
12488 && NILP (w->region_showing)
12489 && NILP (Vshow_trailing_whitespace)
12490 /* Right after splitting windows, last_point may be nil. */
12491 && INTEGERP (w->last_point)
12492 /* This code is not used for mini-buffer for the sake of the case
12493 of redisplaying to replace an echo area message; since in
12494 that case the mini-buffer contents per se are usually
12495 unchanged. This code is of no real use in the mini-buffer
12496 since the handling of this_line_start_pos, etc., in redisplay
12497 handles the same cases. */
12498 && !EQ (window, minibuf_window)
12499 /* When splitting windows or for new windows, it happens that
12500 redisplay is called with a nil window_end_vpos or one being
12501 larger than the window. This should really be fixed in
12502 window.c. I don't have this on my list, now, so we do
12503 approximately the same as the old redisplay code. --gerd. */
12504 && INTEGERP (w->window_end_vpos)
12505 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12506 && (FRAME_WINDOW_P (f)
12507 || !overlay_arrow_in_current_buffer_p ()))
12508 {
12509 int this_scroll_margin, top_scroll_margin;
12510 struct glyph_row *row = NULL;
12511
12512 #if GLYPH_DEBUG
12513 debug_method_add (w, "cursor movement");
12514 #endif
12515
12516 /* Scroll if point within this distance from the top or bottom
12517 of the window. This is a pixel value. */
12518 this_scroll_margin = max (0, scroll_margin);
12519 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12520 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12521
12522 top_scroll_margin = this_scroll_margin;
12523 if (WINDOW_WANTS_HEADER_LINE_P (w))
12524 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12525
12526 /* Start with the row the cursor was displayed during the last
12527 not paused redisplay. Give up if that row is not valid. */
12528 if (w->last_cursor.vpos < 0
12529 || w->last_cursor.vpos >= w->current_matrix->nrows)
12530 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12531 else
12532 {
12533 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12534 if (row->mode_line_p)
12535 ++row;
12536 if (!row->enabled_p)
12537 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12538 }
12539
12540 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12541 {
12542 int scroll_p = 0;
12543 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12544
12545 if (PT > XFASTINT (w->last_point))
12546 {
12547 /* Point has moved forward. */
12548 while (MATRIX_ROW_END_CHARPOS (row) < PT
12549 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12550 {
12551 xassert (row->enabled_p);
12552 ++row;
12553 }
12554
12555 /* The end position of a row equals the start position
12556 of the next row. If PT is there, we would rather
12557 display it in the next line. */
12558 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12559 && MATRIX_ROW_END_CHARPOS (row) == PT
12560 && !cursor_row_p (w, row))
12561 ++row;
12562
12563 /* If within the scroll margin, scroll. Note that
12564 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12565 the next line would be drawn, and that
12566 this_scroll_margin can be zero. */
12567 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12568 || PT > MATRIX_ROW_END_CHARPOS (row)
12569 /* Line is completely visible last line in window
12570 and PT is to be set in the next line. */
12571 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12572 && PT == MATRIX_ROW_END_CHARPOS (row)
12573 && !row->ends_at_zv_p
12574 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12575 scroll_p = 1;
12576 }
12577 else if (PT < XFASTINT (w->last_point))
12578 {
12579 /* Cursor has to be moved backward. Note that PT >=
12580 CHARPOS (startp) because of the outer if-statement. */
12581 while (!row->mode_line_p
12582 && (MATRIX_ROW_START_CHARPOS (row) > PT
12583 || (MATRIX_ROW_START_CHARPOS (row) == PT
12584 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12585 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12586 row > w->current_matrix->rows
12587 && (row-1)->ends_in_newline_from_string_p))))
12588 && (row->y > top_scroll_margin
12589 || CHARPOS (startp) == BEGV))
12590 {
12591 xassert (row->enabled_p);
12592 --row;
12593 }
12594
12595 /* Consider the following case: Window starts at BEGV,
12596 there is invisible, intangible text at BEGV, so that
12597 display starts at some point START > BEGV. It can
12598 happen that we are called with PT somewhere between
12599 BEGV and START. Try to handle that case. */
12600 if (row < w->current_matrix->rows
12601 || row->mode_line_p)
12602 {
12603 row = w->current_matrix->rows;
12604 if (row->mode_line_p)
12605 ++row;
12606 }
12607
12608 /* Due to newlines in overlay strings, we may have to
12609 skip forward over overlay strings. */
12610 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12611 && MATRIX_ROW_END_CHARPOS (row) == PT
12612 && !cursor_row_p (w, row))
12613 ++row;
12614
12615 /* If within the scroll margin, scroll. */
12616 if (row->y < top_scroll_margin
12617 && CHARPOS (startp) != BEGV)
12618 scroll_p = 1;
12619 }
12620 else
12621 {
12622 /* Cursor did not move. So don't scroll even if cursor line
12623 is partially visible, as it was so before. */
12624 rc = CURSOR_MOVEMENT_SUCCESS;
12625 }
12626
12627 if (PT < MATRIX_ROW_START_CHARPOS (row)
12628 || PT > MATRIX_ROW_END_CHARPOS (row))
12629 {
12630 /* if PT is not in the glyph row, give up. */
12631 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12632 }
12633 else if (rc != CURSOR_MOVEMENT_SUCCESS
12634 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12635 && make_cursor_line_fully_visible_p)
12636 {
12637 if (PT == MATRIX_ROW_END_CHARPOS (row)
12638 && !row->ends_at_zv_p
12639 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12640 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12641 else if (row->height > window_box_height (w))
12642 {
12643 /* If we end up in a partially visible line, let's
12644 make it fully visible, except when it's taller
12645 than the window, in which case we can't do much
12646 about it. */
12647 *scroll_step = 1;
12648 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12649 }
12650 else
12651 {
12652 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12653 if (!cursor_row_fully_visible_p (w, 0, 1))
12654 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12655 else
12656 rc = CURSOR_MOVEMENT_SUCCESS;
12657 }
12658 }
12659 else if (scroll_p)
12660 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12661 else
12662 {
12663 do
12664 {
12665 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12666 {
12667 rc = CURSOR_MOVEMENT_SUCCESS;
12668 break;
12669 }
12670 ++row;
12671 }
12672 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12673 && MATRIX_ROW_START_CHARPOS (row) == PT
12674 && cursor_row_p (w, row));
12675 }
12676 }
12677 }
12678
12679 return rc;
12680 }
12681
12682 void
12683 set_vertical_scroll_bar (w)
12684 struct window *w;
12685 {
12686 int start, end, whole;
12687
12688 /* Calculate the start and end positions for the current window.
12689 At some point, it would be nice to choose between scrollbars
12690 which reflect the whole buffer size, with special markers
12691 indicating narrowing, and scrollbars which reflect only the
12692 visible region.
12693
12694 Note that mini-buffers sometimes aren't displaying any text. */
12695 if (!MINI_WINDOW_P (w)
12696 || (w == XWINDOW (minibuf_window)
12697 && NILP (echo_area_buffer[0])))
12698 {
12699 struct buffer *buf = XBUFFER (w->buffer);
12700 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12701 start = marker_position (w->start) - BUF_BEGV (buf);
12702 /* I don't think this is guaranteed to be right. For the
12703 moment, we'll pretend it is. */
12704 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12705
12706 if (end < start)
12707 end = start;
12708 if (whole < (end - start))
12709 whole = end - start;
12710 }
12711 else
12712 start = end = whole = 0;
12713
12714 /* Indicate what this scroll bar ought to be displaying now. */
12715 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12716 }
12717
12718
12719 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12720 selected_window is redisplayed.
12721
12722 We can return without actually redisplaying the window if
12723 fonts_changed_p is nonzero. In that case, redisplay_internal will
12724 retry. */
12725
12726 static void
12727 redisplay_window (window, just_this_one_p)
12728 Lisp_Object window;
12729 int just_this_one_p;
12730 {
12731 struct window *w = XWINDOW (window);
12732 struct frame *f = XFRAME (w->frame);
12733 struct buffer *buffer = XBUFFER (w->buffer);
12734 struct buffer *old = current_buffer;
12735 struct text_pos lpoint, opoint, startp;
12736 int update_mode_line;
12737 int tem;
12738 struct it it;
12739 /* Record it now because it's overwritten. */
12740 int current_matrix_up_to_date_p = 0;
12741 int used_current_matrix_p = 0;
12742 /* This is less strict than current_matrix_up_to_date_p.
12743 It indictes that the buffer contents and narrowing are unchanged. */
12744 int buffer_unchanged_p = 0;
12745 int temp_scroll_step = 0;
12746 int count = SPECPDL_INDEX ();
12747 int rc;
12748 int centering_position = -1;
12749 int last_line_misfit = 0;
12750
12751 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12752 opoint = lpoint;
12753
12754 /* W must be a leaf window here. */
12755 xassert (!NILP (w->buffer));
12756 #if GLYPH_DEBUG
12757 *w->desired_matrix->method = 0;
12758 #endif
12759
12760 specbind (Qinhibit_point_motion_hooks, Qt);
12761
12762 reconsider_clip_changes (w, buffer);
12763
12764 /* Has the mode line to be updated? */
12765 update_mode_line = (!NILP (w->update_mode_line)
12766 || update_mode_lines
12767 || buffer->clip_changed
12768 || buffer->prevent_redisplay_optimizations_p);
12769
12770 if (MINI_WINDOW_P (w))
12771 {
12772 if (w == XWINDOW (echo_area_window)
12773 && !NILP (echo_area_buffer[0]))
12774 {
12775 if (update_mode_line)
12776 /* We may have to update a tty frame's menu bar or a
12777 tool-bar. Example `M-x C-h C-h C-g'. */
12778 goto finish_menu_bars;
12779 else
12780 /* We've already displayed the echo area glyphs in this window. */
12781 goto finish_scroll_bars;
12782 }
12783 else if ((w != XWINDOW (minibuf_window)
12784 || minibuf_level == 0)
12785 /* When buffer is nonempty, redisplay window normally. */
12786 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12787 /* Quail displays non-mini buffers in minibuffer window.
12788 In that case, redisplay the window normally. */
12789 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12790 {
12791 /* W is a mini-buffer window, but it's not active, so clear
12792 it. */
12793 int yb = window_text_bottom_y (w);
12794 struct glyph_row *row;
12795 int y;
12796
12797 for (y = 0, row = w->desired_matrix->rows;
12798 y < yb;
12799 y += row->height, ++row)
12800 blank_row (w, row, y);
12801 goto finish_scroll_bars;
12802 }
12803
12804 clear_glyph_matrix (w->desired_matrix);
12805 }
12806
12807 /* Otherwise set up data on this window; select its buffer and point
12808 value. */
12809 /* Really select the buffer, for the sake of buffer-local
12810 variables. */
12811 set_buffer_internal_1 (XBUFFER (w->buffer));
12812 SET_TEXT_POS (opoint, PT, PT_BYTE);
12813
12814 current_matrix_up_to_date_p
12815 = (!NILP (w->window_end_valid)
12816 && !current_buffer->clip_changed
12817 && !current_buffer->prevent_redisplay_optimizations_p
12818 && XFASTINT (w->last_modified) >= MODIFF
12819 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12820
12821 buffer_unchanged_p
12822 = (!NILP (w->window_end_valid)
12823 && !current_buffer->clip_changed
12824 && XFASTINT (w->last_modified) >= MODIFF
12825 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12826
12827 /* When windows_or_buffers_changed is non-zero, we can't rely on
12828 the window end being valid, so set it to nil there. */
12829 if (windows_or_buffers_changed)
12830 {
12831 /* If window starts on a continuation line, maybe adjust the
12832 window start in case the window's width changed. */
12833 if (XMARKER (w->start)->buffer == current_buffer)
12834 compute_window_start_on_continuation_line (w);
12835
12836 w->window_end_valid = Qnil;
12837 }
12838
12839 /* Some sanity checks. */
12840 CHECK_WINDOW_END (w);
12841 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12842 abort ();
12843 if (BYTEPOS (opoint) < CHARPOS (opoint))
12844 abort ();
12845
12846 /* If %c is in mode line, update it if needed. */
12847 if (!NILP (w->column_number_displayed)
12848 /* This alternative quickly identifies a common case
12849 where no change is needed. */
12850 && !(PT == XFASTINT (w->last_point)
12851 && XFASTINT (w->last_modified) >= MODIFF
12852 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12853 && (XFASTINT (w->column_number_displayed)
12854 != (int) current_column ())) /* iftc */
12855 update_mode_line = 1;
12856
12857 /* Count number of windows showing the selected buffer. An indirect
12858 buffer counts as its base buffer. */
12859 if (!just_this_one_p)
12860 {
12861 struct buffer *current_base, *window_base;
12862 current_base = current_buffer;
12863 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12864 if (current_base->base_buffer)
12865 current_base = current_base->base_buffer;
12866 if (window_base->base_buffer)
12867 window_base = window_base->base_buffer;
12868 if (current_base == window_base)
12869 buffer_shared++;
12870 }
12871
12872 /* Point refers normally to the selected window. For any other
12873 window, set up appropriate value. */
12874 if (!EQ (window, selected_window))
12875 {
12876 int new_pt = XMARKER (w->pointm)->charpos;
12877 int new_pt_byte = marker_byte_position (w->pointm);
12878 if (new_pt < BEGV)
12879 {
12880 new_pt = BEGV;
12881 new_pt_byte = BEGV_BYTE;
12882 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12883 }
12884 else if (new_pt > (ZV - 1))
12885 {
12886 new_pt = ZV;
12887 new_pt_byte = ZV_BYTE;
12888 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12889 }
12890
12891 /* We don't use SET_PT so that the point-motion hooks don't run. */
12892 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12893 }
12894
12895 /* If any of the character widths specified in the display table
12896 have changed, invalidate the width run cache. It's true that
12897 this may be a bit late to catch such changes, but the rest of
12898 redisplay goes (non-fatally) haywire when the display table is
12899 changed, so why should we worry about doing any better? */
12900 if (current_buffer->width_run_cache)
12901 {
12902 struct Lisp_Char_Table *disptab = buffer_display_table ();
12903
12904 if (! disptab_matches_widthtab (disptab,
12905 XVECTOR (current_buffer->width_table)))
12906 {
12907 invalidate_region_cache (current_buffer,
12908 current_buffer->width_run_cache,
12909 BEG, Z);
12910 recompute_width_table (current_buffer, disptab);
12911 }
12912 }
12913
12914 /* If window-start is screwed up, choose a new one. */
12915 if (XMARKER (w->start)->buffer != current_buffer)
12916 goto recenter;
12917
12918 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12919
12920 /* If someone specified a new starting point but did not insist,
12921 check whether it can be used. */
12922 if (!NILP (w->optional_new_start)
12923 && CHARPOS (startp) >= BEGV
12924 && CHARPOS (startp) <= ZV)
12925 {
12926 w->optional_new_start = Qnil;
12927 start_display (&it, w, startp);
12928 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12929 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12930 if (IT_CHARPOS (it) == PT)
12931 w->force_start = Qt;
12932 /* IT may overshoot PT if text at PT is invisible. */
12933 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12934 w->force_start = Qt;
12935 }
12936
12937 /* Handle case where place to start displaying has been specified,
12938 unless the specified location is outside the accessible range. */
12939 if (!NILP (w->force_start)
12940 || w->frozen_window_start_p)
12941 {
12942 /* We set this later on if we have to adjust point. */
12943 int new_vpos = -1;
12944 int val;
12945
12946 w->force_start = Qnil;
12947 w->vscroll = 0;
12948 w->window_end_valid = Qnil;
12949
12950 /* Forget any recorded base line for line number display. */
12951 if (!buffer_unchanged_p)
12952 w->base_line_number = Qnil;
12953
12954 /* Redisplay the mode line. Select the buffer properly for that.
12955 Also, run the hook window-scroll-functions
12956 because we have scrolled. */
12957 /* Note, we do this after clearing force_start because
12958 if there's an error, it is better to forget about force_start
12959 than to get into an infinite loop calling the hook functions
12960 and having them get more errors. */
12961 if (!update_mode_line
12962 || ! NILP (Vwindow_scroll_functions))
12963 {
12964 update_mode_line = 1;
12965 w->update_mode_line = Qt;
12966 startp = run_window_scroll_functions (window, startp);
12967 }
12968
12969 w->last_modified = make_number (0);
12970 w->last_overlay_modified = make_number (0);
12971 if (CHARPOS (startp) < BEGV)
12972 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12973 else if (CHARPOS (startp) > ZV)
12974 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12975
12976 /* Redisplay, then check if cursor has been set during the
12977 redisplay. Give up if new fonts were loaded. */
12978 val = try_window (window, startp, 1);
12979 if (!val)
12980 {
12981 w->force_start = Qt;
12982 clear_glyph_matrix (w->desired_matrix);
12983 goto need_larger_matrices;
12984 }
12985 /* Point was outside the scroll margins. */
12986 if (val < 0)
12987 new_vpos = window_box_height (w) / 2;
12988
12989 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12990 {
12991 /* If point does not appear, try to move point so it does
12992 appear. The desired matrix has been built above, so we
12993 can use it here. */
12994 new_vpos = window_box_height (w) / 2;
12995 }
12996
12997 if (!cursor_row_fully_visible_p (w, 0, 0))
12998 {
12999 /* Point does appear, but on a line partly visible at end of window.
13000 Move it back to a fully-visible line. */
13001 new_vpos = window_box_height (w);
13002 }
13003
13004 /* If we need to move point for either of the above reasons,
13005 now actually do it. */
13006 if (new_vpos >= 0)
13007 {
13008 struct glyph_row *row;
13009
13010 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
13011 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
13012 ++row;
13013
13014 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
13015 MATRIX_ROW_START_BYTEPOS (row));
13016
13017 if (w != XWINDOW (selected_window))
13018 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
13019 else if (current_buffer == old)
13020 SET_TEXT_POS (lpoint, PT, PT_BYTE);
13021
13022 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
13023
13024 /* If we are highlighting the region, then we just changed
13025 the region, so redisplay to show it. */
13026 if (!NILP (Vtransient_mark_mode)
13027 && !NILP (current_buffer->mark_active))
13028 {
13029 clear_glyph_matrix (w->desired_matrix);
13030 if (!try_window (window, startp, 0))
13031 goto need_larger_matrices;
13032 }
13033 }
13034
13035 #if GLYPH_DEBUG
13036 debug_method_add (w, "forced window start");
13037 #endif
13038 goto done;
13039 }
13040
13041 /* Handle case where text has not changed, only point, and it has
13042 not moved off the frame, and we are not retrying after hscroll.
13043 (current_matrix_up_to_date_p is nonzero when retrying.) */
13044 if (current_matrix_up_to_date_p
13045 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13046 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13047 {
13048 switch (rc)
13049 {
13050 case CURSOR_MOVEMENT_SUCCESS:
13051 used_current_matrix_p = 1;
13052 goto done;
13053
13054 #if 0 /* try_cursor_movement never returns this value. */
13055 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13056 goto need_larger_matrices;
13057 #endif
13058
13059 case CURSOR_MOVEMENT_MUST_SCROLL:
13060 goto try_to_scroll;
13061
13062 default:
13063 abort ();
13064 }
13065 }
13066 /* If current starting point was originally the beginning of a line
13067 but no longer is, find a new starting point. */
13068 else if (!NILP (w->start_at_line_beg)
13069 && !(CHARPOS (startp) <= BEGV
13070 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13071 {
13072 #if GLYPH_DEBUG
13073 debug_method_add (w, "recenter 1");
13074 #endif
13075 goto recenter;
13076 }
13077
13078 /* Try scrolling with try_window_id. Value is > 0 if update has
13079 been done, it is -1 if we know that the same window start will
13080 not work. It is 0 if unsuccessful for some other reason. */
13081 else if ((tem = try_window_id (w)) != 0)
13082 {
13083 #if GLYPH_DEBUG
13084 debug_method_add (w, "try_window_id %d", tem);
13085 #endif
13086
13087 if (fonts_changed_p)
13088 goto need_larger_matrices;
13089 if (tem > 0)
13090 goto done;
13091
13092 /* Otherwise try_window_id has returned -1 which means that we
13093 don't want the alternative below this comment to execute. */
13094 }
13095 else if (CHARPOS (startp) >= BEGV
13096 && CHARPOS (startp) <= ZV
13097 && PT >= CHARPOS (startp)
13098 && (CHARPOS (startp) < ZV
13099 /* Avoid starting at end of buffer. */
13100 || CHARPOS (startp) == BEGV
13101 || (XFASTINT (w->last_modified) >= MODIFF
13102 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13103 {
13104
13105 /* If first window line is a continuation line, and window start
13106 is inside the modified region, but the first change is before
13107 current window start, we must select a new window start.*/
13108 if (NILP (w->start_at_line_beg)
13109 && CHARPOS (startp) > BEGV)
13110 {
13111 /* Make sure beg_unchanged and end_unchanged are up to date.
13112 Do it only if buffer has really changed. This may or may
13113 not have been done by try_window_id (see which) already. */
13114 if (MODIFF > SAVE_MODIFF
13115 /* This seems to happen sometimes after saving a buffer. */
13116 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13117 {
13118 if (GPT - BEG < BEG_UNCHANGED)
13119 BEG_UNCHANGED = GPT - BEG;
13120 if (Z - GPT < END_UNCHANGED)
13121 END_UNCHANGED = Z - GPT;
13122 }
13123
13124 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13125 && CHARPOS (startp) <= Z - END_UNCHANGED)
13126 {
13127 /* There doesn't seems to be a simple way to find a new
13128 window start that is near the old window start, so
13129 we just recenter. */
13130 goto recenter;
13131 }
13132 }
13133
13134 #if GLYPH_DEBUG
13135 debug_method_add (w, "same window start");
13136 #endif
13137
13138 /* Try to redisplay starting at same place as before.
13139 If point has not moved off frame, accept the results. */
13140 if (!current_matrix_up_to_date_p
13141 /* Don't use try_window_reusing_current_matrix in this case
13142 because a window scroll function can have changed the
13143 buffer. */
13144 || !NILP (Vwindow_scroll_functions)
13145 || MINI_WINDOW_P (w)
13146 || !(used_current_matrix_p
13147 = try_window_reusing_current_matrix (w)))
13148 {
13149 IF_DEBUG (debug_method_add (w, "1"));
13150 if (try_window (window, startp, 1) < 0)
13151 /* -1 means we need to scroll.
13152 0 means we need new matrices, but fonts_changed_p
13153 is set in that case, so we will detect it below. */
13154 goto try_to_scroll;
13155 }
13156
13157 if (fonts_changed_p)
13158 goto need_larger_matrices;
13159
13160 if (w->cursor.vpos >= 0)
13161 {
13162 if (!just_this_one_p
13163 || current_buffer->clip_changed
13164 || BEG_UNCHANGED < CHARPOS (startp))
13165 /* Forget any recorded base line for line number display. */
13166 w->base_line_number = Qnil;
13167
13168 if (!cursor_row_fully_visible_p (w, 1, 0))
13169 {
13170 clear_glyph_matrix (w->desired_matrix);
13171 last_line_misfit = 1;
13172 }
13173 /* Drop through and scroll. */
13174 else
13175 goto done;
13176 }
13177 else
13178 clear_glyph_matrix (w->desired_matrix);
13179 }
13180
13181 try_to_scroll:
13182
13183 w->last_modified = make_number (0);
13184 w->last_overlay_modified = make_number (0);
13185
13186 /* Redisplay the mode line. Select the buffer properly for that. */
13187 if (!update_mode_line)
13188 {
13189 update_mode_line = 1;
13190 w->update_mode_line = Qt;
13191 }
13192
13193 /* Try to scroll by specified few lines. */
13194 if ((scroll_conservatively
13195 || scroll_step
13196 || temp_scroll_step
13197 || NUMBERP (current_buffer->scroll_up_aggressively)
13198 || NUMBERP (current_buffer->scroll_down_aggressively))
13199 && !current_buffer->clip_changed
13200 && CHARPOS (startp) >= BEGV
13201 && CHARPOS (startp) <= ZV)
13202 {
13203 /* The function returns -1 if new fonts were loaded, 1 if
13204 successful, 0 if not successful. */
13205 int rc = try_scrolling (window, just_this_one_p,
13206 scroll_conservatively,
13207 scroll_step,
13208 temp_scroll_step, last_line_misfit);
13209 switch (rc)
13210 {
13211 case SCROLLING_SUCCESS:
13212 goto done;
13213
13214 case SCROLLING_NEED_LARGER_MATRICES:
13215 goto need_larger_matrices;
13216
13217 case SCROLLING_FAILED:
13218 break;
13219
13220 default:
13221 abort ();
13222 }
13223 }
13224
13225 /* Finally, just choose place to start which centers point */
13226
13227 recenter:
13228 if (centering_position < 0)
13229 centering_position = window_box_height (w) / 2;
13230
13231 #if GLYPH_DEBUG
13232 debug_method_add (w, "recenter");
13233 #endif
13234
13235 /* w->vscroll = 0; */
13236
13237 /* Forget any previously recorded base line for line number display. */
13238 if (!buffer_unchanged_p)
13239 w->base_line_number = Qnil;
13240
13241 /* Move backward half the height of the window. */
13242 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13243 it.current_y = it.last_visible_y;
13244 move_it_vertically_backward (&it, centering_position);
13245 xassert (IT_CHARPOS (it) >= BEGV);
13246
13247 /* The function move_it_vertically_backward may move over more
13248 than the specified y-distance. If it->w is small, e.g. a
13249 mini-buffer window, we may end up in front of the window's
13250 display area. Start displaying at the start of the line
13251 containing PT in this case. */
13252 if (it.current_y <= 0)
13253 {
13254 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13255 move_it_vertically_backward (&it, 0);
13256 #if 0
13257 /* I think this assert is bogus if buffer contains
13258 invisible text or images. KFS. */
13259 xassert (IT_CHARPOS (it) <= PT);
13260 #endif
13261 it.current_y = 0;
13262 }
13263
13264 it.current_x = it.hpos = 0;
13265
13266 /* Set startp here explicitly in case that helps avoid an infinite loop
13267 in case the window-scroll-functions functions get errors. */
13268 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13269
13270 /* Run scroll hooks. */
13271 startp = run_window_scroll_functions (window, it.current.pos);
13272
13273 /* Redisplay the window. */
13274 if (!current_matrix_up_to_date_p
13275 || windows_or_buffers_changed
13276 || cursor_type_changed
13277 /* Don't use try_window_reusing_current_matrix in this case
13278 because it can have changed the buffer. */
13279 || !NILP (Vwindow_scroll_functions)
13280 || !just_this_one_p
13281 || MINI_WINDOW_P (w)
13282 || !(used_current_matrix_p
13283 = try_window_reusing_current_matrix (w)))
13284 try_window (window, startp, 0);
13285
13286 /* If new fonts have been loaded (due to fontsets), give up. We
13287 have to start a new redisplay since we need to re-adjust glyph
13288 matrices. */
13289 if (fonts_changed_p)
13290 goto need_larger_matrices;
13291
13292 /* If cursor did not appear assume that the middle of the window is
13293 in the first line of the window. Do it again with the next line.
13294 (Imagine a window of height 100, displaying two lines of height
13295 60. Moving back 50 from it->last_visible_y will end in the first
13296 line.) */
13297 if (w->cursor.vpos < 0)
13298 {
13299 if (!NILP (w->window_end_valid)
13300 && PT >= Z - XFASTINT (w->window_end_pos))
13301 {
13302 clear_glyph_matrix (w->desired_matrix);
13303 move_it_by_lines (&it, 1, 0);
13304 try_window (window, it.current.pos, 0);
13305 }
13306 else if (PT < IT_CHARPOS (it))
13307 {
13308 clear_glyph_matrix (w->desired_matrix);
13309 move_it_by_lines (&it, -1, 0);
13310 try_window (window, it.current.pos, 0);
13311 }
13312 else
13313 {
13314 /* Not much we can do about it. */
13315 }
13316 }
13317
13318 /* Consider the following case: Window starts at BEGV, there is
13319 invisible, intangible text at BEGV, so that display starts at
13320 some point START > BEGV. It can happen that we are called with
13321 PT somewhere between BEGV and START. Try to handle that case. */
13322 if (w->cursor.vpos < 0)
13323 {
13324 struct glyph_row *row = w->current_matrix->rows;
13325 if (row->mode_line_p)
13326 ++row;
13327 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13328 }
13329
13330 if (!cursor_row_fully_visible_p (w, 0, 0))
13331 {
13332 /* If vscroll is enabled, disable it and try again. */
13333 if (w->vscroll)
13334 {
13335 w->vscroll = 0;
13336 clear_glyph_matrix (w->desired_matrix);
13337 goto recenter;
13338 }
13339
13340 /* If centering point failed to make the whole line visible,
13341 put point at the top instead. That has to make the whole line
13342 visible, if it can be done. */
13343 if (centering_position == 0)
13344 goto done;
13345
13346 clear_glyph_matrix (w->desired_matrix);
13347 centering_position = 0;
13348 goto recenter;
13349 }
13350
13351 done:
13352
13353 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13354 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13355 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13356 ? Qt : Qnil);
13357
13358 /* Display the mode line, if we must. */
13359 if ((update_mode_line
13360 /* If window not full width, must redo its mode line
13361 if (a) the window to its side is being redone and
13362 (b) we do a frame-based redisplay. This is a consequence
13363 of how inverted lines are drawn in frame-based redisplay. */
13364 || (!just_this_one_p
13365 && !FRAME_WINDOW_P (f)
13366 && !WINDOW_FULL_WIDTH_P (w))
13367 /* Line number to display. */
13368 || INTEGERP (w->base_line_pos)
13369 /* Column number is displayed and different from the one displayed. */
13370 || (!NILP (w->column_number_displayed)
13371 && (XFASTINT (w->column_number_displayed)
13372 != (int) current_column ()))) /* iftc */
13373 /* This means that the window has a mode line. */
13374 && (WINDOW_WANTS_MODELINE_P (w)
13375 || WINDOW_WANTS_HEADER_LINE_P (w)))
13376 {
13377 display_mode_lines (w);
13378
13379 /* If mode line height has changed, arrange for a thorough
13380 immediate redisplay using the correct mode line height. */
13381 if (WINDOW_WANTS_MODELINE_P (w)
13382 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13383 {
13384 fonts_changed_p = 1;
13385 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13386 = DESIRED_MODE_LINE_HEIGHT (w);
13387 }
13388
13389 /* If top line height has changed, arrange for a thorough
13390 immediate redisplay using the correct mode line height. */
13391 if (WINDOW_WANTS_HEADER_LINE_P (w)
13392 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13393 {
13394 fonts_changed_p = 1;
13395 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13396 = DESIRED_HEADER_LINE_HEIGHT (w);
13397 }
13398
13399 if (fonts_changed_p)
13400 goto need_larger_matrices;
13401 }
13402
13403 if (!line_number_displayed
13404 && !BUFFERP (w->base_line_pos))
13405 {
13406 w->base_line_pos = Qnil;
13407 w->base_line_number = Qnil;
13408 }
13409
13410 finish_menu_bars:
13411
13412 /* When we reach a frame's selected window, redo the frame's menu bar. */
13413 if (update_mode_line
13414 && EQ (FRAME_SELECTED_WINDOW (f), window))
13415 {
13416 int redisplay_menu_p = 0;
13417 int redisplay_tool_bar_p = 0;
13418
13419 if (FRAME_WINDOW_P (f))
13420 {
13421 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13422 || defined (USE_GTK)
13423 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13424 #else
13425 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13426 #endif
13427 }
13428 else
13429 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13430
13431 if (redisplay_menu_p)
13432 display_menu_bar (w);
13433
13434 #ifdef HAVE_WINDOW_SYSTEM
13435 #ifdef USE_GTK
13436 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13437 #else
13438 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13439 && (FRAME_TOOL_BAR_LINES (f) > 0
13440 || auto_resize_tool_bars_p);
13441
13442 #endif
13443
13444 if (redisplay_tool_bar_p)
13445 redisplay_tool_bar (f);
13446 #endif
13447 }
13448
13449 #ifdef HAVE_WINDOW_SYSTEM
13450 if (FRAME_WINDOW_P (f)
13451 && update_window_fringes (w, (just_this_one_p
13452 || (!used_current_matrix_p && !overlay_arrow_seen)
13453 || w->pseudo_window_p)))
13454 {
13455 update_begin (f);
13456 BLOCK_INPUT;
13457 if (draw_window_fringes (w, 1))
13458 x_draw_vertical_border (w);
13459 UNBLOCK_INPUT;
13460 update_end (f);
13461 }
13462 #endif /* HAVE_WINDOW_SYSTEM */
13463
13464 /* We go to this label, with fonts_changed_p nonzero,
13465 if it is necessary to try again using larger glyph matrices.
13466 We have to redeem the scroll bar even in this case,
13467 because the loop in redisplay_internal expects that. */
13468 need_larger_matrices:
13469 ;
13470 finish_scroll_bars:
13471
13472 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13473 {
13474 /* Set the thumb's position and size. */
13475 set_vertical_scroll_bar (w);
13476
13477 /* Note that we actually used the scroll bar attached to this
13478 window, so it shouldn't be deleted at the end of redisplay. */
13479 redeem_scroll_bar_hook (w);
13480 }
13481
13482 /* Restore current_buffer and value of point in it. */
13483 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13484 set_buffer_internal_1 (old);
13485 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13486
13487 unbind_to (count, Qnil);
13488 }
13489
13490
13491 /* Build the complete desired matrix of WINDOW with a window start
13492 buffer position POS.
13493
13494 Value is 1 if successful. It is zero if fonts were loaded during
13495 redisplay which makes re-adjusting glyph matrices necessary, and -1
13496 if point would appear in the scroll margins.
13497 (We check that only if CHECK_MARGINS is nonzero. */
13498
13499 int
13500 try_window (window, pos, check_margins)
13501 Lisp_Object window;
13502 struct text_pos pos;
13503 int check_margins;
13504 {
13505 struct window *w = XWINDOW (window);
13506 struct it it;
13507 struct glyph_row *last_text_row = NULL;
13508
13509 /* Make POS the new window start. */
13510 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13511
13512 /* Mark cursor position as unknown. No overlay arrow seen. */
13513 w->cursor.vpos = -1;
13514 overlay_arrow_seen = 0;
13515
13516 /* Initialize iterator and info to start at POS. */
13517 start_display (&it, w, pos);
13518
13519 /* Display all lines of W. */
13520 while (it.current_y < it.last_visible_y)
13521 {
13522 if (display_line (&it))
13523 last_text_row = it.glyph_row - 1;
13524 if (fonts_changed_p)
13525 return 0;
13526 }
13527
13528 /* Don't let the cursor end in the scroll margins. */
13529 if (check_margins
13530 && !MINI_WINDOW_P (w))
13531 {
13532 int this_scroll_margin;
13533
13534 this_scroll_margin = max (0, scroll_margin);
13535 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13536 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13537
13538 if ((w->cursor.y >= 0 /* not vscrolled */
13539 && w->cursor.y < this_scroll_margin
13540 && CHARPOS (pos) > BEGV
13541 && IT_CHARPOS (it) < ZV)
13542 /* rms: considering make_cursor_line_fully_visible_p here
13543 seems to give wrong results. We don't want to recenter
13544 when the last line is partly visible, we want to allow
13545 that case to be handled in the usual way. */
13546 || (w->cursor.y + 1) > it.last_visible_y)
13547 {
13548 w->cursor.vpos = -1;
13549 clear_glyph_matrix (w->desired_matrix);
13550 return -1;
13551 }
13552 }
13553
13554 /* If bottom moved off end of frame, change mode line percentage. */
13555 if (XFASTINT (w->window_end_pos) <= 0
13556 && Z != IT_CHARPOS (it))
13557 w->update_mode_line = Qt;
13558
13559 /* Set window_end_pos to the offset of the last character displayed
13560 on the window from the end of current_buffer. Set
13561 window_end_vpos to its row number. */
13562 if (last_text_row)
13563 {
13564 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13565 w->window_end_bytepos
13566 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13567 w->window_end_pos
13568 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13569 w->window_end_vpos
13570 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13571 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13572 ->displays_text_p);
13573 }
13574 else
13575 {
13576 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13577 w->window_end_pos = make_number (Z - ZV);
13578 w->window_end_vpos = make_number (0);
13579 }
13580
13581 /* But that is not valid info until redisplay finishes. */
13582 w->window_end_valid = Qnil;
13583 return 1;
13584 }
13585
13586
13587 \f
13588 /************************************************************************
13589 Window redisplay reusing current matrix when buffer has not changed
13590 ************************************************************************/
13591
13592 /* Try redisplay of window W showing an unchanged buffer with a
13593 different window start than the last time it was displayed by
13594 reusing its current matrix. Value is non-zero if successful.
13595 W->start is the new window start. */
13596
13597 static int
13598 try_window_reusing_current_matrix (w)
13599 struct window *w;
13600 {
13601 struct frame *f = XFRAME (w->frame);
13602 struct glyph_row *row, *bottom_row;
13603 struct it it;
13604 struct run run;
13605 struct text_pos start, new_start;
13606 int nrows_scrolled, i;
13607 struct glyph_row *last_text_row;
13608 struct glyph_row *last_reused_text_row;
13609 struct glyph_row *start_row;
13610 int start_vpos, min_y, max_y;
13611
13612 #if GLYPH_DEBUG
13613 if (inhibit_try_window_reusing)
13614 return 0;
13615 #endif
13616
13617 if (/* This function doesn't handle terminal frames. */
13618 !FRAME_WINDOW_P (f)
13619 /* Don't try to reuse the display if windows have been split
13620 or such. */
13621 || windows_or_buffers_changed
13622 || cursor_type_changed)
13623 return 0;
13624
13625 /* Can't do this if region may have changed. */
13626 if ((!NILP (Vtransient_mark_mode)
13627 && !NILP (current_buffer->mark_active))
13628 || !NILP (w->region_showing)
13629 || !NILP (Vshow_trailing_whitespace))
13630 return 0;
13631
13632 /* If top-line visibility has changed, give up. */
13633 if (WINDOW_WANTS_HEADER_LINE_P (w)
13634 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13635 return 0;
13636
13637 /* Give up if old or new display is scrolled vertically. We could
13638 make this function handle this, but right now it doesn't. */
13639 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13640 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13641 return 0;
13642
13643 /* The variable new_start now holds the new window start. The old
13644 start `start' can be determined from the current matrix. */
13645 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13646 start = start_row->start.pos;
13647 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13648
13649 /* Clear the desired matrix for the display below. */
13650 clear_glyph_matrix (w->desired_matrix);
13651
13652 if (CHARPOS (new_start) <= CHARPOS (start))
13653 {
13654 int first_row_y;
13655
13656 /* Don't use this method if the display starts with an ellipsis
13657 displayed for invisible text. It's not easy to handle that case
13658 below, and it's certainly not worth the effort since this is
13659 not a frequent case. */
13660 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13661 return 0;
13662
13663 IF_DEBUG (debug_method_add (w, "twu1"));
13664
13665 /* Display up to a row that can be reused. The variable
13666 last_text_row is set to the last row displayed that displays
13667 text. Note that it.vpos == 0 if or if not there is a
13668 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13669 start_display (&it, w, new_start);
13670 first_row_y = it.current_y;
13671 w->cursor.vpos = -1;
13672 last_text_row = last_reused_text_row = NULL;
13673
13674 while (it.current_y < it.last_visible_y
13675 && !fonts_changed_p)
13676 {
13677 /* If we have reached into the characters in the START row,
13678 that means the line boundaries have changed. So we
13679 can't start copying with the row START. Maybe it will
13680 work to start copying with the following row. */
13681 while (IT_CHARPOS (it) > CHARPOS (start))
13682 {
13683 /* Advance to the next row as the "start". */
13684 start_row++;
13685 start = start_row->start.pos;
13686 /* If there are no more rows to try, or just one, give up. */
13687 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13688 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13689 || CHARPOS (start) == ZV)
13690 {
13691 clear_glyph_matrix (w->desired_matrix);
13692 return 0;
13693 }
13694
13695 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13696 }
13697 /* If we have reached alignment,
13698 we can copy the rest of the rows. */
13699 if (IT_CHARPOS (it) == CHARPOS (start))
13700 break;
13701
13702 if (display_line (&it))
13703 last_text_row = it.glyph_row - 1;
13704 }
13705
13706 /* A value of current_y < last_visible_y means that we stopped
13707 at the previous window start, which in turn means that we
13708 have at least one reusable row. */
13709 if (it.current_y < it.last_visible_y)
13710 {
13711 /* IT.vpos always starts from 0; it counts text lines. */
13712 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13713
13714 /* Find PT if not already found in the lines displayed. */
13715 if (w->cursor.vpos < 0)
13716 {
13717 int dy = it.current_y - start_row->y;
13718
13719 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13720 row = row_containing_pos (w, PT, row, NULL, dy);
13721 if (row)
13722 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13723 dy, nrows_scrolled);
13724 else
13725 {
13726 clear_glyph_matrix (w->desired_matrix);
13727 return 0;
13728 }
13729 }
13730
13731 /* Scroll the display. Do it before the current matrix is
13732 changed. The problem here is that update has not yet
13733 run, i.e. part of the current matrix is not up to date.
13734 scroll_run_hook will clear the cursor, and use the
13735 current matrix to get the height of the row the cursor is
13736 in. */
13737 run.current_y = start_row->y;
13738 run.desired_y = it.current_y;
13739 run.height = it.last_visible_y - it.current_y;
13740
13741 if (run.height > 0 && run.current_y != run.desired_y)
13742 {
13743 update_begin (f);
13744 rif->update_window_begin_hook (w);
13745 rif->clear_window_mouse_face (w);
13746 rif->scroll_run_hook (w, &run);
13747 rif->update_window_end_hook (w, 0, 0);
13748 update_end (f);
13749 }
13750
13751 /* Shift current matrix down by nrows_scrolled lines. */
13752 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13753 rotate_matrix (w->current_matrix,
13754 start_vpos,
13755 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13756 nrows_scrolled);
13757
13758 /* Disable lines that must be updated. */
13759 for (i = 0; i < it.vpos; ++i)
13760 (start_row + i)->enabled_p = 0;
13761
13762 /* Re-compute Y positions. */
13763 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13764 max_y = it.last_visible_y;
13765 for (row = start_row + nrows_scrolled;
13766 row < bottom_row;
13767 ++row)
13768 {
13769 row->y = it.current_y;
13770 row->visible_height = row->height;
13771
13772 if (row->y < min_y)
13773 row->visible_height -= min_y - row->y;
13774 if (row->y + row->height > max_y)
13775 row->visible_height -= row->y + row->height - max_y;
13776 row->redraw_fringe_bitmaps_p = 1;
13777
13778 it.current_y += row->height;
13779
13780 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13781 last_reused_text_row = row;
13782 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13783 break;
13784 }
13785
13786 /* Disable lines in the current matrix which are now
13787 below the window. */
13788 for (++row; row < bottom_row; ++row)
13789 row->enabled_p = row->mode_line_p = 0;
13790 }
13791
13792 /* Update window_end_pos etc.; last_reused_text_row is the last
13793 reused row from the current matrix containing text, if any.
13794 The value of last_text_row is the last displayed line
13795 containing text. */
13796 if (last_reused_text_row)
13797 {
13798 w->window_end_bytepos
13799 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13800 w->window_end_pos
13801 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13802 w->window_end_vpos
13803 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13804 w->current_matrix));
13805 }
13806 else if (last_text_row)
13807 {
13808 w->window_end_bytepos
13809 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13810 w->window_end_pos
13811 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13812 w->window_end_vpos
13813 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13814 }
13815 else
13816 {
13817 /* This window must be completely empty. */
13818 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13819 w->window_end_pos = make_number (Z - ZV);
13820 w->window_end_vpos = make_number (0);
13821 }
13822 w->window_end_valid = Qnil;
13823
13824 /* Update hint: don't try scrolling again in update_window. */
13825 w->desired_matrix->no_scrolling_p = 1;
13826
13827 #if GLYPH_DEBUG
13828 debug_method_add (w, "try_window_reusing_current_matrix 1");
13829 #endif
13830 return 1;
13831 }
13832 else if (CHARPOS (new_start) > CHARPOS (start))
13833 {
13834 struct glyph_row *pt_row, *row;
13835 struct glyph_row *first_reusable_row;
13836 struct glyph_row *first_row_to_display;
13837 int dy;
13838 int yb = window_text_bottom_y (w);
13839
13840 /* Find the row starting at new_start, if there is one. Don't
13841 reuse a partially visible line at the end. */
13842 first_reusable_row = start_row;
13843 while (first_reusable_row->enabled_p
13844 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13845 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13846 < CHARPOS (new_start)))
13847 ++first_reusable_row;
13848
13849 /* Give up if there is no row to reuse. */
13850 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13851 || !first_reusable_row->enabled_p
13852 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13853 != CHARPOS (new_start)))
13854 return 0;
13855
13856 /* We can reuse fully visible rows beginning with
13857 first_reusable_row to the end of the window. Set
13858 first_row_to_display to the first row that cannot be reused.
13859 Set pt_row to the row containing point, if there is any. */
13860 pt_row = NULL;
13861 for (first_row_to_display = first_reusable_row;
13862 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13863 ++first_row_to_display)
13864 {
13865 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13866 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13867 pt_row = first_row_to_display;
13868 }
13869
13870 /* Start displaying at the start of first_row_to_display. */
13871 xassert (first_row_to_display->y < yb);
13872 init_to_row_start (&it, w, first_row_to_display);
13873
13874 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13875 - start_vpos);
13876 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13877 - nrows_scrolled);
13878 it.current_y = (first_row_to_display->y - first_reusable_row->y
13879 + WINDOW_HEADER_LINE_HEIGHT (w));
13880
13881 /* Display lines beginning with first_row_to_display in the
13882 desired matrix. Set last_text_row to the last row displayed
13883 that displays text. */
13884 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13885 if (pt_row == NULL)
13886 w->cursor.vpos = -1;
13887 last_text_row = NULL;
13888 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13889 if (display_line (&it))
13890 last_text_row = it.glyph_row - 1;
13891
13892 /* Give up If point isn't in a row displayed or reused. */
13893 if (w->cursor.vpos < 0)
13894 {
13895 clear_glyph_matrix (w->desired_matrix);
13896 return 0;
13897 }
13898
13899 /* If point is in a reused row, adjust y and vpos of the cursor
13900 position. */
13901 if (pt_row)
13902 {
13903 w->cursor.vpos -= nrows_scrolled;
13904 w->cursor.y -= first_reusable_row->y - start_row->y;
13905 }
13906
13907 /* Scroll the display. */
13908 run.current_y = first_reusable_row->y;
13909 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13910 run.height = it.last_visible_y - run.current_y;
13911 dy = run.current_y - run.desired_y;
13912
13913 if (run.height)
13914 {
13915 update_begin (f);
13916 rif->update_window_begin_hook (w);
13917 rif->clear_window_mouse_face (w);
13918 rif->scroll_run_hook (w, &run);
13919 rif->update_window_end_hook (w, 0, 0);
13920 update_end (f);
13921 }
13922
13923 /* Adjust Y positions of reused rows. */
13924 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13925 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13926 max_y = it.last_visible_y;
13927 for (row = first_reusable_row; row < first_row_to_display; ++row)
13928 {
13929 row->y -= dy;
13930 row->visible_height = row->height;
13931 if (row->y < min_y)
13932 row->visible_height -= min_y - row->y;
13933 if (row->y + row->height > max_y)
13934 row->visible_height -= row->y + row->height - max_y;
13935 row->redraw_fringe_bitmaps_p = 1;
13936 }
13937
13938 /* Scroll the current matrix. */
13939 xassert (nrows_scrolled > 0);
13940 rotate_matrix (w->current_matrix,
13941 start_vpos,
13942 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13943 -nrows_scrolled);
13944
13945 /* Disable rows not reused. */
13946 for (row -= nrows_scrolled; row < bottom_row; ++row)
13947 row->enabled_p = 0;
13948
13949 /* Point may have moved to a different line, so we cannot assume that
13950 the previous cursor position is valid; locate the correct row. */
13951 if (pt_row)
13952 {
13953 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13954 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13955 row++)
13956 {
13957 w->cursor.vpos++;
13958 w->cursor.y = row->y;
13959 }
13960 if (row < bottom_row)
13961 {
13962 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13963 while (glyph->charpos < PT)
13964 {
13965 w->cursor.hpos++;
13966 w->cursor.x += glyph->pixel_width;
13967 glyph++;
13968 }
13969 }
13970 }
13971
13972 /* Adjust window end. A null value of last_text_row means that
13973 the window end is in reused rows which in turn means that
13974 only its vpos can have changed. */
13975 if (last_text_row)
13976 {
13977 w->window_end_bytepos
13978 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13979 w->window_end_pos
13980 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13981 w->window_end_vpos
13982 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13983 }
13984 else
13985 {
13986 w->window_end_vpos
13987 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13988 }
13989
13990 w->window_end_valid = Qnil;
13991 w->desired_matrix->no_scrolling_p = 1;
13992
13993 #if GLYPH_DEBUG
13994 debug_method_add (w, "try_window_reusing_current_matrix 2");
13995 #endif
13996 return 1;
13997 }
13998
13999 return 0;
14000 }
14001
14002
14003 \f
14004 /************************************************************************
14005 Window redisplay reusing current matrix when buffer has changed
14006 ************************************************************************/
14007
14008 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
14009 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
14010 int *, int *));
14011 static struct glyph_row *
14012 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
14013 struct glyph_row *));
14014
14015
14016 /* Return the last row in MATRIX displaying text. If row START is
14017 non-null, start searching with that row. IT gives the dimensions
14018 of the display. Value is null if matrix is empty; otherwise it is
14019 a pointer to the row found. */
14020
14021 static struct glyph_row *
14022 find_last_row_displaying_text (matrix, it, start)
14023 struct glyph_matrix *matrix;
14024 struct it *it;
14025 struct glyph_row *start;
14026 {
14027 struct glyph_row *row, *row_found;
14028
14029 /* Set row_found to the last row in IT->w's current matrix
14030 displaying text. The loop looks funny but think of partially
14031 visible lines. */
14032 row_found = NULL;
14033 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14034 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14035 {
14036 xassert (row->enabled_p);
14037 row_found = row;
14038 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14039 break;
14040 ++row;
14041 }
14042
14043 return row_found;
14044 }
14045
14046
14047 /* Return the last row in the current matrix of W that is not affected
14048 by changes at the start of current_buffer that occurred since W's
14049 current matrix was built. Value is null if no such row exists.
14050
14051 BEG_UNCHANGED us the number of characters unchanged at the start of
14052 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14053 first changed character in current_buffer. Characters at positions <
14054 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14055 when the current matrix was built. */
14056
14057 static struct glyph_row *
14058 find_last_unchanged_at_beg_row (w)
14059 struct window *w;
14060 {
14061 int first_changed_pos = BEG + BEG_UNCHANGED;
14062 struct glyph_row *row;
14063 struct glyph_row *row_found = NULL;
14064 int yb = window_text_bottom_y (w);
14065
14066 /* Find the last row displaying unchanged text. */
14067 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14068 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14069 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14070 {
14071 if (/* If row ends before first_changed_pos, it is unchanged,
14072 except in some case. */
14073 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14074 /* When row ends in ZV and we write at ZV it is not
14075 unchanged. */
14076 && !row->ends_at_zv_p
14077 /* When first_changed_pos is the end of a continued line,
14078 row is not unchanged because it may be no longer
14079 continued. */
14080 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14081 && (row->continued_p
14082 || row->exact_window_width_line_p)))
14083 row_found = row;
14084
14085 /* Stop if last visible row. */
14086 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14087 break;
14088
14089 ++row;
14090 }
14091
14092 return row_found;
14093 }
14094
14095
14096 /* Find the first glyph row in the current matrix of W that is not
14097 affected by changes at the end of current_buffer since the
14098 time W's current matrix was built.
14099
14100 Return in *DELTA the number of chars by which buffer positions in
14101 unchanged text at the end of current_buffer must be adjusted.
14102
14103 Return in *DELTA_BYTES the corresponding number of bytes.
14104
14105 Value is null if no such row exists, i.e. all rows are affected by
14106 changes. */
14107
14108 static struct glyph_row *
14109 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14110 struct window *w;
14111 int *delta, *delta_bytes;
14112 {
14113 struct glyph_row *row;
14114 struct glyph_row *row_found = NULL;
14115
14116 *delta = *delta_bytes = 0;
14117
14118 /* Display must not have been paused, otherwise the current matrix
14119 is not up to date. */
14120 if (NILP (w->window_end_valid))
14121 abort ();
14122
14123 /* A value of window_end_pos >= END_UNCHANGED means that the window
14124 end is in the range of changed text. If so, there is no
14125 unchanged row at the end of W's current matrix. */
14126 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14127 return NULL;
14128
14129 /* Set row to the last row in W's current matrix displaying text. */
14130 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14131
14132 /* If matrix is entirely empty, no unchanged row exists. */
14133 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14134 {
14135 /* The value of row is the last glyph row in the matrix having a
14136 meaningful buffer position in it. The end position of row
14137 corresponds to window_end_pos. This allows us to translate
14138 buffer positions in the current matrix to current buffer
14139 positions for characters not in changed text. */
14140 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14141 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14142 int last_unchanged_pos, last_unchanged_pos_old;
14143 struct glyph_row *first_text_row
14144 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14145
14146 *delta = Z - Z_old;
14147 *delta_bytes = Z_BYTE - Z_BYTE_old;
14148
14149 /* Set last_unchanged_pos to the buffer position of the last
14150 character in the buffer that has not been changed. Z is the
14151 index + 1 of the last character in current_buffer, i.e. by
14152 subtracting END_UNCHANGED we get the index of the last
14153 unchanged character, and we have to add BEG to get its buffer
14154 position. */
14155 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14156 last_unchanged_pos_old = last_unchanged_pos - *delta;
14157
14158 /* Search backward from ROW for a row displaying a line that
14159 starts at a minimum position >= last_unchanged_pos_old. */
14160 for (; row > first_text_row; --row)
14161 {
14162 /* This used to abort, but it can happen.
14163 It is ok to just stop the search instead here. KFS. */
14164 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14165 break;
14166
14167 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14168 row_found = row;
14169 }
14170 }
14171
14172 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14173 abort ();
14174
14175 return row_found;
14176 }
14177
14178
14179 /* Make sure that glyph rows in the current matrix of window W
14180 reference the same glyph memory as corresponding rows in the
14181 frame's frame matrix. This function is called after scrolling W's
14182 current matrix on a terminal frame in try_window_id and
14183 try_window_reusing_current_matrix. */
14184
14185 static void
14186 sync_frame_with_window_matrix_rows (w)
14187 struct window *w;
14188 {
14189 struct frame *f = XFRAME (w->frame);
14190 struct glyph_row *window_row, *window_row_end, *frame_row;
14191
14192 /* Preconditions: W must be a leaf window and full-width. Its frame
14193 must have a frame matrix. */
14194 xassert (NILP (w->hchild) && NILP (w->vchild));
14195 xassert (WINDOW_FULL_WIDTH_P (w));
14196 xassert (!FRAME_WINDOW_P (f));
14197
14198 /* If W is a full-width window, glyph pointers in W's current matrix
14199 have, by definition, to be the same as glyph pointers in the
14200 corresponding frame matrix. Note that frame matrices have no
14201 marginal areas (see build_frame_matrix). */
14202 window_row = w->current_matrix->rows;
14203 window_row_end = window_row + w->current_matrix->nrows;
14204 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14205 while (window_row < window_row_end)
14206 {
14207 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14208 struct glyph *end = window_row->glyphs[LAST_AREA];
14209
14210 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14211 frame_row->glyphs[TEXT_AREA] = start;
14212 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14213 frame_row->glyphs[LAST_AREA] = end;
14214
14215 /* Disable frame rows whose corresponding window rows have
14216 been disabled in try_window_id. */
14217 if (!window_row->enabled_p)
14218 frame_row->enabled_p = 0;
14219
14220 ++window_row, ++frame_row;
14221 }
14222 }
14223
14224
14225 /* Find the glyph row in window W containing CHARPOS. Consider all
14226 rows between START and END (not inclusive). END null means search
14227 all rows to the end of the display area of W. Value is the row
14228 containing CHARPOS or null. */
14229
14230 struct glyph_row *
14231 row_containing_pos (w, charpos, start, end, dy)
14232 struct window *w;
14233 int charpos;
14234 struct glyph_row *start, *end;
14235 int dy;
14236 {
14237 struct glyph_row *row = start;
14238 int last_y;
14239
14240 /* If we happen to start on a header-line, skip that. */
14241 if (row->mode_line_p)
14242 ++row;
14243
14244 if ((end && row >= end) || !row->enabled_p)
14245 return NULL;
14246
14247 last_y = window_text_bottom_y (w) - dy;
14248
14249 while (1)
14250 {
14251 /* Give up if we have gone too far. */
14252 if (end && row >= end)
14253 return NULL;
14254 /* This formerly returned if they were equal.
14255 I think that both quantities are of a "last plus one" type;
14256 if so, when they are equal, the row is within the screen. -- rms. */
14257 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14258 return NULL;
14259
14260 /* If it is in this row, return this row. */
14261 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14262 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14263 /* The end position of a row equals the start
14264 position of the next row. If CHARPOS is there, we
14265 would rather display it in the next line, except
14266 when this line ends in ZV. */
14267 && !row->ends_at_zv_p
14268 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14269 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14270 return row;
14271 ++row;
14272 }
14273 }
14274
14275
14276 /* Try to redisplay window W by reusing its existing display. W's
14277 current matrix must be up to date when this function is called,
14278 i.e. window_end_valid must not be nil.
14279
14280 Value is
14281
14282 1 if display has been updated
14283 0 if otherwise unsuccessful
14284 -1 if redisplay with same window start is known not to succeed
14285
14286 The following steps are performed:
14287
14288 1. Find the last row in the current matrix of W that is not
14289 affected by changes at the start of current_buffer. If no such row
14290 is found, give up.
14291
14292 2. Find the first row in W's current matrix that is not affected by
14293 changes at the end of current_buffer. Maybe there is no such row.
14294
14295 3. Display lines beginning with the row + 1 found in step 1 to the
14296 row found in step 2 or, if step 2 didn't find a row, to the end of
14297 the window.
14298
14299 4. If cursor is not known to appear on the window, give up.
14300
14301 5. If display stopped at the row found in step 2, scroll the
14302 display and current matrix as needed.
14303
14304 6. Maybe display some lines at the end of W, if we must. This can
14305 happen under various circumstances, like a partially visible line
14306 becoming fully visible, or because newly displayed lines are displayed
14307 in smaller font sizes.
14308
14309 7. Update W's window end information. */
14310
14311 static int
14312 try_window_id (w)
14313 struct window *w;
14314 {
14315 struct frame *f = XFRAME (w->frame);
14316 struct glyph_matrix *current_matrix = w->current_matrix;
14317 struct glyph_matrix *desired_matrix = w->desired_matrix;
14318 struct glyph_row *last_unchanged_at_beg_row;
14319 struct glyph_row *first_unchanged_at_end_row;
14320 struct glyph_row *row;
14321 struct glyph_row *bottom_row;
14322 int bottom_vpos;
14323 struct it it;
14324 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14325 struct text_pos start_pos;
14326 struct run run;
14327 int first_unchanged_at_end_vpos = 0;
14328 struct glyph_row *last_text_row, *last_text_row_at_end;
14329 struct text_pos start;
14330 int first_changed_charpos, last_changed_charpos;
14331
14332 #if GLYPH_DEBUG
14333 if (inhibit_try_window_id)
14334 return 0;
14335 #endif
14336
14337 /* This is handy for debugging. */
14338 #if 0
14339 #define GIVE_UP(X) \
14340 do { \
14341 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14342 return 0; \
14343 } while (0)
14344 #else
14345 #define GIVE_UP(X) return 0
14346 #endif
14347
14348 SET_TEXT_POS_FROM_MARKER (start, w->start);
14349
14350 /* Don't use this for mini-windows because these can show
14351 messages and mini-buffers, and we don't handle that here. */
14352 if (MINI_WINDOW_P (w))
14353 GIVE_UP (1);
14354
14355 /* This flag is used to prevent redisplay optimizations. */
14356 if (windows_or_buffers_changed || cursor_type_changed)
14357 GIVE_UP (2);
14358
14359 /* Verify that narrowing has not changed.
14360 Also verify that we were not told to prevent redisplay optimizations.
14361 It would be nice to further
14362 reduce the number of cases where this prevents try_window_id. */
14363 if (current_buffer->clip_changed
14364 || current_buffer->prevent_redisplay_optimizations_p)
14365 GIVE_UP (3);
14366
14367 /* Window must either use window-based redisplay or be full width. */
14368 if (!FRAME_WINDOW_P (f)
14369 && (!line_ins_del_ok
14370 || !WINDOW_FULL_WIDTH_P (w)))
14371 GIVE_UP (4);
14372
14373 /* Give up if point is not known NOT to appear in W. */
14374 if (PT < CHARPOS (start))
14375 GIVE_UP (5);
14376
14377 /* Another way to prevent redisplay optimizations. */
14378 if (XFASTINT (w->last_modified) == 0)
14379 GIVE_UP (6);
14380
14381 /* Verify that window is not hscrolled. */
14382 if (XFASTINT (w->hscroll) != 0)
14383 GIVE_UP (7);
14384
14385 /* Verify that display wasn't paused. */
14386 if (NILP (w->window_end_valid))
14387 GIVE_UP (8);
14388
14389 /* Can't use this if highlighting a region because a cursor movement
14390 will do more than just set the cursor. */
14391 if (!NILP (Vtransient_mark_mode)
14392 && !NILP (current_buffer->mark_active))
14393 GIVE_UP (9);
14394
14395 /* Likewise if highlighting trailing whitespace. */
14396 if (!NILP (Vshow_trailing_whitespace))
14397 GIVE_UP (11);
14398
14399 /* Likewise if showing a region. */
14400 if (!NILP (w->region_showing))
14401 GIVE_UP (10);
14402
14403 /* Can use this if overlay arrow position and or string have changed. */
14404 if (overlay_arrows_changed_p ())
14405 GIVE_UP (12);
14406
14407
14408 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14409 only if buffer has really changed. The reason is that the gap is
14410 initially at Z for freshly visited files. The code below would
14411 set end_unchanged to 0 in that case. */
14412 if (MODIFF > SAVE_MODIFF
14413 /* This seems to happen sometimes after saving a buffer. */
14414 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14415 {
14416 if (GPT - BEG < BEG_UNCHANGED)
14417 BEG_UNCHANGED = GPT - BEG;
14418 if (Z - GPT < END_UNCHANGED)
14419 END_UNCHANGED = Z - GPT;
14420 }
14421
14422 /* The position of the first and last character that has been changed. */
14423 first_changed_charpos = BEG + BEG_UNCHANGED;
14424 last_changed_charpos = Z - END_UNCHANGED;
14425
14426 /* If window starts after a line end, and the last change is in
14427 front of that newline, then changes don't affect the display.
14428 This case happens with stealth-fontification. Note that although
14429 the display is unchanged, glyph positions in the matrix have to
14430 be adjusted, of course. */
14431 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14432 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14433 && ((last_changed_charpos < CHARPOS (start)
14434 && CHARPOS (start) == BEGV)
14435 || (last_changed_charpos < CHARPOS (start) - 1
14436 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14437 {
14438 int Z_old, delta, Z_BYTE_old, delta_bytes;
14439 struct glyph_row *r0;
14440
14441 /* Compute how many chars/bytes have been added to or removed
14442 from the buffer. */
14443 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14444 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14445 delta = Z - Z_old;
14446 delta_bytes = Z_BYTE - Z_BYTE_old;
14447
14448 /* Give up if PT is not in the window. Note that it already has
14449 been checked at the start of try_window_id that PT is not in
14450 front of the window start. */
14451 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14452 GIVE_UP (13);
14453
14454 /* If window start is unchanged, we can reuse the whole matrix
14455 as is, after adjusting glyph positions. No need to compute
14456 the window end again, since its offset from Z hasn't changed. */
14457 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14458 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14459 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14460 /* PT must not be in a partially visible line. */
14461 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14462 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14463 {
14464 /* Adjust positions in the glyph matrix. */
14465 if (delta || delta_bytes)
14466 {
14467 struct glyph_row *r1
14468 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14469 increment_matrix_positions (w->current_matrix,
14470 MATRIX_ROW_VPOS (r0, current_matrix),
14471 MATRIX_ROW_VPOS (r1, current_matrix),
14472 delta, delta_bytes);
14473 }
14474
14475 /* Set the cursor. */
14476 row = row_containing_pos (w, PT, r0, NULL, 0);
14477 if (row)
14478 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14479 else
14480 abort ();
14481 return 1;
14482 }
14483 }
14484
14485 /* Handle the case that changes are all below what is displayed in
14486 the window, and that PT is in the window. This shortcut cannot
14487 be taken if ZV is visible in the window, and text has been added
14488 there that is visible in the window. */
14489 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14490 /* ZV is not visible in the window, or there are no
14491 changes at ZV, actually. */
14492 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14493 || first_changed_charpos == last_changed_charpos))
14494 {
14495 struct glyph_row *r0;
14496
14497 /* Give up if PT is not in the window. Note that it already has
14498 been checked at the start of try_window_id that PT is not in
14499 front of the window start. */
14500 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14501 GIVE_UP (14);
14502
14503 /* If window start is unchanged, we can reuse the whole matrix
14504 as is, without changing glyph positions since no text has
14505 been added/removed in front of the window end. */
14506 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14507 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14508 /* PT must not be in a partially visible line. */
14509 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14510 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14511 {
14512 /* We have to compute the window end anew since text
14513 can have been added/removed after it. */
14514 w->window_end_pos
14515 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14516 w->window_end_bytepos
14517 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14518
14519 /* Set the cursor. */
14520 row = row_containing_pos (w, PT, r0, NULL, 0);
14521 if (row)
14522 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14523 else
14524 abort ();
14525 return 2;
14526 }
14527 }
14528
14529 /* Give up if window start is in the changed area.
14530
14531 The condition used to read
14532
14533 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14534
14535 but why that was tested escapes me at the moment. */
14536 if (CHARPOS (start) >= first_changed_charpos
14537 && CHARPOS (start) <= last_changed_charpos)
14538 GIVE_UP (15);
14539
14540 /* Check that window start agrees with the start of the first glyph
14541 row in its current matrix. Check this after we know the window
14542 start is not in changed text, otherwise positions would not be
14543 comparable. */
14544 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14545 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14546 GIVE_UP (16);
14547
14548 /* Give up if the window ends in strings. Overlay strings
14549 at the end are difficult to handle, so don't try. */
14550 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14551 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14552 GIVE_UP (20);
14553
14554 /* Compute the position at which we have to start displaying new
14555 lines. Some of the lines at the top of the window might be
14556 reusable because they are not displaying changed text. Find the
14557 last row in W's current matrix not affected by changes at the
14558 start of current_buffer. Value is null if changes start in the
14559 first line of window. */
14560 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14561 if (last_unchanged_at_beg_row)
14562 {
14563 /* Avoid starting to display in the moddle of a character, a TAB
14564 for instance. This is easier than to set up the iterator
14565 exactly, and it's not a frequent case, so the additional
14566 effort wouldn't really pay off. */
14567 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14568 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14569 && last_unchanged_at_beg_row > w->current_matrix->rows)
14570 --last_unchanged_at_beg_row;
14571
14572 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14573 GIVE_UP (17);
14574
14575 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14576 GIVE_UP (18);
14577 start_pos = it.current.pos;
14578
14579 /* Start displaying new lines in the desired matrix at the same
14580 vpos we would use in the current matrix, i.e. below
14581 last_unchanged_at_beg_row. */
14582 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14583 current_matrix);
14584 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14585 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14586
14587 xassert (it.hpos == 0 && it.current_x == 0);
14588 }
14589 else
14590 {
14591 /* There are no reusable lines at the start of the window.
14592 Start displaying in the first text line. */
14593 start_display (&it, w, start);
14594 it.vpos = it.first_vpos;
14595 start_pos = it.current.pos;
14596 }
14597
14598 /* Find the first row that is not affected by changes at the end of
14599 the buffer. Value will be null if there is no unchanged row, in
14600 which case we must redisplay to the end of the window. delta
14601 will be set to the value by which buffer positions beginning with
14602 first_unchanged_at_end_row have to be adjusted due to text
14603 changes. */
14604 first_unchanged_at_end_row
14605 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14606 IF_DEBUG (debug_delta = delta);
14607 IF_DEBUG (debug_delta_bytes = delta_bytes);
14608
14609 /* Set stop_pos to the buffer position up to which we will have to
14610 display new lines. If first_unchanged_at_end_row != NULL, this
14611 is the buffer position of the start of the line displayed in that
14612 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14613 that we don't stop at a buffer position. */
14614 stop_pos = 0;
14615 if (first_unchanged_at_end_row)
14616 {
14617 xassert (last_unchanged_at_beg_row == NULL
14618 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14619
14620 /* If this is a continuation line, move forward to the next one
14621 that isn't. Changes in lines above affect this line.
14622 Caution: this may move first_unchanged_at_end_row to a row
14623 not displaying text. */
14624 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14625 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14626 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14627 < it.last_visible_y))
14628 ++first_unchanged_at_end_row;
14629
14630 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14631 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14632 >= it.last_visible_y))
14633 first_unchanged_at_end_row = NULL;
14634 else
14635 {
14636 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14637 + delta);
14638 first_unchanged_at_end_vpos
14639 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14640 xassert (stop_pos >= Z - END_UNCHANGED);
14641 }
14642 }
14643 else if (last_unchanged_at_beg_row == NULL)
14644 GIVE_UP (19);
14645
14646
14647 #if GLYPH_DEBUG
14648
14649 /* Either there is no unchanged row at the end, or the one we have
14650 now displays text. This is a necessary condition for the window
14651 end pos calculation at the end of this function. */
14652 xassert (first_unchanged_at_end_row == NULL
14653 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14654
14655 debug_last_unchanged_at_beg_vpos
14656 = (last_unchanged_at_beg_row
14657 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14658 : -1);
14659 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14660
14661 #endif /* GLYPH_DEBUG != 0 */
14662
14663
14664 /* Display new lines. Set last_text_row to the last new line
14665 displayed which has text on it, i.e. might end up as being the
14666 line where the window_end_vpos is. */
14667 w->cursor.vpos = -1;
14668 last_text_row = NULL;
14669 overlay_arrow_seen = 0;
14670 while (it.current_y < it.last_visible_y
14671 && !fonts_changed_p
14672 && (first_unchanged_at_end_row == NULL
14673 || IT_CHARPOS (it) < stop_pos))
14674 {
14675 if (display_line (&it))
14676 last_text_row = it.glyph_row - 1;
14677 }
14678
14679 if (fonts_changed_p)
14680 return -1;
14681
14682
14683 /* Compute differences in buffer positions, y-positions etc. for
14684 lines reused at the bottom of the window. Compute what we can
14685 scroll. */
14686 if (first_unchanged_at_end_row
14687 /* No lines reused because we displayed everything up to the
14688 bottom of the window. */
14689 && it.current_y < it.last_visible_y)
14690 {
14691 dvpos = (it.vpos
14692 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14693 current_matrix));
14694 dy = it.current_y - first_unchanged_at_end_row->y;
14695 run.current_y = first_unchanged_at_end_row->y;
14696 run.desired_y = run.current_y + dy;
14697 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14698 }
14699 else
14700 {
14701 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14702 first_unchanged_at_end_row = NULL;
14703 }
14704 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14705
14706
14707 /* Find the cursor if not already found. We have to decide whether
14708 PT will appear on this window (it sometimes doesn't, but this is
14709 not a very frequent case.) This decision has to be made before
14710 the current matrix is altered. A value of cursor.vpos < 0 means
14711 that PT is either in one of the lines beginning at
14712 first_unchanged_at_end_row or below the window. Don't care for
14713 lines that might be displayed later at the window end; as
14714 mentioned, this is not a frequent case. */
14715 if (w->cursor.vpos < 0)
14716 {
14717 /* Cursor in unchanged rows at the top? */
14718 if (PT < CHARPOS (start_pos)
14719 && last_unchanged_at_beg_row)
14720 {
14721 row = row_containing_pos (w, PT,
14722 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14723 last_unchanged_at_beg_row + 1, 0);
14724 if (row)
14725 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14726 }
14727
14728 /* Start from first_unchanged_at_end_row looking for PT. */
14729 else if (first_unchanged_at_end_row)
14730 {
14731 row = row_containing_pos (w, PT - delta,
14732 first_unchanged_at_end_row, NULL, 0);
14733 if (row)
14734 set_cursor_from_row (w, row, w->current_matrix, delta,
14735 delta_bytes, dy, dvpos);
14736 }
14737
14738 /* Give up if cursor was not found. */
14739 if (w->cursor.vpos < 0)
14740 {
14741 clear_glyph_matrix (w->desired_matrix);
14742 return -1;
14743 }
14744 }
14745
14746 /* Don't let the cursor end in the scroll margins. */
14747 {
14748 int this_scroll_margin, cursor_height;
14749
14750 this_scroll_margin = max (0, scroll_margin);
14751 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14752 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14753 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14754
14755 if ((w->cursor.y < this_scroll_margin
14756 && CHARPOS (start) > BEGV)
14757 /* Old redisplay didn't take scroll margin into account at the bottom,
14758 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14759 || (w->cursor.y + (make_cursor_line_fully_visible_p
14760 ? cursor_height + this_scroll_margin
14761 : 1)) > it.last_visible_y)
14762 {
14763 w->cursor.vpos = -1;
14764 clear_glyph_matrix (w->desired_matrix);
14765 return -1;
14766 }
14767 }
14768
14769 /* Scroll the display. Do it before changing the current matrix so
14770 that xterm.c doesn't get confused about where the cursor glyph is
14771 found. */
14772 if (dy && run.height)
14773 {
14774 update_begin (f);
14775
14776 if (FRAME_WINDOW_P (f))
14777 {
14778 rif->update_window_begin_hook (w);
14779 rif->clear_window_mouse_face (w);
14780 rif->scroll_run_hook (w, &run);
14781 rif->update_window_end_hook (w, 0, 0);
14782 }
14783 else
14784 {
14785 /* Terminal frame. In this case, dvpos gives the number of
14786 lines to scroll by; dvpos < 0 means scroll up. */
14787 int first_unchanged_at_end_vpos
14788 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14789 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14790 int end = (WINDOW_TOP_EDGE_LINE (w)
14791 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14792 + window_internal_height (w));
14793
14794 /* Perform the operation on the screen. */
14795 if (dvpos > 0)
14796 {
14797 /* Scroll last_unchanged_at_beg_row to the end of the
14798 window down dvpos lines. */
14799 set_terminal_window (end);
14800
14801 /* On dumb terminals delete dvpos lines at the end
14802 before inserting dvpos empty lines. */
14803 if (!scroll_region_ok)
14804 ins_del_lines (end - dvpos, -dvpos);
14805
14806 /* Insert dvpos empty lines in front of
14807 last_unchanged_at_beg_row. */
14808 ins_del_lines (from, dvpos);
14809 }
14810 else if (dvpos < 0)
14811 {
14812 /* Scroll up last_unchanged_at_beg_vpos to the end of
14813 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14814 set_terminal_window (end);
14815
14816 /* Delete dvpos lines in front of
14817 last_unchanged_at_beg_vpos. ins_del_lines will set
14818 the cursor to the given vpos and emit |dvpos| delete
14819 line sequences. */
14820 ins_del_lines (from + dvpos, dvpos);
14821
14822 /* On a dumb terminal insert dvpos empty lines at the
14823 end. */
14824 if (!scroll_region_ok)
14825 ins_del_lines (end + dvpos, -dvpos);
14826 }
14827
14828 set_terminal_window (0);
14829 }
14830
14831 update_end (f);
14832 }
14833
14834 /* Shift reused rows of the current matrix to the right position.
14835 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14836 text. */
14837 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14838 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14839 if (dvpos < 0)
14840 {
14841 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14842 bottom_vpos, dvpos);
14843 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14844 bottom_vpos, 0);
14845 }
14846 else if (dvpos > 0)
14847 {
14848 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14849 bottom_vpos, dvpos);
14850 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14851 first_unchanged_at_end_vpos + dvpos, 0);
14852 }
14853
14854 /* For frame-based redisplay, make sure that current frame and window
14855 matrix are in sync with respect to glyph memory. */
14856 if (!FRAME_WINDOW_P (f))
14857 sync_frame_with_window_matrix_rows (w);
14858
14859 /* Adjust buffer positions in reused rows. */
14860 if (delta)
14861 increment_matrix_positions (current_matrix,
14862 first_unchanged_at_end_vpos + dvpos,
14863 bottom_vpos, delta, delta_bytes);
14864
14865 /* Adjust Y positions. */
14866 if (dy)
14867 shift_glyph_matrix (w, current_matrix,
14868 first_unchanged_at_end_vpos + dvpos,
14869 bottom_vpos, dy);
14870
14871 if (first_unchanged_at_end_row)
14872 {
14873 first_unchanged_at_end_row += dvpos;
14874 if (first_unchanged_at_end_row->y >= it.last_visible_y
14875 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14876 first_unchanged_at_end_row = NULL;
14877 }
14878
14879 /* If scrolling up, there may be some lines to display at the end of
14880 the window. */
14881 last_text_row_at_end = NULL;
14882 if (dy < 0)
14883 {
14884 /* Scrolling up can leave for example a partially visible line
14885 at the end of the window to be redisplayed. */
14886 /* Set last_row to the glyph row in the current matrix where the
14887 window end line is found. It has been moved up or down in
14888 the matrix by dvpos. */
14889 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14890 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14891
14892 /* If last_row is the window end line, it should display text. */
14893 xassert (last_row->displays_text_p);
14894
14895 /* If window end line was partially visible before, begin
14896 displaying at that line. Otherwise begin displaying with the
14897 line following it. */
14898 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14899 {
14900 init_to_row_start (&it, w, last_row);
14901 it.vpos = last_vpos;
14902 it.current_y = last_row->y;
14903 }
14904 else
14905 {
14906 init_to_row_end (&it, w, last_row);
14907 it.vpos = 1 + last_vpos;
14908 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14909 ++last_row;
14910 }
14911
14912 /* We may start in a continuation line. If so, we have to
14913 get the right continuation_lines_width and current_x. */
14914 it.continuation_lines_width = last_row->continuation_lines_width;
14915 it.hpos = it.current_x = 0;
14916
14917 /* Display the rest of the lines at the window end. */
14918 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14919 while (it.current_y < it.last_visible_y
14920 && !fonts_changed_p)
14921 {
14922 /* Is it always sure that the display agrees with lines in
14923 the current matrix? I don't think so, so we mark rows
14924 displayed invalid in the current matrix by setting their
14925 enabled_p flag to zero. */
14926 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14927 if (display_line (&it))
14928 last_text_row_at_end = it.glyph_row - 1;
14929 }
14930 }
14931
14932 /* Update window_end_pos and window_end_vpos. */
14933 if (first_unchanged_at_end_row
14934 && !last_text_row_at_end)
14935 {
14936 /* Window end line if one of the preserved rows from the current
14937 matrix. Set row to the last row displaying text in current
14938 matrix starting at first_unchanged_at_end_row, after
14939 scrolling. */
14940 xassert (first_unchanged_at_end_row->displays_text_p);
14941 row = find_last_row_displaying_text (w->current_matrix, &it,
14942 first_unchanged_at_end_row);
14943 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14944
14945 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14946 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14947 w->window_end_vpos
14948 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14949 xassert (w->window_end_bytepos >= 0);
14950 IF_DEBUG (debug_method_add (w, "A"));
14951 }
14952 else if (last_text_row_at_end)
14953 {
14954 w->window_end_pos
14955 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14956 w->window_end_bytepos
14957 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14958 w->window_end_vpos
14959 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14960 xassert (w->window_end_bytepos >= 0);
14961 IF_DEBUG (debug_method_add (w, "B"));
14962 }
14963 else if (last_text_row)
14964 {
14965 /* We have displayed either to the end of the window or at the
14966 end of the window, i.e. the last row with text is to be found
14967 in the desired matrix. */
14968 w->window_end_pos
14969 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14970 w->window_end_bytepos
14971 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14972 w->window_end_vpos
14973 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14974 xassert (w->window_end_bytepos >= 0);
14975 }
14976 else if (first_unchanged_at_end_row == NULL
14977 && last_text_row == NULL
14978 && last_text_row_at_end == NULL)
14979 {
14980 /* Displayed to end of window, but no line containing text was
14981 displayed. Lines were deleted at the end of the window. */
14982 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14983 int vpos = XFASTINT (w->window_end_vpos);
14984 struct glyph_row *current_row = current_matrix->rows + vpos;
14985 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14986
14987 for (row = NULL;
14988 row == NULL && vpos >= first_vpos;
14989 --vpos, --current_row, --desired_row)
14990 {
14991 if (desired_row->enabled_p)
14992 {
14993 if (desired_row->displays_text_p)
14994 row = desired_row;
14995 }
14996 else if (current_row->displays_text_p)
14997 row = current_row;
14998 }
14999
15000 xassert (row != NULL);
15001 w->window_end_vpos = make_number (vpos + 1);
15002 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
15003 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
15004 xassert (w->window_end_bytepos >= 0);
15005 IF_DEBUG (debug_method_add (w, "C"));
15006 }
15007 else
15008 abort ();
15009
15010 #if 0 /* This leads to problems, for instance when the cursor is
15011 at ZV, and the cursor line displays no text. */
15012 /* Disable rows below what's displayed in the window. This makes
15013 debugging easier. */
15014 enable_glyph_matrix_rows (current_matrix,
15015 XFASTINT (w->window_end_vpos) + 1,
15016 bottom_vpos, 0);
15017 #endif
15018
15019 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
15020 debug_end_vpos = XFASTINT (w->window_end_vpos));
15021
15022 /* Record that display has not been completed. */
15023 w->window_end_valid = Qnil;
15024 w->desired_matrix->no_scrolling_p = 1;
15025 return 3;
15026
15027 #undef GIVE_UP
15028 }
15029
15030
15031 \f
15032 /***********************************************************************
15033 More debugging support
15034 ***********************************************************************/
15035
15036 #if GLYPH_DEBUG
15037
15038 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15039 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15040 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15041
15042
15043 /* Dump the contents of glyph matrix MATRIX on stderr.
15044
15045 GLYPHS 0 means don't show glyph contents.
15046 GLYPHS 1 means show glyphs in short form
15047 GLYPHS > 1 means show glyphs in long form. */
15048
15049 void
15050 dump_glyph_matrix (matrix, glyphs)
15051 struct glyph_matrix *matrix;
15052 int glyphs;
15053 {
15054 int i;
15055 for (i = 0; i < matrix->nrows; ++i)
15056 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15057 }
15058
15059
15060 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15061 the glyph row and area where the glyph comes from. */
15062
15063 void
15064 dump_glyph (row, glyph, area)
15065 struct glyph_row *row;
15066 struct glyph *glyph;
15067 int area;
15068 {
15069 if (glyph->type == CHAR_GLYPH)
15070 {
15071 fprintf (stderr,
15072 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15073 glyph - row->glyphs[TEXT_AREA],
15074 'C',
15075 glyph->charpos,
15076 (BUFFERP (glyph->object)
15077 ? 'B'
15078 : (STRINGP (glyph->object)
15079 ? 'S'
15080 : '-')),
15081 glyph->pixel_width,
15082 glyph->u.ch,
15083 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15084 ? glyph->u.ch
15085 : '.'),
15086 glyph->face_id,
15087 glyph->left_box_line_p,
15088 glyph->right_box_line_p);
15089 }
15090 else if (glyph->type == STRETCH_GLYPH)
15091 {
15092 fprintf (stderr,
15093 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15094 glyph - row->glyphs[TEXT_AREA],
15095 'S',
15096 glyph->charpos,
15097 (BUFFERP (glyph->object)
15098 ? 'B'
15099 : (STRINGP (glyph->object)
15100 ? 'S'
15101 : '-')),
15102 glyph->pixel_width,
15103 0,
15104 '.',
15105 glyph->face_id,
15106 glyph->left_box_line_p,
15107 glyph->right_box_line_p);
15108 }
15109 else if (glyph->type == IMAGE_GLYPH)
15110 {
15111 fprintf (stderr,
15112 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15113 glyph - row->glyphs[TEXT_AREA],
15114 'I',
15115 glyph->charpos,
15116 (BUFFERP (glyph->object)
15117 ? 'B'
15118 : (STRINGP (glyph->object)
15119 ? 'S'
15120 : '-')),
15121 glyph->pixel_width,
15122 glyph->u.img_id,
15123 '.',
15124 glyph->face_id,
15125 glyph->left_box_line_p,
15126 glyph->right_box_line_p);
15127 }
15128 else if (glyph->type == COMPOSITE_GLYPH)
15129 {
15130 fprintf (stderr,
15131 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15132 glyph - row->glyphs[TEXT_AREA],
15133 '+',
15134 glyph->charpos,
15135 (BUFFERP (glyph->object)
15136 ? 'B'
15137 : (STRINGP (glyph->object)
15138 ? 'S'
15139 : '-')),
15140 glyph->pixel_width,
15141 glyph->u.cmp_id,
15142 '.',
15143 glyph->face_id,
15144 glyph->left_box_line_p,
15145 glyph->right_box_line_p);
15146 }
15147 }
15148
15149
15150 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15151 GLYPHS 0 means don't show glyph contents.
15152 GLYPHS 1 means show glyphs in short form
15153 GLYPHS > 1 means show glyphs in long form. */
15154
15155 void
15156 dump_glyph_row (row, vpos, glyphs)
15157 struct glyph_row *row;
15158 int vpos, glyphs;
15159 {
15160 if (glyphs != 1)
15161 {
15162 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15163 fprintf (stderr, "======================================================================\n");
15164
15165 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15166 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15167 vpos,
15168 MATRIX_ROW_START_CHARPOS (row),
15169 MATRIX_ROW_END_CHARPOS (row),
15170 row->used[TEXT_AREA],
15171 row->contains_overlapping_glyphs_p,
15172 row->enabled_p,
15173 row->truncated_on_left_p,
15174 row->truncated_on_right_p,
15175 row->continued_p,
15176 MATRIX_ROW_CONTINUATION_LINE_P (row),
15177 row->displays_text_p,
15178 row->ends_at_zv_p,
15179 row->fill_line_p,
15180 row->ends_in_middle_of_char_p,
15181 row->starts_in_middle_of_char_p,
15182 row->mouse_face_p,
15183 row->x,
15184 row->y,
15185 row->pixel_width,
15186 row->height,
15187 row->visible_height,
15188 row->ascent,
15189 row->phys_ascent);
15190 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15191 row->end.overlay_string_index,
15192 row->continuation_lines_width);
15193 fprintf (stderr, "%9d %5d\n",
15194 CHARPOS (row->start.string_pos),
15195 CHARPOS (row->end.string_pos));
15196 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15197 row->end.dpvec_index);
15198 }
15199
15200 if (glyphs > 1)
15201 {
15202 int area;
15203
15204 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15205 {
15206 struct glyph *glyph = row->glyphs[area];
15207 struct glyph *glyph_end = glyph + row->used[area];
15208
15209 /* Glyph for a line end in text. */
15210 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15211 ++glyph_end;
15212
15213 if (glyph < glyph_end)
15214 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15215
15216 for (; glyph < glyph_end; ++glyph)
15217 dump_glyph (row, glyph, area);
15218 }
15219 }
15220 else if (glyphs == 1)
15221 {
15222 int area;
15223
15224 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15225 {
15226 char *s = (char *) alloca (row->used[area] + 1);
15227 int i;
15228
15229 for (i = 0; i < row->used[area]; ++i)
15230 {
15231 struct glyph *glyph = row->glyphs[area] + i;
15232 if (glyph->type == CHAR_GLYPH
15233 && glyph->u.ch < 0x80
15234 && glyph->u.ch >= ' ')
15235 s[i] = glyph->u.ch;
15236 else
15237 s[i] = '.';
15238 }
15239
15240 s[i] = '\0';
15241 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15242 }
15243 }
15244 }
15245
15246
15247 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15248 Sdump_glyph_matrix, 0, 1, "p",
15249 doc: /* Dump the current matrix of the selected window to stderr.
15250 Shows contents of glyph row structures. With non-nil
15251 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15252 glyphs in short form, otherwise show glyphs in long form. */)
15253 (glyphs)
15254 Lisp_Object glyphs;
15255 {
15256 struct window *w = XWINDOW (selected_window);
15257 struct buffer *buffer = XBUFFER (w->buffer);
15258
15259 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15260 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15261 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15262 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15263 fprintf (stderr, "=============================================\n");
15264 dump_glyph_matrix (w->current_matrix,
15265 NILP (glyphs) ? 0 : XINT (glyphs));
15266 return Qnil;
15267 }
15268
15269
15270 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15271 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15272 ()
15273 {
15274 struct frame *f = XFRAME (selected_frame);
15275 dump_glyph_matrix (f->current_matrix, 1);
15276 return Qnil;
15277 }
15278
15279
15280 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15281 doc: /* Dump glyph row ROW to stderr.
15282 GLYPH 0 means don't dump glyphs.
15283 GLYPH 1 means dump glyphs in short form.
15284 GLYPH > 1 or omitted means dump glyphs in long form. */)
15285 (row, glyphs)
15286 Lisp_Object row, glyphs;
15287 {
15288 struct glyph_matrix *matrix;
15289 int vpos;
15290
15291 CHECK_NUMBER (row);
15292 matrix = XWINDOW (selected_window)->current_matrix;
15293 vpos = XINT (row);
15294 if (vpos >= 0 && vpos < matrix->nrows)
15295 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15296 vpos,
15297 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15298 return Qnil;
15299 }
15300
15301
15302 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15303 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15304 GLYPH 0 means don't dump glyphs.
15305 GLYPH 1 means dump glyphs in short form.
15306 GLYPH > 1 or omitted means dump glyphs in long form. */)
15307 (row, glyphs)
15308 Lisp_Object row, glyphs;
15309 {
15310 struct frame *sf = SELECTED_FRAME ();
15311 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15312 int vpos;
15313
15314 CHECK_NUMBER (row);
15315 vpos = XINT (row);
15316 if (vpos >= 0 && vpos < m->nrows)
15317 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15318 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15319 return Qnil;
15320 }
15321
15322
15323 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15324 doc: /* Toggle tracing of redisplay.
15325 With ARG, turn tracing on if and only if ARG is positive. */)
15326 (arg)
15327 Lisp_Object arg;
15328 {
15329 if (NILP (arg))
15330 trace_redisplay_p = !trace_redisplay_p;
15331 else
15332 {
15333 arg = Fprefix_numeric_value (arg);
15334 trace_redisplay_p = XINT (arg) > 0;
15335 }
15336
15337 return Qnil;
15338 }
15339
15340
15341 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15342 doc: /* Like `format', but print result to stderr.
15343 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15344 (nargs, args)
15345 int nargs;
15346 Lisp_Object *args;
15347 {
15348 Lisp_Object s = Fformat (nargs, args);
15349 fprintf (stderr, "%s", SDATA (s));
15350 return Qnil;
15351 }
15352
15353 #endif /* GLYPH_DEBUG */
15354
15355
15356 \f
15357 /***********************************************************************
15358 Building Desired Matrix Rows
15359 ***********************************************************************/
15360
15361 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15362 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15363
15364 static struct glyph_row *
15365 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15366 struct window *w;
15367 Lisp_Object overlay_arrow_string;
15368 {
15369 struct frame *f = XFRAME (WINDOW_FRAME (w));
15370 struct buffer *buffer = XBUFFER (w->buffer);
15371 struct buffer *old = current_buffer;
15372 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15373 int arrow_len = SCHARS (overlay_arrow_string);
15374 const unsigned char *arrow_end = arrow_string + arrow_len;
15375 const unsigned char *p;
15376 struct it it;
15377 int multibyte_p;
15378 int n_glyphs_before;
15379
15380 set_buffer_temp (buffer);
15381 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15382 it.glyph_row->used[TEXT_AREA] = 0;
15383 SET_TEXT_POS (it.position, 0, 0);
15384
15385 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15386 p = arrow_string;
15387 while (p < arrow_end)
15388 {
15389 Lisp_Object face, ilisp;
15390
15391 /* Get the next character. */
15392 if (multibyte_p)
15393 it.c = string_char_and_length (p, arrow_len, &it.len);
15394 else
15395 it.c = *p, it.len = 1;
15396 p += it.len;
15397
15398 /* Get its face. */
15399 ilisp = make_number (p - arrow_string);
15400 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15401 it.face_id = compute_char_face (f, it.c, face);
15402
15403 /* Compute its width, get its glyphs. */
15404 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15405 SET_TEXT_POS (it.position, -1, -1);
15406 PRODUCE_GLYPHS (&it);
15407
15408 /* If this character doesn't fit any more in the line, we have
15409 to remove some glyphs. */
15410 if (it.current_x > it.last_visible_x)
15411 {
15412 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15413 break;
15414 }
15415 }
15416
15417 set_buffer_temp (old);
15418 return it.glyph_row;
15419 }
15420
15421
15422 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15423 glyphs are only inserted for terminal frames since we can't really
15424 win with truncation glyphs when partially visible glyphs are
15425 involved. Which glyphs to insert is determined by
15426 produce_special_glyphs. */
15427
15428 static void
15429 insert_left_trunc_glyphs (it)
15430 struct it *it;
15431 {
15432 struct it truncate_it;
15433 struct glyph *from, *end, *to, *toend;
15434
15435 xassert (!FRAME_WINDOW_P (it->f));
15436
15437 /* Get the truncation glyphs. */
15438 truncate_it = *it;
15439 truncate_it.current_x = 0;
15440 truncate_it.face_id = DEFAULT_FACE_ID;
15441 truncate_it.glyph_row = &scratch_glyph_row;
15442 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15443 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15444 truncate_it.object = make_number (0);
15445 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15446
15447 /* Overwrite glyphs from IT with truncation glyphs. */
15448 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15449 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15450 to = it->glyph_row->glyphs[TEXT_AREA];
15451 toend = to + it->glyph_row->used[TEXT_AREA];
15452
15453 while (from < end)
15454 *to++ = *from++;
15455
15456 /* There may be padding glyphs left over. Overwrite them too. */
15457 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15458 {
15459 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15460 while (from < end)
15461 *to++ = *from++;
15462 }
15463
15464 if (to > toend)
15465 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15466 }
15467
15468
15469 /* Compute the pixel height and width of IT->glyph_row.
15470
15471 Most of the time, ascent and height of a display line will be equal
15472 to the max_ascent and max_height values of the display iterator
15473 structure. This is not the case if
15474
15475 1. We hit ZV without displaying anything. In this case, max_ascent
15476 and max_height will be zero.
15477
15478 2. We have some glyphs that don't contribute to the line height.
15479 (The glyph row flag contributes_to_line_height_p is for future
15480 pixmap extensions).
15481
15482 The first case is easily covered by using default values because in
15483 these cases, the line height does not really matter, except that it
15484 must not be zero. */
15485
15486 static void
15487 compute_line_metrics (it)
15488 struct it *it;
15489 {
15490 struct glyph_row *row = it->glyph_row;
15491 int area, i;
15492
15493 if (FRAME_WINDOW_P (it->f))
15494 {
15495 int i, min_y, max_y;
15496
15497 /* The line may consist of one space only, that was added to
15498 place the cursor on it. If so, the row's height hasn't been
15499 computed yet. */
15500 if (row->height == 0)
15501 {
15502 if (it->max_ascent + it->max_descent == 0)
15503 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15504 row->ascent = it->max_ascent;
15505 row->height = it->max_ascent + it->max_descent;
15506 row->phys_ascent = it->max_phys_ascent;
15507 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15508 row->extra_line_spacing = it->max_extra_line_spacing;
15509 }
15510
15511 /* Compute the width of this line. */
15512 row->pixel_width = row->x;
15513 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15514 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15515
15516 xassert (row->pixel_width >= 0);
15517 xassert (row->ascent >= 0 && row->height > 0);
15518
15519 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15520 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15521
15522 /* If first line's physical ascent is larger than its logical
15523 ascent, use the physical ascent, and make the row taller.
15524 This makes accented characters fully visible. */
15525 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15526 && row->phys_ascent > row->ascent)
15527 {
15528 row->height += row->phys_ascent - row->ascent;
15529 row->ascent = row->phys_ascent;
15530 }
15531
15532 /* Compute how much of the line is visible. */
15533 row->visible_height = row->height;
15534
15535 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15536 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15537
15538 if (row->y < min_y)
15539 row->visible_height -= min_y - row->y;
15540 if (row->y + row->height > max_y)
15541 row->visible_height -= row->y + row->height - max_y;
15542 }
15543 else
15544 {
15545 row->pixel_width = row->used[TEXT_AREA];
15546 if (row->continued_p)
15547 row->pixel_width -= it->continuation_pixel_width;
15548 else if (row->truncated_on_right_p)
15549 row->pixel_width -= it->truncation_pixel_width;
15550 row->ascent = row->phys_ascent = 0;
15551 row->height = row->phys_height = row->visible_height = 1;
15552 row->extra_line_spacing = 0;
15553 }
15554
15555 /* Compute a hash code for this row. */
15556 row->hash = 0;
15557 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15558 for (i = 0; i < row->used[area]; ++i)
15559 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15560 + row->glyphs[area][i].u.val
15561 + row->glyphs[area][i].face_id
15562 + row->glyphs[area][i].padding_p
15563 + (row->glyphs[area][i].type << 2));
15564
15565 it->max_ascent = it->max_descent = 0;
15566 it->max_phys_ascent = it->max_phys_descent = 0;
15567 }
15568
15569
15570 /* Append one space to the glyph row of iterator IT if doing a
15571 window-based redisplay. The space has the same face as
15572 IT->face_id. Value is non-zero if a space was added.
15573
15574 This function is called to make sure that there is always one glyph
15575 at the end of a glyph row that the cursor can be set on under
15576 window-systems. (If there weren't such a glyph we would not know
15577 how wide and tall a box cursor should be displayed).
15578
15579 At the same time this space let's a nicely handle clearing to the
15580 end of the line if the row ends in italic text. */
15581
15582 static int
15583 append_space_for_newline (it, default_face_p)
15584 struct it *it;
15585 int default_face_p;
15586 {
15587 if (FRAME_WINDOW_P (it->f))
15588 {
15589 int n = it->glyph_row->used[TEXT_AREA];
15590
15591 if (it->glyph_row->glyphs[TEXT_AREA] + n
15592 < it->glyph_row->glyphs[1 + TEXT_AREA])
15593 {
15594 /* Save some values that must not be changed.
15595 Must save IT->c and IT->len because otherwise
15596 ITERATOR_AT_END_P wouldn't work anymore after
15597 append_space_for_newline has been called. */
15598 enum display_element_type saved_what = it->what;
15599 int saved_c = it->c, saved_len = it->len;
15600 int saved_x = it->current_x;
15601 int saved_face_id = it->face_id;
15602 struct text_pos saved_pos;
15603 Lisp_Object saved_object;
15604 struct face *face;
15605
15606 saved_object = it->object;
15607 saved_pos = it->position;
15608
15609 it->what = IT_CHARACTER;
15610 bzero (&it->position, sizeof it->position);
15611 it->object = make_number (0);
15612 it->c = ' ';
15613 it->len = 1;
15614
15615 if (default_face_p)
15616 it->face_id = DEFAULT_FACE_ID;
15617 else if (it->face_before_selective_p)
15618 it->face_id = it->saved_face_id;
15619 face = FACE_FROM_ID (it->f, it->face_id);
15620 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15621
15622 PRODUCE_GLYPHS (it);
15623
15624 it->override_ascent = -1;
15625 it->constrain_row_ascent_descent_p = 0;
15626 it->current_x = saved_x;
15627 it->object = saved_object;
15628 it->position = saved_pos;
15629 it->what = saved_what;
15630 it->face_id = saved_face_id;
15631 it->len = saved_len;
15632 it->c = saved_c;
15633 return 1;
15634 }
15635 }
15636
15637 return 0;
15638 }
15639
15640
15641 /* Extend the face of the last glyph in the text area of IT->glyph_row
15642 to the end of the display line. Called from display_line.
15643 If the glyph row is empty, add a space glyph to it so that we
15644 know the face to draw. Set the glyph row flag fill_line_p. */
15645
15646 static void
15647 extend_face_to_end_of_line (it)
15648 struct it *it;
15649 {
15650 struct face *face;
15651 struct frame *f = it->f;
15652
15653 /* If line is already filled, do nothing. */
15654 if (it->current_x >= it->last_visible_x)
15655 return;
15656
15657 /* Face extension extends the background and box of IT->face_id
15658 to the end of the line. If the background equals the background
15659 of the frame, we don't have to do anything. */
15660 if (it->face_before_selective_p)
15661 face = FACE_FROM_ID (it->f, it->saved_face_id);
15662 else
15663 face = FACE_FROM_ID (f, it->face_id);
15664
15665 if (FRAME_WINDOW_P (f)
15666 && it->glyph_row->displays_text_p
15667 && face->box == FACE_NO_BOX
15668 && face->background == FRAME_BACKGROUND_PIXEL (f)
15669 && !face->stipple)
15670 return;
15671
15672 /* Set the glyph row flag indicating that the face of the last glyph
15673 in the text area has to be drawn to the end of the text area. */
15674 it->glyph_row->fill_line_p = 1;
15675
15676 /* If current character of IT is not ASCII, make sure we have the
15677 ASCII face. This will be automatically undone the next time
15678 get_next_display_element returns a multibyte character. Note
15679 that the character will always be single byte in unibyte text. */
15680 if (!SINGLE_BYTE_CHAR_P (it->c))
15681 {
15682 it->face_id = FACE_FOR_CHAR (f, face, 0);
15683 }
15684
15685 if (FRAME_WINDOW_P (f))
15686 {
15687 /* If the row is empty, add a space with the current face of IT,
15688 so that we know which face to draw. */
15689 if (it->glyph_row->used[TEXT_AREA] == 0)
15690 {
15691 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15692 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15693 it->glyph_row->used[TEXT_AREA] = 1;
15694 }
15695 }
15696 else
15697 {
15698 /* Save some values that must not be changed. */
15699 int saved_x = it->current_x;
15700 struct text_pos saved_pos;
15701 Lisp_Object saved_object;
15702 enum display_element_type saved_what = it->what;
15703 int saved_face_id = it->face_id;
15704
15705 saved_object = it->object;
15706 saved_pos = it->position;
15707
15708 it->what = IT_CHARACTER;
15709 bzero (&it->position, sizeof it->position);
15710 it->object = make_number (0);
15711 it->c = ' ';
15712 it->len = 1;
15713 it->face_id = face->id;
15714
15715 PRODUCE_GLYPHS (it);
15716
15717 while (it->current_x <= it->last_visible_x)
15718 PRODUCE_GLYPHS (it);
15719
15720 /* Don't count these blanks really. It would let us insert a left
15721 truncation glyph below and make us set the cursor on them, maybe. */
15722 it->current_x = saved_x;
15723 it->object = saved_object;
15724 it->position = saved_pos;
15725 it->what = saved_what;
15726 it->face_id = saved_face_id;
15727 }
15728 }
15729
15730
15731 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15732 trailing whitespace. */
15733
15734 static int
15735 trailing_whitespace_p (charpos)
15736 int charpos;
15737 {
15738 int bytepos = CHAR_TO_BYTE (charpos);
15739 int c = 0;
15740
15741 while (bytepos < ZV_BYTE
15742 && (c = FETCH_CHAR (bytepos),
15743 c == ' ' || c == '\t'))
15744 ++bytepos;
15745
15746 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15747 {
15748 if (bytepos != PT_BYTE)
15749 return 1;
15750 }
15751 return 0;
15752 }
15753
15754
15755 /* Highlight trailing whitespace, if any, in ROW. */
15756
15757 void
15758 highlight_trailing_whitespace (f, row)
15759 struct frame *f;
15760 struct glyph_row *row;
15761 {
15762 int used = row->used[TEXT_AREA];
15763
15764 if (used)
15765 {
15766 struct glyph *start = row->glyphs[TEXT_AREA];
15767 struct glyph *glyph = start + used - 1;
15768
15769 /* Skip over glyphs inserted to display the cursor at the
15770 end of a line, for extending the face of the last glyph
15771 to the end of the line on terminals, and for truncation
15772 and continuation glyphs. */
15773 while (glyph >= start
15774 && glyph->type == CHAR_GLYPH
15775 && INTEGERP (glyph->object))
15776 --glyph;
15777
15778 /* If last glyph is a space or stretch, and it's trailing
15779 whitespace, set the face of all trailing whitespace glyphs in
15780 IT->glyph_row to `trailing-whitespace'. */
15781 if (glyph >= start
15782 && BUFFERP (glyph->object)
15783 && (glyph->type == STRETCH_GLYPH
15784 || (glyph->type == CHAR_GLYPH
15785 && glyph->u.ch == ' '))
15786 && trailing_whitespace_p (glyph->charpos))
15787 {
15788 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15789 if (face_id < 0)
15790 return;
15791
15792 while (glyph >= start
15793 && BUFFERP (glyph->object)
15794 && (glyph->type == STRETCH_GLYPH
15795 || (glyph->type == CHAR_GLYPH
15796 && glyph->u.ch == ' ')))
15797 (glyph--)->face_id = face_id;
15798 }
15799 }
15800 }
15801
15802
15803 /* Value is non-zero if glyph row ROW in window W should be
15804 used to hold the cursor. */
15805
15806 static int
15807 cursor_row_p (w, row)
15808 struct window *w;
15809 struct glyph_row *row;
15810 {
15811 int cursor_row_p = 1;
15812
15813 if (PT == MATRIX_ROW_END_CHARPOS (row))
15814 {
15815 /* If the row ends with a newline from a string, we don't want
15816 the cursor there, but we still want it at the start of the
15817 string if the string starts in this row.
15818 If the row is continued it doesn't end in a newline. */
15819 if (CHARPOS (row->end.string_pos) >= 0)
15820 cursor_row_p = (row->continued_p
15821 || PT >= MATRIX_ROW_START_CHARPOS (row));
15822 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15823 {
15824 /* If the row ends in middle of a real character,
15825 and the line is continued, we want the cursor here.
15826 That's because MATRIX_ROW_END_CHARPOS would equal
15827 PT if PT is before the character. */
15828 if (!row->ends_in_ellipsis_p)
15829 cursor_row_p = row->continued_p;
15830 else
15831 /* If the row ends in an ellipsis, then
15832 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15833 We want that position to be displayed after the ellipsis. */
15834 cursor_row_p = 0;
15835 }
15836 /* If the row ends at ZV, display the cursor at the end of that
15837 row instead of at the start of the row below. */
15838 else if (row->ends_at_zv_p)
15839 cursor_row_p = 1;
15840 else
15841 cursor_row_p = 0;
15842 }
15843
15844 return cursor_row_p;
15845 }
15846
15847
15848 /* Construct the glyph row IT->glyph_row in the desired matrix of
15849 IT->w from text at the current position of IT. See dispextern.h
15850 for an overview of struct it. Value is non-zero if
15851 IT->glyph_row displays text, as opposed to a line displaying ZV
15852 only. */
15853
15854 static int
15855 display_line (it)
15856 struct it *it;
15857 {
15858 struct glyph_row *row = it->glyph_row;
15859 Lisp_Object overlay_arrow_string;
15860
15861 /* We always start displaying at hpos zero even if hscrolled. */
15862 xassert (it->hpos == 0 && it->current_x == 0);
15863
15864 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15865 >= it->w->desired_matrix->nrows)
15866 {
15867 it->w->nrows_scale_factor++;
15868 fonts_changed_p = 1;
15869 return 0;
15870 }
15871
15872 /* Is IT->w showing the region? */
15873 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15874
15875 /* Clear the result glyph row and enable it. */
15876 prepare_desired_row (row);
15877
15878 row->y = it->current_y;
15879 row->start = it->start;
15880 row->continuation_lines_width = it->continuation_lines_width;
15881 row->displays_text_p = 1;
15882 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15883 it->starts_in_middle_of_char_p = 0;
15884
15885 /* Arrange the overlays nicely for our purposes. Usually, we call
15886 display_line on only one line at a time, in which case this
15887 can't really hurt too much, or we call it on lines which appear
15888 one after another in the buffer, in which case all calls to
15889 recenter_overlay_lists but the first will be pretty cheap. */
15890 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15891
15892 /* Move over display elements that are not visible because we are
15893 hscrolled. This may stop at an x-position < IT->first_visible_x
15894 if the first glyph is partially visible or if we hit a line end. */
15895 if (it->current_x < it->first_visible_x)
15896 {
15897 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15898 MOVE_TO_POS | MOVE_TO_X);
15899 }
15900
15901 /* Get the initial row height. This is either the height of the
15902 text hscrolled, if there is any, or zero. */
15903 row->ascent = it->max_ascent;
15904 row->height = it->max_ascent + it->max_descent;
15905 row->phys_ascent = it->max_phys_ascent;
15906 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15907 row->extra_line_spacing = it->max_extra_line_spacing;
15908
15909 /* Loop generating characters. The loop is left with IT on the next
15910 character to display. */
15911 while (1)
15912 {
15913 int n_glyphs_before, hpos_before, x_before;
15914 int x, i, nglyphs;
15915 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15916
15917 /* Retrieve the next thing to display. Value is zero if end of
15918 buffer reached. */
15919 if (!get_next_display_element (it))
15920 {
15921 /* Maybe add a space at the end of this line that is used to
15922 display the cursor there under X. Set the charpos of the
15923 first glyph of blank lines not corresponding to any text
15924 to -1. */
15925 #ifdef HAVE_WINDOW_SYSTEM
15926 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15927 row->exact_window_width_line_p = 1;
15928 else
15929 #endif /* HAVE_WINDOW_SYSTEM */
15930 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15931 || row->used[TEXT_AREA] == 0)
15932 {
15933 row->glyphs[TEXT_AREA]->charpos = -1;
15934 row->displays_text_p = 0;
15935
15936 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15937 && (!MINI_WINDOW_P (it->w)
15938 || (minibuf_level && EQ (it->window, minibuf_window))))
15939 row->indicate_empty_line_p = 1;
15940 }
15941
15942 it->continuation_lines_width = 0;
15943 row->ends_at_zv_p = 1;
15944 break;
15945 }
15946
15947 /* Now, get the metrics of what we want to display. This also
15948 generates glyphs in `row' (which is IT->glyph_row). */
15949 n_glyphs_before = row->used[TEXT_AREA];
15950 x = it->current_x;
15951
15952 /* Remember the line height so far in case the next element doesn't
15953 fit on the line. */
15954 if (!it->truncate_lines_p)
15955 {
15956 ascent = it->max_ascent;
15957 descent = it->max_descent;
15958 phys_ascent = it->max_phys_ascent;
15959 phys_descent = it->max_phys_descent;
15960 }
15961
15962 PRODUCE_GLYPHS (it);
15963
15964 /* If this display element was in marginal areas, continue with
15965 the next one. */
15966 if (it->area != TEXT_AREA)
15967 {
15968 row->ascent = max (row->ascent, it->max_ascent);
15969 row->height = max (row->height, it->max_ascent + it->max_descent);
15970 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15971 row->phys_height = max (row->phys_height,
15972 it->max_phys_ascent + it->max_phys_descent);
15973 row->extra_line_spacing = max (row->extra_line_spacing,
15974 it->max_extra_line_spacing);
15975 set_iterator_to_next (it, 1);
15976 continue;
15977 }
15978
15979 /* Does the display element fit on the line? If we truncate
15980 lines, we should draw past the right edge of the window. If
15981 we don't truncate, we want to stop so that we can display the
15982 continuation glyph before the right margin. If lines are
15983 continued, there are two possible strategies for characters
15984 resulting in more than 1 glyph (e.g. tabs): Display as many
15985 glyphs as possible in this line and leave the rest for the
15986 continuation line, or display the whole element in the next
15987 line. Original redisplay did the former, so we do it also. */
15988 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15989 hpos_before = it->hpos;
15990 x_before = x;
15991
15992 if (/* Not a newline. */
15993 nglyphs > 0
15994 /* Glyphs produced fit entirely in the line. */
15995 && it->current_x < it->last_visible_x)
15996 {
15997 it->hpos += nglyphs;
15998 row->ascent = max (row->ascent, it->max_ascent);
15999 row->height = max (row->height, it->max_ascent + it->max_descent);
16000 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16001 row->phys_height = max (row->phys_height,
16002 it->max_phys_ascent + it->max_phys_descent);
16003 row->extra_line_spacing = max (row->extra_line_spacing,
16004 it->max_extra_line_spacing);
16005 if (it->current_x - it->pixel_width < it->first_visible_x)
16006 row->x = x - it->first_visible_x;
16007 }
16008 else
16009 {
16010 int new_x;
16011 struct glyph *glyph;
16012
16013 for (i = 0; i < nglyphs; ++i, x = new_x)
16014 {
16015 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
16016 new_x = x + glyph->pixel_width;
16017
16018 if (/* Lines are continued. */
16019 !it->truncate_lines_p
16020 && (/* Glyph doesn't fit on the line. */
16021 new_x > it->last_visible_x
16022 /* Or it fits exactly on a window system frame. */
16023 || (new_x == it->last_visible_x
16024 && FRAME_WINDOW_P (it->f))))
16025 {
16026 /* End of a continued line. */
16027
16028 if (it->hpos == 0
16029 || (new_x == it->last_visible_x
16030 && FRAME_WINDOW_P (it->f)))
16031 {
16032 /* Current glyph is the only one on the line or
16033 fits exactly on the line. We must continue
16034 the line because we can't draw the cursor
16035 after the glyph. */
16036 row->continued_p = 1;
16037 it->current_x = new_x;
16038 it->continuation_lines_width += new_x;
16039 ++it->hpos;
16040 if (i == nglyphs - 1)
16041 {
16042 set_iterator_to_next (it, 1);
16043 #ifdef HAVE_WINDOW_SYSTEM
16044 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16045 {
16046 if (!get_next_display_element (it))
16047 {
16048 row->exact_window_width_line_p = 1;
16049 it->continuation_lines_width = 0;
16050 row->continued_p = 0;
16051 row->ends_at_zv_p = 1;
16052 }
16053 else if (ITERATOR_AT_END_OF_LINE_P (it))
16054 {
16055 row->continued_p = 0;
16056 row->exact_window_width_line_p = 1;
16057 }
16058 }
16059 #endif /* HAVE_WINDOW_SYSTEM */
16060 }
16061 }
16062 else if (CHAR_GLYPH_PADDING_P (*glyph)
16063 && !FRAME_WINDOW_P (it->f))
16064 {
16065 /* A padding glyph that doesn't fit on this line.
16066 This means the whole character doesn't fit
16067 on the line. */
16068 row->used[TEXT_AREA] = n_glyphs_before;
16069
16070 /* Fill the rest of the row with continuation
16071 glyphs like in 20.x. */
16072 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16073 < row->glyphs[1 + TEXT_AREA])
16074 produce_special_glyphs (it, IT_CONTINUATION);
16075
16076 row->continued_p = 1;
16077 it->current_x = x_before;
16078 it->continuation_lines_width += x_before;
16079
16080 /* Restore the height to what it was before the
16081 element not fitting on the line. */
16082 it->max_ascent = ascent;
16083 it->max_descent = descent;
16084 it->max_phys_ascent = phys_ascent;
16085 it->max_phys_descent = phys_descent;
16086 }
16087 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16088 {
16089 /* A TAB that extends past the right edge of the
16090 window. This produces a single glyph on
16091 window system frames. We leave the glyph in
16092 this row and let it fill the row, but don't
16093 consume the TAB. */
16094 it->continuation_lines_width += it->last_visible_x;
16095 row->ends_in_middle_of_char_p = 1;
16096 row->continued_p = 1;
16097 glyph->pixel_width = it->last_visible_x - x;
16098 it->starts_in_middle_of_char_p = 1;
16099 }
16100 else
16101 {
16102 /* Something other than a TAB that draws past
16103 the right edge of the window. Restore
16104 positions to values before the element. */
16105 row->used[TEXT_AREA] = n_glyphs_before + i;
16106
16107 /* Display continuation glyphs. */
16108 if (!FRAME_WINDOW_P (it->f))
16109 produce_special_glyphs (it, IT_CONTINUATION);
16110 row->continued_p = 1;
16111
16112 it->current_x = x_before;
16113 it->continuation_lines_width += x;
16114 extend_face_to_end_of_line (it);
16115
16116 if (nglyphs > 1 && i > 0)
16117 {
16118 row->ends_in_middle_of_char_p = 1;
16119 it->starts_in_middle_of_char_p = 1;
16120 }
16121
16122 /* Restore the height to what it was before the
16123 element not fitting on the line. */
16124 it->max_ascent = ascent;
16125 it->max_descent = descent;
16126 it->max_phys_ascent = phys_ascent;
16127 it->max_phys_descent = phys_descent;
16128 }
16129
16130 break;
16131 }
16132 else if (new_x > it->first_visible_x)
16133 {
16134 /* Increment number of glyphs actually displayed. */
16135 ++it->hpos;
16136
16137 if (x < it->first_visible_x)
16138 /* Glyph is partially visible, i.e. row starts at
16139 negative X position. */
16140 row->x = x - it->first_visible_x;
16141 }
16142 else
16143 {
16144 /* Glyph is completely off the left margin of the
16145 window. This should not happen because of the
16146 move_it_in_display_line at the start of this
16147 function, unless the text display area of the
16148 window is empty. */
16149 xassert (it->first_visible_x <= it->last_visible_x);
16150 }
16151 }
16152
16153 row->ascent = max (row->ascent, it->max_ascent);
16154 row->height = max (row->height, it->max_ascent + it->max_descent);
16155 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16156 row->phys_height = max (row->phys_height,
16157 it->max_phys_ascent + it->max_phys_descent);
16158 row->extra_line_spacing = max (row->extra_line_spacing,
16159 it->max_extra_line_spacing);
16160
16161 /* End of this display line if row is continued. */
16162 if (row->continued_p || row->ends_at_zv_p)
16163 break;
16164 }
16165
16166 at_end_of_line:
16167 /* Is this a line end? If yes, we're also done, after making
16168 sure that a non-default face is extended up to the right
16169 margin of the window. */
16170 if (ITERATOR_AT_END_OF_LINE_P (it))
16171 {
16172 int used_before = row->used[TEXT_AREA];
16173
16174 row->ends_in_newline_from_string_p = STRINGP (it->object);
16175
16176 #ifdef HAVE_WINDOW_SYSTEM
16177 /* Add a space at the end of the line that is used to
16178 display the cursor there. */
16179 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16180 append_space_for_newline (it, 0);
16181 #endif /* HAVE_WINDOW_SYSTEM */
16182
16183 /* Extend the face to the end of the line. */
16184 extend_face_to_end_of_line (it);
16185
16186 /* Make sure we have the position. */
16187 if (used_before == 0)
16188 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16189
16190 /* Consume the line end. This skips over invisible lines. */
16191 set_iterator_to_next (it, 1);
16192 it->continuation_lines_width = 0;
16193 break;
16194 }
16195
16196 /* Proceed with next display element. Note that this skips
16197 over lines invisible because of selective display. */
16198 set_iterator_to_next (it, 1);
16199
16200 /* If we truncate lines, we are done when the last displayed
16201 glyphs reach past the right margin of the window. */
16202 if (it->truncate_lines_p
16203 && (FRAME_WINDOW_P (it->f)
16204 ? (it->current_x >= it->last_visible_x)
16205 : (it->current_x > it->last_visible_x)))
16206 {
16207 /* Maybe add truncation glyphs. */
16208 if (!FRAME_WINDOW_P (it->f))
16209 {
16210 int i, n;
16211
16212 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16213 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16214 break;
16215
16216 for (n = row->used[TEXT_AREA]; i < n; ++i)
16217 {
16218 row->used[TEXT_AREA] = i;
16219 produce_special_glyphs (it, IT_TRUNCATION);
16220 }
16221 }
16222 #ifdef HAVE_WINDOW_SYSTEM
16223 else
16224 {
16225 /* Don't truncate if we can overflow newline into fringe. */
16226 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16227 {
16228 if (!get_next_display_element (it))
16229 {
16230 it->continuation_lines_width = 0;
16231 row->ends_at_zv_p = 1;
16232 row->exact_window_width_line_p = 1;
16233 break;
16234 }
16235 if (ITERATOR_AT_END_OF_LINE_P (it))
16236 {
16237 row->exact_window_width_line_p = 1;
16238 goto at_end_of_line;
16239 }
16240 }
16241 }
16242 #endif /* HAVE_WINDOW_SYSTEM */
16243
16244 row->truncated_on_right_p = 1;
16245 it->continuation_lines_width = 0;
16246 reseat_at_next_visible_line_start (it, 0);
16247 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16248 it->hpos = hpos_before;
16249 it->current_x = x_before;
16250 break;
16251 }
16252 }
16253
16254 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16255 at the left window margin. */
16256 if (it->first_visible_x
16257 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16258 {
16259 if (!FRAME_WINDOW_P (it->f))
16260 insert_left_trunc_glyphs (it);
16261 row->truncated_on_left_p = 1;
16262 }
16263
16264 /* If the start of this line is the overlay arrow-position, then
16265 mark this glyph row as the one containing the overlay arrow.
16266 This is clearly a mess with variable size fonts. It would be
16267 better to let it be displayed like cursors under X. */
16268 if ((row->displays_text_p || !overlay_arrow_seen)
16269 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16270 !NILP (overlay_arrow_string)))
16271 {
16272 /* Overlay arrow in window redisplay is a fringe bitmap. */
16273 if (STRINGP (overlay_arrow_string))
16274 {
16275 struct glyph_row *arrow_row
16276 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16277 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16278 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16279 struct glyph *p = row->glyphs[TEXT_AREA];
16280 struct glyph *p2, *end;
16281
16282 /* Copy the arrow glyphs. */
16283 while (glyph < arrow_end)
16284 *p++ = *glyph++;
16285
16286 /* Throw away padding glyphs. */
16287 p2 = p;
16288 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16289 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16290 ++p2;
16291 if (p2 > p)
16292 {
16293 while (p2 < end)
16294 *p++ = *p2++;
16295 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16296 }
16297 }
16298 else
16299 {
16300 xassert (INTEGERP (overlay_arrow_string));
16301 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16302 }
16303 overlay_arrow_seen = 1;
16304 }
16305
16306 /* Compute pixel dimensions of this line. */
16307 compute_line_metrics (it);
16308
16309 /* Remember the position at which this line ends. */
16310 row->end = it->current;
16311
16312 /* Record whether this row ends inside an ellipsis. */
16313 row->ends_in_ellipsis_p
16314 = (it->method == GET_FROM_DISPLAY_VECTOR
16315 && it->ellipsis_p);
16316
16317 /* Save fringe bitmaps in this row. */
16318 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16319 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16320 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16321 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16322
16323 it->left_user_fringe_bitmap = 0;
16324 it->left_user_fringe_face_id = 0;
16325 it->right_user_fringe_bitmap = 0;
16326 it->right_user_fringe_face_id = 0;
16327
16328 /* Maybe set the cursor. */
16329 if (it->w->cursor.vpos < 0
16330 && PT >= MATRIX_ROW_START_CHARPOS (row)
16331 && PT <= MATRIX_ROW_END_CHARPOS (row)
16332 && cursor_row_p (it->w, row))
16333 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16334
16335 /* Highlight trailing whitespace. */
16336 if (!NILP (Vshow_trailing_whitespace))
16337 highlight_trailing_whitespace (it->f, it->glyph_row);
16338
16339 /* Prepare for the next line. This line starts horizontally at (X
16340 HPOS) = (0 0). Vertical positions are incremented. As a
16341 convenience for the caller, IT->glyph_row is set to the next
16342 row to be used. */
16343 it->current_x = it->hpos = 0;
16344 it->current_y += row->height;
16345 ++it->vpos;
16346 ++it->glyph_row;
16347 it->start = it->current;
16348 return row->displays_text_p;
16349 }
16350
16351
16352 \f
16353 /***********************************************************************
16354 Menu Bar
16355 ***********************************************************************/
16356
16357 /* Redisplay the menu bar in the frame for window W.
16358
16359 The menu bar of X frames that don't have X toolkit support is
16360 displayed in a special window W->frame->menu_bar_window.
16361
16362 The menu bar of terminal frames is treated specially as far as
16363 glyph matrices are concerned. Menu bar lines are not part of
16364 windows, so the update is done directly on the frame matrix rows
16365 for the menu bar. */
16366
16367 static void
16368 display_menu_bar (w)
16369 struct window *w;
16370 {
16371 struct frame *f = XFRAME (WINDOW_FRAME (w));
16372 struct it it;
16373 Lisp_Object items;
16374 int i;
16375
16376 /* Don't do all this for graphical frames. */
16377 #ifdef HAVE_NTGUI
16378 if (!NILP (Vwindow_system))
16379 return;
16380 #endif
16381 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16382 if (FRAME_X_P (f))
16383 return;
16384 #endif
16385 #ifdef MAC_OS
16386 if (FRAME_MAC_P (f))
16387 return;
16388 #endif
16389
16390 #ifdef USE_X_TOOLKIT
16391 xassert (!FRAME_WINDOW_P (f));
16392 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16393 it.first_visible_x = 0;
16394 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16395 #else /* not USE_X_TOOLKIT */
16396 if (FRAME_WINDOW_P (f))
16397 {
16398 /* Menu bar lines are displayed in the desired matrix of the
16399 dummy window menu_bar_window. */
16400 struct window *menu_w;
16401 xassert (WINDOWP (f->menu_bar_window));
16402 menu_w = XWINDOW (f->menu_bar_window);
16403 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16404 MENU_FACE_ID);
16405 it.first_visible_x = 0;
16406 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16407 }
16408 else
16409 {
16410 /* This is a TTY frame, i.e. character hpos/vpos are used as
16411 pixel x/y. */
16412 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16413 MENU_FACE_ID);
16414 it.first_visible_x = 0;
16415 it.last_visible_x = FRAME_COLS (f);
16416 }
16417 #endif /* not USE_X_TOOLKIT */
16418
16419 if (! mode_line_inverse_video)
16420 /* Force the menu-bar to be displayed in the default face. */
16421 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16422
16423 /* Clear all rows of the menu bar. */
16424 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16425 {
16426 struct glyph_row *row = it.glyph_row + i;
16427 clear_glyph_row (row);
16428 row->enabled_p = 1;
16429 row->full_width_p = 1;
16430 }
16431
16432 /* Display all items of the menu bar. */
16433 items = FRAME_MENU_BAR_ITEMS (it.f);
16434 for (i = 0; i < XVECTOR (items)->size; i += 4)
16435 {
16436 Lisp_Object string;
16437
16438 /* Stop at nil string. */
16439 string = AREF (items, i + 1);
16440 if (NILP (string))
16441 break;
16442
16443 /* Remember where item was displayed. */
16444 AREF (items, i + 3) = make_number (it.hpos);
16445
16446 /* Display the item, pad with one space. */
16447 if (it.current_x < it.last_visible_x)
16448 display_string (NULL, string, Qnil, 0, 0, &it,
16449 SCHARS (string) + 1, 0, 0, -1);
16450 }
16451
16452 /* Fill out the line with spaces. */
16453 if (it.current_x < it.last_visible_x)
16454 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16455
16456 /* Compute the total height of the lines. */
16457 compute_line_metrics (&it);
16458 }
16459
16460
16461 \f
16462 /***********************************************************************
16463 Mode Line
16464 ***********************************************************************/
16465
16466 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16467 FORCE is non-zero, redisplay mode lines unconditionally.
16468 Otherwise, redisplay only mode lines that are garbaged. Value is
16469 the number of windows whose mode lines were redisplayed. */
16470
16471 static int
16472 redisplay_mode_lines (window, force)
16473 Lisp_Object window;
16474 int force;
16475 {
16476 int nwindows = 0;
16477
16478 while (!NILP (window))
16479 {
16480 struct window *w = XWINDOW (window);
16481
16482 if (WINDOWP (w->hchild))
16483 nwindows += redisplay_mode_lines (w->hchild, force);
16484 else if (WINDOWP (w->vchild))
16485 nwindows += redisplay_mode_lines (w->vchild, force);
16486 else if (force
16487 || FRAME_GARBAGED_P (XFRAME (w->frame))
16488 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16489 {
16490 struct text_pos lpoint;
16491 struct buffer *old = current_buffer;
16492
16493 /* Set the window's buffer for the mode line display. */
16494 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16495 set_buffer_internal_1 (XBUFFER (w->buffer));
16496
16497 /* Point refers normally to the selected window. For any
16498 other window, set up appropriate value. */
16499 if (!EQ (window, selected_window))
16500 {
16501 struct text_pos pt;
16502
16503 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16504 if (CHARPOS (pt) < BEGV)
16505 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16506 else if (CHARPOS (pt) > (ZV - 1))
16507 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16508 else
16509 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16510 }
16511
16512 /* Display mode lines. */
16513 clear_glyph_matrix (w->desired_matrix);
16514 if (display_mode_lines (w))
16515 {
16516 ++nwindows;
16517 w->must_be_updated_p = 1;
16518 }
16519
16520 /* Restore old settings. */
16521 set_buffer_internal_1 (old);
16522 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16523 }
16524
16525 window = w->next;
16526 }
16527
16528 return nwindows;
16529 }
16530
16531
16532 /* Display the mode and/or top line of window W. Value is the number
16533 of mode lines displayed. */
16534
16535 static int
16536 display_mode_lines (w)
16537 struct window *w;
16538 {
16539 Lisp_Object old_selected_window, old_selected_frame;
16540 int n = 0;
16541
16542 old_selected_frame = selected_frame;
16543 selected_frame = w->frame;
16544 old_selected_window = selected_window;
16545 XSETWINDOW (selected_window, w);
16546
16547 /* These will be set while the mode line specs are processed. */
16548 line_number_displayed = 0;
16549 w->column_number_displayed = Qnil;
16550
16551 if (WINDOW_WANTS_MODELINE_P (w))
16552 {
16553 struct window *sel_w = XWINDOW (old_selected_window);
16554
16555 /* Select mode line face based on the real selected window. */
16556 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16557 current_buffer->mode_line_format);
16558 ++n;
16559 }
16560
16561 if (WINDOW_WANTS_HEADER_LINE_P (w))
16562 {
16563 display_mode_line (w, HEADER_LINE_FACE_ID,
16564 current_buffer->header_line_format);
16565 ++n;
16566 }
16567
16568 selected_frame = old_selected_frame;
16569 selected_window = old_selected_window;
16570 return n;
16571 }
16572
16573
16574 /* Display mode or top line of window W. FACE_ID specifies which line
16575 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16576 FORMAT is the mode line format to display. Value is the pixel
16577 height of the mode line displayed. */
16578
16579 static int
16580 display_mode_line (w, face_id, format)
16581 struct window *w;
16582 enum face_id face_id;
16583 Lisp_Object format;
16584 {
16585 struct it it;
16586 struct face *face;
16587 int count = SPECPDL_INDEX ();
16588
16589 init_iterator (&it, w, -1, -1, NULL, face_id);
16590 prepare_desired_row (it.glyph_row);
16591
16592 it.glyph_row->mode_line_p = 1;
16593
16594 if (! mode_line_inverse_video)
16595 /* Force the mode-line to be displayed in the default face. */
16596 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16597
16598 record_unwind_protect (unwind_format_mode_line,
16599 format_mode_line_unwind_data (NULL, 0));
16600
16601 mode_line_target = MODE_LINE_DISPLAY;
16602
16603 /* Temporarily make frame's keyboard the current kboard so that
16604 kboard-local variables in the mode_line_format will get the right
16605 values. */
16606 push_frame_kboard (it.f);
16607 record_unwind_save_match_data ();
16608 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16609 pop_frame_kboard ();
16610
16611 unbind_to (count, Qnil);
16612
16613 /* Fill up with spaces. */
16614 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16615
16616 compute_line_metrics (&it);
16617 it.glyph_row->full_width_p = 1;
16618 it.glyph_row->continued_p = 0;
16619 it.glyph_row->truncated_on_left_p = 0;
16620 it.glyph_row->truncated_on_right_p = 0;
16621
16622 /* Make a 3D mode-line have a shadow at its right end. */
16623 face = FACE_FROM_ID (it.f, face_id);
16624 extend_face_to_end_of_line (&it);
16625 if (face->box != FACE_NO_BOX)
16626 {
16627 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16628 + it.glyph_row->used[TEXT_AREA] - 1);
16629 last->right_box_line_p = 1;
16630 }
16631
16632 return it.glyph_row->height;
16633 }
16634
16635 /* Move element ELT in LIST to the front of LIST.
16636 Return the updated list. */
16637
16638 static Lisp_Object
16639 move_elt_to_front (elt, list)
16640 Lisp_Object elt, list;
16641 {
16642 register Lisp_Object tail, prev;
16643 register Lisp_Object tem;
16644
16645 tail = list;
16646 prev = Qnil;
16647 while (CONSP (tail))
16648 {
16649 tem = XCAR (tail);
16650
16651 if (EQ (elt, tem))
16652 {
16653 /* Splice out the link TAIL. */
16654 if (NILP (prev))
16655 list = XCDR (tail);
16656 else
16657 Fsetcdr (prev, XCDR (tail));
16658
16659 /* Now make it the first. */
16660 Fsetcdr (tail, list);
16661 return tail;
16662 }
16663 else
16664 prev = tail;
16665 tail = XCDR (tail);
16666 QUIT;
16667 }
16668
16669 /* Not found--return unchanged LIST. */
16670 return list;
16671 }
16672
16673 /* Contribute ELT to the mode line for window IT->w. How it
16674 translates into text depends on its data type.
16675
16676 IT describes the display environment in which we display, as usual.
16677
16678 DEPTH is the depth in recursion. It is used to prevent
16679 infinite recursion here.
16680
16681 FIELD_WIDTH is the number of characters the display of ELT should
16682 occupy in the mode line, and PRECISION is the maximum number of
16683 characters to display from ELT's representation. See
16684 display_string for details.
16685
16686 Returns the hpos of the end of the text generated by ELT.
16687
16688 PROPS is a property list to add to any string we encounter.
16689
16690 If RISKY is nonzero, remove (disregard) any properties in any string
16691 we encounter, and ignore :eval and :propertize.
16692
16693 The global variable `mode_line_target' determines whether the
16694 output is passed to `store_mode_line_noprop',
16695 `store_mode_line_string', or `display_string'. */
16696
16697 static int
16698 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16699 struct it *it;
16700 int depth;
16701 int field_width, precision;
16702 Lisp_Object elt, props;
16703 int risky;
16704 {
16705 int n = 0, field, prec;
16706 int literal = 0;
16707
16708 tail_recurse:
16709 if (depth > 100)
16710 elt = build_string ("*too-deep*");
16711
16712 depth++;
16713
16714 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16715 {
16716 case Lisp_String:
16717 {
16718 /* A string: output it and check for %-constructs within it. */
16719 unsigned char c;
16720 int offset = 0;
16721
16722 if (SCHARS (elt) > 0
16723 && (!NILP (props) || risky))
16724 {
16725 Lisp_Object oprops, aelt;
16726 oprops = Ftext_properties_at (make_number (0), elt);
16727
16728 /* If the starting string's properties are not what
16729 we want, translate the string. Also, if the string
16730 is risky, do that anyway. */
16731
16732 if (NILP (Fequal (props, oprops)) || risky)
16733 {
16734 /* If the starting string has properties,
16735 merge the specified ones onto the existing ones. */
16736 if (! NILP (oprops) && !risky)
16737 {
16738 Lisp_Object tem;
16739
16740 oprops = Fcopy_sequence (oprops);
16741 tem = props;
16742 while (CONSP (tem))
16743 {
16744 oprops = Fplist_put (oprops, XCAR (tem),
16745 XCAR (XCDR (tem)));
16746 tem = XCDR (XCDR (tem));
16747 }
16748 props = oprops;
16749 }
16750
16751 aelt = Fassoc (elt, mode_line_proptrans_alist);
16752 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16753 {
16754 /* AELT is what we want. Move it to the front
16755 without consing. */
16756 elt = XCAR (aelt);
16757 mode_line_proptrans_alist
16758 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16759 }
16760 else
16761 {
16762 Lisp_Object tem;
16763
16764 /* If AELT has the wrong props, it is useless.
16765 so get rid of it. */
16766 if (! NILP (aelt))
16767 mode_line_proptrans_alist
16768 = Fdelq (aelt, mode_line_proptrans_alist);
16769
16770 elt = Fcopy_sequence (elt);
16771 Fset_text_properties (make_number (0), Flength (elt),
16772 props, elt);
16773 /* Add this item to mode_line_proptrans_alist. */
16774 mode_line_proptrans_alist
16775 = Fcons (Fcons (elt, props),
16776 mode_line_proptrans_alist);
16777 /* Truncate mode_line_proptrans_alist
16778 to at most 50 elements. */
16779 tem = Fnthcdr (make_number (50),
16780 mode_line_proptrans_alist);
16781 if (! NILP (tem))
16782 XSETCDR (tem, Qnil);
16783 }
16784 }
16785 }
16786
16787 offset = 0;
16788
16789 if (literal)
16790 {
16791 prec = precision - n;
16792 switch (mode_line_target)
16793 {
16794 case MODE_LINE_NOPROP:
16795 case MODE_LINE_TITLE:
16796 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16797 break;
16798 case MODE_LINE_STRING:
16799 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16800 break;
16801 case MODE_LINE_DISPLAY:
16802 n += display_string (NULL, elt, Qnil, 0, 0, it,
16803 0, prec, 0, STRING_MULTIBYTE (elt));
16804 break;
16805 }
16806
16807 break;
16808 }
16809
16810 /* Handle the non-literal case. */
16811
16812 while ((precision <= 0 || n < precision)
16813 && SREF (elt, offset) != 0
16814 && (mode_line_target != MODE_LINE_DISPLAY
16815 || it->current_x < it->last_visible_x))
16816 {
16817 int last_offset = offset;
16818
16819 /* Advance to end of string or next format specifier. */
16820 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16821 ;
16822
16823 if (offset - 1 != last_offset)
16824 {
16825 int nchars, nbytes;
16826
16827 /* Output to end of string or up to '%'. Field width
16828 is length of string. Don't output more than
16829 PRECISION allows us. */
16830 offset--;
16831
16832 prec = c_string_width (SDATA (elt) + last_offset,
16833 offset - last_offset, precision - n,
16834 &nchars, &nbytes);
16835
16836 switch (mode_line_target)
16837 {
16838 case MODE_LINE_NOPROP:
16839 case MODE_LINE_TITLE:
16840 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16841 break;
16842 case MODE_LINE_STRING:
16843 {
16844 int bytepos = last_offset;
16845 int charpos = string_byte_to_char (elt, bytepos);
16846 int endpos = (precision <= 0
16847 ? string_byte_to_char (elt, offset)
16848 : charpos + nchars);
16849
16850 n += store_mode_line_string (NULL,
16851 Fsubstring (elt, make_number (charpos),
16852 make_number (endpos)),
16853 0, 0, 0, Qnil);
16854 }
16855 break;
16856 case MODE_LINE_DISPLAY:
16857 {
16858 int bytepos = last_offset;
16859 int charpos = string_byte_to_char (elt, bytepos);
16860
16861 if (precision <= 0)
16862 nchars = string_byte_to_char (elt, offset) - charpos;
16863 n += display_string (NULL, elt, Qnil, 0, charpos,
16864 it, 0, nchars, 0,
16865 STRING_MULTIBYTE (elt));
16866 }
16867 break;
16868 }
16869 }
16870 else /* c == '%' */
16871 {
16872 int percent_position = offset;
16873
16874 /* Get the specified minimum width. Zero means
16875 don't pad. */
16876 field = 0;
16877 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16878 field = field * 10 + c - '0';
16879
16880 /* Don't pad beyond the total padding allowed. */
16881 if (field_width - n > 0 && field > field_width - n)
16882 field = field_width - n;
16883
16884 /* Note that either PRECISION <= 0 or N < PRECISION. */
16885 prec = precision - n;
16886
16887 if (c == 'M')
16888 n += display_mode_element (it, depth, field, prec,
16889 Vglobal_mode_string, props,
16890 risky);
16891 else if (c != 0)
16892 {
16893 int multibyte;
16894 int bytepos, charpos;
16895 unsigned char *spec;
16896
16897 bytepos = percent_position;
16898 charpos = (STRING_MULTIBYTE (elt)
16899 ? string_byte_to_char (elt, bytepos)
16900 : bytepos);
16901
16902 spec
16903 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16904
16905 switch (mode_line_target)
16906 {
16907 case MODE_LINE_NOPROP:
16908 case MODE_LINE_TITLE:
16909 n += store_mode_line_noprop (spec, field, prec);
16910 break;
16911 case MODE_LINE_STRING:
16912 {
16913 int len = strlen (spec);
16914 Lisp_Object tem = make_string (spec, len);
16915 props = Ftext_properties_at (make_number (charpos), elt);
16916 /* Should only keep face property in props */
16917 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16918 }
16919 break;
16920 case MODE_LINE_DISPLAY:
16921 {
16922 int nglyphs_before, nwritten;
16923
16924 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16925 nwritten = display_string (spec, Qnil, elt,
16926 charpos, 0, it,
16927 field, prec, 0,
16928 multibyte);
16929
16930 /* Assign to the glyphs written above the
16931 string where the `%x' came from, position
16932 of the `%'. */
16933 if (nwritten > 0)
16934 {
16935 struct glyph *glyph
16936 = (it->glyph_row->glyphs[TEXT_AREA]
16937 + nglyphs_before);
16938 int i;
16939
16940 for (i = 0; i < nwritten; ++i)
16941 {
16942 glyph[i].object = elt;
16943 glyph[i].charpos = charpos;
16944 }
16945
16946 n += nwritten;
16947 }
16948 }
16949 break;
16950 }
16951 }
16952 else /* c == 0 */
16953 break;
16954 }
16955 }
16956 }
16957 break;
16958
16959 case Lisp_Symbol:
16960 /* A symbol: process the value of the symbol recursively
16961 as if it appeared here directly. Avoid error if symbol void.
16962 Special case: if value of symbol is a string, output the string
16963 literally. */
16964 {
16965 register Lisp_Object tem;
16966
16967 /* If the variable is not marked as risky to set
16968 then its contents are risky to use. */
16969 if (NILP (Fget (elt, Qrisky_local_variable)))
16970 risky = 1;
16971
16972 tem = Fboundp (elt);
16973 if (!NILP (tem))
16974 {
16975 tem = Fsymbol_value (elt);
16976 /* If value is a string, output that string literally:
16977 don't check for % within it. */
16978 if (STRINGP (tem))
16979 literal = 1;
16980
16981 if (!EQ (tem, elt))
16982 {
16983 /* Give up right away for nil or t. */
16984 elt = tem;
16985 goto tail_recurse;
16986 }
16987 }
16988 }
16989 break;
16990
16991 case Lisp_Cons:
16992 {
16993 register Lisp_Object car, tem;
16994
16995 /* A cons cell: five distinct cases.
16996 If first element is :eval or :propertize, do something special.
16997 If first element is a string or a cons, process all the elements
16998 and effectively concatenate them.
16999 If first element is a negative number, truncate displaying cdr to
17000 at most that many characters. If positive, pad (with spaces)
17001 to at least that many characters.
17002 If first element is a symbol, process the cadr or caddr recursively
17003 according to whether the symbol's value is non-nil or nil. */
17004 car = XCAR (elt);
17005 if (EQ (car, QCeval))
17006 {
17007 /* An element of the form (:eval FORM) means evaluate FORM
17008 and use the result as mode line elements. */
17009
17010 if (risky)
17011 break;
17012
17013 if (CONSP (XCDR (elt)))
17014 {
17015 Lisp_Object spec;
17016 spec = safe_eval (XCAR (XCDR (elt)));
17017 n += display_mode_element (it, depth, field_width - n,
17018 precision - n, spec, props,
17019 risky);
17020 }
17021 }
17022 else if (EQ (car, QCpropertize))
17023 {
17024 /* An element of the form (:propertize ELT PROPS...)
17025 means display ELT but applying properties PROPS. */
17026
17027 if (risky)
17028 break;
17029
17030 if (CONSP (XCDR (elt)))
17031 n += display_mode_element (it, depth, field_width - n,
17032 precision - n, XCAR (XCDR (elt)),
17033 XCDR (XCDR (elt)), risky);
17034 }
17035 else if (SYMBOLP (car))
17036 {
17037 tem = Fboundp (car);
17038 elt = XCDR (elt);
17039 if (!CONSP (elt))
17040 goto invalid;
17041 /* elt is now the cdr, and we know it is a cons cell.
17042 Use its car if CAR has a non-nil value. */
17043 if (!NILP (tem))
17044 {
17045 tem = Fsymbol_value (car);
17046 if (!NILP (tem))
17047 {
17048 elt = XCAR (elt);
17049 goto tail_recurse;
17050 }
17051 }
17052 /* Symbol's value is nil (or symbol is unbound)
17053 Get the cddr of the original list
17054 and if possible find the caddr and use that. */
17055 elt = XCDR (elt);
17056 if (NILP (elt))
17057 break;
17058 else if (!CONSP (elt))
17059 goto invalid;
17060 elt = XCAR (elt);
17061 goto tail_recurse;
17062 }
17063 else if (INTEGERP (car))
17064 {
17065 register int lim = XINT (car);
17066 elt = XCDR (elt);
17067 if (lim < 0)
17068 {
17069 /* Negative int means reduce maximum width. */
17070 if (precision <= 0)
17071 precision = -lim;
17072 else
17073 precision = min (precision, -lim);
17074 }
17075 else if (lim > 0)
17076 {
17077 /* Padding specified. Don't let it be more than
17078 current maximum. */
17079 if (precision > 0)
17080 lim = min (precision, lim);
17081
17082 /* If that's more padding than already wanted, queue it.
17083 But don't reduce padding already specified even if
17084 that is beyond the current truncation point. */
17085 field_width = max (lim, field_width);
17086 }
17087 goto tail_recurse;
17088 }
17089 else if (STRINGP (car) || CONSP (car))
17090 {
17091 register int limit = 50;
17092 /* Limit is to protect against circular lists. */
17093 while (CONSP (elt)
17094 && --limit > 0
17095 && (precision <= 0 || n < precision))
17096 {
17097 n += display_mode_element (it, depth,
17098 /* Do padding only after the last
17099 element in the list. */
17100 (! CONSP (XCDR (elt))
17101 ? field_width - n
17102 : 0),
17103 precision - n, XCAR (elt),
17104 props, risky);
17105 elt = XCDR (elt);
17106 }
17107 }
17108 }
17109 break;
17110
17111 default:
17112 invalid:
17113 elt = build_string ("*invalid*");
17114 goto tail_recurse;
17115 }
17116
17117 /* Pad to FIELD_WIDTH. */
17118 if (field_width > 0 && n < field_width)
17119 {
17120 switch (mode_line_target)
17121 {
17122 case MODE_LINE_NOPROP:
17123 case MODE_LINE_TITLE:
17124 n += store_mode_line_noprop ("", field_width - n, 0);
17125 break;
17126 case MODE_LINE_STRING:
17127 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17128 break;
17129 case MODE_LINE_DISPLAY:
17130 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17131 0, 0, 0);
17132 break;
17133 }
17134 }
17135
17136 return n;
17137 }
17138
17139 /* Store a mode-line string element in mode_line_string_list.
17140
17141 If STRING is non-null, display that C string. Otherwise, the Lisp
17142 string LISP_STRING is displayed.
17143
17144 FIELD_WIDTH is the minimum number of output glyphs to produce.
17145 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17146 with spaces. FIELD_WIDTH <= 0 means don't pad.
17147
17148 PRECISION is the maximum number of characters to output from
17149 STRING. PRECISION <= 0 means don't truncate the string.
17150
17151 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17152 properties to the string.
17153
17154 PROPS are the properties to add to the string.
17155 The mode_line_string_face face property is always added to the string.
17156 */
17157
17158 static int
17159 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17160 char *string;
17161 Lisp_Object lisp_string;
17162 int copy_string;
17163 int field_width;
17164 int precision;
17165 Lisp_Object props;
17166 {
17167 int len;
17168 int n = 0;
17169
17170 if (string != NULL)
17171 {
17172 len = strlen (string);
17173 if (precision > 0 && len > precision)
17174 len = precision;
17175 lisp_string = make_string (string, len);
17176 if (NILP (props))
17177 props = mode_line_string_face_prop;
17178 else if (!NILP (mode_line_string_face))
17179 {
17180 Lisp_Object face = Fplist_get (props, Qface);
17181 props = Fcopy_sequence (props);
17182 if (NILP (face))
17183 face = mode_line_string_face;
17184 else
17185 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17186 props = Fplist_put (props, Qface, face);
17187 }
17188 Fadd_text_properties (make_number (0), make_number (len),
17189 props, lisp_string);
17190 }
17191 else
17192 {
17193 len = XFASTINT (Flength (lisp_string));
17194 if (precision > 0 && len > precision)
17195 {
17196 len = precision;
17197 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17198 precision = -1;
17199 }
17200 if (!NILP (mode_line_string_face))
17201 {
17202 Lisp_Object face;
17203 if (NILP (props))
17204 props = Ftext_properties_at (make_number (0), lisp_string);
17205 face = Fplist_get (props, Qface);
17206 if (NILP (face))
17207 face = mode_line_string_face;
17208 else
17209 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17210 props = Fcons (Qface, Fcons (face, Qnil));
17211 if (copy_string)
17212 lisp_string = Fcopy_sequence (lisp_string);
17213 }
17214 if (!NILP (props))
17215 Fadd_text_properties (make_number (0), make_number (len),
17216 props, lisp_string);
17217 }
17218
17219 if (len > 0)
17220 {
17221 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17222 n += len;
17223 }
17224
17225 if (field_width > len)
17226 {
17227 field_width -= len;
17228 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17229 if (!NILP (props))
17230 Fadd_text_properties (make_number (0), make_number (field_width),
17231 props, lisp_string);
17232 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17233 n += field_width;
17234 }
17235
17236 return n;
17237 }
17238
17239
17240 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17241 1, 4, 0,
17242 doc: /* Format a string out of a mode line format specification.
17243 First arg FORMAT specifies the mode line format (see `mode-line-format'
17244 for details) to use.
17245
17246 Optional second arg FACE specifies the face property to put
17247 on all characters for which no face is specified.
17248 t means whatever face the window's mode line currently uses
17249 \(either `mode-line' or `mode-line-inactive', depending).
17250 nil means the default is no face property.
17251 If FACE is an integer, the value string has no text properties.
17252
17253 Optional third and fourth args WINDOW and BUFFER specify the window
17254 and buffer to use as the context for the formatting (defaults
17255 are the selected window and the window's buffer). */)
17256 (format, face, window, buffer)
17257 Lisp_Object format, face, window, buffer;
17258 {
17259 struct it it;
17260 int len;
17261 struct window *w;
17262 struct buffer *old_buffer = NULL;
17263 int face_id = -1;
17264 int no_props = INTEGERP (face);
17265 int count = SPECPDL_INDEX ();
17266 Lisp_Object str;
17267 int string_start = 0;
17268
17269 if (NILP (window))
17270 window = selected_window;
17271 CHECK_WINDOW (window);
17272 w = XWINDOW (window);
17273
17274 if (NILP (buffer))
17275 buffer = w->buffer;
17276 CHECK_BUFFER (buffer);
17277
17278 if (NILP (format))
17279 return build_string ("");
17280
17281 if (no_props)
17282 face = Qnil;
17283
17284 if (!NILP (face))
17285 {
17286 if (EQ (face, Qt))
17287 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17288 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17289 }
17290
17291 if (face_id < 0)
17292 face_id = DEFAULT_FACE_ID;
17293
17294 if (XBUFFER (buffer) != current_buffer)
17295 old_buffer = current_buffer;
17296
17297 /* Save things including mode_line_proptrans_alist,
17298 and set that to nil so that we don't alter the outer value. */
17299 record_unwind_protect (unwind_format_mode_line,
17300 format_mode_line_unwind_data (old_buffer, 1));
17301 mode_line_proptrans_alist = Qnil;
17302
17303 if (old_buffer)
17304 set_buffer_internal_1 (XBUFFER (buffer));
17305
17306 init_iterator (&it, w, -1, -1, NULL, face_id);
17307
17308 if (no_props)
17309 {
17310 mode_line_target = MODE_LINE_NOPROP;
17311 mode_line_string_face_prop = Qnil;
17312 mode_line_string_list = Qnil;
17313 string_start = MODE_LINE_NOPROP_LEN (0);
17314 }
17315 else
17316 {
17317 mode_line_target = MODE_LINE_STRING;
17318 mode_line_string_list = Qnil;
17319 mode_line_string_face = face;
17320 mode_line_string_face_prop
17321 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17322 }
17323
17324 push_frame_kboard (it.f);
17325 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17326 pop_frame_kboard ();
17327
17328 if (no_props)
17329 {
17330 len = MODE_LINE_NOPROP_LEN (string_start);
17331 str = make_string (mode_line_noprop_buf + string_start, len);
17332 }
17333 else
17334 {
17335 mode_line_string_list = Fnreverse (mode_line_string_list);
17336 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17337 make_string ("", 0));
17338 }
17339
17340 unbind_to (count, Qnil);
17341 return str;
17342 }
17343
17344 /* Write a null-terminated, right justified decimal representation of
17345 the positive integer D to BUF using a minimal field width WIDTH. */
17346
17347 static void
17348 pint2str (buf, width, d)
17349 register char *buf;
17350 register int width;
17351 register int d;
17352 {
17353 register char *p = buf;
17354
17355 if (d <= 0)
17356 *p++ = '0';
17357 else
17358 {
17359 while (d > 0)
17360 {
17361 *p++ = d % 10 + '0';
17362 d /= 10;
17363 }
17364 }
17365
17366 for (width -= (int) (p - buf); width > 0; --width)
17367 *p++ = ' ';
17368 *p-- = '\0';
17369 while (p > buf)
17370 {
17371 d = *buf;
17372 *buf++ = *p;
17373 *p-- = d;
17374 }
17375 }
17376
17377 /* Write a null-terminated, right justified decimal and "human
17378 readable" representation of the nonnegative integer D to BUF using
17379 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17380
17381 static const char power_letter[] =
17382 {
17383 0, /* not used */
17384 'k', /* kilo */
17385 'M', /* mega */
17386 'G', /* giga */
17387 'T', /* tera */
17388 'P', /* peta */
17389 'E', /* exa */
17390 'Z', /* zetta */
17391 'Y' /* yotta */
17392 };
17393
17394 static void
17395 pint2hrstr (buf, width, d)
17396 char *buf;
17397 int width;
17398 int d;
17399 {
17400 /* We aim to represent the nonnegative integer D as
17401 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17402 int quotient = d;
17403 int remainder = 0;
17404 /* -1 means: do not use TENTHS. */
17405 int tenths = -1;
17406 int exponent = 0;
17407
17408 /* Length of QUOTIENT.TENTHS as a string. */
17409 int length;
17410
17411 char * psuffix;
17412 char * p;
17413
17414 if (1000 <= quotient)
17415 {
17416 /* Scale to the appropriate EXPONENT. */
17417 do
17418 {
17419 remainder = quotient % 1000;
17420 quotient /= 1000;
17421 exponent++;
17422 }
17423 while (1000 <= quotient);
17424
17425 /* Round to nearest and decide whether to use TENTHS or not. */
17426 if (quotient <= 9)
17427 {
17428 tenths = remainder / 100;
17429 if (50 <= remainder % 100)
17430 {
17431 if (tenths < 9)
17432 tenths++;
17433 else
17434 {
17435 quotient++;
17436 if (quotient == 10)
17437 tenths = -1;
17438 else
17439 tenths = 0;
17440 }
17441 }
17442 }
17443 else
17444 if (500 <= remainder)
17445 {
17446 if (quotient < 999)
17447 quotient++;
17448 else
17449 {
17450 quotient = 1;
17451 exponent++;
17452 tenths = 0;
17453 }
17454 }
17455 }
17456
17457 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17458 if (tenths == -1 && quotient <= 99)
17459 if (quotient <= 9)
17460 length = 1;
17461 else
17462 length = 2;
17463 else
17464 length = 3;
17465 p = psuffix = buf + max (width, length);
17466
17467 /* Print EXPONENT. */
17468 if (exponent)
17469 *psuffix++ = power_letter[exponent];
17470 *psuffix = '\0';
17471
17472 /* Print TENTHS. */
17473 if (tenths >= 0)
17474 {
17475 *--p = '0' + tenths;
17476 *--p = '.';
17477 }
17478
17479 /* Print QUOTIENT. */
17480 do
17481 {
17482 int digit = quotient % 10;
17483 *--p = '0' + digit;
17484 }
17485 while ((quotient /= 10) != 0);
17486
17487 /* Print leading spaces. */
17488 while (buf < p)
17489 *--p = ' ';
17490 }
17491
17492 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17493 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17494 type of CODING_SYSTEM. Return updated pointer into BUF. */
17495
17496 static unsigned char invalid_eol_type[] = "(*invalid*)";
17497
17498 static char *
17499 decode_mode_spec_coding (coding_system, buf, eol_flag)
17500 Lisp_Object coding_system;
17501 register char *buf;
17502 int eol_flag;
17503 {
17504 Lisp_Object val;
17505 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17506 const unsigned char *eol_str;
17507 int eol_str_len;
17508 /* The EOL conversion we are using. */
17509 Lisp_Object eoltype;
17510
17511 val = Fget (coding_system, Qcoding_system);
17512 eoltype = Qnil;
17513
17514 if (!VECTORP (val)) /* Not yet decided. */
17515 {
17516 if (multibyte)
17517 *buf++ = '-';
17518 if (eol_flag)
17519 eoltype = eol_mnemonic_undecided;
17520 /* Don't mention EOL conversion if it isn't decided. */
17521 }
17522 else
17523 {
17524 Lisp_Object eolvalue;
17525
17526 eolvalue = Fget (coding_system, Qeol_type);
17527
17528 if (multibyte)
17529 *buf++ = XFASTINT (AREF (val, 1));
17530
17531 if (eol_flag)
17532 {
17533 /* The EOL conversion that is normal on this system. */
17534
17535 if (NILP (eolvalue)) /* Not yet decided. */
17536 eoltype = eol_mnemonic_undecided;
17537 else if (VECTORP (eolvalue)) /* Not yet decided. */
17538 eoltype = eol_mnemonic_undecided;
17539 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17540 eoltype = (XFASTINT (eolvalue) == 0
17541 ? eol_mnemonic_unix
17542 : (XFASTINT (eolvalue) == 1
17543 ? eol_mnemonic_dos : eol_mnemonic_mac));
17544 }
17545 }
17546
17547 if (eol_flag)
17548 {
17549 /* Mention the EOL conversion if it is not the usual one. */
17550 if (STRINGP (eoltype))
17551 {
17552 eol_str = SDATA (eoltype);
17553 eol_str_len = SBYTES (eoltype);
17554 }
17555 else if (INTEGERP (eoltype)
17556 && CHAR_VALID_P (XINT (eoltype), 0))
17557 {
17558 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17559 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17560 eol_str = tmp;
17561 }
17562 else
17563 {
17564 eol_str = invalid_eol_type;
17565 eol_str_len = sizeof (invalid_eol_type) - 1;
17566 }
17567 bcopy (eol_str, buf, eol_str_len);
17568 buf += eol_str_len;
17569 }
17570
17571 return buf;
17572 }
17573
17574 /* Return a string for the output of a mode line %-spec for window W,
17575 generated by character C. PRECISION >= 0 means don't return a
17576 string longer than that value. FIELD_WIDTH > 0 means pad the
17577 string returned with spaces to that value. Return 1 in *MULTIBYTE
17578 if the result is multibyte text.
17579
17580 Note we operate on the current buffer for most purposes,
17581 the exception being w->base_line_pos. */
17582
17583 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17584
17585 static char *
17586 decode_mode_spec (w, c, field_width, precision, multibyte)
17587 struct window *w;
17588 register int c;
17589 int field_width, precision;
17590 int *multibyte;
17591 {
17592 Lisp_Object obj;
17593 struct frame *f = XFRAME (WINDOW_FRAME (w));
17594 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17595 struct buffer *b = current_buffer;
17596
17597 obj = Qnil;
17598 *multibyte = 0;
17599
17600 switch (c)
17601 {
17602 case '*':
17603 if (!NILP (b->read_only))
17604 return "%";
17605 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17606 return "*";
17607 return "-";
17608
17609 case '+':
17610 /* This differs from %* only for a modified read-only buffer. */
17611 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17612 return "*";
17613 if (!NILP (b->read_only))
17614 return "%";
17615 return "-";
17616
17617 case '&':
17618 /* This differs from %* in ignoring read-only-ness. */
17619 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17620 return "*";
17621 return "-";
17622
17623 case '%':
17624 return "%";
17625
17626 case '[':
17627 {
17628 int i;
17629 char *p;
17630
17631 if (command_loop_level > 5)
17632 return "[[[... ";
17633 p = decode_mode_spec_buf;
17634 for (i = 0; i < command_loop_level; i++)
17635 *p++ = '[';
17636 *p = 0;
17637 return decode_mode_spec_buf;
17638 }
17639
17640 case ']':
17641 {
17642 int i;
17643 char *p;
17644
17645 if (command_loop_level > 5)
17646 return " ...]]]";
17647 p = decode_mode_spec_buf;
17648 for (i = 0; i < command_loop_level; i++)
17649 *p++ = ']';
17650 *p = 0;
17651 return decode_mode_spec_buf;
17652 }
17653
17654 case '-':
17655 {
17656 register int i;
17657
17658 /* Let lots_of_dashes be a string of infinite length. */
17659 if (mode_line_target == MODE_LINE_NOPROP ||
17660 mode_line_target == MODE_LINE_STRING)
17661 return "--";
17662 if (field_width <= 0
17663 || field_width > sizeof (lots_of_dashes))
17664 {
17665 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17666 decode_mode_spec_buf[i] = '-';
17667 decode_mode_spec_buf[i] = '\0';
17668 return decode_mode_spec_buf;
17669 }
17670 else
17671 return lots_of_dashes;
17672 }
17673
17674 case 'b':
17675 obj = b->name;
17676 break;
17677
17678 case 'c':
17679 {
17680 int col = (int) current_column (); /* iftc */
17681 w->column_number_displayed = make_number (col);
17682 pint2str (decode_mode_spec_buf, field_width, col);
17683 return decode_mode_spec_buf;
17684 }
17685
17686 case 'e':
17687 #ifndef SYSTEM_MALLOC
17688 {
17689 if (NILP (Vmemory_full))
17690 return "";
17691 else
17692 return "!MEM FULL! ";
17693 }
17694 #else
17695 return "";
17696 #endif
17697
17698 case 'F':
17699 /* %F displays the frame name. */
17700 if (!NILP (f->title))
17701 return (char *) SDATA (f->title);
17702 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17703 return (char *) SDATA (f->name);
17704 return "Emacs";
17705
17706 case 'f':
17707 obj = b->filename;
17708 break;
17709
17710 case 'i':
17711 {
17712 int size = ZV - BEGV;
17713 pint2str (decode_mode_spec_buf, field_width, size);
17714 return decode_mode_spec_buf;
17715 }
17716
17717 case 'I':
17718 {
17719 int size = ZV - BEGV;
17720 pint2hrstr (decode_mode_spec_buf, field_width, size);
17721 return decode_mode_spec_buf;
17722 }
17723
17724 case 'l':
17725 {
17726 int startpos = XMARKER (w->start)->charpos;
17727 int startpos_byte = marker_byte_position (w->start);
17728 int line, linepos, linepos_byte, topline;
17729 int nlines, junk;
17730 int height = WINDOW_TOTAL_LINES (w);
17731
17732 /* If we decided that this buffer isn't suitable for line numbers,
17733 don't forget that too fast. */
17734 if (EQ (w->base_line_pos, w->buffer))
17735 goto no_value;
17736 /* But do forget it, if the window shows a different buffer now. */
17737 else if (BUFFERP (w->base_line_pos))
17738 w->base_line_pos = Qnil;
17739
17740 /* If the buffer is very big, don't waste time. */
17741 if (INTEGERP (Vline_number_display_limit)
17742 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17743 {
17744 w->base_line_pos = Qnil;
17745 w->base_line_number = Qnil;
17746 goto no_value;
17747 }
17748
17749 if (!NILP (w->base_line_number)
17750 && !NILP (w->base_line_pos)
17751 && XFASTINT (w->base_line_pos) <= startpos)
17752 {
17753 line = XFASTINT (w->base_line_number);
17754 linepos = XFASTINT (w->base_line_pos);
17755 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17756 }
17757 else
17758 {
17759 line = 1;
17760 linepos = BUF_BEGV (b);
17761 linepos_byte = BUF_BEGV_BYTE (b);
17762 }
17763
17764 /* Count lines from base line to window start position. */
17765 nlines = display_count_lines (linepos, linepos_byte,
17766 startpos_byte,
17767 startpos, &junk);
17768
17769 topline = nlines + line;
17770
17771 /* Determine a new base line, if the old one is too close
17772 or too far away, or if we did not have one.
17773 "Too close" means it's plausible a scroll-down would
17774 go back past it. */
17775 if (startpos == BUF_BEGV (b))
17776 {
17777 w->base_line_number = make_number (topline);
17778 w->base_line_pos = make_number (BUF_BEGV (b));
17779 }
17780 else if (nlines < height + 25 || nlines > height * 3 + 50
17781 || linepos == BUF_BEGV (b))
17782 {
17783 int limit = BUF_BEGV (b);
17784 int limit_byte = BUF_BEGV_BYTE (b);
17785 int position;
17786 int distance = (height * 2 + 30) * line_number_display_limit_width;
17787
17788 if (startpos - distance > limit)
17789 {
17790 limit = startpos - distance;
17791 limit_byte = CHAR_TO_BYTE (limit);
17792 }
17793
17794 nlines = display_count_lines (startpos, startpos_byte,
17795 limit_byte,
17796 - (height * 2 + 30),
17797 &position);
17798 /* If we couldn't find the lines we wanted within
17799 line_number_display_limit_width chars per line,
17800 give up on line numbers for this window. */
17801 if (position == limit_byte && limit == startpos - distance)
17802 {
17803 w->base_line_pos = w->buffer;
17804 w->base_line_number = Qnil;
17805 goto no_value;
17806 }
17807
17808 w->base_line_number = make_number (topline - nlines);
17809 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17810 }
17811
17812 /* Now count lines from the start pos to point. */
17813 nlines = display_count_lines (startpos, startpos_byte,
17814 PT_BYTE, PT, &junk);
17815
17816 /* Record that we did display the line number. */
17817 line_number_displayed = 1;
17818
17819 /* Make the string to show. */
17820 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17821 return decode_mode_spec_buf;
17822 no_value:
17823 {
17824 char* p = decode_mode_spec_buf;
17825 int pad = field_width - 2;
17826 while (pad-- > 0)
17827 *p++ = ' ';
17828 *p++ = '?';
17829 *p++ = '?';
17830 *p = '\0';
17831 return decode_mode_spec_buf;
17832 }
17833 }
17834 break;
17835
17836 case 'm':
17837 obj = b->mode_name;
17838 break;
17839
17840 case 'n':
17841 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17842 return " Narrow";
17843 break;
17844
17845 case 'p':
17846 {
17847 int pos = marker_position (w->start);
17848 int total = BUF_ZV (b) - BUF_BEGV (b);
17849
17850 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17851 {
17852 if (pos <= BUF_BEGV (b))
17853 return "All";
17854 else
17855 return "Bottom";
17856 }
17857 else if (pos <= BUF_BEGV (b))
17858 return "Top";
17859 else
17860 {
17861 if (total > 1000000)
17862 /* Do it differently for a large value, to avoid overflow. */
17863 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17864 else
17865 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17866 /* We can't normally display a 3-digit number,
17867 so get us a 2-digit number that is close. */
17868 if (total == 100)
17869 total = 99;
17870 sprintf (decode_mode_spec_buf, "%2d%%", total);
17871 return decode_mode_spec_buf;
17872 }
17873 }
17874
17875 /* Display percentage of size above the bottom of the screen. */
17876 case 'P':
17877 {
17878 int toppos = marker_position (w->start);
17879 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17880 int total = BUF_ZV (b) - BUF_BEGV (b);
17881
17882 if (botpos >= BUF_ZV (b))
17883 {
17884 if (toppos <= BUF_BEGV (b))
17885 return "All";
17886 else
17887 return "Bottom";
17888 }
17889 else
17890 {
17891 if (total > 1000000)
17892 /* Do it differently for a large value, to avoid overflow. */
17893 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17894 else
17895 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17896 /* We can't normally display a 3-digit number,
17897 so get us a 2-digit number that is close. */
17898 if (total == 100)
17899 total = 99;
17900 if (toppos <= BUF_BEGV (b))
17901 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17902 else
17903 sprintf (decode_mode_spec_buf, "%2d%%", total);
17904 return decode_mode_spec_buf;
17905 }
17906 }
17907
17908 case 's':
17909 /* status of process */
17910 obj = Fget_buffer_process (Fcurrent_buffer ());
17911 if (NILP (obj))
17912 return "no process";
17913 #ifdef subprocesses
17914 obj = Fsymbol_name (Fprocess_status (obj));
17915 #endif
17916 break;
17917
17918 case 't': /* indicate TEXT or BINARY */
17919 #ifdef MODE_LINE_BINARY_TEXT
17920 return MODE_LINE_BINARY_TEXT (b);
17921 #else
17922 return "T";
17923 #endif
17924
17925 case 'z':
17926 /* coding-system (not including end-of-line format) */
17927 case 'Z':
17928 /* coding-system (including end-of-line type) */
17929 {
17930 int eol_flag = (c == 'Z');
17931 char *p = decode_mode_spec_buf;
17932
17933 if (! FRAME_WINDOW_P (f))
17934 {
17935 /* No need to mention EOL here--the terminal never needs
17936 to do EOL conversion. */
17937 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17938 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17939 }
17940 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17941 p, eol_flag);
17942
17943 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17944 #ifdef subprocesses
17945 obj = Fget_buffer_process (Fcurrent_buffer ());
17946 if (PROCESSP (obj))
17947 {
17948 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17949 p, eol_flag);
17950 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17951 p, eol_flag);
17952 }
17953 #endif /* subprocesses */
17954 #endif /* 0 */
17955 *p = 0;
17956 return decode_mode_spec_buf;
17957 }
17958 }
17959
17960 if (STRINGP (obj))
17961 {
17962 *multibyte = STRING_MULTIBYTE (obj);
17963 return (char *) SDATA (obj);
17964 }
17965 else
17966 return "";
17967 }
17968
17969
17970 /* Count up to COUNT lines starting from START / START_BYTE.
17971 But don't go beyond LIMIT_BYTE.
17972 Return the number of lines thus found (always nonnegative).
17973
17974 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17975
17976 static int
17977 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17978 int start, start_byte, limit_byte, count;
17979 int *byte_pos_ptr;
17980 {
17981 register unsigned char *cursor;
17982 unsigned char *base;
17983
17984 register int ceiling;
17985 register unsigned char *ceiling_addr;
17986 int orig_count = count;
17987
17988 /* If we are not in selective display mode,
17989 check only for newlines. */
17990 int selective_display = (!NILP (current_buffer->selective_display)
17991 && !INTEGERP (current_buffer->selective_display));
17992
17993 if (count > 0)
17994 {
17995 while (start_byte < limit_byte)
17996 {
17997 ceiling = BUFFER_CEILING_OF (start_byte);
17998 ceiling = min (limit_byte - 1, ceiling);
17999 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
18000 base = (cursor = BYTE_POS_ADDR (start_byte));
18001 while (1)
18002 {
18003 if (selective_display)
18004 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
18005 ;
18006 else
18007 while (*cursor != '\n' && ++cursor != ceiling_addr)
18008 ;
18009
18010 if (cursor != ceiling_addr)
18011 {
18012 if (--count == 0)
18013 {
18014 start_byte += cursor - base + 1;
18015 *byte_pos_ptr = start_byte;
18016 return orig_count;
18017 }
18018 else
18019 if (++cursor == ceiling_addr)
18020 break;
18021 }
18022 else
18023 break;
18024 }
18025 start_byte += cursor - base;
18026 }
18027 }
18028 else
18029 {
18030 while (start_byte > limit_byte)
18031 {
18032 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18033 ceiling = max (limit_byte, ceiling);
18034 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18035 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18036 while (1)
18037 {
18038 if (selective_display)
18039 while (--cursor != ceiling_addr
18040 && *cursor != '\n' && *cursor != 015)
18041 ;
18042 else
18043 while (--cursor != ceiling_addr && *cursor != '\n')
18044 ;
18045
18046 if (cursor != ceiling_addr)
18047 {
18048 if (++count == 0)
18049 {
18050 start_byte += cursor - base + 1;
18051 *byte_pos_ptr = start_byte;
18052 /* When scanning backwards, we should
18053 not count the newline posterior to which we stop. */
18054 return - orig_count - 1;
18055 }
18056 }
18057 else
18058 break;
18059 }
18060 /* Here we add 1 to compensate for the last decrement
18061 of CURSOR, which took it past the valid range. */
18062 start_byte += cursor - base + 1;
18063 }
18064 }
18065
18066 *byte_pos_ptr = limit_byte;
18067
18068 if (count < 0)
18069 return - orig_count + count;
18070 return orig_count - count;
18071
18072 }
18073
18074
18075 \f
18076 /***********************************************************************
18077 Displaying strings
18078 ***********************************************************************/
18079
18080 /* Display a NUL-terminated string, starting with index START.
18081
18082 If STRING is non-null, display that C string. Otherwise, the Lisp
18083 string LISP_STRING is displayed.
18084
18085 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18086 FACE_STRING. Display STRING or LISP_STRING with the face at
18087 FACE_STRING_POS in FACE_STRING:
18088
18089 Display the string in the environment given by IT, but use the
18090 standard display table, temporarily.
18091
18092 FIELD_WIDTH is the minimum number of output glyphs to produce.
18093 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18094 with spaces. If STRING has more characters, more than FIELD_WIDTH
18095 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18096
18097 PRECISION is the maximum number of characters to output from
18098 STRING. PRECISION < 0 means don't truncate the string.
18099
18100 This is roughly equivalent to printf format specifiers:
18101
18102 FIELD_WIDTH PRECISION PRINTF
18103 ----------------------------------------
18104 -1 -1 %s
18105 -1 10 %.10s
18106 10 -1 %10s
18107 20 10 %20.10s
18108
18109 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18110 display them, and < 0 means obey the current buffer's value of
18111 enable_multibyte_characters.
18112
18113 Value is the number of columns displayed. */
18114
18115 static int
18116 display_string (string, lisp_string, face_string, face_string_pos,
18117 start, it, field_width, precision, max_x, multibyte)
18118 unsigned char *string;
18119 Lisp_Object lisp_string;
18120 Lisp_Object face_string;
18121 int face_string_pos;
18122 int start;
18123 struct it *it;
18124 int field_width, precision, max_x;
18125 int multibyte;
18126 {
18127 int hpos_at_start = it->hpos;
18128 int saved_face_id = it->face_id;
18129 struct glyph_row *row = it->glyph_row;
18130
18131 /* Initialize the iterator IT for iteration over STRING beginning
18132 with index START. */
18133 reseat_to_string (it, string, lisp_string, start,
18134 precision, field_width, multibyte);
18135
18136 /* If displaying STRING, set up the face of the iterator
18137 from LISP_STRING, if that's given. */
18138 if (STRINGP (face_string))
18139 {
18140 int endptr;
18141 struct face *face;
18142
18143 it->face_id
18144 = face_at_string_position (it->w, face_string, face_string_pos,
18145 0, it->region_beg_charpos,
18146 it->region_end_charpos,
18147 &endptr, it->base_face_id, 0);
18148 face = FACE_FROM_ID (it->f, it->face_id);
18149 it->face_box_p = face->box != FACE_NO_BOX;
18150 }
18151
18152 /* Set max_x to the maximum allowed X position. Don't let it go
18153 beyond the right edge of the window. */
18154 if (max_x <= 0)
18155 max_x = it->last_visible_x;
18156 else
18157 max_x = min (max_x, it->last_visible_x);
18158
18159 /* Skip over display elements that are not visible. because IT->w is
18160 hscrolled. */
18161 if (it->current_x < it->first_visible_x)
18162 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18163 MOVE_TO_POS | MOVE_TO_X);
18164
18165 row->ascent = it->max_ascent;
18166 row->height = it->max_ascent + it->max_descent;
18167 row->phys_ascent = it->max_phys_ascent;
18168 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18169 row->extra_line_spacing = it->max_extra_line_spacing;
18170
18171 /* This condition is for the case that we are called with current_x
18172 past last_visible_x. */
18173 while (it->current_x < max_x)
18174 {
18175 int x_before, x, n_glyphs_before, i, nglyphs;
18176
18177 /* Get the next display element. */
18178 if (!get_next_display_element (it))
18179 break;
18180
18181 /* Produce glyphs. */
18182 x_before = it->current_x;
18183 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18184 PRODUCE_GLYPHS (it);
18185
18186 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18187 i = 0;
18188 x = x_before;
18189 while (i < nglyphs)
18190 {
18191 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18192
18193 if (!it->truncate_lines_p
18194 && x + glyph->pixel_width > max_x)
18195 {
18196 /* End of continued line or max_x reached. */
18197 if (CHAR_GLYPH_PADDING_P (*glyph))
18198 {
18199 /* A wide character is unbreakable. */
18200 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18201 it->current_x = x_before;
18202 }
18203 else
18204 {
18205 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18206 it->current_x = x;
18207 }
18208 break;
18209 }
18210 else if (x + glyph->pixel_width > it->first_visible_x)
18211 {
18212 /* Glyph is at least partially visible. */
18213 ++it->hpos;
18214 if (x < it->first_visible_x)
18215 it->glyph_row->x = x - it->first_visible_x;
18216 }
18217 else
18218 {
18219 /* Glyph is off the left margin of the display area.
18220 Should not happen. */
18221 abort ();
18222 }
18223
18224 row->ascent = max (row->ascent, it->max_ascent);
18225 row->height = max (row->height, it->max_ascent + it->max_descent);
18226 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18227 row->phys_height = max (row->phys_height,
18228 it->max_phys_ascent + it->max_phys_descent);
18229 row->extra_line_spacing = max (row->extra_line_spacing,
18230 it->max_extra_line_spacing);
18231 x += glyph->pixel_width;
18232 ++i;
18233 }
18234
18235 /* Stop if max_x reached. */
18236 if (i < nglyphs)
18237 break;
18238
18239 /* Stop at line ends. */
18240 if (ITERATOR_AT_END_OF_LINE_P (it))
18241 {
18242 it->continuation_lines_width = 0;
18243 break;
18244 }
18245
18246 set_iterator_to_next (it, 1);
18247
18248 /* Stop if truncating at the right edge. */
18249 if (it->truncate_lines_p
18250 && it->current_x >= it->last_visible_x)
18251 {
18252 /* Add truncation mark, but don't do it if the line is
18253 truncated at a padding space. */
18254 if (IT_CHARPOS (*it) < it->string_nchars)
18255 {
18256 if (!FRAME_WINDOW_P (it->f))
18257 {
18258 int i, n;
18259
18260 if (it->current_x > it->last_visible_x)
18261 {
18262 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18263 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18264 break;
18265 for (n = row->used[TEXT_AREA]; i < n; ++i)
18266 {
18267 row->used[TEXT_AREA] = i;
18268 produce_special_glyphs (it, IT_TRUNCATION);
18269 }
18270 }
18271 produce_special_glyphs (it, IT_TRUNCATION);
18272 }
18273 it->glyph_row->truncated_on_right_p = 1;
18274 }
18275 break;
18276 }
18277 }
18278
18279 /* Maybe insert a truncation at the left. */
18280 if (it->first_visible_x
18281 && IT_CHARPOS (*it) > 0)
18282 {
18283 if (!FRAME_WINDOW_P (it->f))
18284 insert_left_trunc_glyphs (it);
18285 it->glyph_row->truncated_on_left_p = 1;
18286 }
18287
18288 it->face_id = saved_face_id;
18289
18290 /* Value is number of columns displayed. */
18291 return it->hpos - hpos_at_start;
18292 }
18293
18294
18295 \f
18296 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18297 appears as an element of LIST or as the car of an element of LIST.
18298 If PROPVAL is a list, compare each element against LIST in that
18299 way, and return 1/2 if any element of PROPVAL is found in LIST.
18300 Otherwise return 0. This function cannot quit.
18301 The return value is 2 if the text is invisible but with an ellipsis
18302 and 1 if it's invisible and without an ellipsis. */
18303
18304 int
18305 invisible_p (propval, list)
18306 register Lisp_Object propval;
18307 Lisp_Object list;
18308 {
18309 register Lisp_Object tail, proptail;
18310
18311 for (tail = list; CONSP (tail); tail = XCDR (tail))
18312 {
18313 register Lisp_Object tem;
18314 tem = XCAR (tail);
18315 if (EQ (propval, tem))
18316 return 1;
18317 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18318 return NILP (XCDR (tem)) ? 1 : 2;
18319 }
18320
18321 if (CONSP (propval))
18322 {
18323 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18324 {
18325 Lisp_Object propelt;
18326 propelt = XCAR (proptail);
18327 for (tail = list; CONSP (tail); tail = XCDR (tail))
18328 {
18329 register Lisp_Object tem;
18330 tem = XCAR (tail);
18331 if (EQ (propelt, tem))
18332 return 1;
18333 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18334 return NILP (XCDR (tem)) ? 1 : 2;
18335 }
18336 }
18337 }
18338
18339 return 0;
18340 }
18341
18342 /* Calculate a width or height in pixels from a specification using
18343 the following elements:
18344
18345 SPEC ::=
18346 NUM - a (fractional) multiple of the default font width/height
18347 (NUM) - specifies exactly NUM pixels
18348 UNIT - a fixed number of pixels, see below.
18349 ELEMENT - size of a display element in pixels, see below.
18350 (NUM . SPEC) - equals NUM * SPEC
18351 (+ SPEC SPEC ...) - add pixel values
18352 (- SPEC SPEC ...) - subtract pixel values
18353 (- SPEC) - negate pixel value
18354
18355 NUM ::=
18356 INT or FLOAT - a number constant
18357 SYMBOL - use symbol's (buffer local) variable binding.
18358
18359 UNIT ::=
18360 in - pixels per inch *)
18361 mm - pixels per 1/1000 meter *)
18362 cm - pixels per 1/100 meter *)
18363 width - width of current font in pixels.
18364 height - height of current font in pixels.
18365
18366 *) using the ratio(s) defined in display-pixels-per-inch.
18367
18368 ELEMENT ::=
18369
18370 left-fringe - left fringe width in pixels
18371 right-fringe - right fringe width in pixels
18372
18373 left-margin - left margin width in pixels
18374 right-margin - right margin width in pixels
18375
18376 scroll-bar - scroll-bar area width in pixels
18377
18378 Examples:
18379
18380 Pixels corresponding to 5 inches:
18381 (5 . in)
18382
18383 Total width of non-text areas on left side of window (if scroll-bar is on left):
18384 '(space :width (+ left-fringe left-margin scroll-bar))
18385
18386 Align to first text column (in header line):
18387 '(space :align-to 0)
18388
18389 Align to middle of text area minus half the width of variable `my-image'
18390 containing a loaded image:
18391 '(space :align-to (0.5 . (- text my-image)))
18392
18393 Width of left margin minus width of 1 character in the default font:
18394 '(space :width (- left-margin 1))
18395
18396 Width of left margin minus width of 2 characters in the current font:
18397 '(space :width (- left-margin (2 . width)))
18398
18399 Center 1 character over left-margin (in header line):
18400 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18401
18402 Different ways to express width of left fringe plus left margin minus one pixel:
18403 '(space :width (- (+ left-fringe left-margin) (1)))
18404 '(space :width (+ left-fringe left-margin (- (1))))
18405 '(space :width (+ left-fringe left-margin (-1)))
18406
18407 */
18408
18409 #define NUMVAL(X) \
18410 ((INTEGERP (X) || FLOATP (X)) \
18411 ? XFLOATINT (X) \
18412 : - 1)
18413
18414 int
18415 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18416 double *res;
18417 struct it *it;
18418 Lisp_Object prop;
18419 void *font;
18420 int width_p, *align_to;
18421 {
18422 double pixels;
18423
18424 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18425 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18426
18427 if (NILP (prop))
18428 return OK_PIXELS (0);
18429
18430 if (SYMBOLP (prop))
18431 {
18432 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18433 {
18434 char *unit = SDATA (SYMBOL_NAME (prop));
18435
18436 if (unit[0] == 'i' && unit[1] == 'n')
18437 pixels = 1.0;
18438 else if (unit[0] == 'm' && unit[1] == 'm')
18439 pixels = 25.4;
18440 else if (unit[0] == 'c' && unit[1] == 'm')
18441 pixels = 2.54;
18442 else
18443 pixels = 0;
18444 if (pixels > 0)
18445 {
18446 double ppi;
18447 #ifdef HAVE_WINDOW_SYSTEM
18448 if (FRAME_WINDOW_P (it->f)
18449 && (ppi = (width_p
18450 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18451 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18452 ppi > 0))
18453 return OK_PIXELS (ppi / pixels);
18454 #endif
18455
18456 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18457 || (CONSP (Vdisplay_pixels_per_inch)
18458 && (ppi = (width_p
18459 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18460 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18461 ppi > 0)))
18462 return OK_PIXELS (ppi / pixels);
18463
18464 return 0;
18465 }
18466 }
18467
18468 #ifdef HAVE_WINDOW_SYSTEM
18469 if (EQ (prop, Qheight))
18470 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18471 if (EQ (prop, Qwidth))
18472 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18473 #else
18474 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18475 return OK_PIXELS (1);
18476 #endif
18477
18478 if (EQ (prop, Qtext))
18479 return OK_PIXELS (width_p
18480 ? window_box_width (it->w, TEXT_AREA)
18481 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18482
18483 if (align_to && *align_to < 0)
18484 {
18485 *res = 0;
18486 if (EQ (prop, Qleft))
18487 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18488 if (EQ (prop, Qright))
18489 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18490 if (EQ (prop, Qcenter))
18491 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18492 + window_box_width (it->w, TEXT_AREA) / 2);
18493 if (EQ (prop, Qleft_fringe))
18494 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18495 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18496 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18497 if (EQ (prop, Qright_fringe))
18498 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18499 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18500 : window_box_right_offset (it->w, TEXT_AREA));
18501 if (EQ (prop, Qleft_margin))
18502 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18503 if (EQ (prop, Qright_margin))
18504 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18505 if (EQ (prop, Qscroll_bar))
18506 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18507 ? 0
18508 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18509 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18510 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18511 : 0)));
18512 }
18513 else
18514 {
18515 if (EQ (prop, Qleft_fringe))
18516 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18517 if (EQ (prop, Qright_fringe))
18518 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18519 if (EQ (prop, Qleft_margin))
18520 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18521 if (EQ (prop, Qright_margin))
18522 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18523 if (EQ (prop, Qscroll_bar))
18524 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18525 }
18526
18527 prop = Fbuffer_local_value (prop, it->w->buffer);
18528 }
18529
18530 if (INTEGERP (prop) || FLOATP (prop))
18531 {
18532 int base_unit = (width_p
18533 ? FRAME_COLUMN_WIDTH (it->f)
18534 : FRAME_LINE_HEIGHT (it->f));
18535 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18536 }
18537
18538 if (CONSP (prop))
18539 {
18540 Lisp_Object car = XCAR (prop);
18541 Lisp_Object cdr = XCDR (prop);
18542
18543 if (SYMBOLP (car))
18544 {
18545 #ifdef HAVE_WINDOW_SYSTEM
18546 if (valid_image_p (prop))
18547 {
18548 int id = lookup_image (it->f, prop);
18549 struct image *img = IMAGE_FROM_ID (it->f, id);
18550
18551 return OK_PIXELS (width_p ? img->width : img->height);
18552 }
18553 #endif
18554 if (EQ (car, Qplus) || EQ (car, Qminus))
18555 {
18556 int first = 1;
18557 double px;
18558
18559 pixels = 0;
18560 while (CONSP (cdr))
18561 {
18562 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18563 font, width_p, align_to))
18564 return 0;
18565 if (first)
18566 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18567 else
18568 pixels += px;
18569 cdr = XCDR (cdr);
18570 }
18571 if (EQ (car, Qminus))
18572 pixels = -pixels;
18573 return OK_PIXELS (pixels);
18574 }
18575
18576 car = Fbuffer_local_value (car, it->w->buffer);
18577 }
18578
18579 if (INTEGERP (car) || FLOATP (car))
18580 {
18581 double fact;
18582 pixels = XFLOATINT (car);
18583 if (NILP (cdr))
18584 return OK_PIXELS (pixels);
18585 if (calc_pixel_width_or_height (&fact, it, cdr,
18586 font, width_p, align_to))
18587 return OK_PIXELS (pixels * fact);
18588 return 0;
18589 }
18590
18591 return 0;
18592 }
18593
18594 return 0;
18595 }
18596
18597 \f
18598 /***********************************************************************
18599 Glyph Display
18600 ***********************************************************************/
18601
18602 #ifdef HAVE_WINDOW_SYSTEM
18603
18604 #if GLYPH_DEBUG
18605
18606 void
18607 dump_glyph_string (s)
18608 struct glyph_string *s;
18609 {
18610 fprintf (stderr, "glyph string\n");
18611 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18612 s->x, s->y, s->width, s->height);
18613 fprintf (stderr, " ybase = %d\n", s->ybase);
18614 fprintf (stderr, " hl = %d\n", s->hl);
18615 fprintf (stderr, " left overhang = %d, right = %d\n",
18616 s->left_overhang, s->right_overhang);
18617 fprintf (stderr, " nchars = %d\n", s->nchars);
18618 fprintf (stderr, " extends to end of line = %d\n",
18619 s->extends_to_end_of_line_p);
18620 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18621 fprintf (stderr, " bg width = %d\n", s->background_width);
18622 }
18623
18624 #endif /* GLYPH_DEBUG */
18625
18626 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18627 of XChar2b structures for S; it can't be allocated in
18628 init_glyph_string because it must be allocated via `alloca'. W
18629 is the window on which S is drawn. ROW and AREA are the glyph row
18630 and area within the row from which S is constructed. START is the
18631 index of the first glyph structure covered by S. HL is a
18632 face-override for drawing S. */
18633
18634 #ifdef HAVE_NTGUI
18635 #define OPTIONAL_HDC(hdc) hdc,
18636 #define DECLARE_HDC(hdc) HDC hdc;
18637 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18638 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18639 #endif
18640
18641 #ifndef OPTIONAL_HDC
18642 #define OPTIONAL_HDC(hdc)
18643 #define DECLARE_HDC(hdc)
18644 #define ALLOCATE_HDC(hdc, f)
18645 #define RELEASE_HDC(hdc, f)
18646 #endif
18647
18648 static void
18649 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18650 struct glyph_string *s;
18651 DECLARE_HDC (hdc)
18652 XChar2b *char2b;
18653 struct window *w;
18654 struct glyph_row *row;
18655 enum glyph_row_area area;
18656 int start;
18657 enum draw_glyphs_face hl;
18658 {
18659 bzero (s, sizeof *s);
18660 s->w = w;
18661 s->f = XFRAME (w->frame);
18662 #ifdef HAVE_NTGUI
18663 s->hdc = hdc;
18664 #endif
18665 s->display = FRAME_X_DISPLAY (s->f);
18666 s->window = FRAME_X_WINDOW (s->f);
18667 s->char2b = char2b;
18668 s->hl = hl;
18669 s->row = row;
18670 s->area = area;
18671 s->first_glyph = row->glyphs[area] + start;
18672 s->height = row->height;
18673 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18674
18675 /* Display the internal border below the tool-bar window. */
18676 if (WINDOWP (s->f->tool_bar_window)
18677 && s->w == XWINDOW (s->f->tool_bar_window))
18678 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18679
18680 s->ybase = s->y + row->ascent;
18681 }
18682
18683
18684 /* Append the list of glyph strings with head H and tail T to the list
18685 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18686
18687 static INLINE void
18688 append_glyph_string_lists (head, tail, h, t)
18689 struct glyph_string **head, **tail;
18690 struct glyph_string *h, *t;
18691 {
18692 if (h)
18693 {
18694 if (*head)
18695 (*tail)->next = h;
18696 else
18697 *head = h;
18698 h->prev = *tail;
18699 *tail = t;
18700 }
18701 }
18702
18703
18704 /* Prepend the list of glyph strings with head H and tail T to the
18705 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18706 result. */
18707
18708 static INLINE void
18709 prepend_glyph_string_lists (head, tail, h, t)
18710 struct glyph_string **head, **tail;
18711 struct glyph_string *h, *t;
18712 {
18713 if (h)
18714 {
18715 if (*head)
18716 (*head)->prev = t;
18717 else
18718 *tail = t;
18719 t->next = *head;
18720 *head = h;
18721 }
18722 }
18723
18724
18725 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18726 Set *HEAD and *TAIL to the resulting list. */
18727
18728 static INLINE void
18729 append_glyph_string (head, tail, s)
18730 struct glyph_string **head, **tail;
18731 struct glyph_string *s;
18732 {
18733 s->next = s->prev = NULL;
18734 append_glyph_string_lists (head, tail, s, s);
18735 }
18736
18737
18738 /* Get face and two-byte form of character glyph GLYPH on frame F.
18739 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18740 a pointer to a realized face that is ready for display. */
18741
18742 static INLINE struct face *
18743 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18744 struct frame *f;
18745 struct glyph *glyph;
18746 XChar2b *char2b;
18747 int *two_byte_p;
18748 {
18749 struct face *face;
18750
18751 xassert (glyph->type == CHAR_GLYPH);
18752 face = FACE_FROM_ID (f, glyph->face_id);
18753
18754 if (two_byte_p)
18755 *two_byte_p = 0;
18756
18757 if (!glyph->multibyte_p)
18758 {
18759 /* Unibyte case. We don't have to encode, but we have to make
18760 sure to use a face suitable for unibyte. */
18761 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18762 }
18763 else if (glyph->u.ch < 128)
18764 {
18765 /* Case of ASCII in a face known to fit ASCII. */
18766 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18767 }
18768 else
18769 {
18770 int c1, c2, charset;
18771
18772 /* Split characters into bytes. If c2 is -1 afterwards, C is
18773 really a one-byte character so that byte1 is zero. */
18774 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18775 if (c2 > 0)
18776 STORE_XCHAR2B (char2b, c1, c2);
18777 else
18778 STORE_XCHAR2B (char2b, 0, c1);
18779
18780 /* Maybe encode the character in *CHAR2B. */
18781 if (charset != CHARSET_ASCII)
18782 {
18783 struct font_info *font_info
18784 = FONT_INFO_FROM_ID (f, face->font_info_id);
18785 if (font_info)
18786 glyph->font_type
18787 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18788 }
18789 }
18790
18791 /* Make sure X resources of the face are allocated. */
18792 xassert (face != NULL);
18793 PREPARE_FACE_FOR_DISPLAY (f, face);
18794 return face;
18795 }
18796
18797
18798 /* Fill glyph string S with composition components specified by S->cmp.
18799
18800 FACES is an array of faces for all components of this composition.
18801 S->gidx is the index of the first component for S.
18802
18803 OVERLAPS non-zero means S should draw the foreground only, and use
18804 its physical height for clipping. See also draw_glyphs.
18805
18806 Value is the index of a component not in S. */
18807
18808 static int
18809 fill_composite_glyph_string (s, faces, overlaps)
18810 struct glyph_string *s;
18811 struct face **faces;
18812 int overlaps;
18813 {
18814 int i;
18815
18816 xassert (s);
18817
18818 s->for_overlaps = overlaps;
18819
18820 s->face = faces[s->gidx];
18821 s->font = s->face->font;
18822 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18823
18824 /* For all glyphs of this composition, starting at the offset
18825 S->gidx, until we reach the end of the definition or encounter a
18826 glyph that requires the different face, add it to S. */
18827 ++s->nchars;
18828 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18829 ++s->nchars;
18830
18831 /* All glyph strings for the same composition has the same width,
18832 i.e. the width set for the first component of the composition. */
18833
18834 s->width = s->first_glyph->pixel_width;
18835
18836 /* If the specified font could not be loaded, use the frame's
18837 default font, but record the fact that we couldn't load it in
18838 the glyph string so that we can draw rectangles for the
18839 characters of the glyph string. */
18840 if (s->font == NULL)
18841 {
18842 s->font_not_found_p = 1;
18843 s->font = FRAME_FONT (s->f);
18844 }
18845
18846 /* Adjust base line for subscript/superscript text. */
18847 s->ybase += s->first_glyph->voffset;
18848
18849 xassert (s->face && s->face->gc);
18850
18851 /* This glyph string must always be drawn with 16-bit functions. */
18852 s->two_byte_p = 1;
18853
18854 return s->gidx + s->nchars;
18855 }
18856
18857
18858 /* Fill glyph string S from a sequence of character glyphs.
18859
18860 FACE_ID is the face id of the string. START is the index of the
18861 first glyph to consider, END is the index of the last + 1.
18862 OVERLAPS non-zero means S should draw the foreground only, and use
18863 its physical height for clipping. See also draw_glyphs.
18864
18865 Value is the index of the first glyph not in S. */
18866
18867 static int
18868 fill_glyph_string (s, face_id, start, end, overlaps)
18869 struct glyph_string *s;
18870 int face_id;
18871 int start, end, overlaps;
18872 {
18873 struct glyph *glyph, *last;
18874 int voffset;
18875 int glyph_not_available_p;
18876
18877 xassert (s->f == XFRAME (s->w->frame));
18878 xassert (s->nchars == 0);
18879 xassert (start >= 0 && end > start);
18880
18881 s->for_overlaps = overlaps,
18882 glyph = s->row->glyphs[s->area] + start;
18883 last = s->row->glyphs[s->area] + end;
18884 voffset = glyph->voffset;
18885
18886 glyph_not_available_p = glyph->glyph_not_available_p;
18887
18888 while (glyph < last
18889 && glyph->type == CHAR_GLYPH
18890 && glyph->voffset == voffset
18891 /* Same face id implies same font, nowadays. */
18892 && glyph->face_id == face_id
18893 && glyph->glyph_not_available_p == glyph_not_available_p)
18894 {
18895 int two_byte_p;
18896
18897 s->face = get_glyph_face_and_encoding (s->f, glyph,
18898 s->char2b + s->nchars,
18899 &two_byte_p);
18900 s->two_byte_p = two_byte_p;
18901 ++s->nchars;
18902 xassert (s->nchars <= end - start);
18903 s->width += glyph->pixel_width;
18904 ++glyph;
18905 }
18906
18907 s->font = s->face->font;
18908 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18909
18910 /* If the specified font could not be loaded, use the frame's font,
18911 but record the fact that we couldn't load it in
18912 S->font_not_found_p so that we can draw rectangles for the
18913 characters of the glyph string. */
18914 if (s->font == NULL || glyph_not_available_p)
18915 {
18916 s->font_not_found_p = 1;
18917 s->font = FRAME_FONT (s->f);
18918 }
18919
18920 /* Adjust base line for subscript/superscript text. */
18921 s->ybase += voffset;
18922
18923 xassert (s->face && s->face->gc);
18924 return glyph - s->row->glyphs[s->area];
18925 }
18926
18927
18928 /* Fill glyph string S from image glyph S->first_glyph. */
18929
18930 static void
18931 fill_image_glyph_string (s)
18932 struct glyph_string *s;
18933 {
18934 xassert (s->first_glyph->type == IMAGE_GLYPH);
18935 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18936 xassert (s->img);
18937 s->slice = s->first_glyph->slice;
18938 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18939 s->font = s->face->font;
18940 s->width = s->first_glyph->pixel_width;
18941
18942 /* Adjust base line for subscript/superscript text. */
18943 s->ybase += s->first_glyph->voffset;
18944 }
18945
18946
18947 /* Fill glyph string S from a sequence of stretch glyphs.
18948
18949 ROW is the glyph row in which the glyphs are found, AREA is the
18950 area within the row. START is the index of the first glyph to
18951 consider, END is the index of the last + 1.
18952
18953 Value is the index of the first glyph not in S. */
18954
18955 static int
18956 fill_stretch_glyph_string (s, row, area, start, end)
18957 struct glyph_string *s;
18958 struct glyph_row *row;
18959 enum glyph_row_area area;
18960 int start, end;
18961 {
18962 struct glyph *glyph, *last;
18963 int voffset, face_id;
18964
18965 xassert (s->first_glyph->type == STRETCH_GLYPH);
18966
18967 glyph = s->row->glyphs[s->area] + start;
18968 last = s->row->glyphs[s->area] + end;
18969 face_id = glyph->face_id;
18970 s->face = FACE_FROM_ID (s->f, face_id);
18971 s->font = s->face->font;
18972 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18973 s->width = glyph->pixel_width;
18974 s->nchars = 1;
18975 voffset = glyph->voffset;
18976
18977 for (++glyph;
18978 (glyph < last
18979 && glyph->type == STRETCH_GLYPH
18980 && glyph->voffset == voffset
18981 && glyph->face_id == face_id);
18982 ++glyph)
18983 s->width += glyph->pixel_width;
18984
18985 /* Adjust base line for subscript/superscript text. */
18986 s->ybase += voffset;
18987
18988 /* The case that face->gc == 0 is handled when drawing the glyph
18989 string by calling PREPARE_FACE_FOR_DISPLAY. */
18990 xassert (s->face);
18991 return glyph - s->row->glyphs[s->area];
18992 }
18993
18994
18995 /* EXPORT for RIF:
18996 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18997 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18998 assumed to be zero. */
18999
19000 void
19001 x_get_glyph_overhangs (glyph, f, left, right)
19002 struct glyph *glyph;
19003 struct frame *f;
19004 int *left, *right;
19005 {
19006 *left = *right = 0;
19007
19008 if (glyph->type == CHAR_GLYPH)
19009 {
19010 XFontStruct *font;
19011 struct face *face;
19012 struct font_info *font_info;
19013 XChar2b char2b;
19014 XCharStruct *pcm;
19015
19016 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
19017 font = face->font;
19018 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
19019 if (font /* ++KFS: Should this be font_info ? */
19020 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
19021 {
19022 if (pcm->rbearing > pcm->width)
19023 *right = pcm->rbearing - pcm->width;
19024 if (pcm->lbearing < 0)
19025 *left = -pcm->lbearing;
19026 }
19027 }
19028 }
19029
19030
19031 /* Return the index of the first glyph preceding glyph string S that
19032 is overwritten by S because of S's left overhang. Value is -1
19033 if no glyphs are overwritten. */
19034
19035 static int
19036 left_overwritten (s)
19037 struct glyph_string *s;
19038 {
19039 int k;
19040
19041 if (s->left_overhang)
19042 {
19043 int x = 0, i;
19044 struct glyph *glyphs = s->row->glyphs[s->area];
19045 int first = s->first_glyph - glyphs;
19046
19047 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19048 x -= glyphs[i].pixel_width;
19049
19050 k = i + 1;
19051 }
19052 else
19053 k = -1;
19054
19055 return k;
19056 }
19057
19058
19059 /* Return the index of the first glyph preceding glyph string S that
19060 is overwriting S because of its right overhang. Value is -1 if no
19061 glyph in front of S overwrites S. */
19062
19063 static int
19064 left_overwriting (s)
19065 struct glyph_string *s;
19066 {
19067 int i, k, x;
19068 struct glyph *glyphs = s->row->glyphs[s->area];
19069 int first = s->first_glyph - glyphs;
19070
19071 k = -1;
19072 x = 0;
19073 for (i = first - 1; i >= 0; --i)
19074 {
19075 int left, right;
19076 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19077 if (x + right > 0)
19078 k = i;
19079 x -= glyphs[i].pixel_width;
19080 }
19081
19082 return k;
19083 }
19084
19085
19086 /* Return the index of the last glyph following glyph string S that is
19087 not overwritten by S because of S's right overhang. Value is -1 if
19088 no such glyph is found. */
19089
19090 static int
19091 right_overwritten (s)
19092 struct glyph_string *s;
19093 {
19094 int k = -1;
19095
19096 if (s->right_overhang)
19097 {
19098 int x = 0, i;
19099 struct glyph *glyphs = s->row->glyphs[s->area];
19100 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19101 int end = s->row->used[s->area];
19102
19103 for (i = first; i < end && s->right_overhang > x; ++i)
19104 x += glyphs[i].pixel_width;
19105
19106 k = i;
19107 }
19108
19109 return k;
19110 }
19111
19112
19113 /* Return the index of the last glyph following glyph string S that
19114 overwrites S because of its left overhang. Value is negative
19115 if no such glyph is found. */
19116
19117 static int
19118 right_overwriting (s)
19119 struct glyph_string *s;
19120 {
19121 int i, k, x;
19122 int end = s->row->used[s->area];
19123 struct glyph *glyphs = s->row->glyphs[s->area];
19124 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19125
19126 k = -1;
19127 x = 0;
19128 for (i = first; i < end; ++i)
19129 {
19130 int left, right;
19131 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19132 if (x - left < 0)
19133 k = i;
19134 x += glyphs[i].pixel_width;
19135 }
19136
19137 return k;
19138 }
19139
19140
19141 /* Get face and two-byte form of character C in face FACE_ID on frame
19142 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19143 means we want to display multibyte text. DISPLAY_P non-zero means
19144 make sure that X resources for the face returned are allocated.
19145 Value is a pointer to a realized face that is ready for display if
19146 DISPLAY_P is non-zero. */
19147
19148 static INLINE struct face *
19149 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19150 struct frame *f;
19151 int c, face_id;
19152 XChar2b *char2b;
19153 int multibyte_p, display_p;
19154 {
19155 struct face *face = FACE_FROM_ID (f, face_id);
19156
19157 if (!multibyte_p)
19158 {
19159 /* Unibyte case. We don't have to encode, but we have to make
19160 sure to use a face suitable for unibyte. */
19161 STORE_XCHAR2B (char2b, 0, c);
19162 face_id = FACE_FOR_CHAR (f, face, c);
19163 face = FACE_FROM_ID (f, face_id);
19164 }
19165 else if (c < 128)
19166 {
19167 /* Case of ASCII in a face known to fit ASCII. */
19168 STORE_XCHAR2B (char2b, 0, c);
19169 }
19170 else
19171 {
19172 int c1, c2, charset;
19173
19174 /* Split characters into bytes. If c2 is -1 afterwards, C is
19175 really a one-byte character so that byte1 is zero. */
19176 SPLIT_CHAR (c, charset, c1, c2);
19177 if (c2 > 0)
19178 STORE_XCHAR2B (char2b, c1, c2);
19179 else
19180 STORE_XCHAR2B (char2b, 0, c1);
19181
19182 /* Maybe encode the character in *CHAR2B. */
19183 if (face->font != NULL)
19184 {
19185 struct font_info *font_info
19186 = FONT_INFO_FROM_ID (f, face->font_info_id);
19187 if (font_info)
19188 rif->encode_char (c, char2b, font_info, 0);
19189 }
19190 }
19191
19192 /* Make sure X resources of the face are allocated. */
19193 #ifdef HAVE_X_WINDOWS
19194 if (display_p)
19195 #endif
19196 {
19197 xassert (face != NULL);
19198 PREPARE_FACE_FOR_DISPLAY (f, face);
19199 }
19200
19201 return face;
19202 }
19203
19204
19205 /* Set background width of glyph string S. START is the index of the
19206 first glyph following S. LAST_X is the right-most x-position + 1
19207 in the drawing area. */
19208
19209 static INLINE void
19210 set_glyph_string_background_width (s, start, last_x)
19211 struct glyph_string *s;
19212 int start;
19213 int last_x;
19214 {
19215 /* If the face of this glyph string has to be drawn to the end of
19216 the drawing area, set S->extends_to_end_of_line_p. */
19217
19218 if (start == s->row->used[s->area]
19219 && s->area == TEXT_AREA
19220 && ((s->row->fill_line_p
19221 && (s->hl == DRAW_NORMAL_TEXT
19222 || s->hl == DRAW_IMAGE_RAISED
19223 || s->hl == DRAW_IMAGE_SUNKEN))
19224 || s->hl == DRAW_MOUSE_FACE))
19225 s->extends_to_end_of_line_p = 1;
19226
19227 /* If S extends its face to the end of the line, set its
19228 background_width to the distance to the right edge of the drawing
19229 area. */
19230 if (s->extends_to_end_of_line_p)
19231 s->background_width = last_x - s->x + 1;
19232 else
19233 s->background_width = s->width;
19234 }
19235
19236
19237 /* Compute overhangs and x-positions for glyph string S and its
19238 predecessors, or successors. X is the starting x-position for S.
19239 BACKWARD_P non-zero means process predecessors. */
19240
19241 static void
19242 compute_overhangs_and_x (s, x, backward_p)
19243 struct glyph_string *s;
19244 int x;
19245 int backward_p;
19246 {
19247 if (backward_p)
19248 {
19249 while (s)
19250 {
19251 if (rif->compute_glyph_string_overhangs)
19252 rif->compute_glyph_string_overhangs (s);
19253 x -= s->width;
19254 s->x = x;
19255 s = s->prev;
19256 }
19257 }
19258 else
19259 {
19260 while (s)
19261 {
19262 if (rif->compute_glyph_string_overhangs)
19263 rif->compute_glyph_string_overhangs (s);
19264 s->x = x;
19265 x += s->width;
19266 s = s->next;
19267 }
19268 }
19269 }
19270
19271
19272
19273 /* The following macros are only called from draw_glyphs below.
19274 They reference the following parameters of that function directly:
19275 `w', `row', `area', and `overlap_p'
19276 as well as the following local variables:
19277 `s', `f', and `hdc' (in W32) */
19278
19279 #ifdef HAVE_NTGUI
19280 /* On W32, silently add local `hdc' variable to argument list of
19281 init_glyph_string. */
19282 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19283 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19284 #else
19285 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19286 init_glyph_string (s, char2b, w, row, area, start, hl)
19287 #endif
19288
19289 /* Add a glyph string for a stretch glyph to the list of strings
19290 between HEAD and TAIL. START is the index of the stretch glyph in
19291 row area AREA of glyph row ROW. END is the index of the last glyph
19292 in that glyph row area. X is the current output position assigned
19293 to the new glyph string constructed. HL overrides that face of the
19294 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19295 is the right-most x-position of the drawing area. */
19296
19297 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19298 and below -- keep them on one line. */
19299 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19300 do \
19301 { \
19302 s = (struct glyph_string *) alloca (sizeof *s); \
19303 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19304 START = fill_stretch_glyph_string (s, row, area, START, END); \
19305 append_glyph_string (&HEAD, &TAIL, s); \
19306 s->x = (X); \
19307 } \
19308 while (0)
19309
19310
19311 /* Add a glyph string for an image glyph to the list of strings
19312 between HEAD and TAIL. START is the index of the image glyph in
19313 row area AREA of glyph row ROW. END is the index of the last glyph
19314 in that glyph row area. X is the current output position assigned
19315 to the new glyph string constructed. HL overrides that face of the
19316 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19317 is the right-most x-position of the drawing area. */
19318
19319 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19320 do \
19321 { \
19322 s = (struct glyph_string *) alloca (sizeof *s); \
19323 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19324 fill_image_glyph_string (s); \
19325 append_glyph_string (&HEAD, &TAIL, s); \
19326 ++START; \
19327 s->x = (X); \
19328 } \
19329 while (0)
19330
19331
19332 /* Add a glyph string for a sequence of character glyphs to the list
19333 of strings between HEAD and TAIL. START is the index of the first
19334 glyph in row area AREA of glyph row ROW that is part of the new
19335 glyph string. END is the index of the last glyph in that glyph row
19336 area. X is the current output position assigned to the new glyph
19337 string constructed. HL overrides that face of the glyph; e.g. it
19338 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19339 right-most x-position of the drawing area. */
19340
19341 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19342 do \
19343 { \
19344 int c, face_id; \
19345 XChar2b *char2b; \
19346 \
19347 c = (row)->glyphs[area][START].u.ch; \
19348 face_id = (row)->glyphs[area][START].face_id; \
19349 \
19350 s = (struct glyph_string *) alloca (sizeof *s); \
19351 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19352 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19353 append_glyph_string (&HEAD, &TAIL, s); \
19354 s->x = (X); \
19355 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19356 } \
19357 while (0)
19358
19359
19360 /* Add a glyph string for a composite sequence to the list of strings
19361 between HEAD and TAIL. START is the index of the first glyph in
19362 row area AREA of glyph row ROW that is part of the new glyph
19363 string. END is the index of the last glyph in that glyph row area.
19364 X is the current output position assigned to the new glyph string
19365 constructed. HL overrides that face of the glyph; e.g. it is
19366 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19367 x-position of the drawing area. */
19368
19369 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19370 do { \
19371 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19372 int face_id = (row)->glyphs[area][START].face_id; \
19373 struct face *base_face = FACE_FROM_ID (f, face_id); \
19374 struct composition *cmp = composition_table[cmp_id]; \
19375 int glyph_len = cmp->glyph_len; \
19376 XChar2b *char2b; \
19377 struct face **faces; \
19378 struct glyph_string *first_s = NULL; \
19379 int n; \
19380 \
19381 base_face = base_face->ascii_face; \
19382 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19383 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19384 /* At first, fill in `char2b' and `faces'. */ \
19385 for (n = 0; n < glyph_len; n++) \
19386 { \
19387 int c = COMPOSITION_GLYPH (cmp, n); \
19388 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19389 faces[n] = FACE_FROM_ID (f, this_face_id); \
19390 get_char_face_and_encoding (f, c, this_face_id, \
19391 char2b + n, 1, 1); \
19392 } \
19393 \
19394 /* Make glyph_strings for each glyph sequence that is drawable by \
19395 the same face, and append them to HEAD/TAIL. */ \
19396 for (n = 0; n < cmp->glyph_len;) \
19397 { \
19398 s = (struct glyph_string *) alloca (sizeof *s); \
19399 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19400 append_glyph_string (&(HEAD), &(TAIL), s); \
19401 s->cmp = cmp; \
19402 s->gidx = n; \
19403 s->x = (X); \
19404 \
19405 if (n == 0) \
19406 first_s = s; \
19407 \
19408 n = fill_composite_glyph_string (s, faces, overlaps); \
19409 } \
19410 \
19411 ++START; \
19412 s = first_s; \
19413 } while (0)
19414
19415
19416 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19417 of AREA of glyph row ROW on window W between indices START and END.
19418 HL overrides the face for drawing glyph strings, e.g. it is
19419 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19420 x-positions of the drawing area.
19421
19422 This is an ugly monster macro construct because we must use alloca
19423 to allocate glyph strings (because draw_glyphs can be called
19424 asynchronously). */
19425
19426 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19427 do \
19428 { \
19429 HEAD = TAIL = NULL; \
19430 while (START < END) \
19431 { \
19432 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19433 switch (first_glyph->type) \
19434 { \
19435 case CHAR_GLYPH: \
19436 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19437 HL, X, LAST_X); \
19438 break; \
19439 \
19440 case COMPOSITE_GLYPH: \
19441 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19442 HL, X, LAST_X); \
19443 break; \
19444 \
19445 case STRETCH_GLYPH: \
19446 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19447 HL, X, LAST_X); \
19448 break; \
19449 \
19450 case IMAGE_GLYPH: \
19451 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19452 HL, X, LAST_X); \
19453 break; \
19454 \
19455 default: \
19456 abort (); \
19457 } \
19458 \
19459 set_glyph_string_background_width (s, START, LAST_X); \
19460 (X) += s->width; \
19461 } \
19462 } \
19463 while (0)
19464
19465
19466 /* Draw glyphs between START and END in AREA of ROW on window W,
19467 starting at x-position X. X is relative to AREA in W. HL is a
19468 face-override with the following meaning:
19469
19470 DRAW_NORMAL_TEXT draw normally
19471 DRAW_CURSOR draw in cursor face
19472 DRAW_MOUSE_FACE draw in mouse face.
19473 DRAW_INVERSE_VIDEO draw in mode line face
19474 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19475 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19476
19477 If OVERLAPS is non-zero, draw only the foreground of characters and
19478 clip to the physical height of ROW. Non-zero value also defines
19479 the overlapping part to be drawn:
19480
19481 OVERLAPS_PRED overlap with preceding rows
19482 OVERLAPS_SUCC overlap with succeeding rows
19483 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19484 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19485
19486 Value is the x-position reached, relative to AREA of W. */
19487
19488 static int
19489 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19490 struct window *w;
19491 int x;
19492 struct glyph_row *row;
19493 enum glyph_row_area area;
19494 int start, end;
19495 enum draw_glyphs_face hl;
19496 int overlaps;
19497 {
19498 struct glyph_string *head, *tail;
19499 struct glyph_string *s;
19500 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19501 int last_x, area_width;
19502 int x_reached;
19503 int i, j;
19504 struct frame *f = XFRAME (WINDOW_FRAME (w));
19505 DECLARE_HDC (hdc);
19506
19507 ALLOCATE_HDC (hdc, f);
19508
19509 /* Let's rather be paranoid than getting a SEGV. */
19510 end = min (end, row->used[area]);
19511 start = max (0, start);
19512 start = min (end, start);
19513
19514 /* Translate X to frame coordinates. Set last_x to the right
19515 end of the drawing area. */
19516 if (row->full_width_p)
19517 {
19518 /* X is relative to the left edge of W, without scroll bars
19519 or fringes. */
19520 x += WINDOW_LEFT_EDGE_X (w);
19521 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19522 }
19523 else
19524 {
19525 int area_left = window_box_left (w, area);
19526 x += area_left;
19527 area_width = window_box_width (w, area);
19528 last_x = area_left + area_width;
19529 }
19530
19531 /* Build a doubly-linked list of glyph_string structures between
19532 head and tail from what we have to draw. Note that the macro
19533 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19534 the reason we use a separate variable `i'. */
19535 i = start;
19536 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19537 if (tail)
19538 x_reached = tail->x + tail->background_width;
19539 else
19540 x_reached = x;
19541
19542 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19543 the row, redraw some glyphs in front or following the glyph
19544 strings built above. */
19545 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19546 {
19547 int dummy_x = 0;
19548 struct glyph_string *h, *t;
19549
19550 /* Compute overhangs for all glyph strings. */
19551 if (rif->compute_glyph_string_overhangs)
19552 for (s = head; s; s = s->next)
19553 rif->compute_glyph_string_overhangs (s);
19554
19555 /* Prepend glyph strings for glyphs in front of the first glyph
19556 string that are overwritten because of the first glyph
19557 string's left overhang. The background of all strings
19558 prepended must be drawn because the first glyph string
19559 draws over it. */
19560 i = left_overwritten (head);
19561 if (i >= 0)
19562 {
19563 j = i;
19564 BUILD_GLYPH_STRINGS (j, start, h, t,
19565 DRAW_NORMAL_TEXT, dummy_x, last_x);
19566 start = i;
19567 compute_overhangs_and_x (t, head->x, 1);
19568 prepend_glyph_string_lists (&head, &tail, h, t);
19569 clip_head = head;
19570 }
19571
19572 /* Prepend glyph strings for glyphs in front of the first glyph
19573 string that overwrite that glyph string because of their
19574 right overhang. For these strings, only the foreground must
19575 be drawn, because it draws over the glyph string at `head'.
19576 The background must not be drawn because this would overwrite
19577 right overhangs of preceding glyphs for which no glyph
19578 strings exist. */
19579 i = left_overwriting (head);
19580 if (i >= 0)
19581 {
19582 clip_head = head;
19583 BUILD_GLYPH_STRINGS (i, start, h, t,
19584 DRAW_NORMAL_TEXT, dummy_x, last_x);
19585 for (s = h; s; s = s->next)
19586 s->background_filled_p = 1;
19587 compute_overhangs_and_x (t, head->x, 1);
19588 prepend_glyph_string_lists (&head, &tail, h, t);
19589 }
19590
19591 /* Append glyphs strings for glyphs following the last glyph
19592 string tail that are overwritten by tail. The background of
19593 these strings has to be drawn because tail's foreground draws
19594 over it. */
19595 i = right_overwritten (tail);
19596 if (i >= 0)
19597 {
19598 BUILD_GLYPH_STRINGS (end, i, h, t,
19599 DRAW_NORMAL_TEXT, x, last_x);
19600 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19601 append_glyph_string_lists (&head, &tail, h, t);
19602 clip_tail = tail;
19603 }
19604
19605 /* Append glyph strings for glyphs following the last glyph
19606 string tail that overwrite tail. The foreground of such
19607 glyphs has to be drawn because it writes into the background
19608 of tail. The background must not be drawn because it could
19609 paint over the foreground of following glyphs. */
19610 i = right_overwriting (tail);
19611 if (i >= 0)
19612 {
19613 clip_tail = tail;
19614 BUILD_GLYPH_STRINGS (end, i, h, t,
19615 DRAW_NORMAL_TEXT, x, last_x);
19616 for (s = h; s; s = s->next)
19617 s->background_filled_p = 1;
19618 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19619 append_glyph_string_lists (&head, &tail, h, t);
19620 }
19621 if (clip_head || clip_tail)
19622 for (s = head; s; s = s->next)
19623 {
19624 s->clip_head = clip_head;
19625 s->clip_tail = clip_tail;
19626 }
19627 }
19628
19629 /* Draw all strings. */
19630 for (s = head; s; s = s->next)
19631 rif->draw_glyph_string (s);
19632
19633 if (area == TEXT_AREA
19634 && !row->full_width_p
19635 /* When drawing overlapping rows, only the glyph strings'
19636 foreground is drawn, which doesn't erase a cursor
19637 completely. */
19638 && !overlaps)
19639 {
19640 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19641 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19642 : (tail ? tail->x + tail->background_width : x));
19643
19644 int text_left = window_box_left (w, TEXT_AREA);
19645 x0 -= text_left;
19646 x1 -= text_left;
19647
19648 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19649 row->y, MATRIX_ROW_BOTTOM_Y (row));
19650 }
19651
19652 /* Value is the x-position up to which drawn, relative to AREA of W.
19653 This doesn't include parts drawn because of overhangs. */
19654 if (row->full_width_p)
19655 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19656 else
19657 x_reached -= window_box_left (w, area);
19658
19659 RELEASE_HDC (hdc, f);
19660
19661 return x_reached;
19662 }
19663
19664 /* Expand row matrix if too narrow. Don't expand if area
19665 is not present. */
19666
19667 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19668 { \
19669 if (!fonts_changed_p \
19670 && (it->glyph_row->glyphs[area] \
19671 < it->glyph_row->glyphs[area + 1])) \
19672 { \
19673 it->w->ncols_scale_factor++; \
19674 fonts_changed_p = 1; \
19675 } \
19676 }
19677
19678 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19679 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19680
19681 static INLINE void
19682 append_glyph (it)
19683 struct it *it;
19684 {
19685 struct glyph *glyph;
19686 enum glyph_row_area area = it->area;
19687
19688 xassert (it->glyph_row);
19689 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19690
19691 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19692 if (glyph < it->glyph_row->glyphs[area + 1])
19693 {
19694 glyph->charpos = CHARPOS (it->position);
19695 glyph->object = it->object;
19696 glyph->pixel_width = it->pixel_width;
19697 glyph->ascent = it->ascent;
19698 glyph->descent = it->descent;
19699 glyph->voffset = it->voffset;
19700 glyph->type = CHAR_GLYPH;
19701 glyph->multibyte_p = it->multibyte_p;
19702 glyph->left_box_line_p = it->start_of_box_run_p;
19703 glyph->right_box_line_p = it->end_of_box_run_p;
19704 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19705 || it->phys_descent > it->descent);
19706 glyph->padding_p = 0;
19707 glyph->glyph_not_available_p = it->glyph_not_available_p;
19708 glyph->face_id = it->face_id;
19709 glyph->u.ch = it->char_to_display;
19710 glyph->slice = null_glyph_slice;
19711 glyph->font_type = FONT_TYPE_UNKNOWN;
19712 ++it->glyph_row->used[area];
19713 }
19714 else
19715 IT_EXPAND_MATRIX_WIDTH (it, area);
19716 }
19717
19718 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19719 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19720
19721 static INLINE void
19722 append_composite_glyph (it)
19723 struct it *it;
19724 {
19725 struct glyph *glyph;
19726 enum glyph_row_area area = it->area;
19727
19728 xassert (it->glyph_row);
19729
19730 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19731 if (glyph < it->glyph_row->glyphs[area + 1])
19732 {
19733 glyph->charpos = CHARPOS (it->position);
19734 glyph->object = it->object;
19735 glyph->pixel_width = it->pixel_width;
19736 glyph->ascent = it->ascent;
19737 glyph->descent = it->descent;
19738 glyph->voffset = it->voffset;
19739 glyph->type = COMPOSITE_GLYPH;
19740 glyph->multibyte_p = it->multibyte_p;
19741 glyph->left_box_line_p = it->start_of_box_run_p;
19742 glyph->right_box_line_p = it->end_of_box_run_p;
19743 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19744 || it->phys_descent > it->descent);
19745 glyph->padding_p = 0;
19746 glyph->glyph_not_available_p = 0;
19747 glyph->face_id = it->face_id;
19748 glyph->u.cmp_id = it->cmp_id;
19749 glyph->slice = null_glyph_slice;
19750 glyph->font_type = FONT_TYPE_UNKNOWN;
19751 ++it->glyph_row->used[area];
19752 }
19753 else
19754 IT_EXPAND_MATRIX_WIDTH (it, area);
19755 }
19756
19757
19758 /* Change IT->ascent and IT->height according to the setting of
19759 IT->voffset. */
19760
19761 static INLINE void
19762 take_vertical_position_into_account (it)
19763 struct it *it;
19764 {
19765 if (it->voffset)
19766 {
19767 if (it->voffset < 0)
19768 /* Increase the ascent so that we can display the text higher
19769 in the line. */
19770 it->ascent -= it->voffset;
19771 else
19772 /* Increase the descent so that we can display the text lower
19773 in the line. */
19774 it->descent += it->voffset;
19775 }
19776 }
19777
19778
19779 /* Produce glyphs/get display metrics for the image IT is loaded with.
19780 See the description of struct display_iterator in dispextern.h for
19781 an overview of struct display_iterator. */
19782
19783 static void
19784 produce_image_glyph (it)
19785 struct it *it;
19786 {
19787 struct image *img;
19788 struct face *face;
19789 int glyph_ascent, crop;
19790 struct glyph_slice slice;
19791
19792 xassert (it->what == IT_IMAGE);
19793
19794 face = FACE_FROM_ID (it->f, it->face_id);
19795 xassert (face);
19796 /* Make sure X resources of the face is loaded. */
19797 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19798
19799 if (it->image_id < 0)
19800 {
19801 /* Fringe bitmap. */
19802 it->ascent = it->phys_ascent = 0;
19803 it->descent = it->phys_descent = 0;
19804 it->pixel_width = 0;
19805 it->nglyphs = 0;
19806 return;
19807 }
19808
19809 img = IMAGE_FROM_ID (it->f, it->image_id);
19810 xassert (img);
19811 /* Make sure X resources of the image is loaded. */
19812 prepare_image_for_display (it->f, img);
19813
19814 slice.x = slice.y = 0;
19815 slice.width = img->width;
19816 slice.height = img->height;
19817
19818 if (INTEGERP (it->slice.x))
19819 slice.x = XINT (it->slice.x);
19820 else if (FLOATP (it->slice.x))
19821 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19822
19823 if (INTEGERP (it->slice.y))
19824 slice.y = XINT (it->slice.y);
19825 else if (FLOATP (it->slice.y))
19826 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19827
19828 if (INTEGERP (it->slice.width))
19829 slice.width = XINT (it->slice.width);
19830 else if (FLOATP (it->slice.width))
19831 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19832
19833 if (INTEGERP (it->slice.height))
19834 slice.height = XINT (it->slice.height);
19835 else if (FLOATP (it->slice.height))
19836 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19837
19838 if (slice.x >= img->width)
19839 slice.x = img->width;
19840 if (slice.y >= img->height)
19841 slice.y = img->height;
19842 if (slice.x + slice.width >= img->width)
19843 slice.width = img->width - slice.x;
19844 if (slice.y + slice.height > img->height)
19845 slice.height = img->height - slice.y;
19846
19847 if (slice.width == 0 || slice.height == 0)
19848 return;
19849
19850 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19851
19852 it->descent = slice.height - glyph_ascent;
19853 if (slice.y == 0)
19854 it->descent += img->vmargin;
19855 if (slice.y + slice.height == img->height)
19856 it->descent += img->vmargin;
19857 it->phys_descent = it->descent;
19858
19859 it->pixel_width = slice.width;
19860 if (slice.x == 0)
19861 it->pixel_width += img->hmargin;
19862 if (slice.x + slice.width == img->width)
19863 it->pixel_width += img->hmargin;
19864
19865 /* It's quite possible for images to have an ascent greater than
19866 their height, so don't get confused in that case. */
19867 if (it->descent < 0)
19868 it->descent = 0;
19869
19870 #if 0 /* this breaks image tiling */
19871 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19872 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19873 if (face_ascent > it->ascent)
19874 it->ascent = it->phys_ascent = face_ascent;
19875 #endif
19876
19877 it->nglyphs = 1;
19878
19879 if (face->box != FACE_NO_BOX)
19880 {
19881 if (face->box_line_width > 0)
19882 {
19883 if (slice.y == 0)
19884 it->ascent += face->box_line_width;
19885 if (slice.y + slice.height == img->height)
19886 it->descent += face->box_line_width;
19887 }
19888
19889 if (it->start_of_box_run_p && slice.x == 0)
19890 it->pixel_width += abs (face->box_line_width);
19891 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19892 it->pixel_width += abs (face->box_line_width);
19893 }
19894
19895 take_vertical_position_into_account (it);
19896
19897 /* Automatically crop wide image glyphs at right edge so we can
19898 draw the cursor on same display row. */
19899 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
19900 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
19901 {
19902 it->pixel_width -= crop;
19903 slice.width -= crop;
19904 }
19905
19906 if (it->glyph_row)
19907 {
19908 struct glyph *glyph;
19909 enum glyph_row_area area = it->area;
19910
19911 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19912 if (glyph < it->glyph_row->glyphs[area + 1])
19913 {
19914 glyph->charpos = CHARPOS (it->position);
19915 glyph->object = it->object;
19916 glyph->pixel_width = it->pixel_width;
19917 glyph->ascent = glyph_ascent;
19918 glyph->descent = it->descent;
19919 glyph->voffset = it->voffset;
19920 glyph->type = IMAGE_GLYPH;
19921 glyph->multibyte_p = it->multibyte_p;
19922 glyph->left_box_line_p = it->start_of_box_run_p;
19923 glyph->right_box_line_p = it->end_of_box_run_p;
19924 glyph->overlaps_vertically_p = 0;
19925 glyph->padding_p = 0;
19926 glyph->glyph_not_available_p = 0;
19927 glyph->face_id = it->face_id;
19928 glyph->u.img_id = img->id;
19929 glyph->slice = slice;
19930 glyph->font_type = FONT_TYPE_UNKNOWN;
19931 ++it->glyph_row->used[area];
19932 }
19933 else
19934 IT_EXPAND_MATRIX_WIDTH (it, area);
19935 }
19936 }
19937
19938
19939 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19940 of the glyph, WIDTH and HEIGHT are the width and height of the
19941 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19942
19943 static void
19944 append_stretch_glyph (it, object, width, height, ascent)
19945 struct it *it;
19946 Lisp_Object object;
19947 int width, height;
19948 int ascent;
19949 {
19950 struct glyph *glyph;
19951 enum glyph_row_area area = it->area;
19952
19953 xassert (ascent >= 0 && ascent <= height);
19954
19955 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19956 if (glyph < it->glyph_row->glyphs[area + 1])
19957 {
19958 glyph->charpos = CHARPOS (it->position);
19959 glyph->object = object;
19960 glyph->pixel_width = width;
19961 glyph->ascent = ascent;
19962 glyph->descent = height - ascent;
19963 glyph->voffset = it->voffset;
19964 glyph->type = STRETCH_GLYPH;
19965 glyph->multibyte_p = it->multibyte_p;
19966 glyph->left_box_line_p = it->start_of_box_run_p;
19967 glyph->right_box_line_p = it->end_of_box_run_p;
19968 glyph->overlaps_vertically_p = 0;
19969 glyph->padding_p = 0;
19970 glyph->glyph_not_available_p = 0;
19971 glyph->face_id = it->face_id;
19972 glyph->u.stretch.ascent = ascent;
19973 glyph->u.stretch.height = height;
19974 glyph->slice = null_glyph_slice;
19975 glyph->font_type = FONT_TYPE_UNKNOWN;
19976 ++it->glyph_row->used[area];
19977 }
19978 else
19979 IT_EXPAND_MATRIX_WIDTH (it, area);
19980 }
19981
19982
19983 /* Produce a stretch glyph for iterator IT. IT->object is the value
19984 of the glyph property displayed. The value must be a list
19985 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19986 being recognized:
19987
19988 1. `:width WIDTH' specifies that the space should be WIDTH *
19989 canonical char width wide. WIDTH may be an integer or floating
19990 point number.
19991
19992 2. `:relative-width FACTOR' specifies that the width of the stretch
19993 should be computed from the width of the first character having the
19994 `glyph' property, and should be FACTOR times that width.
19995
19996 3. `:align-to HPOS' specifies that the space should be wide enough
19997 to reach HPOS, a value in canonical character units.
19998
19999 Exactly one of the above pairs must be present.
20000
20001 4. `:height HEIGHT' specifies that the height of the stretch produced
20002 should be HEIGHT, measured in canonical character units.
20003
20004 5. `:relative-height FACTOR' specifies that the height of the
20005 stretch should be FACTOR times the height of the characters having
20006 the glyph property.
20007
20008 Either none or exactly one of 4 or 5 must be present.
20009
20010 6. `:ascent ASCENT' specifies that ASCENT percent of the height
20011 of the stretch should be used for the ascent of the stretch.
20012 ASCENT must be in the range 0 <= ASCENT <= 100. */
20013
20014 static void
20015 produce_stretch_glyph (it)
20016 struct it *it;
20017 {
20018 /* (space :width WIDTH :height HEIGHT ...) */
20019 Lisp_Object prop, plist;
20020 int width = 0, height = 0, align_to = -1;
20021 int zero_width_ok_p = 0, zero_height_ok_p = 0;
20022 int ascent = 0;
20023 double tem;
20024 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20025 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
20026
20027 PREPARE_FACE_FOR_DISPLAY (it->f, face);
20028
20029 /* List should start with `space'. */
20030 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
20031 plist = XCDR (it->object);
20032
20033 /* Compute the width of the stretch. */
20034 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
20035 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
20036 {
20037 /* Absolute width `:width WIDTH' specified and valid. */
20038 zero_width_ok_p = 1;
20039 width = (int)tem;
20040 }
20041 else if (prop = Fplist_get (plist, QCrelative_width),
20042 NUMVAL (prop) > 0)
20043 {
20044 /* Relative width `:relative-width FACTOR' specified and valid.
20045 Compute the width of the characters having the `glyph'
20046 property. */
20047 struct it it2;
20048 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20049
20050 it2 = *it;
20051 if (it->multibyte_p)
20052 {
20053 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20054 - IT_BYTEPOS (*it));
20055 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20056 }
20057 else
20058 it2.c = *p, it2.len = 1;
20059
20060 it2.glyph_row = NULL;
20061 it2.what = IT_CHARACTER;
20062 x_produce_glyphs (&it2);
20063 width = NUMVAL (prop) * it2.pixel_width;
20064 }
20065 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20066 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20067 {
20068 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20069 align_to = (align_to < 0
20070 ? 0
20071 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20072 else if (align_to < 0)
20073 align_to = window_box_left_offset (it->w, TEXT_AREA);
20074 width = max (0, (int)tem + align_to - it->current_x);
20075 zero_width_ok_p = 1;
20076 }
20077 else
20078 /* Nothing specified -> width defaults to canonical char width. */
20079 width = FRAME_COLUMN_WIDTH (it->f);
20080
20081 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20082 width = 1;
20083
20084 /* Compute height. */
20085 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20086 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20087 {
20088 height = (int)tem;
20089 zero_height_ok_p = 1;
20090 }
20091 else if (prop = Fplist_get (plist, QCrelative_height),
20092 NUMVAL (prop) > 0)
20093 height = FONT_HEIGHT (font) * NUMVAL (prop);
20094 else
20095 height = FONT_HEIGHT (font);
20096
20097 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20098 height = 1;
20099
20100 /* Compute percentage of height used for ascent. If
20101 `:ascent ASCENT' is present and valid, use that. Otherwise,
20102 derive the ascent from the font in use. */
20103 if (prop = Fplist_get (plist, QCascent),
20104 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20105 ascent = height * NUMVAL (prop) / 100.0;
20106 else if (!NILP (prop)
20107 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20108 ascent = min (max (0, (int)tem), height);
20109 else
20110 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20111
20112 if (width > 0 && !it->truncate_lines_p
20113 && it->current_x + width > it->last_visible_x)
20114 width = it->last_visible_x - it->current_x - 1;
20115
20116 if (width > 0 && height > 0 && it->glyph_row)
20117 {
20118 Lisp_Object object = it->stack[it->sp - 1].string;
20119 if (!STRINGP (object))
20120 object = it->w->buffer;
20121 append_stretch_glyph (it, object, width, height, ascent);
20122 }
20123
20124 it->pixel_width = width;
20125 it->ascent = it->phys_ascent = ascent;
20126 it->descent = it->phys_descent = height - it->ascent;
20127 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20128
20129 take_vertical_position_into_account (it);
20130 }
20131
20132 /* Get line-height and line-spacing property at point.
20133 If line-height has format (HEIGHT TOTAL), return TOTAL
20134 in TOTAL_HEIGHT. */
20135
20136 static Lisp_Object
20137 get_line_height_property (it, prop)
20138 struct it *it;
20139 Lisp_Object prop;
20140 {
20141 Lisp_Object position;
20142
20143 if (STRINGP (it->object))
20144 position = make_number (IT_STRING_CHARPOS (*it));
20145 else if (BUFFERP (it->object))
20146 position = make_number (IT_CHARPOS (*it));
20147 else
20148 return Qnil;
20149
20150 return Fget_char_property (position, prop, it->object);
20151 }
20152
20153 /* Calculate line-height and line-spacing properties.
20154 An integer value specifies explicit pixel value.
20155 A float value specifies relative value to current face height.
20156 A cons (float . face-name) specifies relative value to
20157 height of specified face font.
20158
20159 Returns height in pixels, or nil. */
20160
20161
20162 static Lisp_Object
20163 calc_line_height_property (it, val, font, boff, override)
20164 struct it *it;
20165 Lisp_Object val;
20166 XFontStruct *font;
20167 int boff, override;
20168 {
20169 Lisp_Object face_name = Qnil;
20170 int ascent, descent, height;
20171
20172 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20173 return val;
20174
20175 if (CONSP (val))
20176 {
20177 face_name = XCAR (val);
20178 val = XCDR (val);
20179 if (!NUMBERP (val))
20180 val = make_number (1);
20181 if (NILP (face_name))
20182 {
20183 height = it->ascent + it->descent;
20184 goto scale;
20185 }
20186 }
20187
20188 if (NILP (face_name))
20189 {
20190 font = FRAME_FONT (it->f);
20191 boff = FRAME_BASELINE_OFFSET (it->f);
20192 }
20193 else if (EQ (face_name, Qt))
20194 {
20195 override = 0;
20196 }
20197 else
20198 {
20199 int face_id;
20200 struct face *face;
20201 struct font_info *font_info;
20202
20203 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20204 if (face_id < 0)
20205 return make_number (-1);
20206
20207 face = FACE_FROM_ID (it->f, face_id);
20208 font = face->font;
20209 if (font == NULL)
20210 return make_number (-1);
20211
20212 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20213 boff = font_info->baseline_offset;
20214 if (font_info->vertical_centering)
20215 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20216 }
20217
20218 ascent = FONT_BASE (font) + boff;
20219 descent = FONT_DESCENT (font) - boff;
20220
20221 if (override)
20222 {
20223 it->override_ascent = ascent;
20224 it->override_descent = descent;
20225 it->override_boff = boff;
20226 }
20227
20228 height = ascent + descent;
20229
20230 scale:
20231 if (FLOATP (val))
20232 height = (int)(XFLOAT_DATA (val) * height);
20233 else if (INTEGERP (val))
20234 height *= XINT (val);
20235
20236 return make_number (height);
20237 }
20238
20239
20240 /* RIF:
20241 Produce glyphs/get display metrics for the display element IT is
20242 loaded with. See the description of struct it in dispextern.h
20243 for an overview of struct it. */
20244
20245 void
20246 x_produce_glyphs (it)
20247 struct it *it;
20248 {
20249 int extra_line_spacing = it->extra_line_spacing;
20250
20251 it->glyph_not_available_p = 0;
20252
20253 if (it->what == IT_CHARACTER)
20254 {
20255 XChar2b char2b;
20256 XFontStruct *font;
20257 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20258 XCharStruct *pcm;
20259 int font_not_found_p;
20260 struct font_info *font_info;
20261 int boff; /* baseline offset */
20262 /* We may change it->multibyte_p upon unibyte<->multibyte
20263 conversion. So, save the current value now and restore it
20264 later.
20265
20266 Note: It seems that we don't have to record multibyte_p in
20267 struct glyph because the character code itself tells if or
20268 not the character is multibyte. Thus, in the future, we must
20269 consider eliminating the field `multibyte_p' in the struct
20270 glyph. */
20271 int saved_multibyte_p = it->multibyte_p;
20272
20273 /* Maybe translate single-byte characters to multibyte, or the
20274 other way. */
20275 it->char_to_display = it->c;
20276 if (!ASCII_BYTE_P (it->c))
20277 {
20278 if (unibyte_display_via_language_environment
20279 && SINGLE_BYTE_CHAR_P (it->c)
20280 && (it->c >= 0240
20281 || !NILP (Vnonascii_translation_table)))
20282 {
20283 it->char_to_display = unibyte_char_to_multibyte (it->c);
20284 it->multibyte_p = 1;
20285 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20286 face = FACE_FROM_ID (it->f, it->face_id);
20287 }
20288 else if (!SINGLE_BYTE_CHAR_P (it->c)
20289 && !it->multibyte_p)
20290 {
20291 it->multibyte_p = 1;
20292 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20293 face = FACE_FROM_ID (it->f, it->face_id);
20294 }
20295 }
20296
20297 /* Get font to use. Encode IT->char_to_display. */
20298 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20299 &char2b, it->multibyte_p, 0);
20300 font = face->font;
20301
20302 /* When no suitable font found, use the default font. */
20303 font_not_found_p = font == NULL;
20304 if (font_not_found_p)
20305 {
20306 font = FRAME_FONT (it->f);
20307 boff = FRAME_BASELINE_OFFSET (it->f);
20308 font_info = NULL;
20309 }
20310 else
20311 {
20312 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20313 boff = font_info->baseline_offset;
20314 if (font_info->vertical_centering)
20315 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20316 }
20317
20318 if (it->char_to_display >= ' '
20319 && (!it->multibyte_p || it->char_to_display < 128))
20320 {
20321 /* Either unibyte or ASCII. */
20322 int stretched_p;
20323
20324 it->nglyphs = 1;
20325
20326 pcm = rif->per_char_metric (font, &char2b,
20327 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20328
20329 if (it->override_ascent >= 0)
20330 {
20331 it->ascent = it->override_ascent;
20332 it->descent = it->override_descent;
20333 boff = it->override_boff;
20334 }
20335 else
20336 {
20337 it->ascent = FONT_BASE (font) + boff;
20338 it->descent = FONT_DESCENT (font) - boff;
20339 }
20340
20341 if (pcm)
20342 {
20343 it->phys_ascent = pcm->ascent + boff;
20344 it->phys_descent = pcm->descent - boff;
20345 it->pixel_width = pcm->width;
20346 }
20347 else
20348 {
20349 it->glyph_not_available_p = 1;
20350 it->phys_ascent = it->ascent;
20351 it->phys_descent = it->descent;
20352 it->pixel_width = FONT_WIDTH (font);
20353 }
20354
20355 if (it->constrain_row_ascent_descent_p)
20356 {
20357 if (it->descent > it->max_descent)
20358 {
20359 it->ascent += it->descent - it->max_descent;
20360 it->descent = it->max_descent;
20361 }
20362 if (it->ascent > it->max_ascent)
20363 {
20364 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20365 it->ascent = it->max_ascent;
20366 }
20367 it->phys_ascent = min (it->phys_ascent, it->ascent);
20368 it->phys_descent = min (it->phys_descent, it->descent);
20369 extra_line_spacing = 0;
20370 }
20371
20372 /* If this is a space inside a region of text with
20373 `space-width' property, change its width. */
20374 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20375 if (stretched_p)
20376 it->pixel_width *= XFLOATINT (it->space_width);
20377
20378 /* If face has a box, add the box thickness to the character
20379 height. If character has a box line to the left and/or
20380 right, add the box line width to the character's width. */
20381 if (face->box != FACE_NO_BOX)
20382 {
20383 int thick = face->box_line_width;
20384
20385 if (thick > 0)
20386 {
20387 it->ascent += thick;
20388 it->descent += thick;
20389 }
20390 else
20391 thick = -thick;
20392
20393 if (it->start_of_box_run_p)
20394 it->pixel_width += thick;
20395 if (it->end_of_box_run_p)
20396 it->pixel_width += thick;
20397 }
20398
20399 /* If face has an overline, add the height of the overline
20400 (1 pixel) and a 1 pixel margin to the character height. */
20401 if (face->overline_p)
20402 it->ascent += overline_margin;
20403
20404 if (it->constrain_row_ascent_descent_p)
20405 {
20406 if (it->ascent > it->max_ascent)
20407 it->ascent = it->max_ascent;
20408 if (it->descent > it->max_descent)
20409 it->descent = it->max_descent;
20410 }
20411
20412 take_vertical_position_into_account (it);
20413
20414 /* If we have to actually produce glyphs, do it. */
20415 if (it->glyph_row)
20416 {
20417 if (stretched_p)
20418 {
20419 /* Translate a space with a `space-width' property
20420 into a stretch glyph. */
20421 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20422 / FONT_HEIGHT (font));
20423 append_stretch_glyph (it, it->object, it->pixel_width,
20424 it->ascent + it->descent, ascent);
20425 }
20426 else
20427 append_glyph (it);
20428
20429 /* If characters with lbearing or rbearing are displayed
20430 in this line, record that fact in a flag of the
20431 glyph row. This is used to optimize X output code. */
20432 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20433 it->glyph_row->contains_overlapping_glyphs_p = 1;
20434 }
20435 }
20436 else if (it->char_to_display == '\n')
20437 {
20438 /* A newline has no width but we need the height of the line.
20439 But if previous part of the line set a height, don't
20440 increase that height */
20441
20442 Lisp_Object height;
20443 Lisp_Object total_height = Qnil;
20444
20445 it->override_ascent = -1;
20446 it->pixel_width = 0;
20447 it->nglyphs = 0;
20448
20449 height = get_line_height_property(it, Qline_height);
20450 /* Split (line-height total-height) list */
20451 if (CONSP (height)
20452 && CONSP (XCDR (height))
20453 && NILP (XCDR (XCDR (height))))
20454 {
20455 total_height = XCAR (XCDR (height));
20456 height = XCAR (height);
20457 }
20458 height = calc_line_height_property(it, height, font, boff, 1);
20459
20460 if (it->override_ascent >= 0)
20461 {
20462 it->ascent = it->override_ascent;
20463 it->descent = it->override_descent;
20464 boff = it->override_boff;
20465 }
20466 else
20467 {
20468 it->ascent = FONT_BASE (font) + boff;
20469 it->descent = FONT_DESCENT (font) - boff;
20470 }
20471
20472 if (EQ (height, Qt))
20473 {
20474 if (it->descent > it->max_descent)
20475 {
20476 it->ascent += it->descent - it->max_descent;
20477 it->descent = it->max_descent;
20478 }
20479 if (it->ascent > it->max_ascent)
20480 {
20481 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20482 it->ascent = it->max_ascent;
20483 }
20484 it->phys_ascent = min (it->phys_ascent, it->ascent);
20485 it->phys_descent = min (it->phys_descent, it->descent);
20486 it->constrain_row_ascent_descent_p = 1;
20487 extra_line_spacing = 0;
20488 }
20489 else
20490 {
20491 Lisp_Object spacing;
20492
20493 it->phys_ascent = it->ascent;
20494 it->phys_descent = it->descent;
20495
20496 if ((it->max_ascent > 0 || it->max_descent > 0)
20497 && face->box != FACE_NO_BOX
20498 && face->box_line_width > 0)
20499 {
20500 it->ascent += face->box_line_width;
20501 it->descent += face->box_line_width;
20502 }
20503 if (!NILP (height)
20504 && XINT (height) > it->ascent + it->descent)
20505 it->ascent = XINT (height) - it->descent;
20506
20507 if (!NILP (total_height))
20508 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20509 else
20510 {
20511 spacing = get_line_height_property(it, Qline_spacing);
20512 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20513 }
20514 if (INTEGERP (spacing))
20515 {
20516 extra_line_spacing = XINT (spacing);
20517 if (!NILP (total_height))
20518 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20519 }
20520 }
20521 }
20522 else if (it->char_to_display == '\t')
20523 {
20524 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20525 int x = it->current_x + it->continuation_lines_width;
20526 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20527
20528 /* If the distance from the current position to the next tab
20529 stop is less than a space character width, use the
20530 tab stop after that. */
20531 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20532 next_tab_x += tab_width;
20533
20534 it->pixel_width = next_tab_x - x;
20535 it->nglyphs = 1;
20536 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20537 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20538
20539 if (it->glyph_row)
20540 {
20541 append_stretch_glyph (it, it->object, it->pixel_width,
20542 it->ascent + it->descent, it->ascent);
20543 }
20544 }
20545 else
20546 {
20547 /* A multi-byte character. Assume that the display width of the
20548 character is the width of the character multiplied by the
20549 width of the font. */
20550
20551 /* If we found a font, this font should give us the right
20552 metrics. If we didn't find a font, use the frame's
20553 default font and calculate the width of the character
20554 from the charset width; this is what old redisplay code
20555 did. */
20556
20557 pcm = rif->per_char_metric (font, &char2b,
20558 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20559
20560 if (font_not_found_p || !pcm)
20561 {
20562 int charset = CHAR_CHARSET (it->char_to_display);
20563
20564 it->glyph_not_available_p = 1;
20565 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20566 * CHARSET_WIDTH (charset));
20567 it->phys_ascent = FONT_BASE (font) + boff;
20568 it->phys_descent = FONT_DESCENT (font) - boff;
20569 }
20570 else
20571 {
20572 it->pixel_width = pcm->width;
20573 it->phys_ascent = pcm->ascent + boff;
20574 it->phys_descent = pcm->descent - boff;
20575 if (it->glyph_row
20576 && (pcm->lbearing < 0
20577 || pcm->rbearing > pcm->width))
20578 it->glyph_row->contains_overlapping_glyphs_p = 1;
20579 }
20580 it->nglyphs = 1;
20581 it->ascent = FONT_BASE (font) + boff;
20582 it->descent = FONT_DESCENT (font) - boff;
20583 if (face->box != FACE_NO_BOX)
20584 {
20585 int thick = face->box_line_width;
20586
20587 if (thick > 0)
20588 {
20589 it->ascent += thick;
20590 it->descent += thick;
20591 }
20592 else
20593 thick = - thick;
20594
20595 if (it->start_of_box_run_p)
20596 it->pixel_width += thick;
20597 if (it->end_of_box_run_p)
20598 it->pixel_width += thick;
20599 }
20600
20601 /* If face has an overline, add the height of the overline
20602 (1 pixel) and a 1 pixel margin to the character height. */
20603 if (face->overline_p)
20604 it->ascent += overline_margin;
20605
20606 take_vertical_position_into_account (it);
20607
20608 if (it->glyph_row)
20609 append_glyph (it);
20610 }
20611 it->multibyte_p = saved_multibyte_p;
20612 }
20613 else if (it->what == IT_COMPOSITION)
20614 {
20615 /* Note: A composition is represented as one glyph in the
20616 glyph matrix. There are no padding glyphs. */
20617 XChar2b char2b;
20618 XFontStruct *font;
20619 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20620 XCharStruct *pcm;
20621 int font_not_found_p;
20622 struct font_info *font_info;
20623 int boff; /* baseline offset */
20624 struct composition *cmp = composition_table[it->cmp_id];
20625
20626 /* Maybe translate single-byte characters to multibyte. */
20627 it->char_to_display = it->c;
20628 if (unibyte_display_via_language_environment
20629 && SINGLE_BYTE_CHAR_P (it->c)
20630 && (it->c >= 0240
20631 || (it->c >= 0200
20632 && !NILP (Vnonascii_translation_table))))
20633 {
20634 it->char_to_display = unibyte_char_to_multibyte (it->c);
20635 }
20636
20637 /* Get face and font to use. Encode IT->char_to_display. */
20638 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20639 face = FACE_FROM_ID (it->f, it->face_id);
20640 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20641 &char2b, it->multibyte_p, 0);
20642 font = face->font;
20643
20644 /* When no suitable font found, use the default font. */
20645 font_not_found_p = font == NULL;
20646 if (font_not_found_p)
20647 {
20648 font = FRAME_FONT (it->f);
20649 boff = FRAME_BASELINE_OFFSET (it->f);
20650 font_info = NULL;
20651 }
20652 else
20653 {
20654 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20655 boff = font_info->baseline_offset;
20656 if (font_info->vertical_centering)
20657 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20658 }
20659
20660 /* There are no padding glyphs, so there is only one glyph to
20661 produce for the composition. Important is that pixel_width,
20662 ascent and descent are the values of what is drawn by
20663 draw_glyphs (i.e. the values of the overall glyphs composed). */
20664 it->nglyphs = 1;
20665
20666 /* If we have not yet calculated pixel size data of glyphs of
20667 the composition for the current face font, calculate them
20668 now. Theoretically, we have to check all fonts for the
20669 glyphs, but that requires much time and memory space. So,
20670 here we check only the font of the first glyph. This leads
20671 to incorrect display very rarely, and C-l (recenter) can
20672 correct the display anyway. */
20673 if (cmp->font != (void *) font)
20674 {
20675 /* Ascent and descent of the font of the first character of
20676 this composition (adjusted by baseline offset). Ascent
20677 and descent of overall glyphs should not be less than
20678 them respectively. */
20679 int font_ascent = FONT_BASE (font) + boff;
20680 int font_descent = FONT_DESCENT (font) - boff;
20681 /* Bounding box of the overall glyphs. */
20682 int leftmost, rightmost, lowest, highest;
20683 int i, width, ascent, descent;
20684
20685 cmp->font = (void *) font;
20686
20687 /* Initialize the bounding box. */
20688 if (font_info
20689 && (pcm = rif->per_char_metric (font, &char2b,
20690 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20691 {
20692 width = pcm->width;
20693 ascent = pcm->ascent;
20694 descent = pcm->descent;
20695 }
20696 else
20697 {
20698 width = FONT_WIDTH (font);
20699 ascent = FONT_BASE (font);
20700 descent = FONT_DESCENT (font);
20701 }
20702
20703 rightmost = width;
20704 lowest = - descent + boff;
20705 highest = ascent + boff;
20706 leftmost = 0;
20707
20708 if (font_info
20709 && font_info->default_ascent
20710 && CHAR_TABLE_P (Vuse_default_ascent)
20711 && !NILP (Faref (Vuse_default_ascent,
20712 make_number (it->char_to_display))))
20713 highest = font_info->default_ascent + boff;
20714
20715 /* Draw the first glyph at the normal position. It may be
20716 shifted to right later if some other glyphs are drawn at
20717 the left. */
20718 cmp->offsets[0] = 0;
20719 cmp->offsets[1] = boff;
20720
20721 /* Set cmp->offsets for the remaining glyphs. */
20722 for (i = 1; i < cmp->glyph_len; i++)
20723 {
20724 int left, right, btm, top;
20725 int ch = COMPOSITION_GLYPH (cmp, i);
20726 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20727
20728 face = FACE_FROM_ID (it->f, face_id);
20729 get_char_face_and_encoding (it->f, ch, face->id,
20730 &char2b, it->multibyte_p, 0);
20731 font = face->font;
20732 if (font == NULL)
20733 {
20734 font = FRAME_FONT (it->f);
20735 boff = FRAME_BASELINE_OFFSET (it->f);
20736 font_info = NULL;
20737 }
20738 else
20739 {
20740 font_info
20741 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20742 boff = font_info->baseline_offset;
20743 if (font_info->vertical_centering)
20744 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20745 }
20746
20747 if (font_info
20748 && (pcm = rif->per_char_metric (font, &char2b,
20749 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20750 {
20751 width = pcm->width;
20752 ascent = pcm->ascent;
20753 descent = pcm->descent;
20754 }
20755 else
20756 {
20757 width = FONT_WIDTH (font);
20758 ascent = 1;
20759 descent = 0;
20760 }
20761
20762 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20763 {
20764 /* Relative composition with or without
20765 alternate chars. */
20766 left = (leftmost + rightmost - width) / 2;
20767 btm = - descent + boff;
20768 if (font_info && font_info->relative_compose
20769 && (! CHAR_TABLE_P (Vignore_relative_composition)
20770 || NILP (Faref (Vignore_relative_composition,
20771 make_number (ch)))))
20772 {
20773
20774 if (- descent >= font_info->relative_compose)
20775 /* One extra pixel between two glyphs. */
20776 btm = highest + 1;
20777 else if (ascent <= 0)
20778 /* One extra pixel between two glyphs. */
20779 btm = lowest - 1 - ascent - descent;
20780 }
20781 }
20782 else
20783 {
20784 /* A composition rule is specified by an integer
20785 value that encodes global and new reference
20786 points (GREF and NREF). GREF and NREF are
20787 specified by numbers as below:
20788
20789 0---1---2 -- ascent
20790 | |
20791 | |
20792 | |
20793 9--10--11 -- center
20794 | |
20795 ---3---4---5--- baseline
20796 | |
20797 6---7---8 -- descent
20798 */
20799 int rule = COMPOSITION_RULE (cmp, i);
20800 int gref, nref, grefx, grefy, nrefx, nrefy;
20801
20802 COMPOSITION_DECODE_RULE (rule, gref, nref);
20803 grefx = gref % 3, nrefx = nref % 3;
20804 grefy = gref / 3, nrefy = nref / 3;
20805
20806 left = (leftmost
20807 + grefx * (rightmost - leftmost) / 2
20808 - nrefx * width / 2);
20809 btm = ((grefy == 0 ? highest
20810 : grefy == 1 ? 0
20811 : grefy == 2 ? lowest
20812 : (highest + lowest) / 2)
20813 - (nrefy == 0 ? ascent + descent
20814 : nrefy == 1 ? descent - boff
20815 : nrefy == 2 ? 0
20816 : (ascent + descent) / 2));
20817 }
20818
20819 cmp->offsets[i * 2] = left;
20820 cmp->offsets[i * 2 + 1] = btm + descent;
20821
20822 /* Update the bounding box of the overall glyphs. */
20823 right = left + width;
20824 top = btm + descent + ascent;
20825 if (left < leftmost)
20826 leftmost = left;
20827 if (right > rightmost)
20828 rightmost = right;
20829 if (top > highest)
20830 highest = top;
20831 if (btm < lowest)
20832 lowest = btm;
20833 }
20834
20835 /* If there are glyphs whose x-offsets are negative,
20836 shift all glyphs to the right and make all x-offsets
20837 non-negative. */
20838 if (leftmost < 0)
20839 {
20840 for (i = 0; i < cmp->glyph_len; i++)
20841 cmp->offsets[i * 2] -= leftmost;
20842 rightmost -= leftmost;
20843 }
20844
20845 cmp->pixel_width = rightmost;
20846 cmp->ascent = highest;
20847 cmp->descent = - lowest;
20848 if (cmp->ascent < font_ascent)
20849 cmp->ascent = font_ascent;
20850 if (cmp->descent < font_descent)
20851 cmp->descent = font_descent;
20852 }
20853
20854 it->pixel_width = cmp->pixel_width;
20855 it->ascent = it->phys_ascent = cmp->ascent;
20856 it->descent = it->phys_descent = cmp->descent;
20857
20858 if (face->box != FACE_NO_BOX)
20859 {
20860 int thick = face->box_line_width;
20861
20862 if (thick > 0)
20863 {
20864 it->ascent += thick;
20865 it->descent += thick;
20866 }
20867 else
20868 thick = - thick;
20869
20870 if (it->start_of_box_run_p)
20871 it->pixel_width += thick;
20872 if (it->end_of_box_run_p)
20873 it->pixel_width += thick;
20874 }
20875
20876 /* If face has an overline, add the height of the overline
20877 (1 pixel) and a 1 pixel margin to the character height. */
20878 if (face->overline_p)
20879 it->ascent += overline_margin;
20880
20881 take_vertical_position_into_account (it);
20882
20883 if (it->glyph_row)
20884 append_composite_glyph (it);
20885 }
20886 else if (it->what == IT_IMAGE)
20887 produce_image_glyph (it);
20888 else if (it->what == IT_STRETCH)
20889 produce_stretch_glyph (it);
20890
20891 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20892 because this isn't true for images with `:ascent 100'. */
20893 xassert (it->ascent >= 0 && it->descent >= 0);
20894 if (it->area == TEXT_AREA)
20895 it->current_x += it->pixel_width;
20896
20897 if (extra_line_spacing > 0)
20898 {
20899 it->descent += extra_line_spacing;
20900 if (extra_line_spacing > it->max_extra_line_spacing)
20901 it->max_extra_line_spacing = extra_line_spacing;
20902 }
20903
20904 it->max_ascent = max (it->max_ascent, it->ascent);
20905 it->max_descent = max (it->max_descent, it->descent);
20906 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20907 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20908 }
20909
20910 /* EXPORT for RIF:
20911 Output LEN glyphs starting at START at the nominal cursor position.
20912 Advance the nominal cursor over the text. The global variable
20913 updated_window contains the window being updated, updated_row is
20914 the glyph row being updated, and updated_area is the area of that
20915 row being updated. */
20916
20917 void
20918 x_write_glyphs (start, len)
20919 struct glyph *start;
20920 int len;
20921 {
20922 int x, hpos;
20923
20924 xassert (updated_window && updated_row);
20925 BLOCK_INPUT;
20926
20927 /* Write glyphs. */
20928
20929 hpos = start - updated_row->glyphs[updated_area];
20930 x = draw_glyphs (updated_window, output_cursor.x,
20931 updated_row, updated_area,
20932 hpos, hpos + len,
20933 DRAW_NORMAL_TEXT, 0);
20934
20935 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20936 if (updated_area == TEXT_AREA
20937 && updated_window->phys_cursor_on_p
20938 && updated_window->phys_cursor.vpos == output_cursor.vpos
20939 && updated_window->phys_cursor.hpos >= hpos
20940 && updated_window->phys_cursor.hpos < hpos + len)
20941 updated_window->phys_cursor_on_p = 0;
20942
20943 UNBLOCK_INPUT;
20944
20945 /* Advance the output cursor. */
20946 output_cursor.hpos += len;
20947 output_cursor.x = x;
20948 }
20949
20950
20951 /* EXPORT for RIF:
20952 Insert LEN glyphs from START at the nominal cursor position. */
20953
20954 void
20955 x_insert_glyphs (start, len)
20956 struct glyph *start;
20957 int len;
20958 {
20959 struct frame *f;
20960 struct window *w;
20961 int line_height, shift_by_width, shifted_region_width;
20962 struct glyph_row *row;
20963 struct glyph *glyph;
20964 int frame_x, frame_y, hpos;
20965
20966 xassert (updated_window && updated_row);
20967 BLOCK_INPUT;
20968 w = updated_window;
20969 f = XFRAME (WINDOW_FRAME (w));
20970
20971 /* Get the height of the line we are in. */
20972 row = updated_row;
20973 line_height = row->height;
20974
20975 /* Get the width of the glyphs to insert. */
20976 shift_by_width = 0;
20977 for (glyph = start; glyph < start + len; ++glyph)
20978 shift_by_width += glyph->pixel_width;
20979
20980 /* Get the width of the region to shift right. */
20981 shifted_region_width = (window_box_width (w, updated_area)
20982 - output_cursor.x
20983 - shift_by_width);
20984
20985 /* Shift right. */
20986 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20987 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20988
20989 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20990 line_height, shift_by_width);
20991
20992 /* Write the glyphs. */
20993 hpos = start - row->glyphs[updated_area];
20994 draw_glyphs (w, output_cursor.x, row, updated_area,
20995 hpos, hpos + len,
20996 DRAW_NORMAL_TEXT, 0);
20997
20998 /* Advance the output cursor. */
20999 output_cursor.hpos += len;
21000 output_cursor.x += shift_by_width;
21001 UNBLOCK_INPUT;
21002 }
21003
21004
21005 /* EXPORT for RIF:
21006 Erase the current text line from the nominal cursor position
21007 (inclusive) to pixel column TO_X (exclusive). The idea is that
21008 everything from TO_X onward is already erased.
21009
21010 TO_X is a pixel position relative to updated_area of
21011 updated_window. TO_X == -1 means clear to the end of this area. */
21012
21013 void
21014 x_clear_end_of_line (to_x)
21015 int to_x;
21016 {
21017 struct frame *f;
21018 struct window *w = updated_window;
21019 int max_x, min_y, max_y;
21020 int from_x, from_y, to_y;
21021
21022 xassert (updated_window && updated_row);
21023 f = XFRAME (w->frame);
21024
21025 if (updated_row->full_width_p)
21026 max_x = WINDOW_TOTAL_WIDTH (w);
21027 else
21028 max_x = window_box_width (w, updated_area);
21029 max_y = window_text_bottom_y (w);
21030
21031 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
21032 of window. For TO_X > 0, truncate to end of drawing area. */
21033 if (to_x == 0)
21034 return;
21035 else if (to_x < 0)
21036 to_x = max_x;
21037 else
21038 to_x = min (to_x, max_x);
21039
21040 to_y = min (max_y, output_cursor.y + updated_row->height);
21041
21042 /* Notice if the cursor will be cleared by this operation. */
21043 if (!updated_row->full_width_p)
21044 notice_overwritten_cursor (w, updated_area,
21045 output_cursor.x, -1,
21046 updated_row->y,
21047 MATRIX_ROW_BOTTOM_Y (updated_row));
21048
21049 from_x = output_cursor.x;
21050
21051 /* Translate to frame coordinates. */
21052 if (updated_row->full_width_p)
21053 {
21054 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21055 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21056 }
21057 else
21058 {
21059 int area_left = window_box_left (w, updated_area);
21060 from_x += area_left;
21061 to_x += area_left;
21062 }
21063
21064 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21065 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21066 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21067
21068 /* Prevent inadvertently clearing to end of the X window. */
21069 if (to_x > from_x && to_y > from_y)
21070 {
21071 BLOCK_INPUT;
21072 rif->clear_frame_area (f, from_x, from_y,
21073 to_x - from_x, to_y - from_y);
21074 UNBLOCK_INPUT;
21075 }
21076 }
21077
21078 #endif /* HAVE_WINDOW_SYSTEM */
21079
21080
21081 \f
21082 /***********************************************************************
21083 Cursor types
21084 ***********************************************************************/
21085
21086 /* Value is the internal representation of the specified cursor type
21087 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21088 of the bar cursor. */
21089
21090 static enum text_cursor_kinds
21091 get_specified_cursor_type (arg, width)
21092 Lisp_Object arg;
21093 int *width;
21094 {
21095 enum text_cursor_kinds type;
21096
21097 if (NILP (arg))
21098 return NO_CURSOR;
21099
21100 if (EQ (arg, Qbox))
21101 return FILLED_BOX_CURSOR;
21102
21103 if (EQ (arg, Qhollow))
21104 return HOLLOW_BOX_CURSOR;
21105
21106 if (EQ (arg, Qbar))
21107 {
21108 *width = 2;
21109 return BAR_CURSOR;
21110 }
21111
21112 if (CONSP (arg)
21113 && EQ (XCAR (arg), Qbar)
21114 && INTEGERP (XCDR (arg))
21115 && XINT (XCDR (arg)) >= 0)
21116 {
21117 *width = XINT (XCDR (arg));
21118 return BAR_CURSOR;
21119 }
21120
21121 if (EQ (arg, Qhbar))
21122 {
21123 *width = 2;
21124 return HBAR_CURSOR;
21125 }
21126
21127 if (CONSP (arg)
21128 && EQ (XCAR (arg), Qhbar)
21129 && INTEGERP (XCDR (arg))
21130 && XINT (XCDR (arg)) >= 0)
21131 {
21132 *width = XINT (XCDR (arg));
21133 return HBAR_CURSOR;
21134 }
21135
21136 /* Treat anything unknown as "hollow box cursor".
21137 It was bad to signal an error; people have trouble fixing
21138 .Xdefaults with Emacs, when it has something bad in it. */
21139 type = HOLLOW_BOX_CURSOR;
21140
21141 return type;
21142 }
21143
21144 /* Set the default cursor types for specified frame. */
21145 void
21146 set_frame_cursor_types (f, arg)
21147 struct frame *f;
21148 Lisp_Object arg;
21149 {
21150 int width;
21151 Lisp_Object tem;
21152
21153 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21154 FRAME_CURSOR_WIDTH (f) = width;
21155
21156 /* By default, set up the blink-off state depending on the on-state. */
21157
21158 tem = Fassoc (arg, Vblink_cursor_alist);
21159 if (!NILP (tem))
21160 {
21161 FRAME_BLINK_OFF_CURSOR (f)
21162 = get_specified_cursor_type (XCDR (tem), &width);
21163 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21164 }
21165 else
21166 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21167 }
21168
21169
21170 /* Return the cursor we want to be displayed in window W. Return
21171 width of bar/hbar cursor through WIDTH arg. Return with
21172 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21173 (i.e. if the `system caret' should track this cursor).
21174
21175 In a mini-buffer window, we want the cursor only to appear if we
21176 are reading input from this window. For the selected window, we
21177 want the cursor type given by the frame parameter or buffer local
21178 setting of cursor-type. If explicitly marked off, draw no cursor.
21179 In all other cases, we want a hollow box cursor. */
21180
21181 static enum text_cursor_kinds
21182 get_window_cursor_type (w, glyph, width, active_cursor)
21183 struct window *w;
21184 struct glyph *glyph;
21185 int *width;
21186 int *active_cursor;
21187 {
21188 struct frame *f = XFRAME (w->frame);
21189 struct buffer *b = XBUFFER (w->buffer);
21190 int cursor_type = DEFAULT_CURSOR;
21191 Lisp_Object alt_cursor;
21192 int non_selected = 0;
21193
21194 *active_cursor = 1;
21195
21196 /* Echo area */
21197 if (cursor_in_echo_area
21198 && FRAME_HAS_MINIBUF_P (f)
21199 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21200 {
21201 if (w == XWINDOW (echo_area_window))
21202 {
21203 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21204 {
21205 *width = FRAME_CURSOR_WIDTH (f);
21206 return FRAME_DESIRED_CURSOR (f);
21207 }
21208 else
21209 return get_specified_cursor_type (b->cursor_type, width);
21210 }
21211
21212 *active_cursor = 0;
21213 non_selected = 1;
21214 }
21215
21216 /* Nonselected window or nonselected frame. */
21217 else if (w != XWINDOW (f->selected_window)
21218 #ifdef HAVE_WINDOW_SYSTEM
21219 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21220 #endif
21221 )
21222 {
21223 *active_cursor = 0;
21224
21225 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21226 return NO_CURSOR;
21227
21228 non_selected = 1;
21229 }
21230
21231 /* Never display a cursor in a window in which cursor-type is nil. */
21232 if (NILP (b->cursor_type))
21233 return NO_CURSOR;
21234
21235 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21236 if (non_selected)
21237 {
21238 alt_cursor = b->cursor_in_non_selected_windows;
21239 return get_specified_cursor_type (alt_cursor, width);
21240 }
21241
21242 /* Get the normal cursor type for this window. */
21243 if (EQ (b->cursor_type, Qt))
21244 {
21245 cursor_type = FRAME_DESIRED_CURSOR (f);
21246 *width = FRAME_CURSOR_WIDTH (f);
21247 }
21248 else
21249 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21250
21251 /* Use normal cursor if not blinked off. */
21252 if (!w->cursor_off_p)
21253 {
21254 #ifdef HAVE_WINDOW_SYSTEM
21255 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
21256 {
21257 if (cursor_type == FILLED_BOX_CURSOR)
21258 {
21259 /* Using a block cursor on large images can be very annoying.
21260 So use a hollow cursor for "large" images.
21261 If image is not transparent (no mask), also use hollow cursor. */
21262 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
21263 if (img != NULL && IMAGEP (img->spec))
21264 {
21265 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
21266 where N = size of default frame font size.
21267 This should cover most of the "tiny" icons people may use. */
21268 if (!img->mask
21269 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
21270 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
21271 cursor_type = HOLLOW_BOX_CURSOR;
21272 }
21273 }
21274 else if (cursor_type != NO_CURSOR)
21275 {
21276 /* Display current only supports BOX and HOLLOW cursors for images.
21277 So for now, unconditionally use a HOLLOW cursor when cursor is
21278 not a solid box cursor. */
21279 cursor_type = HOLLOW_BOX_CURSOR;
21280 }
21281 }
21282 #endif
21283 return cursor_type;
21284 }
21285
21286 /* Cursor is blinked off, so determine how to "toggle" it. */
21287
21288 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21289 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21290 return get_specified_cursor_type (XCDR (alt_cursor), width);
21291
21292 /* Then see if frame has specified a specific blink off cursor type. */
21293 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21294 {
21295 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21296 return FRAME_BLINK_OFF_CURSOR (f);
21297 }
21298
21299 #if 0
21300 /* Some people liked having a permanently visible blinking cursor,
21301 while others had very strong opinions against it. So it was
21302 decided to remove it. KFS 2003-09-03 */
21303
21304 /* Finally perform built-in cursor blinking:
21305 filled box <-> hollow box
21306 wide [h]bar <-> narrow [h]bar
21307 narrow [h]bar <-> no cursor
21308 other type <-> no cursor */
21309
21310 if (cursor_type == FILLED_BOX_CURSOR)
21311 return HOLLOW_BOX_CURSOR;
21312
21313 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21314 {
21315 *width = 1;
21316 return cursor_type;
21317 }
21318 #endif
21319
21320 return NO_CURSOR;
21321 }
21322
21323
21324 #ifdef HAVE_WINDOW_SYSTEM
21325
21326 /* Notice when the text cursor of window W has been completely
21327 overwritten by a drawing operation that outputs glyphs in AREA
21328 starting at X0 and ending at X1 in the line starting at Y0 and
21329 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21330 the rest of the line after X0 has been written. Y coordinates
21331 are window-relative. */
21332
21333 static void
21334 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21335 struct window *w;
21336 enum glyph_row_area area;
21337 int x0, y0, x1, y1;
21338 {
21339 int cx0, cx1, cy0, cy1;
21340 struct glyph_row *row;
21341
21342 if (!w->phys_cursor_on_p)
21343 return;
21344 if (area != TEXT_AREA)
21345 return;
21346
21347 if (w->phys_cursor.vpos < 0
21348 || w->phys_cursor.vpos >= w->current_matrix->nrows
21349 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21350 !(row->enabled_p && row->displays_text_p)))
21351 return;
21352
21353 if (row->cursor_in_fringe_p)
21354 {
21355 row->cursor_in_fringe_p = 0;
21356 draw_fringe_bitmap (w, row, 0);
21357 w->phys_cursor_on_p = 0;
21358 return;
21359 }
21360
21361 cx0 = w->phys_cursor.x;
21362 cx1 = cx0 + w->phys_cursor_width;
21363 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21364 return;
21365
21366 /* The cursor image will be completely removed from the
21367 screen if the output area intersects the cursor area in
21368 y-direction. When we draw in [y0 y1[, and some part of
21369 the cursor is at y < y0, that part must have been drawn
21370 before. When scrolling, the cursor is erased before
21371 actually scrolling, so we don't come here. When not
21372 scrolling, the rows above the old cursor row must have
21373 changed, and in this case these rows must have written
21374 over the cursor image.
21375
21376 Likewise if part of the cursor is below y1, with the
21377 exception of the cursor being in the first blank row at
21378 the buffer and window end because update_text_area
21379 doesn't draw that row. (Except when it does, but
21380 that's handled in update_text_area.) */
21381
21382 cy0 = w->phys_cursor.y;
21383 cy1 = cy0 + w->phys_cursor_height;
21384 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21385 return;
21386
21387 w->phys_cursor_on_p = 0;
21388 }
21389
21390 #endif /* HAVE_WINDOW_SYSTEM */
21391
21392 \f
21393 /************************************************************************
21394 Mouse Face
21395 ************************************************************************/
21396
21397 #ifdef HAVE_WINDOW_SYSTEM
21398
21399 /* EXPORT for RIF:
21400 Fix the display of area AREA of overlapping row ROW in window W
21401 with respect to the overlapping part OVERLAPS. */
21402
21403 void
21404 x_fix_overlapping_area (w, row, area, overlaps)
21405 struct window *w;
21406 struct glyph_row *row;
21407 enum glyph_row_area area;
21408 int overlaps;
21409 {
21410 int i, x;
21411
21412 BLOCK_INPUT;
21413
21414 x = 0;
21415 for (i = 0; i < row->used[area];)
21416 {
21417 if (row->glyphs[area][i].overlaps_vertically_p)
21418 {
21419 int start = i, start_x = x;
21420
21421 do
21422 {
21423 x += row->glyphs[area][i].pixel_width;
21424 ++i;
21425 }
21426 while (i < row->used[area]
21427 && row->glyphs[area][i].overlaps_vertically_p);
21428
21429 draw_glyphs (w, start_x, row, area,
21430 start, i,
21431 DRAW_NORMAL_TEXT, overlaps);
21432 }
21433 else
21434 {
21435 x += row->glyphs[area][i].pixel_width;
21436 ++i;
21437 }
21438 }
21439
21440 UNBLOCK_INPUT;
21441 }
21442
21443
21444 /* EXPORT:
21445 Draw the cursor glyph of window W in glyph row ROW. See the
21446 comment of draw_glyphs for the meaning of HL. */
21447
21448 void
21449 draw_phys_cursor_glyph (w, row, hl)
21450 struct window *w;
21451 struct glyph_row *row;
21452 enum draw_glyphs_face hl;
21453 {
21454 /* If cursor hpos is out of bounds, don't draw garbage. This can
21455 happen in mini-buffer windows when switching between echo area
21456 glyphs and mini-buffer. */
21457 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21458 {
21459 int on_p = w->phys_cursor_on_p;
21460 int x1;
21461 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21462 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21463 hl, 0);
21464 w->phys_cursor_on_p = on_p;
21465
21466 if (hl == DRAW_CURSOR)
21467 w->phys_cursor_width = x1 - w->phys_cursor.x;
21468 /* When we erase the cursor, and ROW is overlapped by other
21469 rows, make sure that these overlapping parts of other rows
21470 are redrawn. */
21471 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21472 {
21473 w->phys_cursor_width = x1 - w->phys_cursor.x;
21474
21475 if (row > w->current_matrix->rows
21476 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21477 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21478 OVERLAPS_ERASED_CURSOR);
21479
21480 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21481 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21482 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21483 OVERLAPS_ERASED_CURSOR);
21484 }
21485 }
21486 }
21487
21488
21489 /* EXPORT:
21490 Erase the image of a cursor of window W from the screen. */
21491
21492 void
21493 erase_phys_cursor (w)
21494 struct window *w;
21495 {
21496 struct frame *f = XFRAME (w->frame);
21497 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21498 int hpos = w->phys_cursor.hpos;
21499 int vpos = w->phys_cursor.vpos;
21500 int mouse_face_here_p = 0;
21501 struct glyph_matrix *active_glyphs = w->current_matrix;
21502 struct glyph_row *cursor_row;
21503 struct glyph *cursor_glyph;
21504 enum draw_glyphs_face hl;
21505
21506 /* No cursor displayed or row invalidated => nothing to do on the
21507 screen. */
21508 if (w->phys_cursor_type == NO_CURSOR)
21509 goto mark_cursor_off;
21510
21511 /* VPOS >= active_glyphs->nrows means that window has been resized.
21512 Don't bother to erase the cursor. */
21513 if (vpos >= active_glyphs->nrows)
21514 goto mark_cursor_off;
21515
21516 /* If row containing cursor is marked invalid, there is nothing we
21517 can do. */
21518 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21519 if (!cursor_row->enabled_p)
21520 goto mark_cursor_off;
21521
21522 /* If line spacing is > 0, old cursor may only be partially visible in
21523 window after split-window. So adjust visible height. */
21524 cursor_row->visible_height = min (cursor_row->visible_height,
21525 window_text_bottom_y (w) - cursor_row->y);
21526
21527 /* If row is completely invisible, don't attempt to delete a cursor which
21528 isn't there. This can happen if cursor is at top of a window, and
21529 we switch to a buffer with a header line in that window. */
21530 if (cursor_row->visible_height <= 0)
21531 goto mark_cursor_off;
21532
21533 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21534 if (cursor_row->cursor_in_fringe_p)
21535 {
21536 cursor_row->cursor_in_fringe_p = 0;
21537 draw_fringe_bitmap (w, cursor_row, 0);
21538 goto mark_cursor_off;
21539 }
21540
21541 /* This can happen when the new row is shorter than the old one.
21542 In this case, either draw_glyphs or clear_end_of_line
21543 should have cleared the cursor. Note that we wouldn't be
21544 able to erase the cursor in this case because we don't have a
21545 cursor glyph at hand. */
21546 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21547 goto mark_cursor_off;
21548
21549 /* If the cursor is in the mouse face area, redisplay that when
21550 we clear the cursor. */
21551 if (! NILP (dpyinfo->mouse_face_window)
21552 && w == XWINDOW (dpyinfo->mouse_face_window)
21553 && (vpos > dpyinfo->mouse_face_beg_row
21554 || (vpos == dpyinfo->mouse_face_beg_row
21555 && hpos >= dpyinfo->mouse_face_beg_col))
21556 && (vpos < dpyinfo->mouse_face_end_row
21557 || (vpos == dpyinfo->mouse_face_end_row
21558 && hpos < dpyinfo->mouse_face_end_col))
21559 /* Don't redraw the cursor's spot in mouse face if it is at the
21560 end of a line (on a newline). The cursor appears there, but
21561 mouse highlighting does not. */
21562 && cursor_row->used[TEXT_AREA] > hpos)
21563 mouse_face_here_p = 1;
21564
21565 /* Maybe clear the display under the cursor. */
21566 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21567 {
21568 int x, y, left_x;
21569 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21570 int width;
21571
21572 cursor_glyph = get_phys_cursor_glyph (w);
21573 if (cursor_glyph == NULL)
21574 goto mark_cursor_off;
21575
21576 width = cursor_glyph->pixel_width;
21577 left_x = window_box_left_offset (w, TEXT_AREA);
21578 x = w->phys_cursor.x;
21579 if (x < left_x)
21580 width -= left_x - x;
21581 width = min (width, window_box_width (w, TEXT_AREA) - x);
21582 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21583 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21584
21585 if (width > 0)
21586 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21587 }
21588
21589 /* Erase the cursor by redrawing the character underneath it. */
21590 if (mouse_face_here_p)
21591 hl = DRAW_MOUSE_FACE;
21592 else
21593 hl = DRAW_NORMAL_TEXT;
21594 draw_phys_cursor_glyph (w, cursor_row, hl);
21595
21596 mark_cursor_off:
21597 w->phys_cursor_on_p = 0;
21598 w->phys_cursor_type = NO_CURSOR;
21599 }
21600
21601
21602 /* EXPORT:
21603 Display or clear cursor of window W. If ON is zero, clear the
21604 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21605 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21606
21607 void
21608 display_and_set_cursor (w, on, hpos, vpos, x, y)
21609 struct window *w;
21610 int on, hpos, vpos, x, y;
21611 {
21612 struct frame *f = XFRAME (w->frame);
21613 int new_cursor_type;
21614 int new_cursor_width;
21615 int active_cursor;
21616 struct glyph_row *glyph_row;
21617 struct glyph *glyph;
21618
21619 /* This is pointless on invisible frames, and dangerous on garbaged
21620 windows and frames; in the latter case, the frame or window may
21621 be in the midst of changing its size, and x and y may be off the
21622 window. */
21623 if (! FRAME_VISIBLE_P (f)
21624 || FRAME_GARBAGED_P (f)
21625 || vpos >= w->current_matrix->nrows
21626 || hpos >= w->current_matrix->matrix_w)
21627 return;
21628
21629 /* If cursor is off and we want it off, return quickly. */
21630 if (!on && !w->phys_cursor_on_p)
21631 return;
21632
21633 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21634 /* If cursor row is not enabled, we don't really know where to
21635 display the cursor. */
21636 if (!glyph_row->enabled_p)
21637 {
21638 w->phys_cursor_on_p = 0;
21639 return;
21640 }
21641
21642 glyph = NULL;
21643 if (!glyph_row->exact_window_width_line_p
21644 || hpos < glyph_row->used[TEXT_AREA])
21645 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21646
21647 xassert (interrupt_input_blocked);
21648
21649 /* Set new_cursor_type to the cursor we want to be displayed. */
21650 new_cursor_type = get_window_cursor_type (w, glyph,
21651 &new_cursor_width, &active_cursor);
21652
21653 /* If cursor is currently being shown and we don't want it to be or
21654 it is in the wrong place, or the cursor type is not what we want,
21655 erase it. */
21656 if (w->phys_cursor_on_p
21657 && (!on
21658 || w->phys_cursor.x != x
21659 || w->phys_cursor.y != y
21660 || new_cursor_type != w->phys_cursor_type
21661 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21662 && new_cursor_width != w->phys_cursor_width)))
21663 erase_phys_cursor (w);
21664
21665 /* Don't check phys_cursor_on_p here because that flag is only set
21666 to zero in some cases where we know that the cursor has been
21667 completely erased, to avoid the extra work of erasing the cursor
21668 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21669 still not be visible, or it has only been partly erased. */
21670 if (on)
21671 {
21672 w->phys_cursor_ascent = glyph_row->ascent;
21673 w->phys_cursor_height = glyph_row->height;
21674
21675 /* Set phys_cursor_.* before x_draw_.* is called because some
21676 of them may need the information. */
21677 w->phys_cursor.x = x;
21678 w->phys_cursor.y = glyph_row->y;
21679 w->phys_cursor.hpos = hpos;
21680 w->phys_cursor.vpos = vpos;
21681 }
21682
21683 rif->draw_window_cursor (w, glyph_row, x, y,
21684 new_cursor_type, new_cursor_width,
21685 on, active_cursor);
21686 }
21687
21688
21689 /* Switch the display of W's cursor on or off, according to the value
21690 of ON. */
21691
21692 static void
21693 update_window_cursor (w, on)
21694 struct window *w;
21695 int on;
21696 {
21697 /* Don't update cursor in windows whose frame is in the process
21698 of being deleted. */
21699 if (w->current_matrix)
21700 {
21701 BLOCK_INPUT;
21702 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21703 w->phys_cursor.x, w->phys_cursor.y);
21704 UNBLOCK_INPUT;
21705 }
21706 }
21707
21708
21709 /* Call update_window_cursor with parameter ON_P on all leaf windows
21710 in the window tree rooted at W. */
21711
21712 static void
21713 update_cursor_in_window_tree (w, on_p)
21714 struct window *w;
21715 int on_p;
21716 {
21717 while (w)
21718 {
21719 if (!NILP (w->hchild))
21720 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21721 else if (!NILP (w->vchild))
21722 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21723 else
21724 update_window_cursor (w, on_p);
21725
21726 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21727 }
21728 }
21729
21730
21731 /* EXPORT:
21732 Display the cursor on window W, or clear it, according to ON_P.
21733 Don't change the cursor's position. */
21734
21735 void
21736 x_update_cursor (f, on_p)
21737 struct frame *f;
21738 int on_p;
21739 {
21740 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21741 }
21742
21743
21744 /* EXPORT:
21745 Clear the cursor of window W to background color, and mark the
21746 cursor as not shown. This is used when the text where the cursor
21747 is is about to be rewritten. */
21748
21749 void
21750 x_clear_cursor (w)
21751 struct window *w;
21752 {
21753 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21754 update_window_cursor (w, 0);
21755 }
21756
21757
21758 /* EXPORT:
21759 Display the active region described by mouse_face_* according to DRAW. */
21760
21761 void
21762 show_mouse_face (dpyinfo, draw)
21763 Display_Info *dpyinfo;
21764 enum draw_glyphs_face draw;
21765 {
21766 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21767 struct frame *f = XFRAME (WINDOW_FRAME (w));
21768
21769 if (/* If window is in the process of being destroyed, don't bother
21770 to do anything. */
21771 w->current_matrix != NULL
21772 /* Don't update mouse highlight if hidden */
21773 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21774 /* Recognize when we are called to operate on rows that don't exist
21775 anymore. This can happen when a window is split. */
21776 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21777 {
21778 int phys_cursor_on_p = w->phys_cursor_on_p;
21779 struct glyph_row *row, *first, *last;
21780
21781 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21782 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21783
21784 for (row = first; row <= last && row->enabled_p; ++row)
21785 {
21786 int start_hpos, end_hpos, start_x;
21787
21788 /* For all but the first row, the highlight starts at column 0. */
21789 if (row == first)
21790 {
21791 start_hpos = dpyinfo->mouse_face_beg_col;
21792 start_x = dpyinfo->mouse_face_beg_x;
21793 }
21794 else
21795 {
21796 start_hpos = 0;
21797 start_x = 0;
21798 }
21799
21800 if (row == last)
21801 end_hpos = dpyinfo->mouse_face_end_col;
21802 else
21803 {
21804 end_hpos = row->used[TEXT_AREA];
21805 if (draw == DRAW_NORMAL_TEXT)
21806 row->fill_line_p = 1; /* Clear to end of line */
21807 }
21808
21809 if (end_hpos > start_hpos)
21810 {
21811 draw_glyphs (w, start_x, row, TEXT_AREA,
21812 start_hpos, end_hpos,
21813 draw, 0);
21814
21815 row->mouse_face_p
21816 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21817 }
21818 }
21819
21820 /* When we've written over the cursor, arrange for it to
21821 be displayed again. */
21822 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21823 {
21824 BLOCK_INPUT;
21825 display_and_set_cursor (w, 1,
21826 w->phys_cursor.hpos, w->phys_cursor.vpos,
21827 w->phys_cursor.x, w->phys_cursor.y);
21828 UNBLOCK_INPUT;
21829 }
21830 }
21831
21832 /* Change the mouse cursor. */
21833 if (draw == DRAW_NORMAL_TEXT)
21834 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21835 else if (draw == DRAW_MOUSE_FACE)
21836 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21837 else
21838 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21839 }
21840
21841 /* EXPORT:
21842 Clear out the mouse-highlighted active region.
21843 Redraw it un-highlighted first. Value is non-zero if mouse
21844 face was actually drawn unhighlighted. */
21845
21846 int
21847 clear_mouse_face (dpyinfo)
21848 Display_Info *dpyinfo;
21849 {
21850 int cleared = 0;
21851
21852 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21853 {
21854 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21855 cleared = 1;
21856 }
21857
21858 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21859 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21860 dpyinfo->mouse_face_window = Qnil;
21861 dpyinfo->mouse_face_overlay = Qnil;
21862 return cleared;
21863 }
21864
21865
21866 /* EXPORT:
21867 Non-zero if physical cursor of window W is within mouse face. */
21868
21869 int
21870 cursor_in_mouse_face_p (w)
21871 struct window *w;
21872 {
21873 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21874 int in_mouse_face = 0;
21875
21876 if (WINDOWP (dpyinfo->mouse_face_window)
21877 && XWINDOW (dpyinfo->mouse_face_window) == w)
21878 {
21879 int hpos = w->phys_cursor.hpos;
21880 int vpos = w->phys_cursor.vpos;
21881
21882 if (vpos >= dpyinfo->mouse_face_beg_row
21883 && vpos <= dpyinfo->mouse_face_end_row
21884 && (vpos > dpyinfo->mouse_face_beg_row
21885 || hpos >= dpyinfo->mouse_face_beg_col)
21886 && (vpos < dpyinfo->mouse_face_end_row
21887 || hpos < dpyinfo->mouse_face_end_col
21888 || dpyinfo->mouse_face_past_end))
21889 in_mouse_face = 1;
21890 }
21891
21892 return in_mouse_face;
21893 }
21894
21895
21896
21897 \f
21898 /* Find the glyph matrix position of buffer position CHARPOS in window
21899 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21900 current glyphs must be up to date. If CHARPOS is above window
21901 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21902 of last line in W. In the row containing CHARPOS, stop before glyphs
21903 having STOP as object. */
21904
21905 #if 1 /* This is a version of fast_find_position that's more correct
21906 in the presence of hscrolling, for example. I didn't install
21907 it right away because the problem fixed is minor, it failed
21908 in 20.x as well, and I think it's too risky to install
21909 so near the release of 21.1. 2001-09-25 gerd. */
21910
21911 static int
21912 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21913 struct window *w;
21914 int charpos;
21915 int *hpos, *vpos, *x, *y;
21916 Lisp_Object stop;
21917 {
21918 struct glyph_row *row, *first;
21919 struct glyph *glyph, *end;
21920 int past_end = 0;
21921
21922 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21923 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21924 {
21925 *x = first->x;
21926 *y = first->y;
21927 *hpos = 0;
21928 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21929 return 1;
21930 }
21931
21932 row = row_containing_pos (w, charpos, first, NULL, 0);
21933 if (row == NULL)
21934 {
21935 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21936 past_end = 1;
21937 }
21938
21939 /* If whole rows or last part of a row came from a display overlay,
21940 row_containing_pos will skip over such rows because their end pos
21941 equals the start pos of the overlay or interval.
21942
21943 Move back if we have a STOP object and previous row's
21944 end glyph came from STOP. */
21945 if (!NILP (stop))
21946 {
21947 struct glyph_row *prev;
21948 while ((prev = row - 1, prev >= first)
21949 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21950 && prev->used[TEXT_AREA] > 0)
21951 {
21952 struct glyph *beg = prev->glyphs[TEXT_AREA];
21953 glyph = beg + prev->used[TEXT_AREA];
21954 while (--glyph >= beg
21955 && INTEGERP (glyph->object));
21956 if (glyph < beg
21957 || !EQ (stop, glyph->object))
21958 break;
21959 row = prev;
21960 }
21961 }
21962
21963 *x = row->x;
21964 *y = row->y;
21965 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21966
21967 glyph = row->glyphs[TEXT_AREA];
21968 end = glyph + row->used[TEXT_AREA];
21969
21970 /* Skip over glyphs not having an object at the start of the row.
21971 These are special glyphs like truncation marks on terminal
21972 frames. */
21973 if (row->displays_text_p)
21974 while (glyph < end
21975 && INTEGERP (glyph->object)
21976 && !EQ (stop, glyph->object)
21977 && glyph->charpos < 0)
21978 {
21979 *x += glyph->pixel_width;
21980 ++glyph;
21981 }
21982
21983 while (glyph < end
21984 && !INTEGERP (glyph->object)
21985 && !EQ (stop, glyph->object)
21986 && (!BUFFERP (glyph->object)
21987 || glyph->charpos < charpos))
21988 {
21989 *x += glyph->pixel_width;
21990 ++glyph;
21991 }
21992
21993 *hpos = glyph - row->glyphs[TEXT_AREA];
21994 return !past_end;
21995 }
21996
21997 #else /* not 1 */
21998
21999 static int
22000 fast_find_position (w, pos, hpos, vpos, x, y, stop)
22001 struct window *w;
22002 int pos;
22003 int *hpos, *vpos, *x, *y;
22004 Lisp_Object stop;
22005 {
22006 int i;
22007 int lastcol;
22008 int maybe_next_line_p = 0;
22009 int line_start_position;
22010 int yb = window_text_bottom_y (w);
22011 struct glyph_row *row, *best_row;
22012 int row_vpos, best_row_vpos;
22013 int current_x;
22014
22015 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22016 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
22017
22018 while (row->y < yb)
22019 {
22020 if (row->used[TEXT_AREA])
22021 line_start_position = row->glyphs[TEXT_AREA]->charpos;
22022 else
22023 line_start_position = 0;
22024
22025 if (line_start_position > pos)
22026 break;
22027 /* If the position sought is the end of the buffer,
22028 don't include the blank lines at the bottom of the window. */
22029 else if (line_start_position == pos
22030 && pos == BUF_ZV (XBUFFER (w->buffer)))
22031 {
22032 maybe_next_line_p = 1;
22033 break;
22034 }
22035 else if (line_start_position > 0)
22036 {
22037 best_row = row;
22038 best_row_vpos = row_vpos;
22039 }
22040
22041 if (row->y + row->height >= yb)
22042 break;
22043
22044 ++row;
22045 ++row_vpos;
22046 }
22047
22048 /* Find the right column within BEST_ROW. */
22049 lastcol = 0;
22050 current_x = best_row->x;
22051 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
22052 {
22053 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
22054 int charpos = glyph->charpos;
22055
22056 if (BUFFERP (glyph->object))
22057 {
22058 if (charpos == pos)
22059 {
22060 *hpos = i;
22061 *vpos = best_row_vpos;
22062 *x = current_x;
22063 *y = best_row->y;
22064 return 1;
22065 }
22066 else if (charpos > pos)
22067 break;
22068 }
22069 else if (EQ (glyph->object, stop))
22070 break;
22071
22072 if (charpos > 0)
22073 lastcol = i;
22074 current_x += glyph->pixel_width;
22075 }
22076
22077 /* If we're looking for the end of the buffer,
22078 and we didn't find it in the line we scanned,
22079 use the start of the following line. */
22080 if (maybe_next_line_p)
22081 {
22082 ++best_row;
22083 ++best_row_vpos;
22084 lastcol = 0;
22085 current_x = best_row->x;
22086 }
22087
22088 *vpos = best_row_vpos;
22089 *hpos = lastcol + 1;
22090 *x = current_x;
22091 *y = best_row->y;
22092 return 0;
22093 }
22094
22095 #endif /* not 1 */
22096
22097
22098 /* Find the position of the glyph for position POS in OBJECT in
22099 window W's current matrix, and return in *X, *Y the pixel
22100 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22101
22102 RIGHT_P non-zero means return the position of the right edge of the
22103 glyph, RIGHT_P zero means return the left edge position.
22104
22105 If no glyph for POS exists in the matrix, return the position of
22106 the glyph with the next smaller position that is in the matrix, if
22107 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22108 exists in the matrix, return the position of the glyph with the
22109 next larger position in OBJECT.
22110
22111 Value is non-zero if a glyph was found. */
22112
22113 static int
22114 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22115 struct window *w;
22116 int pos;
22117 Lisp_Object object;
22118 int *hpos, *vpos, *x, *y;
22119 int right_p;
22120 {
22121 int yb = window_text_bottom_y (w);
22122 struct glyph_row *r;
22123 struct glyph *best_glyph = NULL;
22124 struct glyph_row *best_row = NULL;
22125 int best_x = 0;
22126
22127 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22128 r->enabled_p && r->y < yb;
22129 ++r)
22130 {
22131 struct glyph *g = r->glyphs[TEXT_AREA];
22132 struct glyph *e = g + r->used[TEXT_AREA];
22133 int gx;
22134
22135 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22136 if (EQ (g->object, object))
22137 {
22138 if (g->charpos == pos)
22139 {
22140 best_glyph = g;
22141 best_x = gx;
22142 best_row = r;
22143 goto found;
22144 }
22145 else if (best_glyph == NULL
22146 || ((abs (g->charpos - pos)
22147 < abs (best_glyph->charpos - pos))
22148 && (right_p
22149 ? g->charpos < pos
22150 : g->charpos > pos)))
22151 {
22152 best_glyph = g;
22153 best_x = gx;
22154 best_row = r;
22155 }
22156 }
22157 }
22158
22159 found:
22160
22161 if (best_glyph)
22162 {
22163 *x = best_x;
22164 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22165
22166 if (right_p)
22167 {
22168 *x += best_glyph->pixel_width;
22169 ++*hpos;
22170 }
22171
22172 *y = best_row->y;
22173 *vpos = best_row - w->current_matrix->rows;
22174 }
22175
22176 return best_glyph != NULL;
22177 }
22178
22179
22180 /* See if position X, Y is within a hot-spot of an image. */
22181
22182 static int
22183 on_hot_spot_p (hot_spot, x, y)
22184 Lisp_Object hot_spot;
22185 int x, y;
22186 {
22187 if (!CONSP (hot_spot))
22188 return 0;
22189
22190 if (EQ (XCAR (hot_spot), Qrect))
22191 {
22192 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22193 Lisp_Object rect = XCDR (hot_spot);
22194 Lisp_Object tem;
22195 if (!CONSP (rect))
22196 return 0;
22197 if (!CONSP (XCAR (rect)))
22198 return 0;
22199 if (!CONSP (XCDR (rect)))
22200 return 0;
22201 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22202 return 0;
22203 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22204 return 0;
22205 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22206 return 0;
22207 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22208 return 0;
22209 return 1;
22210 }
22211 else if (EQ (XCAR (hot_spot), Qcircle))
22212 {
22213 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22214 Lisp_Object circ = XCDR (hot_spot);
22215 Lisp_Object lr, lx0, ly0;
22216 if (CONSP (circ)
22217 && CONSP (XCAR (circ))
22218 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22219 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22220 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22221 {
22222 double r = XFLOATINT (lr);
22223 double dx = XINT (lx0) - x;
22224 double dy = XINT (ly0) - y;
22225 return (dx * dx + dy * dy <= r * r);
22226 }
22227 }
22228 else if (EQ (XCAR (hot_spot), Qpoly))
22229 {
22230 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22231 if (VECTORP (XCDR (hot_spot)))
22232 {
22233 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22234 Lisp_Object *poly = v->contents;
22235 int n = v->size;
22236 int i;
22237 int inside = 0;
22238 Lisp_Object lx, ly;
22239 int x0, y0;
22240
22241 /* Need an even number of coordinates, and at least 3 edges. */
22242 if (n < 6 || n & 1)
22243 return 0;
22244
22245 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22246 If count is odd, we are inside polygon. Pixels on edges
22247 may or may not be included depending on actual geometry of the
22248 polygon. */
22249 if ((lx = poly[n-2], !INTEGERP (lx))
22250 || (ly = poly[n-1], !INTEGERP (lx)))
22251 return 0;
22252 x0 = XINT (lx), y0 = XINT (ly);
22253 for (i = 0; i < n; i += 2)
22254 {
22255 int x1 = x0, y1 = y0;
22256 if ((lx = poly[i], !INTEGERP (lx))
22257 || (ly = poly[i+1], !INTEGERP (ly)))
22258 return 0;
22259 x0 = XINT (lx), y0 = XINT (ly);
22260
22261 /* Does this segment cross the X line? */
22262 if (x0 >= x)
22263 {
22264 if (x1 >= x)
22265 continue;
22266 }
22267 else if (x1 < x)
22268 continue;
22269 if (y > y0 && y > y1)
22270 continue;
22271 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22272 inside = !inside;
22273 }
22274 return inside;
22275 }
22276 }
22277 /* If we don't understand the format, pretend we're not in the hot-spot. */
22278 return 0;
22279 }
22280
22281 Lisp_Object
22282 find_hot_spot (map, x, y)
22283 Lisp_Object map;
22284 int x, y;
22285 {
22286 while (CONSP (map))
22287 {
22288 if (CONSP (XCAR (map))
22289 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22290 return XCAR (map);
22291 map = XCDR (map);
22292 }
22293
22294 return Qnil;
22295 }
22296
22297 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22298 3, 3, 0,
22299 doc: /* Lookup in image map MAP coordinates X and Y.
22300 An image map is an alist where each element has the format (AREA ID PLIST).
22301 An AREA is specified as either a rectangle, a circle, or a polygon:
22302 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22303 pixel coordinates of the upper left and bottom right corners.
22304 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22305 and the radius of the circle; r may be a float or integer.
22306 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22307 vector describes one corner in the polygon.
22308 Returns the alist element for the first matching AREA in MAP. */)
22309 (map, x, y)
22310 Lisp_Object map;
22311 Lisp_Object x, y;
22312 {
22313 if (NILP (map))
22314 return Qnil;
22315
22316 CHECK_NUMBER (x);
22317 CHECK_NUMBER (y);
22318
22319 return find_hot_spot (map, XINT (x), XINT (y));
22320 }
22321
22322
22323 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22324 static void
22325 define_frame_cursor1 (f, cursor, pointer)
22326 struct frame *f;
22327 Cursor cursor;
22328 Lisp_Object pointer;
22329 {
22330 /* Do not change cursor shape while dragging mouse. */
22331 if (!NILP (do_mouse_tracking))
22332 return;
22333
22334 if (!NILP (pointer))
22335 {
22336 if (EQ (pointer, Qarrow))
22337 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22338 else if (EQ (pointer, Qhand))
22339 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22340 else if (EQ (pointer, Qtext))
22341 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22342 else if (EQ (pointer, intern ("hdrag")))
22343 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22344 #ifdef HAVE_X_WINDOWS
22345 else if (EQ (pointer, intern ("vdrag")))
22346 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22347 #endif
22348 else if (EQ (pointer, intern ("hourglass")))
22349 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22350 else if (EQ (pointer, Qmodeline))
22351 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22352 else
22353 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22354 }
22355
22356 if (cursor != No_Cursor)
22357 rif->define_frame_cursor (f, cursor);
22358 }
22359
22360 /* Take proper action when mouse has moved to the mode or header line
22361 or marginal area AREA of window W, x-position X and y-position Y.
22362 X is relative to the start of the text display area of W, so the
22363 width of bitmap areas and scroll bars must be subtracted to get a
22364 position relative to the start of the mode line. */
22365
22366 static void
22367 note_mode_line_or_margin_highlight (window, x, y, area)
22368 Lisp_Object window;
22369 int x, y;
22370 enum window_part area;
22371 {
22372 struct window *w = XWINDOW (window);
22373 struct frame *f = XFRAME (w->frame);
22374 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22375 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22376 Lisp_Object pointer = Qnil;
22377 int charpos, dx, dy, width, height;
22378 Lisp_Object string, object = Qnil;
22379 Lisp_Object pos, help;
22380
22381 Lisp_Object mouse_face;
22382 int original_x_pixel = x;
22383 struct glyph * glyph = NULL;
22384 struct glyph_row *row;
22385
22386 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22387 {
22388 int x0;
22389 struct glyph *end;
22390
22391 string = mode_line_string (w, area, &x, &y, &charpos,
22392 &object, &dx, &dy, &width, &height);
22393
22394 row = (area == ON_MODE_LINE
22395 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22396 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22397
22398 /* Find glyph */
22399 if (row->mode_line_p && row->enabled_p)
22400 {
22401 glyph = row->glyphs[TEXT_AREA];
22402 end = glyph + row->used[TEXT_AREA];
22403
22404 for (x0 = original_x_pixel;
22405 glyph < end && x0 >= glyph->pixel_width;
22406 ++glyph)
22407 x0 -= glyph->pixel_width;
22408
22409 if (glyph >= end)
22410 glyph = NULL;
22411 }
22412 }
22413 else
22414 {
22415 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22416 string = marginal_area_string (w, area, &x, &y, &charpos,
22417 &object, &dx, &dy, &width, &height);
22418 }
22419
22420 help = Qnil;
22421
22422 if (IMAGEP (object))
22423 {
22424 Lisp_Object image_map, hotspot;
22425 if ((image_map = Fplist_get (XCDR (object), QCmap),
22426 !NILP (image_map))
22427 && (hotspot = find_hot_spot (image_map, dx, dy),
22428 CONSP (hotspot))
22429 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22430 {
22431 Lisp_Object area_id, plist;
22432
22433 area_id = XCAR (hotspot);
22434 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22435 If so, we could look for mouse-enter, mouse-leave
22436 properties in PLIST (and do something...). */
22437 hotspot = XCDR (hotspot);
22438 if (CONSP (hotspot)
22439 && (plist = XCAR (hotspot), CONSP (plist)))
22440 {
22441 pointer = Fplist_get (plist, Qpointer);
22442 if (NILP (pointer))
22443 pointer = Qhand;
22444 help = Fplist_get (plist, Qhelp_echo);
22445 if (!NILP (help))
22446 {
22447 help_echo_string = help;
22448 /* Is this correct? ++kfs */
22449 XSETWINDOW (help_echo_window, w);
22450 help_echo_object = w->buffer;
22451 help_echo_pos = charpos;
22452 }
22453 }
22454 }
22455 if (NILP (pointer))
22456 pointer = Fplist_get (XCDR (object), QCpointer);
22457 }
22458
22459 if (STRINGP (string))
22460 {
22461 pos = make_number (charpos);
22462 /* If we're on a string with `help-echo' text property, arrange
22463 for the help to be displayed. This is done by setting the
22464 global variable help_echo_string to the help string. */
22465 if (NILP (help))
22466 {
22467 help = Fget_text_property (pos, Qhelp_echo, string);
22468 if (!NILP (help))
22469 {
22470 help_echo_string = help;
22471 XSETWINDOW (help_echo_window, w);
22472 help_echo_object = string;
22473 help_echo_pos = charpos;
22474 }
22475 }
22476
22477 if (NILP (pointer))
22478 pointer = Fget_text_property (pos, Qpointer, string);
22479
22480 /* Change the mouse pointer according to what is under X/Y. */
22481 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22482 {
22483 Lisp_Object map;
22484 map = Fget_text_property (pos, Qlocal_map, string);
22485 if (!KEYMAPP (map))
22486 map = Fget_text_property (pos, Qkeymap, string);
22487 if (!KEYMAPP (map))
22488 cursor = dpyinfo->vertical_scroll_bar_cursor;
22489 }
22490
22491 /* Change the mouse face according to what is under X/Y. */
22492 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22493 if (!NILP (mouse_face)
22494 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22495 && glyph)
22496 {
22497 Lisp_Object b, e;
22498
22499 struct glyph * tmp_glyph;
22500
22501 int gpos;
22502 int gseq_length;
22503 int total_pixel_width;
22504 int ignore;
22505
22506 int vpos, hpos;
22507
22508 b = Fprevious_single_property_change (make_number (charpos + 1),
22509 Qmouse_face, string, Qnil);
22510 if (NILP (b))
22511 b = make_number (0);
22512
22513 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22514 if (NILP (e))
22515 e = make_number (SCHARS (string));
22516
22517 /* Calculate the position(glyph position: GPOS) of GLYPH in
22518 displayed string. GPOS is different from CHARPOS.
22519
22520 CHARPOS is the position of glyph in internal string
22521 object. A mode line string format has structures which
22522 is converted to a flatten by emacs lisp interpreter.
22523 The internal string is an element of the structures.
22524 The displayed string is the flatten string. */
22525 for (tmp_glyph = glyph - 1, gpos = 0;
22526 tmp_glyph->charpos >= XINT (b);
22527 tmp_glyph--, gpos++)
22528 {
22529 if (!EQ (tmp_glyph->object, glyph->object))
22530 break;
22531 }
22532
22533 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22534 displayed string holding GLYPH.
22535
22536 GSEQ_LENGTH is different from SCHARS (STRING).
22537 SCHARS (STRING) returns the length of the internal string. */
22538 for (tmp_glyph = glyph, gseq_length = gpos;
22539 tmp_glyph->charpos < XINT (e);
22540 tmp_glyph++, gseq_length++)
22541 {
22542 if (!EQ (tmp_glyph->object, glyph->object))
22543 break;
22544 }
22545
22546 total_pixel_width = 0;
22547 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22548 total_pixel_width += tmp_glyph->pixel_width;
22549
22550 /* Pre calculation of re-rendering position */
22551 vpos = (x - gpos);
22552 hpos = (area == ON_MODE_LINE
22553 ? (w->current_matrix)->nrows - 1
22554 : 0);
22555
22556 /* If the re-rendering position is included in the last
22557 re-rendering area, we should do nothing. */
22558 if ( EQ (window, dpyinfo->mouse_face_window)
22559 && dpyinfo->mouse_face_beg_col <= vpos
22560 && vpos < dpyinfo->mouse_face_end_col
22561 && dpyinfo->mouse_face_beg_row == hpos )
22562 return;
22563
22564 if (clear_mouse_face (dpyinfo))
22565 cursor = No_Cursor;
22566
22567 dpyinfo->mouse_face_beg_col = vpos;
22568 dpyinfo->mouse_face_beg_row = hpos;
22569
22570 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22571 dpyinfo->mouse_face_beg_y = 0;
22572
22573 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22574 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22575
22576 dpyinfo->mouse_face_end_x = 0;
22577 dpyinfo->mouse_face_end_y = 0;
22578
22579 dpyinfo->mouse_face_past_end = 0;
22580 dpyinfo->mouse_face_window = window;
22581
22582 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22583 charpos,
22584 0, 0, 0, &ignore,
22585 glyph->face_id, 1);
22586 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22587
22588 if (NILP (pointer))
22589 pointer = Qhand;
22590 }
22591 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22592 clear_mouse_face (dpyinfo);
22593 }
22594 define_frame_cursor1 (f, cursor, pointer);
22595 }
22596
22597
22598 /* EXPORT:
22599 Take proper action when the mouse has moved to position X, Y on
22600 frame F as regards highlighting characters that have mouse-face
22601 properties. Also de-highlighting chars where the mouse was before.
22602 X and Y can be negative or out of range. */
22603
22604 void
22605 note_mouse_highlight (f, x, y)
22606 struct frame *f;
22607 int x, y;
22608 {
22609 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22610 enum window_part part;
22611 Lisp_Object window;
22612 struct window *w;
22613 Cursor cursor = No_Cursor;
22614 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22615 struct buffer *b;
22616
22617 /* When a menu is active, don't highlight because this looks odd. */
22618 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22619 if (popup_activated ())
22620 return;
22621 #endif
22622
22623 if (NILP (Vmouse_highlight)
22624 || !f->glyphs_initialized_p)
22625 return;
22626
22627 dpyinfo->mouse_face_mouse_x = x;
22628 dpyinfo->mouse_face_mouse_y = y;
22629 dpyinfo->mouse_face_mouse_frame = f;
22630
22631 if (dpyinfo->mouse_face_defer)
22632 return;
22633
22634 if (gc_in_progress)
22635 {
22636 dpyinfo->mouse_face_deferred_gc = 1;
22637 return;
22638 }
22639
22640 /* Which window is that in? */
22641 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22642
22643 /* If we were displaying active text in another window, clear that.
22644 Also clear if we move out of text area in same window. */
22645 if (! EQ (window, dpyinfo->mouse_face_window)
22646 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22647 && !NILP (dpyinfo->mouse_face_window)))
22648 clear_mouse_face (dpyinfo);
22649
22650 /* Not on a window -> return. */
22651 if (!WINDOWP (window))
22652 return;
22653
22654 /* Reset help_echo_string. It will get recomputed below. */
22655 help_echo_string = Qnil;
22656
22657 /* Convert to window-relative pixel coordinates. */
22658 w = XWINDOW (window);
22659 frame_to_window_pixel_xy (w, &x, &y);
22660
22661 /* Handle tool-bar window differently since it doesn't display a
22662 buffer. */
22663 if (EQ (window, f->tool_bar_window))
22664 {
22665 note_tool_bar_highlight (f, x, y);
22666 return;
22667 }
22668
22669 /* Mouse is on the mode, header line or margin? */
22670 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22671 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22672 {
22673 note_mode_line_or_margin_highlight (window, x, y, part);
22674 return;
22675 }
22676
22677 if (part == ON_VERTICAL_BORDER)
22678 {
22679 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22680 help_echo_string = build_string ("drag-mouse-1: resize");
22681 }
22682 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22683 || part == ON_SCROLL_BAR)
22684 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22685 else
22686 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22687
22688 /* Are we in a window whose display is up to date?
22689 And verify the buffer's text has not changed. */
22690 b = XBUFFER (w->buffer);
22691 if (part == ON_TEXT
22692 && EQ (w->window_end_valid, w->buffer)
22693 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22694 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22695 {
22696 int hpos, vpos, pos, i, dx, dy, area;
22697 struct glyph *glyph;
22698 Lisp_Object object;
22699 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22700 Lisp_Object *overlay_vec = NULL;
22701 int noverlays;
22702 struct buffer *obuf;
22703 int obegv, ozv, same_region;
22704
22705 /* Find the glyph under X/Y. */
22706 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22707
22708 /* Look for :pointer property on image. */
22709 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22710 {
22711 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22712 if (img != NULL && IMAGEP (img->spec))
22713 {
22714 Lisp_Object image_map, hotspot;
22715 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22716 !NILP (image_map))
22717 && (hotspot = find_hot_spot (image_map,
22718 glyph->slice.x + dx,
22719 glyph->slice.y + dy),
22720 CONSP (hotspot))
22721 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22722 {
22723 Lisp_Object area_id, plist;
22724
22725 area_id = XCAR (hotspot);
22726 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22727 If so, we could look for mouse-enter, mouse-leave
22728 properties in PLIST (and do something...). */
22729 hotspot = XCDR (hotspot);
22730 if (CONSP (hotspot)
22731 && (plist = XCAR (hotspot), CONSP (plist)))
22732 {
22733 pointer = Fplist_get (plist, Qpointer);
22734 if (NILP (pointer))
22735 pointer = Qhand;
22736 help_echo_string = Fplist_get (plist, Qhelp_echo);
22737 if (!NILP (help_echo_string))
22738 {
22739 help_echo_window = window;
22740 help_echo_object = glyph->object;
22741 help_echo_pos = glyph->charpos;
22742 }
22743 }
22744 }
22745 if (NILP (pointer))
22746 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22747 }
22748 }
22749
22750 /* Clear mouse face if X/Y not over text. */
22751 if (glyph == NULL
22752 || area != TEXT_AREA
22753 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22754 {
22755 if (clear_mouse_face (dpyinfo))
22756 cursor = No_Cursor;
22757 if (NILP (pointer))
22758 {
22759 if (area != TEXT_AREA)
22760 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22761 else
22762 pointer = Vvoid_text_area_pointer;
22763 }
22764 goto set_cursor;
22765 }
22766
22767 pos = glyph->charpos;
22768 object = glyph->object;
22769 if (!STRINGP (object) && !BUFFERP (object))
22770 goto set_cursor;
22771
22772 /* If we get an out-of-range value, return now; avoid an error. */
22773 if (BUFFERP (object) && pos > BUF_Z (b))
22774 goto set_cursor;
22775
22776 /* Make the window's buffer temporarily current for
22777 overlays_at and compute_char_face. */
22778 obuf = current_buffer;
22779 current_buffer = b;
22780 obegv = BEGV;
22781 ozv = ZV;
22782 BEGV = BEG;
22783 ZV = Z;
22784
22785 /* Is this char mouse-active or does it have help-echo? */
22786 position = make_number (pos);
22787
22788 if (BUFFERP (object))
22789 {
22790 /* Put all the overlays we want in a vector in overlay_vec. */
22791 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22792 /* Sort overlays into increasing priority order. */
22793 noverlays = sort_overlays (overlay_vec, noverlays, w);
22794 }
22795 else
22796 noverlays = 0;
22797
22798 same_region = (EQ (window, dpyinfo->mouse_face_window)
22799 && vpos >= dpyinfo->mouse_face_beg_row
22800 && vpos <= dpyinfo->mouse_face_end_row
22801 && (vpos > dpyinfo->mouse_face_beg_row
22802 || hpos >= dpyinfo->mouse_face_beg_col)
22803 && (vpos < dpyinfo->mouse_face_end_row
22804 || hpos < dpyinfo->mouse_face_end_col
22805 || dpyinfo->mouse_face_past_end));
22806
22807 if (same_region)
22808 cursor = No_Cursor;
22809
22810 /* Check mouse-face highlighting. */
22811 if (! same_region
22812 /* If there exists an overlay with mouse-face overlapping
22813 the one we are currently highlighting, we have to
22814 check if we enter the overlapping overlay, and then
22815 highlight only that. */
22816 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22817 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22818 {
22819 /* Find the highest priority overlay that has a mouse-face
22820 property. */
22821 overlay = Qnil;
22822 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22823 {
22824 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22825 if (!NILP (mouse_face))
22826 overlay = overlay_vec[i];
22827 }
22828
22829 /* If we're actually highlighting the same overlay as
22830 before, there's no need to do that again. */
22831 if (!NILP (overlay)
22832 && EQ (overlay, dpyinfo->mouse_face_overlay))
22833 goto check_help_echo;
22834
22835 dpyinfo->mouse_face_overlay = overlay;
22836
22837 /* Clear the display of the old active region, if any. */
22838 if (clear_mouse_face (dpyinfo))
22839 cursor = No_Cursor;
22840
22841 /* If no overlay applies, get a text property. */
22842 if (NILP (overlay))
22843 mouse_face = Fget_text_property (position, Qmouse_face, object);
22844
22845 /* Handle the overlay case. */
22846 if (!NILP (overlay))
22847 {
22848 /* Find the range of text around this char that
22849 should be active. */
22850 Lisp_Object before, after;
22851 int ignore;
22852
22853 before = Foverlay_start (overlay);
22854 after = Foverlay_end (overlay);
22855 /* Record this as the current active region. */
22856 fast_find_position (w, XFASTINT (before),
22857 &dpyinfo->mouse_face_beg_col,
22858 &dpyinfo->mouse_face_beg_row,
22859 &dpyinfo->mouse_face_beg_x,
22860 &dpyinfo->mouse_face_beg_y, Qnil);
22861
22862 dpyinfo->mouse_face_past_end
22863 = !fast_find_position (w, XFASTINT (after),
22864 &dpyinfo->mouse_face_end_col,
22865 &dpyinfo->mouse_face_end_row,
22866 &dpyinfo->mouse_face_end_x,
22867 &dpyinfo->mouse_face_end_y, Qnil);
22868 dpyinfo->mouse_face_window = window;
22869
22870 dpyinfo->mouse_face_face_id
22871 = face_at_buffer_position (w, pos, 0, 0,
22872 &ignore, pos + 1,
22873 !dpyinfo->mouse_face_hidden);
22874
22875 /* Display it as active. */
22876 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22877 cursor = No_Cursor;
22878 }
22879 /* Handle the text property case. */
22880 else if (!NILP (mouse_face) && BUFFERP (object))
22881 {
22882 /* Find the range of text around this char that
22883 should be active. */
22884 Lisp_Object before, after, beginning, end;
22885 int ignore;
22886
22887 beginning = Fmarker_position (w->start);
22888 end = make_number (BUF_Z (XBUFFER (object))
22889 - XFASTINT (w->window_end_pos));
22890 before
22891 = Fprevious_single_property_change (make_number (pos + 1),
22892 Qmouse_face,
22893 object, beginning);
22894 after
22895 = Fnext_single_property_change (position, Qmouse_face,
22896 object, end);
22897
22898 /* Record this as the current active region. */
22899 fast_find_position (w, XFASTINT (before),
22900 &dpyinfo->mouse_face_beg_col,
22901 &dpyinfo->mouse_face_beg_row,
22902 &dpyinfo->mouse_face_beg_x,
22903 &dpyinfo->mouse_face_beg_y, Qnil);
22904 dpyinfo->mouse_face_past_end
22905 = !fast_find_position (w, XFASTINT (after),
22906 &dpyinfo->mouse_face_end_col,
22907 &dpyinfo->mouse_face_end_row,
22908 &dpyinfo->mouse_face_end_x,
22909 &dpyinfo->mouse_face_end_y, Qnil);
22910 dpyinfo->mouse_face_window = window;
22911
22912 if (BUFFERP (object))
22913 dpyinfo->mouse_face_face_id
22914 = face_at_buffer_position (w, pos, 0, 0,
22915 &ignore, pos + 1,
22916 !dpyinfo->mouse_face_hidden);
22917
22918 /* Display it as active. */
22919 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22920 cursor = No_Cursor;
22921 }
22922 else if (!NILP (mouse_face) && STRINGP (object))
22923 {
22924 Lisp_Object b, e;
22925 int ignore;
22926
22927 b = Fprevious_single_property_change (make_number (pos + 1),
22928 Qmouse_face,
22929 object, Qnil);
22930 e = Fnext_single_property_change (position, Qmouse_face,
22931 object, Qnil);
22932 if (NILP (b))
22933 b = make_number (0);
22934 if (NILP (e))
22935 e = make_number (SCHARS (object) - 1);
22936
22937 fast_find_string_pos (w, XINT (b), object,
22938 &dpyinfo->mouse_face_beg_col,
22939 &dpyinfo->mouse_face_beg_row,
22940 &dpyinfo->mouse_face_beg_x,
22941 &dpyinfo->mouse_face_beg_y, 0);
22942 fast_find_string_pos (w, XINT (e), object,
22943 &dpyinfo->mouse_face_end_col,
22944 &dpyinfo->mouse_face_end_row,
22945 &dpyinfo->mouse_face_end_x,
22946 &dpyinfo->mouse_face_end_y, 1);
22947 dpyinfo->mouse_face_past_end = 0;
22948 dpyinfo->mouse_face_window = window;
22949 dpyinfo->mouse_face_face_id
22950 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22951 glyph->face_id, 1);
22952 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22953 cursor = No_Cursor;
22954 }
22955 else if (STRINGP (object) && NILP (mouse_face))
22956 {
22957 /* A string which doesn't have mouse-face, but
22958 the text ``under'' it might have. */
22959 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22960 int start = MATRIX_ROW_START_CHARPOS (r);
22961
22962 pos = string_buffer_position (w, object, start);
22963 if (pos > 0)
22964 mouse_face = get_char_property_and_overlay (make_number (pos),
22965 Qmouse_face,
22966 w->buffer,
22967 &overlay);
22968 if (!NILP (mouse_face) && !NILP (overlay))
22969 {
22970 Lisp_Object before = Foverlay_start (overlay);
22971 Lisp_Object after = Foverlay_end (overlay);
22972 int ignore;
22973
22974 /* Note that we might not be able to find position
22975 BEFORE in the glyph matrix if the overlay is
22976 entirely covered by a `display' property. In
22977 this case, we overshoot. So let's stop in
22978 the glyph matrix before glyphs for OBJECT. */
22979 fast_find_position (w, XFASTINT (before),
22980 &dpyinfo->mouse_face_beg_col,
22981 &dpyinfo->mouse_face_beg_row,
22982 &dpyinfo->mouse_face_beg_x,
22983 &dpyinfo->mouse_face_beg_y,
22984 object);
22985
22986 dpyinfo->mouse_face_past_end
22987 = !fast_find_position (w, XFASTINT (after),
22988 &dpyinfo->mouse_face_end_col,
22989 &dpyinfo->mouse_face_end_row,
22990 &dpyinfo->mouse_face_end_x,
22991 &dpyinfo->mouse_face_end_y,
22992 Qnil);
22993 dpyinfo->mouse_face_window = window;
22994 dpyinfo->mouse_face_face_id
22995 = face_at_buffer_position (w, pos, 0, 0,
22996 &ignore, pos + 1,
22997 !dpyinfo->mouse_face_hidden);
22998
22999 /* Display it as active. */
23000 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
23001 cursor = No_Cursor;
23002 }
23003 }
23004 }
23005
23006 check_help_echo:
23007
23008 /* Look for a `help-echo' property. */
23009 if (NILP (help_echo_string)) {
23010 Lisp_Object help, overlay;
23011
23012 /* Check overlays first. */
23013 help = overlay = Qnil;
23014 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
23015 {
23016 overlay = overlay_vec[i];
23017 help = Foverlay_get (overlay, Qhelp_echo);
23018 }
23019
23020 if (!NILP (help))
23021 {
23022 help_echo_string = help;
23023 help_echo_window = window;
23024 help_echo_object = overlay;
23025 help_echo_pos = pos;
23026 }
23027 else
23028 {
23029 Lisp_Object object = glyph->object;
23030 int charpos = glyph->charpos;
23031
23032 /* Try text properties. */
23033 if (STRINGP (object)
23034 && charpos >= 0
23035 && charpos < SCHARS (object))
23036 {
23037 help = Fget_text_property (make_number (charpos),
23038 Qhelp_echo, object);
23039 if (NILP (help))
23040 {
23041 /* If the string itself doesn't specify a help-echo,
23042 see if the buffer text ``under'' it does. */
23043 struct glyph_row *r
23044 = MATRIX_ROW (w->current_matrix, vpos);
23045 int start = MATRIX_ROW_START_CHARPOS (r);
23046 int pos = string_buffer_position (w, object, start);
23047 if (pos > 0)
23048 {
23049 help = Fget_char_property (make_number (pos),
23050 Qhelp_echo, w->buffer);
23051 if (!NILP (help))
23052 {
23053 charpos = pos;
23054 object = w->buffer;
23055 }
23056 }
23057 }
23058 }
23059 else if (BUFFERP (object)
23060 && charpos >= BEGV
23061 && charpos < ZV)
23062 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23063 object);
23064
23065 if (!NILP (help))
23066 {
23067 help_echo_string = help;
23068 help_echo_window = window;
23069 help_echo_object = object;
23070 help_echo_pos = charpos;
23071 }
23072 }
23073 }
23074
23075 /* Look for a `pointer' property. */
23076 if (NILP (pointer))
23077 {
23078 /* Check overlays first. */
23079 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23080 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23081
23082 if (NILP (pointer))
23083 {
23084 Lisp_Object object = glyph->object;
23085 int charpos = glyph->charpos;
23086
23087 /* Try text properties. */
23088 if (STRINGP (object)
23089 && charpos >= 0
23090 && charpos < SCHARS (object))
23091 {
23092 pointer = Fget_text_property (make_number (charpos),
23093 Qpointer, object);
23094 if (NILP (pointer))
23095 {
23096 /* If the string itself doesn't specify a pointer,
23097 see if the buffer text ``under'' it does. */
23098 struct glyph_row *r
23099 = MATRIX_ROW (w->current_matrix, vpos);
23100 int start = MATRIX_ROW_START_CHARPOS (r);
23101 int pos = string_buffer_position (w, object, start);
23102 if (pos > 0)
23103 pointer = Fget_char_property (make_number (pos),
23104 Qpointer, w->buffer);
23105 }
23106 }
23107 else if (BUFFERP (object)
23108 && charpos >= BEGV
23109 && charpos < ZV)
23110 pointer = Fget_text_property (make_number (charpos),
23111 Qpointer, object);
23112 }
23113 }
23114
23115 BEGV = obegv;
23116 ZV = ozv;
23117 current_buffer = obuf;
23118 }
23119
23120 set_cursor:
23121
23122 define_frame_cursor1 (f, cursor, pointer);
23123 }
23124
23125
23126 /* EXPORT for RIF:
23127 Clear any mouse-face on window W. This function is part of the
23128 redisplay interface, and is called from try_window_id and similar
23129 functions to ensure the mouse-highlight is off. */
23130
23131 void
23132 x_clear_window_mouse_face (w)
23133 struct window *w;
23134 {
23135 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23136 Lisp_Object window;
23137
23138 BLOCK_INPUT;
23139 XSETWINDOW (window, w);
23140 if (EQ (window, dpyinfo->mouse_face_window))
23141 clear_mouse_face (dpyinfo);
23142 UNBLOCK_INPUT;
23143 }
23144
23145
23146 /* EXPORT:
23147 Just discard the mouse face information for frame F, if any.
23148 This is used when the size of F is changed. */
23149
23150 void
23151 cancel_mouse_face (f)
23152 struct frame *f;
23153 {
23154 Lisp_Object window;
23155 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23156
23157 window = dpyinfo->mouse_face_window;
23158 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23159 {
23160 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23161 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23162 dpyinfo->mouse_face_window = Qnil;
23163 }
23164 }
23165
23166
23167 #endif /* HAVE_WINDOW_SYSTEM */
23168
23169 \f
23170 /***********************************************************************
23171 Exposure Events
23172 ***********************************************************************/
23173
23174 #ifdef HAVE_WINDOW_SYSTEM
23175
23176 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23177 which intersects rectangle R. R is in window-relative coordinates. */
23178
23179 static void
23180 expose_area (w, row, r, area)
23181 struct window *w;
23182 struct glyph_row *row;
23183 XRectangle *r;
23184 enum glyph_row_area area;
23185 {
23186 struct glyph *first = row->glyphs[area];
23187 struct glyph *end = row->glyphs[area] + row->used[area];
23188 struct glyph *last;
23189 int first_x, start_x, x;
23190
23191 if (area == TEXT_AREA && row->fill_line_p)
23192 /* If row extends face to end of line write the whole line. */
23193 draw_glyphs (w, 0, row, area,
23194 0, row->used[area],
23195 DRAW_NORMAL_TEXT, 0);
23196 else
23197 {
23198 /* Set START_X to the window-relative start position for drawing glyphs of
23199 AREA. The first glyph of the text area can be partially visible.
23200 The first glyphs of other areas cannot. */
23201 start_x = window_box_left_offset (w, area);
23202 x = start_x;
23203 if (area == TEXT_AREA)
23204 x += row->x;
23205
23206 /* Find the first glyph that must be redrawn. */
23207 while (first < end
23208 && x + first->pixel_width < r->x)
23209 {
23210 x += first->pixel_width;
23211 ++first;
23212 }
23213
23214 /* Find the last one. */
23215 last = first;
23216 first_x = x;
23217 while (last < end
23218 && x < r->x + r->width)
23219 {
23220 x += last->pixel_width;
23221 ++last;
23222 }
23223
23224 /* Repaint. */
23225 if (last > first)
23226 draw_glyphs (w, first_x - start_x, row, area,
23227 first - row->glyphs[area], last - row->glyphs[area],
23228 DRAW_NORMAL_TEXT, 0);
23229 }
23230 }
23231
23232
23233 /* Redraw the parts of the glyph row ROW on window W intersecting
23234 rectangle R. R is in window-relative coordinates. Value is
23235 non-zero if mouse-face was overwritten. */
23236
23237 static int
23238 expose_line (w, row, r)
23239 struct window *w;
23240 struct glyph_row *row;
23241 XRectangle *r;
23242 {
23243 xassert (row->enabled_p);
23244
23245 if (row->mode_line_p || w->pseudo_window_p)
23246 draw_glyphs (w, 0, row, TEXT_AREA,
23247 0, row->used[TEXT_AREA],
23248 DRAW_NORMAL_TEXT, 0);
23249 else
23250 {
23251 if (row->used[LEFT_MARGIN_AREA])
23252 expose_area (w, row, r, LEFT_MARGIN_AREA);
23253 if (row->used[TEXT_AREA])
23254 expose_area (w, row, r, TEXT_AREA);
23255 if (row->used[RIGHT_MARGIN_AREA])
23256 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23257 draw_row_fringe_bitmaps (w, row);
23258 }
23259
23260 return row->mouse_face_p;
23261 }
23262
23263
23264 /* Redraw those parts of glyphs rows during expose event handling that
23265 overlap other rows. Redrawing of an exposed line writes over parts
23266 of lines overlapping that exposed line; this function fixes that.
23267
23268 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23269 row in W's current matrix that is exposed and overlaps other rows.
23270 LAST_OVERLAPPING_ROW is the last such row. */
23271
23272 static void
23273 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23274 struct window *w;
23275 struct glyph_row *first_overlapping_row;
23276 struct glyph_row *last_overlapping_row;
23277 {
23278 struct glyph_row *row;
23279
23280 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23281 if (row->overlapping_p)
23282 {
23283 xassert (row->enabled_p && !row->mode_line_p);
23284
23285 if (row->used[LEFT_MARGIN_AREA])
23286 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23287
23288 if (row->used[TEXT_AREA])
23289 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23290
23291 if (row->used[RIGHT_MARGIN_AREA])
23292 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23293 }
23294 }
23295
23296
23297 /* Return non-zero if W's cursor intersects rectangle R. */
23298
23299 static int
23300 phys_cursor_in_rect_p (w, r)
23301 struct window *w;
23302 XRectangle *r;
23303 {
23304 XRectangle cr, result;
23305 struct glyph *cursor_glyph;
23306
23307 cursor_glyph = get_phys_cursor_glyph (w);
23308 if (cursor_glyph)
23309 {
23310 /* r is relative to W's box, but w->phys_cursor.x is relative
23311 to left edge of W's TEXT area. Adjust it. */
23312 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23313 cr.y = w->phys_cursor.y;
23314 cr.width = cursor_glyph->pixel_width;
23315 cr.height = w->phys_cursor_height;
23316 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23317 I assume the effect is the same -- and this is portable. */
23318 return x_intersect_rectangles (&cr, r, &result);
23319 }
23320 else
23321 return 0;
23322 }
23323
23324
23325 /* EXPORT:
23326 Draw a vertical window border to the right of window W if W doesn't
23327 have vertical scroll bars. */
23328
23329 void
23330 x_draw_vertical_border (w)
23331 struct window *w;
23332 {
23333 /* We could do better, if we knew what type of scroll-bar the adjacent
23334 windows (on either side) have... But we don't :-(
23335 However, I think this works ok. ++KFS 2003-04-25 */
23336
23337 /* Redraw borders between horizontally adjacent windows. Don't
23338 do it for frames with vertical scroll bars because either the
23339 right scroll bar of a window, or the left scroll bar of its
23340 neighbor will suffice as a border. */
23341 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23342 return;
23343
23344 if (!WINDOW_RIGHTMOST_P (w)
23345 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23346 {
23347 int x0, x1, y0, y1;
23348
23349 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23350 y1 -= 1;
23351
23352 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23353 x1 -= 1;
23354
23355 rif->draw_vertical_window_border (w, x1, y0, y1);
23356 }
23357 else if (!WINDOW_LEFTMOST_P (w)
23358 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23359 {
23360 int x0, x1, y0, y1;
23361
23362 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23363 y1 -= 1;
23364
23365 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23366 x0 -= 1;
23367
23368 rif->draw_vertical_window_border (w, x0, y0, y1);
23369 }
23370 }
23371
23372
23373 /* Redraw the part of window W intersection rectangle FR. Pixel
23374 coordinates in FR are frame-relative. Call this function with
23375 input blocked. Value is non-zero if the exposure overwrites
23376 mouse-face. */
23377
23378 static int
23379 expose_window (w, fr)
23380 struct window *w;
23381 XRectangle *fr;
23382 {
23383 struct frame *f = XFRAME (w->frame);
23384 XRectangle wr, r;
23385 int mouse_face_overwritten_p = 0;
23386
23387 /* If window is not yet fully initialized, do nothing. This can
23388 happen when toolkit scroll bars are used and a window is split.
23389 Reconfiguring the scroll bar will generate an expose for a newly
23390 created window. */
23391 if (w->current_matrix == NULL)
23392 return 0;
23393
23394 /* When we're currently updating the window, display and current
23395 matrix usually don't agree. Arrange for a thorough display
23396 later. */
23397 if (w == updated_window)
23398 {
23399 SET_FRAME_GARBAGED (f);
23400 return 0;
23401 }
23402
23403 /* Frame-relative pixel rectangle of W. */
23404 wr.x = WINDOW_LEFT_EDGE_X (w);
23405 wr.y = WINDOW_TOP_EDGE_Y (w);
23406 wr.width = WINDOW_TOTAL_WIDTH (w);
23407 wr.height = WINDOW_TOTAL_HEIGHT (w);
23408
23409 if (x_intersect_rectangles (fr, &wr, &r))
23410 {
23411 int yb = window_text_bottom_y (w);
23412 struct glyph_row *row;
23413 int cursor_cleared_p;
23414 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23415
23416 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23417 r.x, r.y, r.width, r.height));
23418
23419 /* Convert to window coordinates. */
23420 r.x -= WINDOW_LEFT_EDGE_X (w);
23421 r.y -= WINDOW_TOP_EDGE_Y (w);
23422
23423 /* Turn off the cursor. */
23424 if (!w->pseudo_window_p
23425 && phys_cursor_in_rect_p (w, &r))
23426 {
23427 x_clear_cursor (w);
23428 cursor_cleared_p = 1;
23429 }
23430 else
23431 cursor_cleared_p = 0;
23432
23433 /* Update lines intersecting rectangle R. */
23434 first_overlapping_row = last_overlapping_row = NULL;
23435 for (row = w->current_matrix->rows;
23436 row->enabled_p;
23437 ++row)
23438 {
23439 int y0 = row->y;
23440 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23441
23442 if ((y0 >= r.y && y0 < r.y + r.height)
23443 || (y1 > r.y && y1 < r.y + r.height)
23444 || (r.y >= y0 && r.y < y1)
23445 || (r.y + r.height > y0 && r.y + r.height < y1))
23446 {
23447 /* A header line may be overlapping, but there is no need
23448 to fix overlapping areas for them. KFS 2005-02-12 */
23449 if (row->overlapping_p && !row->mode_line_p)
23450 {
23451 if (first_overlapping_row == NULL)
23452 first_overlapping_row = row;
23453 last_overlapping_row = row;
23454 }
23455
23456 if (expose_line (w, row, &r))
23457 mouse_face_overwritten_p = 1;
23458 }
23459
23460 if (y1 >= yb)
23461 break;
23462 }
23463
23464 /* Display the mode line if there is one. */
23465 if (WINDOW_WANTS_MODELINE_P (w)
23466 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23467 row->enabled_p)
23468 && row->y < r.y + r.height)
23469 {
23470 if (expose_line (w, row, &r))
23471 mouse_face_overwritten_p = 1;
23472 }
23473
23474 if (!w->pseudo_window_p)
23475 {
23476 /* Fix the display of overlapping rows. */
23477 if (first_overlapping_row)
23478 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23479
23480 /* Draw border between windows. */
23481 x_draw_vertical_border (w);
23482
23483 /* Turn the cursor on again. */
23484 if (cursor_cleared_p)
23485 update_window_cursor (w, 1);
23486 }
23487 }
23488
23489 return mouse_face_overwritten_p;
23490 }
23491
23492
23493
23494 /* Redraw (parts) of all windows in the window tree rooted at W that
23495 intersect R. R contains frame pixel coordinates. Value is
23496 non-zero if the exposure overwrites mouse-face. */
23497
23498 static int
23499 expose_window_tree (w, r)
23500 struct window *w;
23501 XRectangle *r;
23502 {
23503 struct frame *f = XFRAME (w->frame);
23504 int mouse_face_overwritten_p = 0;
23505
23506 while (w && !FRAME_GARBAGED_P (f))
23507 {
23508 if (!NILP (w->hchild))
23509 mouse_face_overwritten_p
23510 |= expose_window_tree (XWINDOW (w->hchild), r);
23511 else if (!NILP (w->vchild))
23512 mouse_face_overwritten_p
23513 |= expose_window_tree (XWINDOW (w->vchild), r);
23514 else
23515 mouse_face_overwritten_p |= expose_window (w, r);
23516
23517 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23518 }
23519
23520 return mouse_face_overwritten_p;
23521 }
23522
23523
23524 /* EXPORT:
23525 Redisplay an exposed area of frame F. X and Y are the upper-left
23526 corner of the exposed rectangle. W and H are width and height of
23527 the exposed area. All are pixel values. W or H zero means redraw
23528 the entire frame. */
23529
23530 void
23531 expose_frame (f, x, y, w, h)
23532 struct frame *f;
23533 int x, y, w, h;
23534 {
23535 XRectangle r;
23536 int mouse_face_overwritten_p = 0;
23537
23538 TRACE ((stderr, "expose_frame "));
23539
23540 /* No need to redraw if frame will be redrawn soon. */
23541 if (FRAME_GARBAGED_P (f))
23542 {
23543 TRACE ((stderr, " garbaged\n"));
23544 return;
23545 }
23546
23547 /* If basic faces haven't been realized yet, there is no point in
23548 trying to redraw anything. This can happen when we get an expose
23549 event while Emacs is starting, e.g. by moving another window. */
23550 if (FRAME_FACE_CACHE (f) == NULL
23551 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23552 {
23553 TRACE ((stderr, " no faces\n"));
23554 return;
23555 }
23556
23557 if (w == 0 || h == 0)
23558 {
23559 r.x = r.y = 0;
23560 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23561 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23562 }
23563 else
23564 {
23565 r.x = x;
23566 r.y = y;
23567 r.width = w;
23568 r.height = h;
23569 }
23570
23571 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23572 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23573
23574 if (WINDOWP (f->tool_bar_window))
23575 mouse_face_overwritten_p
23576 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23577
23578 #ifdef HAVE_X_WINDOWS
23579 #ifndef MSDOS
23580 #ifndef USE_X_TOOLKIT
23581 if (WINDOWP (f->menu_bar_window))
23582 mouse_face_overwritten_p
23583 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23584 #endif /* not USE_X_TOOLKIT */
23585 #endif
23586 #endif
23587
23588 /* Some window managers support a focus-follows-mouse style with
23589 delayed raising of frames. Imagine a partially obscured frame,
23590 and moving the mouse into partially obscured mouse-face on that
23591 frame. The visible part of the mouse-face will be highlighted,
23592 then the WM raises the obscured frame. With at least one WM, KDE
23593 2.1, Emacs is not getting any event for the raising of the frame
23594 (even tried with SubstructureRedirectMask), only Expose events.
23595 These expose events will draw text normally, i.e. not
23596 highlighted. Which means we must redo the highlight here.
23597 Subsume it under ``we love X''. --gerd 2001-08-15 */
23598 /* Included in Windows version because Windows most likely does not
23599 do the right thing if any third party tool offers
23600 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23601 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23602 {
23603 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23604 if (f == dpyinfo->mouse_face_mouse_frame)
23605 {
23606 int x = dpyinfo->mouse_face_mouse_x;
23607 int y = dpyinfo->mouse_face_mouse_y;
23608 clear_mouse_face (dpyinfo);
23609 note_mouse_highlight (f, x, y);
23610 }
23611 }
23612 }
23613
23614
23615 /* EXPORT:
23616 Determine the intersection of two rectangles R1 and R2. Return
23617 the intersection in *RESULT. Value is non-zero if RESULT is not
23618 empty. */
23619
23620 int
23621 x_intersect_rectangles (r1, r2, result)
23622 XRectangle *r1, *r2, *result;
23623 {
23624 XRectangle *left, *right;
23625 XRectangle *upper, *lower;
23626 int intersection_p = 0;
23627
23628 /* Rearrange so that R1 is the left-most rectangle. */
23629 if (r1->x < r2->x)
23630 left = r1, right = r2;
23631 else
23632 left = r2, right = r1;
23633
23634 /* X0 of the intersection is right.x0, if this is inside R1,
23635 otherwise there is no intersection. */
23636 if (right->x <= left->x + left->width)
23637 {
23638 result->x = right->x;
23639
23640 /* The right end of the intersection is the minimum of the
23641 the right ends of left and right. */
23642 result->width = (min (left->x + left->width, right->x + right->width)
23643 - result->x);
23644
23645 /* Same game for Y. */
23646 if (r1->y < r2->y)
23647 upper = r1, lower = r2;
23648 else
23649 upper = r2, lower = r1;
23650
23651 /* The upper end of the intersection is lower.y0, if this is inside
23652 of upper. Otherwise, there is no intersection. */
23653 if (lower->y <= upper->y + upper->height)
23654 {
23655 result->y = lower->y;
23656
23657 /* The lower end of the intersection is the minimum of the lower
23658 ends of upper and lower. */
23659 result->height = (min (lower->y + lower->height,
23660 upper->y + upper->height)
23661 - result->y);
23662 intersection_p = 1;
23663 }
23664 }
23665
23666 return intersection_p;
23667 }
23668
23669 #endif /* HAVE_WINDOW_SYSTEM */
23670
23671 \f
23672 /***********************************************************************
23673 Initialization
23674 ***********************************************************************/
23675
23676 void
23677 syms_of_xdisp ()
23678 {
23679 Vwith_echo_area_save_vector = Qnil;
23680 staticpro (&Vwith_echo_area_save_vector);
23681
23682 Vmessage_stack = Qnil;
23683 staticpro (&Vmessage_stack);
23684
23685 Qinhibit_redisplay = intern ("inhibit-redisplay");
23686 staticpro (&Qinhibit_redisplay);
23687
23688 message_dolog_marker1 = Fmake_marker ();
23689 staticpro (&message_dolog_marker1);
23690 message_dolog_marker2 = Fmake_marker ();
23691 staticpro (&message_dolog_marker2);
23692 message_dolog_marker3 = Fmake_marker ();
23693 staticpro (&message_dolog_marker3);
23694
23695 #if GLYPH_DEBUG
23696 defsubr (&Sdump_frame_glyph_matrix);
23697 defsubr (&Sdump_glyph_matrix);
23698 defsubr (&Sdump_glyph_row);
23699 defsubr (&Sdump_tool_bar_row);
23700 defsubr (&Strace_redisplay);
23701 defsubr (&Strace_to_stderr);
23702 #endif
23703 #ifdef HAVE_WINDOW_SYSTEM
23704 defsubr (&Stool_bar_lines_needed);
23705 defsubr (&Slookup_image_map);
23706 #endif
23707 defsubr (&Sformat_mode_line);
23708
23709 staticpro (&Qmenu_bar_update_hook);
23710 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23711
23712 staticpro (&Qoverriding_terminal_local_map);
23713 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23714
23715 staticpro (&Qoverriding_local_map);
23716 Qoverriding_local_map = intern ("overriding-local-map");
23717
23718 staticpro (&Qwindow_scroll_functions);
23719 Qwindow_scroll_functions = intern ("window-scroll-functions");
23720
23721 staticpro (&Qredisplay_end_trigger_functions);
23722 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23723
23724 staticpro (&Qinhibit_point_motion_hooks);
23725 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23726
23727 QCdata = intern (":data");
23728 staticpro (&QCdata);
23729 Qdisplay = intern ("display");
23730 staticpro (&Qdisplay);
23731 Qspace_width = intern ("space-width");
23732 staticpro (&Qspace_width);
23733 Qraise = intern ("raise");
23734 staticpro (&Qraise);
23735 Qslice = intern ("slice");
23736 staticpro (&Qslice);
23737 Qspace = intern ("space");
23738 staticpro (&Qspace);
23739 Qmargin = intern ("margin");
23740 staticpro (&Qmargin);
23741 Qpointer = intern ("pointer");
23742 staticpro (&Qpointer);
23743 Qleft_margin = intern ("left-margin");
23744 staticpro (&Qleft_margin);
23745 Qright_margin = intern ("right-margin");
23746 staticpro (&Qright_margin);
23747 Qcenter = intern ("center");
23748 staticpro (&Qcenter);
23749 Qline_height = intern ("line-height");
23750 staticpro (&Qline_height);
23751 QCalign_to = intern (":align-to");
23752 staticpro (&QCalign_to);
23753 QCrelative_width = intern (":relative-width");
23754 staticpro (&QCrelative_width);
23755 QCrelative_height = intern (":relative-height");
23756 staticpro (&QCrelative_height);
23757 QCeval = intern (":eval");
23758 staticpro (&QCeval);
23759 QCpropertize = intern (":propertize");
23760 staticpro (&QCpropertize);
23761 QCfile = intern (":file");
23762 staticpro (&QCfile);
23763 Qfontified = intern ("fontified");
23764 staticpro (&Qfontified);
23765 Qfontification_functions = intern ("fontification-functions");
23766 staticpro (&Qfontification_functions);
23767 Qtrailing_whitespace = intern ("trailing-whitespace");
23768 staticpro (&Qtrailing_whitespace);
23769 Qescape_glyph = intern ("escape-glyph");
23770 staticpro (&Qescape_glyph);
23771 Qnobreak_space = intern ("nobreak-space");
23772 staticpro (&Qnobreak_space);
23773 Qimage = intern ("image");
23774 staticpro (&Qimage);
23775 QCmap = intern (":map");
23776 staticpro (&QCmap);
23777 QCpointer = intern (":pointer");
23778 staticpro (&QCpointer);
23779 Qrect = intern ("rect");
23780 staticpro (&Qrect);
23781 Qcircle = intern ("circle");
23782 staticpro (&Qcircle);
23783 Qpoly = intern ("poly");
23784 staticpro (&Qpoly);
23785 Qmessage_truncate_lines = intern ("message-truncate-lines");
23786 staticpro (&Qmessage_truncate_lines);
23787 Qgrow_only = intern ("grow-only");
23788 staticpro (&Qgrow_only);
23789 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23790 staticpro (&Qinhibit_menubar_update);
23791 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23792 staticpro (&Qinhibit_eval_during_redisplay);
23793 Qposition = intern ("position");
23794 staticpro (&Qposition);
23795 Qbuffer_position = intern ("buffer-position");
23796 staticpro (&Qbuffer_position);
23797 Qobject = intern ("object");
23798 staticpro (&Qobject);
23799 Qbar = intern ("bar");
23800 staticpro (&Qbar);
23801 Qhbar = intern ("hbar");
23802 staticpro (&Qhbar);
23803 Qbox = intern ("box");
23804 staticpro (&Qbox);
23805 Qhollow = intern ("hollow");
23806 staticpro (&Qhollow);
23807 Qhand = intern ("hand");
23808 staticpro (&Qhand);
23809 Qarrow = intern ("arrow");
23810 staticpro (&Qarrow);
23811 Qtext = intern ("text");
23812 staticpro (&Qtext);
23813 Qrisky_local_variable = intern ("risky-local-variable");
23814 staticpro (&Qrisky_local_variable);
23815 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23816 staticpro (&Qinhibit_free_realized_faces);
23817
23818 list_of_error = Fcons (Fcons (intern ("error"),
23819 Fcons (intern ("void-variable"), Qnil)),
23820 Qnil);
23821 staticpro (&list_of_error);
23822
23823 Qlast_arrow_position = intern ("last-arrow-position");
23824 staticpro (&Qlast_arrow_position);
23825 Qlast_arrow_string = intern ("last-arrow-string");
23826 staticpro (&Qlast_arrow_string);
23827
23828 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23829 staticpro (&Qoverlay_arrow_string);
23830 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23831 staticpro (&Qoverlay_arrow_bitmap);
23832
23833 echo_buffer[0] = echo_buffer[1] = Qnil;
23834 staticpro (&echo_buffer[0]);
23835 staticpro (&echo_buffer[1]);
23836
23837 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23838 staticpro (&echo_area_buffer[0]);
23839 staticpro (&echo_area_buffer[1]);
23840
23841 Vmessages_buffer_name = build_string ("*Messages*");
23842 staticpro (&Vmessages_buffer_name);
23843
23844 mode_line_proptrans_alist = Qnil;
23845 staticpro (&mode_line_proptrans_alist);
23846 mode_line_string_list = Qnil;
23847 staticpro (&mode_line_string_list);
23848 mode_line_string_face = Qnil;
23849 staticpro (&mode_line_string_face);
23850 mode_line_string_face_prop = Qnil;
23851 staticpro (&mode_line_string_face_prop);
23852 Vmode_line_unwind_vector = Qnil;
23853 staticpro (&Vmode_line_unwind_vector);
23854
23855 help_echo_string = Qnil;
23856 staticpro (&help_echo_string);
23857 help_echo_object = Qnil;
23858 staticpro (&help_echo_object);
23859 help_echo_window = Qnil;
23860 staticpro (&help_echo_window);
23861 previous_help_echo_string = Qnil;
23862 staticpro (&previous_help_echo_string);
23863 help_echo_pos = -1;
23864
23865 #ifdef HAVE_WINDOW_SYSTEM
23866 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23867 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23868 For example, if a block cursor is over a tab, it will be drawn as
23869 wide as that tab on the display. */);
23870 x_stretch_cursor_p = 0;
23871 #endif
23872
23873 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23874 doc: /* *Non-nil means highlight trailing whitespace.
23875 The face used for trailing whitespace is `trailing-whitespace'. */);
23876 Vshow_trailing_whitespace = Qnil;
23877
23878 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23879 doc: /* *Control highlighting of nobreak space and soft hyphen.
23880 A value of t means highlight the character itself (for nobreak space,
23881 use face `nobreak-space').
23882 A value of nil means no highlighting.
23883 Other values mean display the escape glyph followed by an ordinary
23884 space or ordinary hyphen. */);
23885 Vnobreak_char_display = Qt;
23886
23887 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23888 doc: /* *The pointer shape to show in void text areas.
23889 A value of nil means to show the text pointer. Other options are `arrow',
23890 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23891 Vvoid_text_area_pointer = Qarrow;
23892
23893 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23894 doc: /* Non-nil means don't actually do any redisplay.
23895 This is used for internal purposes. */);
23896 Vinhibit_redisplay = Qnil;
23897
23898 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23899 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23900 Vglobal_mode_string = Qnil;
23901
23902 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23903 doc: /* Marker for where to display an arrow on top of the buffer text.
23904 This must be the beginning of a line in order to work.
23905 See also `overlay-arrow-string'. */);
23906 Voverlay_arrow_position = Qnil;
23907
23908 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23909 doc: /* String to display as an arrow in non-window frames.
23910 See also `overlay-arrow-position'. */);
23911 Voverlay_arrow_string = build_string ("=>");
23912
23913 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23914 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23915 The symbols on this list are examined during redisplay to determine
23916 where to display overlay arrows. */);
23917 Voverlay_arrow_variable_list
23918 = Fcons (intern ("overlay-arrow-position"), Qnil);
23919
23920 DEFVAR_INT ("scroll-step", &scroll_step,
23921 doc: /* *The number of lines to try scrolling a window by when point moves out.
23922 If that fails to bring point back on frame, point is centered instead.
23923 If this is zero, point is always centered after it moves off frame.
23924 If you want scrolling to always be a line at a time, you should set
23925 `scroll-conservatively' to a large value rather than set this to 1. */);
23926
23927 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23928 doc: /* *Scroll up to this many lines, to bring point back on screen.
23929 A value of zero means to scroll the text to center point vertically
23930 in the window. */);
23931 scroll_conservatively = 0;
23932
23933 DEFVAR_INT ("scroll-margin", &scroll_margin,
23934 doc: /* *Number of lines of margin at the top and bottom of a window.
23935 Recenter the window whenever point gets within this many lines
23936 of the top or bottom of the window. */);
23937 scroll_margin = 0;
23938
23939 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23940 doc: /* Pixels per inch value for non-window system displays.
23941 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23942 Vdisplay_pixels_per_inch = make_float (72.0);
23943
23944 #if GLYPH_DEBUG
23945 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23946 #endif
23947
23948 DEFVAR_BOOL ("truncate-partial-width-windows",
23949 &truncate_partial_width_windows,
23950 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23951 truncate_partial_width_windows = 1;
23952
23953 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23954 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23955 Any other value means to use the appropriate face, `mode-line',
23956 `header-line', or `menu' respectively. */);
23957 mode_line_inverse_video = 1;
23958
23959 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23960 doc: /* *Maximum buffer size for which line number should be displayed.
23961 If the buffer is bigger than this, the line number does not appear
23962 in the mode line. A value of nil means no limit. */);
23963 Vline_number_display_limit = Qnil;
23964
23965 DEFVAR_INT ("line-number-display-limit-width",
23966 &line_number_display_limit_width,
23967 doc: /* *Maximum line width (in characters) for line number display.
23968 If the average length of the lines near point is bigger than this, then the
23969 line number may be omitted from the mode line. */);
23970 line_number_display_limit_width = 200;
23971
23972 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23973 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23974 highlight_nonselected_windows = 0;
23975
23976 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23977 doc: /* Non-nil if more than one frame is visible on this display.
23978 Minibuffer-only frames don't count, but iconified frames do.
23979 This variable is not guaranteed to be accurate except while processing
23980 `frame-title-format' and `icon-title-format'. */);
23981
23982 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23983 doc: /* Template for displaying the title bar of visible frames.
23984 \(Assuming the window manager supports this feature.)
23985 This variable has the same structure as `mode-line-format' (which see),
23986 and is used only on frames for which no explicit name has been set
23987 \(see `modify-frame-parameters'). */);
23988
23989 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23990 doc: /* Template for displaying the title bar of an iconified frame.
23991 \(Assuming the window manager supports this feature.)
23992 This variable has the same structure as `mode-line-format' (which see),
23993 and is used only on frames for which no explicit name has been set
23994 \(see `modify-frame-parameters'). */);
23995 Vicon_title_format
23996 = Vframe_title_format
23997 = Fcons (intern ("multiple-frames"),
23998 Fcons (build_string ("%b"),
23999 Fcons (Fcons (empty_string,
24000 Fcons (intern ("invocation-name"),
24001 Fcons (build_string ("@"),
24002 Fcons (intern ("system-name"),
24003 Qnil)))),
24004 Qnil)));
24005
24006 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
24007 doc: /* Maximum number of lines to keep in the message log buffer.
24008 If nil, disable message logging. If t, log messages but don't truncate
24009 the buffer when it becomes large. */);
24010 Vmessage_log_max = make_number (50);
24011
24012 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
24013 doc: /* Functions called before redisplay, if window sizes have changed.
24014 The value should be a list of functions that take one argument.
24015 Just before redisplay, for each frame, if any of its windows have changed
24016 size since the last redisplay, or have been split or deleted,
24017 all the functions in the list are called, with the frame as argument. */);
24018 Vwindow_size_change_functions = Qnil;
24019
24020 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
24021 doc: /* List of functions to call before redisplaying a window with scrolling.
24022 Each function is called with two arguments, the window
24023 and its new display-start position. Note that the value of `window-end'
24024 is not valid when these functions are called. */);
24025 Vwindow_scroll_functions = Qnil;
24026
24027 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
24028 doc: /* Functions called when redisplay of a window reaches the end trigger.
24029 Each function is called with two arguments, the window and the end trigger value.
24030 See `set-window-redisplay-end-trigger'. */);
24031 Vredisplay_end_trigger_functions = Qnil;
24032
24033 DEFVAR_LISP ("mouse-autoselect-window", &Vmouse_autoselect_window,
24034 doc: /* *Non-nil means autoselect window with mouse pointer.
24035 If nil, do not autoselect windows.
24036 A positive number means delay autoselection by that many seconds: a
24037 window is autoselected only after the mouse has remained in that
24038 window for the duration of the delay.
24039 A negative number has a similar effect, but causes windows to be
24040 autoselected only after the mouse has stopped moving. \(Because of
24041 the way Emacs compares mouse events, you will occasionally wait twice
24042 that time before the window gets selected.\)
24043 Any other value means to autoselect window instantaneously when the
24044 mouse pointer enters it.
24045
24046 Autoselection selects the minibuffer only if it is active, and never
24047 unselects the minibuffer if it is active. */);
24048 Vmouse_autoselect_window = Qnil;
24049
24050 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
24051 doc: /* *Non-nil means automatically resize tool-bars.
24052 This increases a tool-bar's height if not all tool-bar items are visible.
24053 It decreases a tool-bar's height when it would display blank lines
24054 otherwise. */);
24055 auto_resize_tool_bars_p = 1;
24056
24057 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
24058 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
24059 auto_raise_tool_bar_buttons_p = 1;
24060
24061 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
24062 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
24063 make_cursor_line_fully_visible_p = 1;
24064
24065 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
24066 doc: /* *Border below tool-bar in pixels.
24067 If an integer, use it as the height of the border.
24068 If it is one of `internal-border-width' or `border-width', use the
24069 value of the corresponding frame parameter.
24070 Otherwise, no border is added below the tool-bar. */);
24071 Vtool_bar_border = Qinternal_border_width;
24072
24073 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
24074 doc: /* *Margin around tool-bar buttons in pixels.
24075 If an integer, use that for both horizontal and vertical margins.
24076 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24077 HORZ specifying the horizontal margin, and VERT specifying the
24078 vertical margin. */);
24079 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24080
24081 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24082 doc: /* *Relief thickness of tool-bar buttons. */);
24083 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24084
24085 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24086 doc: /* List of functions to call to fontify regions of text.
24087 Each function is called with one argument POS. Functions must
24088 fontify a region starting at POS in the current buffer, and give
24089 fontified regions the property `fontified'. */);
24090 Vfontification_functions = Qnil;
24091 Fmake_variable_buffer_local (Qfontification_functions);
24092
24093 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24094 &unibyte_display_via_language_environment,
24095 doc: /* *Non-nil means display unibyte text according to language environment.
24096 Specifically this means that unibyte non-ASCII characters
24097 are displayed by converting them to the equivalent multibyte characters
24098 according to the current language environment. As a result, they are
24099 displayed according to the current fontset. */);
24100 unibyte_display_via_language_environment = 0;
24101
24102 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24103 doc: /* *Maximum height for resizing mini-windows.
24104 If a float, it specifies a fraction of the mini-window frame's height.
24105 If an integer, it specifies a number of lines. */);
24106 Vmax_mini_window_height = make_float (0.25);
24107
24108 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24109 doc: /* *How to resize mini-windows.
24110 A value of nil means don't automatically resize mini-windows.
24111 A value of t means resize them to fit the text displayed in them.
24112 A value of `grow-only', the default, means let mini-windows grow
24113 only, until their display becomes empty, at which point the windows
24114 go back to their normal size. */);
24115 Vresize_mini_windows = Qgrow_only;
24116
24117 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24118 doc: /* Alist specifying how to blink the cursor off.
24119 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24120 `cursor-type' frame-parameter or variable equals ON-STATE,
24121 comparing using `equal', Emacs uses OFF-STATE to specify
24122 how to blink it off. ON-STATE and OFF-STATE are values for
24123 the `cursor-type' frame parameter.
24124
24125 If a frame's ON-STATE has no entry in this list,
24126 the frame's other specifications determine how to blink the cursor off. */);
24127 Vblink_cursor_alist = Qnil;
24128
24129 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24130 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24131 automatic_hscrolling_p = 1;
24132
24133 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24134 doc: /* *How many columns away from the window edge point is allowed to get
24135 before automatic hscrolling will horizontally scroll the window. */);
24136 hscroll_margin = 5;
24137
24138 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24139 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24140 When point is less than `hscroll-margin' columns from the window
24141 edge, automatic hscrolling will scroll the window by the amount of columns
24142 determined by this variable. If its value is a positive integer, scroll that
24143 many columns. If it's a positive floating-point number, it specifies the
24144 fraction of the window's width to scroll. If it's nil or zero, point will be
24145 centered horizontally after the scroll. Any other value, including negative
24146 numbers, are treated as if the value were zero.
24147
24148 Automatic hscrolling always moves point outside the scroll margin, so if
24149 point was more than scroll step columns inside the margin, the window will
24150 scroll more than the value given by the scroll step.
24151
24152 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24153 and `scroll-right' overrides this variable's effect. */);
24154 Vhscroll_step = make_number (0);
24155
24156 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24157 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24158 Bind this around calls to `message' to let it take effect. */);
24159 message_truncate_lines = 0;
24160
24161 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24162 doc: /* Normal hook run to update the menu bar definitions.
24163 Redisplay runs this hook before it redisplays the menu bar.
24164 This is used to update submenus such as Buffers,
24165 whose contents depend on various data. */);
24166 Vmenu_bar_update_hook = Qnil;
24167
24168 DEFVAR_LISP ("menu-updating-frame", &Vmenu_updating_frame,
24169 doc: /* Frame for which we are updating a menu.
24170 The enable predicate for a menu binding should check this variable. */);
24171 Vmenu_updating_frame = Qnil;
24172
24173 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24174 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24175 inhibit_menubar_update = 0;
24176
24177 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24178 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24179 inhibit_eval_during_redisplay = 0;
24180
24181 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24182 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24183 inhibit_free_realized_faces = 0;
24184
24185 #if GLYPH_DEBUG
24186 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24187 doc: /* Inhibit try_window_id display optimization. */);
24188 inhibit_try_window_id = 0;
24189
24190 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24191 doc: /* Inhibit try_window_reusing display optimization. */);
24192 inhibit_try_window_reusing = 0;
24193
24194 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24195 doc: /* Inhibit try_cursor_movement display optimization. */);
24196 inhibit_try_cursor_movement = 0;
24197 #endif /* GLYPH_DEBUG */
24198
24199 DEFVAR_INT ("overline-margin", &overline_margin,
24200 doc: /* *Space between overline and text, in pixels.
24201 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24202 margin to the caracter height. */);
24203 overline_margin = 2;
24204 }
24205
24206
24207 /* Initialize this module when Emacs starts. */
24208
24209 void
24210 init_xdisp ()
24211 {
24212 Lisp_Object root_window;
24213 struct window *mini_w;
24214
24215 current_header_line_height = current_mode_line_height = -1;
24216
24217 CHARPOS (this_line_start_pos) = 0;
24218
24219 mini_w = XWINDOW (minibuf_window);
24220 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24221
24222 if (!noninteractive)
24223 {
24224 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24225 int i;
24226
24227 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24228 set_window_height (root_window,
24229 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24230 0);
24231 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24232 set_window_height (minibuf_window, 1, 0);
24233
24234 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24235 mini_w->total_cols = make_number (FRAME_COLS (f));
24236
24237 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24238 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24239 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24240
24241 /* The default ellipsis glyphs `...'. */
24242 for (i = 0; i < 3; ++i)
24243 default_invis_vector[i] = make_number ('.');
24244 }
24245
24246 {
24247 /* Allocate the buffer for frame titles.
24248 Also used for `format-mode-line'. */
24249 int size = 100;
24250 mode_line_noprop_buf = (char *) xmalloc (size);
24251 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24252 mode_line_noprop_ptr = mode_line_noprop_buf;
24253 mode_line_target = MODE_LINE_DISPLAY;
24254 }
24255
24256 help_echo_showing_p = 0;
24257 }
24258
24259
24260 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24261 (do not change this comment) */