]> code.delx.au - gnu-emacs/blob - src/xdisp.c
(Idle Timers): run-with-idle-timer allows Lisp time value. Add xref.
[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-zero means automatically select any window when the mouse
260 cursor moves into it. */
261 int mouse_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 /* Maximum height for resizing mini-windows. Either a float
619 specifying a fraction of the available height, or an integer
620 specifying a number of lines. */
621
622 Lisp_Object Vmax_mini_window_height;
623
624 /* Non-zero means messages should be displayed with truncated
625 lines instead of being continued. */
626
627 int message_truncate_lines;
628 Lisp_Object Qmessage_truncate_lines;
629
630 /* Set to 1 in clear_message to make redisplay_internal aware
631 of an emptied echo area. */
632
633 static int message_cleared_p;
634
635 /* How to blink the default frame cursor off. */
636 Lisp_Object Vblink_cursor_alist;
637
638 /* A scratch glyph row with contents used for generating truncation
639 glyphs. Also used in direct_output_for_insert. */
640
641 #define MAX_SCRATCH_GLYPHS 100
642 struct glyph_row scratch_glyph_row;
643 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
644
645 /* Ascent and height of the last line processed by move_it_to. */
646
647 static int last_max_ascent, last_height;
648
649 /* Non-zero if there's a help-echo in the echo area. */
650
651 int help_echo_showing_p;
652
653 /* If >= 0, computed, exact values of mode-line and header-line height
654 to use in the macros CURRENT_MODE_LINE_HEIGHT and
655 CURRENT_HEADER_LINE_HEIGHT. */
656
657 int current_mode_line_height, current_header_line_height;
658
659 /* The maximum distance to look ahead for text properties. Values
660 that are too small let us call compute_char_face and similar
661 functions too often which is expensive. Values that are too large
662 let us call compute_char_face and alike too often because we
663 might not be interested in text properties that far away. */
664
665 #define TEXT_PROP_DISTANCE_LIMIT 100
666
667 #if GLYPH_DEBUG
668
669 /* Variables to turn off display optimizations from Lisp. */
670
671 int inhibit_try_window_id, inhibit_try_window_reusing;
672 int inhibit_try_cursor_movement;
673
674 /* Non-zero means print traces of redisplay if compiled with
675 GLYPH_DEBUG != 0. */
676
677 int trace_redisplay_p;
678
679 #endif /* GLYPH_DEBUG */
680
681 #ifdef DEBUG_TRACE_MOVE
682 /* Non-zero means trace with TRACE_MOVE to stderr. */
683 int trace_move;
684
685 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
686 #else
687 #define TRACE_MOVE(x) (void) 0
688 #endif
689
690 /* Non-zero means automatically scroll windows horizontally to make
691 point visible. */
692
693 int automatic_hscrolling_p;
694
695 /* How close to the margin can point get before the window is scrolled
696 horizontally. */
697 EMACS_INT hscroll_margin;
698
699 /* How much to scroll horizontally when point is inside the above margin. */
700 Lisp_Object Vhscroll_step;
701
702 /* The variable `resize-mini-windows'. If nil, don't resize
703 mini-windows. If t, always resize them to fit the text they
704 display. If `grow-only', let mini-windows grow only until they
705 become empty. */
706
707 Lisp_Object Vresize_mini_windows;
708
709 /* Buffer being redisplayed -- for redisplay_window_error. */
710
711 struct buffer *displayed_buffer;
712
713 /* Space between overline and text. */
714
715 EMACS_INT overline_margin;
716
717 /* Value returned from text property handlers (see below). */
718
719 enum prop_handled
720 {
721 HANDLED_NORMALLY,
722 HANDLED_RECOMPUTE_PROPS,
723 HANDLED_OVERLAY_STRING_CONSUMED,
724 HANDLED_RETURN
725 };
726
727 /* A description of text properties that redisplay is interested
728 in. */
729
730 struct props
731 {
732 /* The name of the property. */
733 Lisp_Object *name;
734
735 /* A unique index for the property. */
736 enum prop_idx idx;
737
738 /* A handler function called to set up iterator IT from the property
739 at IT's current position. Value is used to steer handle_stop. */
740 enum prop_handled (*handler) P_ ((struct it *it));
741 };
742
743 static enum prop_handled handle_face_prop P_ ((struct it *));
744 static enum prop_handled handle_invisible_prop P_ ((struct it *));
745 static enum prop_handled handle_display_prop P_ ((struct it *));
746 static enum prop_handled handle_composition_prop P_ ((struct it *));
747 static enum prop_handled handle_overlay_change P_ ((struct it *));
748 static enum prop_handled handle_fontified_prop P_ ((struct it *));
749
750 /* Properties handled by iterators. */
751
752 static struct props it_props[] =
753 {
754 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
755 /* Handle `face' before `display' because some sub-properties of
756 `display' need to know the face. */
757 {&Qface, FACE_PROP_IDX, handle_face_prop},
758 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
759 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
760 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
761 {NULL, 0, NULL}
762 };
763
764 /* Value is the position described by X. If X is a marker, value is
765 the marker_position of X. Otherwise, value is X. */
766
767 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
768
769 /* Enumeration returned by some move_it_.* functions internally. */
770
771 enum move_it_result
772 {
773 /* Not used. Undefined value. */
774 MOVE_UNDEFINED,
775
776 /* Move ended at the requested buffer position or ZV. */
777 MOVE_POS_MATCH_OR_ZV,
778
779 /* Move ended at the requested X pixel position. */
780 MOVE_X_REACHED,
781
782 /* Move within a line ended at the end of a line that must be
783 continued. */
784 MOVE_LINE_CONTINUED,
785
786 /* Move within a line ended at the end of a line that would
787 be displayed truncated. */
788 MOVE_LINE_TRUNCATED,
789
790 /* Move within a line ended at a line end. */
791 MOVE_NEWLINE_OR_CR
792 };
793
794 /* This counter is used to clear the face cache every once in a while
795 in redisplay_internal. It is incremented for each redisplay.
796 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
797 cleared. */
798
799 #define CLEAR_FACE_CACHE_COUNT 500
800 static int clear_face_cache_count;
801
802 /* Similarly for the image cache. */
803
804 #ifdef HAVE_WINDOW_SYSTEM
805 #define CLEAR_IMAGE_CACHE_COUNT 101
806 static int clear_image_cache_count;
807 #endif
808
809 /* Record the previous terminal frame we displayed. */
810
811 static struct frame *previous_terminal_frame;
812
813 /* Non-zero while redisplay_internal is in progress. */
814
815 int redisplaying_p;
816
817 /* Non-zero means don't free realized faces. Bound while freeing
818 realized faces is dangerous because glyph matrices might still
819 reference them. */
820
821 int inhibit_free_realized_faces;
822 Lisp_Object Qinhibit_free_realized_faces;
823
824 /* If a string, XTread_socket generates an event to display that string.
825 (The display is done in read_char.) */
826
827 Lisp_Object help_echo_string;
828 Lisp_Object help_echo_window;
829 Lisp_Object help_echo_object;
830 int help_echo_pos;
831
832 /* Temporary variable for XTread_socket. */
833
834 Lisp_Object previous_help_echo_string;
835
836 /* Null glyph slice */
837
838 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
839
840 \f
841 /* Function prototypes. */
842
843 static void setup_for_ellipsis P_ ((struct it *, int));
844 static void mark_window_display_accurate_1 P_ ((struct window *, int));
845 static int single_display_spec_string_p P_ ((Lisp_Object, Lisp_Object));
846 static int display_prop_string_p P_ ((Lisp_Object, Lisp_Object));
847 static int cursor_row_p P_ ((struct window *, struct glyph_row *));
848 static int redisplay_mode_lines P_ ((Lisp_Object, int));
849 static char *decode_mode_spec_coding P_ ((Lisp_Object, char *, int));
850
851 #if 0
852 static int invisible_text_between_p P_ ((struct it *, int, int));
853 #endif
854
855 static void pint2str P_ ((char *, int, int));
856 static void pint2hrstr P_ ((char *, int, int));
857 static struct text_pos run_window_scroll_functions P_ ((Lisp_Object,
858 struct text_pos));
859 static void reconsider_clip_changes P_ ((struct window *, struct buffer *));
860 static int text_outside_line_unchanged_p P_ ((struct window *, int, int));
861 static void store_mode_line_noprop_char P_ ((char));
862 static int store_mode_line_noprop P_ ((const unsigned char *, int, int));
863 static void x_consider_frame_title P_ ((Lisp_Object));
864 static void handle_stop P_ ((struct it *));
865 static int tool_bar_lines_needed P_ ((struct frame *, int *));
866 static int single_display_spec_intangible_p P_ ((Lisp_Object));
867 static void ensure_echo_area_buffers P_ ((void));
868 static Lisp_Object unwind_with_echo_area_buffer P_ ((Lisp_Object));
869 static Lisp_Object with_echo_area_buffer_unwind_data P_ ((struct window *));
870 static int with_echo_area_buffer P_ ((struct window *, int,
871 int (*) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
872 EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
873 static void clear_garbaged_frames P_ ((void));
874 static int current_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
875 static int truncate_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
876 static int set_message_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
877 static int display_echo_area P_ ((struct window *));
878 static int display_echo_area_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
879 static int resize_mini_window_1 P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
880 static Lisp_Object unwind_redisplay P_ ((Lisp_Object));
881 static int string_char_and_length P_ ((const unsigned char *, int, int *));
882 static struct text_pos display_prop_end P_ ((struct it *, Lisp_Object,
883 struct text_pos));
884 static int compute_window_start_on_continuation_line P_ ((struct window *));
885 static Lisp_Object safe_eval_handler P_ ((Lisp_Object));
886 static void insert_left_trunc_glyphs P_ ((struct it *));
887 static struct glyph_row *get_overlay_arrow_glyph_row P_ ((struct window *,
888 Lisp_Object));
889 static void extend_face_to_end_of_line P_ ((struct it *));
890 static int append_space_for_newline P_ ((struct it *, int));
891 static int cursor_row_fully_visible_p P_ ((struct window *, int, int));
892 static int try_scrolling P_ ((Lisp_Object, int, EMACS_INT, EMACS_INT, int, int));
893 static int try_cursor_movement P_ ((Lisp_Object, struct text_pos, int *));
894 static int trailing_whitespace_p P_ ((int));
895 static int message_log_check_duplicate P_ ((int, int, int, int));
896 static void push_it P_ ((struct it *));
897 static void pop_it P_ ((struct it *));
898 static void sync_frame_with_window_matrix_rows P_ ((struct window *));
899 static void select_frame_for_redisplay P_ ((Lisp_Object));
900 static void redisplay_internal P_ ((int));
901 static int echo_area_display P_ ((int));
902 static void redisplay_windows P_ ((Lisp_Object));
903 static void redisplay_window P_ ((Lisp_Object, int));
904 static Lisp_Object redisplay_window_error ();
905 static Lisp_Object redisplay_window_0 P_ ((Lisp_Object));
906 static Lisp_Object redisplay_window_1 P_ ((Lisp_Object));
907 static int update_menu_bar P_ ((struct frame *, int, int));
908 static int try_window_reusing_current_matrix P_ ((struct window *));
909 static int try_window_id P_ ((struct window *));
910 static int display_line P_ ((struct it *));
911 static int display_mode_lines P_ ((struct window *));
912 static int display_mode_line P_ ((struct window *, enum face_id, Lisp_Object));
913 static int display_mode_element P_ ((struct it *, int, int, int, Lisp_Object, Lisp_Object, int));
914 static int store_mode_line_string P_ ((char *, Lisp_Object, int, int, int, Lisp_Object));
915 static char *decode_mode_spec P_ ((struct window *, int, int, int, int *));
916 static void display_menu_bar P_ ((struct window *));
917 static int display_count_lines P_ ((int, int, int, int, int *));
918 static int display_string P_ ((unsigned char *, Lisp_Object, Lisp_Object,
919 int, int, struct it *, int, int, int, int));
920 static void compute_line_metrics P_ ((struct it *));
921 static void run_redisplay_end_trigger_hook P_ ((struct it *));
922 static int get_overlay_strings P_ ((struct it *, int));
923 static int get_overlay_strings_1 P_ ((struct it *, int, int));
924 static void next_overlay_string P_ ((struct it *));
925 static void reseat P_ ((struct it *, struct text_pos, int));
926 static void reseat_1 P_ ((struct it *, struct text_pos, int));
927 static void back_to_previous_visible_line_start P_ ((struct it *));
928 void reseat_at_previous_visible_line_start P_ ((struct it *));
929 static void reseat_at_next_visible_line_start P_ ((struct it *, int));
930 static int next_element_from_ellipsis P_ ((struct it *));
931 static int next_element_from_display_vector P_ ((struct it *));
932 static int next_element_from_string P_ ((struct it *));
933 static int next_element_from_c_string P_ ((struct it *));
934 static int next_element_from_buffer P_ ((struct it *));
935 static int next_element_from_composition P_ ((struct it *));
936 static int next_element_from_image P_ ((struct it *));
937 static int next_element_from_stretch P_ ((struct it *));
938 static void load_overlay_strings P_ ((struct it *, int));
939 static int init_from_display_pos P_ ((struct it *, struct window *,
940 struct display_pos *));
941 static void reseat_to_string P_ ((struct it *, unsigned char *,
942 Lisp_Object, int, int, int, int));
943 static enum move_it_result move_it_in_display_line_to P_ ((struct it *,
944 int, int, int));
945 void move_it_vertically_backward P_ ((struct it *, int));
946 static void init_to_row_start P_ ((struct it *, struct window *,
947 struct glyph_row *));
948 static int init_to_row_end P_ ((struct it *, struct window *,
949 struct glyph_row *));
950 static void back_to_previous_line_start P_ ((struct it *));
951 static int forward_to_next_line_start P_ ((struct it *, int *));
952 static struct text_pos string_pos_nchars_ahead P_ ((struct text_pos,
953 Lisp_Object, int));
954 static struct text_pos string_pos P_ ((int, Lisp_Object));
955 static struct text_pos c_string_pos P_ ((int, unsigned char *, int));
956 static int number_of_chars P_ ((unsigned char *, int));
957 static void compute_stop_pos P_ ((struct it *));
958 static void compute_string_pos P_ ((struct text_pos *, struct text_pos,
959 Lisp_Object));
960 static int face_before_or_after_it_pos P_ ((struct it *, int));
961 static int next_overlay_change P_ ((int));
962 static int handle_single_display_spec P_ ((struct it *, Lisp_Object,
963 Lisp_Object, struct text_pos *,
964 int));
965 static int underlying_face_id P_ ((struct it *));
966 static int in_ellipses_for_invisible_text_p P_ ((struct display_pos *,
967 struct window *));
968
969 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
970 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
971
972 #ifdef HAVE_WINDOW_SYSTEM
973
974 static void update_tool_bar P_ ((struct frame *, int));
975 static void build_desired_tool_bar_string P_ ((struct frame *f));
976 static int redisplay_tool_bar P_ ((struct frame *));
977 static void display_tool_bar_line P_ ((struct it *, int));
978 static void notice_overwritten_cursor P_ ((struct window *,
979 enum glyph_row_area,
980 int, int, int, int));
981
982
983
984 #endif /* HAVE_WINDOW_SYSTEM */
985
986 \f
987 /***********************************************************************
988 Window display dimensions
989 ***********************************************************************/
990
991 /* Return the bottom boundary y-position for text lines in window W.
992 This is the first y position at which a line cannot start.
993 It is relative to the top of the window.
994
995 This is the height of W minus the height of a mode line, if any. */
996
997 INLINE int
998 window_text_bottom_y (w)
999 struct window *w;
1000 {
1001 int height = WINDOW_TOTAL_HEIGHT (w);
1002
1003 if (WINDOW_WANTS_MODELINE_P (w))
1004 height -= CURRENT_MODE_LINE_HEIGHT (w);
1005 return height;
1006 }
1007
1008 /* Return the pixel width of display area AREA of window W. AREA < 0
1009 means return the total width of W, not including fringes to
1010 the left and right of the window. */
1011
1012 INLINE int
1013 window_box_width (w, area)
1014 struct window *w;
1015 int area;
1016 {
1017 int cols = XFASTINT (w->total_cols);
1018 int pixels = 0;
1019
1020 if (!w->pseudo_window_p)
1021 {
1022 cols -= WINDOW_SCROLL_BAR_COLS (w);
1023
1024 if (area == TEXT_AREA)
1025 {
1026 if (INTEGERP (w->left_margin_cols))
1027 cols -= XFASTINT (w->left_margin_cols);
1028 if (INTEGERP (w->right_margin_cols))
1029 cols -= XFASTINT (w->right_margin_cols);
1030 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1031 }
1032 else if (area == LEFT_MARGIN_AREA)
1033 {
1034 cols = (INTEGERP (w->left_margin_cols)
1035 ? XFASTINT (w->left_margin_cols) : 0);
1036 pixels = 0;
1037 }
1038 else if (area == RIGHT_MARGIN_AREA)
1039 {
1040 cols = (INTEGERP (w->right_margin_cols)
1041 ? XFASTINT (w->right_margin_cols) : 0);
1042 pixels = 0;
1043 }
1044 }
1045
1046 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1047 }
1048
1049
1050 /* Return the pixel height of the display area of window W, not
1051 including mode lines of W, if any. */
1052
1053 INLINE int
1054 window_box_height (w)
1055 struct window *w;
1056 {
1057 struct frame *f = XFRAME (w->frame);
1058 int height = WINDOW_TOTAL_HEIGHT (w);
1059
1060 xassert (height >= 0);
1061
1062 /* Note: the code below that determines the mode-line/header-line
1063 height is essentially the same as that contained in the macro
1064 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1065 the appropriate glyph row has its `mode_line_p' flag set,
1066 and if it doesn't, uses estimate_mode_line_height instead. */
1067
1068 if (WINDOW_WANTS_MODELINE_P (w))
1069 {
1070 struct glyph_row *ml_row
1071 = (w->current_matrix && w->current_matrix->rows
1072 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1073 : 0);
1074 if (ml_row && ml_row->mode_line_p)
1075 height -= ml_row->height;
1076 else
1077 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1078 }
1079
1080 if (WINDOW_WANTS_HEADER_LINE_P (w))
1081 {
1082 struct glyph_row *hl_row
1083 = (w->current_matrix && w->current_matrix->rows
1084 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1085 : 0);
1086 if (hl_row && hl_row->mode_line_p)
1087 height -= hl_row->height;
1088 else
1089 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1090 }
1091
1092 /* With a very small font and a mode-line that's taller than
1093 default, we might end up with a negative height. */
1094 return max (0, height);
1095 }
1096
1097 /* Return the window-relative coordinate of the left edge of display
1098 area AREA of window W. AREA < 0 means return the left edge of the
1099 whole window, to the right of the left fringe of W. */
1100
1101 INLINE int
1102 window_box_left_offset (w, area)
1103 struct window *w;
1104 int area;
1105 {
1106 int x;
1107
1108 if (w->pseudo_window_p)
1109 return 0;
1110
1111 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1112
1113 if (area == TEXT_AREA)
1114 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1115 + window_box_width (w, LEFT_MARGIN_AREA));
1116 else if (area == RIGHT_MARGIN_AREA)
1117 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1118 + window_box_width (w, LEFT_MARGIN_AREA)
1119 + window_box_width (w, TEXT_AREA)
1120 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1121 ? 0
1122 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1123 else if (area == LEFT_MARGIN_AREA
1124 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1125 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1126
1127 return x;
1128 }
1129
1130
1131 /* Return the window-relative coordinate of the right edge of display
1132 area AREA of window W. AREA < 0 means return the left edge of the
1133 whole window, to the left of the right fringe of W. */
1134
1135 INLINE int
1136 window_box_right_offset (w, area)
1137 struct window *w;
1138 int area;
1139 {
1140 return window_box_left_offset (w, area) + window_box_width (w, area);
1141 }
1142
1143 /* Return the frame-relative coordinate of the left edge of display
1144 area AREA of window W. AREA < 0 means return the left edge of the
1145 whole window, to the right of the left fringe of W. */
1146
1147 INLINE int
1148 window_box_left (w, area)
1149 struct window *w;
1150 int area;
1151 {
1152 struct frame *f = XFRAME (w->frame);
1153 int x;
1154
1155 if (w->pseudo_window_p)
1156 return FRAME_INTERNAL_BORDER_WIDTH (f);
1157
1158 x = (WINDOW_LEFT_EDGE_X (w)
1159 + window_box_left_offset (w, area));
1160
1161 return x;
1162 }
1163
1164
1165 /* Return the frame-relative coordinate of the right edge of display
1166 area AREA of window W. AREA < 0 means return the left edge of the
1167 whole window, to the left of the right fringe of W. */
1168
1169 INLINE int
1170 window_box_right (w, area)
1171 struct window *w;
1172 int area;
1173 {
1174 return window_box_left (w, area) + window_box_width (w, area);
1175 }
1176
1177 /* Get the bounding box of the display area AREA of window W, without
1178 mode lines, in frame-relative coordinates. AREA < 0 means the
1179 whole window, not including the left and right fringes of
1180 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1181 coordinates of the upper-left corner of the box. Return in
1182 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1183
1184 INLINE void
1185 window_box (w, area, box_x, box_y, box_width, box_height)
1186 struct window *w;
1187 int area;
1188 int *box_x, *box_y, *box_width, *box_height;
1189 {
1190 if (box_width)
1191 *box_width = window_box_width (w, area);
1192 if (box_height)
1193 *box_height = window_box_height (w);
1194 if (box_x)
1195 *box_x = window_box_left (w, area);
1196 if (box_y)
1197 {
1198 *box_y = WINDOW_TOP_EDGE_Y (w);
1199 if (WINDOW_WANTS_HEADER_LINE_P (w))
1200 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1201 }
1202 }
1203
1204
1205 /* Get the bounding box of the display area AREA of window W, without
1206 mode lines. AREA < 0 means the whole window, not including the
1207 left and right fringe of the window. Return in *TOP_LEFT_X
1208 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1209 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1210 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1211 box. */
1212
1213 INLINE void
1214 window_box_edges (w, area, top_left_x, top_left_y,
1215 bottom_right_x, bottom_right_y)
1216 struct window *w;
1217 int area;
1218 int *top_left_x, *top_left_y, *bottom_right_x, *bottom_right_y;
1219 {
1220 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1221 bottom_right_y);
1222 *bottom_right_x += *top_left_x;
1223 *bottom_right_y += *top_left_y;
1224 }
1225
1226
1227 \f
1228 /***********************************************************************
1229 Utilities
1230 ***********************************************************************/
1231
1232 /* Return the bottom y-position of the line the iterator IT is in.
1233 This can modify IT's settings. */
1234
1235 int
1236 line_bottom_y (it)
1237 struct it *it;
1238 {
1239 int line_height = it->max_ascent + it->max_descent;
1240 int line_top_y = it->current_y;
1241
1242 if (line_height == 0)
1243 {
1244 if (last_height)
1245 line_height = last_height;
1246 else if (IT_CHARPOS (*it) < ZV)
1247 {
1248 move_it_by_lines (it, 1, 1);
1249 line_height = (it->max_ascent || it->max_descent
1250 ? it->max_ascent + it->max_descent
1251 : last_height);
1252 }
1253 else
1254 {
1255 struct glyph_row *row = it->glyph_row;
1256
1257 /* Use the default character height. */
1258 it->glyph_row = NULL;
1259 it->what = IT_CHARACTER;
1260 it->c = ' ';
1261 it->len = 1;
1262 PRODUCE_GLYPHS (it);
1263 line_height = it->ascent + it->descent;
1264 it->glyph_row = row;
1265 }
1266 }
1267
1268 return line_top_y + line_height;
1269 }
1270
1271
1272 /* Return 1 if position CHARPOS is visible in window W.
1273 If visible, set *X and *Y to pixel coordinates of top left corner.
1274 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1275 EXACT_MODE_LINE_HEIGHTS_P non-zero means compute exact mode-line
1276 and header-lines heights. */
1277
1278 int
1279 pos_visible_p (w, charpos, x, y, rtop, rbot, exact_mode_line_heights_p)
1280 struct window *w;
1281 int charpos, *x, *y, *rtop, *rbot, exact_mode_line_heights_p;
1282 {
1283 struct it it;
1284 struct text_pos top;
1285 int visible_p = 0;
1286 struct buffer *old_buffer = NULL;
1287
1288 if (noninteractive)
1289 return visible_p;
1290
1291 if (XBUFFER (w->buffer) != current_buffer)
1292 {
1293 old_buffer = current_buffer;
1294 set_buffer_internal_1 (XBUFFER (w->buffer));
1295 }
1296
1297 SET_TEXT_POS_FROM_MARKER (top, w->start);
1298
1299 /* Compute exact mode line heights, if requested. */
1300 if (exact_mode_line_heights_p)
1301 {
1302 if (WINDOW_WANTS_MODELINE_P (w))
1303 current_mode_line_height
1304 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1305 current_buffer->mode_line_format);
1306
1307 if (WINDOW_WANTS_HEADER_LINE_P (w))
1308 current_header_line_height
1309 = display_mode_line (w, HEADER_LINE_FACE_ID,
1310 current_buffer->header_line_format);
1311 }
1312
1313 start_display (&it, w, top);
1314 move_it_to (&it, charpos, -1, it.last_visible_y, -1,
1315 MOVE_TO_POS | MOVE_TO_Y);
1316
1317 /* Note that we may overshoot because of invisible text. */
1318 if (IT_CHARPOS (it) >= charpos)
1319 {
1320 int top_x = it.current_x;
1321 int top_y = it.current_y;
1322 int bottom_y = (last_height = 0, line_bottom_y (&it));
1323 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1324
1325 if (top_y < window_top_y)
1326 visible_p = bottom_y > window_top_y;
1327 else if (top_y < it.last_visible_y)
1328 visible_p = 1;
1329 if (visible_p)
1330 {
1331 *x = top_x;
1332 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1333 *rtop = max (0, window_top_y - top_y);
1334 *rbot = max (0, bottom_y - it.last_visible_y);
1335 }
1336 }
1337 else
1338 {
1339 struct it it2;
1340
1341 it2 = it;
1342 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1343 move_it_by_lines (&it, 1, 0);
1344 if (charpos < IT_CHARPOS (it))
1345 {
1346 visible_p = 1;
1347 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1348 *x = it2.current_x;
1349 *y = it2.current_y + it2.max_ascent - it2.ascent;
1350 *rtop = max (0, -it2.current_y);
1351 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1352 - it.last_visible_y));
1353 }
1354 }
1355
1356 if (old_buffer)
1357 set_buffer_internal_1 (old_buffer);
1358
1359 current_header_line_height = current_mode_line_height = -1;
1360
1361 if (visible_p && XFASTINT (w->hscroll) > 0)
1362 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1363
1364 return visible_p;
1365 }
1366
1367
1368 /* Return the next character from STR which is MAXLEN bytes long.
1369 Return in *LEN the length of the character. This is like
1370 STRING_CHAR_AND_LENGTH but never returns an invalid character. If
1371 we find one, we return a `?', but with the length of the invalid
1372 character. */
1373
1374 static INLINE int
1375 string_char_and_length (str, maxlen, len)
1376 const unsigned char *str;
1377 int maxlen, *len;
1378 {
1379 int c;
1380
1381 c = STRING_CHAR_AND_LENGTH (str, maxlen, *len);
1382 if (!CHAR_VALID_P (c, 1))
1383 /* We may not change the length here because other places in Emacs
1384 don't use this function, i.e. they silently accept invalid
1385 characters. */
1386 c = '?';
1387
1388 return c;
1389 }
1390
1391
1392
1393 /* Given a position POS containing a valid character and byte position
1394 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1395
1396 static struct text_pos
1397 string_pos_nchars_ahead (pos, string, nchars)
1398 struct text_pos pos;
1399 Lisp_Object string;
1400 int nchars;
1401 {
1402 xassert (STRINGP (string) && nchars >= 0);
1403
1404 if (STRING_MULTIBYTE (string))
1405 {
1406 int rest = SBYTES (string) - BYTEPOS (pos);
1407 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1408 int len;
1409
1410 while (nchars--)
1411 {
1412 string_char_and_length (p, rest, &len);
1413 p += len, rest -= len;
1414 xassert (rest >= 0);
1415 CHARPOS (pos) += 1;
1416 BYTEPOS (pos) += len;
1417 }
1418 }
1419 else
1420 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1421
1422 return pos;
1423 }
1424
1425
1426 /* Value is the text position, i.e. character and byte position,
1427 for character position CHARPOS in STRING. */
1428
1429 static INLINE struct text_pos
1430 string_pos (charpos, string)
1431 int charpos;
1432 Lisp_Object string;
1433 {
1434 struct text_pos pos;
1435 xassert (STRINGP (string));
1436 xassert (charpos >= 0);
1437 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1438 return pos;
1439 }
1440
1441
1442 /* Value is a text position, i.e. character and byte position, for
1443 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1444 means recognize multibyte characters. */
1445
1446 static struct text_pos
1447 c_string_pos (charpos, s, multibyte_p)
1448 int charpos;
1449 unsigned char *s;
1450 int multibyte_p;
1451 {
1452 struct text_pos pos;
1453
1454 xassert (s != NULL);
1455 xassert (charpos >= 0);
1456
1457 if (multibyte_p)
1458 {
1459 int rest = strlen (s), len;
1460
1461 SET_TEXT_POS (pos, 0, 0);
1462 while (charpos--)
1463 {
1464 string_char_and_length (s, rest, &len);
1465 s += len, rest -= len;
1466 xassert (rest >= 0);
1467 CHARPOS (pos) += 1;
1468 BYTEPOS (pos) += len;
1469 }
1470 }
1471 else
1472 SET_TEXT_POS (pos, charpos, charpos);
1473
1474 return pos;
1475 }
1476
1477
1478 /* Value is the number of characters in C string S. MULTIBYTE_P
1479 non-zero means recognize multibyte characters. */
1480
1481 static int
1482 number_of_chars (s, multibyte_p)
1483 unsigned char *s;
1484 int multibyte_p;
1485 {
1486 int nchars;
1487
1488 if (multibyte_p)
1489 {
1490 int rest = strlen (s), len;
1491 unsigned char *p = (unsigned char *) s;
1492
1493 for (nchars = 0; rest > 0; ++nchars)
1494 {
1495 string_char_and_length (p, rest, &len);
1496 rest -= len, p += len;
1497 }
1498 }
1499 else
1500 nchars = strlen (s);
1501
1502 return nchars;
1503 }
1504
1505
1506 /* Compute byte position NEWPOS->bytepos corresponding to
1507 NEWPOS->charpos. POS is a known position in string STRING.
1508 NEWPOS->charpos must be >= POS.charpos. */
1509
1510 static void
1511 compute_string_pos (newpos, pos, string)
1512 struct text_pos *newpos, pos;
1513 Lisp_Object string;
1514 {
1515 xassert (STRINGP (string));
1516 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1517
1518 if (STRING_MULTIBYTE (string))
1519 *newpos = string_pos_nchars_ahead (pos, string,
1520 CHARPOS (*newpos) - CHARPOS (pos));
1521 else
1522 BYTEPOS (*newpos) = CHARPOS (*newpos);
1523 }
1524
1525 /* EXPORT:
1526 Return an estimation of the pixel height of mode or top lines on
1527 frame F. FACE_ID specifies what line's height to estimate. */
1528
1529 int
1530 estimate_mode_line_height (f, face_id)
1531 struct frame *f;
1532 enum face_id face_id;
1533 {
1534 #ifdef HAVE_WINDOW_SYSTEM
1535 if (FRAME_WINDOW_P (f))
1536 {
1537 int height = FONT_HEIGHT (FRAME_FONT (f));
1538
1539 /* This function is called so early when Emacs starts that the face
1540 cache and mode line face are not yet initialized. */
1541 if (FRAME_FACE_CACHE (f))
1542 {
1543 struct face *face = FACE_FROM_ID (f, face_id);
1544 if (face)
1545 {
1546 if (face->font)
1547 height = FONT_HEIGHT (face->font);
1548 if (face->box_line_width > 0)
1549 height += 2 * face->box_line_width;
1550 }
1551 }
1552
1553 return height;
1554 }
1555 #endif
1556
1557 return 1;
1558 }
1559
1560 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1561 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1562 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1563 not force the value into range. */
1564
1565 void
1566 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
1567 FRAME_PTR f;
1568 register int pix_x, pix_y;
1569 int *x, *y;
1570 NativeRectangle *bounds;
1571 int noclip;
1572 {
1573
1574 #ifdef HAVE_WINDOW_SYSTEM
1575 if (FRAME_WINDOW_P (f))
1576 {
1577 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1578 even for negative values. */
1579 if (pix_x < 0)
1580 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1581 if (pix_y < 0)
1582 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1583
1584 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1585 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1586
1587 if (bounds)
1588 STORE_NATIVE_RECT (*bounds,
1589 FRAME_COL_TO_PIXEL_X (f, pix_x),
1590 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1591 FRAME_COLUMN_WIDTH (f) - 1,
1592 FRAME_LINE_HEIGHT (f) - 1);
1593
1594 if (!noclip)
1595 {
1596 if (pix_x < 0)
1597 pix_x = 0;
1598 else if (pix_x > FRAME_TOTAL_COLS (f))
1599 pix_x = FRAME_TOTAL_COLS (f);
1600
1601 if (pix_y < 0)
1602 pix_y = 0;
1603 else if (pix_y > FRAME_LINES (f))
1604 pix_y = FRAME_LINES (f);
1605 }
1606 }
1607 #endif
1608
1609 *x = pix_x;
1610 *y = pix_y;
1611 }
1612
1613
1614 /* Given HPOS/VPOS in the current matrix of W, return corresponding
1615 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
1616 can't tell the positions because W's display is not up to date,
1617 return 0. */
1618
1619 int
1620 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
1621 struct window *w;
1622 int hpos, vpos;
1623 int *frame_x, *frame_y;
1624 {
1625 #ifdef HAVE_WINDOW_SYSTEM
1626 if (FRAME_WINDOW_P (XFRAME (WINDOW_FRAME (w))))
1627 {
1628 int success_p;
1629
1630 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
1631 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
1632
1633 if (display_completed)
1634 {
1635 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
1636 struct glyph *glyph = row->glyphs[TEXT_AREA];
1637 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
1638
1639 hpos = row->x;
1640 vpos = row->y;
1641 while (glyph < end)
1642 {
1643 hpos += glyph->pixel_width;
1644 ++glyph;
1645 }
1646
1647 /* If first glyph is partially visible, its first visible position is still 0. */
1648 if (hpos < 0)
1649 hpos = 0;
1650
1651 success_p = 1;
1652 }
1653 else
1654 {
1655 hpos = vpos = 0;
1656 success_p = 0;
1657 }
1658
1659 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, hpos);
1660 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, vpos);
1661 return success_p;
1662 }
1663 #endif
1664
1665 *frame_x = hpos;
1666 *frame_y = vpos;
1667 return 1;
1668 }
1669
1670
1671 #ifdef HAVE_WINDOW_SYSTEM
1672
1673 /* Find the glyph under window-relative coordinates X/Y in window W.
1674 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1675 strings. Return in *HPOS and *VPOS the row and column number of
1676 the glyph found. Return in *AREA the glyph area containing X.
1677 Value is a pointer to the glyph found or null if X/Y is not on
1678 text, or we can't tell because W's current matrix is not up to
1679 date. */
1680
1681 static struct glyph *
1682 x_y_to_hpos_vpos (w, x, y, hpos, vpos, dx, dy, area)
1683 struct window *w;
1684 int x, y;
1685 int *hpos, *vpos, *dx, *dy, *area;
1686 {
1687 struct glyph *glyph, *end;
1688 struct glyph_row *row = NULL;
1689 int x0, i;
1690
1691 /* Find row containing Y. Give up if some row is not enabled. */
1692 for (i = 0; i < w->current_matrix->nrows; ++i)
1693 {
1694 row = MATRIX_ROW (w->current_matrix, i);
1695 if (!row->enabled_p)
1696 return NULL;
1697 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1698 break;
1699 }
1700
1701 *vpos = i;
1702 *hpos = 0;
1703
1704 /* Give up if Y is not in the window. */
1705 if (i == w->current_matrix->nrows)
1706 return NULL;
1707
1708 /* Get the glyph area containing X. */
1709 if (w->pseudo_window_p)
1710 {
1711 *area = TEXT_AREA;
1712 x0 = 0;
1713 }
1714 else
1715 {
1716 if (x < window_box_left_offset (w, TEXT_AREA))
1717 {
1718 *area = LEFT_MARGIN_AREA;
1719 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1720 }
1721 else if (x < window_box_right_offset (w, TEXT_AREA))
1722 {
1723 *area = TEXT_AREA;
1724 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1725 }
1726 else
1727 {
1728 *area = RIGHT_MARGIN_AREA;
1729 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1730 }
1731 }
1732
1733 /* Find glyph containing X. */
1734 glyph = row->glyphs[*area];
1735 end = glyph + row->used[*area];
1736 x -= x0;
1737 while (glyph < end && x >= glyph->pixel_width)
1738 {
1739 x -= glyph->pixel_width;
1740 ++glyph;
1741 }
1742
1743 if (glyph == end)
1744 return NULL;
1745
1746 if (dx)
1747 {
1748 *dx = x;
1749 *dy = y - (row->y + row->ascent - glyph->ascent);
1750 }
1751
1752 *hpos = glyph - row->glyphs[*area];
1753 return glyph;
1754 }
1755
1756
1757 /* EXPORT:
1758 Convert frame-relative x/y to coordinates relative to window W.
1759 Takes pseudo-windows into account. */
1760
1761 void
1762 frame_to_window_pixel_xy (w, x, y)
1763 struct window *w;
1764 int *x, *y;
1765 {
1766 if (w->pseudo_window_p)
1767 {
1768 /* A pseudo-window is always full-width, and starts at the
1769 left edge of the frame, plus a frame border. */
1770 struct frame *f = XFRAME (w->frame);
1771 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1772 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1773 }
1774 else
1775 {
1776 *x -= WINDOW_LEFT_EDGE_X (w);
1777 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1778 }
1779 }
1780
1781 /* EXPORT:
1782 Return in RECTS[] at most N clipping rectangles for glyph string S.
1783 Return the number of stored rectangles. */
1784
1785 int
1786 get_glyph_string_clip_rects (s, rects, n)
1787 struct glyph_string *s;
1788 NativeRectangle *rects;
1789 int n;
1790 {
1791 XRectangle r;
1792
1793 if (n <= 0)
1794 return 0;
1795
1796 if (s->row->full_width_p)
1797 {
1798 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1799 r.x = WINDOW_LEFT_EDGE_X (s->w);
1800 r.width = WINDOW_TOTAL_WIDTH (s->w);
1801
1802 /* Unless displaying a mode or menu bar line, which are always
1803 fully visible, clip to the visible part of the row. */
1804 if (s->w->pseudo_window_p)
1805 r.height = s->row->visible_height;
1806 else
1807 r.height = s->height;
1808 }
1809 else
1810 {
1811 /* This is a text line that may be partially visible. */
1812 r.x = window_box_left (s->w, s->area);
1813 r.width = window_box_width (s->w, s->area);
1814 r.height = s->row->visible_height;
1815 }
1816
1817 if (s->clip_head)
1818 if (r.x < s->clip_head->x)
1819 {
1820 if (r.width >= s->clip_head->x - r.x)
1821 r.width -= s->clip_head->x - r.x;
1822 else
1823 r.width = 0;
1824 r.x = s->clip_head->x;
1825 }
1826 if (s->clip_tail)
1827 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1828 {
1829 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1830 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1831 else
1832 r.width = 0;
1833 }
1834
1835 /* If S draws overlapping rows, it's sufficient to use the top and
1836 bottom of the window for clipping because this glyph string
1837 intentionally draws over other lines. */
1838 if (s->for_overlaps)
1839 {
1840 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1841 r.height = window_text_bottom_y (s->w) - r.y;
1842
1843 /* Alas, the above simple strategy does not work for the
1844 environments with anti-aliased text: if the same text is
1845 drawn onto the same place multiple times, it gets thicker.
1846 If the overlap we are processing is for the erased cursor, we
1847 take the intersection with the rectagle of the cursor. */
1848 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1849 {
1850 XRectangle rc, r_save = r;
1851
1852 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1853 rc.y = s->w->phys_cursor.y;
1854 rc.width = s->w->phys_cursor_width;
1855 rc.height = s->w->phys_cursor_height;
1856
1857 x_intersect_rectangles (&r_save, &rc, &r);
1858 }
1859 }
1860 else
1861 {
1862 /* Don't use S->y for clipping because it doesn't take partially
1863 visible lines into account. For example, it can be negative for
1864 partially visible lines at the top of a window. */
1865 if (!s->row->full_width_p
1866 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1867 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1868 else
1869 r.y = max (0, s->row->y);
1870
1871 /* If drawing a tool-bar window, draw it over the internal border
1872 at the top of the window. */
1873 if (WINDOWP (s->f->tool_bar_window)
1874 && s->w == XWINDOW (s->f->tool_bar_window))
1875 r.y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
1876 }
1877
1878 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1879
1880 /* If drawing the cursor, don't let glyph draw outside its
1881 advertised boundaries. Cleartype does this under some circumstances. */
1882 if (s->hl == DRAW_CURSOR)
1883 {
1884 struct glyph *glyph = s->first_glyph;
1885 int height, max_y;
1886
1887 if (s->x > r.x)
1888 {
1889 r.width -= s->x - r.x;
1890 r.x = s->x;
1891 }
1892 r.width = min (r.width, glyph->pixel_width);
1893
1894 /* If r.y is below window bottom, ensure that we still see a cursor. */
1895 height = min (glyph->ascent + glyph->descent,
1896 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1897 max_y = window_text_bottom_y (s->w) - height;
1898 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1899 if (s->ybase - glyph->ascent > max_y)
1900 {
1901 r.y = max_y;
1902 r.height = height;
1903 }
1904 else
1905 {
1906 /* Don't draw cursor glyph taller than our actual glyph. */
1907 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1908 if (height < r.height)
1909 {
1910 max_y = r.y + r.height;
1911 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1912 r.height = min (max_y - r.y, height);
1913 }
1914 }
1915 }
1916
1917 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1918 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1919 {
1920 #ifdef CONVERT_FROM_XRECT
1921 CONVERT_FROM_XRECT (r, *rects);
1922 #else
1923 *rects = r;
1924 #endif
1925 return 1;
1926 }
1927 else
1928 {
1929 /* If we are processing overlapping and allowed to return
1930 multiple clipping rectangles, we exclude the row of the glyph
1931 string from the clipping rectangle. This is to avoid drawing
1932 the same text on the environment with anti-aliasing. */
1933 #ifdef CONVERT_FROM_XRECT
1934 XRectangle rs[2];
1935 #else
1936 XRectangle *rs = rects;
1937 #endif
1938 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1939
1940 if (s->for_overlaps & OVERLAPS_PRED)
1941 {
1942 rs[i] = r;
1943 if (r.y + r.height > row_y)
1944 {
1945 if (r.y < row_y)
1946 rs[i].height = row_y - r.y;
1947 else
1948 rs[i].height = 0;
1949 }
1950 i++;
1951 }
1952 if (s->for_overlaps & OVERLAPS_SUCC)
1953 {
1954 rs[i] = r;
1955 if (r.y < row_y + s->row->visible_height)
1956 {
1957 if (r.y + r.height > row_y + s->row->visible_height)
1958 {
1959 rs[i].y = row_y + s->row->visible_height;
1960 rs[i].height = r.y + r.height - rs[i].y;
1961 }
1962 else
1963 rs[i].height = 0;
1964 }
1965 i++;
1966 }
1967
1968 n = i;
1969 #ifdef CONVERT_FROM_XRECT
1970 for (i = 0; i < n; i++)
1971 CONVERT_FROM_XRECT (rs[i], rects[i]);
1972 #endif
1973 return n;
1974 }
1975 }
1976
1977 /* EXPORT:
1978 Return in *NR the clipping rectangle for glyph string S. */
1979
1980 void
1981 get_glyph_string_clip_rect (s, nr)
1982 struct glyph_string *s;
1983 NativeRectangle *nr;
1984 {
1985 get_glyph_string_clip_rects (s, nr, 1);
1986 }
1987
1988
1989 /* EXPORT:
1990 Return the position and height of the phys cursor in window W.
1991 Set w->phys_cursor_width to width of phys cursor.
1992 */
1993
1994 void
1995 get_phys_cursor_geometry (w, row, glyph, xp, yp, heightp)
1996 struct window *w;
1997 struct glyph_row *row;
1998 struct glyph *glyph;
1999 int *xp, *yp, *heightp;
2000 {
2001 struct frame *f = XFRAME (WINDOW_FRAME (w));
2002 int x, y, wd, h, h0, y0;
2003
2004 /* Compute the width of the rectangle to draw. If on a stretch
2005 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2006 rectangle as wide as the glyph, but use a canonical character
2007 width instead. */
2008 wd = glyph->pixel_width - 1;
2009 #ifdef HAVE_NTGUI
2010 wd++; /* Why? */
2011 #endif
2012
2013 x = w->phys_cursor.x;
2014 if (x < 0)
2015 {
2016 wd += x;
2017 x = 0;
2018 }
2019
2020 if (glyph->type == STRETCH_GLYPH
2021 && !x_stretch_cursor_p)
2022 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2023 w->phys_cursor_width = wd;
2024
2025 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2026
2027 /* If y is below window bottom, ensure that we still see a cursor. */
2028 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2029
2030 h = max (h0, glyph->ascent + glyph->descent);
2031 h0 = min (h0, glyph->ascent + glyph->descent);
2032
2033 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2034 if (y < y0)
2035 {
2036 h = max (h - (y0 - y) + 1, h0);
2037 y = y0 - 1;
2038 }
2039 else
2040 {
2041 y0 = window_text_bottom_y (w) - h0;
2042 if (y > y0)
2043 {
2044 h += y - y0;
2045 y = y0;
2046 }
2047 }
2048
2049 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2050 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2051 *heightp = h;
2052 }
2053
2054 /*
2055 * Remember which glyph the mouse is over.
2056 */
2057
2058 void
2059 remember_mouse_glyph (f, gx, gy, rect)
2060 struct frame *f;
2061 int gx, gy;
2062 NativeRectangle *rect;
2063 {
2064 Lisp_Object window;
2065 struct window *w;
2066 struct glyph_row *r, *gr, *end_row;
2067 enum window_part part;
2068 enum glyph_row_area area;
2069 int x, y, width, height;
2070
2071 /* Try to determine frame pixel position and size of the glyph under
2072 frame pixel coordinates X/Y on frame F. */
2073
2074 window = window_from_coordinates (f, gx, gy, &part, &x, &y, 0);
2075 if (NILP (window))
2076 {
2077 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2078 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2079 goto virtual_glyph;
2080 }
2081
2082 w = XWINDOW (window);
2083 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2084 height = WINDOW_FRAME_LINE_HEIGHT (w);
2085
2086 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2087 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2088
2089 if (w->pseudo_window_p)
2090 {
2091 area = TEXT_AREA;
2092 part = ON_MODE_LINE; /* Don't adjust margin. */
2093 goto text_glyph;
2094 }
2095
2096 switch (part)
2097 {
2098 case ON_LEFT_MARGIN:
2099 area = LEFT_MARGIN_AREA;
2100 goto text_glyph;
2101
2102 case ON_RIGHT_MARGIN:
2103 area = RIGHT_MARGIN_AREA;
2104 goto text_glyph;
2105
2106 case ON_HEADER_LINE:
2107 case ON_MODE_LINE:
2108 gr = (part == ON_HEADER_LINE
2109 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2110 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2111 gy = gr->y;
2112 area = TEXT_AREA;
2113 goto text_glyph_row_found;
2114
2115 case ON_TEXT:
2116 area = TEXT_AREA;
2117
2118 text_glyph:
2119 gr = 0; gy = 0;
2120 for (; r <= end_row && r->enabled_p; ++r)
2121 if (r->y + r->height > y)
2122 {
2123 gr = r; gy = r->y;
2124 break;
2125 }
2126
2127 text_glyph_row_found:
2128 if (gr && gy <= y)
2129 {
2130 struct glyph *g = gr->glyphs[area];
2131 struct glyph *end = g + gr->used[area];
2132
2133 height = gr->height;
2134 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2135 if (gx + g->pixel_width > x)
2136 break;
2137
2138 if (g < end)
2139 {
2140 if (g->type == IMAGE_GLYPH)
2141 {
2142 /* Don't remember when mouse is over image, as
2143 image may have hot-spots. */
2144 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2145 return;
2146 }
2147 width = g->pixel_width;
2148 }
2149 else
2150 {
2151 /* Use nominal char spacing at end of line. */
2152 x -= gx;
2153 gx += (x / width) * width;
2154 }
2155
2156 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2157 gx += window_box_left_offset (w, area);
2158 }
2159 else
2160 {
2161 /* Use nominal line height at end of window. */
2162 gx = (x / width) * width;
2163 y -= gy;
2164 gy += (y / height) * height;
2165 }
2166 break;
2167
2168 case ON_LEFT_FRINGE:
2169 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2170 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2171 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2172 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2173 goto row_glyph;
2174
2175 case ON_RIGHT_FRINGE:
2176 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2177 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2178 : window_box_right_offset (w, TEXT_AREA));
2179 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2180 goto row_glyph;
2181
2182 case ON_SCROLL_BAR:
2183 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2184 ? 0
2185 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2186 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2187 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2188 : 0)));
2189 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2190
2191 row_glyph:
2192 gr = 0, gy = 0;
2193 for (; r <= end_row && r->enabled_p; ++r)
2194 if (r->y + r->height > y)
2195 {
2196 gr = r; gy = r->y;
2197 break;
2198 }
2199
2200 if (gr && gy <= y)
2201 height = gr->height;
2202 else
2203 {
2204 /* Use nominal line height at end of window. */
2205 y -= gy;
2206 gy += (y / height) * height;
2207 }
2208 break;
2209
2210 default:
2211 ;
2212 virtual_glyph:
2213 /* If there is no glyph under the mouse, then we divide the screen
2214 into a grid of the smallest glyph in the frame, and use that
2215 as our "glyph". */
2216
2217 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2218 round down even for negative values. */
2219 if (gx < 0)
2220 gx -= width - 1;
2221 if (gy < 0)
2222 gy -= height - 1;
2223
2224 gx = (gx / width) * width;
2225 gy = (gy / height) * height;
2226
2227 goto store_rect;
2228 }
2229
2230 gx += WINDOW_LEFT_EDGE_X (w);
2231 gy += WINDOW_TOP_EDGE_Y (w);
2232
2233 store_rect:
2234 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2235
2236 /* Visible feedback for debugging. */
2237 #if 0
2238 #if HAVE_X_WINDOWS
2239 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2240 f->output_data.x->normal_gc,
2241 gx, gy, width, height);
2242 #endif
2243 #endif
2244 }
2245
2246
2247 #endif /* HAVE_WINDOW_SYSTEM */
2248
2249 \f
2250 /***********************************************************************
2251 Lisp form evaluation
2252 ***********************************************************************/
2253
2254 /* Error handler for safe_eval and safe_call. */
2255
2256 static Lisp_Object
2257 safe_eval_handler (arg)
2258 Lisp_Object arg;
2259 {
2260 add_to_log ("Error during redisplay: %s", arg, Qnil);
2261 return Qnil;
2262 }
2263
2264
2265 /* Evaluate SEXPR and return the result, or nil if something went
2266 wrong. Prevent redisplay during the evaluation. */
2267
2268 Lisp_Object
2269 safe_eval (sexpr)
2270 Lisp_Object sexpr;
2271 {
2272 Lisp_Object val;
2273
2274 if (inhibit_eval_during_redisplay)
2275 val = Qnil;
2276 else
2277 {
2278 int count = SPECPDL_INDEX ();
2279 struct gcpro gcpro1;
2280
2281 GCPRO1 (sexpr);
2282 specbind (Qinhibit_redisplay, Qt);
2283 /* Use Qt to ensure debugger does not run,
2284 so there is no possibility of wanting to redisplay. */
2285 val = internal_condition_case_1 (Feval, sexpr, Qt,
2286 safe_eval_handler);
2287 UNGCPRO;
2288 val = unbind_to (count, val);
2289 }
2290
2291 return val;
2292 }
2293
2294
2295 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2296 Return the result, or nil if something went wrong. Prevent
2297 redisplay during the evaluation. */
2298
2299 Lisp_Object
2300 safe_call (nargs, args)
2301 int nargs;
2302 Lisp_Object *args;
2303 {
2304 Lisp_Object val;
2305
2306 if (inhibit_eval_during_redisplay)
2307 val = Qnil;
2308 else
2309 {
2310 int count = SPECPDL_INDEX ();
2311 struct gcpro gcpro1;
2312
2313 GCPRO1 (args[0]);
2314 gcpro1.nvars = nargs;
2315 specbind (Qinhibit_redisplay, Qt);
2316 /* Use Qt to ensure debugger does not run,
2317 so there is no possibility of wanting to redisplay. */
2318 val = internal_condition_case_2 (Ffuncall, nargs, args, Qt,
2319 safe_eval_handler);
2320 UNGCPRO;
2321 val = unbind_to (count, val);
2322 }
2323
2324 return val;
2325 }
2326
2327
2328 /* Call function FN with one argument ARG.
2329 Return the result, or nil if something went wrong. */
2330
2331 Lisp_Object
2332 safe_call1 (fn, arg)
2333 Lisp_Object fn, arg;
2334 {
2335 Lisp_Object args[2];
2336 args[0] = fn;
2337 args[1] = arg;
2338 return safe_call (2, args);
2339 }
2340
2341
2342 \f
2343 /***********************************************************************
2344 Debugging
2345 ***********************************************************************/
2346
2347 #if 0
2348
2349 /* Define CHECK_IT to perform sanity checks on iterators.
2350 This is for debugging. It is too slow to do unconditionally. */
2351
2352 static void
2353 check_it (it)
2354 struct it *it;
2355 {
2356 if (it->method == GET_FROM_STRING)
2357 {
2358 xassert (STRINGP (it->string));
2359 xassert (IT_STRING_CHARPOS (*it) >= 0);
2360 }
2361 else
2362 {
2363 xassert (IT_STRING_CHARPOS (*it) < 0);
2364 if (it->method == GET_FROM_BUFFER)
2365 {
2366 /* Check that character and byte positions agree. */
2367 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2368 }
2369 }
2370
2371 if (it->dpvec)
2372 xassert (it->current.dpvec_index >= 0);
2373 else
2374 xassert (it->current.dpvec_index < 0);
2375 }
2376
2377 #define CHECK_IT(IT) check_it ((IT))
2378
2379 #else /* not 0 */
2380
2381 #define CHECK_IT(IT) (void) 0
2382
2383 #endif /* not 0 */
2384
2385
2386 #if GLYPH_DEBUG
2387
2388 /* Check that the window end of window W is what we expect it
2389 to be---the last row in the current matrix displaying text. */
2390
2391 static void
2392 check_window_end (w)
2393 struct window *w;
2394 {
2395 if (!MINI_WINDOW_P (w)
2396 && !NILP (w->window_end_valid))
2397 {
2398 struct glyph_row *row;
2399 xassert ((row = MATRIX_ROW (w->current_matrix,
2400 XFASTINT (w->window_end_vpos)),
2401 !row->enabled_p
2402 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2403 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2404 }
2405 }
2406
2407 #define CHECK_WINDOW_END(W) check_window_end ((W))
2408
2409 #else /* not GLYPH_DEBUG */
2410
2411 #define CHECK_WINDOW_END(W) (void) 0
2412
2413 #endif /* not GLYPH_DEBUG */
2414
2415
2416 \f
2417 /***********************************************************************
2418 Iterator initialization
2419 ***********************************************************************/
2420
2421 /* Initialize IT for displaying current_buffer in window W, starting
2422 at character position CHARPOS. CHARPOS < 0 means that no buffer
2423 position is specified which is useful when the iterator is assigned
2424 a position later. BYTEPOS is the byte position corresponding to
2425 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2426
2427 If ROW is not null, calls to produce_glyphs with IT as parameter
2428 will produce glyphs in that row.
2429
2430 BASE_FACE_ID is the id of a base face to use. It must be one of
2431 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2432 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2433 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2434
2435 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2436 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2437 will be initialized to use the corresponding mode line glyph row of
2438 the desired matrix of W. */
2439
2440 void
2441 init_iterator (it, w, charpos, bytepos, row, base_face_id)
2442 struct it *it;
2443 struct window *w;
2444 int charpos, bytepos;
2445 struct glyph_row *row;
2446 enum face_id base_face_id;
2447 {
2448 int highlight_region_p;
2449
2450 /* Some precondition checks. */
2451 xassert (w != NULL && it != NULL);
2452 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2453 && charpos <= ZV));
2454
2455 /* If face attributes have been changed since the last redisplay,
2456 free realized faces now because they depend on face definitions
2457 that might have changed. Don't free faces while there might be
2458 desired matrices pending which reference these faces. */
2459 if (face_change_count && !inhibit_free_realized_faces)
2460 {
2461 face_change_count = 0;
2462 free_all_realized_faces (Qnil);
2463 }
2464
2465 /* Use one of the mode line rows of W's desired matrix if
2466 appropriate. */
2467 if (row == NULL)
2468 {
2469 if (base_face_id == MODE_LINE_FACE_ID
2470 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2471 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2472 else if (base_face_id == HEADER_LINE_FACE_ID)
2473 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2474 }
2475
2476 /* Clear IT. */
2477 bzero (it, sizeof *it);
2478 it->current.overlay_string_index = -1;
2479 it->current.dpvec_index = -1;
2480 it->base_face_id = base_face_id;
2481 it->string = Qnil;
2482 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2483
2484 /* The window in which we iterate over current_buffer: */
2485 XSETWINDOW (it->window, w);
2486 it->w = w;
2487 it->f = XFRAME (w->frame);
2488
2489 /* Extra space between lines (on window systems only). */
2490 if (base_face_id == DEFAULT_FACE_ID
2491 && FRAME_WINDOW_P (it->f))
2492 {
2493 if (NATNUMP (current_buffer->extra_line_spacing))
2494 it->extra_line_spacing = XFASTINT (current_buffer->extra_line_spacing);
2495 else if (FLOATP (current_buffer->extra_line_spacing))
2496 it->extra_line_spacing = (XFLOAT_DATA (current_buffer->extra_line_spacing)
2497 * FRAME_LINE_HEIGHT (it->f));
2498 else if (it->f->extra_line_spacing > 0)
2499 it->extra_line_spacing = it->f->extra_line_spacing;
2500 it->max_extra_line_spacing = 0;
2501 }
2502
2503 /* If realized faces have been removed, e.g. because of face
2504 attribute changes of named faces, recompute them. When running
2505 in batch mode, the face cache of Vterminal_frame is null. If
2506 we happen to get called, make a dummy face cache. */
2507 if (noninteractive && FRAME_FACE_CACHE (it->f) == NULL)
2508 init_frame_faces (it->f);
2509 if (FRAME_FACE_CACHE (it->f)->used == 0)
2510 recompute_basic_faces (it->f);
2511
2512 /* Current value of the `slice', `space-width', and 'height' properties. */
2513 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2514 it->space_width = Qnil;
2515 it->font_height = Qnil;
2516 it->override_ascent = -1;
2517
2518 /* Are control characters displayed as `^C'? */
2519 it->ctl_arrow_p = !NILP (current_buffer->ctl_arrow);
2520
2521 /* -1 means everything between a CR and the following line end
2522 is invisible. >0 means lines indented more than this value are
2523 invisible. */
2524 it->selective = (INTEGERP (current_buffer->selective_display)
2525 ? XFASTINT (current_buffer->selective_display)
2526 : (!NILP (current_buffer->selective_display)
2527 ? -1 : 0));
2528 it->selective_display_ellipsis_p
2529 = !NILP (current_buffer->selective_display_ellipses);
2530
2531 /* Display table to use. */
2532 it->dp = window_display_table (w);
2533
2534 /* Are multibyte characters enabled in current_buffer? */
2535 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
2536
2537 /* Non-zero if we should highlight the region. */
2538 highlight_region_p
2539 = (!NILP (Vtransient_mark_mode)
2540 && !NILP (current_buffer->mark_active)
2541 && XMARKER (current_buffer->mark)->buffer != 0);
2542
2543 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2544 start and end of a visible region in window IT->w. Set both to
2545 -1 to indicate no region. */
2546 if (highlight_region_p
2547 /* Maybe highlight only in selected window. */
2548 && (/* Either show region everywhere. */
2549 highlight_nonselected_windows
2550 /* Or show region in the selected window. */
2551 || w == XWINDOW (selected_window)
2552 /* Or show the region if we are in the mini-buffer and W is
2553 the window the mini-buffer refers to. */
2554 || (MINI_WINDOW_P (XWINDOW (selected_window))
2555 && WINDOWP (minibuf_selected_window)
2556 && w == XWINDOW (minibuf_selected_window))))
2557 {
2558 int charpos = marker_position (current_buffer->mark);
2559 it->region_beg_charpos = min (PT, charpos);
2560 it->region_end_charpos = max (PT, charpos);
2561 }
2562 else
2563 it->region_beg_charpos = it->region_end_charpos = -1;
2564
2565 /* Get the position at which the redisplay_end_trigger hook should
2566 be run, if it is to be run at all. */
2567 if (MARKERP (w->redisplay_end_trigger)
2568 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2569 it->redisplay_end_trigger_charpos
2570 = marker_position (w->redisplay_end_trigger);
2571 else if (INTEGERP (w->redisplay_end_trigger))
2572 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2573
2574 /* Correct bogus values of tab_width. */
2575 it->tab_width = XINT (current_buffer->tab_width);
2576 if (it->tab_width <= 0 || it->tab_width > 1000)
2577 it->tab_width = 8;
2578
2579 /* Are lines in the display truncated? */
2580 it->truncate_lines_p
2581 = (base_face_id != DEFAULT_FACE_ID
2582 || XINT (it->w->hscroll)
2583 || (truncate_partial_width_windows
2584 && !WINDOW_FULL_WIDTH_P (it->w))
2585 || !NILP (current_buffer->truncate_lines));
2586
2587 /* Get dimensions of truncation and continuation glyphs. These are
2588 displayed as fringe bitmaps under X, so we don't need them for such
2589 frames. */
2590 if (!FRAME_WINDOW_P (it->f))
2591 {
2592 if (it->truncate_lines_p)
2593 {
2594 /* We will need the truncation glyph. */
2595 xassert (it->glyph_row == NULL);
2596 produce_special_glyphs (it, IT_TRUNCATION);
2597 it->truncation_pixel_width = it->pixel_width;
2598 }
2599 else
2600 {
2601 /* We will need the continuation glyph. */
2602 xassert (it->glyph_row == NULL);
2603 produce_special_glyphs (it, IT_CONTINUATION);
2604 it->continuation_pixel_width = it->pixel_width;
2605 }
2606
2607 /* Reset these values to zero because the produce_special_glyphs
2608 above has changed them. */
2609 it->pixel_width = it->ascent = it->descent = 0;
2610 it->phys_ascent = it->phys_descent = 0;
2611 }
2612
2613 /* Set this after getting the dimensions of truncation and
2614 continuation glyphs, so that we don't produce glyphs when calling
2615 produce_special_glyphs, above. */
2616 it->glyph_row = row;
2617 it->area = TEXT_AREA;
2618
2619 /* Get the dimensions of the display area. The display area
2620 consists of the visible window area plus a horizontally scrolled
2621 part to the left of the window. All x-values are relative to the
2622 start of this total display area. */
2623 if (base_face_id != DEFAULT_FACE_ID)
2624 {
2625 /* Mode lines, menu bar in terminal frames. */
2626 it->first_visible_x = 0;
2627 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2628 }
2629 else
2630 {
2631 it->first_visible_x
2632 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2633 it->last_visible_x = (it->first_visible_x
2634 + window_box_width (w, TEXT_AREA));
2635
2636 /* If we truncate lines, leave room for the truncator glyph(s) at
2637 the right margin. Otherwise, leave room for the continuation
2638 glyph(s). Truncation and continuation glyphs are not inserted
2639 for window-based redisplay. */
2640 if (!FRAME_WINDOW_P (it->f))
2641 {
2642 if (it->truncate_lines_p)
2643 it->last_visible_x -= it->truncation_pixel_width;
2644 else
2645 it->last_visible_x -= it->continuation_pixel_width;
2646 }
2647
2648 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2649 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2650 }
2651
2652 /* Leave room for a border glyph. */
2653 if (!FRAME_WINDOW_P (it->f)
2654 && !WINDOW_RIGHTMOST_P (it->w))
2655 it->last_visible_x -= 1;
2656
2657 it->last_visible_y = window_text_bottom_y (w);
2658
2659 /* For mode lines and alike, arrange for the first glyph having a
2660 left box line if the face specifies a box. */
2661 if (base_face_id != DEFAULT_FACE_ID)
2662 {
2663 struct face *face;
2664
2665 it->face_id = base_face_id;
2666
2667 /* If we have a boxed mode line, make the first character appear
2668 with a left box line. */
2669 face = FACE_FROM_ID (it->f, base_face_id);
2670 if (face->box != FACE_NO_BOX)
2671 it->start_of_box_run_p = 1;
2672 }
2673
2674 /* If a buffer position was specified, set the iterator there,
2675 getting overlays and face properties from that position. */
2676 if (charpos >= BUF_BEG (current_buffer))
2677 {
2678 it->end_charpos = ZV;
2679 it->face_id = -1;
2680 IT_CHARPOS (*it) = charpos;
2681
2682 /* Compute byte position if not specified. */
2683 if (bytepos < charpos)
2684 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2685 else
2686 IT_BYTEPOS (*it) = bytepos;
2687
2688 it->start = it->current;
2689
2690 /* Compute faces etc. */
2691 reseat (it, it->current.pos, 1);
2692 }
2693
2694 CHECK_IT (it);
2695 }
2696
2697
2698 /* Initialize IT for the display of window W with window start POS. */
2699
2700 void
2701 start_display (it, w, pos)
2702 struct it *it;
2703 struct window *w;
2704 struct text_pos pos;
2705 {
2706 struct glyph_row *row;
2707 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2708
2709 row = w->desired_matrix->rows + first_vpos;
2710 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2711 it->first_vpos = first_vpos;
2712
2713 /* Don't reseat to previous visible line start if current start
2714 position is in a string or image. */
2715 if (it->method == GET_FROM_BUFFER && !it->truncate_lines_p)
2716 {
2717 int start_at_line_beg_p;
2718 int first_y = it->current_y;
2719
2720 /* If window start is not at a line start, skip forward to POS to
2721 get the correct continuation lines width. */
2722 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2723 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2724 if (!start_at_line_beg_p)
2725 {
2726 int new_x;
2727
2728 reseat_at_previous_visible_line_start (it);
2729 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2730
2731 new_x = it->current_x + it->pixel_width;
2732
2733 /* If lines are continued, this line may end in the middle
2734 of a multi-glyph character (e.g. a control character
2735 displayed as \003, or in the middle of an overlay
2736 string). In this case move_it_to above will not have
2737 taken us to the start of the continuation line but to the
2738 end of the continued line. */
2739 if (it->current_x > 0
2740 && !it->truncate_lines_p /* Lines are continued. */
2741 && (/* And glyph doesn't fit on the line. */
2742 new_x > it->last_visible_x
2743 /* Or it fits exactly and we're on a window
2744 system frame. */
2745 || (new_x == it->last_visible_x
2746 && FRAME_WINDOW_P (it->f))))
2747 {
2748 if (it->current.dpvec_index >= 0
2749 || it->current.overlay_string_index >= 0)
2750 {
2751 set_iterator_to_next (it, 1);
2752 move_it_in_display_line_to (it, -1, -1, 0);
2753 }
2754
2755 it->continuation_lines_width += it->current_x;
2756 }
2757
2758 /* We're starting a new display line, not affected by the
2759 height of the continued line, so clear the appropriate
2760 fields in the iterator structure. */
2761 it->max_ascent = it->max_descent = 0;
2762 it->max_phys_ascent = it->max_phys_descent = 0;
2763
2764 it->current_y = first_y;
2765 it->vpos = 0;
2766 it->current_x = it->hpos = 0;
2767 }
2768 }
2769
2770 #if 0 /* Don't assert the following because start_display is sometimes
2771 called intentionally with a window start that is not at a
2772 line start. Please leave this code in as a comment. */
2773
2774 /* Window start should be on a line start, now. */
2775 xassert (it->continuation_lines_width
2776 || IT_CHARPOS (it) == BEGV
2777 || FETCH_BYTE (IT_BYTEPOS (it) - 1) == '\n');
2778 #endif /* 0 */
2779 }
2780
2781
2782 /* Return 1 if POS is a position in ellipses displayed for invisible
2783 text. W is the window we display, for text property lookup. */
2784
2785 static int
2786 in_ellipses_for_invisible_text_p (pos, w)
2787 struct display_pos *pos;
2788 struct window *w;
2789 {
2790 Lisp_Object prop, window;
2791 int ellipses_p = 0;
2792 int charpos = CHARPOS (pos->pos);
2793
2794 /* If POS specifies a position in a display vector, this might
2795 be for an ellipsis displayed for invisible text. We won't
2796 get the iterator set up for delivering that ellipsis unless
2797 we make sure that it gets aware of the invisible text. */
2798 if (pos->dpvec_index >= 0
2799 && pos->overlay_string_index < 0
2800 && CHARPOS (pos->string_pos) < 0
2801 && charpos > BEGV
2802 && (XSETWINDOW (window, w),
2803 prop = Fget_char_property (make_number (charpos),
2804 Qinvisible, window),
2805 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2806 {
2807 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2808 window);
2809 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2810 }
2811
2812 return ellipses_p;
2813 }
2814
2815
2816 /* Initialize IT for stepping through current_buffer in window W,
2817 starting at position POS that includes overlay string and display
2818 vector/ control character translation position information. Value
2819 is zero if there are overlay strings with newlines at POS. */
2820
2821 static int
2822 init_from_display_pos (it, w, pos)
2823 struct it *it;
2824 struct window *w;
2825 struct display_pos *pos;
2826 {
2827 int charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2828 int i, overlay_strings_with_newlines = 0;
2829
2830 /* If POS specifies a position in a display vector, this might
2831 be for an ellipsis displayed for invisible text. We won't
2832 get the iterator set up for delivering that ellipsis unless
2833 we make sure that it gets aware of the invisible text. */
2834 if (in_ellipses_for_invisible_text_p (pos, w))
2835 {
2836 --charpos;
2837 bytepos = 0;
2838 }
2839
2840 /* Keep in mind: the call to reseat in init_iterator skips invisible
2841 text, so we might end up at a position different from POS. This
2842 is only a problem when POS is a row start after a newline and an
2843 overlay starts there with an after-string, and the overlay has an
2844 invisible property. Since we don't skip invisible text in
2845 display_line and elsewhere immediately after consuming the
2846 newline before the row start, such a POS will not be in a string,
2847 but the call to init_iterator below will move us to the
2848 after-string. */
2849 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2850
2851 /* This only scans the current chunk -- it should scan all chunks.
2852 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2853 to 16 in 22.1 to make this a lesser problem. */
2854 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2855 {
2856 const char *s = SDATA (it->overlay_strings[i]);
2857 const char *e = s + SBYTES (it->overlay_strings[i]);
2858
2859 while (s < e && *s != '\n')
2860 ++s;
2861
2862 if (s < e)
2863 {
2864 overlay_strings_with_newlines = 1;
2865 break;
2866 }
2867 }
2868
2869 /* If position is within an overlay string, set up IT to the right
2870 overlay string. */
2871 if (pos->overlay_string_index >= 0)
2872 {
2873 int relative_index;
2874
2875 /* If the first overlay string happens to have a `display'
2876 property for an image, the iterator will be set up for that
2877 image, and we have to undo that setup first before we can
2878 correct the overlay string index. */
2879 if (it->method == GET_FROM_IMAGE)
2880 pop_it (it);
2881
2882 /* We already have the first chunk of overlay strings in
2883 IT->overlay_strings. Load more until the one for
2884 pos->overlay_string_index is in IT->overlay_strings. */
2885 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2886 {
2887 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2888 it->current.overlay_string_index = 0;
2889 while (n--)
2890 {
2891 load_overlay_strings (it, 0);
2892 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2893 }
2894 }
2895
2896 it->current.overlay_string_index = pos->overlay_string_index;
2897 relative_index = (it->current.overlay_string_index
2898 % OVERLAY_STRING_CHUNK_SIZE);
2899 it->string = it->overlay_strings[relative_index];
2900 xassert (STRINGP (it->string));
2901 it->current.string_pos = pos->string_pos;
2902 it->method = GET_FROM_STRING;
2903 }
2904
2905 #if 0 /* This is bogus because POS not having an overlay string
2906 position does not mean it's after the string. Example: A
2907 line starting with a before-string and initialization of IT
2908 to the previous row's end position. */
2909 else if (it->current.overlay_string_index >= 0)
2910 {
2911 /* If POS says we're already after an overlay string ending at
2912 POS, make sure to pop the iterator because it will be in
2913 front of that overlay string. When POS is ZV, we've thereby
2914 also ``processed'' overlay strings at ZV. */
2915 while (it->sp)
2916 pop_it (it);
2917 xassert (it->current.overlay_string_index == -1);
2918 xassert (it->method == GET_FROM_BUFFER);
2919 if (CHARPOS (pos->pos) == ZV)
2920 it->overlay_strings_at_end_processed_p = 1;
2921 }
2922 #endif /* 0 */
2923
2924 if (CHARPOS (pos->string_pos) >= 0)
2925 {
2926 /* Recorded position is not in an overlay string, but in another
2927 string. This can only be a string from a `display' property.
2928 IT should already be filled with that string. */
2929 it->current.string_pos = pos->string_pos;
2930 xassert (STRINGP (it->string));
2931 }
2932
2933 /* Restore position in display vector translations, control
2934 character translations or ellipses. */
2935 if (pos->dpvec_index >= 0)
2936 {
2937 if (it->dpvec == NULL)
2938 get_next_display_element (it);
2939 xassert (it->dpvec && it->current.dpvec_index == 0);
2940 it->current.dpvec_index = pos->dpvec_index;
2941 }
2942
2943 CHECK_IT (it);
2944 return !overlay_strings_with_newlines;
2945 }
2946
2947
2948 /* Initialize IT for stepping through current_buffer in window W
2949 starting at ROW->start. */
2950
2951 static void
2952 init_to_row_start (it, w, row)
2953 struct it *it;
2954 struct window *w;
2955 struct glyph_row *row;
2956 {
2957 init_from_display_pos (it, w, &row->start);
2958 it->start = row->start;
2959 it->continuation_lines_width = row->continuation_lines_width;
2960 CHECK_IT (it);
2961 }
2962
2963
2964 /* Initialize IT for stepping through current_buffer in window W
2965 starting in the line following ROW, i.e. starting at ROW->end.
2966 Value is zero if there are overlay strings with newlines at ROW's
2967 end position. */
2968
2969 static int
2970 init_to_row_end (it, w, row)
2971 struct it *it;
2972 struct window *w;
2973 struct glyph_row *row;
2974 {
2975 int success = 0;
2976
2977 if (init_from_display_pos (it, w, &row->end))
2978 {
2979 if (row->continued_p)
2980 it->continuation_lines_width
2981 = row->continuation_lines_width + row->pixel_width;
2982 CHECK_IT (it);
2983 success = 1;
2984 }
2985
2986 return success;
2987 }
2988
2989
2990
2991 \f
2992 /***********************************************************************
2993 Text properties
2994 ***********************************************************************/
2995
2996 /* Called when IT reaches IT->stop_charpos. Handle text property and
2997 overlay changes. Set IT->stop_charpos to the next position where
2998 to stop. */
2999
3000 static void
3001 handle_stop (it)
3002 struct it *it;
3003 {
3004 enum prop_handled handled;
3005 int handle_overlay_change_p;
3006 struct props *p;
3007
3008 it->dpvec = NULL;
3009 it->current.dpvec_index = -1;
3010 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3011 it->ignore_overlay_strings_at_pos_p = 0;
3012
3013 /* Use face of preceding text for ellipsis (if invisible) */
3014 if (it->selective_display_ellipsis_p)
3015 it->saved_face_id = it->face_id;
3016
3017 do
3018 {
3019 handled = HANDLED_NORMALLY;
3020
3021 /* Call text property handlers. */
3022 for (p = it_props; p->handler; ++p)
3023 {
3024 handled = p->handler (it);
3025
3026 if (handled == HANDLED_RECOMPUTE_PROPS)
3027 break;
3028 else if (handled == HANDLED_RETURN)
3029 {
3030 /* We still want to show before and after strings from
3031 overlays even if the actual buffer text is replaced. */
3032 if (!handle_overlay_change_p || it->sp > 1)
3033 return;
3034 if (!get_overlay_strings_1 (it, 0, 0))
3035 return;
3036 it->ignore_overlay_strings_at_pos_p = 1;
3037 it->string_from_display_prop_p = 0;
3038 handle_overlay_change_p = 0;
3039 handled = HANDLED_RECOMPUTE_PROPS;
3040 break;
3041 }
3042 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3043 handle_overlay_change_p = 0;
3044 }
3045
3046 if (handled != HANDLED_RECOMPUTE_PROPS)
3047 {
3048 /* Don't check for overlay strings below when set to deliver
3049 characters from a display vector. */
3050 if (it->method == GET_FROM_DISPLAY_VECTOR)
3051 handle_overlay_change_p = 0;
3052
3053 /* Handle overlay changes. */
3054 if (handle_overlay_change_p)
3055 handled = handle_overlay_change (it);
3056
3057 /* Determine where to stop next. */
3058 if (handled == HANDLED_NORMALLY)
3059 compute_stop_pos (it);
3060 }
3061 }
3062 while (handled == HANDLED_RECOMPUTE_PROPS);
3063 }
3064
3065
3066 /* Compute IT->stop_charpos from text property and overlay change
3067 information for IT's current position. */
3068
3069 static void
3070 compute_stop_pos (it)
3071 struct it *it;
3072 {
3073 register INTERVAL iv, next_iv;
3074 Lisp_Object object, limit, position;
3075
3076 /* If nowhere else, stop at the end. */
3077 it->stop_charpos = it->end_charpos;
3078
3079 if (STRINGP (it->string))
3080 {
3081 /* Strings are usually short, so don't limit the search for
3082 properties. */
3083 object = it->string;
3084 limit = Qnil;
3085 position = make_number (IT_STRING_CHARPOS (*it));
3086 }
3087 else
3088 {
3089 int charpos;
3090
3091 /* If next overlay change is in front of the current stop pos
3092 (which is IT->end_charpos), stop there. Note: value of
3093 next_overlay_change is point-max if no overlay change
3094 follows. */
3095 charpos = next_overlay_change (IT_CHARPOS (*it));
3096 if (charpos < it->stop_charpos)
3097 it->stop_charpos = charpos;
3098
3099 /* If showing the region, we have to stop at the region
3100 start or end because the face might change there. */
3101 if (it->region_beg_charpos > 0)
3102 {
3103 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3104 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3105 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3106 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3107 }
3108
3109 /* Set up variables for computing the stop position from text
3110 property changes. */
3111 XSETBUFFER (object, current_buffer);
3112 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3113 position = make_number (IT_CHARPOS (*it));
3114
3115 }
3116
3117 /* Get the interval containing IT's position. Value is a null
3118 interval if there isn't such an interval. */
3119 iv = validate_interval_range (object, &position, &position, 0);
3120 if (!NULL_INTERVAL_P (iv))
3121 {
3122 Lisp_Object values_here[LAST_PROP_IDX];
3123 struct props *p;
3124
3125 /* Get properties here. */
3126 for (p = it_props; p->handler; ++p)
3127 values_here[p->idx] = textget (iv->plist, *p->name);
3128
3129 /* Look for an interval following iv that has different
3130 properties. */
3131 for (next_iv = next_interval (iv);
3132 (!NULL_INTERVAL_P (next_iv)
3133 && (NILP (limit)
3134 || XFASTINT (limit) > next_iv->position));
3135 next_iv = next_interval (next_iv))
3136 {
3137 for (p = it_props; p->handler; ++p)
3138 {
3139 Lisp_Object new_value;
3140
3141 new_value = textget (next_iv->plist, *p->name);
3142 if (!EQ (values_here[p->idx], new_value))
3143 break;
3144 }
3145
3146 if (p->handler)
3147 break;
3148 }
3149
3150 if (!NULL_INTERVAL_P (next_iv))
3151 {
3152 if (INTEGERP (limit)
3153 && next_iv->position >= XFASTINT (limit))
3154 /* No text property change up to limit. */
3155 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3156 else
3157 /* Text properties change in next_iv. */
3158 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3159 }
3160 }
3161
3162 xassert (STRINGP (it->string)
3163 || (it->stop_charpos >= BEGV
3164 && it->stop_charpos >= IT_CHARPOS (*it)));
3165 }
3166
3167
3168 /* Return the position of the next overlay change after POS in
3169 current_buffer. Value is point-max if no overlay change
3170 follows. This is like `next-overlay-change' but doesn't use
3171 xmalloc. */
3172
3173 static int
3174 next_overlay_change (pos)
3175 int pos;
3176 {
3177 int noverlays;
3178 int endpos;
3179 Lisp_Object *overlays;
3180 int i;
3181
3182 /* Get all overlays at the given position. */
3183 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3184
3185 /* If any of these overlays ends before endpos,
3186 use its ending point instead. */
3187 for (i = 0; i < noverlays; ++i)
3188 {
3189 Lisp_Object oend;
3190 int oendpos;
3191
3192 oend = OVERLAY_END (overlays[i]);
3193 oendpos = OVERLAY_POSITION (oend);
3194 endpos = min (endpos, oendpos);
3195 }
3196
3197 return endpos;
3198 }
3199
3200
3201 \f
3202 /***********************************************************************
3203 Fontification
3204 ***********************************************************************/
3205
3206 /* Handle changes in the `fontified' property of the current buffer by
3207 calling hook functions from Qfontification_functions to fontify
3208 regions of text. */
3209
3210 static enum prop_handled
3211 handle_fontified_prop (it)
3212 struct it *it;
3213 {
3214 Lisp_Object prop, pos;
3215 enum prop_handled handled = HANDLED_NORMALLY;
3216
3217 if (!NILP (Vmemory_full))
3218 return handled;
3219
3220 /* Get the value of the `fontified' property at IT's current buffer
3221 position. (The `fontified' property doesn't have a special
3222 meaning in strings.) If the value is nil, call functions from
3223 Qfontification_functions. */
3224 if (!STRINGP (it->string)
3225 && it->s == NULL
3226 && !NILP (Vfontification_functions)
3227 && !NILP (Vrun_hooks)
3228 && (pos = make_number (IT_CHARPOS (*it)),
3229 prop = Fget_char_property (pos, Qfontified, Qnil),
3230 NILP (prop)))
3231 {
3232 int count = SPECPDL_INDEX ();
3233 Lisp_Object val;
3234
3235 val = Vfontification_functions;
3236 specbind (Qfontification_functions, Qnil);
3237
3238 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3239 safe_call1 (val, pos);
3240 else
3241 {
3242 Lisp_Object globals, fn;
3243 struct gcpro gcpro1, gcpro2;
3244
3245 globals = Qnil;
3246 GCPRO2 (val, globals);
3247
3248 for (; CONSP (val); val = XCDR (val))
3249 {
3250 fn = XCAR (val);
3251
3252 if (EQ (fn, Qt))
3253 {
3254 /* A value of t indicates this hook has a local
3255 binding; it means to run the global binding too.
3256 In a global value, t should not occur. If it
3257 does, we must ignore it to avoid an endless
3258 loop. */
3259 for (globals = Fdefault_value (Qfontification_functions);
3260 CONSP (globals);
3261 globals = XCDR (globals))
3262 {
3263 fn = XCAR (globals);
3264 if (!EQ (fn, Qt))
3265 safe_call1 (fn, pos);
3266 }
3267 }
3268 else
3269 safe_call1 (fn, pos);
3270 }
3271
3272 UNGCPRO;
3273 }
3274
3275 unbind_to (count, Qnil);
3276
3277 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3278 something. This avoids an endless loop if they failed to
3279 fontify the text for which reason ever. */
3280 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3281 handled = HANDLED_RECOMPUTE_PROPS;
3282 }
3283
3284 return handled;
3285 }
3286
3287
3288 \f
3289 /***********************************************************************
3290 Faces
3291 ***********************************************************************/
3292
3293 /* Set up iterator IT from face properties at its current position.
3294 Called from handle_stop. */
3295
3296 static enum prop_handled
3297 handle_face_prop (it)
3298 struct it *it;
3299 {
3300 int new_face_id, next_stop;
3301
3302 if (!STRINGP (it->string))
3303 {
3304 new_face_id
3305 = face_at_buffer_position (it->w,
3306 IT_CHARPOS (*it),
3307 it->region_beg_charpos,
3308 it->region_end_charpos,
3309 &next_stop,
3310 (IT_CHARPOS (*it)
3311 + TEXT_PROP_DISTANCE_LIMIT),
3312 0);
3313
3314 /* Is this a start of a run of characters with box face?
3315 Caveat: this can be called for a freshly initialized
3316 iterator; face_id is -1 in this case. We know that the new
3317 face will not change until limit, i.e. if the new face has a
3318 box, all characters up to limit will have one. But, as
3319 usual, we don't know whether limit is really the end. */
3320 if (new_face_id != it->face_id)
3321 {
3322 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3323
3324 /* If new face has a box but old face has not, this is
3325 the start of a run of characters with box, i.e. it has
3326 a shadow on the left side. The value of face_id of the
3327 iterator will be -1 if this is the initial call that gets
3328 the face. In this case, we have to look in front of IT's
3329 position and see whether there is a face != new_face_id. */
3330 it->start_of_box_run_p
3331 = (new_face->box != FACE_NO_BOX
3332 && (it->face_id >= 0
3333 || IT_CHARPOS (*it) == BEG
3334 || new_face_id != face_before_it_pos (it)));
3335 it->face_box_p = new_face->box != FACE_NO_BOX;
3336 }
3337 }
3338 else
3339 {
3340 int base_face_id, bufpos;
3341
3342 if (it->current.overlay_string_index >= 0)
3343 bufpos = IT_CHARPOS (*it);
3344 else
3345 bufpos = 0;
3346
3347 /* For strings from a buffer, i.e. overlay strings or strings
3348 from a `display' property, use the face at IT's current
3349 buffer position as the base face to merge with, so that
3350 overlay strings appear in the same face as surrounding
3351 text, unless they specify their own faces. */
3352 base_face_id = underlying_face_id (it);
3353
3354 new_face_id = face_at_string_position (it->w,
3355 it->string,
3356 IT_STRING_CHARPOS (*it),
3357 bufpos,
3358 it->region_beg_charpos,
3359 it->region_end_charpos,
3360 &next_stop,
3361 base_face_id, 0);
3362
3363 #if 0 /* This shouldn't be neccessary. Let's check it. */
3364 /* If IT is used to display a mode line we would really like to
3365 use the mode line face instead of the frame's default face. */
3366 if (it->glyph_row == MATRIX_MODE_LINE_ROW (it->w->desired_matrix)
3367 && new_face_id == DEFAULT_FACE_ID)
3368 new_face_id = CURRENT_MODE_LINE_FACE_ID (it->w);
3369 #endif
3370
3371 /* Is this a start of a run of characters with box? Caveat:
3372 this can be called for a freshly allocated iterator; face_id
3373 is -1 is this case. We know that the new face will not
3374 change until the next check pos, i.e. if the new face has a
3375 box, all characters up to that position will have a
3376 box. But, as usual, we don't know whether that position
3377 is really the end. */
3378 if (new_face_id != it->face_id)
3379 {
3380 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3381 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3382
3383 /* If new face has a box but old face hasn't, this is the
3384 start of a run of characters with box, i.e. it has a
3385 shadow on the left side. */
3386 it->start_of_box_run_p
3387 = new_face->box && (old_face == NULL || !old_face->box);
3388 it->face_box_p = new_face->box != FACE_NO_BOX;
3389 }
3390 }
3391
3392 it->face_id = new_face_id;
3393 return HANDLED_NORMALLY;
3394 }
3395
3396
3397 /* Return the ID of the face ``underlying'' IT's current position,
3398 which is in a string. If the iterator is associated with a
3399 buffer, return the face at IT's current buffer position.
3400 Otherwise, use the iterator's base_face_id. */
3401
3402 static int
3403 underlying_face_id (it)
3404 struct it *it;
3405 {
3406 int face_id = it->base_face_id, i;
3407
3408 xassert (STRINGP (it->string));
3409
3410 for (i = it->sp - 1; i >= 0; --i)
3411 if (NILP (it->stack[i].string))
3412 face_id = it->stack[i].face_id;
3413
3414 return face_id;
3415 }
3416
3417
3418 /* Compute the face one character before or after the current position
3419 of IT. BEFORE_P non-zero means get the face in front of IT's
3420 position. Value is the id of the face. */
3421
3422 static int
3423 face_before_or_after_it_pos (it, before_p)
3424 struct it *it;
3425 int before_p;
3426 {
3427 int face_id, limit;
3428 int next_check_charpos;
3429 struct text_pos pos;
3430
3431 xassert (it->s == NULL);
3432
3433 if (STRINGP (it->string))
3434 {
3435 int bufpos, base_face_id;
3436
3437 /* No face change past the end of the string (for the case
3438 we are padding with spaces). No face change before the
3439 string start. */
3440 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3441 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3442 return it->face_id;
3443
3444 /* Set pos to the position before or after IT's current position. */
3445 if (before_p)
3446 pos = string_pos (IT_STRING_CHARPOS (*it) - 1, it->string);
3447 else
3448 /* For composition, we must check the character after the
3449 composition. */
3450 pos = (it->what == IT_COMPOSITION
3451 ? string_pos (IT_STRING_CHARPOS (*it) + it->cmp_len, it->string)
3452 : string_pos (IT_STRING_CHARPOS (*it) + 1, it->string));
3453
3454 if (it->current.overlay_string_index >= 0)
3455 bufpos = IT_CHARPOS (*it);
3456 else
3457 bufpos = 0;
3458
3459 base_face_id = underlying_face_id (it);
3460
3461 /* Get the face for ASCII, or unibyte. */
3462 face_id = face_at_string_position (it->w,
3463 it->string,
3464 CHARPOS (pos),
3465 bufpos,
3466 it->region_beg_charpos,
3467 it->region_end_charpos,
3468 &next_check_charpos,
3469 base_face_id, 0);
3470
3471 /* Correct the face for charsets different from ASCII. Do it
3472 for the multibyte case only. The face returned above is
3473 suitable for unibyte text if IT->string is unibyte. */
3474 if (STRING_MULTIBYTE (it->string))
3475 {
3476 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos);
3477 int rest = SBYTES (it->string) - BYTEPOS (pos);
3478 int c, len;
3479 struct face *face = FACE_FROM_ID (it->f, face_id);
3480
3481 c = string_char_and_length (p, rest, &len);
3482 face_id = FACE_FOR_CHAR (it->f, face, c);
3483 }
3484 }
3485 else
3486 {
3487 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3488 || (IT_CHARPOS (*it) <= BEGV && before_p))
3489 return it->face_id;
3490
3491 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3492 pos = it->current.pos;
3493
3494 if (before_p)
3495 DEC_TEXT_POS (pos, it->multibyte_p);
3496 else
3497 {
3498 if (it->what == IT_COMPOSITION)
3499 /* For composition, we must check the position after the
3500 composition. */
3501 pos.charpos += it->cmp_len, pos.bytepos += it->len;
3502 else
3503 INC_TEXT_POS (pos, it->multibyte_p);
3504 }
3505
3506 /* Determine face for CHARSET_ASCII, or unibyte. */
3507 face_id = face_at_buffer_position (it->w,
3508 CHARPOS (pos),
3509 it->region_beg_charpos,
3510 it->region_end_charpos,
3511 &next_check_charpos,
3512 limit, 0);
3513
3514 /* Correct the face for charsets different from ASCII. Do it
3515 for the multibyte case only. The face returned above is
3516 suitable for unibyte text if current_buffer is unibyte. */
3517 if (it->multibyte_p)
3518 {
3519 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3520 struct face *face = FACE_FROM_ID (it->f, face_id);
3521 face_id = FACE_FOR_CHAR (it->f, face, c);
3522 }
3523 }
3524
3525 return face_id;
3526 }
3527
3528
3529 \f
3530 /***********************************************************************
3531 Invisible text
3532 ***********************************************************************/
3533
3534 /* Set up iterator IT from invisible properties at its current
3535 position. Called from handle_stop. */
3536
3537 static enum prop_handled
3538 handle_invisible_prop (it)
3539 struct it *it;
3540 {
3541 enum prop_handled handled = HANDLED_NORMALLY;
3542
3543 if (STRINGP (it->string))
3544 {
3545 extern Lisp_Object Qinvisible;
3546 Lisp_Object prop, end_charpos, limit, charpos;
3547
3548 /* Get the value of the invisible text property at the
3549 current position. Value will be nil if there is no such
3550 property. */
3551 charpos = make_number (IT_STRING_CHARPOS (*it));
3552 prop = Fget_text_property (charpos, Qinvisible, it->string);
3553
3554 if (!NILP (prop)
3555 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3556 {
3557 handled = HANDLED_RECOMPUTE_PROPS;
3558
3559 /* Get the position at which the next change of the
3560 invisible text property can be found in IT->string.
3561 Value will be nil if the property value is the same for
3562 all the rest of IT->string. */
3563 XSETINT (limit, SCHARS (it->string));
3564 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3565 it->string, limit);
3566
3567 /* Text at current position is invisible. The next
3568 change in the property is at position end_charpos.
3569 Move IT's current position to that position. */
3570 if (INTEGERP (end_charpos)
3571 && XFASTINT (end_charpos) < XFASTINT (limit))
3572 {
3573 struct text_pos old;
3574 old = it->current.string_pos;
3575 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3576 compute_string_pos (&it->current.string_pos, old, it->string);
3577 }
3578 else
3579 {
3580 /* The rest of the string is invisible. If this is an
3581 overlay string, proceed with the next overlay string
3582 or whatever comes and return a character from there. */
3583 if (it->current.overlay_string_index >= 0)
3584 {
3585 next_overlay_string (it);
3586 /* Don't check for overlay strings when we just
3587 finished processing them. */
3588 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3589 }
3590 else
3591 {
3592 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3593 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3594 }
3595 }
3596 }
3597 }
3598 else
3599 {
3600 int invis_p, newpos, next_stop, start_charpos;
3601 Lisp_Object pos, prop, overlay;
3602
3603 /* First of all, is there invisible text at this position? */
3604 start_charpos = IT_CHARPOS (*it);
3605 pos = make_number (IT_CHARPOS (*it));
3606 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3607 &overlay);
3608 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3609
3610 /* If we are on invisible text, skip over it. */
3611 if (invis_p && IT_CHARPOS (*it) < it->end_charpos)
3612 {
3613 /* Record whether we have to display an ellipsis for the
3614 invisible text. */
3615 int display_ellipsis_p = invis_p == 2;
3616
3617 handled = HANDLED_RECOMPUTE_PROPS;
3618
3619 /* Loop skipping over invisible text. The loop is left at
3620 ZV or with IT on the first char being visible again. */
3621 do
3622 {
3623 /* Try to skip some invisible text. Return value is the
3624 position reached which can be equal to IT's position
3625 if there is nothing invisible here. This skips both
3626 over invisible text properties and overlays with
3627 invisible property. */
3628 newpos = skip_invisible (IT_CHARPOS (*it),
3629 &next_stop, ZV, it->window);
3630
3631 /* If we skipped nothing at all we weren't at invisible
3632 text in the first place. If everything to the end of
3633 the buffer was skipped, end the loop. */
3634 if (newpos == IT_CHARPOS (*it) || newpos >= ZV)
3635 invis_p = 0;
3636 else
3637 {
3638 /* We skipped some characters but not necessarily
3639 all there are. Check if we ended up on visible
3640 text. Fget_char_property returns the property of
3641 the char before the given position, i.e. if we
3642 get invis_p = 0, this means that the char at
3643 newpos is visible. */
3644 pos = make_number (newpos);
3645 prop = Fget_char_property (pos, Qinvisible, it->window);
3646 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3647 }
3648
3649 /* If we ended up on invisible text, proceed to
3650 skip starting with next_stop. */
3651 if (invis_p)
3652 IT_CHARPOS (*it) = next_stop;
3653
3654 /* If there are adjacent invisible texts, don't lose the
3655 second one's ellipsis. */
3656 if (invis_p == 2)
3657 display_ellipsis_p = 1;
3658 }
3659 while (invis_p);
3660
3661 /* The position newpos is now either ZV or on visible text. */
3662 IT_CHARPOS (*it) = newpos;
3663 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3664
3665 /* If there are before-strings at the start of invisible
3666 text, and the text is invisible because of a text
3667 property, arrange to show before-strings because 20.x did
3668 it that way. (If the text is invisible because of an
3669 overlay property instead of a text property, this is
3670 already handled in the overlay code.) */
3671 if (NILP (overlay)
3672 && get_overlay_strings (it, start_charpos))
3673 {
3674 handled = HANDLED_RECOMPUTE_PROPS;
3675 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3676 }
3677 else if (display_ellipsis_p)
3678 {
3679 /* Make sure that the glyphs of the ellipsis will get
3680 correct `charpos' values. If we would not update
3681 it->position here, the glyphs would belong to the
3682 last visible character _before_ the invisible
3683 text, which confuses `set_cursor_from_row'.
3684
3685 We use the last invisible position instead of the
3686 first because this way the cursor is always drawn on
3687 the first "." of the ellipsis, whenever PT is inside
3688 the invisible text. Otherwise the cursor would be
3689 placed _after_ the ellipsis when the point is after the
3690 first invisible character. */
3691 if (!STRINGP (it->object))
3692 {
3693 it->position.charpos = IT_CHARPOS (*it) - 1;
3694 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3695 }
3696 setup_for_ellipsis (it, 0);
3697 }
3698 }
3699 }
3700
3701 return handled;
3702 }
3703
3704
3705 /* Make iterator IT return `...' next.
3706 Replaces LEN characters from buffer. */
3707
3708 static void
3709 setup_for_ellipsis (it, len)
3710 struct it *it;
3711 int len;
3712 {
3713 /* Use the display table definition for `...'. Invalid glyphs
3714 will be handled by the method returning elements from dpvec. */
3715 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3716 {
3717 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3718 it->dpvec = v->contents;
3719 it->dpend = v->contents + v->size;
3720 }
3721 else
3722 {
3723 /* Default `...'. */
3724 it->dpvec = default_invis_vector;
3725 it->dpend = default_invis_vector + 3;
3726 }
3727
3728 it->dpvec_char_len = len;
3729 it->current.dpvec_index = 0;
3730 it->dpvec_face_id = -1;
3731
3732 /* Remember the current face id in case glyphs specify faces.
3733 IT's face is restored in set_iterator_to_next.
3734 saved_face_id was set to preceding char's face in handle_stop. */
3735 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3736 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3737
3738 it->method = GET_FROM_DISPLAY_VECTOR;
3739 it->ellipsis_p = 1;
3740 }
3741
3742
3743 \f
3744 /***********************************************************************
3745 'display' property
3746 ***********************************************************************/
3747
3748 /* Set up iterator IT from `display' property at its current position.
3749 Called from handle_stop.
3750 We return HANDLED_RETURN if some part of the display property
3751 overrides the display of the buffer text itself.
3752 Otherwise we return HANDLED_NORMALLY. */
3753
3754 static enum prop_handled
3755 handle_display_prop (it)
3756 struct it *it;
3757 {
3758 Lisp_Object prop, object;
3759 struct text_pos *position;
3760 /* Nonzero if some property replaces the display of the text itself. */
3761 int display_replaced_p = 0;
3762
3763 if (STRINGP (it->string))
3764 {
3765 object = it->string;
3766 position = &it->current.string_pos;
3767 }
3768 else
3769 {
3770 XSETWINDOW (object, it->w);
3771 position = &it->current.pos;
3772 }
3773
3774 /* Reset those iterator values set from display property values. */
3775 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
3776 it->space_width = Qnil;
3777 it->font_height = Qnil;
3778 it->voffset = 0;
3779
3780 /* We don't support recursive `display' properties, i.e. string
3781 values that have a string `display' property, that have a string
3782 `display' property etc. */
3783 if (!it->string_from_display_prop_p)
3784 it->area = TEXT_AREA;
3785
3786 prop = Fget_char_property (make_number (position->charpos),
3787 Qdisplay, object);
3788 if (NILP (prop))
3789 return HANDLED_NORMALLY;
3790
3791 if (!STRINGP (it->string))
3792 object = it->w->buffer;
3793
3794 if (CONSP (prop)
3795 /* Simple properties. */
3796 && !EQ (XCAR (prop), Qimage)
3797 && !EQ (XCAR (prop), Qspace)
3798 && !EQ (XCAR (prop), Qwhen)
3799 && !EQ (XCAR (prop), Qslice)
3800 && !EQ (XCAR (prop), Qspace_width)
3801 && !EQ (XCAR (prop), Qheight)
3802 && !EQ (XCAR (prop), Qraise)
3803 /* Marginal area specifications. */
3804 && !(CONSP (XCAR (prop)) && EQ (XCAR (XCAR (prop)), Qmargin))
3805 && !EQ (XCAR (prop), Qleft_fringe)
3806 && !EQ (XCAR (prop), Qright_fringe)
3807 && !NILP (XCAR (prop)))
3808 {
3809 for (; CONSP (prop); prop = XCDR (prop))
3810 {
3811 if (handle_single_display_spec (it, XCAR (prop), object,
3812 position, display_replaced_p))
3813 display_replaced_p = 1;
3814 }
3815 }
3816 else if (VECTORP (prop))
3817 {
3818 int i;
3819 for (i = 0; i < ASIZE (prop); ++i)
3820 if (handle_single_display_spec (it, AREF (prop, i), object,
3821 position, display_replaced_p))
3822 display_replaced_p = 1;
3823 }
3824 else
3825 {
3826 int ret = handle_single_display_spec (it, prop, object, position, 0);
3827 if (ret < 0) /* Replaced by "", i.e. nothing. */
3828 return HANDLED_RECOMPUTE_PROPS;
3829 if (ret)
3830 display_replaced_p = 1;
3831 }
3832
3833 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
3834 }
3835
3836
3837 /* Value is the position of the end of the `display' property starting
3838 at START_POS in OBJECT. */
3839
3840 static struct text_pos
3841 display_prop_end (it, object, start_pos)
3842 struct it *it;
3843 Lisp_Object object;
3844 struct text_pos start_pos;
3845 {
3846 Lisp_Object end;
3847 struct text_pos end_pos;
3848
3849 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
3850 Qdisplay, object, Qnil);
3851 CHARPOS (end_pos) = XFASTINT (end);
3852 if (STRINGP (object))
3853 compute_string_pos (&end_pos, start_pos, it->string);
3854 else
3855 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
3856
3857 return end_pos;
3858 }
3859
3860
3861 /* Set up IT from a single `display' specification PROP. OBJECT
3862 is the object in which the `display' property was found. *POSITION
3863 is the position at which it was found. DISPLAY_REPLACED_P non-zero
3864 means that we previously saw a display specification which already
3865 replaced text display with something else, for example an image;
3866 we ignore such properties after the first one has been processed.
3867
3868 If PROP is a `space' or `image' specification, and in some other
3869 cases too, set *POSITION to the position where the `display'
3870 property ends.
3871
3872 Value is non-zero if something was found which replaces the display
3873 of buffer or string text. Specifically, the value is -1 if that
3874 "something" is "nothing". */
3875
3876 static int
3877 handle_single_display_spec (it, spec, object, position,
3878 display_replaced_before_p)
3879 struct it *it;
3880 Lisp_Object spec;
3881 Lisp_Object object;
3882 struct text_pos *position;
3883 int display_replaced_before_p;
3884 {
3885 Lisp_Object form;
3886 Lisp_Object location, value;
3887 struct text_pos start_pos, save_pos;
3888 int valid_p;
3889
3890 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
3891 If the result is non-nil, use VALUE instead of SPEC. */
3892 form = Qt;
3893 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
3894 {
3895 spec = XCDR (spec);
3896 if (!CONSP (spec))
3897 return 0;
3898 form = XCAR (spec);
3899 spec = XCDR (spec);
3900 }
3901
3902 if (!NILP (form) && !EQ (form, Qt))
3903 {
3904 int count = SPECPDL_INDEX ();
3905 struct gcpro gcpro1;
3906
3907 /* Bind `object' to the object having the `display' property, a
3908 buffer or string. Bind `position' to the position in the
3909 object where the property was found, and `buffer-position'
3910 to the current position in the buffer. */
3911 specbind (Qobject, object);
3912 specbind (Qposition, make_number (CHARPOS (*position)));
3913 specbind (Qbuffer_position,
3914 make_number (STRINGP (object)
3915 ? IT_CHARPOS (*it) : CHARPOS (*position)));
3916 GCPRO1 (form);
3917 form = safe_eval (form);
3918 UNGCPRO;
3919 unbind_to (count, Qnil);
3920 }
3921
3922 if (NILP (form))
3923 return 0;
3924
3925 /* Handle `(height HEIGHT)' specifications. */
3926 if (CONSP (spec)
3927 && EQ (XCAR (spec), Qheight)
3928 && CONSP (XCDR (spec)))
3929 {
3930 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3931 return 0;
3932
3933 it->font_height = XCAR (XCDR (spec));
3934 if (!NILP (it->font_height))
3935 {
3936 struct face *face = FACE_FROM_ID (it->f, it->face_id);
3937 int new_height = -1;
3938
3939 if (CONSP (it->font_height)
3940 && (EQ (XCAR (it->font_height), Qplus)
3941 || EQ (XCAR (it->font_height), Qminus))
3942 && CONSP (XCDR (it->font_height))
3943 && INTEGERP (XCAR (XCDR (it->font_height))))
3944 {
3945 /* `(+ N)' or `(- N)' where N is an integer. */
3946 int steps = XINT (XCAR (XCDR (it->font_height)));
3947 if (EQ (XCAR (it->font_height), Qplus))
3948 steps = - steps;
3949 it->face_id = smaller_face (it->f, it->face_id, steps);
3950 }
3951 else if (FUNCTIONP (it->font_height))
3952 {
3953 /* Call function with current height as argument.
3954 Value is the new height. */
3955 Lisp_Object height;
3956 height = safe_call1 (it->font_height,
3957 face->lface[LFACE_HEIGHT_INDEX]);
3958 if (NUMBERP (height))
3959 new_height = XFLOATINT (height);
3960 }
3961 else if (NUMBERP (it->font_height))
3962 {
3963 /* Value is a multiple of the canonical char height. */
3964 struct face *face;
3965
3966 face = FACE_FROM_ID (it->f, DEFAULT_FACE_ID);
3967 new_height = (XFLOATINT (it->font_height)
3968 * XINT (face->lface[LFACE_HEIGHT_INDEX]));
3969 }
3970 else
3971 {
3972 /* Evaluate IT->font_height with `height' bound to the
3973 current specified height to get the new height. */
3974 int count = SPECPDL_INDEX ();
3975
3976 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
3977 value = safe_eval (it->font_height);
3978 unbind_to (count, Qnil);
3979
3980 if (NUMBERP (value))
3981 new_height = XFLOATINT (value);
3982 }
3983
3984 if (new_height > 0)
3985 it->face_id = face_with_height (it->f, it->face_id, new_height);
3986 }
3987
3988 return 0;
3989 }
3990
3991 /* Handle `(space_width WIDTH)'. */
3992 if (CONSP (spec)
3993 && EQ (XCAR (spec), Qspace_width)
3994 && CONSP (XCDR (spec)))
3995 {
3996 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
3997 return 0;
3998
3999 value = XCAR (XCDR (spec));
4000 if (NUMBERP (value) && XFLOATINT (value) > 0)
4001 it->space_width = value;
4002
4003 return 0;
4004 }
4005
4006 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4007 if (CONSP (spec)
4008 && EQ (XCAR (spec), Qslice))
4009 {
4010 Lisp_Object tem;
4011
4012 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4013 return 0;
4014
4015 if (tem = XCDR (spec), CONSP (tem))
4016 {
4017 it->slice.x = XCAR (tem);
4018 if (tem = XCDR (tem), CONSP (tem))
4019 {
4020 it->slice.y = XCAR (tem);
4021 if (tem = XCDR (tem), CONSP (tem))
4022 {
4023 it->slice.width = XCAR (tem);
4024 if (tem = XCDR (tem), CONSP (tem))
4025 it->slice.height = XCAR (tem);
4026 }
4027 }
4028 }
4029
4030 return 0;
4031 }
4032
4033 /* Handle `(raise FACTOR)'. */
4034 if (CONSP (spec)
4035 && EQ (XCAR (spec), Qraise)
4036 && CONSP (XCDR (spec)))
4037 {
4038 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4039 return 0;
4040
4041 #ifdef HAVE_WINDOW_SYSTEM
4042 value = XCAR (XCDR (spec));
4043 if (NUMBERP (value))
4044 {
4045 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4046 it->voffset = - (XFLOATINT (value)
4047 * (FONT_HEIGHT (face->font)));
4048 }
4049 #endif /* HAVE_WINDOW_SYSTEM */
4050
4051 return 0;
4052 }
4053
4054 /* Don't handle the other kinds of display specifications
4055 inside a string that we got from a `display' property. */
4056 if (it->string_from_display_prop_p)
4057 return 0;
4058
4059 /* Characters having this form of property are not displayed, so
4060 we have to find the end of the property. */
4061 start_pos = *position;
4062 *position = display_prop_end (it, object, start_pos);
4063 value = Qnil;
4064
4065 /* Stop the scan at that end position--we assume that all
4066 text properties change there. */
4067 it->stop_charpos = position->charpos;
4068
4069 /* Handle `(left-fringe BITMAP [FACE])'
4070 and `(right-fringe BITMAP [FACE])'. */
4071 if (CONSP (spec)
4072 && (EQ (XCAR (spec), Qleft_fringe)
4073 || EQ (XCAR (spec), Qright_fringe))
4074 && CONSP (XCDR (spec)))
4075 {
4076 int face_id = DEFAULT_FACE_ID;
4077 int fringe_bitmap;
4078
4079 if (FRAME_TERMCAP_P (it->f) || FRAME_MSDOS_P (it->f))
4080 /* If we return here, POSITION has been advanced
4081 across the text with this property. */
4082 return 0;
4083
4084 #ifdef HAVE_WINDOW_SYSTEM
4085 value = XCAR (XCDR (spec));
4086 if (!SYMBOLP (value)
4087 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4088 /* If we return here, POSITION has been advanced
4089 across the text with this property. */
4090 return 0;
4091
4092 if (CONSP (XCDR (XCDR (spec))))
4093 {
4094 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4095 int face_id2 = lookup_derived_face (it->f, face_name,
4096 'A', FRINGE_FACE_ID, 0);
4097 if (face_id2 >= 0)
4098 face_id = face_id2;
4099 }
4100
4101 /* Save current settings of IT so that we can restore them
4102 when we are finished with the glyph property value. */
4103
4104 save_pos = it->position;
4105 it->position = *position;
4106 push_it (it);
4107 it->position = save_pos;
4108
4109 it->area = TEXT_AREA;
4110 it->what = IT_IMAGE;
4111 it->image_id = -1; /* no image */
4112 it->position = start_pos;
4113 it->object = NILP (object) ? it->w->buffer : object;
4114 it->method = GET_FROM_IMAGE;
4115 it->face_id = face_id;
4116
4117 /* Say that we haven't consumed the characters with
4118 `display' property yet. The call to pop_it in
4119 set_iterator_to_next will clean this up. */
4120 *position = start_pos;
4121
4122 if (EQ (XCAR (spec), Qleft_fringe))
4123 {
4124 it->left_user_fringe_bitmap = fringe_bitmap;
4125 it->left_user_fringe_face_id = face_id;
4126 }
4127 else
4128 {
4129 it->right_user_fringe_bitmap = fringe_bitmap;
4130 it->right_user_fringe_face_id = face_id;
4131 }
4132 #endif /* HAVE_WINDOW_SYSTEM */
4133 return 1;
4134 }
4135
4136 /* Prepare to handle `((margin left-margin) ...)',
4137 `((margin right-margin) ...)' and `((margin nil) ...)'
4138 prefixes for display specifications. */
4139 location = Qunbound;
4140 if (CONSP (spec) && CONSP (XCAR (spec)))
4141 {
4142 Lisp_Object tem;
4143
4144 value = XCDR (spec);
4145 if (CONSP (value))
4146 value = XCAR (value);
4147
4148 tem = XCAR (spec);
4149 if (EQ (XCAR (tem), Qmargin)
4150 && (tem = XCDR (tem),
4151 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4152 (NILP (tem)
4153 || EQ (tem, Qleft_margin)
4154 || EQ (tem, Qright_margin))))
4155 location = tem;
4156 }
4157
4158 if (EQ (location, Qunbound))
4159 {
4160 location = Qnil;
4161 value = spec;
4162 }
4163
4164 /* After this point, VALUE is the property after any
4165 margin prefix has been stripped. It must be a string,
4166 an image specification, or `(space ...)'.
4167
4168 LOCATION specifies where to display: `left-margin',
4169 `right-margin' or nil. */
4170
4171 valid_p = (STRINGP (value)
4172 #ifdef HAVE_WINDOW_SYSTEM
4173 || (!FRAME_TERMCAP_P (it->f) && valid_image_p (value))
4174 #endif /* not HAVE_WINDOW_SYSTEM */
4175 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4176
4177 if (valid_p && !display_replaced_before_p)
4178 {
4179 /* Save current settings of IT so that we can restore them
4180 when we are finished with the glyph property value. */
4181 save_pos = it->position;
4182 it->position = *position;
4183 push_it (it);
4184 it->position = save_pos;
4185
4186 if (NILP (location))
4187 it->area = TEXT_AREA;
4188 else if (EQ (location, Qleft_margin))
4189 it->area = LEFT_MARGIN_AREA;
4190 else
4191 it->area = RIGHT_MARGIN_AREA;
4192
4193 if (STRINGP (value))
4194 {
4195 if (SCHARS (value) == 0)
4196 {
4197 pop_it (it);
4198 return -1; /* Replaced by "", i.e. nothing. */
4199 }
4200 it->string = value;
4201 it->multibyte_p = STRING_MULTIBYTE (it->string);
4202 it->current.overlay_string_index = -1;
4203 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4204 it->end_charpos = it->string_nchars = SCHARS (it->string);
4205 it->method = GET_FROM_STRING;
4206 it->stop_charpos = 0;
4207 it->string_from_display_prop_p = 1;
4208 /* Say that we haven't consumed the characters with
4209 `display' property yet. The call to pop_it in
4210 set_iterator_to_next will clean this up. */
4211 *position = start_pos;
4212 }
4213 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4214 {
4215 it->method = GET_FROM_STRETCH;
4216 it->object = value;
4217 *position = it->position = start_pos;
4218 }
4219 #ifdef HAVE_WINDOW_SYSTEM
4220 else
4221 {
4222 it->what = IT_IMAGE;
4223 it->image_id = lookup_image (it->f, value);
4224 it->position = start_pos;
4225 it->object = NILP (object) ? it->w->buffer : object;
4226 it->method = GET_FROM_IMAGE;
4227
4228 /* Say that we haven't consumed the characters with
4229 `display' property yet. The call to pop_it in
4230 set_iterator_to_next will clean this up. */
4231 *position = start_pos;
4232 }
4233 #endif /* HAVE_WINDOW_SYSTEM */
4234
4235 return 1;
4236 }
4237
4238 /* Invalid property or property not supported. Restore
4239 POSITION to what it was before. */
4240 *position = start_pos;
4241 return 0;
4242 }
4243
4244
4245 /* Check if SPEC is a display specification value whose text should be
4246 treated as intangible. */
4247
4248 static int
4249 single_display_spec_intangible_p (prop)
4250 Lisp_Object prop;
4251 {
4252 /* Skip over `when FORM'. */
4253 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4254 {
4255 prop = XCDR (prop);
4256 if (!CONSP (prop))
4257 return 0;
4258 prop = XCDR (prop);
4259 }
4260
4261 if (STRINGP (prop))
4262 return 1;
4263
4264 if (!CONSP (prop))
4265 return 0;
4266
4267 /* Skip over `margin LOCATION'. If LOCATION is in the margins,
4268 we don't need to treat text as intangible. */
4269 if (EQ (XCAR (prop), Qmargin))
4270 {
4271 prop = XCDR (prop);
4272 if (!CONSP (prop))
4273 return 0;
4274
4275 prop = XCDR (prop);
4276 if (!CONSP (prop)
4277 || EQ (XCAR (prop), Qleft_margin)
4278 || EQ (XCAR (prop), Qright_margin))
4279 return 0;
4280 }
4281
4282 return (CONSP (prop)
4283 && (EQ (XCAR (prop), Qimage)
4284 || EQ (XCAR (prop), Qspace)));
4285 }
4286
4287
4288 /* Check if PROP is a display property value whose text should be
4289 treated as intangible. */
4290
4291 int
4292 display_prop_intangible_p (prop)
4293 Lisp_Object prop;
4294 {
4295 if (CONSP (prop)
4296 && CONSP (XCAR (prop))
4297 && !EQ (Qmargin, XCAR (XCAR (prop))))
4298 {
4299 /* A list of sub-properties. */
4300 while (CONSP (prop))
4301 {
4302 if (single_display_spec_intangible_p (XCAR (prop)))
4303 return 1;
4304 prop = XCDR (prop);
4305 }
4306 }
4307 else if (VECTORP (prop))
4308 {
4309 /* A vector of sub-properties. */
4310 int i;
4311 for (i = 0; i < ASIZE (prop); ++i)
4312 if (single_display_spec_intangible_p (AREF (prop, i)))
4313 return 1;
4314 }
4315 else
4316 return single_display_spec_intangible_p (prop);
4317
4318 return 0;
4319 }
4320
4321
4322 /* Return 1 if PROP is a display sub-property value containing STRING. */
4323
4324 static int
4325 single_display_spec_string_p (prop, string)
4326 Lisp_Object prop, string;
4327 {
4328 if (EQ (string, prop))
4329 return 1;
4330
4331 /* Skip over `when FORM'. */
4332 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4333 {
4334 prop = XCDR (prop);
4335 if (!CONSP (prop))
4336 return 0;
4337 prop = XCDR (prop);
4338 }
4339
4340 if (CONSP (prop))
4341 /* Skip over `margin LOCATION'. */
4342 if (EQ (XCAR (prop), Qmargin))
4343 {
4344 prop = XCDR (prop);
4345 if (!CONSP (prop))
4346 return 0;
4347
4348 prop = XCDR (prop);
4349 if (!CONSP (prop))
4350 return 0;
4351 }
4352
4353 return CONSP (prop) && EQ (XCAR (prop), string);
4354 }
4355
4356
4357 /* Return 1 if STRING appears in the `display' property PROP. */
4358
4359 static int
4360 display_prop_string_p (prop, string)
4361 Lisp_Object prop, string;
4362 {
4363 if (CONSP (prop)
4364 && CONSP (XCAR (prop))
4365 && !EQ (Qmargin, XCAR (XCAR (prop))))
4366 {
4367 /* A list of sub-properties. */
4368 while (CONSP (prop))
4369 {
4370 if (single_display_spec_string_p (XCAR (prop), string))
4371 return 1;
4372 prop = XCDR (prop);
4373 }
4374 }
4375 else if (VECTORP (prop))
4376 {
4377 /* A vector of sub-properties. */
4378 int i;
4379 for (i = 0; i < ASIZE (prop); ++i)
4380 if (single_display_spec_string_p (AREF (prop, i), string))
4381 return 1;
4382 }
4383 else
4384 return single_display_spec_string_p (prop, string);
4385
4386 return 0;
4387 }
4388
4389
4390 /* Determine from which buffer position in W's buffer STRING comes
4391 from. AROUND_CHARPOS is an approximate position where it could
4392 be from. Value is the buffer position or 0 if it couldn't be
4393 determined.
4394
4395 W's buffer must be current.
4396
4397 This function is necessary because we don't record buffer positions
4398 in glyphs generated from strings (to keep struct glyph small).
4399 This function may only use code that doesn't eval because it is
4400 called asynchronously from note_mouse_highlight. */
4401
4402 int
4403 string_buffer_position (w, string, around_charpos)
4404 struct window *w;
4405 Lisp_Object string;
4406 int around_charpos;
4407 {
4408 Lisp_Object limit, prop, pos;
4409 const int MAX_DISTANCE = 1000;
4410 int found = 0;
4411
4412 pos = make_number (around_charpos);
4413 limit = make_number (min (XINT (pos) + MAX_DISTANCE, ZV));
4414 while (!found && !EQ (pos, limit))
4415 {
4416 prop = Fget_char_property (pos, Qdisplay, Qnil);
4417 if (!NILP (prop) && display_prop_string_p (prop, string))
4418 found = 1;
4419 else
4420 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil, limit);
4421 }
4422
4423 if (!found)
4424 {
4425 pos = make_number (around_charpos);
4426 limit = make_number (max (XINT (pos) - MAX_DISTANCE, BEGV));
4427 while (!found && !EQ (pos, limit))
4428 {
4429 prop = Fget_char_property (pos, Qdisplay, Qnil);
4430 if (!NILP (prop) && display_prop_string_p (prop, string))
4431 found = 1;
4432 else
4433 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4434 limit);
4435 }
4436 }
4437
4438 return found ? XINT (pos) : 0;
4439 }
4440
4441
4442 \f
4443 /***********************************************************************
4444 `composition' property
4445 ***********************************************************************/
4446
4447 /* Set up iterator IT from `composition' property at its current
4448 position. Called from handle_stop. */
4449
4450 static enum prop_handled
4451 handle_composition_prop (it)
4452 struct it *it;
4453 {
4454 Lisp_Object prop, string;
4455 int pos, pos_byte, end;
4456 enum prop_handled handled = HANDLED_NORMALLY;
4457
4458 if (STRINGP (it->string))
4459 {
4460 pos = IT_STRING_CHARPOS (*it);
4461 pos_byte = IT_STRING_BYTEPOS (*it);
4462 string = it->string;
4463 }
4464 else
4465 {
4466 pos = IT_CHARPOS (*it);
4467 pos_byte = IT_BYTEPOS (*it);
4468 string = Qnil;
4469 }
4470
4471 /* If there's a valid composition and point is not inside of the
4472 composition (in the case that the composition is from the current
4473 buffer), draw a glyph composed from the composition components. */
4474 if (find_composition (pos, -1, &pos, &end, &prop, string)
4475 && COMPOSITION_VALID_P (pos, end, prop)
4476 && (STRINGP (it->string) || (PT <= pos || PT >= end)))
4477 {
4478 int id = get_composition_id (pos, pos_byte, end - pos, prop, string);
4479
4480 if (id >= 0)
4481 {
4482 struct composition *cmp = composition_table[id];
4483
4484 if (cmp->glyph_len == 0)
4485 {
4486 /* No glyph. */
4487 if (STRINGP (it->string))
4488 {
4489 IT_STRING_CHARPOS (*it) = end;
4490 IT_STRING_BYTEPOS (*it) = string_char_to_byte (it->string,
4491 end);
4492 }
4493 else
4494 {
4495 IT_CHARPOS (*it) = end;
4496 IT_BYTEPOS (*it) = CHAR_TO_BYTE (end);
4497 }
4498 return HANDLED_RECOMPUTE_PROPS;
4499 }
4500
4501 it->stop_charpos = end;
4502 push_it (it);
4503
4504 it->method = GET_FROM_COMPOSITION;
4505 it->cmp_id = id;
4506 it->cmp_len = COMPOSITION_LENGTH (prop);
4507 /* For a terminal, draw only the first character of the
4508 components. */
4509 it->c = COMPOSITION_GLYPH (composition_table[id], 0);
4510 it->len = (STRINGP (it->string)
4511 ? string_char_to_byte (it->string, end)
4512 : CHAR_TO_BYTE (end)) - pos_byte;
4513 handled = HANDLED_RETURN;
4514 }
4515 }
4516
4517 return handled;
4518 }
4519
4520
4521 \f
4522 /***********************************************************************
4523 Overlay strings
4524 ***********************************************************************/
4525
4526 /* The following structure is used to record overlay strings for
4527 later sorting in load_overlay_strings. */
4528
4529 struct overlay_entry
4530 {
4531 Lisp_Object overlay;
4532 Lisp_Object string;
4533 int priority;
4534 int after_string_p;
4535 };
4536
4537
4538 /* Set up iterator IT from overlay strings at its current position.
4539 Called from handle_stop. */
4540
4541 static enum prop_handled
4542 handle_overlay_change (it)
4543 struct it *it;
4544 {
4545 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4546 return HANDLED_RECOMPUTE_PROPS;
4547 else
4548 return HANDLED_NORMALLY;
4549 }
4550
4551
4552 /* Set up the next overlay string for delivery by IT, if there is an
4553 overlay string to deliver. Called by set_iterator_to_next when the
4554 end of the current overlay string is reached. If there are more
4555 overlay strings to display, IT->string and
4556 IT->current.overlay_string_index are set appropriately here.
4557 Otherwise IT->string is set to nil. */
4558
4559 static void
4560 next_overlay_string (it)
4561 struct it *it;
4562 {
4563 ++it->current.overlay_string_index;
4564 if (it->current.overlay_string_index == it->n_overlay_strings)
4565 {
4566 /* No more overlay strings. Restore IT's settings to what
4567 they were before overlay strings were processed, and
4568 continue to deliver from current_buffer. */
4569 int display_ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
4570
4571 pop_it (it);
4572 xassert (it->sp > 0
4573 || it->method == GET_FROM_COMPOSITION
4574 || (NILP (it->string)
4575 && it->method == GET_FROM_BUFFER
4576 && it->stop_charpos >= BEGV
4577 && it->stop_charpos <= it->end_charpos));
4578 it->current.overlay_string_index = -1;
4579 it->n_overlay_strings = 0;
4580
4581 /* If we're at the end of the buffer, record that we have
4582 processed the overlay strings there already, so that
4583 next_element_from_buffer doesn't try it again. */
4584 if (IT_CHARPOS (*it) >= it->end_charpos)
4585 it->overlay_strings_at_end_processed_p = 1;
4586
4587 /* If we have to display `...' for invisible text, set
4588 the iterator up for that. */
4589 if (display_ellipsis_p)
4590 setup_for_ellipsis (it, 0);
4591 }
4592 else
4593 {
4594 /* There are more overlay strings to process. If
4595 IT->current.overlay_string_index has advanced to a position
4596 where we must load IT->overlay_strings with more strings, do
4597 it. */
4598 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4599
4600 if (it->current.overlay_string_index && i == 0)
4601 load_overlay_strings (it, 0);
4602
4603 /* Initialize IT to deliver display elements from the overlay
4604 string. */
4605 it->string = it->overlay_strings[i];
4606 it->multibyte_p = STRING_MULTIBYTE (it->string);
4607 SET_TEXT_POS (it->current.string_pos, 0, 0);
4608 it->method = GET_FROM_STRING;
4609 it->stop_charpos = 0;
4610 }
4611
4612 CHECK_IT (it);
4613 }
4614
4615
4616 /* Compare two overlay_entry structures E1 and E2. Used as a
4617 comparison function for qsort in load_overlay_strings. Overlay
4618 strings for the same position are sorted so that
4619
4620 1. All after-strings come in front of before-strings, except
4621 when they come from the same overlay.
4622
4623 2. Within after-strings, strings are sorted so that overlay strings
4624 from overlays with higher priorities come first.
4625
4626 2. Within before-strings, strings are sorted so that overlay
4627 strings from overlays with higher priorities come last.
4628
4629 Value is analogous to strcmp. */
4630
4631
4632 static int
4633 compare_overlay_entries (e1, e2)
4634 void *e1, *e2;
4635 {
4636 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4637 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4638 int result;
4639
4640 if (entry1->after_string_p != entry2->after_string_p)
4641 {
4642 /* Let after-strings appear in front of before-strings if
4643 they come from different overlays. */
4644 if (EQ (entry1->overlay, entry2->overlay))
4645 result = entry1->after_string_p ? 1 : -1;
4646 else
4647 result = entry1->after_string_p ? -1 : 1;
4648 }
4649 else if (entry1->after_string_p)
4650 /* After-strings sorted in order of decreasing priority. */
4651 result = entry2->priority - entry1->priority;
4652 else
4653 /* Before-strings sorted in order of increasing priority. */
4654 result = entry1->priority - entry2->priority;
4655
4656 return result;
4657 }
4658
4659
4660 /* Load the vector IT->overlay_strings with overlay strings from IT's
4661 current buffer position, or from CHARPOS if that is > 0. Set
4662 IT->n_overlays to the total number of overlay strings found.
4663
4664 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4665 a time. On entry into load_overlay_strings,
4666 IT->current.overlay_string_index gives the number of overlay
4667 strings that have already been loaded by previous calls to this
4668 function.
4669
4670 IT->add_overlay_start contains an additional overlay start
4671 position to consider for taking overlay strings from, if non-zero.
4672 This position comes into play when the overlay has an `invisible'
4673 property, and both before and after-strings. When we've skipped to
4674 the end of the overlay, because of its `invisible' property, we
4675 nevertheless want its before-string to appear.
4676 IT->add_overlay_start will contain the overlay start position
4677 in this case.
4678
4679 Overlay strings are sorted so that after-string strings come in
4680 front of before-string strings. Within before and after-strings,
4681 strings are sorted by overlay priority. See also function
4682 compare_overlay_entries. */
4683
4684 static void
4685 load_overlay_strings (it, charpos)
4686 struct it *it;
4687 int charpos;
4688 {
4689 extern Lisp_Object Qafter_string, Qbefore_string, Qwindow, Qpriority;
4690 Lisp_Object overlay, window, str, invisible;
4691 struct Lisp_Overlay *ov;
4692 int start, end;
4693 int size = 20;
4694 int n = 0, i, j, invis_p;
4695 struct overlay_entry *entries
4696 = (struct overlay_entry *) alloca (size * sizeof *entries);
4697
4698 if (charpos <= 0)
4699 charpos = IT_CHARPOS (*it);
4700
4701 /* Append the overlay string STRING of overlay OVERLAY to vector
4702 `entries' which has size `size' and currently contains `n'
4703 elements. AFTER_P non-zero means STRING is an after-string of
4704 OVERLAY. */
4705 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
4706 do \
4707 { \
4708 Lisp_Object priority; \
4709 \
4710 if (n == size) \
4711 { \
4712 int new_size = 2 * size; \
4713 struct overlay_entry *old = entries; \
4714 entries = \
4715 (struct overlay_entry *) alloca (new_size \
4716 * sizeof *entries); \
4717 bcopy (old, entries, size * sizeof *entries); \
4718 size = new_size; \
4719 } \
4720 \
4721 entries[n].string = (STRING); \
4722 entries[n].overlay = (OVERLAY); \
4723 priority = Foverlay_get ((OVERLAY), Qpriority); \
4724 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
4725 entries[n].after_string_p = (AFTER_P); \
4726 ++n; \
4727 } \
4728 while (0)
4729
4730 /* Process overlay before the overlay center. */
4731 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
4732 {
4733 XSETMISC (overlay, ov);
4734 xassert (OVERLAYP (overlay));
4735 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4736 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4737
4738 if (end < charpos)
4739 break;
4740
4741 /* Skip this overlay if it doesn't start or end at IT's current
4742 position. */
4743 if (end != charpos && start != charpos)
4744 continue;
4745
4746 /* Skip this overlay if it doesn't apply to IT->w. */
4747 window = Foverlay_get (overlay, Qwindow);
4748 if (WINDOWP (window) && XWINDOW (window) != it->w)
4749 continue;
4750
4751 /* If the text ``under'' the overlay is invisible, both before-
4752 and after-strings from this overlay are visible; start and
4753 end position are indistinguishable. */
4754 invisible = Foverlay_get (overlay, Qinvisible);
4755 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4756
4757 /* If overlay has a non-empty before-string, record it. */
4758 if ((start == charpos || (end == charpos && invis_p))
4759 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4760 && SCHARS (str))
4761 RECORD_OVERLAY_STRING (overlay, str, 0);
4762
4763 /* If overlay has a non-empty after-string, record it. */
4764 if ((end == charpos || (start == charpos && invis_p))
4765 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4766 && SCHARS (str))
4767 RECORD_OVERLAY_STRING (overlay, str, 1);
4768 }
4769
4770 /* Process overlays after the overlay center. */
4771 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
4772 {
4773 XSETMISC (overlay, ov);
4774 xassert (OVERLAYP (overlay));
4775 start = OVERLAY_POSITION (OVERLAY_START (overlay));
4776 end = OVERLAY_POSITION (OVERLAY_END (overlay));
4777
4778 if (start > charpos)
4779 break;
4780
4781 /* Skip this overlay if it doesn't start or end at IT's current
4782 position. */
4783 if (end != charpos && start != charpos)
4784 continue;
4785
4786 /* Skip this overlay if it doesn't apply to IT->w. */
4787 window = Foverlay_get (overlay, Qwindow);
4788 if (WINDOWP (window) && XWINDOW (window) != it->w)
4789 continue;
4790
4791 /* If the text ``under'' the overlay is invisible, it has a zero
4792 dimension, and both before- and after-strings apply. */
4793 invisible = Foverlay_get (overlay, Qinvisible);
4794 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
4795
4796 /* If overlay has a non-empty before-string, record it. */
4797 if ((start == charpos || (end == charpos && invis_p))
4798 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
4799 && SCHARS (str))
4800 RECORD_OVERLAY_STRING (overlay, str, 0);
4801
4802 /* If overlay has a non-empty after-string, record it. */
4803 if ((end == charpos || (start == charpos && invis_p))
4804 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
4805 && SCHARS (str))
4806 RECORD_OVERLAY_STRING (overlay, str, 1);
4807 }
4808
4809 #undef RECORD_OVERLAY_STRING
4810
4811 /* Sort entries. */
4812 if (n > 1)
4813 qsort (entries, n, sizeof *entries, compare_overlay_entries);
4814
4815 /* Record the total number of strings to process. */
4816 it->n_overlay_strings = n;
4817
4818 /* IT->current.overlay_string_index is the number of overlay strings
4819 that have already been consumed by IT. Copy some of the
4820 remaining overlay strings to IT->overlay_strings. */
4821 i = 0;
4822 j = it->current.overlay_string_index;
4823 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
4824 it->overlay_strings[i++] = entries[j++].string;
4825
4826 CHECK_IT (it);
4827 }
4828
4829
4830 /* Get the first chunk of overlay strings at IT's current buffer
4831 position, or at CHARPOS if that is > 0. Value is non-zero if at
4832 least one overlay string was found. */
4833
4834 static int
4835 get_overlay_strings_1 (it, charpos, compute_stop_p)
4836 struct it *it;
4837 int charpos;
4838 {
4839 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
4840 process. This fills IT->overlay_strings with strings, and sets
4841 IT->n_overlay_strings to the total number of strings to process.
4842 IT->pos.overlay_string_index has to be set temporarily to zero
4843 because load_overlay_strings needs this; it must be set to -1
4844 when no overlay strings are found because a zero value would
4845 indicate a position in the first overlay string. */
4846 it->current.overlay_string_index = 0;
4847 load_overlay_strings (it, charpos);
4848
4849 /* If we found overlay strings, set up IT to deliver display
4850 elements from the first one. Otherwise set up IT to deliver
4851 from current_buffer. */
4852 if (it->n_overlay_strings)
4853 {
4854 /* Make sure we know settings in current_buffer, so that we can
4855 restore meaningful values when we're done with the overlay
4856 strings. */
4857 if (compute_stop_p)
4858 compute_stop_pos (it);
4859 xassert (it->face_id >= 0);
4860
4861 /* Save IT's settings. They are restored after all overlay
4862 strings have been processed. */
4863 xassert (!compute_stop_p || it->sp == 0);
4864 push_it (it);
4865
4866 /* Set up IT to deliver display elements from the first overlay
4867 string. */
4868 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4869 it->string = it->overlay_strings[0];
4870 it->stop_charpos = 0;
4871 xassert (STRINGP (it->string));
4872 it->end_charpos = SCHARS (it->string);
4873 it->multibyte_p = STRING_MULTIBYTE (it->string);
4874 it->method = GET_FROM_STRING;
4875 return 1;
4876 }
4877
4878 it->current.overlay_string_index = -1;
4879 return 0;
4880 }
4881
4882 static int
4883 get_overlay_strings (it, charpos)
4884 struct it *it;
4885 int charpos;
4886 {
4887 it->string = Qnil;
4888 it->method = GET_FROM_BUFFER;
4889
4890 (void) get_overlay_strings_1 (it, charpos, 1);
4891
4892 CHECK_IT (it);
4893
4894 /* Value is non-zero if we found at least one overlay string. */
4895 return STRINGP (it->string);
4896 }
4897
4898
4899 \f
4900 /***********************************************************************
4901 Saving and restoring state
4902 ***********************************************************************/
4903
4904 /* Save current settings of IT on IT->stack. Called, for example,
4905 before setting up IT for an overlay string, to be able to restore
4906 IT's settings to what they were after the overlay string has been
4907 processed. */
4908
4909 static void
4910 push_it (it)
4911 struct it *it;
4912 {
4913 struct iterator_stack_entry *p;
4914
4915 xassert (it->sp < IT_STACK_SIZE);
4916 p = it->stack + it->sp;
4917
4918 p->stop_charpos = it->stop_charpos;
4919 xassert (it->face_id >= 0);
4920 p->face_id = it->face_id;
4921 p->string = it->string;
4922 p->method = it->method;
4923 switch (p->method)
4924 {
4925 case GET_FROM_IMAGE:
4926 p->u.image.object = it->object;
4927 p->u.image.image_id = it->image_id;
4928 p->u.image.slice = it->slice;
4929 break;
4930 case GET_FROM_COMPOSITION:
4931 p->u.comp.object = it->object;
4932 p->u.comp.c = it->c;
4933 p->u.comp.len = it->len;
4934 p->u.comp.cmp_id = it->cmp_id;
4935 p->u.comp.cmp_len = it->cmp_len;
4936 break;
4937 case GET_FROM_STRETCH:
4938 p->u.stretch.object = it->object;
4939 break;
4940 }
4941 p->position = it->position;
4942 p->current = it->current;
4943 p->end_charpos = it->end_charpos;
4944 p->string_nchars = it->string_nchars;
4945 p->area = it->area;
4946 p->multibyte_p = it->multibyte_p;
4947 p->space_width = it->space_width;
4948 p->font_height = it->font_height;
4949 p->voffset = it->voffset;
4950 p->string_from_display_prop_p = it->string_from_display_prop_p;
4951 p->display_ellipsis_p = 0;
4952 ++it->sp;
4953 }
4954
4955
4956 /* Restore IT's settings from IT->stack. Called, for example, when no
4957 more overlay strings must be processed, and we return to delivering
4958 display elements from a buffer, or when the end of a string from a
4959 `display' property is reached and we return to delivering display
4960 elements from an overlay string, or from a buffer. */
4961
4962 static void
4963 pop_it (it)
4964 struct it *it;
4965 {
4966 struct iterator_stack_entry *p;
4967
4968 xassert (it->sp > 0);
4969 --it->sp;
4970 p = it->stack + it->sp;
4971 it->stop_charpos = p->stop_charpos;
4972 it->face_id = p->face_id;
4973 it->current = p->current;
4974 it->position = p->position;
4975 it->string = p->string;
4976 if (NILP (it->string))
4977 SET_TEXT_POS (it->current.string_pos, -1, -1);
4978 it->method = p->method;
4979 switch (it->method)
4980 {
4981 case GET_FROM_IMAGE:
4982 it->image_id = p->u.image.image_id;
4983 it->object = p->u.image.object;
4984 it->slice = p->u.image.slice;
4985 break;
4986 case GET_FROM_COMPOSITION:
4987 it->object = p->u.comp.object;
4988 it->c = p->u.comp.c;
4989 it->len = p->u.comp.len;
4990 it->cmp_id = p->u.comp.cmp_id;
4991 it->cmp_len = p->u.comp.cmp_len;
4992 break;
4993 case GET_FROM_STRETCH:
4994 it->object = p->u.comp.object;
4995 break;
4996 case GET_FROM_BUFFER:
4997 it->object = it->w->buffer;
4998 break;
4999 case GET_FROM_STRING:
5000 it->object = it->string;
5001 break;
5002 }
5003 it->end_charpos = p->end_charpos;
5004 it->string_nchars = p->string_nchars;
5005 it->area = p->area;
5006 it->multibyte_p = p->multibyte_p;
5007 it->space_width = p->space_width;
5008 it->font_height = p->font_height;
5009 it->voffset = p->voffset;
5010 it->string_from_display_prop_p = p->string_from_display_prop_p;
5011 }
5012
5013
5014 \f
5015 /***********************************************************************
5016 Moving over lines
5017 ***********************************************************************/
5018
5019 /* Set IT's current position to the previous line start. */
5020
5021 static void
5022 back_to_previous_line_start (it)
5023 struct it *it;
5024 {
5025 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5026 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5027 }
5028
5029
5030 /* Move IT to the next line start.
5031
5032 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5033 we skipped over part of the text (as opposed to moving the iterator
5034 continuously over the text). Otherwise, don't change the value
5035 of *SKIPPED_P.
5036
5037 Newlines may come from buffer text, overlay strings, or strings
5038 displayed via the `display' property. That's the reason we can't
5039 simply use find_next_newline_no_quit.
5040
5041 Note that this function may not skip over invisible text that is so
5042 because of text properties and immediately follows a newline. If
5043 it would, function reseat_at_next_visible_line_start, when called
5044 from set_iterator_to_next, would effectively make invisible
5045 characters following a newline part of the wrong glyph row, which
5046 leads to wrong cursor motion. */
5047
5048 static int
5049 forward_to_next_line_start (it, skipped_p)
5050 struct it *it;
5051 int *skipped_p;
5052 {
5053 int old_selective, newline_found_p, n;
5054 const int MAX_NEWLINE_DISTANCE = 500;
5055
5056 /* If already on a newline, just consume it to avoid unintended
5057 skipping over invisible text below. */
5058 if (it->what == IT_CHARACTER
5059 && it->c == '\n'
5060 && CHARPOS (it->position) == IT_CHARPOS (*it))
5061 {
5062 set_iterator_to_next (it, 0);
5063 it->c = 0;
5064 return 1;
5065 }
5066
5067 /* Don't handle selective display in the following. It's (a)
5068 unnecessary because it's done by the caller, and (b) leads to an
5069 infinite recursion because next_element_from_ellipsis indirectly
5070 calls this function. */
5071 old_selective = it->selective;
5072 it->selective = 0;
5073
5074 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5075 from buffer text. */
5076 for (n = newline_found_p = 0;
5077 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5078 n += STRINGP (it->string) ? 0 : 1)
5079 {
5080 if (!get_next_display_element (it))
5081 return 0;
5082 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5083 set_iterator_to_next (it, 0);
5084 }
5085
5086 /* If we didn't find a newline near enough, see if we can use a
5087 short-cut. */
5088 if (!newline_found_p)
5089 {
5090 int start = IT_CHARPOS (*it);
5091 int limit = find_next_newline_no_quit (start, 1);
5092 Lisp_Object pos;
5093
5094 xassert (!STRINGP (it->string));
5095
5096 /* If there isn't any `display' property in sight, and no
5097 overlays, we can just use the position of the newline in
5098 buffer text. */
5099 if (it->stop_charpos >= limit
5100 || ((pos = Fnext_single_property_change (make_number (start),
5101 Qdisplay,
5102 Qnil, make_number (limit)),
5103 NILP (pos))
5104 && next_overlay_change (start) == ZV))
5105 {
5106 IT_CHARPOS (*it) = limit;
5107 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5108 *skipped_p = newline_found_p = 1;
5109 }
5110 else
5111 {
5112 while (get_next_display_element (it)
5113 && !newline_found_p)
5114 {
5115 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5116 set_iterator_to_next (it, 0);
5117 }
5118 }
5119 }
5120
5121 it->selective = old_selective;
5122 return newline_found_p;
5123 }
5124
5125
5126 /* Set IT's current position to the previous visible line start. Skip
5127 invisible text that is so either due to text properties or due to
5128 selective display. Caution: this does not change IT->current_x and
5129 IT->hpos. */
5130
5131 static void
5132 back_to_previous_visible_line_start (it)
5133 struct it *it;
5134 {
5135 while (IT_CHARPOS (*it) > BEGV)
5136 {
5137 back_to_previous_line_start (it);
5138
5139 if (IT_CHARPOS (*it) <= BEGV)
5140 break;
5141
5142 /* If selective > 0, then lines indented more than that values
5143 are invisible. */
5144 if (it->selective > 0
5145 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5146 (double) it->selective)) /* iftc */
5147 continue;
5148
5149 /* Check the newline before point for invisibility. */
5150 {
5151 Lisp_Object prop;
5152 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5153 Qinvisible, it->window);
5154 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5155 continue;
5156 }
5157
5158 if (IT_CHARPOS (*it) <= BEGV)
5159 break;
5160
5161 {
5162 struct it it2;
5163 int pos;
5164 int beg, end;
5165 Lisp_Object val, overlay;
5166
5167 /* If newline is part of a composition, continue from start of composition */
5168 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5169 && beg < IT_CHARPOS (*it))
5170 goto replaced;
5171
5172 /* If newline is replaced by a display property, find start of overlay
5173 or interval and continue search from that point. */
5174 it2 = *it;
5175 pos = --IT_CHARPOS (it2);
5176 --IT_BYTEPOS (it2);
5177 it2.sp = 0;
5178 if (handle_display_prop (&it2) == HANDLED_RETURN
5179 && !NILP (val = get_char_property_and_overlay
5180 (make_number (pos), Qdisplay, Qnil, &overlay))
5181 && (OVERLAYP (overlay)
5182 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5183 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5184 goto replaced;
5185
5186 /* Newline is not replaced by anything -- so we are done. */
5187 break;
5188
5189 replaced:
5190 if (beg < BEGV)
5191 beg = BEGV;
5192 IT_CHARPOS (*it) = beg;
5193 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5194 }
5195 }
5196
5197 it->continuation_lines_width = 0;
5198
5199 xassert (IT_CHARPOS (*it) >= BEGV);
5200 xassert (IT_CHARPOS (*it) == BEGV
5201 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5202 CHECK_IT (it);
5203 }
5204
5205
5206 /* Reseat iterator IT at the previous visible line start. Skip
5207 invisible text that is so either due to text properties or due to
5208 selective display. At the end, update IT's overlay information,
5209 face information etc. */
5210
5211 void
5212 reseat_at_previous_visible_line_start (it)
5213 struct it *it;
5214 {
5215 back_to_previous_visible_line_start (it);
5216 reseat (it, it->current.pos, 1);
5217 CHECK_IT (it);
5218 }
5219
5220
5221 /* Reseat iterator IT on the next visible line start in the current
5222 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5223 preceding the line start. Skip over invisible text that is so
5224 because of selective display. Compute faces, overlays etc at the
5225 new position. Note that this function does not skip over text that
5226 is invisible because of text properties. */
5227
5228 static void
5229 reseat_at_next_visible_line_start (it, on_newline_p)
5230 struct it *it;
5231 int on_newline_p;
5232 {
5233 int newline_found_p, skipped_p = 0;
5234
5235 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5236
5237 /* Skip over lines that are invisible because they are indented
5238 more than the value of IT->selective. */
5239 if (it->selective > 0)
5240 while (IT_CHARPOS (*it) < ZV
5241 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5242 (double) it->selective)) /* iftc */
5243 {
5244 xassert (IT_BYTEPOS (*it) == BEGV
5245 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5246 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5247 }
5248
5249 /* Position on the newline if that's what's requested. */
5250 if (on_newline_p && newline_found_p)
5251 {
5252 if (STRINGP (it->string))
5253 {
5254 if (IT_STRING_CHARPOS (*it) > 0)
5255 {
5256 --IT_STRING_CHARPOS (*it);
5257 --IT_STRING_BYTEPOS (*it);
5258 }
5259 }
5260 else if (IT_CHARPOS (*it) > BEGV)
5261 {
5262 --IT_CHARPOS (*it);
5263 --IT_BYTEPOS (*it);
5264 reseat (it, it->current.pos, 0);
5265 }
5266 }
5267 else if (skipped_p)
5268 reseat (it, it->current.pos, 0);
5269
5270 CHECK_IT (it);
5271 }
5272
5273
5274 \f
5275 /***********************************************************************
5276 Changing an iterator's position
5277 ***********************************************************************/
5278
5279 /* Change IT's current position to POS in current_buffer. If FORCE_P
5280 is non-zero, always check for text properties at the new position.
5281 Otherwise, text properties are only looked up if POS >=
5282 IT->check_charpos of a property. */
5283
5284 static void
5285 reseat (it, pos, force_p)
5286 struct it *it;
5287 struct text_pos pos;
5288 int force_p;
5289 {
5290 int original_pos = IT_CHARPOS (*it);
5291
5292 reseat_1 (it, pos, 0);
5293
5294 /* Determine where to check text properties. Avoid doing it
5295 where possible because text property lookup is very expensive. */
5296 if (force_p
5297 || CHARPOS (pos) > it->stop_charpos
5298 || CHARPOS (pos) < original_pos)
5299 handle_stop (it);
5300
5301 CHECK_IT (it);
5302 }
5303
5304
5305 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5306 IT->stop_pos to POS, also. */
5307
5308 static void
5309 reseat_1 (it, pos, set_stop_p)
5310 struct it *it;
5311 struct text_pos pos;
5312 int set_stop_p;
5313 {
5314 /* Don't call this function when scanning a C string. */
5315 xassert (it->s == NULL);
5316
5317 /* POS must be a reasonable value. */
5318 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5319
5320 it->current.pos = it->position = pos;
5321 it->end_charpos = ZV;
5322 it->dpvec = NULL;
5323 it->current.dpvec_index = -1;
5324 it->current.overlay_string_index = -1;
5325 IT_STRING_CHARPOS (*it) = -1;
5326 IT_STRING_BYTEPOS (*it) = -1;
5327 it->string = Qnil;
5328 it->method = GET_FROM_BUFFER;
5329 it->object = it->w->buffer;
5330 it->area = TEXT_AREA;
5331 it->multibyte_p = !NILP (current_buffer->enable_multibyte_characters);
5332 it->sp = 0;
5333 it->string_from_display_prop_p = 0;
5334 it->face_before_selective_p = 0;
5335
5336 if (set_stop_p)
5337 it->stop_charpos = CHARPOS (pos);
5338 }
5339
5340
5341 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5342 If S is non-null, it is a C string to iterate over. Otherwise,
5343 STRING gives a Lisp string to iterate over.
5344
5345 If PRECISION > 0, don't return more then PRECISION number of
5346 characters from the string.
5347
5348 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5349 characters have been returned. FIELD_WIDTH < 0 means an infinite
5350 field width.
5351
5352 MULTIBYTE = 0 means disable processing of multibyte characters,
5353 MULTIBYTE > 0 means enable it,
5354 MULTIBYTE < 0 means use IT->multibyte_p.
5355
5356 IT must be initialized via a prior call to init_iterator before
5357 calling this function. */
5358
5359 static void
5360 reseat_to_string (it, s, string, charpos, precision, field_width, multibyte)
5361 struct it *it;
5362 unsigned char *s;
5363 Lisp_Object string;
5364 int charpos;
5365 int precision, field_width, multibyte;
5366 {
5367 /* No region in strings. */
5368 it->region_beg_charpos = it->region_end_charpos = -1;
5369
5370 /* No text property checks performed by default, but see below. */
5371 it->stop_charpos = -1;
5372
5373 /* Set iterator position and end position. */
5374 bzero (&it->current, sizeof it->current);
5375 it->current.overlay_string_index = -1;
5376 it->current.dpvec_index = -1;
5377 xassert (charpos >= 0);
5378
5379 /* If STRING is specified, use its multibyteness, otherwise use the
5380 setting of MULTIBYTE, if specified. */
5381 if (multibyte >= 0)
5382 it->multibyte_p = multibyte > 0;
5383
5384 if (s == NULL)
5385 {
5386 xassert (STRINGP (string));
5387 it->string = string;
5388 it->s = NULL;
5389 it->end_charpos = it->string_nchars = SCHARS (string);
5390 it->method = GET_FROM_STRING;
5391 it->current.string_pos = string_pos (charpos, string);
5392 }
5393 else
5394 {
5395 it->s = s;
5396 it->string = Qnil;
5397
5398 /* Note that we use IT->current.pos, not it->current.string_pos,
5399 for displaying C strings. */
5400 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5401 if (it->multibyte_p)
5402 {
5403 it->current.pos = c_string_pos (charpos, s, 1);
5404 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5405 }
5406 else
5407 {
5408 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5409 it->end_charpos = it->string_nchars = strlen (s);
5410 }
5411
5412 it->method = GET_FROM_C_STRING;
5413 }
5414
5415 /* PRECISION > 0 means don't return more than PRECISION characters
5416 from the string. */
5417 if (precision > 0 && it->end_charpos - charpos > precision)
5418 it->end_charpos = it->string_nchars = charpos + precision;
5419
5420 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5421 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5422 FIELD_WIDTH < 0 means infinite field width. This is useful for
5423 padding with `-' at the end of a mode line. */
5424 if (field_width < 0)
5425 field_width = INFINITY;
5426 if (field_width > it->end_charpos - charpos)
5427 it->end_charpos = charpos + field_width;
5428
5429 /* Use the standard display table for displaying strings. */
5430 if (DISP_TABLE_P (Vstandard_display_table))
5431 it->dp = XCHAR_TABLE (Vstandard_display_table);
5432
5433 it->stop_charpos = charpos;
5434 CHECK_IT (it);
5435 }
5436
5437
5438 \f
5439 /***********************************************************************
5440 Iteration
5441 ***********************************************************************/
5442
5443 /* Map enum it_method value to corresponding next_element_from_* function. */
5444
5445 static int (* get_next_element[NUM_IT_METHODS]) P_ ((struct it *it)) =
5446 {
5447 next_element_from_buffer,
5448 next_element_from_display_vector,
5449 next_element_from_composition,
5450 next_element_from_string,
5451 next_element_from_c_string,
5452 next_element_from_image,
5453 next_element_from_stretch
5454 };
5455
5456
5457 /* Load IT's display element fields with information about the next
5458 display element from the current position of IT. Value is zero if
5459 end of buffer (or C string) is reached. */
5460
5461 static struct frame *last_escape_glyph_frame = NULL;
5462 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
5463 static int last_escape_glyph_merged_face_id = 0;
5464
5465 int
5466 get_next_display_element (it)
5467 struct it *it;
5468 {
5469 /* Non-zero means that we found a display element. Zero means that
5470 we hit the end of what we iterate over. Performance note: the
5471 function pointer `method' used here turns out to be faster than
5472 using a sequence of if-statements. */
5473 int success_p;
5474
5475 get_next:
5476 success_p = (*get_next_element[it->method]) (it);
5477
5478 if (it->what == IT_CHARACTER)
5479 {
5480 /* Map via display table or translate control characters.
5481 IT->c, IT->len etc. have been set to the next character by
5482 the function call above. If we have a display table, and it
5483 contains an entry for IT->c, translate it. Don't do this if
5484 IT->c itself comes from a display table, otherwise we could
5485 end up in an infinite recursion. (An alternative could be to
5486 count the recursion depth of this function and signal an
5487 error when a certain maximum depth is reached.) Is it worth
5488 it? */
5489 if (success_p && it->dpvec == NULL)
5490 {
5491 Lisp_Object dv;
5492
5493 if (it->dp
5494 && (dv = DISP_CHAR_VECTOR (it->dp, it->c),
5495 VECTORP (dv)))
5496 {
5497 struct Lisp_Vector *v = XVECTOR (dv);
5498
5499 /* Return the first character from the display table
5500 entry, if not empty. If empty, don't display the
5501 current character. */
5502 if (v->size)
5503 {
5504 it->dpvec_char_len = it->len;
5505 it->dpvec = v->contents;
5506 it->dpend = v->contents + v->size;
5507 it->current.dpvec_index = 0;
5508 it->dpvec_face_id = -1;
5509 it->saved_face_id = it->face_id;
5510 it->method = GET_FROM_DISPLAY_VECTOR;
5511 it->ellipsis_p = 0;
5512 }
5513 else
5514 {
5515 set_iterator_to_next (it, 0);
5516 }
5517 goto get_next;
5518 }
5519
5520 /* Translate control characters into `\003' or `^C' form.
5521 Control characters coming from a display table entry are
5522 currently not translated because we use IT->dpvec to hold
5523 the translation. This could easily be changed but I
5524 don't believe that it is worth doing.
5525
5526 If it->multibyte_p is nonzero, eight-bit characters and
5527 non-printable multibyte characters are also translated to
5528 octal form.
5529
5530 If it->multibyte_p is zero, eight-bit characters that
5531 don't have corresponding multibyte char code are also
5532 translated to octal form. */
5533 else if ((it->c < ' '
5534 && (it->area != TEXT_AREA
5535 /* In mode line, treat \n like other crl chars. */
5536 || (it->c != '\t'
5537 && it->glyph_row && it->glyph_row->mode_line_p)
5538 || (it->c != '\n' && it->c != '\t')))
5539 || (it->multibyte_p
5540 ? ((it->c >= 127
5541 && it->len == 1)
5542 || !CHAR_PRINTABLE_P (it->c)
5543 || (!NILP (Vnobreak_char_display)
5544 && (it->c == 0x8a0 || it->c == 0x8ad
5545 || it->c == 0x920 || it->c == 0x92d
5546 || it->c == 0xe20 || it->c == 0xe2d
5547 || it->c == 0xf20 || it->c == 0xf2d)))
5548 : (it->c >= 127
5549 && (!unibyte_display_via_language_environment
5550 || it->c == unibyte_char_to_multibyte (it->c)))))
5551 {
5552 /* IT->c is a control character which must be displayed
5553 either as '\003' or as `^C' where the '\\' and '^'
5554 can be defined in the display table. Fill
5555 IT->ctl_chars with glyphs for what we have to
5556 display. Then, set IT->dpvec to these glyphs. */
5557 GLYPH g;
5558 int ctl_len;
5559 int face_id, lface_id = 0 ;
5560 GLYPH escape_glyph;
5561
5562 /* Handle control characters with ^. */
5563
5564 if (it->c < 128 && it->ctl_arrow_p)
5565 {
5566 g = '^'; /* default glyph for Control */
5567 /* Set IT->ctl_chars[0] to the glyph for `^'. */
5568 if (it->dp
5569 && INTEGERP (DISP_CTRL_GLYPH (it->dp))
5570 && GLYPH_CHAR_VALID_P (XINT (DISP_CTRL_GLYPH (it->dp))))
5571 {
5572 g = XINT (DISP_CTRL_GLYPH (it->dp));
5573 lface_id = FAST_GLYPH_FACE (g);
5574 }
5575 if (lface_id)
5576 {
5577 g = FAST_GLYPH_CHAR (g);
5578 face_id = merge_faces (it->f, Qt, lface_id,
5579 it->face_id);
5580 }
5581 else if (it->f == last_escape_glyph_frame
5582 && it->face_id == last_escape_glyph_face_id)
5583 {
5584 face_id = last_escape_glyph_merged_face_id;
5585 }
5586 else
5587 {
5588 /* Merge the escape-glyph face into the current face. */
5589 face_id = merge_faces (it->f, Qescape_glyph, 0,
5590 it->face_id);
5591 last_escape_glyph_frame = it->f;
5592 last_escape_glyph_face_id = it->face_id;
5593 last_escape_glyph_merged_face_id = face_id;
5594 }
5595
5596 XSETINT (it->ctl_chars[0], g);
5597 g = it->c ^ 0100;
5598 XSETINT (it->ctl_chars[1], g);
5599 ctl_len = 2;
5600 goto display_control;
5601 }
5602
5603 /* Handle non-break space in the mode where it only gets
5604 highlighting. */
5605
5606 if (EQ (Vnobreak_char_display, Qt)
5607 && (it->c == 0x8a0 || it->c == 0x920
5608 || it->c == 0xe20 || it->c == 0xf20))
5609 {
5610 /* Merge the no-break-space face into the current face. */
5611 face_id = merge_faces (it->f, Qnobreak_space, 0,
5612 it->face_id);
5613
5614 g = it->c = ' ';
5615 XSETINT (it->ctl_chars[0], g);
5616 ctl_len = 1;
5617 goto display_control;
5618 }
5619
5620 /* Handle sequences that start with the "escape glyph". */
5621
5622 /* the default escape glyph is \. */
5623 escape_glyph = '\\';
5624
5625 if (it->dp
5626 && INTEGERP (DISP_ESCAPE_GLYPH (it->dp))
5627 && GLYPH_CHAR_VALID_P (XFASTINT (DISP_ESCAPE_GLYPH (it->dp))))
5628 {
5629 escape_glyph = XFASTINT (DISP_ESCAPE_GLYPH (it->dp));
5630 lface_id = FAST_GLYPH_FACE (escape_glyph);
5631 }
5632 if (lface_id)
5633 {
5634 /* The display table specified a face.
5635 Merge it into face_id and also into escape_glyph. */
5636 escape_glyph = FAST_GLYPH_CHAR (escape_glyph);
5637 face_id = merge_faces (it->f, Qt, lface_id,
5638 it->face_id);
5639 }
5640 else if (it->f == last_escape_glyph_frame
5641 && it->face_id == last_escape_glyph_face_id)
5642 {
5643 face_id = last_escape_glyph_merged_face_id;
5644 }
5645 else
5646 {
5647 /* Merge the escape-glyph face into the current face. */
5648 face_id = merge_faces (it->f, Qescape_glyph, 0,
5649 it->face_id);
5650 last_escape_glyph_frame = it->f;
5651 last_escape_glyph_face_id = it->face_id;
5652 last_escape_glyph_merged_face_id = face_id;
5653 }
5654
5655 /* Handle soft hyphens in the mode where they only get
5656 highlighting. */
5657
5658 if (EQ (Vnobreak_char_display, Qt)
5659 && (it->c == 0x8ad || it->c == 0x92d
5660 || it->c == 0xe2d || it->c == 0xf2d))
5661 {
5662 g = it->c = '-';
5663 XSETINT (it->ctl_chars[0], g);
5664 ctl_len = 1;
5665 goto display_control;
5666 }
5667
5668 /* Handle non-break space and soft hyphen
5669 with the escape glyph. */
5670
5671 if (it->c == 0x8a0 || it->c == 0x8ad
5672 || it->c == 0x920 || it->c == 0x92d
5673 || it->c == 0xe20 || it->c == 0xe2d
5674 || it->c == 0xf20 || it->c == 0xf2d)
5675 {
5676 XSETINT (it->ctl_chars[0], escape_glyph);
5677 g = it->c = ((it->c & 0xf) == 0 ? ' ' : '-');
5678 XSETINT (it->ctl_chars[1], g);
5679 ctl_len = 2;
5680 goto display_control;
5681 }
5682
5683 {
5684 unsigned char str[MAX_MULTIBYTE_LENGTH];
5685 int len;
5686 int i;
5687
5688 /* Set IT->ctl_chars[0] to the glyph for `\\'. */
5689 if (SINGLE_BYTE_CHAR_P (it->c))
5690 str[0] = it->c, len = 1;
5691 else
5692 {
5693 len = CHAR_STRING_NO_SIGNAL (it->c, str);
5694 if (len < 0)
5695 {
5696 /* It's an invalid character, which shouldn't
5697 happen actually, but due to bugs it may
5698 happen. Let's print the char as is, there's
5699 not much meaningful we can do with it. */
5700 str[0] = it->c;
5701 str[1] = it->c >> 8;
5702 str[2] = it->c >> 16;
5703 str[3] = it->c >> 24;
5704 len = 4;
5705 }
5706 }
5707
5708 for (i = 0; i < len; i++)
5709 {
5710 XSETINT (it->ctl_chars[i * 4], escape_glyph);
5711 /* Insert three more glyphs into IT->ctl_chars for
5712 the octal display of the character. */
5713 g = ((str[i] >> 6) & 7) + '0';
5714 XSETINT (it->ctl_chars[i * 4 + 1], g);
5715 g = ((str[i] >> 3) & 7) + '0';
5716 XSETINT (it->ctl_chars[i * 4 + 2], g);
5717 g = (str[i] & 7) + '0';
5718 XSETINT (it->ctl_chars[i * 4 + 3], g);
5719 }
5720 ctl_len = len * 4;
5721 }
5722
5723 display_control:
5724 /* Set up IT->dpvec and return first character from it. */
5725 it->dpvec_char_len = it->len;
5726 it->dpvec = it->ctl_chars;
5727 it->dpend = it->dpvec + ctl_len;
5728 it->current.dpvec_index = 0;
5729 it->dpvec_face_id = face_id;
5730 it->saved_face_id = it->face_id;
5731 it->method = GET_FROM_DISPLAY_VECTOR;
5732 it->ellipsis_p = 0;
5733 goto get_next;
5734 }
5735 }
5736
5737 /* Adjust face id for a multibyte character. There are no
5738 multibyte character in unibyte text. */
5739 if (it->multibyte_p
5740 && success_p
5741 && FRAME_WINDOW_P (it->f))
5742 {
5743 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5744 it->face_id = FACE_FOR_CHAR (it->f, face, it->c);
5745 }
5746 }
5747
5748 /* Is this character the last one of a run of characters with
5749 box? If yes, set IT->end_of_box_run_p to 1. */
5750 if (it->face_box_p
5751 && it->s == NULL)
5752 {
5753 int face_id;
5754 struct face *face;
5755
5756 it->end_of_box_run_p
5757 = ((face_id = face_after_it_pos (it),
5758 face_id != it->face_id)
5759 && (face = FACE_FROM_ID (it->f, face_id),
5760 face->box == FACE_NO_BOX));
5761 }
5762
5763 /* Value is 0 if end of buffer or string reached. */
5764 return success_p;
5765 }
5766
5767
5768 /* Move IT to the next display element.
5769
5770 RESEAT_P non-zero means if called on a newline in buffer text,
5771 skip to the next visible line start.
5772
5773 Functions get_next_display_element and set_iterator_to_next are
5774 separate because I find this arrangement easier to handle than a
5775 get_next_display_element function that also increments IT's
5776 position. The way it is we can first look at an iterator's current
5777 display element, decide whether it fits on a line, and if it does,
5778 increment the iterator position. The other way around we probably
5779 would either need a flag indicating whether the iterator has to be
5780 incremented the next time, or we would have to implement a
5781 decrement position function which would not be easy to write. */
5782
5783 void
5784 set_iterator_to_next (it, reseat_p)
5785 struct it *it;
5786 int reseat_p;
5787 {
5788 /* Reset flags indicating start and end of a sequence of characters
5789 with box. Reset them at the start of this function because
5790 moving the iterator to a new position might set them. */
5791 it->start_of_box_run_p = it->end_of_box_run_p = 0;
5792
5793 switch (it->method)
5794 {
5795 case GET_FROM_BUFFER:
5796 /* The current display element of IT is a character from
5797 current_buffer. Advance in the buffer, and maybe skip over
5798 invisible lines that are so because of selective display. */
5799 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
5800 reseat_at_next_visible_line_start (it, 0);
5801 else
5802 {
5803 xassert (it->len != 0);
5804 IT_BYTEPOS (*it) += it->len;
5805 IT_CHARPOS (*it) += 1;
5806 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
5807 }
5808 break;
5809
5810 case GET_FROM_COMPOSITION:
5811 xassert (it->cmp_id >= 0 && it->cmp_id < n_compositions);
5812 xassert (it->sp > 0);
5813 pop_it (it);
5814 if (it->method == GET_FROM_STRING)
5815 {
5816 IT_STRING_BYTEPOS (*it) += it->len;
5817 IT_STRING_CHARPOS (*it) += it->cmp_len;
5818 goto consider_string_end;
5819 }
5820 else if (it->method == GET_FROM_BUFFER)
5821 {
5822 IT_BYTEPOS (*it) += it->len;
5823 IT_CHARPOS (*it) += it->cmp_len;
5824 }
5825 break;
5826
5827 case GET_FROM_C_STRING:
5828 /* Current display element of IT is from a C string. */
5829 IT_BYTEPOS (*it) += it->len;
5830 IT_CHARPOS (*it) += 1;
5831 break;
5832
5833 case GET_FROM_DISPLAY_VECTOR:
5834 /* Current display element of IT is from a display table entry.
5835 Advance in the display table definition. Reset it to null if
5836 end reached, and continue with characters from buffers/
5837 strings. */
5838 ++it->current.dpvec_index;
5839
5840 /* Restore face of the iterator to what they were before the
5841 display vector entry (these entries may contain faces). */
5842 it->face_id = it->saved_face_id;
5843
5844 if (it->dpvec + it->current.dpvec_index == it->dpend)
5845 {
5846 int recheck_faces = it->ellipsis_p;
5847
5848 if (it->s)
5849 it->method = GET_FROM_C_STRING;
5850 else if (STRINGP (it->string))
5851 it->method = GET_FROM_STRING;
5852 else
5853 {
5854 it->method = GET_FROM_BUFFER;
5855 it->object = it->w->buffer;
5856 }
5857
5858 it->dpvec = NULL;
5859 it->current.dpvec_index = -1;
5860
5861 /* Skip over characters which were displayed via IT->dpvec. */
5862 if (it->dpvec_char_len < 0)
5863 reseat_at_next_visible_line_start (it, 1);
5864 else if (it->dpvec_char_len > 0)
5865 {
5866 if (it->method == GET_FROM_STRING
5867 && it->n_overlay_strings > 0)
5868 it->ignore_overlay_strings_at_pos_p = 1;
5869 it->len = it->dpvec_char_len;
5870 set_iterator_to_next (it, reseat_p);
5871 }
5872
5873 /* Maybe recheck faces after display vector */
5874 if (recheck_faces)
5875 it->stop_charpos = IT_CHARPOS (*it);
5876 }
5877 break;
5878
5879 case GET_FROM_STRING:
5880 /* Current display element is a character from a Lisp string. */
5881 xassert (it->s == NULL && STRINGP (it->string));
5882 IT_STRING_BYTEPOS (*it) += it->len;
5883 IT_STRING_CHARPOS (*it) += 1;
5884
5885 consider_string_end:
5886
5887 if (it->current.overlay_string_index >= 0)
5888 {
5889 /* IT->string is an overlay string. Advance to the
5890 next, if there is one. */
5891 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
5892 next_overlay_string (it);
5893 }
5894 else
5895 {
5896 /* IT->string is not an overlay string. If we reached
5897 its end, and there is something on IT->stack, proceed
5898 with what is on the stack. This can be either another
5899 string, this time an overlay string, or a buffer. */
5900 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
5901 && it->sp > 0)
5902 {
5903 pop_it (it);
5904 if (it->method == GET_FROM_STRING)
5905 goto consider_string_end;
5906 }
5907 }
5908 break;
5909
5910 case GET_FROM_IMAGE:
5911 case GET_FROM_STRETCH:
5912 /* The position etc with which we have to proceed are on
5913 the stack. The position may be at the end of a string,
5914 if the `display' property takes up the whole string. */
5915 xassert (it->sp > 0);
5916 pop_it (it);
5917 if (it->method == GET_FROM_STRING)
5918 goto consider_string_end;
5919 break;
5920
5921 default:
5922 /* There are no other methods defined, so this should be a bug. */
5923 abort ();
5924 }
5925
5926 xassert (it->method != GET_FROM_STRING
5927 || (STRINGP (it->string)
5928 && IT_STRING_CHARPOS (*it) >= 0));
5929 }
5930
5931 /* Load IT's display element fields with information about the next
5932 display element which comes from a display table entry or from the
5933 result of translating a control character to one of the forms `^C'
5934 or `\003'.
5935
5936 IT->dpvec holds the glyphs to return as characters.
5937 IT->saved_face_id holds the face id before the display vector--
5938 it is restored into IT->face_idin set_iterator_to_next. */
5939
5940 static int
5941 next_element_from_display_vector (it)
5942 struct it *it;
5943 {
5944 /* Precondition. */
5945 xassert (it->dpvec && it->current.dpvec_index >= 0);
5946
5947 it->face_id = it->saved_face_id;
5948
5949 if (INTEGERP (*it->dpvec)
5950 && GLYPH_CHAR_VALID_P (XFASTINT (*it->dpvec)))
5951 {
5952 GLYPH g;
5953
5954 g = XFASTINT (it->dpvec[it->current.dpvec_index]);
5955 it->c = FAST_GLYPH_CHAR (g);
5956 it->len = CHAR_BYTES (it->c);
5957
5958 /* The entry may contain a face id to use. Such a face id is
5959 the id of a Lisp face, not a realized face. A face id of
5960 zero means no face is specified. */
5961 if (it->dpvec_face_id >= 0)
5962 it->face_id = it->dpvec_face_id;
5963 else
5964 {
5965 int lface_id = FAST_GLYPH_FACE (g);
5966 if (lface_id > 0)
5967 it->face_id = merge_faces (it->f, Qt, lface_id,
5968 it->saved_face_id);
5969 }
5970 }
5971 else
5972 /* Display table entry is invalid. Return a space. */
5973 it->c = ' ', it->len = 1;
5974
5975 /* Don't change position and object of the iterator here. They are
5976 still the values of the character that had this display table
5977 entry or was translated, and that's what we want. */
5978 it->what = IT_CHARACTER;
5979 return 1;
5980 }
5981
5982
5983 /* Load IT with the next display element from Lisp string IT->string.
5984 IT->current.string_pos is the current position within the string.
5985 If IT->current.overlay_string_index >= 0, the Lisp string is an
5986 overlay string. */
5987
5988 static int
5989 next_element_from_string (it)
5990 struct it *it;
5991 {
5992 struct text_pos position;
5993
5994 xassert (STRINGP (it->string));
5995 xassert (IT_STRING_CHARPOS (*it) >= 0);
5996 position = it->current.string_pos;
5997
5998 /* Time to check for invisible text? */
5999 if (IT_STRING_CHARPOS (*it) < it->end_charpos
6000 && IT_STRING_CHARPOS (*it) == it->stop_charpos)
6001 {
6002 handle_stop (it);
6003
6004 /* Since a handler may have changed IT->method, we must
6005 recurse here. */
6006 return get_next_display_element (it);
6007 }
6008
6009 if (it->current.overlay_string_index >= 0)
6010 {
6011 /* Get the next character from an overlay string. In overlay
6012 strings, There is no field width or padding with spaces to
6013 do. */
6014 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6015 {
6016 it->what = IT_EOB;
6017 return 0;
6018 }
6019 else if (STRING_MULTIBYTE (it->string))
6020 {
6021 int remaining = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6022 const unsigned char *s = (SDATA (it->string)
6023 + IT_STRING_BYTEPOS (*it));
6024 it->c = string_char_and_length (s, remaining, &it->len);
6025 }
6026 else
6027 {
6028 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6029 it->len = 1;
6030 }
6031 }
6032 else
6033 {
6034 /* Get the next character from a Lisp string that is not an
6035 overlay string. Such strings come from the mode line, for
6036 example. We may have to pad with spaces, or truncate the
6037 string. See also next_element_from_c_string. */
6038 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
6039 {
6040 it->what = IT_EOB;
6041 return 0;
6042 }
6043 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
6044 {
6045 /* Pad with spaces. */
6046 it->c = ' ', it->len = 1;
6047 CHARPOS (position) = BYTEPOS (position) = -1;
6048 }
6049 else if (STRING_MULTIBYTE (it->string))
6050 {
6051 int maxlen = SBYTES (it->string) - IT_STRING_BYTEPOS (*it);
6052 const unsigned char *s = (SDATA (it->string)
6053 + IT_STRING_BYTEPOS (*it));
6054 it->c = string_char_and_length (s, maxlen, &it->len);
6055 }
6056 else
6057 {
6058 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
6059 it->len = 1;
6060 }
6061 }
6062
6063 /* Record what we have and where it came from. */
6064 it->what = IT_CHARACTER;
6065 it->object = it->string;
6066 it->position = position;
6067 return 1;
6068 }
6069
6070
6071 /* Load IT with next display element from C string IT->s.
6072 IT->string_nchars is the maximum number of characters to return
6073 from the string. IT->end_charpos may be greater than
6074 IT->string_nchars when this function is called, in which case we
6075 may have to return padding spaces. Value is zero if end of string
6076 reached, including padding spaces. */
6077
6078 static int
6079 next_element_from_c_string (it)
6080 struct it *it;
6081 {
6082 int success_p = 1;
6083
6084 xassert (it->s);
6085 it->what = IT_CHARACTER;
6086 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
6087 it->object = Qnil;
6088
6089 /* IT's position can be greater IT->string_nchars in case a field
6090 width or precision has been specified when the iterator was
6091 initialized. */
6092 if (IT_CHARPOS (*it) >= it->end_charpos)
6093 {
6094 /* End of the game. */
6095 it->what = IT_EOB;
6096 success_p = 0;
6097 }
6098 else if (IT_CHARPOS (*it) >= it->string_nchars)
6099 {
6100 /* Pad with spaces. */
6101 it->c = ' ', it->len = 1;
6102 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
6103 }
6104 else if (it->multibyte_p)
6105 {
6106 /* Implementation note: The calls to strlen apparently aren't a
6107 performance problem because there is no noticeable performance
6108 difference between Emacs running in unibyte or multibyte mode. */
6109 int maxlen = strlen (it->s) - IT_BYTEPOS (*it);
6110 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it),
6111 maxlen, &it->len);
6112 }
6113 else
6114 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
6115
6116 return success_p;
6117 }
6118
6119
6120 /* Set up IT to return characters from an ellipsis, if appropriate.
6121 The definition of the ellipsis glyphs may come from a display table
6122 entry. This function Fills IT with the first glyph from the
6123 ellipsis if an ellipsis is to be displayed. */
6124
6125 static int
6126 next_element_from_ellipsis (it)
6127 struct it *it;
6128 {
6129 if (it->selective_display_ellipsis_p)
6130 setup_for_ellipsis (it, it->len);
6131 else
6132 {
6133 /* The face at the current position may be different from the
6134 face we find after the invisible text. Remember what it
6135 was in IT->saved_face_id, and signal that it's there by
6136 setting face_before_selective_p. */
6137 it->saved_face_id = it->face_id;
6138 it->method = GET_FROM_BUFFER;
6139 it->object = it->w->buffer;
6140 reseat_at_next_visible_line_start (it, 1);
6141 it->face_before_selective_p = 1;
6142 }
6143
6144 return get_next_display_element (it);
6145 }
6146
6147
6148 /* Deliver an image display element. The iterator IT is already
6149 filled with image information (done in handle_display_prop). Value
6150 is always 1. */
6151
6152
6153 static int
6154 next_element_from_image (it)
6155 struct it *it;
6156 {
6157 it->what = IT_IMAGE;
6158 return 1;
6159 }
6160
6161
6162 /* Fill iterator IT with next display element from a stretch glyph
6163 property. IT->object is the value of the text property. Value is
6164 always 1. */
6165
6166 static int
6167 next_element_from_stretch (it)
6168 struct it *it;
6169 {
6170 it->what = IT_STRETCH;
6171 return 1;
6172 }
6173
6174
6175 /* Load IT with the next display element from current_buffer. Value
6176 is zero if end of buffer reached. IT->stop_charpos is the next
6177 position at which to stop and check for text properties or buffer
6178 end. */
6179
6180 static int
6181 next_element_from_buffer (it)
6182 struct it *it;
6183 {
6184 int success_p = 1;
6185
6186 /* Check this assumption, otherwise, we would never enter the
6187 if-statement, below. */
6188 xassert (IT_CHARPOS (*it) >= BEGV
6189 && IT_CHARPOS (*it) <= it->stop_charpos);
6190
6191 if (IT_CHARPOS (*it) >= it->stop_charpos)
6192 {
6193 if (IT_CHARPOS (*it) >= it->end_charpos)
6194 {
6195 int overlay_strings_follow_p;
6196
6197 /* End of the game, except when overlay strings follow that
6198 haven't been returned yet. */
6199 if (it->overlay_strings_at_end_processed_p)
6200 overlay_strings_follow_p = 0;
6201 else
6202 {
6203 it->overlay_strings_at_end_processed_p = 1;
6204 overlay_strings_follow_p = get_overlay_strings (it, 0);
6205 }
6206
6207 if (overlay_strings_follow_p)
6208 success_p = get_next_display_element (it);
6209 else
6210 {
6211 it->what = IT_EOB;
6212 it->position = it->current.pos;
6213 success_p = 0;
6214 }
6215 }
6216 else
6217 {
6218 handle_stop (it);
6219 return get_next_display_element (it);
6220 }
6221 }
6222 else
6223 {
6224 /* No face changes, overlays etc. in sight, so just return a
6225 character from current_buffer. */
6226 unsigned char *p;
6227
6228 /* Maybe run the redisplay end trigger hook. Performance note:
6229 This doesn't seem to cost measurable time. */
6230 if (it->redisplay_end_trigger_charpos
6231 && it->glyph_row
6232 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
6233 run_redisplay_end_trigger_hook (it);
6234
6235 /* Get the next character, maybe multibyte. */
6236 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
6237 if (it->multibyte_p && !ASCII_BYTE_P (*p))
6238 {
6239 int maxlen = ((IT_BYTEPOS (*it) >= GPT_BYTE ? ZV_BYTE : GPT_BYTE)
6240 - IT_BYTEPOS (*it));
6241 it->c = string_char_and_length (p, maxlen, &it->len);
6242 }
6243 else
6244 it->c = *p, it->len = 1;
6245
6246 /* Record what we have and where it came from. */
6247 it->what = IT_CHARACTER;;
6248 it->object = it->w->buffer;
6249 it->position = it->current.pos;
6250
6251 /* Normally we return the character found above, except when we
6252 really want to return an ellipsis for selective display. */
6253 if (it->selective)
6254 {
6255 if (it->c == '\n')
6256 {
6257 /* A value of selective > 0 means hide lines indented more
6258 than that number of columns. */
6259 if (it->selective > 0
6260 && IT_CHARPOS (*it) + 1 < ZV
6261 && indented_beyond_p (IT_CHARPOS (*it) + 1,
6262 IT_BYTEPOS (*it) + 1,
6263 (double) it->selective)) /* iftc */
6264 {
6265 success_p = next_element_from_ellipsis (it);
6266 it->dpvec_char_len = -1;
6267 }
6268 }
6269 else if (it->c == '\r' && it->selective == -1)
6270 {
6271 /* A value of selective == -1 means that everything from the
6272 CR to the end of the line is invisible, with maybe an
6273 ellipsis displayed for it. */
6274 success_p = next_element_from_ellipsis (it);
6275 it->dpvec_char_len = -1;
6276 }
6277 }
6278 }
6279
6280 /* Value is zero if end of buffer reached. */
6281 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
6282 return success_p;
6283 }
6284
6285
6286 /* Run the redisplay end trigger hook for IT. */
6287
6288 static void
6289 run_redisplay_end_trigger_hook (it)
6290 struct it *it;
6291 {
6292 Lisp_Object args[3];
6293
6294 /* IT->glyph_row should be non-null, i.e. we should be actually
6295 displaying something, or otherwise we should not run the hook. */
6296 xassert (it->glyph_row);
6297
6298 /* Set up hook arguments. */
6299 args[0] = Qredisplay_end_trigger_functions;
6300 args[1] = it->window;
6301 XSETINT (args[2], it->redisplay_end_trigger_charpos);
6302 it->redisplay_end_trigger_charpos = 0;
6303
6304 /* Since we are *trying* to run these functions, don't try to run
6305 them again, even if they get an error. */
6306 it->w->redisplay_end_trigger = Qnil;
6307 Frun_hook_with_args (3, args);
6308
6309 /* Notice if it changed the face of the character we are on. */
6310 handle_face_prop (it);
6311 }
6312
6313
6314 /* Deliver a composition display element. The iterator IT is already
6315 filled with composition information (done in
6316 handle_composition_prop). Value is always 1. */
6317
6318 static int
6319 next_element_from_composition (it)
6320 struct it *it;
6321 {
6322 it->what = IT_COMPOSITION;
6323 it->position = (STRINGP (it->string)
6324 ? it->current.string_pos
6325 : it->current.pos);
6326 if (STRINGP (it->string))
6327 it->object = it->string;
6328 else
6329 it->object = it->w->buffer;
6330 return 1;
6331 }
6332
6333
6334 \f
6335 /***********************************************************************
6336 Moving an iterator without producing glyphs
6337 ***********************************************************************/
6338
6339 /* Check if iterator is at a position corresponding to a valid buffer
6340 position after some move_it_ call. */
6341
6342 #define IT_POS_VALID_AFTER_MOVE_P(it) \
6343 ((it)->method == GET_FROM_STRING \
6344 ? IT_STRING_CHARPOS (*it) == 0 \
6345 : 1)
6346
6347
6348 /* Move iterator IT to a specified buffer or X position within one
6349 line on the display without producing glyphs.
6350
6351 OP should be a bit mask including some or all of these bits:
6352 MOVE_TO_X: Stop on reaching x-position TO_X.
6353 MOVE_TO_POS: Stop on reaching buffer or string position TO_CHARPOS.
6354 Regardless of OP's value, stop in reaching the end of the display line.
6355
6356 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
6357 This means, in particular, that TO_X includes window's horizontal
6358 scroll amount.
6359
6360 The return value has several possible values that
6361 say what condition caused the scan to stop:
6362
6363 MOVE_POS_MATCH_OR_ZV
6364 - when TO_POS or ZV was reached.
6365
6366 MOVE_X_REACHED
6367 -when TO_X was reached before TO_POS or ZV were reached.
6368
6369 MOVE_LINE_CONTINUED
6370 - when we reached the end of the display area and the line must
6371 be continued.
6372
6373 MOVE_LINE_TRUNCATED
6374 - when we reached the end of the display area and the line is
6375 truncated.
6376
6377 MOVE_NEWLINE_OR_CR
6378 - when we stopped at a line end, i.e. a newline or a CR and selective
6379 display is on. */
6380
6381 static enum move_it_result
6382 move_it_in_display_line_to (it, to_charpos, to_x, op)
6383 struct it *it;
6384 int to_charpos, to_x, op;
6385 {
6386 enum move_it_result result = MOVE_UNDEFINED;
6387 struct glyph_row *saved_glyph_row;
6388
6389 /* Don't produce glyphs in produce_glyphs. */
6390 saved_glyph_row = it->glyph_row;
6391 it->glyph_row = NULL;
6392
6393 #define BUFFER_POS_REACHED_P() \
6394 ((op & MOVE_TO_POS) != 0 \
6395 && BUFFERP (it->object) \
6396 && IT_CHARPOS (*it) >= to_charpos \
6397 && (it->method == GET_FROM_BUFFER \
6398 || (it->method == GET_FROM_DISPLAY_VECTOR \
6399 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
6400
6401
6402 while (1)
6403 {
6404 int x, i, ascent = 0, descent = 0;
6405
6406 /* Stop if we move beyond TO_CHARPOS (after an image or stretch glyph). */
6407 if ((op & MOVE_TO_POS) != 0
6408 && BUFFERP (it->object)
6409 && it->method == GET_FROM_BUFFER
6410 && IT_CHARPOS (*it) > to_charpos)
6411 {
6412 result = MOVE_POS_MATCH_OR_ZV;
6413 break;
6414 }
6415
6416 /* Stop when ZV reached.
6417 We used to stop here when TO_CHARPOS reached as well, but that is
6418 too soon if this glyph does not fit on this line. So we handle it
6419 explicitly below. */
6420 if (!get_next_display_element (it)
6421 || (it->truncate_lines_p
6422 && BUFFER_POS_REACHED_P ()))
6423 {
6424 result = MOVE_POS_MATCH_OR_ZV;
6425 break;
6426 }
6427
6428 /* The call to produce_glyphs will get the metrics of the
6429 display element IT is loaded with. We record in x the
6430 x-position before this display element in case it does not
6431 fit on the line. */
6432 x = it->current_x;
6433
6434 /* Remember the line height so far in case the next element doesn't
6435 fit on the line. */
6436 if (!it->truncate_lines_p)
6437 {
6438 ascent = it->max_ascent;
6439 descent = it->max_descent;
6440 }
6441
6442 PRODUCE_GLYPHS (it);
6443
6444 if (it->area != TEXT_AREA)
6445 {
6446 set_iterator_to_next (it, 1);
6447 continue;
6448 }
6449
6450 /* The number of glyphs we get back in IT->nglyphs will normally
6451 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
6452 character on a terminal frame, or (iii) a line end. For the
6453 second case, IT->nglyphs - 1 padding glyphs will be present
6454 (on X frames, there is only one glyph produced for a
6455 composite character.
6456
6457 The behavior implemented below means, for continuation lines,
6458 that as many spaces of a TAB as fit on the current line are
6459 displayed there. For terminal frames, as many glyphs of a
6460 multi-glyph character are displayed in the current line, too.
6461 This is what the old redisplay code did, and we keep it that
6462 way. Under X, the whole shape of a complex character must
6463 fit on the line or it will be completely displayed in the
6464 next line.
6465
6466 Note that both for tabs and padding glyphs, all glyphs have
6467 the same width. */
6468 if (it->nglyphs)
6469 {
6470 /* More than one glyph or glyph doesn't fit on line. All
6471 glyphs have the same width. */
6472 int single_glyph_width = it->pixel_width / it->nglyphs;
6473 int new_x;
6474 int x_before_this_char = x;
6475 int hpos_before_this_char = it->hpos;
6476
6477 for (i = 0; i < it->nglyphs; ++i, x = new_x)
6478 {
6479 new_x = x + single_glyph_width;
6480
6481 /* We want to leave anything reaching TO_X to the caller. */
6482 if ((op & MOVE_TO_X) && new_x > to_x)
6483 {
6484 if (BUFFER_POS_REACHED_P ())
6485 goto buffer_pos_reached;
6486 it->current_x = x;
6487 result = MOVE_X_REACHED;
6488 break;
6489 }
6490 else if (/* Lines are continued. */
6491 !it->truncate_lines_p
6492 && (/* And glyph doesn't fit on the line. */
6493 new_x > it->last_visible_x
6494 /* Or it fits exactly and we're on a window
6495 system frame. */
6496 || (new_x == it->last_visible_x
6497 && FRAME_WINDOW_P (it->f))))
6498 {
6499 if (/* IT->hpos == 0 means the very first glyph
6500 doesn't fit on the line, e.g. a wide image. */
6501 it->hpos == 0
6502 || (new_x == it->last_visible_x
6503 && FRAME_WINDOW_P (it->f)))
6504 {
6505 ++it->hpos;
6506 it->current_x = new_x;
6507
6508 /* The character's last glyph just barely fits
6509 in this row. */
6510 if (i == it->nglyphs - 1)
6511 {
6512 /* If this is the destination position,
6513 return a position *before* it in this row,
6514 now that we know it fits in this row. */
6515 if (BUFFER_POS_REACHED_P ())
6516 {
6517 it->hpos = hpos_before_this_char;
6518 it->current_x = x_before_this_char;
6519 result = MOVE_POS_MATCH_OR_ZV;
6520 break;
6521 }
6522
6523 set_iterator_to_next (it, 1);
6524 #ifdef HAVE_WINDOW_SYSTEM
6525 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6526 {
6527 if (!get_next_display_element (it))
6528 {
6529 result = MOVE_POS_MATCH_OR_ZV;
6530 break;
6531 }
6532 if (BUFFER_POS_REACHED_P ())
6533 {
6534 if (ITERATOR_AT_END_OF_LINE_P (it))
6535 result = MOVE_POS_MATCH_OR_ZV;
6536 else
6537 result = MOVE_LINE_CONTINUED;
6538 break;
6539 }
6540 if (ITERATOR_AT_END_OF_LINE_P (it))
6541 {
6542 result = MOVE_NEWLINE_OR_CR;
6543 break;
6544 }
6545 }
6546 #endif /* HAVE_WINDOW_SYSTEM */
6547 }
6548 }
6549 else
6550 {
6551 it->current_x = x;
6552 it->max_ascent = ascent;
6553 it->max_descent = descent;
6554 }
6555
6556 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
6557 IT_CHARPOS (*it)));
6558 result = MOVE_LINE_CONTINUED;
6559 break;
6560 }
6561 else if (BUFFER_POS_REACHED_P ())
6562 goto buffer_pos_reached;
6563 else if (new_x > it->first_visible_x)
6564 {
6565 /* Glyph is visible. Increment number of glyphs that
6566 would be displayed. */
6567 ++it->hpos;
6568 }
6569 else
6570 {
6571 /* Glyph is completely off the left margin of the display
6572 area. Nothing to do. */
6573 }
6574 }
6575
6576 if (result != MOVE_UNDEFINED)
6577 break;
6578 }
6579 else if (BUFFER_POS_REACHED_P ())
6580 {
6581 buffer_pos_reached:
6582 it->current_x = x;
6583 it->max_ascent = ascent;
6584 it->max_descent = descent;
6585 result = MOVE_POS_MATCH_OR_ZV;
6586 break;
6587 }
6588 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
6589 {
6590 /* Stop when TO_X specified and reached. This check is
6591 necessary here because of lines consisting of a line end,
6592 only. The line end will not produce any glyphs and we
6593 would never get MOVE_X_REACHED. */
6594 xassert (it->nglyphs == 0);
6595 result = MOVE_X_REACHED;
6596 break;
6597 }
6598
6599 /* Is this a line end? If yes, we're done. */
6600 if (ITERATOR_AT_END_OF_LINE_P (it))
6601 {
6602 result = MOVE_NEWLINE_OR_CR;
6603 break;
6604 }
6605
6606 /* The current display element has been consumed. Advance
6607 to the next. */
6608 set_iterator_to_next (it, 1);
6609
6610 /* Stop if lines are truncated and IT's current x-position is
6611 past the right edge of the window now. */
6612 if (it->truncate_lines_p
6613 && it->current_x >= it->last_visible_x)
6614 {
6615 #ifdef HAVE_WINDOW_SYSTEM
6616 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
6617 {
6618 if (!get_next_display_element (it)
6619 || BUFFER_POS_REACHED_P ())
6620 {
6621 result = MOVE_POS_MATCH_OR_ZV;
6622 break;
6623 }
6624 if (ITERATOR_AT_END_OF_LINE_P (it))
6625 {
6626 result = MOVE_NEWLINE_OR_CR;
6627 break;
6628 }
6629 }
6630 #endif /* HAVE_WINDOW_SYSTEM */
6631 result = MOVE_LINE_TRUNCATED;
6632 break;
6633 }
6634 }
6635
6636 #undef BUFFER_POS_REACHED_P
6637
6638 /* Restore the iterator settings altered at the beginning of this
6639 function. */
6640 it->glyph_row = saved_glyph_row;
6641 return result;
6642 }
6643
6644
6645 /* Move IT forward until it satisfies one or more of the criteria in
6646 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
6647
6648 OP is a bit-mask that specifies where to stop, and in particular,
6649 which of those four position arguments makes a difference. See the
6650 description of enum move_operation_enum.
6651
6652 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
6653 screen line, this function will set IT to the next position >
6654 TO_CHARPOS. */
6655
6656 void
6657 move_it_to (it, to_charpos, to_x, to_y, to_vpos, op)
6658 struct it *it;
6659 int to_charpos, to_x, to_y, to_vpos;
6660 int op;
6661 {
6662 enum move_it_result skip, skip2 = MOVE_X_REACHED;
6663 int line_height;
6664 int reached = 0;
6665
6666 for (;;)
6667 {
6668 if (op & MOVE_TO_VPOS)
6669 {
6670 /* If no TO_CHARPOS and no TO_X specified, stop at the
6671 start of the line TO_VPOS. */
6672 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
6673 {
6674 if (it->vpos == to_vpos)
6675 {
6676 reached = 1;
6677 break;
6678 }
6679 else
6680 skip = move_it_in_display_line_to (it, -1, -1, 0);
6681 }
6682 else
6683 {
6684 /* TO_VPOS >= 0 means stop at TO_X in the line at
6685 TO_VPOS, or at TO_POS, whichever comes first. */
6686 if (it->vpos == to_vpos)
6687 {
6688 reached = 2;
6689 break;
6690 }
6691
6692 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
6693
6694 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
6695 {
6696 reached = 3;
6697 break;
6698 }
6699 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
6700 {
6701 /* We have reached TO_X but not in the line we want. */
6702 skip = move_it_in_display_line_to (it, to_charpos,
6703 -1, MOVE_TO_POS);
6704 if (skip == MOVE_POS_MATCH_OR_ZV)
6705 {
6706 reached = 4;
6707 break;
6708 }
6709 }
6710 }
6711 }
6712 else if (op & MOVE_TO_Y)
6713 {
6714 struct it it_backup;
6715
6716 /* TO_Y specified means stop at TO_X in the line containing
6717 TO_Y---or at TO_CHARPOS if this is reached first. The
6718 problem is that we can't really tell whether the line
6719 contains TO_Y before we have completely scanned it, and
6720 this may skip past TO_X. What we do is to first scan to
6721 TO_X.
6722
6723 If TO_X is not specified, use a TO_X of zero. The reason
6724 is to make the outcome of this function more predictable.
6725 If we didn't use TO_X == 0, we would stop at the end of
6726 the line which is probably not what a caller would expect
6727 to happen. */
6728 skip = move_it_in_display_line_to (it, to_charpos,
6729 ((op & MOVE_TO_X)
6730 ? to_x : 0),
6731 (MOVE_TO_X
6732 | (op & MOVE_TO_POS)));
6733
6734 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
6735 if (skip == MOVE_POS_MATCH_OR_ZV)
6736 {
6737 reached = 5;
6738 break;
6739 }
6740
6741 /* If TO_X was reached, we would like to know whether TO_Y
6742 is in the line. This can only be said if we know the
6743 total line height which requires us to scan the rest of
6744 the line. */
6745 if (skip == MOVE_X_REACHED)
6746 {
6747 it_backup = *it;
6748 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
6749 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
6750 op & MOVE_TO_POS);
6751 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
6752 }
6753
6754 /* Now, decide whether TO_Y is in this line. */
6755 line_height = it->max_ascent + it->max_descent;
6756 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
6757
6758 if (to_y >= it->current_y
6759 && to_y < it->current_y + line_height)
6760 {
6761 if (skip == MOVE_X_REACHED)
6762 /* If TO_Y is in this line and TO_X was reached above,
6763 we scanned too far. We have to restore IT's settings
6764 to the ones before skipping. */
6765 *it = it_backup;
6766 reached = 6;
6767 }
6768 else if (skip == MOVE_X_REACHED)
6769 {
6770 skip = skip2;
6771 if (skip == MOVE_POS_MATCH_OR_ZV)
6772 reached = 7;
6773 }
6774
6775 if (reached)
6776 break;
6777 }
6778 else if (BUFFERP (it->object)
6779 && it->method == GET_FROM_BUFFER
6780 && IT_CHARPOS (*it) >= to_charpos)
6781 skip = MOVE_POS_MATCH_OR_ZV;
6782 else
6783 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
6784
6785 switch (skip)
6786 {
6787 case MOVE_POS_MATCH_OR_ZV:
6788 reached = 8;
6789 goto out;
6790
6791 case MOVE_NEWLINE_OR_CR:
6792 set_iterator_to_next (it, 1);
6793 it->continuation_lines_width = 0;
6794 break;
6795
6796 case MOVE_LINE_TRUNCATED:
6797 it->continuation_lines_width = 0;
6798 reseat_at_next_visible_line_start (it, 0);
6799 if ((op & MOVE_TO_POS) != 0
6800 && IT_CHARPOS (*it) > to_charpos)
6801 {
6802 reached = 9;
6803 goto out;
6804 }
6805 break;
6806
6807 case MOVE_LINE_CONTINUED:
6808 it->continuation_lines_width += it->current_x;
6809 break;
6810
6811 default:
6812 abort ();
6813 }
6814
6815 /* Reset/increment for the next run. */
6816 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
6817 it->current_x = it->hpos = 0;
6818 it->current_y += it->max_ascent + it->max_descent;
6819 ++it->vpos;
6820 last_height = it->max_ascent + it->max_descent;
6821 last_max_ascent = it->max_ascent;
6822 it->max_ascent = it->max_descent = 0;
6823 }
6824
6825 out:
6826
6827 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
6828 }
6829
6830
6831 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
6832
6833 If DY > 0, move IT backward at least that many pixels. DY = 0
6834 means move IT backward to the preceding line start or BEGV. This
6835 function may move over more than DY pixels if IT->current_y - DY
6836 ends up in the middle of a line; in this case IT->current_y will be
6837 set to the top of the line moved to. */
6838
6839 void
6840 move_it_vertically_backward (it, dy)
6841 struct it *it;
6842 int dy;
6843 {
6844 int nlines, h;
6845 struct it it2, it3;
6846 int start_pos;
6847
6848 move_further_back:
6849 xassert (dy >= 0);
6850
6851 start_pos = IT_CHARPOS (*it);
6852
6853 /* Estimate how many newlines we must move back. */
6854 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
6855
6856 /* Set the iterator's position that many lines back. */
6857 while (nlines-- && IT_CHARPOS (*it) > BEGV)
6858 back_to_previous_visible_line_start (it);
6859
6860 /* Reseat the iterator here. When moving backward, we don't want
6861 reseat to skip forward over invisible text, set up the iterator
6862 to deliver from overlay strings at the new position etc. So,
6863 use reseat_1 here. */
6864 reseat_1 (it, it->current.pos, 1);
6865
6866 /* We are now surely at a line start. */
6867 it->current_x = it->hpos = 0;
6868 it->continuation_lines_width = 0;
6869
6870 /* Move forward and see what y-distance we moved. First move to the
6871 start of the next line so that we get its height. We need this
6872 height to be able to tell whether we reached the specified
6873 y-distance. */
6874 it2 = *it;
6875 it2.max_ascent = it2.max_descent = 0;
6876 do
6877 {
6878 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
6879 MOVE_TO_POS | MOVE_TO_VPOS);
6880 }
6881 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
6882 xassert (IT_CHARPOS (*it) >= BEGV);
6883 it3 = it2;
6884
6885 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
6886 xassert (IT_CHARPOS (*it) >= BEGV);
6887 /* H is the actual vertical distance from the position in *IT
6888 and the starting position. */
6889 h = it2.current_y - it->current_y;
6890 /* NLINES is the distance in number of lines. */
6891 nlines = it2.vpos - it->vpos;
6892
6893 /* Correct IT's y and vpos position
6894 so that they are relative to the starting point. */
6895 it->vpos -= nlines;
6896 it->current_y -= h;
6897
6898 if (dy == 0)
6899 {
6900 /* DY == 0 means move to the start of the screen line. The
6901 value of nlines is > 0 if continuation lines were involved. */
6902 if (nlines > 0)
6903 move_it_by_lines (it, nlines, 1);
6904 #if 0
6905 /* I think this assert is bogus if buffer contains
6906 invisible text or images. KFS. */
6907 xassert (IT_CHARPOS (*it) <= start_pos);
6908 #endif
6909 }
6910 else
6911 {
6912 /* The y-position we try to reach, relative to *IT.
6913 Note that H has been subtracted in front of the if-statement. */
6914 int target_y = it->current_y + h - dy;
6915 int y0 = it3.current_y;
6916 int y1 = line_bottom_y (&it3);
6917 int line_height = y1 - y0;
6918
6919 /* If we did not reach target_y, try to move further backward if
6920 we can. If we moved too far backward, try to move forward. */
6921 if (target_y < it->current_y
6922 /* This is heuristic. In a window that's 3 lines high, with
6923 a line height of 13 pixels each, recentering with point
6924 on the bottom line will try to move -39/2 = 19 pixels
6925 backward. Try to avoid moving into the first line. */
6926 && (it->current_y - target_y
6927 > min (window_box_height (it->w), line_height * 2 / 3))
6928 && IT_CHARPOS (*it) > BEGV)
6929 {
6930 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
6931 target_y - it->current_y));
6932 dy = it->current_y - target_y;
6933 goto move_further_back;
6934 }
6935 else if (target_y >= it->current_y + line_height
6936 && IT_CHARPOS (*it) < ZV)
6937 {
6938 /* Should move forward by at least one line, maybe more.
6939
6940 Note: Calling move_it_by_lines can be expensive on
6941 terminal frames, where compute_motion is used (via
6942 vmotion) to do the job, when there are very long lines
6943 and truncate-lines is nil. That's the reason for
6944 treating terminal frames specially here. */
6945
6946 if (!FRAME_WINDOW_P (it->f))
6947 move_it_vertically (it, target_y - (it->current_y + line_height));
6948 else
6949 {
6950 do
6951 {
6952 move_it_by_lines (it, 1, 1);
6953 }
6954 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
6955 }
6956
6957 #if 0
6958 /* I think this assert is bogus if buffer contains
6959 invisible text or images. KFS. */
6960 xassert (IT_CHARPOS (*it) >= BEGV);
6961 #endif
6962 }
6963 }
6964 }
6965
6966
6967 /* Move IT by a specified amount of pixel lines DY. DY negative means
6968 move backwards. DY = 0 means move to start of screen line. At the
6969 end, IT will be on the start of a screen line. */
6970
6971 void
6972 move_it_vertically (it, dy)
6973 struct it *it;
6974 int dy;
6975 {
6976 if (dy <= 0)
6977 move_it_vertically_backward (it, -dy);
6978 else
6979 {
6980 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
6981 move_it_to (it, ZV, -1, it->current_y + dy, -1,
6982 MOVE_TO_POS | MOVE_TO_Y);
6983 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
6984
6985 /* If buffer ends in ZV without a newline, move to the start of
6986 the line to satisfy the post-condition. */
6987 if (IT_CHARPOS (*it) == ZV
6988 && ZV > BEGV
6989 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
6990 move_it_by_lines (it, 0, 0);
6991 }
6992 }
6993
6994
6995 /* Move iterator IT past the end of the text line it is in. */
6996
6997 void
6998 move_it_past_eol (it)
6999 struct it *it;
7000 {
7001 enum move_it_result rc;
7002
7003 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
7004 if (rc == MOVE_NEWLINE_OR_CR)
7005 set_iterator_to_next (it, 0);
7006 }
7007
7008
7009 #if 0 /* Currently not used. */
7010
7011 /* Return non-zero if some text between buffer positions START_CHARPOS
7012 and END_CHARPOS is invisible. IT->window is the window for text
7013 property lookup. */
7014
7015 static int
7016 invisible_text_between_p (it, start_charpos, end_charpos)
7017 struct it *it;
7018 int start_charpos, end_charpos;
7019 {
7020 Lisp_Object prop, limit;
7021 int invisible_found_p;
7022
7023 xassert (it != NULL && start_charpos <= end_charpos);
7024
7025 /* Is text at START invisible? */
7026 prop = Fget_char_property (make_number (start_charpos), Qinvisible,
7027 it->window);
7028 if (TEXT_PROP_MEANS_INVISIBLE (prop))
7029 invisible_found_p = 1;
7030 else
7031 {
7032 limit = Fnext_single_char_property_change (make_number (start_charpos),
7033 Qinvisible, Qnil,
7034 make_number (end_charpos));
7035 invisible_found_p = XFASTINT (limit) < end_charpos;
7036 }
7037
7038 return invisible_found_p;
7039 }
7040
7041 #endif /* 0 */
7042
7043
7044 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
7045 negative means move up. DVPOS == 0 means move to the start of the
7046 screen line. NEED_Y_P non-zero means calculate IT->current_y. If
7047 NEED_Y_P is zero, IT->current_y will be left unchanged.
7048
7049 Further optimization ideas: If we would know that IT->f doesn't use
7050 a face with proportional font, we could be faster for
7051 truncate-lines nil. */
7052
7053 void
7054 move_it_by_lines (it, dvpos, need_y_p)
7055 struct it *it;
7056 int dvpos, need_y_p;
7057 {
7058 struct position pos;
7059
7060 if (!FRAME_WINDOW_P (it->f))
7061 {
7062 struct text_pos textpos;
7063
7064 /* We can use vmotion on frames without proportional fonts. */
7065 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
7066 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
7067 reseat (it, textpos, 1);
7068 it->vpos += pos.vpos;
7069 it->current_y += pos.vpos;
7070 }
7071 else if (dvpos == 0)
7072 {
7073 /* DVPOS == 0 means move to the start of the screen line. */
7074 move_it_vertically_backward (it, 0);
7075 xassert (it->current_x == 0 && it->hpos == 0);
7076 /* Let next call to line_bottom_y calculate real line height */
7077 last_height = 0;
7078 }
7079 else if (dvpos > 0)
7080 {
7081 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
7082 if (!IT_POS_VALID_AFTER_MOVE_P (it))
7083 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
7084 }
7085 else
7086 {
7087 struct it it2;
7088 int start_charpos, i;
7089
7090 /* Start at the beginning of the screen line containing IT's
7091 position. This may actually move vertically backwards,
7092 in case of overlays, so adjust dvpos accordingly. */
7093 dvpos += it->vpos;
7094 move_it_vertically_backward (it, 0);
7095 dvpos -= it->vpos;
7096
7097 /* Go back -DVPOS visible lines and reseat the iterator there. */
7098 start_charpos = IT_CHARPOS (*it);
7099 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
7100 back_to_previous_visible_line_start (it);
7101 reseat (it, it->current.pos, 1);
7102
7103 /* Move further back if we end up in a string or an image. */
7104 while (!IT_POS_VALID_AFTER_MOVE_P (it))
7105 {
7106 /* First try to move to start of display line. */
7107 dvpos += it->vpos;
7108 move_it_vertically_backward (it, 0);
7109 dvpos -= it->vpos;
7110 if (IT_POS_VALID_AFTER_MOVE_P (it))
7111 break;
7112 /* If start of line is still in string or image,
7113 move further back. */
7114 back_to_previous_visible_line_start (it);
7115 reseat (it, it->current.pos, 1);
7116 dvpos--;
7117 }
7118
7119 it->current_x = it->hpos = 0;
7120
7121 /* Above call may have moved too far if continuation lines
7122 are involved. Scan forward and see if it did. */
7123 it2 = *it;
7124 it2.vpos = it2.current_y = 0;
7125 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
7126 it->vpos -= it2.vpos;
7127 it->current_y -= it2.current_y;
7128 it->current_x = it->hpos = 0;
7129
7130 /* If we moved too far back, move IT some lines forward. */
7131 if (it2.vpos > -dvpos)
7132 {
7133 int delta = it2.vpos + dvpos;
7134 it2 = *it;
7135 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
7136 /* Move back again if we got too far ahead. */
7137 if (IT_CHARPOS (*it) >= start_charpos)
7138 *it = it2;
7139 }
7140 }
7141 }
7142
7143 /* Return 1 if IT points into the middle of a display vector. */
7144
7145 int
7146 in_display_vector_p (it)
7147 struct it *it;
7148 {
7149 return (it->method == GET_FROM_DISPLAY_VECTOR
7150 && it->current.dpvec_index > 0
7151 && it->dpvec + it->current.dpvec_index != it->dpend);
7152 }
7153
7154 \f
7155 /***********************************************************************
7156 Messages
7157 ***********************************************************************/
7158
7159
7160 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
7161 to *Messages*. */
7162
7163 void
7164 add_to_log (format, arg1, arg2)
7165 char *format;
7166 Lisp_Object arg1, arg2;
7167 {
7168 Lisp_Object args[3];
7169 Lisp_Object msg, fmt;
7170 char *buffer;
7171 int len;
7172 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
7173 USE_SAFE_ALLOCA;
7174
7175 /* Do nothing if called asynchronously. Inserting text into
7176 a buffer may call after-change-functions and alike and
7177 that would means running Lisp asynchronously. */
7178 if (handling_signal)
7179 return;
7180
7181 fmt = msg = Qnil;
7182 GCPRO4 (fmt, msg, arg1, arg2);
7183
7184 args[0] = fmt = build_string (format);
7185 args[1] = arg1;
7186 args[2] = arg2;
7187 msg = Fformat (3, args);
7188
7189 len = SBYTES (msg) + 1;
7190 SAFE_ALLOCA (buffer, char *, len);
7191 bcopy (SDATA (msg), buffer, len);
7192
7193 message_dolog (buffer, len - 1, 1, 0);
7194 SAFE_FREE ();
7195
7196 UNGCPRO;
7197 }
7198
7199
7200 /* Output a newline in the *Messages* buffer if "needs" one. */
7201
7202 void
7203 message_log_maybe_newline ()
7204 {
7205 if (message_log_need_newline)
7206 message_dolog ("", 0, 1, 0);
7207 }
7208
7209
7210 /* Add a string M of length NBYTES to the message log, optionally
7211 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
7212 nonzero, means interpret the contents of M as multibyte. This
7213 function calls low-level routines in order to bypass text property
7214 hooks, etc. which might not be safe to run.
7215
7216 This may GC (insert may run before/after change hooks),
7217 so the buffer M must NOT point to a Lisp string. */
7218
7219 void
7220 message_dolog (m, nbytes, nlflag, multibyte)
7221 const char *m;
7222 int nbytes, nlflag, multibyte;
7223 {
7224 if (!NILP (Vmemory_full))
7225 return;
7226
7227 if (!NILP (Vmessage_log_max))
7228 {
7229 struct buffer *oldbuf;
7230 Lisp_Object oldpoint, oldbegv, oldzv;
7231 int old_windows_or_buffers_changed = windows_or_buffers_changed;
7232 int point_at_end = 0;
7233 int zv_at_end = 0;
7234 Lisp_Object old_deactivate_mark, tem;
7235 struct gcpro gcpro1;
7236
7237 old_deactivate_mark = Vdeactivate_mark;
7238 oldbuf = current_buffer;
7239 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
7240 current_buffer->undo_list = Qt;
7241
7242 oldpoint = message_dolog_marker1;
7243 set_marker_restricted (oldpoint, make_number (PT), Qnil);
7244 oldbegv = message_dolog_marker2;
7245 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
7246 oldzv = message_dolog_marker3;
7247 set_marker_restricted (oldzv, make_number (ZV), Qnil);
7248 GCPRO1 (old_deactivate_mark);
7249
7250 if (PT == Z)
7251 point_at_end = 1;
7252 if (ZV == Z)
7253 zv_at_end = 1;
7254
7255 BEGV = BEG;
7256 BEGV_BYTE = BEG_BYTE;
7257 ZV = Z;
7258 ZV_BYTE = Z_BYTE;
7259 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7260
7261 /* Insert the string--maybe converting multibyte to single byte
7262 or vice versa, so that all the text fits the buffer. */
7263 if (multibyte
7264 && NILP (current_buffer->enable_multibyte_characters))
7265 {
7266 int i, c, char_bytes;
7267 unsigned char work[1];
7268
7269 /* Convert a multibyte string to single-byte
7270 for the *Message* buffer. */
7271 for (i = 0; i < nbytes; i += char_bytes)
7272 {
7273 c = string_char_and_length (m + i, nbytes - i, &char_bytes);
7274 work[0] = (SINGLE_BYTE_CHAR_P (c)
7275 ? c
7276 : multibyte_char_to_unibyte (c, Qnil));
7277 insert_1_both (work, 1, 1, 1, 0, 0);
7278 }
7279 }
7280 else if (! multibyte
7281 && ! NILP (current_buffer->enable_multibyte_characters))
7282 {
7283 int i, c, char_bytes;
7284 unsigned char *msg = (unsigned char *) m;
7285 unsigned char str[MAX_MULTIBYTE_LENGTH];
7286 /* Convert a single-byte string to multibyte
7287 for the *Message* buffer. */
7288 for (i = 0; i < nbytes; i++)
7289 {
7290 c = unibyte_char_to_multibyte (msg[i]);
7291 char_bytes = CHAR_STRING (c, str);
7292 insert_1_both (str, 1, char_bytes, 1, 0, 0);
7293 }
7294 }
7295 else if (nbytes)
7296 insert_1 (m, nbytes, 1, 0, 0);
7297
7298 if (nlflag)
7299 {
7300 int this_bol, this_bol_byte, prev_bol, prev_bol_byte, dup;
7301 insert_1 ("\n", 1, 1, 0, 0);
7302
7303 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
7304 this_bol = PT;
7305 this_bol_byte = PT_BYTE;
7306
7307 /* See if this line duplicates the previous one.
7308 If so, combine duplicates. */
7309 if (this_bol > BEG)
7310 {
7311 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
7312 prev_bol = PT;
7313 prev_bol_byte = PT_BYTE;
7314
7315 dup = message_log_check_duplicate (prev_bol, prev_bol_byte,
7316 this_bol, this_bol_byte);
7317 if (dup)
7318 {
7319 del_range_both (prev_bol, prev_bol_byte,
7320 this_bol, this_bol_byte, 0);
7321 if (dup > 1)
7322 {
7323 char dupstr[40];
7324 int duplen;
7325
7326 /* If you change this format, don't forget to also
7327 change message_log_check_duplicate. */
7328 sprintf (dupstr, " [%d times]", dup);
7329 duplen = strlen (dupstr);
7330 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
7331 insert_1 (dupstr, duplen, 1, 0, 1);
7332 }
7333 }
7334 }
7335
7336 /* If we have more than the desired maximum number of lines
7337 in the *Messages* buffer now, delete the oldest ones.
7338 This is safe because we don't have undo in this buffer. */
7339
7340 if (NATNUMP (Vmessage_log_max))
7341 {
7342 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
7343 -XFASTINT (Vmessage_log_max) - 1, 0);
7344 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
7345 }
7346 }
7347 BEGV = XMARKER (oldbegv)->charpos;
7348 BEGV_BYTE = marker_byte_position (oldbegv);
7349
7350 if (zv_at_end)
7351 {
7352 ZV = Z;
7353 ZV_BYTE = Z_BYTE;
7354 }
7355 else
7356 {
7357 ZV = XMARKER (oldzv)->charpos;
7358 ZV_BYTE = marker_byte_position (oldzv);
7359 }
7360
7361 if (point_at_end)
7362 TEMP_SET_PT_BOTH (Z, Z_BYTE);
7363 else
7364 /* We can't do Fgoto_char (oldpoint) because it will run some
7365 Lisp code. */
7366 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
7367 XMARKER (oldpoint)->bytepos);
7368
7369 UNGCPRO;
7370 unchain_marker (XMARKER (oldpoint));
7371 unchain_marker (XMARKER (oldbegv));
7372 unchain_marker (XMARKER (oldzv));
7373
7374 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
7375 set_buffer_internal (oldbuf);
7376 if (NILP (tem))
7377 windows_or_buffers_changed = old_windows_or_buffers_changed;
7378 message_log_need_newline = !nlflag;
7379 Vdeactivate_mark = old_deactivate_mark;
7380 }
7381 }
7382
7383
7384 /* We are at the end of the buffer after just having inserted a newline.
7385 (Note: We depend on the fact we won't be crossing the gap.)
7386 Check to see if the most recent message looks a lot like the previous one.
7387 Return 0 if different, 1 if the new one should just replace it, or a
7388 value N > 1 if we should also append " [N times]". */
7389
7390 static int
7391 message_log_check_duplicate (prev_bol, prev_bol_byte, this_bol, this_bol_byte)
7392 int prev_bol, this_bol;
7393 int prev_bol_byte, this_bol_byte;
7394 {
7395 int i;
7396 int len = Z_BYTE - 1 - this_bol_byte;
7397 int seen_dots = 0;
7398 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
7399 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
7400
7401 for (i = 0; i < len; i++)
7402 {
7403 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
7404 seen_dots = 1;
7405 if (p1[i] != p2[i])
7406 return seen_dots;
7407 }
7408 p1 += len;
7409 if (*p1 == '\n')
7410 return 2;
7411 if (*p1++ == ' ' && *p1++ == '[')
7412 {
7413 int n = 0;
7414 while (*p1 >= '0' && *p1 <= '9')
7415 n = n * 10 + *p1++ - '0';
7416 if (strncmp (p1, " times]\n", 8) == 0)
7417 return n+1;
7418 }
7419 return 0;
7420 }
7421 \f
7422
7423 /* Display an echo area message M with a specified length of NBYTES
7424 bytes. The string may include null characters. If M is 0, clear
7425 out any existing message, and let the mini-buffer text show
7426 through.
7427
7428 This may GC, so the buffer M must NOT point to a Lisp string. */
7429
7430 void
7431 message2 (m, nbytes, multibyte)
7432 const char *m;
7433 int nbytes;
7434 int multibyte;
7435 {
7436 /* First flush out any partial line written with print. */
7437 message_log_maybe_newline ();
7438 if (m)
7439 message_dolog (m, nbytes, 1, multibyte);
7440 message2_nolog (m, nbytes, multibyte);
7441 }
7442
7443
7444 /* The non-logging counterpart of message2. */
7445
7446 void
7447 message2_nolog (m, nbytes, multibyte)
7448 const char *m;
7449 int nbytes, multibyte;
7450 {
7451 struct frame *sf = SELECTED_FRAME ();
7452 message_enable_multibyte = multibyte;
7453
7454 if (noninteractive)
7455 {
7456 if (noninteractive_need_newline)
7457 putc ('\n', stderr);
7458 noninteractive_need_newline = 0;
7459 if (m)
7460 fwrite (m, nbytes, 1, stderr);
7461 if (cursor_in_echo_area == 0)
7462 fprintf (stderr, "\n");
7463 fflush (stderr);
7464 }
7465 /* A null message buffer means that the frame hasn't really been
7466 initialized yet. Error messages get reported properly by
7467 cmd_error, so this must be just an informative message; toss it. */
7468 else if (INTERACTIVE
7469 && sf->glyphs_initialized_p
7470 && FRAME_MESSAGE_BUF (sf))
7471 {
7472 Lisp_Object mini_window;
7473 struct frame *f;
7474
7475 /* Get the frame containing the mini-buffer
7476 that the selected frame is using. */
7477 mini_window = FRAME_MINIBUF_WINDOW (sf);
7478 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7479
7480 FRAME_SAMPLE_VISIBILITY (f);
7481 if (FRAME_VISIBLE_P (sf)
7482 && ! FRAME_VISIBLE_P (f))
7483 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
7484
7485 if (m)
7486 {
7487 set_message (m, Qnil, nbytes, multibyte);
7488 if (minibuffer_auto_raise)
7489 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
7490 }
7491 else
7492 clear_message (1, 1);
7493
7494 do_pending_window_change (0);
7495 echo_area_display (1);
7496 do_pending_window_change (0);
7497 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7498 (*frame_up_to_date_hook) (f);
7499 }
7500 }
7501
7502
7503 /* Display an echo area message M with a specified length of NBYTES
7504 bytes. The string may include null characters. If M is not a
7505 string, clear out any existing message, and let the mini-buffer
7506 text show through.
7507
7508 This function cancels echoing. */
7509
7510 void
7511 message3 (m, nbytes, multibyte)
7512 Lisp_Object m;
7513 int nbytes;
7514 int multibyte;
7515 {
7516 struct gcpro gcpro1;
7517
7518 GCPRO1 (m);
7519 clear_message (1,1);
7520 cancel_echoing ();
7521
7522 /* First flush out any partial line written with print. */
7523 message_log_maybe_newline ();
7524 if (STRINGP (m))
7525 {
7526 char *buffer;
7527 USE_SAFE_ALLOCA;
7528
7529 SAFE_ALLOCA (buffer, char *, nbytes);
7530 bcopy (SDATA (m), buffer, nbytes);
7531 message_dolog (buffer, nbytes, 1, multibyte);
7532 SAFE_FREE ();
7533 }
7534 message3_nolog (m, nbytes, multibyte);
7535
7536 UNGCPRO;
7537 }
7538
7539
7540 /* The non-logging version of message3.
7541 This does not cancel echoing, because it is used for echoing.
7542 Perhaps we need to make a separate function for echoing
7543 and make this cancel echoing. */
7544
7545 void
7546 message3_nolog (m, nbytes, multibyte)
7547 Lisp_Object m;
7548 int nbytes, multibyte;
7549 {
7550 struct frame *sf = SELECTED_FRAME ();
7551 message_enable_multibyte = multibyte;
7552
7553 if (noninteractive)
7554 {
7555 if (noninteractive_need_newline)
7556 putc ('\n', stderr);
7557 noninteractive_need_newline = 0;
7558 if (STRINGP (m))
7559 fwrite (SDATA (m), nbytes, 1, stderr);
7560 if (cursor_in_echo_area == 0)
7561 fprintf (stderr, "\n");
7562 fflush (stderr);
7563 }
7564 /* A null message buffer means that the frame hasn't really been
7565 initialized yet. Error messages get reported properly by
7566 cmd_error, so this must be just an informative message; toss it. */
7567 else if (INTERACTIVE
7568 && sf->glyphs_initialized_p
7569 && FRAME_MESSAGE_BUF (sf))
7570 {
7571 Lisp_Object mini_window;
7572 Lisp_Object frame;
7573 struct frame *f;
7574
7575 /* Get the frame containing the mini-buffer
7576 that the selected frame is using. */
7577 mini_window = FRAME_MINIBUF_WINDOW (sf);
7578 frame = XWINDOW (mini_window)->frame;
7579 f = XFRAME (frame);
7580
7581 FRAME_SAMPLE_VISIBILITY (f);
7582 if (FRAME_VISIBLE_P (sf)
7583 && !FRAME_VISIBLE_P (f))
7584 Fmake_frame_visible (frame);
7585
7586 if (STRINGP (m) && SCHARS (m) > 0)
7587 {
7588 set_message (NULL, m, nbytes, multibyte);
7589 if (minibuffer_auto_raise)
7590 Fraise_frame (frame);
7591 /* Assume we are not echoing.
7592 (If we are, echo_now will override this.) */
7593 echo_message_buffer = Qnil;
7594 }
7595 else
7596 clear_message (1, 1);
7597
7598 do_pending_window_change (0);
7599 echo_area_display (1);
7600 do_pending_window_change (0);
7601 if (frame_up_to_date_hook != 0 && ! gc_in_progress)
7602 (*frame_up_to_date_hook) (f);
7603 }
7604 }
7605
7606
7607 /* Display a null-terminated echo area message M. If M is 0, clear
7608 out any existing message, and let the mini-buffer text show through.
7609
7610 The buffer M must continue to exist until after the echo area gets
7611 cleared or some other message gets displayed there. Do not pass
7612 text that is stored in a Lisp string. Do not pass text in a buffer
7613 that was alloca'd. */
7614
7615 void
7616 message1 (m)
7617 char *m;
7618 {
7619 message2 (m, (m ? strlen (m) : 0), 0);
7620 }
7621
7622
7623 /* The non-logging counterpart of message1. */
7624
7625 void
7626 message1_nolog (m)
7627 char *m;
7628 {
7629 message2_nolog (m, (m ? strlen (m) : 0), 0);
7630 }
7631
7632 /* Display a message M which contains a single %s
7633 which gets replaced with STRING. */
7634
7635 void
7636 message_with_string (m, string, log)
7637 char *m;
7638 Lisp_Object string;
7639 int log;
7640 {
7641 CHECK_STRING (string);
7642
7643 if (noninteractive)
7644 {
7645 if (m)
7646 {
7647 if (noninteractive_need_newline)
7648 putc ('\n', stderr);
7649 noninteractive_need_newline = 0;
7650 fprintf (stderr, m, SDATA (string));
7651 if (cursor_in_echo_area == 0)
7652 fprintf (stderr, "\n");
7653 fflush (stderr);
7654 }
7655 }
7656 else if (INTERACTIVE)
7657 {
7658 /* The frame whose minibuffer we're going to display the message on.
7659 It may be larger than the selected frame, so we need
7660 to use its buffer, not the selected frame's buffer. */
7661 Lisp_Object mini_window;
7662 struct frame *f, *sf = SELECTED_FRAME ();
7663
7664 /* Get the frame containing the minibuffer
7665 that the selected frame is using. */
7666 mini_window = FRAME_MINIBUF_WINDOW (sf);
7667 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7668
7669 /* A null message buffer means that the frame hasn't really been
7670 initialized yet. Error messages get reported properly by
7671 cmd_error, so this must be just an informative message; toss it. */
7672 if (FRAME_MESSAGE_BUF (f))
7673 {
7674 Lisp_Object args[2], message;
7675 struct gcpro gcpro1, gcpro2;
7676
7677 args[0] = build_string (m);
7678 args[1] = message = string;
7679 GCPRO2 (args[0], message);
7680 gcpro1.nvars = 2;
7681
7682 message = Fformat (2, args);
7683
7684 if (log)
7685 message3 (message, SBYTES (message), STRING_MULTIBYTE (message));
7686 else
7687 message3_nolog (message, SBYTES (message), STRING_MULTIBYTE (message));
7688
7689 UNGCPRO;
7690
7691 /* Print should start at the beginning of the message
7692 buffer next time. */
7693 message_buf_print = 0;
7694 }
7695 }
7696 }
7697
7698
7699 /* Dump an informative message to the minibuf. If M is 0, clear out
7700 any existing message, and let the mini-buffer text show through. */
7701
7702 /* VARARGS 1 */
7703 void
7704 message (m, a1, a2, a3)
7705 char *m;
7706 EMACS_INT a1, a2, a3;
7707 {
7708 if (noninteractive)
7709 {
7710 if (m)
7711 {
7712 if (noninteractive_need_newline)
7713 putc ('\n', stderr);
7714 noninteractive_need_newline = 0;
7715 fprintf (stderr, m, a1, a2, a3);
7716 if (cursor_in_echo_area == 0)
7717 fprintf (stderr, "\n");
7718 fflush (stderr);
7719 }
7720 }
7721 else if (INTERACTIVE)
7722 {
7723 /* The frame whose mini-buffer we're going to display the message
7724 on. It may be larger than the selected frame, so we need to
7725 use its buffer, not the selected frame's buffer. */
7726 Lisp_Object mini_window;
7727 struct frame *f, *sf = SELECTED_FRAME ();
7728
7729 /* Get the frame containing the mini-buffer
7730 that the selected frame is using. */
7731 mini_window = FRAME_MINIBUF_WINDOW (sf);
7732 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
7733
7734 /* A null message buffer means that the frame hasn't really been
7735 initialized yet. Error messages get reported properly by
7736 cmd_error, so this must be just an informative message; toss
7737 it. */
7738 if (FRAME_MESSAGE_BUF (f))
7739 {
7740 if (m)
7741 {
7742 int len;
7743 #ifdef NO_ARG_ARRAY
7744 char *a[3];
7745 a[0] = (char *) a1;
7746 a[1] = (char *) a2;
7747 a[2] = (char *) a3;
7748
7749 len = doprnt (FRAME_MESSAGE_BUF (f),
7750 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3, a);
7751 #else
7752 len = doprnt (FRAME_MESSAGE_BUF (f),
7753 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, 3,
7754 (char **) &a1);
7755 #endif /* NO_ARG_ARRAY */
7756
7757 message2 (FRAME_MESSAGE_BUF (f), len, 0);
7758 }
7759 else
7760 message1 (0);
7761
7762 /* Print should start at the beginning of the message
7763 buffer next time. */
7764 message_buf_print = 0;
7765 }
7766 }
7767 }
7768
7769
7770 /* The non-logging version of message. */
7771
7772 void
7773 message_nolog (m, a1, a2, a3)
7774 char *m;
7775 EMACS_INT a1, a2, a3;
7776 {
7777 Lisp_Object old_log_max;
7778 old_log_max = Vmessage_log_max;
7779 Vmessage_log_max = Qnil;
7780 message (m, a1, a2, a3);
7781 Vmessage_log_max = old_log_max;
7782 }
7783
7784
7785 /* Display the current message in the current mini-buffer. This is
7786 only called from error handlers in process.c, and is not time
7787 critical. */
7788
7789 void
7790 update_echo_area ()
7791 {
7792 if (!NILP (echo_area_buffer[0]))
7793 {
7794 Lisp_Object string;
7795 string = Fcurrent_message ();
7796 message3 (string, SBYTES (string),
7797 !NILP (current_buffer->enable_multibyte_characters));
7798 }
7799 }
7800
7801
7802 /* Make sure echo area buffers in `echo_buffers' are live.
7803 If they aren't, make new ones. */
7804
7805 static void
7806 ensure_echo_area_buffers ()
7807 {
7808 int i;
7809
7810 for (i = 0; i < 2; ++i)
7811 if (!BUFFERP (echo_buffer[i])
7812 || NILP (XBUFFER (echo_buffer[i])->name))
7813 {
7814 char name[30];
7815 Lisp_Object old_buffer;
7816 int j;
7817
7818 old_buffer = echo_buffer[i];
7819 sprintf (name, " *Echo Area %d*", i);
7820 echo_buffer[i] = Fget_buffer_create (build_string (name));
7821 XBUFFER (echo_buffer[i])->truncate_lines = Qnil;
7822
7823 for (j = 0; j < 2; ++j)
7824 if (EQ (old_buffer, echo_area_buffer[j]))
7825 echo_area_buffer[j] = echo_buffer[i];
7826 }
7827 }
7828
7829
7830 /* Call FN with args A1..A4 with either the current or last displayed
7831 echo_area_buffer as current buffer.
7832
7833 WHICH zero means use the current message buffer
7834 echo_area_buffer[0]. If that is nil, choose a suitable buffer
7835 from echo_buffer[] and clear it.
7836
7837 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
7838 suitable buffer from echo_buffer[] and clear it.
7839
7840 Value is what FN returns. */
7841
7842 static int
7843 with_echo_area_buffer (w, which, fn, a1, a2, a3, a4)
7844 struct window *w;
7845 int which;
7846 int (*fn) P_ ((EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT));
7847 EMACS_INT a1;
7848 Lisp_Object a2;
7849 EMACS_INT a3, a4;
7850 {
7851 Lisp_Object buffer;
7852 int this_one, the_other, clear_buffer_p, rc;
7853 int count = SPECPDL_INDEX ();
7854
7855 /* If buffers aren't live, make new ones. */
7856 ensure_echo_area_buffers ();
7857
7858 clear_buffer_p = 0;
7859
7860 if (which == 0)
7861 this_one = 0, the_other = 1;
7862 else if (which > 0)
7863 this_one = 1, the_other = 0;
7864
7865 /* Choose a suitable buffer from echo_buffer[] is we don't
7866 have one. */
7867 if (NILP (echo_area_buffer[this_one]))
7868 {
7869 echo_area_buffer[this_one]
7870 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
7871 ? echo_buffer[the_other]
7872 : echo_buffer[this_one]);
7873 clear_buffer_p = 1;
7874 }
7875
7876 buffer = echo_area_buffer[this_one];
7877
7878 /* Don't get confused by reusing the buffer used for echoing
7879 for a different purpose. */
7880 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
7881 cancel_echoing ();
7882
7883 record_unwind_protect (unwind_with_echo_area_buffer,
7884 with_echo_area_buffer_unwind_data (w));
7885
7886 /* Make the echo area buffer current. Note that for display
7887 purposes, it is not necessary that the displayed window's buffer
7888 == current_buffer, except for text property lookup. So, let's
7889 only set that buffer temporarily here without doing a full
7890 Fset_window_buffer. We must also change w->pointm, though,
7891 because otherwise an assertions in unshow_buffer fails, and Emacs
7892 aborts. */
7893 set_buffer_internal_1 (XBUFFER (buffer));
7894 if (w)
7895 {
7896 w->buffer = buffer;
7897 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
7898 }
7899
7900 current_buffer->undo_list = Qt;
7901 current_buffer->read_only = Qnil;
7902 specbind (Qinhibit_read_only, Qt);
7903 specbind (Qinhibit_modification_hooks, Qt);
7904
7905 if (clear_buffer_p && Z > BEG)
7906 del_range (BEG, Z);
7907
7908 xassert (BEGV >= BEG);
7909 xassert (ZV <= Z && ZV >= BEGV);
7910
7911 rc = fn (a1, a2, a3, a4);
7912
7913 xassert (BEGV >= BEG);
7914 xassert (ZV <= Z && ZV >= BEGV);
7915
7916 unbind_to (count, Qnil);
7917 return rc;
7918 }
7919
7920
7921 /* Save state that should be preserved around the call to the function
7922 FN called in with_echo_area_buffer. */
7923
7924 static Lisp_Object
7925 with_echo_area_buffer_unwind_data (w)
7926 struct window *w;
7927 {
7928 int i = 0;
7929 Lisp_Object vector;
7930
7931 /* Reduce consing by keeping one vector in
7932 Vwith_echo_area_save_vector. */
7933 vector = Vwith_echo_area_save_vector;
7934 Vwith_echo_area_save_vector = Qnil;
7935
7936 if (NILP (vector))
7937 vector = Fmake_vector (make_number (7), Qnil);
7938
7939 XSETBUFFER (AREF (vector, i), current_buffer); ++i;
7940 AREF (vector, i) = Vdeactivate_mark, ++i;
7941 AREF (vector, i) = make_number (windows_or_buffers_changed), ++i;
7942
7943 if (w)
7944 {
7945 XSETWINDOW (AREF (vector, i), w); ++i;
7946 AREF (vector, i) = w->buffer; ++i;
7947 AREF (vector, i) = make_number (XMARKER (w->pointm)->charpos); ++i;
7948 AREF (vector, i) = make_number (XMARKER (w->pointm)->bytepos); ++i;
7949 }
7950 else
7951 {
7952 int end = i + 4;
7953 for (; i < end; ++i)
7954 AREF (vector, i) = Qnil;
7955 }
7956
7957 xassert (i == ASIZE (vector));
7958 return vector;
7959 }
7960
7961
7962 /* Restore global state from VECTOR which was created by
7963 with_echo_area_buffer_unwind_data. */
7964
7965 static Lisp_Object
7966 unwind_with_echo_area_buffer (vector)
7967 Lisp_Object vector;
7968 {
7969 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
7970 Vdeactivate_mark = AREF (vector, 1);
7971 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
7972
7973 if (WINDOWP (AREF (vector, 3)))
7974 {
7975 struct window *w;
7976 Lisp_Object buffer, charpos, bytepos;
7977
7978 w = XWINDOW (AREF (vector, 3));
7979 buffer = AREF (vector, 4);
7980 charpos = AREF (vector, 5);
7981 bytepos = AREF (vector, 6);
7982
7983 w->buffer = buffer;
7984 set_marker_both (w->pointm, buffer,
7985 XFASTINT (charpos), XFASTINT (bytepos));
7986 }
7987
7988 Vwith_echo_area_save_vector = vector;
7989 return Qnil;
7990 }
7991
7992
7993 /* Set up the echo area for use by print functions. MULTIBYTE_P
7994 non-zero means we will print multibyte. */
7995
7996 void
7997 setup_echo_area_for_printing (multibyte_p)
7998 int multibyte_p;
7999 {
8000 /* If we can't find an echo area any more, exit. */
8001 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
8002 Fkill_emacs (Qnil);
8003
8004 ensure_echo_area_buffers ();
8005
8006 if (!message_buf_print)
8007 {
8008 /* A message has been output since the last time we printed.
8009 Choose a fresh echo area buffer. */
8010 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8011 echo_area_buffer[0] = echo_buffer[1];
8012 else
8013 echo_area_buffer[0] = echo_buffer[0];
8014
8015 /* Switch to that buffer and clear it. */
8016 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8017 current_buffer->truncate_lines = Qnil;
8018
8019 if (Z > BEG)
8020 {
8021 int count = SPECPDL_INDEX ();
8022 specbind (Qinhibit_read_only, Qt);
8023 /* Note that undo recording is always disabled. */
8024 del_range (BEG, Z);
8025 unbind_to (count, Qnil);
8026 }
8027 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8028
8029 /* Set up the buffer for the multibyteness we need. */
8030 if (multibyte_p
8031 != !NILP (current_buffer->enable_multibyte_characters))
8032 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
8033
8034 /* Raise the frame containing the echo area. */
8035 if (minibuffer_auto_raise)
8036 {
8037 struct frame *sf = SELECTED_FRAME ();
8038 Lisp_Object mini_window;
8039 mini_window = FRAME_MINIBUF_WINDOW (sf);
8040 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8041 }
8042
8043 message_log_maybe_newline ();
8044 message_buf_print = 1;
8045 }
8046 else
8047 {
8048 if (NILP (echo_area_buffer[0]))
8049 {
8050 if (EQ (echo_area_buffer[1], echo_buffer[0]))
8051 echo_area_buffer[0] = echo_buffer[1];
8052 else
8053 echo_area_buffer[0] = echo_buffer[0];
8054 }
8055
8056 if (current_buffer != XBUFFER (echo_area_buffer[0]))
8057 {
8058 /* Someone switched buffers between print requests. */
8059 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
8060 current_buffer->truncate_lines = Qnil;
8061 }
8062 }
8063 }
8064
8065
8066 /* Display an echo area message in window W. Value is non-zero if W's
8067 height is changed. If display_last_displayed_message_p is
8068 non-zero, display the message that was last displayed, otherwise
8069 display the current message. */
8070
8071 static int
8072 display_echo_area (w)
8073 struct window *w;
8074 {
8075 int i, no_message_p, window_height_changed_p, count;
8076
8077 /* Temporarily disable garbage collections while displaying the echo
8078 area. This is done because a GC can print a message itself.
8079 That message would modify the echo area buffer's contents while a
8080 redisplay of the buffer is going on, and seriously confuse
8081 redisplay. */
8082 count = inhibit_garbage_collection ();
8083
8084 /* If there is no message, we must call display_echo_area_1
8085 nevertheless because it resizes the window. But we will have to
8086 reset the echo_area_buffer in question to nil at the end because
8087 with_echo_area_buffer will sets it to an empty buffer. */
8088 i = display_last_displayed_message_p ? 1 : 0;
8089 no_message_p = NILP (echo_area_buffer[i]);
8090
8091 window_height_changed_p
8092 = with_echo_area_buffer (w, display_last_displayed_message_p,
8093 display_echo_area_1,
8094 (EMACS_INT) w, Qnil, 0, 0);
8095
8096 if (no_message_p)
8097 echo_area_buffer[i] = Qnil;
8098
8099 unbind_to (count, Qnil);
8100 return window_height_changed_p;
8101 }
8102
8103
8104 /* Helper for display_echo_area. Display the current buffer which
8105 contains the current echo area message in window W, a mini-window,
8106 a pointer to which is passed in A1. A2..A4 are currently not used.
8107 Change the height of W so that all of the message is displayed.
8108 Value is non-zero if height of W was changed. */
8109
8110 static int
8111 display_echo_area_1 (a1, a2, a3, a4)
8112 EMACS_INT a1;
8113 Lisp_Object a2;
8114 EMACS_INT a3, a4;
8115 {
8116 struct window *w = (struct window *) a1;
8117 Lisp_Object window;
8118 struct text_pos start;
8119 int window_height_changed_p = 0;
8120
8121 /* Do this before displaying, so that we have a large enough glyph
8122 matrix for the display. If we can't get enough space for the
8123 whole text, display the last N lines. That works by setting w->start. */
8124 window_height_changed_p = resize_mini_window (w, 0);
8125
8126 /* Use the starting position chosen by resize_mini_window. */
8127 SET_TEXT_POS_FROM_MARKER (start, w->start);
8128
8129 /* Display. */
8130 clear_glyph_matrix (w->desired_matrix);
8131 XSETWINDOW (window, w);
8132 try_window (window, start, 0);
8133
8134 return window_height_changed_p;
8135 }
8136
8137
8138 /* Resize the echo area window to exactly the size needed for the
8139 currently displayed message, if there is one. If a mini-buffer
8140 is active, don't shrink it. */
8141
8142 void
8143 resize_echo_area_exactly ()
8144 {
8145 if (BUFFERP (echo_area_buffer[0])
8146 && WINDOWP (echo_area_window))
8147 {
8148 struct window *w = XWINDOW (echo_area_window);
8149 int resized_p;
8150 Lisp_Object resize_exactly;
8151
8152 if (minibuf_level == 0)
8153 resize_exactly = Qt;
8154 else
8155 resize_exactly = Qnil;
8156
8157 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
8158 (EMACS_INT) w, resize_exactly, 0, 0);
8159 if (resized_p)
8160 {
8161 ++windows_or_buffers_changed;
8162 ++update_mode_lines;
8163 redisplay_internal (0);
8164 }
8165 }
8166 }
8167
8168
8169 /* Callback function for with_echo_area_buffer, when used from
8170 resize_echo_area_exactly. A1 contains a pointer to the window to
8171 resize, EXACTLY non-nil means resize the mini-window exactly to the
8172 size of the text displayed. A3 and A4 are not used. Value is what
8173 resize_mini_window returns. */
8174
8175 static int
8176 resize_mini_window_1 (a1, exactly, a3, a4)
8177 EMACS_INT a1;
8178 Lisp_Object exactly;
8179 EMACS_INT a3, a4;
8180 {
8181 return resize_mini_window ((struct window *) a1, !NILP (exactly));
8182 }
8183
8184
8185 /* Resize mini-window W to fit the size of its contents. EXACT:P
8186 means size the window exactly to the size needed. Otherwise, it's
8187 only enlarged until W's buffer is empty.
8188
8189 Set W->start to the right place to begin display. If the whole
8190 contents fit, start at the beginning. Otherwise, start so as
8191 to make the end of the contents appear. This is particularly
8192 important for y-or-n-p, but seems desirable generally.
8193
8194 Value is non-zero if the window height has been changed. */
8195
8196 int
8197 resize_mini_window (w, exact_p)
8198 struct window *w;
8199 int exact_p;
8200 {
8201 struct frame *f = XFRAME (w->frame);
8202 int window_height_changed_p = 0;
8203
8204 xassert (MINI_WINDOW_P (w));
8205
8206 /* By default, start display at the beginning. */
8207 set_marker_both (w->start, w->buffer,
8208 BUF_BEGV (XBUFFER (w->buffer)),
8209 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
8210
8211 /* Don't resize windows while redisplaying a window; it would
8212 confuse redisplay functions when the size of the window they are
8213 displaying changes from under them. Such a resizing can happen,
8214 for instance, when which-func prints a long message while
8215 we are running fontification-functions. We're running these
8216 functions with safe_call which binds inhibit-redisplay to t. */
8217 if (!NILP (Vinhibit_redisplay))
8218 return 0;
8219
8220 /* Nil means don't try to resize. */
8221 if (NILP (Vresize_mini_windows)
8222 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
8223 return 0;
8224
8225 if (!FRAME_MINIBUF_ONLY_P (f))
8226 {
8227 struct it it;
8228 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
8229 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
8230 int height, max_height;
8231 int unit = FRAME_LINE_HEIGHT (f);
8232 struct text_pos start;
8233 struct buffer *old_current_buffer = NULL;
8234
8235 if (current_buffer != XBUFFER (w->buffer))
8236 {
8237 old_current_buffer = current_buffer;
8238 set_buffer_internal (XBUFFER (w->buffer));
8239 }
8240
8241 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
8242
8243 /* Compute the max. number of lines specified by the user. */
8244 if (FLOATP (Vmax_mini_window_height))
8245 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
8246 else if (INTEGERP (Vmax_mini_window_height))
8247 max_height = XINT (Vmax_mini_window_height);
8248 else
8249 max_height = total_height / 4;
8250
8251 /* Correct that max. height if it's bogus. */
8252 max_height = max (1, max_height);
8253 max_height = min (total_height, max_height);
8254
8255 /* Find out the height of the text in the window. */
8256 if (it.truncate_lines_p)
8257 height = 1;
8258 else
8259 {
8260 last_height = 0;
8261 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
8262 if (it.max_ascent == 0 && it.max_descent == 0)
8263 height = it.current_y + last_height;
8264 else
8265 height = it.current_y + it.max_ascent + it.max_descent;
8266 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
8267 height = (height + unit - 1) / unit;
8268 }
8269
8270 /* Compute a suitable window start. */
8271 if (height > max_height)
8272 {
8273 height = max_height;
8274 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
8275 move_it_vertically_backward (&it, (height - 1) * unit);
8276 start = it.current.pos;
8277 }
8278 else
8279 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
8280 SET_MARKER_FROM_TEXT_POS (w->start, start);
8281
8282 if (EQ (Vresize_mini_windows, Qgrow_only))
8283 {
8284 /* Let it grow only, until we display an empty message, in which
8285 case the window shrinks again. */
8286 if (height > WINDOW_TOTAL_LINES (w))
8287 {
8288 int old_height = WINDOW_TOTAL_LINES (w);
8289 freeze_window_starts (f, 1);
8290 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8291 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8292 }
8293 else if (height < WINDOW_TOTAL_LINES (w)
8294 && (exact_p || BEGV == ZV))
8295 {
8296 int old_height = WINDOW_TOTAL_LINES (w);
8297 freeze_window_starts (f, 0);
8298 shrink_mini_window (w);
8299 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8300 }
8301 }
8302 else
8303 {
8304 /* Always resize to exact size needed. */
8305 if (height > WINDOW_TOTAL_LINES (w))
8306 {
8307 int old_height = WINDOW_TOTAL_LINES (w);
8308 freeze_window_starts (f, 1);
8309 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8310 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8311 }
8312 else if (height < WINDOW_TOTAL_LINES (w))
8313 {
8314 int old_height = WINDOW_TOTAL_LINES (w);
8315 freeze_window_starts (f, 0);
8316 shrink_mini_window (w);
8317
8318 if (height)
8319 {
8320 freeze_window_starts (f, 1);
8321 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
8322 }
8323
8324 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
8325 }
8326 }
8327
8328 if (old_current_buffer)
8329 set_buffer_internal (old_current_buffer);
8330 }
8331
8332 return window_height_changed_p;
8333 }
8334
8335
8336 /* Value is the current message, a string, or nil if there is no
8337 current message. */
8338
8339 Lisp_Object
8340 current_message ()
8341 {
8342 Lisp_Object msg;
8343
8344 if (NILP (echo_area_buffer[0]))
8345 msg = Qnil;
8346 else
8347 {
8348 with_echo_area_buffer (0, 0, current_message_1,
8349 (EMACS_INT) &msg, Qnil, 0, 0);
8350 if (NILP (msg))
8351 echo_area_buffer[0] = Qnil;
8352 }
8353
8354 return msg;
8355 }
8356
8357
8358 static int
8359 current_message_1 (a1, a2, a3, a4)
8360 EMACS_INT a1;
8361 Lisp_Object a2;
8362 EMACS_INT a3, a4;
8363 {
8364 Lisp_Object *msg = (Lisp_Object *) a1;
8365
8366 if (Z > BEG)
8367 *msg = make_buffer_string (BEG, Z, 1);
8368 else
8369 *msg = Qnil;
8370 return 0;
8371 }
8372
8373
8374 /* Push the current message on Vmessage_stack for later restauration
8375 by restore_message. Value is non-zero if the current message isn't
8376 empty. This is a relatively infrequent operation, so it's not
8377 worth optimizing. */
8378
8379 int
8380 push_message ()
8381 {
8382 Lisp_Object msg;
8383 msg = current_message ();
8384 Vmessage_stack = Fcons (msg, Vmessage_stack);
8385 return STRINGP (msg);
8386 }
8387
8388
8389 /* Restore message display from the top of Vmessage_stack. */
8390
8391 void
8392 restore_message ()
8393 {
8394 Lisp_Object msg;
8395
8396 xassert (CONSP (Vmessage_stack));
8397 msg = XCAR (Vmessage_stack);
8398 if (STRINGP (msg))
8399 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
8400 else
8401 message3_nolog (msg, 0, 0);
8402 }
8403
8404
8405 /* Handler for record_unwind_protect calling pop_message. */
8406
8407 Lisp_Object
8408 pop_message_unwind (dummy)
8409 Lisp_Object dummy;
8410 {
8411 pop_message ();
8412 return Qnil;
8413 }
8414
8415 /* Pop the top-most entry off Vmessage_stack. */
8416
8417 void
8418 pop_message ()
8419 {
8420 xassert (CONSP (Vmessage_stack));
8421 Vmessage_stack = XCDR (Vmessage_stack);
8422 }
8423
8424
8425 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
8426 exits. If the stack is not empty, we have a missing pop_message
8427 somewhere. */
8428
8429 void
8430 check_message_stack ()
8431 {
8432 if (!NILP (Vmessage_stack))
8433 abort ();
8434 }
8435
8436
8437 /* Truncate to NCHARS what will be displayed in the echo area the next
8438 time we display it---but don't redisplay it now. */
8439
8440 void
8441 truncate_echo_area (nchars)
8442 int nchars;
8443 {
8444 if (nchars == 0)
8445 echo_area_buffer[0] = Qnil;
8446 /* A null message buffer means that the frame hasn't really been
8447 initialized yet. Error messages get reported properly by
8448 cmd_error, so this must be just an informative message; toss it. */
8449 else if (!noninteractive
8450 && INTERACTIVE
8451 && !NILP (echo_area_buffer[0]))
8452 {
8453 struct frame *sf = SELECTED_FRAME ();
8454 if (FRAME_MESSAGE_BUF (sf))
8455 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
8456 }
8457 }
8458
8459
8460 /* Helper function for truncate_echo_area. Truncate the current
8461 message to at most NCHARS characters. */
8462
8463 static int
8464 truncate_message_1 (nchars, a2, a3, a4)
8465 EMACS_INT nchars;
8466 Lisp_Object a2;
8467 EMACS_INT a3, a4;
8468 {
8469 if (BEG + nchars < Z)
8470 del_range (BEG + nchars, Z);
8471 if (Z == BEG)
8472 echo_area_buffer[0] = Qnil;
8473 return 0;
8474 }
8475
8476
8477 /* Set the current message to a substring of S or STRING.
8478
8479 If STRING is a Lisp string, set the message to the first NBYTES
8480 bytes from STRING. NBYTES zero means use the whole string. If
8481 STRING is multibyte, the message will be displayed multibyte.
8482
8483 If S is not null, set the message to the first LEN bytes of S. LEN
8484 zero means use the whole string. MULTIBYTE_P non-zero means S is
8485 multibyte. Display the message multibyte in that case.
8486
8487 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
8488 to t before calling set_message_1 (which calls insert).
8489 */
8490
8491 void
8492 set_message (s, string, nbytes, multibyte_p)
8493 const char *s;
8494 Lisp_Object string;
8495 int nbytes, multibyte_p;
8496 {
8497 message_enable_multibyte
8498 = ((s && multibyte_p)
8499 || (STRINGP (string) && STRING_MULTIBYTE (string)));
8500
8501 with_echo_area_buffer (0, 0, set_message_1,
8502 (EMACS_INT) s, string, nbytes, multibyte_p);
8503 message_buf_print = 0;
8504 help_echo_showing_p = 0;
8505 }
8506
8507
8508 /* Helper function for set_message. Arguments have the same meaning
8509 as there, with A1 corresponding to S and A2 corresponding to STRING
8510 This function is called with the echo area buffer being
8511 current. */
8512
8513 static int
8514 set_message_1 (a1, a2, nbytes, multibyte_p)
8515 EMACS_INT a1;
8516 Lisp_Object a2;
8517 EMACS_INT nbytes, multibyte_p;
8518 {
8519 const char *s = (const char *) a1;
8520 Lisp_Object string = a2;
8521
8522 /* Change multibyteness of the echo buffer appropriately. */
8523 if (message_enable_multibyte
8524 != !NILP (current_buffer->enable_multibyte_characters))
8525 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
8526
8527 current_buffer->truncate_lines = message_truncate_lines ? Qt : Qnil;
8528
8529 /* Insert new message at BEG. */
8530 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
8531 Ferase_buffer ();
8532
8533 if (STRINGP (string))
8534 {
8535 int nchars;
8536
8537 if (nbytes == 0)
8538 nbytes = SBYTES (string);
8539 nchars = string_byte_to_char (string, nbytes);
8540
8541 /* This function takes care of single/multibyte conversion. We
8542 just have to ensure that the echo area buffer has the right
8543 setting of enable_multibyte_characters. */
8544 insert_from_string (string, 0, 0, nchars, nbytes, 1);
8545 }
8546 else if (s)
8547 {
8548 if (nbytes == 0)
8549 nbytes = strlen (s);
8550
8551 if (multibyte_p && NILP (current_buffer->enable_multibyte_characters))
8552 {
8553 /* Convert from multi-byte to single-byte. */
8554 int i, c, n;
8555 unsigned char work[1];
8556
8557 /* Convert a multibyte string to single-byte. */
8558 for (i = 0; i < nbytes; i += n)
8559 {
8560 c = string_char_and_length (s + i, nbytes - i, &n);
8561 work[0] = (SINGLE_BYTE_CHAR_P (c)
8562 ? c
8563 : multibyte_char_to_unibyte (c, Qnil));
8564 insert_1_both (work, 1, 1, 1, 0, 0);
8565 }
8566 }
8567 else if (!multibyte_p
8568 && !NILP (current_buffer->enable_multibyte_characters))
8569 {
8570 /* Convert from single-byte to multi-byte. */
8571 int i, c, n;
8572 const unsigned char *msg = (const unsigned char *) s;
8573 unsigned char str[MAX_MULTIBYTE_LENGTH];
8574
8575 /* Convert a single-byte string to multibyte. */
8576 for (i = 0; i < nbytes; i++)
8577 {
8578 c = unibyte_char_to_multibyte (msg[i]);
8579 n = CHAR_STRING (c, str);
8580 insert_1_both (str, 1, n, 1, 0, 0);
8581 }
8582 }
8583 else
8584 insert_1 (s, nbytes, 1, 0, 0);
8585 }
8586
8587 return 0;
8588 }
8589
8590
8591 /* Clear messages. CURRENT_P non-zero means clear the current
8592 message. LAST_DISPLAYED_P non-zero means clear the message
8593 last displayed. */
8594
8595 void
8596 clear_message (current_p, last_displayed_p)
8597 int current_p, last_displayed_p;
8598 {
8599 if (current_p)
8600 {
8601 echo_area_buffer[0] = Qnil;
8602 message_cleared_p = 1;
8603 }
8604
8605 if (last_displayed_p)
8606 echo_area_buffer[1] = Qnil;
8607
8608 message_buf_print = 0;
8609 }
8610
8611 /* Clear garbaged frames.
8612
8613 This function is used where the old redisplay called
8614 redraw_garbaged_frames which in turn called redraw_frame which in
8615 turn called clear_frame. The call to clear_frame was a source of
8616 flickering. I believe a clear_frame is not necessary. It should
8617 suffice in the new redisplay to invalidate all current matrices,
8618 and ensure a complete redisplay of all windows. */
8619
8620 static void
8621 clear_garbaged_frames ()
8622 {
8623 if (frame_garbaged)
8624 {
8625 Lisp_Object tail, frame;
8626 int changed_count = 0;
8627
8628 FOR_EACH_FRAME (tail, frame)
8629 {
8630 struct frame *f = XFRAME (frame);
8631
8632 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
8633 {
8634 if (f->resized_p)
8635 {
8636 Fredraw_frame (frame);
8637 f->force_flush_display_p = 1;
8638 }
8639 clear_current_matrices (f);
8640 changed_count++;
8641 f->garbaged = 0;
8642 f->resized_p = 0;
8643 }
8644 }
8645
8646 frame_garbaged = 0;
8647 if (changed_count)
8648 ++windows_or_buffers_changed;
8649 }
8650 }
8651
8652
8653 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
8654 is non-zero update selected_frame. Value is non-zero if the
8655 mini-windows height has been changed. */
8656
8657 static int
8658 echo_area_display (update_frame_p)
8659 int update_frame_p;
8660 {
8661 Lisp_Object mini_window;
8662 struct window *w;
8663 struct frame *f;
8664 int window_height_changed_p = 0;
8665 struct frame *sf = SELECTED_FRAME ();
8666
8667 mini_window = FRAME_MINIBUF_WINDOW (sf);
8668 w = XWINDOW (mini_window);
8669 f = XFRAME (WINDOW_FRAME (w));
8670
8671 /* Don't display if frame is invisible or not yet initialized. */
8672 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
8673 return 0;
8674
8675 /* The terminal frame is used as the first Emacs frame on the Mac OS. */
8676 #ifndef MAC_OS8
8677 #ifdef HAVE_WINDOW_SYSTEM
8678 /* When Emacs starts, selected_frame may be a visible terminal
8679 frame, even if we run under a window system. If we let this
8680 through, a message would be displayed on the terminal. */
8681 if (EQ (selected_frame, Vterminal_frame)
8682 && !NILP (Vwindow_system))
8683 return 0;
8684 #endif /* HAVE_WINDOW_SYSTEM */
8685 #endif
8686
8687 /* Redraw garbaged frames. */
8688 if (frame_garbaged)
8689 clear_garbaged_frames ();
8690
8691 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
8692 {
8693 echo_area_window = mini_window;
8694 window_height_changed_p = display_echo_area (w);
8695 w->must_be_updated_p = 1;
8696
8697 /* Update the display, unless called from redisplay_internal.
8698 Also don't update the screen during redisplay itself. The
8699 update will happen at the end of redisplay, and an update
8700 here could cause confusion. */
8701 if (update_frame_p && !redisplaying_p)
8702 {
8703 int n = 0;
8704
8705 /* If the display update has been interrupted by pending
8706 input, update mode lines in the frame. Due to the
8707 pending input, it might have been that redisplay hasn't
8708 been called, so that mode lines above the echo area are
8709 garbaged. This looks odd, so we prevent it here. */
8710 if (!display_completed)
8711 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
8712
8713 if (window_height_changed_p
8714 /* Don't do this if Emacs is shutting down. Redisplay
8715 needs to run hooks. */
8716 && !NILP (Vrun_hooks))
8717 {
8718 /* Must update other windows. Likewise as in other
8719 cases, don't let this update be interrupted by
8720 pending input. */
8721 int count = SPECPDL_INDEX ();
8722 specbind (Qredisplay_dont_pause, Qt);
8723 windows_or_buffers_changed = 1;
8724 redisplay_internal (0);
8725 unbind_to (count, Qnil);
8726 }
8727 else if (FRAME_WINDOW_P (f) && n == 0)
8728 {
8729 /* Window configuration is the same as before.
8730 Can do with a display update of the echo area,
8731 unless we displayed some mode lines. */
8732 update_single_window (w, 1);
8733 rif->flush_display (f);
8734 }
8735 else
8736 update_frame (f, 1, 1);
8737
8738 /* If cursor is in the echo area, make sure that the next
8739 redisplay displays the minibuffer, so that the cursor will
8740 be replaced with what the minibuffer wants. */
8741 if (cursor_in_echo_area)
8742 ++windows_or_buffers_changed;
8743 }
8744 }
8745 else if (!EQ (mini_window, selected_window))
8746 windows_or_buffers_changed++;
8747
8748 /* The current message is now also the last one displayed. */
8749 echo_area_buffer[1] = echo_area_buffer[0];
8750
8751 /* Prevent redisplay optimization in redisplay_internal by resetting
8752 this_line_start_pos. This is done because the mini-buffer now
8753 displays the message instead of its buffer text. */
8754 if (EQ (mini_window, selected_window))
8755 CHARPOS (this_line_start_pos) = 0;
8756
8757 return window_height_changed_p;
8758 }
8759
8760
8761 \f
8762 /***********************************************************************
8763 Mode Lines and Frame Titles
8764 ***********************************************************************/
8765
8766 /* A buffer for constructing non-propertized mode-line strings and
8767 frame titles in it; allocated from the heap in init_xdisp and
8768 resized as needed in store_mode_line_noprop_char. */
8769
8770 static char *mode_line_noprop_buf;
8771
8772 /* The buffer's end, and a current output position in it. */
8773
8774 static char *mode_line_noprop_buf_end;
8775 static char *mode_line_noprop_ptr;
8776
8777 #define MODE_LINE_NOPROP_LEN(start) \
8778 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
8779
8780 static enum {
8781 MODE_LINE_DISPLAY = 0,
8782 MODE_LINE_TITLE,
8783 MODE_LINE_NOPROP,
8784 MODE_LINE_STRING
8785 } mode_line_target;
8786
8787 /* Alist that caches the results of :propertize.
8788 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
8789 static Lisp_Object mode_line_proptrans_alist;
8790
8791 /* List of strings making up the mode-line. */
8792 static Lisp_Object mode_line_string_list;
8793
8794 /* Base face property when building propertized mode line string. */
8795 static Lisp_Object mode_line_string_face;
8796 static Lisp_Object mode_line_string_face_prop;
8797
8798
8799 /* Unwind data for mode line strings */
8800
8801 static Lisp_Object Vmode_line_unwind_vector;
8802
8803 static Lisp_Object
8804 format_mode_line_unwind_data (obuf, save_proptrans)
8805 struct buffer *obuf;
8806 {
8807 Lisp_Object vector;
8808
8809 /* Reduce consing by keeping one vector in
8810 Vwith_echo_area_save_vector. */
8811 vector = Vmode_line_unwind_vector;
8812 Vmode_line_unwind_vector = Qnil;
8813
8814 if (NILP (vector))
8815 vector = Fmake_vector (make_number (7), Qnil);
8816
8817 AREF (vector, 0) = make_number (mode_line_target);
8818 AREF (vector, 1) = make_number (MODE_LINE_NOPROP_LEN (0));
8819 AREF (vector, 2) = mode_line_string_list;
8820 AREF (vector, 3) = (save_proptrans ? mode_line_proptrans_alist : Qt);
8821 AREF (vector, 4) = mode_line_string_face;
8822 AREF (vector, 5) = mode_line_string_face_prop;
8823
8824 if (obuf)
8825 XSETBUFFER (AREF (vector, 6), obuf);
8826 else
8827 AREF (vector, 6) = Qnil;
8828
8829 return vector;
8830 }
8831
8832 static Lisp_Object
8833 unwind_format_mode_line (vector)
8834 Lisp_Object vector;
8835 {
8836 mode_line_target = XINT (AREF (vector, 0));
8837 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
8838 mode_line_string_list = AREF (vector, 2);
8839 if (! EQ (AREF (vector, 3), Qt))
8840 mode_line_proptrans_alist = AREF (vector, 3);
8841 mode_line_string_face = AREF (vector, 4);
8842 mode_line_string_face_prop = AREF (vector, 5);
8843
8844 if (!NILP (AREF (vector, 6)))
8845 {
8846 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
8847 AREF (vector, 6) = Qnil;
8848 }
8849
8850 Vmode_line_unwind_vector = vector;
8851 return Qnil;
8852 }
8853
8854
8855 /* Store a single character C for the frame title in mode_line_noprop_buf.
8856 Re-allocate mode_line_noprop_buf if necessary. */
8857
8858 static void
8859 #ifdef PROTOTYPES
8860 store_mode_line_noprop_char (char c)
8861 #else
8862 store_mode_line_noprop_char (c)
8863 char c;
8864 #endif
8865 {
8866 /* If output position has reached the end of the allocated buffer,
8867 double the buffer's size. */
8868 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
8869 {
8870 int len = MODE_LINE_NOPROP_LEN (0);
8871 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
8872 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
8873 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
8874 mode_line_noprop_ptr = mode_line_noprop_buf + len;
8875 }
8876
8877 *mode_line_noprop_ptr++ = c;
8878 }
8879
8880
8881 /* Store part of a frame title in mode_line_noprop_buf, beginning at
8882 mode_line_noprop_ptr. STR is the string to store. Do not copy
8883 characters that yield more columns than PRECISION; PRECISION <= 0
8884 means copy the whole string. Pad with spaces until FIELD_WIDTH
8885 number of characters have been copied; FIELD_WIDTH <= 0 means don't
8886 pad. Called from display_mode_element when it is used to build a
8887 frame title. */
8888
8889 static int
8890 store_mode_line_noprop (str, field_width, precision)
8891 const unsigned char *str;
8892 int field_width, precision;
8893 {
8894 int n = 0;
8895 int dummy, nbytes;
8896
8897 /* Copy at most PRECISION chars from STR. */
8898 nbytes = strlen (str);
8899 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
8900 while (nbytes--)
8901 store_mode_line_noprop_char (*str++);
8902
8903 /* Fill up with spaces until FIELD_WIDTH reached. */
8904 while (field_width > 0
8905 && n < field_width)
8906 {
8907 store_mode_line_noprop_char (' ');
8908 ++n;
8909 }
8910
8911 return n;
8912 }
8913
8914 /***********************************************************************
8915 Frame Titles
8916 ***********************************************************************/
8917
8918 #ifdef HAVE_WINDOW_SYSTEM
8919
8920 /* Set the title of FRAME, if it has changed. The title format is
8921 Vicon_title_format if FRAME is iconified, otherwise it is
8922 frame_title_format. */
8923
8924 static void
8925 x_consider_frame_title (frame)
8926 Lisp_Object frame;
8927 {
8928 struct frame *f = XFRAME (frame);
8929
8930 if (FRAME_WINDOW_P (f)
8931 || FRAME_MINIBUF_ONLY_P (f)
8932 || f->explicit_name)
8933 {
8934 /* Do we have more than one visible frame on this X display? */
8935 Lisp_Object tail;
8936 Lisp_Object fmt;
8937 int title_start;
8938 char *title;
8939 int len;
8940 struct it it;
8941 int count = SPECPDL_INDEX ();
8942
8943 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
8944 {
8945 Lisp_Object other_frame = XCAR (tail);
8946 struct frame *tf = XFRAME (other_frame);
8947
8948 if (tf != f
8949 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
8950 && !FRAME_MINIBUF_ONLY_P (tf)
8951 && !EQ (other_frame, tip_frame)
8952 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
8953 break;
8954 }
8955
8956 /* Set global variable indicating that multiple frames exist. */
8957 multiple_frames = CONSP (tail);
8958
8959 /* Switch to the buffer of selected window of the frame. Set up
8960 mode_line_target so that display_mode_element will output into
8961 mode_line_noprop_buf; then display the title. */
8962 record_unwind_protect (unwind_format_mode_line,
8963 format_mode_line_unwind_data (current_buffer, 0));
8964
8965 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
8966 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
8967
8968 mode_line_target = MODE_LINE_TITLE;
8969 title_start = MODE_LINE_NOPROP_LEN (0);
8970 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
8971 NULL, DEFAULT_FACE_ID);
8972 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
8973 len = MODE_LINE_NOPROP_LEN (title_start);
8974 title = mode_line_noprop_buf + title_start;
8975 unbind_to (count, Qnil);
8976
8977 /* Set the title only if it's changed. This avoids consing in
8978 the common case where it hasn't. (If it turns out that we've
8979 already wasted too much time by walking through the list with
8980 display_mode_element, then we might need to optimize at a
8981 higher level than this.) */
8982 if (! STRINGP (f->name)
8983 || SBYTES (f->name) != len
8984 || bcmp (title, SDATA (f->name), len) != 0)
8985 x_implicitly_set_name (f, make_string (title, len), Qnil);
8986 }
8987 }
8988
8989 #endif /* not HAVE_WINDOW_SYSTEM */
8990
8991
8992
8993 \f
8994 /***********************************************************************
8995 Menu Bars
8996 ***********************************************************************/
8997
8998
8999 /* Prepare for redisplay by updating menu-bar item lists when
9000 appropriate. This can call eval. */
9001
9002 void
9003 prepare_menu_bars ()
9004 {
9005 int all_windows;
9006 struct gcpro gcpro1, gcpro2;
9007 struct frame *f;
9008 Lisp_Object tooltip_frame;
9009
9010 #ifdef HAVE_WINDOW_SYSTEM
9011 tooltip_frame = tip_frame;
9012 #else
9013 tooltip_frame = Qnil;
9014 #endif
9015
9016 /* Update all frame titles based on their buffer names, etc. We do
9017 this before the menu bars so that the buffer-menu will show the
9018 up-to-date frame titles. */
9019 #ifdef HAVE_WINDOW_SYSTEM
9020 if (windows_or_buffers_changed || update_mode_lines)
9021 {
9022 Lisp_Object tail, frame;
9023
9024 FOR_EACH_FRAME (tail, frame)
9025 {
9026 f = XFRAME (frame);
9027 if (!EQ (frame, tooltip_frame)
9028 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
9029 x_consider_frame_title (frame);
9030 }
9031 }
9032 #endif /* HAVE_WINDOW_SYSTEM */
9033
9034 /* Update the menu bar item lists, if appropriate. This has to be
9035 done before any actual redisplay or generation of display lines. */
9036 all_windows = (update_mode_lines
9037 || buffer_shared > 1
9038 || windows_or_buffers_changed);
9039 if (all_windows)
9040 {
9041 Lisp_Object tail, frame;
9042 int count = SPECPDL_INDEX ();
9043 /* 1 means that update_menu_bar has run its hooks
9044 so any further calls to update_menu_bar shouldn't do so again. */
9045 int menu_bar_hooks_run = 0;
9046
9047 record_unwind_save_match_data ();
9048
9049 FOR_EACH_FRAME (tail, frame)
9050 {
9051 f = XFRAME (frame);
9052
9053 /* Ignore tooltip frame. */
9054 if (EQ (frame, tooltip_frame))
9055 continue;
9056
9057 /* If a window on this frame changed size, report that to
9058 the user and clear the size-change flag. */
9059 if (FRAME_WINDOW_SIZES_CHANGED (f))
9060 {
9061 Lisp_Object functions;
9062
9063 /* Clear flag first in case we get an error below. */
9064 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
9065 functions = Vwindow_size_change_functions;
9066 GCPRO2 (tail, functions);
9067
9068 while (CONSP (functions))
9069 {
9070 call1 (XCAR (functions), frame);
9071 functions = XCDR (functions);
9072 }
9073 UNGCPRO;
9074 }
9075
9076 GCPRO1 (tail);
9077 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
9078 #ifdef HAVE_WINDOW_SYSTEM
9079 update_tool_bar (f, 0);
9080 #ifdef MAC_OS
9081 mac_update_title_bar (f, 0);
9082 #endif
9083 #endif
9084 UNGCPRO;
9085 }
9086
9087 unbind_to (count, Qnil);
9088 }
9089 else
9090 {
9091 struct frame *sf = SELECTED_FRAME ();
9092 update_menu_bar (sf, 1, 0);
9093 #ifdef HAVE_WINDOW_SYSTEM
9094 update_tool_bar (sf, 1);
9095 #ifdef MAC_OS
9096 mac_update_title_bar (sf, 1);
9097 #endif
9098 #endif
9099 }
9100
9101 /* Motif needs this. See comment in xmenu.c. Turn it off when
9102 pending_menu_activation is not defined. */
9103 #ifdef USE_X_TOOLKIT
9104 pending_menu_activation = 0;
9105 #endif
9106 }
9107
9108
9109 /* Update the menu bar item list for frame F. This has to be done
9110 before we start to fill in any display lines, because it can call
9111 eval.
9112
9113 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
9114
9115 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
9116 already ran the menu bar hooks for this redisplay, so there
9117 is no need to run them again. The return value is the
9118 updated value of this flag, to pass to the next call. */
9119
9120 static int
9121 update_menu_bar (f, save_match_data, hooks_run)
9122 struct frame *f;
9123 int save_match_data;
9124 int hooks_run;
9125 {
9126 Lisp_Object window;
9127 register struct window *w;
9128
9129 /* If called recursively during a menu update, do nothing. This can
9130 happen when, for instance, an activate-menubar-hook causes a
9131 redisplay. */
9132 if (inhibit_menubar_update)
9133 return;
9134
9135 window = FRAME_SELECTED_WINDOW (f);
9136 w = XWINDOW (window);
9137
9138 #if 0 /* The if statement below this if statement used to include the
9139 condition !NILP (w->update_mode_line), rather than using
9140 update_mode_lines directly, and this if statement may have
9141 been added to make that condition work. Now the if
9142 statement below matches its comment, this isn't needed. */
9143 if (update_mode_lines)
9144 w->update_mode_line = Qt;
9145 #endif
9146
9147 if (FRAME_WINDOW_P (f)
9148 ?
9149 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9150 || defined (USE_GTK)
9151 FRAME_EXTERNAL_MENU_BAR (f)
9152 #else
9153 FRAME_MENU_BAR_LINES (f) > 0
9154 #endif
9155 : FRAME_MENU_BAR_LINES (f) > 0)
9156 {
9157 /* If the user has switched buffers or windows, we need to
9158 recompute to reflect the new bindings. But we'll
9159 recompute when update_mode_lines is set too; that means
9160 that people can use force-mode-line-update to request
9161 that the menu bar be recomputed. The adverse effect on
9162 the rest of the redisplay algorithm is about the same as
9163 windows_or_buffers_changed anyway. */
9164 if (windows_or_buffers_changed
9165 /* This used to test w->update_mode_line, but we believe
9166 there is no need to recompute the menu in that case. */
9167 || update_mode_lines
9168 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9169 < BUF_MODIFF (XBUFFER (w->buffer)))
9170 != !NILP (w->last_had_star))
9171 || ((!NILP (Vtransient_mark_mode)
9172 && !NILP (XBUFFER (w->buffer)->mark_active))
9173 != !NILP (w->region_showing)))
9174 {
9175 struct buffer *prev = current_buffer;
9176 int count = SPECPDL_INDEX ();
9177
9178 specbind (Qinhibit_menubar_update, Qt);
9179
9180 set_buffer_internal_1 (XBUFFER (w->buffer));
9181 if (save_match_data)
9182 record_unwind_save_match_data ();
9183 if (NILP (Voverriding_local_map_menu_flag))
9184 {
9185 specbind (Qoverriding_terminal_local_map, Qnil);
9186 specbind (Qoverriding_local_map, Qnil);
9187 }
9188
9189 if (!hooks_run)
9190 {
9191 /* Run the Lucid hook. */
9192 safe_run_hooks (Qactivate_menubar_hook);
9193
9194 /* If it has changed current-menubar from previous value,
9195 really recompute the menu-bar from the value. */
9196 if (! NILP (Vlucid_menu_bar_dirty_flag))
9197 call0 (Qrecompute_lucid_menubar);
9198
9199 safe_run_hooks (Qmenu_bar_update_hook);
9200
9201 hooks_run = 1;
9202 }
9203
9204 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
9205
9206 /* Redisplay the menu bar in case we changed it. */
9207 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
9208 || defined (USE_GTK)
9209 if (FRAME_WINDOW_P (f))
9210 {
9211 #ifdef MAC_OS
9212 /* All frames on Mac OS share the same menubar. So only
9213 the selected frame should be allowed to set it. */
9214 if (f == SELECTED_FRAME ())
9215 #endif
9216 set_frame_menubar (f, 0, 0);
9217 }
9218 else
9219 /* On a terminal screen, the menu bar is an ordinary screen
9220 line, and this makes it get updated. */
9221 w->update_mode_line = Qt;
9222 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9223 /* In the non-toolkit version, the menu bar is an ordinary screen
9224 line, and this makes it get updated. */
9225 w->update_mode_line = Qt;
9226 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || MAC_OS || USE_GTK) */
9227
9228 unbind_to (count, Qnil);
9229 set_buffer_internal_1 (prev);
9230 }
9231 }
9232
9233 return hooks_run;
9234 }
9235
9236
9237 \f
9238 /***********************************************************************
9239 Output Cursor
9240 ***********************************************************************/
9241
9242 #ifdef HAVE_WINDOW_SYSTEM
9243
9244 /* EXPORT:
9245 Nominal cursor position -- where to draw output.
9246 HPOS and VPOS are window relative glyph matrix coordinates.
9247 X and Y are window relative pixel coordinates. */
9248
9249 struct cursor_pos output_cursor;
9250
9251
9252 /* EXPORT:
9253 Set the global variable output_cursor to CURSOR. All cursor
9254 positions are relative to updated_window. */
9255
9256 void
9257 set_output_cursor (cursor)
9258 struct cursor_pos *cursor;
9259 {
9260 output_cursor.hpos = cursor->hpos;
9261 output_cursor.vpos = cursor->vpos;
9262 output_cursor.x = cursor->x;
9263 output_cursor.y = cursor->y;
9264 }
9265
9266
9267 /* EXPORT for RIF:
9268 Set a nominal cursor position.
9269
9270 HPOS and VPOS are column/row positions in a window glyph matrix. X
9271 and Y are window text area relative pixel positions.
9272
9273 If this is done during an update, updated_window will contain the
9274 window that is being updated and the position is the future output
9275 cursor position for that window. If updated_window is null, use
9276 selected_window and display the cursor at the given position. */
9277
9278 void
9279 x_cursor_to (vpos, hpos, y, x)
9280 int vpos, hpos, y, x;
9281 {
9282 struct window *w;
9283
9284 /* If updated_window is not set, work on selected_window. */
9285 if (updated_window)
9286 w = updated_window;
9287 else
9288 w = XWINDOW (selected_window);
9289
9290 /* Set the output cursor. */
9291 output_cursor.hpos = hpos;
9292 output_cursor.vpos = vpos;
9293 output_cursor.x = x;
9294 output_cursor.y = y;
9295
9296 /* If not called as part of an update, really display the cursor.
9297 This will also set the cursor position of W. */
9298 if (updated_window == NULL)
9299 {
9300 BLOCK_INPUT;
9301 display_and_set_cursor (w, 1, hpos, vpos, x, y);
9302 if (rif->flush_display_optional)
9303 rif->flush_display_optional (SELECTED_FRAME ());
9304 UNBLOCK_INPUT;
9305 }
9306 }
9307
9308 #endif /* HAVE_WINDOW_SYSTEM */
9309
9310 \f
9311 /***********************************************************************
9312 Tool-bars
9313 ***********************************************************************/
9314
9315 #ifdef HAVE_WINDOW_SYSTEM
9316
9317 /* Where the mouse was last time we reported a mouse event. */
9318
9319 FRAME_PTR last_mouse_frame;
9320
9321 /* Tool-bar item index of the item on which a mouse button was pressed
9322 or -1. */
9323
9324 int last_tool_bar_item;
9325
9326
9327 /* Update the tool-bar item list for frame F. This has to be done
9328 before we start to fill in any display lines. Called from
9329 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
9330 and restore it here. */
9331
9332 static void
9333 update_tool_bar (f, save_match_data)
9334 struct frame *f;
9335 int save_match_data;
9336 {
9337 #ifdef USE_GTK
9338 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
9339 #else
9340 int do_update = WINDOWP (f->tool_bar_window)
9341 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
9342 #endif
9343
9344 if (do_update)
9345 {
9346 Lisp_Object window;
9347 struct window *w;
9348
9349 window = FRAME_SELECTED_WINDOW (f);
9350 w = XWINDOW (window);
9351
9352 /* If the user has switched buffers or windows, we need to
9353 recompute to reflect the new bindings. But we'll
9354 recompute when update_mode_lines is set too; that means
9355 that people can use force-mode-line-update to request
9356 that the menu bar be recomputed. The adverse effect on
9357 the rest of the redisplay algorithm is about the same as
9358 windows_or_buffers_changed anyway. */
9359 if (windows_or_buffers_changed
9360 || !NILP (w->update_mode_line)
9361 || update_mode_lines
9362 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
9363 < BUF_MODIFF (XBUFFER (w->buffer)))
9364 != !NILP (w->last_had_star))
9365 || ((!NILP (Vtransient_mark_mode)
9366 && !NILP (XBUFFER (w->buffer)->mark_active))
9367 != !NILP (w->region_showing)))
9368 {
9369 struct buffer *prev = current_buffer;
9370 int count = SPECPDL_INDEX ();
9371 Lisp_Object new_tool_bar;
9372 int new_n_tool_bar;
9373 struct gcpro gcpro1;
9374
9375 /* Set current_buffer to the buffer of the selected
9376 window of the frame, so that we get the right local
9377 keymaps. */
9378 set_buffer_internal_1 (XBUFFER (w->buffer));
9379
9380 /* Save match data, if we must. */
9381 if (save_match_data)
9382 record_unwind_save_match_data ();
9383
9384 /* Make sure that we don't accidentally use bogus keymaps. */
9385 if (NILP (Voverriding_local_map_menu_flag))
9386 {
9387 specbind (Qoverriding_terminal_local_map, Qnil);
9388 specbind (Qoverriding_local_map, Qnil);
9389 }
9390
9391 GCPRO1 (new_tool_bar);
9392
9393 /* Build desired tool-bar items from keymaps. */
9394 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
9395 &new_n_tool_bar);
9396
9397 /* Redisplay the tool-bar if we changed it. */
9398 if (new_n_tool_bar != f->n_tool_bar_items
9399 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
9400 {
9401 /* Redisplay that happens asynchronously due to an expose event
9402 may access f->tool_bar_items. Make sure we update both
9403 variables within BLOCK_INPUT so no such event interrupts. */
9404 BLOCK_INPUT;
9405 f->tool_bar_items = new_tool_bar;
9406 f->n_tool_bar_items = new_n_tool_bar;
9407 w->update_mode_line = Qt;
9408 UNBLOCK_INPUT;
9409 }
9410
9411 UNGCPRO;
9412
9413 unbind_to (count, Qnil);
9414 set_buffer_internal_1 (prev);
9415 }
9416 }
9417 }
9418
9419
9420 /* Set F->desired_tool_bar_string to a Lisp string representing frame
9421 F's desired tool-bar contents. F->tool_bar_items must have
9422 been set up previously by calling prepare_menu_bars. */
9423
9424 static void
9425 build_desired_tool_bar_string (f)
9426 struct frame *f;
9427 {
9428 int i, size, size_needed;
9429 struct gcpro gcpro1, gcpro2, gcpro3;
9430 Lisp_Object image, plist, props;
9431
9432 image = plist = props = Qnil;
9433 GCPRO3 (image, plist, props);
9434
9435 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
9436 Otherwise, make a new string. */
9437
9438 /* The size of the string we might be able to reuse. */
9439 size = (STRINGP (f->desired_tool_bar_string)
9440 ? SCHARS (f->desired_tool_bar_string)
9441 : 0);
9442
9443 /* We need one space in the string for each image. */
9444 size_needed = f->n_tool_bar_items;
9445
9446 /* Reuse f->desired_tool_bar_string, if possible. */
9447 if (size < size_needed || NILP (f->desired_tool_bar_string))
9448 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
9449 make_number (' '));
9450 else
9451 {
9452 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
9453 Fremove_text_properties (make_number (0), make_number (size),
9454 props, f->desired_tool_bar_string);
9455 }
9456
9457 /* Put a `display' property on the string for the images to display,
9458 put a `menu_item' property on tool-bar items with a value that
9459 is the index of the item in F's tool-bar item vector. */
9460 for (i = 0; i < f->n_tool_bar_items; ++i)
9461 {
9462 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
9463
9464 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
9465 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
9466 int hmargin, vmargin, relief, idx, end;
9467 extern Lisp_Object QCrelief, QCmargin, QCconversion;
9468
9469 /* If image is a vector, choose the image according to the
9470 button state. */
9471 image = PROP (TOOL_BAR_ITEM_IMAGES);
9472 if (VECTORP (image))
9473 {
9474 if (enabled_p)
9475 idx = (selected_p
9476 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
9477 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
9478 else
9479 idx = (selected_p
9480 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
9481 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
9482
9483 xassert (ASIZE (image) >= idx);
9484 image = AREF (image, idx);
9485 }
9486 else
9487 idx = -1;
9488
9489 /* Ignore invalid image specifications. */
9490 if (!valid_image_p (image))
9491 continue;
9492
9493 /* Display the tool-bar button pressed, or depressed. */
9494 plist = Fcopy_sequence (XCDR (image));
9495
9496 /* Compute margin and relief to draw. */
9497 relief = (tool_bar_button_relief >= 0
9498 ? tool_bar_button_relief
9499 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
9500 hmargin = vmargin = relief;
9501
9502 if (INTEGERP (Vtool_bar_button_margin)
9503 && XINT (Vtool_bar_button_margin) > 0)
9504 {
9505 hmargin += XFASTINT (Vtool_bar_button_margin);
9506 vmargin += XFASTINT (Vtool_bar_button_margin);
9507 }
9508 else if (CONSP (Vtool_bar_button_margin))
9509 {
9510 if (INTEGERP (XCAR (Vtool_bar_button_margin))
9511 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
9512 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
9513
9514 if (INTEGERP (XCDR (Vtool_bar_button_margin))
9515 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
9516 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
9517 }
9518
9519 if (auto_raise_tool_bar_buttons_p)
9520 {
9521 /* Add a `:relief' property to the image spec if the item is
9522 selected. */
9523 if (selected_p)
9524 {
9525 plist = Fplist_put (plist, QCrelief, make_number (-relief));
9526 hmargin -= relief;
9527 vmargin -= relief;
9528 }
9529 }
9530 else
9531 {
9532 /* If image is selected, display it pressed, i.e. with a
9533 negative relief. If it's not selected, display it with a
9534 raised relief. */
9535 plist = Fplist_put (plist, QCrelief,
9536 (selected_p
9537 ? make_number (-relief)
9538 : make_number (relief)));
9539 hmargin -= relief;
9540 vmargin -= relief;
9541 }
9542
9543 /* Put a margin around the image. */
9544 if (hmargin || vmargin)
9545 {
9546 if (hmargin == vmargin)
9547 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
9548 else
9549 plist = Fplist_put (plist, QCmargin,
9550 Fcons (make_number (hmargin),
9551 make_number (vmargin)));
9552 }
9553
9554 /* If button is not enabled, and we don't have special images
9555 for the disabled state, make the image appear disabled by
9556 applying an appropriate algorithm to it. */
9557 if (!enabled_p && idx < 0)
9558 plist = Fplist_put (plist, QCconversion, Qdisabled);
9559
9560 /* Put a `display' text property on the string for the image to
9561 display. Put a `menu-item' property on the string that gives
9562 the start of this item's properties in the tool-bar items
9563 vector. */
9564 image = Fcons (Qimage, plist);
9565 props = list4 (Qdisplay, image,
9566 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
9567
9568 /* Let the last image hide all remaining spaces in the tool bar
9569 string. The string can be longer than needed when we reuse a
9570 previous string. */
9571 if (i + 1 == f->n_tool_bar_items)
9572 end = SCHARS (f->desired_tool_bar_string);
9573 else
9574 end = i + 1;
9575 Fadd_text_properties (make_number (i), make_number (end),
9576 props, f->desired_tool_bar_string);
9577 #undef PROP
9578 }
9579
9580 UNGCPRO;
9581 }
9582
9583
9584 /* Display one line of the tool-bar of frame IT->f.
9585
9586 HEIGHT specifies the desired height of the tool-bar line.
9587 If the actual height of the glyph row is less than HEIGHT, the
9588 row's height is increased to HEIGHT, and the icons are centered
9589 vertically in the new height.
9590
9591 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
9592 count a final empty row in case the tool-bar width exactly matches
9593 the window width.
9594 */
9595
9596 static void
9597 display_tool_bar_line (it, height)
9598 struct it *it;
9599 int height;
9600 {
9601 struct glyph_row *row = it->glyph_row;
9602 int max_x = it->last_visible_x;
9603 struct glyph *last;
9604
9605 prepare_desired_row (row);
9606 row->y = it->current_y;
9607
9608 /* Note that this isn't made use of if the face hasn't a box,
9609 so there's no need to check the face here. */
9610 it->start_of_box_run_p = 1;
9611
9612 while (it->current_x < max_x)
9613 {
9614 int x, n_glyphs_before, i, nglyphs;
9615 struct it it_before;
9616
9617 /* Get the next display element. */
9618 if (!get_next_display_element (it))
9619 {
9620 /* Don't count empty row if we are counting needed tool-bar lines. */
9621 if (height < 0 && !it->hpos)
9622 return;
9623 break;
9624 }
9625
9626 /* Produce glyphs. */
9627 n_glyphs_before = row->used[TEXT_AREA];
9628 it_before = *it;
9629
9630 PRODUCE_GLYPHS (it);
9631
9632 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
9633 i = 0;
9634 x = it_before.current_x;
9635 while (i < nglyphs)
9636 {
9637 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
9638
9639 if (x + glyph->pixel_width > max_x)
9640 {
9641 /* Glyph doesn't fit on line. Backtrack. */
9642 row->used[TEXT_AREA] = n_glyphs_before;
9643 *it = it_before;
9644 /* If this is the only glyph on this line, it will never fit on the
9645 toolbar, so skip it. But ensure there is at least one glyph,
9646 so we don't accidentally disable the tool-bar. */
9647 if (n_glyphs_before == 0
9648 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
9649 break;
9650 goto out;
9651 }
9652
9653 ++it->hpos;
9654 x += glyph->pixel_width;
9655 ++i;
9656 }
9657
9658 /* Stop at line ends. */
9659 if (ITERATOR_AT_END_OF_LINE_P (it))
9660 break;
9661
9662 set_iterator_to_next (it, 1);
9663 }
9664
9665 out:;
9666
9667 row->displays_text_p = row->used[TEXT_AREA] != 0;
9668 /* Use default face for the border below the tool bar. */
9669 if (!row->displays_text_p)
9670 it->face_id = DEFAULT_FACE_ID;
9671 extend_face_to_end_of_line (it);
9672 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
9673 last->right_box_line_p = 1;
9674 if (last == row->glyphs[TEXT_AREA])
9675 last->left_box_line_p = 1;
9676
9677 /* Make line the desired height and center it vertically. */
9678 if ((height -= it->max_ascent + it->max_descent) > 0)
9679 {
9680 /* Don't add more than one line height. */
9681 height %= FRAME_LINE_HEIGHT (it->f);
9682 it->max_ascent += height / 2;
9683 it->max_descent += (height + 1) / 2;
9684 }
9685
9686 compute_line_metrics (it);
9687
9688 /* If line is empty, make it occupy the rest of the tool-bar. */
9689 if (!row->displays_text_p)
9690 {
9691 row->height = row->phys_height = it->last_visible_y - row->y;
9692 row->ascent = row->phys_ascent = 0;
9693 row->extra_line_spacing = 0;
9694 }
9695
9696 row->full_width_p = 1;
9697 row->continued_p = 0;
9698 row->truncated_on_left_p = 0;
9699 row->truncated_on_right_p = 0;
9700
9701 it->current_x = it->hpos = 0;
9702 it->current_y += row->height;
9703 ++it->vpos;
9704 ++it->glyph_row;
9705 }
9706
9707
9708 /* Max tool-bar height. */
9709
9710 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
9711 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
9712
9713 /* Value is the number of screen lines needed to make all tool-bar
9714 items of frame F visible. The number of actual rows needed is
9715 returned in *N_ROWS if non-NULL. */
9716
9717 static int
9718 tool_bar_lines_needed (f, n_rows)
9719 struct frame *f;
9720 int *n_rows;
9721 {
9722 struct window *w = XWINDOW (f->tool_bar_window);
9723 struct it it;
9724 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
9725 the desired matrix, so use (unused) mode-line row as temporary row to
9726 avoid destroying the first tool-bar row. */
9727 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
9728
9729 /* Initialize an iterator for iteration over
9730 F->desired_tool_bar_string in the tool-bar window of frame F. */
9731 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
9732 it.first_visible_x = 0;
9733 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9734 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9735
9736 while (!ITERATOR_AT_END_P (&it))
9737 {
9738 clear_glyph_row (temp_row);
9739 it.glyph_row = temp_row;
9740 display_tool_bar_line (&it, -1);
9741 }
9742 clear_glyph_row (temp_row);
9743
9744 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
9745 if (n_rows)
9746 *n_rows = it.vpos > 0 ? it.vpos : -1;
9747
9748 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
9749 }
9750
9751
9752 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
9753 0, 1, 0,
9754 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
9755 (frame)
9756 Lisp_Object frame;
9757 {
9758 struct frame *f;
9759 struct window *w;
9760 int nlines = 0;
9761
9762 if (NILP (frame))
9763 frame = selected_frame;
9764 else
9765 CHECK_FRAME (frame);
9766 f = XFRAME (frame);
9767
9768 if (WINDOWP (f->tool_bar_window)
9769 || (w = XWINDOW (f->tool_bar_window),
9770 WINDOW_TOTAL_LINES (w) > 0))
9771 {
9772 update_tool_bar (f, 1);
9773 if (f->n_tool_bar_items)
9774 {
9775 build_desired_tool_bar_string (f);
9776 nlines = tool_bar_lines_needed (f, NULL);
9777 }
9778 }
9779
9780 return make_number (nlines);
9781 }
9782
9783
9784 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
9785 height should be changed. */
9786
9787 static int
9788 redisplay_tool_bar (f)
9789 struct frame *f;
9790 {
9791 struct window *w;
9792 struct it it;
9793 struct glyph_row *row;
9794 int change_height_p = 0;
9795
9796 #ifdef USE_GTK
9797 if (FRAME_EXTERNAL_TOOL_BAR (f))
9798 update_frame_tool_bar (f);
9799 return 0;
9800 #endif
9801
9802 /* If frame hasn't a tool-bar window or if it is zero-height, don't
9803 do anything. This means you must start with tool-bar-lines
9804 non-zero to get the auto-sizing effect. Or in other words, you
9805 can turn off tool-bars by specifying tool-bar-lines zero. */
9806 if (!WINDOWP (f->tool_bar_window)
9807 || (w = XWINDOW (f->tool_bar_window),
9808 WINDOW_TOTAL_LINES (w) == 0))
9809 return 0;
9810
9811 /* Set up an iterator for the tool-bar window. */
9812 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
9813 it.first_visible_x = 0;
9814 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
9815 row = it.glyph_row;
9816
9817 /* Build a string that represents the contents of the tool-bar. */
9818 build_desired_tool_bar_string (f);
9819 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
9820
9821 if (f->n_tool_bar_rows == 0)
9822 {
9823 int nlines;
9824
9825 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
9826 nlines != WINDOW_TOTAL_LINES (w)))
9827 {
9828 extern Lisp_Object Qtool_bar_lines;
9829 Lisp_Object frame;
9830 int old_height = WINDOW_TOTAL_LINES (w);
9831
9832 XSETFRAME (frame, f);
9833 Fmodify_frame_parameters (frame,
9834 Fcons (Fcons (Qtool_bar_lines,
9835 make_number (nlines)),
9836 Qnil));
9837 if (WINDOW_TOTAL_LINES (w) != old_height)
9838 {
9839 clear_glyph_matrix (w->desired_matrix);
9840 fonts_changed_p = 1;
9841 return 1;
9842 }
9843 }
9844 }
9845
9846 /* Display as many lines as needed to display all tool-bar items. */
9847
9848 if (f->n_tool_bar_rows > 0)
9849 {
9850 int border, rows, height, extra;
9851
9852 if (INTEGERP (Vtool_bar_border))
9853 border = XINT (Vtool_bar_border);
9854 else if (EQ (Vtool_bar_border, Qinternal_border_width))
9855 border = FRAME_INTERNAL_BORDER_WIDTH (f);
9856 else if (EQ (Vtool_bar_border, Qborder_width))
9857 border = f->border_width;
9858 else
9859 border = 0;
9860 if (border < 0)
9861 border = 0;
9862
9863 rows = f->n_tool_bar_rows;
9864 height = max (1, (it.last_visible_y - border) / rows);
9865 extra = it.last_visible_y - border - height * rows;
9866
9867 while (it.current_y < it.last_visible_y)
9868 {
9869 int h = 0;
9870 if (extra > 0 && rows-- > 0)
9871 {
9872 h = (extra + rows - 1) / rows;
9873 extra -= h;
9874 }
9875 display_tool_bar_line (&it, height + h);
9876 }
9877 }
9878 else
9879 {
9880 while (it.current_y < it.last_visible_y)
9881 display_tool_bar_line (&it, 0);
9882 }
9883
9884 /* It doesn't make much sense to try scrolling in the tool-bar
9885 window, so don't do it. */
9886 w->desired_matrix->no_scrolling_p = 1;
9887 w->must_be_updated_p = 1;
9888
9889 if (auto_resize_tool_bars_p)
9890 {
9891 int nlines, nrows;
9892 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
9893
9894 /* If we couldn't display everything, change the tool-bar's
9895 height if there is room for more. */
9896 if (IT_STRING_CHARPOS (it) < it.end_charpos
9897 && it.current_y < max_tool_bar_height)
9898 change_height_p = 1;
9899
9900 row = it.glyph_row - 1;
9901
9902 /* If there are blank lines at the end, except for a partially
9903 visible blank line at the end that is smaller than
9904 FRAME_LINE_HEIGHT, change the tool-bar's height. */
9905 if (!row->displays_text_p
9906 && row->height >= FRAME_LINE_HEIGHT (f))
9907 change_height_p = 1;
9908
9909 /* If row displays tool-bar items, but is partially visible,
9910 change the tool-bar's height. */
9911 if (row->displays_text_p
9912 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
9913 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
9914 change_height_p = 1;
9915
9916 /* Resize windows as needed by changing the `tool-bar-lines'
9917 frame parameter. */
9918 if (change_height_p
9919 && (nlines = tool_bar_lines_needed (f, &nrows),
9920 nlines != WINDOW_TOTAL_LINES (w)))
9921 {
9922 extern Lisp_Object Qtool_bar_lines;
9923 Lisp_Object frame;
9924 int old_height = WINDOW_TOTAL_LINES (w);
9925
9926 XSETFRAME (frame, f);
9927 Fmodify_frame_parameters (frame,
9928 Fcons (Fcons (Qtool_bar_lines,
9929 make_number (nlines)),
9930 Qnil));
9931 if (WINDOW_TOTAL_LINES (w) != old_height)
9932 {
9933 clear_glyph_matrix (w->desired_matrix);
9934 f->n_tool_bar_rows = nrows;
9935 fonts_changed_p = 1;
9936 }
9937 }
9938 }
9939
9940 return change_height_p;
9941 }
9942
9943
9944 /* Get information about the tool-bar item which is displayed in GLYPH
9945 on frame F. Return in *PROP_IDX the index where tool-bar item
9946 properties start in F->tool_bar_items. Value is zero if
9947 GLYPH doesn't display a tool-bar item. */
9948
9949 static int
9950 tool_bar_item_info (f, glyph, prop_idx)
9951 struct frame *f;
9952 struct glyph *glyph;
9953 int *prop_idx;
9954 {
9955 Lisp_Object prop;
9956 int success_p;
9957 int charpos;
9958
9959 /* This function can be called asynchronously, which means we must
9960 exclude any possibility that Fget_text_property signals an
9961 error. */
9962 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
9963 charpos = max (0, charpos);
9964
9965 /* Get the text property `menu-item' at pos. The value of that
9966 property is the start index of this item's properties in
9967 F->tool_bar_items. */
9968 prop = Fget_text_property (make_number (charpos),
9969 Qmenu_item, f->current_tool_bar_string);
9970 if (INTEGERP (prop))
9971 {
9972 *prop_idx = XINT (prop);
9973 success_p = 1;
9974 }
9975 else
9976 success_p = 0;
9977
9978 return success_p;
9979 }
9980
9981 \f
9982 /* Get information about the tool-bar item at position X/Y on frame F.
9983 Return in *GLYPH a pointer to the glyph of the tool-bar item in
9984 the current matrix of the tool-bar window of F, or NULL if not
9985 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
9986 item in F->tool_bar_items. Value is
9987
9988 -1 if X/Y is not on a tool-bar item
9989 0 if X/Y is on the same item that was highlighted before.
9990 1 otherwise. */
9991
9992 static int
9993 get_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
9994 struct frame *f;
9995 int x, y;
9996 struct glyph **glyph;
9997 int *hpos, *vpos, *prop_idx;
9998 {
9999 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10000 struct window *w = XWINDOW (f->tool_bar_window);
10001 int area;
10002
10003 /* Find the glyph under X/Y. */
10004 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
10005 if (*glyph == NULL)
10006 return -1;
10007
10008 /* Get the start of this tool-bar item's properties in
10009 f->tool_bar_items. */
10010 if (!tool_bar_item_info (f, *glyph, prop_idx))
10011 return -1;
10012
10013 /* Is mouse on the highlighted item? */
10014 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
10015 && *vpos >= dpyinfo->mouse_face_beg_row
10016 && *vpos <= dpyinfo->mouse_face_end_row
10017 && (*vpos > dpyinfo->mouse_face_beg_row
10018 || *hpos >= dpyinfo->mouse_face_beg_col)
10019 && (*vpos < dpyinfo->mouse_face_end_row
10020 || *hpos < dpyinfo->mouse_face_end_col
10021 || dpyinfo->mouse_face_past_end))
10022 return 0;
10023
10024 return 1;
10025 }
10026
10027
10028 /* EXPORT:
10029 Handle mouse button event on the tool-bar of frame F, at
10030 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
10031 0 for button release. MODIFIERS is event modifiers for button
10032 release. */
10033
10034 void
10035 handle_tool_bar_click (f, x, y, down_p, modifiers)
10036 struct frame *f;
10037 int x, y, down_p;
10038 unsigned int modifiers;
10039 {
10040 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10041 struct window *w = XWINDOW (f->tool_bar_window);
10042 int hpos, vpos, prop_idx;
10043 struct glyph *glyph;
10044 Lisp_Object enabled_p;
10045
10046 /* If not on the highlighted tool-bar item, return. */
10047 frame_to_window_pixel_xy (w, &x, &y);
10048 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
10049 return;
10050
10051 /* If item is disabled, do nothing. */
10052 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10053 if (NILP (enabled_p))
10054 return;
10055
10056 if (down_p)
10057 {
10058 /* Show item in pressed state. */
10059 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
10060 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
10061 last_tool_bar_item = prop_idx;
10062 }
10063 else
10064 {
10065 Lisp_Object key, frame;
10066 struct input_event event;
10067 EVENT_INIT (event);
10068
10069 /* Show item in released state. */
10070 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
10071 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
10072
10073 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
10074
10075 XSETFRAME (frame, f);
10076 event.kind = TOOL_BAR_EVENT;
10077 event.frame_or_window = frame;
10078 event.arg = frame;
10079 kbd_buffer_store_event (&event);
10080
10081 event.kind = TOOL_BAR_EVENT;
10082 event.frame_or_window = frame;
10083 event.arg = key;
10084 event.modifiers = modifiers;
10085 kbd_buffer_store_event (&event);
10086 last_tool_bar_item = -1;
10087 }
10088 }
10089
10090
10091 /* Possibly highlight a tool-bar item on frame F when mouse moves to
10092 tool-bar window-relative coordinates X/Y. Called from
10093 note_mouse_highlight. */
10094
10095 static void
10096 note_tool_bar_highlight (f, x, y)
10097 struct frame *f;
10098 int x, y;
10099 {
10100 Lisp_Object window = f->tool_bar_window;
10101 struct window *w = XWINDOW (window);
10102 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10103 int hpos, vpos;
10104 struct glyph *glyph;
10105 struct glyph_row *row;
10106 int i;
10107 Lisp_Object enabled_p;
10108 int prop_idx;
10109 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
10110 int mouse_down_p, rc;
10111
10112 /* Function note_mouse_highlight is called with negative x(y
10113 values when mouse moves outside of the frame. */
10114 if (x <= 0 || y <= 0)
10115 {
10116 clear_mouse_face (dpyinfo);
10117 return;
10118 }
10119
10120 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
10121 if (rc < 0)
10122 {
10123 /* Not on tool-bar item. */
10124 clear_mouse_face (dpyinfo);
10125 return;
10126 }
10127 else if (rc == 0)
10128 /* On same tool-bar item as before. */
10129 goto set_help_echo;
10130
10131 clear_mouse_face (dpyinfo);
10132
10133 /* Mouse is down, but on different tool-bar item? */
10134 mouse_down_p = (dpyinfo->grabbed
10135 && f == last_mouse_frame
10136 && FRAME_LIVE_P (f));
10137 if (mouse_down_p
10138 && last_tool_bar_item != prop_idx)
10139 return;
10140
10141 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
10142 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
10143
10144 /* If tool-bar item is not enabled, don't highlight it. */
10145 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
10146 if (!NILP (enabled_p))
10147 {
10148 /* Compute the x-position of the glyph. In front and past the
10149 image is a space. We include this in the highlighted area. */
10150 row = MATRIX_ROW (w->current_matrix, vpos);
10151 for (i = x = 0; i < hpos; ++i)
10152 x += row->glyphs[TEXT_AREA][i].pixel_width;
10153
10154 /* Record this as the current active region. */
10155 dpyinfo->mouse_face_beg_col = hpos;
10156 dpyinfo->mouse_face_beg_row = vpos;
10157 dpyinfo->mouse_face_beg_x = x;
10158 dpyinfo->mouse_face_beg_y = row->y;
10159 dpyinfo->mouse_face_past_end = 0;
10160
10161 dpyinfo->mouse_face_end_col = hpos + 1;
10162 dpyinfo->mouse_face_end_row = vpos;
10163 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
10164 dpyinfo->mouse_face_end_y = row->y;
10165 dpyinfo->mouse_face_window = window;
10166 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
10167
10168 /* Display it as active. */
10169 show_mouse_face (dpyinfo, draw);
10170 dpyinfo->mouse_face_image_state = draw;
10171 }
10172
10173 set_help_echo:
10174
10175 /* Set help_echo_string to a help string to display for this tool-bar item.
10176 XTread_socket does the rest. */
10177 help_echo_object = help_echo_window = Qnil;
10178 help_echo_pos = -1;
10179 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
10180 if (NILP (help_echo_string))
10181 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
10182 }
10183
10184 #endif /* HAVE_WINDOW_SYSTEM */
10185
10186
10187 \f
10188 /************************************************************************
10189 Horizontal scrolling
10190 ************************************************************************/
10191
10192 static int hscroll_window_tree P_ ((Lisp_Object));
10193 static int hscroll_windows P_ ((Lisp_Object));
10194
10195 /* For all leaf windows in the window tree rooted at WINDOW, set their
10196 hscroll value so that PT is (i) visible in the window, and (ii) so
10197 that it is not within a certain margin at the window's left and
10198 right border. Value is non-zero if any window's hscroll has been
10199 changed. */
10200
10201 static int
10202 hscroll_window_tree (window)
10203 Lisp_Object window;
10204 {
10205 int hscrolled_p = 0;
10206 int hscroll_relative_p = FLOATP (Vhscroll_step);
10207 int hscroll_step_abs = 0;
10208 double hscroll_step_rel = 0;
10209
10210 if (hscroll_relative_p)
10211 {
10212 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
10213 if (hscroll_step_rel < 0)
10214 {
10215 hscroll_relative_p = 0;
10216 hscroll_step_abs = 0;
10217 }
10218 }
10219 else if (INTEGERP (Vhscroll_step))
10220 {
10221 hscroll_step_abs = XINT (Vhscroll_step);
10222 if (hscroll_step_abs < 0)
10223 hscroll_step_abs = 0;
10224 }
10225 else
10226 hscroll_step_abs = 0;
10227
10228 while (WINDOWP (window))
10229 {
10230 struct window *w = XWINDOW (window);
10231
10232 if (WINDOWP (w->hchild))
10233 hscrolled_p |= hscroll_window_tree (w->hchild);
10234 else if (WINDOWP (w->vchild))
10235 hscrolled_p |= hscroll_window_tree (w->vchild);
10236 else if (w->cursor.vpos >= 0)
10237 {
10238 int h_margin;
10239 int text_area_width;
10240 struct glyph_row *current_cursor_row
10241 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
10242 struct glyph_row *desired_cursor_row
10243 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
10244 struct glyph_row *cursor_row
10245 = (desired_cursor_row->enabled_p
10246 ? desired_cursor_row
10247 : current_cursor_row);
10248
10249 text_area_width = window_box_width (w, TEXT_AREA);
10250
10251 /* Scroll when cursor is inside this scroll margin. */
10252 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
10253
10254 if ((XFASTINT (w->hscroll)
10255 && w->cursor.x <= h_margin)
10256 || (cursor_row->enabled_p
10257 && cursor_row->truncated_on_right_p
10258 && (w->cursor.x >= text_area_width - h_margin)))
10259 {
10260 struct it it;
10261 int hscroll;
10262 struct buffer *saved_current_buffer;
10263 int pt;
10264 int wanted_x;
10265
10266 /* Find point in a display of infinite width. */
10267 saved_current_buffer = current_buffer;
10268 current_buffer = XBUFFER (w->buffer);
10269
10270 if (w == XWINDOW (selected_window))
10271 pt = BUF_PT (current_buffer);
10272 else
10273 {
10274 pt = marker_position (w->pointm);
10275 pt = max (BEGV, pt);
10276 pt = min (ZV, pt);
10277 }
10278
10279 /* Move iterator to pt starting at cursor_row->start in
10280 a line with infinite width. */
10281 init_to_row_start (&it, w, cursor_row);
10282 it.last_visible_x = INFINITY;
10283 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
10284 current_buffer = saved_current_buffer;
10285
10286 /* Position cursor in window. */
10287 if (!hscroll_relative_p && hscroll_step_abs == 0)
10288 hscroll = max (0, (it.current_x
10289 - (ITERATOR_AT_END_OF_LINE_P (&it)
10290 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
10291 : (text_area_width / 2))))
10292 / FRAME_COLUMN_WIDTH (it.f);
10293 else if (w->cursor.x >= text_area_width - h_margin)
10294 {
10295 if (hscroll_relative_p)
10296 wanted_x = text_area_width * (1 - hscroll_step_rel)
10297 - h_margin;
10298 else
10299 wanted_x = text_area_width
10300 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10301 - h_margin;
10302 hscroll
10303 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10304 }
10305 else
10306 {
10307 if (hscroll_relative_p)
10308 wanted_x = text_area_width * hscroll_step_rel
10309 + h_margin;
10310 else
10311 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
10312 + h_margin;
10313 hscroll
10314 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
10315 }
10316 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
10317
10318 /* Don't call Fset_window_hscroll if value hasn't
10319 changed because it will prevent redisplay
10320 optimizations. */
10321 if (XFASTINT (w->hscroll) != hscroll)
10322 {
10323 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
10324 w->hscroll = make_number (hscroll);
10325 hscrolled_p = 1;
10326 }
10327 }
10328 }
10329
10330 window = w->next;
10331 }
10332
10333 /* Value is non-zero if hscroll of any leaf window has been changed. */
10334 return hscrolled_p;
10335 }
10336
10337
10338 /* Set hscroll so that cursor is visible and not inside horizontal
10339 scroll margins for all windows in the tree rooted at WINDOW. See
10340 also hscroll_window_tree above. Value is non-zero if any window's
10341 hscroll has been changed. If it has, desired matrices on the frame
10342 of WINDOW are cleared. */
10343
10344 static int
10345 hscroll_windows (window)
10346 Lisp_Object window;
10347 {
10348 int hscrolled_p;
10349
10350 if (automatic_hscrolling_p)
10351 {
10352 hscrolled_p = hscroll_window_tree (window);
10353 if (hscrolled_p)
10354 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
10355 }
10356 else
10357 hscrolled_p = 0;
10358 return hscrolled_p;
10359 }
10360
10361
10362 \f
10363 /************************************************************************
10364 Redisplay
10365 ************************************************************************/
10366
10367 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
10368 to a non-zero value. This is sometimes handy to have in a debugger
10369 session. */
10370
10371 #if GLYPH_DEBUG
10372
10373 /* First and last unchanged row for try_window_id. */
10374
10375 int debug_first_unchanged_at_end_vpos;
10376 int debug_last_unchanged_at_beg_vpos;
10377
10378 /* Delta vpos and y. */
10379
10380 int debug_dvpos, debug_dy;
10381
10382 /* Delta in characters and bytes for try_window_id. */
10383
10384 int debug_delta, debug_delta_bytes;
10385
10386 /* Values of window_end_pos and window_end_vpos at the end of
10387 try_window_id. */
10388
10389 EMACS_INT debug_end_pos, debug_end_vpos;
10390
10391 /* Append a string to W->desired_matrix->method. FMT is a printf
10392 format string. A1...A9 are a supplement for a variable-length
10393 argument list. If trace_redisplay_p is non-zero also printf the
10394 resulting string to stderr. */
10395
10396 static void
10397 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
10398 struct window *w;
10399 char *fmt;
10400 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
10401 {
10402 char buffer[512];
10403 char *method = w->desired_matrix->method;
10404 int len = strlen (method);
10405 int size = sizeof w->desired_matrix->method;
10406 int remaining = size - len - 1;
10407
10408 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
10409 if (len && remaining)
10410 {
10411 method[len] = '|';
10412 --remaining, ++len;
10413 }
10414
10415 strncpy (method + len, buffer, remaining);
10416
10417 if (trace_redisplay_p)
10418 fprintf (stderr, "%p (%s): %s\n",
10419 w,
10420 ((BUFFERP (w->buffer)
10421 && STRINGP (XBUFFER (w->buffer)->name))
10422 ? (char *) SDATA (XBUFFER (w->buffer)->name)
10423 : "no buffer"),
10424 buffer);
10425 }
10426
10427 #endif /* GLYPH_DEBUG */
10428
10429
10430 /* Value is non-zero if all changes in window W, which displays
10431 current_buffer, are in the text between START and END. START is a
10432 buffer position, END is given as a distance from Z. Used in
10433 redisplay_internal for display optimization. */
10434
10435 static INLINE int
10436 text_outside_line_unchanged_p (w, start, end)
10437 struct window *w;
10438 int start, end;
10439 {
10440 int unchanged_p = 1;
10441
10442 /* If text or overlays have changed, see where. */
10443 if (XFASTINT (w->last_modified) < MODIFF
10444 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10445 {
10446 /* Gap in the line? */
10447 if (GPT < start || Z - GPT < end)
10448 unchanged_p = 0;
10449
10450 /* Changes start in front of the line, or end after it? */
10451 if (unchanged_p
10452 && (BEG_UNCHANGED < start - 1
10453 || END_UNCHANGED < end))
10454 unchanged_p = 0;
10455
10456 /* If selective display, can't optimize if changes start at the
10457 beginning of the line. */
10458 if (unchanged_p
10459 && INTEGERP (current_buffer->selective_display)
10460 && XINT (current_buffer->selective_display) > 0
10461 && (BEG_UNCHANGED < start || GPT <= start))
10462 unchanged_p = 0;
10463
10464 /* If there are overlays at the start or end of the line, these
10465 may have overlay strings with newlines in them. A change at
10466 START, for instance, may actually concern the display of such
10467 overlay strings as well, and they are displayed on different
10468 lines. So, quickly rule out this case. (For the future, it
10469 might be desirable to implement something more telling than
10470 just BEG/END_UNCHANGED.) */
10471 if (unchanged_p)
10472 {
10473 if (BEG + BEG_UNCHANGED == start
10474 && overlay_touches_p (start))
10475 unchanged_p = 0;
10476 if (END_UNCHANGED == end
10477 && overlay_touches_p (Z - end))
10478 unchanged_p = 0;
10479 }
10480 }
10481
10482 return unchanged_p;
10483 }
10484
10485
10486 /* Do a frame update, taking possible shortcuts into account. This is
10487 the main external entry point for redisplay.
10488
10489 If the last redisplay displayed an echo area message and that message
10490 is no longer requested, we clear the echo area or bring back the
10491 mini-buffer if that is in use. */
10492
10493 void
10494 redisplay ()
10495 {
10496 redisplay_internal (0);
10497 }
10498
10499
10500 static Lisp_Object
10501 overlay_arrow_string_or_property (var)
10502 Lisp_Object var;
10503 {
10504 Lisp_Object val;
10505
10506 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
10507 return val;
10508
10509 return Voverlay_arrow_string;
10510 }
10511
10512 /* Return 1 if there are any overlay-arrows in current_buffer. */
10513 static int
10514 overlay_arrow_in_current_buffer_p ()
10515 {
10516 Lisp_Object vlist;
10517
10518 for (vlist = Voverlay_arrow_variable_list;
10519 CONSP (vlist);
10520 vlist = XCDR (vlist))
10521 {
10522 Lisp_Object var = XCAR (vlist);
10523 Lisp_Object val;
10524
10525 if (!SYMBOLP (var))
10526 continue;
10527 val = find_symbol_value (var);
10528 if (MARKERP (val)
10529 && current_buffer == XMARKER (val)->buffer)
10530 return 1;
10531 }
10532 return 0;
10533 }
10534
10535
10536 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
10537 has changed. */
10538
10539 static int
10540 overlay_arrows_changed_p ()
10541 {
10542 Lisp_Object vlist;
10543
10544 for (vlist = Voverlay_arrow_variable_list;
10545 CONSP (vlist);
10546 vlist = XCDR (vlist))
10547 {
10548 Lisp_Object var = XCAR (vlist);
10549 Lisp_Object val, pstr;
10550
10551 if (!SYMBOLP (var))
10552 continue;
10553 val = find_symbol_value (var);
10554 if (!MARKERP (val))
10555 continue;
10556 if (! EQ (COERCE_MARKER (val),
10557 Fget (var, Qlast_arrow_position))
10558 || ! (pstr = overlay_arrow_string_or_property (var),
10559 EQ (pstr, Fget (var, Qlast_arrow_string))))
10560 return 1;
10561 }
10562 return 0;
10563 }
10564
10565 /* Mark overlay arrows to be updated on next redisplay. */
10566
10567 static void
10568 update_overlay_arrows (up_to_date)
10569 int up_to_date;
10570 {
10571 Lisp_Object vlist;
10572
10573 for (vlist = Voverlay_arrow_variable_list;
10574 CONSP (vlist);
10575 vlist = XCDR (vlist))
10576 {
10577 Lisp_Object var = XCAR (vlist);
10578
10579 if (!SYMBOLP (var))
10580 continue;
10581
10582 if (up_to_date > 0)
10583 {
10584 Lisp_Object val = find_symbol_value (var);
10585 Fput (var, Qlast_arrow_position,
10586 COERCE_MARKER (val));
10587 Fput (var, Qlast_arrow_string,
10588 overlay_arrow_string_or_property (var));
10589 }
10590 else if (up_to_date < 0
10591 || !NILP (Fget (var, Qlast_arrow_position)))
10592 {
10593 Fput (var, Qlast_arrow_position, Qt);
10594 Fput (var, Qlast_arrow_string, Qt);
10595 }
10596 }
10597 }
10598
10599
10600 /* Return overlay arrow string to display at row.
10601 Return integer (bitmap number) for arrow bitmap in left fringe.
10602 Return nil if no overlay arrow. */
10603
10604 static Lisp_Object
10605 overlay_arrow_at_row (it, row)
10606 struct it *it;
10607 struct glyph_row *row;
10608 {
10609 Lisp_Object vlist;
10610
10611 for (vlist = Voverlay_arrow_variable_list;
10612 CONSP (vlist);
10613 vlist = XCDR (vlist))
10614 {
10615 Lisp_Object var = XCAR (vlist);
10616 Lisp_Object val;
10617
10618 if (!SYMBOLP (var))
10619 continue;
10620
10621 val = find_symbol_value (var);
10622
10623 if (MARKERP (val)
10624 && current_buffer == XMARKER (val)->buffer
10625 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
10626 {
10627 if (FRAME_WINDOW_P (it->f)
10628 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
10629 {
10630 #ifdef HAVE_WINDOW_SYSTEM
10631 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
10632 {
10633 int fringe_bitmap;
10634 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
10635 return make_number (fringe_bitmap);
10636 }
10637 #endif
10638 return make_number (-1); /* Use default arrow bitmap */
10639 }
10640 return overlay_arrow_string_or_property (var);
10641 }
10642 }
10643
10644 return Qnil;
10645 }
10646
10647 /* Return 1 if point moved out of or into a composition. Otherwise
10648 return 0. PREV_BUF and PREV_PT are the last point buffer and
10649 position. BUF and PT are the current point buffer and position. */
10650
10651 int
10652 check_point_in_composition (prev_buf, prev_pt, buf, pt)
10653 struct buffer *prev_buf, *buf;
10654 int prev_pt, pt;
10655 {
10656 int start, end;
10657 Lisp_Object prop;
10658 Lisp_Object buffer;
10659
10660 XSETBUFFER (buffer, buf);
10661 /* Check a composition at the last point if point moved within the
10662 same buffer. */
10663 if (prev_buf == buf)
10664 {
10665 if (prev_pt == pt)
10666 /* Point didn't move. */
10667 return 0;
10668
10669 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
10670 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
10671 && COMPOSITION_VALID_P (start, end, prop)
10672 && start < prev_pt && end > prev_pt)
10673 /* The last point was within the composition. Return 1 iff
10674 point moved out of the composition. */
10675 return (pt <= start || pt >= end);
10676 }
10677
10678 /* Check a composition at the current point. */
10679 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
10680 && find_composition (pt, -1, &start, &end, &prop, buffer)
10681 && COMPOSITION_VALID_P (start, end, prop)
10682 && start < pt && end > pt);
10683 }
10684
10685
10686 /* Reconsider the setting of B->clip_changed which is displayed
10687 in window W. */
10688
10689 static INLINE void
10690 reconsider_clip_changes (w, b)
10691 struct window *w;
10692 struct buffer *b;
10693 {
10694 if (b->clip_changed
10695 && !NILP (w->window_end_valid)
10696 && w->current_matrix->buffer == b
10697 && w->current_matrix->zv == BUF_ZV (b)
10698 && w->current_matrix->begv == BUF_BEGV (b))
10699 b->clip_changed = 0;
10700
10701 /* If display wasn't paused, and W is not a tool bar window, see if
10702 point has been moved into or out of a composition. In that case,
10703 we set b->clip_changed to 1 to force updating the screen. If
10704 b->clip_changed has already been set to 1, we can skip this
10705 check. */
10706 if (!b->clip_changed
10707 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
10708 {
10709 int pt;
10710
10711 if (w == XWINDOW (selected_window))
10712 pt = BUF_PT (current_buffer);
10713 else
10714 pt = marker_position (w->pointm);
10715
10716 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
10717 || pt != XINT (w->last_point))
10718 && check_point_in_composition (w->current_matrix->buffer,
10719 XINT (w->last_point),
10720 XBUFFER (w->buffer), pt))
10721 b->clip_changed = 1;
10722 }
10723 }
10724 \f
10725
10726 /* Select FRAME to forward the values of frame-local variables into C
10727 variables so that the redisplay routines can access those values
10728 directly. */
10729
10730 static void
10731 select_frame_for_redisplay (frame)
10732 Lisp_Object frame;
10733 {
10734 Lisp_Object tail, sym, val;
10735 Lisp_Object old = selected_frame;
10736
10737 selected_frame = frame;
10738
10739 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
10740 if (CONSP (XCAR (tail))
10741 && (sym = XCAR (XCAR (tail)),
10742 SYMBOLP (sym))
10743 && (sym = indirect_variable (sym),
10744 val = SYMBOL_VALUE (sym),
10745 (BUFFER_LOCAL_VALUEP (val)
10746 || SOME_BUFFER_LOCAL_VALUEP (val)))
10747 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10748 /* Use find_symbol_value rather than Fsymbol_value
10749 to avoid an error if it is void. */
10750 find_symbol_value (sym);
10751
10752 for (tail = XFRAME (old)->param_alist; CONSP (tail); tail = XCDR (tail))
10753 if (CONSP (XCAR (tail))
10754 && (sym = XCAR (XCAR (tail)),
10755 SYMBOLP (sym))
10756 && (sym = indirect_variable (sym),
10757 val = SYMBOL_VALUE (sym),
10758 (BUFFER_LOCAL_VALUEP (val)
10759 || SOME_BUFFER_LOCAL_VALUEP (val)))
10760 && XBUFFER_LOCAL_VALUE (val)->check_frame)
10761 find_symbol_value (sym);
10762 }
10763
10764
10765 #define STOP_POLLING \
10766 do { if (! polling_stopped_here) stop_polling (); \
10767 polling_stopped_here = 1; } while (0)
10768
10769 #define RESUME_POLLING \
10770 do { if (polling_stopped_here) start_polling (); \
10771 polling_stopped_here = 0; } while (0)
10772
10773
10774 /* If PRESERVE_ECHO_AREA is nonzero, it means this redisplay is not in
10775 response to any user action; therefore, we should preserve the echo
10776 area. (Actually, our caller does that job.) Perhaps in the future
10777 avoid recentering windows if it is not necessary; currently that
10778 causes some problems. */
10779
10780 static void
10781 redisplay_internal (preserve_echo_area)
10782 int preserve_echo_area;
10783 {
10784 struct window *w = XWINDOW (selected_window);
10785 struct frame *f = XFRAME (w->frame);
10786 int pause;
10787 int must_finish = 0;
10788 struct text_pos tlbufpos, tlendpos;
10789 int number_of_visible_frames;
10790 int count;
10791 struct frame *sf = SELECTED_FRAME ();
10792 int polling_stopped_here = 0;
10793
10794 /* Non-zero means redisplay has to consider all windows on all
10795 frames. Zero means, only selected_window is considered. */
10796 int consider_all_windows_p;
10797
10798 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
10799
10800 /* No redisplay if running in batch mode or frame is not yet fully
10801 initialized, or redisplay is explicitly turned off by setting
10802 Vinhibit_redisplay. */
10803 if (noninteractive
10804 || !NILP (Vinhibit_redisplay)
10805 || !f->glyphs_initialized_p)
10806 return;
10807
10808 /* The flag redisplay_performed_directly_p is set by
10809 direct_output_for_insert when it already did the whole screen
10810 update necessary. */
10811 if (redisplay_performed_directly_p)
10812 {
10813 redisplay_performed_directly_p = 0;
10814 if (!hscroll_windows (selected_window))
10815 return;
10816 }
10817
10818 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10819 if (popup_activated ())
10820 return;
10821 #endif
10822
10823 /* I don't think this happens but let's be paranoid. */
10824 if (redisplaying_p)
10825 return;
10826
10827 /* Record a function that resets redisplaying_p to its old value
10828 when we leave this function. */
10829 count = SPECPDL_INDEX ();
10830 record_unwind_protect (unwind_redisplay,
10831 Fcons (make_number (redisplaying_p), selected_frame));
10832 ++redisplaying_p;
10833 specbind (Qinhibit_free_realized_faces, Qnil);
10834
10835 {
10836 Lisp_Object tail, frame;
10837
10838 FOR_EACH_FRAME (tail, frame)
10839 {
10840 struct frame *f = XFRAME (frame);
10841 f->already_hscrolled_p = 0;
10842 }
10843 }
10844
10845 retry:
10846 pause = 0;
10847 reconsider_clip_changes (w, current_buffer);
10848 last_escape_glyph_frame = NULL;
10849 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
10850
10851 /* If new fonts have been loaded that make a glyph matrix adjustment
10852 necessary, do it. */
10853 if (fonts_changed_p)
10854 {
10855 adjust_glyphs (NULL);
10856 ++windows_or_buffers_changed;
10857 fonts_changed_p = 0;
10858 }
10859
10860 /* If face_change_count is non-zero, init_iterator will free all
10861 realized faces, which includes the faces referenced from current
10862 matrices. So, we can't reuse current matrices in this case. */
10863 if (face_change_count)
10864 ++windows_or_buffers_changed;
10865
10866 if (! FRAME_WINDOW_P (sf)
10867 && previous_terminal_frame != sf)
10868 {
10869 /* Since frames on an ASCII terminal share the same display
10870 area, displaying a different frame means redisplay the whole
10871 thing. */
10872 windows_or_buffers_changed++;
10873 SET_FRAME_GARBAGED (sf);
10874 XSETFRAME (Vterminal_frame, sf);
10875 }
10876 previous_terminal_frame = sf;
10877
10878 /* Set the visible flags for all frames. Do this before checking
10879 for resized or garbaged frames; they want to know if their frames
10880 are visible. See the comment in frame.h for
10881 FRAME_SAMPLE_VISIBILITY. */
10882 {
10883 Lisp_Object tail, frame;
10884
10885 number_of_visible_frames = 0;
10886
10887 FOR_EACH_FRAME (tail, frame)
10888 {
10889 struct frame *f = XFRAME (frame);
10890
10891 FRAME_SAMPLE_VISIBILITY (f);
10892 if (FRAME_VISIBLE_P (f))
10893 ++number_of_visible_frames;
10894 clear_desired_matrices (f);
10895 }
10896 }
10897
10898 /* Notice any pending interrupt request to change frame size. */
10899 do_pending_window_change (1);
10900
10901 /* Clear frames marked as garbaged. */
10902 if (frame_garbaged)
10903 clear_garbaged_frames ();
10904
10905 /* Build menubar and tool-bar items. */
10906 if (NILP (Vmemory_full))
10907 prepare_menu_bars ();
10908
10909 if (windows_or_buffers_changed)
10910 update_mode_lines++;
10911
10912 /* Detect case that we need to write or remove a star in the mode line. */
10913 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
10914 {
10915 w->update_mode_line = Qt;
10916 if (buffer_shared > 1)
10917 update_mode_lines++;
10918 }
10919
10920 /* If %c is in the mode line, update it if needed. */
10921 if (!NILP (w->column_number_displayed)
10922 /* This alternative quickly identifies a common case
10923 where no change is needed. */
10924 && !(PT == XFASTINT (w->last_point)
10925 && XFASTINT (w->last_modified) >= MODIFF
10926 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
10927 && (XFASTINT (w->column_number_displayed)
10928 != (int) current_column ())) /* iftc */
10929 w->update_mode_line = Qt;
10930
10931 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
10932
10933 /* The variable buffer_shared is set in redisplay_window and
10934 indicates that we redisplay a buffer in different windows. See
10935 there. */
10936 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
10937 || cursor_type_changed);
10938
10939 /* If specs for an arrow have changed, do thorough redisplay
10940 to ensure we remove any arrow that should no longer exist. */
10941 if (overlay_arrows_changed_p ())
10942 consider_all_windows_p = windows_or_buffers_changed = 1;
10943
10944 /* Normally the message* functions will have already displayed and
10945 updated the echo area, but the frame may have been trashed, or
10946 the update may have been preempted, so display the echo area
10947 again here. Checking message_cleared_p captures the case that
10948 the echo area should be cleared. */
10949 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
10950 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
10951 || (message_cleared_p
10952 && minibuf_level == 0
10953 /* If the mini-window is currently selected, this means the
10954 echo-area doesn't show through. */
10955 && !MINI_WINDOW_P (XWINDOW (selected_window))))
10956 {
10957 int window_height_changed_p = echo_area_display (0);
10958 must_finish = 1;
10959
10960 /* If we don't display the current message, don't clear the
10961 message_cleared_p flag, because, if we did, we wouldn't clear
10962 the echo area in the next redisplay which doesn't preserve
10963 the echo area. */
10964 if (!display_last_displayed_message_p)
10965 message_cleared_p = 0;
10966
10967 if (fonts_changed_p)
10968 goto retry;
10969 else if (window_height_changed_p)
10970 {
10971 consider_all_windows_p = 1;
10972 ++update_mode_lines;
10973 ++windows_or_buffers_changed;
10974
10975 /* If window configuration was changed, frames may have been
10976 marked garbaged. Clear them or we will experience
10977 surprises wrt scrolling. */
10978 if (frame_garbaged)
10979 clear_garbaged_frames ();
10980 }
10981 }
10982 else if (EQ (selected_window, minibuf_window)
10983 && (current_buffer->clip_changed
10984 || XFASTINT (w->last_modified) < MODIFF
10985 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
10986 && resize_mini_window (w, 0))
10987 {
10988 /* Resized active mini-window to fit the size of what it is
10989 showing if its contents might have changed. */
10990 must_finish = 1;
10991 consider_all_windows_p = 1;
10992 ++windows_or_buffers_changed;
10993 ++update_mode_lines;
10994
10995 /* If window configuration was changed, frames may have been
10996 marked garbaged. Clear them or we will experience
10997 surprises wrt scrolling. */
10998 if (frame_garbaged)
10999 clear_garbaged_frames ();
11000 }
11001
11002
11003 /* If showing the region, and mark has changed, we must redisplay
11004 the whole window. The assignment to this_line_start_pos prevents
11005 the optimization directly below this if-statement. */
11006 if (((!NILP (Vtransient_mark_mode)
11007 && !NILP (XBUFFER (w->buffer)->mark_active))
11008 != !NILP (w->region_showing))
11009 || (!NILP (w->region_showing)
11010 && !EQ (w->region_showing,
11011 Fmarker_position (XBUFFER (w->buffer)->mark))))
11012 CHARPOS (this_line_start_pos) = 0;
11013
11014 /* Optimize the case that only the line containing the cursor in the
11015 selected window has changed. Variables starting with this_ are
11016 set in display_line and record information about the line
11017 containing the cursor. */
11018 tlbufpos = this_line_start_pos;
11019 tlendpos = this_line_end_pos;
11020 if (!consider_all_windows_p
11021 && CHARPOS (tlbufpos) > 0
11022 && NILP (w->update_mode_line)
11023 && !current_buffer->clip_changed
11024 && !current_buffer->prevent_redisplay_optimizations_p
11025 && FRAME_VISIBLE_P (XFRAME (w->frame))
11026 && !FRAME_OBSCURED_P (XFRAME (w->frame))
11027 /* Make sure recorded data applies to current buffer, etc. */
11028 && this_line_buffer == current_buffer
11029 && current_buffer == XBUFFER (w->buffer)
11030 && NILP (w->force_start)
11031 && NILP (w->optional_new_start)
11032 /* Point must be on the line that we have info recorded about. */
11033 && PT >= CHARPOS (tlbufpos)
11034 && PT <= Z - CHARPOS (tlendpos)
11035 /* All text outside that line, including its final newline,
11036 must be unchanged */
11037 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
11038 CHARPOS (tlendpos)))
11039 {
11040 if (CHARPOS (tlbufpos) > BEGV
11041 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
11042 && (CHARPOS (tlbufpos) == ZV
11043 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
11044 /* Former continuation line has disappeared by becoming empty */
11045 goto cancel;
11046 else if (XFASTINT (w->last_modified) < MODIFF
11047 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
11048 || MINI_WINDOW_P (w))
11049 {
11050 /* We have to handle the case of continuation around a
11051 wide-column character (See the comment in indent.c around
11052 line 885).
11053
11054 For instance, in the following case:
11055
11056 -------- Insert --------
11057 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
11058 J_I_ ==> J_I_ `^^' are cursors.
11059 ^^ ^^
11060 -------- --------
11061
11062 As we have to redraw the line above, we should goto cancel. */
11063
11064 struct it it;
11065 int line_height_before = this_line_pixel_height;
11066
11067 /* Note that start_display will handle the case that the
11068 line starting at tlbufpos is a continuation lines. */
11069 start_display (&it, w, tlbufpos);
11070
11071 /* Implementation note: It this still necessary? */
11072 if (it.current_x != this_line_start_x)
11073 goto cancel;
11074
11075 TRACE ((stderr, "trying display optimization 1\n"));
11076 w->cursor.vpos = -1;
11077 overlay_arrow_seen = 0;
11078 it.vpos = this_line_vpos;
11079 it.current_y = this_line_y;
11080 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
11081 display_line (&it);
11082
11083 /* If line contains point, is not continued,
11084 and ends at same distance from eob as before, we win */
11085 if (w->cursor.vpos >= 0
11086 /* Line is not continued, otherwise this_line_start_pos
11087 would have been set to 0 in display_line. */
11088 && CHARPOS (this_line_start_pos)
11089 /* Line ends as before. */
11090 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
11091 /* Line has same height as before. Otherwise other lines
11092 would have to be shifted up or down. */
11093 && this_line_pixel_height == line_height_before)
11094 {
11095 /* If this is not the window's last line, we must adjust
11096 the charstarts of the lines below. */
11097 if (it.current_y < it.last_visible_y)
11098 {
11099 struct glyph_row *row
11100 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
11101 int delta, delta_bytes;
11102
11103 if (Z - CHARPOS (tlendpos) == ZV)
11104 {
11105 /* This line ends at end of (accessible part of)
11106 buffer. There is no newline to count. */
11107 delta = (Z
11108 - CHARPOS (tlendpos)
11109 - MATRIX_ROW_START_CHARPOS (row));
11110 delta_bytes = (Z_BYTE
11111 - BYTEPOS (tlendpos)
11112 - MATRIX_ROW_START_BYTEPOS (row));
11113 }
11114 else
11115 {
11116 /* This line ends in a newline. Must take
11117 account of the newline and the rest of the
11118 text that follows. */
11119 delta = (Z
11120 - CHARPOS (tlendpos)
11121 - MATRIX_ROW_START_CHARPOS (row));
11122 delta_bytes = (Z_BYTE
11123 - BYTEPOS (tlendpos)
11124 - MATRIX_ROW_START_BYTEPOS (row));
11125 }
11126
11127 increment_matrix_positions (w->current_matrix,
11128 this_line_vpos + 1,
11129 w->current_matrix->nrows,
11130 delta, delta_bytes);
11131 }
11132
11133 /* If this row displays text now but previously didn't,
11134 or vice versa, w->window_end_vpos may have to be
11135 adjusted. */
11136 if ((it.glyph_row - 1)->displays_text_p)
11137 {
11138 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
11139 XSETINT (w->window_end_vpos, this_line_vpos);
11140 }
11141 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
11142 && this_line_vpos > 0)
11143 XSETINT (w->window_end_vpos, this_line_vpos - 1);
11144 w->window_end_valid = Qnil;
11145
11146 /* Update hint: No need to try to scroll in update_window. */
11147 w->desired_matrix->no_scrolling_p = 1;
11148
11149 #if GLYPH_DEBUG
11150 *w->desired_matrix->method = 0;
11151 debug_method_add (w, "optimization 1");
11152 #endif
11153 #ifdef HAVE_WINDOW_SYSTEM
11154 update_window_fringes (w, 0);
11155 #endif
11156 goto update;
11157 }
11158 else
11159 goto cancel;
11160 }
11161 else if (/* Cursor position hasn't changed. */
11162 PT == XFASTINT (w->last_point)
11163 /* Make sure the cursor was last displayed
11164 in this window. Otherwise we have to reposition it. */
11165 && 0 <= w->cursor.vpos
11166 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
11167 {
11168 if (!must_finish)
11169 {
11170 do_pending_window_change (1);
11171
11172 /* We used to always goto end_of_redisplay here, but this
11173 isn't enough if we have a blinking cursor. */
11174 if (w->cursor_off_p == w->last_cursor_off_p)
11175 goto end_of_redisplay;
11176 }
11177 goto update;
11178 }
11179 /* If highlighting the region, or if the cursor is in the echo area,
11180 then we can't just move the cursor. */
11181 else if (! (!NILP (Vtransient_mark_mode)
11182 && !NILP (current_buffer->mark_active))
11183 && (EQ (selected_window, current_buffer->last_selected_window)
11184 || highlight_nonselected_windows)
11185 && NILP (w->region_showing)
11186 && NILP (Vshow_trailing_whitespace)
11187 && !cursor_in_echo_area)
11188 {
11189 struct it it;
11190 struct glyph_row *row;
11191
11192 /* Skip from tlbufpos to PT and see where it is. Note that
11193 PT may be in invisible text. If so, we will end at the
11194 next visible position. */
11195 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
11196 NULL, DEFAULT_FACE_ID);
11197 it.current_x = this_line_start_x;
11198 it.current_y = this_line_y;
11199 it.vpos = this_line_vpos;
11200
11201 /* The call to move_it_to stops in front of PT, but
11202 moves over before-strings. */
11203 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
11204
11205 if (it.vpos == this_line_vpos
11206 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
11207 row->enabled_p))
11208 {
11209 xassert (this_line_vpos == it.vpos);
11210 xassert (this_line_y == it.current_y);
11211 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
11212 #if GLYPH_DEBUG
11213 *w->desired_matrix->method = 0;
11214 debug_method_add (w, "optimization 3");
11215 #endif
11216 goto update;
11217 }
11218 else
11219 goto cancel;
11220 }
11221
11222 cancel:
11223 /* Text changed drastically or point moved off of line. */
11224 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
11225 }
11226
11227 CHARPOS (this_line_start_pos) = 0;
11228 consider_all_windows_p |= buffer_shared > 1;
11229 ++clear_face_cache_count;
11230 #ifdef HAVE_WINDOW_SYSTEM
11231 ++clear_image_cache_count;
11232 #endif
11233
11234 /* Build desired matrices, and update the display. If
11235 consider_all_windows_p is non-zero, do it for all windows on all
11236 frames. Otherwise do it for selected_window, only. */
11237
11238 if (consider_all_windows_p)
11239 {
11240 Lisp_Object tail, frame;
11241
11242 FOR_EACH_FRAME (tail, frame)
11243 XFRAME (frame)->updated_p = 0;
11244
11245 /* Recompute # windows showing selected buffer. This will be
11246 incremented each time such a window is displayed. */
11247 buffer_shared = 0;
11248
11249 FOR_EACH_FRAME (tail, frame)
11250 {
11251 struct frame *f = XFRAME (frame);
11252
11253 if (FRAME_WINDOW_P (f) || f == sf)
11254 {
11255 if (! EQ (frame, selected_frame))
11256 /* Select the frame, for the sake of frame-local
11257 variables. */
11258 select_frame_for_redisplay (frame);
11259
11260 /* Mark all the scroll bars to be removed; we'll redeem
11261 the ones we want when we redisplay their windows. */
11262 if (condemn_scroll_bars_hook)
11263 condemn_scroll_bars_hook (f);
11264
11265 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11266 redisplay_windows (FRAME_ROOT_WINDOW (f));
11267
11268 /* Any scroll bars which redisplay_windows should have
11269 nuked should now go away. */
11270 if (judge_scroll_bars_hook)
11271 judge_scroll_bars_hook (f);
11272
11273 /* If fonts changed, display again. */
11274 /* ??? rms: I suspect it is a mistake to jump all the way
11275 back to retry here. It should just retry this frame. */
11276 if (fonts_changed_p)
11277 goto retry;
11278
11279 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
11280 {
11281 /* See if we have to hscroll. */
11282 if (!f->already_hscrolled_p)
11283 {
11284 f->already_hscrolled_p = 1;
11285 if (hscroll_windows (f->root_window))
11286 goto retry;
11287 }
11288
11289 /* Prevent various kinds of signals during display
11290 update. stdio is not robust about handling
11291 signals, which can cause an apparent I/O
11292 error. */
11293 if (interrupt_input)
11294 unrequest_sigio ();
11295 STOP_POLLING;
11296
11297 /* Update the display. */
11298 set_window_update_flags (XWINDOW (f->root_window), 1);
11299 pause |= update_frame (f, 0, 0);
11300 #if 0 /* Exiting the loop can leave the wrong value for buffer_shared. */
11301 if (pause)
11302 break;
11303 #endif
11304
11305 f->updated_p = 1;
11306 }
11307 }
11308 }
11309
11310 if (!pause)
11311 {
11312 /* Do the mark_window_display_accurate after all windows have
11313 been redisplayed because this call resets flags in buffers
11314 which are needed for proper redisplay. */
11315 FOR_EACH_FRAME (tail, frame)
11316 {
11317 struct frame *f = XFRAME (frame);
11318 if (f->updated_p)
11319 {
11320 mark_window_display_accurate (f->root_window, 1);
11321 if (frame_up_to_date_hook)
11322 frame_up_to_date_hook (f);
11323 }
11324 }
11325 }
11326 }
11327 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11328 {
11329 Lisp_Object mini_window;
11330 struct frame *mini_frame;
11331
11332 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
11333 /* Use list_of_error, not Qerror, so that
11334 we catch only errors and don't run the debugger. */
11335 internal_condition_case_1 (redisplay_window_1, selected_window,
11336 list_of_error,
11337 redisplay_window_error);
11338
11339 /* Compare desired and current matrices, perform output. */
11340
11341 update:
11342 /* If fonts changed, display again. */
11343 if (fonts_changed_p)
11344 goto retry;
11345
11346 /* Prevent various kinds of signals during display update.
11347 stdio is not robust about handling signals,
11348 which can cause an apparent I/O error. */
11349 if (interrupt_input)
11350 unrequest_sigio ();
11351 STOP_POLLING;
11352
11353 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
11354 {
11355 if (hscroll_windows (selected_window))
11356 goto retry;
11357
11358 XWINDOW (selected_window)->must_be_updated_p = 1;
11359 pause = update_frame (sf, 0, 0);
11360 }
11361
11362 /* We may have called echo_area_display at the top of this
11363 function. If the echo area is on another frame, that may
11364 have put text on a frame other than the selected one, so the
11365 above call to update_frame would not have caught it. Catch
11366 it here. */
11367 mini_window = FRAME_MINIBUF_WINDOW (sf);
11368 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
11369
11370 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
11371 {
11372 XWINDOW (mini_window)->must_be_updated_p = 1;
11373 pause |= update_frame (mini_frame, 0, 0);
11374 if (!pause && hscroll_windows (mini_window))
11375 goto retry;
11376 }
11377 }
11378
11379 /* If display was paused because of pending input, make sure we do a
11380 thorough update the next time. */
11381 if (pause)
11382 {
11383 /* Prevent the optimization at the beginning of
11384 redisplay_internal that tries a single-line update of the
11385 line containing the cursor in the selected window. */
11386 CHARPOS (this_line_start_pos) = 0;
11387
11388 /* Let the overlay arrow be updated the next time. */
11389 update_overlay_arrows (0);
11390
11391 /* If we pause after scrolling, some rows in the current
11392 matrices of some windows are not valid. */
11393 if (!WINDOW_FULL_WIDTH_P (w)
11394 && !FRAME_WINDOW_P (XFRAME (w->frame)))
11395 update_mode_lines = 1;
11396 }
11397 else
11398 {
11399 if (!consider_all_windows_p)
11400 {
11401 /* This has already been done above if
11402 consider_all_windows_p is set. */
11403 mark_window_display_accurate_1 (w, 1);
11404
11405 /* Say overlay arrows are up to date. */
11406 update_overlay_arrows (1);
11407
11408 if (frame_up_to_date_hook != 0)
11409 frame_up_to_date_hook (sf);
11410 }
11411
11412 update_mode_lines = 0;
11413 windows_or_buffers_changed = 0;
11414 cursor_type_changed = 0;
11415 }
11416
11417 /* Start SIGIO interrupts coming again. Having them off during the
11418 code above makes it less likely one will discard output, but not
11419 impossible, since there might be stuff in the system buffer here.
11420 But it is much hairier to try to do anything about that. */
11421 if (interrupt_input)
11422 request_sigio ();
11423 RESUME_POLLING;
11424
11425 /* If a frame has become visible which was not before, redisplay
11426 again, so that we display it. Expose events for such a frame
11427 (which it gets when becoming visible) don't call the parts of
11428 redisplay constructing glyphs, so simply exposing a frame won't
11429 display anything in this case. So, we have to display these
11430 frames here explicitly. */
11431 if (!pause)
11432 {
11433 Lisp_Object tail, frame;
11434 int new_count = 0;
11435
11436 FOR_EACH_FRAME (tail, frame)
11437 {
11438 int this_is_visible = 0;
11439
11440 if (XFRAME (frame)->visible)
11441 this_is_visible = 1;
11442 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
11443 if (XFRAME (frame)->visible)
11444 this_is_visible = 1;
11445
11446 if (this_is_visible)
11447 new_count++;
11448 }
11449
11450 if (new_count != number_of_visible_frames)
11451 windows_or_buffers_changed++;
11452 }
11453
11454 /* Change frame size now if a change is pending. */
11455 do_pending_window_change (1);
11456
11457 /* If we just did a pending size change, or have additional
11458 visible frames, redisplay again. */
11459 if (windows_or_buffers_changed && !pause)
11460 goto retry;
11461
11462 /* Clear the face cache eventually. */
11463 if (consider_all_windows_p)
11464 {
11465 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
11466 {
11467 clear_face_cache (0);
11468 clear_face_cache_count = 0;
11469 }
11470 #ifdef HAVE_WINDOW_SYSTEM
11471 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
11472 {
11473 Lisp_Object tail, frame;
11474 FOR_EACH_FRAME (tail, frame)
11475 {
11476 struct frame *f = XFRAME (frame);
11477 if (FRAME_WINDOW_P (f))
11478 clear_image_cache (f, 0);
11479 }
11480 clear_image_cache_count = 0;
11481 }
11482 #endif /* HAVE_WINDOW_SYSTEM */
11483 }
11484
11485 end_of_redisplay:
11486 unbind_to (count, Qnil);
11487 RESUME_POLLING;
11488 }
11489
11490
11491 /* Redisplay, but leave alone any recent echo area message unless
11492 another message has been requested in its place.
11493
11494 This is useful in situations where you need to redisplay but no
11495 user action has occurred, making it inappropriate for the message
11496 area to be cleared. See tracking_off and
11497 wait_reading_process_output for examples of these situations.
11498
11499 FROM_WHERE is an integer saying from where this function was
11500 called. This is useful for debugging. */
11501
11502 void
11503 redisplay_preserve_echo_area (from_where)
11504 int from_where;
11505 {
11506 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
11507
11508 if (!NILP (echo_area_buffer[1]))
11509 {
11510 /* We have a previously displayed message, but no current
11511 message. Redisplay the previous message. */
11512 display_last_displayed_message_p = 1;
11513 redisplay_internal (1);
11514 display_last_displayed_message_p = 0;
11515 }
11516 else
11517 redisplay_internal (1);
11518
11519 if (rif != NULL && rif->flush_display_optional)
11520 rif->flush_display_optional (NULL);
11521 }
11522
11523
11524 /* Function registered with record_unwind_protect in
11525 redisplay_internal. Reset redisplaying_p to the value it had
11526 before redisplay_internal was called, and clear
11527 prevent_freeing_realized_faces_p. It also selects the previously
11528 selected frame. */
11529
11530 static Lisp_Object
11531 unwind_redisplay (val)
11532 Lisp_Object val;
11533 {
11534 Lisp_Object old_redisplaying_p, old_frame;
11535
11536 old_redisplaying_p = XCAR (val);
11537 redisplaying_p = XFASTINT (old_redisplaying_p);
11538 old_frame = XCDR (val);
11539 if (! EQ (old_frame, selected_frame))
11540 select_frame_for_redisplay (old_frame);
11541 return Qnil;
11542 }
11543
11544
11545 /* Mark the display of window W as accurate or inaccurate. If
11546 ACCURATE_P is non-zero mark display of W as accurate. If
11547 ACCURATE_P is zero, arrange for W to be redisplayed the next time
11548 redisplay_internal is called. */
11549
11550 static void
11551 mark_window_display_accurate_1 (w, accurate_p)
11552 struct window *w;
11553 int accurate_p;
11554 {
11555 if (BUFFERP (w->buffer))
11556 {
11557 struct buffer *b = XBUFFER (w->buffer);
11558
11559 w->last_modified
11560 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
11561 w->last_overlay_modified
11562 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
11563 w->last_had_star
11564 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
11565
11566 if (accurate_p)
11567 {
11568 b->clip_changed = 0;
11569 b->prevent_redisplay_optimizations_p = 0;
11570
11571 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
11572 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
11573 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
11574 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
11575
11576 w->current_matrix->buffer = b;
11577 w->current_matrix->begv = BUF_BEGV (b);
11578 w->current_matrix->zv = BUF_ZV (b);
11579
11580 w->last_cursor = w->cursor;
11581 w->last_cursor_off_p = w->cursor_off_p;
11582
11583 if (w == XWINDOW (selected_window))
11584 w->last_point = make_number (BUF_PT (b));
11585 else
11586 w->last_point = make_number (XMARKER (w->pointm)->charpos);
11587 }
11588 }
11589
11590 if (accurate_p)
11591 {
11592 w->window_end_valid = w->buffer;
11593 #if 0 /* This is incorrect with variable-height lines. */
11594 xassert (XINT (w->window_end_vpos)
11595 < (WINDOW_TOTAL_LINES (w)
11596 - (WINDOW_WANTS_MODELINE_P (w) ? 1 : 0)));
11597 #endif
11598 w->update_mode_line = Qnil;
11599 }
11600 }
11601
11602
11603 /* Mark the display of windows in the window tree rooted at WINDOW as
11604 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
11605 windows as accurate. If ACCURATE_P is zero, arrange for windows to
11606 be redisplayed the next time redisplay_internal is called. */
11607
11608 void
11609 mark_window_display_accurate (window, accurate_p)
11610 Lisp_Object window;
11611 int accurate_p;
11612 {
11613 struct window *w;
11614
11615 for (; !NILP (window); window = w->next)
11616 {
11617 w = XWINDOW (window);
11618 mark_window_display_accurate_1 (w, accurate_p);
11619
11620 if (!NILP (w->vchild))
11621 mark_window_display_accurate (w->vchild, accurate_p);
11622 if (!NILP (w->hchild))
11623 mark_window_display_accurate (w->hchild, accurate_p);
11624 }
11625
11626 if (accurate_p)
11627 {
11628 update_overlay_arrows (1);
11629 }
11630 else
11631 {
11632 /* Force a thorough redisplay the next time by setting
11633 last_arrow_position and last_arrow_string to t, which is
11634 unequal to any useful value of Voverlay_arrow_... */
11635 update_overlay_arrows (-1);
11636 }
11637 }
11638
11639
11640 /* Return value in display table DP (Lisp_Char_Table *) for character
11641 C. Since a display table doesn't have any parent, we don't have to
11642 follow parent. Do not call this function directly but use the
11643 macro DISP_CHAR_VECTOR. */
11644
11645 Lisp_Object
11646 disp_char_vector (dp, c)
11647 struct Lisp_Char_Table *dp;
11648 int c;
11649 {
11650 int code[4], i;
11651 Lisp_Object val;
11652
11653 if (SINGLE_BYTE_CHAR_P (c))
11654 return (dp->contents[c]);
11655
11656 SPLIT_CHAR (c, code[0], code[1], code[2]);
11657 if (code[1] < 32)
11658 code[1] = -1;
11659 else if (code[2] < 32)
11660 code[2] = -1;
11661
11662 /* Here, the possible range of code[0] (== charset ID) is
11663 128..max_charset. Since the top level char table contains data
11664 for multibyte characters after 256th element, we must increment
11665 code[0] by 128 to get a correct index. */
11666 code[0] += 128;
11667 code[3] = -1; /* anchor */
11668
11669 for (i = 0; code[i] >= 0; i++, dp = XCHAR_TABLE (val))
11670 {
11671 val = dp->contents[code[i]];
11672 if (!SUB_CHAR_TABLE_P (val))
11673 return (NILP (val) ? dp->defalt : val);
11674 }
11675
11676 /* Here, val is a sub char table. We return the default value of
11677 it. */
11678 return (dp->defalt);
11679 }
11680
11681
11682 \f
11683 /***********************************************************************
11684 Window Redisplay
11685 ***********************************************************************/
11686
11687 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
11688
11689 static void
11690 redisplay_windows (window)
11691 Lisp_Object window;
11692 {
11693 while (!NILP (window))
11694 {
11695 struct window *w = XWINDOW (window);
11696
11697 if (!NILP (w->hchild))
11698 redisplay_windows (w->hchild);
11699 else if (!NILP (w->vchild))
11700 redisplay_windows (w->vchild);
11701 else
11702 {
11703 displayed_buffer = XBUFFER (w->buffer);
11704 /* Use list_of_error, not Qerror, so that
11705 we catch only errors and don't run the debugger. */
11706 internal_condition_case_1 (redisplay_window_0, window,
11707 list_of_error,
11708 redisplay_window_error);
11709 }
11710
11711 window = w->next;
11712 }
11713 }
11714
11715 static Lisp_Object
11716 redisplay_window_error ()
11717 {
11718 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
11719 return Qnil;
11720 }
11721
11722 static Lisp_Object
11723 redisplay_window_0 (window)
11724 Lisp_Object window;
11725 {
11726 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11727 redisplay_window (window, 0);
11728 return Qnil;
11729 }
11730
11731 static Lisp_Object
11732 redisplay_window_1 (window)
11733 Lisp_Object window;
11734 {
11735 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
11736 redisplay_window (window, 1);
11737 return Qnil;
11738 }
11739 \f
11740
11741 /* Increment GLYPH until it reaches END or CONDITION fails while
11742 adding (GLYPH)->pixel_width to X. */
11743
11744 #define SKIP_GLYPHS(glyph, end, x, condition) \
11745 do \
11746 { \
11747 (x) += (glyph)->pixel_width; \
11748 ++(glyph); \
11749 } \
11750 while ((glyph) < (end) && (condition))
11751
11752
11753 /* Set cursor position of W. PT is assumed to be displayed in ROW.
11754 DELTA is the number of bytes by which positions recorded in ROW
11755 differ from current buffer positions.
11756
11757 Return 0 if cursor is not on this row. 1 otherwise. */
11758
11759 int
11760 set_cursor_from_row (w, row, matrix, delta, delta_bytes, dy, dvpos)
11761 struct window *w;
11762 struct glyph_row *row;
11763 struct glyph_matrix *matrix;
11764 int delta, delta_bytes, dy, dvpos;
11765 {
11766 struct glyph *glyph = row->glyphs[TEXT_AREA];
11767 struct glyph *end = glyph + row->used[TEXT_AREA];
11768 struct glyph *cursor = NULL;
11769 /* The first glyph that starts a sequence of glyphs from string. */
11770 struct glyph *string_start;
11771 /* The X coordinate of string_start. */
11772 int string_start_x;
11773 /* The last known character position. */
11774 int last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
11775 /* The last known character position before string_start. */
11776 int string_before_pos;
11777 int x = row->x;
11778 int cursor_x = x;
11779 int cursor_from_overlay_pos = 0;
11780 int pt_old = PT - delta;
11781
11782 /* Skip over glyphs not having an object at the start of the row.
11783 These are special glyphs like truncation marks on terminal
11784 frames. */
11785 if (row->displays_text_p)
11786 while (glyph < end
11787 && INTEGERP (glyph->object)
11788 && glyph->charpos < 0)
11789 {
11790 x += glyph->pixel_width;
11791 ++glyph;
11792 }
11793
11794 string_start = NULL;
11795 while (glyph < end
11796 && !INTEGERP (glyph->object)
11797 && (!BUFFERP (glyph->object)
11798 || (last_pos = glyph->charpos) < pt_old))
11799 {
11800 if (! STRINGP (glyph->object))
11801 {
11802 string_start = NULL;
11803 x += glyph->pixel_width;
11804 ++glyph;
11805 if (cursor_from_overlay_pos
11806 && last_pos >= cursor_from_overlay_pos)
11807 {
11808 cursor_from_overlay_pos = 0;
11809 cursor = 0;
11810 }
11811 }
11812 else
11813 {
11814 if (string_start == NULL)
11815 {
11816 string_before_pos = last_pos;
11817 string_start = glyph;
11818 string_start_x = x;
11819 }
11820 /* Skip all glyphs from string. */
11821 do
11822 {
11823 Lisp_Object cprop;
11824 int pos;
11825 if ((cursor == NULL || glyph > cursor)
11826 && (cprop = Fget_char_property (make_number ((glyph)->charpos),
11827 Qcursor, (glyph)->object),
11828 !NILP (cprop))
11829 && (pos = string_buffer_position (w, glyph->object,
11830 string_before_pos),
11831 (pos == 0 /* From overlay */
11832 || pos == pt_old)))
11833 {
11834 /* Estimate overlay buffer position from the buffer
11835 positions of the glyphs before and after the overlay.
11836 Add 1 to last_pos so that if point corresponds to the
11837 glyph right after the overlay, we still use a 'cursor'
11838 property found in that overlay. */
11839 cursor_from_overlay_pos = (pos ? 0 : last_pos
11840 + (INTEGERP (cprop) ? XINT (cprop) : 0));
11841 cursor = glyph;
11842 cursor_x = x;
11843 }
11844 x += glyph->pixel_width;
11845 ++glyph;
11846 }
11847 while (glyph < end && EQ (glyph->object, string_start->object));
11848 }
11849 }
11850
11851 if (cursor != NULL)
11852 {
11853 glyph = cursor;
11854 x = cursor_x;
11855 }
11856 else if (row->ends_in_ellipsis_p && glyph == end)
11857 {
11858 /* Scan back over the ellipsis glyphs, decrementing positions. */
11859 while (glyph > row->glyphs[TEXT_AREA]
11860 && (glyph - 1)->charpos == last_pos)
11861 glyph--, x -= glyph->pixel_width;
11862 /* That loop always goes one position too far,
11863 including the glyph before the ellipsis.
11864 So scan forward over that one. */
11865 x += glyph->pixel_width;
11866 glyph++;
11867 }
11868 else if (string_start
11869 && (glyph == end || !BUFFERP (glyph->object) || last_pos > pt_old))
11870 {
11871 /* We may have skipped over point because the previous glyphs
11872 are from string. As there's no easy way to know the
11873 character position of the current glyph, find the correct
11874 glyph on point by scanning from string_start again. */
11875 Lisp_Object limit;
11876 Lisp_Object string;
11877 struct glyph *stop = glyph;
11878 int pos;
11879
11880 limit = make_number (pt_old + 1);
11881 glyph = string_start;
11882 x = string_start_x;
11883 string = glyph->object;
11884 pos = string_buffer_position (w, string, string_before_pos);
11885 /* If STRING is from overlay, LAST_POS == 0. We skip such glyphs
11886 because we always put cursor after overlay strings. */
11887 while (pos == 0 && glyph < stop)
11888 {
11889 string = glyph->object;
11890 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11891 if (glyph < stop)
11892 pos = string_buffer_position (w, glyph->object, string_before_pos);
11893 }
11894
11895 while (glyph < stop)
11896 {
11897 pos = XINT (Fnext_single_char_property_change
11898 (make_number (pos), Qdisplay, Qnil, limit));
11899 if (pos > pt_old)
11900 break;
11901 /* Skip glyphs from the same string. */
11902 string = glyph->object;
11903 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11904 /* Skip glyphs from an overlay. */
11905 while (glyph < stop
11906 && ! string_buffer_position (w, glyph->object, pos))
11907 {
11908 string = glyph->object;
11909 SKIP_GLYPHS (glyph, stop, x, EQ (glyph->object, string));
11910 }
11911 }
11912
11913 /* If we reached the end of the line, and end was from a string,
11914 cursor is not on this line. */
11915 if (glyph == end && row->continued_p)
11916 return 0;
11917 }
11918
11919 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
11920 w->cursor.x = x;
11921 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
11922 w->cursor.y = row->y + dy;
11923
11924 if (w == XWINDOW (selected_window))
11925 {
11926 if (!row->continued_p
11927 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
11928 && row->x == 0)
11929 {
11930 this_line_buffer = XBUFFER (w->buffer);
11931
11932 CHARPOS (this_line_start_pos)
11933 = MATRIX_ROW_START_CHARPOS (row) + delta;
11934 BYTEPOS (this_line_start_pos)
11935 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
11936
11937 CHARPOS (this_line_end_pos)
11938 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
11939 BYTEPOS (this_line_end_pos)
11940 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
11941
11942 this_line_y = w->cursor.y;
11943 this_line_pixel_height = row->height;
11944 this_line_vpos = w->cursor.vpos;
11945 this_line_start_x = row->x;
11946 }
11947 else
11948 CHARPOS (this_line_start_pos) = 0;
11949 }
11950
11951 return 1;
11952 }
11953
11954
11955 /* Run window scroll functions, if any, for WINDOW with new window
11956 start STARTP. Sets the window start of WINDOW to that position.
11957
11958 We assume that the window's buffer is really current. */
11959
11960 static INLINE struct text_pos
11961 run_window_scroll_functions (window, startp)
11962 Lisp_Object window;
11963 struct text_pos startp;
11964 {
11965 struct window *w = XWINDOW (window);
11966 SET_MARKER_FROM_TEXT_POS (w->start, startp);
11967
11968 if (current_buffer != XBUFFER (w->buffer))
11969 abort ();
11970
11971 if (!NILP (Vwindow_scroll_functions))
11972 {
11973 run_hook_with_args_2 (Qwindow_scroll_functions, window,
11974 make_number (CHARPOS (startp)));
11975 SET_TEXT_POS_FROM_MARKER (startp, w->start);
11976 /* In case the hook functions switch buffers. */
11977 if (current_buffer != XBUFFER (w->buffer))
11978 set_buffer_internal_1 (XBUFFER (w->buffer));
11979 }
11980
11981 return startp;
11982 }
11983
11984
11985 /* Make sure the line containing the cursor is fully visible.
11986 A value of 1 means there is nothing to be done.
11987 (Either the line is fully visible, or it cannot be made so,
11988 or we cannot tell.)
11989
11990 If FORCE_P is non-zero, return 0 even if partial visible cursor row
11991 is higher than window.
11992
11993 A value of 0 means the caller should do scrolling
11994 as if point had gone off the screen. */
11995
11996 static int
11997 cursor_row_fully_visible_p (w, force_p, current_matrix_p)
11998 struct window *w;
11999 int force_p;
12000 int current_matrix_p;
12001 {
12002 struct glyph_matrix *matrix;
12003 struct glyph_row *row;
12004 int window_height;
12005
12006 if (!make_cursor_line_fully_visible_p)
12007 return 1;
12008
12009 /* It's not always possible to find the cursor, e.g, when a window
12010 is full of overlay strings. Don't do anything in that case. */
12011 if (w->cursor.vpos < 0)
12012 return 1;
12013
12014 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
12015 row = MATRIX_ROW (matrix, w->cursor.vpos);
12016
12017 /* If the cursor row is not partially visible, there's nothing to do. */
12018 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
12019 return 1;
12020
12021 /* If the row the cursor is in is taller than the window's height,
12022 it's not clear what to do, so do nothing. */
12023 window_height = window_box_height (w);
12024 if (row->height >= window_height)
12025 {
12026 if (!force_p || MINI_WINDOW_P (w) || w->vscroll)
12027 return 1;
12028 }
12029 return 0;
12030
12031 #if 0
12032 /* This code used to try to scroll the window just enough to make
12033 the line visible. It returned 0 to say that the caller should
12034 allocate larger glyph matrices. */
12035
12036 if (MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
12037 {
12038 int dy = row->height - row->visible_height;
12039 w->vscroll = 0;
12040 w->cursor.y += dy;
12041 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12042 }
12043 else /* MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row)) */
12044 {
12045 int dy = - (row->height - row->visible_height);
12046 w->vscroll = dy;
12047 w->cursor.y += dy;
12048 shift_glyph_matrix (w, matrix, 0, matrix->nrows, dy);
12049 }
12050
12051 /* When we change the cursor y-position of the selected window,
12052 change this_line_y as well so that the display optimization for
12053 the cursor line of the selected window in redisplay_internal uses
12054 the correct y-position. */
12055 if (w == XWINDOW (selected_window))
12056 this_line_y = w->cursor.y;
12057
12058 /* If vscrolling requires a larger glyph matrix, arrange for a fresh
12059 redisplay with larger matrices. */
12060 if (matrix->nrows < required_matrix_height (w))
12061 {
12062 fonts_changed_p = 1;
12063 return 0;
12064 }
12065
12066 return 1;
12067 #endif /* 0 */
12068 }
12069
12070
12071 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
12072 non-zero means only WINDOW is redisplayed in redisplay_internal.
12073 TEMP_SCROLL_STEP has the same meaning as scroll_step, and is used
12074 in redisplay_window to bring a partially visible line into view in
12075 the case that only the cursor has moved.
12076
12077 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
12078 last screen line's vertical height extends past the end of the screen.
12079
12080 Value is
12081
12082 1 if scrolling succeeded
12083
12084 0 if scrolling didn't find point.
12085
12086 -1 if new fonts have been loaded so that we must interrupt
12087 redisplay, adjust glyph matrices, and try again. */
12088
12089 enum
12090 {
12091 SCROLLING_SUCCESS,
12092 SCROLLING_FAILED,
12093 SCROLLING_NEED_LARGER_MATRICES
12094 };
12095
12096 static int
12097 try_scrolling (window, just_this_one_p, scroll_conservatively,
12098 scroll_step, temp_scroll_step, last_line_misfit)
12099 Lisp_Object window;
12100 int just_this_one_p;
12101 EMACS_INT scroll_conservatively, scroll_step;
12102 int temp_scroll_step;
12103 int last_line_misfit;
12104 {
12105 struct window *w = XWINDOW (window);
12106 struct frame *f = XFRAME (w->frame);
12107 struct text_pos scroll_margin_pos;
12108 struct text_pos pos;
12109 struct text_pos startp;
12110 struct it it;
12111 Lisp_Object window_end;
12112 int this_scroll_margin;
12113 int dy = 0;
12114 int scroll_max;
12115 int rc;
12116 int amount_to_scroll = 0;
12117 Lisp_Object aggressive;
12118 int height;
12119 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
12120
12121 #if GLYPH_DEBUG
12122 debug_method_add (w, "try_scrolling");
12123 #endif
12124
12125 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12126
12127 /* Compute scroll margin height in pixels. We scroll when point is
12128 within this distance from the top or bottom of the window. */
12129 if (scroll_margin > 0)
12130 {
12131 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12132 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12133 }
12134 else
12135 this_scroll_margin = 0;
12136
12137 /* Force scroll_conservatively to have a reasonable value so it doesn't
12138 cause an overflow while computing how much to scroll. */
12139 if (scroll_conservatively)
12140 scroll_conservatively = min (scroll_conservatively,
12141 MOST_POSITIVE_FIXNUM / FRAME_LINE_HEIGHT (f));
12142
12143 /* Compute how much we should try to scroll maximally to bring point
12144 into view. */
12145 if (scroll_step || scroll_conservatively || temp_scroll_step)
12146 scroll_max = max (scroll_step,
12147 max (scroll_conservatively, temp_scroll_step));
12148 else if (NUMBERP (current_buffer->scroll_down_aggressively)
12149 || NUMBERP (current_buffer->scroll_up_aggressively))
12150 /* We're trying to scroll because of aggressive scrolling
12151 but no scroll_step is set. Choose an arbitrary one. Maybe
12152 there should be a variable for this. */
12153 scroll_max = 10;
12154 else
12155 scroll_max = 0;
12156 scroll_max *= FRAME_LINE_HEIGHT (f);
12157
12158 /* Decide whether we have to scroll down. Start at the window end
12159 and move this_scroll_margin up to find the position of the scroll
12160 margin. */
12161 window_end = Fwindow_end (window, Qt);
12162
12163 too_near_end:
12164
12165 CHARPOS (scroll_margin_pos) = XINT (window_end);
12166 BYTEPOS (scroll_margin_pos) = CHAR_TO_BYTE (CHARPOS (scroll_margin_pos));
12167
12168 if (this_scroll_margin || extra_scroll_margin_lines)
12169 {
12170 start_display (&it, w, scroll_margin_pos);
12171 if (this_scroll_margin)
12172 move_it_vertically_backward (&it, this_scroll_margin);
12173 if (extra_scroll_margin_lines)
12174 move_it_by_lines (&it, - extra_scroll_margin_lines, 0);
12175 scroll_margin_pos = it.current.pos;
12176 }
12177
12178 if (PT >= CHARPOS (scroll_margin_pos))
12179 {
12180 int y0;
12181
12182 /* Point is in the scroll margin at the bottom of the window, or
12183 below. Compute a new window start that makes point visible. */
12184
12185 /* Compute the distance from the scroll margin to PT.
12186 Give up if the distance is greater than scroll_max. */
12187 start_display (&it, w, scroll_margin_pos);
12188 y0 = it.current_y;
12189 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12190 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12191
12192 /* To make point visible, we have to move the window start
12193 down so that the line the cursor is in is visible, which
12194 means we have to add in the height of the cursor line. */
12195 dy = line_bottom_y (&it) - y0;
12196
12197 if (dy > scroll_max)
12198 return SCROLLING_FAILED;
12199
12200 /* Move the window start down. If scrolling conservatively,
12201 move it just enough down to make point visible. If
12202 scroll_step is set, move it down by scroll_step. */
12203 start_display (&it, w, startp);
12204
12205 if (scroll_conservatively)
12206 /* Set AMOUNT_TO_SCROLL to at least one line,
12207 and at most scroll_conservatively lines. */
12208 amount_to_scroll
12209 = min (max (dy, FRAME_LINE_HEIGHT (f)),
12210 FRAME_LINE_HEIGHT (f) * scroll_conservatively);
12211 else if (scroll_step || temp_scroll_step)
12212 amount_to_scroll = scroll_max;
12213 else
12214 {
12215 aggressive = current_buffer->scroll_up_aggressively;
12216 height = WINDOW_BOX_TEXT_HEIGHT (w);
12217 if (NUMBERP (aggressive))
12218 {
12219 double float_amount = XFLOATINT (aggressive) * height;
12220 amount_to_scroll = float_amount;
12221 if (amount_to_scroll == 0 && float_amount > 0)
12222 amount_to_scroll = 1;
12223 }
12224 }
12225
12226 if (amount_to_scroll <= 0)
12227 return SCROLLING_FAILED;
12228
12229 /* If moving by amount_to_scroll leaves STARTP unchanged,
12230 move it down one screen line. */
12231
12232 move_it_vertically (&it, amount_to_scroll);
12233 if (CHARPOS (it.current.pos) == CHARPOS (startp))
12234 move_it_by_lines (&it, 1, 1);
12235 startp = it.current.pos;
12236 }
12237 else
12238 {
12239 /* See if point is inside the scroll margin at the top of the
12240 window. */
12241 scroll_margin_pos = startp;
12242 if (this_scroll_margin)
12243 {
12244 start_display (&it, w, startp);
12245 move_it_vertically (&it, this_scroll_margin);
12246 scroll_margin_pos = it.current.pos;
12247 }
12248
12249 if (PT < CHARPOS (scroll_margin_pos))
12250 {
12251 /* Point is in the scroll margin at the top of the window or
12252 above what is displayed in the window. */
12253 int y0;
12254
12255 /* Compute the vertical distance from PT to the scroll
12256 margin position. Give up if distance is greater than
12257 scroll_max. */
12258 SET_TEXT_POS (pos, PT, PT_BYTE);
12259 start_display (&it, w, pos);
12260 y0 = it.current_y;
12261 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
12262 it.last_visible_y, -1,
12263 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12264 dy = it.current_y - y0;
12265 if (dy > scroll_max)
12266 return SCROLLING_FAILED;
12267
12268 /* Compute new window start. */
12269 start_display (&it, w, startp);
12270
12271 if (scroll_conservatively)
12272 amount_to_scroll
12273 = max (dy, FRAME_LINE_HEIGHT (f) * max (scroll_step, temp_scroll_step));
12274 else if (scroll_step || temp_scroll_step)
12275 amount_to_scroll = scroll_max;
12276 else
12277 {
12278 aggressive = current_buffer->scroll_down_aggressively;
12279 height = WINDOW_BOX_TEXT_HEIGHT (w);
12280 if (NUMBERP (aggressive))
12281 {
12282 double float_amount = XFLOATINT (aggressive) * height;
12283 amount_to_scroll = float_amount;
12284 if (amount_to_scroll == 0 && float_amount > 0)
12285 amount_to_scroll = 1;
12286 }
12287 }
12288
12289 if (amount_to_scroll <= 0)
12290 return SCROLLING_FAILED;
12291
12292 move_it_vertically_backward (&it, amount_to_scroll);
12293 startp = it.current.pos;
12294 }
12295 }
12296
12297 /* Run window scroll functions. */
12298 startp = run_window_scroll_functions (window, startp);
12299
12300 /* Display the window. Give up if new fonts are loaded, or if point
12301 doesn't appear. */
12302 if (!try_window (window, startp, 0))
12303 rc = SCROLLING_NEED_LARGER_MATRICES;
12304 else if (w->cursor.vpos < 0)
12305 {
12306 clear_glyph_matrix (w->desired_matrix);
12307 rc = SCROLLING_FAILED;
12308 }
12309 else
12310 {
12311 /* Maybe forget recorded base line for line number display. */
12312 if (!just_this_one_p
12313 || current_buffer->clip_changed
12314 || BEG_UNCHANGED < CHARPOS (startp))
12315 w->base_line_number = Qnil;
12316
12317 /* If cursor ends up on a partially visible line,
12318 treat that as being off the bottom of the screen. */
12319 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0))
12320 {
12321 clear_glyph_matrix (w->desired_matrix);
12322 ++extra_scroll_margin_lines;
12323 goto too_near_end;
12324 }
12325 rc = SCROLLING_SUCCESS;
12326 }
12327
12328 return rc;
12329 }
12330
12331
12332 /* Compute a suitable window start for window W if display of W starts
12333 on a continuation line. Value is non-zero if a new window start
12334 was computed.
12335
12336 The new window start will be computed, based on W's width, starting
12337 from the start of the continued line. It is the start of the
12338 screen line with the minimum distance from the old start W->start. */
12339
12340 static int
12341 compute_window_start_on_continuation_line (w)
12342 struct window *w;
12343 {
12344 struct text_pos pos, start_pos;
12345 int window_start_changed_p = 0;
12346
12347 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
12348
12349 /* If window start is on a continuation line... Window start may be
12350 < BEGV in case there's invisible text at the start of the
12351 buffer (M-x rmail, for example). */
12352 if (CHARPOS (start_pos) > BEGV
12353 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
12354 {
12355 struct it it;
12356 struct glyph_row *row;
12357
12358 /* Handle the case that the window start is out of range. */
12359 if (CHARPOS (start_pos) < BEGV)
12360 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
12361 else if (CHARPOS (start_pos) > ZV)
12362 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
12363
12364 /* Find the start of the continued line. This should be fast
12365 because scan_buffer is fast (newline cache). */
12366 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
12367 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
12368 row, DEFAULT_FACE_ID);
12369 reseat_at_previous_visible_line_start (&it);
12370
12371 /* If the line start is "too far" away from the window start,
12372 say it takes too much time to compute a new window start. */
12373 if (CHARPOS (start_pos) - IT_CHARPOS (it)
12374 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
12375 {
12376 int min_distance, distance;
12377
12378 /* Move forward by display lines to find the new window
12379 start. If window width was enlarged, the new start can
12380 be expected to be > the old start. If window width was
12381 decreased, the new window start will be < the old start.
12382 So, we're looking for the display line start with the
12383 minimum distance from the old window start. */
12384 pos = it.current.pos;
12385 min_distance = INFINITY;
12386 while ((distance = abs (CHARPOS (start_pos) - IT_CHARPOS (it))),
12387 distance < min_distance)
12388 {
12389 min_distance = distance;
12390 pos = it.current.pos;
12391 move_it_by_lines (&it, 1, 0);
12392 }
12393
12394 /* Set the window start there. */
12395 SET_MARKER_FROM_TEXT_POS (w->start, pos);
12396 window_start_changed_p = 1;
12397 }
12398 }
12399
12400 return window_start_changed_p;
12401 }
12402
12403
12404 /* Try cursor movement in case text has not changed in window WINDOW,
12405 with window start STARTP. Value is
12406
12407 CURSOR_MOVEMENT_SUCCESS if successful
12408
12409 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
12410
12411 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
12412 display. *SCROLL_STEP is set to 1, under certain circumstances, if
12413 we want to scroll as if scroll-step were set to 1. See the code.
12414
12415 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
12416 which case we have to abort this redisplay, and adjust matrices
12417 first. */
12418
12419 enum
12420 {
12421 CURSOR_MOVEMENT_SUCCESS,
12422 CURSOR_MOVEMENT_CANNOT_BE_USED,
12423 CURSOR_MOVEMENT_MUST_SCROLL,
12424 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
12425 };
12426
12427 static int
12428 try_cursor_movement (window, startp, scroll_step)
12429 Lisp_Object window;
12430 struct text_pos startp;
12431 int *scroll_step;
12432 {
12433 struct window *w = XWINDOW (window);
12434 struct frame *f = XFRAME (w->frame);
12435 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
12436
12437 #if GLYPH_DEBUG
12438 if (inhibit_try_cursor_movement)
12439 return rc;
12440 #endif
12441
12442 /* Handle case where text has not changed, only point, and it has
12443 not moved off the frame. */
12444 if (/* Point may be in this window. */
12445 PT >= CHARPOS (startp)
12446 /* Selective display hasn't changed. */
12447 && !current_buffer->clip_changed
12448 /* Function force-mode-line-update is used to force a thorough
12449 redisplay. It sets either windows_or_buffers_changed or
12450 update_mode_lines. So don't take a shortcut here for these
12451 cases. */
12452 && !update_mode_lines
12453 && !windows_or_buffers_changed
12454 && !cursor_type_changed
12455 /* Can't use this case if highlighting a region. When a
12456 region exists, cursor movement has to do more than just
12457 set the cursor. */
12458 && !(!NILP (Vtransient_mark_mode)
12459 && !NILP (current_buffer->mark_active))
12460 && NILP (w->region_showing)
12461 && NILP (Vshow_trailing_whitespace)
12462 /* Right after splitting windows, last_point may be nil. */
12463 && INTEGERP (w->last_point)
12464 /* This code is not used for mini-buffer for the sake of the case
12465 of redisplaying to replace an echo area message; since in
12466 that case the mini-buffer contents per se are usually
12467 unchanged. This code is of no real use in the mini-buffer
12468 since the handling of this_line_start_pos, etc., in redisplay
12469 handles the same cases. */
12470 && !EQ (window, minibuf_window)
12471 /* When splitting windows or for new windows, it happens that
12472 redisplay is called with a nil window_end_vpos or one being
12473 larger than the window. This should really be fixed in
12474 window.c. I don't have this on my list, now, so we do
12475 approximately the same as the old redisplay code. --gerd. */
12476 && INTEGERP (w->window_end_vpos)
12477 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
12478 && (FRAME_WINDOW_P (f)
12479 || !overlay_arrow_in_current_buffer_p ()))
12480 {
12481 int this_scroll_margin, top_scroll_margin;
12482 struct glyph_row *row = NULL;
12483
12484 #if GLYPH_DEBUG
12485 debug_method_add (w, "cursor movement");
12486 #endif
12487
12488 /* Scroll if point within this distance from the top or bottom
12489 of the window. This is a pixel value. */
12490 this_scroll_margin = max (0, scroll_margin);
12491 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
12492 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
12493
12494 top_scroll_margin = this_scroll_margin;
12495 if (WINDOW_WANTS_HEADER_LINE_P (w))
12496 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
12497
12498 /* Start with the row the cursor was displayed during the last
12499 not paused redisplay. Give up if that row is not valid. */
12500 if (w->last_cursor.vpos < 0
12501 || w->last_cursor.vpos >= w->current_matrix->nrows)
12502 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12503 else
12504 {
12505 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
12506 if (row->mode_line_p)
12507 ++row;
12508 if (!row->enabled_p)
12509 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12510 }
12511
12512 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
12513 {
12514 int scroll_p = 0;
12515 int last_y = window_text_bottom_y (w) - this_scroll_margin;
12516
12517 if (PT > XFASTINT (w->last_point))
12518 {
12519 /* Point has moved forward. */
12520 while (MATRIX_ROW_END_CHARPOS (row) < PT
12521 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
12522 {
12523 xassert (row->enabled_p);
12524 ++row;
12525 }
12526
12527 /* The end position of a row equals the start position
12528 of the next row. If PT is there, we would rather
12529 display it in the next line. */
12530 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12531 && MATRIX_ROW_END_CHARPOS (row) == PT
12532 && !cursor_row_p (w, row))
12533 ++row;
12534
12535 /* If within the scroll margin, scroll. Note that
12536 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
12537 the next line would be drawn, and that
12538 this_scroll_margin can be zero. */
12539 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
12540 || PT > MATRIX_ROW_END_CHARPOS (row)
12541 /* Line is completely visible last line in window
12542 and PT is to be set in the next line. */
12543 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
12544 && PT == MATRIX_ROW_END_CHARPOS (row)
12545 && !row->ends_at_zv_p
12546 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
12547 scroll_p = 1;
12548 }
12549 else if (PT < XFASTINT (w->last_point))
12550 {
12551 /* Cursor has to be moved backward. Note that PT >=
12552 CHARPOS (startp) because of the outer if-statement. */
12553 while (!row->mode_line_p
12554 && (MATRIX_ROW_START_CHARPOS (row) > PT
12555 || (MATRIX_ROW_START_CHARPOS (row) == PT
12556 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
12557 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
12558 row > w->current_matrix->rows
12559 && (row-1)->ends_in_newline_from_string_p))))
12560 && (row->y > top_scroll_margin
12561 || CHARPOS (startp) == BEGV))
12562 {
12563 xassert (row->enabled_p);
12564 --row;
12565 }
12566
12567 /* Consider the following case: Window starts at BEGV,
12568 there is invisible, intangible text at BEGV, so that
12569 display starts at some point START > BEGV. It can
12570 happen that we are called with PT somewhere between
12571 BEGV and START. Try to handle that case. */
12572 if (row < w->current_matrix->rows
12573 || row->mode_line_p)
12574 {
12575 row = w->current_matrix->rows;
12576 if (row->mode_line_p)
12577 ++row;
12578 }
12579
12580 /* Due to newlines in overlay strings, we may have to
12581 skip forward over overlay strings. */
12582 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12583 && MATRIX_ROW_END_CHARPOS (row) == PT
12584 && !cursor_row_p (w, row))
12585 ++row;
12586
12587 /* If within the scroll margin, scroll. */
12588 if (row->y < top_scroll_margin
12589 && CHARPOS (startp) != BEGV)
12590 scroll_p = 1;
12591 }
12592 else
12593 {
12594 /* Cursor did not move. So don't scroll even if cursor line
12595 is partially visible, as it was so before. */
12596 rc = CURSOR_MOVEMENT_SUCCESS;
12597 }
12598
12599 if (PT < MATRIX_ROW_START_CHARPOS (row)
12600 || PT > MATRIX_ROW_END_CHARPOS (row))
12601 {
12602 /* if PT is not in the glyph row, give up. */
12603 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12604 }
12605 else if (rc != CURSOR_MOVEMENT_SUCCESS
12606 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
12607 && make_cursor_line_fully_visible_p)
12608 {
12609 if (PT == MATRIX_ROW_END_CHARPOS (row)
12610 && !row->ends_at_zv_p
12611 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
12612 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12613 else if (row->height > window_box_height (w))
12614 {
12615 /* If we end up in a partially visible line, let's
12616 make it fully visible, except when it's taller
12617 than the window, in which case we can't do much
12618 about it. */
12619 *scroll_step = 1;
12620 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12621 }
12622 else
12623 {
12624 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12625 if (!cursor_row_fully_visible_p (w, 0, 1))
12626 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12627 else
12628 rc = CURSOR_MOVEMENT_SUCCESS;
12629 }
12630 }
12631 else if (scroll_p)
12632 rc = CURSOR_MOVEMENT_MUST_SCROLL;
12633 else
12634 {
12635 do
12636 {
12637 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
12638 {
12639 rc = CURSOR_MOVEMENT_SUCCESS;
12640 break;
12641 }
12642 ++row;
12643 }
12644 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
12645 && MATRIX_ROW_START_CHARPOS (row) == PT
12646 && cursor_row_p (w, row));
12647 }
12648 }
12649 }
12650
12651 return rc;
12652 }
12653
12654 void
12655 set_vertical_scroll_bar (w)
12656 struct window *w;
12657 {
12658 int start, end, whole;
12659
12660 /* Calculate the start and end positions for the current window.
12661 At some point, it would be nice to choose between scrollbars
12662 which reflect the whole buffer size, with special markers
12663 indicating narrowing, and scrollbars which reflect only the
12664 visible region.
12665
12666 Note that mini-buffers sometimes aren't displaying any text. */
12667 if (!MINI_WINDOW_P (w)
12668 || (w == XWINDOW (minibuf_window)
12669 && NILP (echo_area_buffer[0])))
12670 {
12671 struct buffer *buf = XBUFFER (w->buffer);
12672 whole = BUF_ZV (buf) - BUF_BEGV (buf);
12673 start = marker_position (w->start) - BUF_BEGV (buf);
12674 /* I don't think this is guaranteed to be right. For the
12675 moment, we'll pretend it is. */
12676 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
12677
12678 if (end < start)
12679 end = start;
12680 if (whole < (end - start))
12681 whole = end - start;
12682 }
12683 else
12684 start = end = whole = 0;
12685
12686 /* Indicate what this scroll bar ought to be displaying now. */
12687 set_vertical_scroll_bar_hook (w, end - start, whole, start);
12688 }
12689
12690
12691 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
12692 selected_window is redisplayed.
12693
12694 We can return without actually redisplaying the window if
12695 fonts_changed_p is nonzero. In that case, redisplay_internal will
12696 retry. */
12697
12698 static void
12699 redisplay_window (window, just_this_one_p)
12700 Lisp_Object window;
12701 int just_this_one_p;
12702 {
12703 struct window *w = XWINDOW (window);
12704 struct frame *f = XFRAME (w->frame);
12705 struct buffer *buffer = XBUFFER (w->buffer);
12706 struct buffer *old = current_buffer;
12707 struct text_pos lpoint, opoint, startp;
12708 int update_mode_line;
12709 int tem;
12710 struct it it;
12711 /* Record it now because it's overwritten. */
12712 int current_matrix_up_to_date_p = 0;
12713 int used_current_matrix_p = 0;
12714 /* This is less strict than current_matrix_up_to_date_p.
12715 It indictes that the buffer contents and narrowing are unchanged. */
12716 int buffer_unchanged_p = 0;
12717 int temp_scroll_step = 0;
12718 int count = SPECPDL_INDEX ();
12719 int rc;
12720 int centering_position = -1;
12721 int last_line_misfit = 0;
12722
12723 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12724 opoint = lpoint;
12725
12726 /* W must be a leaf window here. */
12727 xassert (!NILP (w->buffer));
12728 #if GLYPH_DEBUG
12729 *w->desired_matrix->method = 0;
12730 #endif
12731
12732 specbind (Qinhibit_point_motion_hooks, Qt);
12733
12734 reconsider_clip_changes (w, buffer);
12735
12736 /* Has the mode line to be updated? */
12737 update_mode_line = (!NILP (w->update_mode_line)
12738 || update_mode_lines
12739 || buffer->clip_changed
12740 || buffer->prevent_redisplay_optimizations_p);
12741
12742 if (MINI_WINDOW_P (w))
12743 {
12744 if (w == XWINDOW (echo_area_window)
12745 && !NILP (echo_area_buffer[0]))
12746 {
12747 if (update_mode_line)
12748 /* We may have to update a tty frame's menu bar or a
12749 tool-bar. Example `M-x C-h C-h C-g'. */
12750 goto finish_menu_bars;
12751 else
12752 /* We've already displayed the echo area glyphs in this window. */
12753 goto finish_scroll_bars;
12754 }
12755 else if ((w != XWINDOW (minibuf_window)
12756 || minibuf_level == 0)
12757 /* When buffer is nonempty, redisplay window normally. */
12758 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
12759 /* Quail displays non-mini buffers in minibuffer window.
12760 In that case, redisplay the window normally. */
12761 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
12762 {
12763 /* W is a mini-buffer window, but it's not active, so clear
12764 it. */
12765 int yb = window_text_bottom_y (w);
12766 struct glyph_row *row;
12767 int y;
12768
12769 for (y = 0, row = w->desired_matrix->rows;
12770 y < yb;
12771 y += row->height, ++row)
12772 blank_row (w, row, y);
12773 goto finish_scroll_bars;
12774 }
12775
12776 clear_glyph_matrix (w->desired_matrix);
12777 }
12778
12779 /* Otherwise set up data on this window; select its buffer and point
12780 value. */
12781 /* Really select the buffer, for the sake of buffer-local
12782 variables. */
12783 set_buffer_internal_1 (XBUFFER (w->buffer));
12784 SET_TEXT_POS (opoint, PT, PT_BYTE);
12785
12786 current_matrix_up_to_date_p
12787 = (!NILP (w->window_end_valid)
12788 && !current_buffer->clip_changed
12789 && !current_buffer->prevent_redisplay_optimizations_p
12790 && XFASTINT (w->last_modified) >= MODIFF
12791 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12792
12793 buffer_unchanged_p
12794 = (!NILP (w->window_end_valid)
12795 && !current_buffer->clip_changed
12796 && XFASTINT (w->last_modified) >= MODIFF
12797 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
12798
12799 /* When windows_or_buffers_changed is non-zero, we can't rely on
12800 the window end being valid, so set it to nil there. */
12801 if (windows_or_buffers_changed)
12802 {
12803 /* If window starts on a continuation line, maybe adjust the
12804 window start in case the window's width changed. */
12805 if (XMARKER (w->start)->buffer == current_buffer)
12806 compute_window_start_on_continuation_line (w);
12807
12808 w->window_end_valid = Qnil;
12809 }
12810
12811 /* Some sanity checks. */
12812 CHECK_WINDOW_END (w);
12813 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
12814 abort ();
12815 if (BYTEPOS (opoint) < CHARPOS (opoint))
12816 abort ();
12817
12818 /* If %c is in mode line, update it if needed. */
12819 if (!NILP (w->column_number_displayed)
12820 /* This alternative quickly identifies a common case
12821 where no change is needed. */
12822 && !(PT == XFASTINT (w->last_point)
12823 && XFASTINT (w->last_modified) >= MODIFF
12824 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12825 && (XFASTINT (w->column_number_displayed)
12826 != (int) current_column ())) /* iftc */
12827 update_mode_line = 1;
12828
12829 /* Count number of windows showing the selected buffer. An indirect
12830 buffer counts as its base buffer. */
12831 if (!just_this_one_p)
12832 {
12833 struct buffer *current_base, *window_base;
12834 current_base = current_buffer;
12835 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
12836 if (current_base->base_buffer)
12837 current_base = current_base->base_buffer;
12838 if (window_base->base_buffer)
12839 window_base = window_base->base_buffer;
12840 if (current_base == window_base)
12841 buffer_shared++;
12842 }
12843
12844 /* Point refers normally to the selected window. For any other
12845 window, set up appropriate value. */
12846 if (!EQ (window, selected_window))
12847 {
12848 int new_pt = XMARKER (w->pointm)->charpos;
12849 int new_pt_byte = marker_byte_position (w->pointm);
12850 if (new_pt < BEGV)
12851 {
12852 new_pt = BEGV;
12853 new_pt_byte = BEGV_BYTE;
12854 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
12855 }
12856 else if (new_pt > (ZV - 1))
12857 {
12858 new_pt = ZV;
12859 new_pt_byte = ZV_BYTE;
12860 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
12861 }
12862
12863 /* We don't use SET_PT so that the point-motion hooks don't run. */
12864 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
12865 }
12866
12867 /* If any of the character widths specified in the display table
12868 have changed, invalidate the width run cache. It's true that
12869 this may be a bit late to catch such changes, but the rest of
12870 redisplay goes (non-fatally) haywire when the display table is
12871 changed, so why should we worry about doing any better? */
12872 if (current_buffer->width_run_cache)
12873 {
12874 struct Lisp_Char_Table *disptab = buffer_display_table ();
12875
12876 if (! disptab_matches_widthtab (disptab,
12877 XVECTOR (current_buffer->width_table)))
12878 {
12879 invalidate_region_cache (current_buffer,
12880 current_buffer->width_run_cache,
12881 BEG, Z);
12882 recompute_width_table (current_buffer, disptab);
12883 }
12884 }
12885
12886 /* If window-start is screwed up, choose a new one. */
12887 if (XMARKER (w->start)->buffer != current_buffer)
12888 goto recenter;
12889
12890 SET_TEXT_POS_FROM_MARKER (startp, w->start);
12891
12892 /* If someone specified a new starting point but did not insist,
12893 check whether it can be used. */
12894 if (!NILP (w->optional_new_start)
12895 && CHARPOS (startp) >= BEGV
12896 && CHARPOS (startp) <= ZV)
12897 {
12898 w->optional_new_start = Qnil;
12899 start_display (&it, w, startp);
12900 move_it_to (&it, PT, 0, it.last_visible_y, -1,
12901 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
12902 if (IT_CHARPOS (it) == PT)
12903 w->force_start = Qt;
12904 /* IT may overshoot PT if text at PT is invisible. */
12905 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
12906 w->force_start = Qt;
12907 }
12908
12909 /* Handle case where place to start displaying has been specified,
12910 unless the specified location is outside the accessible range. */
12911 if (!NILP (w->force_start)
12912 || w->frozen_window_start_p)
12913 {
12914 /* We set this later on if we have to adjust point. */
12915 int new_vpos = -1;
12916 int val;
12917
12918 w->force_start = Qnil;
12919 w->vscroll = 0;
12920 w->window_end_valid = Qnil;
12921
12922 /* Forget any recorded base line for line number display. */
12923 if (!buffer_unchanged_p)
12924 w->base_line_number = Qnil;
12925
12926 /* Redisplay the mode line. Select the buffer properly for that.
12927 Also, run the hook window-scroll-functions
12928 because we have scrolled. */
12929 /* Note, we do this after clearing force_start because
12930 if there's an error, it is better to forget about force_start
12931 than to get into an infinite loop calling the hook functions
12932 and having them get more errors. */
12933 if (!update_mode_line
12934 || ! NILP (Vwindow_scroll_functions))
12935 {
12936 update_mode_line = 1;
12937 w->update_mode_line = Qt;
12938 startp = run_window_scroll_functions (window, startp);
12939 }
12940
12941 w->last_modified = make_number (0);
12942 w->last_overlay_modified = make_number (0);
12943 if (CHARPOS (startp) < BEGV)
12944 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
12945 else if (CHARPOS (startp) > ZV)
12946 SET_TEXT_POS (startp, ZV, ZV_BYTE);
12947
12948 /* Redisplay, then check if cursor has been set during the
12949 redisplay. Give up if new fonts were loaded. */
12950 val = try_window (window, startp, 1);
12951 if (!val)
12952 {
12953 w->force_start = Qt;
12954 clear_glyph_matrix (w->desired_matrix);
12955 goto need_larger_matrices;
12956 }
12957 /* Point was outside the scroll margins. */
12958 if (val < 0)
12959 new_vpos = window_box_height (w) / 2;
12960
12961 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
12962 {
12963 /* If point does not appear, try to move point so it does
12964 appear. The desired matrix has been built above, so we
12965 can use it here. */
12966 new_vpos = window_box_height (w) / 2;
12967 }
12968
12969 if (!cursor_row_fully_visible_p (w, 0, 0))
12970 {
12971 /* Point does appear, but on a line partly visible at end of window.
12972 Move it back to a fully-visible line. */
12973 new_vpos = window_box_height (w);
12974 }
12975
12976 /* If we need to move point for either of the above reasons,
12977 now actually do it. */
12978 if (new_vpos >= 0)
12979 {
12980 struct glyph_row *row;
12981
12982 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
12983 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
12984 ++row;
12985
12986 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
12987 MATRIX_ROW_START_BYTEPOS (row));
12988
12989 if (w != XWINDOW (selected_window))
12990 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
12991 else if (current_buffer == old)
12992 SET_TEXT_POS (lpoint, PT, PT_BYTE);
12993
12994 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
12995
12996 /* If we are highlighting the region, then we just changed
12997 the region, so redisplay to show it. */
12998 if (!NILP (Vtransient_mark_mode)
12999 && !NILP (current_buffer->mark_active))
13000 {
13001 clear_glyph_matrix (w->desired_matrix);
13002 if (!try_window (window, startp, 0))
13003 goto need_larger_matrices;
13004 }
13005 }
13006
13007 #if GLYPH_DEBUG
13008 debug_method_add (w, "forced window start");
13009 #endif
13010 goto done;
13011 }
13012
13013 /* Handle case where text has not changed, only point, and it has
13014 not moved off the frame, and we are not retrying after hscroll.
13015 (current_matrix_up_to_date_p is nonzero when retrying.) */
13016 if (current_matrix_up_to_date_p
13017 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
13018 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
13019 {
13020 switch (rc)
13021 {
13022 case CURSOR_MOVEMENT_SUCCESS:
13023 used_current_matrix_p = 1;
13024 goto done;
13025
13026 #if 0 /* try_cursor_movement never returns this value. */
13027 case CURSOR_MOVEMENT_NEED_LARGER_MATRICES:
13028 goto need_larger_matrices;
13029 #endif
13030
13031 case CURSOR_MOVEMENT_MUST_SCROLL:
13032 goto try_to_scroll;
13033
13034 default:
13035 abort ();
13036 }
13037 }
13038 /* If current starting point was originally the beginning of a line
13039 but no longer is, find a new starting point. */
13040 else if (!NILP (w->start_at_line_beg)
13041 && !(CHARPOS (startp) <= BEGV
13042 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
13043 {
13044 #if GLYPH_DEBUG
13045 debug_method_add (w, "recenter 1");
13046 #endif
13047 goto recenter;
13048 }
13049
13050 /* Try scrolling with try_window_id. Value is > 0 if update has
13051 been done, it is -1 if we know that the same window start will
13052 not work. It is 0 if unsuccessful for some other reason. */
13053 else if ((tem = try_window_id (w)) != 0)
13054 {
13055 #if GLYPH_DEBUG
13056 debug_method_add (w, "try_window_id %d", tem);
13057 #endif
13058
13059 if (fonts_changed_p)
13060 goto need_larger_matrices;
13061 if (tem > 0)
13062 goto done;
13063
13064 /* Otherwise try_window_id has returned -1 which means that we
13065 don't want the alternative below this comment to execute. */
13066 }
13067 else if (CHARPOS (startp) >= BEGV
13068 && CHARPOS (startp) <= ZV
13069 && PT >= CHARPOS (startp)
13070 && (CHARPOS (startp) < ZV
13071 /* Avoid starting at end of buffer. */
13072 || CHARPOS (startp) == BEGV
13073 || (XFASTINT (w->last_modified) >= MODIFF
13074 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
13075 {
13076
13077 /* If first window line is a continuation line, and window start
13078 is inside the modified region, but the first change is before
13079 current window start, we must select a new window start.*/
13080 if (NILP (w->start_at_line_beg)
13081 && CHARPOS (startp) > BEGV)
13082 {
13083 /* Make sure beg_unchanged and end_unchanged are up to date.
13084 Do it only if buffer has really changed. This may or may
13085 not have been done by try_window_id (see which) already. */
13086 if (MODIFF > SAVE_MODIFF
13087 /* This seems to happen sometimes after saving a buffer. */
13088 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
13089 {
13090 if (GPT - BEG < BEG_UNCHANGED)
13091 BEG_UNCHANGED = GPT - BEG;
13092 if (Z - GPT < END_UNCHANGED)
13093 END_UNCHANGED = Z - GPT;
13094 }
13095
13096 if (CHARPOS (startp) > BEG + BEG_UNCHANGED
13097 && CHARPOS (startp) <= Z - END_UNCHANGED)
13098 {
13099 /* There doesn't seems to be a simple way to find a new
13100 window start that is near the old window start, so
13101 we just recenter. */
13102 goto recenter;
13103 }
13104 }
13105
13106 #if GLYPH_DEBUG
13107 debug_method_add (w, "same window start");
13108 #endif
13109
13110 /* Try to redisplay starting at same place as before.
13111 If point has not moved off frame, accept the results. */
13112 if (!current_matrix_up_to_date_p
13113 /* Don't use try_window_reusing_current_matrix in this case
13114 because a window scroll function can have changed the
13115 buffer. */
13116 || !NILP (Vwindow_scroll_functions)
13117 || MINI_WINDOW_P (w)
13118 || !(used_current_matrix_p
13119 = try_window_reusing_current_matrix (w)))
13120 {
13121 IF_DEBUG (debug_method_add (w, "1"));
13122 if (try_window (window, startp, 1) < 0)
13123 /* -1 means we need to scroll.
13124 0 means we need new matrices, but fonts_changed_p
13125 is set in that case, so we will detect it below. */
13126 goto try_to_scroll;
13127 }
13128
13129 if (fonts_changed_p)
13130 goto need_larger_matrices;
13131
13132 if (w->cursor.vpos >= 0)
13133 {
13134 if (!just_this_one_p
13135 || current_buffer->clip_changed
13136 || BEG_UNCHANGED < CHARPOS (startp))
13137 /* Forget any recorded base line for line number display. */
13138 w->base_line_number = Qnil;
13139
13140 if (!cursor_row_fully_visible_p (w, 1, 0))
13141 {
13142 clear_glyph_matrix (w->desired_matrix);
13143 last_line_misfit = 1;
13144 }
13145 /* Drop through and scroll. */
13146 else
13147 goto done;
13148 }
13149 else
13150 clear_glyph_matrix (w->desired_matrix);
13151 }
13152
13153 try_to_scroll:
13154
13155 w->last_modified = make_number (0);
13156 w->last_overlay_modified = make_number (0);
13157
13158 /* Redisplay the mode line. Select the buffer properly for that. */
13159 if (!update_mode_line)
13160 {
13161 update_mode_line = 1;
13162 w->update_mode_line = Qt;
13163 }
13164
13165 /* Try to scroll by specified few lines. */
13166 if ((scroll_conservatively
13167 || scroll_step
13168 || temp_scroll_step
13169 || NUMBERP (current_buffer->scroll_up_aggressively)
13170 || NUMBERP (current_buffer->scroll_down_aggressively))
13171 && !current_buffer->clip_changed
13172 && CHARPOS (startp) >= BEGV
13173 && CHARPOS (startp) <= ZV)
13174 {
13175 /* The function returns -1 if new fonts were loaded, 1 if
13176 successful, 0 if not successful. */
13177 int rc = try_scrolling (window, just_this_one_p,
13178 scroll_conservatively,
13179 scroll_step,
13180 temp_scroll_step, last_line_misfit);
13181 switch (rc)
13182 {
13183 case SCROLLING_SUCCESS:
13184 goto done;
13185
13186 case SCROLLING_NEED_LARGER_MATRICES:
13187 goto need_larger_matrices;
13188
13189 case SCROLLING_FAILED:
13190 break;
13191
13192 default:
13193 abort ();
13194 }
13195 }
13196
13197 /* Finally, just choose place to start which centers point */
13198
13199 recenter:
13200 if (centering_position < 0)
13201 centering_position = window_box_height (w) / 2;
13202
13203 #if GLYPH_DEBUG
13204 debug_method_add (w, "recenter");
13205 #endif
13206
13207 /* w->vscroll = 0; */
13208
13209 /* Forget any previously recorded base line for line number display. */
13210 if (!buffer_unchanged_p)
13211 w->base_line_number = Qnil;
13212
13213 /* Move backward half the height of the window. */
13214 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13215 it.current_y = it.last_visible_y;
13216 move_it_vertically_backward (&it, centering_position);
13217 xassert (IT_CHARPOS (it) >= BEGV);
13218
13219 /* The function move_it_vertically_backward may move over more
13220 than the specified y-distance. If it->w is small, e.g. a
13221 mini-buffer window, we may end up in front of the window's
13222 display area. Start displaying at the start of the line
13223 containing PT in this case. */
13224 if (it.current_y <= 0)
13225 {
13226 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
13227 move_it_vertically_backward (&it, 0);
13228 #if 0
13229 /* I think this assert is bogus if buffer contains
13230 invisible text or images. KFS. */
13231 xassert (IT_CHARPOS (it) <= PT);
13232 #endif
13233 it.current_y = 0;
13234 }
13235
13236 it.current_x = it.hpos = 0;
13237
13238 /* Set startp here explicitly in case that helps avoid an infinite loop
13239 in case the window-scroll-functions functions get errors. */
13240 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
13241
13242 /* Run scroll hooks. */
13243 startp = run_window_scroll_functions (window, it.current.pos);
13244
13245 /* Redisplay the window. */
13246 if (!current_matrix_up_to_date_p
13247 || windows_or_buffers_changed
13248 || cursor_type_changed
13249 /* Don't use try_window_reusing_current_matrix in this case
13250 because it can have changed the buffer. */
13251 || !NILP (Vwindow_scroll_functions)
13252 || !just_this_one_p
13253 || MINI_WINDOW_P (w)
13254 || !(used_current_matrix_p
13255 = try_window_reusing_current_matrix (w)))
13256 try_window (window, startp, 0);
13257
13258 /* If new fonts have been loaded (due to fontsets), give up. We
13259 have to start a new redisplay since we need to re-adjust glyph
13260 matrices. */
13261 if (fonts_changed_p)
13262 goto need_larger_matrices;
13263
13264 /* If cursor did not appear assume that the middle of the window is
13265 in the first line of the window. Do it again with the next line.
13266 (Imagine a window of height 100, displaying two lines of height
13267 60. Moving back 50 from it->last_visible_y will end in the first
13268 line.) */
13269 if (w->cursor.vpos < 0)
13270 {
13271 if (!NILP (w->window_end_valid)
13272 && PT >= Z - XFASTINT (w->window_end_pos))
13273 {
13274 clear_glyph_matrix (w->desired_matrix);
13275 move_it_by_lines (&it, 1, 0);
13276 try_window (window, it.current.pos, 0);
13277 }
13278 else if (PT < IT_CHARPOS (it))
13279 {
13280 clear_glyph_matrix (w->desired_matrix);
13281 move_it_by_lines (&it, -1, 0);
13282 try_window (window, it.current.pos, 0);
13283 }
13284 else
13285 {
13286 /* Not much we can do about it. */
13287 }
13288 }
13289
13290 /* Consider the following case: Window starts at BEGV, there is
13291 invisible, intangible text at BEGV, so that display starts at
13292 some point START > BEGV. It can happen that we are called with
13293 PT somewhere between BEGV and START. Try to handle that case. */
13294 if (w->cursor.vpos < 0)
13295 {
13296 struct glyph_row *row = w->current_matrix->rows;
13297 if (row->mode_line_p)
13298 ++row;
13299 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13300 }
13301
13302 if (!cursor_row_fully_visible_p (w, 0, 0))
13303 {
13304 /* If vscroll is enabled, disable it and try again. */
13305 if (w->vscroll)
13306 {
13307 w->vscroll = 0;
13308 clear_glyph_matrix (w->desired_matrix);
13309 goto recenter;
13310 }
13311
13312 /* If centering point failed to make the whole line visible,
13313 put point at the top instead. That has to make the whole line
13314 visible, if it can be done. */
13315 if (centering_position == 0)
13316 goto done;
13317
13318 clear_glyph_matrix (w->desired_matrix);
13319 centering_position = 0;
13320 goto recenter;
13321 }
13322
13323 done:
13324
13325 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13326 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
13327 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
13328 ? Qt : Qnil);
13329
13330 /* Display the mode line, if we must. */
13331 if ((update_mode_line
13332 /* If window not full width, must redo its mode line
13333 if (a) the window to its side is being redone and
13334 (b) we do a frame-based redisplay. This is a consequence
13335 of how inverted lines are drawn in frame-based redisplay. */
13336 || (!just_this_one_p
13337 && !FRAME_WINDOW_P (f)
13338 && !WINDOW_FULL_WIDTH_P (w))
13339 /* Line number to display. */
13340 || INTEGERP (w->base_line_pos)
13341 /* Column number is displayed and different from the one displayed. */
13342 || (!NILP (w->column_number_displayed)
13343 && (XFASTINT (w->column_number_displayed)
13344 != (int) current_column ()))) /* iftc */
13345 /* This means that the window has a mode line. */
13346 && (WINDOW_WANTS_MODELINE_P (w)
13347 || WINDOW_WANTS_HEADER_LINE_P (w)))
13348 {
13349 display_mode_lines (w);
13350
13351 /* If mode line height has changed, arrange for a thorough
13352 immediate redisplay using the correct mode line height. */
13353 if (WINDOW_WANTS_MODELINE_P (w)
13354 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
13355 {
13356 fonts_changed_p = 1;
13357 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
13358 = DESIRED_MODE_LINE_HEIGHT (w);
13359 }
13360
13361 /* If top line height has changed, arrange for a thorough
13362 immediate redisplay using the correct mode line height. */
13363 if (WINDOW_WANTS_HEADER_LINE_P (w)
13364 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
13365 {
13366 fonts_changed_p = 1;
13367 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
13368 = DESIRED_HEADER_LINE_HEIGHT (w);
13369 }
13370
13371 if (fonts_changed_p)
13372 goto need_larger_matrices;
13373 }
13374
13375 if (!line_number_displayed
13376 && !BUFFERP (w->base_line_pos))
13377 {
13378 w->base_line_pos = Qnil;
13379 w->base_line_number = Qnil;
13380 }
13381
13382 finish_menu_bars:
13383
13384 /* When we reach a frame's selected window, redo the frame's menu bar. */
13385 if (update_mode_line
13386 && EQ (FRAME_SELECTED_WINDOW (f), window))
13387 {
13388 int redisplay_menu_p = 0;
13389 int redisplay_tool_bar_p = 0;
13390
13391 if (FRAME_WINDOW_P (f))
13392 {
13393 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) || defined (MAC_OS) \
13394 || defined (USE_GTK)
13395 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
13396 #else
13397 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13398 #endif
13399 }
13400 else
13401 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
13402
13403 if (redisplay_menu_p)
13404 display_menu_bar (w);
13405
13406 #ifdef HAVE_WINDOW_SYSTEM
13407 #ifdef USE_GTK
13408 redisplay_tool_bar_p = FRAME_EXTERNAL_TOOL_BAR (f);
13409 #else
13410 redisplay_tool_bar_p = WINDOWP (f->tool_bar_window)
13411 && (FRAME_TOOL_BAR_LINES (f) > 0
13412 || auto_resize_tool_bars_p);
13413
13414 #endif
13415
13416 if (redisplay_tool_bar_p)
13417 redisplay_tool_bar (f);
13418 #endif
13419 }
13420
13421 #ifdef HAVE_WINDOW_SYSTEM
13422 if (FRAME_WINDOW_P (f)
13423 && update_window_fringes (w, (just_this_one_p
13424 || (!used_current_matrix_p && !overlay_arrow_seen)
13425 || w->pseudo_window_p)))
13426 {
13427 update_begin (f);
13428 BLOCK_INPUT;
13429 if (draw_window_fringes (w, 1))
13430 x_draw_vertical_border (w);
13431 UNBLOCK_INPUT;
13432 update_end (f);
13433 }
13434 #endif /* HAVE_WINDOW_SYSTEM */
13435
13436 /* We go to this label, with fonts_changed_p nonzero,
13437 if it is necessary to try again using larger glyph matrices.
13438 We have to redeem the scroll bar even in this case,
13439 because the loop in redisplay_internal expects that. */
13440 need_larger_matrices:
13441 ;
13442 finish_scroll_bars:
13443
13444 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
13445 {
13446 /* Set the thumb's position and size. */
13447 set_vertical_scroll_bar (w);
13448
13449 /* Note that we actually used the scroll bar attached to this
13450 window, so it shouldn't be deleted at the end of redisplay. */
13451 redeem_scroll_bar_hook (w);
13452 }
13453
13454 /* Restore current_buffer and value of point in it. */
13455 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
13456 set_buffer_internal_1 (old);
13457 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
13458
13459 unbind_to (count, Qnil);
13460 }
13461
13462
13463 /* Build the complete desired matrix of WINDOW with a window start
13464 buffer position POS.
13465
13466 Value is 1 if successful. It is zero if fonts were loaded during
13467 redisplay which makes re-adjusting glyph matrices necessary, and -1
13468 if point would appear in the scroll margins.
13469 (We check that only if CHECK_MARGINS is nonzero. */
13470
13471 int
13472 try_window (window, pos, check_margins)
13473 Lisp_Object window;
13474 struct text_pos pos;
13475 int check_margins;
13476 {
13477 struct window *w = XWINDOW (window);
13478 struct it it;
13479 struct glyph_row *last_text_row = NULL;
13480
13481 /* Make POS the new window start. */
13482 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
13483
13484 /* Mark cursor position as unknown. No overlay arrow seen. */
13485 w->cursor.vpos = -1;
13486 overlay_arrow_seen = 0;
13487
13488 /* Initialize iterator and info to start at POS. */
13489 start_display (&it, w, pos);
13490
13491 /* Display all lines of W. */
13492 while (it.current_y < it.last_visible_y)
13493 {
13494 if (display_line (&it))
13495 last_text_row = it.glyph_row - 1;
13496 if (fonts_changed_p)
13497 return 0;
13498 }
13499
13500 /* Don't let the cursor end in the scroll margins. */
13501 if (check_margins
13502 && !MINI_WINDOW_P (w))
13503 {
13504 int this_scroll_margin;
13505
13506 this_scroll_margin = max (0, scroll_margin);
13507 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
13508 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
13509
13510 if ((w->cursor.y < this_scroll_margin
13511 && CHARPOS (pos) > BEGV
13512 && IT_CHARPOS (it) < ZV)
13513 /* rms: considering make_cursor_line_fully_visible_p here
13514 seems to give wrong results. We don't want to recenter
13515 when the last line is partly visible, we want to allow
13516 that case to be handled in the usual way. */
13517 || (w->cursor.y + 1) > it.last_visible_y)
13518 {
13519 w->cursor.vpos = -1;
13520 clear_glyph_matrix (w->desired_matrix);
13521 return -1;
13522 }
13523 }
13524
13525 /* If bottom moved off end of frame, change mode line percentage. */
13526 if (XFASTINT (w->window_end_pos) <= 0
13527 && Z != IT_CHARPOS (it))
13528 w->update_mode_line = Qt;
13529
13530 /* Set window_end_pos to the offset of the last character displayed
13531 on the window from the end of current_buffer. Set
13532 window_end_vpos to its row number. */
13533 if (last_text_row)
13534 {
13535 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
13536 w->window_end_bytepos
13537 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13538 w->window_end_pos
13539 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13540 w->window_end_vpos
13541 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13542 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
13543 ->displays_text_p);
13544 }
13545 else
13546 {
13547 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13548 w->window_end_pos = make_number (Z - ZV);
13549 w->window_end_vpos = make_number (0);
13550 }
13551
13552 /* But that is not valid info until redisplay finishes. */
13553 w->window_end_valid = Qnil;
13554 return 1;
13555 }
13556
13557
13558 \f
13559 /************************************************************************
13560 Window redisplay reusing current matrix when buffer has not changed
13561 ************************************************************************/
13562
13563 /* Try redisplay of window W showing an unchanged buffer with a
13564 different window start than the last time it was displayed by
13565 reusing its current matrix. Value is non-zero if successful.
13566 W->start is the new window start. */
13567
13568 static int
13569 try_window_reusing_current_matrix (w)
13570 struct window *w;
13571 {
13572 struct frame *f = XFRAME (w->frame);
13573 struct glyph_row *row, *bottom_row;
13574 struct it it;
13575 struct run run;
13576 struct text_pos start, new_start;
13577 int nrows_scrolled, i;
13578 struct glyph_row *last_text_row;
13579 struct glyph_row *last_reused_text_row;
13580 struct glyph_row *start_row;
13581 int start_vpos, min_y, max_y;
13582
13583 #if GLYPH_DEBUG
13584 if (inhibit_try_window_reusing)
13585 return 0;
13586 #endif
13587
13588 if (/* This function doesn't handle terminal frames. */
13589 !FRAME_WINDOW_P (f)
13590 /* Don't try to reuse the display if windows have been split
13591 or such. */
13592 || windows_or_buffers_changed
13593 || cursor_type_changed)
13594 return 0;
13595
13596 /* Can't do this if region may have changed. */
13597 if ((!NILP (Vtransient_mark_mode)
13598 && !NILP (current_buffer->mark_active))
13599 || !NILP (w->region_showing)
13600 || !NILP (Vshow_trailing_whitespace))
13601 return 0;
13602
13603 /* If top-line visibility has changed, give up. */
13604 if (WINDOW_WANTS_HEADER_LINE_P (w)
13605 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
13606 return 0;
13607
13608 /* Give up if old or new display is scrolled vertically. We could
13609 make this function handle this, but right now it doesn't. */
13610 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13611 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
13612 return 0;
13613
13614 /* The variable new_start now holds the new window start. The old
13615 start `start' can be determined from the current matrix. */
13616 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
13617 start = start_row->start.pos;
13618 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13619
13620 /* Clear the desired matrix for the display below. */
13621 clear_glyph_matrix (w->desired_matrix);
13622
13623 if (CHARPOS (new_start) <= CHARPOS (start))
13624 {
13625 int first_row_y;
13626
13627 /* Don't use this method if the display starts with an ellipsis
13628 displayed for invisible text. It's not easy to handle that case
13629 below, and it's certainly not worth the effort since this is
13630 not a frequent case. */
13631 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
13632 return 0;
13633
13634 IF_DEBUG (debug_method_add (w, "twu1"));
13635
13636 /* Display up to a row that can be reused. The variable
13637 last_text_row is set to the last row displayed that displays
13638 text. Note that it.vpos == 0 if or if not there is a
13639 header-line; it's not the same as the MATRIX_ROW_VPOS! */
13640 start_display (&it, w, new_start);
13641 first_row_y = it.current_y;
13642 w->cursor.vpos = -1;
13643 last_text_row = last_reused_text_row = NULL;
13644
13645 while (it.current_y < it.last_visible_y
13646 && !fonts_changed_p)
13647 {
13648 /* If we have reached into the characters in the START row,
13649 that means the line boundaries have changed. So we
13650 can't start copying with the row START. Maybe it will
13651 work to start copying with the following row. */
13652 while (IT_CHARPOS (it) > CHARPOS (start))
13653 {
13654 /* Advance to the next row as the "start". */
13655 start_row++;
13656 start = start_row->start.pos;
13657 /* If there are no more rows to try, or just one, give up. */
13658 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
13659 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
13660 || CHARPOS (start) == ZV)
13661 {
13662 clear_glyph_matrix (w->desired_matrix);
13663 return 0;
13664 }
13665
13666 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
13667 }
13668 /* If we have reached alignment,
13669 we can copy the rest of the rows. */
13670 if (IT_CHARPOS (it) == CHARPOS (start))
13671 break;
13672
13673 if (display_line (&it))
13674 last_text_row = it.glyph_row - 1;
13675 }
13676
13677 /* A value of current_y < last_visible_y means that we stopped
13678 at the previous window start, which in turn means that we
13679 have at least one reusable row. */
13680 if (it.current_y < it.last_visible_y)
13681 {
13682 /* IT.vpos always starts from 0; it counts text lines. */
13683 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
13684
13685 /* Find PT if not already found in the lines displayed. */
13686 if (w->cursor.vpos < 0)
13687 {
13688 int dy = it.current_y - start_row->y;
13689
13690 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
13691 row = row_containing_pos (w, PT, row, NULL, dy);
13692 if (row)
13693 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
13694 dy, nrows_scrolled);
13695 else
13696 {
13697 clear_glyph_matrix (w->desired_matrix);
13698 return 0;
13699 }
13700 }
13701
13702 /* Scroll the display. Do it before the current matrix is
13703 changed. The problem here is that update has not yet
13704 run, i.e. part of the current matrix is not up to date.
13705 scroll_run_hook will clear the cursor, and use the
13706 current matrix to get the height of the row the cursor is
13707 in. */
13708 run.current_y = start_row->y;
13709 run.desired_y = it.current_y;
13710 run.height = it.last_visible_y - it.current_y;
13711
13712 if (run.height > 0 && run.current_y != run.desired_y)
13713 {
13714 update_begin (f);
13715 rif->update_window_begin_hook (w);
13716 rif->clear_window_mouse_face (w);
13717 rif->scroll_run_hook (w, &run);
13718 rif->update_window_end_hook (w, 0, 0);
13719 update_end (f);
13720 }
13721
13722 /* Shift current matrix down by nrows_scrolled lines. */
13723 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13724 rotate_matrix (w->current_matrix,
13725 start_vpos,
13726 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13727 nrows_scrolled);
13728
13729 /* Disable lines that must be updated. */
13730 for (i = 0; i < it.vpos; ++i)
13731 (start_row + i)->enabled_p = 0;
13732
13733 /* Re-compute Y positions. */
13734 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13735 max_y = it.last_visible_y;
13736 for (row = start_row + nrows_scrolled;
13737 row < bottom_row;
13738 ++row)
13739 {
13740 row->y = it.current_y;
13741 row->visible_height = row->height;
13742
13743 if (row->y < min_y)
13744 row->visible_height -= min_y - row->y;
13745 if (row->y + row->height > max_y)
13746 row->visible_height -= row->y + row->height - max_y;
13747 row->redraw_fringe_bitmaps_p = 1;
13748
13749 it.current_y += row->height;
13750
13751 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13752 last_reused_text_row = row;
13753 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
13754 break;
13755 }
13756
13757 /* Disable lines in the current matrix which are now
13758 below the window. */
13759 for (++row; row < bottom_row; ++row)
13760 row->enabled_p = row->mode_line_p = 0;
13761 }
13762
13763 /* Update window_end_pos etc.; last_reused_text_row is the last
13764 reused row from the current matrix containing text, if any.
13765 The value of last_text_row is the last displayed line
13766 containing text. */
13767 if (last_reused_text_row)
13768 {
13769 w->window_end_bytepos
13770 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
13771 w->window_end_pos
13772 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
13773 w->window_end_vpos
13774 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
13775 w->current_matrix));
13776 }
13777 else if (last_text_row)
13778 {
13779 w->window_end_bytepos
13780 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13781 w->window_end_pos
13782 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13783 w->window_end_vpos
13784 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13785 }
13786 else
13787 {
13788 /* This window must be completely empty. */
13789 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
13790 w->window_end_pos = make_number (Z - ZV);
13791 w->window_end_vpos = make_number (0);
13792 }
13793 w->window_end_valid = Qnil;
13794
13795 /* Update hint: don't try scrolling again in update_window. */
13796 w->desired_matrix->no_scrolling_p = 1;
13797
13798 #if GLYPH_DEBUG
13799 debug_method_add (w, "try_window_reusing_current_matrix 1");
13800 #endif
13801 return 1;
13802 }
13803 else if (CHARPOS (new_start) > CHARPOS (start))
13804 {
13805 struct glyph_row *pt_row, *row;
13806 struct glyph_row *first_reusable_row;
13807 struct glyph_row *first_row_to_display;
13808 int dy;
13809 int yb = window_text_bottom_y (w);
13810
13811 /* Find the row starting at new_start, if there is one. Don't
13812 reuse a partially visible line at the end. */
13813 first_reusable_row = start_row;
13814 while (first_reusable_row->enabled_p
13815 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
13816 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13817 < CHARPOS (new_start)))
13818 ++first_reusable_row;
13819
13820 /* Give up if there is no row to reuse. */
13821 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
13822 || !first_reusable_row->enabled_p
13823 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
13824 != CHARPOS (new_start)))
13825 return 0;
13826
13827 /* We can reuse fully visible rows beginning with
13828 first_reusable_row to the end of the window. Set
13829 first_row_to_display to the first row that cannot be reused.
13830 Set pt_row to the row containing point, if there is any. */
13831 pt_row = NULL;
13832 for (first_row_to_display = first_reusable_row;
13833 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
13834 ++first_row_to_display)
13835 {
13836 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
13837 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
13838 pt_row = first_row_to_display;
13839 }
13840
13841 /* Start displaying at the start of first_row_to_display. */
13842 xassert (first_row_to_display->y < yb);
13843 init_to_row_start (&it, w, first_row_to_display);
13844
13845 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
13846 - start_vpos);
13847 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
13848 - nrows_scrolled);
13849 it.current_y = (first_row_to_display->y - first_reusable_row->y
13850 + WINDOW_HEADER_LINE_HEIGHT (w));
13851
13852 /* Display lines beginning with first_row_to_display in the
13853 desired matrix. Set last_text_row to the last row displayed
13854 that displays text. */
13855 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
13856 if (pt_row == NULL)
13857 w->cursor.vpos = -1;
13858 last_text_row = NULL;
13859 while (it.current_y < it.last_visible_y && !fonts_changed_p)
13860 if (display_line (&it))
13861 last_text_row = it.glyph_row - 1;
13862
13863 /* Give up If point isn't in a row displayed or reused. */
13864 if (w->cursor.vpos < 0)
13865 {
13866 clear_glyph_matrix (w->desired_matrix);
13867 return 0;
13868 }
13869
13870 /* If point is in a reused row, adjust y and vpos of the cursor
13871 position. */
13872 if (pt_row)
13873 {
13874 w->cursor.vpos -= nrows_scrolled;
13875 w->cursor.y -= first_reusable_row->y - start_row->y;
13876 }
13877
13878 /* Scroll the display. */
13879 run.current_y = first_reusable_row->y;
13880 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
13881 run.height = it.last_visible_y - run.current_y;
13882 dy = run.current_y - run.desired_y;
13883
13884 if (run.height)
13885 {
13886 update_begin (f);
13887 rif->update_window_begin_hook (w);
13888 rif->clear_window_mouse_face (w);
13889 rif->scroll_run_hook (w, &run);
13890 rif->update_window_end_hook (w, 0, 0);
13891 update_end (f);
13892 }
13893
13894 /* Adjust Y positions of reused rows. */
13895 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
13896 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
13897 max_y = it.last_visible_y;
13898 for (row = first_reusable_row; row < first_row_to_display; ++row)
13899 {
13900 row->y -= dy;
13901 row->visible_height = row->height;
13902 if (row->y < min_y)
13903 row->visible_height -= min_y - row->y;
13904 if (row->y + row->height > max_y)
13905 row->visible_height -= row->y + row->height - max_y;
13906 row->redraw_fringe_bitmaps_p = 1;
13907 }
13908
13909 /* Scroll the current matrix. */
13910 xassert (nrows_scrolled > 0);
13911 rotate_matrix (w->current_matrix,
13912 start_vpos,
13913 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
13914 -nrows_scrolled);
13915
13916 /* Disable rows not reused. */
13917 for (row -= nrows_scrolled; row < bottom_row; ++row)
13918 row->enabled_p = 0;
13919
13920 /* Point may have moved to a different line, so we cannot assume that
13921 the previous cursor position is valid; locate the correct row. */
13922 if (pt_row)
13923 {
13924 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
13925 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
13926 row++)
13927 {
13928 w->cursor.vpos++;
13929 w->cursor.y = row->y;
13930 }
13931 if (row < bottom_row)
13932 {
13933 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
13934 while (glyph->charpos < PT)
13935 {
13936 w->cursor.hpos++;
13937 w->cursor.x += glyph->pixel_width;
13938 glyph++;
13939 }
13940 }
13941 }
13942
13943 /* Adjust window end. A null value of last_text_row means that
13944 the window end is in reused rows which in turn means that
13945 only its vpos can have changed. */
13946 if (last_text_row)
13947 {
13948 w->window_end_bytepos
13949 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
13950 w->window_end_pos
13951 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
13952 w->window_end_vpos
13953 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
13954 }
13955 else
13956 {
13957 w->window_end_vpos
13958 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
13959 }
13960
13961 w->window_end_valid = Qnil;
13962 w->desired_matrix->no_scrolling_p = 1;
13963
13964 #if GLYPH_DEBUG
13965 debug_method_add (w, "try_window_reusing_current_matrix 2");
13966 #endif
13967 return 1;
13968 }
13969
13970 return 0;
13971 }
13972
13973
13974 \f
13975 /************************************************************************
13976 Window redisplay reusing current matrix when buffer has changed
13977 ************************************************************************/
13978
13979 static struct glyph_row *find_last_unchanged_at_beg_row P_ ((struct window *));
13980 static struct glyph_row *find_first_unchanged_at_end_row P_ ((struct window *,
13981 int *, int *));
13982 static struct glyph_row *
13983 find_last_row_displaying_text P_ ((struct glyph_matrix *, struct it *,
13984 struct glyph_row *));
13985
13986
13987 /* Return the last row in MATRIX displaying text. If row START is
13988 non-null, start searching with that row. IT gives the dimensions
13989 of the display. Value is null if matrix is empty; otherwise it is
13990 a pointer to the row found. */
13991
13992 static struct glyph_row *
13993 find_last_row_displaying_text (matrix, it, start)
13994 struct glyph_matrix *matrix;
13995 struct it *it;
13996 struct glyph_row *start;
13997 {
13998 struct glyph_row *row, *row_found;
13999
14000 /* Set row_found to the last row in IT->w's current matrix
14001 displaying text. The loop looks funny but think of partially
14002 visible lines. */
14003 row_found = NULL;
14004 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
14005 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14006 {
14007 xassert (row->enabled_p);
14008 row_found = row;
14009 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
14010 break;
14011 ++row;
14012 }
14013
14014 return row_found;
14015 }
14016
14017
14018 /* Return the last row in the current matrix of W that is not affected
14019 by changes at the start of current_buffer that occurred since W's
14020 current matrix was built. Value is null if no such row exists.
14021
14022 BEG_UNCHANGED us the number of characters unchanged at the start of
14023 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
14024 first changed character in current_buffer. Characters at positions <
14025 BEG + BEG_UNCHANGED are at the same buffer positions as they were
14026 when the current matrix was built. */
14027
14028 static struct glyph_row *
14029 find_last_unchanged_at_beg_row (w)
14030 struct window *w;
14031 {
14032 int first_changed_pos = BEG + BEG_UNCHANGED;
14033 struct glyph_row *row;
14034 struct glyph_row *row_found = NULL;
14035 int yb = window_text_bottom_y (w);
14036
14037 /* Find the last row displaying unchanged text. */
14038 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14039 while (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14040 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos)
14041 {
14042 if (/* If row ends before first_changed_pos, it is unchanged,
14043 except in some case. */
14044 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
14045 /* When row ends in ZV and we write at ZV it is not
14046 unchanged. */
14047 && !row->ends_at_zv_p
14048 /* When first_changed_pos is the end of a continued line,
14049 row is not unchanged because it may be no longer
14050 continued. */
14051 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
14052 && (row->continued_p
14053 || row->exact_window_width_line_p)))
14054 row_found = row;
14055
14056 /* Stop if last visible row. */
14057 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
14058 break;
14059
14060 ++row;
14061 }
14062
14063 return row_found;
14064 }
14065
14066
14067 /* Find the first glyph row in the current matrix of W that is not
14068 affected by changes at the end of current_buffer since the
14069 time W's current matrix was built.
14070
14071 Return in *DELTA the number of chars by which buffer positions in
14072 unchanged text at the end of current_buffer must be adjusted.
14073
14074 Return in *DELTA_BYTES the corresponding number of bytes.
14075
14076 Value is null if no such row exists, i.e. all rows are affected by
14077 changes. */
14078
14079 static struct glyph_row *
14080 find_first_unchanged_at_end_row (w, delta, delta_bytes)
14081 struct window *w;
14082 int *delta, *delta_bytes;
14083 {
14084 struct glyph_row *row;
14085 struct glyph_row *row_found = NULL;
14086
14087 *delta = *delta_bytes = 0;
14088
14089 /* Display must not have been paused, otherwise the current matrix
14090 is not up to date. */
14091 if (NILP (w->window_end_valid))
14092 abort ();
14093
14094 /* A value of window_end_pos >= END_UNCHANGED means that the window
14095 end is in the range of changed text. If so, there is no
14096 unchanged row at the end of W's current matrix. */
14097 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
14098 return NULL;
14099
14100 /* Set row to the last row in W's current matrix displaying text. */
14101 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14102
14103 /* If matrix is entirely empty, no unchanged row exists. */
14104 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14105 {
14106 /* The value of row is the last glyph row in the matrix having a
14107 meaningful buffer position in it. The end position of row
14108 corresponds to window_end_pos. This allows us to translate
14109 buffer positions in the current matrix to current buffer
14110 positions for characters not in changed text. */
14111 int Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14112 int Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14113 int last_unchanged_pos, last_unchanged_pos_old;
14114 struct glyph_row *first_text_row
14115 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14116
14117 *delta = Z - Z_old;
14118 *delta_bytes = Z_BYTE - Z_BYTE_old;
14119
14120 /* Set last_unchanged_pos to the buffer position of the last
14121 character in the buffer that has not been changed. Z is the
14122 index + 1 of the last character in current_buffer, i.e. by
14123 subtracting END_UNCHANGED we get the index of the last
14124 unchanged character, and we have to add BEG to get its buffer
14125 position. */
14126 last_unchanged_pos = Z - END_UNCHANGED + BEG;
14127 last_unchanged_pos_old = last_unchanged_pos - *delta;
14128
14129 /* Search backward from ROW for a row displaying a line that
14130 starts at a minimum position >= last_unchanged_pos_old. */
14131 for (; row > first_text_row; --row)
14132 {
14133 /* This used to abort, but it can happen.
14134 It is ok to just stop the search instead here. KFS. */
14135 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
14136 break;
14137
14138 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
14139 row_found = row;
14140 }
14141 }
14142
14143 if (row_found && !MATRIX_ROW_DISPLAYS_TEXT_P (row_found))
14144 abort ();
14145
14146 return row_found;
14147 }
14148
14149
14150 /* Make sure that glyph rows in the current matrix of window W
14151 reference the same glyph memory as corresponding rows in the
14152 frame's frame matrix. This function is called after scrolling W's
14153 current matrix on a terminal frame in try_window_id and
14154 try_window_reusing_current_matrix. */
14155
14156 static void
14157 sync_frame_with_window_matrix_rows (w)
14158 struct window *w;
14159 {
14160 struct frame *f = XFRAME (w->frame);
14161 struct glyph_row *window_row, *window_row_end, *frame_row;
14162
14163 /* Preconditions: W must be a leaf window and full-width. Its frame
14164 must have a frame matrix. */
14165 xassert (NILP (w->hchild) && NILP (w->vchild));
14166 xassert (WINDOW_FULL_WIDTH_P (w));
14167 xassert (!FRAME_WINDOW_P (f));
14168
14169 /* If W is a full-width window, glyph pointers in W's current matrix
14170 have, by definition, to be the same as glyph pointers in the
14171 corresponding frame matrix. Note that frame matrices have no
14172 marginal areas (see build_frame_matrix). */
14173 window_row = w->current_matrix->rows;
14174 window_row_end = window_row + w->current_matrix->nrows;
14175 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
14176 while (window_row < window_row_end)
14177 {
14178 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
14179 struct glyph *end = window_row->glyphs[LAST_AREA];
14180
14181 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
14182 frame_row->glyphs[TEXT_AREA] = start;
14183 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
14184 frame_row->glyphs[LAST_AREA] = end;
14185
14186 /* Disable frame rows whose corresponding window rows have
14187 been disabled in try_window_id. */
14188 if (!window_row->enabled_p)
14189 frame_row->enabled_p = 0;
14190
14191 ++window_row, ++frame_row;
14192 }
14193 }
14194
14195
14196 /* Find the glyph row in window W containing CHARPOS. Consider all
14197 rows between START and END (not inclusive). END null means search
14198 all rows to the end of the display area of W. Value is the row
14199 containing CHARPOS or null. */
14200
14201 struct glyph_row *
14202 row_containing_pos (w, charpos, start, end, dy)
14203 struct window *w;
14204 int charpos;
14205 struct glyph_row *start, *end;
14206 int dy;
14207 {
14208 struct glyph_row *row = start;
14209 int last_y;
14210
14211 /* If we happen to start on a header-line, skip that. */
14212 if (row->mode_line_p)
14213 ++row;
14214
14215 if ((end && row >= end) || !row->enabled_p)
14216 return NULL;
14217
14218 last_y = window_text_bottom_y (w) - dy;
14219
14220 while (1)
14221 {
14222 /* Give up if we have gone too far. */
14223 if (end && row >= end)
14224 return NULL;
14225 /* This formerly returned if they were equal.
14226 I think that both quantities are of a "last plus one" type;
14227 if so, when they are equal, the row is within the screen. -- rms. */
14228 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
14229 return NULL;
14230
14231 /* If it is in this row, return this row. */
14232 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
14233 || (MATRIX_ROW_END_CHARPOS (row) == charpos
14234 /* The end position of a row equals the start
14235 position of the next row. If CHARPOS is there, we
14236 would rather display it in the next line, except
14237 when this line ends in ZV. */
14238 && !row->ends_at_zv_p
14239 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14240 && charpos >= MATRIX_ROW_START_CHARPOS (row))
14241 return row;
14242 ++row;
14243 }
14244 }
14245
14246
14247 /* Try to redisplay window W by reusing its existing display. W's
14248 current matrix must be up to date when this function is called,
14249 i.e. window_end_valid must not be nil.
14250
14251 Value is
14252
14253 1 if display has been updated
14254 0 if otherwise unsuccessful
14255 -1 if redisplay with same window start is known not to succeed
14256
14257 The following steps are performed:
14258
14259 1. Find the last row in the current matrix of W that is not
14260 affected by changes at the start of current_buffer. If no such row
14261 is found, give up.
14262
14263 2. Find the first row in W's current matrix that is not affected by
14264 changes at the end of current_buffer. Maybe there is no such row.
14265
14266 3. Display lines beginning with the row + 1 found in step 1 to the
14267 row found in step 2 or, if step 2 didn't find a row, to the end of
14268 the window.
14269
14270 4. If cursor is not known to appear on the window, give up.
14271
14272 5. If display stopped at the row found in step 2, scroll the
14273 display and current matrix as needed.
14274
14275 6. Maybe display some lines at the end of W, if we must. This can
14276 happen under various circumstances, like a partially visible line
14277 becoming fully visible, or because newly displayed lines are displayed
14278 in smaller font sizes.
14279
14280 7. Update W's window end information. */
14281
14282 static int
14283 try_window_id (w)
14284 struct window *w;
14285 {
14286 struct frame *f = XFRAME (w->frame);
14287 struct glyph_matrix *current_matrix = w->current_matrix;
14288 struct glyph_matrix *desired_matrix = w->desired_matrix;
14289 struct glyph_row *last_unchanged_at_beg_row;
14290 struct glyph_row *first_unchanged_at_end_row;
14291 struct glyph_row *row;
14292 struct glyph_row *bottom_row;
14293 int bottom_vpos;
14294 struct it it;
14295 int delta = 0, delta_bytes = 0, stop_pos, dvpos, dy;
14296 struct text_pos start_pos;
14297 struct run run;
14298 int first_unchanged_at_end_vpos = 0;
14299 struct glyph_row *last_text_row, *last_text_row_at_end;
14300 struct text_pos start;
14301 int first_changed_charpos, last_changed_charpos;
14302
14303 #if GLYPH_DEBUG
14304 if (inhibit_try_window_id)
14305 return 0;
14306 #endif
14307
14308 /* This is handy for debugging. */
14309 #if 0
14310 #define GIVE_UP(X) \
14311 do { \
14312 fprintf (stderr, "try_window_id give up %d\n", (X)); \
14313 return 0; \
14314 } while (0)
14315 #else
14316 #define GIVE_UP(X) return 0
14317 #endif
14318
14319 SET_TEXT_POS_FROM_MARKER (start, w->start);
14320
14321 /* Don't use this for mini-windows because these can show
14322 messages and mini-buffers, and we don't handle that here. */
14323 if (MINI_WINDOW_P (w))
14324 GIVE_UP (1);
14325
14326 /* This flag is used to prevent redisplay optimizations. */
14327 if (windows_or_buffers_changed || cursor_type_changed)
14328 GIVE_UP (2);
14329
14330 /* Verify that narrowing has not changed.
14331 Also verify that we were not told to prevent redisplay optimizations.
14332 It would be nice to further
14333 reduce the number of cases where this prevents try_window_id. */
14334 if (current_buffer->clip_changed
14335 || current_buffer->prevent_redisplay_optimizations_p)
14336 GIVE_UP (3);
14337
14338 /* Window must either use window-based redisplay or be full width. */
14339 if (!FRAME_WINDOW_P (f)
14340 && (!line_ins_del_ok
14341 || !WINDOW_FULL_WIDTH_P (w)))
14342 GIVE_UP (4);
14343
14344 /* Give up if point is not known NOT to appear in W. */
14345 if (PT < CHARPOS (start))
14346 GIVE_UP (5);
14347
14348 /* Another way to prevent redisplay optimizations. */
14349 if (XFASTINT (w->last_modified) == 0)
14350 GIVE_UP (6);
14351
14352 /* Verify that window is not hscrolled. */
14353 if (XFASTINT (w->hscroll) != 0)
14354 GIVE_UP (7);
14355
14356 /* Verify that display wasn't paused. */
14357 if (NILP (w->window_end_valid))
14358 GIVE_UP (8);
14359
14360 /* Can't use this if highlighting a region because a cursor movement
14361 will do more than just set the cursor. */
14362 if (!NILP (Vtransient_mark_mode)
14363 && !NILP (current_buffer->mark_active))
14364 GIVE_UP (9);
14365
14366 /* Likewise if highlighting trailing whitespace. */
14367 if (!NILP (Vshow_trailing_whitespace))
14368 GIVE_UP (11);
14369
14370 /* Likewise if showing a region. */
14371 if (!NILP (w->region_showing))
14372 GIVE_UP (10);
14373
14374 /* Can use this if overlay arrow position and or string have changed. */
14375 if (overlay_arrows_changed_p ())
14376 GIVE_UP (12);
14377
14378
14379 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
14380 only if buffer has really changed. The reason is that the gap is
14381 initially at Z for freshly visited files. The code below would
14382 set end_unchanged to 0 in that case. */
14383 if (MODIFF > SAVE_MODIFF
14384 /* This seems to happen sometimes after saving a buffer. */
14385 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
14386 {
14387 if (GPT - BEG < BEG_UNCHANGED)
14388 BEG_UNCHANGED = GPT - BEG;
14389 if (Z - GPT < END_UNCHANGED)
14390 END_UNCHANGED = Z - GPT;
14391 }
14392
14393 /* The position of the first and last character that has been changed. */
14394 first_changed_charpos = BEG + BEG_UNCHANGED;
14395 last_changed_charpos = Z - END_UNCHANGED;
14396
14397 /* If window starts after a line end, and the last change is in
14398 front of that newline, then changes don't affect the display.
14399 This case happens with stealth-fontification. Note that although
14400 the display is unchanged, glyph positions in the matrix have to
14401 be adjusted, of course. */
14402 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
14403 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
14404 && ((last_changed_charpos < CHARPOS (start)
14405 && CHARPOS (start) == BEGV)
14406 || (last_changed_charpos < CHARPOS (start) - 1
14407 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
14408 {
14409 int Z_old, delta, Z_BYTE_old, delta_bytes;
14410 struct glyph_row *r0;
14411
14412 /* Compute how many chars/bytes have been added to or removed
14413 from the buffer. */
14414 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
14415 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
14416 delta = Z - Z_old;
14417 delta_bytes = Z_BYTE - Z_BYTE_old;
14418
14419 /* Give up if PT is not in the window. Note that it already has
14420 been checked at the start of try_window_id that PT is not in
14421 front of the window start. */
14422 if (PT >= MATRIX_ROW_END_CHARPOS (row) + delta)
14423 GIVE_UP (13);
14424
14425 /* If window start is unchanged, we can reuse the whole matrix
14426 as is, after adjusting glyph positions. No need to compute
14427 the window end again, since its offset from Z hasn't changed. */
14428 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14429 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + delta
14430 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + delta_bytes
14431 /* PT must not be in a partially visible line. */
14432 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + delta
14433 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14434 {
14435 /* Adjust positions in the glyph matrix. */
14436 if (delta || delta_bytes)
14437 {
14438 struct glyph_row *r1
14439 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14440 increment_matrix_positions (w->current_matrix,
14441 MATRIX_ROW_VPOS (r0, current_matrix),
14442 MATRIX_ROW_VPOS (r1, current_matrix),
14443 delta, delta_bytes);
14444 }
14445
14446 /* Set the cursor. */
14447 row = row_containing_pos (w, PT, r0, NULL, 0);
14448 if (row)
14449 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14450 else
14451 abort ();
14452 return 1;
14453 }
14454 }
14455
14456 /* Handle the case that changes are all below what is displayed in
14457 the window, and that PT is in the window. This shortcut cannot
14458 be taken if ZV is visible in the window, and text has been added
14459 there that is visible in the window. */
14460 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
14461 /* ZV is not visible in the window, or there are no
14462 changes at ZV, actually. */
14463 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
14464 || first_changed_charpos == last_changed_charpos))
14465 {
14466 struct glyph_row *r0;
14467
14468 /* Give up if PT is not in the window. Note that it already has
14469 been checked at the start of try_window_id that PT is not in
14470 front of the window start. */
14471 if (PT >= MATRIX_ROW_END_CHARPOS (row))
14472 GIVE_UP (14);
14473
14474 /* If window start is unchanged, we can reuse the whole matrix
14475 as is, without changing glyph positions since no text has
14476 been added/removed in front of the window end. */
14477 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
14478 if (TEXT_POS_EQUAL_P (start, r0->start.pos)
14479 /* PT must not be in a partially visible line. */
14480 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
14481 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
14482 {
14483 /* We have to compute the window end anew since text
14484 can have been added/removed after it. */
14485 w->window_end_pos
14486 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14487 w->window_end_bytepos
14488 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14489
14490 /* Set the cursor. */
14491 row = row_containing_pos (w, PT, r0, NULL, 0);
14492 if (row)
14493 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
14494 else
14495 abort ();
14496 return 2;
14497 }
14498 }
14499
14500 /* Give up if window start is in the changed area.
14501
14502 The condition used to read
14503
14504 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
14505
14506 but why that was tested escapes me at the moment. */
14507 if (CHARPOS (start) >= first_changed_charpos
14508 && CHARPOS (start) <= last_changed_charpos)
14509 GIVE_UP (15);
14510
14511 /* Check that window start agrees with the start of the first glyph
14512 row in its current matrix. Check this after we know the window
14513 start is not in changed text, otherwise positions would not be
14514 comparable. */
14515 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
14516 if (!TEXT_POS_EQUAL_P (start, row->start.pos))
14517 GIVE_UP (16);
14518
14519 /* Give up if the window ends in strings. Overlay strings
14520 at the end are difficult to handle, so don't try. */
14521 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
14522 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
14523 GIVE_UP (20);
14524
14525 /* Compute the position at which we have to start displaying new
14526 lines. Some of the lines at the top of the window might be
14527 reusable because they are not displaying changed text. Find the
14528 last row in W's current matrix not affected by changes at the
14529 start of current_buffer. Value is null if changes start in the
14530 first line of window. */
14531 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
14532 if (last_unchanged_at_beg_row)
14533 {
14534 /* Avoid starting to display in the moddle of a character, a TAB
14535 for instance. This is easier than to set up the iterator
14536 exactly, and it's not a frequent case, so the additional
14537 effort wouldn't really pay off. */
14538 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
14539 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
14540 && last_unchanged_at_beg_row > w->current_matrix->rows)
14541 --last_unchanged_at_beg_row;
14542
14543 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
14544 GIVE_UP (17);
14545
14546 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
14547 GIVE_UP (18);
14548 start_pos = it.current.pos;
14549
14550 /* Start displaying new lines in the desired matrix at the same
14551 vpos we would use in the current matrix, i.e. below
14552 last_unchanged_at_beg_row. */
14553 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
14554 current_matrix);
14555 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14556 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
14557
14558 xassert (it.hpos == 0 && it.current_x == 0);
14559 }
14560 else
14561 {
14562 /* There are no reusable lines at the start of the window.
14563 Start displaying in the first text line. */
14564 start_display (&it, w, start);
14565 it.vpos = it.first_vpos;
14566 start_pos = it.current.pos;
14567 }
14568
14569 /* Find the first row that is not affected by changes at the end of
14570 the buffer. Value will be null if there is no unchanged row, in
14571 which case we must redisplay to the end of the window. delta
14572 will be set to the value by which buffer positions beginning with
14573 first_unchanged_at_end_row have to be adjusted due to text
14574 changes. */
14575 first_unchanged_at_end_row
14576 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
14577 IF_DEBUG (debug_delta = delta);
14578 IF_DEBUG (debug_delta_bytes = delta_bytes);
14579
14580 /* Set stop_pos to the buffer position up to which we will have to
14581 display new lines. If first_unchanged_at_end_row != NULL, this
14582 is the buffer position of the start of the line displayed in that
14583 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
14584 that we don't stop at a buffer position. */
14585 stop_pos = 0;
14586 if (first_unchanged_at_end_row)
14587 {
14588 xassert (last_unchanged_at_beg_row == NULL
14589 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
14590
14591 /* If this is a continuation line, move forward to the next one
14592 that isn't. Changes in lines above affect this line.
14593 Caution: this may move first_unchanged_at_end_row to a row
14594 not displaying text. */
14595 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
14596 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14597 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14598 < it.last_visible_y))
14599 ++first_unchanged_at_end_row;
14600
14601 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
14602 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
14603 >= it.last_visible_y))
14604 first_unchanged_at_end_row = NULL;
14605 else
14606 {
14607 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
14608 + delta);
14609 first_unchanged_at_end_vpos
14610 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
14611 xassert (stop_pos >= Z - END_UNCHANGED);
14612 }
14613 }
14614 else if (last_unchanged_at_beg_row == NULL)
14615 GIVE_UP (19);
14616
14617
14618 #if GLYPH_DEBUG
14619
14620 /* Either there is no unchanged row at the end, or the one we have
14621 now displays text. This is a necessary condition for the window
14622 end pos calculation at the end of this function. */
14623 xassert (first_unchanged_at_end_row == NULL
14624 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
14625
14626 debug_last_unchanged_at_beg_vpos
14627 = (last_unchanged_at_beg_row
14628 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
14629 : -1);
14630 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
14631
14632 #endif /* GLYPH_DEBUG != 0 */
14633
14634
14635 /* Display new lines. Set last_text_row to the last new line
14636 displayed which has text on it, i.e. might end up as being the
14637 line where the window_end_vpos is. */
14638 w->cursor.vpos = -1;
14639 last_text_row = NULL;
14640 overlay_arrow_seen = 0;
14641 while (it.current_y < it.last_visible_y
14642 && !fonts_changed_p
14643 && (first_unchanged_at_end_row == NULL
14644 || IT_CHARPOS (it) < stop_pos))
14645 {
14646 if (display_line (&it))
14647 last_text_row = it.glyph_row - 1;
14648 }
14649
14650 if (fonts_changed_p)
14651 return -1;
14652
14653
14654 /* Compute differences in buffer positions, y-positions etc. for
14655 lines reused at the bottom of the window. Compute what we can
14656 scroll. */
14657 if (first_unchanged_at_end_row
14658 /* No lines reused because we displayed everything up to the
14659 bottom of the window. */
14660 && it.current_y < it.last_visible_y)
14661 {
14662 dvpos = (it.vpos
14663 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
14664 current_matrix));
14665 dy = it.current_y - first_unchanged_at_end_row->y;
14666 run.current_y = first_unchanged_at_end_row->y;
14667 run.desired_y = run.current_y + dy;
14668 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
14669 }
14670 else
14671 {
14672 delta = dvpos = dy = run.current_y = run.desired_y = run.height = 0;
14673 first_unchanged_at_end_row = NULL;
14674 }
14675 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
14676
14677
14678 /* Find the cursor if not already found. We have to decide whether
14679 PT will appear on this window (it sometimes doesn't, but this is
14680 not a very frequent case.) This decision has to be made before
14681 the current matrix is altered. A value of cursor.vpos < 0 means
14682 that PT is either in one of the lines beginning at
14683 first_unchanged_at_end_row or below the window. Don't care for
14684 lines that might be displayed later at the window end; as
14685 mentioned, this is not a frequent case. */
14686 if (w->cursor.vpos < 0)
14687 {
14688 /* Cursor in unchanged rows at the top? */
14689 if (PT < CHARPOS (start_pos)
14690 && last_unchanged_at_beg_row)
14691 {
14692 row = row_containing_pos (w, PT,
14693 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
14694 last_unchanged_at_beg_row + 1, 0);
14695 if (row)
14696 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14697 }
14698
14699 /* Start from first_unchanged_at_end_row looking for PT. */
14700 else if (first_unchanged_at_end_row)
14701 {
14702 row = row_containing_pos (w, PT - delta,
14703 first_unchanged_at_end_row, NULL, 0);
14704 if (row)
14705 set_cursor_from_row (w, row, w->current_matrix, delta,
14706 delta_bytes, dy, dvpos);
14707 }
14708
14709 /* Give up if cursor was not found. */
14710 if (w->cursor.vpos < 0)
14711 {
14712 clear_glyph_matrix (w->desired_matrix);
14713 return -1;
14714 }
14715 }
14716
14717 /* Don't let the cursor end in the scroll margins. */
14718 {
14719 int this_scroll_margin, cursor_height;
14720
14721 this_scroll_margin = max (0, scroll_margin);
14722 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14723 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
14724 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
14725
14726 if ((w->cursor.y < this_scroll_margin
14727 && CHARPOS (start) > BEGV)
14728 /* Old redisplay didn't take scroll margin into account at the bottom,
14729 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
14730 || (w->cursor.y + (make_cursor_line_fully_visible_p
14731 ? cursor_height + this_scroll_margin
14732 : 1)) > it.last_visible_y)
14733 {
14734 w->cursor.vpos = -1;
14735 clear_glyph_matrix (w->desired_matrix);
14736 return -1;
14737 }
14738 }
14739
14740 /* Scroll the display. Do it before changing the current matrix so
14741 that xterm.c doesn't get confused about where the cursor glyph is
14742 found. */
14743 if (dy && run.height)
14744 {
14745 update_begin (f);
14746
14747 if (FRAME_WINDOW_P (f))
14748 {
14749 rif->update_window_begin_hook (w);
14750 rif->clear_window_mouse_face (w);
14751 rif->scroll_run_hook (w, &run);
14752 rif->update_window_end_hook (w, 0, 0);
14753 }
14754 else
14755 {
14756 /* Terminal frame. In this case, dvpos gives the number of
14757 lines to scroll by; dvpos < 0 means scroll up. */
14758 int first_unchanged_at_end_vpos
14759 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
14760 int from = WINDOW_TOP_EDGE_LINE (w) + first_unchanged_at_end_vpos;
14761 int end = (WINDOW_TOP_EDGE_LINE (w)
14762 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
14763 + window_internal_height (w));
14764
14765 /* Perform the operation on the screen. */
14766 if (dvpos > 0)
14767 {
14768 /* Scroll last_unchanged_at_beg_row to the end of the
14769 window down dvpos lines. */
14770 set_terminal_window (end);
14771
14772 /* On dumb terminals delete dvpos lines at the end
14773 before inserting dvpos empty lines. */
14774 if (!scroll_region_ok)
14775 ins_del_lines (end - dvpos, -dvpos);
14776
14777 /* Insert dvpos empty lines in front of
14778 last_unchanged_at_beg_row. */
14779 ins_del_lines (from, dvpos);
14780 }
14781 else if (dvpos < 0)
14782 {
14783 /* Scroll up last_unchanged_at_beg_vpos to the end of
14784 the window to last_unchanged_at_beg_vpos - |dvpos|. */
14785 set_terminal_window (end);
14786
14787 /* Delete dvpos lines in front of
14788 last_unchanged_at_beg_vpos. ins_del_lines will set
14789 the cursor to the given vpos and emit |dvpos| delete
14790 line sequences. */
14791 ins_del_lines (from + dvpos, dvpos);
14792
14793 /* On a dumb terminal insert dvpos empty lines at the
14794 end. */
14795 if (!scroll_region_ok)
14796 ins_del_lines (end + dvpos, -dvpos);
14797 }
14798
14799 set_terminal_window (0);
14800 }
14801
14802 update_end (f);
14803 }
14804
14805 /* Shift reused rows of the current matrix to the right position.
14806 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
14807 text. */
14808 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
14809 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
14810 if (dvpos < 0)
14811 {
14812 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
14813 bottom_vpos, dvpos);
14814 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
14815 bottom_vpos, 0);
14816 }
14817 else if (dvpos > 0)
14818 {
14819 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
14820 bottom_vpos, dvpos);
14821 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
14822 first_unchanged_at_end_vpos + dvpos, 0);
14823 }
14824
14825 /* For frame-based redisplay, make sure that current frame and window
14826 matrix are in sync with respect to glyph memory. */
14827 if (!FRAME_WINDOW_P (f))
14828 sync_frame_with_window_matrix_rows (w);
14829
14830 /* Adjust buffer positions in reused rows. */
14831 if (delta)
14832 increment_matrix_positions (current_matrix,
14833 first_unchanged_at_end_vpos + dvpos,
14834 bottom_vpos, delta, delta_bytes);
14835
14836 /* Adjust Y positions. */
14837 if (dy)
14838 shift_glyph_matrix (w, current_matrix,
14839 first_unchanged_at_end_vpos + dvpos,
14840 bottom_vpos, dy);
14841
14842 if (first_unchanged_at_end_row)
14843 {
14844 first_unchanged_at_end_row += dvpos;
14845 if (first_unchanged_at_end_row->y >= it.last_visible_y
14846 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
14847 first_unchanged_at_end_row = NULL;
14848 }
14849
14850 /* If scrolling up, there may be some lines to display at the end of
14851 the window. */
14852 last_text_row_at_end = NULL;
14853 if (dy < 0)
14854 {
14855 /* Scrolling up can leave for example a partially visible line
14856 at the end of the window to be redisplayed. */
14857 /* Set last_row to the glyph row in the current matrix where the
14858 window end line is found. It has been moved up or down in
14859 the matrix by dvpos. */
14860 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
14861 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
14862
14863 /* If last_row is the window end line, it should display text. */
14864 xassert (last_row->displays_text_p);
14865
14866 /* If window end line was partially visible before, begin
14867 displaying at that line. Otherwise begin displaying with the
14868 line following it. */
14869 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
14870 {
14871 init_to_row_start (&it, w, last_row);
14872 it.vpos = last_vpos;
14873 it.current_y = last_row->y;
14874 }
14875 else
14876 {
14877 init_to_row_end (&it, w, last_row);
14878 it.vpos = 1 + last_vpos;
14879 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
14880 ++last_row;
14881 }
14882
14883 /* We may start in a continuation line. If so, we have to
14884 get the right continuation_lines_width and current_x. */
14885 it.continuation_lines_width = last_row->continuation_lines_width;
14886 it.hpos = it.current_x = 0;
14887
14888 /* Display the rest of the lines at the window end. */
14889 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
14890 while (it.current_y < it.last_visible_y
14891 && !fonts_changed_p)
14892 {
14893 /* Is it always sure that the display agrees with lines in
14894 the current matrix? I don't think so, so we mark rows
14895 displayed invalid in the current matrix by setting their
14896 enabled_p flag to zero. */
14897 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
14898 if (display_line (&it))
14899 last_text_row_at_end = it.glyph_row - 1;
14900 }
14901 }
14902
14903 /* Update window_end_pos and window_end_vpos. */
14904 if (first_unchanged_at_end_row
14905 && !last_text_row_at_end)
14906 {
14907 /* Window end line if one of the preserved rows from the current
14908 matrix. Set row to the last row displaying text in current
14909 matrix starting at first_unchanged_at_end_row, after
14910 scrolling. */
14911 xassert (first_unchanged_at_end_row->displays_text_p);
14912 row = find_last_row_displaying_text (w->current_matrix, &it,
14913 first_unchanged_at_end_row);
14914 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
14915
14916 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14917 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14918 w->window_end_vpos
14919 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
14920 xassert (w->window_end_bytepos >= 0);
14921 IF_DEBUG (debug_method_add (w, "A"));
14922 }
14923 else if (last_text_row_at_end)
14924 {
14925 w->window_end_pos
14926 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
14927 w->window_end_bytepos
14928 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
14929 w->window_end_vpos
14930 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
14931 xassert (w->window_end_bytepos >= 0);
14932 IF_DEBUG (debug_method_add (w, "B"));
14933 }
14934 else if (last_text_row)
14935 {
14936 /* We have displayed either to the end of the window or at the
14937 end of the window, i.e. the last row with text is to be found
14938 in the desired matrix. */
14939 w->window_end_pos
14940 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
14941 w->window_end_bytepos
14942 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
14943 w->window_end_vpos
14944 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
14945 xassert (w->window_end_bytepos >= 0);
14946 }
14947 else if (first_unchanged_at_end_row == NULL
14948 && last_text_row == NULL
14949 && last_text_row_at_end == NULL)
14950 {
14951 /* Displayed to end of window, but no line containing text was
14952 displayed. Lines were deleted at the end of the window. */
14953 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
14954 int vpos = XFASTINT (w->window_end_vpos);
14955 struct glyph_row *current_row = current_matrix->rows + vpos;
14956 struct glyph_row *desired_row = desired_matrix->rows + vpos;
14957
14958 for (row = NULL;
14959 row == NULL && vpos >= first_vpos;
14960 --vpos, --current_row, --desired_row)
14961 {
14962 if (desired_row->enabled_p)
14963 {
14964 if (desired_row->displays_text_p)
14965 row = desired_row;
14966 }
14967 else if (current_row->displays_text_p)
14968 row = current_row;
14969 }
14970
14971 xassert (row != NULL);
14972 w->window_end_vpos = make_number (vpos + 1);
14973 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
14974 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
14975 xassert (w->window_end_bytepos >= 0);
14976 IF_DEBUG (debug_method_add (w, "C"));
14977 }
14978 else
14979 abort ();
14980
14981 #if 0 /* This leads to problems, for instance when the cursor is
14982 at ZV, and the cursor line displays no text. */
14983 /* Disable rows below what's displayed in the window. This makes
14984 debugging easier. */
14985 enable_glyph_matrix_rows (current_matrix,
14986 XFASTINT (w->window_end_vpos) + 1,
14987 bottom_vpos, 0);
14988 #endif
14989
14990 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
14991 debug_end_vpos = XFASTINT (w->window_end_vpos));
14992
14993 /* Record that display has not been completed. */
14994 w->window_end_valid = Qnil;
14995 w->desired_matrix->no_scrolling_p = 1;
14996 return 3;
14997
14998 #undef GIVE_UP
14999 }
15000
15001
15002 \f
15003 /***********************************************************************
15004 More debugging support
15005 ***********************************************************************/
15006
15007 #if GLYPH_DEBUG
15008
15009 void dump_glyph_row P_ ((struct glyph_row *, int, int));
15010 void dump_glyph_matrix P_ ((struct glyph_matrix *, int));
15011 void dump_glyph P_ ((struct glyph_row *, struct glyph *, int));
15012
15013
15014 /* Dump the contents of glyph matrix MATRIX on stderr.
15015
15016 GLYPHS 0 means don't show glyph contents.
15017 GLYPHS 1 means show glyphs in short form
15018 GLYPHS > 1 means show glyphs in long form. */
15019
15020 void
15021 dump_glyph_matrix (matrix, glyphs)
15022 struct glyph_matrix *matrix;
15023 int glyphs;
15024 {
15025 int i;
15026 for (i = 0; i < matrix->nrows; ++i)
15027 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
15028 }
15029
15030
15031 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
15032 the glyph row and area where the glyph comes from. */
15033
15034 void
15035 dump_glyph (row, glyph, area)
15036 struct glyph_row *row;
15037 struct glyph *glyph;
15038 int area;
15039 {
15040 if (glyph->type == CHAR_GLYPH)
15041 {
15042 fprintf (stderr,
15043 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15044 glyph - row->glyphs[TEXT_AREA],
15045 'C',
15046 glyph->charpos,
15047 (BUFFERP (glyph->object)
15048 ? 'B'
15049 : (STRINGP (glyph->object)
15050 ? 'S'
15051 : '-')),
15052 glyph->pixel_width,
15053 glyph->u.ch,
15054 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
15055 ? glyph->u.ch
15056 : '.'),
15057 glyph->face_id,
15058 glyph->left_box_line_p,
15059 glyph->right_box_line_p);
15060 }
15061 else if (glyph->type == STRETCH_GLYPH)
15062 {
15063 fprintf (stderr,
15064 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15065 glyph - row->glyphs[TEXT_AREA],
15066 'S',
15067 glyph->charpos,
15068 (BUFFERP (glyph->object)
15069 ? 'B'
15070 : (STRINGP (glyph->object)
15071 ? 'S'
15072 : '-')),
15073 glyph->pixel_width,
15074 0,
15075 '.',
15076 glyph->face_id,
15077 glyph->left_box_line_p,
15078 glyph->right_box_line_p);
15079 }
15080 else if (glyph->type == IMAGE_GLYPH)
15081 {
15082 fprintf (stderr,
15083 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15084 glyph - row->glyphs[TEXT_AREA],
15085 'I',
15086 glyph->charpos,
15087 (BUFFERP (glyph->object)
15088 ? 'B'
15089 : (STRINGP (glyph->object)
15090 ? 'S'
15091 : '-')),
15092 glyph->pixel_width,
15093 glyph->u.img_id,
15094 '.',
15095 glyph->face_id,
15096 glyph->left_box_line_p,
15097 glyph->right_box_line_p);
15098 }
15099 else if (glyph->type == COMPOSITE_GLYPH)
15100 {
15101 fprintf (stderr,
15102 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
15103 glyph - row->glyphs[TEXT_AREA],
15104 '+',
15105 glyph->charpos,
15106 (BUFFERP (glyph->object)
15107 ? 'B'
15108 : (STRINGP (glyph->object)
15109 ? 'S'
15110 : '-')),
15111 glyph->pixel_width,
15112 glyph->u.cmp_id,
15113 '.',
15114 glyph->face_id,
15115 glyph->left_box_line_p,
15116 glyph->right_box_line_p);
15117 }
15118 }
15119
15120
15121 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
15122 GLYPHS 0 means don't show glyph contents.
15123 GLYPHS 1 means show glyphs in short form
15124 GLYPHS > 1 means show glyphs in long form. */
15125
15126 void
15127 dump_glyph_row (row, vpos, glyphs)
15128 struct glyph_row *row;
15129 int vpos, glyphs;
15130 {
15131 if (glyphs != 1)
15132 {
15133 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
15134 fprintf (stderr, "======================================================================\n");
15135
15136 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
15137 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
15138 vpos,
15139 MATRIX_ROW_START_CHARPOS (row),
15140 MATRIX_ROW_END_CHARPOS (row),
15141 row->used[TEXT_AREA],
15142 row->contains_overlapping_glyphs_p,
15143 row->enabled_p,
15144 row->truncated_on_left_p,
15145 row->truncated_on_right_p,
15146 row->continued_p,
15147 MATRIX_ROW_CONTINUATION_LINE_P (row),
15148 row->displays_text_p,
15149 row->ends_at_zv_p,
15150 row->fill_line_p,
15151 row->ends_in_middle_of_char_p,
15152 row->starts_in_middle_of_char_p,
15153 row->mouse_face_p,
15154 row->x,
15155 row->y,
15156 row->pixel_width,
15157 row->height,
15158 row->visible_height,
15159 row->ascent,
15160 row->phys_ascent);
15161 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
15162 row->end.overlay_string_index,
15163 row->continuation_lines_width);
15164 fprintf (stderr, "%9d %5d\n",
15165 CHARPOS (row->start.string_pos),
15166 CHARPOS (row->end.string_pos));
15167 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
15168 row->end.dpvec_index);
15169 }
15170
15171 if (glyphs > 1)
15172 {
15173 int area;
15174
15175 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15176 {
15177 struct glyph *glyph = row->glyphs[area];
15178 struct glyph *glyph_end = glyph + row->used[area];
15179
15180 /* Glyph for a line end in text. */
15181 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
15182 ++glyph_end;
15183
15184 if (glyph < glyph_end)
15185 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
15186
15187 for (; glyph < glyph_end; ++glyph)
15188 dump_glyph (row, glyph, area);
15189 }
15190 }
15191 else if (glyphs == 1)
15192 {
15193 int area;
15194
15195 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15196 {
15197 char *s = (char *) alloca (row->used[area] + 1);
15198 int i;
15199
15200 for (i = 0; i < row->used[area]; ++i)
15201 {
15202 struct glyph *glyph = row->glyphs[area] + i;
15203 if (glyph->type == CHAR_GLYPH
15204 && glyph->u.ch < 0x80
15205 && glyph->u.ch >= ' ')
15206 s[i] = glyph->u.ch;
15207 else
15208 s[i] = '.';
15209 }
15210
15211 s[i] = '\0';
15212 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
15213 }
15214 }
15215 }
15216
15217
15218 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
15219 Sdump_glyph_matrix, 0, 1, "p",
15220 doc: /* Dump the current matrix of the selected window to stderr.
15221 Shows contents of glyph row structures. With non-nil
15222 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
15223 glyphs in short form, otherwise show glyphs in long form. */)
15224 (glyphs)
15225 Lisp_Object glyphs;
15226 {
15227 struct window *w = XWINDOW (selected_window);
15228 struct buffer *buffer = XBUFFER (w->buffer);
15229
15230 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
15231 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
15232 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
15233 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
15234 fprintf (stderr, "=============================================\n");
15235 dump_glyph_matrix (w->current_matrix,
15236 NILP (glyphs) ? 0 : XINT (glyphs));
15237 return Qnil;
15238 }
15239
15240
15241 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
15242 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
15243 ()
15244 {
15245 struct frame *f = XFRAME (selected_frame);
15246 dump_glyph_matrix (f->current_matrix, 1);
15247 return Qnil;
15248 }
15249
15250
15251 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
15252 doc: /* Dump glyph row ROW to stderr.
15253 GLYPH 0 means don't dump glyphs.
15254 GLYPH 1 means dump glyphs in short form.
15255 GLYPH > 1 or omitted means dump glyphs in long form. */)
15256 (row, glyphs)
15257 Lisp_Object row, glyphs;
15258 {
15259 struct glyph_matrix *matrix;
15260 int vpos;
15261
15262 CHECK_NUMBER (row);
15263 matrix = XWINDOW (selected_window)->current_matrix;
15264 vpos = XINT (row);
15265 if (vpos >= 0 && vpos < matrix->nrows)
15266 dump_glyph_row (MATRIX_ROW (matrix, vpos),
15267 vpos,
15268 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15269 return Qnil;
15270 }
15271
15272
15273 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
15274 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
15275 GLYPH 0 means don't dump glyphs.
15276 GLYPH 1 means dump glyphs in short form.
15277 GLYPH > 1 or omitted means dump glyphs in long form. */)
15278 (row, glyphs)
15279 Lisp_Object row, glyphs;
15280 {
15281 struct frame *sf = SELECTED_FRAME ();
15282 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
15283 int vpos;
15284
15285 CHECK_NUMBER (row);
15286 vpos = XINT (row);
15287 if (vpos >= 0 && vpos < m->nrows)
15288 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
15289 INTEGERP (glyphs) ? XINT (glyphs) : 2);
15290 return Qnil;
15291 }
15292
15293
15294 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
15295 doc: /* Toggle tracing of redisplay.
15296 With ARG, turn tracing on if and only if ARG is positive. */)
15297 (arg)
15298 Lisp_Object arg;
15299 {
15300 if (NILP (arg))
15301 trace_redisplay_p = !trace_redisplay_p;
15302 else
15303 {
15304 arg = Fprefix_numeric_value (arg);
15305 trace_redisplay_p = XINT (arg) > 0;
15306 }
15307
15308 return Qnil;
15309 }
15310
15311
15312 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
15313 doc: /* Like `format', but print result to stderr.
15314 usage: (trace-to-stderr STRING &rest OBJECTS) */)
15315 (nargs, args)
15316 int nargs;
15317 Lisp_Object *args;
15318 {
15319 Lisp_Object s = Fformat (nargs, args);
15320 fprintf (stderr, "%s", SDATA (s));
15321 return Qnil;
15322 }
15323
15324 #endif /* GLYPH_DEBUG */
15325
15326
15327 \f
15328 /***********************************************************************
15329 Building Desired Matrix Rows
15330 ***********************************************************************/
15331
15332 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
15333 Used for non-window-redisplay windows, and for windows w/o left fringe. */
15334
15335 static struct glyph_row *
15336 get_overlay_arrow_glyph_row (w, overlay_arrow_string)
15337 struct window *w;
15338 Lisp_Object overlay_arrow_string;
15339 {
15340 struct frame *f = XFRAME (WINDOW_FRAME (w));
15341 struct buffer *buffer = XBUFFER (w->buffer);
15342 struct buffer *old = current_buffer;
15343 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
15344 int arrow_len = SCHARS (overlay_arrow_string);
15345 const unsigned char *arrow_end = arrow_string + arrow_len;
15346 const unsigned char *p;
15347 struct it it;
15348 int multibyte_p;
15349 int n_glyphs_before;
15350
15351 set_buffer_temp (buffer);
15352 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
15353 it.glyph_row->used[TEXT_AREA] = 0;
15354 SET_TEXT_POS (it.position, 0, 0);
15355
15356 multibyte_p = !NILP (buffer->enable_multibyte_characters);
15357 p = arrow_string;
15358 while (p < arrow_end)
15359 {
15360 Lisp_Object face, ilisp;
15361
15362 /* Get the next character. */
15363 if (multibyte_p)
15364 it.c = string_char_and_length (p, arrow_len, &it.len);
15365 else
15366 it.c = *p, it.len = 1;
15367 p += it.len;
15368
15369 /* Get its face. */
15370 ilisp = make_number (p - arrow_string);
15371 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
15372 it.face_id = compute_char_face (f, it.c, face);
15373
15374 /* Compute its width, get its glyphs. */
15375 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
15376 SET_TEXT_POS (it.position, -1, -1);
15377 PRODUCE_GLYPHS (&it);
15378
15379 /* If this character doesn't fit any more in the line, we have
15380 to remove some glyphs. */
15381 if (it.current_x > it.last_visible_x)
15382 {
15383 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
15384 break;
15385 }
15386 }
15387
15388 set_buffer_temp (old);
15389 return it.glyph_row;
15390 }
15391
15392
15393 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
15394 glyphs are only inserted for terminal frames since we can't really
15395 win with truncation glyphs when partially visible glyphs are
15396 involved. Which glyphs to insert is determined by
15397 produce_special_glyphs. */
15398
15399 static void
15400 insert_left_trunc_glyphs (it)
15401 struct it *it;
15402 {
15403 struct it truncate_it;
15404 struct glyph *from, *end, *to, *toend;
15405
15406 xassert (!FRAME_WINDOW_P (it->f));
15407
15408 /* Get the truncation glyphs. */
15409 truncate_it = *it;
15410 truncate_it.current_x = 0;
15411 truncate_it.face_id = DEFAULT_FACE_ID;
15412 truncate_it.glyph_row = &scratch_glyph_row;
15413 truncate_it.glyph_row->used[TEXT_AREA] = 0;
15414 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
15415 truncate_it.object = make_number (0);
15416 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
15417
15418 /* Overwrite glyphs from IT with truncation glyphs. */
15419 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15420 end = from + truncate_it.glyph_row->used[TEXT_AREA];
15421 to = it->glyph_row->glyphs[TEXT_AREA];
15422 toend = to + it->glyph_row->used[TEXT_AREA];
15423
15424 while (from < end)
15425 *to++ = *from++;
15426
15427 /* There may be padding glyphs left over. Overwrite them too. */
15428 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
15429 {
15430 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
15431 while (from < end)
15432 *to++ = *from++;
15433 }
15434
15435 if (to > toend)
15436 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
15437 }
15438
15439
15440 /* Compute the pixel height and width of IT->glyph_row.
15441
15442 Most of the time, ascent and height of a display line will be equal
15443 to the max_ascent and max_height values of the display iterator
15444 structure. This is not the case if
15445
15446 1. We hit ZV without displaying anything. In this case, max_ascent
15447 and max_height will be zero.
15448
15449 2. We have some glyphs that don't contribute to the line height.
15450 (The glyph row flag contributes_to_line_height_p is for future
15451 pixmap extensions).
15452
15453 The first case is easily covered by using default values because in
15454 these cases, the line height does not really matter, except that it
15455 must not be zero. */
15456
15457 static void
15458 compute_line_metrics (it)
15459 struct it *it;
15460 {
15461 struct glyph_row *row = it->glyph_row;
15462 int area, i;
15463
15464 if (FRAME_WINDOW_P (it->f))
15465 {
15466 int i, min_y, max_y;
15467
15468 /* The line may consist of one space only, that was added to
15469 place the cursor on it. If so, the row's height hasn't been
15470 computed yet. */
15471 if (row->height == 0)
15472 {
15473 if (it->max_ascent + it->max_descent == 0)
15474 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
15475 row->ascent = it->max_ascent;
15476 row->height = it->max_ascent + it->max_descent;
15477 row->phys_ascent = it->max_phys_ascent;
15478 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15479 row->extra_line_spacing = it->max_extra_line_spacing;
15480 }
15481
15482 /* Compute the width of this line. */
15483 row->pixel_width = row->x;
15484 for (i = 0; i < row->used[TEXT_AREA]; ++i)
15485 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
15486
15487 xassert (row->pixel_width >= 0);
15488 xassert (row->ascent >= 0 && row->height > 0);
15489
15490 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
15491 || MATRIX_ROW_OVERLAPS_PRED_P (row));
15492
15493 /* If first line's physical ascent is larger than its logical
15494 ascent, use the physical ascent, and make the row taller.
15495 This makes accented characters fully visible. */
15496 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
15497 && row->phys_ascent > row->ascent)
15498 {
15499 row->height += row->phys_ascent - row->ascent;
15500 row->ascent = row->phys_ascent;
15501 }
15502
15503 /* Compute how much of the line is visible. */
15504 row->visible_height = row->height;
15505
15506 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
15507 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
15508
15509 if (row->y < min_y)
15510 row->visible_height -= min_y - row->y;
15511 if (row->y + row->height > max_y)
15512 row->visible_height -= row->y + row->height - max_y;
15513 }
15514 else
15515 {
15516 row->pixel_width = row->used[TEXT_AREA];
15517 if (row->continued_p)
15518 row->pixel_width -= it->continuation_pixel_width;
15519 else if (row->truncated_on_right_p)
15520 row->pixel_width -= it->truncation_pixel_width;
15521 row->ascent = row->phys_ascent = 0;
15522 row->height = row->phys_height = row->visible_height = 1;
15523 row->extra_line_spacing = 0;
15524 }
15525
15526 /* Compute a hash code for this row. */
15527 row->hash = 0;
15528 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
15529 for (i = 0; i < row->used[area]; ++i)
15530 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
15531 + row->glyphs[area][i].u.val
15532 + row->glyphs[area][i].face_id
15533 + row->glyphs[area][i].padding_p
15534 + (row->glyphs[area][i].type << 2));
15535
15536 it->max_ascent = it->max_descent = 0;
15537 it->max_phys_ascent = it->max_phys_descent = 0;
15538 }
15539
15540
15541 /* Append one space to the glyph row of iterator IT if doing a
15542 window-based redisplay. The space has the same face as
15543 IT->face_id. Value is non-zero if a space was added.
15544
15545 This function is called to make sure that there is always one glyph
15546 at the end of a glyph row that the cursor can be set on under
15547 window-systems. (If there weren't such a glyph we would not know
15548 how wide and tall a box cursor should be displayed).
15549
15550 At the same time this space let's a nicely handle clearing to the
15551 end of the line if the row ends in italic text. */
15552
15553 static int
15554 append_space_for_newline (it, default_face_p)
15555 struct it *it;
15556 int default_face_p;
15557 {
15558 if (FRAME_WINDOW_P (it->f))
15559 {
15560 int n = it->glyph_row->used[TEXT_AREA];
15561
15562 if (it->glyph_row->glyphs[TEXT_AREA] + n
15563 < it->glyph_row->glyphs[1 + TEXT_AREA])
15564 {
15565 /* Save some values that must not be changed.
15566 Must save IT->c and IT->len because otherwise
15567 ITERATOR_AT_END_P wouldn't work anymore after
15568 append_space_for_newline has been called. */
15569 enum display_element_type saved_what = it->what;
15570 int saved_c = it->c, saved_len = it->len;
15571 int saved_x = it->current_x;
15572 int saved_face_id = it->face_id;
15573 struct text_pos saved_pos;
15574 Lisp_Object saved_object;
15575 struct face *face;
15576
15577 saved_object = it->object;
15578 saved_pos = it->position;
15579
15580 it->what = IT_CHARACTER;
15581 bzero (&it->position, sizeof it->position);
15582 it->object = make_number (0);
15583 it->c = ' ';
15584 it->len = 1;
15585
15586 if (default_face_p)
15587 it->face_id = DEFAULT_FACE_ID;
15588 else if (it->face_before_selective_p)
15589 it->face_id = it->saved_face_id;
15590 face = FACE_FROM_ID (it->f, it->face_id);
15591 it->face_id = FACE_FOR_CHAR (it->f, face, 0);
15592
15593 PRODUCE_GLYPHS (it);
15594
15595 it->override_ascent = -1;
15596 it->constrain_row_ascent_descent_p = 0;
15597 it->current_x = saved_x;
15598 it->object = saved_object;
15599 it->position = saved_pos;
15600 it->what = saved_what;
15601 it->face_id = saved_face_id;
15602 it->len = saved_len;
15603 it->c = saved_c;
15604 return 1;
15605 }
15606 }
15607
15608 return 0;
15609 }
15610
15611
15612 /* Extend the face of the last glyph in the text area of IT->glyph_row
15613 to the end of the display line. Called from display_line.
15614 If the glyph row is empty, add a space glyph to it so that we
15615 know the face to draw. Set the glyph row flag fill_line_p. */
15616
15617 static void
15618 extend_face_to_end_of_line (it)
15619 struct it *it;
15620 {
15621 struct face *face;
15622 struct frame *f = it->f;
15623
15624 /* If line is already filled, do nothing. */
15625 if (it->current_x >= it->last_visible_x)
15626 return;
15627
15628 /* Face extension extends the background and box of IT->face_id
15629 to the end of the line. If the background equals the background
15630 of the frame, we don't have to do anything. */
15631 if (it->face_before_selective_p)
15632 face = FACE_FROM_ID (it->f, it->saved_face_id);
15633 else
15634 face = FACE_FROM_ID (f, it->face_id);
15635
15636 if (FRAME_WINDOW_P (f)
15637 && it->glyph_row->displays_text_p
15638 && face->box == FACE_NO_BOX
15639 && face->background == FRAME_BACKGROUND_PIXEL (f)
15640 && !face->stipple)
15641 return;
15642
15643 /* Set the glyph row flag indicating that the face of the last glyph
15644 in the text area has to be drawn to the end of the text area. */
15645 it->glyph_row->fill_line_p = 1;
15646
15647 /* If current character of IT is not ASCII, make sure we have the
15648 ASCII face. This will be automatically undone the next time
15649 get_next_display_element returns a multibyte character. Note
15650 that the character will always be single byte in unibyte text. */
15651 if (!SINGLE_BYTE_CHAR_P (it->c))
15652 {
15653 it->face_id = FACE_FOR_CHAR (f, face, 0);
15654 }
15655
15656 if (FRAME_WINDOW_P (f))
15657 {
15658 /* If the row is empty, add a space with the current face of IT,
15659 so that we know which face to draw. */
15660 if (it->glyph_row->used[TEXT_AREA] == 0)
15661 {
15662 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
15663 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
15664 it->glyph_row->used[TEXT_AREA] = 1;
15665 }
15666 }
15667 else
15668 {
15669 /* Save some values that must not be changed. */
15670 int saved_x = it->current_x;
15671 struct text_pos saved_pos;
15672 Lisp_Object saved_object;
15673 enum display_element_type saved_what = it->what;
15674 int saved_face_id = it->face_id;
15675
15676 saved_object = it->object;
15677 saved_pos = it->position;
15678
15679 it->what = IT_CHARACTER;
15680 bzero (&it->position, sizeof it->position);
15681 it->object = make_number (0);
15682 it->c = ' ';
15683 it->len = 1;
15684 it->face_id = face->id;
15685
15686 PRODUCE_GLYPHS (it);
15687
15688 while (it->current_x <= it->last_visible_x)
15689 PRODUCE_GLYPHS (it);
15690
15691 /* Don't count these blanks really. It would let us insert a left
15692 truncation glyph below and make us set the cursor on them, maybe. */
15693 it->current_x = saved_x;
15694 it->object = saved_object;
15695 it->position = saved_pos;
15696 it->what = saved_what;
15697 it->face_id = saved_face_id;
15698 }
15699 }
15700
15701
15702 /* Value is non-zero if text starting at CHARPOS in current_buffer is
15703 trailing whitespace. */
15704
15705 static int
15706 trailing_whitespace_p (charpos)
15707 int charpos;
15708 {
15709 int bytepos = CHAR_TO_BYTE (charpos);
15710 int c = 0;
15711
15712 while (bytepos < ZV_BYTE
15713 && (c = FETCH_CHAR (bytepos),
15714 c == ' ' || c == '\t'))
15715 ++bytepos;
15716
15717 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
15718 {
15719 if (bytepos != PT_BYTE)
15720 return 1;
15721 }
15722 return 0;
15723 }
15724
15725
15726 /* Highlight trailing whitespace, if any, in ROW. */
15727
15728 void
15729 highlight_trailing_whitespace (f, row)
15730 struct frame *f;
15731 struct glyph_row *row;
15732 {
15733 int used = row->used[TEXT_AREA];
15734
15735 if (used)
15736 {
15737 struct glyph *start = row->glyphs[TEXT_AREA];
15738 struct glyph *glyph = start + used - 1;
15739
15740 /* Skip over glyphs inserted to display the cursor at the
15741 end of a line, for extending the face of the last glyph
15742 to the end of the line on terminals, and for truncation
15743 and continuation glyphs. */
15744 while (glyph >= start
15745 && glyph->type == CHAR_GLYPH
15746 && INTEGERP (glyph->object))
15747 --glyph;
15748
15749 /* If last glyph is a space or stretch, and it's trailing
15750 whitespace, set the face of all trailing whitespace glyphs in
15751 IT->glyph_row to `trailing-whitespace'. */
15752 if (glyph >= start
15753 && BUFFERP (glyph->object)
15754 && (glyph->type == STRETCH_GLYPH
15755 || (glyph->type == CHAR_GLYPH
15756 && glyph->u.ch == ' '))
15757 && trailing_whitespace_p (glyph->charpos))
15758 {
15759 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0, 0);
15760 if (face_id < 0)
15761 return;
15762
15763 while (glyph >= start
15764 && BUFFERP (glyph->object)
15765 && (glyph->type == STRETCH_GLYPH
15766 || (glyph->type == CHAR_GLYPH
15767 && glyph->u.ch == ' ')))
15768 (glyph--)->face_id = face_id;
15769 }
15770 }
15771 }
15772
15773
15774 /* Value is non-zero if glyph row ROW in window W should be
15775 used to hold the cursor. */
15776
15777 static int
15778 cursor_row_p (w, row)
15779 struct window *w;
15780 struct glyph_row *row;
15781 {
15782 int cursor_row_p = 1;
15783
15784 if (PT == MATRIX_ROW_END_CHARPOS (row))
15785 {
15786 /* If the row ends with a newline from a string, we don't want
15787 the cursor there, but we still want it at the start of the
15788 string if the string starts in this row.
15789 If the row is continued it doesn't end in a newline. */
15790 if (CHARPOS (row->end.string_pos) >= 0)
15791 cursor_row_p = (row->continued_p
15792 || PT >= MATRIX_ROW_START_CHARPOS (row));
15793 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15794 {
15795 /* If the row ends in middle of a real character,
15796 and the line is continued, we want the cursor here.
15797 That's because MATRIX_ROW_END_CHARPOS would equal
15798 PT if PT is before the character. */
15799 if (!row->ends_in_ellipsis_p)
15800 cursor_row_p = row->continued_p;
15801 else
15802 /* If the row ends in an ellipsis, then
15803 MATRIX_ROW_END_CHARPOS will equal point after the invisible text.
15804 We want that position to be displayed after the ellipsis. */
15805 cursor_row_p = 0;
15806 }
15807 /* If the row ends at ZV, display the cursor at the end of that
15808 row instead of at the start of the row below. */
15809 else if (row->ends_at_zv_p)
15810 cursor_row_p = 1;
15811 else
15812 cursor_row_p = 0;
15813 }
15814
15815 return cursor_row_p;
15816 }
15817
15818
15819 /* Construct the glyph row IT->glyph_row in the desired matrix of
15820 IT->w from text at the current position of IT. See dispextern.h
15821 for an overview of struct it. Value is non-zero if
15822 IT->glyph_row displays text, as opposed to a line displaying ZV
15823 only. */
15824
15825 static int
15826 display_line (it)
15827 struct it *it;
15828 {
15829 struct glyph_row *row = it->glyph_row;
15830 Lisp_Object overlay_arrow_string;
15831
15832 /* We always start displaying at hpos zero even if hscrolled. */
15833 xassert (it->hpos == 0 && it->current_x == 0);
15834
15835 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
15836 >= it->w->desired_matrix->nrows)
15837 {
15838 it->w->nrows_scale_factor++;
15839 fonts_changed_p = 1;
15840 return 0;
15841 }
15842
15843 /* Is IT->w showing the region? */
15844 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
15845
15846 /* Clear the result glyph row and enable it. */
15847 prepare_desired_row (row);
15848
15849 row->y = it->current_y;
15850 row->start = it->start;
15851 row->continuation_lines_width = it->continuation_lines_width;
15852 row->displays_text_p = 1;
15853 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
15854 it->starts_in_middle_of_char_p = 0;
15855
15856 /* Arrange the overlays nicely for our purposes. Usually, we call
15857 display_line on only one line at a time, in which case this
15858 can't really hurt too much, or we call it on lines which appear
15859 one after another in the buffer, in which case all calls to
15860 recenter_overlay_lists but the first will be pretty cheap. */
15861 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
15862
15863 /* Move over display elements that are not visible because we are
15864 hscrolled. This may stop at an x-position < IT->first_visible_x
15865 if the first glyph is partially visible or if we hit a line end. */
15866 if (it->current_x < it->first_visible_x)
15867 {
15868 move_it_in_display_line_to (it, ZV, it->first_visible_x,
15869 MOVE_TO_POS | MOVE_TO_X);
15870 }
15871
15872 /* Get the initial row height. This is either the height of the
15873 text hscrolled, if there is any, or zero. */
15874 row->ascent = it->max_ascent;
15875 row->height = it->max_ascent + it->max_descent;
15876 row->phys_ascent = it->max_phys_ascent;
15877 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
15878 row->extra_line_spacing = it->max_extra_line_spacing;
15879
15880 /* Loop generating characters. The loop is left with IT on the next
15881 character to display. */
15882 while (1)
15883 {
15884 int n_glyphs_before, hpos_before, x_before;
15885 int x, i, nglyphs;
15886 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
15887
15888 /* Retrieve the next thing to display. Value is zero if end of
15889 buffer reached. */
15890 if (!get_next_display_element (it))
15891 {
15892 /* Maybe add a space at the end of this line that is used to
15893 display the cursor there under X. Set the charpos of the
15894 first glyph of blank lines not corresponding to any text
15895 to -1. */
15896 #ifdef HAVE_WINDOW_SYSTEM
15897 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
15898 row->exact_window_width_line_p = 1;
15899 else
15900 #endif /* HAVE_WINDOW_SYSTEM */
15901 if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
15902 || row->used[TEXT_AREA] == 0)
15903 {
15904 row->glyphs[TEXT_AREA]->charpos = -1;
15905 row->displays_text_p = 0;
15906
15907 if (!NILP (XBUFFER (it->w->buffer)->indicate_empty_lines)
15908 && (!MINI_WINDOW_P (it->w)
15909 || (minibuf_level && EQ (it->window, minibuf_window))))
15910 row->indicate_empty_line_p = 1;
15911 }
15912
15913 it->continuation_lines_width = 0;
15914 row->ends_at_zv_p = 1;
15915 break;
15916 }
15917
15918 /* Now, get the metrics of what we want to display. This also
15919 generates glyphs in `row' (which is IT->glyph_row). */
15920 n_glyphs_before = row->used[TEXT_AREA];
15921 x = it->current_x;
15922
15923 /* Remember the line height so far in case the next element doesn't
15924 fit on the line. */
15925 if (!it->truncate_lines_p)
15926 {
15927 ascent = it->max_ascent;
15928 descent = it->max_descent;
15929 phys_ascent = it->max_phys_ascent;
15930 phys_descent = it->max_phys_descent;
15931 }
15932
15933 PRODUCE_GLYPHS (it);
15934
15935 /* If this display element was in marginal areas, continue with
15936 the next one. */
15937 if (it->area != TEXT_AREA)
15938 {
15939 row->ascent = max (row->ascent, it->max_ascent);
15940 row->height = max (row->height, it->max_ascent + it->max_descent);
15941 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15942 row->phys_height = max (row->phys_height,
15943 it->max_phys_ascent + it->max_phys_descent);
15944 row->extra_line_spacing = max (row->extra_line_spacing,
15945 it->max_extra_line_spacing);
15946 set_iterator_to_next (it, 1);
15947 continue;
15948 }
15949
15950 /* Does the display element fit on the line? If we truncate
15951 lines, we should draw past the right edge of the window. If
15952 we don't truncate, we want to stop so that we can display the
15953 continuation glyph before the right margin. If lines are
15954 continued, there are two possible strategies for characters
15955 resulting in more than 1 glyph (e.g. tabs): Display as many
15956 glyphs as possible in this line and leave the rest for the
15957 continuation line, or display the whole element in the next
15958 line. Original redisplay did the former, so we do it also. */
15959 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
15960 hpos_before = it->hpos;
15961 x_before = x;
15962
15963 if (/* Not a newline. */
15964 nglyphs > 0
15965 /* Glyphs produced fit entirely in the line. */
15966 && it->current_x < it->last_visible_x)
15967 {
15968 it->hpos += nglyphs;
15969 row->ascent = max (row->ascent, it->max_ascent);
15970 row->height = max (row->height, it->max_ascent + it->max_descent);
15971 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
15972 row->phys_height = max (row->phys_height,
15973 it->max_phys_ascent + it->max_phys_descent);
15974 row->extra_line_spacing = max (row->extra_line_spacing,
15975 it->max_extra_line_spacing);
15976 if (it->current_x - it->pixel_width < it->first_visible_x)
15977 row->x = x - it->first_visible_x;
15978 }
15979 else
15980 {
15981 int new_x;
15982 struct glyph *glyph;
15983
15984 for (i = 0; i < nglyphs; ++i, x = new_x)
15985 {
15986 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
15987 new_x = x + glyph->pixel_width;
15988
15989 if (/* Lines are continued. */
15990 !it->truncate_lines_p
15991 && (/* Glyph doesn't fit on the line. */
15992 new_x > it->last_visible_x
15993 /* Or it fits exactly on a window system frame. */
15994 || (new_x == it->last_visible_x
15995 && FRAME_WINDOW_P (it->f))))
15996 {
15997 /* End of a continued line. */
15998
15999 if (it->hpos == 0
16000 || (new_x == it->last_visible_x
16001 && FRAME_WINDOW_P (it->f)))
16002 {
16003 /* Current glyph is the only one on the line or
16004 fits exactly on the line. We must continue
16005 the line because we can't draw the cursor
16006 after the glyph. */
16007 row->continued_p = 1;
16008 it->current_x = new_x;
16009 it->continuation_lines_width += new_x;
16010 ++it->hpos;
16011 if (i == nglyphs - 1)
16012 {
16013 set_iterator_to_next (it, 1);
16014 #ifdef HAVE_WINDOW_SYSTEM
16015 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16016 {
16017 if (!get_next_display_element (it))
16018 {
16019 row->exact_window_width_line_p = 1;
16020 it->continuation_lines_width = 0;
16021 row->continued_p = 0;
16022 row->ends_at_zv_p = 1;
16023 }
16024 else if (ITERATOR_AT_END_OF_LINE_P (it))
16025 {
16026 row->continued_p = 0;
16027 row->exact_window_width_line_p = 1;
16028 }
16029 }
16030 #endif /* HAVE_WINDOW_SYSTEM */
16031 }
16032 }
16033 else if (CHAR_GLYPH_PADDING_P (*glyph)
16034 && !FRAME_WINDOW_P (it->f))
16035 {
16036 /* A padding glyph that doesn't fit on this line.
16037 This means the whole character doesn't fit
16038 on the line. */
16039 row->used[TEXT_AREA] = n_glyphs_before;
16040
16041 /* Fill the rest of the row with continuation
16042 glyphs like in 20.x. */
16043 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
16044 < row->glyphs[1 + TEXT_AREA])
16045 produce_special_glyphs (it, IT_CONTINUATION);
16046
16047 row->continued_p = 1;
16048 it->current_x = x_before;
16049 it->continuation_lines_width += x_before;
16050
16051 /* Restore the height to what it was before the
16052 element not fitting on the line. */
16053 it->max_ascent = ascent;
16054 it->max_descent = descent;
16055 it->max_phys_ascent = phys_ascent;
16056 it->max_phys_descent = phys_descent;
16057 }
16058 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
16059 {
16060 /* A TAB that extends past the right edge of the
16061 window. This produces a single glyph on
16062 window system frames. We leave the glyph in
16063 this row and let it fill the row, but don't
16064 consume the TAB. */
16065 it->continuation_lines_width += it->last_visible_x;
16066 row->ends_in_middle_of_char_p = 1;
16067 row->continued_p = 1;
16068 glyph->pixel_width = it->last_visible_x - x;
16069 it->starts_in_middle_of_char_p = 1;
16070 }
16071 else
16072 {
16073 /* Something other than a TAB that draws past
16074 the right edge of the window. Restore
16075 positions to values before the element. */
16076 row->used[TEXT_AREA] = n_glyphs_before + i;
16077
16078 /* Display continuation glyphs. */
16079 if (!FRAME_WINDOW_P (it->f))
16080 produce_special_glyphs (it, IT_CONTINUATION);
16081 row->continued_p = 1;
16082
16083 it->current_x = x_before;
16084 it->continuation_lines_width += x;
16085 extend_face_to_end_of_line (it);
16086
16087 if (nglyphs > 1 && i > 0)
16088 {
16089 row->ends_in_middle_of_char_p = 1;
16090 it->starts_in_middle_of_char_p = 1;
16091 }
16092
16093 /* Restore the height to what it was before the
16094 element not fitting on the line. */
16095 it->max_ascent = ascent;
16096 it->max_descent = descent;
16097 it->max_phys_ascent = phys_ascent;
16098 it->max_phys_descent = phys_descent;
16099 }
16100
16101 break;
16102 }
16103 else if (new_x > it->first_visible_x)
16104 {
16105 /* Increment number of glyphs actually displayed. */
16106 ++it->hpos;
16107
16108 if (x < it->first_visible_x)
16109 /* Glyph is partially visible, i.e. row starts at
16110 negative X position. */
16111 row->x = x - it->first_visible_x;
16112 }
16113 else
16114 {
16115 /* Glyph is completely off the left margin of the
16116 window. This should not happen because of the
16117 move_it_in_display_line at the start of this
16118 function, unless the text display area of the
16119 window is empty. */
16120 xassert (it->first_visible_x <= it->last_visible_x);
16121 }
16122 }
16123
16124 row->ascent = max (row->ascent, it->max_ascent);
16125 row->height = max (row->height, it->max_ascent + it->max_descent);
16126 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
16127 row->phys_height = max (row->phys_height,
16128 it->max_phys_ascent + it->max_phys_descent);
16129 row->extra_line_spacing = max (row->extra_line_spacing,
16130 it->max_extra_line_spacing);
16131
16132 /* End of this display line if row is continued. */
16133 if (row->continued_p || row->ends_at_zv_p)
16134 break;
16135 }
16136
16137 at_end_of_line:
16138 /* Is this a line end? If yes, we're also done, after making
16139 sure that a non-default face is extended up to the right
16140 margin of the window. */
16141 if (ITERATOR_AT_END_OF_LINE_P (it))
16142 {
16143 int used_before = row->used[TEXT_AREA];
16144
16145 row->ends_in_newline_from_string_p = STRINGP (it->object);
16146
16147 #ifdef HAVE_WINDOW_SYSTEM
16148 /* Add a space at the end of the line that is used to
16149 display the cursor there. */
16150 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16151 append_space_for_newline (it, 0);
16152 #endif /* HAVE_WINDOW_SYSTEM */
16153
16154 /* Extend the face to the end of the line. */
16155 extend_face_to_end_of_line (it);
16156
16157 /* Make sure we have the position. */
16158 if (used_before == 0)
16159 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
16160
16161 /* Consume the line end. This skips over invisible lines. */
16162 set_iterator_to_next (it, 1);
16163 it->continuation_lines_width = 0;
16164 break;
16165 }
16166
16167 /* Proceed with next display element. Note that this skips
16168 over lines invisible because of selective display. */
16169 set_iterator_to_next (it, 1);
16170
16171 /* If we truncate lines, we are done when the last displayed
16172 glyphs reach past the right margin of the window. */
16173 if (it->truncate_lines_p
16174 && (FRAME_WINDOW_P (it->f)
16175 ? (it->current_x >= it->last_visible_x)
16176 : (it->current_x > it->last_visible_x)))
16177 {
16178 /* Maybe add truncation glyphs. */
16179 if (!FRAME_WINDOW_P (it->f))
16180 {
16181 int i, n;
16182
16183 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
16184 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
16185 break;
16186
16187 for (n = row->used[TEXT_AREA]; i < n; ++i)
16188 {
16189 row->used[TEXT_AREA] = i;
16190 produce_special_glyphs (it, IT_TRUNCATION);
16191 }
16192 }
16193 #ifdef HAVE_WINDOW_SYSTEM
16194 else
16195 {
16196 /* Don't truncate if we can overflow newline into fringe. */
16197 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
16198 {
16199 if (!get_next_display_element (it))
16200 {
16201 it->continuation_lines_width = 0;
16202 row->ends_at_zv_p = 1;
16203 row->exact_window_width_line_p = 1;
16204 break;
16205 }
16206 if (ITERATOR_AT_END_OF_LINE_P (it))
16207 {
16208 row->exact_window_width_line_p = 1;
16209 goto at_end_of_line;
16210 }
16211 }
16212 }
16213 #endif /* HAVE_WINDOW_SYSTEM */
16214
16215 row->truncated_on_right_p = 1;
16216 it->continuation_lines_width = 0;
16217 reseat_at_next_visible_line_start (it, 0);
16218 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
16219 it->hpos = hpos_before;
16220 it->current_x = x_before;
16221 break;
16222 }
16223 }
16224
16225 /* If line is not empty and hscrolled, maybe insert truncation glyphs
16226 at the left window margin. */
16227 if (it->first_visible_x
16228 && IT_CHARPOS (*it) != MATRIX_ROW_START_CHARPOS (row))
16229 {
16230 if (!FRAME_WINDOW_P (it->f))
16231 insert_left_trunc_glyphs (it);
16232 row->truncated_on_left_p = 1;
16233 }
16234
16235 /* If the start of this line is the overlay arrow-position, then
16236 mark this glyph row as the one containing the overlay arrow.
16237 This is clearly a mess with variable size fonts. It would be
16238 better to let it be displayed like cursors under X. */
16239 if ((row->displays_text_p || !overlay_arrow_seen)
16240 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
16241 !NILP (overlay_arrow_string)))
16242 {
16243 /* Overlay arrow in window redisplay is a fringe bitmap. */
16244 if (STRINGP (overlay_arrow_string))
16245 {
16246 struct glyph_row *arrow_row
16247 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
16248 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
16249 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
16250 struct glyph *p = row->glyphs[TEXT_AREA];
16251 struct glyph *p2, *end;
16252
16253 /* Copy the arrow glyphs. */
16254 while (glyph < arrow_end)
16255 *p++ = *glyph++;
16256
16257 /* Throw away padding glyphs. */
16258 p2 = p;
16259 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16260 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
16261 ++p2;
16262 if (p2 > p)
16263 {
16264 while (p2 < end)
16265 *p++ = *p2++;
16266 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
16267 }
16268 }
16269 else
16270 {
16271 xassert (INTEGERP (overlay_arrow_string));
16272 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
16273 }
16274 overlay_arrow_seen = 1;
16275 }
16276
16277 /* Compute pixel dimensions of this line. */
16278 compute_line_metrics (it);
16279
16280 /* Remember the position at which this line ends. */
16281 row->end = it->current;
16282
16283 /* Record whether this row ends inside an ellipsis. */
16284 row->ends_in_ellipsis_p
16285 = (it->method == GET_FROM_DISPLAY_VECTOR
16286 && it->ellipsis_p);
16287
16288 /* Save fringe bitmaps in this row. */
16289 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
16290 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
16291 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
16292 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
16293
16294 it->left_user_fringe_bitmap = 0;
16295 it->left_user_fringe_face_id = 0;
16296 it->right_user_fringe_bitmap = 0;
16297 it->right_user_fringe_face_id = 0;
16298
16299 /* Maybe set the cursor. */
16300 if (it->w->cursor.vpos < 0
16301 && PT >= MATRIX_ROW_START_CHARPOS (row)
16302 && PT <= MATRIX_ROW_END_CHARPOS (row)
16303 && cursor_row_p (it->w, row))
16304 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
16305
16306 /* Highlight trailing whitespace. */
16307 if (!NILP (Vshow_trailing_whitespace))
16308 highlight_trailing_whitespace (it->f, it->glyph_row);
16309
16310 /* Prepare for the next line. This line starts horizontally at (X
16311 HPOS) = (0 0). Vertical positions are incremented. As a
16312 convenience for the caller, IT->glyph_row is set to the next
16313 row to be used. */
16314 it->current_x = it->hpos = 0;
16315 it->current_y += row->height;
16316 ++it->vpos;
16317 ++it->glyph_row;
16318 it->start = it->current;
16319 return row->displays_text_p;
16320 }
16321
16322
16323 \f
16324 /***********************************************************************
16325 Menu Bar
16326 ***********************************************************************/
16327
16328 /* Redisplay the menu bar in the frame for window W.
16329
16330 The menu bar of X frames that don't have X toolkit support is
16331 displayed in a special window W->frame->menu_bar_window.
16332
16333 The menu bar of terminal frames is treated specially as far as
16334 glyph matrices are concerned. Menu bar lines are not part of
16335 windows, so the update is done directly on the frame matrix rows
16336 for the menu bar. */
16337
16338 static void
16339 display_menu_bar (w)
16340 struct window *w;
16341 {
16342 struct frame *f = XFRAME (WINDOW_FRAME (w));
16343 struct it it;
16344 Lisp_Object items;
16345 int i;
16346
16347 /* Don't do all this for graphical frames. */
16348 #ifdef HAVE_NTGUI
16349 if (!NILP (Vwindow_system))
16350 return;
16351 #endif
16352 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
16353 if (FRAME_X_P (f))
16354 return;
16355 #endif
16356 #ifdef MAC_OS
16357 if (FRAME_MAC_P (f))
16358 return;
16359 #endif
16360
16361 #ifdef USE_X_TOOLKIT
16362 xassert (!FRAME_WINDOW_P (f));
16363 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
16364 it.first_visible_x = 0;
16365 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16366 #else /* not USE_X_TOOLKIT */
16367 if (FRAME_WINDOW_P (f))
16368 {
16369 /* Menu bar lines are displayed in the desired matrix of the
16370 dummy window menu_bar_window. */
16371 struct window *menu_w;
16372 xassert (WINDOWP (f->menu_bar_window));
16373 menu_w = XWINDOW (f->menu_bar_window);
16374 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
16375 MENU_FACE_ID);
16376 it.first_visible_x = 0;
16377 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
16378 }
16379 else
16380 {
16381 /* This is a TTY frame, i.e. character hpos/vpos are used as
16382 pixel x/y. */
16383 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
16384 MENU_FACE_ID);
16385 it.first_visible_x = 0;
16386 it.last_visible_x = FRAME_COLS (f);
16387 }
16388 #endif /* not USE_X_TOOLKIT */
16389
16390 if (! mode_line_inverse_video)
16391 /* Force the menu-bar to be displayed in the default face. */
16392 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16393
16394 /* Clear all rows of the menu bar. */
16395 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
16396 {
16397 struct glyph_row *row = it.glyph_row + i;
16398 clear_glyph_row (row);
16399 row->enabled_p = 1;
16400 row->full_width_p = 1;
16401 }
16402
16403 /* Display all items of the menu bar. */
16404 items = FRAME_MENU_BAR_ITEMS (it.f);
16405 for (i = 0; i < XVECTOR (items)->size; i += 4)
16406 {
16407 Lisp_Object string;
16408
16409 /* Stop at nil string. */
16410 string = AREF (items, i + 1);
16411 if (NILP (string))
16412 break;
16413
16414 /* Remember where item was displayed. */
16415 AREF (items, i + 3) = make_number (it.hpos);
16416
16417 /* Display the item, pad with one space. */
16418 if (it.current_x < it.last_visible_x)
16419 display_string (NULL, string, Qnil, 0, 0, &it,
16420 SCHARS (string) + 1, 0, 0, -1);
16421 }
16422
16423 /* Fill out the line with spaces. */
16424 if (it.current_x < it.last_visible_x)
16425 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
16426
16427 /* Compute the total height of the lines. */
16428 compute_line_metrics (&it);
16429 }
16430
16431
16432 \f
16433 /***********************************************************************
16434 Mode Line
16435 ***********************************************************************/
16436
16437 /* Redisplay mode lines in the window tree whose root is WINDOW. If
16438 FORCE is non-zero, redisplay mode lines unconditionally.
16439 Otherwise, redisplay only mode lines that are garbaged. Value is
16440 the number of windows whose mode lines were redisplayed. */
16441
16442 static int
16443 redisplay_mode_lines (window, force)
16444 Lisp_Object window;
16445 int force;
16446 {
16447 int nwindows = 0;
16448
16449 while (!NILP (window))
16450 {
16451 struct window *w = XWINDOW (window);
16452
16453 if (WINDOWP (w->hchild))
16454 nwindows += redisplay_mode_lines (w->hchild, force);
16455 else if (WINDOWP (w->vchild))
16456 nwindows += redisplay_mode_lines (w->vchild, force);
16457 else if (force
16458 || FRAME_GARBAGED_P (XFRAME (w->frame))
16459 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
16460 {
16461 struct text_pos lpoint;
16462 struct buffer *old = current_buffer;
16463
16464 /* Set the window's buffer for the mode line display. */
16465 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16466 set_buffer_internal_1 (XBUFFER (w->buffer));
16467
16468 /* Point refers normally to the selected window. For any
16469 other window, set up appropriate value. */
16470 if (!EQ (window, selected_window))
16471 {
16472 struct text_pos pt;
16473
16474 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
16475 if (CHARPOS (pt) < BEGV)
16476 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16477 else if (CHARPOS (pt) > (ZV - 1))
16478 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
16479 else
16480 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
16481 }
16482
16483 /* Display mode lines. */
16484 clear_glyph_matrix (w->desired_matrix);
16485 if (display_mode_lines (w))
16486 {
16487 ++nwindows;
16488 w->must_be_updated_p = 1;
16489 }
16490
16491 /* Restore old settings. */
16492 set_buffer_internal_1 (old);
16493 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16494 }
16495
16496 window = w->next;
16497 }
16498
16499 return nwindows;
16500 }
16501
16502
16503 /* Display the mode and/or top line of window W. Value is the number
16504 of mode lines displayed. */
16505
16506 static int
16507 display_mode_lines (w)
16508 struct window *w;
16509 {
16510 Lisp_Object old_selected_window, old_selected_frame;
16511 int n = 0;
16512
16513 old_selected_frame = selected_frame;
16514 selected_frame = w->frame;
16515 old_selected_window = selected_window;
16516 XSETWINDOW (selected_window, w);
16517
16518 /* These will be set while the mode line specs are processed. */
16519 line_number_displayed = 0;
16520 w->column_number_displayed = Qnil;
16521
16522 if (WINDOW_WANTS_MODELINE_P (w))
16523 {
16524 struct window *sel_w = XWINDOW (old_selected_window);
16525
16526 /* Select mode line face based on the real selected window. */
16527 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
16528 current_buffer->mode_line_format);
16529 ++n;
16530 }
16531
16532 if (WINDOW_WANTS_HEADER_LINE_P (w))
16533 {
16534 display_mode_line (w, HEADER_LINE_FACE_ID,
16535 current_buffer->header_line_format);
16536 ++n;
16537 }
16538
16539 selected_frame = old_selected_frame;
16540 selected_window = old_selected_window;
16541 return n;
16542 }
16543
16544
16545 /* Display mode or top line of window W. FACE_ID specifies which line
16546 to display; it is either MODE_LINE_FACE_ID or HEADER_LINE_FACE_ID.
16547 FORMAT is the mode line format to display. Value is the pixel
16548 height of the mode line displayed. */
16549
16550 static int
16551 display_mode_line (w, face_id, format)
16552 struct window *w;
16553 enum face_id face_id;
16554 Lisp_Object format;
16555 {
16556 struct it it;
16557 struct face *face;
16558 int count = SPECPDL_INDEX ();
16559
16560 init_iterator (&it, w, -1, -1, NULL, face_id);
16561 prepare_desired_row (it.glyph_row);
16562
16563 it.glyph_row->mode_line_p = 1;
16564
16565 if (! mode_line_inverse_video)
16566 /* Force the mode-line to be displayed in the default face. */
16567 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
16568
16569 record_unwind_protect (unwind_format_mode_line,
16570 format_mode_line_unwind_data (NULL, 0));
16571
16572 mode_line_target = MODE_LINE_DISPLAY;
16573
16574 /* Temporarily make frame's keyboard the current kboard so that
16575 kboard-local variables in the mode_line_format will get the right
16576 values. */
16577 push_frame_kboard (it.f);
16578 record_unwind_save_match_data ();
16579 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
16580 pop_frame_kboard ();
16581
16582 unbind_to (count, Qnil);
16583
16584 /* Fill up with spaces. */
16585 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
16586
16587 compute_line_metrics (&it);
16588 it.glyph_row->full_width_p = 1;
16589 it.glyph_row->continued_p = 0;
16590 it.glyph_row->truncated_on_left_p = 0;
16591 it.glyph_row->truncated_on_right_p = 0;
16592
16593 /* Make a 3D mode-line have a shadow at its right end. */
16594 face = FACE_FROM_ID (it.f, face_id);
16595 extend_face_to_end_of_line (&it);
16596 if (face->box != FACE_NO_BOX)
16597 {
16598 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
16599 + it.glyph_row->used[TEXT_AREA] - 1);
16600 last->right_box_line_p = 1;
16601 }
16602
16603 return it.glyph_row->height;
16604 }
16605
16606 /* Move element ELT in LIST to the front of LIST.
16607 Return the updated list. */
16608
16609 static Lisp_Object
16610 move_elt_to_front (elt, list)
16611 Lisp_Object elt, list;
16612 {
16613 register Lisp_Object tail, prev;
16614 register Lisp_Object tem;
16615
16616 tail = list;
16617 prev = Qnil;
16618 while (CONSP (tail))
16619 {
16620 tem = XCAR (tail);
16621
16622 if (EQ (elt, tem))
16623 {
16624 /* Splice out the link TAIL. */
16625 if (NILP (prev))
16626 list = XCDR (tail);
16627 else
16628 Fsetcdr (prev, XCDR (tail));
16629
16630 /* Now make it the first. */
16631 Fsetcdr (tail, list);
16632 return tail;
16633 }
16634 else
16635 prev = tail;
16636 tail = XCDR (tail);
16637 QUIT;
16638 }
16639
16640 /* Not found--return unchanged LIST. */
16641 return list;
16642 }
16643
16644 /* Contribute ELT to the mode line for window IT->w. How it
16645 translates into text depends on its data type.
16646
16647 IT describes the display environment in which we display, as usual.
16648
16649 DEPTH is the depth in recursion. It is used to prevent
16650 infinite recursion here.
16651
16652 FIELD_WIDTH is the number of characters the display of ELT should
16653 occupy in the mode line, and PRECISION is the maximum number of
16654 characters to display from ELT's representation. See
16655 display_string for details.
16656
16657 Returns the hpos of the end of the text generated by ELT.
16658
16659 PROPS is a property list to add to any string we encounter.
16660
16661 If RISKY is nonzero, remove (disregard) any properties in any string
16662 we encounter, and ignore :eval and :propertize.
16663
16664 The global variable `mode_line_target' determines whether the
16665 output is passed to `store_mode_line_noprop',
16666 `store_mode_line_string', or `display_string'. */
16667
16668 static int
16669 display_mode_element (it, depth, field_width, precision, elt, props, risky)
16670 struct it *it;
16671 int depth;
16672 int field_width, precision;
16673 Lisp_Object elt, props;
16674 int risky;
16675 {
16676 int n = 0, field, prec;
16677 int literal = 0;
16678
16679 tail_recurse:
16680 if (depth > 100)
16681 elt = build_string ("*too-deep*");
16682
16683 depth++;
16684
16685 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
16686 {
16687 case Lisp_String:
16688 {
16689 /* A string: output it and check for %-constructs within it. */
16690 unsigned char c;
16691 int offset = 0;
16692
16693 if (SCHARS (elt) > 0
16694 && (!NILP (props) || risky))
16695 {
16696 Lisp_Object oprops, aelt;
16697 oprops = Ftext_properties_at (make_number (0), elt);
16698
16699 /* If the starting string's properties are not what
16700 we want, translate the string. Also, if the string
16701 is risky, do that anyway. */
16702
16703 if (NILP (Fequal (props, oprops)) || risky)
16704 {
16705 /* If the starting string has properties,
16706 merge the specified ones onto the existing ones. */
16707 if (! NILP (oprops) && !risky)
16708 {
16709 Lisp_Object tem;
16710
16711 oprops = Fcopy_sequence (oprops);
16712 tem = props;
16713 while (CONSP (tem))
16714 {
16715 oprops = Fplist_put (oprops, XCAR (tem),
16716 XCAR (XCDR (tem)));
16717 tem = XCDR (XCDR (tem));
16718 }
16719 props = oprops;
16720 }
16721
16722 aelt = Fassoc (elt, mode_line_proptrans_alist);
16723 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
16724 {
16725 /* AELT is what we want. Move it to the front
16726 without consing. */
16727 elt = XCAR (aelt);
16728 mode_line_proptrans_alist
16729 = move_elt_to_front (aelt, mode_line_proptrans_alist);
16730 }
16731 else
16732 {
16733 Lisp_Object tem;
16734
16735 /* If AELT has the wrong props, it is useless.
16736 so get rid of it. */
16737 if (! NILP (aelt))
16738 mode_line_proptrans_alist
16739 = Fdelq (aelt, mode_line_proptrans_alist);
16740
16741 elt = Fcopy_sequence (elt);
16742 Fset_text_properties (make_number (0), Flength (elt),
16743 props, elt);
16744 /* Add this item to mode_line_proptrans_alist. */
16745 mode_line_proptrans_alist
16746 = Fcons (Fcons (elt, props),
16747 mode_line_proptrans_alist);
16748 /* Truncate mode_line_proptrans_alist
16749 to at most 50 elements. */
16750 tem = Fnthcdr (make_number (50),
16751 mode_line_proptrans_alist);
16752 if (! NILP (tem))
16753 XSETCDR (tem, Qnil);
16754 }
16755 }
16756 }
16757
16758 offset = 0;
16759
16760 if (literal)
16761 {
16762 prec = precision - n;
16763 switch (mode_line_target)
16764 {
16765 case MODE_LINE_NOPROP:
16766 case MODE_LINE_TITLE:
16767 n += store_mode_line_noprop (SDATA (elt), -1, prec);
16768 break;
16769 case MODE_LINE_STRING:
16770 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
16771 break;
16772 case MODE_LINE_DISPLAY:
16773 n += display_string (NULL, elt, Qnil, 0, 0, it,
16774 0, prec, 0, STRING_MULTIBYTE (elt));
16775 break;
16776 }
16777
16778 break;
16779 }
16780
16781 /* Handle the non-literal case. */
16782
16783 while ((precision <= 0 || n < precision)
16784 && SREF (elt, offset) != 0
16785 && (mode_line_target != MODE_LINE_DISPLAY
16786 || it->current_x < it->last_visible_x))
16787 {
16788 int last_offset = offset;
16789
16790 /* Advance to end of string or next format specifier. */
16791 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
16792 ;
16793
16794 if (offset - 1 != last_offset)
16795 {
16796 int nchars, nbytes;
16797
16798 /* Output to end of string or up to '%'. Field width
16799 is length of string. Don't output more than
16800 PRECISION allows us. */
16801 offset--;
16802
16803 prec = c_string_width (SDATA (elt) + last_offset,
16804 offset - last_offset, precision - n,
16805 &nchars, &nbytes);
16806
16807 switch (mode_line_target)
16808 {
16809 case MODE_LINE_NOPROP:
16810 case MODE_LINE_TITLE:
16811 n += store_mode_line_noprop (SDATA (elt) + last_offset, 0, prec);
16812 break;
16813 case MODE_LINE_STRING:
16814 {
16815 int bytepos = last_offset;
16816 int charpos = string_byte_to_char (elt, bytepos);
16817 int endpos = (precision <= 0
16818 ? string_byte_to_char (elt, offset)
16819 : charpos + nchars);
16820
16821 n += store_mode_line_string (NULL,
16822 Fsubstring (elt, make_number (charpos),
16823 make_number (endpos)),
16824 0, 0, 0, Qnil);
16825 }
16826 break;
16827 case MODE_LINE_DISPLAY:
16828 {
16829 int bytepos = last_offset;
16830 int charpos = string_byte_to_char (elt, bytepos);
16831
16832 if (precision <= 0)
16833 nchars = string_byte_to_char (elt, offset) - charpos;
16834 n += display_string (NULL, elt, Qnil, 0, charpos,
16835 it, 0, nchars, 0,
16836 STRING_MULTIBYTE (elt));
16837 }
16838 break;
16839 }
16840 }
16841 else /* c == '%' */
16842 {
16843 int percent_position = offset;
16844
16845 /* Get the specified minimum width. Zero means
16846 don't pad. */
16847 field = 0;
16848 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
16849 field = field * 10 + c - '0';
16850
16851 /* Don't pad beyond the total padding allowed. */
16852 if (field_width - n > 0 && field > field_width - n)
16853 field = field_width - n;
16854
16855 /* Note that either PRECISION <= 0 or N < PRECISION. */
16856 prec = precision - n;
16857
16858 if (c == 'M')
16859 n += display_mode_element (it, depth, field, prec,
16860 Vglobal_mode_string, props,
16861 risky);
16862 else if (c != 0)
16863 {
16864 int multibyte;
16865 int bytepos, charpos;
16866 unsigned char *spec;
16867
16868 bytepos = percent_position;
16869 charpos = (STRING_MULTIBYTE (elt)
16870 ? string_byte_to_char (elt, bytepos)
16871 : bytepos);
16872
16873 spec
16874 = decode_mode_spec (it->w, c, field, prec, &multibyte);
16875
16876 switch (mode_line_target)
16877 {
16878 case MODE_LINE_NOPROP:
16879 case MODE_LINE_TITLE:
16880 n += store_mode_line_noprop (spec, field, prec);
16881 break;
16882 case MODE_LINE_STRING:
16883 {
16884 int len = strlen (spec);
16885 Lisp_Object tem = make_string (spec, len);
16886 props = Ftext_properties_at (make_number (charpos), elt);
16887 /* Should only keep face property in props */
16888 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
16889 }
16890 break;
16891 case MODE_LINE_DISPLAY:
16892 {
16893 int nglyphs_before, nwritten;
16894
16895 nglyphs_before = it->glyph_row->used[TEXT_AREA];
16896 nwritten = display_string (spec, Qnil, elt,
16897 charpos, 0, it,
16898 field, prec, 0,
16899 multibyte);
16900
16901 /* Assign to the glyphs written above the
16902 string where the `%x' came from, position
16903 of the `%'. */
16904 if (nwritten > 0)
16905 {
16906 struct glyph *glyph
16907 = (it->glyph_row->glyphs[TEXT_AREA]
16908 + nglyphs_before);
16909 int i;
16910
16911 for (i = 0; i < nwritten; ++i)
16912 {
16913 glyph[i].object = elt;
16914 glyph[i].charpos = charpos;
16915 }
16916
16917 n += nwritten;
16918 }
16919 }
16920 break;
16921 }
16922 }
16923 else /* c == 0 */
16924 break;
16925 }
16926 }
16927 }
16928 break;
16929
16930 case Lisp_Symbol:
16931 /* A symbol: process the value of the symbol recursively
16932 as if it appeared here directly. Avoid error if symbol void.
16933 Special case: if value of symbol is a string, output the string
16934 literally. */
16935 {
16936 register Lisp_Object tem;
16937
16938 /* If the variable is not marked as risky to set
16939 then its contents are risky to use. */
16940 if (NILP (Fget (elt, Qrisky_local_variable)))
16941 risky = 1;
16942
16943 tem = Fboundp (elt);
16944 if (!NILP (tem))
16945 {
16946 tem = Fsymbol_value (elt);
16947 /* If value is a string, output that string literally:
16948 don't check for % within it. */
16949 if (STRINGP (tem))
16950 literal = 1;
16951
16952 if (!EQ (tem, elt))
16953 {
16954 /* Give up right away for nil or t. */
16955 elt = tem;
16956 goto tail_recurse;
16957 }
16958 }
16959 }
16960 break;
16961
16962 case Lisp_Cons:
16963 {
16964 register Lisp_Object car, tem;
16965
16966 /* A cons cell: five distinct cases.
16967 If first element is :eval or :propertize, do something special.
16968 If first element is a string or a cons, process all the elements
16969 and effectively concatenate them.
16970 If first element is a negative number, truncate displaying cdr to
16971 at most that many characters. If positive, pad (with spaces)
16972 to at least that many characters.
16973 If first element is a symbol, process the cadr or caddr recursively
16974 according to whether the symbol's value is non-nil or nil. */
16975 car = XCAR (elt);
16976 if (EQ (car, QCeval))
16977 {
16978 /* An element of the form (:eval FORM) means evaluate FORM
16979 and use the result as mode line elements. */
16980
16981 if (risky)
16982 break;
16983
16984 if (CONSP (XCDR (elt)))
16985 {
16986 Lisp_Object spec;
16987 spec = safe_eval (XCAR (XCDR (elt)));
16988 n += display_mode_element (it, depth, field_width - n,
16989 precision - n, spec, props,
16990 risky);
16991 }
16992 }
16993 else if (EQ (car, QCpropertize))
16994 {
16995 /* An element of the form (:propertize ELT PROPS...)
16996 means display ELT but applying properties PROPS. */
16997
16998 if (risky)
16999 break;
17000
17001 if (CONSP (XCDR (elt)))
17002 n += display_mode_element (it, depth, field_width - n,
17003 precision - n, XCAR (XCDR (elt)),
17004 XCDR (XCDR (elt)), risky);
17005 }
17006 else if (SYMBOLP (car))
17007 {
17008 tem = Fboundp (car);
17009 elt = XCDR (elt);
17010 if (!CONSP (elt))
17011 goto invalid;
17012 /* elt is now the cdr, and we know it is a cons cell.
17013 Use its car if CAR has a non-nil value. */
17014 if (!NILP (tem))
17015 {
17016 tem = Fsymbol_value (car);
17017 if (!NILP (tem))
17018 {
17019 elt = XCAR (elt);
17020 goto tail_recurse;
17021 }
17022 }
17023 /* Symbol's value is nil (or symbol is unbound)
17024 Get the cddr of the original list
17025 and if possible find the caddr and use that. */
17026 elt = XCDR (elt);
17027 if (NILP (elt))
17028 break;
17029 else if (!CONSP (elt))
17030 goto invalid;
17031 elt = XCAR (elt);
17032 goto tail_recurse;
17033 }
17034 else if (INTEGERP (car))
17035 {
17036 register int lim = XINT (car);
17037 elt = XCDR (elt);
17038 if (lim < 0)
17039 {
17040 /* Negative int means reduce maximum width. */
17041 if (precision <= 0)
17042 precision = -lim;
17043 else
17044 precision = min (precision, -lim);
17045 }
17046 else if (lim > 0)
17047 {
17048 /* Padding specified. Don't let it be more than
17049 current maximum. */
17050 if (precision > 0)
17051 lim = min (precision, lim);
17052
17053 /* If that's more padding than already wanted, queue it.
17054 But don't reduce padding already specified even if
17055 that is beyond the current truncation point. */
17056 field_width = max (lim, field_width);
17057 }
17058 goto tail_recurse;
17059 }
17060 else if (STRINGP (car) || CONSP (car))
17061 {
17062 register int limit = 50;
17063 /* Limit is to protect against circular lists. */
17064 while (CONSP (elt)
17065 && --limit > 0
17066 && (precision <= 0 || n < precision))
17067 {
17068 n += display_mode_element (it, depth,
17069 /* Do padding only after the last
17070 element in the list. */
17071 (! CONSP (XCDR (elt))
17072 ? field_width - n
17073 : 0),
17074 precision - n, XCAR (elt),
17075 props, risky);
17076 elt = XCDR (elt);
17077 }
17078 }
17079 }
17080 break;
17081
17082 default:
17083 invalid:
17084 elt = build_string ("*invalid*");
17085 goto tail_recurse;
17086 }
17087
17088 /* Pad to FIELD_WIDTH. */
17089 if (field_width > 0 && n < field_width)
17090 {
17091 switch (mode_line_target)
17092 {
17093 case MODE_LINE_NOPROP:
17094 case MODE_LINE_TITLE:
17095 n += store_mode_line_noprop ("", field_width - n, 0);
17096 break;
17097 case MODE_LINE_STRING:
17098 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
17099 break;
17100 case MODE_LINE_DISPLAY:
17101 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
17102 0, 0, 0);
17103 break;
17104 }
17105 }
17106
17107 return n;
17108 }
17109
17110 /* Store a mode-line string element in mode_line_string_list.
17111
17112 If STRING is non-null, display that C string. Otherwise, the Lisp
17113 string LISP_STRING is displayed.
17114
17115 FIELD_WIDTH is the minimum number of output glyphs to produce.
17116 If STRING has fewer characters than FIELD_WIDTH, pad to the right
17117 with spaces. FIELD_WIDTH <= 0 means don't pad.
17118
17119 PRECISION is the maximum number of characters to output from
17120 STRING. PRECISION <= 0 means don't truncate the string.
17121
17122 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
17123 properties to the string.
17124
17125 PROPS are the properties to add to the string.
17126 The mode_line_string_face face property is always added to the string.
17127 */
17128
17129 static int
17130 store_mode_line_string (string, lisp_string, copy_string, field_width, precision, props)
17131 char *string;
17132 Lisp_Object lisp_string;
17133 int copy_string;
17134 int field_width;
17135 int precision;
17136 Lisp_Object props;
17137 {
17138 int len;
17139 int n = 0;
17140
17141 if (string != NULL)
17142 {
17143 len = strlen (string);
17144 if (precision > 0 && len > precision)
17145 len = precision;
17146 lisp_string = make_string (string, len);
17147 if (NILP (props))
17148 props = mode_line_string_face_prop;
17149 else if (!NILP (mode_line_string_face))
17150 {
17151 Lisp_Object face = Fplist_get (props, Qface);
17152 props = Fcopy_sequence (props);
17153 if (NILP (face))
17154 face = mode_line_string_face;
17155 else
17156 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17157 props = Fplist_put (props, Qface, face);
17158 }
17159 Fadd_text_properties (make_number (0), make_number (len),
17160 props, lisp_string);
17161 }
17162 else
17163 {
17164 len = XFASTINT (Flength (lisp_string));
17165 if (precision > 0 && len > precision)
17166 {
17167 len = precision;
17168 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
17169 precision = -1;
17170 }
17171 if (!NILP (mode_line_string_face))
17172 {
17173 Lisp_Object face;
17174 if (NILP (props))
17175 props = Ftext_properties_at (make_number (0), lisp_string);
17176 face = Fplist_get (props, Qface);
17177 if (NILP (face))
17178 face = mode_line_string_face;
17179 else
17180 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
17181 props = Fcons (Qface, Fcons (face, Qnil));
17182 if (copy_string)
17183 lisp_string = Fcopy_sequence (lisp_string);
17184 }
17185 if (!NILP (props))
17186 Fadd_text_properties (make_number (0), make_number (len),
17187 props, lisp_string);
17188 }
17189
17190 if (len > 0)
17191 {
17192 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17193 n += len;
17194 }
17195
17196 if (field_width > len)
17197 {
17198 field_width -= len;
17199 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
17200 if (!NILP (props))
17201 Fadd_text_properties (make_number (0), make_number (field_width),
17202 props, lisp_string);
17203 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
17204 n += field_width;
17205 }
17206
17207 return n;
17208 }
17209
17210
17211 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
17212 1, 4, 0,
17213 doc: /* Format a string out of a mode line format specification.
17214 First arg FORMAT specifies the mode line format (see `mode-line-format'
17215 for details) to use.
17216
17217 Optional second arg FACE specifies the face property to put
17218 on all characters for which no face is specified.
17219 t means whatever face the window's mode line currently uses
17220 \(either `mode-line' or `mode-line-inactive', depending).
17221 nil means the default is no face property.
17222 If FACE is an integer, the value string has no text properties.
17223
17224 Optional third and fourth args WINDOW and BUFFER specify the window
17225 and buffer to use as the context for the formatting (defaults
17226 are the selected window and the window's buffer). */)
17227 (format, face, window, buffer)
17228 Lisp_Object format, face, window, buffer;
17229 {
17230 struct it it;
17231 int len;
17232 struct window *w;
17233 struct buffer *old_buffer = NULL;
17234 int face_id = -1;
17235 int no_props = INTEGERP (face);
17236 int count = SPECPDL_INDEX ();
17237 Lisp_Object str;
17238 int string_start = 0;
17239
17240 if (NILP (window))
17241 window = selected_window;
17242 CHECK_WINDOW (window);
17243 w = XWINDOW (window);
17244
17245 if (NILP (buffer))
17246 buffer = w->buffer;
17247 CHECK_BUFFER (buffer);
17248
17249 if (NILP (format))
17250 return build_string ("");
17251
17252 if (no_props)
17253 face = Qnil;
17254
17255 if (!NILP (face))
17256 {
17257 if (EQ (face, Qt))
17258 face = (EQ (window, selected_window) ? Qmode_line : Qmode_line_inactive);
17259 face_id = lookup_named_face (XFRAME (WINDOW_FRAME (w)), face, 0, 0);
17260 }
17261
17262 if (face_id < 0)
17263 face_id = DEFAULT_FACE_ID;
17264
17265 if (XBUFFER (buffer) != current_buffer)
17266 old_buffer = current_buffer;
17267
17268 /* Save things including mode_line_proptrans_alist,
17269 and set that to nil so that we don't alter the outer value. */
17270 record_unwind_protect (unwind_format_mode_line,
17271 format_mode_line_unwind_data (old_buffer, 1));
17272 mode_line_proptrans_alist = Qnil;
17273
17274 if (old_buffer)
17275 set_buffer_internal_1 (XBUFFER (buffer));
17276
17277 init_iterator (&it, w, -1, -1, NULL, face_id);
17278
17279 if (no_props)
17280 {
17281 mode_line_target = MODE_LINE_NOPROP;
17282 mode_line_string_face_prop = Qnil;
17283 mode_line_string_list = Qnil;
17284 string_start = MODE_LINE_NOPROP_LEN (0);
17285 }
17286 else
17287 {
17288 mode_line_target = MODE_LINE_STRING;
17289 mode_line_string_list = Qnil;
17290 mode_line_string_face = face;
17291 mode_line_string_face_prop
17292 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
17293 }
17294
17295 push_frame_kboard (it.f);
17296 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
17297 pop_frame_kboard ();
17298
17299 if (no_props)
17300 {
17301 len = MODE_LINE_NOPROP_LEN (string_start);
17302 str = make_string (mode_line_noprop_buf + string_start, len);
17303 }
17304 else
17305 {
17306 mode_line_string_list = Fnreverse (mode_line_string_list);
17307 str = Fmapconcat (intern ("identity"), mode_line_string_list,
17308 make_string ("", 0));
17309 }
17310
17311 unbind_to (count, Qnil);
17312 return str;
17313 }
17314
17315 /* Write a null-terminated, right justified decimal representation of
17316 the positive integer D to BUF using a minimal field width WIDTH. */
17317
17318 static void
17319 pint2str (buf, width, d)
17320 register char *buf;
17321 register int width;
17322 register int d;
17323 {
17324 register char *p = buf;
17325
17326 if (d <= 0)
17327 *p++ = '0';
17328 else
17329 {
17330 while (d > 0)
17331 {
17332 *p++ = d % 10 + '0';
17333 d /= 10;
17334 }
17335 }
17336
17337 for (width -= (int) (p - buf); width > 0; --width)
17338 *p++ = ' ';
17339 *p-- = '\0';
17340 while (p > buf)
17341 {
17342 d = *buf;
17343 *buf++ = *p;
17344 *p-- = d;
17345 }
17346 }
17347
17348 /* Write a null-terminated, right justified decimal and "human
17349 readable" representation of the nonnegative integer D to BUF using
17350 a minimal field width WIDTH. D should be smaller than 999.5e24. */
17351
17352 static const char power_letter[] =
17353 {
17354 0, /* not used */
17355 'k', /* kilo */
17356 'M', /* mega */
17357 'G', /* giga */
17358 'T', /* tera */
17359 'P', /* peta */
17360 'E', /* exa */
17361 'Z', /* zetta */
17362 'Y' /* yotta */
17363 };
17364
17365 static void
17366 pint2hrstr (buf, width, d)
17367 char *buf;
17368 int width;
17369 int d;
17370 {
17371 /* We aim to represent the nonnegative integer D as
17372 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
17373 int quotient = d;
17374 int remainder = 0;
17375 /* -1 means: do not use TENTHS. */
17376 int tenths = -1;
17377 int exponent = 0;
17378
17379 /* Length of QUOTIENT.TENTHS as a string. */
17380 int length;
17381
17382 char * psuffix;
17383 char * p;
17384
17385 if (1000 <= quotient)
17386 {
17387 /* Scale to the appropriate EXPONENT. */
17388 do
17389 {
17390 remainder = quotient % 1000;
17391 quotient /= 1000;
17392 exponent++;
17393 }
17394 while (1000 <= quotient);
17395
17396 /* Round to nearest and decide whether to use TENTHS or not. */
17397 if (quotient <= 9)
17398 {
17399 tenths = remainder / 100;
17400 if (50 <= remainder % 100)
17401 {
17402 if (tenths < 9)
17403 tenths++;
17404 else
17405 {
17406 quotient++;
17407 if (quotient == 10)
17408 tenths = -1;
17409 else
17410 tenths = 0;
17411 }
17412 }
17413 }
17414 else
17415 if (500 <= remainder)
17416 {
17417 if (quotient < 999)
17418 quotient++;
17419 else
17420 {
17421 quotient = 1;
17422 exponent++;
17423 tenths = 0;
17424 }
17425 }
17426 }
17427
17428 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
17429 if (tenths == -1 && quotient <= 99)
17430 if (quotient <= 9)
17431 length = 1;
17432 else
17433 length = 2;
17434 else
17435 length = 3;
17436 p = psuffix = buf + max (width, length);
17437
17438 /* Print EXPONENT. */
17439 if (exponent)
17440 *psuffix++ = power_letter[exponent];
17441 *psuffix = '\0';
17442
17443 /* Print TENTHS. */
17444 if (tenths >= 0)
17445 {
17446 *--p = '0' + tenths;
17447 *--p = '.';
17448 }
17449
17450 /* Print QUOTIENT. */
17451 do
17452 {
17453 int digit = quotient % 10;
17454 *--p = '0' + digit;
17455 }
17456 while ((quotient /= 10) != 0);
17457
17458 /* Print leading spaces. */
17459 while (buf < p)
17460 *--p = ' ';
17461 }
17462
17463 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
17464 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
17465 type of CODING_SYSTEM. Return updated pointer into BUF. */
17466
17467 static unsigned char invalid_eol_type[] = "(*invalid*)";
17468
17469 static char *
17470 decode_mode_spec_coding (coding_system, buf, eol_flag)
17471 Lisp_Object coding_system;
17472 register char *buf;
17473 int eol_flag;
17474 {
17475 Lisp_Object val;
17476 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
17477 const unsigned char *eol_str;
17478 int eol_str_len;
17479 /* The EOL conversion we are using. */
17480 Lisp_Object eoltype;
17481
17482 val = Fget (coding_system, Qcoding_system);
17483 eoltype = Qnil;
17484
17485 if (!VECTORP (val)) /* Not yet decided. */
17486 {
17487 if (multibyte)
17488 *buf++ = '-';
17489 if (eol_flag)
17490 eoltype = eol_mnemonic_undecided;
17491 /* Don't mention EOL conversion if it isn't decided. */
17492 }
17493 else
17494 {
17495 Lisp_Object eolvalue;
17496
17497 eolvalue = Fget (coding_system, Qeol_type);
17498
17499 if (multibyte)
17500 *buf++ = XFASTINT (AREF (val, 1));
17501
17502 if (eol_flag)
17503 {
17504 /* The EOL conversion that is normal on this system. */
17505
17506 if (NILP (eolvalue)) /* Not yet decided. */
17507 eoltype = eol_mnemonic_undecided;
17508 else if (VECTORP (eolvalue)) /* Not yet decided. */
17509 eoltype = eol_mnemonic_undecided;
17510 else /* INTEGERP (eolvalue) -- 0:LF, 1:CRLF, 2:CR */
17511 eoltype = (XFASTINT (eolvalue) == 0
17512 ? eol_mnemonic_unix
17513 : (XFASTINT (eolvalue) == 1
17514 ? eol_mnemonic_dos : eol_mnemonic_mac));
17515 }
17516 }
17517
17518 if (eol_flag)
17519 {
17520 /* Mention the EOL conversion if it is not the usual one. */
17521 if (STRINGP (eoltype))
17522 {
17523 eol_str = SDATA (eoltype);
17524 eol_str_len = SBYTES (eoltype);
17525 }
17526 else if (INTEGERP (eoltype)
17527 && CHAR_VALID_P (XINT (eoltype), 0))
17528 {
17529 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
17530 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
17531 eol_str = tmp;
17532 }
17533 else
17534 {
17535 eol_str = invalid_eol_type;
17536 eol_str_len = sizeof (invalid_eol_type) - 1;
17537 }
17538 bcopy (eol_str, buf, eol_str_len);
17539 buf += eol_str_len;
17540 }
17541
17542 return buf;
17543 }
17544
17545 /* Return a string for the output of a mode line %-spec for window W,
17546 generated by character C. PRECISION >= 0 means don't return a
17547 string longer than that value. FIELD_WIDTH > 0 means pad the
17548 string returned with spaces to that value. Return 1 in *MULTIBYTE
17549 if the result is multibyte text.
17550
17551 Note we operate on the current buffer for most purposes,
17552 the exception being w->base_line_pos. */
17553
17554 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
17555
17556 static char *
17557 decode_mode_spec (w, c, field_width, precision, multibyte)
17558 struct window *w;
17559 register int c;
17560 int field_width, precision;
17561 int *multibyte;
17562 {
17563 Lisp_Object obj;
17564 struct frame *f = XFRAME (WINDOW_FRAME (w));
17565 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
17566 struct buffer *b = current_buffer;
17567
17568 obj = Qnil;
17569 *multibyte = 0;
17570
17571 switch (c)
17572 {
17573 case '*':
17574 if (!NILP (b->read_only))
17575 return "%";
17576 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17577 return "*";
17578 return "-";
17579
17580 case '+':
17581 /* This differs from %* only for a modified read-only buffer. */
17582 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17583 return "*";
17584 if (!NILP (b->read_only))
17585 return "%";
17586 return "-";
17587
17588 case '&':
17589 /* This differs from %* in ignoring read-only-ness. */
17590 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
17591 return "*";
17592 return "-";
17593
17594 case '%':
17595 return "%";
17596
17597 case '[':
17598 {
17599 int i;
17600 char *p;
17601
17602 if (command_loop_level > 5)
17603 return "[[[... ";
17604 p = decode_mode_spec_buf;
17605 for (i = 0; i < command_loop_level; i++)
17606 *p++ = '[';
17607 *p = 0;
17608 return decode_mode_spec_buf;
17609 }
17610
17611 case ']':
17612 {
17613 int i;
17614 char *p;
17615
17616 if (command_loop_level > 5)
17617 return " ...]]]";
17618 p = decode_mode_spec_buf;
17619 for (i = 0; i < command_loop_level; i++)
17620 *p++ = ']';
17621 *p = 0;
17622 return decode_mode_spec_buf;
17623 }
17624
17625 case '-':
17626 {
17627 register int i;
17628
17629 /* Let lots_of_dashes be a string of infinite length. */
17630 if (mode_line_target == MODE_LINE_NOPROP ||
17631 mode_line_target == MODE_LINE_STRING)
17632 return "--";
17633 if (field_width <= 0
17634 || field_width > sizeof (lots_of_dashes))
17635 {
17636 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
17637 decode_mode_spec_buf[i] = '-';
17638 decode_mode_spec_buf[i] = '\0';
17639 return decode_mode_spec_buf;
17640 }
17641 else
17642 return lots_of_dashes;
17643 }
17644
17645 case 'b':
17646 obj = b->name;
17647 break;
17648
17649 case 'c':
17650 {
17651 int col = (int) current_column (); /* iftc */
17652 w->column_number_displayed = make_number (col);
17653 pint2str (decode_mode_spec_buf, field_width, col);
17654 return decode_mode_spec_buf;
17655 }
17656
17657 case 'e':
17658 #ifndef SYSTEM_MALLOC
17659 {
17660 if (NILP (Vmemory_full))
17661 return "";
17662 else
17663 return "!MEM FULL! ";
17664 }
17665 #else
17666 return "";
17667 #endif
17668
17669 case 'F':
17670 /* %F displays the frame name. */
17671 if (!NILP (f->title))
17672 return (char *) SDATA (f->title);
17673 if (f->explicit_name || ! FRAME_WINDOW_P (f))
17674 return (char *) SDATA (f->name);
17675 return "Emacs";
17676
17677 case 'f':
17678 obj = b->filename;
17679 break;
17680
17681 case 'i':
17682 {
17683 int size = ZV - BEGV;
17684 pint2str (decode_mode_spec_buf, field_width, size);
17685 return decode_mode_spec_buf;
17686 }
17687
17688 case 'I':
17689 {
17690 int size = ZV - BEGV;
17691 pint2hrstr (decode_mode_spec_buf, field_width, size);
17692 return decode_mode_spec_buf;
17693 }
17694
17695 case 'l':
17696 {
17697 int startpos = XMARKER (w->start)->charpos;
17698 int startpos_byte = marker_byte_position (w->start);
17699 int line, linepos, linepos_byte, topline;
17700 int nlines, junk;
17701 int height = WINDOW_TOTAL_LINES (w);
17702
17703 /* If we decided that this buffer isn't suitable for line numbers,
17704 don't forget that too fast. */
17705 if (EQ (w->base_line_pos, w->buffer))
17706 goto no_value;
17707 /* But do forget it, if the window shows a different buffer now. */
17708 else if (BUFFERP (w->base_line_pos))
17709 w->base_line_pos = Qnil;
17710
17711 /* If the buffer is very big, don't waste time. */
17712 if (INTEGERP (Vline_number_display_limit)
17713 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
17714 {
17715 w->base_line_pos = Qnil;
17716 w->base_line_number = Qnil;
17717 goto no_value;
17718 }
17719
17720 if (!NILP (w->base_line_number)
17721 && !NILP (w->base_line_pos)
17722 && XFASTINT (w->base_line_pos) <= startpos)
17723 {
17724 line = XFASTINT (w->base_line_number);
17725 linepos = XFASTINT (w->base_line_pos);
17726 linepos_byte = buf_charpos_to_bytepos (b, linepos);
17727 }
17728 else
17729 {
17730 line = 1;
17731 linepos = BUF_BEGV (b);
17732 linepos_byte = BUF_BEGV_BYTE (b);
17733 }
17734
17735 /* Count lines from base line to window start position. */
17736 nlines = display_count_lines (linepos, linepos_byte,
17737 startpos_byte,
17738 startpos, &junk);
17739
17740 topline = nlines + line;
17741
17742 /* Determine a new base line, if the old one is too close
17743 or too far away, or if we did not have one.
17744 "Too close" means it's plausible a scroll-down would
17745 go back past it. */
17746 if (startpos == BUF_BEGV (b))
17747 {
17748 w->base_line_number = make_number (topline);
17749 w->base_line_pos = make_number (BUF_BEGV (b));
17750 }
17751 else if (nlines < height + 25 || nlines > height * 3 + 50
17752 || linepos == BUF_BEGV (b))
17753 {
17754 int limit = BUF_BEGV (b);
17755 int limit_byte = BUF_BEGV_BYTE (b);
17756 int position;
17757 int distance = (height * 2 + 30) * line_number_display_limit_width;
17758
17759 if (startpos - distance > limit)
17760 {
17761 limit = startpos - distance;
17762 limit_byte = CHAR_TO_BYTE (limit);
17763 }
17764
17765 nlines = display_count_lines (startpos, startpos_byte,
17766 limit_byte,
17767 - (height * 2 + 30),
17768 &position);
17769 /* If we couldn't find the lines we wanted within
17770 line_number_display_limit_width chars per line,
17771 give up on line numbers for this window. */
17772 if (position == limit_byte && limit == startpos - distance)
17773 {
17774 w->base_line_pos = w->buffer;
17775 w->base_line_number = Qnil;
17776 goto no_value;
17777 }
17778
17779 w->base_line_number = make_number (topline - nlines);
17780 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
17781 }
17782
17783 /* Now count lines from the start pos to point. */
17784 nlines = display_count_lines (startpos, startpos_byte,
17785 PT_BYTE, PT, &junk);
17786
17787 /* Record that we did display the line number. */
17788 line_number_displayed = 1;
17789
17790 /* Make the string to show. */
17791 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
17792 return decode_mode_spec_buf;
17793 no_value:
17794 {
17795 char* p = decode_mode_spec_buf;
17796 int pad = field_width - 2;
17797 while (pad-- > 0)
17798 *p++ = ' ';
17799 *p++ = '?';
17800 *p++ = '?';
17801 *p = '\0';
17802 return decode_mode_spec_buf;
17803 }
17804 }
17805 break;
17806
17807 case 'm':
17808 obj = b->mode_name;
17809 break;
17810
17811 case 'n':
17812 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
17813 return " Narrow";
17814 break;
17815
17816 case 'p':
17817 {
17818 int pos = marker_position (w->start);
17819 int total = BUF_ZV (b) - BUF_BEGV (b);
17820
17821 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
17822 {
17823 if (pos <= BUF_BEGV (b))
17824 return "All";
17825 else
17826 return "Bottom";
17827 }
17828 else if (pos <= BUF_BEGV (b))
17829 return "Top";
17830 else
17831 {
17832 if (total > 1000000)
17833 /* Do it differently for a large value, to avoid overflow. */
17834 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17835 else
17836 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
17837 /* We can't normally display a 3-digit number,
17838 so get us a 2-digit number that is close. */
17839 if (total == 100)
17840 total = 99;
17841 sprintf (decode_mode_spec_buf, "%2d%%", total);
17842 return decode_mode_spec_buf;
17843 }
17844 }
17845
17846 /* Display percentage of size above the bottom of the screen. */
17847 case 'P':
17848 {
17849 int toppos = marker_position (w->start);
17850 int botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
17851 int total = BUF_ZV (b) - BUF_BEGV (b);
17852
17853 if (botpos >= BUF_ZV (b))
17854 {
17855 if (toppos <= BUF_BEGV (b))
17856 return "All";
17857 else
17858 return "Bottom";
17859 }
17860 else
17861 {
17862 if (total > 1000000)
17863 /* Do it differently for a large value, to avoid overflow. */
17864 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
17865 else
17866 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
17867 /* We can't normally display a 3-digit number,
17868 so get us a 2-digit number that is close. */
17869 if (total == 100)
17870 total = 99;
17871 if (toppos <= BUF_BEGV (b))
17872 sprintf (decode_mode_spec_buf, "Top%2d%%", total);
17873 else
17874 sprintf (decode_mode_spec_buf, "%2d%%", total);
17875 return decode_mode_spec_buf;
17876 }
17877 }
17878
17879 case 's':
17880 /* status of process */
17881 obj = Fget_buffer_process (Fcurrent_buffer ());
17882 if (NILP (obj))
17883 return "no process";
17884 #ifdef subprocesses
17885 obj = Fsymbol_name (Fprocess_status (obj));
17886 #endif
17887 break;
17888
17889 case 't': /* indicate TEXT or BINARY */
17890 #ifdef MODE_LINE_BINARY_TEXT
17891 return MODE_LINE_BINARY_TEXT (b);
17892 #else
17893 return "T";
17894 #endif
17895
17896 case 'z':
17897 /* coding-system (not including end-of-line format) */
17898 case 'Z':
17899 /* coding-system (including end-of-line type) */
17900 {
17901 int eol_flag = (c == 'Z');
17902 char *p = decode_mode_spec_buf;
17903
17904 if (! FRAME_WINDOW_P (f))
17905 {
17906 /* No need to mention EOL here--the terminal never needs
17907 to do EOL conversion. */
17908 p = decode_mode_spec_coding (keyboard_coding.symbol, p, 0);
17909 p = decode_mode_spec_coding (terminal_coding.symbol, p, 0);
17910 }
17911 p = decode_mode_spec_coding (b->buffer_file_coding_system,
17912 p, eol_flag);
17913
17914 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
17915 #ifdef subprocesses
17916 obj = Fget_buffer_process (Fcurrent_buffer ());
17917 if (PROCESSP (obj))
17918 {
17919 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
17920 p, eol_flag);
17921 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
17922 p, eol_flag);
17923 }
17924 #endif /* subprocesses */
17925 #endif /* 0 */
17926 *p = 0;
17927 return decode_mode_spec_buf;
17928 }
17929 }
17930
17931 if (STRINGP (obj))
17932 {
17933 *multibyte = STRING_MULTIBYTE (obj);
17934 return (char *) SDATA (obj);
17935 }
17936 else
17937 return "";
17938 }
17939
17940
17941 /* Count up to COUNT lines starting from START / START_BYTE.
17942 But don't go beyond LIMIT_BYTE.
17943 Return the number of lines thus found (always nonnegative).
17944
17945 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
17946
17947 static int
17948 display_count_lines (start, start_byte, limit_byte, count, byte_pos_ptr)
17949 int start, start_byte, limit_byte, count;
17950 int *byte_pos_ptr;
17951 {
17952 register unsigned char *cursor;
17953 unsigned char *base;
17954
17955 register int ceiling;
17956 register unsigned char *ceiling_addr;
17957 int orig_count = count;
17958
17959 /* If we are not in selective display mode,
17960 check only for newlines. */
17961 int selective_display = (!NILP (current_buffer->selective_display)
17962 && !INTEGERP (current_buffer->selective_display));
17963
17964 if (count > 0)
17965 {
17966 while (start_byte < limit_byte)
17967 {
17968 ceiling = BUFFER_CEILING_OF (start_byte);
17969 ceiling = min (limit_byte - 1, ceiling);
17970 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
17971 base = (cursor = BYTE_POS_ADDR (start_byte));
17972 while (1)
17973 {
17974 if (selective_display)
17975 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
17976 ;
17977 else
17978 while (*cursor != '\n' && ++cursor != ceiling_addr)
17979 ;
17980
17981 if (cursor != ceiling_addr)
17982 {
17983 if (--count == 0)
17984 {
17985 start_byte += cursor - base + 1;
17986 *byte_pos_ptr = start_byte;
17987 return orig_count;
17988 }
17989 else
17990 if (++cursor == ceiling_addr)
17991 break;
17992 }
17993 else
17994 break;
17995 }
17996 start_byte += cursor - base;
17997 }
17998 }
17999 else
18000 {
18001 while (start_byte > limit_byte)
18002 {
18003 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
18004 ceiling = max (limit_byte, ceiling);
18005 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
18006 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
18007 while (1)
18008 {
18009 if (selective_display)
18010 while (--cursor != ceiling_addr
18011 && *cursor != '\n' && *cursor != 015)
18012 ;
18013 else
18014 while (--cursor != ceiling_addr && *cursor != '\n')
18015 ;
18016
18017 if (cursor != ceiling_addr)
18018 {
18019 if (++count == 0)
18020 {
18021 start_byte += cursor - base + 1;
18022 *byte_pos_ptr = start_byte;
18023 /* When scanning backwards, we should
18024 not count the newline posterior to which we stop. */
18025 return - orig_count - 1;
18026 }
18027 }
18028 else
18029 break;
18030 }
18031 /* Here we add 1 to compensate for the last decrement
18032 of CURSOR, which took it past the valid range. */
18033 start_byte += cursor - base + 1;
18034 }
18035 }
18036
18037 *byte_pos_ptr = limit_byte;
18038
18039 if (count < 0)
18040 return - orig_count + count;
18041 return orig_count - count;
18042
18043 }
18044
18045
18046 \f
18047 /***********************************************************************
18048 Displaying strings
18049 ***********************************************************************/
18050
18051 /* Display a NUL-terminated string, starting with index START.
18052
18053 If STRING is non-null, display that C string. Otherwise, the Lisp
18054 string LISP_STRING is displayed.
18055
18056 If FACE_STRING is not nil, FACE_STRING_POS is a position in
18057 FACE_STRING. Display STRING or LISP_STRING with the face at
18058 FACE_STRING_POS in FACE_STRING:
18059
18060 Display the string in the environment given by IT, but use the
18061 standard display table, temporarily.
18062
18063 FIELD_WIDTH is the minimum number of output glyphs to produce.
18064 If STRING has fewer characters than FIELD_WIDTH, pad to the right
18065 with spaces. If STRING has more characters, more than FIELD_WIDTH
18066 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
18067
18068 PRECISION is the maximum number of characters to output from
18069 STRING. PRECISION < 0 means don't truncate the string.
18070
18071 This is roughly equivalent to printf format specifiers:
18072
18073 FIELD_WIDTH PRECISION PRINTF
18074 ----------------------------------------
18075 -1 -1 %s
18076 -1 10 %.10s
18077 10 -1 %10s
18078 20 10 %20.10s
18079
18080 MULTIBYTE zero means do not display multibyte chars, > 0 means do
18081 display them, and < 0 means obey the current buffer's value of
18082 enable_multibyte_characters.
18083
18084 Value is the number of columns displayed. */
18085
18086 static int
18087 display_string (string, lisp_string, face_string, face_string_pos,
18088 start, it, field_width, precision, max_x, multibyte)
18089 unsigned char *string;
18090 Lisp_Object lisp_string;
18091 Lisp_Object face_string;
18092 int face_string_pos;
18093 int start;
18094 struct it *it;
18095 int field_width, precision, max_x;
18096 int multibyte;
18097 {
18098 int hpos_at_start = it->hpos;
18099 int saved_face_id = it->face_id;
18100 struct glyph_row *row = it->glyph_row;
18101
18102 /* Initialize the iterator IT for iteration over STRING beginning
18103 with index START. */
18104 reseat_to_string (it, string, lisp_string, start,
18105 precision, field_width, multibyte);
18106
18107 /* If displaying STRING, set up the face of the iterator
18108 from LISP_STRING, if that's given. */
18109 if (STRINGP (face_string))
18110 {
18111 int endptr;
18112 struct face *face;
18113
18114 it->face_id
18115 = face_at_string_position (it->w, face_string, face_string_pos,
18116 0, it->region_beg_charpos,
18117 it->region_end_charpos,
18118 &endptr, it->base_face_id, 0);
18119 face = FACE_FROM_ID (it->f, it->face_id);
18120 it->face_box_p = face->box != FACE_NO_BOX;
18121 }
18122
18123 /* Set max_x to the maximum allowed X position. Don't let it go
18124 beyond the right edge of the window. */
18125 if (max_x <= 0)
18126 max_x = it->last_visible_x;
18127 else
18128 max_x = min (max_x, it->last_visible_x);
18129
18130 /* Skip over display elements that are not visible. because IT->w is
18131 hscrolled. */
18132 if (it->current_x < it->first_visible_x)
18133 move_it_in_display_line_to (it, 100000, it->first_visible_x,
18134 MOVE_TO_POS | MOVE_TO_X);
18135
18136 row->ascent = it->max_ascent;
18137 row->height = it->max_ascent + it->max_descent;
18138 row->phys_ascent = it->max_phys_ascent;
18139 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18140 row->extra_line_spacing = it->max_extra_line_spacing;
18141
18142 /* This condition is for the case that we are called with current_x
18143 past last_visible_x. */
18144 while (it->current_x < max_x)
18145 {
18146 int x_before, x, n_glyphs_before, i, nglyphs;
18147
18148 /* Get the next display element. */
18149 if (!get_next_display_element (it))
18150 break;
18151
18152 /* Produce glyphs. */
18153 x_before = it->current_x;
18154 n_glyphs_before = it->glyph_row->used[TEXT_AREA];
18155 PRODUCE_GLYPHS (it);
18156
18157 nglyphs = it->glyph_row->used[TEXT_AREA] - n_glyphs_before;
18158 i = 0;
18159 x = x_before;
18160 while (i < nglyphs)
18161 {
18162 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18163
18164 if (!it->truncate_lines_p
18165 && x + glyph->pixel_width > max_x)
18166 {
18167 /* End of continued line or max_x reached. */
18168 if (CHAR_GLYPH_PADDING_P (*glyph))
18169 {
18170 /* A wide character is unbreakable. */
18171 it->glyph_row->used[TEXT_AREA] = n_glyphs_before;
18172 it->current_x = x_before;
18173 }
18174 else
18175 {
18176 it->glyph_row->used[TEXT_AREA] = n_glyphs_before + i;
18177 it->current_x = x;
18178 }
18179 break;
18180 }
18181 else if (x + glyph->pixel_width > it->first_visible_x)
18182 {
18183 /* Glyph is at least partially visible. */
18184 ++it->hpos;
18185 if (x < it->first_visible_x)
18186 it->glyph_row->x = x - it->first_visible_x;
18187 }
18188 else
18189 {
18190 /* Glyph is off the left margin of the display area.
18191 Should not happen. */
18192 abort ();
18193 }
18194
18195 row->ascent = max (row->ascent, it->max_ascent);
18196 row->height = max (row->height, it->max_ascent + it->max_descent);
18197 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18198 row->phys_height = max (row->phys_height,
18199 it->max_phys_ascent + it->max_phys_descent);
18200 row->extra_line_spacing = max (row->extra_line_spacing,
18201 it->max_extra_line_spacing);
18202 x += glyph->pixel_width;
18203 ++i;
18204 }
18205
18206 /* Stop if max_x reached. */
18207 if (i < nglyphs)
18208 break;
18209
18210 /* Stop at line ends. */
18211 if (ITERATOR_AT_END_OF_LINE_P (it))
18212 {
18213 it->continuation_lines_width = 0;
18214 break;
18215 }
18216
18217 set_iterator_to_next (it, 1);
18218
18219 /* Stop if truncating at the right edge. */
18220 if (it->truncate_lines_p
18221 && it->current_x >= it->last_visible_x)
18222 {
18223 /* Add truncation mark, but don't do it if the line is
18224 truncated at a padding space. */
18225 if (IT_CHARPOS (*it) < it->string_nchars)
18226 {
18227 if (!FRAME_WINDOW_P (it->f))
18228 {
18229 int i, n;
18230
18231 if (it->current_x > it->last_visible_x)
18232 {
18233 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18234 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18235 break;
18236 for (n = row->used[TEXT_AREA]; i < n; ++i)
18237 {
18238 row->used[TEXT_AREA] = i;
18239 produce_special_glyphs (it, IT_TRUNCATION);
18240 }
18241 }
18242 produce_special_glyphs (it, IT_TRUNCATION);
18243 }
18244 it->glyph_row->truncated_on_right_p = 1;
18245 }
18246 break;
18247 }
18248 }
18249
18250 /* Maybe insert a truncation at the left. */
18251 if (it->first_visible_x
18252 && IT_CHARPOS (*it) > 0)
18253 {
18254 if (!FRAME_WINDOW_P (it->f))
18255 insert_left_trunc_glyphs (it);
18256 it->glyph_row->truncated_on_left_p = 1;
18257 }
18258
18259 it->face_id = saved_face_id;
18260
18261 /* Value is number of columns displayed. */
18262 return it->hpos - hpos_at_start;
18263 }
18264
18265
18266 \f
18267 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
18268 appears as an element of LIST or as the car of an element of LIST.
18269 If PROPVAL is a list, compare each element against LIST in that
18270 way, and return 1/2 if any element of PROPVAL is found in LIST.
18271 Otherwise return 0. This function cannot quit.
18272 The return value is 2 if the text is invisible but with an ellipsis
18273 and 1 if it's invisible and without an ellipsis. */
18274
18275 int
18276 invisible_p (propval, list)
18277 register Lisp_Object propval;
18278 Lisp_Object list;
18279 {
18280 register Lisp_Object tail, proptail;
18281
18282 for (tail = list; CONSP (tail); tail = XCDR (tail))
18283 {
18284 register Lisp_Object tem;
18285 tem = XCAR (tail);
18286 if (EQ (propval, tem))
18287 return 1;
18288 if (CONSP (tem) && EQ (propval, XCAR (tem)))
18289 return NILP (XCDR (tem)) ? 1 : 2;
18290 }
18291
18292 if (CONSP (propval))
18293 {
18294 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
18295 {
18296 Lisp_Object propelt;
18297 propelt = XCAR (proptail);
18298 for (tail = list; CONSP (tail); tail = XCDR (tail))
18299 {
18300 register Lisp_Object tem;
18301 tem = XCAR (tail);
18302 if (EQ (propelt, tem))
18303 return 1;
18304 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
18305 return NILP (XCDR (tem)) ? 1 : 2;
18306 }
18307 }
18308 }
18309
18310 return 0;
18311 }
18312
18313 /* Calculate a width or height in pixels from a specification using
18314 the following elements:
18315
18316 SPEC ::=
18317 NUM - a (fractional) multiple of the default font width/height
18318 (NUM) - specifies exactly NUM pixels
18319 UNIT - a fixed number of pixels, see below.
18320 ELEMENT - size of a display element in pixels, see below.
18321 (NUM . SPEC) - equals NUM * SPEC
18322 (+ SPEC SPEC ...) - add pixel values
18323 (- SPEC SPEC ...) - subtract pixel values
18324 (- SPEC) - negate pixel value
18325
18326 NUM ::=
18327 INT or FLOAT - a number constant
18328 SYMBOL - use symbol's (buffer local) variable binding.
18329
18330 UNIT ::=
18331 in - pixels per inch *)
18332 mm - pixels per 1/1000 meter *)
18333 cm - pixels per 1/100 meter *)
18334 width - width of current font in pixels.
18335 height - height of current font in pixels.
18336
18337 *) using the ratio(s) defined in display-pixels-per-inch.
18338
18339 ELEMENT ::=
18340
18341 left-fringe - left fringe width in pixels
18342 right-fringe - right fringe width in pixels
18343
18344 left-margin - left margin width in pixels
18345 right-margin - right margin width in pixels
18346
18347 scroll-bar - scroll-bar area width in pixels
18348
18349 Examples:
18350
18351 Pixels corresponding to 5 inches:
18352 (5 . in)
18353
18354 Total width of non-text areas on left side of window (if scroll-bar is on left):
18355 '(space :width (+ left-fringe left-margin scroll-bar))
18356
18357 Align to first text column (in header line):
18358 '(space :align-to 0)
18359
18360 Align to middle of text area minus half the width of variable `my-image'
18361 containing a loaded image:
18362 '(space :align-to (0.5 . (- text my-image)))
18363
18364 Width of left margin minus width of 1 character in the default font:
18365 '(space :width (- left-margin 1))
18366
18367 Width of left margin minus width of 2 characters in the current font:
18368 '(space :width (- left-margin (2 . width)))
18369
18370 Center 1 character over left-margin (in header line):
18371 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
18372
18373 Different ways to express width of left fringe plus left margin minus one pixel:
18374 '(space :width (- (+ left-fringe left-margin) (1)))
18375 '(space :width (+ left-fringe left-margin (- (1))))
18376 '(space :width (+ left-fringe left-margin (-1)))
18377
18378 */
18379
18380 #define NUMVAL(X) \
18381 ((INTEGERP (X) || FLOATP (X)) \
18382 ? XFLOATINT (X) \
18383 : - 1)
18384
18385 int
18386 calc_pixel_width_or_height (res, it, prop, font, width_p, align_to)
18387 double *res;
18388 struct it *it;
18389 Lisp_Object prop;
18390 void *font;
18391 int width_p, *align_to;
18392 {
18393 double pixels;
18394
18395 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
18396 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
18397
18398 if (NILP (prop))
18399 return OK_PIXELS (0);
18400
18401 if (SYMBOLP (prop))
18402 {
18403 if (SCHARS (SYMBOL_NAME (prop)) == 2)
18404 {
18405 char *unit = SDATA (SYMBOL_NAME (prop));
18406
18407 if (unit[0] == 'i' && unit[1] == 'n')
18408 pixels = 1.0;
18409 else if (unit[0] == 'm' && unit[1] == 'm')
18410 pixels = 25.4;
18411 else if (unit[0] == 'c' && unit[1] == 'm')
18412 pixels = 2.54;
18413 else
18414 pixels = 0;
18415 if (pixels > 0)
18416 {
18417 double ppi;
18418 #ifdef HAVE_WINDOW_SYSTEM
18419 if (FRAME_WINDOW_P (it->f)
18420 && (ppi = (width_p
18421 ? FRAME_X_DISPLAY_INFO (it->f)->resx
18422 : FRAME_X_DISPLAY_INFO (it->f)->resy),
18423 ppi > 0))
18424 return OK_PIXELS (ppi / pixels);
18425 #endif
18426
18427 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
18428 || (CONSP (Vdisplay_pixels_per_inch)
18429 && (ppi = (width_p
18430 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
18431 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
18432 ppi > 0)))
18433 return OK_PIXELS (ppi / pixels);
18434
18435 return 0;
18436 }
18437 }
18438
18439 #ifdef HAVE_WINDOW_SYSTEM
18440 if (EQ (prop, Qheight))
18441 return OK_PIXELS (font ? FONT_HEIGHT ((XFontStruct *)font) : FRAME_LINE_HEIGHT (it->f));
18442 if (EQ (prop, Qwidth))
18443 return OK_PIXELS (font ? FONT_WIDTH ((XFontStruct *)font) : FRAME_COLUMN_WIDTH (it->f));
18444 #else
18445 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
18446 return OK_PIXELS (1);
18447 #endif
18448
18449 if (EQ (prop, Qtext))
18450 return OK_PIXELS (width_p
18451 ? window_box_width (it->w, TEXT_AREA)
18452 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
18453
18454 if (align_to && *align_to < 0)
18455 {
18456 *res = 0;
18457 if (EQ (prop, Qleft))
18458 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
18459 if (EQ (prop, Qright))
18460 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
18461 if (EQ (prop, Qcenter))
18462 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
18463 + window_box_width (it->w, TEXT_AREA) / 2);
18464 if (EQ (prop, Qleft_fringe))
18465 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18466 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
18467 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
18468 if (EQ (prop, Qright_fringe))
18469 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18470 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18471 : window_box_right_offset (it->w, TEXT_AREA));
18472 if (EQ (prop, Qleft_margin))
18473 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
18474 if (EQ (prop, Qright_margin))
18475 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
18476 if (EQ (prop, Qscroll_bar))
18477 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
18478 ? 0
18479 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
18480 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
18481 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
18482 : 0)));
18483 }
18484 else
18485 {
18486 if (EQ (prop, Qleft_fringe))
18487 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
18488 if (EQ (prop, Qright_fringe))
18489 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
18490 if (EQ (prop, Qleft_margin))
18491 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
18492 if (EQ (prop, Qright_margin))
18493 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
18494 if (EQ (prop, Qscroll_bar))
18495 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
18496 }
18497
18498 prop = Fbuffer_local_value (prop, it->w->buffer);
18499 }
18500
18501 if (INTEGERP (prop) || FLOATP (prop))
18502 {
18503 int base_unit = (width_p
18504 ? FRAME_COLUMN_WIDTH (it->f)
18505 : FRAME_LINE_HEIGHT (it->f));
18506 return OK_PIXELS (XFLOATINT (prop) * base_unit);
18507 }
18508
18509 if (CONSP (prop))
18510 {
18511 Lisp_Object car = XCAR (prop);
18512 Lisp_Object cdr = XCDR (prop);
18513
18514 if (SYMBOLP (car))
18515 {
18516 #ifdef HAVE_WINDOW_SYSTEM
18517 if (valid_image_p (prop))
18518 {
18519 int id = lookup_image (it->f, prop);
18520 struct image *img = IMAGE_FROM_ID (it->f, id);
18521
18522 return OK_PIXELS (width_p ? img->width : img->height);
18523 }
18524 #endif
18525 if (EQ (car, Qplus) || EQ (car, Qminus))
18526 {
18527 int first = 1;
18528 double px;
18529
18530 pixels = 0;
18531 while (CONSP (cdr))
18532 {
18533 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
18534 font, width_p, align_to))
18535 return 0;
18536 if (first)
18537 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
18538 else
18539 pixels += px;
18540 cdr = XCDR (cdr);
18541 }
18542 if (EQ (car, Qminus))
18543 pixels = -pixels;
18544 return OK_PIXELS (pixels);
18545 }
18546
18547 car = Fbuffer_local_value (car, it->w->buffer);
18548 }
18549
18550 if (INTEGERP (car) || FLOATP (car))
18551 {
18552 double fact;
18553 pixels = XFLOATINT (car);
18554 if (NILP (cdr))
18555 return OK_PIXELS (pixels);
18556 if (calc_pixel_width_or_height (&fact, it, cdr,
18557 font, width_p, align_to))
18558 return OK_PIXELS (pixels * fact);
18559 return 0;
18560 }
18561
18562 return 0;
18563 }
18564
18565 return 0;
18566 }
18567
18568 \f
18569 /***********************************************************************
18570 Glyph Display
18571 ***********************************************************************/
18572
18573 #ifdef HAVE_WINDOW_SYSTEM
18574
18575 #if GLYPH_DEBUG
18576
18577 void
18578 dump_glyph_string (s)
18579 struct glyph_string *s;
18580 {
18581 fprintf (stderr, "glyph string\n");
18582 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
18583 s->x, s->y, s->width, s->height);
18584 fprintf (stderr, " ybase = %d\n", s->ybase);
18585 fprintf (stderr, " hl = %d\n", s->hl);
18586 fprintf (stderr, " left overhang = %d, right = %d\n",
18587 s->left_overhang, s->right_overhang);
18588 fprintf (stderr, " nchars = %d\n", s->nchars);
18589 fprintf (stderr, " extends to end of line = %d\n",
18590 s->extends_to_end_of_line_p);
18591 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
18592 fprintf (stderr, " bg width = %d\n", s->background_width);
18593 }
18594
18595 #endif /* GLYPH_DEBUG */
18596
18597 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
18598 of XChar2b structures for S; it can't be allocated in
18599 init_glyph_string because it must be allocated via `alloca'. W
18600 is the window on which S is drawn. ROW and AREA are the glyph row
18601 and area within the row from which S is constructed. START is the
18602 index of the first glyph structure covered by S. HL is a
18603 face-override for drawing S. */
18604
18605 #ifdef HAVE_NTGUI
18606 #define OPTIONAL_HDC(hdc) hdc,
18607 #define DECLARE_HDC(hdc) HDC hdc;
18608 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
18609 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
18610 #endif
18611
18612 #ifndef OPTIONAL_HDC
18613 #define OPTIONAL_HDC(hdc)
18614 #define DECLARE_HDC(hdc)
18615 #define ALLOCATE_HDC(hdc, f)
18616 #define RELEASE_HDC(hdc, f)
18617 #endif
18618
18619 static void
18620 init_glyph_string (s, OPTIONAL_HDC (hdc) char2b, w, row, area, start, hl)
18621 struct glyph_string *s;
18622 DECLARE_HDC (hdc)
18623 XChar2b *char2b;
18624 struct window *w;
18625 struct glyph_row *row;
18626 enum glyph_row_area area;
18627 int start;
18628 enum draw_glyphs_face hl;
18629 {
18630 bzero (s, sizeof *s);
18631 s->w = w;
18632 s->f = XFRAME (w->frame);
18633 #ifdef HAVE_NTGUI
18634 s->hdc = hdc;
18635 #endif
18636 s->display = FRAME_X_DISPLAY (s->f);
18637 s->window = FRAME_X_WINDOW (s->f);
18638 s->char2b = char2b;
18639 s->hl = hl;
18640 s->row = row;
18641 s->area = area;
18642 s->first_glyph = row->glyphs[area] + start;
18643 s->height = row->height;
18644 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
18645
18646 /* Display the internal border below the tool-bar window. */
18647 if (WINDOWP (s->f->tool_bar_window)
18648 && s->w == XWINDOW (s->f->tool_bar_window))
18649 s->y -= FRAME_INTERNAL_BORDER_WIDTH (s->f);
18650
18651 s->ybase = s->y + row->ascent;
18652 }
18653
18654
18655 /* Append the list of glyph strings with head H and tail T to the list
18656 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
18657
18658 static INLINE void
18659 append_glyph_string_lists (head, tail, h, t)
18660 struct glyph_string **head, **tail;
18661 struct glyph_string *h, *t;
18662 {
18663 if (h)
18664 {
18665 if (*head)
18666 (*tail)->next = h;
18667 else
18668 *head = h;
18669 h->prev = *tail;
18670 *tail = t;
18671 }
18672 }
18673
18674
18675 /* Prepend the list of glyph strings with head H and tail T to the
18676 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
18677 result. */
18678
18679 static INLINE void
18680 prepend_glyph_string_lists (head, tail, h, t)
18681 struct glyph_string **head, **tail;
18682 struct glyph_string *h, *t;
18683 {
18684 if (h)
18685 {
18686 if (*head)
18687 (*head)->prev = t;
18688 else
18689 *tail = t;
18690 t->next = *head;
18691 *head = h;
18692 }
18693 }
18694
18695
18696 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
18697 Set *HEAD and *TAIL to the resulting list. */
18698
18699 static INLINE void
18700 append_glyph_string (head, tail, s)
18701 struct glyph_string **head, **tail;
18702 struct glyph_string *s;
18703 {
18704 s->next = s->prev = NULL;
18705 append_glyph_string_lists (head, tail, s, s);
18706 }
18707
18708
18709 /* Get face and two-byte form of character glyph GLYPH on frame F.
18710 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
18711 a pointer to a realized face that is ready for display. */
18712
18713 static INLINE struct face *
18714 get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
18715 struct frame *f;
18716 struct glyph *glyph;
18717 XChar2b *char2b;
18718 int *two_byte_p;
18719 {
18720 struct face *face;
18721
18722 xassert (glyph->type == CHAR_GLYPH);
18723 face = FACE_FROM_ID (f, glyph->face_id);
18724
18725 if (two_byte_p)
18726 *two_byte_p = 0;
18727
18728 if (!glyph->multibyte_p)
18729 {
18730 /* Unibyte case. We don't have to encode, but we have to make
18731 sure to use a face suitable for unibyte. */
18732 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18733 }
18734 else if (glyph->u.ch < 128)
18735 {
18736 /* Case of ASCII in a face known to fit ASCII. */
18737 STORE_XCHAR2B (char2b, 0, glyph->u.ch);
18738 }
18739 else
18740 {
18741 int c1, c2, charset;
18742
18743 /* Split characters into bytes. If c2 is -1 afterwards, C is
18744 really a one-byte character so that byte1 is zero. */
18745 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
18746 if (c2 > 0)
18747 STORE_XCHAR2B (char2b, c1, c2);
18748 else
18749 STORE_XCHAR2B (char2b, 0, c1);
18750
18751 /* Maybe encode the character in *CHAR2B. */
18752 if (charset != CHARSET_ASCII)
18753 {
18754 struct font_info *font_info
18755 = FONT_INFO_FROM_ID (f, face->font_info_id);
18756 if (font_info)
18757 glyph->font_type
18758 = rif->encode_char (glyph->u.ch, char2b, font_info, two_byte_p);
18759 }
18760 }
18761
18762 /* Make sure X resources of the face are allocated. */
18763 xassert (face != NULL);
18764 PREPARE_FACE_FOR_DISPLAY (f, face);
18765 return face;
18766 }
18767
18768
18769 /* Fill glyph string S with composition components specified by S->cmp.
18770
18771 FACES is an array of faces for all components of this composition.
18772 S->gidx is the index of the first component for S.
18773
18774 OVERLAPS non-zero means S should draw the foreground only, and use
18775 its physical height for clipping. See also draw_glyphs.
18776
18777 Value is the index of a component not in S. */
18778
18779 static int
18780 fill_composite_glyph_string (s, faces, overlaps)
18781 struct glyph_string *s;
18782 struct face **faces;
18783 int overlaps;
18784 {
18785 int i;
18786
18787 xassert (s);
18788
18789 s->for_overlaps = overlaps;
18790
18791 s->face = faces[s->gidx];
18792 s->font = s->face->font;
18793 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18794
18795 /* For all glyphs of this composition, starting at the offset
18796 S->gidx, until we reach the end of the definition or encounter a
18797 glyph that requires the different face, add it to S. */
18798 ++s->nchars;
18799 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
18800 ++s->nchars;
18801
18802 /* All glyph strings for the same composition has the same width,
18803 i.e. the width set for the first component of the composition. */
18804
18805 s->width = s->first_glyph->pixel_width;
18806
18807 /* If the specified font could not be loaded, use the frame's
18808 default font, but record the fact that we couldn't load it in
18809 the glyph string so that we can draw rectangles for the
18810 characters of the glyph string. */
18811 if (s->font == NULL)
18812 {
18813 s->font_not_found_p = 1;
18814 s->font = FRAME_FONT (s->f);
18815 }
18816
18817 /* Adjust base line for subscript/superscript text. */
18818 s->ybase += s->first_glyph->voffset;
18819
18820 xassert (s->face && s->face->gc);
18821
18822 /* This glyph string must always be drawn with 16-bit functions. */
18823 s->two_byte_p = 1;
18824
18825 return s->gidx + s->nchars;
18826 }
18827
18828
18829 /* Fill glyph string S from a sequence of character glyphs.
18830
18831 FACE_ID is the face id of the string. START is the index of the
18832 first glyph to consider, END is the index of the last + 1.
18833 OVERLAPS non-zero means S should draw the foreground only, and use
18834 its physical height for clipping. See also draw_glyphs.
18835
18836 Value is the index of the first glyph not in S. */
18837
18838 static int
18839 fill_glyph_string (s, face_id, start, end, overlaps)
18840 struct glyph_string *s;
18841 int face_id;
18842 int start, end, overlaps;
18843 {
18844 struct glyph *glyph, *last;
18845 int voffset;
18846 int glyph_not_available_p;
18847
18848 xassert (s->f == XFRAME (s->w->frame));
18849 xassert (s->nchars == 0);
18850 xassert (start >= 0 && end > start);
18851
18852 s->for_overlaps = overlaps,
18853 glyph = s->row->glyphs[s->area] + start;
18854 last = s->row->glyphs[s->area] + end;
18855 voffset = glyph->voffset;
18856
18857 glyph_not_available_p = glyph->glyph_not_available_p;
18858
18859 while (glyph < last
18860 && glyph->type == CHAR_GLYPH
18861 && glyph->voffset == voffset
18862 /* Same face id implies same font, nowadays. */
18863 && glyph->face_id == face_id
18864 && glyph->glyph_not_available_p == glyph_not_available_p)
18865 {
18866 int two_byte_p;
18867
18868 s->face = get_glyph_face_and_encoding (s->f, glyph,
18869 s->char2b + s->nchars,
18870 &two_byte_p);
18871 s->two_byte_p = two_byte_p;
18872 ++s->nchars;
18873 xassert (s->nchars <= end - start);
18874 s->width += glyph->pixel_width;
18875 ++glyph;
18876 }
18877
18878 s->font = s->face->font;
18879 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18880
18881 /* If the specified font could not be loaded, use the frame's font,
18882 but record the fact that we couldn't load it in
18883 S->font_not_found_p so that we can draw rectangles for the
18884 characters of the glyph string. */
18885 if (s->font == NULL || glyph_not_available_p)
18886 {
18887 s->font_not_found_p = 1;
18888 s->font = FRAME_FONT (s->f);
18889 }
18890
18891 /* Adjust base line for subscript/superscript text. */
18892 s->ybase += voffset;
18893
18894 xassert (s->face && s->face->gc);
18895 return glyph - s->row->glyphs[s->area];
18896 }
18897
18898
18899 /* Fill glyph string S from image glyph S->first_glyph. */
18900
18901 static void
18902 fill_image_glyph_string (s)
18903 struct glyph_string *s;
18904 {
18905 xassert (s->first_glyph->type == IMAGE_GLYPH);
18906 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
18907 xassert (s->img);
18908 s->slice = s->first_glyph->slice;
18909 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
18910 s->font = s->face->font;
18911 s->width = s->first_glyph->pixel_width;
18912
18913 /* Adjust base line for subscript/superscript text. */
18914 s->ybase += s->first_glyph->voffset;
18915 }
18916
18917
18918 /* Fill glyph string S from a sequence of stretch glyphs.
18919
18920 ROW is the glyph row in which the glyphs are found, AREA is the
18921 area within the row. START is the index of the first glyph to
18922 consider, END is the index of the last + 1.
18923
18924 Value is the index of the first glyph not in S. */
18925
18926 static int
18927 fill_stretch_glyph_string (s, row, area, start, end)
18928 struct glyph_string *s;
18929 struct glyph_row *row;
18930 enum glyph_row_area area;
18931 int start, end;
18932 {
18933 struct glyph *glyph, *last;
18934 int voffset, face_id;
18935
18936 xassert (s->first_glyph->type == STRETCH_GLYPH);
18937
18938 glyph = s->row->glyphs[s->area] + start;
18939 last = s->row->glyphs[s->area] + end;
18940 face_id = glyph->face_id;
18941 s->face = FACE_FROM_ID (s->f, face_id);
18942 s->font = s->face->font;
18943 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
18944 s->width = glyph->pixel_width;
18945 s->nchars = 1;
18946 voffset = glyph->voffset;
18947
18948 for (++glyph;
18949 (glyph < last
18950 && glyph->type == STRETCH_GLYPH
18951 && glyph->voffset == voffset
18952 && glyph->face_id == face_id);
18953 ++glyph)
18954 s->width += glyph->pixel_width;
18955
18956 /* Adjust base line for subscript/superscript text. */
18957 s->ybase += voffset;
18958
18959 /* The case that face->gc == 0 is handled when drawing the glyph
18960 string by calling PREPARE_FACE_FOR_DISPLAY. */
18961 xassert (s->face);
18962 return glyph - s->row->glyphs[s->area];
18963 }
18964
18965
18966 /* EXPORT for RIF:
18967 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
18968 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
18969 assumed to be zero. */
18970
18971 void
18972 x_get_glyph_overhangs (glyph, f, left, right)
18973 struct glyph *glyph;
18974 struct frame *f;
18975 int *left, *right;
18976 {
18977 *left = *right = 0;
18978
18979 if (glyph->type == CHAR_GLYPH)
18980 {
18981 XFontStruct *font;
18982 struct face *face;
18983 struct font_info *font_info;
18984 XChar2b char2b;
18985 XCharStruct *pcm;
18986
18987 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
18988 font = face->font;
18989 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
18990 if (font /* ++KFS: Should this be font_info ? */
18991 && (pcm = rif->per_char_metric (font, &char2b, glyph->font_type)))
18992 {
18993 if (pcm->rbearing > pcm->width)
18994 *right = pcm->rbearing - pcm->width;
18995 if (pcm->lbearing < 0)
18996 *left = -pcm->lbearing;
18997 }
18998 }
18999 }
19000
19001
19002 /* Return the index of the first glyph preceding glyph string S that
19003 is overwritten by S because of S's left overhang. Value is -1
19004 if no glyphs are overwritten. */
19005
19006 static int
19007 left_overwritten (s)
19008 struct glyph_string *s;
19009 {
19010 int k;
19011
19012 if (s->left_overhang)
19013 {
19014 int x = 0, i;
19015 struct glyph *glyphs = s->row->glyphs[s->area];
19016 int first = s->first_glyph - glyphs;
19017
19018 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
19019 x -= glyphs[i].pixel_width;
19020
19021 k = i + 1;
19022 }
19023 else
19024 k = -1;
19025
19026 return k;
19027 }
19028
19029
19030 /* Return the index of the first glyph preceding glyph string S that
19031 is overwriting S because of its right overhang. Value is -1 if no
19032 glyph in front of S overwrites S. */
19033
19034 static int
19035 left_overwriting (s)
19036 struct glyph_string *s;
19037 {
19038 int i, k, x;
19039 struct glyph *glyphs = s->row->glyphs[s->area];
19040 int first = s->first_glyph - glyphs;
19041
19042 k = -1;
19043 x = 0;
19044 for (i = first - 1; i >= 0; --i)
19045 {
19046 int left, right;
19047 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19048 if (x + right > 0)
19049 k = i;
19050 x -= glyphs[i].pixel_width;
19051 }
19052
19053 return k;
19054 }
19055
19056
19057 /* Return the index of the last glyph following glyph string S that is
19058 not overwritten by S because of S's right overhang. Value is -1 if
19059 no such glyph is found. */
19060
19061 static int
19062 right_overwritten (s)
19063 struct glyph_string *s;
19064 {
19065 int k = -1;
19066
19067 if (s->right_overhang)
19068 {
19069 int x = 0, i;
19070 struct glyph *glyphs = s->row->glyphs[s->area];
19071 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19072 int end = s->row->used[s->area];
19073
19074 for (i = first; i < end && s->right_overhang > x; ++i)
19075 x += glyphs[i].pixel_width;
19076
19077 k = i;
19078 }
19079
19080 return k;
19081 }
19082
19083
19084 /* Return the index of the last glyph following glyph string S that
19085 overwrites S because of its left overhang. Value is negative
19086 if no such glyph is found. */
19087
19088 static int
19089 right_overwriting (s)
19090 struct glyph_string *s;
19091 {
19092 int i, k, x;
19093 int end = s->row->used[s->area];
19094 struct glyph *glyphs = s->row->glyphs[s->area];
19095 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
19096
19097 k = -1;
19098 x = 0;
19099 for (i = first; i < end; ++i)
19100 {
19101 int left, right;
19102 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
19103 if (x - left < 0)
19104 k = i;
19105 x += glyphs[i].pixel_width;
19106 }
19107
19108 return k;
19109 }
19110
19111
19112 /* Get face and two-byte form of character C in face FACE_ID on frame
19113 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
19114 means we want to display multibyte text. DISPLAY_P non-zero means
19115 make sure that X resources for the face returned are allocated.
19116 Value is a pointer to a realized face that is ready for display if
19117 DISPLAY_P is non-zero. */
19118
19119 static INLINE struct face *
19120 get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
19121 struct frame *f;
19122 int c, face_id;
19123 XChar2b *char2b;
19124 int multibyte_p, display_p;
19125 {
19126 struct face *face = FACE_FROM_ID (f, face_id);
19127
19128 if (!multibyte_p)
19129 {
19130 /* Unibyte case. We don't have to encode, but we have to make
19131 sure to use a face suitable for unibyte. */
19132 STORE_XCHAR2B (char2b, 0, c);
19133 face_id = FACE_FOR_CHAR (f, face, c);
19134 face = FACE_FROM_ID (f, face_id);
19135 }
19136 else if (c < 128)
19137 {
19138 /* Case of ASCII in a face known to fit ASCII. */
19139 STORE_XCHAR2B (char2b, 0, c);
19140 }
19141 else
19142 {
19143 int c1, c2, charset;
19144
19145 /* Split characters into bytes. If c2 is -1 afterwards, C is
19146 really a one-byte character so that byte1 is zero. */
19147 SPLIT_CHAR (c, charset, c1, c2);
19148 if (c2 > 0)
19149 STORE_XCHAR2B (char2b, c1, c2);
19150 else
19151 STORE_XCHAR2B (char2b, 0, c1);
19152
19153 /* Maybe encode the character in *CHAR2B. */
19154 if (face->font != NULL)
19155 {
19156 struct font_info *font_info
19157 = FONT_INFO_FROM_ID (f, face->font_info_id);
19158 if (font_info)
19159 rif->encode_char (c, char2b, font_info, 0);
19160 }
19161 }
19162
19163 /* Make sure X resources of the face are allocated. */
19164 #ifdef HAVE_X_WINDOWS
19165 if (display_p)
19166 #endif
19167 {
19168 xassert (face != NULL);
19169 PREPARE_FACE_FOR_DISPLAY (f, face);
19170 }
19171
19172 return face;
19173 }
19174
19175
19176 /* Set background width of glyph string S. START is the index of the
19177 first glyph following S. LAST_X is the right-most x-position + 1
19178 in the drawing area. */
19179
19180 static INLINE void
19181 set_glyph_string_background_width (s, start, last_x)
19182 struct glyph_string *s;
19183 int start;
19184 int last_x;
19185 {
19186 /* If the face of this glyph string has to be drawn to the end of
19187 the drawing area, set S->extends_to_end_of_line_p. */
19188
19189 if (start == s->row->used[s->area]
19190 && s->area == TEXT_AREA
19191 && ((s->row->fill_line_p
19192 && (s->hl == DRAW_NORMAL_TEXT
19193 || s->hl == DRAW_IMAGE_RAISED
19194 || s->hl == DRAW_IMAGE_SUNKEN))
19195 || s->hl == DRAW_MOUSE_FACE))
19196 s->extends_to_end_of_line_p = 1;
19197
19198 /* If S extends its face to the end of the line, set its
19199 background_width to the distance to the right edge of the drawing
19200 area. */
19201 if (s->extends_to_end_of_line_p)
19202 s->background_width = last_x - s->x + 1;
19203 else
19204 s->background_width = s->width;
19205 }
19206
19207
19208 /* Compute overhangs and x-positions for glyph string S and its
19209 predecessors, or successors. X is the starting x-position for S.
19210 BACKWARD_P non-zero means process predecessors. */
19211
19212 static void
19213 compute_overhangs_and_x (s, x, backward_p)
19214 struct glyph_string *s;
19215 int x;
19216 int backward_p;
19217 {
19218 if (backward_p)
19219 {
19220 while (s)
19221 {
19222 if (rif->compute_glyph_string_overhangs)
19223 rif->compute_glyph_string_overhangs (s);
19224 x -= s->width;
19225 s->x = x;
19226 s = s->prev;
19227 }
19228 }
19229 else
19230 {
19231 while (s)
19232 {
19233 if (rif->compute_glyph_string_overhangs)
19234 rif->compute_glyph_string_overhangs (s);
19235 s->x = x;
19236 x += s->width;
19237 s = s->next;
19238 }
19239 }
19240 }
19241
19242
19243
19244 /* The following macros are only called from draw_glyphs below.
19245 They reference the following parameters of that function directly:
19246 `w', `row', `area', and `overlap_p'
19247 as well as the following local variables:
19248 `s', `f', and `hdc' (in W32) */
19249
19250 #ifdef HAVE_NTGUI
19251 /* On W32, silently add local `hdc' variable to argument list of
19252 init_glyph_string. */
19253 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19254 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
19255 #else
19256 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
19257 init_glyph_string (s, char2b, w, row, area, start, hl)
19258 #endif
19259
19260 /* Add a glyph string for a stretch glyph to the list of strings
19261 between HEAD and TAIL. START is the index of the stretch glyph in
19262 row area AREA of glyph row ROW. END is the index of the last glyph
19263 in that glyph row area. X is the current output position assigned
19264 to the new glyph string constructed. HL overrides that face of the
19265 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19266 is the right-most x-position of the drawing area. */
19267
19268 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
19269 and below -- keep them on one line. */
19270 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19271 do \
19272 { \
19273 s = (struct glyph_string *) alloca (sizeof *s); \
19274 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19275 START = fill_stretch_glyph_string (s, row, area, START, END); \
19276 append_glyph_string (&HEAD, &TAIL, s); \
19277 s->x = (X); \
19278 } \
19279 while (0)
19280
19281
19282 /* Add a glyph string for an image glyph to the list of strings
19283 between HEAD and TAIL. START is the index of the image glyph in
19284 row area AREA of glyph row ROW. END is the index of the last glyph
19285 in that glyph row area. X is the current output position assigned
19286 to the new glyph string constructed. HL overrides that face of the
19287 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
19288 is the right-most x-position of the drawing area. */
19289
19290 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19291 do \
19292 { \
19293 s = (struct glyph_string *) alloca (sizeof *s); \
19294 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
19295 fill_image_glyph_string (s); \
19296 append_glyph_string (&HEAD, &TAIL, s); \
19297 ++START; \
19298 s->x = (X); \
19299 } \
19300 while (0)
19301
19302
19303 /* Add a glyph string for a sequence of character glyphs to the list
19304 of strings between HEAD and TAIL. START is the index of the first
19305 glyph in row area AREA of glyph row ROW that is part of the new
19306 glyph string. END is the index of the last glyph in that glyph row
19307 area. X is the current output position assigned to the new glyph
19308 string constructed. HL overrides that face of the glyph; e.g. it
19309 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
19310 right-most x-position of the drawing area. */
19311
19312 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19313 do \
19314 { \
19315 int c, face_id; \
19316 XChar2b *char2b; \
19317 \
19318 c = (row)->glyphs[area][START].u.ch; \
19319 face_id = (row)->glyphs[area][START].face_id; \
19320 \
19321 s = (struct glyph_string *) alloca (sizeof *s); \
19322 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
19323 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
19324 append_glyph_string (&HEAD, &TAIL, s); \
19325 s->x = (X); \
19326 START = fill_glyph_string (s, face_id, START, END, overlaps); \
19327 } \
19328 while (0)
19329
19330
19331 /* Add a glyph string for a composite sequence to the list of strings
19332 between HEAD and TAIL. START is the index of the first glyph in
19333 row area AREA of glyph row ROW that is part of the new glyph
19334 string. END is the index of the last glyph in that glyph row area.
19335 X is the current output position assigned to the new glyph string
19336 constructed. HL overrides that face of the glyph; e.g. it is
19337 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
19338 x-position of the drawing area. */
19339
19340 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
19341 do { \
19342 int cmp_id = (row)->glyphs[area][START].u.cmp_id; \
19343 int face_id = (row)->glyphs[area][START].face_id; \
19344 struct face *base_face = FACE_FROM_ID (f, face_id); \
19345 struct composition *cmp = composition_table[cmp_id]; \
19346 int glyph_len = cmp->glyph_len; \
19347 XChar2b *char2b; \
19348 struct face **faces; \
19349 struct glyph_string *first_s = NULL; \
19350 int n; \
19351 \
19352 base_face = base_face->ascii_face; \
19353 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
19354 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
19355 /* At first, fill in `char2b' and `faces'. */ \
19356 for (n = 0; n < glyph_len; n++) \
19357 { \
19358 int c = COMPOSITION_GLYPH (cmp, n); \
19359 int this_face_id = FACE_FOR_CHAR (f, base_face, c); \
19360 faces[n] = FACE_FROM_ID (f, this_face_id); \
19361 get_char_face_and_encoding (f, c, this_face_id, \
19362 char2b + n, 1, 1); \
19363 } \
19364 \
19365 /* Make glyph_strings for each glyph sequence that is drawable by \
19366 the same face, and append them to HEAD/TAIL. */ \
19367 for (n = 0; n < cmp->glyph_len;) \
19368 { \
19369 s = (struct glyph_string *) alloca (sizeof *s); \
19370 INIT_GLYPH_STRING (s, char2b + n, w, row, area, START, HL); \
19371 append_glyph_string (&(HEAD), &(TAIL), s); \
19372 s->cmp = cmp; \
19373 s->gidx = n; \
19374 s->x = (X); \
19375 \
19376 if (n == 0) \
19377 first_s = s; \
19378 \
19379 n = fill_composite_glyph_string (s, faces, overlaps); \
19380 } \
19381 \
19382 ++START; \
19383 s = first_s; \
19384 } while (0)
19385
19386
19387 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
19388 of AREA of glyph row ROW on window W between indices START and END.
19389 HL overrides the face for drawing glyph strings, e.g. it is
19390 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
19391 x-positions of the drawing area.
19392
19393 This is an ugly monster macro construct because we must use alloca
19394 to allocate glyph strings (because draw_glyphs can be called
19395 asynchronously). */
19396
19397 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
19398 do \
19399 { \
19400 HEAD = TAIL = NULL; \
19401 while (START < END) \
19402 { \
19403 struct glyph *first_glyph = (row)->glyphs[area] + START; \
19404 switch (first_glyph->type) \
19405 { \
19406 case CHAR_GLYPH: \
19407 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
19408 HL, X, LAST_X); \
19409 break; \
19410 \
19411 case COMPOSITE_GLYPH: \
19412 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
19413 HL, X, LAST_X); \
19414 break; \
19415 \
19416 case STRETCH_GLYPH: \
19417 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
19418 HL, X, LAST_X); \
19419 break; \
19420 \
19421 case IMAGE_GLYPH: \
19422 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
19423 HL, X, LAST_X); \
19424 break; \
19425 \
19426 default: \
19427 abort (); \
19428 } \
19429 \
19430 set_glyph_string_background_width (s, START, LAST_X); \
19431 (X) += s->width; \
19432 } \
19433 } \
19434 while (0)
19435
19436
19437 /* Draw glyphs between START and END in AREA of ROW on window W,
19438 starting at x-position X. X is relative to AREA in W. HL is a
19439 face-override with the following meaning:
19440
19441 DRAW_NORMAL_TEXT draw normally
19442 DRAW_CURSOR draw in cursor face
19443 DRAW_MOUSE_FACE draw in mouse face.
19444 DRAW_INVERSE_VIDEO draw in mode line face
19445 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
19446 DRAW_IMAGE_RAISED draw an image with a raised relief around it
19447
19448 If OVERLAPS is non-zero, draw only the foreground of characters and
19449 clip to the physical height of ROW. Non-zero value also defines
19450 the overlapping part to be drawn:
19451
19452 OVERLAPS_PRED overlap with preceding rows
19453 OVERLAPS_SUCC overlap with succeeding rows
19454 OVERLAPS_BOTH overlap with both preceding/succeeding rows
19455 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
19456
19457 Value is the x-position reached, relative to AREA of W. */
19458
19459 static int
19460 draw_glyphs (w, x, row, area, start, end, hl, overlaps)
19461 struct window *w;
19462 int x;
19463 struct glyph_row *row;
19464 enum glyph_row_area area;
19465 int start, end;
19466 enum draw_glyphs_face hl;
19467 int overlaps;
19468 {
19469 struct glyph_string *head, *tail;
19470 struct glyph_string *s;
19471 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
19472 int last_x, area_width;
19473 int x_reached;
19474 int i, j;
19475 struct frame *f = XFRAME (WINDOW_FRAME (w));
19476 DECLARE_HDC (hdc);
19477
19478 ALLOCATE_HDC (hdc, f);
19479
19480 /* Let's rather be paranoid than getting a SEGV. */
19481 end = min (end, row->used[area]);
19482 start = max (0, start);
19483 start = min (end, start);
19484
19485 /* Translate X to frame coordinates. Set last_x to the right
19486 end of the drawing area. */
19487 if (row->full_width_p)
19488 {
19489 /* X is relative to the left edge of W, without scroll bars
19490 or fringes. */
19491 x += WINDOW_LEFT_EDGE_X (w);
19492 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
19493 }
19494 else
19495 {
19496 int area_left = window_box_left (w, area);
19497 x += area_left;
19498 area_width = window_box_width (w, area);
19499 last_x = area_left + area_width;
19500 }
19501
19502 /* Build a doubly-linked list of glyph_string structures between
19503 head and tail from what we have to draw. Note that the macro
19504 BUILD_GLYPH_STRINGS will modify its start parameter. That's
19505 the reason we use a separate variable `i'. */
19506 i = start;
19507 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
19508 if (tail)
19509 x_reached = tail->x + tail->background_width;
19510 else
19511 x_reached = x;
19512
19513 /* If there are any glyphs with lbearing < 0 or rbearing > width in
19514 the row, redraw some glyphs in front or following the glyph
19515 strings built above. */
19516 if (head && !overlaps && row->contains_overlapping_glyphs_p)
19517 {
19518 int dummy_x = 0;
19519 struct glyph_string *h, *t;
19520
19521 /* Compute overhangs for all glyph strings. */
19522 if (rif->compute_glyph_string_overhangs)
19523 for (s = head; s; s = s->next)
19524 rif->compute_glyph_string_overhangs (s);
19525
19526 /* Prepend glyph strings for glyphs in front of the first glyph
19527 string that are overwritten because of the first glyph
19528 string's left overhang. The background of all strings
19529 prepended must be drawn because the first glyph string
19530 draws over it. */
19531 i = left_overwritten (head);
19532 if (i >= 0)
19533 {
19534 j = i;
19535 BUILD_GLYPH_STRINGS (j, start, h, t,
19536 DRAW_NORMAL_TEXT, dummy_x, last_x);
19537 start = i;
19538 compute_overhangs_and_x (t, head->x, 1);
19539 prepend_glyph_string_lists (&head, &tail, h, t);
19540 clip_head = head;
19541 }
19542
19543 /* Prepend glyph strings for glyphs in front of the first glyph
19544 string that overwrite that glyph string because of their
19545 right overhang. For these strings, only the foreground must
19546 be drawn, because it draws over the glyph string at `head'.
19547 The background must not be drawn because this would overwrite
19548 right overhangs of preceding glyphs for which no glyph
19549 strings exist. */
19550 i = left_overwriting (head);
19551 if (i >= 0)
19552 {
19553 clip_head = head;
19554 BUILD_GLYPH_STRINGS (i, start, h, t,
19555 DRAW_NORMAL_TEXT, dummy_x, last_x);
19556 for (s = h; s; s = s->next)
19557 s->background_filled_p = 1;
19558 compute_overhangs_and_x (t, head->x, 1);
19559 prepend_glyph_string_lists (&head, &tail, h, t);
19560 }
19561
19562 /* Append glyphs strings for glyphs following the last glyph
19563 string tail that are overwritten by tail. The background of
19564 these strings has to be drawn because tail's foreground draws
19565 over it. */
19566 i = right_overwritten (tail);
19567 if (i >= 0)
19568 {
19569 BUILD_GLYPH_STRINGS (end, i, h, t,
19570 DRAW_NORMAL_TEXT, x, last_x);
19571 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19572 append_glyph_string_lists (&head, &tail, h, t);
19573 clip_tail = tail;
19574 }
19575
19576 /* Append glyph strings for glyphs following the last glyph
19577 string tail that overwrite tail. The foreground of such
19578 glyphs has to be drawn because it writes into the background
19579 of tail. The background must not be drawn because it could
19580 paint over the foreground of following glyphs. */
19581 i = right_overwriting (tail);
19582 if (i >= 0)
19583 {
19584 clip_tail = tail;
19585 BUILD_GLYPH_STRINGS (end, i, h, t,
19586 DRAW_NORMAL_TEXT, x, last_x);
19587 for (s = h; s; s = s->next)
19588 s->background_filled_p = 1;
19589 compute_overhangs_and_x (h, tail->x + tail->width, 0);
19590 append_glyph_string_lists (&head, &tail, h, t);
19591 }
19592 if (clip_head || clip_tail)
19593 for (s = head; s; s = s->next)
19594 {
19595 s->clip_head = clip_head;
19596 s->clip_tail = clip_tail;
19597 }
19598 }
19599
19600 /* Draw all strings. */
19601 for (s = head; s; s = s->next)
19602 rif->draw_glyph_string (s);
19603
19604 if (area == TEXT_AREA
19605 && !row->full_width_p
19606 /* When drawing overlapping rows, only the glyph strings'
19607 foreground is drawn, which doesn't erase a cursor
19608 completely. */
19609 && !overlaps)
19610 {
19611 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
19612 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
19613 : (tail ? tail->x + tail->background_width : x));
19614
19615 int text_left = window_box_left (w, TEXT_AREA);
19616 x0 -= text_left;
19617 x1 -= text_left;
19618
19619 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
19620 row->y, MATRIX_ROW_BOTTOM_Y (row));
19621 }
19622
19623 /* Value is the x-position up to which drawn, relative to AREA of W.
19624 This doesn't include parts drawn because of overhangs. */
19625 if (row->full_width_p)
19626 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
19627 else
19628 x_reached -= window_box_left (w, area);
19629
19630 RELEASE_HDC (hdc, f);
19631
19632 return x_reached;
19633 }
19634
19635 /* Expand row matrix if too narrow. Don't expand if area
19636 is not present. */
19637
19638 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
19639 { \
19640 if (!fonts_changed_p \
19641 && (it->glyph_row->glyphs[area] \
19642 < it->glyph_row->glyphs[area + 1])) \
19643 { \
19644 it->w->ncols_scale_factor++; \
19645 fonts_changed_p = 1; \
19646 } \
19647 }
19648
19649 /* Store one glyph for IT->char_to_display in IT->glyph_row.
19650 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19651
19652 static INLINE void
19653 append_glyph (it)
19654 struct it *it;
19655 {
19656 struct glyph *glyph;
19657 enum glyph_row_area area = it->area;
19658
19659 xassert (it->glyph_row);
19660 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
19661
19662 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19663 if (glyph < it->glyph_row->glyphs[area + 1])
19664 {
19665 glyph->charpos = CHARPOS (it->position);
19666 glyph->object = it->object;
19667 glyph->pixel_width = it->pixel_width;
19668 glyph->ascent = it->ascent;
19669 glyph->descent = it->descent;
19670 glyph->voffset = it->voffset;
19671 glyph->type = CHAR_GLYPH;
19672 glyph->multibyte_p = it->multibyte_p;
19673 glyph->left_box_line_p = it->start_of_box_run_p;
19674 glyph->right_box_line_p = it->end_of_box_run_p;
19675 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19676 || it->phys_descent > it->descent);
19677 glyph->padding_p = 0;
19678 glyph->glyph_not_available_p = it->glyph_not_available_p;
19679 glyph->face_id = it->face_id;
19680 glyph->u.ch = it->char_to_display;
19681 glyph->slice = null_glyph_slice;
19682 glyph->font_type = FONT_TYPE_UNKNOWN;
19683 ++it->glyph_row->used[area];
19684 }
19685 else
19686 IT_EXPAND_MATRIX_WIDTH (it, area);
19687 }
19688
19689 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
19690 Called from x_produce_glyphs when IT->glyph_row is non-null. */
19691
19692 static INLINE void
19693 append_composite_glyph (it)
19694 struct it *it;
19695 {
19696 struct glyph *glyph;
19697 enum glyph_row_area area = it->area;
19698
19699 xassert (it->glyph_row);
19700
19701 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19702 if (glyph < it->glyph_row->glyphs[area + 1])
19703 {
19704 glyph->charpos = CHARPOS (it->position);
19705 glyph->object = it->object;
19706 glyph->pixel_width = it->pixel_width;
19707 glyph->ascent = it->ascent;
19708 glyph->descent = it->descent;
19709 glyph->voffset = it->voffset;
19710 glyph->type = COMPOSITE_GLYPH;
19711 glyph->multibyte_p = it->multibyte_p;
19712 glyph->left_box_line_p = it->start_of_box_run_p;
19713 glyph->right_box_line_p = it->end_of_box_run_p;
19714 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
19715 || it->phys_descent > it->descent);
19716 glyph->padding_p = 0;
19717 glyph->glyph_not_available_p = 0;
19718 glyph->face_id = it->face_id;
19719 glyph->u.cmp_id = it->cmp_id;
19720 glyph->slice = null_glyph_slice;
19721 glyph->font_type = FONT_TYPE_UNKNOWN;
19722 ++it->glyph_row->used[area];
19723 }
19724 else
19725 IT_EXPAND_MATRIX_WIDTH (it, area);
19726 }
19727
19728
19729 /* Change IT->ascent and IT->height according to the setting of
19730 IT->voffset. */
19731
19732 static INLINE void
19733 take_vertical_position_into_account (it)
19734 struct it *it;
19735 {
19736 if (it->voffset)
19737 {
19738 if (it->voffset < 0)
19739 /* Increase the ascent so that we can display the text higher
19740 in the line. */
19741 it->ascent -= it->voffset;
19742 else
19743 /* Increase the descent so that we can display the text lower
19744 in the line. */
19745 it->descent += it->voffset;
19746 }
19747 }
19748
19749
19750 /* Produce glyphs/get display metrics for the image IT is loaded with.
19751 See the description of struct display_iterator in dispextern.h for
19752 an overview of struct display_iterator. */
19753
19754 static void
19755 produce_image_glyph (it)
19756 struct it *it;
19757 {
19758 struct image *img;
19759 struct face *face;
19760 int glyph_ascent;
19761 struct glyph_slice slice;
19762
19763 xassert (it->what == IT_IMAGE);
19764
19765 face = FACE_FROM_ID (it->f, it->face_id);
19766 xassert (face);
19767 /* Make sure X resources of the face is loaded. */
19768 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19769
19770 if (it->image_id < 0)
19771 {
19772 /* Fringe bitmap. */
19773 it->ascent = it->phys_ascent = 0;
19774 it->descent = it->phys_descent = 0;
19775 it->pixel_width = 0;
19776 it->nglyphs = 0;
19777 return;
19778 }
19779
19780 img = IMAGE_FROM_ID (it->f, it->image_id);
19781 xassert (img);
19782 /* Make sure X resources of the image is loaded. */
19783 prepare_image_for_display (it->f, img);
19784
19785 slice.x = slice.y = 0;
19786 slice.width = img->width;
19787 slice.height = img->height;
19788
19789 if (INTEGERP (it->slice.x))
19790 slice.x = XINT (it->slice.x);
19791 else if (FLOATP (it->slice.x))
19792 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
19793
19794 if (INTEGERP (it->slice.y))
19795 slice.y = XINT (it->slice.y);
19796 else if (FLOATP (it->slice.y))
19797 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
19798
19799 if (INTEGERP (it->slice.width))
19800 slice.width = XINT (it->slice.width);
19801 else if (FLOATP (it->slice.width))
19802 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
19803
19804 if (INTEGERP (it->slice.height))
19805 slice.height = XINT (it->slice.height);
19806 else if (FLOATP (it->slice.height))
19807 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
19808
19809 if (slice.x >= img->width)
19810 slice.x = img->width;
19811 if (slice.y >= img->height)
19812 slice.y = img->height;
19813 if (slice.x + slice.width >= img->width)
19814 slice.width = img->width - slice.x;
19815 if (slice.y + slice.height > img->height)
19816 slice.height = img->height - slice.y;
19817
19818 if (slice.width == 0 || slice.height == 0)
19819 return;
19820
19821 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
19822
19823 it->descent = slice.height - glyph_ascent;
19824 if (slice.y == 0)
19825 it->descent += img->vmargin;
19826 if (slice.y + slice.height == img->height)
19827 it->descent += img->vmargin;
19828 it->phys_descent = it->descent;
19829
19830 it->pixel_width = slice.width;
19831 if (slice.x == 0)
19832 it->pixel_width += img->hmargin;
19833 if (slice.x + slice.width == img->width)
19834 it->pixel_width += img->hmargin;
19835
19836 /* It's quite possible for images to have an ascent greater than
19837 their height, so don't get confused in that case. */
19838 if (it->descent < 0)
19839 it->descent = 0;
19840
19841 #if 0 /* this breaks image tiling */
19842 /* If this glyph is alone on the last line, adjust it.ascent to minimum row ascent. */
19843 int face_ascent = face->font ? FONT_BASE (face->font) : FRAME_BASELINE_OFFSET (it->f);
19844 if (face_ascent > it->ascent)
19845 it->ascent = it->phys_ascent = face_ascent;
19846 #endif
19847
19848 it->nglyphs = 1;
19849
19850 if (face->box != FACE_NO_BOX)
19851 {
19852 if (face->box_line_width > 0)
19853 {
19854 if (slice.y == 0)
19855 it->ascent += face->box_line_width;
19856 if (slice.y + slice.height == img->height)
19857 it->descent += face->box_line_width;
19858 }
19859
19860 if (it->start_of_box_run_p && slice.x == 0)
19861 it->pixel_width += abs (face->box_line_width);
19862 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
19863 it->pixel_width += abs (face->box_line_width);
19864 }
19865
19866 take_vertical_position_into_account (it);
19867
19868 if (it->glyph_row)
19869 {
19870 struct glyph *glyph;
19871 enum glyph_row_area area = it->area;
19872
19873 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19874 if (glyph < it->glyph_row->glyphs[area + 1])
19875 {
19876 glyph->charpos = CHARPOS (it->position);
19877 glyph->object = it->object;
19878 glyph->pixel_width = it->pixel_width;
19879 glyph->ascent = glyph_ascent;
19880 glyph->descent = it->descent;
19881 glyph->voffset = it->voffset;
19882 glyph->type = IMAGE_GLYPH;
19883 glyph->multibyte_p = it->multibyte_p;
19884 glyph->left_box_line_p = it->start_of_box_run_p;
19885 glyph->right_box_line_p = it->end_of_box_run_p;
19886 glyph->overlaps_vertically_p = 0;
19887 glyph->padding_p = 0;
19888 glyph->glyph_not_available_p = 0;
19889 glyph->face_id = it->face_id;
19890 glyph->u.img_id = img->id;
19891 glyph->slice = slice;
19892 glyph->font_type = FONT_TYPE_UNKNOWN;
19893 ++it->glyph_row->used[area];
19894 }
19895 else
19896 IT_EXPAND_MATRIX_WIDTH (it, area);
19897 }
19898 }
19899
19900
19901 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
19902 of the glyph, WIDTH and HEIGHT are the width and height of the
19903 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
19904
19905 static void
19906 append_stretch_glyph (it, object, width, height, ascent)
19907 struct it *it;
19908 Lisp_Object object;
19909 int width, height;
19910 int ascent;
19911 {
19912 struct glyph *glyph;
19913 enum glyph_row_area area = it->area;
19914
19915 xassert (ascent >= 0 && ascent <= height);
19916
19917 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
19918 if (glyph < it->glyph_row->glyphs[area + 1])
19919 {
19920 glyph->charpos = CHARPOS (it->position);
19921 glyph->object = object;
19922 glyph->pixel_width = width;
19923 glyph->ascent = ascent;
19924 glyph->descent = height - ascent;
19925 glyph->voffset = it->voffset;
19926 glyph->type = STRETCH_GLYPH;
19927 glyph->multibyte_p = it->multibyte_p;
19928 glyph->left_box_line_p = it->start_of_box_run_p;
19929 glyph->right_box_line_p = it->end_of_box_run_p;
19930 glyph->overlaps_vertically_p = 0;
19931 glyph->padding_p = 0;
19932 glyph->glyph_not_available_p = 0;
19933 glyph->face_id = it->face_id;
19934 glyph->u.stretch.ascent = ascent;
19935 glyph->u.stretch.height = height;
19936 glyph->slice = null_glyph_slice;
19937 glyph->font_type = FONT_TYPE_UNKNOWN;
19938 ++it->glyph_row->used[area];
19939 }
19940 else
19941 IT_EXPAND_MATRIX_WIDTH (it, area);
19942 }
19943
19944
19945 /* Produce a stretch glyph for iterator IT. IT->object is the value
19946 of the glyph property displayed. The value must be a list
19947 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
19948 being recognized:
19949
19950 1. `:width WIDTH' specifies that the space should be WIDTH *
19951 canonical char width wide. WIDTH may be an integer or floating
19952 point number.
19953
19954 2. `:relative-width FACTOR' specifies that the width of the stretch
19955 should be computed from the width of the first character having the
19956 `glyph' property, and should be FACTOR times that width.
19957
19958 3. `:align-to HPOS' specifies that the space should be wide enough
19959 to reach HPOS, a value in canonical character units.
19960
19961 Exactly one of the above pairs must be present.
19962
19963 4. `:height HEIGHT' specifies that the height of the stretch produced
19964 should be HEIGHT, measured in canonical character units.
19965
19966 5. `:relative-height FACTOR' specifies that the height of the
19967 stretch should be FACTOR times the height of the characters having
19968 the glyph property.
19969
19970 Either none or exactly one of 4 or 5 must be present.
19971
19972 6. `:ascent ASCENT' specifies that ASCENT percent of the height
19973 of the stretch should be used for the ascent of the stretch.
19974 ASCENT must be in the range 0 <= ASCENT <= 100. */
19975
19976 static void
19977 produce_stretch_glyph (it)
19978 struct it *it;
19979 {
19980 /* (space :width WIDTH :height HEIGHT ...) */
19981 Lisp_Object prop, plist;
19982 int width = 0, height = 0, align_to = -1;
19983 int zero_width_ok_p = 0, zero_height_ok_p = 0;
19984 int ascent = 0;
19985 double tem;
19986 struct face *face = FACE_FROM_ID (it->f, it->face_id);
19987 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
19988
19989 PREPARE_FACE_FOR_DISPLAY (it->f, face);
19990
19991 /* List should start with `space'. */
19992 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
19993 plist = XCDR (it->object);
19994
19995 /* Compute the width of the stretch. */
19996 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
19997 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
19998 {
19999 /* Absolute width `:width WIDTH' specified and valid. */
20000 zero_width_ok_p = 1;
20001 width = (int)tem;
20002 }
20003 else if (prop = Fplist_get (plist, QCrelative_width),
20004 NUMVAL (prop) > 0)
20005 {
20006 /* Relative width `:relative-width FACTOR' specified and valid.
20007 Compute the width of the characters having the `glyph'
20008 property. */
20009 struct it it2;
20010 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
20011
20012 it2 = *it;
20013 if (it->multibyte_p)
20014 {
20015 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
20016 - IT_BYTEPOS (*it));
20017 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
20018 }
20019 else
20020 it2.c = *p, it2.len = 1;
20021
20022 it2.glyph_row = NULL;
20023 it2.what = IT_CHARACTER;
20024 x_produce_glyphs (&it2);
20025 width = NUMVAL (prop) * it2.pixel_width;
20026 }
20027 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
20028 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
20029 {
20030 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
20031 align_to = (align_to < 0
20032 ? 0
20033 : align_to - window_box_left_offset (it->w, TEXT_AREA));
20034 else if (align_to < 0)
20035 align_to = window_box_left_offset (it->w, TEXT_AREA);
20036 width = max (0, (int)tem + align_to - it->current_x);
20037 zero_width_ok_p = 1;
20038 }
20039 else
20040 /* Nothing specified -> width defaults to canonical char width. */
20041 width = FRAME_COLUMN_WIDTH (it->f);
20042
20043 if (width <= 0 && (width < 0 || !zero_width_ok_p))
20044 width = 1;
20045
20046 /* Compute height. */
20047 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
20048 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20049 {
20050 height = (int)tem;
20051 zero_height_ok_p = 1;
20052 }
20053 else if (prop = Fplist_get (plist, QCrelative_height),
20054 NUMVAL (prop) > 0)
20055 height = FONT_HEIGHT (font) * NUMVAL (prop);
20056 else
20057 height = FONT_HEIGHT (font);
20058
20059 if (height <= 0 && (height < 0 || !zero_height_ok_p))
20060 height = 1;
20061
20062 /* Compute percentage of height used for ascent. If
20063 `:ascent ASCENT' is present and valid, use that. Otherwise,
20064 derive the ascent from the font in use. */
20065 if (prop = Fplist_get (plist, QCascent),
20066 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
20067 ascent = height * NUMVAL (prop) / 100.0;
20068 else if (!NILP (prop)
20069 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
20070 ascent = min (max (0, (int)tem), height);
20071 else
20072 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
20073
20074 if (width > 0 && !it->truncate_lines_p
20075 && it->current_x + width > it->last_visible_x)
20076 width = it->last_visible_x - it->current_x - 1;
20077
20078 if (width > 0 && height > 0 && it->glyph_row)
20079 {
20080 Lisp_Object object = it->stack[it->sp - 1].string;
20081 if (!STRINGP (object))
20082 object = it->w->buffer;
20083 append_stretch_glyph (it, object, width, height, ascent);
20084 }
20085
20086 it->pixel_width = width;
20087 it->ascent = it->phys_ascent = ascent;
20088 it->descent = it->phys_descent = height - it->ascent;
20089 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
20090
20091 take_vertical_position_into_account (it);
20092 }
20093
20094 /* Get line-height and line-spacing property at point.
20095 If line-height has format (HEIGHT TOTAL), return TOTAL
20096 in TOTAL_HEIGHT. */
20097
20098 static Lisp_Object
20099 get_line_height_property (it, prop)
20100 struct it *it;
20101 Lisp_Object prop;
20102 {
20103 Lisp_Object position;
20104
20105 if (STRINGP (it->object))
20106 position = make_number (IT_STRING_CHARPOS (*it));
20107 else if (BUFFERP (it->object))
20108 position = make_number (IT_CHARPOS (*it));
20109 else
20110 return Qnil;
20111
20112 return Fget_char_property (position, prop, it->object);
20113 }
20114
20115 /* Calculate line-height and line-spacing properties.
20116 An integer value specifies explicit pixel value.
20117 A float value specifies relative value to current face height.
20118 A cons (float . face-name) specifies relative value to
20119 height of specified face font.
20120
20121 Returns height in pixels, or nil. */
20122
20123
20124 static Lisp_Object
20125 calc_line_height_property (it, val, font, boff, override)
20126 struct it *it;
20127 Lisp_Object val;
20128 XFontStruct *font;
20129 int boff, override;
20130 {
20131 Lisp_Object face_name = Qnil;
20132 int ascent, descent, height;
20133
20134 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
20135 return val;
20136
20137 if (CONSP (val))
20138 {
20139 face_name = XCAR (val);
20140 val = XCDR (val);
20141 if (!NUMBERP (val))
20142 val = make_number (1);
20143 if (NILP (face_name))
20144 {
20145 height = it->ascent + it->descent;
20146 goto scale;
20147 }
20148 }
20149
20150 if (NILP (face_name))
20151 {
20152 font = FRAME_FONT (it->f);
20153 boff = FRAME_BASELINE_OFFSET (it->f);
20154 }
20155 else if (EQ (face_name, Qt))
20156 {
20157 override = 0;
20158 }
20159 else
20160 {
20161 int face_id;
20162 struct face *face;
20163 struct font_info *font_info;
20164
20165 face_id = lookup_named_face (it->f, face_name, ' ', 0);
20166 if (face_id < 0)
20167 return make_number (-1);
20168
20169 face = FACE_FROM_ID (it->f, face_id);
20170 font = face->font;
20171 if (font == NULL)
20172 return make_number (-1);
20173
20174 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20175 boff = font_info->baseline_offset;
20176 if (font_info->vertical_centering)
20177 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20178 }
20179
20180 ascent = FONT_BASE (font) + boff;
20181 descent = FONT_DESCENT (font) - boff;
20182
20183 if (override)
20184 {
20185 it->override_ascent = ascent;
20186 it->override_descent = descent;
20187 it->override_boff = boff;
20188 }
20189
20190 height = ascent + descent;
20191
20192 scale:
20193 if (FLOATP (val))
20194 height = (int)(XFLOAT_DATA (val) * height);
20195 else if (INTEGERP (val))
20196 height *= XINT (val);
20197
20198 return make_number (height);
20199 }
20200
20201
20202 /* RIF:
20203 Produce glyphs/get display metrics for the display element IT is
20204 loaded with. See the description of struct it in dispextern.h
20205 for an overview of struct it. */
20206
20207 void
20208 x_produce_glyphs (it)
20209 struct it *it;
20210 {
20211 int extra_line_spacing = it->extra_line_spacing;
20212
20213 it->glyph_not_available_p = 0;
20214
20215 if (it->what == IT_CHARACTER)
20216 {
20217 XChar2b char2b;
20218 XFontStruct *font;
20219 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20220 XCharStruct *pcm;
20221 int font_not_found_p;
20222 struct font_info *font_info;
20223 int boff; /* baseline offset */
20224 /* We may change it->multibyte_p upon unibyte<->multibyte
20225 conversion. So, save the current value now and restore it
20226 later.
20227
20228 Note: It seems that we don't have to record multibyte_p in
20229 struct glyph because the character code itself tells if or
20230 not the character is multibyte. Thus, in the future, we must
20231 consider eliminating the field `multibyte_p' in the struct
20232 glyph. */
20233 int saved_multibyte_p = it->multibyte_p;
20234
20235 /* Maybe translate single-byte characters to multibyte, or the
20236 other way. */
20237 it->char_to_display = it->c;
20238 if (!ASCII_BYTE_P (it->c))
20239 {
20240 if (unibyte_display_via_language_environment
20241 && SINGLE_BYTE_CHAR_P (it->c)
20242 && (it->c >= 0240
20243 || !NILP (Vnonascii_translation_table)))
20244 {
20245 it->char_to_display = unibyte_char_to_multibyte (it->c);
20246 it->multibyte_p = 1;
20247 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20248 face = FACE_FROM_ID (it->f, it->face_id);
20249 }
20250 else if (!SINGLE_BYTE_CHAR_P (it->c)
20251 && !it->multibyte_p)
20252 {
20253 it->multibyte_p = 1;
20254 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20255 face = FACE_FROM_ID (it->f, it->face_id);
20256 }
20257 }
20258
20259 /* Get font to use. Encode IT->char_to_display. */
20260 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20261 &char2b, it->multibyte_p, 0);
20262 font = face->font;
20263
20264 /* When no suitable font found, use the default font. */
20265 font_not_found_p = font == NULL;
20266 if (font_not_found_p)
20267 {
20268 font = FRAME_FONT (it->f);
20269 boff = FRAME_BASELINE_OFFSET (it->f);
20270 font_info = NULL;
20271 }
20272 else
20273 {
20274 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20275 boff = font_info->baseline_offset;
20276 if (font_info->vertical_centering)
20277 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20278 }
20279
20280 if (it->char_to_display >= ' '
20281 && (!it->multibyte_p || it->char_to_display < 128))
20282 {
20283 /* Either unibyte or ASCII. */
20284 int stretched_p;
20285
20286 it->nglyphs = 1;
20287
20288 pcm = rif->per_char_metric (font, &char2b,
20289 FONT_TYPE_FOR_UNIBYTE (font, it->char_to_display));
20290
20291 if (it->override_ascent >= 0)
20292 {
20293 it->ascent = it->override_ascent;
20294 it->descent = it->override_descent;
20295 boff = it->override_boff;
20296 }
20297 else
20298 {
20299 it->ascent = FONT_BASE (font) + boff;
20300 it->descent = FONT_DESCENT (font) - boff;
20301 }
20302
20303 if (pcm)
20304 {
20305 it->phys_ascent = pcm->ascent + boff;
20306 it->phys_descent = pcm->descent - boff;
20307 it->pixel_width = pcm->width;
20308 }
20309 else
20310 {
20311 it->glyph_not_available_p = 1;
20312 it->phys_ascent = it->ascent;
20313 it->phys_descent = it->descent;
20314 it->pixel_width = FONT_WIDTH (font);
20315 }
20316
20317 if (it->constrain_row_ascent_descent_p)
20318 {
20319 if (it->descent > it->max_descent)
20320 {
20321 it->ascent += it->descent - it->max_descent;
20322 it->descent = it->max_descent;
20323 }
20324 if (it->ascent > it->max_ascent)
20325 {
20326 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20327 it->ascent = it->max_ascent;
20328 }
20329 it->phys_ascent = min (it->phys_ascent, it->ascent);
20330 it->phys_descent = min (it->phys_descent, it->descent);
20331 extra_line_spacing = 0;
20332 }
20333
20334 /* If this is a space inside a region of text with
20335 `space-width' property, change its width. */
20336 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
20337 if (stretched_p)
20338 it->pixel_width *= XFLOATINT (it->space_width);
20339
20340 /* If face has a box, add the box thickness to the character
20341 height. If character has a box line to the left and/or
20342 right, add the box line width to the character's width. */
20343 if (face->box != FACE_NO_BOX)
20344 {
20345 int thick = face->box_line_width;
20346
20347 if (thick > 0)
20348 {
20349 it->ascent += thick;
20350 it->descent += thick;
20351 }
20352 else
20353 thick = -thick;
20354
20355 if (it->start_of_box_run_p)
20356 it->pixel_width += thick;
20357 if (it->end_of_box_run_p)
20358 it->pixel_width += thick;
20359 }
20360
20361 /* If face has an overline, add the height of the overline
20362 (1 pixel) and a 1 pixel margin to the character height. */
20363 if (face->overline_p)
20364 it->ascent += overline_margin;
20365
20366 if (it->constrain_row_ascent_descent_p)
20367 {
20368 if (it->ascent > it->max_ascent)
20369 it->ascent = it->max_ascent;
20370 if (it->descent > it->max_descent)
20371 it->descent = it->max_descent;
20372 }
20373
20374 take_vertical_position_into_account (it);
20375
20376 /* If we have to actually produce glyphs, do it. */
20377 if (it->glyph_row)
20378 {
20379 if (stretched_p)
20380 {
20381 /* Translate a space with a `space-width' property
20382 into a stretch glyph. */
20383 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
20384 / FONT_HEIGHT (font));
20385 append_stretch_glyph (it, it->object, it->pixel_width,
20386 it->ascent + it->descent, ascent);
20387 }
20388 else
20389 append_glyph (it);
20390
20391 /* If characters with lbearing or rbearing are displayed
20392 in this line, record that fact in a flag of the
20393 glyph row. This is used to optimize X output code. */
20394 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
20395 it->glyph_row->contains_overlapping_glyphs_p = 1;
20396 }
20397 }
20398 else if (it->char_to_display == '\n')
20399 {
20400 /* A newline has no width but we need the height of the line.
20401 But if previous part of the line set a height, don't
20402 increase that height */
20403
20404 Lisp_Object height;
20405 Lisp_Object total_height = Qnil;
20406
20407 it->override_ascent = -1;
20408 it->pixel_width = 0;
20409 it->nglyphs = 0;
20410
20411 height = get_line_height_property(it, Qline_height);
20412 /* Split (line-height total-height) list */
20413 if (CONSP (height)
20414 && CONSP (XCDR (height))
20415 && NILP (XCDR (XCDR (height))))
20416 {
20417 total_height = XCAR (XCDR (height));
20418 height = XCAR (height);
20419 }
20420 height = calc_line_height_property(it, height, font, boff, 1);
20421
20422 if (it->override_ascent >= 0)
20423 {
20424 it->ascent = it->override_ascent;
20425 it->descent = it->override_descent;
20426 boff = it->override_boff;
20427 }
20428 else
20429 {
20430 it->ascent = FONT_BASE (font) + boff;
20431 it->descent = FONT_DESCENT (font) - boff;
20432 }
20433
20434 if (EQ (height, Qt))
20435 {
20436 if (it->descent > it->max_descent)
20437 {
20438 it->ascent += it->descent - it->max_descent;
20439 it->descent = it->max_descent;
20440 }
20441 if (it->ascent > it->max_ascent)
20442 {
20443 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
20444 it->ascent = it->max_ascent;
20445 }
20446 it->phys_ascent = min (it->phys_ascent, it->ascent);
20447 it->phys_descent = min (it->phys_descent, it->descent);
20448 it->constrain_row_ascent_descent_p = 1;
20449 extra_line_spacing = 0;
20450 }
20451 else
20452 {
20453 Lisp_Object spacing;
20454
20455 it->phys_ascent = it->ascent;
20456 it->phys_descent = it->descent;
20457
20458 if ((it->max_ascent > 0 || it->max_descent > 0)
20459 && face->box != FACE_NO_BOX
20460 && face->box_line_width > 0)
20461 {
20462 it->ascent += face->box_line_width;
20463 it->descent += face->box_line_width;
20464 }
20465 if (!NILP (height)
20466 && XINT (height) > it->ascent + it->descent)
20467 it->ascent = XINT (height) - it->descent;
20468
20469 if (!NILP (total_height))
20470 spacing = calc_line_height_property(it, total_height, font, boff, 0);
20471 else
20472 {
20473 spacing = get_line_height_property(it, Qline_spacing);
20474 spacing = calc_line_height_property(it, spacing, font, boff, 0);
20475 }
20476 if (INTEGERP (spacing))
20477 {
20478 extra_line_spacing = XINT (spacing);
20479 if (!NILP (total_height))
20480 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
20481 }
20482 }
20483 }
20484 else if (it->char_to_display == '\t')
20485 {
20486 int tab_width = it->tab_width * FRAME_SPACE_WIDTH (it->f);
20487 int x = it->current_x + it->continuation_lines_width;
20488 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
20489
20490 /* If the distance from the current position to the next tab
20491 stop is less than a space character width, use the
20492 tab stop after that. */
20493 if (next_tab_x - x < FRAME_SPACE_WIDTH (it->f))
20494 next_tab_x += tab_width;
20495
20496 it->pixel_width = next_tab_x - x;
20497 it->nglyphs = 1;
20498 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
20499 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
20500
20501 if (it->glyph_row)
20502 {
20503 append_stretch_glyph (it, it->object, it->pixel_width,
20504 it->ascent + it->descent, it->ascent);
20505 }
20506 }
20507 else
20508 {
20509 /* A multi-byte character. Assume that the display width of the
20510 character is the width of the character multiplied by the
20511 width of the font. */
20512
20513 /* If we found a font, this font should give us the right
20514 metrics. If we didn't find a font, use the frame's
20515 default font and calculate the width of the character
20516 from the charset width; this is what old redisplay code
20517 did. */
20518
20519 pcm = rif->per_char_metric (font, &char2b,
20520 FONT_TYPE_FOR_MULTIBYTE (font, it->c));
20521
20522 if (font_not_found_p || !pcm)
20523 {
20524 int charset = CHAR_CHARSET (it->char_to_display);
20525
20526 it->glyph_not_available_p = 1;
20527 it->pixel_width = (FRAME_COLUMN_WIDTH (it->f)
20528 * CHARSET_WIDTH (charset));
20529 it->phys_ascent = FONT_BASE (font) + boff;
20530 it->phys_descent = FONT_DESCENT (font) - boff;
20531 }
20532 else
20533 {
20534 it->pixel_width = pcm->width;
20535 it->phys_ascent = pcm->ascent + boff;
20536 it->phys_descent = pcm->descent - boff;
20537 if (it->glyph_row
20538 && (pcm->lbearing < 0
20539 || pcm->rbearing > pcm->width))
20540 it->glyph_row->contains_overlapping_glyphs_p = 1;
20541 }
20542 it->nglyphs = 1;
20543 it->ascent = FONT_BASE (font) + boff;
20544 it->descent = FONT_DESCENT (font) - boff;
20545 if (face->box != FACE_NO_BOX)
20546 {
20547 int thick = face->box_line_width;
20548
20549 if (thick > 0)
20550 {
20551 it->ascent += thick;
20552 it->descent += thick;
20553 }
20554 else
20555 thick = - thick;
20556
20557 if (it->start_of_box_run_p)
20558 it->pixel_width += thick;
20559 if (it->end_of_box_run_p)
20560 it->pixel_width += thick;
20561 }
20562
20563 /* If face has an overline, add the height of the overline
20564 (1 pixel) and a 1 pixel margin to the character height. */
20565 if (face->overline_p)
20566 it->ascent += overline_margin;
20567
20568 take_vertical_position_into_account (it);
20569
20570 if (it->glyph_row)
20571 append_glyph (it);
20572 }
20573 it->multibyte_p = saved_multibyte_p;
20574 }
20575 else if (it->what == IT_COMPOSITION)
20576 {
20577 /* Note: A composition is represented as one glyph in the
20578 glyph matrix. There are no padding glyphs. */
20579 XChar2b char2b;
20580 XFontStruct *font;
20581 struct face *face = FACE_FROM_ID (it->f, it->face_id);
20582 XCharStruct *pcm;
20583 int font_not_found_p;
20584 struct font_info *font_info;
20585 int boff; /* baseline offset */
20586 struct composition *cmp = composition_table[it->cmp_id];
20587
20588 /* Maybe translate single-byte characters to multibyte. */
20589 it->char_to_display = it->c;
20590 if (unibyte_display_via_language_environment
20591 && SINGLE_BYTE_CHAR_P (it->c)
20592 && (it->c >= 0240
20593 || (it->c >= 0200
20594 && !NILP (Vnonascii_translation_table))))
20595 {
20596 it->char_to_display = unibyte_char_to_multibyte (it->c);
20597 }
20598
20599 /* Get face and font to use. Encode IT->char_to_display. */
20600 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
20601 face = FACE_FROM_ID (it->f, it->face_id);
20602 get_char_face_and_encoding (it->f, it->char_to_display, it->face_id,
20603 &char2b, it->multibyte_p, 0);
20604 font = face->font;
20605
20606 /* When no suitable font found, use the default font. */
20607 font_not_found_p = font == NULL;
20608 if (font_not_found_p)
20609 {
20610 font = FRAME_FONT (it->f);
20611 boff = FRAME_BASELINE_OFFSET (it->f);
20612 font_info = NULL;
20613 }
20614 else
20615 {
20616 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20617 boff = font_info->baseline_offset;
20618 if (font_info->vertical_centering)
20619 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20620 }
20621
20622 /* There are no padding glyphs, so there is only one glyph to
20623 produce for the composition. Important is that pixel_width,
20624 ascent and descent are the values of what is drawn by
20625 draw_glyphs (i.e. the values of the overall glyphs composed). */
20626 it->nglyphs = 1;
20627
20628 /* If we have not yet calculated pixel size data of glyphs of
20629 the composition for the current face font, calculate them
20630 now. Theoretically, we have to check all fonts for the
20631 glyphs, but that requires much time and memory space. So,
20632 here we check only the font of the first glyph. This leads
20633 to incorrect display very rarely, and C-l (recenter) can
20634 correct the display anyway. */
20635 if (cmp->font != (void *) font)
20636 {
20637 /* Ascent and descent of the font of the first character of
20638 this composition (adjusted by baseline offset). Ascent
20639 and descent of overall glyphs should not be less than
20640 them respectively. */
20641 int font_ascent = FONT_BASE (font) + boff;
20642 int font_descent = FONT_DESCENT (font) - boff;
20643 /* Bounding box of the overall glyphs. */
20644 int leftmost, rightmost, lowest, highest;
20645 int i, width, ascent, descent;
20646
20647 cmp->font = (void *) font;
20648
20649 /* Initialize the bounding box. */
20650 if (font_info
20651 && (pcm = rif->per_char_metric (font, &char2b,
20652 FONT_TYPE_FOR_MULTIBYTE (font, it->c))))
20653 {
20654 width = pcm->width;
20655 ascent = pcm->ascent;
20656 descent = pcm->descent;
20657 }
20658 else
20659 {
20660 width = FONT_WIDTH (font);
20661 ascent = FONT_BASE (font);
20662 descent = FONT_DESCENT (font);
20663 }
20664
20665 rightmost = width;
20666 lowest = - descent + boff;
20667 highest = ascent + boff;
20668 leftmost = 0;
20669
20670 if (font_info
20671 && font_info->default_ascent
20672 && CHAR_TABLE_P (Vuse_default_ascent)
20673 && !NILP (Faref (Vuse_default_ascent,
20674 make_number (it->char_to_display))))
20675 highest = font_info->default_ascent + boff;
20676
20677 /* Draw the first glyph at the normal position. It may be
20678 shifted to right later if some other glyphs are drawn at
20679 the left. */
20680 cmp->offsets[0] = 0;
20681 cmp->offsets[1] = boff;
20682
20683 /* Set cmp->offsets for the remaining glyphs. */
20684 for (i = 1; i < cmp->glyph_len; i++)
20685 {
20686 int left, right, btm, top;
20687 int ch = COMPOSITION_GLYPH (cmp, i);
20688 int face_id = FACE_FOR_CHAR (it->f, face, ch);
20689
20690 face = FACE_FROM_ID (it->f, face_id);
20691 get_char_face_and_encoding (it->f, ch, face->id,
20692 &char2b, it->multibyte_p, 0);
20693 font = face->font;
20694 if (font == NULL)
20695 {
20696 font = FRAME_FONT (it->f);
20697 boff = FRAME_BASELINE_OFFSET (it->f);
20698 font_info = NULL;
20699 }
20700 else
20701 {
20702 font_info
20703 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
20704 boff = font_info->baseline_offset;
20705 if (font_info->vertical_centering)
20706 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
20707 }
20708
20709 if (font_info
20710 && (pcm = rif->per_char_metric (font, &char2b,
20711 FONT_TYPE_FOR_MULTIBYTE (font, ch))))
20712 {
20713 width = pcm->width;
20714 ascent = pcm->ascent;
20715 descent = pcm->descent;
20716 }
20717 else
20718 {
20719 width = FONT_WIDTH (font);
20720 ascent = 1;
20721 descent = 0;
20722 }
20723
20724 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
20725 {
20726 /* Relative composition with or without
20727 alternate chars. */
20728 left = (leftmost + rightmost - width) / 2;
20729 btm = - descent + boff;
20730 if (font_info && font_info->relative_compose
20731 && (! CHAR_TABLE_P (Vignore_relative_composition)
20732 || NILP (Faref (Vignore_relative_composition,
20733 make_number (ch)))))
20734 {
20735
20736 if (- descent >= font_info->relative_compose)
20737 /* One extra pixel between two glyphs. */
20738 btm = highest + 1;
20739 else if (ascent <= 0)
20740 /* One extra pixel between two glyphs. */
20741 btm = lowest - 1 - ascent - descent;
20742 }
20743 }
20744 else
20745 {
20746 /* A composition rule is specified by an integer
20747 value that encodes global and new reference
20748 points (GREF and NREF). GREF and NREF are
20749 specified by numbers as below:
20750
20751 0---1---2 -- ascent
20752 | |
20753 | |
20754 | |
20755 9--10--11 -- center
20756 | |
20757 ---3---4---5--- baseline
20758 | |
20759 6---7---8 -- descent
20760 */
20761 int rule = COMPOSITION_RULE (cmp, i);
20762 int gref, nref, grefx, grefy, nrefx, nrefy;
20763
20764 COMPOSITION_DECODE_RULE (rule, gref, nref);
20765 grefx = gref % 3, nrefx = nref % 3;
20766 grefy = gref / 3, nrefy = nref / 3;
20767
20768 left = (leftmost
20769 + grefx * (rightmost - leftmost) / 2
20770 - nrefx * width / 2);
20771 btm = ((grefy == 0 ? highest
20772 : grefy == 1 ? 0
20773 : grefy == 2 ? lowest
20774 : (highest + lowest) / 2)
20775 - (nrefy == 0 ? ascent + descent
20776 : nrefy == 1 ? descent - boff
20777 : nrefy == 2 ? 0
20778 : (ascent + descent) / 2));
20779 }
20780
20781 cmp->offsets[i * 2] = left;
20782 cmp->offsets[i * 2 + 1] = btm + descent;
20783
20784 /* Update the bounding box of the overall glyphs. */
20785 right = left + width;
20786 top = btm + descent + ascent;
20787 if (left < leftmost)
20788 leftmost = left;
20789 if (right > rightmost)
20790 rightmost = right;
20791 if (top > highest)
20792 highest = top;
20793 if (btm < lowest)
20794 lowest = btm;
20795 }
20796
20797 /* If there are glyphs whose x-offsets are negative,
20798 shift all glyphs to the right and make all x-offsets
20799 non-negative. */
20800 if (leftmost < 0)
20801 {
20802 for (i = 0; i < cmp->glyph_len; i++)
20803 cmp->offsets[i * 2] -= leftmost;
20804 rightmost -= leftmost;
20805 }
20806
20807 cmp->pixel_width = rightmost;
20808 cmp->ascent = highest;
20809 cmp->descent = - lowest;
20810 if (cmp->ascent < font_ascent)
20811 cmp->ascent = font_ascent;
20812 if (cmp->descent < font_descent)
20813 cmp->descent = font_descent;
20814 }
20815
20816 it->pixel_width = cmp->pixel_width;
20817 it->ascent = it->phys_ascent = cmp->ascent;
20818 it->descent = it->phys_descent = cmp->descent;
20819
20820 if (face->box != FACE_NO_BOX)
20821 {
20822 int thick = face->box_line_width;
20823
20824 if (thick > 0)
20825 {
20826 it->ascent += thick;
20827 it->descent += thick;
20828 }
20829 else
20830 thick = - thick;
20831
20832 if (it->start_of_box_run_p)
20833 it->pixel_width += thick;
20834 if (it->end_of_box_run_p)
20835 it->pixel_width += thick;
20836 }
20837
20838 /* If face has an overline, add the height of the overline
20839 (1 pixel) and a 1 pixel margin to the character height. */
20840 if (face->overline_p)
20841 it->ascent += overline_margin;
20842
20843 take_vertical_position_into_account (it);
20844
20845 if (it->glyph_row)
20846 append_composite_glyph (it);
20847 }
20848 else if (it->what == IT_IMAGE)
20849 produce_image_glyph (it);
20850 else if (it->what == IT_STRETCH)
20851 produce_stretch_glyph (it);
20852
20853 /* Accumulate dimensions. Note: can't assume that it->descent > 0
20854 because this isn't true for images with `:ascent 100'. */
20855 xassert (it->ascent >= 0 && it->descent >= 0);
20856 if (it->area == TEXT_AREA)
20857 it->current_x += it->pixel_width;
20858
20859 if (extra_line_spacing > 0)
20860 {
20861 it->descent += extra_line_spacing;
20862 if (extra_line_spacing > it->max_extra_line_spacing)
20863 it->max_extra_line_spacing = extra_line_spacing;
20864 }
20865
20866 it->max_ascent = max (it->max_ascent, it->ascent);
20867 it->max_descent = max (it->max_descent, it->descent);
20868 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
20869 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
20870 }
20871
20872 /* EXPORT for RIF:
20873 Output LEN glyphs starting at START at the nominal cursor position.
20874 Advance the nominal cursor over the text. The global variable
20875 updated_window contains the window being updated, updated_row is
20876 the glyph row being updated, and updated_area is the area of that
20877 row being updated. */
20878
20879 void
20880 x_write_glyphs (start, len)
20881 struct glyph *start;
20882 int len;
20883 {
20884 int x, hpos;
20885
20886 xassert (updated_window && updated_row);
20887 BLOCK_INPUT;
20888
20889 /* Write glyphs. */
20890
20891 hpos = start - updated_row->glyphs[updated_area];
20892 x = draw_glyphs (updated_window, output_cursor.x,
20893 updated_row, updated_area,
20894 hpos, hpos + len,
20895 DRAW_NORMAL_TEXT, 0);
20896
20897 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
20898 if (updated_area == TEXT_AREA
20899 && updated_window->phys_cursor_on_p
20900 && updated_window->phys_cursor.vpos == output_cursor.vpos
20901 && updated_window->phys_cursor.hpos >= hpos
20902 && updated_window->phys_cursor.hpos < hpos + len)
20903 updated_window->phys_cursor_on_p = 0;
20904
20905 UNBLOCK_INPUT;
20906
20907 /* Advance the output cursor. */
20908 output_cursor.hpos += len;
20909 output_cursor.x = x;
20910 }
20911
20912
20913 /* EXPORT for RIF:
20914 Insert LEN glyphs from START at the nominal cursor position. */
20915
20916 void
20917 x_insert_glyphs (start, len)
20918 struct glyph *start;
20919 int len;
20920 {
20921 struct frame *f;
20922 struct window *w;
20923 int line_height, shift_by_width, shifted_region_width;
20924 struct glyph_row *row;
20925 struct glyph *glyph;
20926 int frame_x, frame_y, hpos;
20927
20928 xassert (updated_window && updated_row);
20929 BLOCK_INPUT;
20930 w = updated_window;
20931 f = XFRAME (WINDOW_FRAME (w));
20932
20933 /* Get the height of the line we are in. */
20934 row = updated_row;
20935 line_height = row->height;
20936
20937 /* Get the width of the glyphs to insert. */
20938 shift_by_width = 0;
20939 for (glyph = start; glyph < start + len; ++glyph)
20940 shift_by_width += glyph->pixel_width;
20941
20942 /* Get the width of the region to shift right. */
20943 shifted_region_width = (window_box_width (w, updated_area)
20944 - output_cursor.x
20945 - shift_by_width);
20946
20947 /* Shift right. */
20948 frame_x = window_box_left (w, updated_area) + output_cursor.x;
20949 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
20950
20951 rif->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
20952 line_height, shift_by_width);
20953
20954 /* Write the glyphs. */
20955 hpos = start - row->glyphs[updated_area];
20956 draw_glyphs (w, output_cursor.x, row, updated_area,
20957 hpos, hpos + len,
20958 DRAW_NORMAL_TEXT, 0);
20959
20960 /* Advance the output cursor. */
20961 output_cursor.hpos += len;
20962 output_cursor.x += shift_by_width;
20963 UNBLOCK_INPUT;
20964 }
20965
20966
20967 /* EXPORT for RIF:
20968 Erase the current text line from the nominal cursor position
20969 (inclusive) to pixel column TO_X (exclusive). The idea is that
20970 everything from TO_X onward is already erased.
20971
20972 TO_X is a pixel position relative to updated_area of
20973 updated_window. TO_X == -1 means clear to the end of this area. */
20974
20975 void
20976 x_clear_end_of_line (to_x)
20977 int to_x;
20978 {
20979 struct frame *f;
20980 struct window *w = updated_window;
20981 int max_x, min_y, max_y;
20982 int from_x, from_y, to_y;
20983
20984 xassert (updated_window && updated_row);
20985 f = XFRAME (w->frame);
20986
20987 if (updated_row->full_width_p)
20988 max_x = WINDOW_TOTAL_WIDTH (w);
20989 else
20990 max_x = window_box_width (w, updated_area);
20991 max_y = window_text_bottom_y (w);
20992
20993 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
20994 of window. For TO_X > 0, truncate to end of drawing area. */
20995 if (to_x == 0)
20996 return;
20997 else if (to_x < 0)
20998 to_x = max_x;
20999 else
21000 to_x = min (to_x, max_x);
21001
21002 to_y = min (max_y, output_cursor.y + updated_row->height);
21003
21004 /* Notice if the cursor will be cleared by this operation. */
21005 if (!updated_row->full_width_p)
21006 notice_overwritten_cursor (w, updated_area,
21007 output_cursor.x, -1,
21008 updated_row->y,
21009 MATRIX_ROW_BOTTOM_Y (updated_row));
21010
21011 from_x = output_cursor.x;
21012
21013 /* Translate to frame coordinates. */
21014 if (updated_row->full_width_p)
21015 {
21016 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
21017 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
21018 }
21019 else
21020 {
21021 int area_left = window_box_left (w, updated_area);
21022 from_x += area_left;
21023 to_x += area_left;
21024 }
21025
21026 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
21027 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
21028 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
21029
21030 /* Prevent inadvertently clearing to end of the X window. */
21031 if (to_x > from_x && to_y > from_y)
21032 {
21033 BLOCK_INPUT;
21034 rif->clear_frame_area (f, from_x, from_y,
21035 to_x - from_x, to_y - from_y);
21036 UNBLOCK_INPUT;
21037 }
21038 }
21039
21040 #endif /* HAVE_WINDOW_SYSTEM */
21041
21042
21043 \f
21044 /***********************************************************************
21045 Cursor types
21046 ***********************************************************************/
21047
21048 /* Value is the internal representation of the specified cursor type
21049 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
21050 of the bar cursor. */
21051
21052 static enum text_cursor_kinds
21053 get_specified_cursor_type (arg, width)
21054 Lisp_Object arg;
21055 int *width;
21056 {
21057 enum text_cursor_kinds type;
21058
21059 if (NILP (arg))
21060 return NO_CURSOR;
21061
21062 if (EQ (arg, Qbox))
21063 return FILLED_BOX_CURSOR;
21064
21065 if (EQ (arg, Qhollow))
21066 return HOLLOW_BOX_CURSOR;
21067
21068 if (EQ (arg, Qbar))
21069 {
21070 *width = 2;
21071 return BAR_CURSOR;
21072 }
21073
21074 if (CONSP (arg)
21075 && EQ (XCAR (arg), Qbar)
21076 && INTEGERP (XCDR (arg))
21077 && XINT (XCDR (arg)) >= 0)
21078 {
21079 *width = XINT (XCDR (arg));
21080 return BAR_CURSOR;
21081 }
21082
21083 if (EQ (arg, Qhbar))
21084 {
21085 *width = 2;
21086 return HBAR_CURSOR;
21087 }
21088
21089 if (CONSP (arg)
21090 && EQ (XCAR (arg), Qhbar)
21091 && INTEGERP (XCDR (arg))
21092 && XINT (XCDR (arg)) >= 0)
21093 {
21094 *width = XINT (XCDR (arg));
21095 return HBAR_CURSOR;
21096 }
21097
21098 /* Treat anything unknown as "hollow box cursor".
21099 It was bad to signal an error; people have trouble fixing
21100 .Xdefaults with Emacs, when it has something bad in it. */
21101 type = HOLLOW_BOX_CURSOR;
21102
21103 return type;
21104 }
21105
21106 /* Set the default cursor types for specified frame. */
21107 void
21108 set_frame_cursor_types (f, arg)
21109 struct frame *f;
21110 Lisp_Object arg;
21111 {
21112 int width;
21113 Lisp_Object tem;
21114
21115 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
21116 FRAME_CURSOR_WIDTH (f) = width;
21117
21118 /* By default, set up the blink-off state depending on the on-state. */
21119
21120 tem = Fassoc (arg, Vblink_cursor_alist);
21121 if (!NILP (tem))
21122 {
21123 FRAME_BLINK_OFF_CURSOR (f)
21124 = get_specified_cursor_type (XCDR (tem), &width);
21125 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
21126 }
21127 else
21128 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
21129 }
21130
21131
21132 /* Return the cursor we want to be displayed in window W. Return
21133 width of bar/hbar cursor through WIDTH arg. Return with
21134 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
21135 (i.e. if the `system caret' should track this cursor).
21136
21137 In a mini-buffer window, we want the cursor only to appear if we
21138 are reading input from this window. For the selected window, we
21139 want the cursor type given by the frame parameter or buffer local
21140 setting of cursor-type. If explicitly marked off, draw no cursor.
21141 In all other cases, we want a hollow box cursor. */
21142
21143 static enum text_cursor_kinds
21144 get_window_cursor_type (w, glyph, width, active_cursor)
21145 struct window *w;
21146 struct glyph *glyph;
21147 int *width;
21148 int *active_cursor;
21149 {
21150 struct frame *f = XFRAME (w->frame);
21151 struct buffer *b = XBUFFER (w->buffer);
21152 int cursor_type = DEFAULT_CURSOR;
21153 Lisp_Object alt_cursor;
21154 int non_selected = 0;
21155
21156 *active_cursor = 1;
21157
21158 /* Echo area */
21159 if (cursor_in_echo_area
21160 && FRAME_HAS_MINIBUF_P (f)
21161 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
21162 {
21163 if (w == XWINDOW (echo_area_window))
21164 {
21165 if (EQ (b->cursor_type, Qt) || NILP (b->cursor_type))
21166 {
21167 *width = FRAME_CURSOR_WIDTH (f);
21168 return FRAME_DESIRED_CURSOR (f);
21169 }
21170 else
21171 return get_specified_cursor_type (b->cursor_type, width);
21172 }
21173
21174 *active_cursor = 0;
21175 non_selected = 1;
21176 }
21177
21178 /* Nonselected window or nonselected frame. */
21179 else if (w != XWINDOW (f->selected_window)
21180 #ifdef HAVE_WINDOW_SYSTEM
21181 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
21182 #endif
21183 )
21184 {
21185 *active_cursor = 0;
21186
21187 if (MINI_WINDOW_P (w) && minibuf_level == 0)
21188 return NO_CURSOR;
21189
21190 non_selected = 1;
21191 }
21192
21193 /* Never display a cursor in a window in which cursor-type is nil. */
21194 if (NILP (b->cursor_type))
21195 return NO_CURSOR;
21196
21197 /* Use cursor-in-non-selected-windows for non-selected window or frame. */
21198 if (non_selected)
21199 {
21200 alt_cursor = b->cursor_in_non_selected_windows;
21201 return get_specified_cursor_type (alt_cursor, width);
21202 }
21203
21204 /* Get the normal cursor type for this window. */
21205 if (EQ (b->cursor_type, Qt))
21206 {
21207 cursor_type = FRAME_DESIRED_CURSOR (f);
21208 *width = FRAME_CURSOR_WIDTH (f);
21209 }
21210 else
21211 cursor_type = get_specified_cursor_type (b->cursor_type, width);
21212
21213 /* Use normal cursor if not blinked off. */
21214 if (!w->cursor_off_p)
21215 {
21216 if (glyph != NULL && glyph->type == IMAGE_GLYPH) {
21217 if (cursor_type == FILLED_BOX_CURSOR)
21218 cursor_type = HOLLOW_BOX_CURSOR;
21219 }
21220 return cursor_type;
21221 }
21222
21223 /* Cursor is blinked off, so determine how to "toggle" it. */
21224
21225 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
21226 if ((alt_cursor = Fassoc (b->cursor_type, Vblink_cursor_alist), !NILP (alt_cursor)))
21227 return get_specified_cursor_type (XCDR (alt_cursor), width);
21228
21229 /* Then see if frame has specified a specific blink off cursor type. */
21230 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
21231 {
21232 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
21233 return FRAME_BLINK_OFF_CURSOR (f);
21234 }
21235
21236 #if 0
21237 /* Some people liked having a permanently visible blinking cursor,
21238 while others had very strong opinions against it. So it was
21239 decided to remove it. KFS 2003-09-03 */
21240
21241 /* Finally perform built-in cursor blinking:
21242 filled box <-> hollow box
21243 wide [h]bar <-> narrow [h]bar
21244 narrow [h]bar <-> no cursor
21245 other type <-> no cursor */
21246
21247 if (cursor_type == FILLED_BOX_CURSOR)
21248 return HOLLOW_BOX_CURSOR;
21249
21250 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
21251 {
21252 *width = 1;
21253 return cursor_type;
21254 }
21255 #endif
21256
21257 return NO_CURSOR;
21258 }
21259
21260
21261 #ifdef HAVE_WINDOW_SYSTEM
21262
21263 /* Notice when the text cursor of window W has been completely
21264 overwritten by a drawing operation that outputs glyphs in AREA
21265 starting at X0 and ending at X1 in the line starting at Y0 and
21266 ending at Y1. X coordinates are area-relative. X1 < 0 means all
21267 the rest of the line after X0 has been written. Y coordinates
21268 are window-relative. */
21269
21270 static void
21271 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
21272 struct window *w;
21273 enum glyph_row_area area;
21274 int x0, y0, x1, y1;
21275 {
21276 int cx0, cx1, cy0, cy1;
21277 struct glyph_row *row;
21278
21279 if (!w->phys_cursor_on_p)
21280 return;
21281 if (area != TEXT_AREA)
21282 return;
21283
21284 if (w->phys_cursor.vpos < 0
21285 || w->phys_cursor.vpos >= w->current_matrix->nrows
21286 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
21287 !(row->enabled_p && row->displays_text_p)))
21288 return;
21289
21290 if (row->cursor_in_fringe_p)
21291 {
21292 row->cursor_in_fringe_p = 0;
21293 draw_fringe_bitmap (w, row, 0);
21294 w->phys_cursor_on_p = 0;
21295 return;
21296 }
21297
21298 cx0 = w->phys_cursor.x;
21299 cx1 = cx0 + w->phys_cursor_width;
21300 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
21301 return;
21302
21303 /* The cursor image will be completely removed from the
21304 screen if the output area intersects the cursor area in
21305 y-direction. When we draw in [y0 y1[, and some part of
21306 the cursor is at y < y0, that part must have been drawn
21307 before. When scrolling, the cursor is erased before
21308 actually scrolling, so we don't come here. When not
21309 scrolling, the rows above the old cursor row must have
21310 changed, and in this case these rows must have written
21311 over the cursor image.
21312
21313 Likewise if part of the cursor is below y1, with the
21314 exception of the cursor being in the first blank row at
21315 the buffer and window end because update_text_area
21316 doesn't draw that row. (Except when it does, but
21317 that's handled in update_text_area.) */
21318
21319 cy0 = w->phys_cursor.y;
21320 cy1 = cy0 + w->phys_cursor_height;
21321 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
21322 return;
21323
21324 w->phys_cursor_on_p = 0;
21325 }
21326
21327 #endif /* HAVE_WINDOW_SYSTEM */
21328
21329 \f
21330 /************************************************************************
21331 Mouse Face
21332 ************************************************************************/
21333
21334 #ifdef HAVE_WINDOW_SYSTEM
21335
21336 /* EXPORT for RIF:
21337 Fix the display of area AREA of overlapping row ROW in window W
21338 with respect to the overlapping part OVERLAPS. */
21339
21340 void
21341 x_fix_overlapping_area (w, row, area, overlaps)
21342 struct window *w;
21343 struct glyph_row *row;
21344 enum glyph_row_area area;
21345 int overlaps;
21346 {
21347 int i, x;
21348
21349 BLOCK_INPUT;
21350
21351 x = 0;
21352 for (i = 0; i < row->used[area];)
21353 {
21354 if (row->glyphs[area][i].overlaps_vertically_p)
21355 {
21356 int start = i, start_x = x;
21357
21358 do
21359 {
21360 x += row->glyphs[area][i].pixel_width;
21361 ++i;
21362 }
21363 while (i < row->used[area]
21364 && row->glyphs[area][i].overlaps_vertically_p);
21365
21366 draw_glyphs (w, start_x, row, area,
21367 start, i,
21368 DRAW_NORMAL_TEXT, overlaps);
21369 }
21370 else
21371 {
21372 x += row->glyphs[area][i].pixel_width;
21373 ++i;
21374 }
21375 }
21376
21377 UNBLOCK_INPUT;
21378 }
21379
21380
21381 /* EXPORT:
21382 Draw the cursor glyph of window W in glyph row ROW. See the
21383 comment of draw_glyphs for the meaning of HL. */
21384
21385 void
21386 draw_phys_cursor_glyph (w, row, hl)
21387 struct window *w;
21388 struct glyph_row *row;
21389 enum draw_glyphs_face hl;
21390 {
21391 /* If cursor hpos is out of bounds, don't draw garbage. This can
21392 happen in mini-buffer windows when switching between echo area
21393 glyphs and mini-buffer. */
21394 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
21395 {
21396 int on_p = w->phys_cursor_on_p;
21397 int x1;
21398 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
21399 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
21400 hl, 0);
21401 w->phys_cursor_on_p = on_p;
21402
21403 if (hl == DRAW_CURSOR)
21404 w->phys_cursor_width = x1 - w->phys_cursor.x;
21405 /* When we erase the cursor, and ROW is overlapped by other
21406 rows, make sure that these overlapping parts of other rows
21407 are redrawn. */
21408 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
21409 {
21410 w->phys_cursor_width = x1 - w->phys_cursor.x;
21411
21412 if (row > w->current_matrix->rows
21413 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
21414 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
21415 OVERLAPS_ERASED_CURSOR);
21416
21417 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
21418 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
21419 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
21420 OVERLAPS_ERASED_CURSOR);
21421 }
21422 }
21423 }
21424
21425
21426 /* EXPORT:
21427 Erase the image of a cursor of window W from the screen. */
21428
21429 void
21430 erase_phys_cursor (w)
21431 struct window *w;
21432 {
21433 struct frame *f = XFRAME (w->frame);
21434 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
21435 int hpos = w->phys_cursor.hpos;
21436 int vpos = w->phys_cursor.vpos;
21437 int mouse_face_here_p = 0;
21438 struct glyph_matrix *active_glyphs = w->current_matrix;
21439 struct glyph_row *cursor_row;
21440 struct glyph *cursor_glyph;
21441 enum draw_glyphs_face hl;
21442
21443 /* No cursor displayed or row invalidated => nothing to do on the
21444 screen. */
21445 if (w->phys_cursor_type == NO_CURSOR)
21446 goto mark_cursor_off;
21447
21448 /* VPOS >= active_glyphs->nrows means that window has been resized.
21449 Don't bother to erase the cursor. */
21450 if (vpos >= active_glyphs->nrows)
21451 goto mark_cursor_off;
21452
21453 /* If row containing cursor is marked invalid, there is nothing we
21454 can do. */
21455 cursor_row = MATRIX_ROW (active_glyphs, vpos);
21456 if (!cursor_row->enabled_p)
21457 goto mark_cursor_off;
21458
21459 /* If line spacing is > 0, old cursor may only be partially visible in
21460 window after split-window. So adjust visible height. */
21461 cursor_row->visible_height = min (cursor_row->visible_height,
21462 window_text_bottom_y (w) - cursor_row->y);
21463
21464 /* If row is completely invisible, don't attempt to delete a cursor which
21465 isn't there. This can happen if cursor is at top of a window, and
21466 we switch to a buffer with a header line in that window. */
21467 if (cursor_row->visible_height <= 0)
21468 goto mark_cursor_off;
21469
21470 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
21471 if (cursor_row->cursor_in_fringe_p)
21472 {
21473 cursor_row->cursor_in_fringe_p = 0;
21474 draw_fringe_bitmap (w, cursor_row, 0);
21475 goto mark_cursor_off;
21476 }
21477
21478 /* This can happen when the new row is shorter than the old one.
21479 In this case, either draw_glyphs or clear_end_of_line
21480 should have cleared the cursor. Note that we wouldn't be
21481 able to erase the cursor in this case because we don't have a
21482 cursor glyph at hand. */
21483 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
21484 goto mark_cursor_off;
21485
21486 /* If the cursor is in the mouse face area, redisplay that when
21487 we clear the cursor. */
21488 if (! NILP (dpyinfo->mouse_face_window)
21489 && w == XWINDOW (dpyinfo->mouse_face_window)
21490 && (vpos > dpyinfo->mouse_face_beg_row
21491 || (vpos == dpyinfo->mouse_face_beg_row
21492 && hpos >= dpyinfo->mouse_face_beg_col))
21493 && (vpos < dpyinfo->mouse_face_end_row
21494 || (vpos == dpyinfo->mouse_face_end_row
21495 && hpos < dpyinfo->mouse_face_end_col))
21496 /* Don't redraw the cursor's spot in mouse face if it is at the
21497 end of a line (on a newline). The cursor appears there, but
21498 mouse highlighting does not. */
21499 && cursor_row->used[TEXT_AREA] > hpos)
21500 mouse_face_here_p = 1;
21501
21502 /* Maybe clear the display under the cursor. */
21503 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
21504 {
21505 int x, y, left_x;
21506 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
21507 int width;
21508
21509 cursor_glyph = get_phys_cursor_glyph (w);
21510 if (cursor_glyph == NULL)
21511 goto mark_cursor_off;
21512
21513 width = cursor_glyph->pixel_width;
21514 left_x = window_box_left_offset (w, TEXT_AREA);
21515 x = w->phys_cursor.x;
21516 if (x < left_x)
21517 width -= left_x - x;
21518 width = min (width, window_box_width (w, TEXT_AREA) - x);
21519 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
21520 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
21521
21522 if (width > 0)
21523 rif->clear_frame_area (f, x, y, width, cursor_row->visible_height);
21524 }
21525
21526 /* Erase the cursor by redrawing the character underneath it. */
21527 if (mouse_face_here_p)
21528 hl = DRAW_MOUSE_FACE;
21529 else
21530 hl = DRAW_NORMAL_TEXT;
21531 draw_phys_cursor_glyph (w, cursor_row, hl);
21532
21533 mark_cursor_off:
21534 w->phys_cursor_on_p = 0;
21535 w->phys_cursor_type = NO_CURSOR;
21536 }
21537
21538
21539 /* EXPORT:
21540 Display or clear cursor of window W. If ON is zero, clear the
21541 cursor. If it is non-zero, display the cursor. If ON is nonzero,
21542 where to put the cursor is specified by HPOS, VPOS, X and Y. */
21543
21544 void
21545 display_and_set_cursor (w, on, hpos, vpos, x, y)
21546 struct window *w;
21547 int on, hpos, vpos, x, y;
21548 {
21549 struct frame *f = XFRAME (w->frame);
21550 int new_cursor_type;
21551 int new_cursor_width;
21552 int active_cursor;
21553 struct glyph_row *glyph_row;
21554 struct glyph *glyph;
21555
21556 /* This is pointless on invisible frames, and dangerous on garbaged
21557 windows and frames; in the latter case, the frame or window may
21558 be in the midst of changing its size, and x and y may be off the
21559 window. */
21560 if (! FRAME_VISIBLE_P (f)
21561 || FRAME_GARBAGED_P (f)
21562 || vpos >= w->current_matrix->nrows
21563 || hpos >= w->current_matrix->matrix_w)
21564 return;
21565
21566 /* If cursor is off and we want it off, return quickly. */
21567 if (!on && !w->phys_cursor_on_p)
21568 return;
21569
21570 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
21571 /* If cursor row is not enabled, we don't really know where to
21572 display the cursor. */
21573 if (!glyph_row->enabled_p)
21574 {
21575 w->phys_cursor_on_p = 0;
21576 return;
21577 }
21578
21579 glyph = NULL;
21580 if (!glyph_row->exact_window_width_line_p
21581 || hpos < glyph_row->used[TEXT_AREA])
21582 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
21583
21584 xassert (interrupt_input_blocked);
21585
21586 /* Set new_cursor_type to the cursor we want to be displayed. */
21587 new_cursor_type = get_window_cursor_type (w, glyph,
21588 &new_cursor_width, &active_cursor);
21589
21590 /* If cursor is currently being shown and we don't want it to be or
21591 it is in the wrong place, or the cursor type is not what we want,
21592 erase it. */
21593 if (w->phys_cursor_on_p
21594 && (!on
21595 || w->phys_cursor.x != x
21596 || w->phys_cursor.y != y
21597 || new_cursor_type != w->phys_cursor_type
21598 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
21599 && new_cursor_width != w->phys_cursor_width)))
21600 erase_phys_cursor (w);
21601
21602 /* Don't check phys_cursor_on_p here because that flag is only set
21603 to zero in some cases where we know that the cursor has been
21604 completely erased, to avoid the extra work of erasing the cursor
21605 twice. In other words, phys_cursor_on_p can be 1 and the cursor
21606 still not be visible, or it has only been partly erased. */
21607 if (on)
21608 {
21609 w->phys_cursor_ascent = glyph_row->ascent;
21610 w->phys_cursor_height = glyph_row->height;
21611
21612 /* Set phys_cursor_.* before x_draw_.* is called because some
21613 of them may need the information. */
21614 w->phys_cursor.x = x;
21615 w->phys_cursor.y = glyph_row->y;
21616 w->phys_cursor.hpos = hpos;
21617 w->phys_cursor.vpos = vpos;
21618 }
21619
21620 rif->draw_window_cursor (w, glyph_row, x, y,
21621 new_cursor_type, new_cursor_width,
21622 on, active_cursor);
21623 }
21624
21625
21626 /* Switch the display of W's cursor on or off, according to the value
21627 of ON. */
21628
21629 static void
21630 update_window_cursor (w, on)
21631 struct window *w;
21632 int on;
21633 {
21634 /* Don't update cursor in windows whose frame is in the process
21635 of being deleted. */
21636 if (w->current_matrix)
21637 {
21638 BLOCK_INPUT;
21639 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
21640 w->phys_cursor.x, w->phys_cursor.y);
21641 UNBLOCK_INPUT;
21642 }
21643 }
21644
21645
21646 /* Call update_window_cursor with parameter ON_P on all leaf windows
21647 in the window tree rooted at W. */
21648
21649 static void
21650 update_cursor_in_window_tree (w, on_p)
21651 struct window *w;
21652 int on_p;
21653 {
21654 while (w)
21655 {
21656 if (!NILP (w->hchild))
21657 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
21658 else if (!NILP (w->vchild))
21659 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
21660 else
21661 update_window_cursor (w, on_p);
21662
21663 w = NILP (w->next) ? 0 : XWINDOW (w->next);
21664 }
21665 }
21666
21667
21668 /* EXPORT:
21669 Display the cursor on window W, or clear it, according to ON_P.
21670 Don't change the cursor's position. */
21671
21672 void
21673 x_update_cursor (f, on_p)
21674 struct frame *f;
21675 int on_p;
21676 {
21677 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
21678 }
21679
21680
21681 /* EXPORT:
21682 Clear the cursor of window W to background color, and mark the
21683 cursor as not shown. This is used when the text where the cursor
21684 is is about to be rewritten. */
21685
21686 void
21687 x_clear_cursor (w)
21688 struct window *w;
21689 {
21690 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
21691 update_window_cursor (w, 0);
21692 }
21693
21694
21695 /* EXPORT:
21696 Display the active region described by mouse_face_* according to DRAW. */
21697
21698 void
21699 show_mouse_face (dpyinfo, draw)
21700 Display_Info *dpyinfo;
21701 enum draw_glyphs_face draw;
21702 {
21703 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
21704 struct frame *f = XFRAME (WINDOW_FRAME (w));
21705
21706 if (/* If window is in the process of being destroyed, don't bother
21707 to do anything. */
21708 w->current_matrix != NULL
21709 /* Don't update mouse highlight if hidden */
21710 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
21711 /* Recognize when we are called to operate on rows that don't exist
21712 anymore. This can happen when a window is split. */
21713 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
21714 {
21715 int phys_cursor_on_p = w->phys_cursor_on_p;
21716 struct glyph_row *row, *first, *last;
21717
21718 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
21719 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
21720
21721 for (row = first; row <= last && row->enabled_p; ++row)
21722 {
21723 int start_hpos, end_hpos, start_x;
21724
21725 /* For all but the first row, the highlight starts at column 0. */
21726 if (row == first)
21727 {
21728 start_hpos = dpyinfo->mouse_face_beg_col;
21729 start_x = dpyinfo->mouse_face_beg_x;
21730 }
21731 else
21732 {
21733 start_hpos = 0;
21734 start_x = 0;
21735 }
21736
21737 if (row == last)
21738 end_hpos = dpyinfo->mouse_face_end_col;
21739 else
21740 {
21741 end_hpos = row->used[TEXT_AREA];
21742 if (draw == DRAW_NORMAL_TEXT)
21743 row->fill_line_p = 1; /* Clear to end of line */
21744 }
21745
21746 if (end_hpos > start_hpos)
21747 {
21748 draw_glyphs (w, start_x, row, TEXT_AREA,
21749 start_hpos, end_hpos,
21750 draw, 0);
21751
21752 row->mouse_face_p
21753 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
21754 }
21755 }
21756
21757 /* When we've written over the cursor, arrange for it to
21758 be displayed again. */
21759 if (phys_cursor_on_p && !w->phys_cursor_on_p)
21760 {
21761 BLOCK_INPUT;
21762 display_and_set_cursor (w, 1,
21763 w->phys_cursor.hpos, w->phys_cursor.vpos,
21764 w->phys_cursor.x, w->phys_cursor.y);
21765 UNBLOCK_INPUT;
21766 }
21767 }
21768
21769 /* Change the mouse cursor. */
21770 if (draw == DRAW_NORMAL_TEXT)
21771 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
21772 else if (draw == DRAW_MOUSE_FACE)
21773 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
21774 else
21775 rif->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
21776 }
21777
21778 /* EXPORT:
21779 Clear out the mouse-highlighted active region.
21780 Redraw it un-highlighted first. Value is non-zero if mouse
21781 face was actually drawn unhighlighted. */
21782
21783 int
21784 clear_mouse_face (dpyinfo)
21785 Display_Info *dpyinfo;
21786 {
21787 int cleared = 0;
21788
21789 if (!dpyinfo->mouse_face_hidden && !NILP (dpyinfo->mouse_face_window))
21790 {
21791 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
21792 cleared = 1;
21793 }
21794
21795 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
21796 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
21797 dpyinfo->mouse_face_window = Qnil;
21798 dpyinfo->mouse_face_overlay = Qnil;
21799 return cleared;
21800 }
21801
21802
21803 /* EXPORT:
21804 Non-zero if physical cursor of window W is within mouse face. */
21805
21806 int
21807 cursor_in_mouse_face_p (w)
21808 struct window *w;
21809 {
21810 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
21811 int in_mouse_face = 0;
21812
21813 if (WINDOWP (dpyinfo->mouse_face_window)
21814 && XWINDOW (dpyinfo->mouse_face_window) == w)
21815 {
21816 int hpos = w->phys_cursor.hpos;
21817 int vpos = w->phys_cursor.vpos;
21818
21819 if (vpos >= dpyinfo->mouse_face_beg_row
21820 && vpos <= dpyinfo->mouse_face_end_row
21821 && (vpos > dpyinfo->mouse_face_beg_row
21822 || hpos >= dpyinfo->mouse_face_beg_col)
21823 && (vpos < dpyinfo->mouse_face_end_row
21824 || hpos < dpyinfo->mouse_face_end_col
21825 || dpyinfo->mouse_face_past_end))
21826 in_mouse_face = 1;
21827 }
21828
21829 return in_mouse_face;
21830 }
21831
21832
21833
21834 \f
21835 /* Find the glyph matrix position of buffer position CHARPOS in window
21836 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
21837 current glyphs must be up to date. If CHARPOS is above window
21838 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
21839 of last line in W. In the row containing CHARPOS, stop before glyphs
21840 having STOP as object. */
21841
21842 #if 1 /* This is a version of fast_find_position that's more correct
21843 in the presence of hscrolling, for example. I didn't install
21844 it right away because the problem fixed is minor, it failed
21845 in 20.x as well, and I think it's too risky to install
21846 so near the release of 21.1. 2001-09-25 gerd. */
21847
21848 static int
21849 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
21850 struct window *w;
21851 int charpos;
21852 int *hpos, *vpos, *x, *y;
21853 Lisp_Object stop;
21854 {
21855 struct glyph_row *row, *first;
21856 struct glyph *glyph, *end;
21857 int past_end = 0;
21858
21859 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21860 if (charpos < MATRIX_ROW_START_CHARPOS (first))
21861 {
21862 *x = first->x;
21863 *y = first->y;
21864 *hpos = 0;
21865 *vpos = MATRIX_ROW_VPOS (first, w->current_matrix);
21866 return 1;
21867 }
21868
21869 row = row_containing_pos (w, charpos, first, NULL, 0);
21870 if (row == NULL)
21871 {
21872 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
21873 past_end = 1;
21874 }
21875
21876 /* If whole rows or last part of a row came from a display overlay,
21877 row_containing_pos will skip over such rows because their end pos
21878 equals the start pos of the overlay or interval.
21879
21880 Move back if we have a STOP object and previous row's
21881 end glyph came from STOP. */
21882 if (!NILP (stop))
21883 {
21884 struct glyph_row *prev;
21885 while ((prev = row - 1, prev >= first)
21886 && MATRIX_ROW_END_CHARPOS (prev) == charpos
21887 && prev->used[TEXT_AREA] > 0)
21888 {
21889 struct glyph *beg = prev->glyphs[TEXT_AREA];
21890 glyph = beg + prev->used[TEXT_AREA];
21891 while (--glyph >= beg
21892 && INTEGERP (glyph->object));
21893 if (glyph < beg
21894 || !EQ (stop, glyph->object))
21895 break;
21896 row = prev;
21897 }
21898 }
21899
21900 *x = row->x;
21901 *y = row->y;
21902 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21903
21904 glyph = row->glyphs[TEXT_AREA];
21905 end = glyph + row->used[TEXT_AREA];
21906
21907 /* Skip over glyphs not having an object at the start of the row.
21908 These are special glyphs like truncation marks on terminal
21909 frames. */
21910 if (row->displays_text_p)
21911 while (glyph < end
21912 && INTEGERP (glyph->object)
21913 && !EQ (stop, glyph->object)
21914 && glyph->charpos < 0)
21915 {
21916 *x += glyph->pixel_width;
21917 ++glyph;
21918 }
21919
21920 while (glyph < end
21921 && !INTEGERP (glyph->object)
21922 && !EQ (stop, glyph->object)
21923 && (!BUFFERP (glyph->object)
21924 || glyph->charpos < charpos))
21925 {
21926 *x += glyph->pixel_width;
21927 ++glyph;
21928 }
21929
21930 *hpos = glyph - row->glyphs[TEXT_AREA];
21931 return !past_end;
21932 }
21933
21934 #else /* not 1 */
21935
21936 static int
21937 fast_find_position (w, pos, hpos, vpos, x, y, stop)
21938 struct window *w;
21939 int pos;
21940 int *hpos, *vpos, *x, *y;
21941 Lisp_Object stop;
21942 {
21943 int i;
21944 int lastcol;
21945 int maybe_next_line_p = 0;
21946 int line_start_position;
21947 int yb = window_text_bottom_y (w);
21948 struct glyph_row *row, *best_row;
21949 int row_vpos, best_row_vpos;
21950 int current_x;
21951
21952 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
21953 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
21954
21955 while (row->y < yb)
21956 {
21957 if (row->used[TEXT_AREA])
21958 line_start_position = row->glyphs[TEXT_AREA]->charpos;
21959 else
21960 line_start_position = 0;
21961
21962 if (line_start_position > pos)
21963 break;
21964 /* If the position sought is the end of the buffer,
21965 don't include the blank lines at the bottom of the window. */
21966 else if (line_start_position == pos
21967 && pos == BUF_ZV (XBUFFER (w->buffer)))
21968 {
21969 maybe_next_line_p = 1;
21970 break;
21971 }
21972 else if (line_start_position > 0)
21973 {
21974 best_row = row;
21975 best_row_vpos = row_vpos;
21976 }
21977
21978 if (row->y + row->height >= yb)
21979 break;
21980
21981 ++row;
21982 ++row_vpos;
21983 }
21984
21985 /* Find the right column within BEST_ROW. */
21986 lastcol = 0;
21987 current_x = best_row->x;
21988 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
21989 {
21990 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
21991 int charpos = glyph->charpos;
21992
21993 if (BUFFERP (glyph->object))
21994 {
21995 if (charpos == pos)
21996 {
21997 *hpos = i;
21998 *vpos = best_row_vpos;
21999 *x = current_x;
22000 *y = best_row->y;
22001 return 1;
22002 }
22003 else if (charpos > pos)
22004 break;
22005 }
22006 else if (EQ (glyph->object, stop))
22007 break;
22008
22009 if (charpos > 0)
22010 lastcol = i;
22011 current_x += glyph->pixel_width;
22012 }
22013
22014 /* If we're looking for the end of the buffer,
22015 and we didn't find it in the line we scanned,
22016 use the start of the following line. */
22017 if (maybe_next_line_p)
22018 {
22019 ++best_row;
22020 ++best_row_vpos;
22021 lastcol = 0;
22022 current_x = best_row->x;
22023 }
22024
22025 *vpos = best_row_vpos;
22026 *hpos = lastcol + 1;
22027 *x = current_x;
22028 *y = best_row->y;
22029 return 0;
22030 }
22031
22032 #endif /* not 1 */
22033
22034
22035 /* Find the position of the glyph for position POS in OBJECT in
22036 window W's current matrix, and return in *X, *Y the pixel
22037 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
22038
22039 RIGHT_P non-zero means return the position of the right edge of the
22040 glyph, RIGHT_P zero means return the left edge position.
22041
22042 If no glyph for POS exists in the matrix, return the position of
22043 the glyph with the next smaller position that is in the matrix, if
22044 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
22045 exists in the matrix, return the position of the glyph with the
22046 next larger position in OBJECT.
22047
22048 Value is non-zero if a glyph was found. */
22049
22050 static int
22051 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
22052 struct window *w;
22053 int pos;
22054 Lisp_Object object;
22055 int *hpos, *vpos, *x, *y;
22056 int right_p;
22057 {
22058 int yb = window_text_bottom_y (w);
22059 struct glyph_row *r;
22060 struct glyph *best_glyph = NULL;
22061 struct glyph_row *best_row = NULL;
22062 int best_x = 0;
22063
22064 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
22065 r->enabled_p && r->y < yb;
22066 ++r)
22067 {
22068 struct glyph *g = r->glyphs[TEXT_AREA];
22069 struct glyph *e = g + r->used[TEXT_AREA];
22070 int gx;
22071
22072 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
22073 if (EQ (g->object, object))
22074 {
22075 if (g->charpos == pos)
22076 {
22077 best_glyph = g;
22078 best_x = gx;
22079 best_row = r;
22080 goto found;
22081 }
22082 else if (best_glyph == NULL
22083 || ((abs (g->charpos - pos)
22084 < abs (best_glyph->charpos - pos))
22085 && (right_p
22086 ? g->charpos < pos
22087 : g->charpos > pos)))
22088 {
22089 best_glyph = g;
22090 best_x = gx;
22091 best_row = r;
22092 }
22093 }
22094 }
22095
22096 found:
22097
22098 if (best_glyph)
22099 {
22100 *x = best_x;
22101 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
22102
22103 if (right_p)
22104 {
22105 *x += best_glyph->pixel_width;
22106 ++*hpos;
22107 }
22108
22109 *y = best_row->y;
22110 *vpos = best_row - w->current_matrix->rows;
22111 }
22112
22113 return best_glyph != NULL;
22114 }
22115
22116
22117 /* See if position X, Y is within a hot-spot of an image. */
22118
22119 static int
22120 on_hot_spot_p (hot_spot, x, y)
22121 Lisp_Object hot_spot;
22122 int x, y;
22123 {
22124 if (!CONSP (hot_spot))
22125 return 0;
22126
22127 if (EQ (XCAR (hot_spot), Qrect))
22128 {
22129 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
22130 Lisp_Object rect = XCDR (hot_spot);
22131 Lisp_Object tem;
22132 if (!CONSP (rect))
22133 return 0;
22134 if (!CONSP (XCAR (rect)))
22135 return 0;
22136 if (!CONSP (XCDR (rect)))
22137 return 0;
22138 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
22139 return 0;
22140 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
22141 return 0;
22142 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
22143 return 0;
22144 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
22145 return 0;
22146 return 1;
22147 }
22148 else if (EQ (XCAR (hot_spot), Qcircle))
22149 {
22150 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
22151 Lisp_Object circ = XCDR (hot_spot);
22152 Lisp_Object lr, lx0, ly0;
22153 if (CONSP (circ)
22154 && CONSP (XCAR (circ))
22155 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
22156 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
22157 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
22158 {
22159 double r = XFLOATINT (lr);
22160 double dx = XINT (lx0) - x;
22161 double dy = XINT (ly0) - y;
22162 return (dx * dx + dy * dy <= r * r);
22163 }
22164 }
22165 else if (EQ (XCAR (hot_spot), Qpoly))
22166 {
22167 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
22168 if (VECTORP (XCDR (hot_spot)))
22169 {
22170 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
22171 Lisp_Object *poly = v->contents;
22172 int n = v->size;
22173 int i;
22174 int inside = 0;
22175 Lisp_Object lx, ly;
22176 int x0, y0;
22177
22178 /* Need an even number of coordinates, and at least 3 edges. */
22179 if (n < 6 || n & 1)
22180 return 0;
22181
22182 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
22183 If count is odd, we are inside polygon. Pixels on edges
22184 may or may not be included depending on actual geometry of the
22185 polygon. */
22186 if ((lx = poly[n-2], !INTEGERP (lx))
22187 || (ly = poly[n-1], !INTEGERP (lx)))
22188 return 0;
22189 x0 = XINT (lx), y0 = XINT (ly);
22190 for (i = 0; i < n; i += 2)
22191 {
22192 int x1 = x0, y1 = y0;
22193 if ((lx = poly[i], !INTEGERP (lx))
22194 || (ly = poly[i+1], !INTEGERP (ly)))
22195 return 0;
22196 x0 = XINT (lx), y0 = XINT (ly);
22197
22198 /* Does this segment cross the X line? */
22199 if (x0 >= x)
22200 {
22201 if (x1 >= x)
22202 continue;
22203 }
22204 else if (x1 < x)
22205 continue;
22206 if (y > y0 && y > y1)
22207 continue;
22208 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
22209 inside = !inside;
22210 }
22211 return inside;
22212 }
22213 }
22214 /* If we don't understand the format, pretend we're not in the hot-spot. */
22215 return 0;
22216 }
22217
22218 Lisp_Object
22219 find_hot_spot (map, x, y)
22220 Lisp_Object map;
22221 int x, y;
22222 {
22223 while (CONSP (map))
22224 {
22225 if (CONSP (XCAR (map))
22226 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
22227 return XCAR (map);
22228 map = XCDR (map);
22229 }
22230
22231 return Qnil;
22232 }
22233
22234 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
22235 3, 3, 0,
22236 doc: /* Lookup in image map MAP coordinates X and Y.
22237 An image map is an alist where each element has the format (AREA ID PLIST).
22238 An AREA is specified as either a rectangle, a circle, or a polygon:
22239 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
22240 pixel coordinates of the upper left and bottom right corners.
22241 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
22242 and the radius of the circle; r may be a float or integer.
22243 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
22244 vector describes one corner in the polygon.
22245 Returns the alist element for the first matching AREA in MAP. */)
22246 (map, x, y)
22247 Lisp_Object map;
22248 Lisp_Object x, y;
22249 {
22250 if (NILP (map))
22251 return Qnil;
22252
22253 CHECK_NUMBER (x);
22254 CHECK_NUMBER (y);
22255
22256 return find_hot_spot (map, XINT (x), XINT (y));
22257 }
22258
22259
22260 /* Display frame CURSOR, optionally using shape defined by POINTER. */
22261 static void
22262 define_frame_cursor1 (f, cursor, pointer)
22263 struct frame *f;
22264 Cursor cursor;
22265 Lisp_Object pointer;
22266 {
22267 /* Do not change cursor shape while dragging mouse. */
22268 if (!NILP (do_mouse_tracking))
22269 return;
22270
22271 if (!NILP (pointer))
22272 {
22273 if (EQ (pointer, Qarrow))
22274 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22275 else if (EQ (pointer, Qhand))
22276 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
22277 else if (EQ (pointer, Qtext))
22278 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22279 else if (EQ (pointer, intern ("hdrag")))
22280 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22281 #ifdef HAVE_X_WINDOWS
22282 else if (EQ (pointer, intern ("vdrag")))
22283 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
22284 #endif
22285 else if (EQ (pointer, intern ("hourglass")))
22286 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
22287 else if (EQ (pointer, Qmodeline))
22288 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
22289 else
22290 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22291 }
22292
22293 if (cursor != No_Cursor)
22294 rif->define_frame_cursor (f, cursor);
22295 }
22296
22297 /* Take proper action when mouse has moved to the mode or header line
22298 or marginal area AREA of window W, x-position X and y-position Y.
22299 X is relative to the start of the text display area of W, so the
22300 width of bitmap areas and scroll bars must be subtracted to get a
22301 position relative to the start of the mode line. */
22302
22303 static void
22304 note_mode_line_or_margin_highlight (window, x, y, area)
22305 Lisp_Object window;
22306 int x, y;
22307 enum window_part area;
22308 {
22309 struct window *w = XWINDOW (window);
22310 struct frame *f = XFRAME (w->frame);
22311 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22312 Cursor cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22313 Lisp_Object pointer = Qnil;
22314 int charpos, dx, dy, width, height;
22315 Lisp_Object string, object = Qnil;
22316 Lisp_Object pos, help;
22317
22318 Lisp_Object mouse_face;
22319 int original_x_pixel = x;
22320 struct glyph * glyph = NULL;
22321 struct glyph_row *row;
22322
22323 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
22324 {
22325 int x0;
22326 struct glyph *end;
22327
22328 string = mode_line_string (w, area, &x, &y, &charpos,
22329 &object, &dx, &dy, &width, &height);
22330
22331 row = (area == ON_MODE_LINE
22332 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
22333 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
22334
22335 /* Find glyph */
22336 if (row->mode_line_p && row->enabled_p)
22337 {
22338 glyph = row->glyphs[TEXT_AREA];
22339 end = glyph + row->used[TEXT_AREA];
22340
22341 for (x0 = original_x_pixel;
22342 glyph < end && x0 >= glyph->pixel_width;
22343 ++glyph)
22344 x0 -= glyph->pixel_width;
22345
22346 if (glyph >= end)
22347 glyph = NULL;
22348 }
22349 }
22350 else
22351 {
22352 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
22353 string = marginal_area_string (w, area, &x, &y, &charpos,
22354 &object, &dx, &dy, &width, &height);
22355 }
22356
22357 help = Qnil;
22358
22359 if (IMAGEP (object))
22360 {
22361 Lisp_Object image_map, hotspot;
22362 if ((image_map = Fplist_get (XCDR (object), QCmap),
22363 !NILP (image_map))
22364 && (hotspot = find_hot_spot (image_map, dx, dy),
22365 CONSP (hotspot))
22366 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22367 {
22368 Lisp_Object area_id, plist;
22369
22370 area_id = XCAR (hotspot);
22371 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22372 If so, we could look for mouse-enter, mouse-leave
22373 properties in PLIST (and do something...). */
22374 hotspot = XCDR (hotspot);
22375 if (CONSP (hotspot)
22376 && (plist = XCAR (hotspot), CONSP (plist)))
22377 {
22378 pointer = Fplist_get (plist, Qpointer);
22379 if (NILP (pointer))
22380 pointer = Qhand;
22381 help = Fplist_get (plist, Qhelp_echo);
22382 if (!NILP (help))
22383 {
22384 help_echo_string = help;
22385 /* Is this correct? ++kfs */
22386 XSETWINDOW (help_echo_window, w);
22387 help_echo_object = w->buffer;
22388 help_echo_pos = charpos;
22389 }
22390 }
22391 }
22392 if (NILP (pointer))
22393 pointer = Fplist_get (XCDR (object), QCpointer);
22394 }
22395
22396 if (STRINGP (string))
22397 {
22398 pos = make_number (charpos);
22399 /* If we're on a string with `help-echo' text property, arrange
22400 for the help to be displayed. This is done by setting the
22401 global variable help_echo_string to the help string. */
22402 if (NILP (help))
22403 {
22404 help = Fget_text_property (pos, Qhelp_echo, string);
22405 if (!NILP (help))
22406 {
22407 help_echo_string = help;
22408 XSETWINDOW (help_echo_window, w);
22409 help_echo_object = string;
22410 help_echo_pos = charpos;
22411 }
22412 }
22413
22414 if (NILP (pointer))
22415 pointer = Fget_text_property (pos, Qpointer, string);
22416
22417 /* Change the mouse pointer according to what is under X/Y. */
22418 if (NILP (pointer) && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
22419 {
22420 Lisp_Object map;
22421 map = Fget_text_property (pos, Qlocal_map, string);
22422 if (!KEYMAPP (map))
22423 map = Fget_text_property (pos, Qkeymap, string);
22424 if (!KEYMAPP (map))
22425 cursor = dpyinfo->vertical_scroll_bar_cursor;
22426 }
22427
22428 /* Change the mouse face according to what is under X/Y. */
22429 mouse_face = Fget_text_property (pos, Qmouse_face, string);
22430 if (!NILP (mouse_face)
22431 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22432 && glyph)
22433 {
22434 Lisp_Object b, e;
22435
22436 struct glyph * tmp_glyph;
22437
22438 int gpos;
22439 int gseq_length;
22440 int total_pixel_width;
22441 int ignore;
22442
22443 int vpos, hpos;
22444
22445 b = Fprevious_single_property_change (make_number (charpos + 1),
22446 Qmouse_face, string, Qnil);
22447 if (NILP (b))
22448 b = make_number (0);
22449
22450 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
22451 if (NILP (e))
22452 e = make_number (SCHARS (string));
22453
22454 /* Calculate the position(glyph position: GPOS) of GLYPH in
22455 displayed string. GPOS is different from CHARPOS.
22456
22457 CHARPOS is the position of glyph in internal string
22458 object. A mode line string format has structures which
22459 is converted to a flatten by emacs lisp interpreter.
22460 The internal string is an element of the structures.
22461 The displayed string is the flatten string. */
22462 for (tmp_glyph = glyph - 1, gpos = 0;
22463 tmp_glyph->charpos >= XINT (b);
22464 tmp_glyph--, gpos++)
22465 {
22466 if (!EQ (tmp_glyph->object, glyph->object))
22467 break;
22468 }
22469
22470 /* Calculate the lenght(glyph sequence length: GSEQ_LENGTH) of
22471 displayed string holding GLYPH.
22472
22473 GSEQ_LENGTH is different from SCHARS (STRING).
22474 SCHARS (STRING) returns the length of the internal string. */
22475 for (tmp_glyph = glyph, gseq_length = gpos;
22476 tmp_glyph->charpos < XINT (e);
22477 tmp_glyph++, gseq_length++)
22478 {
22479 if (!EQ (tmp_glyph->object, glyph->object))
22480 break;
22481 }
22482
22483 total_pixel_width = 0;
22484 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
22485 total_pixel_width += tmp_glyph->pixel_width;
22486
22487 /* Pre calculation of re-rendering position */
22488 vpos = (x - gpos);
22489 hpos = (area == ON_MODE_LINE
22490 ? (w->current_matrix)->nrows - 1
22491 : 0);
22492
22493 /* If the re-rendering position is included in the last
22494 re-rendering area, we should do nothing. */
22495 if ( EQ (window, dpyinfo->mouse_face_window)
22496 && dpyinfo->mouse_face_beg_col <= vpos
22497 && vpos < dpyinfo->mouse_face_end_col
22498 && dpyinfo->mouse_face_beg_row == hpos )
22499 return;
22500
22501 if (clear_mouse_face (dpyinfo))
22502 cursor = No_Cursor;
22503
22504 dpyinfo->mouse_face_beg_col = vpos;
22505 dpyinfo->mouse_face_beg_row = hpos;
22506
22507 dpyinfo->mouse_face_beg_x = original_x_pixel - (total_pixel_width + dx);
22508 dpyinfo->mouse_face_beg_y = 0;
22509
22510 dpyinfo->mouse_face_end_col = vpos + gseq_length;
22511 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_beg_row;
22512
22513 dpyinfo->mouse_face_end_x = 0;
22514 dpyinfo->mouse_face_end_y = 0;
22515
22516 dpyinfo->mouse_face_past_end = 0;
22517 dpyinfo->mouse_face_window = window;
22518
22519 dpyinfo->mouse_face_face_id = face_at_string_position (w, string,
22520 charpos,
22521 0, 0, 0, &ignore,
22522 glyph->face_id, 1);
22523 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22524
22525 if (NILP (pointer))
22526 pointer = Qhand;
22527 }
22528 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
22529 clear_mouse_face (dpyinfo);
22530 }
22531 define_frame_cursor1 (f, cursor, pointer);
22532 }
22533
22534
22535 /* EXPORT:
22536 Take proper action when the mouse has moved to position X, Y on
22537 frame F as regards highlighting characters that have mouse-face
22538 properties. Also de-highlighting chars where the mouse was before.
22539 X and Y can be negative or out of range. */
22540
22541 void
22542 note_mouse_highlight (f, x, y)
22543 struct frame *f;
22544 int x, y;
22545 {
22546 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
22547 enum window_part part;
22548 Lisp_Object window;
22549 struct window *w;
22550 Cursor cursor = No_Cursor;
22551 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
22552 struct buffer *b;
22553
22554 /* When a menu is active, don't highlight because this looks odd. */
22555 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NTGUI)
22556 if (popup_activated ())
22557 return;
22558 #endif
22559
22560 if (NILP (Vmouse_highlight)
22561 || !f->glyphs_initialized_p)
22562 return;
22563
22564 dpyinfo->mouse_face_mouse_x = x;
22565 dpyinfo->mouse_face_mouse_y = y;
22566 dpyinfo->mouse_face_mouse_frame = f;
22567
22568 if (dpyinfo->mouse_face_defer)
22569 return;
22570
22571 if (gc_in_progress)
22572 {
22573 dpyinfo->mouse_face_deferred_gc = 1;
22574 return;
22575 }
22576
22577 /* Which window is that in? */
22578 window = window_from_coordinates (f, x, y, &part, 0, 0, 1);
22579
22580 /* If we were displaying active text in another window, clear that.
22581 Also clear if we move out of text area in same window. */
22582 if (! EQ (window, dpyinfo->mouse_face_window)
22583 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
22584 && !NILP (dpyinfo->mouse_face_window)))
22585 clear_mouse_face (dpyinfo);
22586
22587 /* Not on a window -> return. */
22588 if (!WINDOWP (window))
22589 return;
22590
22591 /* Reset help_echo_string. It will get recomputed below. */
22592 help_echo_string = Qnil;
22593
22594 /* Convert to window-relative pixel coordinates. */
22595 w = XWINDOW (window);
22596 frame_to_window_pixel_xy (w, &x, &y);
22597
22598 /* Handle tool-bar window differently since it doesn't display a
22599 buffer. */
22600 if (EQ (window, f->tool_bar_window))
22601 {
22602 note_tool_bar_highlight (f, x, y);
22603 return;
22604 }
22605
22606 /* Mouse is on the mode, header line or margin? */
22607 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
22608 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
22609 {
22610 note_mode_line_or_margin_highlight (window, x, y, part);
22611 return;
22612 }
22613
22614 if (part == ON_VERTICAL_BORDER)
22615 {
22616 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
22617 help_echo_string = build_string ("drag-mouse-1: resize");
22618 }
22619 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
22620 || part == ON_SCROLL_BAR)
22621 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22622 else
22623 cursor = FRAME_X_OUTPUT (f)->text_cursor;
22624
22625 /* Are we in a window whose display is up to date?
22626 And verify the buffer's text has not changed. */
22627 b = XBUFFER (w->buffer);
22628 if (part == ON_TEXT
22629 && EQ (w->window_end_valid, w->buffer)
22630 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
22631 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
22632 {
22633 int hpos, vpos, pos, i, dx, dy, area;
22634 struct glyph *glyph;
22635 Lisp_Object object;
22636 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
22637 Lisp_Object *overlay_vec = NULL;
22638 int noverlays;
22639 struct buffer *obuf;
22640 int obegv, ozv, same_region;
22641
22642 /* Find the glyph under X/Y. */
22643 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
22644
22645 /* Look for :pointer property on image. */
22646 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
22647 {
22648 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
22649 if (img != NULL && IMAGEP (img->spec))
22650 {
22651 Lisp_Object image_map, hotspot;
22652 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
22653 !NILP (image_map))
22654 && (hotspot = find_hot_spot (image_map,
22655 glyph->slice.x + dx,
22656 glyph->slice.y + dy),
22657 CONSP (hotspot))
22658 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
22659 {
22660 Lisp_Object area_id, plist;
22661
22662 area_id = XCAR (hotspot);
22663 /* Could check AREA_ID to see if we enter/leave this hot-spot.
22664 If so, we could look for mouse-enter, mouse-leave
22665 properties in PLIST (and do something...). */
22666 hotspot = XCDR (hotspot);
22667 if (CONSP (hotspot)
22668 && (plist = XCAR (hotspot), CONSP (plist)))
22669 {
22670 pointer = Fplist_get (plist, Qpointer);
22671 if (NILP (pointer))
22672 pointer = Qhand;
22673 help_echo_string = Fplist_get (plist, Qhelp_echo);
22674 if (!NILP (help_echo_string))
22675 {
22676 help_echo_window = window;
22677 help_echo_object = glyph->object;
22678 help_echo_pos = glyph->charpos;
22679 }
22680 }
22681 }
22682 if (NILP (pointer))
22683 pointer = Fplist_get (XCDR (img->spec), QCpointer);
22684 }
22685 }
22686
22687 /* Clear mouse face if X/Y not over text. */
22688 if (glyph == NULL
22689 || area != TEXT_AREA
22690 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
22691 {
22692 if (clear_mouse_face (dpyinfo))
22693 cursor = No_Cursor;
22694 if (NILP (pointer))
22695 {
22696 if (area != TEXT_AREA)
22697 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
22698 else
22699 pointer = Vvoid_text_area_pointer;
22700 }
22701 goto set_cursor;
22702 }
22703
22704 pos = glyph->charpos;
22705 object = glyph->object;
22706 if (!STRINGP (object) && !BUFFERP (object))
22707 goto set_cursor;
22708
22709 /* If we get an out-of-range value, return now; avoid an error. */
22710 if (BUFFERP (object) && pos > BUF_Z (b))
22711 goto set_cursor;
22712
22713 /* Make the window's buffer temporarily current for
22714 overlays_at and compute_char_face. */
22715 obuf = current_buffer;
22716 current_buffer = b;
22717 obegv = BEGV;
22718 ozv = ZV;
22719 BEGV = BEG;
22720 ZV = Z;
22721
22722 /* Is this char mouse-active or does it have help-echo? */
22723 position = make_number (pos);
22724
22725 if (BUFFERP (object))
22726 {
22727 /* Put all the overlays we want in a vector in overlay_vec. */
22728 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
22729 /* Sort overlays into increasing priority order. */
22730 noverlays = sort_overlays (overlay_vec, noverlays, w);
22731 }
22732 else
22733 noverlays = 0;
22734
22735 same_region = (EQ (window, dpyinfo->mouse_face_window)
22736 && vpos >= dpyinfo->mouse_face_beg_row
22737 && vpos <= dpyinfo->mouse_face_end_row
22738 && (vpos > dpyinfo->mouse_face_beg_row
22739 || hpos >= dpyinfo->mouse_face_beg_col)
22740 && (vpos < dpyinfo->mouse_face_end_row
22741 || hpos < dpyinfo->mouse_face_end_col
22742 || dpyinfo->mouse_face_past_end));
22743
22744 if (same_region)
22745 cursor = No_Cursor;
22746
22747 /* Check mouse-face highlighting. */
22748 if (! same_region
22749 /* If there exists an overlay with mouse-face overlapping
22750 the one we are currently highlighting, we have to
22751 check if we enter the overlapping overlay, and then
22752 highlight only that. */
22753 || (OVERLAYP (dpyinfo->mouse_face_overlay)
22754 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
22755 {
22756 /* Find the highest priority overlay that has a mouse-face
22757 property. */
22758 overlay = Qnil;
22759 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
22760 {
22761 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
22762 if (!NILP (mouse_face))
22763 overlay = overlay_vec[i];
22764 }
22765
22766 /* If we're actually highlighting the same overlay as
22767 before, there's no need to do that again. */
22768 if (!NILP (overlay)
22769 && EQ (overlay, dpyinfo->mouse_face_overlay))
22770 goto check_help_echo;
22771
22772 dpyinfo->mouse_face_overlay = overlay;
22773
22774 /* Clear the display of the old active region, if any. */
22775 if (clear_mouse_face (dpyinfo))
22776 cursor = No_Cursor;
22777
22778 /* If no overlay applies, get a text property. */
22779 if (NILP (overlay))
22780 mouse_face = Fget_text_property (position, Qmouse_face, object);
22781
22782 /* Handle the overlay case. */
22783 if (!NILP (overlay))
22784 {
22785 /* Find the range of text around this char that
22786 should be active. */
22787 Lisp_Object before, after;
22788 int ignore;
22789
22790 before = Foverlay_start (overlay);
22791 after = Foverlay_end (overlay);
22792 /* Record this as the current active region. */
22793 fast_find_position (w, XFASTINT (before),
22794 &dpyinfo->mouse_face_beg_col,
22795 &dpyinfo->mouse_face_beg_row,
22796 &dpyinfo->mouse_face_beg_x,
22797 &dpyinfo->mouse_face_beg_y, Qnil);
22798
22799 dpyinfo->mouse_face_past_end
22800 = !fast_find_position (w, XFASTINT (after),
22801 &dpyinfo->mouse_face_end_col,
22802 &dpyinfo->mouse_face_end_row,
22803 &dpyinfo->mouse_face_end_x,
22804 &dpyinfo->mouse_face_end_y, Qnil);
22805 dpyinfo->mouse_face_window = window;
22806
22807 dpyinfo->mouse_face_face_id
22808 = face_at_buffer_position (w, pos, 0, 0,
22809 &ignore, pos + 1,
22810 !dpyinfo->mouse_face_hidden);
22811
22812 /* Display it as active. */
22813 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22814 cursor = No_Cursor;
22815 }
22816 /* Handle the text property case. */
22817 else if (!NILP (mouse_face) && BUFFERP (object))
22818 {
22819 /* Find the range of text around this char that
22820 should be active. */
22821 Lisp_Object before, after, beginning, end;
22822 int ignore;
22823
22824 beginning = Fmarker_position (w->start);
22825 end = make_number (BUF_Z (XBUFFER (object))
22826 - XFASTINT (w->window_end_pos));
22827 before
22828 = Fprevious_single_property_change (make_number (pos + 1),
22829 Qmouse_face,
22830 object, beginning);
22831 after
22832 = Fnext_single_property_change (position, Qmouse_face,
22833 object, end);
22834
22835 /* Record this as the current active region. */
22836 fast_find_position (w, XFASTINT (before),
22837 &dpyinfo->mouse_face_beg_col,
22838 &dpyinfo->mouse_face_beg_row,
22839 &dpyinfo->mouse_face_beg_x,
22840 &dpyinfo->mouse_face_beg_y, Qnil);
22841 dpyinfo->mouse_face_past_end
22842 = !fast_find_position (w, XFASTINT (after),
22843 &dpyinfo->mouse_face_end_col,
22844 &dpyinfo->mouse_face_end_row,
22845 &dpyinfo->mouse_face_end_x,
22846 &dpyinfo->mouse_face_end_y, Qnil);
22847 dpyinfo->mouse_face_window = window;
22848
22849 if (BUFFERP (object))
22850 dpyinfo->mouse_face_face_id
22851 = face_at_buffer_position (w, pos, 0, 0,
22852 &ignore, pos + 1,
22853 !dpyinfo->mouse_face_hidden);
22854
22855 /* Display it as active. */
22856 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22857 cursor = No_Cursor;
22858 }
22859 else if (!NILP (mouse_face) && STRINGP (object))
22860 {
22861 Lisp_Object b, e;
22862 int ignore;
22863
22864 b = Fprevious_single_property_change (make_number (pos + 1),
22865 Qmouse_face,
22866 object, Qnil);
22867 e = Fnext_single_property_change (position, Qmouse_face,
22868 object, Qnil);
22869 if (NILP (b))
22870 b = make_number (0);
22871 if (NILP (e))
22872 e = make_number (SCHARS (object) - 1);
22873
22874 fast_find_string_pos (w, XINT (b), object,
22875 &dpyinfo->mouse_face_beg_col,
22876 &dpyinfo->mouse_face_beg_row,
22877 &dpyinfo->mouse_face_beg_x,
22878 &dpyinfo->mouse_face_beg_y, 0);
22879 fast_find_string_pos (w, XINT (e), object,
22880 &dpyinfo->mouse_face_end_col,
22881 &dpyinfo->mouse_face_end_row,
22882 &dpyinfo->mouse_face_end_x,
22883 &dpyinfo->mouse_face_end_y, 1);
22884 dpyinfo->mouse_face_past_end = 0;
22885 dpyinfo->mouse_face_window = window;
22886 dpyinfo->mouse_face_face_id
22887 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
22888 glyph->face_id, 1);
22889 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22890 cursor = No_Cursor;
22891 }
22892 else if (STRINGP (object) && NILP (mouse_face))
22893 {
22894 /* A string which doesn't have mouse-face, but
22895 the text ``under'' it might have. */
22896 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
22897 int start = MATRIX_ROW_START_CHARPOS (r);
22898
22899 pos = string_buffer_position (w, object, start);
22900 if (pos > 0)
22901 mouse_face = get_char_property_and_overlay (make_number (pos),
22902 Qmouse_face,
22903 w->buffer,
22904 &overlay);
22905 if (!NILP (mouse_face) && !NILP (overlay))
22906 {
22907 Lisp_Object before = Foverlay_start (overlay);
22908 Lisp_Object after = Foverlay_end (overlay);
22909 int ignore;
22910
22911 /* Note that we might not be able to find position
22912 BEFORE in the glyph matrix if the overlay is
22913 entirely covered by a `display' property. In
22914 this case, we overshoot. So let's stop in
22915 the glyph matrix before glyphs for OBJECT. */
22916 fast_find_position (w, XFASTINT (before),
22917 &dpyinfo->mouse_face_beg_col,
22918 &dpyinfo->mouse_face_beg_row,
22919 &dpyinfo->mouse_face_beg_x,
22920 &dpyinfo->mouse_face_beg_y,
22921 object);
22922
22923 dpyinfo->mouse_face_past_end
22924 = !fast_find_position (w, XFASTINT (after),
22925 &dpyinfo->mouse_face_end_col,
22926 &dpyinfo->mouse_face_end_row,
22927 &dpyinfo->mouse_face_end_x,
22928 &dpyinfo->mouse_face_end_y,
22929 Qnil);
22930 dpyinfo->mouse_face_window = window;
22931 dpyinfo->mouse_face_face_id
22932 = face_at_buffer_position (w, pos, 0, 0,
22933 &ignore, pos + 1,
22934 !dpyinfo->mouse_face_hidden);
22935
22936 /* Display it as active. */
22937 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
22938 cursor = No_Cursor;
22939 }
22940 }
22941 }
22942
22943 check_help_echo:
22944
22945 /* Look for a `help-echo' property. */
22946 if (NILP (help_echo_string)) {
22947 Lisp_Object help, overlay;
22948
22949 /* Check overlays first. */
22950 help = overlay = Qnil;
22951 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
22952 {
22953 overlay = overlay_vec[i];
22954 help = Foverlay_get (overlay, Qhelp_echo);
22955 }
22956
22957 if (!NILP (help))
22958 {
22959 help_echo_string = help;
22960 help_echo_window = window;
22961 help_echo_object = overlay;
22962 help_echo_pos = pos;
22963 }
22964 else
22965 {
22966 Lisp_Object object = glyph->object;
22967 int charpos = glyph->charpos;
22968
22969 /* Try text properties. */
22970 if (STRINGP (object)
22971 && charpos >= 0
22972 && charpos < SCHARS (object))
22973 {
22974 help = Fget_text_property (make_number (charpos),
22975 Qhelp_echo, object);
22976 if (NILP (help))
22977 {
22978 /* If the string itself doesn't specify a help-echo,
22979 see if the buffer text ``under'' it does. */
22980 struct glyph_row *r
22981 = MATRIX_ROW (w->current_matrix, vpos);
22982 int start = MATRIX_ROW_START_CHARPOS (r);
22983 int pos = string_buffer_position (w, object, start);
22984 if (pos > 0)
22985 {
22986 help = Fget_char_property (make_number (pos),
22987 Qhelp_echo, w->buffer);
22988 if (!NILP (help))
22989 {
22990 charpos = pos;
22991 object = w->buffer;
22992 }
22993 }
22994 }
22995 }
22996 else if (BUFFERP (object)
22997 && charpos >= BEGV
22998 && charpos < ZV)
22999 help = Fget_text_property (make_number (charpos), Qhelp_echo,
23000 object);
23001
23002 if (!NILP (help))
23003 {
23004 help_echo_string = help;
23005 help_echo_window = window;
23006 help_echo_object = object;
23007 help_echo_pos = charpos;
23008 }
23009 }
23010 }
23011
23012 /* Look for a `pointer' property. */
23013 if (NILP (pointer))
23014 {
23015 /* Check overlays first. */
23016 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
23017 pointer = Foverlay_get (overlay_vec[i], Qpointer);
23018
23019 if (NILP (pointer))
23020 {
23021 Lisp_Object object = glyph->object;
23022 int charpos = glyph->charpos;
23023
23024 /* Try text properties. */
23025 if (STRINGP (object)
23026 && charpos >= 0
23027 && charpos < SCHARS (object))
23028 {
23029 pointer = Fget_text_property (make_number (charpos),
23030 Qpointer, object);
23031 if (NILP (pointer))
23032 {
23033 /* If the string itself doesn't specify a pointer,
23034 see if the buffer text ``under'' it does. */
23035 struct glyph_row *r
23036 = MATRIX_ROW (w->current_matrix, vpos);
23037 int start = MATRIX_ROW_START_CHARPOS (r);
23038 int pos = string_buffer_position (w, object, start);
23039 if (pos > 0)
23040 pointer = Fget_char_property (make_number (pos),
23041 Qpointer, w->buffer);
23042 }
23043 }
23044 else if (BUFFERP (object)
23045 && charpos >= BEGV
23046 && charpos < ZV)
23047 pointer = Fget_text_property (make_number (charpos),
23048 Qpointer, object);
23049 }
23050 }
23051
23052 BEGV = obegv;
23053 ZV = ozv;
23054 current_buffer = obuf;
23055 }
23056
23057 set_cursor:
23058
23059 define_frame_cursor1 (f, cursor, pointer);
23060 }
23061
23062
23063 /* EXPORT for RIF:
23064 Clear any mouse-face on window W. This function is part of the
23065 redisplay interface, and is called from try_window_id and similar
23066 functions to ensure the mouse-highlight is off. */
23067
23068 void
23069 x_clear_window_mouse_face (w)
23070 struct window *w;
23071 {
23072 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
23073 Lisp_Object window;
23074
23075 BLOCK_INPUT;
23076 XSETWINDOW (window, w);
23077 if (EQ (window, dpyinfo->mouse_face_window))
23078 clear_mouse_face (dpyinfo);
23079 UNBLOCK_INPUT;
23080 }
23081
23082
23083 /* EXPORT:
23084 Just discard the mouse face information for frame F, if any.
23085 This is used when the size of F is changed. */
23086
23087 void
23088 cancel_mouse_face (f)
23089 struct frame *f;
23090 {
23091 Lisp_Object window;
23092 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23093
23094 window = dpyinfo->mouse_face_window;
23095 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
23096 {
23097 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
23098 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
23099 dpyinfo->mouse_face_window = Qnil;
23100 }
23101 }
23102
23103
23104 #endif /* HAVE_WINDOW_SYSTEM */
23105
23106 \f
23107 /***********************************************************************
23108 Exposure Events
23109 ***********************************************************************/
23110
23111 #ifdef HAVE_WINDOW_SYSTEM
23112
23113 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
23114 which intersects rectangle R. R is in window-relative coordinates. */
23115
23116 static void
23117 expose_area (w, row, r, area)
23118 struct window *w;
23119 struct glyph_row *row;
23120 XRectangle *r;
23121 enum glyph_row_area area;
23122 {
23123 struct glyph *first = row->glyphs[area];
23124 struct glyph *end = row->glyphs[area] + row->used[area];
23125 struct glyph *last;
23126 int first_x, start_x, x;
23127
23128 if (area == TEXT_AREA && row->fill_line_p)
23129 /* If row extends face to end of line write the whole line. */
23130 draw_glyphs (w, 0, row, area,
23131 0, row->used[area],
23132 DRAW_NORMAL_TEXT, 0);
23133 else
23134 {
23135 /* Set START_X to the window-relative start position for drawing glyphs of
23136 AREA. The first glyph of the text area can be partially visible.
23137 The first glyphs of other areas cannot. */
23138 start_x = window_box_left_offset (w, area);
23139 x = start_x;
23140 if (area == TEXT_AREA)
23141 x += row->x;
23142
23143 /* Find the first glyph that must be redrawn. */
23144 while (first < end
23145 && x + first->pixel_width < r->x)
23146 {
23147 x += first->pixel_width;
23148 ++first;
23149 }
23150
23151 /* Find the last one. */
23152 last = first;
23153 first_x = x;
23154 while (last < end
23155 && x < r->x + r->width)
23156 {
23157 x += last->pixel_width;
23158 ++last;
23159 }
23160
23161 /* Repaint. */
23162 if (last > first)
23163 draw_glyphs (w, first_x - start_x, row, area,
23164 first - row->glyphs[area], last - row->glyphs[area],
23165 DRAW_NORMAL_TEXT, 0);
23166 }
23167 }
23168
23169
23170 /* Redraw the parts of the glyph row ROW on window W intersecting
23171 rectangle R. R is in window-relative coordinates. Value is
23172 non-zero if mouse-face was overwritten. */
23173
23174 static int
23175 expose_line (w, row, r)
23176 struct window *w;
23177 struct glyph_row *row;
23178 XRectangle *r;
23179 {
23180 xassert (row->enabled_p);
23181
23182 if (row->mode_line_p || w->pseudo_window_p)
23183 draw_glyphs (w, 0, row, TEXT_AREA,
23184 0, row->used[TEXT_AREA],
23185 DRAW_NORMAL_TEXT, 0);
23186 else
23187 {
23188 if (row->used[LEFT_MARGIN_AREA])
23189 expose_area (w, row, r, LEFT_MARGIN_AREA);
23190 if (row->used[TEXT_AREA])
23191 expose_area (w, row, r, TEXT_AREA);
23192 if (row->used[RIGHT_MARGIN_AREA])
23193 expose_area (w, row, r, RIGHT_MARGIN_AREA);
23194 draw_row_fringe_bitmaps (w, row);
23195 }
23196
23197 return row->mouse_face_p;
23198 }
23199
23200
23201 /* Redraw those parts of glyphs rows during expose event handling that
23202 overlap other rows. Redrawing of an exposed line writes over parts
23203 of lines overlapping that exposed line; this function fixes that.
23204
23205 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
23206 row in W's current matrix that is exposed and overlaps other rows.
23207 LAST_OVERLAPPING_ROW is the last such row. */
23208
23209 static void
23210 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
23211 struct window *w;
23212 struct glyph_row *first_overlapping_row;
23213 struct glyph_row *last_overlapping_row;
23214 {
23215 struct glyph_row *row;
23216
23217 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
23218 if (row->overlapping_p)
23219 {
23220 xassert (row->enabled_p && !row->mode_line_p);
23221
23222 if (row->used[LEFT_MARGIN_AREA])
23223 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
23224
23225 if (row->used[TEXT_AREA])
23226 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
23227
23228 if (row->used[RIGHT_MARGIN_AREA])
23229 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
23230 }
23231 }
23232
23233
23234 /* Return non-zero if W's cursor intersects rectangle R. */
23235
23236 static int
23237 phys_cursor_in_rect_p (w, r)
23238 struct window *w;
23239 XRectangle *r;
23240 {
23241 XRectangle cr, result;
23242 struct glyph *cursor_glyph;
23243
23244 cursor_glyph = get_phys_cursor_glyph (w);
23245 if (cursor_glyph)
23246 {
23247 /* r is relative to W's box, but w->phys_cursor.x is relative
23248 to left edge of W's TEXT area. Adjust it. */
23249 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
23250 cr.y = w->phys_cursor.y;
23251 cr.width = cursor_glyph->pixel_width;
23252 cr.height = w->phys_cursor_height;
23253 /* ++KFS: W32 version used W32-specific IntersectRect here, but
23254 I assume the effect is the same -- and this is portable. */
23255 return x_intersect_rectangles (&cr, r, &result);
23256 }
23257 else
23258 return 0;
23259 }
23260
23261
23262 /* EXPORT:
23263 Draw a vertical window border to the right of window W if W doesn't
23264 have vertical scroll bars. */
23265
23266 void
23267 x_draw_vertical_border (w)
23268 struct window *w;
23269 {
23270 /* We could do better, if we knew what type of scroll-bar the adjacent
23271 windows (on either side) have... But we don't :-(
23272 However, I think this works ok. ++KFS 2003-04-25 */
23273
23274 /* Redraw borders between horizontally adjacent windows. Don't
23275 do it for frames with vertical scroll bars because either the
23276 right scroll bar of a window, or the left scroll bar of its
23277 neighbor will suffice as a border. */
23278 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
23279 return;
23280
23281 if (!WINDOW_RIGHTMOST_P (w)
23282 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
23283 {
23284 int x0, x1, y0, y1;
23285
23286 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23287 y1 -= 1;
23288
23289 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23290 x1 -= 1;
23291
23292 rif->draw_vertical_window_border (w, x1, y0, y1);
23293 }
23294 else if (!WINDOW_LEFTMOST_P (w)
23295 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
23296 {
23297 int x0, x1, y0, y1;
23298
23299 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
23300 y1 -= 1;
23301
23302 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
23303 x0 -= 1;
23304
23305 rif->draw_vertical_window_border (w, x0, y0, y1);
23306 }
23307 }
23308
23309
23310 /* Redraw the part of window W intersection rectangle FR. Pixel
23311 coordinates in FR are frame-relative. Call this function with
23312 input blocked. Value is non-zero if the exposure overwrites
23313 mouse-face. */
23314
23315 static int
23316 expose_window (w, fr)
23317 struct window *w;
23318 XRectangle *fr;
23319 {
23320 struct frame *f = XFRAME (w->frame);
23321 XRectangle wr, r;
23322 int mouse_face_overwritten_p = 0;
23323
23324 /* If window is not yet fully initialized, do nothing. This can
23325 happen when toolkit scroll bars are used and a window is split.
23326 Reconfiguring the scroll bar will generate an expose for a newly
23327 created window. */
23328 if (w->current_matrix == NULL)
23329 return 0;
23330
23331 /* When we're currently updating the window, display and current
23332 matrix usually don't agree. Arrange for a thorough display
23333 later. */
23334 if (w == updated_window)
23335 {
23336 SET_FRAME_GARBAGED (f);
23337 return 0;
23338 }
23339
23340 /* Frame-relative pixel rectangle of W. */
23341 wr.x = WINDOW_LEFT_EDGE_X (w);
23342 wr.y = WINDOW_TOP_EDGE_Y (w);
23343 wr.width = WINDOW_TOTAL_WIDTH (w);
23344 wr.height = WINDOW_TOTAL_HEIGHT (w);
23345
23346 if (x_intersect_rectangles (fr, &wr, &r))
23347 {
23348 int yb = window_text_bottom_y (w);
23349 struct glyph_row *row;
23350 int cursor_cleared_p;
23351 struct glyph_row *first_overlapping_row, *last_overlapping_row;
23352
23353 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
23354 r.x, r.y, r.width, r.height));
23355
23356 /* Convert to window coordinates. */
23357 r.x -= WINDOW_LEFT_EDGE_X (w);
23358 r.y -= WINDOW_TOP_EDGE_Y (w);
23359
23360 /* Turn off the cursor. */
23361 if (!w->pseudo_window_p
23362 && phys_cursor_in_rect_p (w, &r))
23363 {
23364 x_clear_cursor (w);
23365 cursor_cleared_p = 1;
23366 }
23367 else
23368 cursor_cleared_p = 0;
23369
23370 /* Update lines intersecting rectangle R. */
23371 first_overlapping_row = last_overlapping_row = NULL;
23372 for (row = w->current_matrix->rows;
23373 row->enabled_p;
23374 ++row)
23375 {
23376 int y0 = row->y;
23377 int y1 = MATRIX_ROW_BOTTOM_Y (row);
23378
23379 if ((y0 >= r.y && y0 < r.y + r.height)
23380 || (y1 > r.y && y1 < r.y + r.height)
23381 || (r.y >= y0 && r.y < y1)
23382 || (r.y + r.height > y0 && r.y + r.height < y1))
23383 {
23384 /* A header line may be overlapping, but there is no need
23385 to fix overlapping areas for them. KFS 2005-02-12 */
23386 if (row->overlapping_p && !row->mode_line_p)
23387 {
23388 if (first_overlapping_row == NULL)
23389 first_overlapping_row = row;
23390 last_overlapping_row = row;
23391 }
23392
23393 if (expose_line (w, row, &r))
23394 mouse_face_overwritten_p = 1;
23395 }
23396
23397 if (y1 >= yb)
23398 break;
23399 }
23400
23401 /* Display the mode line if there is one. */
23402 if (WINDOW_WANTS_MODELINE_P (w)
23403 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
23404 row->enabled_p)
23405 && row->y < r.y + r.height)
23406 {
23407 if (expose_line (w, row, &r))
23408 mouse_face_overwritten_p = 1;
23409 }
23410
23411 if (!w->pseudo_window_p)
23412 {
23413 /* Fix the display of overlapping rows. */
23414 if (first_overlapping_row)
23415 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
23416
23417 /* Draw border between windows. */
23418 x_draw_vertical_border (w);
23419
23420 /* Turn the cursor on again. */
23421 if (cursor_cleared_p)
23422 update_window_cursor (w, 1);
23423 }
23424 }
23425
23426 return mouse_face_overwritten_p;
23427 }
23428
23429
23430
23431 /* Redraw (parts) of all windows in the window tree rooted at W that
23432 intersect R. R contains frame pixel coordinates. Value is
23433 non-zero if the exposure overwrites mouse-face. */
23434
23435 static int
23436 expose_window_tree (w, r)
23437 struct window *w;
23438 XRectangle *r;
23439 {
23440 struct frame *f = XFRAME (w->frame);
23441 int mouse_face_overwritten_p = 0;
23442
23443 while (w && !FRAME_GARBAGED_P (f))
23444 {
23445 if (!NILP (w->hchild))
23446 mouse_face_overwritten_p
23447 |= expose_window_tree (XWINDOW (w->hchild), r);
23448 else if (!NILP (w->vchild))
23449 mouse_face_overwritten_p
23450 |= expose_window_tree (XWINDOW (w->vchild), r);
23451 else
23452 mouse_face_overwritten_p |= expose_window (w, r);
23453
23454 w = NILP (w->next) ? NULL : XWINDOW (w->next);
23455 }
23456
23457 return mouse_face_overwritten_p;
23458 }
23459
23460
23461 /* EXPORT:
23462 Redisplay an exposed area of frame F. X and Y are the upper-left
23463 corner of the exposed rectangle. W and H are width and height of
23464 the exposed area. All are pixel values. W or H zero means redraw
23465 the entire frame. */
23466
23467 void
23468 expose_frame (f, x, y, w, h)
23469 struct frame *f;
23470 int x, y, w, h;
23471 {
23472 XRectangle r;
23473 int mouse_face_overwritten_p = 0;
23474
23475 TRACE ((stderr, "expose_frame "));
23476
23477 /* No need to redraw if frame will be redrawn soon. */
23478 if (FRAME_GARBAGED_P (f))
23479 {
23480 TRACE ((stderr, " garbaged\n"));
23481 return;
23482 }
23483
23484 /* If basic faces haven't been realized yet, there is no point in
23485 trying to redraw anything. This can happen when we get an expose
23486 event while Emacs is starting, e.g. by moving another window. */
23487 if (FRAME_FACE_CACHE (f) == NULL
23488 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
23489 {
23490 TRACE ((stderr, " no faces\n"));
23491 return;
23492 }
23493
23494 if (w == 0 || h == 0)
23495 {
23496 r.x = r.y = 0;
23497 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
23498 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
23499 }
23500 else
23501 {
23502 r.x = x;
23503 r.y = y;
23504 r.width = w;
23505 r.height = h;
23506 }
23507
23508 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
23509 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
23510
23511 if (WINDOWP (f->tool_bar_window))
23512 mouse_face_overwritten_p
23513 |= expose_window (XWINDOW (f->tool_bar_window), &r);
23514
23515 #ifdef HAVE_X_WINDOWS
23516 #ifndef MSDOS
23517 #ifndef USE_X_TOOLKIT
23518 if (WINDOWP (f->menu_bar_window))
23519 mouse_face_overwritten_p
23520 |= expose_window (XWINDOW (f->menu_bar_window), &r);
23521 #endif /* not USE_X_TOOLKIT */
23522 #endif
23523 #endif
23524
23525 /* Some window managers support a focus-follows-mouse style with
23526 delayed raising of frames. Imagine a partially obscured frame,
23527 and moving the mouse into partially obscured mouse-face on that
23528 frame. The visible part of the mouse-face will be highlighted,
23529 then the WM raises the obscured frame. With at least one WM, KDE
23530 2.1, Emacs is not getting any event for the raising of the frame
23531 (even tried with SubstructureRedirectMask), only Expose events.
23532 These expose events will draw text normally, i.e. not
23533 highlighted. Which means we must redo the highlight here.
23534 Subsume it under ``we love X''. --gerd 2001-08-15 */
23535 /* Included in Windows version because Windows most likely does not
23536 do the right thing if any third party tool offers
23537 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
23538 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
23539 {
23540 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
23541 if (f == dpyinfo->mouse_face_mouse_frame)
23542 {
23543 int x = dpyinfo->mouse_face_mouse_x;
23544 int y = dpyinfo->mouse_face_mouse_y;
23545 clear_mouse_face (dpyinfo);
23546 note_mouse_highlight (f, x, y);
23547 }
23548 }
23549 }
23550
23551
23552 /* EXPORT:
23553 Determine the intersection of two rectangles R1 and R2. Return
23554 the intersection in *RESULT. Value is non-zero if RESULT is not
23555 empty. */
23556
23557 int
23558 x_intersect_rectangles (r1, r2, result)
23559 XRectangle *r1, *r2, *result;
23560 {
23561 XRectangle *left, *right;
23562 XRectangle *upper, *lower;
23563 int intersection_p = 0;
23564
23565 /* Rearrange so that R1 is the left-most rectangle. */
23566 if (r1->x < r2->x)
23567 left = r1, right = r2;
23568 else
23569 left = r2, right = r1;
23570
23571 /* X0 of the intersection is right.x0, if this is inside R1,
23572 otherwise there is no intersection. */
23573 if (right->x <= left->x + left->width)
23574 {
23575 result->x = right->x;
23576
23577 /* The right end of the intersection is the minimum of the
23578 the right ends of left and right. */
23579 result->width = (min (left->x + left->width, right->x + right->width)
23580 - result->x);
23581
23582 /* Same game for Y. */
23583 if (r1->y < r2->y)
23584 upper = r1, lower = r2;
23585 else
23586 upper = r2, lower = r1;
23587
23588 /* The upper end of the intersection is lower.y0, if this is inside
23589 of upper. Otherwise, there is no intersection. */
23590 if (lower->y <= upper->y + upper->height)
23591 {
23592 result->y = lower->y;
23593
23594 /* The lower end of the intersection is the minimum of the lower
23595 ends of upper and lower. */
23596 result->height = (min (lower->y + lower->height,
23597 upper->y + upper->height)
23598 - result->y);
23599 intersection_p = 1;
23600 }
23601 }
23602
23603 return intersection_p;
23604 }
23605
23606 #endif /* HAVE_WINDOW_SYSTEM */
23607
23608 \f
23609 /***********************************************************************
23610 Initialization
23611 ***********************************************************************/
23612
23613 void
23614 syms_of_xdisp ()
23615 {
23616 Vwith_echo_area_save_vector = Qnil;
23617 staticpro (&Vwith_echo_area_save_vector);
23618
23619 Vmessage_stack = Qnil;
23620 staticpro (&Vmessage_stack);
23621
23622 Qinhibit_redisplay = intern ("inhibit-redisplay");
23623 staticpro (&Qinhibit_redisplay);
23624
23625 message_dolog_marker1 = Fmake_marker ();
23626 staticpro (&message_dolog_marker1);
23627 message_dolog_marker2 = Fmake_marker ();
23628 staticpro (&message_dolog_marker2);
23629 message_dolog_marker3 = Fmake_marker ();
23630 staticpro (&message_dolog_marker3);
23631
23632 #if GLYPH_DEBUG
23633 defsubr (&Sdump_frame_glyph_matrix);
23634 defsubr (&Sdump_glyph_matrix);
23635 defsubr (&Sdump_glyph_row);
23636 defsubr (&Sdump_tool_bar_row);
23637 defsubr (&Strace_redisplay);
23638 defsubr (&Strace_to_stderr);
23639 #endif
23640 #ifdef HAVE_WINDOW_SYSTEM
23641 defsubr (&Stool_bar_lines_needed);
23642 defsubr (&Slookup_image_map);
23643 #endif
23644 defsubr (&Sformat_mode_line);
23645
23646 staticpro (&Qmenu_bar_update_hook);
23647 Qmenu_bar_update_hook = intern ("menu-bar-update-hook");
23648
23649 staticpro (&Qoverriding_terminal_local_map);
23650 Qoverriding_terminal_local_map = intern ("overriding-terminal-local-map");
23651
23652 staticpro (&Qoverriding_local_map);
23653 Qoverriding_local_map = intern ("overriding-local-map");
23654
23655 staticpro (&Qwindow_scroll_functions);
23656 Qwindow_scroll_functions = intern ("window-scroll-functions");
23657
23658 staticpro (&Qredisplay_end_trigger_functions);
23659 Qredisplay_end_trigger_functions = intern ("redisplay-end-trigger-functions");
23660
23661 staticpro (&Qinhibit_point_motion_hooks);
23662 Qinhibit_point_motion_hooks = intern ("inhibit-point-motion-hooks");
23663
23664 QCdata = intern (":data");
23665 staticpro (&QCdata);
23666 Qdisplay = intern ("display");
23667 staticpro (&Qdisplay);
23668 Qspace_width = intern ("space-width");
23669 staticpro (&Qspace_width);
23670 Qraise = intern ("raise");
23671 staticpro (&Qraise);
23672 Qslice = intern ("slice");
23673 staticpro (&Qslice);
23674 Qspace = intern ("space");
23675 staticpro (&Qspace);
23676 Qmargin = intern ("margin");
23677 staticpro (&Qmargin);
23678 Qpointer = intern ("pointer");
23679 staticpro (&Qpointer);
23680 Qleft_margin = intern ("left-margin");
23681 staticpro (&Qleft_margin);
23682 Qright_margin = intern ("right-margin");
23683 staticpro (&Qright_margin);
23684 Qcenter = intern ("center");
23685 staticpro (&Qcenter);
23686 Qline_height = intern ("line-height");
23687 staticpro (&Qline_height);
23688 QCalign_to = intern (":align-to");
23689 staticpro (&QCalign_to);
23690 QCrelative_width = intern (":relative-width");
23691 staticpro (&QCrelative_width);
23692 QCrelative_height = intern (":relative-height");
23693 staticpro (&QCrelative_height);
23694 QCeval = intern (":eval");
23695 staticpro (&QCeval);
23696 QCpropertize = intern (":propertize");
23697 staticpro (&QCpropertize);
23698 QCfile = intern (":file");
23699 staticpro (&QCfile);
23700 Qfontified = intern ("fontified");
23701 staticpro (&Qfontified);
23702 Qfontification_functions = intern ("fontification-functions");
23703 staticpro (&Qfontification_functions);
23704 Qtrailing_whitespace = intern ("trailing-whitespace");
23705 staticpro (&Qtrailing_whitespace);
23706 Qescape_glyph = intern ("escape-glyph");
23707 staticpro (&Qescape_glyph);
23708 Qnobreak_space = intern ("nobreak-space");
23709 staticpro (&Qnobreak_space);
23710 Qimage = intern ("image");
23711 staticpro (&Qimage);
23712 QCmap = intern (":map");
23713 staticpro (&QCmap);
23714 QCpointer = intern (":pointer");
23715 staticpro (&QCpointer);
23716 Qrect = intern ("rect");
23717 staticpro (&Qrect);
23718 Qcircle = intern ("circle");
23719 staticpro (&Qcircle);
23720 Qpoly = intern ("poly");
23721 staticpro (&Qpoly);
23722 Qmessage_truncate_lines = intern ("message-truncate-lines");
23723 staticpro (&Qmessage_truncate_lines);
23724 Qgrow_only = intern ("grow-only");
23725 staticpro (&Qgrow_only);
23726 Qinhibit_menubar_update = intern ("inhibit-menubar-update");
23727 staticpro (&Qinhibit_menubar_update);
23728 Qinhibit_eval_during_redisplay = intern ("inhibit-eval-during-redisplay");
23729 staticpro (&Qinhibit_eval_during_redisplay);
23730 Qposition = intern ("position");
23731 staticpro (&Qposition);
23732 Qbuffer_position = intern ("buffer-position");
23733 staticpro (&Qbuffer_position);
23734 Qobject = intern ("object");
23735 staticpro (&Qobject);
23736 Qbar = intern ("bar");
23737 staticpro (&Qbar);
23738 Qhbar = intern ("hbar");
23739 staticpro (&Qhbar);
23740 Qbox = intern ("box");
23741 staticpro (&Qbox);
23742 Qhollow = intern ("hollow");
23743 staticpro (&Qhollow);
23744 Qhand = intern ("hand");
23745 staticpro (&Qhand);
23746 Qarrow = intern ("arrow");
23747 staticpro (&Qarrow);
23748 Qtext = intern ("text");
23749 staticpro (&Qtext);
23750 Qrisky_local_variable = intern ("risky-local-variable");
23751 staticpro (&Qrisky_local_variable);
23752 Qinhibit_free_realized_faces = intern ("inhibit-free-realized-faces");
23753 staticpro (&Qinhibit_free_realized_faces);
23754
23755 list_of_error = Fcons (Fcons (intern ("error"),
23756 Fcons (intern ("void-variable"), Qnil)),
23757 Qnil);
23758 staticpro (&list_of_error);
23759
23760 Qlast_arrow_position = intern ("last-arrow-position");
23761 staticpro (&Qlast_arrow_position);
23762 Qlast_arrow_string = intern ("last-arrow-string");
23763 staticpro (&Qlast_arrow_string);
23764
23765 Qoverlay_arrow_string = intern ("overlay-arrow-string");
23766 staticpro (&Qoverlay_arrow_string);
23767 Qoverlay_arrow_bitmap = intern ("overlay-arrow-bitmap");
23768 staticpro (&Qoverlay_arrow_bitmap);
23769
23770 echo_buffer[0] = echo_buffer[1] = Qnil;
23771 staticpro (&echo_buffer[0]);
23772 staticpro (&echo_buffer[1]);
23773
23774 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
23775 staticpro (&echo_area_buffer[0]);
23776 staticpro (&echo_area_buffer[1]);
23777
23778 Vmessages_buffer_name = build_string ("*Messages*");
23779 staticpro (&Vmessages_buffer_name);
23780
23781 mode_line_proptrans_alist = Qnil;
23782 staticpro (&mode_line_proptrans_alist);
23783 mode_line_string_list = Qnil;
23784 staticpro (&mode_line_string_list);
23785 mode_line_string_face = Qnil;
23786 staticpro (&mode_line_string_face);
23787 mode_line_string_face_prop = Qnil;
23788 staticpro (&mode_line_string_face_prop);
23789 Vmode_line_unwind_vector = Qnil;
23790 staticpro (&Vmode_line_unwind_vector);
23791
23792 help_echo_string = Qnil;
23793 staticpro (&help_echo_string);
23794 help_echo_object = Qnil;
23795 staticpro (&help_echo_object);
23796 help_echo_window = Qnil;
23797 staticpro (&help_echo_window);
23798 previous_help_echo_string = Qnil;
23799 staticpro (&previous_help_echo_string);
23800 help_echo_pos = -1;
23801
23802 #ifdef HAVE_WINDOW_SYSTEM
23803 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
23804 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
23805 For example, if a block cursor is over a tab, it will be drawn as
23806 wide as that tab on the display. */);
23807 x_stretch_cursor_p = 0;
23808 #endif
23809
23810 DEFVAR_LISP ("show-trailing-whitespace", &Vshow_trailing_whitespace,
23811 doc: /* *Non-nil means highlight trailing whitespace.
23812 The face used for trailing whitespace is `trailing-whitespace'. */);
23813 Vshow_trailing_whitespace = Qnil;
23814
23815 DEFVAR_LISP ("nobreak-char-display", &Vnobreak_char_display,
23816 doc: /* *Control highlighting of nobreak space and soft hyphen.
23817 A value of t means highlight the character itself (for nobreak space,
23818 use face `nobreak-space').
23819 A value of nil means no highlighting.
23820 Other values mean display the escape glyph followed by an ordinary
23821 space or ordinary hyphen. */);
23822 Vnobreak_char_display = Qt;
23823
23824 DEFVAR_LISP ("void-text-area-pointer", &Vvoid_text_area_pointer,
23825 doc: /* *The pointer shape to show in void text areas.
23826 A value of nil means to show the text pointer. Other options are `arrow',
23827 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
23828 Vvoid_text_area_pointer = Qarrow;
23829
23830 DEFVAR_LISP ("inhibit-redisplay", &Vinhibit_redisplay,
23831 doc: /* Non-nil means don't actually do any redisplay.
23832 This is used for internal purposes. */);
23833 Vinhibit_redisplay = Qnil;
23834
23835 DEFVAR_LISP ("global-mode-string", &Vglobal_mode_string,
23836 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
23837 Vglobal_mode_string = Qnil;
23838
23839 DEFVAR_LISP ("overlay-arrow-position", &Voverlay_arrow_position,
23840 doc: /* Marker for where to display an arrow on top of the buffer text.
23841 This must be the beginning of a line in order to work.
23842 See also `overlay-arrow-string'. */);
23843 Voverlay_arrow_position = Qnil;
23844
23845 DEFVAR_LISP ("overlay-arrow-string", &Voverlay_arrow_string,
23846 doc: /* String to display as an arrow in non-window frames.
23847 See also `overlay-arrow-position'. */);
23848 Voverlay_arrow_string = build_string ("=>");
23849
23850 DEFVAR_LISP ("overlay-arrow-variable-list", &Voverlay_arrow_variable_list,
23851 doc: /* List of variables (symbols) which hold markers for overlay arrows.
23852 The symbols on this list are examined during redisplay to determine
23853 where to display overlay arrows. */);
23854 Voverlay_arrow_variable_list
23855 = Fcons (intern ("overlay-arrow-position"), Qnil);
23856
23857 DEFVAR_INT ("scroll-step", &scroll_step,
23858 doc: /* *The number of lines to try scrolling a window by when point moves out.
23859 If that fails to bring point back on frame, point is centered instead.
23860 If this is zero, point is always centered after it moves off frame.
23861 If you want scrolling to always be a line at a time, you should set
23862 `scroll-conservatively' to a large value rather than set this to 1. */);
23863
23864 DEFVAR_INT ("scroll-conservatively", &scroll_conservatively,
23865 doc: /* *Scroll up to this many lines, to bring point back on screen.
23866 A value of zero means to scroll the text to center point vertically
23867 in the window. */);
23868 scroll_conservatively = 0;
23869
23870 DEFVAR_INT ("scroll-margin", &scroll_margin,
23871 doc: /* *Number of lines of margin at the top and bottom of a window.
23872 Recenter the window whenever point gets within this many lines
23873 of the top or bottom of the window. */);
23874 scroll_margin = 0;
23875
23876 DEFVAR_LISP ("display-pixels-per-inch", &Vdisplay_pixels_per_inch,
23877 doc: /* Pixels per inch value for non-window system displays.
23878 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
23879 Vdisplay_pixels_per_inch = make_float (72.0);
23880
23881 #if GLYPH_DEBUG
23882 DEFVAR_INT ("debug-end-pos", &debug_end_pos, doc: /* Don't ask. */);
23883 #endif
23884
23885 DEFVAR_BOOL ("truncate-partial-width-windows",
23886 &truncate_partial_width_windows,
23887 doc: /* *Non-nil means truncate lines in all windows less than full frame wide. */);
23888 truncate_partial_width_windows = 1;
23889
23890 DEFVAR_BOOL ("mode-line-inverse-video", &mode_line_inverse_video,
23891 doc: /* nil means display the mode-line/header-line/menu-bar in the default face.
23892 Any other value means to use the appropriate face, `mode-line',
23893 `header-line', or `menu' respectively. */);
23894 mode_line_inverse_video = 1;
23895
23896 DEFVAR_LISP ("line-number-display-limit", &Vline_number_display_limit,
23897 doc: /* *Maximum buffer size for which line number should be displayed.
23898 If the buffer is bigger than this, the line number does not appear
23899 in the mode line. A value of nil means no limit. */);
23900 Vline_number_display_limit = Qnil;
23901
23902 DEFVAR_INT ("line-number-display-limit-width",
23903 &line_number_display_limit_width,
23904 doc: /* *Maximum line width (in characters) for line number display.
23905 If the average length of the lines near point is bigger than this, then the
23906 line number may be omitted from the mode line. */);
23907 line_number_display_limit_width = 200;
23908
23909 DEFVAR_BOOL ("highlight-nonselected-windows", &highlight_nonselected_windows,
23910 doc: /* *Non-nil means highlight region even in nonselected windows. */);
23911 highlight_nonselected_windows = 0;
23912
23913 DEFVAR_BOOL ("multiple-frames", &multiple_frames,
23914 doc: /* Non-nil if more than one frame is visible on this display.
23915 Minibuffer-only frames don't count, but iconified frames do.
23916 This variable is not guaranteed to be accurate except while processing
23917 `frame-title-format' and `icon-title-format'. */);
23918
23919 DEFVAR_LISP ("frame-title-format", &Vframe_title_format,
23920 doc: /* Template for displaying the title bar of visible frames.
23921 \(Assuming the window manager supports this feature.)
23922 This variable has the same structure as `mode-line-format' (which see),
23923 and is used only on frames for which no explicit name has been set
23924 \(see `modify-frame-parameters'). */);
23925
23926 DEFVAR_LISP ("icon-title-format", &Vicon_title_format,
23927 doc: /* Template for displaying the title bar of an iconified frame.
23928 \(Assuming the window manager supports this feature.)
23929 This variable has the same structure as `mode-line-format' (which see),
23930 and is used only on frames for which no explicit name has been set
23931 \(see `modify-frame-parameters'). */);
23932 Vicon_title_format
23933 = Vframe_title_format
23934 = Fcons (intern ("multiple-frames"),
23935 Fcons (build_string ("%b"),
23936 Fcons (Fcons (empty_string,
23937 Fcons (intern ("invocation-name"),
23938 Fcons (build_string ("@"),
23939 Fcons (intern ("system-name"),
23940 Qnil)))),
23941 Qnil)));
23942
23943 DEFVAR_LISP ("message-log-max", &Vmessage_log_max,
23944 doc: /* Maximum number of lines to keep in the message log buffer.
23945 If nil, disable message logging. If t, log messages but don't truncate
23946 the buffer when it becomes large. */);
23947 Vmessage_log_max = make_number (50);
23948
23949 DEFVAR_LISP ("window-size-change-functions", &Vwindow_size_change_functions,
23950 doc: /* Functions called before redisplay, if window sizes have changed.
23951 The value should be a list of functions that take one argument.
23952 Just before redisplay, for each frame, if any of its windows have changed
23953 size since the last redisplay, or have been split or deleted,
23954 all the functions in the list are called, with the frame as argument. */);
23955 Vwindow_size_change_functions = Qnil;
23956
23957 DEFVAR_LISP ("window-scroll-functions", &Vwindow_scroll_functions,
23958 doc: /* List of functions to call before redisplaying a window with scrolling.
23959 Each function is called with two arguments, the window
23960 and its new display-start position. Note that the value of `window-end'
23961 is not valid when these functions are called. */);
23962 Vwindow_scroll_functions = Qnil;
23963
23964 DEFVAR_LISP ("redisplay-end-trigger-functions", &Vredisplay_end_trigger_functions,
23965 doc: /* Functions called when redisplay of a window reaches the end trigger.
23966 Each function is called with two arguments, the window and the end trigger value.
23967 See `set-window-redisplay-end-trigger'. */);
23968 Vredisplay_end_trigger_functions = Qnil;
23969
23970 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
23971 doc: /* *Non-nil means autoselect window with mouse pointer. */);
23972 mouse_autoselect_window = 0;
23973
23974 DEFVAR_BOOL ("auto-resize-tool-bars", &auto_resize_tool_bars_p,
23975 doc: /* *Non-nil means automatically resize tool-bars.
23976 This increases a tool-bar's height if not all tool-bar items are visible.
23977 It decreases a tool-bar's height when it would display blank lines
23978 otherwise. */);
23979 auto_resize_tool_bars_p = 1;
23980
23981 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", &auto_raise_tool_bar_buttons_p,
23982 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
23983 auto_raise_tool_bar_buttons_p = 1;
23984
23985 DEFVAR_BOOL ("make-cursor-line-fully-visible", &make_cursor_line_fully_visible_p,
23986 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
23987 make_cursor_line_fully_visible_p = 1;
23988
23989 DEFVAR_LISP ("tool-bar-border", &Vtool_bar_border,
23990 doc: /* *Border below tool-bar in pixels.
23991 If an integer, use it as the height of the border.
23992 If it is one of `internal-border-width' or `border-width', use the
23993 value of the corresponding frame parameter.
23994 Otherwise, no border is added below the tool-bar. */);
23995 Vtool_bar_border = Qinternal_border_width;
23996
23997 DEFVAR_LISP ("tool-bar-button-margin", &Vtool_bar_button_margin,
23998 doc: /* *Margin around tool-bar buttons in pixels.
23999 If an integer, use that for both horizontal and vertical margins.
24000 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
24001 HORZ specifying the horizontal margin, and VERT specifying the
24002 vertical margin. */);
24003 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
24004
24005 DEFVAR_INT ("tool-bar-button-relief", &tool_bar_button_relief,
24006 doc: /* *Relief thickness of tool-bar buttons. */);
24007 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
24008
24009 DEFVAR_LISP ("fontification-functions", &Vfontification_functions,
24010 doc: /* List of functions to call to fontify regions of text.
24011 Each function is called with one argument POS. Functions must
24012 fontify a region starting at POS in the current buffer, and give
24013 fontified regions the property `fontified'. */);
24014 Vfontification_functions = Qnil;
24015 Fmake_variable_buffer_local (Qfontification_functions);
24016
24017 DEFVAR_BOOL ("unibyte-display-via-language-environment",
24018 &unibyte_display_via_language_environment,
24019 doc: /* *Non-nil means display unibyte text according to language environment.
24020 Specifically this means that unibyte non-ASCII characters
24021 are displayed by converting them to the equivalent multibyte characters
24022 according to the current language environment. As a result, they are
24023 displayed according to the current fontset. */);
24024 unibyte_display_via_language_environment = 0;
24025
24026 DEFVAR_LISP ("max-mini-window-height", &Vmax_mini_window_height,
24027 doc: /* *Maximum height for resizing mini-windows.
24028 If a float, it specifies a fraction of the mini-window frame's height.
24029 If an integer, it specifies a number of lines. */);
24030 Vmax_mini_window_height = make_float (0.25);
24031
24032 DEFVAR_LISP ("resize-mini-windows", &Vresize_mini_windows,
24033 doc: /* *How to resize mini-windows.
24034 A value of nil means don't automatically resize mini-windows.
24035 A value of t means resize them to fit the text displayed in them.
24036 A value of `grow-only', the default, means let mini-windows grow
24037 only, until their display becomes empty, at which point the windows
24038 go back to their normal size. */);
24039 Vresize_mini_windows = Qgrow_only;
24040
24041 DEFVAR_LISP ("blink-cursor-alist", &Vblink_cursor_alist,
24042 doc: /* Alist specifying how to blink the cursor off.
24043 Each element has the form (ON-STATE . OFF-STATE). Whenever the
24044 `cursor-type' frame-parameter or variable equals ON-STATE,
24045 comparing using `equal', Emacs uses OFF-STATE to specify
24046 how to blink it off. ON-STATE and OFF-STATE are values for
24047 the `cursor-type' frame parameter.
24048
24049 If a frame's ON-STATE has no entry in this list,
24050 the frame's other specifications determine how to blink the cursor off. */);
24051 Vblink_cursor_alist = Qnil;
24052
24053 DEFVAR_BOOL ("auto-hscroll-mode", &automatic_hscrolling_p,
24054 doc: /* *Non-nil means scroll the display automatically to make point visible. */);
24055 automatic_hscrolling_p = 1;
24056
24057 DEFVAR_INT ("hscroll-margin", &hscroll_margin,
24058 doc: /* *How many columns away from the window edge point is allowed to get
24059 before automatic hscrolling will horizontally scroll the window. */);
24060 hscroll_margin = 5;
24061
24062 DEFVAR_LISP ("hscroll-step", &Vhscroll_step,
24063 doc: /* *How many columns to scroll the window when point gets too close to the edge.
24064 When point is less than `hscroll-margin' columns from the window
24065 edge, automatic hscrolling will scroll the window by the amount of columns
24066 determined by this variable. If its value is a positive integer, scroll that
24067 many columns. If it's a positive floating-point number, it specifies the
24068 fraction of the window's width to scroll. If it's nil or zero, point will be
24069 centered horizontally after the scroll. Any other value, including negative
24070 numbers, are treated as if the value were zero.
24071
24072 Automatic hscrolling always moves point outside the scroll margin, so if
24073 point was more than scroll step columns inside the margin, the window will
24074 scroll more than the value given by the scroll step.
24075
24076 Note that the lower bound for automatic hscrolling specified by `scroll-left'
24077 and `scroll-right' overrides this variable's effect. */);
24078 Vhscroll_step = make_number (0);
24079
24080 DEFVAR_BOOL ("message-truncate-lines", &message_truncate_lines,
24081 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
24082 Bind this around calls to `message' to let it take effect. */);
24083 message_truncate_lines = 0;
24084
24085 DEFVAR_LISP ("menu-bar-update-hook", &Vmenu_bar_update_hook,
24086 doc: /* Normal hook run to update the menu bar definitions.
24087 Redisplay runs this hook before it redisplays the menu bar.
24088 This is used to update submenus such as Buffers,
24089 whose contents depend on various data. */);
24090 Vmenu_bar_update_hook = Qnil;
24091
24092 DEFVAR_BOOL ("inhibit-menubar-update", &inhibit_menubar_update,
24093 doc: /* Non-nil means don't update menu bars. Internal use only. */);
24094 inhibit_menubar_update = 0;
24095
24096 DEFVAR_BOOL ("inhibit-eval-during-redisplay", &inhibit_eval_during_redisplay,
24097 doc: /* Non-nil means don't eval Lisp during redisplay. */);
24098 inhibit_eval_during_redisplay = 0;
24099
24100 DEFVAR_BOOL ("inhibit-free-realized-faces", &inhibit_free_realized_faces,
24101 doc: /* Non-nil means don't free realized faces. Internal use only. */);
24102 inhibit_free_realized_faces = 0;
24103
24104 #if GLYPH_DEBUG
24105 DEFVAR_BOOL ("inhibit-try-window-id", &inhibit_try_window_id,
24106 doc: /* Inhibit try_window_id display optimization. */);
24107 inhibit_try_window_id = 0;
24108
24109 DEFVAR_BOOL ("inhibit-try-window-reusing", &inhibit_try_window_reusing,
24110 doc: /* Inhibit try_window_reusing display optimization. */);
24111 inhibit_try_window_reusing = 0;
24112
24113 DEFVAR_BOOL ("inhibit-try-cursor-movement", &inhibit_try_cursor_movement,
24114 doc: /* Inhibit try_cursor_movement display optimization. */);
24115 inhibit_try_cursor_movement = 0;
24116 #endif /* GLYPH_DEBUG */
24117
24118 DEFVAR_INT ("overline-margin", &overline_margin,
24119 doc: /* *Space between overline and text, in pixels.
24120 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
24121 margin to the caracter height. */);
24122 overline_margin = 2;
24123 }
24124
24125
24126 /* Initialize this module when Emacs starts. */
24127
24128 void
24129 init_xdisp ()
24130 {
24131 Lisp_Object root_window;
24132 struct window *mini_w;
24133
24134 current_header_line_height = current_mode_line_height = -1;
24135
24136 CHARPOS (this_line_start_pos) = 0;
24137
24138 mini_w = XWINDOW (minibuf_window);
24139 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
24140
24141 if (!noninteractive)
24142 {
24143 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
24144 int i;
24145
24146 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
24147 set_window_height (root_window,
24148 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
24149 0);
24150 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
24151 set_window_height (minibuf_window, 1, 0);
24152
24153 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
24154 mini_w->total_cols = make_number (FRAME_COLS (f));
24155
24156 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
24157 scratch_glyph_row.glyphs[TEXT_AREA + 1]
24158 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
24159
24160 /* The default ellipsis glyphs `...'. */
24161 for (i = 0; i < 3; ++i)
24162 default_invis_vector[i] = make_number ('.');
24163 }
24164
24165 {
24166 /* Allocate the buffer for frame titles.
24167 Also used for `format-mode-line'. */
24168 int size = 100;
24169 mode_line_noprop_buf = (char *) xmalloc (size);
24170 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
24171 mode_line_noprop_ptr = mode_line_noprop_buf;
24172 mode_line_target = MODE_LINE_DISPLAY;
24173 }
24174
24175 help_echo_showing_p = 0;
24176 }
24177
24178
24179 /* arch-tag: eacc864d-bb6a-4b74-894a-1a4399a1358b
24180 (do not change this comment) */